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/MVC/CDispatcherBasic.php (5 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\MVC;
4
5
/**
6
 * Dispatching to controllers.
7
 *
8
 */
9
class CDispatcherBasic implements \Anax\DI\IInjectionAware
10
{
11
    use \Anax\DI\TInjectionAware;
12
13
14
15
    /**
16
     * Properties
17
     *
18
     */
19
    private $controllerName;    // Name of controller
20
    private $controller;        // Actual controller
21
    private $action;            // Name of action
22
    private $params;            // Params
23
24
25
26
    /**
27
     * Prepare the name.
28
     *
29
     * @param string $name to prepare.
30
     *
31
     * @return string as the prepared name.
32
     */
33 1
    public function prepareName($name)
34
    {
35 1
        $name = empty($name) ? 'index' : $name;
36 1
        $name = strtolower($name);
37 1
        $name = str_replace(['-', '_'], ' ', $name);
38 1
        $name = ucwords($name);
39 1
        $name = str_replace(' ', '', $name);
40
        
41 1
        return $name;
42
    }
43
44
45
46
    /**
47
     * Set the name of the controller.
48
     *
49
     * @param string $name of the controller, defaults to 'index'.
50
     *
51
     * @return void
52
     */
53 1
    public function setControllerName($name = 'index')
54
    {
55 1
        $name = $this->prepareName($name) . 'Controller';
56
57 1
        $this->controllerName = $name;
58
59 1
        $this->controller = $this->di->has($name)
60 1
            ? $this->di->get($name)
61 1
            : null;
62 1
    }
63
64
65
66
    /**
67
     * Check if a controller exists with this name.
68
     *
69
     * @return void
70
     */
71 2
    public function isValidController()
72
    {
73 2
        return is_object($this->controller);
74
    }
75
76
77
78
    /**
79
     * Set the name of the action.
80
     *
81
     * @param string $name of the action, defaults to 'index'.
82
     *
83
     * @return void
84
     */
85 1
    public function setActionName($name = 'index')
86
    {
87 1
        $this->action = lcfirst($this->prepareName($name)) . 'Action';
88 1
    }
89
90
91
92
    /**
93
     * Set the params.
94
     *
95
     * @param array $params all parameters, defaults to empty.
96
     *
97
     * @return void
98
     */
99
    public function setParams($params = [])
100
    {
101
        $this->params = $params;
102
    }
103
104
105
106
    /**
107
     * Dispatch to a controller, action with parameters.
108
     *
109
     * @return bool
110
     */
111
    public function isCallable()
112
    {
113
        $handler = [$this->controller, $this->action];
0 ignored issues
show
$handler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
114
115
        if (!method_exists($this->controller, $this->action)) {
116
            return false;
117
        }
118
119
        $reflection = new \ReflectionMethod($this->controller, $this->action);
120
        if (!$reflection->isPublic()) {
121
            return false;
122
        }
123
124
        return true;
125
    }
126
127
128
129
    /**
130
     * Inspect if callable and throw exception if parts is not callable.
131
     *
132
     * @return void.
0 ignored issues
show
The doc-type void. could not be parsed: Unknown type name "void." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
133
     */
134 2
    public function isCallableOrException()
135
    {
136 2
        $validController = $this->isValidController();
137
138 2
        $isMethod   = null;
139 2
        $isCallable = null;
140
141 2
        if ($validController) {
142
            $handler = [$this->controller, $this->action];
0 ignored issues
show
$handler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
143
            $isMethod   = method_exists($this->controller, $this->action);
144
            $isCallable = $this->isCallable();
145
        }
146
147 2
        if (!($isMethod && $isCallable)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $isMethod of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
Bug Best Practice introduced by
The expression $isCallable of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
148
            $msg =
149
                "Trying to dispatch/forward to a non callable item. Controllername = '"
150 2
                . $this->controllerName
151 2
                . "', Action = '"
152 2
                . $this->action
153 2
                . "'."
154 2
            ;
155
156 2
            $not = $validController ? "" : "NOT";
157
            $msg .=
158 2
                " The controller named '$this->controllerName' does $not exist as part of of the
159
                service-container \$di.
160 2
                ";
161
162 2
            $services = $this->di->getServices();
163 2
            natcasesort($services);
164 2
            $services = implode("\n", $services);
165 2
            $msg .= " Loaded services are: <pre>$services</pre>\n";
166
167 2
            if ($validController) {
168
                $not = $isMethod ? "" : "NOT";
169
                $msg .= " The method '$this->action' does $not exist in the class '$this->controllerName'.";
170
171
                $not = $isCallable ? "" : "NOT";
172
                $msg .=
173
                    " The method '$this->action' is $not callable in the class '$this->controllerName'
174
                    (taking magic methods into consideration).";
175
            }
176
177 2
            throw new \Exception($msg);
178
        }
179
    }
180
181
182
183
    /**
184
     * Dispatch to a controller, action with parameters.
185
     *
186
     * @return mixed result from dispatched controller action.
187
     */
188 2
    public function dispatch()
189
    {
190 2
        $handler = [$this->controller, 'initialize'];
191 2
        if (method_exists($this->controller, 'initialize') && is_callable($handler)) {
192
            call_user_func($handler);
193
        }
194
195 2
        $this->isCallableOrException();
196
197
        return call_user_func_array([$this->controller, $this->action], $this->params);
198
    }
199
200
201
    /**
202
     * Forward to a controller, action with parameters.
203
     *
204
     * @param array $forward with details for controller, action, parameters.
205
     *
206
     * @return mixed result from dispatched controller action.
207
     */
208
    public function forward($forward = [])
209
    {
210
        $controller = isset($forward['controller'])
211
            ? $forward['controller']
212
            : null;
213
214
        $action = isset($forward['action'])
215
            ? $forward['action']
216
            : null;
217
        
218
        $params = isset($forward['params'])
219
            ? $forward['params']
220
            : [];
221
222
        $this->setControllerName($controller);
223
        $this->setActionName($action);
224
        $this->setParams($params);
225
226
        $this->isCallableOrException();
227
        return $this->dispatch();
228
    }
229
}
230