Issues (165)

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
    * Properties
13
    *
14
    */
15
    private $requestUri; // Request URI from $_SERVER
16
    private $scriptName; // Scriptname from $_SERVER, actual scriptname part
17
    private $path;       // Scriptname from $_SERVER, path-part
18
19
    private $route;      // The route
20
    private $routeParts; // The route as an array
21
22
23
    private $currentUrl; // Current url
24
    private $siteUrl;    // Url to this site, http://dbwebb.se
25
    private $baseUrl;    // Url to root dir, siteUrl . /some/installation/directory/
26
27
    private $server; // Mapped to $_SERVER
28
    private $get;    // Mapped to $_GET
29
    private $post;   // Mapped to $_POST
30
31
32
33
    /**
34
     * Constructor.
35
     *
36
     *
37
     */
38 18
    public function __construct()
39
    {
40 18
        $this->setGlobals();
41 18
    }
42
43
44
45
    /**
46
     * Read info from the globals.
47
     *
48
     * @param array $globals use to initiate globals with values.
49
     *
50
     * @return void
51
     */
52 18
    public function setGlobals($globals = [])
53
    {
54 18
        $this->server = isset($globals['server']) ? array_merge($_SERVER, $globals['server']) : $_SERVER;
55 18
        $this->get    = isset($globals['get'])    ? array_merge($_GET, $globals['get'])       : $_GET;
56 18
        $this->post   = isset($globals['post'])   ? array_merge($_POST, $globals['post'])     : $_POST;
57 18
    }
58
59
60
61
    /**
62
     * Init the request class by reading information from the request.
63
     *
64
     * @return $this
65
     */
66 4
    public function init()
67
    {
68 4
        $this->requestUri = $this->getServer('REQUEST_URI');
69 4
        $scriptName = $this->getServer('SCRIPT_NAME');
70 4
        $this->path = rtrim(dirname($scriptName), '/');
71 4
        $this->scriptName = basename($scriptName);
72
73
        // The route and its parts
74 4
        $this->extractRoute();
75
76
        // Prepare to create siteUrl and baseUrl by using currentUrl
77 4
        $this->currentUrl = $this->getCurrentUrl();
78 4
        $parts = parse_url($this->currentUrl);
79 4
        $this->siteUrl = "{$parts['scheme']}://{$parts['host']}"
80 4
            . (isset($parts['port'])
81 4
                ? ":{$parts['port']}"
82 4
                : '');
83 4
        $this->baseUrl = $this->siteUrl . $this->path;
84
85 4
        return $this;
86
    }
87
88
89
90
    /**
91
     * Get site url.
92
     *
93
     * @return string
94
     */
95 4
    public function getSiteUrl()
96
    {
97 4
        return $this->siteUrl;
98
    }
99
100
101
102
    /**
103
     * Get base url.
104
     *
105
     * @return string
106
     */
107 4
    public function getBaseUrl()
108
    {
109 4
        return $this->baseUrl;
110
    }
111
112
113
114
    /**
115
     * Get script name.
116
     *
117
     * @return string
118
     */
119
    public function getScriptName()
120
    {
121
        return $this->scriptName;
122
    }
123
124
125
126
    /**
127
     * Get route parts.
128
     *
129
     * @return array
130
     */
131
    public function getRouteParts()
132
    {
133
        return $this->routeParts;
134
    }
135
136
137
138
    /**
139
     * Get the route.
140
     *
141
     * @return string as the current extracted route
142
     */
143 6
    public function getRoute()
144
    {
145 6
        return $this->route;
146
    }
147
148
149
150
    /**
151
     * Extract the part containing the route.
152
     *
153
     * @return string as the current extracted route
154
     */
155 10
    public function extractRoute()
