| 1 |
cvsjoko |
1.1 |
<?php |
| 2 |
|
|
// by Edd Dumbill (C) 1999,2000 |
| 3 |
|
|
// <edd@usefulinc.com> |
| 4 |
joko |
1.3 |
// $Id: Server.php,v 1.2 2002/10/28 00:54:16 cvsjoko Exp $ |
| 5 |
cvsjoko |
1.1 |
|
| 6 |
|
|
// License is granted to use or modify this software ("XML-RPC for PHP") |
| 7 |
|
|
// for commercial or non-commercial use provided the copyright of the author |
| 8 |
|
|
// is preserved in any distributed or derivative work. |
| 9 |
|
|
|
| 10 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESSED OR |
| 11 |
|
|
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 12 |
|
|
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 13 |
|
|
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 14 |
|
|
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 15 |
|
|
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 16 |
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 17 |
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 18 |
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 19 |
|
|
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 20 |
|
|
|
| 21 |
|
|
// Adapted to PEAR standards by Stig Sæther Bakken <stig@php.net> |
| 22 |
|
|
|
| 23 |
|
|
// XML RPC Server class |
| 24 |
|
|
// requires: xmlrpc.inc |
| 25 |
|
|
|
| 26 |
joko |
1.3 |
require_once "XML/RPC/RPC.php"; |
| 27 |
cvsjoko |
1.1 |
|
| 28 |
|
|
// listMethods: either a string, or nothing |
| 29 |
|
|
$GLOBALS['XML_RPC_Server_listMethods_sig']= |
| 30 |
|
|
array(array($GLOBALS['XML_RPC_Array'], $GLOBALS['XML_RPC_String']), |
| 31 |
|
|
array($GLOBALS['XML_RPC_Array'])); |
| 32 |
|
|
$GLOBALS['XML_RPC_Server_listMethods_doc']= |
| 33 |
|
|
'This method lists all the methods that the XML-RPC server knows how to dispatch'; |
| 34 |
|
|
|
| 35 |
|
|
function XML_RPC_Server_listMethods($server, $m) |
| 36 |
|
|
{ |
| 37 |
|
|
global $XML_RPC_err, $XML_RPC_str, $XML_RPC_Server_dmap; |
| 38 |
|
|
$v=new XML_RPC_Value(); |
| 39 |
|
|
$dmap=$server->dmap; |
| 40 |
|
|
$outAr=array(); |
| 41 |
|
|
for(reset($dmap); list($key, $val)=each($dmap); ) { |
| 42 |
|
|
$outAr[]=new XML_RPC_Value($key, "string"); |
| 43 |
|
|
} |
| 44 |
|
|
$dmap=$XML_RPC_Server_dmap; |
| 45 |
|
|
for(reset($dmap); list($key, $val)=each($dmap); ) { |
| 46 |
|
|
$outAr[]=new XML_RPC_Value($key, "string"); |
| 47 |
|
|
} |
| 48 |
|
|
$v->addArray($outAr); |
| 49 |
|
|
return new XML_RPC_Response($v); |
| 50 |
|
|
} |
| 51 |
|
|
|
| 52 |
|
|
$GLOBALS['XML_RPC_Server_methodSignature_sig']= |
| 53 |
|
|
array(array($GLOBALS['XML_RPC_Array'], $GLOBALS['XML_RPC_String'])); |
| 54 |
|
|
$GLOBALS['XML_RPC_Server_methodSignature_doc']= |
| 55 |
|
|
'Returns an array of known signatures (an array of arrays) for the method name passed. If no signatures are known, returns a none-array (test for type != array to detect missing signature)'; |
| 56 |
|
|
|
| 57 |
|
|
function XML_RPC_Server_methodSignature($server, $m) |
| 58 |
|
|
{ |
| 59 |
|
|
global $XML_RPC_err, $XML_RPC_str, $XML_RPC_Server_dmap; |
| 60 |
|
|
|
| 61 |
|
|
$methName=$m->getParam(0); |
| 62 |
|
|
$methName=$methName->scalarval(); |
| 63 |
|
|
if (ereg("^system\.", $methName)) { |
| 64 |
|
|
$dmap=$XML_RPC_Server_dmap; $sysCall=1; |
| 65 |
|
|
} else { |
| 66 |
|
|
$dmap=$server->dmap; $sysCall=0; |
| 67 |
|
|
} |
| 68 |
|
|
// print "<!-- ${methName} -->\n"; |
| 69 |
|
|
if (isset($dmap[$methName])) { |
| 70 |
|
|
if ($dmap[$methName]["signature"]) { |
| 71 |
|
|
$sigs=array(); |
| 72 |
|
|
$thesigs=$dmap[$methName]["signature"]; |
| 73 |
|
|
for($i=0; $i<sizeof($thesigs); $i++) { |
| 74 |
|
|
$cursig=array(); |
| 75 |
|
|
$inSig=$thesigs[$i]; |
| 76 |
|
|
for($j=0; $j<sizeof($inSig); $j++) { |
| 77 |
|
|
$cursig[]=new XML_RPC_Value($inSig[$j], "string"); |
| 78 |
|
|
} |
| 79 |
|
|
$sigs[]=new XML_RPC_Value($cursig, "array"); |
| 80 |
|
|
} |
| 81 |
|
|
$r=new XML_RPC_Response(new XML_RPC_Value($sigs, "array")); |
| 82 |
|
|
} else { |
| 83 |
|
|
$r=new XML_RPC_Response(new XML_RPC_Value("undef", "string")); |
| 84 |
|
|
} |
| 85 |
|
|
} else { |
| 86 |
|
|
$r=new XML_RPC_Response(0, |
| 87 |
|
|
$XML_RPC_err["introspect_unknown"], |
| 88 |
|
|
$XML_RPC_str["introspect_unknown"]); |
| 89 |
|
|
} |
| 90 |
|
|
return $r; |
| 91 |
|
|
} |
| 92 |
|
|
|
| 93 |
|
|
$GLOBALS['XML_RPC_Server_methodHelp_sig']= |
| 94 |
|
|
array(array($GLOBALS['XML_RPC_String'], $GLOBALS['XML_RPC_String'])); |
| 95 |
|
|
$GLOBALS['XML_RPC_Server_methodHelp_doc']= |
| 96 |
|
|
'Returns help text if defined for the method passed, otherwise returns an empty string'; |
| 97 |
|
|
|
| 98 |
|
|
function XML_RPC_Server_methodHelp($server, $m) |
| 99 |
|
|
{ |
| 100 |
|
|
global $XML_RPC_err, $XML_RPC_str, $XML_RPC_Server_dmap; |
| 101 |
|
|
|
| 102 |
|
|
$methName=$m->getParam(0); |
| 103 |
|
|
$methName=$methName->scalarval(); |
| 104 |
|
|
if (ereg("^system\.", $methName)) { |
| 105 |
|
|
$dmap=$XML_RPC_Server_dmap; $sysCall=1; |
| 106 |
|
|
} else { |
| 107 |
|
|
$dmap=$server->dmap; $sysCall=0; |
| 108 |
|
|
} |
| 109 |
|
|
// print "<!-- ${methName} -->\n"; |
| 110 |
|
|
if (isset($dmap[$methName])) { |
| 111 |
|
|
if ($dmap[$methName]["docstring"]) { |
| 112 |
|
|
$r=new XML_RPC_Response(new XML_RPC_Value($dmap[$methName]["docstring"]), |
| 113 |
|
|
"string"); |
| 114 |
|
|
} else { |
| 115 |
|
|
$r=new XML_RPC_Response(new XML_RPC_Value("", "string")); |
| 116 |
|
|
} |
| 117 |
|
|
} else { |
| 118 |
|
|
$r=new XML_RPC_Response(0, |
| 119 |
|
|
$XML_RPC_err["introspect_unknown"], |
| 120 |
|
|
$XML_RPC_str["introspect_unknown"]); |
| 121 |
|
|
} |
| 122 |
|
|
return $r; |
| 123 |
|
|
} |
| 124 |
|
|
|
| 125 |
|
|
$GLOBALS['XML_RPC_Server_dmap']=array( |
| 126 |
|
|
"system.listMethods" => |
| 127 |
|
|
array("function" => "XML_RPC_Server_listMethods", |
| 128 |
|
|
"signature" => $GLOBALS['XML_RPC_Server_listMethods_sig'], |
| 129 |
|
|
"docstring" => $GLOBALS['XML_RPC_Server_listMethods_doc']), |
| 130 |
|
|
|
| 131 |
|
|
"system.methodHelp" => |
| 132 |
|
|
array("function" => "XML_RPC_Server_methodHelp", |
| 133 |
|
|
"signature" => $GLOBALS['XML_RPC_Server_methodHelp_sig'], |
| 134 |
|
|
"docstring" => $GLOBALS['XML_RPC_Server_methodHelp_doc']), |
| 135 |
|
|
|
| 136 |
|
|
"system.methodSignature" => |
| 137 |
|
|
array("function" => "XML_RPC_Server_methodSignature", |
| 138 |
|
|
"signature" => $GLOBALS['XML_RPC_Server_methodSignature_sig'], |
| 139 |
|
|
"docstring" => $GLOBALS['XML_RPC_Server_methodSignature_doc']) |
| 140 |
|
|
); |
| 141 |
|
|
|
| 142 |
|
|
$GLOBALS['XML_RPC_Server_debuginfo']=""; |
| 143 |
|
|
|
| 144 |
|
|
function XML_RPC_Server_debugmsg($m) |
| 145 |
|
|
{ |
| 146 |
|
|
global $XML_RPC_Server_debuginfo; |
| 147 |
|
|
$XML_RPC_Server_debuginfo=$XML_RPC_Server_debuginfo . $m . "\n"; |
| 148 |
|
|
} |
| 149 |
|
|
|
| 150 |
|
|
class XML_RPC_Server |
| 151 |
|
|
{ |
| 152 |
|
|
var $dmap=array(); |
| 153 |
|
|
|
| 154 |
|
|
function XML_RPC_Server($dispMap, $serviceNow=1) |
| 155 |
|
|
{ |
| 156 |
|
|
global $HTTP_RAW_POST_DATA; |
| 157 |
|
|
// dispMap is a despatch array of methods |
| 158 |
|
|
// mapped to function names and signatures |
| 159 |
|
|
// if a method |
| 160 |
|
|
// doesn't appear in the map then an unknown |
| 161 |
|
|
// method error is generated |
| 162 |
|
|
$this->dmap=$dispMap; |
| 163 |
|
|
if ($serviceNow) { |
| 164 |
|
|
$this->service(); |
| 165 |
|
|
} |
| 166 |
|
|
} |
| 167 |
|
|
|
| 168 |
|
|
function serializeDebug() |
| 169 |
|
|
{ |
| 170 |
|
|
global $XML_RPC_Server_debuginfo; |
| 171 |
|
|
if ($XML_RPC_Server_debuginfo!="") |
| 172 |
|
|
return "<!-- DEBUG INFO:\n\n" . |
| 173 |
|
|
$XML_RPC_Server_debuginfo . "\n-->\n"; |
| 174 |
|
|
else |
| 175 |
|
|
return ""; |
| 176 |
|
|
} |
| 177 |
|
|
|
| 178 |
|
|
function service() |
| 179 |
|
|
{ |
| 180 |
|
|
$r=$this->parseRequest(); |
| 181 |
|
|
$payload="<?xml version=\"1.0\"?>\n" . |
| 182 |
|
|
$this->serializeDebug() . |
| 183 |
|
|
$r->serialize(); |
| 184 |
|
|
Header("Content-type: text/xml\nContent-length: " . |
| 185 |
|
|
strlen($payload)); |
| 186 |
|
|
print $payload; |
| 187 |
|
|
} |
| 188 |
|
|
|
| 189 |
|
|
function verifySignature($in, $sig) |
| 190 |
|
|
{ |
| 191 |
|
|
for($i=0; $i<sizeof($sig); $i++) { |
| 192 |
|
|
// check each possible signature in turn |
| 193 |
|
|
$cursig=$sig[$i]; |
| 194 |
|
|
if (sizeof($cursig)==$in->getNumParams()+1) { |
| 195 |
|
|
$itsOK=1; |
| 196 |
|
|
for($n=0; $n<$in->getNumParams(); $n++) { |
| 197 |
|
|
$p=$in->getParam($n); |
| 198 |
|
|
// print "<!-- $p -->\n"; |
| 199 |
|
|
if ($p->kindOf() == "scalar") { |
| 200 |
|
|
$pt=$p->scalartyp(); |
| 201 |
|
|
} else { |
| 202 |
|
|
$pt=$p->kindOf(); |
| 203 |
|
|
} |
| 204 |
|
|
// $n+1 as first type of sig is return type |
| 205 |
|
|
if ($pt != $cursig[$n+1]) { |
| 206 |
|
|
$itsOK=0; |
| 207 |
|
|
$pno=$n+1; $wanted=$cursig[$n+1]; $got=$pt; |
| 208 |
|
|
break; |
| 209 |
|
|
} |
| 210 |
|
|
} |
| 211 |
|
|
if ($itsOK) |
| 212 |
|
|
return array(1); |
| 213 |
|
|
} |
| 214 |
|
|
} |
| 215 |
|
|
return array(0, "Wanted ${wanted}, got ${got} at param ${pno})"); |
| 216 |
|
|
} |
| 217 |
|
|
|
| 218 |
|
|
function parseRequest($data="") |
| 219 |
|
|
{ |
| 220 |
|
|
global $XML_RPC_xh,$HTTP_RAW_POST_DATA; |
| 221 |
|
|
global $XML_RPC_err, $XML_RPC_str, $XML_RPC_errxml, |
| 222 |
|
|
$XML_RPC_defencoding, $XML_RPC_Server_dmap; |
| 223 |
|
|
|
| 224 |
|
|
if ($data=="") { |
| 225 |
|
|
$data=$HTTP_RAW_POST_DATA; |
| 226 |
|
|
} |
| 227 |
|
|
$parser = xml_parser_create($XML_RPC_defencoding); |
| 228 |
|
|
|
| 229 |
|
|
$XML_RPC_xh[$parser]=array(); |
| 230 |
|
|
$XML_RPC_xh[$parser]['st']=""; |
| 231 |
|
|
$XML_RPC_xh[$parser]['cm']=0; |
| 232 |
|
|
$XML_RPC_xh[$parser]['isf']=0; |
| 233 |
|
|
$XML_RPC_xh[$parser]['params']=array(); |
| 234 |
|
|
$XML_RPC_xh[$parser]['method']=""; |
| 235 |
|
|
|
| 236 |
|
|
$plist = ''; |
| 237 |
|
|
|
| 238 |
|
|
// decompose incoming XML into request structure |
| 239 |
|
|
|
| 240 |
|
|
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true); |
| 241 |
|
|
xml_set_element_handler($parser, "XML_RPC_se", "XML_RPC_ee"); |
| 242 |
|
|
xml_set_character_data_handler($parser, "XML_RPC_cd"); |
| 243 |
|
|
xml_set_default_handler($parser, "XML_RPC_dh"); |
| 244 |
|
|
if (!xml_parse($parser, $data, 1)) { |
| 245 |
|
|
// return XML error as a faultCode |
| 246 |
|
|
$r=new XML_RPC_Response(0, |
| 247 |
|
|
$XML_RPC_errxml+xml_get_error_code($parser), |
| 248 |
|
|
sprintf("XML error: %s at line %d", |
| 249 |
|
|
xml_error_string(xml_get_error_code($parser)), |
| 250 |
|
|
xml_get_current_line_number($parser))); |
| 251 |
|
|
xml_parser_free($parser); |
| 252 |
|
|
} else { |
| 253 |
|
|
xml_parser_free($parser); |
| 254 |
|
|
$m=new XML_RPC_Message($XML_RPC_xh[$parser]['method']); |
| 255 |
|
|
// now add parameters in |
| 256 |
|
|
for($i=0; $i<sizeof($XML_RPC_xh[$parser]['params']); $i++) { |
| 257 |
|
|
// print "<!-- " . $XML_RPC_xh[$parser]['params'][$i]. "-->\n"; |
| 258 |
|
|
$plist.="$i - " . $XML_RPC_xh[$parser]['params'][$i]. " \n"; |
| 259 |
|
|
eval('$m->addParam(' . $XML_RPC_xh[$parser]['params'][$i]. ");"); |
| 260 |
|
|
} |
| 261 |
|
|
XML_RPC_Server_debugmsg($plist); |
| 262 |
|
|
// now to deal with the method |
| 263 |
|
|
$methName=$XML_RPC_xh[$parser]['method']; |
| 264 |
|
|
if (ereg("^system\.", $methName)) { |
| 265 |
|
|
$dmap=$XML_RPC_Server_dmap; $sysCall=1; |
| 266 |
|
|
} else { |
| 267 |
|
|
$dmap=$this->dmap; $sysCall=0; |
| 268 |
|
|
} |
| 269 |
|
|
if (isset($dmap[$methName]['function'])) { |
| 270 |
|
|
// dispatch if exists |
| 271 |
|
|
if (isset($dmap[$methName]['signature'])) { |
| 272 |
|
|
$sr=$this->verifySignature($m, |
| 273 |
|
|
$dmap[$methName]['signature'] ); |
| 274 |
|
|
} |
| 275 |
|
|
if ( (!isset($dmap[$methName]['signature'])) || $sr[0]) { |
| 276 |
|
|
// if no signature or correct signature |
| 277 |
|
|
if ($sysCall) { |
| 278 |
|
|
eval('$r=' . $dmap[$methName]['function'] . |
| 279 |
|
|
'($this, $m);'); |
| 280 |
|
|
} else { |
| 281 |
|
|
eval('$r=' . $dmap[$methName]['function'] . |
| 282 |
|
|
'($m);'); |
| 283 |
|
|
} |
| 284 |
|
|
} else { |
| 285 |
|
|
$r=new XML_RPC_Response(0, |
| 286 |
|
|
$XML_RPC_err["incorrect_params"], |
| 287 |
|
|
$XML_RPC_str["incorrect_params"].": ". $sr[1]); |
| 288 |
|
|
} |
| 289 |
|
|
} else { |
| 290 |
|
|
// else prepare error response |
| 291 |
|
|
$r=new XML_RPC_Response(0, |
| 292 |
|
|
$XML_RPC_err["unknown_method"], |
| 293 |
|
|
$XML_RPC_str["unknown_method"]); |
| 294 |
|
|
} |
| 295 |
|
|
} |
| 296 |
|
|
return $r; |
| 297 |
|
|
} |
| 298 |
|
|
|
| 299 |
|
|
function echoInput() { |
| 300 |
|
|
global $HTTP_RAW_POST_DATA; |
| 301 |
|
|
|
| 302 |
|
|
// a debugging routine: just echos back the input |
| 303 |
|
|
// packet as a string value |
| 304 |
|
|
|
| 305 |
|
|
$r=new XML_RPC_Response; |
| 306 |
|
|
$r->xv=new XML_RPC_Value( "'Aha said I: '" . $HTTP_RAW_POST_DATA, "string"); |
| 307 |
|
|
print $r->serialize(); |
| 308 |
|
|
} |
| 309 |
|
|
} |
| 310 |
|
|
|
| 311 |
|
|
?> |