/
common.php
539 lines (472 loc) · 20.6 KB
/
common.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<?php
/**
* Common Setup File
*
* This file initializes up most variables for the site. It is also where most files
* are included from. It also reads and stores certain variables.
*
* @package GetSimple
* @subpackage init
*/
define('IN_GS', TRUE); // GS enviroment flag
// GS Debugger
GLOBAL $GS_debug; // GS debug trace array
if(!isset($GS_debug)) $GS_debug = array();
// @todo remove for production
// debug catcher for this core wide change issues
if(htmlentities($_SERVER['SCRIPT_NAME'], ENT_QUOTES) !== htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES)) die('PHP_SELF mismatch ' . htmlentities($_SERVER['PHP_SELF']));
/**
* Set PHP enviroments
*/
if(function_exists('mb_internal_encoding')) mb_internal_encoding("UTF-8"); // set multibyte encoding
/**
* GSCONFIG definitions
*/
$GS_constants = array(
'GSROOTPATH' => getGSRootPath(), // root path of getsimple
"GSDATEFORMAT" => "M j, Y", // short date-only format fallback
"GSDATEANDTIMEFORMAT" => "F jS, Y - g:i A", // date/time format fallback
"GSTIMEFORMAT" => "g:i A", // time only format fallback
'GSTARTTIME' => microtime(), // micro time stamp for gs loaded
'GSBASE' => false, // front end flag
'GSCONFIGFILE' => 'gsconfig.php', // config filename
'GSWEBSITEFILE' => 'website.xml', // website data filename
'GSCOMPONENTSFILE' => 'components.xml', // components data filename
'GSSNIPPETSFILE' => 'snippets.xml', // snippets data filename
'GSAUTHFILE' => 'authorization.xml', // authorizaton salt data filename
'GSCSSMAINFILE' => 'css.php', // main css file name
'GSCSSCUSTOMFILE' => 'admin.css', // custom css file name
'GSTEMPLATEFILE' => 'template.php', // default template file name
'GSINSTALLTEMPLATE' => 'Innovation', // template to set on install
'GSINSTALLPLUGINS' => 'InnovationPlugin.php', // comma delimited list of plugins to activate on install
'GSSTYLEWIDE' => 'wide', // wide stylesheet
'GSSTYLE_SBFIXED' => 'sbfixed', // fixed sidebar
'GSFRONT' => 1, // (int) front end enum
'GSBACK' => 2, // (int) back end enum
'GSBOTH' => 3, // (int) front and back enum
'GSDEFAULTLANG' => 'en_US', // (str) default language for core
'GSTITLEMAX' => 70, // (int) max length allowed for titles
'GSFILENAMEMAX' => 255, // (int) max length allowed for file names/slugs
'GSPASSLENGTHMIN' => 4, // (itn) min length of passwords
'GSBAKFILESUFFIX' => '', // (str) backup file naming suffix after extension
'GSBAKFILEPREFIX' => '.bak', // (str) backup file naming prefix before extension
'GSRESETFILESUFFIX' => '.reset', // (str) password reset file naming suffix before extension
'GSRESETFILEPREFIX' => '', // (str) password reset file naming prefix after extension
'GSSHOWCODEHINTS' => true, // (bool) show code hints on components page and snippets etc.
'GSHTMLEDITINLINE' => false, // (bool) show html cke editors inline
# -----------------------------------------------------------------------------------------------------------------------------------------------
'GSCONSTANTSLOADED' => true // $GS_constants IS LOADED FLAG
);
$GS_definitions = array(
'GSDEFAULTPAGE' => 'pages.php', // (str) Default backend index page
'GSHEADERCLASS' => '', // (str) custom class to add to header eg. gradient
'GSHTTPPREFIX' => '', // (str) http slug prefix GSHTTPPREFIX.GSSLUGxx
'GSSLUGNOTFOUND' => '404', // (str) http slug for not found
'GSSLUGPRIVATE' => '403', // (str) http slug for private pages
'GSADMIN' => 'admin', // (str) admin foldername
'GSSITEMAPFILE' => 'sitemap.xml', // (str) sitemap file name, must modify in .htaccess as needed
'GSERRORLOGFILE' => 'errorlog.txt', // (str) error log filename
'GSERRORLOGENABLE' => true, // (bool) should GS log php errors to GSERRORLOGFILE
'GSSTYLE' => 'wide,sbfixed', // (str-csv) default style modifiers
'GSWIDTH' => '1024px', // (str) pagewidth on backend,(max-width), null,'none','' for 100% width
'GSWIDTHWIDE' => '1366px', // (str) page width on backend pages defined in GSWIDEPAGES, values as above
'GSWIDEPAGES' => 'theme-edit,components,snippets', // (str-csv) pages to apply GSWIDTHWIED on
'GSALLOWLOGIN' => true, // (bool) allow front end login
'GSALLOWRESETPASS' => true, // (bool) allow front end password resets
'GSTHEMEEDITEXTS' => 'php,css,js,html,htm,txt,xml,', // (str-csv) file extensions to show and edit in theme editor
'GSASSETSCHEMES' => false, // (bool) should $ASSETURL contain the url scheme http|https
'GSASSETURLREL' => true, // (bool) Use root relative urls for $ASSETURL, overrides GSASSETSCHEMES
'GSSITEURLREL' => true, // (bool) Use root relative urls for $SITEURL
'GSALLOWDOWNLOADS' => true, // (bool) allow using downloads.php to download files from /uploads and backups/zip
'GSEDITORHEIGHT' => '500', // (str) wysiwyg editor height in px
'GSEDITORTOOL' => 'basic', // (str) wysiwyg editor toobar
'GSEDITORCONFIGFILE' => 'config.js', // (str) wysiwyg editor toobar
'GSEMAILLINKBACK' => 'http://get-simple.info/', // (str) url used in email template
'GSAJAXSAVE' => true, // (bool) use ajax for saving themes, components, and pages
'GSCHMOD' => 0644, // (octal) chmod mode legacy
'GSCHMODFILE' => 0644, // (octal) chmod mode for files
'GSCHMODDIR' => 0755, // (octal) chmod mode for dirs
'GSDOCHMOD' => true, // (bool) perform chmod after creating files or directories
'GSDEBUGINSTALL' => false, // (bool) debug installs, prevent removal of installation files (install,setup,update)
'GSDEBUGAPI' => false, // (bool) debug api calls
# -----------------------------------------------------------------------------------------------------------------------------------------------
'GSDEFINITIONSLOADED' => true // (bool) $GS_definitions IS LOADED FLAG
);
/* Define Constants */
GS_defineFromArray($GS_constants);
/**
* Variable Globalization
*/
global
$TEMPLATE, // (str) current theme
$GSADMIN, // (str) admin foldername
$GS_debug, // (array) global array for storing debug log entries
$components, // (array) global array for storing components, array of objs from components.xml
$snippets, // (array) global array for storing snippets, array of objs from snippets.xml
$nocache, // (bool) disable site wide cache true, not fully implemented
$microtime_start,// (microtime) used for benchmark timers
$pagesArray, // (array) global array for storing pages cache, used for all page fields aside from content
$pageCacheXml, // (obj) page cache raw xml simpleXMLobj
$plugins_info, // (array) contains registered plugin info for active and inactive plugins
$live_plugins, // (array) contains plugin file ids and enable status
$plugins, // (array) global array for storing action hook callbacks
$filters, // (array) global array for storing action filter callbacks
$GS_scripts, // (array) global array for storing queued asset scripts
$GS_styles // (array) global array for storing queued asset styles
;
if(isset($_GET['nocache'])){
// @todo: disables caching, this should probably only be allowed for auth users, it is also not well inplemented
$nocache = true;
}
/*
* If backend Load config, else do front end stuff
*/
if(!GSBASE){
if (file_exists(GSROOTPATH . GSCONFIGFILE)){
include_once(GSROOTPATH . GSCONFIGFILE);
}
}
else {
$base = GSBASE; // LEGACY frontend flag DEPRECATED
// set loaders, if you want to override these do it your main common wrapper or index.php
if(!isset($load['plugin'])) $load['plugin'] = true; // load plugin system
if(!isset($load['template'])) $load['template'] = true; // load template system
if(!isset($load['login'])) $load['login'] = false; // load template system
}
/*
* Apply default definitions
*/
GS_defineFromArray($GS_definitions);
$GSADMIN = rtrim(GSADMIN,'/\\'); // global GS admin root folder name
/**
* Define Paths
*/
define('GSADMINPATH' , GSROOTPATH . $GSADMIN.'/'); // admin/
define('GSADMININCPATH' , GSADMINPATH . 'inc/'); // admin/inc/
define('GSADMINTPLPATH' , GSADMINPATH . 'template/'); // admin/template/
define('GSPLUGINPATH' , GSROOTPATH . 'plugins/'); // plugins/
define('GSLANGPATH' , GSADMINPATH . 'lang/'); // lang/
// data
define('GSDATAPATH' , GSROOTPATH . 'data/'); // data/
define('GSDATAOTHERPATH' , GSDATAPATH . 'other/'); // data/other/
define('GSDATAPAGESPATH' , GSDATAPATH . 'pages/'); // data/pages/
define('GSAUTOSAVEPATH' , GSDATAPAGESPATH . 'autosave/'); // data/pages/autosave/
define('GSDATAUPLOADPATH', GSDATAPATH . 'uploads/'); // data/uploads/
define('GSTHUMBNAILPATH' , GSDATAPATH . 'thumbs/'); // data/thumbs/
define('GSUSERSPATH' , GSDATAPATH . 'users/'); // data/users/
define('GSCACHEPATH' , GSDATAPATH . 'cache/'); // data/cache/
define('GSBACKUPSPATH' , GSROOTPATH . 'backups/'); // backups/
define('GSBACKUSERSPATH' , GSBACKUPSPATH . 'users/'); // backups/users
define('GSTHEMESPATH' , GSROOTPATH . 'theme/'); // theme/
$reservedSlugs = array($GSADMIN,'data','theme','plugins','backups');
/**
* Init debug mode
*/
if(defined('GSDEBUG') && (bool) GSDEBUG === true) {
error_reporting(-1);
ini_set('display_errors', 1);
// $nocache = true;
} else if( defined('GSSUPPRESSERRORS') && (bool)GSSUPPRESSERRORS === true ) {
error_reporting(0);
ini_set('display_errors', 0);
}
/*
* Enable php error logging
*/
if(defined('GSERRORLOGENABLE') && (bool) GSERRORLOGENABLE === true){
ini_set('log_errors', 1);
ini_set('error_log', GSDATAOTHERPATH .'logs/'. GSERRORLOGFILE);
}
/**
* Basic file inclusions
*/
require_once('basic.php');
require_once('template_functions.php');
require_once('theme_functions.php');
require_once('logging.class.php');
include_once(GSADMININCPATH.'configuration.php');
/**
* Bad stuff protection
*/
require_once('security_functions.php');
if (version_compare(PHP_VERSION, "5") >= 0) {
foreach ($_GET as &$xss) $xss = antixss($xss);
}
/**
* Headers
*/
// charset utf-8
header('content-type: text/html; charset=utf-8');
// no-cache headers
if(!is_frontend()){
$timestamp = gmdate("D, d M Y H:i:s") . " GMT";
header("Expires: " . $timestamp);
header("Last-Modified: " . $timestamp);
header("Pragma: no-cache");
header("Cache-Control: no-cache, must-revalidate");
}
/**
* Global website data
*
* @global (obj) $dataw user xml raw obj from GSDATAOTHERPATH.GSWEBSITEFILE
* @global (str) $SITENAME sitename
* @global (str) $SITEURL siteurl
* @global (str) $TEMPLATE site default theme
* @global (str) $PRETTYURLS toggle pretty urls enabled
* @global (str) $PERMALINK permalink structure, default %parents%/%slug%
* @global (str) $SITEEMAIL default site email for sending email primarily or contacting administrator
* @global (str) $SITETIMEZONE default timezone of server, safer to set than guess from server
* @global (str) $SITELANG default site ITEF langstring, used for login etc. see $LANG
* @global (str) $SITEUSR primary user id that installed GS
* @global (str) $ASSETURL url for asset loading in head depends on GSASSETURLREL and GSASSETSCHEMES settings
*/
GLOBAL
$dataw,
$SITENAME,
$SITEURL,
$SITEURL_ABS,
$SITEURL_REL,
$TEMPLATE,
$PRETTYURLS,
$PERMALINK,
$SITEEMAIL,
$SITETIMEZONE,
$SITELANG,
$SITEUSR,
$ASSETURL
;
// load website data from GSWEBSITEFILE (website.xml)
extract(getWebsiteData(true));
debugLog('GSSITEURLREL = ' . getDef('GSSITEURLREL',true));
debugLog('SITEURL = ' . getSiteURL());
debugLog('SITEURL_ABS = ' . getSiteURL(true));
debugLog('SITEURL_REL = ' . $SITEURL_REL);
debugLog('ASSETURL = ' . $ASSETURL);
/**
* Global user data
*
* @global (str) $datau user xml raw obj from GSUSERSPATH/userid.xml
* @global (str) $USR holds the GS_ADMIN_USERNAME cookie value
* @global (str) $HTMLEDITOR htmleditor toggle for auth user
* @global (str) $TIMEZONE timezone for auth user
* @global (str) $LANG language for auth user
*/
// grab cookie user data from userid.xml
GLOBAL
$datau,
$USR,
$HTMLEDITOR,
$USRTIMEZONE,
$USRLANG
;
extract(getUserData(true));
/**
* Global Language Data
*
* @global (array) $i18n i18n token keyed translation array
* @global (str) $LANG IETF langcode (w/underscore delim) [tag]_[subtag]
*/
GLOBAL
$i18n,
$LANG
;
// load language
$LANG = getDefaultLang(); // set global language from config heirarchy
i18n_merge(null); // load $LANG file into $i18n
i18n_mergeDefault(); // load GSDEFAULTLANG or GSMERGELANG lang into $i18n to override ugly missing {} tokens if set
//set php locale
setCustomLocale(getLocaleConfig());
/**
* Globals for salt and authentication data
*
* @global (obj) $dataa, authorization xml raw obj from GSDATAOTHERPATH.GSAUTHFILE
* @global (str) $SALT, salt from gsconfig else authorization file
* @global (str) $SESSIONHASH used for stateless session confirmation, or as non-expiring nonce for certain operations
*/
GLOBAL
$dataa, // legacy for anyone using
$SALT,
$SESSIONHASH
;
// grab authorization and security data fatal fail if salt is not set
$SALT = getDefaultSalt();
if(!isset($SALT) && $SITEURL !='' && notInInstall()) die(i18n_r('KILL_CANT_CONTINUE')."<br/>".sprintf(i18n_r('NOT_SET'),'SALT') );
$SESSIONHASH = sha1($SALT . $SITENAME);
/**
* Global editor vars (ckeditor)
*
* @global (str) $EDHEIGHT editor custom height
* @global (str) $EDLANG editor custom user lang or lang file specified
* @global (mixed) $EDTOOL editor custom toolbar, json array | php array | 'none' | ck toolbar_ name
* @global (str) $EDOPTIONS editor custom options config, js obj string, comma delimited
*/
// Init Editor globals
GLOBAL
$EDTOOL,
$EDHEIGHT,
$EDLANG,
$EDOPTIONS
;
// init editor globals
$EDHEIGHT = getEditorHeight();
$EDLANG = getEditorLang();
$EDOPTIONS = getEditorOptions();
$EDTOOL = getEditorToolbar();
$TIMEZONE = getDefaultTimezone();
setTimezone($TIMEZONE);
$dump = array(
// 'dataw' => $dataw,
// 'datau' => $datau,
// 'dataa' => $dataa,
'GSROOT' => GSROOTPATH,
'SITENAME' => $SITENAME,
'SITEURL' => $SITEURL,
'TEMPLATE' => $TEMPLATE,
'PRETTYURLS' => $PRETTYURLS,
'PERMALINK' => $PERMALINK,
'SITEEMAIL' => $SITEEMAIL,
'SITETIMEZONE' => $SITETIMEZONE,
'SITELANG' => $SITELANG,
'SITEUSR' => $SITEUSR,
'USR' => $USR,
'HTMLEDITOR' => $HTMLEDITOR,
'USRTIMEZONE' => $USRTIMEZONE,
'USRLANG' => $USRLANG,
'ASSETURL' => $ASSETURL,
'i18n' => count($i18n),
'SALT' => $SALT,
'SESSIONHASH' => $SESSIONHASH,
'EDTOOL' => $EDTOOL,
'EDOPTIONS' => $EDOPTIONS,
'EDLANG' => $EDLANG,
'EDHEIGHT' => $EDHEIGHT,
// '_SERVER' => $_SERVER,
);
// debugLog($dump);
/**
* Check to make sure site is already installed
*/
if (notInInstall()) {
$fullpath = suggest_site_path();
# if there is no SITEURL set, then it's a fresh install. Start installation process
# siteurl check is not good for pre 3.0 since it will be empty, so skip and run update first.
if ($SITEURL == '' && get_gs_version() >= 3.0) {
serviceUnavailable();
redirect($fullpath . $GSADMIN.'/install.php');
}
else {
# if an update file was included in the install package, redirect there first
if (file_exists(GSADMINPATH.'update.php') && !isset($_GET['updated']) && !getDef('GSDEBUGINSTALL')) {
serviceUnavailable();
redirect($fullpath . $GSADMIN.'/update.php');
}
}
if(!getDef('GSDEBUGINSTALL',true)){
# if you've made it this far, the site is already installed so remove the installation files
$filedeletionstatus = true;
if (file_exists(GSADMINPATH.'install.php')) {
$filedeletionstatus = delete_file(GSADMINPATH.'install.php');
}
if (file_exists(GSADMINPATH.'setup.php')) {
$filedeletionstatus = delete_file(GSADMINPATH.'setup.php');
}
if (file_exists(GSADMINPATH.'update.php')) {
$filedeletionstatus = delete_file(GSADMINPATH.'update.php');
}
if (!$filedeletionstatus) {
$error = sprintf(i18n_r('ERR_CANNOT_DELETE'), '<code>/'.$GSADMIN.'/install.php</code>, <code>/'.$GSADMIN.'/setup.php</code> or <code>/'.$GSADMIN.'/update.php</code>');
}
}
}
// fill for install
if(empty($SITEURL)) $SITEURL = suggest_site_path();
if(empty($SITEURL_ABS)) $SITEURL_ABS = $SITEURL;
if(empty($SITEURL_REL)) $SITEURL_REL = $SITEURL;
if(empty($ASSETURL)) $ASSETURL = $SITEURL;
/**
* Include other files depending if they are needed or not
*/
require_once(GSADMININCPATH.'cookie_functions.php');
require_once(GSADMININCPATH.'assets.php');
if(isset($load['plugin']) && $load['plugin']){
// load plugins functions
$live_plugins = array(); // global array for storing active plugins
include_once(GSADMININCPATH.'plugin_functions.php');
// include core plugin for page caching, requires plugin functions for hooks
include_once('caching_functions.php');
// Include plugins files in global scope
loadPluginData();
foreach ($live_plugins as $file=>$en) {
if ($en=='true' && file_exists(GSPLUGINPATH . $file)){
include_once(GSPLUGINPATH . $file);
}
}
exec_action('plugins-loaded'); // @hook plugins-loaded plugin files have been included
// load api
if(get_filename_id()=='settings' || get_filename_id()=='load') {
/* this core plugin only needs to be visible when you are viewing the
settings page since that is where its sidebar item is. */
if (getDef('GSEXTAPI',true)) {
include_once('api.plugin.php');
}
}
# main hook for common.php
exec_action('common'); // @hook common.php common has completed doing its thing
// debugLog('calling common_callout');
if(function_exists('common_callout')) common_callout();
}
// debugLog($live_plugins);
if(isset($load['login']) && $load['login'] && getDef('GSALLOWLOGIN',true)){ require_once(GSADMININCPATH.'login_functions.php'); }
// do the template rendering
if(GSBASE) require_once(GSADMINPATH.'base.php');
// common methods that are required before dpendancy includes
/**
* Debug Console Log
* @since 3.1
* @param $txt string
*/
function debugLog($txt = null) {
global $GS_debug;
array_push($GS_debug,$txt);
if(function_exists('debugLog_callout')) debugLog_callout($txt);
// print_r($GS_debug);
return $txt;
}
/**
* Define from an array
* @param array assoc of keyed values [DEFINITIONNAME] => value
*/
function GS_defineFromArray($definitions){
foreach($definitions as $definition => $value){
if(!defined($definition)) define($definition,$value);
}
}
/**
* Define from an array of global keys
* @param array DEFINITION keys to import from globals
*/
function GS_defineFromGlobals($definitions){
foreach($definitions as $definition){
if(isset($_GLOBALS[$definition])){
$value = $_GLOBALS[$definition];
if(!defined($definition)) define($definition,$value);
}
}
}
/**
* service is unavailable
* performs a service unavailable if front end
*/
function serviceUnavailable(){
if(is_frontend()){
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 7200'); // in seconds
i18n('SERVICE_UNAVAILABLE');
die();
}
}
function getGSRootPath(){
return dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR;
}
/* ?> */