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