-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeterminator.php
More file actions
382 lines (355 loc) · 13.3 KB
/
Copy pathDeterminator.php
File metadata and controls
382 lines (355 loc) · 13.3 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
namespace {
/**
* if does not determine root
*/
if (!defined('ROOT')) {
return;
}
/**
* Code Igniter Version
*/
!defined('CI_VERSION') && define('CI_VERSION', '3.0.6');
/**
* Unique system admin prefix
*/
define('ADMIN_URL_SEPARATOR_PREFIX', 'm:');
/**
* List of system Models name
*/
define('MODEL_NAME_OPTION', 'model.option');
define('MODEL_NAME_TABLE', 'model.table');
define('MODEL_NAME_TEMPLATE_ADMIN', 'model.template.admin');
define('MODEL_NAME_TEMPLATE_USER', 'model.template.user');
define('MODEL_NAME_NOTICE', 'model.notice');
define('MODEL_NAME_USER', 'model.user');
define('MODEL_NAME_ADMIN', 'model.admin');
define('MODEL_NAME_AUTH', 'model.auth');
define('MODEL_NAME_ADMIN_MENU', 'model.menu.admin');
define('MODEL_NAME_USER_MENU', 'model.menu.user');
/**
* if not our script
*/
if (isset($_SERVER['SCRIPT_FILENAME']) && realpath($_SERVER['SCRIPT_FILENAME']) != realpath(ROOT)) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'Invalid File Requested.';
exit(1);
}
// on cli remove arguments
if (!(PHP_SAPI === 'cli' || defined('STDIN'))) {
unset($argv, $argc);
}
// Set the current directory correctly for CLI requests
if (defined('STDIN')) {
chdir(dirname(ROOT));
}
if (empty($GLOBALS) || !is_array($GLOBALS)) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'GLOBALS variable has been override';
exit(3); // EXIT_CONFIG
}
/**
* Reset Globals for security Reason
* delete all variable if possible
*/
array_map(
function($c) {
unset($GLOBALS[$c]);
},
array_diff(
array_keys($GLOBALS),
array(
'GLOBALS',
'_COOKIE',
'_ENV',
'_FILES',
'_GET',
'_POST',
'_REQUEST',
'_SERVER',
'_SESSION',
'argc',
'argv',
'HTTP_RAW_POST_DATA',
'http_response_header',
'php_errormsg'
)
)
);
// config file
if (!defined('CONFIG')) {
define('CONFIG', dirname(ROOT) . DIRECTORY_SEPARATOR . 'config.php');
}
if (defined('CONSTANT_COUNT')) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'Root files only allowed set constant ROOT or CONFIG';
exit(1); // EXIT_ERROR
}
// cache the constant
define('CONSTANT_COUNT', count(get_defined_constants()));
if (is_file(CONFIG)) {
/** @noinspection PhpIncludeInspection */
$config = require CONFIG;
}
/**
* if not defined Environment Constant
*/
if (defined('ENVIRONMENT')) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'Environment must be declared on configuration file.';
exit(3); // EXIT_CONFIG
}
if (isset($config) || CONSTANT_COUNT <> (count(get_defined_constants()) - 1)) {
$intersect = array_intersect(
array_keys(get_defined_constants()),
array(
'SOURCE', 'SOURCEPATH', 'CONTROLLERPATH',
'MODELPATH', 'VIEWPATH', 'CONFIGPATH',
'MODULEPATH', 'LANGUAGEPATH', 'ADMINTEMPLATEPATH',
'RESOURCEPATH', 'APPPATH', 'FCPATH', 'BASEPATH',
'SYSDIR', 'ASSETPATH', 'UPLOADPATH', 'ADMINPATH',
'DYNAMICPATH', 'ENGINE_SALT', 'TEMPLATEPATH', 'TEMPPATH',
'FILE_READ_MODE', 'FILE_WRITE_MODE', 'DIR_READ_MODE', 'DIR_WRITE_MODE',
'FOPEN_READ', 'FOPEN_READ_WRITE', 'FOPEN_WRITE_CREATE_DESTRUCTIVE',
'FOPEN_READ_WRITE_CREATE_DESTRUCTIVE', 'FOPEN_WRITE_CREATE',
'FOPEN_READ_WRITE_CREATE', 'FOPEN_WRITE_CREATE_STRICT', 'FOPEN_READ_WRITE_CREATE_STRICT',
'EXIT_SUCCESS', 'EXIT_ERROR', 'EXIT_CONFIG', 'EXIT_UNKNOWN_FILE',
'EXIT_UNKNOWN_CLASS', 'EXIT_UNKNOWN_METHOD', 'EXIT_USER_INPUT',
'EXIT_DATABASE', 'EXIT__AUTO_MIN', 'EXIT__AUTO_MAX'
)
);
if (!empty($intersect)) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'There was defined system constant. System constant could not defined before init : (' .explode(', ', $intersect) .')';
exit(3); // EXIT_CONFIG
}
}
! defined('DS') && define('DS', DIRECTORY_SEPARATOR);
if (DS !== DS) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'There was defined system constant. Constant `DS` must be equal with DS';
exit(3); // EXIT_CONFIG
}
$configs = isset($config) ? $config : null;
/*
|--------------------------------------------------------------------------
| Directory
|--------------------------------------------------------------------------
*/
$system = 'System';
$resource = 'Resource';
$view = 'Views';
$controller = 'Controller';
$config = 'Config';
$model = 'Models';
$temp = 'Temp';
$module = 'Modules';
$template = 'template';
$asset = 'assets';
$upload = 'uploads';
$admin = 'admin';
$dynamic_asset = 'dynamic_asset';
if (!empty($configs['path']) && is_array($configs['path'])) {
foreach (array(
'system',
'resource',
'view',
'controller',
'config',
'model',
'temp',
'module',
'template',
'asset',
'upload',
'admin',
'dynamic_asset'
) as $v) {
if (!empty($configs['path'][$v])) {
if (!is_string($configs['path'][$v])) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
printf('Invalid setting for `%s` path', $v);
exit(3);
}
if (trim($configs['path'][$v]) != '') {
$$v = preg_replace('/(\\\|\/)+/', DS, $configs['path'][$v]);
}
}
}
}
define('SOURCE', realpath(__DIR__)); //current source dir
define('SOURCEPATH', SOURCE . DS); //current source dir
define('CONTROLLERPATH', SOURCEPATH . $controller . DS);
define('MODELPATH', SOURCEPATH . $model . DS);
define('VIEWPATH', SOURCEPATH . $view . DS);
define('CONFIGPATH', SOURCEPATH . $config . DS);
define('MODULEPATH', SOURCEPATH . $module . DS);
define('LANGUAGEPATH', SOURCEPATH . 'Languages' . DS);
define('ADMINTEMPLATEPATH', SOURCEPATH . 'AdminTemplates' . DS); // admin template
define('RESOURCEPATH', SOURCE . DS . $resource .DS); // alias an application path
define('APPPATH', RESOURCEPATH);
define('FCPATH', dirname(realpath(ROOT)) . DS);// Path to the front controller (this file) directory
define('BASEPATH', RESOURCEPATH . $system . DS);// Path to the system directory
define('SYSDIR', basename(BASEPATH)); // Name of the "system" directory
define('ASSETPATH', FCPATH . $asset . DS); // Name of the "asset" directory
define('UPLOADPATH', FCPATH . $upload . DS); // Name of the "upload" directory
$admin = str_replace(DIRECTORY_SEPARATOR ,'/', strtolower($admin));
define('ADMINPATH',trim($admin, '/')); // Name of the "admin" directory
$dynamic_asset = str_replace(DIRECTORY_SEPARATOR, '/', strtolower($dynamic_asset));
define('DYNAMICPATH',trim($dynamic_asset, '/')); // Name of the "dynamic asset route" directory
define('ENGINE_SALT', sha1(FCPATH . SOURCEPATH . CONSTANT_COUNT));
/**
* Validate Template Path
*/
if (substr($template,0, 1) == '/') {
$template_ = realpath($template);
if ($template_) {
$template = $template_;
} else {
$template = FCPATH . $template . DS;
}
define('TEMPLATEPATH', $template);
} else {
define('TEMPLATEPATH', FCPATH . $template . DS);
}
/**
* Validate Temp path
*/
if ($temp != 'Temp') {
$temp = SOURCEPATH . $temp;
} else {
if (!realpath($temp) || ! is_dir(FCPATH . $temp)) {
$temp = SOURCEPATH . 'Temp';
if (!is_dir($temp)) {
@mkdir($temp, '755', true);
}
} elseif (is_dir(FCPATH . $temp)) {
$temp = FCPATH . $temp;
} else {
$temp = SOURCEPATH . 'Temp';
}
}
define('TEMPPATH', $temp . DS); // Name of the "temp" directory
/**
* Check Upload Path
*/
if (strpos(UPLOADPATH, realpath(SOURCEPATH)) === 0) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'Invalid setting for `upload` path. Upload path must be not in engine application directory';
exit(3); // EXIT_CONFIG
}
if (strpos(UPLOADPATH, realpath(TEMPLATEPATH)) === 0) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'Invalid setting for `upload` path. Upload path must be not in template directory';
exit(3); // EXIT_CONFIG
}
if (strpos(UPLOADPATH, realpath(ASSETPATH)) === 0) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'Invalid setting for `upload` path. Upload path must be not in asset directory';
exit(3); // EXIT_CONFIG
}
if (strpos(UPLOADPATH, realpath(FCPATH)) !== 0) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'Invalid setting for `upload` path. Upload path must be in root directory';
exit(3); // EXIT_CONFIG
}
/**
* Re CHecking Upload Path
*/
if (!is_dir(UPLOADPATH)) {
if (file_exists(UPLOADPATH)) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'Looks like your upload path exist and as not a directory.';
exit(3); // EXIT_CONFIG
} elseif (!@mkdir(UPLOADPATH, 755, true)) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'Could not create upload directory';
exit(3); // EXIT_CONFIG
}
if (!file_exists(UPLOADPATH .'index.html') && is_writable(UPLOADPATH)) {
// add index
@file_put_contents(UPLOADPATH . 'index.html', '');
}
}
/**
* Check Directory Existences
*/
foreach (array(
SOURCEPATH => 'Source',
BASEPATH => 'System',
APPPATH => 'Application',
CONTROLLERPATH => 'Controller',
VIEWPATH => 'Views',
MODELPATH => 'Model',
MODULEPATH => 'Module',
LANGUAGEPATH => 'Language',
ADMINTEMPLATEPATH => 'Admin Templates',
TEMPLATEPATH => 'Templates',
TEMPPATH => 'Temporary',
ASSETPATH => 'assets',
) as $k => $v) {
if (!is_dir($k)) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'Directory ' . $v . ' Does Not exist. Please check your configuration or your folder on source';
}
}
/*
|--------------------------------------------------------------------------
| Display Debug backtrace
|--------------------------------------------------------------------------
*/
!defined('SHOW_DEBUG_BACKTRACE') && define('SHOW_DEBUG_BACKTRACE', true);
/*
|--------------------------------------------------------------------------
| File and Directory Modes
|--------------------------------------------------------------------------
*/
define('FILE_READ_MODE', 0644);
define('FILE_WRITE_MODE', 0666);
define('DIR_READ_MODE', 0755);
define('DIR_WRITE_MODE', 0755);
/*
|--------------------------------------------------------------------------
| File Stream Modes
|--------------------------------------------------------------------------
*/
define('FOPEN_READ', 'rb');
define('FOPEN_READ_WRITE', 'r+b');
define('FOPEN_WRITE_CREATE_DESTRUCTIVE', 'wb'); // truncates existing file data, use with care
define('FOPEN_READ_WRITE_CREATE_DESTRUCTIVE', 'w+b'); // truncates existing file data, use with care
define('FOPEN_WRITE_CREATE', 'ab');
define('FOPEN_READ_WRITE_CREATE', 'a+b');
define('FOPEN_WRITE_CREATE_STRICT', 'xb');
define('FOPEN_READ_WRITE_CREATE_STRICT', 'x+b');
/*
|--------------------------------------------------------------------------
| Exit Status Codes
|--------------------------------------------------------------------------
*/
define('EXIT_SUCCESS', 0); // no errors
define('EXIT_ERROR', 1); // generic error
define('EXIT_CONFIG', 3); // configuration error
define('EXIT_UNKNOWN_FILE', 4); // file not found
define('EXIT_UNKNOWN_CLASS', 5); // unknown class
define('EXIT_UNKNOWN_METHOD', 6); // unknown class member
define('EXIT_USER_INPUT', 7); // invalid user input
define('EXIT_DATABASE', 8); // database error
define('EXIT__AUTO_MIN', 9); // lowest automatically-assigned error code
define('EXIT__AUTO_MAX', 125); // highest automatically-assigned error code
unset($k,
$configs,
$config,
$v,
$system ,
$resource,
$view,
$controller,
$config,
$model,
$temp,
$module,
$template,
$asset,
$upload
);
}