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  LOCALSERVER
8  * @param string  URI
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     public static function getArgs()
22     {
23         $args = array(
24             'DEBUG' => 0,
25             'LOCALSERVER' => 'localhost',
26             'HTTPSSERVER' => 'gggeek.altervista.org',
27             'HTTPSURI' => '/sw/xmlrpc/demo/server/server.php',
28             'HTTPSIGNOREPEER' => false,
29             'HTTPSVERIFYHOST' => 2,
30             'SSLVERSION' => 0,
31             'PROXYSERVER' => null,
32             'LOCALPATH' => __DIR__,
33         );
34
35         // check for command line (env vars) vs. web page input params
36         if (!isset($_SERVER['REQUEST_METHOD'])) {
37             foreach($_SERVER as $key => $val) {
38                 if (array_key_exists($key, $args)) {
39                     $$key = $val;
40                 }
41             }
42         } else {
43             // NB: we might as well consider using $_GET stuff later on...
44             extract($_GET);
45             extract($_POST);
46         }
47
48         if (isset($DEBUG)) {
49             $args['DEBUG'] = intval($DEBUG);
50         }
51         if (isset($LOCALSERVER)) {
52             $args['LOCALSERVER'] = $LOCALSERVER;
53         } else {
54             if (isset($HTTP_HOST)) {
55                 $args['LOCALSERVER'] = $HTTP_HOST;
56             } elseif (isset($_SERVER['HTTP_HOST'])) {
57                 $args['LOCALSERVER'] = $_SERVER['HTTP_HOST'];
58             }
59         }
60         if (isset($HTTPSSERVER)) {
61             $args['HTTPSSERVER'] = $HTTPSSERVER;
62         }
63         if (isset($HTTPSURI)) {
64             $args['HTTPSURI'] = $HTTPSURI;
65         }
66         if (isset($HTTPSIGNOREPEER)) {
67             $args['HTTPSIGNOREPEER'] = (bool)$HTTPSIGNOREPEER;
68         }
69         if (isset($HTTPSVERIFYHOST)) {
70             $args['HTTPSVERIFYHOST'] = (int)$HTTPSVERIFYHOST;
71         }
72         if (isset($SSLVERSION)) {
73             $args['SSLVERSION'] = (int)$SSLVERSION;
74         }
75         if (isset($PROXYSERVER)) {
76             $arr = explode(':', $PROXYSERVER);
77             $args['PROXYSERVER'] = $arr[0];
78             if (count($arr) > 1) {
79                 $args['PROXYPORT'] = $arr[1];
80             } else {
81                 $args['PROXYPORT'] = 8080;
82             }
83         }
84         if (!isset($URI)) {
85             // GUESTIMATE the url of local demo server
86             // play nice to php 3 and 4-5 in retrieving URL of server.php
87             /// @todo filter out query string from REQUEST_URI
88             if (isset($REQUEST_URI)) {
89                 $URI = str_replace('/tests/testsuite.php', '/demo/server/server.php', $REQUEST_URI);
90                 $URI = str_replace('/testsuite.php', '/server.php', $URI);
91                 $URI = str_replace('/tests/benchmark.php', '/demo/server/server.php', $URI);
92                 $URI = str_replace('/benchmark.php', '/server.php', $URI);
93             } elseif (isset($_SERVER['PHP_SELF']) && isset($_SERVER['REQUEST_METHOD'])) {
94                 $URI = str_replace('/tests/testsuite.php', '/demo/server/server.php', $_SERVER['PHP_SELF']);
95                 $URI = str_replace('/testsuite.php', '/server.php', $URI);
96                 $URI = str_replace('/tests/benchmark.php', '/demo/server/server.php', $URI);
97                 $URI = str_replace('/benchmark.php', '/server.php', $URI);
98             } else {
99                 $URI = '/demo/server/server.php';
100             }
101         }
102         if ($URI[0] != '/') {
103             $URI = '/' . $URI;
104         }
105         $args['URI'] = $URI;
106         if (isset($LOCALPATH)) {
107             $args['LOCALPATH'] = $LOCALPATH;
108         }
109
110         return $args;
111     }
112 }