forked from BitweaverCMS/kernel
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBitSystem.php
More file actions
3214 lines (2875 loc) · 106 KB
/
BitSystem.php
File metadata and controls
3214 lines (2875 loc) · 106 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
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 /* -*- Mode: php; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4; -*- */
/**
* Main bitweaver systems functions
*
* @package kernel
* @version $Header$
* @author spider <spider@steelsun.com>
*/
// +----------------------------------------------------------------------+
// | PHP version 4.??
// +----------------------------------------------------------------------+
// | Copyright (c) 2005 bitweaver.org
// +----------------------------------------------------------------------+
// | Copyright (c) 2004-2005, Christian Fowler, et. al.
// | All Rights Reserved. See below for details and a complete list of authors.
// | Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
// |
// | For comments, please use PEAR documentation standards!!!
// | -> see http://pear.php.net/manual/en/standards.comments.php
// | and http://www.phpdoc.org/
// +----------------------------------------------------------------------+
/**
* required setup
*/
require_once( KERNEL_PKG_PATH . 'BitBase.php' );
require_once( KERNEL_PKG_PATH . 'BitDate.php' );
require_once( THEMES_PKG_PATH . 'BitSmarty.php' );
define( 'DEFAULT_PACKAGE', 'kernel' );
define( 'CENTER_COLUMN', 'c' );
define( 'HOMEPAGE_LAYOUT', 'home' );
define( 'PKG_PLUGIN_TYPE_FUNCTION', 'function' );
define( 'PKG_PLUGIN_TYPE_SQL', 'sql' );
define( 'PKG_PLUGIN_TYPE_TPL', 'tpl' );
/**
* kernel::BitSystem
*
* Purpose:
*
* This is the main system class that does the work of seeing bitweaver has an
* operable environment and has methods for modifying that environment.
*
* Currently gBitSystem derives from this class for backward compatibility sake.
* Ultimate goal is to put system code from BitBase here, and base code from
* gBitSystem (code that ALL features need) into BitBase and code gBitSystem that
* is Package specific should be moved into that package
*
* @author spider <spider@steelsun.com>
*
* @package kernel
*/
class BitSystem extends BitBase {
// Initiate class variables
// Essential information about packages
// DEPRECATED @TODO Delete
var $mPackages = array();
// Active Packages
var $mPackagesConfig = array();
// Installed Packages
var $mPackagesInstalled = array();
// Active (ONLY) Package Plugins
var $mPackagePluginsConfig = array();
// Installed Package Plugins
var $mPackagePluginsInstalled = array();
// An array of registered plugin handlers
var $mPackagePluginsHandlers = array();
// Cross Reference Package Directory Name => Package Key used as index into $mPackages
var $mPackagesDirNameXref = array();
// Contains site style information
var $mStyle = array();
// Information about package menus used in all menu modules and top bar
var $mAppMenu = array();
// The currently active page
var $mActivePackage;
// Javascript to be added to the <body onload> attribute
var $mOnload = array();
// Javascript to be added to the <body onunload> attribute
var $mOnunload = array();
// Used by packages to register notification events that can be subscribed to.
var $mNotifyEvents = array();
// Used to store contents of kernel_config
var $mConfig;
// Used to monitor if ::registerPackage() was called. This is used to determine whether to auto-register a package
var $mRegisterCalled;
// The name of the package that is currently being processed
var $mPackageFileName;
// Content classes.
var $mContentClasses = array();
// Debug HTML to be displayed just after the HTML headers
var $mDebugHtml = "";
/**
* mPackagesSchemas
*/
var $mPackagesSchemas = array();
/**
* mPermissionsSchema
*/
var $mPermissionsSchema = array();
// === BitSystem constructor
/**
* base constructor, auto assigns member db variable
*
* @access public
*/
// Constructor receiving a PEAR::Db database object.
function BitSystem() {
global $gBitTimer;
// Call DB constructor which will create the database member variable
BitBase::BitBase();
$this->mAppMenu = array();
$this->mTimer = $gBitTimer;
$this->mServerTimestamp = new BitDate();
$this->loadConfig();
// Critical Preflight Checks
$this->checkEnvironment();
$this->initSmarty();
$this->mRegisterCalled = FALSE;
}
// === initSmarty
/**
* Define and load Smarty components
*
* @param none $
* @return none
* @access private
*/
function initSmarty() {
global $_SERVER, $gBitSmarty;
// Set the separator for PHP generated tags to be & instead of &
// This is necessary for XHTML compliance
ini_set( "arg_separator.output", "&" );
// Remove automatic quotes added to POST/COOKIE by PHP
if( get_magic_quotes_gpc() ) {
foreach( $_REQUEST as $k => $v ) {
if( !is_array( $_REQUEST[$k] ) ) {
$_REQUEST[$k] = stripslashes( $v );
}
}
}
// make sure we only create one BitSmarty
if( !is_object( $gBitSmarty ) ) {
$gBitSmarty = new BitSmarty();
// set the default handler
$gBitSmarty->load_filter( 'pre', 'tr' );
// $gBitSmarty->load_filter('output','trimwhitespace');
if( isset( $_REQUEST['highlight'] ) ) {
$gBitSmarty->load_filter( 'output', 'highlight' );
}
}
}
/**
* Load all preferences and store them in $this->mConfig
*
* @param $pPackage string optional load preferences only for selected package
* $param $pForce boolean force reload of config settings
*/
public function loadConfig( $pPackage = NULL, $pForce = FALSE ) {
$queryVars = array();
$whereClause = '';
if( $pPackage ) {
array_push( $queryVars, $pPackage );
$whereClause = ' WHERE `package`=? ';
}
if ( empty( $this->mConfig ) || $pForce ) {
$this->mConfig = array();
$query = "SELECT `config_name` ,`config_value`, `package` FROM `" . BIT_DB_PREFIX . "kernel_config` " . $whereClause;
if( $rs = $this->mDb->query( $query, $queryVars, -1, -1 ) ) {
while( $row = $rs->fetchRow() ) {
$this->mConfig[$row['config_name']] = $row['config_value'];
}
}
}
return count( $this->mConfig );
}
// <<< getConfig
/**
* Add getConfig / setConfig for more uniform handling of config variables instead of spreading global vars.
* easily get the value of any given preference stored in kernel_config
*
* @access public
**/
public function getConfig( $pName, $pDefault = NULL ) {
if( empty( $this->mConfig ) ) {
$this->loadConfig();
}
return( empty( $this->mConfig[$pName] ) ? $pDefault : $this->mConfig[$pName] );
}
// <<< getConfigMatch
/**
* retreive a group of config variables
*
* @access public
**/
function getConfigMatch( $pPattern, $pSelectValue="" ) {
if( empty( $this->mConfig ) ) {
$this->loadConfig();
}
$matching_keys = preg_grep( $pPattern, array_keys( $this->mConfig ));
$new_array = array();
foreach( $matching_keys as $key=>$value ) {
if ( empty( $pSelectValue ) || ( !empty( $pSelectValue ) && $this->mConfig[$value] == $pSelectValue )) {
$new_array[$value] = $this->mConfig[$value];
}
}
return( $new_array );
}
/**
* storeConfigMatch set a group of config variables
*
* @param string $pPattern Perl regular expression
* @param string $pSelectValue only manipulate settings with this value set
* @param string $pNewValue New value that should be set for the matching settings (NULL will remove the entries from the DB)
* @param string $pPackage Package for which the settings are
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function storeConfigMatch( $pPattern, $pSelectValue = "", $pNewValue = NULL, $pPackage = NULL ) {
if( empty( $this->mConfig ) ) {
$this->loadConfig();
}
$matchingKeys = preg_grep( $pPattern, array_keys( $this->mConfig ));
foreach( $matchingKeys as $key => $config_name ) {
if( empty( $pSelectValue ) || ( !empty( $pSelectValue ) && $this->mConfig[$config_name] == $pSelectValue )) {
$this->storeConfig( $config_name, $pNewValue, $pPackage );
}
}
}
/**
* Set a hash value in the mConfig hash. This does *NOT* store the value in
* the database. It does no checking for existing or duplicate values. the
* main point of this function is to limit direct accessing of the mConfig
* hash. I will probably make mConfig private one day.
*
* @param string Hash key for the mConfig value
* @param string Value for the mConfig hash key
*/
function setConfig( $pName, $pValue ) {
$this->mConfig[$pName] = $pValue;
return( TRUE );
}
// <<< storeConfig
/**
* bitweaver needs lots of settings just to operate.
* loadConfig assigns itself the default preferences, then loads just the differences from the database.
* In storeConfig (and only when storeConfig is called) we make a second copy of defaults to see if
* preferences you are changing is different from the default.
* if it is the same, don't store it!
* So instead updating the whole prefs table, only updat "delta" of the changes delta from defaults.
*
* @access public
**/
function storeConfig( $pName, $pValue, $pPackage = NULL ) {
global $gMultisites;
//stop undefined offset error being thrown after packages are installed
if( !empty( $this->mConfig )) {
// store the pref if we have a value _AND_ it is different from the default
if( ( empty( $this->mConfig[$pName] ) || ( $this->mConfig[$pName] != $pValue ))) {
// make sure the value doesn't exceede database limitations
$pValue = substr( $pValue, 0, 250 );
// store the preference in multisites, if used
if( $this->isPackageActive( 'multisites' ) && @BitBase::verifyId( $gMultisites->mMultisiteId ) && isset( $gMultisites->mConfig[$pName] )) {
$query = "UPDATE `".BIT_DB_PREFIX."multisite_preferences` SET `config_value`=? WHERE `multisite_id`=? AND `config_name`=?";
$result = $this->mDb->query( $query, array( empty( $pValue ) ? '' : $pValue, $gMultisites->mMultisiteId, $pName ) );
} else {
$query = "DELETE FROM `".BIT_DB_PREFIX."kernel_config` WHERE `config_name`=?";
$result = $this->mDb->query( $query, array( $pName ) );
// make sure only non-empty values get saved, including '0'
if( isset( $pValue ) && ( !empty( $pValue ) || is_numeric( $pValue ))) {
$query = "INSERT INTO `".BIT_DB_PREFIX."kernel_config`(`config_name`,`config_value`,`package`) VALUES (?,?,?)";
$result = $this->mDb->query( $query, array( $pName, $pValue, strtolower( $pPackage )));
}
}
// Force the ADODB cache to flush
$isCaching = $this->mDb->isCachingActive();
$this->mDb->setCaching( FALSE );
$this->loadConfig();
$this->mDb->setCaching( $isCaching );
}
}
$this->setConfig( $pName, $pValue );
return TRUE;
}
// <<< expungePackageConfig
/**
* Delete all prefences for the given package
* @access public
**/
function expungePackageConfig( $pPackageName ) {
if( !empty( $pPackageName ) ) {
$query = "DELETE FROM `".BIT_DB_PREFIX."kernel_config` WHERE `package`=?";
$result = $this->mDb->query( $query, array( strtolower( $pPackageName ) ) );
// let's force a reload of the prefs
unset( $this->mConfig );
$this->loadConfig();
}
}
// === hasValidSenderEmail
/**
* Determines if this site has a legitimate sender address set.
*
* @param $mid the name of the template for the page content
* @access public
*/
function hasValidSenderEmail( $pSenderEmail=NULL ) {
if( empty( $pSenderEmail ) ) {
$pSenderEmail = $this->getConfig( 'site_sender_email' );
}
return( !empty( $pSenderEmail ) && !preg_match( '/.*localhost$/', $pSenderEmail ) );
}
// === getErrorEmail
/**
* Smartly determines where error emails should go
*
* @access public
*/
function getErrorEmail() {
$ret = NULL;
if( defined('ERROR_EMAIL') ) {
$ret = ERROR_EMAIL;
} elseif( $this->getConfig( 'bitmailer_sysadmin_email' ) ) {
$ret = $this->getConfig( 'bitmailer_sysadmin_email' );
} elseif( !empty( $_SERVER['SERVER_ADMIN'] ) ) {
$ret = $_SERVER['SERVER_ADMIN'];
} else {
$ret = 'root@localhost';
}
return $ret;
}
// === sendEmail
/**
* centralized function for send emails
*
* @param $mid the name of the template for the page content
* @access public
*/
function sendEmail( $pMailHash ) {
$extraHeaders = '';
if( $this->getConfig( 'bcc_email' ) ) {
$extraHeaders = "Bcc: ".$this->getConfig( 'bcc_email' )."\r\n";
}
if( !empty( $pMailHash['Reply-to'] ) ) {
$extraHeaders = "Reply-to: ".$pMailHash['Reply-to']."\r\n";
}
mail($pMailHash['email'],
$pMailHash['subject'].' '.$_SERVER["SERVER_NAME"],
$pMailHash['body'],
"From: ".$this->getConfig( 'site_sender_email' )."\r\nContent-type: text/plain;charset=utf-8\r\n$extraHeaders"
);
}
/**
* Set the http status, most notably for 404 not found for deleted content
*
* @param $pHttpStatus numerical HTTP status, most typically 404 (not found) or 403 (forbidden)
* @access public
*/
function setHttpStatus( $pHttpStatus ) {
$this->mHttpStatus = $pHttpStatus;
}
/**
* Display the main page template
*
* @param $mid the name of the template for the page content
* @param $browserTitle a string to be displayed in the top browser bar
* @param $format the output format - xml, ajax, content, full - relays to setRenderFormat
* @access public
*/
function display( $pMid, $pBrowserTitle = NULL, $pOptionsHash = array() ) {
global $gBitSmarty, $gBitThemes, $gContent;
$gBitSmarty->verifyCompileDir();
// see if we have a custom status other than 200 OK
if( isset( $this->mHttpStatus ) ) {
switch( $this->mHttpStatus ) {
// before you can spunky and decide to enter every HTTP status code under the sun here, please have the code needed someplace first
case '403':
header( "HTTP/1.0 403 Forbidden" );
break;
case '404':
header( "HTTP/1.0 404 Not Found" );
break;
}
}
// set the correct headers if it hasn't been done yet
if( empty( $gBitThemes->mFormatHeader )) {
// display is the last thing we call and therefore we need to set a default
$gBitThemes->setFormatHeader( !empty( $pOptionsHash['format'] ) ? $pOptionsHash['format'] : 'html' );
}
// set the desired display mode - this lets bitweaver know what type of page we are viewing
if( empty( $gBitThemes->mDisplayMode )) {
// display is the last thing we call and therefore we need to set a default
$gBitThemes->setDisplayMode( !empty( $pOptionsHash['display_mode'] ) ? $pOptionsHash['display_mode'] : 'display' );
}
if( $pMid == 'error.tpl' ) {
$this->setBrowserTitle( !empty( $pBrowserTitle ) ? $pBrowserTitle : tra( 'Error' ) );
$pMid = 'bitpackage:kernel/error.tpl';
}
// only using the default html header will print modules and all the rest of it.
if( $gBitThemes->mFormatHeader != 'html' ) {
$gBitSmarty->assign_by_ref( 'gBitSystem', $this );
$gBitSmarty->display( $pMid );
return;
}
// @TODO debug - causes where problems loading existing data
// $gBitThemes->loadAjax('jquery');
// $gBitThemes->loadJavascript( UTIL_PKG_PATH.'uniform/js/uni-form.jquery.js' );
if( !empty( $pBrowserTitle )) {
$this->setBrowserTitle( $pBrowserTitle );
}
// populate meta description with something useful so you are not penalized/ignored by web crawlers
if( is_object( $gContent ) && $gContent->isValid() ) {
if( $summary = $gContent->getField( 'summary' ) ) {
$desc = $gContent->parseData( $summary );
} elseif( $desc = $gContent->getField( 'parsed' ) ) {
} elseif( $summary = $gContent->getField( 'data' ) ) {
$desc = $gContent->parseData( $summary );
}
if( !empty( $desc ) ) {
$desc = $gContent->getContentTypeName().': '.$desc;
$gBitSmarty->assign_by_ref( 'metaDescription', substr( strip_tags( $desc ), 0, 256 ) );
}
}
$this->preDisplay( $pMid );
$gBitSmarty->assign( 'mid', $pMid );
// $gBitSmarty->assign( 'page', !empty( $_REQUEST['page'] ) ? $_REQUEST['page'] : NULL );
// Make sure that the gBitSystem symbol available to templates is correct and up-to-date.
$gBitSmarty->assign_by_ref('gBitSystem', $this);
if( !empty( $pOptionsHash['return'] ) && $pOptionsHash['return'] == 'fetch' ){
return $gBitSmarty->fetch( 'bitpackage:kernel/bitweaver.tpl' );
}else{
$gBitSmarty->display( 'bitpackage:kernel/bitweaver.tpl' );
}
$this->postDisplay( $pMid );
}
// === preDisplay
/**
* Take care of any processing that needs to happen just before the template is displayed
*
* @param none $
* @access private
*/
function preDisplay( $pMid ) {
global $gCenterPieces, $gBitSmarty, $gBitThemes, $gDefaultCenter;
$gBitThemes->loadLayout();
// check to see if we are working with a dynamic center area
if( $pMid == 'bitpackage:kernel/dynamic.tpl' ) {
// pre-render dynamic center content
$dynamicContent = "";
if( !empty( $gCenterPieces ) ){
foreach ( $gCenterPieces as $centerPiece ){
$gBitSmarty->assign( 'moduleParams', $centerPiece );
$dynamicContent .= $gBitSmarty->fetch( $centerPiece['module_rsrc'] );
}
}elseif( $gDefaultCenter ){
$dynamicContent = $gBitSmarty->fetch( $gDefaultCenter );
}
$gBitSmarty->assign( 'dynamicContent', $dynamicContent );
}
$gBitThemes->preLoadStyle();
/* @TODO - fetch module php files before rendering tpls.
* The basic problem here is center_list and module files are
* processed during page rendering, which means code in those
* files can not set <head> information before rendering. Kinda sucks.
*
* So what this does is, this calls on a service function allowing any
* package to check if its center or other module file is going to be
* called and gives it a chance to set any information for <head> first.
*
* Remove when TODO is complete. -wjames5
*/
global $gBitUser;
if( isset( $gBitUser )) {
$gBitUser->invokeServices( 'module_display_function' );
}
// process layout
require_once( THEMES_PKG_PATH.'modules_inc.php' );
$gBitThemes->loadStyle();
/* force the session to close *before* displaying. Why? Note this very important comment from http://us4.php.net/exec
edwin at bit dot nl
23-Jan-2002 04:47
If you are using sessions and want to start a background process, you might
have the following problem:
The first time you call your script everything goes fine, but when you call it again
and the process is STILL running, your script seems to "freeze" until you kill the
process you started the first time.
You'll have to call session_write_close(); to solve this problem. It has something
to do with the fact that only one script/process can operate at once on a session.
(others will be lockedout until the script finishes)
I don't know why it somehow seems to be influenced by background processes,
but I tried everything and this is the only solution. (i had a perl script that
"daemonized" it's self like the example in the perl manuals)
Took me a long time to figure out, thanks ian@virtisp.net! :-)
... and a similar issue can happen for very long display times.
*/
session_write_close();
}
// === postDisplay
/**
* Take care of any processing that needs to happen just after the template is displayed
*
* @param none $
* @access private
*/
function postDisplay( $pMid ) {
}
// === setHelpInfo
/**
* Set the smarty variables needed to display the help link for a page.
*
* @param $package Package Name
* @param $context Context of the help within the package
* @param $desc Description of the help link (not the help itself)
* @access private
*/
function setHelpInfo( $package, $context, $desc ) {
global $gBitSmarty;
$gBitSmarty->assign( 'TikiHelpInfo', array( 'URL' => 'http://doc.bitweaver.org/wiki/index.php?page=' . $package . $context , 'Desc' => $desc ) );
}
// === isPackageActive
/**
* check's if a package is active.
* @param $pPackageGuid the guid of the package to test
* @return boolean
* @access public
*/
function isPackageActive( $pPackageGuid ) {
return( $this->getPackageConfigValue( $pPackageGuid, 'active' ) == 'y' );
}
// === isPackageActiveEarly
/**
* check if a package is active; but only do this after making sure a package
* has had it's bit_setup_inc loaded if possible. This func exists for use in
* other packages bit_setup_inc's to avoid dependency on load order and ugly code
* @param $pPackageName the name of the package to test
* where the package name is in the form used to index $mPackages
* See comments in scanPackages for more information
* @return boolean
* @access public
*/
function isPackageActiveEarly( $pPackageName ) {
$ret = FALSE;
$pkgname_l = strtolower( $pPackageName );
if( is_file(BIT_ROOT_PATH.$pkgname_l.'/bit_setup_inc.php') ) {
require_once(BIT_ROOT_PATH.$pkgname_l.'/bit_setup_inc.php');
$ret = $this->isPackageActive( $pPackageName );
} elseif( $pkgname_l == 'kernel' ) {
$ret = TRUE;
}
return( $ret );
}
// === isPackageInstalled
/**
* check's if a package is Installed
* @param $pPackageGuid the guid of the package to test
* @return boolean
* @access public
*/
function isPackageInstalled( $pPackageGuid ){
return !is_null( $this->getInstalledPackageConfig( $pPackageGuid ) );
}
// === isPackageRequired
/**
* check's if a package is required.
* @param $pPackageGuid the guid of the package to test
* @return boolean
* @access public
*/
function isPackageRequired( $pPackageGuid ) {
return( $this->getPackageSchemaValue( $pPackageGuid, 'required' ) == 'y' );
}
// === verifyPackage
/**
* It will verify that the given package is active or it will display the error template and die()
* @param $pPackageGuid the name of the package to test
* @return boolean
* @access public
*/
function verifyPackage( $pPackageGuid ) {
if( !$this->isPackageActive( $pPackageGuid ) ) {
$this->fatalError( tra("This package is disabled").": package_$pPackageGuid" );
}
return( TRUE );
}
// === getPermissionInfo
/**
* It will get information about a permissions
* @param $pPermission value of a given permission
* @return none
* @access public
*/
function getPermissionInfo( $pPermission = NULL, $pPackageName = NULL ) {
$ret = NULL;
$bindVars = array();
$sql = 'SELECT * FROM `'.BIT_DB_PREFIX.'users_permissions` ';
if( !empty( $pPermission ) ) {
$sql .= ' WHERE `perm_name`=? ';
array_push( $bindVars, $pPermission );
} elseif( !empty( $pPackageName ) ) {
$sql .= ' WHERE `package` = ? ';
array_push( $bindVars, substr($pPackageName,0,100));
}
$ret = $this->mDb->getAssoc( $sql, $bindVars );
return $ret;
}
// === verifyPermission
/**
* DEPRECATED - this function has been moved into BitPermUser, use that
*/
function verifyPermission( $pPermission, $pMsg = NULL ) {
global $gBitUser;
return $gBitUser->verifyPermission( $pPermission, $pMsg );
}
// === fatalPermission
/**
* Interupt code execution and show a permission denied message.
* This does not show a big nasty denied message if user is simply not logged in.
* This *could* lead to a user seeing a denied message twice, however this is
* unlikely as logic permission checks should prevent access to non-permed page REQUEST in the first place
* @param $pPermission value of a given permission
* @param $pMsg optional additional information to present to user
* @return none
* @access public
*/
function fatalPermission( $pPermission, $pMsg=NULL ) {
global $gBitUser, $gBitSmarty;
if( !$gBitUser->isRegistered() ) {
$gBitSmarty->assign( 'errorHeading', 'Please login …' );
$title = 'Please login …';
$pMsg .= '</p><p>You must be logged in. Please <a href="'.USERS_PKG_URL.'login.php">login</a>';
if( $this->getConfig( 'users_allow_register', 'y' ) == 'y' ) {
$pMsg .= ' or <a href="'.USERS_PKG_URL.'register.php">register</a>.';
}
$gBitSmarty->assign( 'template', 'bitpackage:users/login_inc.tpl' );
} else {
$title = 'Oops!';
if( empty( $pMsg ) ) {
$permDesc = $this->getPermissionInfo( $pPermission );
$pMsg = "You do not have the required permissions ";
if( !empty( $permDesc[$pPermission]['perm_desc'] ) ) {
if( preg_match( '/administrator,/i', $permDesc[$pPermission]['perm_desc'] ) ) {
$pMsg .= preg_replace( '/^administrator, can/i', ' to ', $permDesc[$pPermission]['perm_desc'] );
} else {
$pMsg .= preg_replace( '/^can /i', ' to ', $permDesc[$pPermission]['perm_desc'] );
}
}
}
$gBitSmarty->assign( 'fatalTitle', tra( "Permission denied." ) );
}
// bit_log_error( "PERMISSION DENIED: $pPermission $pMsg" );
$gBitSmarty->assign( 'msg', tra( $pMsg ) );
$this->display( "error.tpl" , tra("Error"), array( 'display_mode' => 'error' ) );
die;
}
/**
* This code was duplicated _EVERYWHERE_ so here is an easy template to cut that down.
* @param $pFormHash documentation needed
* @param $pMsg documentation needed
* @return none
* @access public
*/
function confirmDialog( $pFormHash, $pMsg, $pDisplayMode = 'edit' ) {
global $gBitSmarty;
if( !empty( $pMsg ) ) {
// automatically preserve pagination
if( !empty( $_REQUEST['find'] ) )
$pFormHash['find'] = $_REQUEST['find'];
if( !empty( $_REQUEST['sort_mode'] ) )
$pFormHash['sort_mode'] = $_REQUEST['sort_mode'];
if( !empty( $_REQUEST['list_page'] ) )
$pFormHash['list_page'] = $_REQUEST['list_page'];
if( !empty( $_REQUEST['offset'] ) )
$pFormHash['offset'] = $_REQUEST['offset'];
if( !empty( $_REQUEST['max_records'] ) )
$pFormHash['max_records'] = $_REQUEST['max_records'];
// cancel
if( empty( $pParamHash['cancel_url'] ) ) {
$gBitSmarty->assign( 'backJavascript', 'onclick="history.back();"' );
}
// reserved hash param 'input' injects custom input html
if( !empty( $pFormHash['input'] ) ) {
$gBitSmarty->assign( 'inputFields', $pFormHash['input'] );
unset( $pFormHash['input'] );
}
// render and exit
$gBitSmarty->assign( 'msgFields', $pMsg );
$gBitSmarty->assign_by_ref( 'hiddenFields', $pFormHash );
$this->display( 'bitpackage:kernel/confirm.tpl', NULL, array( 'display_mode' => $pDisplayMode ));
die;
}
}
// === isFeatureActive
/**
* check's if the specfied feature is active
*
* @param $pKey hash key
* @return none
* @access public
*/
function isFeatureActive( $pFeatureName ) {
$ret = FALSE;
if( $pFeatureName ) {
$featureValue = $this->getConfig($pFeatureName);
$ret = !empty( $featureValue ) && ( $featureValue != 'n' );
}
return( $ret );
}
// === verifyFeature
/**
* It will verify that the given feature is active or it will display the error template and die()
* @param $pFeatureName the name of the package to test
* @return none
* @access public
*
* @param $pKey hash key
*/
function verifyFeature( $pFeatureName ) {
if( !$this->isFeatureActive( $pFeatureName ) ) {
$this->fatalError( tra("This feature is disabled").": $pFeatureName" );
}
return( TRUE );
}
// === registerPackage
/**
* Define name, location and url DEFINE's
*
* @param $pKey hash key
* @return none
* @access public
*/
function registerPackage( $pRegisterHash ) {
global $gBitSystem;
if( $gBitSystem->isFeatureActive( 'kernel_autoscan_pkgs', FALSE ) ){
$this->configPackage( $pRegisterHash );
}
}
function configPackage( $pRegisterHash ){
if( !isset( $pRegisterHash['package_name'] )) {
$this->fatalError( tra("Package name not set in ")."registerPackage: $this->mPackageFileName" );;
} else {
$name = $pRegisterHash['package_name'];
}
if( !isset( $pRegisterHash['package_path'] )) {
$this->fatalError( tra("Package path not set in ")."registerPackage: $this->mPackageFileName" );;
} else {
$path = $pRegisterHash['package_path'];
}
$this->mRegisterCalled = TRUE;
if( empty( $this->mPackagesConfig )) {
$this->loadPackagesConfig();
}
$pkgName = str_replace( ' ', '_', strtoupper( $name ));
$packageGuid = $pkgNameKey = strtolower( $pkgName );
// Define <PACKAGE>_PKG_PATH
$pkgDefine = $pkgName.'_PKG_PATH';
if( !defined( $pkgDefine )) {
define( $pkgDefine, $path );
}
// Define <PACKAGE>_PKG_URL
$pkgDefine = $pkgName.'_PKG_URL';
if( !defined( $pkgDefine )) {
// Force full URI's for offline or exported content (newsletters, etc.)
$root = !empty( $_REQUEST['uri_mode'] ) ? BIT_BASE_URI . BIT_ROOT_URL : BIT_ROOT_URL;
define( $pkgDefine, $root . basename( $path ) . '/' );
}
// Define <PACKAGE>_PKG_URI
$pkgDefine = $pkgName.'_PKG_URI';
if( !defined( $pkgDefine ) && defined( 'BIT_BASE_URI' )) {
define( $pkgDefine, BIT_BASE_URI . BIT_ROOT_URL . basename( $path ) . '/' );
}
// Define <PACKAGE>_PKG_NAME
$pkgDefine = $pkgName.'_PKG_NAME';
if( !defined( $pkgDefine )) {
define( $pkgDefine, $packageGuid );
}
// Define <PACKAGE>_PKG_DIR
$package_dir_name = basename( $path );
$pkgDefine = $pkgName.'_PKG_DIR';
if( !defined( $pkgDefine )) {
define( $pkgDefine, $package_dir_name );
}
// Define <PACKAGE>_PKG_TITLE
$pkgDefine = $pkgName.'_PKG_TITLE';
if( !defined( $pkgDefine )) {
define( $pkgDefine, ucfirst( constant( $pkgName.'_PKG_DIR' ) ) );
}
if( $this->isPackageInstalled( $packageGuid ) ){
// @TODO move this to yaml and registration table
// $this->mPackagesConfig[$pkgNameKey]['service'] = !empty( $pRegisterHash['service'] ) ? $pRegisterHash['service'] : FALSE;
// pass package settings to installed packages hash
$this->mPackagesConfig[$pkgNameKey]['url'] = BIT_ROOT_URL . basename( $path ) . '/';
$this->mPackagesConfig[$pkgNameKey]['path'] = BIT_ROOT_PATH . basename( $path ) . '/';
$this->mPackagesConfig[$pkgNameKey]['dir'] = $package_dir_name;
$this->mPackagesDirNameXref[$package_dir_name] = $pkgNameKey;
}
}
function defineActivePackage(){
if( !empty( $this->mPackagesConfig ) && !defined( 'ACTIVE_PACKAGE' ) ){
$activePackage = NULL;
// Work around for old versions of IIS that do not support $_SERVER['SCRIPT_FILENAME'] - wolff_borg
if( !array_key_exists( 'SCRIPT_FILENAME', $_SERVER )) {
//remove double-backslashes and return
$_SERVER['SCRIPT_FILENAME'] = str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED'] );
}
// Define the package we are currently in
// I tried strpos instead of preg_match here, but it didn't like strings that begin with slash?! - spiderr
$scriptDir = ( basename( dirname( $_SERVER['SCRIPT_FILENAME'] ) ) );
if( isset( $_SERVER['ACTIVE_PACKAGE'] )) {
// perhaps the webserver told us the active package (probably because of mod_rewrites)
$activePackage = $_SERVER['ACTIVE_PACKAGE'];
}else{
foreach( $this->mPackagesConfig as $pkgGuid => $pkgHash ){
if( $scriptDir == $pkgHash['dir']
|| isset( $_SERVER['ACTIVE_PACKAGE'] )
|| preg_match( '!/'.$pkgHash['dir'].'/!', $_SERVER['PHP_SELF'] )
|| preg_match( '!/'.$pkgGuid.'/!', $_SERVER['PHP_SELF'] )
){
$activePackage = $pkgGuid;
break;
}
}
}
if( $activePackage ){
define( 'ACTIVE_PACKAGE', $activePackage );
$this->mActivePackage = $activePackage;
}elseif( !defined( 'ACTIVE_PACKAGE' ) && $defaultPkg = $this->getConfig('bit_index' ) ){
define( 'ACTIVE_PACKAGE', $defaultPkg );
$this->mActivePackage = $defaultPkg;
}
}
}
// === registerAppMenu
/**
* Register global system menu. Due to the startup nature of this method, it need to belong in BitSystem instead of BitThemes, where it would more naturally fit.
*
* @param $pKey hash key
* @return none
* @access public
*/
function registerAppMenu( $pMenuHash, $pMenuTitle = NULL, $pTitleUrl = NULL, $pMenuTemplate = NULL, $pAdminPanel = FALSE ) {
if( is_array( $pMenuHash ) ) {
// shorthand
$pkg = $pMenuHash['package_name'];
// prepare hash
$pMenuHash['style'] = 'display:'.( ( isset( $_COOKIE[$pMenuHash.'menu'] ) && ( $_COOKIE[$pMenuHash.'menu'] == 'o' ) ) ? 'block;' : 'none;' );
$pMenuHash['is_disabled'] = ( $this->getConfig( 'menu_'.$pkg ) == 'n' );
$pMenuHash['menu_title'] = $this->getConfig( $pkg.'_menu_text',
( !empty( $pMenuHash['menu_title'] )
? $pMenuHash['menu_title']
: ucfirst( constant( strtoupper( $pkg ).'_PKG_DIR' )))
);
$pMenuHash['menu_position'] = $this->getConfig( $pkg.'_menu_position',
( !empty( $pMenuHash['menu_position'] )
? $pMenuHash['menu_position']
: NULL )
);
$this->mAppMenu[$pkg] = $pMenuHash;
} else {
deprecated( 'Please use a menu registration hash instead of individual parameters: $gBitSystem->registerAppMenu( $menuHash )' );
$this->mAppMenu[strtolower( $pMenuHash )] = array(
'menu_title' => $pMenuTitle,
'is_disabled' => ( $this->getConfig( 'menu_'.$pMenuHash ) == 'n' ),
'index_url' => $pTitleUrl,
'menu_template' => $pMenuTemplate,
'admin_panel' => $pAdminPanel,
'style' => 'display:'.( empty( $pMenuTitle ) || ( isset( $_COOKIE[$pMenuHash.'menu'] ) && ( $_COOKIE[$pMenuHash.'menu'] == 'o' ) ) ? 'block;' : 'none;' )
);
}
uasort( $this->mAppMenu, 'bit_system_menu_sort' );
}
/**
* registerNotifyEvent
*
* @param array $pEventHash
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function registerNotifyEvent( $pEventHash ) {
$this->mNotifyEvents = array_merge( $this->mNotifyEvents, $pEventHash );
}
// === fatalError
/**
* If an unrecoverable error has occurred, this method should be invoked. script exist occurs
*
* @param string $ pMsg error message to be displayed
* @param string template file used to display error
* @param string error dialog title. default gets site_error_title config, passing '' will result in no title
* @return none this function will DIE DIE DIE!!!