d4e6624ddd6a08b5a696c9815a1b037afac0c723
[plcapi.git] / tests / parse_args.php
1 <?php
2
3 /**
4  * Common parameter parsing for benchmark and tests scripts.
5  *
6  * @param integer DEBUG
7  * @param string  HTTPSERVER
8  * @param string  HTTPURI
9  * @param string  HTTPSSERVER
10  * @param string  HTTPSURI
11  * @param bool    HTTPSIGNOREPEER
12  * @param int     HTTPSVERIFYHOST
13  * @param int     SSLVERSION
14  * @param string  PROXYSERVER
15  *
16  * @copyright (C) 2007-2020 G. Giunta
17  * @license code licensed under the BSD License: see file license.txt
18  *
19  * @todo rename both the class and the file. PhpXmlRpc_TestArgParser ?
20  **/
21 class argParser
22 {
23     /**
24      * @return array
25      */
26     public static function getArgs()
27     {
28         /// @todo should we prefix all test parameters with TESTS_ ?
29         $args = array(
30             'DEBUG' => 0,
31             'HTTPSERVER' => 'localhost',
32             'HTTPURI' => null,
33             // now that we run tests in Docker by default, with a webserver set up for https, let's default to it
34             'HTTPSSERVER' => 'localhost',
35             'HTTPSURI' => null,
36             // example alternative:
37             //'HTTPSSERVER' => 'gggeek.altervista.org',
38             //'HTTPSURI' => '/sw/xmlrpc/demo/server/server.php',
39             'HTTPSIGNOREPEER' => false,
40             'HTTPSVERIFYHOST' => 2,
41             'SSLVERSION' => 0,
42             'PROXYSERVER' => null,
43             //'LOCALPATH' => __DIR__,
44         );
45
46         // check for command line (env vars) vs. web page input params
47         if (!isset($_SERVER['REQUEST_METHOD'])) {
48             foreach($_SERVER as $key => $val) {
49                 if (array_key_exists($key, $args)) {
50                     $$key = $val;
51                 }
52             }
53         } else {
54             // NB: we might as well consider using $_GET stuff later on...
55             extract($_GET);
56             extract($_POST);
57         }
58
59         if (isset($DEBUG)) {
60             $args['DEBUG'] = intval($DEBUG);
61         }
62
63         if (isset($HTTPSERVER)) {
64             $args['HTTPSERVER'] = $HTTPSERVER;
65         } else {
66             if (isset($HTTP_HOST)) {
67                 $args['HTTPSERVER'] = $HTTP_HOST;
68             } elseif (isset($_SERVER['HTTP_HOST'])) {
69                 $args['HTTPSERVER'] = $_SERVER['HTTP_HOST'];
70             }
71         }
72
73         if (!isset($HTTPURI) || $HTTPURI == '') {
74             // GUESTIMATE the url of local demo server
75             // play nice to php 3 and 4-5 in retrieving URL of server.php
76             /// @todo filter out query string from REQUEST_URI
77             if (isset($REQUEST_URI)) {
78                 $HTTPURI = str_replace('/tests/testsuite.php', '/demo/server/server.php', $REQUEST_URI);
79                 $HTTPURI = str_replace('/testsuite.php', '/server.php', $HTTPURI);
80                 $HTTPURI = str_replace('/tests/benchmark.php', '/demo/server/server.php', $HTTPURI);
81                 $HTTPURI = str_replace('/benchmark.php', '/server.php', $HTTPURI);
82             } elseif (isset($_SERVER['PHP_SELF']) && isset($_SERVER['REQUEST_METHOD'])) {
83                 $HTTPURI = str_replace('/tests/testsuite.php', '/demo/server/server.php', $_SERVER['PHP_SELF']);
84                 $HTTPURI = str_replace('/testsuite.php', '/server.php', $HTTPURI);
85                 $HTTPURI = str_replace('/tests/benchmark.php', '/demo/server/server.php', $HTTPURI);
86                 $HTTPURI = str_replace('/benchmark.php', '/server.php', $HTTPURI);
87             } else {
88                 $HTTPURI = '/demo/server/server.php';
89             }
90         }
91         if ($HTTPURI[0] != '/') {
92             $HTTPURI = '/' . $HTTPURI;
93         }
94         $args['HTTPURI'] = $HTTPURI;
95
96         if (isset($HTTPSSERVER)) {
97             $args['HTTPSSERVER'] = $HTTPSSERVER;
98         }
99
100         /// @todo if $HTTPSURI is unset, and HTTPSSERVER == localhost, use HTTPURI
101         if (isset($HTTPSURI)) {
102             $args['HTTPSURI'] = $HTTPSURI;
103         }
104
105         if (isset($HTTPSIGNOREPEER)) {
106             $args['HTTPSIGNOREPEER'] = (bool)$HTTPSIGNOREPEER;
107         }
108
109         if (isset($HTTPSVERIFYHOST)) {
110             $args['HTTPSVERIFYHOST'] = (int)$HTTPSVERIFYHOST;
111         }
112
113         if (isset($SSLVERSION)) {
114             $args['SSLVERSION'] = (int)$SSLVERSION;
115         }
116
117         if (isset($PROXYSERVER)) {
118             $arr = explode(':', $PROXYSERVER);
119             $args['PROXYSERVER'] = $arr[0];
120             if (count($arr) > 1) {
121                 $args['PROXYPORT'] = $arr[1];
122             } else {
123                 $args['PROXYPORT'] = 8080;
124             }
125         }
126
127         //if (isset($LOCALPATH)) {
128         //    $args['LOCALPATH'] = $LOCALPATH;
129         //}
130
131         return $args;
132     }
133 }