forked from francoisjacquet/rosariosis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWarehouse.php
799 lines (664 loc) · 16.9 KB
/
Warehouse.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
<?php
/**
* Warehouse
*
* Get configuration
* Load functions
* Start Session
* Sanitize $_REQUEST array
* Internationalization
* Modules & Plugins
* Update RosarioSIS
* Warehouse() function (Output HTML header (including Bottom & Side menus), or footer)
* isPopup() function (Popup window detection)
* isAJAX() function (AJAX request detection)
* ETagCache() function (ETag cache system)
*
* @package RosarioSIS
*/
define( 'ROSARIO_VERSION', '5.1-beta' );
/**
* Include config.inc.php file.
*
* Do NOT change for require_once, include_once allows the error message to be displayed.
*/
if ( ! include_once 'config.inc.php' )
{
die( 'config.inc.php file not found. Please read the installation directions.' );
}
require_once 'database.inc.php';
/**
* Optional configuration
* You can override the following definitions in the config.inc.php file
*/
if ( ! defined( 'ROSARIO_DEBUG' ) )
{
// Debug mode (for developers): enables notices.
define( 'ROSARIO_DEBUG', false );
}
if ( ROSARIO_DEBUG )
{
error_reporting( E_ALL );
}
else
{
error_reporting( E_ALL ^ E_NOTICE );
}
// Server Paths.
if ( ! isset( $RosarioPath ) )
{
$RosarioPath = dirname( __FILE__ ) . '/'; // PHP 5.3 compatible equivalent to __DIR__.
}
if ( ! isset( $StudentPicturesPath ) )
{
$StudentPicturesPath = 'assets/StudentPhotos/';
}
if ( ! isset( $UserPicturesPath ) )
{
$UserPicturesPath = 'assets/UserPhotos/';
}
if ( ! isset( $FileUploadsPath ) )
{
$FileUploadsPath = 'assets/FileUploads/';
}
if ( ! isset( $LocalePath ) )
{
// Path were the language packs are stored. You need to restart Apache at each change in this directory.
$LocalePath = 'locale';
}
if ( ! isset( $ETagCache ) )
{
// ETag cache system.
$ETagCache = true;
}
/**
* Load functions
*/
$functions = glob( 'functions/*.php' );
foreach ( $functions as $function )
{
require_once $function;
}
if ( isset( $Timezone ) )
{
// Time zone.
// Sets the default time zone used by all date/time functions.
if ( date_default_timezone_set( $Timezone ) )
{
// If valid PHP timezone_identifier, should be OK for Postgres.
DBQuery( "SET TIMEZONE TO '" . $Timezone . "'" );
}
}
// Send email on PHP fatal error.
register_shutdown_function( 'ErrorSendEmail' );
/**
* Start Session
*/
session_name( 'RosarioSIS' );
// See http://php.net/manual/en/session.security.php.
$cookie_path = dirname( $_SERVER['SCRIPT_NAME'] ) === DIRECTORY_SEPARATOR ?
'/' : dirname( $_SERVER['SCRIPT_NAME'] ) . '/';
session_set_cookie_params(
0,
$cookie_path,
'',
// Cookie secure flag for https.
( ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ) ||
( isset( $_SERVER['SERVER_PORT'] ) && $_SERVER['SERVER_PORT'] == 443 ) ),
true
);
session_cache_limiter( 'nocache' );
session_start();
if ( empty( $_SESSION['STAFF_ID'] )
&& empty( $_SESSION['STUDENT_ID'] )
&& basename( $_SERVER['SCRIPT_NAME'] ) !== 'index.php' )
{
// Logout if no Staff or Student session ID.
/**
* Redirect to Modules.php URL after login.
*
* @since 3.8
*/
$redirect_to = basename( $_SERVER['SCRIPT_NAME'] ) === 'Modules.php' ?
'&redirect_to=' . urlencode( $_SERVER['QUERY_STRING'] ) :
'';
// Redirection is done in Javascript in case current request is AJAX.
?>
<script>window.location.href = "index.php?modfunc=logout" +
<?php echo json_encode( $redirect_to ); ?>;</script>
<?php
exit;
}
/**
* Array recursive walk
*
* @param array $array Array by reference.
* @param string $function Function name.
* @return array &$array Array passed through $function function
*/
function array_rwalk( &$array, $function )
{
$key = array_keys( $array );
$size = count( $key );
for ( $i = 0; $i < $size; $i++ )
{
if ( is_array( $array[$key[$i]] ) )
{
array_rwalk( $array[$key[$i]], $function );
}
else
{
$array[$key[$i]] = $function( $array[$key[$i]] );
}
}
}
/**
* Sanitize $_REQUEST array
* ($_POST + $_GET)
*/
// Escape strings for DB queries.
array_rwalk( $_REQUEST, 'DBEscapeString' );
// Remove HTML tags.
array_rwalk( $_REQUEST, 'strip_tags' );
/**
* Internationalization
*/
if ( ! empty( $_GET['locale'] ) )
{
$_SESSION['locale'] = $_GET['locale'];
}
if ( empty( $_SESSION['locale'] ) )
{
$_SESSION['locale'] = $RosarioLocales[0]; // English?
}
$locale = $_SESSION['locale'];
putenv( 'LC_ALL=' . $locale );
setlocale( LC_ALL, $locale );
// FJ numeric separator ".".
setlocale( LC_NUMERIC, 'english', 'en_US', 'en_US.utf8' );
// FJ bugfix for Turkish characters conversion.
if ( $locale === 'tr_TR.utf8' )
{
setlocale( LC_CTYPE, 'english', 'en_US', 'en_US.utf8' );
}
// Binds the messages domain to the locale folder.
bindtextdomain( 'rosariosis', $LocalePath );
// Ensures text returned is utf-8, quite often this is iso-8859-1 by default.
bind_textdomain_codeset( 'rosariosis', 'UTF-8' );
// Sets the domain name, this means gettext will be looking for a file called rosariosis.mo.
textdomain( 'rosariosis' );
// FJ multibyte strings.
mb_internal_encoding( 'UTF-8' );
if ( ROSARIO_DEBUG )
{
require_once 'ProgramFunctions/Debug.fnc.php';
// @since 5.0 Load Kint.
Kint();
}
/**
* Update RosarioSIS
* Automatically runs after manual files update
* To apply eventual incremental DB updates
*
* @see ProgramFunctions/Update.fnc.php
* @since 2.9
*/
// Check if version in DB < ROSARIO_VERSION.
if ( version_compare( Config( 'VERSION' ), ROSARIO_VERSION, '<' ) )
{
require_once 'ProgramFunctions/Update.fnc.php';
// Run Update() to apply updates if any.
Update();
}
/**
* Modules
*
* Core modules (packaged with RosarioSIS):
* Core modules cannot be deleted.
*/
$RosarioCoreModules = array(
'School_Setup',
'Students',
'Users',
'Scheduling',
'Grades',
'Attendance',
'Eligibility',
'Discipline',
'Accounting',
'Student_Billing',
'Food_Service',
'Resources',
'Custom',
);
$RosarioModules = unserialize( Config( 'MODULES' ) );
$non_core_modules = array_diff_key( $RosarioModules, array_flip( $RosarioCoreModules ) );
_LoadAddons( $non_core_modules, 'modules/' );
/**
* Plugins
*
* Core plugins (packaged with RosarioSIS):
* Core plugins cannot be deleted
*/
$RosarioCorePlugins = array(
'Moodle',
);
$RosarioPlugins = unserialize( Config( 'PLUGINS' ) );
$non_core_plugins = array_diff_key( $RosarioPlugins, array_flip( $RosarioCorePlugins ) );
// Load Moodle plugin functions.
if ( $RosarioPlugins['Moodle'] )
{
require_once 'plugins/Moodle/functions.php';
}
_LoadAddons( $non_core_plugins, 'plugins/' );
/**
* Load not core modules & plugins
* (functions & locale)
* Deactivate if does not exist
*
* Local function
*
* @param array $addons Non core addons (Plugins or Modules).
* @param string $folder Plugin or Module folder.
* @return void
*/
function _LoadAddons( $addons, $folder )
{
global $RosarioModules,
$RosarioPlugins;
/**
* Check if non core activated modules exist.
* Load locale.
* Load functions (optional).
*/
foreach ( (array) $addons as $addon => $activated )
{
if ( ! $activated )
{
continue;
}
if ( $folder === 'modules/'
&& ! file_exists( $folder . $addon . '/Menu.php' ) )
{
// If module does not exist, deactivate it.
$RosarioModules[$addon] = false;
continue;
}
$addon_functions = $folder . $addon . '/functions.php';
if ( file_exists( $addon_functions ) )
{
require_once $addon_functions;
}
elseif ( $folder === 'plugins/' )
{
// If plugin does not exist, deactivate it.
$RosarioPlugins[$addon] = false;
continue;
}
// Load addon locale.
$locale_path = $folder . $addon . '/locale';
if ( ! is_dir( $locale_path ) )
{
continue;
}
// Binds the messages domain to the locale folder.
bindtextdomain( $addon, $locale_path );
if ( function_exists( '_bindtextdomain' ) )
{
// Correctly bind domain when MoTranslator is in use.
_bindtextdomain( $addon, $locale_path );
}
// Ensures text returned is utf-8, quite often this is iso-8859-1 by default.
bind_textdomain_codeset( $addon, 'UTF-8' );
}
}
/**
* Output HTML header (including Bottom & Side menus), or footer
*
* @example Warehouse( 'header' );
*
* @since 3.8 Warehouse header head hook
* @since 3.8 Warehouse footer hook
* @since 4.4 Warehouse header hook
*
* @global $_ROSARIO Uses $_ROSARIO['ProgramLoaded']
*
* @uses isPopup()
* @uses isAJAX()
* @uses ETagCache()
*
* @param string $mode 'header' or 'footer'.
*/
function Warehouse( $mode )
{
global $_ROSARIO;
if ( isset( $_REQUEST['_ROSARIO_PDF'] ) )
{
if ( $mode === 'header' )
{
// Start buffer.
ob_start();
}
// Printing PDF, skip, see PDF.fnc.php.
return;
}
switch ( $mode )
{
// Header HTML.
case 'header':
ETagCache( 'start' );
if ( isAJAX() )
{
// If jQuery not available, log out.
if ( $_ROSARIO['page'] === 'modules' ): ?>
<script>if (!window.$) window.location.href = 'index.php?modfunc=logout';</script>
<?php endif;
// AJAX: we only need to generate #body content.
break;
}
$lang_2_chars = mb_substr( $_SESSION['locale'], 0, 2 );
// Right to left direction.
$RTL_languages = array( 'ar', 'he', 'dv', 'fa', 'ur' );
$dir_RTL = in_array( $lang_2_chars, $RTL_languages ) ? ' dir="RTL"' : '';
?>
<!doctype html>
<html lang="<?php echo $lang_2_chars; ?>"<?php echo $dir_RTL; ?>>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title><?php echo ParseMLField( Config( 'TITLE' ) ); ?></title>
<link rel="icon" href="favicon.ico" sizes="32x32" />
<link rel="icon" href="apple-touch-icon.png" sizes="128x128" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="mobile-web-app-capable" content="yes" />
<link rel="stylesheet" href="assets/themes/<?php echo Preferences( 'THEME' ); ?>/stylesheet.css?v=<?php echo ROSARIO_VERSION; ?>" />
<style>.highlight,.highlight-hover:hover{background-color:<?php echo Preferences( 'HIGHLIGHT' ); ?> !important;}</style>
<?php
if ( $_ROSARIO['page'] === 'modules'
|| $_ROSARIO['page'] === 'create-account'
|| $_ROSARIO['page'] === 'password-reset' ): ?>
<script src="assets/js/jquery.js"></script>
<script src="assets/js/plugins.min.js?v=<?php echo ROSARIO_VERSION; ?>"></script>
<script src="assets/js/warehouse.min.js?v=<?php echo ROSARIO_VERSION; ?>"></script>
<script src="assets/js/jscalendar/lang/calendar-<?php echo file_exists( 'assets/js/jscalendar/lang/calendar-' . $lang_2_chars . '.js' ) ? $lang_2_chars : 'en'; ?>.js"></script>
<?php
// Add scripts.js file from theme if any found.
if ( file_exists( 'assets/themes/' . Preferences( 'THEME' ) . '/scripts.js' ) ): ?>
<script src="assets/themes/<?php echo Preferences( 'THEME' ); ?>/scripts.js"></script>
<?php endif;
endif;
/**
* Hook.
*
* Add your extra module/plugin JS and/or CSS (dependencies) to HTML head here.
*
* @since 3.8
*
* @since 4.5.1 Move Header head action hook outisde page condition.
*/
do_action( 'Warehouse.php|header_head' );
?>
<noscript>
<meta http-equiv="REFRESH" content="0;url=index.php?modfunc=logout&reason=javascript" />
</noscript>
</head>
<body class="<?php echo $_ROSARIO['page']; ?>">
<?php
if ( $_ROSARIO['page'] === 'modules' ):
// If popup window, verify it is an actual popup.
if ( isPopup() ):
?>
<script>if(window == top && (!window.opener)) window.location.href = "Modules.php?modname=misc/Portal.php";</script>
<?php else: ?>
<div id="wrap">
<footer id="footer" class="mod">
<?php require_once 'Bottom.php'; // Include Bottom menu. ?>
</footer>
<aside id="menu" class="mod">
<?php require_once 'Side.php'; // Include Side menu. ?>
</aside>
<?php
endif;
endif;
?>
<div id="body" tabindex="0" role="main" class="mod">
<?php
/**
* Hook.
*
* Add your extra module/plugin HTML to body here.
*
* @since 4.4
*/
do_action( 'Warehouse.php|header' );
break;
// Footer HTML.
case 'footer':
?>
<br />
<?php
if ( $_ROSARIO['page'] === 'modules' ): ?>
<script>
var modname = "<?php echo issetVal( $_ROSARIO['ProgramLoaded'], '' ); ?>";
if (typeof menuStudentID !== 'undefined'
&& (menuStudentID != "<?php echo UserStudentID(); ?>"
|| menuStaffID != "<?php echo UserStaffID(); ?>"
|| menuSchool != "<?php echo UserSchool(); ?>"
|| menuMP != "<?php echo UserMP(); ?>"
|| menuCoursePeriod != "<?php echo UserCoursePeriod(); ?>")) {
ajaxLink( 'Side.php?sidefunc=update' );
}
</script>
<?php
/**
* Hook.
*
* Add your extra module/plugin JS (dependencies) to HTML footer here.
*
* @since 3.8
* @since 4.4 Hook always runs (no conditions).
*/
do_action( 'Warehouse.php|footer' );
// If not AJAX request.
if ( ! isAJAX() ):
?>
</div><!-- #body -->
<div class="ajax-error"></div>
<?php
if ( ! isPopup() ):
?>
</div><!-- #wrap -->
<?php endif;
?>
</body></html>
<?php endif;
if ( ! isPopup() ):
// require_once 'ProgramFunctions/Help.fnc.php';
// Check if module has help (not default).
//$has_help_text = GetHelpTextRaw( $_REQUEST['modname'] );
/*if ( $has_help_text )
{
var_dump($_REQUEST['modname']);
echo 'Has Help!!!';
}*/
endif;
else: // Other pages (not modules).
?>
</div><!-- #body -->
</body></html>
<?php endif;
ETagCache( 'stop' );
break;
}
// End switch.
}
// End Warehouse().
/**
* Popup window detection
*
* Set it once in Modules.php:
* @example isPopup( $modname, $_REQUEST['modfunc'] );
* Later call it:
* @example if ( isPopup() )
* @link http://www.securiteam.com/securitynews/6S02U1P6BI.html
*
* @param string $modname Mod name. Optional, defaults to ''.
* @param string $modfunc Mod function. Optional, defaults to ''.
* @return boolean True if popup, else false
*/
function isPopup( $modname = '', $modfunc = '' )
{
/**
* @var mixed
*/
static $is_popup = null;
if ( ! is_null( $is_popup ) )
{
return $is_popup;
}
$is_popup = false;
if ( ! $modname )
{
return $is_popup;
}
/**
* Popup window detection.
*
* FJ security fix, cf http://www.securiteam.com/securitynews/6S02U1P6BI.html
*/
if ( in_array(
$modname,
array(
'misc/ChooseRequest.php',
'misc/ChooseCourse.php',
'misc/ViewContact.php',
)
)
|| ( $modname === 'School_Setup/Calendar.php'
&& $modfunc === 'detail' )
|| ( in_array(
$modname,
array(
'Scheduling/MassDrops.php',
'Scheduling/Schedule.php',
'Scheduling/MassSchedule.php',
'Scheduling/MassRequests.php',
'Scheduling/Courses.php',
)
)
&& $modfunc === 'choose_course' ) )
{
$is_popup = true;
}
return $is_popup;
}
/**
* AJAX request detection
*
* @example if ( isAJAX() )
* @since 3.0.1
*
* @return boolean True if is AJAX, else false
*/
function isAJAX()
{
/**
* @var mixed
*/
static $is_ajax = null;
if ( ! is_null( $is_ajax ) )
{
return $is_ajax;
}
$is_ajax = isset( $_SERVER['HTTP_X_REQUESTED_WITH'] )
&& $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest';
return $is_ajax;
}
/**
* ETag cache system.
* Start buffer, or stop buffer
* and calculate ETag:
* If ETag === If-None-Match, then send 403
* Else send content (buffer) + ETag.
* If no mode is set, will only check if $ETagCache activated.
*
* @global $ETagCache ETag cache system.
* @since 3.1
*
* @param string $mode Mode: start|stop. Optional, defaults to ''.
* @return boolean True if ETagCache activated, else false.
*/
function ETagCache( $mode = '' )
{
global $ETagCache,
$_ROSARIO;
/**
* @var mixed
*/
static $ob_started = false;
if ( ! $ETagCache )
{
return false;
}
if ( $mode === 'start'
&& ! $ob_started )
{
// Start buffer (to generate ETag).
$ob_started = ob_start();
}
elseif ( $mode === 'stop'
&& $ob_started )
{
// Stop & get buffer buffer (to generate ETag).
$etag_buffer = ob_get_clean();
/**
* Generate ETag
* Weak ETag ("W/").
*
* @link https://en.wikipedia.org/wiki/HTTP_ETag
*/
$etag = 'W/' . md5( $etag_buffer );
// If-None-Match header sent by client.
if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] )
&& $_SERVER['HTTP_IF_NONE_MATCH'] === $etag )
{
header( "Cache-Control: private, must-revalidate" );
// Page cached: send 304 + empty content.
header( $_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified' );
exit;
}
else
{
// FJ fix headers already sent error when program outputs buffer.
if ( ! headers_sent() )
{
header( "Cache-Control: private, must-revalidate" );
// Send ETag + content (buffer).
header( 'ETag: ' . $etag );
}
echo $etag_buffer;
}
}
return true;
}
/**
* Null coalesce.
* Useful to prevent PHP undefined index / variable notices.
* Equivalent to:
* `isset( $var ) ? $var : null`
*
* @example $extra['SELECT'] = issetVal( $extra['SELECT'], '' );
*
* @since 5.0
*
* @todo Migrate to PHP7 ?? operator.
* @link https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op
*
* @param mixed &$var Variable.
* @param mixed $default Default value if undefined. Defaults to null.
* @return mixed Variable or default.
*/
function issetVal( &$var, $default = null )
{
return ( isset( $var ) ) ? $var : $default;
}