Example #1
0
 /**
  * Returns json representation of the response.
  * @param string $charset_encoding the charset to be used for serialization. if null, US-ASCII is assumed
  * @return string the json representation of the response
  * @access public
  */
 function serialize($charset_encoding = '')
 {
     if ($charset_encoding != '') {
         $this->content_type = 'application/json; charset=' . $charset_encoding;
     } else {
         $this->content_type = 'application/json';
     }
     $this->payload = serialize_jsonrpcresp($this, $this->id, $charset_encoding);
     return $this->payload;
 }
 /**
  * Note: syntax differs from overridden method, by adding an ID param
  * @access private
  */
 function execute($m, $params = null, $paramtypes = null, $msgID = null)
 {
     if (is_object($m)) {
         // watch out: if $m is an xmlrpcmsg obj, this will raise a warning: no id memeber...
         $methName = $m->method();
         $msgID = $m->id;
     } else {
         $methName = $m;
     }
     $sysCall = $this->allow_system_funcs && ereg("^system\\.", $methName);
     $dmap = $sysCall ? $GLOBALS['_xmlrpcs_dmap'] : $this->dmap;
     if (!isset($dmap[$methName]['function'])) {
         // No such method
         return new jsonrpcresp(0, $GLOBALS['xmlrpcerr']['unknown_method'], $GLOBALS['xmlrpcstr']['unknown_method']);
     }
     // Check signature
     if (isset($dmap[$methName]['signature'])) {
         $sig = $dmap[$methName]['signature'];
         if (is_object($m)) {
             list($ok, $errstr) = $this->verifySignature($m, $sig);
         } else {
             list($ok, $errstr) = $this->verifySignature($paramtypes, $sig);
         }
         if (!$ok) {
             // Didn't match.
             return new jsonrpcresp(0, $GLOBALS['xmlrpcerr']['incorrect_params'], $GLOBALS['xmlrpcstr']['incorrect_params'] . ": {$errstr}");
         }
     }
     $func = $dmap[$methName]['function'];
     // let the 'class::function' syntax be accepted in dispatch maps
     if (is_string($func) && strpos($func, '::')) {
         $func = explode('::', $func);
     }
     // verify that function to be invoked is in fact callable
     if (!is_callable($func)) {
         error_log("XML-RPC: jsonrpc_server::execute: function {$func} registered as method handler is not callable");
         return new jsonrpcresp(0, $GLOBALS['xmlrpcerr']['server_error'], $GLOBALS['xmlrpcstr']['server_error'] . ": no function matches method");
     }
     // If debug level is 3, we should catch all errors generated during
     // processing of user function, and log them as part of response
     if ($this->debug > 2) {
         $GLOBALS['_xmlrpcs_prev_ehandler'] = set_error_handler('_xmlrpcs_errorHandler');
     }
     if (is_object($m)) {
         if ($sysCall) {
             $r = call_user_func($func, $this, $m);
         } else {
             $r = call_user_func($func, $m);
         }
         if (!is_a($r, 'xmlrpcresp')) {
             error_log("XML-RPC: jsonrpc_server::execute: function {$func} registered as method handler does not return an xmlrpcresp object");
             if (is_a($r, 'xmlrpcval')) {
                 $r = new jsonrpcresp($r);
             } else {
                 $r = new jsonrpcresp(0, $GLOBALS['xmlrpcerr']['server_error'], $GLOBALS['xmlrpcstr']['server_error'] . ": function does not return jsonrpcresp or xmlrpcresp object");
             }
         }
     } else {
         // call a 'plain php' function
         if ($sysCall) {
             array_unshift($params, $this);
             $r = call_user_func_array($func, $params);
         } else {
             // 3rd API convention for method-handling functions: EPI-style
             if ($this->functions_parameters_type == 'epivals') {
                 $r = call_user_func_array($func, array($methName, $params, $this->user_data));
                 // mimic EPI behaviour: if we get an array that looks like an error, make it
                 // an eror response
                 if (is_array($r) && array_key_exists('faultCode', $r) && array_key_exists('faultString', $r)) {
                     $r =& new jsonrpcresp(0, (int) $r['faultCode'], (string) $r['faultString']);
                 } else {
                     // functions using EPI api should NOT return resp objects,
                     // so make sure we encode the return type correctly
                     $r =& new jsonrpcresp(php_xmlrpc_encode($r, array('extension_api')));
                 }
             } else {
                 $r = call_user_func_array($func, $params);
             }
         }
         // the return type can be either an xmlrpcresp object or a plain php value...
         if (!is_a($r, 'xmlrpcresp')) {
             // what should we assume here about automatic encoding of datetimes
             // and php classes instances???
             $r = new jsonrpcresp(php_jsonrpc_encode($r));
         }
     }
     // here $r is either an xmlrpcresp or jsonrpcresp
     if (!is_a($r, 'jsonrpcresp')) {
         // dirty trick: user has given us back an xmlrpc response,
         // since he had an existing xmlrpc server with boatloads of code.
         // Be nice to him, and serialize the xmlrpc stuff into JSON.
         // We also override the content_type of the xmlrpc response,
         // but lack knoweledge of intented response charset...
         $r->content_type = 'text/plain';
         $r->payload = serialize_jsonrpcresp($r, $msgID);
     } else {
         $r->id = $msgID;
     }
     if ($this->debug > 2) {
         // note: restore the error handler we found before calling the
         // user func, even if it has been changed inside the func itself
         if ($GLOBALS['_xmlrpcs_prev_ehandler']) {
             set_error_handler($GLOBALS['_xmlrpcs_prev_ehandler']);
         } else {
             restore_error_handler();
         }
     }
     return $r;
 }
 /**
  * Returns json representation of the response.
  * @param string $charset_encoding the charset to be used for serialization. if null, US-ASCII is assumed
  * @return string the json representation of the response
  * @access public
  */
 function serialize($charset_encoding = '')
 {
     $this->payload = serialize_jsonrpcresp($this, $this->id, $charset_encoding);
     return $this->payload;
 }