getParam(0); // param must be there and of the correct type: server object does the validation for us // extract the value of the state number $snv = $sno->scalarval(); // look it up in our array (zero-based) if (isset($GLOBALS['stateNames'][$snv - 1])) { $stateName = $GLOBALS['stateNames'][$snv - 1]; } else { // not there, so complain $err = "I don't have a state for the index '" . $snv . "'"; } // if we generated an error, create an error return response if ($err) { return new Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err); } else { // otherwise, we create the right response with the state name return new Response(new Value($stateName)); } } // Sorting demo // // send me an array of structs thus: // // Dave 35 // Edd 45 // Fred 23 // Barney 37 // // and I'll return it to you in sorted order function agesorter_compare($a, $b) { /// @todo move away from usage of globals for such a simple case global $agesorter_arr; // don't even ask me _why_ these come padded with hyphens, I couldn't tell you :p $a = str_replace("-", "", $a); $b = str_replace("-", "", $b); if ($agesorter_arr[$a] == $agesorter_arr[$b]) { return 0; } return ($agesorter_arr[$a] > $agesorter_arr[$b]) ? -1 : 1; } $agesorter_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray)); $agesorter_doc = 'Send this method an array of [string, int] structs, eg:
Dave 35 Edd 45 Fred 23 Barney 37And the array will be returned with the entries sorted by their numbers. '; function ageSorter($req) { global $agesorter_arr; Server::xmlrpc_debugmsg("Entering 'agesorter'"); // get the parameter $sno = $req->getParam(0); // error string for [if|when] things go wrong $err = ""; $agar = array(); $max = $sno->count(); Server::xmlrpc_debugmsg("Found $max array elements"); foreach ($sno as $i => $rec) { if ($rec->kindOf() != "struct") { $err = "Found non-struct in array at element $i"; break; } // extract name and age from struct $n = $rec["name"]; $a = $rec["age"]; // $n and $a are Values, // so get the scalarval from them $agar[$n->scalarval()] = $a->scalarval(); } // create the output value $v = new Value(array(), Value::$xmlrpcArray); $agesorter_arr = $agar; // hack, must make global as uksort() won't // allow us to pass any other auxiliary information uksort($agesorter_arr, 'agesorter_compare'); foreach($agesorter_arr as $key => $val) { // recreate each struct element $v[] = new Value( array( "name" => new Value($key), "age" => new Value($val, "int") ), Value::$xmlrpcStruct ); } if ($err) { return new Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err); } else { return new Response($v); } } $addtwo_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcInt, Value::$xmlrpcInt)); $addtwo_doc = 'Add two integers together and return the result'; function addTwo($req) { $s = $req->getParam(0); $t = $req->getParam(1); return new Response(new Value($s->scalarval() + $t->scalarval(), Value::$xmlrpcInt)); } $addtwodouble_sig = array(array(Value::$xmlrpcDouble, Value::$xmlrpcDouble, Value::$xmlrpcDouble)); $addtwodouble_doc = 'Add two doubles together and return the result'; function addTwoDouble($req) { $s = $req->getParam(0); $t = $req->getParam(1); return new Response(new Value($s->scalarval() + $t->scalarval(), Value::$xmlrpcDouble)); } $stringecho_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcString)); $stringecho_doc = 'Accepts a string parameter, returns the string.'; function stringEcho($req) { // just sends back a string return new Response(new Value($req->getParam(0)->scalarval())); } $echoback_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcString)); $echoback_doc = 'Accepts a string parameter, returns the entire incoming payload'; function echoBack($req) { // just sends back a string with what i got sent to me, just escaped, that's all $s = "I got the following message:\n" . $req->serialize(); return new Response(new Value($s)); } $echosixtyfour_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcBase64)); $echosixtyfour_doc = 'Accepts a base64 parameter and returns it decoded as a string'; function echoSixtyFour($req) { // Accepts an encoded value, but sends it back as a normal string. // This is to test that base64 encoding is working as expected $incoming = $req->getParam(0); return new Response(new Value($incoming->scalarval(), Value::$xmlrpcString)); } $bitflipper_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray)); $bitflipper_doc = 'Accepts an array of booleans, and returns them inverted'; function bitFlipper($req) { $v = $req->getParam(0); $rv = new Value(array(), Value::$xmlrpcArray); foreach ($v as $b) { if ($b->scalarval()) { $rv[] = new Value(false, Value::$xmlrpcBoolean); } else { $rv[] = new Value(true, Value::$xmlrpcBoolean); } } return new Response($rv); } $getallheaders_sig = array(array(Value::$xmlrpcStruct)); $getallheaders_doc = 'Returns a struct containing all the HTTP headers received with the request. Provides limited functionality with IIS'; function getAllHeaders_xmlrpc($req) { $encoder = new Encoder(); if (function_exists('getallheaders')) { return new Response($encoder->encode(getallheaders())); } else { $headers = array(); // IIS: poor man's version of getallheaders foreach ($_SERVER as $key => $val) { if (strpos($key, 'HTTP_') === 0) { $key = ucfirst(str_replace('_', '-', strtolower(substr($key, 5)))); $headers[$key] = $val; } } return new Response($encoder->encode($headers)); } } $setcookies_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcStruct)); $setcookies_doc = 'Sends to client a response containing a single \'1\' digit, and sets to it http cookies as received in the request (array of structs describing a cookie)'; function setCookies($req) { $encoder = new Encoder(); $cookies = $req->getParam(0); foreach ($cookies as $name => $value) { $cookieDesc = $encoder->decode($value); setcookie($name, @$cookieDesc['value'], @$cookieDesc['expires'], @$cookieDesc['path'], @$cookieDesc['domain'], @$cookieDesc['secure']); } return new Response(new Value(1, Value::$xmlrpcInt)); } $getcookies_sig = array(array(Value::$xmlrpcStruct)); $getcookies_doc = 'Sends to client a response containing all http cookies as received in the request (as struct)'; function getCookies($req) { $encoder = new Encoder(); return new Response($encoder->encode($_COOKIE)); } $mailsend_sig = array(array( Value::$xmlrpcBoolean, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString, )); $mailsend_doc = 'mail.send(recipient, subject, text, sender, cc, bcc, mimetype)