OSDN Git Service

This commit was generated by cvs2svn to compensate for changes in r4,
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / xmlrpcs.inc.php
1 <?php
2 // by Edd Dumbill (C) 1999-2001
3 // <edd@usefulinc.com>
4 // $Id: xmlrpcs.inc.php,v 1.1.1.1 2005-02-28 07:14:54 kimitake Exp $
5
6 // Copyright (c) 1999,2000,2001 Edd Dumbill.
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions
11 // are met:
12 //
13 //    * Redistributions of source code must retain the above copyright
14 //      notice, this list of conditions and the following disclaimer.
15 //
16 //    * Redistributions in binary form must reproduce the above
17 //      copyright notice, this list of conditions and the following
18 //      disclaimer in the documentation and/or other materials provided
19 //      with the distribution.
20 //
21 //    * Neither the name of the "XML-RPC for PHP" nor the names of its
22 //      contributors may be used to endorse or promote products derived
23 //      from this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 // REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
36 // OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 // XML RPC Server class
39 // requires: xmlrpc.inc
40
41 // listMethods: either a string, or nothing
42 $_xmlrpcs_listMethods_sig=array(array($xmlrpcArray, $xmlrpcString), 
43                                                                                                                                 array($xmlrpcArray));
44 $_xmlrpcs_listMethods_doc='This method lists all the methods that the XML-RPC server knows how to dispatch';
45 function _xmlrpcs_listMethods($server, $m) {
46         global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
47         $v=new xmlrpcval();
48         $dmap=$server->dmap;
49         $outAr=array();
50         for(reset($dmap); list($key, $val)=each($dmap); ) {
51                 $outAr[]=new xmlrpcval($key, "string");
52         }
53         $dmap=$_xmlrpcs_dmap;
54         for(reset($dmap); list($key, $val)=each($dmap); ) {
55                 $outAr[]=new xmlrpcval($key, "string");
56         }
57         $v->addArray($outAr);
58         return new xmlrpcresp($v);
59 }
60
61 $_xmlrpcs_methodSignature_sig=array(array($xmlrpcArray, $xmlrpcString));
62 $_xmlrpcs_methodSignature_doc='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)';
63 function _xmlrpcs_methodSignature($server, $m) {
64         global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
65
66         $methName=$m->getParam(0);
67         $methName=$methName->scalarval();
68         if (ereg("^system\.", $methName)) {
69                 $dmap=$_xmlrpcs_dmap; $sysCall=1;
70         } else {
71                 $dmap=$server->dmap; $sysCall=0;
72         }
73         //      print "<!-- ${methName} -->\n";
74         if (isset($dmap[$methName])) {
75                 if ($dmap[$methName]["signature"]) {
76                         $sigs=array();
77                         $thesigs=$dmap[$methName]["signature"];
78                         for($i=0; $i<sizeof($thesigs); $i++) {
79                                 $cursig=array();
80                                 $inSig=$thesigs[$i];
81                                 for($j=0; $j<sizeof($inSig); $j++) {
82                                         $cursig[]=new xmlrpcval($inSig[$j], "string");
83                                 }
84                                 $sigs[]=new xmlrpcval($cursig, "array");
85                         }
86                         $r=new xmlrpcresp(new xmlrpcval($sigs, "array"));
87                 } else {
88                         $r=new xmlrpcresp(new xmlrpcval("undef", "string"));
89                 }
90         } else {
91                         $r=new xmlrpcresp(0,
92                                                   $xmlrpcerr["introspect_unknown"],
93                                                   $xmlrpcstr["introspect_unknown"]);
94         }
95         return $r;
96 }
97
98 $_xmlrpcs_methodHelp_sig=array(array($xmlrpcString, $xmlrpcString));
99 $_xmlrpcs_methodHelp_doc='Returns help text if defined for the method passed, otherwise returns an empty string';
100 function _xmlrpcs_methodHelp($server, $m) {
101         global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
102
103         $methName=$m->getParam(0);
104         $methName=$methName->scalarval();
105         if (ereg("^system\.", $methName)) {
106                 $dmap=$_xmlrpcs_dmap; $sysCall=1;
107         } else {
108                 $dmap=$server->dmap; $sysCall=0;
109         }
110         //      print "<!-- ${methName} -->\n";
111         if (isset($dmap[$methName])) {
112                 if ($dmap[$methName]["docstring"]) {
113                         $r=new xmlrpcresp(new xmlrpcval($dmap[$methName]["docstring"]),
114                                                                                                 "string");
115                 } else {
116                         $r=new xmlrpcresp(new xmlrpcval("", "string"));
117                 }
118         } else {
119                         $r=new xmlrpcresp(0,
120                                                   $xmlrpcerr["introspect_unknown"],
121                                                   $xmlrpcstr["introspect_unknown"]);
122         }
123         return $r;
124 }
125
126 $_xmlrpcs_dmap=array(
127                                                                                  "system.listMethods" =>
128                                                                                  array("function" => "_xmlrpcs_listMethods",
129                                                                                                          "signature" => $_xmlrpcs_listMethods_sig,
130                                                                                                          "docstring" => $_xmlrpcs_listMethods_doc),
131                                                                                  "system.methodHelp" =>
132                                                                                  array("function" => "_xmlrpcs_methodHelp",
133                                                                                                          "signature" => $_xmlrpcs_methodHelp_sig,
134                                                                                                          "docstring" => $_xmlrpcs_methodHelp_doc),
135                                                                                  "system.methodSignature" =>
136                                                                                  array("function" => "_xmlrpcs_methodSignature",
137                                                                                                          "signature" => $_xmlrpcs_methodSignature_sig,
138                                                                                                          "docstring" => $_xmlrpcs_methodSignature_doc)
139                                                                                  );
140
141 $_xmlrpc_debuginfo="";
142 function xmlrpc_debugmsg($m) {
143         global $_xmlrpc_debuginfo;
144         $_xmlrpc_debuginfo=$_xmlrpc_debuginfo . $m . "\n";
145 }
146
147 class xmlrpc_server {
148   var $dmap=array();
149
150   function xmlrpc_server($dispMap, $serviceNow=1) {
151                 global $HTTP_RAW_POST_DATA;
152                 // dispMap is a despatch array of methods
153                 // mapped to function names and signatures
154                 // if a method
155                 // doesn't appear in the map then an unknown
156                 // method error is generated
157                 $this->dmap=$dispMap;
158                 if ($serviceNow) {
159                         $this->service();
160                 }
161   }
162
163         function serializeDebug() {
164                 global $_xmlrpc_debuginfo;
165                 if ($_xmlrpc_debuginfo!="") 
166                         return "<!-- DEBUG INFO:\n\n" .
167                                 $_xmlrpc_debuginfo . "\n-->\n";
168                 else
169                         return "";
170         }
171
172         function service() {
173                 $r=$this->parseRequest();
174                 $payload="<" . "?xml version=\"1.0\"?" . ">\n" . 
175                         $this->serializeDebug() .
176                         $r->serialize();
177                 Header("Content-type: text/xml\r\nContent-length: " . 
178                                          strlen($payload));
179                 print $payload;
180         }
181
182         function verifySignature($in, $sig) {
183                 for($i=0; $i<sizeof($sig); $i++) {
184                         // check each possible signature in turn
185                         $cursig=$sig[$i];
186                         if (sizeof($cursig)==$in->getNumParams()+1) {
187                                 $itsOK=1;
188                                 for($n=0; $n<$in->getNumParams(); $n++) {
189                                         $p=$in->getParam($n);
190                                         // print "<!-- $p -->\n";
191                                         if ($p->kindOf() == "scalar") {
192                                                 $pt=$p->scalartyp();
193                                         } else {
194                                                 $pt=$p->kindOf();
195                                         }
196                                         // $n+1 as first type of sig is return type
197                                         if ($pt != $cursig[$n+1]) {
198                                                 $itsOK=0;
199                                                 $pno=$n+1; $wanted=$cursig[$n+1]; $got=$pt;
200                                                 break;
201                                         }
202                                 }
203                         if ($itsOK) 
204                                 return array(1);
205                         }
206                 }
207                 return array(0, "Wanted ${wanted}, got ${got} at param ${pno})");
208         }
209
210   function parseRequest($data="") {
211         global $_xh,$HTTP_RAW_POST_DATA;
212         global $xmlrpcerr, $xmlrpcstr, $xmlrpcerrxml, $xmlrpc_defencoding,
213                 $_xmlrpcs_dmap;
214
215         
216
217         if ($data=="") {
218           $data=$HTTP_RAW_POST_DATA;
219         }
220         $parser = xml_parser_create($xmlrpc_defencoding);
221
222         $_xh[$parser]=array();
223         $_xh[$parser]['st']="";
224         $_xh[$parser]['cm']=0; 
225         $_xh[$parser]['isf']=0; 
226         $_xh[$parser]['params']=array();
227         $_xh[$parser]['method']="";
228
229         // decompose incoming XML into request structure
230
231         xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
232         xml_set_element_handler($parser, "xmlrpc_se", "xmlrpc_ee");
233         xml_set_character_data_handler($parser, "xmlrpc_cd");
234         xml_set_default_handler($parser, "xmlrpc_dh");
235         if (!xml_parse($parser, $data, 1)) {
236           // return XML error as a faultCode
237           $r=new xmlrpcresp(0,
238                                                 $xmlrpcerrxml+xml_get_error_code($parser),
239                                                 sprintf("XML error: %s at line %d",
240                                         xml_error_string(xml_get_error_code($parser)),
241                                   xml_get_current_line_number($parser)));
242           xml_parser_free($parser);
243         } else {
244           xml_parser_free($parser);
245           $m=new xmlrpcmsg($_xh[$parser]['method']);
246           // now add parameters in
247           $plist="";
248           for($i=0; $i<sizeof($_xh[$parser]['params']); $i++) {
249                         //print "<!-- " . $_xh[$parser]['params'][$i]. "-->\n";
250                         $plist.="$i - " .  $_xh[$parser]['params'][$i]. " \n";
251                         eval('$m->addParam(' . $_xh[$parser]['params'][$i]. ");");
252           }
253                 // uncomment this to really see what the server's getting!
254                 // xmlrpc_debugmsg($plist);
255           // now to deal with the method
256                 $methName=$_xh[$parser]['method'];
257                 if (ereg("^system\.", $methName)) {
258                         $dmap=$_xmlrpcs_dmap; $sysCall=1;
259                 } else {
260                         $dmap=$this->dmap; $sysCall=0;
261                 }
262           if (isset($dmap[$methName]['function'])) {
263                         // dispatch if exists
264                         if (isset($dmap[$methName]['signature'])) {
265                                 $sr=$this->verifySignature($m, 
266                                                                                                                                         $dmap[$methName]['signature'] );
267                         }
268                         if ( (!isset($dmap[$methName]['signature']))
269                                          || $sr[0]) {
270                                 // if no signature or correct signature
271                                 if ($sysCall) { 
272                                         eval('$r=' . $dmap[$methName]['function'] . 
273                                                          '($this, $m);');
274                                 } else {
275                                         eval('$r=' . $dmap[$methName]['function'] . 
276                                                          '($m);');
277                                 }
278                         } else {
279                                 $r=new xmlrpcresp(0,
280                                                   $xmlrpcerr["incorrect_params"],
281                                                   $xmlrpcstr["incorrect_params"].": ". $sr[1]);
282                         }
283           } else {
284                 // else prepare error response
285                 $r=new xmlrpcresp(0,
286                                                   $xmlrpcerr["unknown_method"],
287                                                   $xmlrpcstr["unknown_method"]);
288           }
289         }
290         return $r;
291   }
292
293   function echoInput() {
294         global $HTTP_RAW_POST_DATA;
295
296         // a debugging routine: just echos back the input
297         // packet as a string value
298
299         $r=new xmlrpcresp;
300         $r->xv=new xmlrpcval( "'Aha said I: '" . $HTTP_RAW_POST_DATA, "string");
301         print $r->serialize();
302   }
303 }
304
305 ?>