WIP - more bugfixes and start of testsuite reimplementation
[plcapi.git] / tests / parse_args.php
1 <?php
2 /**
3  * Common parameter parsing for benchmarks and tests scripts
4  *
5  * @param integer DEBUG
6  * @param string  LOCALSERVER
7  * @param string  URI
8  * @param string  HTTPSSERVER
9  * @param string  HTTPSSURI
10  * @param string  PROXY
11  * @param string  NOPROXY
12  *
13  * @copyright (C) 2007-20014 G. Giunta
14  * @license code licensed under the BSD License: http://phpxmlrpc.sourceforge.net/license.txt
15  **/
16
17 class argParser
18 {
19     static public function getArgs()
20     {
21         global $argv;
22
23         $args = array(
24             'DEBUG' => 0,
25             'LOCALSERVER' => 'localhost',
26             'HTTPSSERVER' => 'gggeek.ssl.altervista.org',
27             'HTTPSURI' => '/sw/xmlrpc/demo/server/server.php',
28             'HTTPSIGNOREPEER' => false,
29             'PROXYSERVER' => null,
30             'NOPROXY' => false,
31             'LOCALPATH' => __DIR__
32         );
33
34         // check for command line vs web page input params
35         if(!isset($_SERVER['REQUEST_METHOD']))
36         {
37             if(isset($argv))
38             {
39                 foreach($argv as $param)
40                 {
41                     $param = explode('=', $param);
42                     if(count($param) > 1)
43                     {
44                         $pname = strtoupper(ltrim($param[0], '-'));
45                         $$pname=$param[1];
46                     }
47                 }
48             }
49         }
50         else
51         {
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         {
59             $args['DEBUG'] = intval($DEBUG);
60         }
61         if(isset($LOCALSERVER))
62         {
63             $args['LOCALSERVER'] = $LOCALSERVER;
64         }
65         else
66         {
67             if(isset($HTTP_HOST))
68             {
69                 $args['LOCALSERVER'] = $HTTP_HOST;
70             }
71             elseif(isset($_SERVER['HTTP_HOST']))
72             {
73                 $args['LOCALSERVER'] = $_SERVER['HTTP_HOST'];
74             }
75         }
76         if(isset($HTTPSSERVER))
77         {
78             $args['HTTPSSERVER'] = $HTTPSSERVER;
79         }
80         if(isset($HTTPSURI))
81         {
82             $args['HTTPSURI'] = $HTTPSURI;
83         }
84         if(isset($HTTPSIGNOREPEER))
85         {
86             $args['HTTPSIGNOREPEER'] = bool($HTTPSIGNOREPEER);
87         }
88         if(isset($PROXY))
89         {
90             $arr = explode(':', $PROXY);
91             $args['PROXYSERVER'] = $arr[0];
92             if(count($arr) > 1)
93             {
94                 $args['PROXYPORT'] = $arr[1];
95             }
96             else
97             {
98                 $args['PROXYPORT'] = 8080;
99             }
100         }
101         // used to silence testsuite warnings about proxy code not being tested
102         if(isset($NOPROXY))
103         {
104             $args['NOPROXY'] = true;
105         }
106         if(!isset($URI))
107         {
108             // GUESTIMATE the url of local demo server
109             // play nice to php 3 and 4-5 in retrieving URL of server.php
110             /// @todo filter out query string from REQUEST_URI
111             if(isset($REQUEST_URI))
112             {
113                 $URI = str_replace('/tests/testsuite.php', '/demo/server/server.php', $REQUEST_URI);
114                 $URI = str_replace('/testsuite.php', '/server.php', $URI);
115                 $URI = str_replace('/tests/benchmark.php', '/demo/server/server.php', $URI);
116                 $URI = str_replace('/benchmark.php', '/server.php', $URI);
117             }
118             elseif(isset($_SERVER['PHP_SELF']) && isset($_SERVER['REQUEST_METHOD']))
119             {
120                 $URI = str_replace('/tests/testsuite.php', '/demo/server/server.php', $_SERVER['PHP_SELF']);
121                 $URI = str_replace('/testsuite.php', '/server.php', $URI);
122                 $URI = str_replace('/tests/benchmark.php', '/demo/server/server.php', $URI);
123                 $URI = str_replace('/benchmark.php', '/server.php', $URI);
124             }
125             else
126             {
127                 $URI = '/demo/server/server.php';
128             }
129         }
130         if($URI[0] != '/')
131         {
132             $URI = '/'.$URI;
133         }
134         $args['URI'] = $URI;
135         if(isset($LOCALPATH))
136         {
137             $args['LOCALPATH'] =$LOCALPATH;
138         }
139
140         return $args;
141     }
142
143 }