Friday, September 27, 2013

codeigniter url rewrite index.php redirect to base url

.htaccess

RewriteEngine On

# Keep these lines even in maintenance mode, to have an access to the website
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(application|modules|plugins|system|themes) index.php/$1 [L]

call this php in codeigniter.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class HO_INC {

function __construct()
{
$this->config =& load_class('Config', 'core');
log_message('debug', "CORE Class Initialized");
}

public function index_php_to_base_url($method = 'refresh')
{
if ( ! isset($_SERVER['REQUEST_URI']) OR ! isset($_SERVER['SCRIPT_NAME']))
{
return;
}

$uri = $_SERVER['REQUEST_URI'];
if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
{
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));

            if ( ! preg_match('#^https?://#i', $uri))
            {
                $uri = $this->config->site_url($uri);
            }

            switch($method)
            {
                case 'refresh' : header("Refresh:0;url=".$uri);
                    break;
                default : header("Location: ".$uri, TRUE, 200);
                    break;
            }
            exit;
        }
    }
}

php codeigniter xml document write example with database select sample

XmlDocument.php

<?php

class XmlDocument extends XMLWriter {

    public function __construct($prm_rootElementName, $prm_xsltFilePath = '') {
        $this->openMemory();
        $this->setIndent(true);
        $this->setIndentString(' ');
        $this->startDocument('1.0', 'UTF-8');

        if ($prm_xsltFilePath) {
            $this->writePi('xml-stylesheet', 'type="text/xsl" href="' . $prm_xsltFilePath . '"');
        }

        $this->startElement($prm_rootElementName);
    }

    public function setElement($prm_elementName, $prm_ElementText) {
        $this->startElement($prm_elementName);
        $this->text($prm_ElementText);
        $this->endElement();
    }

    public function fromArray($prm_array) {
        if (is_array($prm_array)) {
            foreach ($prm_array as $index => $element) {
                if (is_array($element)) {
                    $this->startElement($index);
                    $this->fromArray($element);
                    $this->endElement();
                }
                else
                    $this->setElement($index, $element);
            }
        }
    }

    public function getDocument() {
        $this->endElement();
        $this->endDocument();
        return $this->outputMemory();
    }

    public function output() {
        header('Content-type: text/xml');
        echo $this->getDocument();
    }

}

?>


usage 

require APPPATH . 'libraries/XmlDocument.php';
        $contents = array(
            'title' => 'title boroo',
            'description' => 'Simple XHTML document from XML+XSLT files!',
            'attrs' => array(
                "types" => array(
                    "type1" => "type 1",
                    "type2" => "type 2"
                ),
                "size" => "1024kb"
            ),
        );

        $this->load->database();
        $this->db->select('*');
        $this->db->from('table1');
        $query = $this->db->get();
        $result = $query->result_array();
        if ($query->num_rows() > 0) {
            foreach ($result as $row) {
                list($id, $name, $descr, $created) = array_values($row);
                echo "$id, $created<br>";
            }
        }

        $xml = new XmlDocument('root');
        $xml->fromArray($result);
        $xml->output();

php date_helper.php return utc and local datetime string for db

<?php

if (!function_exists('utc_datetime')) {

    function utc_datetime($datetime = '', $format = 'Y-m-d H:i:s') {
        $time = time();
        if ($datetime !== '') {
            $time = strtotime($datetime);
        }

        $date = new DateTime();
        $date->setTimezone(new DateTimeZone('UTC'));
        $date->setTimestamp($time);
        return $date->format($format);
    }

}

if (!function_exists('local_datetime')) {

    function local_datetime($datetime = '', $timezone = 'Asia/Ulaanbaatar', $format = 'Y-m-d H:i:s') {
        $time = time();
        if ($datetime !== '') {
            $time = strtotime($datetime);
        }

        $date = new DateTime();
        $date->setTimezone(new DateTimeZone($timezone));
        $date->setTimestamp($time);
        return $date->format($format);
    }

}
?>

