Remove usages of 'each', as it is deprecated in php 7.2
[plcapi.git] / demo / server / server.php
index efc25bc..1af2554 100644 (file)
@@ -37,15 +37,19 @@ class xmlrpcServerMethodsContainer
 {
     /**
      * Method used to test logging of php warnings generated by user functions.
+     * @param PhpXmlRpc\Request $req
+     * @return PhpXmlRpc\Response
      */
     public function phpWarningGenerator($req)
     {
         $a = $undefinedVariable; // this triggers a warning in E_ALL mode, since $undefinedVariable is undefined
-        return new PhpXmlRpc\Response(new Value(1, 'boolean'));
+        return new PhpXmlRpc\Response(new Value(1, Value::$xmlrpcBoolean));
     }
 
     /**
      * Method used to test catching of exceptions in the server.
+     * @param PhpXmlRpc\Request $req
+     * @throws Exception
      */
     public function exceptionGenerator($req)
     {
@@ -53,14 +57,36 @@ class xmlrpcServerMethodsContainer
     }
 
     /**
-    * A PHP version of the state-number server. Send me an integer and i'll sell you a state
-    * @param integer $num
-    * @return string
-    */
+     * @param string $msg
+     */
+    public function debugMessageGenerator($msg)
+    {
+        PhpXmlRpc\Server::xmlrpc_debugmsg($msg);
+    }
+
+    /**
+     * A PHP version of the state-number server. Send me an integer and i'll sell you a state.
+     * Used to test wrapping of PHP methods into xmlrpc methods.
+     *
+     * @param integer $num
+     * @return string
+     * @throws Exception
+     */
     public static function findState($num)
     {
         return inner_findstate($num);
     }
+
+    /**
+     * Returns an instance of stdClass.
+     * Used to test wrapping of PHP objects with class preservation
+     */
+    public function returnObject()
+    {
+        $obj = new stdClass();
+        $obj->hello = 'world';
+        return $obj;
+    }
 }
 
 // a PHP version of the state-number server
@@ -114,11 +140,13 @@ function findState($req)
 
 /**
  * Inner code of the state-number server.
- * Used to test auto-registration of PHP functions as xmlrpc methods.
+ * Used to test wrapping of PHP functions into xmlrpc methods.
  *
  * @param integer $stateNo the state number
  *
  * @return string the name of the state (or error description)
+ *
+ * @throws Exception if state is not found
  */
 function inner_findstate($stateNo)
 {
@@ -128,25 +156,64 @@ function inner_findstate($stateNo)
         return $stateNames[$stateNo - 1];
     } else {
         // not, there so complain
-        return "I don't have a state for the index '" . $stateNo . "'";
+        throw new Exception("I don't have a state for the index '" . $stateNo . "'", PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser);
     }
 }
 
-$findStateClosure = function ($req)
-{
-    return findState($req);
-};
-
 $wrapper = new PhpXmlRpc\Wrapper();
 
-$findstate2_sig = $wrapper->wrap_php_function('inner_findstate');
+$findstate2_sig = $wrapper->wrapPhpFunction('inner_findstate');
+
+$findstate3_sig = $wrapper->wrapPhpFunction(array('xmlrpcServerMethodsContainer', 'findState'));
+
+$obj = new xmlrpcServerMethodsContainer();
+$findstate4_sig = $wrapper->wrapPhpFunction(array($obj, 'findstate'));
+
+$findstate5_sig = $wrapper->wrapPhpFunction('xmlrpcServerMethodsContainer::findState', '', array('return_source' => true));
+eval($findstate5_sig['source']);
 
-$findstate3_sig = $wrapper->wrap_php_function(array('xmlrpcServerMethodsContainer', 'findState'));
+$findstate6_sig = $wrapper->wrapPhpFunction('inner_findstate', '', array('return_source' => true));
+eval($findstate6_sig['source']);
 
