-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBootstrap.php
301 lines (267 loc) · 8.18 KB
/
Bootstrap.php
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/**
* GSC Tesseract
* php version 8.2
*
* @category CMS
* @package Framework
* @author Fred Brooker <[email protected]>
* @license MIT https://gscloud.cz/LICENSE.txt
* @link https://mini.gscloud.cz
*/
declare (strict_types = 1);
use Nette\Neon\Neon;
use Tracy\Debugger;
// TIMER START
define('TESSERACT_START', microtime(true));
// directory separator
defined('DS') || define('DS', DIRECTORY_SEPARATOR);
// string separator
defined('SS') || define('SS', '_');
// CLI SAPI external include (optional)
if (PHP_SAPI === 'cli') {
$req = getenv('CLI_REQ');
if ($req && file_exists($req) && is_readable($req)) {
include_once $req;
}
}
// PHP INI
ini_set(
'auto_detect_line_endings',
defined('AUTO_DETECT_LINE_ENDINGS') ? AUTO_DETECT_LINE_ENDINGS : 'true'
);
ini_set(
'default_socket_timeout',
defined('DEFAULT_SOCKET_TIMEOUT') ? DEFAULT_SOCKET_TIMEOUT : '15'
);
ini_set(
'display_errors',
defined('DISPLAY_ERRORS') ? DISPLAY_ERRORS : 'true'
);
ob_start();
error_reporting(E_ALL);
// is FastCGI on?
if (getenv('FCGI_ROLE') === 'RESPONDER') {
define('FCGI_ENABLED', true);
} else {
define('FCGI_ENABLED', false);
}
// app root = current working directory
defined('ROOT') || define('ROOT', __DIR__);
// Apache root, not mandatory for the main code
defined('WWW') || define('WWW', ROOT . DS . 'www');
// APP directory, mandatory for the main code (App.php etc. loaded from here)
$d = 'APP';
$x = ROOT . DS . 'app';
if (defined($d) && is_dir($x) && is_readable($x)) {
} else {
if (is_dir($x) && is_readable($x)) {
define($d, $x);
} else {
die('Could not read the APP directory: ' . $x);
}
}
// CACHE directory, can use system temp
$d = 'CACHE';
$x = ROOT . DS . 'temp';
if (defined($d) && is_dir($x) && is_readable($x)) {
} else {
if (is_dir($x) && is_writable($x)) {
define($d, $x);
} else {
define($d, '/tmp');
}
}
// DATA directory, can use system temp
$d = 'DATA';
$x = ROOT . DS . 'data';
if (defined($d) && is_dir($x) && is_writable($x)) {
} else {
if (is_dir($x) && is_writable($x)) {
define($d, $x);
} else {
define($d, '/tmp');
}
}
// configuration file, test later
defined('CONFIG') || define('CONFIG', APP . DS . 'config.neon');
// private configuration file, not mandatory, test later
defined('CONFIG_PRIVATE') || define(
'CONFIG_PRIVATE', APP . DS . 'config_private.neon'
);
// configuration file, use inside Docker only, test later
defined('CONFIG_DOCKER') || define(
'CONFIG_DOCKER', APP . DS . 'config_docker.neon'
);
// GUI based configuration file, not mandatory
defined('CONFIG_GUI') || define(
'CONFIG_GUI', DATA . DS . 'config_gui.json'
);
// CSP definition file, used in App.php, not mandatory
$d = 'CSP';
$x = APP . DS . 'csp.neon';
if (file_exists($x) && is_readable($x)) {
defined($d) || define($d, $x);
} else {
defined($d) || define($d, null);
}
// Mustache templates, not mandatory
$d = 'TEMPLATES';
$x = APP . DS . 'templates';
if (is_dir($x) && is_readable($x)) {
defined($d) || define($d, $x);
} else {
defined($d) || define($d, '/tmp');
}
// Mustache partials, not mandatory
$d = 'PARTIALS';
$x = APP . DS . 'partials';
if (is_dir($x) && is_readable($x)) {
defined($d) || define($d, $x);
} else {
defined($d) || define($d, '/tmp');
}
// DOWNLOAD storage = files generated by Tesseract, not mandatory
$d = 'DOWNLOAD';
$x = WWW . DS . 'download';
if (is_dir($x) && is_readable($x)) {
defined($d) || define($d, $x);
} else {
defined($d) || define($d, null);
}
// UPLOAD storage = files uploaded by users, not mandatory
$d = 'UPLOAD';
$x = WWW . DS . 'upload';
if (is_dir($x) && is_readable($x)) {
defined($d) || define($d, $x);
} else {
defined($d) || define($d, null);
}
// LOGS storage for Tracy, not mandatory
$d = 'LOGS';
$x = ROOT . DS . 'logs';
if (is_dir($x) && is_writable($x)) {
defined($d) || define($d, $x);
} else {
defined($d) || define($d, null);
}
// TEMP files storage, not mandatory
$d = 'TEMP';
$x = ROOT . DS . 'temp';
if (is_dir($x) && is_writable($x)) {
defined($d) || define('TEMP', $x);
} else {
defined($d) || define($d, '/tmp');
}
// running from CLI?
defined('CLI') || define('CLI', (bool) (PHP_SAPI === 'cli'));
// running on localhost?
defined('LOCALHOST') || define(
'LOCALHOST', (bool) (($_SERVER['SERVER_NAME'] ?? '') === 'localhost') || CLI
);
// Composer autoloader
require_once ROOT . DS . 'vendor' . DS . 'autoload.php';
// load CONFIGURATION
$cfg = null;
if (file_exists(CONFIG) && is_readable(CONFIG)) {
$cfg_content = file_get_contents(CONFIG);
if ($cfg_content) {
try {
$cfg = Neon::decode($cfg_content);
} catch (\Nette\Neon\Exception $e) {
error_log("Error parsing NE-ON file: " . CONFIG);
die('FATAL ERROR: '. $e->getMessage());
}
} else {
$cfg = null;
}
if (!is_array($cfg)) {
die('FATAL ERROR: INVALID CONFIG');
}
try {
if (file_exists(CONFIG_PRIVATE) && is_readable(CONFIG_PRIVATE)) {
$arr = null;
if ($content = file_get_contents(CONFIG_PRIVATE)) {
$arr = Neon::decode($content);
}
if (!is_array($arr)) {
error_log("Error parsing NE-ON file: " . CONFIG_PRIVATE);
die('INVALID PRIVATE CONFIG!');
}
$cfg = array_replace_recursive($cfg, $arr);
}
} catch (\Nette\Neon\Exception $e) {
die('FATAL ERROR: ' . $e->getMessage());
}
try {
if (!file_exists(ROOT . DS . '.env')) {
if (file_exists(CONFIG_DOCKER) && is_readable(CONFIG_DOCKER)) {
$arr = null;
if ($content = file_get_contents(CONFIG_DOCKER)) {
$arr = Neon::decode($content);
}
if (!is_array($arr)) {
error_log("Error parsing NE-ON file: " . CONFIG_DOCKER);
die('INVALID DOCKER CONFIG!');
}
$cfg = array_replace_recursive($cfg, $arr);
}
}
} catch (\Nette\Neon\Exception $e) {
die('FATAL ERROR: ' . $e->getMessage());
}
} else {
error_log('FATAL ERROR: CONFIG file not found!');
die('FATAL ERROR: CONFIG file not found!');
}
if (!is_array($cfg)) {
die('FATAL ERROR: INVALID CONFIG!');
}
// DEFAULT TIME ZONE
date_default_timezone_set(
(string) ($cfg['date_default_timezone'] ?? 'Europe/Prague')
);
// DEBUGGER
if (CLI === true) {
defined('DEBUG') || define('DEBUG', false);
}
if (($_SERVER['SERVER_NAME'] ?? '') === 'localhost') {
if (($cfg['dbg'] ?? null) === false) {
defined('DEBUG') || define('DEBUG', false); // DISABLED - configuration
}
defined('DEBUG') || define('DEBUG', true); // ENABLED - localhost
}
if (isset($_SERVER['HTTP_USER_AGENT'])) {
if (strpos($_SERVER['HTTP_USER_AGENT'], 'curl') !== false) {
defined('DEBUG') || define('DEBUG', false); // DISABLED - curl
}
}
defined('DEBUG') || define('DEBUG', (bool) ($cfg['dbg'] ?? false));
if (DEBUG === true) {
// https://api.nette.org/tracy/master/Tracy.html
// https://www.php.net/manual/en/errorfunc.constants.php
Debugger::$logDirectory = LOGS;
Debugger::$logSeverity = 15;
Debugger::$maxDepth = (int) ($cfg['DEBUG_MAX_DEPTH'] ?? 10);
Debugger::$maxLength = (int) ($cfg['DEBUG_MAX_LENGTH'] ?? 5000);
Debugger::$scream = (bool) ($cfg['DEBUG_SCREAM'] ?? true);
Debugger::$showBar = (bool) ($cfg['DEBUG_SHOW_BAR'] ?? true);
Debugger::$showFireLogger = (bool) ($cfg['DEBUG_SHOW_FIRELOGGER'] ?? false);
Debugger::$showLocation = (bool) ($cfg['DEBUG_SHOW_LOCATION'] ?? false);
Debugger::$strictMode = (bool) ($cfg['DEBUG_STRICT_MODE'] ?? true);
// debug cookie name: tracy-debug
if ($cfg['DEBUG_COOKIE'] ?? null) {
if (($_COOKIE['tracy-debug'] ?? null) === $cfg['DEBUG_COOKIE']) {
Debugger::enable(Debugger::Development);
} else {
Debugger::enable(Debugger::Production);
}
} else {
Debugger::enable(
(bool) ($cfg['DEBUG_DEVELOPMENT_MODE'] ?? true)
? Debugger::DEVELOPMENT : Debugger::DETECT, LOGS
);
}
}
Debugger::timer('RUN');
require_once APP . DS . 'App.php';