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