|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Fran López <[email protected]> |
|
4
|
|
|
* @version 1.0 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace PSFS; |
|
8
|
|
|
|
|
9
|
|
|
use PSFS\base\exception\AdminCredentialsException; |
|
10
|
|
|
use PSFS\base\exception\RouterException; |
|
11
|
|
|
use PSFS\base\exception\SecurityException; |
|
12
|
|
|
use PSFS\base\Logger; |
|
13
|
|
|
use PSFS\base\Request; |
|
14
|
|
|
use PSFS\base\Security; |
|
15
|
|
|
use PSFS\base\Singleton; |
|
16
|
|
|
use PSFS\base\types\helpers\I18nHelper; |
|
17
|
|
|
use PSFS\base\types\helpers\RequestHelper; |
|
18
|
|
|
use PSFS\base\types\traits\SystemTrait; |
|
19
|
|
|
use PSFS\controller\ConfigController; |
|
20
|
|
|
use PSFS\controller\UserController; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class Dispatcher |
|
24
|
|
|
* @package PSFS |
|
25
|
|
|
*/ |
|
26
|
|
|
class Dispatcher extends Singleton |
|
27
|
|
|
{ |
|
28
|
|
|
use SystemTrait; |
|
29
|
|
|
/** |
|
30
|
|
|
* @Injectable |
|
31
|
|
|
* @var \PSFS\base\Security $security |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $security; |
|
34
|
|
|
/** |
|
35
|
|
|
* @Injectable |
|
36
|
|
|
* @var \PSFS\base\Router $router |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $router; |
|
39
|
|
|
/** |
|
40
|
|
|
* @Injectable |
|
41
|
|
|
* @var \PSFS\base\config\Config $config |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $config; |
|
44
|
|
|
|
|
45
|
|
|
private $actualUri; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Initializer method |
|
49
|
|
|
*/ |
|
50
|
11 |
|
public function init() |
|
51
|
|
|
{ |
|
52
|
11 |
|
Logger::log('Dispatcher init'); |
|
53
|
11 |
|
parent::init(); |
|
54
|
11 |
|
$this->initiateStats(); |
|
55
|
|
|
I18nHelper::setLocale(); |
|
56
|
|
|
$this->bindWarningAsExceptions(); |
|
57
|
|
|
$this->actualUri = Request::getInstance()->getServer("REQUEST_URI"); |
|
58
|
|
|
Logger::log('End dispatcher init'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Run method |
|
63
|
|
|
* @return string HTML |
|
64
|
|
|
*/ |
|
65
|
|
|
public function run() |
|
66
|
|
|
{ |
|
67
|
|
|
Logger::log('Begin runner'); |
|
68
|
|
|
try { |
|
69
|
|
|
if ($this->config->isConfigured()) { |
|
70
|
|
|
//Check CORS for requests |
|
71
|
|
|
RequestHelper::checkCORS(); |
|
72
|
|
|
if (!Request::getInstance()->isFile()) { |
|
73
|
|
|
return $this->router->execute($this->actualUri); |
|
74
|
|
|
} |
|
75
|
|
|
} else { |
|
76
|
|
|
return ConfigController::getInstance()->config(); |
|
77
|
|
|
} |
|
78
|
|
|
} catch (AdminCredentialsException $a) { |
|
79
|
|
|
return UserController::showAdminManager(); |
|
80
|
|
|
} catch (SecurityException $s) { |
|
81
|
|
|
return $this->security->notAuthorized($this->actualUri); |
|
82
|
|
|
} catch (RouterException $r) { |
|
83
|
|
|
return $this->router->httpNotFound($r); |
|
84
|
|
|
} catch (\Exception $e) { |
|
85
|
|
|
return $this->dumpException($e); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Method that convert an exception to html |
|
91
|
|
|
* |
|
92
|
|
|
* @param \Exception $e |
|
93
|
|
|
* |
|
94
|
|
|
* @return string HTML |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function dumpException(\Exception $e) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
Logger::log('Starting dump exception'); |
|
99
|
|
|
$ex = (NULL !== $e->getPrevious()) ? $e->getPrevious() : $e; |
|
|
|
|
|
|
100
|
|
|
$error = array( |
|
101
|
|
|
"error" => $ex->getMessage(), |
|
102
|
|
|
"file" => $ex->getFile(), |
|
103
|
|
|
"line" => $ex->getLine(), |
|
104
|
|
|
); |
|
105
|
|
|
Logger::log('Throwing exception', LOG_ERR, $error); |
|
106
|
|
|
unset($error); |
|
107
|
|
|
|
|
108
|
|
|
return $this->router->httpNotFound($ex); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.