-$findstate5_sig = $wrapper->wrap_php_function('xmlrpcServerMethodsContainer::findState');
+$findstate7_sig = $wrapper->wrapPhpFunction(array('xmlrpcServerMethodsContainer', 'findState'), '', array('return_source' => true));
+eval($findstate7_sig['source']);
 
 $obj = new xmlrpcServerMethodsContainer();
-$findstate4_sig = $wrapper->wrap_php_function(array($obj, 'findstate'));
+$findstate8_sig = $wrapper->wrapPhpFunction(array($obj, 'findstate'), '', array('return_source' => true));
+eval($findstate8_sig['source']);
+
+$findstate9_sig = $wrapper->wrapPhpFunction('xmlrpcServerMethodsContainer::findState', '', array('return_source' => true));
+eval($findstate9_sig['source']);
+
+$findstate10_sig = array(
+    "function" => function ($req) { return findState($req); },
+    "signature" => $findstate_sig,
+    "docstring" => $findstate_doc,
+);
+
+$findstate11_sig = $wrapper->wrapPhpFunction(function ($stateNo) { return inner_findstate($stateNo); });
+
+$c = new xmlrpcServerMethodsContainer;
+$moreSignatures = $wrapper->wrapPhpClass($c, array('prefix' => 'tests.', 'method_type' => 'all'));
+
+$returnObj_sig =  $wrapper->wrapPhpFunction(array($c, 'returnObject'), '', array('encode_php_objs' => true));
+
+// used to test signatures with NULL params
+$findstate12_sig = array(
+    array(Value::$xmlrpcString, Value::$xmlrpcInt, Value::$xmlrpcNull),
+    array(Value::$xmlrpcString, Value::$xmlrpcNull, Value::$xmlrpcInt),
+);
+
+function findStateWithNulls($req)
+{
+    $a = $req->getParam(0);
+    $b = $req->getParam(1);
+
+    if ($a->scalartyp() == Value::$xmlrpcNull)
+        return new PhpXmlRpc\Response(new Value(inner_findstate($b->scalarval())));
+    else
+        return new PhpXmlRpc\Response(new Value(inner_findstate($a->scalarval())));
+}
 
 $addtwo_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcInt, Value::$xmlrpcInt));
 $addtwo_doc = 'Add two integers together and return the result';
@@ -155,7 +222,7 @@ function addTwo($req)
     $s = $req->getParam(0);
     $t = $req->getParam(1);
 
-    return new PhpXmlRpc\Response(new Value($s->scalarval() + $t->scalarval(), "int"));
+    return new PhpXmlRpc\Response(new Value($s->scalarval() + $t->scalarval(), Value::$xmlrpcInt));
 }
 
 $addtwodouble_sig = array(array(Value::$xmlrpcDouble, Value::$xmlrpcDouble, Value::$xmlrpcDouble));
@@ -165,7 +232,7 @@ function addTwoDouble($req)
     $s = $req->getParam(0);
     $t = $req->getParam(1);
 
-    return new PhpXmlRpc\Response(new Value($s->scalarval() + $t->scalarval(), "double"));
+    return new PhpXmlRpc\Response(new Value($s->scalarval() + $t->scalarval(), Value::$xmlrpcDouble));
 }
 
 $stringecho_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcString));
@@ -194,7 +261,7 @@ function echoSixtyFour($req)
     // This is to test that base64 encoding is working as expected
     $incoming = $req->getParam(0);
 
-    return new PhpXmlRpc\Response(new Value($incoming->scalarval(), "string"));
+    return new PhpXmlRpc\Response(new Value($incoming->scalarval(), Value::$xmlrpcString));
 }
 
 $bitflipper_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray));
