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