forked from e-cidade/e-cidade
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.php
More file actions
203 lines (167 loc) · 6.97 KB
/
app.php
File metadata and controls
203 lines (167 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
use \ECidade\V3\Extension\Registry;
use \ECidade\V3\Extension\Front;
use \ECidade\V3\Extension\Dispatcher;
use \ECidade\V3\Extension\Router;
use \ECidade\V3\Extension\Request;
use \ECidade\V3\Extension\Response;
use \ECidade\V3\Extension\Manager as ExtensionManager;
use \ECidade\V3\Extension\Exceptions\ResponseException;
use \ECidade\V3\Extension\Glob;
use \ECidade\V3\Error\EntityFactory;
use \ECidade\V3\Error\Renderer as ErrorRenderer;
try {
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'bootstrap.php');
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'Support' . DIRECTORY_SEPARATOR . 'RegisterEvents.php');
$front = new Front();
$request = new Request($front->getPath());
$response = new Response();
$router = new Router($request);
$config = Registry::get('app.config');
Registry::set('app.request', $request);
Registry::set('app.response', $response);
// Requiscao /extension/[name]
if ($front->isExtension() && $request->getExtension() != null) {
$front->createWindow();
define('ECIDADE_WINDOW_REQUEST_PATH', $front->getWindowRequestPath());
define('ECIDADE_CURRENT_EXTENSION_PATH', ECIDADE_EXTENSION_PACKAGE_PATH . $request->getExtension() . DS);
define('ECIDADE_CURRENT_EXTENSION_REQUEST_PATH',
ECIDADE_WINDOW_REQUEST_PATH . 'extension' . DS . mb_strtolower($request->getExtension()) . DS
);
ini_set('display_errors', $config->get('php.display_errors'));
ini_set('error_reporting', $config->get('php.error_reporting'));
error_reporting($config->get('php.error_reporting'));
// Extensao contem arquivo de inicializacao
if (file_exists(ECIDADE_CURRENT_EXTENSION_PATH . 'bootstrap.php')) {
require_once(ECIDADE_CURRENT_EXTENSION_PATH . 'bootstrap.php');
}
// Registra eventos adicionado ao cache(metadado)
if (!$request->isAsset()) {
Registry::get('app.container')->get('app.configData')->loadEvents();
}
// Encode das paginas
$response->setCharset($config->get('charset'));
mb_internal_encoding($config->get('charset'));
try {
$request->session()->start();
$dispatcher = new Dispatcher();
$dispatcher->execute($request, $response);
$response->output();
} catch (Exception $error) {
throw new ResponseException($error->getMessage(), $error->getCode());
}
exit();
}
// END EXTENSION
// ecidade encoding charset must be LATIN1
$config->set('charset', 'ISO-8859-1');
$response->setCharset($config->get('charset'));
mb_internal_encoding($config->get('charset'));
$filePath = $front->getPath();
// @todo - validar utilidade
if ($front->isExtension() && $request->getExtension() == null) {
$filePath = 'extension/' . $front->getPath();
}
// base '/', usa index
if (empty($filePath)) {
$filePath = 'index.php';
}
$realpath = realpath(ECIDADE_PATH . $filePath);
$filePath = str_replace(ECIDADE_PATH, '', $realpath);
$fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
$isLaravel = preg_match('/\/web\/[^ ]*/', $_SERVER['REQUEST_URI']);
// Arquivo nao existe, 404
if (($realpath === false || !file_exists($filePath)) && !$isLaravel) {
throw new ResponseException('Pgina nao encontrada: ' . $filePath, 404);
}
// security issue
// bloqueia acesso a arquivos acima do root path
if (strpos($realpath, ECIDADE_PATH) !== 0 && !$isLaravel) {
throw new ResponseException('Acesso negado: ' . $filePath, 403);
}
// security issue
// bloqueia execucao scripts php e html no diretorio tmp/
if (in_array($fileExtension, ['php', 'html']) && strpos($filePath, 'tmp/') === 0) {
// @TODO usar ResponseException
header('HTTP/1.0 403 Forbiden');
exit;
}
if (!$isLaravel) {
$front->fixQueryString();
}
$front->createWindow();
// Requisicoes que devem iniciar sessao somente leitura
if (preg_match(Glob::toRegex($config->get('app.request.session.readOnlyOn'), true, false), $filePath)) {
$request->session()->writeable(false)->start();
$filePath = modification($filePath);
} elseif (preg_match(Glob::toRegex($config->get('app.request.session.attachOn'), true, false), $filePath)) {
// Requisicoes que devem iniciar sessao com escrita
$request->session()->start();
$filePath = modification($filePath);
}
/**
* Asset - define header e retorna o arquivo para o buffer de saida
*/
if ($fileExtension != 'php' && !$isLaravel) {
$request->session()->close();
$response->setFile($filePath);
$response->output();
exit();
}
// Registra eventos adicionado ao cache(metadado)
Registry::get('app.container')->get('app.configData')->loadEvents();
$request->session()->start();
$front->emulateRegisterLongArrays();
// @TODO - achar melhor forma de emular register_globals
// @see \DBSeller\Legacy\PHP53\Emulate::registerGlobals()
// atualmente package DBSeller/Legacy executado antes de criar sessao
if (!ini_get('register_globals')) {
$front->emulateRegisterGlobals(array('SESSION'));
}
// lazyload para verificar se usuario tem extensao desktop instalada
Registry::get('app.container')->register('ECIDADE_DESKTOP', function () use ($request) {
return ExtensionManager::isEnabled('Desktop', $request->session()->get('DB_login'));
});
$request->session()->close();
$response->send();
// Remove todas as variaveis criadas neste arquivo
// para nao ter impacto em outros arquivos, exemplo: iniciar sessao no db_conecta.php
unset($_SESSION, $front, $request, $response, $router, $config);
/**
* Eloquent bootstrap
*/
/**
* @var \ECidade\V3\Window\Session $session
*/
$session = Registry::get('app.request')->session();
$userLoggedIn = $session->has('DB_id_usuario');
if ($userLoggedIn) {
$eloquent = new EloquentBootstrap(
$session->get('DB_servidor'),
$session->get('DB_NBASE', $session->get('DB_base')),
$session->get('DB_user'),
$session->get('DB_senha'),
$session->get('DB_porta')
);
$eloquent->bootstrap();
}
/**
* End Eloquent bootstrap
*/
if ($isLaravel) {
$pos = explode('/', $_SERVER['REQUEST_URI']);
$index = array_search('w', $pos);
unset($pos[$index], $pos[$index + 1]);
$_SERVER['REQUEST_URI'] = implode('/', $pos);
require_once(__DIR__ . '/public/index.php');
} else {
require_once($filePath);
}
} catch (ResponseException $exception) {
$response = Registry::get('app.response');
if ($response) {
$response->setCode($exception->getCode() == 0 ? 500 : $exception->getCode());
}
$entity = EntityFactory::createFromException($exception);
ErrorRenderer::render($entity);
}