-
Notifications
You must be signed in to change notification settings - Fork 19
/
lib-common.php
9174 lines (7937 loc) · 336 KB
/
lib-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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// | Geeklog 2.2 |
// +---------------------------------------------------------------------------+
// | lib-common.php |
// | |
// | Geeklog common library. |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2000-2020 by the following authors: |
// | |
// | Authors: Tony Bibbs - tony AT tonybibbs DOT com |
// | Mark Limburg - mlimburg AT users DOT sourceforge DOT net |
// | Jason Whittenburg - jwhitten AT securitygeeks DOT com |
// | Dirk Haun - dirk AT haun-online DOT de |
// | Vincent Furia - vinny01 AT users DOT sourceforge DOT net |
// +---------------------------------------------------------------------------+
// | |
// | This program is free software; you can redistribute it and/or |
// | modify it under the terms of the GNU General Public License |
// | as published by the Free Software Foundation; either version 2 |
// | of the License, or (at your option) any later version. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software Foundation, |
// | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
// | |
// +---------------------------------------------------------------------------+
use Geeklog\Autoload;
use Geeklog\Cache;
use Geeklog\Input;
use Geeklog\Log;
use Geeklog\Mail;
use Geeklog\Resource;
use Geeklog\Session;
// Prevent PHP from reporting uninitialized variables - Same setting as Geeklog installer
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR);
/**
* This is the common library for Geeklog. Through our code, you will see
* functions with the COM_ prefix (e.g. COM_createHTMLDocument()). Any such functions
* can be found in this file and called by plugins.
* Note: functions with the _ prefix should only be called by Core and not any plugins.
* --- You don't need to modify anything in this file! ---
* WARNING: put any custom hacks in lib-custom.php and not in here. This file is
* modified frequently by the Geeklog development team. If you put your hacks in
* lib-custom.php you will find upgrading much easier.
*/
/**
* Prevent getting any surprise values. But we should really stop
* using $_REQUEST altogether.
*/
$_REQUEST = array_merge($_GET, $_POST);
/**
* Configuration Include:
* You do NOT need to modify anything here any more!
*/
require_once __DIR__ . '/siteconfig.php';
if (COM_isDeveloperMode() &&
isset($_CONF['developer_mode_php'], $_CONF['developer_mode_php']['error_reporting'])) {
error_reporting((int) $_CONF['developer_mode_php']['error_reporting']);
}
/**
* Here, we shall establish an error handler. This will mean that whenever a
* php level error is encountered, our own code handles it. This will hopefully
* go someway towards preventing nasties like path exposures from ever being
* possible. That is, unless someone has overridden our error handler with one
* with a path exposure issue...
* Must make sure that the function hasn't been disabled before calling it.
*/
if (is_callable('set_error_handler')) {
/* Tell the error handler to use the default error reporting options.
* You may like to change this to use it in more/less cases, if so,
* just use the syntax used in the call to error_reporting() above.
*/
$defaultErrorHandler = set_error_handler('COM_handleError', error_reporting());
}
if (is_callable('set_exception_handler')) {
set_exception_handler('COM_handleException');
}
/**
* Turn this on to get various debug messages from the code in this library
*
* @global bool $_COM_VERBOSE
*/
$_COM_VERBOSE = COM_isEnableDeveloperModeLog('common');
COM_checkInstalled();
// Register autoloader
require_once $_CONF['path_system'] . 'classes/Autoload.php';
Autoload::initialize();
// Initialize system classes
// Check for request data first of all
Input::init($_CONF['default_charset']);
if (defined('GL_INSTALL_ACTIVE')) {
// *********************************************************
// IMPORTANT: Have to redeclare the variables below as Global here for Geeklog Install/Upgrade/Migrate.
// For some reason the scope is not correct for these variables when lib-common is included in the Geeklog Install
// For more info see https://github.com/Geeklog-Core/geeklog/issues/980
// Once install is fixed this can be removed...
global $_RIGHTS; // For Geeklog install when lib-common included SEC_getUserPermissions fails as $_RIGHTS doesn't get loaded
global $_USER; // For Geeklog install when lib-common included when current user may already be logged an error happens because of $_USER not retrieved when SESS_sessionCheck is called
global $TEMPLATE_OPTIONS; // For Geeklog install when lib-common included - COM_rdfUpToDateCheck is called when the template class is used an error happens because of $TEMPLATE_OPTIONS being empty.
global $_TOPICS; // For Geeklog install when lib-common included - TOPIC_getIndex errors as $_TOPIC is not an array
// *********************************************************
}
// Load configuration
$config = config::get_instance();
$config->set_configfile($_CONF['path'] . 'db-config.php');
$config->load_baseconfig();
$config->initConfig();
$_CONF = $config->get_config('Core');
// Some hard coded additional config options
$_CONF['theme_geeklog_default'] = 'denim_three'; // Geeklog default theme. If this changes then remember to change default theme in Installer for Install class and config-install.php files
$_CONF['theme_site_default'] = $_CONF['theme']; // Store original theme set in config
$_CONF['language_site_default'] = $_CONF['language']; // Store original site default language before it may get changed depending on other settings
// Geeklog Theme Support
// If ANY theme changes related to:
// template variables (new or deleted)
// template files (name of files, new or deleted files)
// New/Updated Features (for example added the scripts class to handle css and javascript)
// has happened since previous version of Geeklog then current Geeklog version is required.
// If nothing has changed related to this, then the last version of Geeklog that meet these standards can be used here.
// If this version and the gl version of the theme are not compatible (checked in COM_validateTheme) then theme will not load and alternate will be selected
// If default site theme is not compatible then a warning message will be displayed to the Root user
$_CONF['min_theme_gl_version'] = '2.2.1'; // Updated to 2.2.1 since index.thtml file added and header and footer files removed. Multiple new Template Variables also exist
// Installer calls lib-common so make sure it doesn't get affectd by certain config options
if (defined('GL_INSTALL_ACTIVE')) {
$_CONF['site_enabled'] = true;
$_CONF['demo_mode'] = false;
$_CONF['cache_templates'] = false;
$_CONF['cache_resource'] = false; // If site disabled then resources cannot be used on success page which is created by Geeklog since r.php checks site_enabled independantly
}
// Get features that has ft_name like 'config%'
$_CONF_FT = $config->_get_config_features();
// Load Log class
Log::init($_CONF['path_log']);
// Load Cache class
Cache::init(new Cache\FileSystem($_CONF['path'] . 'data/cache/'));
// Load in Geeklog Variables Table
/**
* @global $_VARS array
*/
$_VARS = array();
$result = DB_query("SELECT * FROM {$_TABLES['vars']}");
while ($row = DB_fetchArray($result)) {
$_VARS[$row['name']] = $row['value'];
}
if (isset($_CONF['site_enabled']) && !$_CONF['site_enabled']) {
if (empty($_CONF['site_disabled_msg'])) {
header("HTTP/1.1 503 Service Unavailable");
header("Status: 503 Service Unavailable");
header('Content-Type: text/plain; charset=' . COM_getCharset());
echo $_CONF['site_name'] . ' is temporarily down. Please check back soon.' . LB;
} else {
// if the msg starts with http: assume it's a URL we should redirect to
if (preg_match("/^(https?):/", $_CONF['site_disabled_msg'])) {
COM_redirect($_CONF['site_disabled_msg']);
} else {
header("HTTP/1.1 503 Service Unavailable");
header("Status: 503 Service Unavailable");
header('Content-Type: text/html; charset=' . COM_getCharset());
echo $_CONF['site_disabled_msg'] . LB;
}
}
exit;
}
// this file can't be used on its own - redirect to index.php
if (stripos($_SERVER['PHP_SELF'], basename(__FILE__)) !== false) {
COM_redirect($_CONF['site_url'] . '/index.php');
}
// +---------------------------------------------------------------------------+
// | Library Includes: You shouldn't have to touch anything below here |
// +---------------------------------------------------------------------------+
// Set the web server's timezone
TimeZoneConfig::setSystemTimeZone();
// Include multibyte functions
require_once $_CONF['path_system'] . 'lib-mbyte.php';
/**
* Include plugin class.
* This is a poorly implemented class that was not very well thought out.
* Still very necessary
*
* @global $_PLUGINS array of the names of active plugins
*/
require_once $_CONF['path_system'] . 'lib-plugins.php';
/**
* Include page time -- used to time how fast each page was created
*
* @global $_PAGE_TIMER timerobject
*/
$_PAGE_TIMER = new timerobject();
$_PAGE_TIMER->startTimer();
/**
* This provides optional URL rewriting functionality.
*
* @global $_URL Url
*/
$_URL = new Url($_CONF['url_rewrite'], $_CONF['url_routing']);
/**
* Include Device Detect class
*
* @global $_DEVICE Device
*/
$_DEVICE = new Device();
// This is the security library used for application security
require_once $_CONF['path_system'] . 'lib-security.php';
// This is the syndication library used to offer (RSS) feeds.
require_once $_CONF['path_system'] . 'lib-syndication.php';
// This is the topic library used to manage topics.
require_once $_CONF['path_system'] . 'lib-topic.php';
// This is the block library used to manage blocks.
require_once $_CONF['path_system'] . 'lib-block.php';
// This is the likes library used to manage and display the likes and dislikes.
require_once $_CONF['path_system'].'lib-likes.php';
/**
* This is the custom library.
* It is the sandbox for every Geeklog Admin to play in.
* The lib-custom.php as shipped will never contain required code,
* so it's safe to always use your own copy.
* This should hold all custom hacks to make upgrading easier.
*/
if (file_exists($_CONF['path_system'] . 'lib-custom.php')) {
require_once $_CONF['path_system'] . 'lib-custom.php';
}
// Session management library
require_once $_CONF['path_system'] . 'lib-sessions.php';
SESS_sessionCheck(); // Load user data
TimeZoneConfig::setUserTimeZone();
if (COM_isAnonUser()) {
$_USER['advanced_editor'] = $_CONF['advanced_editor'];
}
// Initiate global topic variable (as of Geeklog v2.2.1 is $_USER['topic_id'], before was $topic). This variable contains the current topic id
// It can be set by anything that uses topics like index.php (topics), article, directory, staticpages, other potential plugins.
// Actual topic id is retrieved via TOPIC_getTopic by the page. This function checks for the topic assignment of the item and if not found uses the last topic which is stored as a session var topic_id
// Not all pages will use TOPIC_getTopic since the item being displayed may not be topic aware therefore we need to set the default all topics here.
// The topic id was retrieved here at one point (instead of using TOPIC_getTopic) but url_rewrite caused issues as lib-common doesn't know the difference between url rewritten topic urls, article urls, staticpage urls, etc...
// NOTE: Global $topic variable has been depreciated as of Geeklog v2.2.1 and checks for it by Core with the function _depreciatedCheckGlobalTopicVariableUsed will be removed by Geeklog v3.0.0. use $_USER['topic_id'] instead of $topic
// Please use the functions TOPIC_setTopic to set the current topic if needed or TOPIC_currentTopic to retrieve the current topic id
TOPIC_setTopic(''); // Initialize current topic id variable and set current topic to All Topics - Current user topic id at this point untill TOPIC_getTopic is called (if ever) or TOPIC_setTopic is called by something else
// Set theme for current user (anonymous or logged in user)
$temp_theme = '';
$found_valid_theme = false;
if ($_CONF['allow_user_themes'] == 1) {
// Check for theme switch via url variable. Don't want plugins messing with setting themes
// Used by plugins like vThemes
if (isset($_POST['gl-usetheme'])) {
$temp_theme = COM_sanitizeFilename($_POST['gl-usetheme'], true);
if (COM_validateTheme($temp_theme)) {
$found_valid_theme = true;
}
}
if (!$found_valid_theme) {
// Check for theme set with a cookie then
// Cookies are set everytime a user logs in (USER_doLogin) and has a theme set or possibly by a plugin like vThemes
if (isset($_COOKIE[$_CONF['cookie_theme']])) {
$temp_theme = COM_sanitizeFilename($_COOKIE[$_CONF['cookie_theme']], true);
if (COM_validateTheme($temp_theme)) {
$found_valid_theme = true;
}
}
}
if (!$found_valid_theme) {
// Check for a user theme then
if (isset($_USER['theme'])) {
$temp_theme = $_USER['theme'];
if (COM_validateTheme($temp_theme)) {
$found_valid_theme = true;
}
}
}
}
$setSystemMessage505_flag = false;
$setSystemMessage506_flag = false;
if (!$found_valid_theme) {
// So either no user themes allowed or user theme set is not valid anymore (ie old theme that has since been removed)
// Lets make sure site default theme is valid
$temp_theme = $_CONF['theme'];
if (COM_validateTheme($temp_theme)) {
$found_valid_theme = true;
} else {
// no valid themes in config settings so try Geeklog default theme
$temp_theme = $_CONF['theme_geeklog_default'];
if (COM_validateTheme($temp_theme)) {
$found_valid_theme = true;
// Theme may be valid but settings are not so notify current user if they are Root
if (!defined('GL_INSTALL_ACTIVE') && SEC_inGroup('Root')) {
$setSystemMessage505_flag = true; // have to set flag here and set later on since language files are not loaded
}
}
if (!$found_valid_theme) {
// Nothing set is valid so lets just try to find any valid theme
$themeFiles = COM_getThemes(true);
if (isset($themeFiles[1])) {
// Grab first valid theme
$temp_theme = $themeFiles[1];
$found_valid_theme = true;
// Theme may be valid but settings are not so notify current user if they are Root
if (!defined('GL_INSTALL_ACTIVE') && SEC_inGroup('Root')) {
$setSystemMessage506_flag = true; // have to set flag here and set later on since language files are not loaded
}
}
}
}
}
if ($found_valid_theme) {
// Set all related theme variables now
$_CONF['theme'] = $temp_theme;
$_CONF['path_layout'] = $_CONF['path_themes'] . $_CONF['theme'] . '/';
$_CONF['layout_url'] = $_CONF['site_url'] . '/layout/' . $_CONF['theme'];
if (!COM_isAnonUser()) {
// User Theme is always saved even if $_CONF['allow_user_themes'] is false as per savepreferences function in usersettings. So do same thing here
if (!isset($_USER['theme']) || (isset($_USER['theme']) && $_USER['theme'] != $_CONF['theme'])) {
// Update user record if it had to be changed due to passed url theme variable, cookie found, or not a valid user theme in record
$_USER['theme'] = $_CONF['theme'];
DB_query("UPDATE {$_TABLES['users']} SET theme='{$_USER['theme']}' WHERE uid = {$_USER['uid']}");
}
}
// Update Cookie as well if needed for anonymous and users
if (!isset($_COOKIE[$_CONF['cookie_theme']]) || (isset($_COOKIE[$_CONF['cookie_theme']]) && $_COOKIE[$_CONF['cookie_theme']] != $_CONF['theme'])) {
if (!headers_sent()) {
@setcookie(
$_CONF['cookie_theme'], $_CONF['theme'], time() + 31536000, $_CONF['cookie_path'],
$_CONF['cookiedomain'], $_CONF['cookiesecure']
);
}
}
// If root lets check if theme_site_default is a valid theme (if we are not already using it) as this can affect other users
if (!defined('GL_INSTALL_ACTIVE') && SEC_inGroup('Root') && $_CONF['allow_user_themes'] && $_CONF['theme_site_default'] != $_CONF['theme']) {
$_CONF['theme_geeklog_default'];
$temp_theme = $_CONF['theme_site_default'];
if (!COM_validateTheme($temp_theme)) {
if ($_CONF['theme'] == $_CONF['theme_geeklog_default']) {
$setSystemMessage505_flag = true; // have to set flag here and set later on since language files are not loaded
} else {
$setSystemMessage506_flag = true; // have to set flag here and set later on since language files are not loaded
}
}
}
} else {
// No valid theme found. Error out here as it will error out later anyways when the template class is used
COM_handleGeeklogError('gl-no-valid-themes', "No valid themes for this version of Geeklog found in: {$_CONF['path_themes']}");
}
// Require theme functions.php should already be loaded and theme_config_foo already be checked to exist along with correct version from above as they could not get this far otherwise
// Get the configuration values from the theme
$_CONF['theme_default'] = ''; // Default is none
$_CONF['path_layout_default'] = ''; // Default is none
$_CONF['theme_gl_version'] = ''; // Required and set by theme. Needs to be set in theme_config
$_CONF['theme_etag'] = false;
$_CONF['theme_plugins'] = ''; // Default is none - CANNOT be a child theme
$_CONF['theme_options'] = array(); // Default is empty array
$func = 'theme_config_' . $_CONF['theme'];
$theme_config = $func();
$_CONF['doctype'] = $theme_config['doctype'];
$_CONF['theme_oauth_icons'] = isset($theme_config['theme_oauth_icons'])
? $theme_config['theme_oauth_icons']
: '';
$_IMAGE_TYPE = $theme_config['image_type'];
if (isset($theme_config['theme_default'])) {
$_CONF['theme_default'] = $theme_config['theme_default'];
$_CONF['path_layout_default'] = $_CONF['path_themes'] . $_CONF['theme_default'] . '/';
}
// Remove references to supported_version_theme by Geeklog v3.0.0 (see See COM_validateTheme as well)
if (isset($theme_config['supported_version_theme'])) {
COM_deprecatedLog('Theme Variable $theme_config[' . "'supported_version_theme'" . ']', '2.2.1', '3.0.0', '$theme_config[' . "'supported_version_theme'" . '] instead');
$_CONF['theme_gl_version'] = $theme_config['supported_version_theme'];
} else {
$_CONF['theme_gl_version'] = $theme_config['theme_gl_version'];
}
$_CONF['theme_etag'] = (!isset($theme_config['etag']))
? $_CONF['theme_etag'] : $theme_config['etag'];
if ($_CONF['theme_etag'] && !file_exists($_CONF['path_layout'] . 'style.css.php')) {
// See if style.css.php file exists that is required
$_CONF['theme_etag'] = false;
}
if (isset($theme_config['theme_path_site_logo']) && !empty($theme_config['theme_path_site_logo'])) {
$_CONF['path_site_logo'] = $theme_config['theme_path_site_logo'];
}
if (isset($theme_config['theme_plugins'])) {
// EXPERIMENTAL for theme_gl_version v2.2.0 and higher (See Geeklog Core theme functions.php and theme_plugins for further explanation or https://github.com/Geeklog-Core/geeklog/issues/767)
$_CONF['theme_plugins'] = $theme_config['theme_plugins'];
}
if (isset($theme_config['options']) && is_array($theme_config['options'])) {
$_CONF['theme_options'] = $theme_config['options'];
}
/**
* This provides the ability to set css and javascript.
*
* @global $_SCRIPTS Geeklog\Resource
*/
$_SCRIPTS = new Resource($_CONF);
/**
* themes can specify the default image type
* fall back to 'gif' if they don't
*
* @global $_IMAGE_TYPE string
*/
if (empty($_IMAGE_TYPE)) {
$_IMAGE_TYPE = 'gif';
}
// Ensure XHTML constant is defined to avoid problems elsewhere
if (!defined('XHTML')) {
switch ($_CONF['doctype']) {
case 'xhtml10transitional':
case 'xhtml10strict':
case 'xhtml5':
define('XHTML', ' /');
break;
default:
define('XHTML', '');
break;
}
}
// Set template class default template variables option
/**
* @global $TEMPLATE_OPTIONS array
*/
$TEMPLATE_OPTIONS = array(
'path_cache' => $_CONF['path_data'] . 'layout_cache/', // location of template cache
'path_prefixes' => array( // used to strip directories off file names. Order is important here.
$_CONF['path_themes'], // this is not path_layout. When stripping directories, you want files in different themes to end up in different directories.
$_CONF['path'],
'/' // this entry must always exist and must always be last
),
'incl_phpself_header' => true, // set this to true if your template cache exists within your web server's docroot.
'cache_by_language' => true, // create cache directories for each language. Takes extra space but moves all $LANG variable text directly into the cached file
'cache_for_mobile' => $_CONF['cache_mobile'], // create cache directories for mobile devices. Non mobile devices uses regular directory. If disabled mobile uses regular cache files. Takes extra space
'default_vars' => array( // list of vars found in all templates.
'xhtml' => XHTML,
'image_type' => $_IMAGE_TYPE,
'site_url' => $_CONF['site_url'],
'site_admin_url' => $_CONF['site_admin_url'],
'layout_url' => $_CONF['layout_url'], // Can be set by lib-common on theme change
'anonymous_user' => COM_isAnonUser(),
'device_mobile' => $_DEVICE->is_mobile(),
'front_page' => COM_onFrontpage(),
'current_url' => COM_getCurrentURL()
),
'hook' => array('set_root' => 'CTL_setTemplateRoot'), // Function found in lib-template and is used to add the ability for child themes. CTL_setTemplateRoot will be depreciated as of Geeklog 3.0.0.
);
Autoload::load('template');
// Template library contains helper functions for template class
require_once $_CONF['path_system'] . 'lib-template.php';
// Set language
if (isset($_COOKIE[$_CONF['cookie_language']]) && empty($_USER['language'])) {
$language = COM_sanitizeFilename($_COOKIE[$_CONF['cookie_language']]);
if (is_file($_CONF['path_language'] . $language . '.php') &&
($_CONF['allow_user_language'] == 1)
) {
$_USER['language'] = $language;
$_CONF['language'] = $language;
}
} elseif (!empty($_USER['language'])) {
if (is_file($_CONF['path_language'] . $_USER['language'] . '.php') &&
($_CONF['allow_user_language'] == 1)
) {
$_CONF['language'] = $_USER['language'];
}
} elseif (COM_isMultiLanguageEnabled()) {
$_CONF['language'] = COM_getLanguage();
}
// Include a language file
require_once $_CONF['path_language'] . $_CONF['language'] . '.php';
if (empty($LANG_DIRECTION)) {
// default to left-to-right
$LANG_DIRECTION = 'ltr';
}
// Update any language specfic Configuration options with proper language if exists
COM_switchLocaleSettings();
if (setlocale(LC_ALL, $_CONF['locale']) === false) {
setlocale(LC_TIME, $_CONF['locale']);
}
// Override language items (since v2.1.2)
Language::init();
$language_overrides = array(
'LANG01', 'LANG03', 'LANG04', 'LANG_MYACCOUNT', 'LANG05', 'LANG08', 'LANG09',
'LANG10', 'LANG11', 'LANG12', 'LANG_LOGVIEW', 'LANG_ENVCHECK', 'LANG20',
'LANG21', 'LANG24', 'LANG27', 'LANG28', 'LANG29', 'LANG31', 'LANG32', 'LANG33',
'MESSAGE', 'LANG_ACCESS', 'LANG_DB_BACKUP', 'LANG_BUTTONS', 'LANG_404',
'LANG_LOGIN', 'LANG_TRB', 'LANG_DIR', 'LANG_SECTEST', 'LANG_WHATSNEW', 'LANG_MONTH',
'LANG_WEEK', 'LANG_ADMIN', 'LANG_commentcodes', 'LANG_commentmodes',
'LANG_cookiecodes', 'LANG_dateformats', 'LANG_featurecodes', 'LANG_frontpagecodes',
'LANG_postmodes', 'LANG_sortcodes', 'LANG_trackbackcodes', 'LANG_CONFIG',
'LANG_VALIDATION');
$language_overrides = array_merge($language_overrides, PLG_getLanguageOverrides());
Language::override($language_overrides);
/**
* Global array of groups current user belongs to
*
* @global $_GROUPS array
*/
$_GROUPS = COM_isAnonUser() ? SEC_getUserGroups(1) : SEC_getUserGroups($_USER['uid']);
/**
* Global array of current user permissions [read,edit]
*
* @global $_RIGHTS array
*/
$_RIGHTS = explode(',', SEC_getUserPermissions());
// Include scripts on behalf of the theme
$func = 'theme_css_' . $_CONF['theme'];
if (!is_callable($func)) {
$func = 'theme_css';
}
if (is_callable($func)) {
foreach ($func() as $info) {
$file = (!empty($info['file'])) ? $info['file'] : '';
$name = (!empty($info['name'])) ? $info['name'] : md5(!empty($file) ? $file : strval(time()));
$constant = (!empty($info['constant'])) ? $info['constant'] : true;
$attributes = (!empty($info['attributes'])) ? $info['attributes'] : array();
$priority = (!empty($info['priority'])) ? $info['priority'] : 100;
$_SCRIPTS->setCSSFile($name, $file, $constant, $attributes, $priority, 'theme');
}
}
$func = 'theme_js_libs_' . $_CONF['theme'];
if (!is_callable($func)) {
$func = 'theme_js_libs';
}
if (is_callable($func)) {
foreach ($func() as $info) {
$footer = true;
if (isset($info['footer']) && !$info['footer']) {
$footer = false;
}
$_SCRIPTS->setJavaScriptLibrary($info['library'], $footer);
}
}
$func = 'theme_js_files_' . $_CONF['theme'];
if (!is_callable($func)) {
$func = 'theme_js_files';
}
if (is_callable($func)) {
foreach ($func() as $info) {
$footer = true;
if (isset($info['footer']) && !$info['footer']) {
$footer = false;
}
$priority = (!empty($info['priority'])) ? $info['priority'] : 100;
$_SCRIPTS->setJavaScriptFile(md5($info['file']), $info['file'], $footer, $priority);
}
}
$func = 'theme_js_' . $_CONF['theme'];
if (!is_callable($func)) {
$func = 'theme_js';
}
if (is_callable($func)) {
foreach ($func() as $info) {
$wrap = true;
if (isset($info['wrap']) && !$info['wrap']) {
$wrap = false;
}
$footer = true;
if (isset($info['footer']) && !$info['footer']) {
$footer = false;
}
$_SCRIPTS->setJavaScript($info['code'], $wrap, $footer);
}
}
$func = 'theme_init_' . $_CONF['theme'];
if (!is_callable($func)) {
$func = 'theme_init';
}
if (is_callable($func)) {
$func();
}
unset($theme_config, $func);
// Forcibly enable Resource cache if current theme is compatible with Modern Curve theme
if ($_SCRIPTS->isCompatibleWithModernCurveTheme()) {
$_CONF['cache_resource'] = true;
}
// Disable Resource cache (combined and minified CSS and JavaScript files)
if (isset($_CONF['cache_resource']) && !$_CONF['cache_resource']) {
Cache::disable();
};
// Clear out any expired sessions
DB_lockTable($_TABLES['sessions']);
DB_query("UPDATE {$_TABLES['sessions']} SET whos_online = 0 WHERE start_time < " . (time() - $_CONF['whosonline_threshold']));
DB_unlockTable($_TABLES['sessions']);
/**
* Build global array of Topics current user has access to
*
* @global $_TOPICS array
*/
// Figure out if we need to update topic tree or retrieve it from the cache
// For anonymous users topic tree data can be shared
$cacheInstance = 'topic_tree__' . CACHE_security_hash();
$serialized_topic_tree = CACHE_check_instance($cacheInstance, true, true); // Not language or mobile cache specific (as this is ALL topic information)
// See if Topic Tree cache exists
if (empty($serialized_topic_tree)) {
$_TOPICS = TOPIC_buildTree(TOPIC_ROOT, true);
// Need this check since this variable is not set correctly when Geeklog is being install
if (isset($GLOBALS['TEMPLATE_OPTIONS']) && is_array($TEMPLATE_OPTIONS) && isset($TEMPLATE_OPTIONS['path_cache'])) {
// Save updated topic tree and date
CACHE_create_instance($cacheInstance, serialize($_TOPICS), true, true); // Not language or mobile cache specific
}
} else {
$_TOPICS = unserialize($serialized_topic_tree);
}
// Figure out if we need to update article feeds. Check last article date published in feed
$sql = "SELECT date FROM {$_TABLES['stories']} "
. "WHERE draft_flag = 0 AND date <= NOW() AND perm_anon > 0 "
. "ORDER BY date DESC LIMIT 1";
$result = DB_query($sql);
$A = DB_fetchArray($result);
if (isset($_VARS['last_article_publish']) && ($_VARS['last_article_publish'] != $A['date'])) {
//Set new latest article published
// Below similar to what is run in STORY_updateLastArticlePublished
DB_query("UPDATE {$_TABLES['vars']} SET value='{$A['date']}' WHERE name='last_article_publish'");
// We need to see if there are currently two featured articles (because of future article).
// Can only have one but you can have one current featured article
// and one for the future...this check will set the latest one as featured
// solely
COM_featuredCheck();
// Geeklog now allows for articles to be published in the future. Because of
// this, we need to check to see if we need to rebuild the RDF file in the case
// that any such articles have now been published. Need to do this for comments
// as well since article can have comments
COM_rdfUpToDateCheck('article');
COM_rdfUpToDateCheck('comment');
// If what's new block is cached, clear it since new article(s) are now online
if ($_CONF['whatsnew_cache_time'] > 0) {
$cacheInstance = 'whatsnew__'; // remove all what's new instances
CACHE_remove_instance($cacheInstance);
}
}
/**
* This provides the ability to generate structure data based on types from schema.org to
* Plugins can add their own structured data so must load after plugin functions.inc file
*/
Autoload::load('structureddata');
$_STRUCT_DATA = new StructuredData();
// Structured Data library contains helper functions for structured data class
require_once $_CONF['path_system'] . 'lib-structureddata.php';
// Now include all plugin functions since everything else is loaded
foreach ($_PLUGINS as $pi_name) {
require_once $_CONF['path'] . 'plugins/' . $pi_name . '/functions.inc';
}
// Plugins can provide Structured Data Types, see if any (needs to be done after loading functions.inc)
$_STRUCT_DATA->load_plugin_types();
// Set any System messages since where the flags set language files not loaded yet
if ($setSystemMessage505_flag) {
COM_setSystemMessage($MESSAGE[505]); // Deals with Site theme setting error
}
if ($setSystemMessage506_flag) {
COM_setSystemMessage($MESSAGE[506]); // Deals with Site theme setting error
}
// +---------------------------------------------------------------------------+
// | HTML WIDGETS |
// +---------------------------------------------------------------------------+
/**
* Return the file to use for a block template.
* This returns the template needed to build the HTML for a block. This function
* allows designers to give a block it's own custom look and feel. If no
* templates for the block are specified, the default blockheader.html and
* blockfooter.html will be used.
*
* @param string $blockName corresponds to name field in block table
* @param string $which can be either 'header' or 'footer' for corresponding template
* @param string $position can be 'left', 'right' or blank. If set, will be used to find a side specific
* override template.
* @param string $plugin name of plugin with blocks location
* @see function COM_startBlock
* @see function COM_endBlock
* @see function COM_showBlocks
* @see function COM_showBlock
* @return string template name
*/
function COM_getBlockTemplate($blockName, $which, $position = '', $plugin = '')
{
global $_BLOCK_TEMPLATE, $_COM_VERBOSE, $_CONF;
if ($_COM_VERBOSE) {
COM_errorLog("_BLOCK_TEMPLATE[$blockName] = " . $_BLOCK_TEMPLATE[$blockName], 1);
}
$template = ($which === 'header') ? 'blockheader.thtml' : 'blockfooter.thtml';
if (!empty($_BLOCK_TEMPLATE[$blockName])) {
$i = ($which === 'header') ? 0 : 1;
$templates = explode(',', $_BLOCK_TEMPLATE[$blockName]);
if (count($templates) === 2 && !empty($templates[$i])) {
$template = $templates[$i];
}
}
// If we have a position specific request, and the template is not already
// position specific then look to see if there is a position specific
// override.
$templateLC = strtolower($template);
if (!empty($position) && (strpos($templateLC, $position) === false)) {
// Trim .thtml from the end.
$positionSpecific = substr($template, 0, strlen($template) - 6);
$positionSpecific .= '-' . $position . '.thtml';
$templatefound = false;
if (!empty($plugin)) {
$plugin_template_paths = CTL_plugin_templatePath($plugin);
foreach($plugin_template_paths as $plugin_template_path) {
if (file_exists($plugin_template_path . '/' . $positionSpecific)) {
$template = $positionSpecific;
$templatefound = true; // If found don't need to search theme or theme default if exist
break;
}
}
}
if (!$templatefound && file_exists($_CONF['path_layout'] . $positionSpecific)) {
$template = $positionSpecific;
$templatefound = true; // If found don't need to search theme default if exist
}
// See if default theme if so check there
if (!$templatefound && !empty($_CONF['theme_default'])) {
if (file_exists($_CONF['path_layout_default'] . $positionSpecific)) {
$template = $positionSpecific;
}
}
}
if ($_COM_VERBOSE) {
COM_errorLog("Block template for the $which of $blockName is: $template", 1);
}
return $template;
}
/**
* See if passed theme is valid.
*
* @param string theme id of theme (which is also the directory name that should be located in the layout folder)
*/
function COM_validateTheme($theme)
{
global $_CONF;
$valid = false;
// All themes require a functions.php (ie child themes don't require any template files) so check for just this one file
// At some point could actualy check for min geeklog version of theme theme_gl_version wgich was introduced in Geeklog v2.2.1
if (!empty($theme)) {
$temp_path_layout = $_CONF['path_themes'] . $theme . '/';
if (file_exists($temp_path_layout . 'functions.php')) {
require_once $temp_path_layout . 'functions.php';
$func = 'theme_config_' . $theme;
if (is_callable($func)) {
$theme_config = $func();
// Remove references to supported_version_theme by Geeklog v3.0.0
$temp_theme_gl_version = '';
if (isset($theme_config['supported_version_theme'])) {
$temp_theme_gl_version = $theme_config['supported_version_theme'];
} else {
if (isset($theme_config['theme_gl_version'])) {
$temp_theme_gl_version = $theme_config['theme_gl_version'];
}
}
// Version Compare. Check to see if Theme Geeklog Version support equals current Geeklog min theme version
if (!empty($temp_theme_gl_version) && COM_versionCompare($temp_theme_gl_version, $_CONF['min_theme_gl_version'], '==')) {
$valid = true;
}
}
}
}
return $valid;
}
/**
* Gets all installed themes
* Returns a list of all the directory names in $_CONF['path_themes'], i.e.
* a list of all the theme names.
*
* @param boolean $all if true, return all themes even if users aren't allowed to change their default themes
* @param boolean $valid if true, return all valid themes only
* @return array All installed themes
*/
function COM_getThemes($all = false, $valid = true)
{
global $_CONF;
$index = 1;
$themes = array();
// If users aren't allowed to change their theme then only return the default theme
if (($_CONF['allow_user_themes'] == 0) && !$all) {
$themes[$index] = $_CONF['theme'];
} else {
$fd = opendir($_CONF['path_themes']);
while (($dir = @readdir($fd)) !== false) {
if (is_dir($_CONF['path_themes'] . $dir) && ($dir !== '.') && ($dir !== '..') &&
($dir !== 'CVS') && (substr($dir, 0, 1) !== '.')
) {
clearstatcache();
if ($valid) {
if (COM_validateTheme($dir)) {
$themes[$index] = $dir;
}
} else {
$themes[$index] = $dir;
}
$index++;
}
}
}
return $themes;
}
/**
* Create the menu, i.e. replace {menu_elements} in the site header with the
* actual menu entries.
*
* @param Template $header reference to the header template
* @param array $plugin_menu array of plugin menu entries, if any
*/
function COM_renderMenu($header, $plugin_menu)
{
global $_CONF, $LANG01;
if (empty($_CONF['menu_elements'])) {
$_CONF['menu_elements'] = array( // default set of links
'contribute', 'search', 'stats', 'directory', 'plugins',
);
}
$anon = COM_isAnonUser();
$menuCounter = 0;
$allowedCounter = 0;
$counter = 0;
$custom_entries = array();
$num_plugins = count($plugin_menu);
if (($num_plugins === 0) && in_array('plugins', $_CONF['menu_elements'])) {
$key = array_search('plugins', $_CONF['menu_elements']);
unset($_CONF['menu_elements'][$key]);
}
if (in_array('custom', $_CONF['menu_elements'])) {
if (function_exists('CUSTOM_menuEntries')) {
$custom_entries = CUSTOM_menuEntries();
}
if (count($custom_entries) === 0) {
$key = array_search('custom', $_CONF['menu_elements']);
unset($_CONF['menu_elements'][$key]);
}
}
$num_elements = count($_CONF['menu_elements']);
foreach ($_CONF['menu_elements'] as $item) {
$counter++;
$allowed = true;
$last_entry = ($counter == $num_elements);
switch ($item) {
case 'contribute':
$url = $_CONF['site_url'] . '/submit.php?type=story';
$header->set_var('current_topic', '');
$label = $LANG01[71];
if ($anon && ($_CONF['loginrequired'] ||
$_CONF['submitloginrequired'])
) {
$allowed = false;
}
break;
case 'custom':
if (function_exists('CUSTOM_renderMenu')) {
CUSTOM_renderMenu($header, $custom_entries, $menuCounter);
} else {
$custom_count = 0;
$custom_size = count($custom_entries);
foreach ($custom_entries as $entry) {
$custom_count++;
if (empty($entry['url']) || empty($entry['label'])) {
continue;
}
$header->set_var('menuitem_url', $entry['url']);
$header->set_var('menuitem_text', $entry['label']);
if ($last_entry && ($custom_count == $custom_size)) {
$header->parse('menu_elements', 'menuitem_last',
true);
} else {
$header->parse('menu_elements', 'menuitem', true);
}
$menuCounter++;
}
}
$url = '';
$label = '';
break;
case 'directory':
$url = $_CONF['site_url'] . '/directory.php';