Issues (14)

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/RSlim.php (7 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 RSlim;
4
5
use \Psr\Http\Message\RequestInterface as Request;
6
use \Psr\Http\Message\ResponseInterface as Response;
7
8
class RSlim
9
{
10
    public $config = [
11
        'base_dir' => null,
12
        'app_dir' => 'apps/_skel',
13
        'base_url' => null,
14
        'app_name' => 'www',
15
        'default_return_type' => 'html',
16
        'bypass_error_handlers' => true
17
    ];
18
    public $container = null;
19
    public $app = null;
20
    public $twig = null;
21
    public $controller = null;
22
    public $method = null;
23
24
    public function __construct($config)
25
    {
26
        $this->config = array_merge($this->config, $config);
27
        define('RSLIM_DEFAULT_RETURN_TYPE',$this->config['default_return_type']);
28
        ini_set("default_charset", "utf-8");
29
        ini_set('date.timezone', $config['app']['timezone']);
30
        $configuration = [
31
            'settings' => [
32
                'displayErrorDetails' => $config['app']['debug'],
33
            ],
34
        ];
35
        $this->container = new \Slim\Container($configuration);
36
        $this->app = new \Slim\App($this->container);
37
    }
38
39
    public function runRoute($request, $response, $route, $action = 'main', $returnType = RSLIM_DEFAULT_RETURN_TYPE, $args = [])
40
    {
41
        $this->controller = $route;
42
        $this->method = $action;
43
        $controller = $this->config['base_dir'] . '/' . $this->config['app_dir'] . '/controllers/' . $route . '/' . $action . '.php';
44
        $template   = '/' . $this->config['app_dir'] . '/templates/' . $route . '/' . $action . '.html';
45
46
        if (file_exists($controller)) {
47
            require_once($controller);
48
49
            $functionName = $route . '_' . $action;
50
51
            if ($returnType == 'html') {
52
                $this->useTwig($request);
53
            }
54
            if (!function_exists($functionName)) {
55
                $message = 'Controller ' . $route . '/' . $action . " has not " . $functionName . " function";
56
                return $this->notFound($response, $returnType, $message);
57
            }
58
            if ($returnType == 'json') {
59
                return $this->returnJson($functionName, $request, $args, $response);
60
            }
61
            return $this->returnHtml($functionName, $request, $args, $response, $route, $action, $template);
62
        }
63
        $message = 'Controller ' . $route . '/' . $action . " not found";
64
        return $this->notFound($response, $returnType, $message);
65
    }
66
    
67
    public function register($requestMethod, $pattern, $controller, $returnType = RSLIM_DEFAULT_RETURN_TYPE)
68
    {
69
        $this->app->map([strtoupper($requestMethod)], $pattern, function(Request $req, Response $res, $args) {
70
            list($route, $action) = explode("/", $args['controller']);
71
            return $args['RSlim']->runRoute($req, $res, $route, $action, $args['returnType'], $args);
72
        })->setArguments(['controller'=>$controller, 'returnType'=>$returnType, 'RSlim'=>$this]);
73
    }
74
    
75
    public function run()
76
    {
77
        $this->container['notFoundHandler'] = function($c) {
78
            return function(Request $req, Response $res) use ($c) {
0 ignored issues
show
The parameter $req is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $res is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
                return $c['response']
80
                ->withStatus(404)
81
                ->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By', "reformo/rslim")
82
                ->write('<h1>404 - Requested URL not found</h1>');
83
            };
84
        };
85
        if ($this->config['bypass_error_handlers'] === true) {
86 View Code Duplication
            $this->container['errorHandler'] = function($container) {
0 ignored issues
show
This code seems to be duplicated across 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...
87
                return function($request, $response, $exception) use ($container) {
88
                    $response->getBody()->rewind();
89
                    return $response->withStatus(500)
90
                    ->withHeader('Content-Type', 'text/html')
91
                    ->write($exception->getMessage());
92
                };
93
            };
94 View Code Duplication
            $this->container['phpErrorHandler'] = function($container) {
0 ignored issues
show
This code seems to be duplicated across 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...
95
                return function($request, $response, $error) use ($container) {
96
                    $response->getBody()->rewind();
97
                    return $response->withStatus(500)
98
                    ->withHeader('Content-Type', 'text/html')
99
                    ->write($error->getMessage());
100
                };
101
            };
102
        }
103
        $this->app->run();
104
    }    
105
    
106
    private function returnJson($functionName, $request, $args, $response)
107
    {
108
        $functionOutput = call_user_func($functionName, $request, $args);
109
        if (isset($functionOutput['redirect'])) {
110
            return $response->withRedirect($functionOutput['redirect']);
111
        }
112
        if (!is_array($functionOutput)) {
113
            $functionOutput = ["status" => 500, "error" => "Internal Server Error"];
114
        } elseif (!isset($functionOutput['status'])) {
115
            $functionOutput['status'] = 200;
116
        }
117
        $status = (int) $functionOutput['status'];
118
        $response->getBody()->write(json_encode($functionOutput));
119
        return $response->withHeader('Content-Type', 'application/json;charset=utf-8')->withHeader('X-Powered-By', "reformo/rslim")->withStatus($status);
120
    }
121
    
122
    private function returnHtml($functionName, $request, $args, $response, $route, $action, $template){
123 View Code Duplication
        if (!file_exists($this->config['base_dir'] . $template)) {
0 ignored issues
show
This code seems to be duplicated across 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...
124
            throw new \Exception("<strong>Template file not found!</strong> " . $route . '/' . $action . " needs a template file at:" . $template);
125
        }
126
        $functionOutput = call_user_func($functionName, $request, $args);
127
        if (isset($functionOutput['redirect'])) {
128
            return $response->withRedirect($functionOutput['redirect']);
129
        }
130
        if (!isset($functionOutput['data'])) {
131
            $functionOutput['data'] = [];
132
        }
133
        
134
        $functionOutput['app_content'] = $this->twig->render($template, $functionOutput['data']);
135
        $mainTemplateName = 'default';
136
        if (isset($functionOutput['app_main_template'])) {
137
            $mainTemplateName = $functionOutput['app_main_template'];
138
        }
139
        $mainTemplate = '/' . $this->config['app_dir'] . '/templates/_' . $mainTemplateName . '.html';
140
141 View Code Duplication
        if (!file_exists($this->config['base_dir'] . $mainTemplate)) {
0 ignored issues
show
This code seems to be duplicated across 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...
142
            throw new \Exception("<strong>Main emplate file not found!</strong> " . $route . '/' . $action .
143
                " needs a main template file at:" . $mainTemplate);
144
        }
145
        $appContent = $this->twig->render($mainTemplate, $functionOutput);
146
        $newResponse = $response->withHeader('X-Powered-By', "reformo/rslim");
147
        $newResponse->write($appContent);
148
        return $newResponse;
149
    }
150
    
151
    public function useTwig($request)
152
    {
153
        $loader     = new \Twig_Loader_Filesystem($this->config['base_dir']);
154
        $this->twig = new \Twig_Environment($loader, [
155
            'cache'         => '/tmp',
156
            'debug'         => $this->config['app']['debug'],
157
            'auto_reload'   => 1
158
        ]);
159
        $filter = new \Twig_SimpleFunction(
160
            'rwidget_*_*',
161
            function($widgetName, $widgetAction, $args = []) {
162
                $widgetFile = $this->config['base_dir'] . '/' . $this->config['app_dir'] . '/widgets/' . $widgetName . '/' . $widgetAction . ".php";
163
                $widgetTemplate = '/' . $this->config['app_dir'] . '/templates/_widgets/' . $widgetName . '/' . $widgetAction . '.html';
164
                if (!file_exists($widgetFile)) {
165
                    throw new \Exception("<strong>Widget file not found!</strong> " . $widgetName . '/' . $widgetAction . "!");
166
                }
167
                require_once $widgetFile;
168
                $widgetContentFunc = "rwidget_" . $widgetName . "_" . $widgetAction;
169
                if (function_exists($widgetContentFunc)) {
170
                    $widgetContent = call_user_func($widgetContentFunc, $args);
171 View Code Duplication
                    if (!file_exists($this->config['base_dir'] . $widgetTemplate)) {
0 ignored issues
show
This code seems to be duplicated across 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...
172
                        throw new \Exception("<strong>Widget should return string:</strong> " . $widgetName . '/' . $widgetAction . "!");
173
                    }
174
                    return $this->twig->render($widgetTemplate, $widgetContent);
175
                }
176
                throw new \Exception("<strong>Widget function not found!</strong> " . $widgetName . '/' . $widgetAction . "!");
177
            },
178
            array('is_safe' => array('html'))
179
        );
180
        $this->twig->addFunction($filter);
181
        $this->twig->addGlobal('runtime_config', $this->config);
182
        $this->twig->addGlobal('url_params', $request->getParams());
183
    }
184
185
    private function notFound($response, $returnType = 'html', $message = "")
186
    {
187
188
        $notFoundTemplate = '/' . $this->config['app_dir'] . '/templates/_404.html';
189
        if ($returnType == 'json') {
190
            $response->getBody()->write(json_encode(['status'=>404, 'message'=>$message]));
191
            return $response->withHeader('Content-Type', 'application/json;charset=utf-8')
192
                ->withHeader('X-Powered-By', "reformo/rslim")->withStatus(404);
193
        }
194
        return $response->withStatus(404)
195
            ->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By', "reformo/rslim")
196
            ->write($this->twig->render($notFoundTemplate, ['message'=>$message]));
197
198
    }
199
200
}
201