nitpicks
[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 interoperability testing (validator1 methods),
6  * and some whose only purpose is to be used for unit-testing the library.
7  *
8  * Please _do not_ copy this file verbatim into your production server.
9  **/
10
11 require_once __DIR__ . "/_prepend.php";
12
13 use PhpXmlRpc\PhpXmlRpc;
14 use PhpXmlRpc\Response;
15 use PhpXmlRpc\Server;
16 use PhpXmlRpc\Value;
17
18 // Most of the code used to implement the webservices, and their signatures, are stowed away in neatly organized
19 // files, each demoing a different topic
20
21 // The simplest way of implementing webservices: as xmlrpc-aware global functions
22 $signatures1 = include(__DIR__.'/methodProviders/functions.php');
23
24 // Examples of exposing as webservices php functions and objects/methods which are not aware of xmlrpc classes
25 $signatures2 = include(__DIR__.'/methodProviders/wrapper.php');
26
27 // Definitions of webservices used for interoperability testing
28 $signatures3 = include(__DIR__.'/methodProviders/interop.php');
29 $signatures4 = include(__DIR__.'/methodProviders/validator1.php');
30
31 // And finally a few examples inline
32
33 // used to test signatures with NULL params
34 $findstate12_sig = array(
35     array(Value::$xmlrpcString, Value::$xmlrpcInt, Value::$xmlrpcNull),
36     array(Value::$xmlrpcString, Value::$xmlrpcNull, Value::$xmlrpcInt),
37 );
38 function findStateWithNulls($req)
39 {
40     $a = $req->getParam(0);
41     $b = $req->getParam(1);
42
43     if ($a->scalartyp() == Value::$xmlrpcNull)
44         return new Response(new Value(plain_findstate($b->scalarval())));
45     else
46         return new Response(new Value(plain_findstate($a->scalarval())));
47 }
48
49 $object = new xmlrpcServerMethodsContainer();
50
51 $signatures = array(
52
53     // signature omitted on purpose
54     "tests.generatePHPWarning" => array(
55         "function" => array($object, "phpWarningGenerator"),
56     ),
57     // signature omitted on purpose
58     "tests.raiseException" => array(
59         "function" => array($object, "exceptionGenerator"),
60     ),
61     // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
62     // NB: we can only register this when setting internal encoding to UTF-8, or it will break system.listMethods
63     "tests.utf8methodname." . 'κόσμε' => array(
64         "function" => "stringEcho",
65         "signature" => $stringecho_sig,
66         "docstring" => $stringecho_doc,
67     ),
68     /*"tests.iso88591methodname." . chr(224) . chr(252) . chr(232) => array(
69         "function" => "stringEcho",
70         "signature" => $stringecho_sig,
71         "docstring" => $stringecho_doc,
72     ),*/
73
74     'tests.getStateName.12' => array(
75         "function" => "findStateWithNulls",
76         "signature" => $findstate12_sig,
77         "docstring" => $findstate_doc,
78     ),
79 );
80
81 $signatures = array_merge($signatures, $signatures1, $signatures2, $signatures3, $signatures4);
82
83 // Enable support for the NULL extension
84 PhpXmlRpc::$xmlrpc_null_extension = true;
85
86 $s = new Server($signatures, false);
87 $s->setDebug(3);
88 $s->compress_response = true;
89
90 // Out-of-band information: let the client manipulate the server operations.
91 // We do this to help the testsuite script: do not reproduce in production!
92 if (isset($_GET['RESPONSE_ENCODING'])) {
93     $s->response_charset_encoding = $_GET['RESPONSE_ENCODING'];
94 }
95 if (isset($_GET['DETECT_ENCODINGS'])) {
96     PhpXmlRpc::$xmlrpc_detectencodings = $_GET['DETECT_ENCODINGS'];
97 }
98 if (isset($_GET['EXCEPTION_HANDLING'])) {
99     $s->exception_handling = $_GET['EXCEPTION_HANDLING'];
100 }
101 if (isset($_GET['FORCE_AUTH'])) {
102     // We implement both  Basic and Digest auth in php to avoid having to set it up in a vhost.
103     // Code taken from php.net
104     // NB: we do NOT check for valid credentials!
105     if ($_GET['FORCE_AUTH'] == 'Basic') {
106         if (!isset($_SERVER['PHP_AUTH_USER']) && !isset($_SERVER['REMOTE_USER']) && !isset($_SERVER['REDIRECT_REMOTE_USER'])) {
107             header('HTTP/1.0 401 Unauthorized');
108             header('WWW-Authenticate: Basic realm="Phpxmlrpc Basic Realm"');
109             die('Text visible if user hits Cancel button');
110         }
111     } elseif ($_GET['FORCE_AUTH'] == 'Digest') {
112         if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
113             header('HTTP/1.1 401 Unauthorized');
114             header('WWW-Authenticate: Digest realm="Phpxmlrpc Digest Realm",qop="auth",nonce="'.uniqid().'",opaque="'.md5('Phpxmlrpc Digest Realm').'"');
115             die('Text visible if user hits Cancel button');
116         }
117     }
118 }
119
120 $s->service();
121 // That should do all we need!
122
123 require_once __DIR__ . "/_append.php";