Saturday, September 6, 2014

[On-Going]Enable USB Printer Share on XiaoMi Router

I have a mission, to enable USB Printer share on XiaoMi Router. This sharing document is still on-going, I have not successfully made it yet.

Pre-requisite
1) Have a not too small spaces to work on. Enabling package and kernel need more spaces.
2) kmod-usb-printer
3) modify /etc/opkg.conf, comment out dest and overlay that makes /data become root, replace it as ur rootfs

1) Enable SSH features for XiaoMi Router. Note: It will void warranty.
2) Make sure usb.essential enabled. http://wiki.openwrt.org/doc/howto/usb.essentials
3)

Optional
1) Enable kmod-fs-hfs and kmod-fs-hfsplus to support apple type filesystem

Wednesday, December 18, 2013

JSON Decode for PHP < 5.2.0

Recently I was in a struggle to enable JSON features for PHP version 5.1.6

Let's first explain my working environment :-

  • We (developer) code locally on PHP 5.3.11
  • Development Server is on PHP 5.2.0
  • Production Server is on PHP 5.1.6
I understand this is very un-healthy, and server migration should be on the way.
Before that, I have to make my JSON supported in the Production Server.

Firstly I have to thank Michal Migurski, Matt Knapp and Brett Stimmereman, they made a Service_JSON available in PHP.

Dependencies:
  • PHP 4.3 or newer
  • PEAR Package: PEAR Installer 1.4.0b1 or newer
You can choose to install from PEAR Package, or manually made this available. Please follows the instruction. Grab Service_JSON package here http://pear.php.net/package/Services_JSON/download

if (!function_exists('json_encode')){
 function json_encode($arg)
 {
  global $services_json;
  if (!isset($services_json)) {
   $services_json = new Services_JSON();
  }
  return $services_json->encode($arg);
 }
}

if (!function_exists('json_decode')){
function json_decode($arg, $assoc = false)
 {
  global $services_json;
  if (!isset($services_json)) {
   $services_json = new Services_JSON();
  }
  
  $json = $services_json->decode($arg);   
  return $assoc ? Object2Array($json) : $json;
 }
}

function Object2Array($object){
 if (is_object($object)){
  // Convert to array using get_object_vars
  $object = get_object_vars($object);
 }
 
 if (is_array($object)){
  // Return array converted to object for a recursive call
  return array_map(__FUNCTION__, $object);
 } else {
  // Return the array
  return $object;
 }
}

Disclaimer: I've found some functions on the net, modify and made them work together.

I use function exists function to make sure code changes when you upgrade to later version of PHP is minimum.

Object2Array function is used to make json_decode works like PHP >= 5.2.0, if second arguments is used, json_decode shall return a PHP array.

Usage:
$array = array();
$array['0'] = array( 'orange' => 'orange', 'grapes' => array( 'green', 'purple'));
$array['1'] = array( 'banana' => 'yellow', 'pear' => array( 'green anjou' => 'green', 'red anjou' => 'red'));

echo "JSON:\n";
$json = json_encode($array);
print_r($json);

echo "ARRAY:\n";
print_r(json_decode($json, true));

Expected Result:
JSON:
[{"orange":"orange","grapes":["green","purple"]},{"banana":"yellow","pear":{"green anjou":"green","red anjou":"red"}}]

JSON Object:
Array
(
    [0] => stdClass Object
        (
            [orange] => orange
            [grapes] => Array
                (
                    [0] => green
                    [1] => purple
                )

        )

    [1] => stdClass Object
        (
            [banana] => yellow
            [pear] => stdClass Object
                (
                    [green anjou] => green
                    [red anjou] => red
                )

        )

)


ARRAY:
Array
(
    [0] => Array
        (
            [orange] => orange
            [grapes] => Array
                (
                    [0] => green
                    [1] => purple
                )

        )

    [1] => Array
        (
            [banana] => yellow
            [pear] => Array
                (
                    [green anjou] => green
                    [red anjou] => red
                )

        )

)