@@ -202,15 +269,13 @@ $bitflipper_doc = 'Accepts an array of booleans, and returns them inverted';
 function bitFlipper($req)
 {
     $v = $req->getParam(0);
-    $sz = $v->arraysize();
     $rv = new Value(array(), Value::$xmlrpcArray);
 
-    for ($j = 0; $j < $sz; $j++) {
-        $b = $v->arraymem($j);
+    foreach ($v as $b) {
         if ($b->scalarval()) {
-            $rv->addScalar(false, "boolean");
+            $rv[] = new Value(false, Value::$xmlrpcBoolean);
         } else {
-            $rv->addScalar(true, "boolean");
+            $rv[] = new Value(true, Value::$xmlrpcBoolean);
         }
     }
 
@@ -262,38 +327,40 @@ function ageSorter($req)
     $sno = $req->getParam(0);
     // error string for [if|when] things go wrong
     $err = "";
-    // create the output value
-    $v = new Value();
     $agar = array();
 
-    $max = $sno->arraysize();
+    $max = $sno->count();
     PhpXmlRpc\Server::xmlrpc_debugmsg("Found $max array elements");
-    for ($i = 0; $i < $max; $i++) {
-        $rec = $sno->arraymem($i);
+    foreach ($sno as $i => $rec) {
         if ($rec->kindOf() != "struct") {
             $err = "Found non-struct in array at element $i";
             break;
         }
         // extract name and age from struct
-        $n = $rec->structmem("name");
-        $a = $rec->structmem("age");
+        $n = $rec["name"];
+        $a = $rec["age"];
         // $n and $a are xmlrpcvals,
         // so get the scalarval from them
         $agar[$n->scalarval()] = $a->scalarval();
     }
 
+    // create the output value
+    $v = new Value(array(), Value::$xmlrpcArray);
+
     $agesorter_arr = $agar;
     // hack, must make global as uksort() won't
     // allow us to pass any other auxiliary information
     uksort($agesorter_arr, 'agesorter_compare');
-    $outAr = array();
-    while (list($key, $val) = each($agesorter_arr)) {
+    foreach($agesorter_arr as $key => $val) {
         // recreate each struct element
-        $outAr[] = new Value(array("name" => new Value($key),
-            "age" => new Value($val, "int"),), "struct");
+        $v[] = new Value(
+            array(
+                "name" => new Value($key),
+                "age" => new Value($val, "int")
+            ),
+            Value::$xmlrpcStruct
+        );
     }
-    // add this array to the output value
-    $v->addArray($outAr);
 
     if ($err) {
         return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
@@ -302,8 +369,7 @@ function ageSorter($req)
     }
 }
 
-// signature and instructions, place these in the dispatch
-// map
+// signature and instructions, place these in the dispatch map
 $mailsend_sig = array(array(
     Value::$xmlrpcBoolean, Value::$xmlrpcString, Value::$xmlrpcString,
     Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString,
@@ -367,7 +433,7 @@ function mailSend($req)
     if ($err) {
         return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
     } else {
-        return new PhpXmlRpc\Response(new Value("true", Value::$xmlrpcBoolean));
+        return new PhpXmlRpc\Response(new Value(true, Value::$xmlrpcBoolean));
     }
 }
 
@@ -398,13 +464,13 @@ $setcookies_doc = 'Sends to client a response containing a single \'1\' digit, a
 function setCookies($req)
 {
     $encoder = new PhpXmlRpc\Encoder();
-    $m = $req->getParam(0);
-    while (list($name, $value) = $m->structeach()) {
+    $cookies = $req->getParam(0);
+    foreach ($cookies as $name => $value) {
         $cookieDesc = $encoder->decode($value);
         setcookie($name, @$cookieDesc['value'], @$cookieDesc['expires'], @$cookieDesc['path'], @$cookieDesc['domain'], @$cookieDesc['secure']);
     }
 
-    return new PhpXmlRpc\Response(new Value(1, 'int'));
+    return new PhpXmlRpc\Response(new Value(1, Value::$xmlrpcInt));
 }
 
 $getcookies_sig = array(array(Value::$xmlrpcStruct));
@@ -421,17 +487,15 @@ function v1_arrayOfStructs($req)
 {
     $sno = $req->getParam(0);
     $numCurly = 0;
-    for ($i = 0; $i < $sno->arraysize(); $i++) {
-        $str = $sno->arraymem($i);
-        $str->structreset();
-        while (list($key, $val) = $str->structeach()) {
+    foreach ($sno as $str) {
+        foreach ($str as $key => $val) {
             if ($key == "curly") {
                 $numCurly += $val->scalarval();
             }
         }
     }
 
-    return new PhpXmlRpc\Response(new Value($numCurly, "int"));
+    return new PhpXmlRpc\Response(new Value($numCurly, Value::$xmlrpcInt));
 }
 
 $v1_easyStruct_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcStruct));
@@ -439,12 +503,12 @@ $v1_easyStruct_doc = 'This handler takes a single parameter, a struct, containin
 function v1_easyStruct($req)
 {
     $sno = $req->getParam(0);
-    $moe = $sno->structmem("moe");
-    $larry = $sno->structmem("larry");
-    $curly = $sno->structmem("curly");
+    $moe = $sno["moe"];
+    $larry = $sno["larry"];
+    $curly = $sno["curly"];
     $num = $moe->scalarval() + $larry->scalarval() + $curly->scalarval();
 
-    return new PhpXmlRpc\Response(new Value($num, "int"));
+    return new PhpXmlRpc\Response(new Value($num, Value::$xmlrpcInt));
 }
 
 $v1_echoStruct_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcStruct));
@@ -464,14 +528,16 @@ $v1_manyTypes_sig = array(array(
 $v1_manyTypes_doc = 'This handler takes six parameters, and returns an array containing all the parameters.';
 function v1_manyTypes($req)
 {
-    return new PhpXmlRpc\Response(new Value(array(
-        $req->getParam(0),
-        $req->getParam(1),
-        $req->getParam(2),
-        $req->getParam(3),
-        $req->getParam(4),
-        $req->getParam(5),),
-        "array"
+    return new PhpXmlRpc\Response(new Value(
+        array(
+            $req->getParam(0),
+            $req->getParam(1),
+            $req->getParam(2),
+            $req->getParam(3),
+            $req->getParam(4),
+            $req->getParam(5)
+        ),
+        Value::$xmlrpcArray
     ));
 }
 
@@ -480,12 +546,12 @@ $v1_moderateSizeArrayCheck_doc = 'This handler takes a single parameter, which i
 function v1_moderateSizeArrayCheck($req)
 {
     $ar = $req->getParam(0);
-    $sz = $ar->arraysize();
-    $first = $ar->arraymem(0);
-    $last = $ar->arraymem($sz - 1);
+    $sz = $ar->count();
+    $first = $ar[0];
+    $last = $ar[$sz - 1];
 
     return new PhpXmlRpc\Response(new Value($first->scalarval() .
-        $last->scalarval(), "string"));
+        $last->scalarval(), Value::$xmlrpcString));
 }
 
 $v1_simpleStructReturn_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcInt));
@@ -495,11 +561,13 @@ function v1_simpleStructReturn($req)
     $sno = $req->getParam(0);
     $v = $sno->scalarval();
 
-    return new PhpXmlRpc\Response(new Value(array(
-        "times10" => new Value($v * 10, "int"),
-        "times100" => new Value($v * 100, "int"),
-        "times1000" => new Value($v * 1000, "int"),),
-        "struct"
+    return new PhpXmlRpc\Response(new Value(
+        array(
+            "times10" => new Value($v * 10, Value::$xmlrpcInt),
+            "times100" => new Value($v * 100, Value::$xmlrpcInt),
+            "times1000" => new Value($v * 1000, Value::$xmlrpcInt)
+        ),
+        Value::$xmlrpcStruct
     ));
 }
 
@@ -509,14 +577,14 @@ function v1_nestedStruct($req)
 {
     $sno = $req->getParam(0);
 
-    $twoK = $sno->structmem("2000");
-    $april = $twoK->structmem("04");
-    $fools = $april->structmem("01");
-    $curly = $fools->structmem("curly");
-    $larry = $fools->structmem("larry");
-    $moe = $fools->structmem("moe");
+    $twoK = $sno["2000"];
+    $april = $twoK["04"];
+    $fools = $april["01"];
+    $curly = $fools["curly"];
+    $larry = $fools["larry"];
+    $moe = $fools["moe"];
 
-    return new PhpXmlRpc\Response(new Value($curly->scalarval() + $larry->scalarval() + $moe->scalarval(), "int"));
+    return new PhpXmlRpc\Response(new Value($curly->scalarval() + $larry->scalarval() + $moe->scalarval(), Value::$xmlrpcInt));
 }
 
 $v1_countTheEntities_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcString));
@@ -553,13 +621,15 @@ function v1_countTheEntities($req)
         }
     }
 
