Implement interface ArrayAccess in the Value class
[plcapi.git] / demo / server / server.php
1 <?php
2 /**
3  * Demo server for xmlrpc library.
4  *
5  * Implements a lot of webservices, including a suite of services used for
6  * interoperability testing (validator1 methods), and some whose only purpose
7  * is to be used for unit-testing the library.
8  *
9  * Please do not copy this file verbatim into your production server.
10  **/
11
12 // give user a chance to see the source for this server instead of running the services
13 if ($_SERVER['REQUEST_METHOD'] != 'POST' && isset($_GET['showSource'])) {
14     highlight_file(__FILE__);
15     die();
16 }
17
18 include_once __DIR__ . "/../../vendor/autoload.php";
19
20 // out-of-band information: let the client manipulate the server operations.
21 // we do this to help the testsuite script: do not reproduce in production!
22 if (isset($_COOKIE['PHPUNIT_SELENIUM_TEST_ID']) && extension_loaded('xdebug')) {
23     $GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'] = '/tmp/phpxmlrpc_coverage';
24     if (!is_dir($GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'])) {
25         mkdir($GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY']);
26     }
27
28     include_once __DIR__ . "/../../vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/SeleniumCommon/prepend.php";
29 }
30
31 use PhpXmlRpc\Value;
32
33 /**
34  * Used to test usage of object methods in dispatch maps and in wrapper code.
35  */
36 class xmlrpcServerMethodsContainer
37 {
38     /**
39      * Method used to test logging of php warnings generated by user functions.
40      * @param PhpXmlRpc\Request $req
41      * @return PhpXmlRpc\Response
42      */
43     public function phpWarningGenerator($req)
44     {
45         $a = $undefinedVariable; // this triggers a warning in E_ALL mode, since $undefinedVariable is undefined
46         return new PhpXmlRpc\Response(new Value(1, 'boolean'));
47     }
48
49     /**
50      * Method used to test catching of exceptions in the server.
51      * @param PhpXmlRpc\Request $req
52      * @throws Exception
53      */
54     public function exceptionGenerator($req)
55     {
56         throw new Exception("it's just a test", 1);
57     }
58
59     /**
60      * @param string $msg
61      */
62     public function debugMessageGenerator($msg)
63     {
64         PhpXmlRpc\Server::xmlrpc_debugmsg($msg);
65     }
66
67     /**
68      * A PHP version of the state-number server. Send me an integer and i'll sell you a state.
69      * Used to test wrapping of PHP methods into xmlrpc methods.
70      *
71      * @param integer $num
72      * @return string
73      * @throws Exception
74      */
75     public static function findState($num)
76     {
77         return inner_findstate($num);
78     }
79
80     /**
81      * Returns an instance of stdClass.
82      * Used to test wrapping of PHP objects with class preservation
83      */
84     public function returnObject()
85     {
86         $obj = new stdClass();
87         $obj->hello = 'world';
88         return $obj;
89     }
90 }
91
92 // a PHP version of the state-number server
93 // send me an integer and i'll sell you a state
94
95 $stateNames = array(
96     "Alabama", "Alaska", "Arizona", "Arkansas", "California",
97     "Colorado", "Columbia", "Connecticut", "Delaware", "Florida",
98     "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas",
99     "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan",
100     "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada",
101     "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina",
102     "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island",
103     "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
104     "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming",
105 );
106
107 $findstate_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcInt));
108 $findstate_doc = 'When passed an integer between 1 and 51 returns the
109 name of a US state, where the integer is the index of that state name
110 in an alphabetic order.';
111
112 function findState($req)
113 {
114     global $stateNames;
115
116     $err = "";
117     // get the first param
118     $sno = $req->getParam(0);
119
120     // param must be there and of the correct type: server object does the validation for us
121
122     // extract the value of the state number
123     $snv = $sno->scalarval();
124     // look it up in our array (zero-based)
125     if (isset($stateNames[$snv - 1])) {
126         $stateName = $stateNames[$snv - 1];
127     } else {
128         // not there, so complain
129         $err = "I don't have a state for the index '" . $snv . "'";
130     }
131
132     // if we generated an error, create an error return response
133     if ($err) {
134         return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
135     } else {
136         // otherwise, we create the right response with the state name
137         return new PhpXmlRpc\Response(new Value($stateName));
138     }
139 }
140
141 /**
142  * Inner code of the state-number server.
143  * Used to test wrapping of PHP functions into xmlrpc methods.
144  *
145  * @param integer $stateNo the state number
146  *
147  * @return string the name of the state (or error description)
148  *
149  * @throws Exception if state is not found
150  */
151 function inner_findstate($stateNo)
152 {
153     global $stateNames;
154
155     if (isset($stateNames[$stateNo - 1])) {
156         return $stateNames[$stateNo - 1];
157     } else {
158         // not, there so complain
159         throw new Exception("I don't have a state for the index '" . $stateNo . "'", PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser);
160     }
161 }
162
163 $wrapper = new PhpXmlRpc\Wrapper();
164
165 $findstate2_sig = $wrapper->wrapPhpFunction('inner_findstate');
166
167 $findstate3_sig = $wrapper->wrapPhpFunction(array('xmlrpcServerMethodsContainer', 'findState'));
168
169 $obj = new xmlrpcServerMethodsContainer();
170 $findstate4_sig = $wrapper->wrapPhpFunction(array($obj, 'findstate'));
171
172 $findstate5_sig = $wrapper->wrapPhpFunction('xmlrpcServerMethodsContainer::findState', '', array('return_source' => true));
173 eval($findstate5_sig['source']);
174
175 $findstate6_sig = $wrapper->wrapPhpFunction('inner_findstate', '', array('return_source' => true));
176 eval($findstate6_sig['source']);
177
178 $findstate7_sig = $wrapper->wrapPhpFunction(array('xmlrpcServerMethodsContainer', 'findState'), '', array('return_source' => true));
179 eval($findstate7_sig['source']);
180
181 $obj = new xmlrpcServerMethodsContainer();
182 $findstate8_sig = $wrapper->wrapPhpFunction(array($obj, 'findstate'), '', array('return_source' => true));
183 eval($findstate8_sig['source']);
184
185 $findstate9_sig = $wrapper->wrapPhpFunction('xmlrpcServerMethodsContainer::findState', '', array('return_source' => true));
186 eval($findstate9_sig['source']);
187
188 $findstate10_sig = array(
189     "function" => function ($req) { return findState($req); },
190     "signature" => $findstate_sig,
191     "docstring" => $findstate_doc,
192 );
193
194 $findstate11_sig = $wrapper->wrapPhpFunction(function ($stateNo) { return inner_findstate($stateNo); });
195
196 $c = new xmlrpcServerMethodsContainer;
197 $moreSignatures = $wrapper->wrapPhpClass($c, array('prefix' => 'tests.', 'method_type' => 'all'));
198
199 $returnObj_sig =  $wrapper->wrapPhpFunction(array($c, 'returnObject'), '', array('encode_php_objs' => true));
200
201 // used to test signatures with NULL params
202 $findstate12_sig = array(
203     array(Value::$xmlrpcString, Value::$xmlrpcInt, Value::$xmlrpcNull),
204     array(Value::$xmlrpcString, Value::$xmlrpcNull, Value::$xmlrpcInt),
205 );
206
207 function findStateWithNulls($req)
208 {
209     $a = $req->getParam(0);
210     $b = $req->getParam(1);
211
212     if ($a->scalartyp() == Value::$xmlrpcNull)
213         return new PhpXmlRpc\Response(new Value(inner_findstate($b->scalarval())));
214     else
215         return new PhpXmlRpc\Response(new Value(inner_findstate($a->scalarval())));
216 }
217
218 $addtwo_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcInt, Value::$xmlrpcInt));
219 $addtwo_doc = 'Add two integers together and return the result';
220 function addTwo($req)
221 {
222     $s = $req->getParam(0);
223     $t = $req->getParam(1);
224
225     return new PhpXmlRpc\Response(new Value($s->scalarval() + $t->scalarval(), "int"));
226 }
227
228 $addtwodouble_sig = array(array(Value::$xmlrpcDouble, Value::$xmlrpcDouble, Value::$xmlrpcDouble));
229 $addtwodouble_doc = 'Add two doubles together and return the result';
230 function addTwoDouble($req)
231 {
232     $s = $req->getParam(0);
233     $t = $req->getParam(1);
234
235     return new PhpXmlRpc\Response(new Value($s->scalarval() + $t->scalarval(), "double"));
236 }
237
238 $stringecho_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcString));
239 $stringecho_doc = 'Accepts a string parameter, returns the string.';
240 function stringEcho($req)
241 {
242     // just sends back a string
243     return new PhpXmlRpc\Response(new Value($req->getParam(0)->scalarval()));
244 }
245
246 $echoback_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcString));
247 $echoback_doc = 'Accepts a string parameter, returns the entire incoming payload';
248 function echoBack($req)
249 {
250     // just sends back a string with what i got sent to me, just escaped, that's all
251     $s = "I got the following message:\n" . $req->serialize();
252
253     return new PhpXmlRpc\Response(new Value($s));
254 }
255
256 $echosixtyfour_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcBase64));
257 $echosixtyfour_doc = 'Accepts a base64 parameter and returns it decoded as a string';
258 function echoSixtyFour($req)
259 {
260     // Accepts an encoded value, but sends it back as a normal string.
261     // This is to test that base64 encoding is working as expected
262     $incoming = $req->getParam(0);
263
264     return new PhpXmlRpc\Response(new Value($incoming->scalarval(), "string"));
265 }
266
267 $bitflipper_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray));
268 $bitflipper_doc = 'Accepts an array of booleans, and returns them inverted';
269 function bitFlipper($req)
270 {
271     $v = $req->getParam(0);
272     $rv = new Value(array(), Value::$xmlrpcArray);
273
274     foreach ($v as $b) {
275         if ($b->scalarval()) {
276             $rv->addScalar(false, "boolean");
277         } else {
278             $rv->addScalar(true, "boolean");
279         }
280     }
281
282     return new PhpXmlRpc\Response($rv);
283 }
284
285 // Sorting demo
286 //
287 // send me an array of structs thus:
288 //
289 // Dave 35
290 // Edd  45
291 // Fred 23
292 // Barney 37
293 //
294 // and I'll return it to you in sorted order
295
296 function agesorter_compare($a, $b)
297 {
298     global $agesorter_arr;
299
300     // don't even ask me _why_ these come padded with hyphens, I couldn't tell you :p
301     $a = str_replace("-", "", $a);
302     $b = str_replace("-", "", $b);
303
304     if ($agesorter_arr[$a] == $agesorter_arr[$b]) {
305         return 0;
306     }
307
308     return ($agesorter_arr[$a] > $agesorter_arr[$b]) ? -1 : 1;
309 }
310
311 $agesorter_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray));
312 $agesorter_doc = 'Send this method an array of [string, int] structs, eg:
313 <pre>
314  Dave   35
315  Edd    45
316  Fred   23
317  Barney 37
318 </pre>
319 And the array will be returned with the entries sorted by their numbers.
320 ';
321 function ageSorter($req)
322 {
323     global $agesorter_arr, $s;
324
325     PhpXmlRpc\Server::xmlrpc_debugmsg("Entering 'agesorter'");
326     // get the parameter
327     $sno = $req->getParam(0);
328     // error string for [if|when] things go wrong
329     $err = "";
330     // create the output value
331     $v = new Value();
332     $agar = array();
333
334     $max = $sno->count();
335     PhpXmlRpc\Server::xmlrpc_debugmsg("Found $max array elements");
336     foreach ($sno as $rec) {
337         if ($rec->kindOf() != "struct") {
338             $err = "Found non-struct in array at element $i";
339             break;
340         }
341         // extract name and age from struct
342         $n = $rec->structmem("name");
343         $a = $rec->structmem("age");
344         // $n and $a are xmlrpcvals,
345         // so get the scalarval from them
346         $agar[$n->scalarval()] = $a->scalarval();
347     }
348
349     $agesorter_arr = $agar;
350     // hack, must make global as uksort() won't
351     // allow us to pass any other auxiliary information
352     uksort($agesorter_arr, 'agesorter_compare');
353     $outAr = array();
354     while (list($key, $val) = each($agesorter_arr)) {
355         // recreate each struct element
356         $outAr[] = new Value(array("name" => new Value($key),
357             "age" => new Value($val, "int"),), "struct");
358     }
359     // add this array to the output value
360     $v->addArray($outAr);
361
362     if ($err) {
363         return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
364     } else {
365         return new PhpXmlRpc\Response($v);
366     }
367 }
368
369 // signature and instructions, place these in the dispatch map
370 $mailsend_sig = array(array(
371     Value::$xmlrpcBoolean, Value::$xmlrpcString, Value::$xmlrpcString,
372     Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString,
373     Value::$xmlrpcString, Value::$xmlrpcString,
374 ));
375 $mailsend_doc = 'mail.send(recipient, subject, text, sender, cc, bcc, mimetype)<br/>
376 recipient, cc, and bcc are strings, comma-separated lists of email addresses, as described above.<br/>
377 subject is a string, the subject of the message.<br/>
378 sender is a string, it\'s the email address of the person sending the message. This string can not be
379 a comma-separated list, it must contain a single email address only.<br/>
380 text is a string, it contains the body of the message.<br/>
381 mimetype, a string, is a standard MIME type, for example, text/plain.
382 ';
383 // WARNING; this functionality depends on the sendmail -t option
384 // it may not work with Windows machines properly; particularly
385 // the Bcc option. Sneak on your friends at your own risk!
386 function mailSend($req)
387 {
388     $err = "";
389
390     $mTo = $req->getParam(0);
391     $mSub = $req->getParam(1);
392     $mBody = $req->getParam(2);
393     $mFrom = $req->getParam(3);
394     $mCc = $req->getParam(4);
395     $mBcc = $req->getParam(5);
396     $mMime = $req->getParam(6);
397
398     if ($mTo->scalarval() == "") {
399         $err = "Error, no 'To' field specified";
400     }
401
402     if ($mFrom->scalarval() == "") {
403         $err = "Error, no 'From' field specified";
404     }
405
406     $msgHdr = "From: " . $mFrom->scalarval() . "\n";
407     $msgHdr .= "To: " . $mTo->scalarval() . "\n";
408
409     if ($mCc->scalarval() != "") {
410         $msgHdr .= "Cc: " . $mCc->scalarval() . "\n";
411     }
412     if ($mBcc->scalarval() != "") {
413         $msgHdr .= "Bcc: " . $mBcc->scalarval() . "\n";
414     }
415     if ($mMime->scalarval() != "") {
416         $msgHdr .= "Content-type: " . $mMime->scalarval() . "\n";
417     }
418     $msgHdr .= "X-Mailer: XML-RPC for PHP mailer 1.0";
419
420     if ($err == "") {
421         if (!mail("",
422             $mSub->scalarval(),
423             $mBody->scalarval(),
424             $msgHdr)
425         ) {
426             $err = "Error, could not send the mail.";
427         }
428     }
429
430     if ($err) {
431         return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
432     } else {
433         return new PhpXmlRpc\Response(new Value("true", Value::$xmlrpcBoolean));
434     }
435 }
436
437 $getallheaders_sig = array(array(Value::$xmlrpcStruct));
438 $getallheaders_doc = 'Returns a struct containing all the HTTP headers received with the request. Provides limited functionality with IIS';
439 function getAllHeaders_xmlrpc($req)
440 {
441     $encoder = new PhpXmlRpc\Encoder();
442
443     if (function_exists('getallheaders')) {
444         return new PhpXmlRpc\Response($encoder->encode(getallheaders()));
445     } else {
446         $headers = array();
447         // IIS: poor man's version of getallheaders
448         foreach ($_SERVER as $key => $val) {
449             if (strpos($key, 'HTTP_') === 0) {
450                 $key = ucfirst(str_replace('_', '-', strtolower(substr($key, 5))));
451                 $headers[$key] = $val;
452             }
453         }
454
455         return new PhpXmlRpc\Response($encoder->encode($headers));
456     }
457 }
458
459 $setcookies_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcStruct));
460 $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)';
461 function setCookies($req)
462 {
463     $encoder = new PhpXmlRpc\Encoder();
464     $cookies = $req->getParam(0);
465     foreach ($cookies as $name => $value) {
466         $cookieDesc = $encoder->decode($value);
467         setcookie($name, @$cookieDesc['value'], @$cookieDesc['expires'], @$cookieDesc['path'], @$cookieDesc['domain'], @$cookieDesc['secure']);
468     }
469
470     return new PhpXmlRpc\Response(new Value(1, 'int'));
471 }
472
473 $getcookies_sig = array(array(Value::$xmlrpcStruct));
474 $getcookies_doc = 'Sends to client a response containing all http cookies as received in the request (as struct)';
475 function getCookies($req)
476 {
477     $encoder = new PhpXmlRpc\Encoder();
478     return new PhpXmlRpc\Response($encoder->encode($_COOKIE));
479 }
480
481 $v1_arrayOfStructs_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcArray));
482 $v1_arrayOfStructs_doc = 'This handler takes a single parameter, an array of structs, each of which contains at least three elements named moe, larry and curly, all <i4>s. Your handler must add all the struct elements named curly and return the result.';
483 function v1_arrayOfStructs($req)
484 {
485     $sno = $req->getParam(0);
486     $numCurly = 0;
487     foreach ($sno as $str) {
488         foreach ($str as $key => $val) {
489             if ($key == "curly") {
490                 $numCurly += $val->scalarval();
491             }
492         }
493     }
494
495     return new PhpXmlRpc\Response(new Value($numCurly, "int"));
496 }
497
498 $v1_easyStruct_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcStruct));
499 $v1_easyStruct_doc = 'This handler takes a single parameter, a struct, containing at least three elements named moe, larry and curly, all &lt;i4&gt;s. Your handler must add the three numbers and return the result.';
500 function v1_easyStruct($req)
501 {
502     $sno = $req->getParam(0);
503     $moe = $sno->structmem("moe");
504     $larry = $sno->structmem("larry");
505     $curly = $sno->structmem("curly");
506     $num = $moe->scalarval() + $larry->scalarval() + $curly->scalarval();
507
508     return new PhpXmlRpc\Response(new Value($num, "int"));
509 }
510
511 $v1_echoStruct_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcStruct));
512 $v1_echoStruct_doc = 'This handler takes a single parameter, a struct. Your handler must return the struct.';
513 function v1_echoStruct($req)
514 {
515     $sno = $req->getParam(0);
516
517     return new PhpXmlRpc\Response($sno);
518 }
519
520 $v1_manyTypes_sig = array(array(
521     Value::$xmlrpcArray, Value::$xmlrpcInt, Value::$xmlrpcBoolean,
522     Value::$xmlrpcString, Value::$xmlrpcDouble, Value::$xmlrpcDateTime,
523     Value::$xmlrpcBase64,
524 ));
525 $v1_manyTypes_doc = 'This handler takes six parameters, and returns an array containing all the parameters.';
526 function v1_manyTypes($req)
527 {
528     return new PhpXmlRpc\Response(new Value(array(
529         $req->getParam(0),
530         $req->getParam(1),
531         $req->getParam(2),
532         $req->getParam(3),
533         $req->getParam(4),
534         $req->getParam(5),),
535         "array"
536     ));
537 }
538
539 $v1_moderateSizeArrayCheck_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcArray));
540 $v1_moderateSizeArrayCheck_doc = 'This handler takes a single parameter, which is an array containing between 100 and 200 elements. Each of the items is a string, your handler must return a string containing the concatenated text of the first and last elements.';
541 function v1_moderateSizeArrayCheck($req)
542 {
543     $ar = $req->getParam(0);
544     $sz = $ar->count();
545     //$first = $ar->arraymem(0);
546     $first = $ar[0];
547     //$last = $ar->arraymem($sz - 1);
548     $last = $ar[$sz - 1];
549
550     return new PhpXmlRpc\Response(new Value($first->scalarval() .
551         $last->scalarval(), "string"));
552 }
553
554 $v1_simpleStructReturn_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcInt));
555 $v1_simpleStructReturn_doc = 'This handler takes one parameter, and returns a struct containing three elements, times10, times100 and times1000, the result of multiplying the number by 10, 100 and 1000.';
556 function v1_simpleStructReturn($req)
557 {
558     $sno = $req->getParam(0);
559     $v = $sno->scalarval();
560
561     return new PhpXmlRpc\Response(new Value(array(
562         "times10" => new Value($v * 10, "int"),
563         "times100" => new Value($v * 100, "int"),
564         "times1000" => new Value($v * 1000, "int"),),
565         "struct"
566     ));
567 }
568
569 $v1_nestedStruct_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcStruct));
570 $v1_nestedStruct_doc = 'This handler takes a single parameter, a struct, that models a daily calendar. At the top level, there is one struct for each year. Each year is broken down into months, and months into days. Most of the days are empty in the struct you receive, but the entry for April 1, 2000 contains a least three elements named moe, larry and curly, all &lt;i4&gt;s. Your handler must add the three numbers and return the result.';
571 function v1_nestedStruct($req)
572 {
573     $sno = $req->getParam(0);
574
575     $twoK = $sno->structmem("2000");
576     $april = $twoK->structmem("04");
577     $fools = $april->structmem("01");
578     $curly = $fools->structmem("curly");
579     $larry = $fools->structmem("larry");
580     $moe = $fools->structmem("moe");
581
582     return new PhpXmlRpc\Response(new Value($curly->scalarval() + $larry->scalarval() + $moe->scalarval(), "int"));
583 }
584
585 $v1_countTheEntities_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcString));
586 $v1_countTheEntities_doc = 'This handler takes a single parameter, a string, that contains any number of predefined entities, namely &lt;, &gt;, &amp; \' and ".<BR>Your handler must return a struct that contains five fields, all numbers: ctLeftAngleBrackets, ctRightAngleBrackets, ctAmpersands, ctApostrophes, ctQuotes.';
587 function v1_countTheEntities($req)
588 {
589     $sno = $req->getParam(0);
590     $str = $sno->scalarval();
591     $gt = 0;
592     $lt = 0;
593     $ap = 0;
594     $qu = 0;
595     $amp = 0;
596     for ($i = 0; $i < strlen($str); $i++) {
597         $c = substr($str, $i, 1);
598         switch ($c) {
599             case ">":
600                 $gt++;
601                 break;
602             case "<":
603                 $lt++;
604                 break;
605             case "\"":
606                 $qu++;
607                 break;
608             case "'":
609                 $ap++;
610                 break;
611             case "&":
612                 $amp++;
613                 break;
614             default:
615                 break;
616         }
617     }
618
619     return new PhpXmlRpc\Response(new Value(array(
620         "ctLeftAngleBrackets" => new Value($lt, "int"),
621         "ctRightAngleBrackets" => new Value($gt, "int"),
622         "ctAmpersands" => new Value($amp, "int"),
623         "ctApostrophes" => new Value($ap, "int"),
624         "ctQuotes" => new Value($qu, "int"),),
625         "struct"
626     ));
627 }
628
629 // trivial interop tests
630 // http://www.xmlrpc.com/stories/storyReader$1636
631
632 $i_echoString_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcString));
633 $i_echoString_doc = "Echoes string.";
634
635 $i_echoStringArray_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray));
636 $i_echoStringArray_doc = "Echoes string array.";
637
638 $i_echoInteger_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcInt));
639 $i_echoInteger_doc = "Echoes integer.";
640
641 $i_echoIntegerArray_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray));
642 $i_echoIntegerArray_doc = "Echoes integer array.";
643
644 $i_echoFloat_sig = array(array(Value::$xmlrpcDouble, Value::$xmlrpcDouble));
645 $i_echoFloat_doc = "Echoes float.";
646
647 $i_echoFloatArray_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray));
648 $i_echoFloatArray_doc = "Echoes float array.";
649
650 $i_echoStruct_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcStruct));
651 $i_echoStruct_doc = "Echoes struct.";
652
653 $i_echoStructArray_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray));
654 $i_echoStructArray_doc = "Echoes struct array.";
655
656 $i_echoValue_doc = "Echoes any value back.";
657 $i_echoValue_sig = array(array(Value::$xmlrpcValue, Value::$xmlrpcValue));
658
659 $i_echoBase64_sig = array(array(Value::$xmlrpcBase64, Value::$xmlrpcBase64));
660 $i_echoBase64_doc = "Echoes base64.";
661
662 $i_echoDate_sig = array(array(Value::$xmlrpcDateTime, Value::$xmlrpcDateTime));
663 $i_echoDate_doc = "Echoes dateTime.";
664
665 function i_echoParam($req)
666 {
667     $s = $req->getParam(0);
668
669     return new PhpXmlRpc\Response($s);
670 }
671
672 function i_echoString($req)
673 {
674     return i_echoParam($req);
675 }
676
677 function i_echoInteger($req)
678 {
679     return i_echoParam($req);
680 }
681
682 function i_echoFloat($req)
683 {
684     return i_echoParam($req);
685 }
686
687 function i_echoStruct($req)
688 {
689     return i_echoParam($req);
690 }
691
692 function i_echoStringArray($req)
693 {
694     return i_echoParam($req);
695 }
696
697 function i_echoIntegerArray($req)
698 {
699     return i_echoParam($req);
700 }
701
702 function i_echoFloatArray($req)
703 {
704     return i_echoParam($req);
705 }
706
707 function i_echoStructArray($req)
708 {
709     return i_echoParam($req);
710 }
711
712 function i_echoValue($req)
713 {
714     return i_echoParam($req);
715 }
716
717 function i_echoBase64($req)
718 {
719     return i_echoParam($req);
720 }
721
722 function i_echoDate($req)
723 {
724     return i_echoParam($req);
725 }
726
727 $i_whichToolkit_sig = array(array(Value::$xmlrpcStruct));
728 $i_whichToolkit_doc = "Returns a struct containing the following strings: toolkitDocsUrl, toolkitName, toolkitVersion, toolkitOperatingSystem.";
729
730 function i_whichToolkit($req)
731 {
732     global $SERVER_SOFTWARE;
733     $ret = array(
734         "toolkitDocsUrl" => "http://phpxmlrpc.sourceforge.net/",
735         "toolkitName" => PhpXmlRpc\PhpXmlRpc::$xmlrpcName,
736         "toolkitVersion" => PhpXmlRpc\PhpXmlRpc::$xmlrpcVersion,
737         "toolkitOperatingSystem" => isset($SERVER_SOFTWARE) ? $SERVER_SOFTWARE : $_SERVER['SERVER_SOFTWARE'],
738     );
739
740     $encoder = new PhpXmlRpc\Encoder();
741     return new PhpXmlRpc\Response($encoder->encode($ret));
742 }
743
744 $object = new xmlrpcServerMethodsContainer();
745 $signatures = array(
746     "examples.getStateName" => array(
747         "function" => "findState",
748         "signature" => $findstate_sig,
749         "docstring" => $findstate_doc,
750     ),
751     "examples.sortByAge" => array(
752         "function" => "ageSorter",
753         "signature" => $agesorter_sig,
754         "docstring" => $agesorter_doc,
755     ),
756     "examples.addtwo" => array(
757         "function" => "addTwo",
758         "signature" => $addtwo_sig,
759         "docstring" => $addtwo_doc,
760     ),
761     "examples.addtwodouble" => array(
762         "function" => "addTwoDouble",
763         "signature" => $addtwodouble_sig,
764         "docstring" => $addtwodouble_doc,
765     ),
766     "examples.stringecho" => array(
767         "function" => "stringEcho",
768         "signature" => $stringecho_sig,
769         "docstring" => $stringecho_doc,
770     ),
771     "examples.echo" => array(
772         "function" => "echoBack",
773         "signature" => $echoback_sig,
774         "docstring" => $echoback_doc,
775     ),
776     "examples.decode64" => array(
777         "function" => "echoSixtyFour",
778         "signature" => $echosixtyfour_sig,
779         "docstring" => $echosixtyfour_doc,
780     ),
781     "examples.invertBooleans" => array(
782         "function" => "bitFlipper",
783         "signature" => $bitflipper_sig,
784         "docstring" => $bitflipper_doc,
785     ),
786     // signature omitted on purpose
787     "tests.generatePHPWarning" => array(
788         "function" => array($object, "phpWarningGenerator"),
789     ),
790     // signature omitted on purpose
791     "tests.raiseException" => array(
792         "function" => array($object, "exceptionGenerator"),
793     ),
794     // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
795     // NB: we can only register this when setting internal encoding to UTF-8, or it will break system.listMethods
796     "tests.utf8methodname." . 'κόσμε' => array(
797         "function" => "stringEcho",
798         "signature" => $stringecho_sig,
799         "docstring" => $stringecho_doc,
800     ),
801     /*"tests.iso88591methodname." . chr(224) . chr(252) . chr(232) => array(
802         "function" => "stringEcho",
803         "signature" => $stringecho_sig,
804         "docstring" => $stringecho_doc,
805     ),*/
806     "examples.getallheaders" => array(
807         "function" => 'getAllHeaders_xmlrpc',
808         "signature" => $getallheaders_sig,
809         "docstring" => $getallheaders_doc,
810     ),
811     "examples.setcookies" => array(
812         "function" => 'setCookies',
813         "signature" => $setcookies_sig,
814         "docstring" => $setcookies_doc,
815     ),
816     "examples.getcookies" => array(
817         "function" => 'getCookies',
818         "signature" => $getcookies_sig,
819         "docstring" => $getcookies_doc,
820     ),
821     "mail.send" => array(
822         "function" => "mailSend",
823         "signature" => $mailsend_sig,
824         "docstring" => $mailsend_doc,
825     ),
826     "validator1.arrayOfStructsTest" => array(
827         "function" => "v1_arrayOfStructs",
828         "signature" => $v1_arrayOfStructs_sig,
829         "docstring" => $v1_arrayOfStructs_doc,
830     ),
831     "validator1.easyStructTest" => array(
832         "function" => "v1_easyStruct",
833         "signature" => $v1_easyStruct_sig,
834         "docstring" => $v1_easyStruct_doc,
835     ),
836     "validator1.echoStructTest" => array(
837         "function" => "v1_echoStruct",
838         "signature" => $v1_echoStruct_sig,
839         "docstring" => $v1_echoStruct_doc,
840     ),
841     "validator1.manyTypesTest" => array(
842         "function" => "v1_manyTypes",
843         "signature" => $v1_manyTypes_sig,
844         "docstring" => $v1_manyTypes_doc,
845     ),
846     "validator1.moderateSizeArrayCheck" => array(
847         "function" => "v1_moderateSizeArrayCheck",
848         "signature" => $v1_moderateSizeArrayCheck_sig,
849         "docstring" => $v1_moderateSizeArrayCheck_doc,
850     ),
851     "validator1.simpleStructReturnTest" => array(
852         "function" => "v1_simpleStructReturn",
853         "signature" => $v1_simpleStructReturn_sig,
854         "docstring" => $v1_simpleStructReturn_doc,
855     ),
856     "validator1.nestedStructTest" => array(
857         "function" => "v1_nestedStruct",
858         "signature" => $v1_nestedStruct_sig,
859         "docstring" => $v1_nestedStruct_doc,
860     ),
861     "validator1.countTheEntities" => array(
862         "function" => "v1_countTheEntities",
863         "signature" => $v1_countTheEntities_sig,
864         "docstring" => $v1_countTheEntities_doc,
865     ),
866     "interopEchoTests.echoString" => array(
867         "function" => "i_echoString",
868         "signature" => $i_echoString_sig,
869         "docstring" => $i_echoString_doc,
870     ),
871     "interopEchoTests.echoStringArray" => array(
872         "function" => "i_echoStringArray",
873         "signature" => $i_echoStringArray_sig,
874         "docstring" => $i_echoStringArray_doc,
875     ),
876     "interopEchoTests.echoInteger" => array(
877         "function" => "i_echoInteger",
878         "signature" => $i_echoInteger_sig,
879         "docstring" => $i_echoInteger_doc,
880     ),
881     "interopEchoTests.echoIntegerArray" => array(
882         "function" => "i_echoIntegerArray",
883         "signature" => $i_echoIntegerArray_sig,
884         "docstring" => $i_echoIntegerArray_doc,
885     ),
886     "interopEchoTests.echoFloat" => array(
887         "function" => "i_echoFloat",
888         "signature" => $i_echoFloat_sig,
889         "docstring" => $i_echoFloat_doc,
890     ),
891     "interopEchoTests.echoFloatArray" => array(
892         "function" => "i_echoFloatArray",
893         "signature" => $i_echoFloatArray_sig,
894         "docstring" => $i_echoFloatArray_doc,
895     ),
896     "interopEchoTests.echoStruct" => array(
897         "function" => "i_echoStruct",
898         "signature" => $i_echoStruct_sig,
899         "docstring" => $i_echoStruct_doc,
900     ),
901     "interopEchoTests.echoStructArray" => array(
902         "function" => "i_echoStructArray",
903         "signature" => $i_echoStructArray_sig,
904         "docstring" => $i_echoStructArray_doc,
905     ),
906     "interopEchoTests.echoValue" => array(
907         "function" => "i_echoValue",
908         "signature" => $i_echoValue_sig,
909         "docstring" => $i_echoValue_doc,
910     ),
911     "interopEchoTests.echoBase64" => array(
912         "function" => "i_echoBase64",
913         "signature" => $i_echoBase64_sig,
914         "docstring" => $i_echoBase64_doc,
915     ),
916     "interopEchoTests.echoDate" => array(
917         "function" => "i_echoDate",
918         "signature" => $i_echoDate_sig,
919         "docstring" => $i_echoDate_doc,
920     ),
921     "interopEchoTests.whichToolkit" => array(
922         "function" => "i_whichToolkit",
923         "signature" => $i_whichToolkit_sig,
924         "docstring" => $i_whichToolkit_doc,
925     ),
926
927     'tests.getStateName.2' => $findstate2_sig,
928     'tests.getStateName.3' => $findstate3_sig,
929     'tests.getStateName.4' => $findstate4_sig,
930     'tests.getStateName.5' => $findstate5_sig,
931     'tests.getStateName.6' => $findstate6_sig,
932     'tests.getStateName.7' => $findstate7_sig,
933     'tests.getStateName.8' => $findstate8_sig,
934     'tests.getStateName.9' => $findstate9_sig,
935     'tests.getStateName.10' => $findstate10_sig,
936     'tests.getStateName.11' => $findstate11_sig,
937
938     'tests.getStateName.12' => array(
939         "function" => "findStateWithNulls",
940         "signature" => $findstate12_sig,
941         "docstring" => $findstate_doc,
942     ),
943
944     'tests.returnPhpObject' => $returnObj_sig,
945 );
946
947 $signatures = array_merge($signatures, $moreSignatures);
948
949 // enable support for the NULL extension
950 PhpXmlRpc\PhpXmlRpc::$xmlrpc_null_extension = true;
951
952 $s = new PhpXmlRpc\Server($signatures, false);
953 $s->setdebug(3);
954 $s->compress_response = true;
955
956 // out-of-band information: let the client manipulate the server operations.
957 // we do this to help the testsuite script: do not reproduce in production!
958 if (isset($_GET['RESPONSE_ENCODING'])) {
959     $s->response_charset_encoding = $_GET['RESPONSE_ENCODING'];
960 }
961 if (isset($_GET['DETECT_ENCODINGS'])) {
962     PhpXmlRpc\PhpXmlRpc::$xmlrpc_detectencodings = $_GET['DETECT_ENCODINGS'];
963 }
964 if (isset($_GET['EXCEPTION_HANDLING'])) {
965     $s->exception_handling = $_GET['EXCEPTION_HANDLING'];
966 }
967 $s->service();
968 // that should do all we need!
969
970 // out-of-band information: let the client manipulate the server operations.
971 // we do this to help the testsuite script: do not reproduce in production!
972 if (isset($_COOKIE['PHPUNIT_SELENIUM_TEST_ID']) && extension_loaded('xdebug')) {
973     include_once __DIR__ . "/../../vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/SeleniumCommon/append.php";
974 }