USAGE

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class boroo extends BaseController {

    function __construct() {
        parent::__construct();
    }

    public function hello() {
        global $RTR;

        $expire = (7 * 24 * 60 * 60);
        $this->input->set_cookie("time", time(), $expire);
        $time = $this->input->cookie('time');
        $timezone = $this->getZone();
        $format = 'Y-m-d H:i:s';

        $this->load->helper('date');
        date_default_timezone_set('Asia/Ulaanbaatar');
        $time = strtotime(utc_datetime('2013-09-10 10:30:45'));
        $newtime = mktime(date('H',$time), date('i',$time), date('s',$time));
        $date = date($format, $newtime);
        echo $date;
        exit;
        echo local_datetime('2013-09-10 10:30:45') . "=>" . utc_datetime('2013-09-10 10:30:45');
        exit;
        $data['message'] = 'Date is ' . $dateStr . ', IP is ' . $this->getIp();
        $this->load->view($RTR->fetch_directory() . $RTR->fetch_class() . '/hello', $data);
    }

}

or

$date = new DateTime('2013-09-10 10:30:45');
        $dateStr = date("Y-m-d H:i:s", strtotime('+5 minutes', $date->getTimestamp()));
        echo $dateStr;
        exit;

Thursday, September 26, 2013

codeigniter basecontroller with cookie, time, ip and user

BaseController.php

<?php
require BASEPATH.'core/Controller.php';

include_once 'WebContext.php';

class BaseController extends CI_Controller {
 
    var $webContext;
 
    function __construct() {
        parent::__construct();
     
        date_default_timezone_set('Asia/Ulaanbaatar');
     
        $this->webContext = new WebContext();
    }
 
    function ip() {
        return $this->input->ip_address();
    }
 
    function user() {
        return $this->webContext->getCurrentUser();
    }
}
?>

BorooController.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
interface IInterface0
{
}
interface IInterface1 extends IInterface0
{
}
class boroo extends BaseController implements IInterface1 {

    function __construct() {
        parent::__construct();
        $clss = class_parents($this);
        foreach($clss as $cls)
        {
            if (substr($cls,0,3) == 'CI_')
                if(is_subclass_of($this, 'BaseController') && is_subclass_of($this, 'CI_Controller'))
                    echo $cls.' is yes<br>';
        }
    }
 
    public function hello()
{
        global $RTR;
     
        global $RTR;
       
        $expire = (7 * 24 * 60 * 60);
        $this->input->set_cookie("time", time(), $expire);
        $time = $this->input->cookie('time');
       
        $dstr = date('Y-m-d H:i:s', $time);
        $data['message'] = 'Date is '.$dstr.', IP is '.$this->ip();
$this->load->view($RTR->fetch_directory().$RTR->fetch_class().'/hello',$data);
    }
}

php object clone example with static variable

class SubObject
{
    static $instances = 0;
    public $instance;

    public function __construct() {
        $this->instance = ++self::$instances;
    }

    public function __clone() {
        $this->instance = ++self::$instances;
    }
}

class MyCloneable
{
    public $object1;
    public $object2;

    function __clone()
    {
        // Force a copy of this->object, otherwise
        // it will point to same object.
        $this->object1 = clone $this->object1;
        $this->object2 = clone $this->object2;
    }
}

$obj = new MyCloneable();
$obj->object1 = new SubObject();
$obj->object2 = new SubObject();

$obj2 = clone $obj;

print("Original Object:<br>");
echo $obj->object1->instance."<br>";
echo $obj->object2->instance."<br>";

print("Cloned Object:<br>");
echo $obj2->object1->instance."<br>";
echo $obj2->object2->instance."<br>";

php reflection example with list, get_object_vars key methods

class myclass {

    var $var1; // this has no default value...
    var $var2 = "xyz";
    var $var3 = 100;
    public $var4; // PHP 5

    // constructor
    function myclass() {
        // change some properties
        $this->var1 = "foo";
        $this->var2 = "bar";
        $this->var4 = '10000000';
        return true;
    }
}

$my_class = new myclass();
$my_class->myclass();

$reflect = new ReflectionClass($my_class);
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);

list($var1,$var2)=$props;
$class_vars = get_object_vars($my_class);

echo $class_vars[$var1->getName()];
echo $class_vars[$var2->getName()];
exit;

Friday, September 13, 2013

cannot-install-4-7-4-incompatible-version-of-xamarin-shell-detected

about incompatible version software fixer

Spent couple of hours but got it to work as follows: 1- Download a program called Orca (http://www.softpedia.com/get/Authoring-tools/Setup-creators/Orca.shtml) 2- right click on mono-androidxxx.msi 3- choose edit with orca 4- Go to InstallUISequence 5- Search for the action name "IncompatibleShellDlg" and edit its condition from "INCOMPATIBLE_SHELL=1" to "INCOMPATIBLE_SHELL=0" 6- SAVE 7- Good Luck. Enjoy!