-    return new PhpXmlRpc\Response(new Value(array(
-        "ctLeftAngleBrackets" => new Value($lt, "int"),
-        "ctRightAngleBrackets" => new Value($gt, "int"),
-        "ctAmpersands" => new Value($amp, "int"),
-        "ctApostrophes" => new Value($ap, "int"),
-        "ctQuotes" => new Value($qu, "int"),),
-        "struct"
+    return new PhpXmlRpc\Response(new Value(
+        array(
+            "ctLeftAngleBrackets" => new Value($lt, Value::$xmlrpcInt),
+            "ctRightAngleBrackets" => new Value($gt, Value::$xmlrpcInt),
+            "ctAmpersands" => new Value($amp, Value::$xmlrpcInt),
+            "ctApostrophes" => new Value($ap, Value::$xmlrpcInt),
+            "ctQuotes" => new Value($qu, Value::$xmlrpcInt)
+        ),
+        Value::$xmlrpcStruct
     ));
 }
 
@@ -860,29 +930,31 @@ $signatures = array(
         "signature" => $i_whichToolkit_sig,
         "docstring" => $i_whichToolkit_doc,
     ),
-);
 
-if ($findstate2_sig) {
-    $signatures['examples.php.getStateName'] = $findstate2_sig;
-}
-
-if ($findstate3_sig) {
-    $signatures['examples.php2.getStateName'] = $findstate3_sig;
-}
+    'tests.getStateName.2' => $findstate2_sig,
+    'tests.getStateName.3' => $findstate3_sig,
+    'tests.getStateName.4' => $findstate4_sig,
+    'tests.getStateName.5' => $findstate5_sig,
+    'tests.getStateName.6' => $findstate6_sig,
+    'tests.getStateName.7' => $findstate7_sig,
+    'tests.getStateName.8' => $findstate8_sig,
+    'tests.getStateName.9' => $findstate9_sig,
+    'tests.getStateName.10' => $findstate10_sig,
+    'tests.getStateName.11' => $findstate11_sig,
+
+    'tests.getStateName.12' => array(
+        "function" => "findStateWithNulls",
+        "signature" => $findstate12_sig,
+        "docstring" => $findstate_doc,
+    ),
 
