Blog
Jamming with XMLRPC,JSON,BYTEARRAYS and ENCRYPTION
2 years, 4 months ago Posted in: Blog Comments Off

[php]

// include server library
include('rpc/class-IXR.php');
include('rpc/JSON.php');

/**
* Server Implementation
* @author mmonti
*
*/
class RPCServer extends IXR_Server {

// constructor
function RPCServer() {
$this->methods = array(
‘pushError’ => ‘this:pushError’,
‘bypass’ => ‘this:bypass’,
‘pushStruct’ => ‘this:pushStruct’
);

// start server
$this->IXR_Server($this->methods);
}

/**
* Push an error back to client
*
* @param $args
* @return unknown_type
*/
function pushError($args) {
return(new IXR_Error(404, __(“Sorry, no such page.”)));
}

/**
* The decode function
* @param $args
* @return unknown_type
*/
function bypass($args) {

// decode the base 64
$mydata = base64_decode($args);

// decompress byearray
$uncompressedPostedData = gzuncompress($mydata);

// fix glitches
$open = stripos($uncompressedPostedData, “{“);
$uncompressedPostedData = substr($uncompressedPostedData, $open);

// print log
//$this->logIO($uncompressedPostedData);

// instance the json decoder
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);

// decode json message
$tmopbj = $json->decode($uncompressedPostedData);

$this->logIO(print_r($tmopbj,true));

//$this->logIO($json->encode($tmopbj));

// Jsonize object
$out = $json->encode($tmopbj);

// compress it
$out = gzcompress($out, 9);

// encode to base 64
$out = base64_encode($out);

return($out);
}

/**
* convert a struct to object
*
* @param $data
* @return unknown_type
*/
function array2object($data) {
if(!is_array($data)) return $data;

$object = new stdClass();
if (is_array($data) && count($data) > 0) {
foreach ($data as $name=>$value) {
$name = strtolower(trim($name));
if (!empty($name)) {
$object->$name = array2object($value);
}
}
}
return $object;
}

/**
* convert an object to struct
* @param $data
* @return unknown_type
*/
function object2array($data){
if(!is_object($data) && !is_array($data)) return $data;

if(is_object($data)) $data = get_object_vars($data);

return array_map(‘object2array’, $data);
}

/**
* Sample method to send a struct back
*
* @return unknown_type
*/
function pushStruct() {
$page_struct = array(
“dateValue” => new IXR_Date(2009-12-25),
“stringValue” => “userid”,
“numberValue” => 1
);

return($page_struct);
}

/**
* Internal logging to file
* @param $io
* @param $msg
* @return unknown_type
*/
function logIO($msg) {
$fp = fopen(dirname(__FILE__).”xmlrpc.log”,”w”);
$date = gmdate(“Y-m-d H:i:s “);
fwrite($fp, “\n\n”.$date.$msg);
fclose($fp);
return true;
}

}

// instance server on page
$RPCServer = new RPCServer();

?>
[/php]

Comments are closed.