Issues (243)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Request/CRequestBasic.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Anax\Request;
4
5
/**
6
 * Storing information from the request and calculating related essentials.
7
 *
8
 */
9
class CRequestBasic
10
{
11
12
13
    /**
14
    * Properties
15
    *
16
    */
17
    private $requestUri; // Request URI from $_SERVER
18
    private $scriptName; // Scriptname from $_SERVER, actual scriptname part
19
    private $path;       // Scriptname from $_SERVER, path-part
20
21
    private $route;      // The route
22
    private $routeParts; // The route as an array
23
24
25
    private $currentUrl; // Current url
26
    private $siteUrl;    // Url to this site, http://dbwebb.se
27
    private $baseUrl;    // Url to root dir, siteUrl . /some/installation/directory/
28
29
    private $server; // Mapped to $_SERVER
30
    private $get;    // Mapped to $_GET
31
    private $post;   // Mapped to $_POST
32
33
34
35
    /**
36
     * Constructor.
37
     *
38
     *
39
     */
40 18
    public function __construct()
41
    {
42 18
        $this->setGlobals();
43 18
    }
44
45
46
47
    /**
48
     * Read info from the globals.
49
     *
50
     * @param array $globals use to initiate globals with values.
51
     *
52
     * @return void
53
     */
54 18
    public function setGlobals($globals = [])
55
    {
56 18
        $this->server = isset($globals['server']) ? array_merge($_SERVER, $globals['server']) : $_SERVER;
57 18
        $this->get    = isset($globals['get'])    ? array_merge($_GET, $globals['get'])       : $_GET;
58 18
        $this->post   = isset($globals['post'])   ? array_merge($_POST, $globals['post'])     : $_POST;
59 18
    }
60
61
62
63
    /**
64
     * Init the request class by reading information from the request.
65
     *
66
     * @return $this
67
     */
68 4
    public function init()
69
    {
70 4
        $this->requestUri = $this->getServer('REQUEST_URI');
71 4
        $scriptName = $this->getServer('SCRIPT_NAME');
72 4
        $this->path = rtrim(dirname($scriptName), '/');
73 4
        $this->scriptName = basename($scriptName);
74
75
        // The route and its parts
76 4
        $this->extractRoute();
77
78
        // Prepare to create siteUrl and baseUrl by using currentUrl
79 4
        $this->currentUrl = $this->getCurrentUrl();
80 4
        $parts = parse_url($this->currentUrl);
81 4
        $this->siteUrl = "{$parts['scheme']}://{$parts['host']}" . (isset($parts['port'])
82 4
            ? ":{$parts['port']}"
83 4
            : '');
84 4
        $this->baseUrl = $this->siteUrl . $this->path;
85
86 4
        return $this;
87
    }
88
89
90
91
    /**
92
     * Get site url.
93
     *
94
     * @return string
95
     */
96 4
    public function getSiteUrl()
97
    {
98 4
        return $this->siteUrl;
99
    }
100
101
102
103
    /**
104
     * Get base url.
105
     *
106
     * @return string
107
     */
108 4
    public function getBaseUrl()
109
    {
110 4
        return $this->baseUrl;
111
    }
112
113
114
115
    /**
116
     * Get script name.
117
     *
118
     * @return string
119
     */
120
    public function getScriptName()
121
    {
122
        return $this->scriptName;
123
    }
124
125
126
127
    /**
128
     * Get route parts.
129
     *
130
     * @return array
131
     */
132
    public function getRouteParts()
133
    {
134
        return $this->routeParts;
135
    }
136
137
138
139
    /**
140
     * Get the route.
141
     *
142
     * @return string as the current extracted route
143
     */
144 6
    public function getRoute()
145
    {
146 6
        return $this->route;
147
    }
148
149
150
151
    /**
152
     * Extract the part containing the route.
153
     *
154
     * @return string as the current extracted route
155
     */
156 10
    public function extractRoute()
157
    {
158 10
        $requestUri = $this->getServer('REQUEST_URI');
159 10
        $scriptName = $this->getServer('SCRIPT_NAME');
160 10
        $scriptPath = dirname($scriptName);
161 10
        $scriptFile = basename($scriptName);
162
163
        // Compare REQUEST_URI and SCRIPT_NAME as long they match,
164
        // leave the rest as current request.
165 10
        $i = 0;
166 10
        $len = min(strlen($requestUri), strlen($scriptPath));
167
        while ($i < $len
168 10
               && $requestUri[$i] == $scriptPath[$i]
169 10
        ) {
170 10
            $i++;
171 10
        }
172 10
        $route = trim(substr($requestUri, $i), '/');
173
174
        // Does the request start with script-name - remove it.
175 10
        $len1 = strlen($route);
176 10
        $len2 = strlen($scriptFile);
177
178
        if ($len2 <= $len1
179 10
            && substr_compare($scriptFile, $route, 0, $len2, true) === 0
180 10
        ) {
181 10
            $route = substr($route, $len2 + 1);
182 10
        }
183
184
        // Remove the ?-part from the query when analysing controller/metod/arg1/arg2
185 10
        $queryPos = strpos($route, '?');
186 10
        if ($queryPos !== false) {
187
            $route = substr($route, 0, $queryPos);
188
        }
189
190 10
        $route = ($route === false) ? '' : $route;
191
192 10
        $this->route = $route;
193 10
        $this->routeParts = explode('/', trim($route, '/'));
194
//var_dump($route);
195 10
        return $this->route;
196
    }
197
198
199
200
    /**
201
     * Get the current url.
202
     *
203
     * @param boolean $queryString attach query string, default is true.
204
     *
205
     * @return string as current url.
206
     */
207 11
    public function getCurrentUrl($queryString = true)
208
    {
209 11
        $rs    = $this->getServer('REQUEST_SCHEME');
210 11
        $https = $this->getServer('HTTPS') == 'on' ? true : false;
211 11
        $sn    = $this->getServer('SERVER_NAME');
212 11
        $port  = $this->getServer('SERVER_PORT');
213
214 11
        $port  = ($port == '80')
215 11
            ? ''
216 11
            : (($port == 443 && $https)
217 6
                ? ''
218 11
                : ':' . $port);
219
220 11
        if ($queryString) {
221 11
            $ru = rtrim($this->getServer('REQUEST_URI'), '/');
222 11
        } else {
223
            $ru = rtrim(strtok($this->getServer('REQUEST_URI'), '?'), '/');
224
        }
225
226
227 11
        $url  = $rs ? $rs : 'http';
228 11
        $url .= $https ? 's' : '';
229 11
        $url .= '://';
230 11
        $url .= $sn . $port . htmlspecialchars($ru);
231
        
232 11
        return $url;
233
    }
234
235
236
237
    /**
238
     * Get a value from the _SERVER array and use default if it is not set.
239
     *
240
     * @param string $key     to check if it exists in the $_SERVER variable
241
     * @param string $default value to return as default
242
     *
243
     * @return mixed
244
     */
245 17
    public function getServer($key, $default = null)
246
    {
247 17
        return isset($this->server[$key]) ? $this->server[$key] : $default;
248
    }
249
250
251
252
    /**
253
     * Set variable in the server array.
254
     *
255
     * @param mixed  $key   the key an the , or an key-value array
256
     * @param string $value the value of the key
257
     *
258
     * @return $this
259
     */
260 17 View Code Duplication
    public function setServer($key, $value = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
261
    {
262 17
        if (is_array($key)) {
263
            $this->server = array_merge($this->server, $key);
264
        } else {
265 17
            $this->server[$key] = $value;
266
        }
267 17
    }
268
269
270
271
    /**
272
     * Get a value from the _GET array and use default if it is not set.
273
     *
274
     * @param string $key     to check if it exists in the $_GET variable
275
     * @param string $default value to return as default
276
     *
277
     * @return mixed
278
     */
279 1
    public function getGet($key, $default = null)
280
    {
281 1
        return isset($this->get[$key]) ? $this->get[$key] : $default;
282
    }
283
284
285
286
    /**
287
     * Set variable in the get array.
288
     *
289
     * @param mixed  $key   the key an the , or an key-value array
290
     * @param string $value the value of the key
291
     *
292
     * @return $this
293
     */
294 1 View Code Duplication
    public function setGet($key, $value = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
295
    {
296 1
        if (is_array($key)) {
297
            $this->get = array_merge($this->get, $key);
298
        } else {
299 1
            $this->get[$key] = $value;
300
        }
301 1
    }
302
303
304
305
    /**
306
     * Get a value from the _POST array and use default if it is not set.
307
     *
308
     * @param string $key     to check if it exists in the $_POST variable
309
     * @param string $default value to return as default
310
     *
311
     * @return mixed
312
     */
313
    public function getPost($key = null, $default = null)
314
    {
315
        if ($key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $key of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
316
            return isset($this->post[$key]) ? $this->post[$key] : $default;
317
        } else {
318
            return $this->post;
319
        }
320
    }
321
}
322