-if ($findstate4_sig) {
-    $signatures['examples.php3.getStateName'] = $findstate4_sig;
-}
+    'tests.returnPhpObject' => $returnObj_sig,
+);
 
-if ($findstate5_sig) {
-    $signatures['examples.php4.getStateName'] = $findstate5_sig;
-}
+$signatures = array_merge($signatures, $moreSignatures);
 
-$signatures['examples.php5.getStateName'] = array(
-    "function" => $findStateClosure,
-    "signature" => $findstate_sig,
-    "docstring" => $findstate_doc,
-);
+// enable support for the NULL extension
+PhpXmlRpc\PhpXmlRpc::$xmlrpc_null_extension = true;
 
 $s = new PhpXmlRpc\Server($signatures, false);
 $s->setdebug(3);
@@ -893,6 +965,9 @@ $s->compress_response = true;
 if (isset($_GET['RESPONSE_ENCODING'])) {
     $s->response_charset_encoding = $_GET['RESPONSE_ENCODING'];
 }
+if (isset($_GET['DETECT_ENCODINGS'])) {
+    PhpXmlRpc\PhpXmlRpc::$xmlrpc_detectencodings = $_GET['DETECT_ENCODINGS'];
+}
 if (isset($_GET['EXCEPTION_HANDLING'])) {
     $s->exception_handling = $_GET['EXCEPTION_HANDLING'];
 }