/
core.php
executable file
·418 lines (343 loc) · 13.1 KB
/
core.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
//
// Copyright (C) 2006-2017 Next Generation CMS (http://ngcms.ru)
// Name: core.php
// Description: core
// Author: NGCMS project team
//
// Configure error display mode
@error_reporting(E_ALL ^ E_NOTICE);
// ============================================================================
// Define global directory constants
// ============================================================================
@define('NGCoreDir', __DIR__.'/'); // Location of Core directory
@define('NGRootDir', dirname(__DIR__).'/'); // Location of SiteRoot
@define('NGClassDir', NGCoreDir.'classes/'); // Location of AutoLoaded classes
@define('NGVendorDir', NGRootDir.'vendor/'); // Location of Vendor classes
$loader = require NGVendorDir.'autoload.php';
// Autoloader for NEW STYLE Classes
spl_autoload_register(function ($className) {
if (file_exists($fName = NGClassDir.$className.'.class.php')) {
require_once $fName;
}
});
// Magic function for immediate closure call
function NGRun($f)
{
$f();
}
// ============================================================================
// MODULE DEPs check + basic setup
// ============================================================================
NGRun(function () {
$depList = [
'sql' => ['pdo' => '', 'pdo_mysql' => ''],
'zlib' => 'ob_gzhandler',
'iconv' => 'iconv',
'GD' => 'imagecreatefromjpeg',
'mbstring' => 'mb_internal_encoding',
];
NGCoreFunctions::resolveDeps($depList);
$sx = NGEngine::getInstance();
$sx->set('events', new NGEvents());
$sx->set('errorHandler', new NGErrorHandler());
});
// ============================================================================
// Global variables definition
// ============================================================================
global $PLUGINS, $EXTRA_HTML_VARS, $EXTRA_CSS;
global $AUTH_METHOD, $AUTH_CAPABILITIES, $PPAGES, $PFILTERS, $RPCFUNC, $TWIGFUNC;
global $RPCADMFUNC, $SUPRESS_TEMPLATE_SHOW, $SUPRESS_MAINBLOCK_SHOW, $SYSTEM_FLAGS;
global $DSlist, $PERM, $confPerm, $confPermUser, $systemAccessURL, $cron;
global $timer, $mysql, $ip, $parse, $tpl, $lang, $config;
global $TemplateCache, $siteDomainName;
global $currentHandler, $ngTrackID, $ngCookieDomain;
global $twigGlobal, $twig, $twigLoader, $twigStringLoader;
global $multiDomainName;
// ============================================================================
// Initialize global variables
// ============================================================================
$EXTRA_HTML_VARS = []; // a list of added HTML vars in <head> block
$EXTRA_CSS = [];
$AUTH_METHOD = [];
$AUTH_CAPABILITIES = [];
$PPAGES = []; // plugin's pages
$PFILTERS = []; // filtering plugins
$RPCFUNC = []; // RPC functions
$TWIGFUNC = []; // TWIG defined functions
$RPCADMFUNC = []; // RPC admin functions
$PERM = []; // PERMISSIONS
$UGROUP = []; // USER GROUPS
$SUPRESS_TEMPLATE_SHOW = 0;
$SUPRESS_MAINBLOCK_SHOW = 0;
$CurrentHandler = [];
$TemplateCache = [];
$lang = [];
$SYSTEM_FLAGS = [
'actions.disabled' => [],
'http.headers' => [
'content-type' => 'text/html; charset=utf-8',
'cache-control' => 'private',
],
];
$twigGlobal = [
'flags' => [
'isLogged' => 0,
],
];
// List of DataSources
$DSlist = [
'news' => 1,
'categories' => 2,
'comments' => 3,
'users' => 4,
'files' => 10,
'images' => 11,
'#xfields:tdata' => 51,
];
$PLUGINS = [
'active' => [],
'active:loaded' => 0,
'loaded' => [],
'loaded:files' => [],
'config' => [],
'config:loaded' => 0,
];
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
// Define global constants "root", "site_root"
@define('root', __DIR__.'/');
@define('site_root', dirname(__DIR__).'/');
// Define domain name for cookies
$ngCookieDomain = preg_match('#^www\.(.+)$#', $_SERVER['HTTP_HOST'], $mHost) ? $mHost[1] : $_SERVER['HTTP_HOST'];
// Remove non-standart port from domain
if (preg_match("#^(.+?)\:\d+$#", $ngCookieDomain, $m)) {
$ngCookieDomain = $m[1];
}
// Manage trackID cookie - can be used for plugins that don't require authentication,
// but need to track user according to his ID
if (!isset($_COOKIE['ngTrackID'])) {
$ngTrackID = md5(md5(uniqid(rand(), 1)));
@setcookie('ngTrackID', $ngTrackID, time() + 86400 * 365, '/', $ngCookieDomain, 0, 1);
} else {
$ngTrackID = $_COOKIE['ngTrackID'];
}
// Initialize last variables
$confArray = [
// Pre-defined init values
'predefined' => [
'HTTP_REFERER' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
'PHP_SELF' => isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '',
'REQUEST_URI' => isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '',
'config' => [],
'catz' => [],
'catmap' => [],
'is_logged' => false,
],
];
// Load pre-defined variables
$predefinedUnsetArray = [
'_GET',
'_POST',
'_SESSION',
'_COOKIE',
'_ENV',
];
foreach ($confArray['predefined'] as $key => $value) {
foreach ($predefinedUnsetArray as $arr) {
if (isset($$arr, $$arr[$key])) {
unset($$arr[$key]);
}
}
$$key = $value;
}
// Prepare variable with access URL
$systemAccessURL = $_SERVER['REQUEST_URI'];
if (($tmp_pos = strpos($systemAccessURL, '?')) !== false) {
$systemAccessURL = mb_substr($systemAccessURL, 0, $tmp_pos);
}
// ============================================================================
// Initialize system libraries
// ============================================================================
// ** Time measurement functions
include_once root.'includes/classes/timer.class.php';
$timer = new microTimer();
$timer->start();
// ** Multisite engine
include_once root.'includes/inc/multimaster.php';
multi_multisites();
/**
* @var $multiDomainName
* @var $multimaster
*/
@define('confroot', root.'conf/'.($multiDomainName && $multimaster && ($multiDomainName != $multimaster) ? 'multi/'.$multiDomainName.'/' : ''));
// ** Load system config
include_once confroot.'config.php';
// [[FIX config variables]]
if (!isset($config['uprefix'])) {
$config['uprefix'] = $config['prefix'];
}
// Set up default timezone [ default: Europe/Moscow ]
date_default_timezone_set($config['timezone'] ?? 'Europe/Moscow');
// [[MARKER]] Configuration file is loaded
$timer->registerEvent('Config file is loaded');
// Call multidomains processor
multi_multidomains();
//print "siteDomainName [".$siteDomainName."]<br/>\n";
// Initiate session - take care about right domain name for sites with/without www. prefix
//print "<pre>".var_export($_SERVER, true).var_export($_COOKIE, true)."</pre>";
@session_set_cookie_params(86400, '/', $ngCookieDomain);
@session_start();
// ** Load system libraries
include_once root.'includes/inc/consts.inc.php';
include_once root.'includes/inc/functions.inc.php';
include_once root.'includes/inc/extras.inc.php';
include_once 'includes/classes/templates.class.php';
include_once 'includes/classes/parse.class.php';
include_once 'includes/classes/uhandler.class.php';
// [[MARKER]] All system libraries are loaded
$timer->registerEvent('Core files are included');
// ** Activate URL processing library
$UHANDLER = new urlHandler();
$UHANDLER->loadConfig();
// ** Other libraries
$parse = new parse();
$tpl = new tpl();
$ip = checkIP();
// ** Load configuration file
if ((!file_exists(confroot.'config.php')) || (filesize(confroot.'config.php') < 10)) {
if (preg_match("#^(.*?)(\/index\.php|\/engine\/admin\.php)$#", $_SERVER['PHP_SELF'], $ms)) {
@header('Location: '.$ms[1].'/engine/install.php');
} else {
@header('Location: '.adminDirName.'/install.php');
}
echo 'NGCMS: Engine is not installed yet. Please run installer from /engine/install.php';
exit;
}
// ** Load user groups
loadGroups();
// ** Init our own exception handler
set_exception_handler('ngExceptionHandler');
set_error_handler('ngErrorHandler');
register_shutdown_function('ngShutdownHandler');
//
// *** Initialize TWIG engine
$twigLoader = new NGTwigLoader(root);
// - Configure environment and general parameters
$twig = new NGTwigEnvironment($twigLoader, [
'cache' => root.'cache/twig/',
'auto_reload' => true,
'autoescape' => false,
'charset' => 'UTF-8',
]);
// [[MARKER]] TWIG template engine is loaded
$timer->registerEvent('Template engine is activated');
// Give domainName to URL handler engine for generating absolute links
$UHANDLER->setOptions(['domainPrefix' => $config['home_url']]);
// Check if engine is installed in subdirectory
if (preg_match('#^http\:\/\/([^\/])+(\/.+)#', $config['home_url'], $match)) {
$UHANDLER->setOptions(['localPrefix' => $match[2]]);
}
// ** Load cache engine
include_once root.'includes/classes/cache.class.php';
// NEW :: PDO driver with global classes handler
NGRun(function () {
global $config, $mysql;
$sx = NGEngine::getInstance();
$sx->set('db', new NGPDO(['host' => $config['dbhost'], 'user' => $config['dbuser'], 'pass' => $config['dbpasswd'], 'db' => $config['dbname'], 'charset' => 'utf8']));
$sx->set('config', $config);
$sx->set('legacyDB', new NGLegacyDB(false));
$sx->getLegacyDB()->connect('', '', '');
$mysql = $sx->getLegacyDB();
// Sync PHP <=> MySQL timezones
$mysql->query('SET @@session.time_zone = "'.date('P').'"');
});
// [[MARKER]] MySQL connection is established
$timer->registerEvent('DB connection established');
// ** Load categories from DB
ngLoadCategories();
// [[MARKER]] Categories are loaded
$timer->registerEvent('DB category list is loaded');
// ** Load compatibility engine [ rewrite old links ]
if ($config['libcompat']) {
include_once root.'includes/inc/libcompat.php';
compatRedirector();
}
//
// Special way to pass authentication cookie via POST params
if (!isset($_COOKIE['zz_auth']) && isset($_POST['ngAuthCookie'])) {
$_COOKIE['zz_auth'] = $_POST['ngAuthCookie'];
}
// [[MARKER]] Ready to load auth plugins
$timer->registerEvent('Ready to load auth plugins');
loadActionHandlers('auth');
// ** Load user's permissions DB
loadPermissions();
// ============================================================================
// Initialize system libraries
// ============================================================================
// System protection
if (!$AUTH_CAPABILITIES[$config['auth_module']]['login']) {
$config['auth_module'] = 'basic';
}
if (!$AUTH_CAPABILITIES[$config['auth_db']]['db']) {
$config['auth_db'] = 'basic';
}
if ((is_object($AUTH_METHOD[$config['auth_module']])) && (is_object($AUTH_METHOD[$config['auth_db']]))) {
// Auth subsystem is activated
// * choose default or user defined auth module
if (isset($_REQUEST['auth_module']) && $AUTH_CAPABILITIES[$_REQUEST['auth_module']]['login'] && is_object($AUTH_METHOD[$_REQUEST['auth_module']])) {
$auth = &$AUTH_METHOD[$_REQUEST['auth_module']];
} else {
$auth = &$AUTH_METHOD[$config['auth_module']];
}
$auth_db = &$AUTH_METHOD[$config['auth_db']];
$xrow = $auth_db->check_auth();
$CURRENT_USER = $xrow;
if (is_array($xrow)) {
NGEngine::getInstance()->set('currentUser', new NGUser($xrow));
}
if (isset($xrow['name']) && $xrow['name']) {
$is_logged_cookie = true;
$is_logged = true;
$username = $xrow['name'];
$userROW = $xrow;
if ($config['x_ng_headers']) {
header('X-NG-UserID: '.(int) $userROW['id']);
header('X-NG-Login: '.htmlentities($username));
}
// - Now every TWIG template will know if user is logged in
$twigGlobal['flags']['isLogged'] = 1;
$twigGlobal['user'] = $userROW;
//$twig->addGlobalRef('user', $userROW);
}
} else {
echo "Fatal error: No auth module is found.<br />Configuration is damaged, please restore from backup or perform manual fix.<br />\n";
}
// [[MARKER]] Authentification process is complete
$timer->registerEvent('Auth procedure is finished');
if ($is_logged) {
@define('name', $userROW['name']);
}
// Init internal cron module
$cron = new cronManager();
// ** Load action handlers for action 'all'
loadActionHandlers('all');
$timer->registerEvent('ALL core-related plugins are loaded');
// ** Execute 'core' action handler
executeActionHandler('core');
$timer->registerEvent('ALL core-related plugins are executed');
// Define last consts
define('tpl_site', site_root.'templates/'.$config['theme'].'/');
define('tpl_url', home.'/templates/'.$config['theme']);
// - TWIG: Reconfigure allowed template paths - site template is also available
$twigLoader->setPaths([tpl_site, root]);
// - TWIG: Added global variable `tpl_url`, `scriptLibrary`
$twig->addGlobal('tpl_url', tpl_url);
$twig->addGlobal('scriptLibrary', scriptLibrary);
// Lang files are loaded _after_ executing core scripts. This is done for switcher plugin
$lang = LoadLang('common');
$lang = LoadLangTheme();
$langShortMonths = explode(',', $lang['short_months']);
$langMonths = explode(',', $lang['months']);
$timer->registerEvent('* CORE.PHP is complete');