156
    {
157 10
        $requestUri = $this->getServer('REQUEST_URI');
158 10
        $scriptName = $this->getServer('SCRIPT_NAME');
159 10
        $scriptPath = dirname($scriptName);
160 10
        $scriptFile = basename($scriptName);
161
162
        $requestUri = urldecode($requestUri);
163
164 10
        // Compare REQUEST_URI and SCRIPT_NAME as long they match,
165 10
        // leave the rest as current request.
166
        $i = 0;
167 10
        $len = min(strlen($requestUri), strlen($scriptPath));
168 10
        while ($i < $len
169 10
               && $requestUri[$i] == $scriptPath[$i]
170 10
        ) {
171 10
            $i++;
172
        }
173
        $route = trim(substr($requestUri, $i), '/');
174 10
175 10
        // Does the request start with script-name - remove it.
176
        $len1 = strlen($route);
177
        $len2 = strlen($scriptFile);
178 10
179 10
        if ($len2 <= $len1
180 10
            && substr_compare($scriptFile, $route, 0, $len2, true) === 0
181 10
        ) {
182
            $route = substr($route, $len2 + 1);
183
        }
184 10
185 10
        // Remove the ?-part from the query when analysing controller/metod/arg1/arg2
186
        $queryPos = strpos($route, '?');
187
        if ($queryPos !== false) {
188
            $route = substr($route, 0, $queryPos);
189 10
        }
190
191 10
        $route = ($route === false) ? '' : $route;
192 10
193
        $this->route = $route;
194 10
        $this->routeParts = explode('/', trim($route, '/'));
195
196
        return $this->route;
197
    }
198
199
200
201
    /**
202
     * Get the current url.
203
     *
204
     * @param boolean $queryString attach query string, default is true.
205
     *
206 11
     * @return string as current url.
207
     */
208 11
    public function getCurrentUrl($queryString = true)
209 11
    {
210 11
        $rs    = $this->getServer('REQUEST_SCHEME');
211 11
        $https = $this->getServer('HTTPS') == 'on' ? true : false;
212
        $sn    = $this->getServer('SERVER_NAME');
213 11
        $port  = $this->getServer('SERVER_PORT');
214 11
215 11
        $port  = ($port == '80')
216 6
            ? ''
217 11
            : (($port == 443 && $https)
218
                ? ''
219 11
                : ':' . $port);
220 11
221 11
        if ($queryString) {
222
            $ru = rtrim($this->getServer('REQUEST_URI'), '/');
223
        } else {
224
            $ru = rtrim(strtok($this->getServer('REQUEST_URI'), '?'), '/');
225 11
        }
226 11
227 11
        $url  = $rs ? $rs : 'http';
228 11
        //$url .= $https ? 's' : '';
229
        $url .= '://';
230 11
        $url .= $sn . $port . htmlspecialchars($ru);
231
        
232
        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 17
     * @return mixed
244
     */
245 17
    public function getServer($key, $default = null)
246
    {
247
        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 17
     * @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
        if (is_array($key)) {
263 17
            $this->server = array_merge($this->server, $key);
264
        } else {
265 17
            $this->server[$key] = $value;
266
        }
267
    }
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 1
     * @return mixed
278
     */
279 1
    public function getGet($key, $default = null)
280
    {
281
        return isset($this->get[$key]) ? $this->get[$key] : $default;
282
    }
283
284
285
286
    /**
287
     * Check if s value is defined in the _GET array.
288
     *
289
     * @param string $key     to check if it exists in the $_GET variable
290
     *
291
     * @return boolean
292 1
     */
293
    public function hasGet($key)
294 1
    {
295
        return isset($this->get[$key]);
296
    }
297 1
298
299 1
300
    /**
301
     * Set variable in the get array.
302
     *
303
     * @param mixed  $key   the key an the , or an key-value array
304
     * @param string $value the value of the key
305
     *
306
     * @return $this
307
     */
308 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...
309
    {
310
        if (is_array($key)) {
311
            $this->get = array_merge($this->get, $key);
312
        } else {
313
            $this->get[$key] = $value;
314
        }
315
    }
316
317
318
319
    /**
320
     * Get a value from the _POST array and use default if it is not set.
321
     *
322
     * @param string $key     to check if it exists in the $_POST variable
323
     * @param string $default value to return as default
324
     *
325
     * @return mixed
326
     */
327
    public function getPost($key = null, $default = null)
328
    {
329
        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...
330
            return isset($this->post[$key]) ? $this->post[$key] : $default;
331
        } else {
332
            return $this->post;
333
        }
334
    }
335
}
336