wip revive and split off doc generatin toolchain
[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-2021 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 4 and 5 in retrieving URL of server.php
76             /// @todo filter out query string from REQUEST_URI
77             /// @todo review this code...
78             if (isset($REQUEST_URI)) {
79                 $HTTPURI = str_replace('/tests/testsuite.php', '/demo/server/server.php', $REQUEST_URI);
80                 $HTTPURI = str_replace('/testsuite.php', '/server.php', $HTTPURI);
81                 $HTTPURI = str_replace('/extras/benchmark.php', '/demo/server/server.php', $HTTPURI);
82                 $HTTPURI = str_replace('/benchmark.php', '/server.php', $HTTPURI);
83             } elseif (isset($_SERVER['PHP_SELF']) && isset($_SERVER['REQUEST_METHOD'])) {
84                 $HTTPURI = str_replace('/tests/testsuite.php', '/demo/server/server.php', $_SERVER['PHP_SELF']);
85                 $HTTPURI = str_replace('/testsuite.php', '/server.php', $HTTPURI);
86                 $HTTPURI = str_replace('/extras/benchmark.php', '/demo/server/server.php', $HTTPURI);
87                 $HTTPURI = str_replace('/benchmark.php', '/server.php', $HTTPURI);
88             } else {
89                 $HTTPURI = '/demo/server/server.php';
90             }
91         }
92         if ($HTTPURI[0] != '/') {
93             $HTTPURI = '/' . $HTTPURI;
94         }
95         $args['HTTPURI'] = $HTTPURI;
96
97         if (isset($HTTPSSERVER)) {
98             $args['HTTPSSERVER'] = $HTTPSSERVER;
99         }
100
101         /// @todo if $HTTPSURI is unset, and HTTPSSERVER == localhost, use HTTPURI
102         if (isset($HTTPSURI)) {
103             $args['HTTPSURI'] = $HTTPSURI;
104         }
105
106         if (isset($HTTPSIGNOREPEER)) {
107             $args['HTTPSIGNOREPEER'] = (bool)$HTTPSIGNOREPEER;
108         }
109
110         if (isset($HTTPSVERIFYHOST)) {
111             $args['HTTPSVERIFYHOST'] = (int)$HTTPSVERIFYHOST;
112         }
113
114         if (isset($SSLVERSION)) {
115             $args['SSLVERSION'] = (int)$SSLVERSION;
116         }
117
118         if (isset($PROXYSERVER)) {
119             $arr = explode(':', $PROXYSERVER);
120             $args['PROXYSERVER'] = $arr[0];
121             if (count($arr) > 1) {
122                 $args['PROXYPORT'] = $arr[1];
123             } else {
124                 $args['PROXYPORT'] = 8080;
125             }
126         }
127
128         //if (isset($LOCALPATH)) {
129         //    $args['LOCALPATH'] = $LOCALPATH;
130         //}
131
132         return $args;
133     }
134 }