Minor cleanup in variable names
[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     $sz = $v->arraysize();
273     $rv = new Value(array(), Value::$xmlrpcArray);
274
275     for ($j = 0; $j < $sz; $j++) {
276         $b = $v->arraymem($j);
277         if ($b->scalarval()) {
278             $rv->addScalar(false, "boolean");
279         } else {
280             $rv->addScalar(true, "boolean");
281         }
282     }
283
284     return new PhpXmlRpc\Response($rv);
285 }
286
287 // Sorting demo
288 //
289 // send me an array of structs thus:
290 //
291 // Dave 35
292 // Edd  45
293 // Fred 23
294 // Barney 37
295 //
296 // and I'll return it to you in sorted order
297
298 function agesorter_compare($a, $b)
299 {
300     global $agesorter_arr;
301
302     // don't even ask me _why_ these come padded with hyphens, I couldn't tell you :p
303     $a = str_replace("-", "", $a);
304     $b = str_replace("-", "", $b);
305
306     if ($agesorter_arr[$a] == $agesorter_arr[$b]) {
307         return 0;
308     }
309
310     return ($agesorter_arr[$a] > $agesorter_arr[$b]) ? -1 : 1;
311 }
312
313 $agesorter_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray));
314 $agesorter_doc = 'Send this method an array of [string, int] structs, eg:
315 <pre>
316  Dave   35
317  Edd    45
318  Fred   23
319  Barney 37
320 </pre>
321 And the array will be returned with the entries sorted by their numbers.
322 ';
323 function ageSorter($req)
324 {
325     global $agesorter_arr, $s;
326
327     PhpXmlRpc\Server::xmlrpc_debugmsg("Entering 'agesorter'");
328     // get the parameter
329     $sno = $req->getParam(0);
330     // error string for [if|when] things go wrong
331     $err = "";
332     // create the output value
333     $v = new Value();
334     $agar = array();
335
336     $max = $sno->arraysize();
337     PhpXmlRpc\Server::xmlrpc_debugmsg("Found $max array elements");
338     for ($i = 0; $i < $max; $i++) {
339         $rec = $sno->arraymem($i);
340         if ($rec->kindOf() != "struct") {
341             $err = "Found non-struct in array at element $i";
342             break;
343         }
344         // extract name and age from struct
345         $n = $rec->structmem("name");
346         $a = $rec->structmem("age");
347         // $n and $a are xmlrpcvals,
348         // so get the scalarval from them
349         $agar[$n->scalarval()] = $a->scalarval();
350     }
351
352     $agesorter_arr = $agar;
353     // hack, must make global as uksort() won't
354     // allow us to pass any other auxiliary information
355     uksort($agesorter_arr, 'agesorter_compare');
356     $outAr = array();
357     while (list($key, $val) = each($agesorter_arr)) {
358         // recreate each struct element
359         $outAr[] = new Value(array("name" => new Value($key),
360             "age" => new Value($val, "int"),), "struct");
361     }
362     // add this array to the output value
363     $v->addArray($outAr);
364
365     if ($err) {
366         return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
367     } else {
368         return new PhpXmlRpc\Response($v);
369     }
370 }
371
372 // signature and instructions, place these in the dispatch map
373 $mailsend_sig = array(array(
374     Value::$xmlrpcBoolean, Value::$xmlrpcString, Value::$xmlrpcString,
375     Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString,
376     Value::$xmlrpcString, Value::$xmlrpcString,
377 ));
378 $mailsend_doc = 'mail.send(recipient, subject, text, sender, cc, bcc, mimetype)<br/>
379 recipient, cc, and bcc are strings, comma-separated lists of email addresses, as described above.<br/>
380 subject is a string, the subject of the message.<br/>
381 sender is a string, it\'s the email address of the person sending the message. This string can not be
382 a comma-separated list, it must contain a single email address only.<br/>
383 text is a string, it contains the body of the message.<br/>
384 mimetype, a string, is a standard MIME type, for example, text/plain.
385 ';
386 // WARNING; this functionality depends on the sendmail -t option
387 // it may not work with Windows machines properly; particularly
388 // the Bcc option. Sneak on your friends at your own risk!
389 function mailSend($req)
390 {
391     $err = "";
392
393     $mTo = $req->getParam(0);
394     $mSub = $req->getParam(1);
395     $mBody = $req->getParam(2);
396     $mFrom = $req->getParam(3);
397     $mCc = $req->getParam(4);
398     $mBcc = $req->getParam(5);
399     $mMime = $req->getParam(6);
400
401     if ($mTo->scalarval() == "") {
402         $err = "Error, no 'To' field specified";
403     }
404
405     if ($mFrom->scalarval() == "") {
406         $err = "Error, no 'From' field specified";
407     }
408
409     $msgHdr = "From: " . $mFrom->scalarval() . "\n";
410     $msgHdr .= "To: " . $mTo->scalarval() . "\n";
411
412     if ($mCc->scalarval() != "") {
413         $msgHdr .= "Cc: " . $mCc->scalarval() . "\n";
414     }
415     if ($mBcc->scalarval() != "") {
416         $msgHdr .= "Bcc: " . $mBcc->scalarval() . "\n";
417     }
418     if ($mMime->scalarval() != "") {
419         $msgHdr .= "Content-type: " . $mMime->scalarval() . "\n";
420     }
421     $msgHdr .= "X-Mailer: XML-RPC for PHP mailer 1.0";
422
423     if ($err == "") {
424         if (!mail("",
425             $mSub->scalarval(),
426             $mBody->scalarval(),
427             $msgHdr)
428         ) {
429             $err = "Error, could not send the mail.";
430         }
431     }
432
433     if ($err) {
434         return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
435     } else {
436         return new PhpXmlRpc\Response(new Value("true", Value::$xmlrpcBoolean));
437     }
438 }
439
440 $getallheaders_sig = array(array(Value::$xmlrpcStruct));
441 $getallheaders_doc = 'Returns a struct containing all the HTTP headers received with the request. Provides limited functionality with IIS';
442 function getAllHeaders_xmlrpc($req)
443 {
444     $encoder = new PhpXmlRpc\Encoder();
445
446     if (function_exists('getallheaders')) {
447         return new PhpXmlRpc\Response($encoder->encode(getallheaders()));
448     } else {
449         $headers = array();
450         // IIS: poor man's version of getallheaders
451         foreach ($_SERVER as $key => $val) {
452             if (strpos($key, 'HTTP_') === 0) {
453                 $key = ucfirst(str_replace('_', '-', strtolower(substr($key, 5))));
454                 $headers[$key] = $val;
455             }
456         }
457
458         return new PhpXmlRpc\Response($encoder->encode($headers));
459     }
460 }
461
462 $setcookies_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcStruct));
463 $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)';
464 function setCookies($req)
465 {
466     $encoder = new PhpXmlRpc\Encoder();
467     $cookies = $req->getParam(0);
468     while (list($name, $value) = $cookies->structeach()) {
469         $cookieDesc = $encoder->decode($value);
470         setcookie($name, @$cookieDesc['value'], @$cookieDesc['expires'], @$cookieDesc['path'], @$cookieDesc['domain'], @$cookieDesc['secure']);
471     }
472
473     return new PhpXmlRpc\Response(new Value(1, 'int'));
474 }
475
476 $getcookies_sig = array(array(Value::$xmlrpcStruct));
477 $getcookies_doc = 'Sends to client a response containing all http cookies as received in the request (as struct)';
478 function getCookies($req)
479 {
480     $encoder = new PhpXmlRpc\Encoder();
481     return new PhpXmlRpc\Response($encoder->encode($_COOKIE));
482 }
483
484 $v1_arrayOfStructs_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcArray));
485 $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.';
486 function v1_arrayOfStructs($req)
487 {
488     $sno = $req->getParam(0);
489     $numCurly = 0;
490     for ($i = 0; $i < $sno->arraysize(); $i++) {
491         $str = $sno->arraymem($i);
492         $str->structreset();
493         while (list($key, $val) = $str->structeach()) {
494             if ($key == "curly") {
495                 $numCurly += $val->scalarval();
496             }
497         }
498     }
499
500     return new PhpXmlRpc\Response(new Value($numCurly, "int"));
501 }
502
503 $v1_easyStruct_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcStruct));
504 $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.';
505 function v1_easyStruct($req)
506 {
507     $sno = $req->getParam(0);
508     $moe = $sno->structmem("moe");
509     $larry = $sno->structmem("larry");
510     $curly = $sno->structmem("curly");
511     $num = $moe->scalarval() + $larry->scalarval() + $curly->scalarval();
512
513     return new PhpXmlRpc\Response(new Value($num, "int"));
514 }
515
516 $v1_echoStruct_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcStruct));
517 $v1_echoStruct_doc = 'This handler takes a single parameter, a struct. Your handler must return the struct.';
518 function v1_echoStruct($req)
519 {
520     $sno = $req->getParam(0);
521
522     return new PhpXmlRpc\Response($sno);
523 }
524
525 $v1_manyTypes_sig = array(array(
526     Value::$xmlrpcArray, Value::$xmlrpcInt, Value::$xmlrpcBoolean,
527     Value::$xmlrpcString, Value::$xmlrpcDouble, Value::$xmlrpcDateTime,
528     Value::$xmlrpcBase64,
529 ));
530 $v1_manyTypes_doc = 'This handler takes six parameters, and returns an array containing all the parameters.';
531 function v1_manyTypes($req)
532 {
533     return new PhpXmlRpc\Response(new Value(array(
534         $req->getParam(0),
535         $req->getParam(1),
536         $req->getParam(2),
537         $req->getParam(3),
538         $req->getParam(4),
539         $req->getParam(5),),
540         "array"
541     ));
542 }
543
544 $v1_moderateSizeArrayCheck_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcArray));
545 $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.';
546 function v1_moderateSizeArrayCheck($req)
547 {
548     $ar = $req->getParam(0);
549     $sz = $ar->arraysize();
550     $first = $ar->arraymem(0);
551     $last = $ar->arraymem($sz - 1);
552
553     return new PhpXmlRpc\Response(new Value($first->scalarval() .
554         $last->scalarval(), "string"));
555 }
556
557 $v1_simpleStructReturn_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcInt));
558 $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.';
559 function v1_simpleStructReturn($req)
560 {
561     $sno = $req->getParam(0);
562     $v = $sno->scalarval();
563
564     return new PhpXmlRpc\Response(new Value(array(
565         "times10" => new Value($v * 10, "int"),
566         "times100" => new Value($v * 100, "int"),
567         "times1000" => new Value($v * 1000, "int"),),
568         "struct"
569     ));
570 }
571
572 $v1_nestedStruct_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcStruct));
573 $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.';
574 function v1_nestedStruct($req)
575 {
576     $sno = $req->getParam(0);
577
578     $twoK = $sno->structmem("2000");
579     $april = $twoK->structmem("04");
580     $fools = $april->structmem("01");
581     $curly = $fools->structmem("curly");
582     $larry = $fools->structmem("larry");
583     $moe = $fools->structmem("moe");
584
585     return new PhpXmlRpc\Response(new Value($curly->scalarval() + $larry->scalarval() + $moe->scalarval(), "int"));
586 }
587
588 $v1_countTheEntities_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcString));
589 $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.';
590 function v1_countTheEntities($req)
591 {
592     $sno = $req->getParam(0);
593     $str = $sno->scalarval();
594     $gt = 0;
595     $lt = 0;
596     $ap = 0;
597     $qu = 0;
598     $amp = 0;
599     for ($i = 0; $i < strlen($str); $i++) {
600         $c = substr($str, $i, 1);
601         switch ($c) {
602             case ">":
603                 $gt++;
604                 break;
605             case "<":
606                 $lt++;
607                 break;
608             case "\"":
609                 $qu++;
610                 break;
611             case "'":
612                 $ap++;
613                 break;
614             case "&":
615                 $amp++;
616                 break;
617             default:
618                 break;
619         }
620     }
621
622     return new PhpXmlRpc\Response(new Value(array(
623         "ctLeftAngleBrackets" => new Value($lt, "int"),
624         "ctRightAngleBrackets" => new Value($gt, "int"),
625         "ctAmpersands" => new Value($amp, "int"),
626         "ctApostrophes" => new Value($ap, "int"),
627         "ctQuotes" => new Value($qu, "int"),),
628         "struct"
629     ));
630 }
631
632 // trivial interop tests
633 // http://www.xmlrpc.com/stories/storyReader$1636
634
635 $i_echoString_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcString));
636 $i_echoString_doc = "Echoes string.";
637
638 $i_echoStringArray_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray));
639 $i_echoStringArray_doc = "Echoes string array.";
640
641 $i_echoInteger_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcInt));
642 $i_echoInteger_doc = "Echoes integer.";
643
644 $i_echoIntegerArray_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray));
645 $i_echoIntegerArray_doc = "Echoes integer array.";
646
647 $i_echoFloat_sig = array(array(Value::$xmlrpcDouble, Value::$xmlrpcDouble));
648 $i_echoFloat_doc = "Echoes float.";
649
650 $i_echoFloatArray_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray));
651 $i_echoFloatArray_doc = "Echoes float array.";
652
653 $i_echoStruct_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcStruct));
654 $i_echoStruct_doc = "Echoes struct.";
655
656 $i_echoStructArray_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray));
657 $i_echoStructArray_doc = "Echoes struct array.";
658
659 $i_echoValue_doc = "Echoes any value back.";
660 $i_echoValue_sig = array(array(Value::$xmlrpcValue, Value::$xmlrpcValue));
661
662 $i_echoBase64_sig = array(array(Value::$xmlrpcBase64, Value::$xmlrpcBase64));
663 $i_echoBase64_doc = "Echoes base64.";
664
665 $i_echoDate_sig = array(array(Value::$xmlrpcDateTime, Value::$xmlrpcDateTime));
666 $i_echoDate_doc = "Echoes dateTime.";
667
668 function i_echoParam($req)
669 {
670     $s = $req->getParam(0);
671
672     return new PhpXmlRpc\Response($s);
673 }
674
675 function i_echoString($req)
676 {
677     return i_echoParam($req);
678 }
679
680 function i_echoInteger($req)
681 {
682     return i_echoParam($req);
683 }
684
685 function i_echoFloat($req)
686 {
687     return i_echoParam($req);
688 }
689
690 function i_echoStruct($req)
691 {
692     return i_echoParam($req);
693 }
694
695 function i_echoStringArray($req)
696 {
697     return i_echoParam($req);
698 }
699
700 function i_echoIntegerArray($req)
701 {
702     return i_echoParam($req);
703 }
704
705 function i_echoFloatArray($req)
706 {
707     return i_echoParam($req);
708 }
709
710 function i_echoStructArray($req)
711 {
712     return i_echoParam($req);
713 }
714
715 function i_echoValue($req)
716 {
717     return i_echoParam($req);
718 }
719
720 function i_echoBase64($req)
721 {
722     return i_echoParam($req);
723 }
724
725 function i_echoDate($req)
726 {
727     return i_echoParam($req);
728 }
729
730 $i_whichToolkit_sig = array(array(Value::$xmlrpcStruct));
731 $i_whichToolkit_doc = "Returns a struct containing the following strings: toolkitDocsUrl, toolkitName, toolkitVersion, toolkitOperatingSystem.";
732
733 function i_whichToolkit($req)
734 {
735     global $SERVER_SOFTWARE;
736     $ret = array(
737         "toolkitDocsUrl" => "http://phpxmlrpc.sourceforge.net/",
738         "toolkitName" => PhpXmlRpc\PhpXmlRpc::$xmlrpcName,
739         "toolkitVersion" => PhpXmlRpc\PhpXmlRpc::$xmlrpcVersion,
740         "toolkitOperatingSystem" => isset($SERVER_SOFTWARE) ? $SERVER_SOFTWARE : $_SERVER['SERVER_SOFTWARE'],
741     );
742
743     $encoder = new PhpXmlRpc\Encoder();
744     return new PhpXmlRpc\Response($encoder->encode($ret));
745 }
746
747 $object = new xmlrpcServerMethodsContainer();
748 $signatures = array(
749     "examples.getStateName" => array(
750         "function" => "findState",
751         "signature" => $findstate_sig,
752         "docstring" => $findstate_doc,
753     ),
754     "examples.sortByAge" => array(
755         "function" => "ageSorter",
756         "signature" => $agesorter_sig,
757         "docstring" => $agesorter_doc,
758     ),
759     "examples.addtwo" => array(
760         "function" => "addTwo",
761         "signature" => $addtwo_sig,
762         "docstring" => $addtwo_doc,
763     ),
764     "examples.addtwodouble" => array(
765         "function" => "addTwoDouble",
766         "signature" => $addtwodouble_sig,
767         "docstring" => $addtwodouble_doc,
768     ),
769     "examples.stringecho" => array(
770         "function" => "stringEcho",
771         "signature" => $stringecho_sig,
772         "docstring" => $stringecho_doc,
773     ),
774     "examples.echo" => array(
775         "function" => "echoBack",
776         "signature" => $echoback_sig,
777         "docstring" => $echoback_doc,
778     ),
779     "examples.decode64" => array(
780         "function" => "echoSixtyFour",
781         "signature" => $echosixtyfour_sig,
782         "docstring" => $echosixtyfour_doc,
783     ),
784     "examples.invertBooleans" => array(
785         "function" => "bitFlipper",
786         "signature" => $bitflipper_sig,
787         "docstring" => $bitflipper_doc,
788     ),
789     // signature omitted on purpose
790     "tests.generatePHPWarning" => array(
791         "function" => array($object, "phpWarningGenerator"),
792     ),
793     // signature omitted on purpose
794     "tests.raiseException" => array(
795         "function" => array($object, "exceptionGenerator"),
796     ),
797     // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
798     // NB: we can only register this when setting internal encoding to UTF-8, or it will break system.listMethods
799     "tests.utf8methodname." . 'κόσμε' => array(
800         "function" => "stringEcho",
801         "signature" => $stringecho_sig,
802         "docstring" => $stringecho_doc,
803     ),
804     /*"tests.iso88591methodname." . chr(224) . chr(252) . chr(232) => array(
805         "function" => "stringEcho",
806         "signature" => $stringecho_sig,
807         "docstring" => $stringecho_doc,
808     ),*/
809     "examples.getallheaders" => array(
810         "function" => 'getAllHeaders_xmlrpc',
811         "signature" => $getallheaders_sig,
812         "docstring" => $getallheaders_doc,
813     ),
814     "examples.setcookies" => array(
815         "function" => 'setCookies',
816         "signature" => $setcookies_sig,
817         "docstring" => $setcookies_doc,
818     ),
819     "examples.getcookies" => array(
820         "function" => 'getCookies',
821         "signature" => $getcookies_sig,
822         "docstring" => $getcookies_doc,
823     ),
824     "mail.send" => array(
825         "function" => "mailSend",
826         "signature" => $mailsend_sig,
827         "docstring" => $mailsend_doc,
828     ),
829     "validator1.arrayOfStructsTest" => array(
830         "function" => "v1_arrayOfStructs",
831         "signature" => $v1_arrayOfStructs_sig,
832         "docstring" => $v1_arrayOfStructs_doc,
833     ),
834     "validator1.easyStructTest" => array(
835         "function" => "v1_easyStruct",
836         "signature" => $v1_easyStruct_sig,
837         "docstring" => $v1_easyStruct_doc,
838     ),
839     "validator1.echoStructTest" => array(
840         "function" => "v1_echoStruct",
841         "signature" => $v1_echoStruct_sig,
842         "docstring" => $v1_echoStruct_doc,
843     ),
844     "validator1.manyTypesTest" => array(
845         "function" => "v1_manyTypes",
846         "signature" => $v1_manyTypes_sig,
847         "docstring" => $v1_manyTypes_doc,
848     ),
849     "validator1.moderateSizeArrayCheck" => array(
850         "function" => "v1_moderateSizeArrayCheck",
851         "signature" => $v1_moderateSizeArrayCheck_sig,
852         "docstring" => $v1_moderateSizeArrayCheck_doc,
853     ),
854     "validator1.simpleStructReturnTest" => array(
855         "function" => "v1_simpleStructReturn",
856         "signature" => $v1_simpleStructReturn_sig,
857         "docstring" => $v1_simpleStructReturn_doc,
858     ),
859     "validator1.nestedStructTest" => array(
860         "function" => "v1_nestedStruct",
861         "signature" => $v1_nestedStruct_sig,
862         "docstring" => $v1_nestedStruct_doc,
863     ),
864     "validator1.countTheEntities" => array(
865         "function" => "v1_countTheEntities",
866         "signature" => $v1_countTheEntities_sig,
867         "docstring" => $v1_countTheEntities_doc,
868     ),
869     "interopEchoTests.echoString" => array(
870         "function" => "i_echoString",
871         "signature" => $i_echoString_sig,
872         "docstring" => $i_echoString_doc,
873     ),
874     "interopEchoTests.echoStringArray" => array(
875         "function" => "i_echoStringArray",
876         "signature" => $i_echoStringArray_sig,
877         "docstring" => $i_echoStringArray_doc,
878     ),
879     "interopEchoTests.echoInteger" => array(
880         "function" => "i_echoInteger",
881         "signature" => $i_echoInteger_sig,
882         "docstring" => $i_echoInteger_doc,
883     ),
884     "interopEchoTests.echoIntegerArray" => array(
885         "function" => "i_echoIntegerArray",
886         "signature" => $i_echoIntegerArray_sig,
887         "docstring" => $i_echoIntegerArray_doc,
888     ),
889     "interopEchoTests.echoFloat" => array(
890         "function" => "i_echoFloat",
891         "signature" => $i_echoFloat_sig,
892         "docstring" => $i_echoFloat_doc,
893     ),
894     "interopEchoTests.echoFloatArray" => array(
895         "function" => "i_echoFloatArray",
896         "signature" => $i_echoFloatArray_sig,
897         "docstring" => $i_echoFloatArray_doc,
898     ),
899     "interopEchoTests.echoStruct" => array(
900         "function" => "i_echoStruct",
901         "signature" => $i_echoStruct_sig,
902         "docstring" => $i_echoStruct_doc,
903     ),
904     "interopEchoTests.echoStructArray" => array(
905         "function" => "i_echoStructArray",
906         "signature" => $i_echoStructArray_sig,
907         "docstring" => $i_echoStructArray_doc,
908     ),
909     "interopEchoTests.echoValue" => array(
910         "function" => "i_echoValue",
911         "signature" => $i_echoValue_sig,
912         "docstring" => $i_echoValue_doc,
913     ),
914     "interopEchoTests.echoBase64" => array(
915         "function" => "i_echoBase64",
916         "signature" => $i_echoBase64_sig,
917         "docstring" => $i_echoBase64_doc,
918     ),
919     "interopEchoTests.echoDate" => array(
920         "function" => "i_echoDate",
921         "signature" => $i_echoDate_sig,
922         "docstring" => $i_echoDate_doc,
923     ),
924     "interopEchoTests.whichToolkit" => array(
925         "function" => "i_whichToolkit",
926         "signature" => $i_whichToolkit_sig,
927         "docstring" => $i_whichToolkit_doc,
928     ),
929
930     'tests.getStateName.2' => $findstate2_sig,
931     'tests.getStateName.3' => $findstate3_sig,
932     'tests.getStateName.4' => $findstate4_sig,
933     'tests.getStateName.5' => $findstate5_sig,
934     'tests.getStateName.6' => $findstate6_sig,
935     'tests.getStateName.7' => $findstate7_sig,
936     'tests.getStateName.8' => $findstate8_sig,
937     'tests.getStateName.9' => $findstate9_sig,
938     'tests.getStateName.10' => $findstate10_sig,
939     'tests.getStateName.11' => $findstate11_sig,
940
941     'tests.getStateName.12' => array(
942         "function" => "findStateWithNulls",
943         "signature" => $findstate12_sig,
944         "docstring" => $findstate_doc,
945     ),
946
947     'tests.returnPhpObject' => $returnObj_sig,
948 );
949
950 $signatures = array_merge($signatures, $moreSignatures);
951
952 // enable support for the NULL extension
953 PhpXmlRpc\PhpXmlRpc::$xmlrpc_null_extension = true;
954
955 $s = new PhpXmlRpc\Server($signatures, false);
956 $s->setdebug(3);
957 $s->compress_response = true;
958
959 // out-of-band information: let the client manipulate the server operations.
960 // we do this to help the testsuite script: do not reproduce in production!
961 if (isset($_GET['RESPONSE_ENCODING'])) {
962     $s->response_charset_encoding = $_GET['RESPONSE_ENCODING'];
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 }