argParser   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 50
c 3
b 0
f 0
dl 0
loc 113
rs 10
wmc 18

1 Method

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