-
Notifications
You must be signed in to change notification settings - Fork 206
/
misc.inc.php
1339 lines (1204 loc) · 41.9 KB
/
misc.inc.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
/* All functions contained herein will be general use functions */
$result=$dbh->prepare("SHOW TABLES;");
$result->execute();
if($result->rowCount()==0) {
header("Location: install.php" );
}
/* Generic html sanitization routine */
if(!function_exists("sanitize")){
function sanitize($string,$stripall=true){
// Convert null to empty string
if ( is_null($string) ) {
$string = "";
}
// Trim any leading or trailing whitespace
$clean=trim($string);
// Convert any special characters to their normal parts
$clean=html_entity_decode($clean,ENT_COMPAT,"UTF-8");
// By default strip all html
$allowedtags=($stripall)?'':'<a><b><i><img><u><br>';
// Strip out the shit we don't allow
$clean=strip_tags($clean, $allowedtags);
// If we decide to strip double quotes instead of encoding them uncomment the
// next line
// $clean=($stripall)?str_replace('"','',$clean):$clean;
// What is this gonna do ?
$clean=filter_var($clean, FILTER_SANITIZE_SPECIAL_CHARS);
// There shoudln't be anything left to escape but wtf do it anyway
$clean=addslashes($clean);
return $clean;
}
}
if (!function_exists('curl_file_create')) {
function curl_file_create($filename, $mimetype = '', $postname = '') {
return "@$filename;filename="
. ($postname ?: basename($filename))
. ($mimetype ? ";type=$mimetype" : '');
}
}
/*
Regex to make sure a valid URL is in the config before offering options for contact lookups
http://www.php.net/manual/en/function.preg-match.php#93824
Example Usage:
if(isValidURL("http://test.com"){//do something}
*/
function isValidURL($url){
$urlregex="((https?|ftp)\:\/\/)?"; // SCHEME
$urlregex.="([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass
$urlregex.="([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP
$urlregex.="(\:[0-9]{2,5})?"; // Port
$urlregex.="(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
$urlregex.="(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query
$urlregex.="(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor
// Testing out the php url validation, leaving the regex for now
// if(preg_match("/^$urlregex$/",$url)){return true;}
return filter_var($url, FILTER_VALIDATE_URL);
}
//Convert hex color codes to rgb values
function html2rgb($color){
if($color[0]=='#'){
$color=substr($color,1);
}
if(strlen($color)==6){
list($r,$g,$b)=array($color[0].$color[1],$color[2].$color[3],$color[4].$color[5]);
}elseif(strlen($color)==3){
list($r,$g,$b)=array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
}else{
return false;
}
$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
return array($r, $g, $b);
}
/*
Used to ensure a properly formatted url in use of instances header("Location")
Example usage:
header("Location: ".redirect());
exit;
- or -
header("Location: ".redirect('storageroom.php'));
exit;
- or -
$url=redirect("index.php?test=23")
header("Location: $url");
exit;
*/
function path(){
$path=explode("/",sanitize($_SERVER['REQUEST_URI']));
unset($path[(count($path)-1)]);
$path=implode("/",$path);
return $path;
}
function redirect($target = null) {
// No argument was passed. If a referrer was set, send them back to whence they came.
if(is_null($target)){
if(isset($_SERVER["HTTP_REFERER"])){
return $_SERVER["HTTP_REFERER"];
}else{
// No referrer was set so send them to the root application directory
$target=path();
}
}else{
//Try to ensure that a properly formatted uri has been passed in.
if(substr($target, 0, 4)!='http'){
//doesn't start with http or https check to see if it is a path
if(substr($target, 0, 1)!='/'){
//didn't start with a slash so it must be a filename
$target=path()."/".$target;
}else{
//started with a slash let's assume they know what they're doing
$target=path().$target;
}
}else{
//Why the heck did you send a full url here instead of just doing a header?
return $target;
}
}
// If we made it here we didn't return above so bring in the config values
$config=new Config();
// Write out the value of the InstallURL to a shorter variable and trim it of whitespace
// just in case some smart ass managed to get something weird in the value
$installURL=trim($config->ParameterArray['InstallURL']);
// Keep some smart ass admin from trying to use this to access weird things
$installURL=str_replace("..","",$installURL);
// Since we format our path above using a / trim any extras from the user supplied
// value or if they pull something cute and put ////
$installURL=rtrim($installURL,"/");
// Check if our $installURL value is blank
if($installURL!=""){
// since we need the full path for the reporting to be able to use installURL it causes
// issues for the redirect function. str_replace will remove the repeated string from
// the baseURL so that we don't end up doubling up to a path that doesn't exist
$installURL=str_replace(path(),"",$installURL);
// $installURL isn't blank so combine it with the target to get a valid redirect target
$url = $installURL.$target;
}else{
// $installURL is blank so let's try to guess what the server will be for the redirect
if(array_key_exists('HTTPS', $_SERVER) && $_SERVER["HTTPS"]=='on') {
$url = "https://".$_SERVER['SERVER_NAME'].$target;
} else {
$url = "http://".@$_SERVER['SERVER_NAME'].$target;
}
}
return $url;
}
// search haystack for needle and return an array of the key path,
// FALSE otherwise.
// if NeedleKey is given, return only for this key
// mixed ArraySearchRecursive(mixed Needle,array Haystack[,NeedleKey[,bool Strict[,array Path]]])
if(!function_exists("ArraySearchRecursive")){
function ArraySearchRecursive($Needle,$Haystack,$NeedleKey="",$Strict=false,$Path=array()) {
if(is_object($Haystack)){
$Haystack=(array) $Haystack;
}elseif(!is_array($Haystack)){
return false;
}
foreach($Haystack as $Key => $Val) {
if((is_array($Val)||is_object($Val))&&$SubPath=ArraySearchRecursive($Needle,$Val,$NeedleKey,$Strict,$Path)) {
$Path=array_merge($Path,Array($Key),$SubPath);
return $Path;
}elseif((!$Strict&&$Val==$Needle&&$Key==(strlen($NeedleKey)>0?$NeedleKey:$Key))||($Strict&&$Val===$Needle&&$Key==(strlen($NeedleKey)>0?$NeedleKey:$Key))) {
$Path[]=$Key;
return $Path;
}
}
return false;
}
}
// Search an array of objects for a specific value on given index parameter
// Returns true if found, false if not
if(!function_exists("objArraySearch")){
function objArraySearch($array, $index, $value)
{
foreach($array as $arrayInf) {
if($arrayInf->{$index} == $value) {
return true;
}
}
return false;
}
}
/*
* Sort multidimentional array in natural order
*
* $array = sort2d ( $array, 'key to sort on')
*/
function sort2d ($array, $index){
//Create array of key and label to sort on.
foreach(array_keys($array) as $key){$temp[$key]=$array[$key][$index];}
//Case insensative natural sorting of temp array.
natcasesort($temp);
//Rebuild original array using the newly sorted order.
foreach(array_keys($temp) as $key){$sorted[$key]=$array[$key];}
return $sorted;
}
/*
* Sort multidimentional array in reverse order
*
* $array = sort2d ( $array, 'key to sort on')
*/
function arsort2d ($array, $index){
//Create array of key and label to sort on.
foreach(array_keys($array) as $key){$temp[$key]=$array[$key][$index];}
//Case insensative natural sorting of temp array.
arsort($temp);
//Rebuild original array using the newly sorted order.
foreach(array_keys($temp) as $key){$sorted[$key]=$array[$key];}
return $sorted;
}
/*
* Extend sql queries
*
*/
function extendsql($prop,$val,&$sql,$loose){
$method=($loose)?" LIKE \"%$val%\"":"=\"$val\"";
if($sql){
$sql.=" AND $prop$method";
}else{
$sql.="WHERE $prop$method";
}
}
function attribsql($attrib,$val,&$sql,$loose){
$method=($loose)?"AttributeID=$attrib AND Value LIKE \"%$val%\"":"AttributeID=$attrib AND Value=\"$val\"";
if($sql){
$sql .= " AND DeviceID IN (SELECT DeviceID FROM fac_DeviceCustomValue WHERE $method)";
} else {
$sql = "WHERE $method";
}
}
/*
* Define multibyte string functions in case they aren't present
*
*/
if(!extension_loaded('mbstring')){
define("MB_CASE_UPPER","MB_CASE_UPPER");
define("MB_CASE_LOWER", "MB_CASE_LOWER");
define("MB_CASE_TITLE", "MB_CASE_TITLE");
function mb_strtoupper($text,$encoding=null){
return strtoupper($text);
}
function mb_strtolower($text,$encoding=null){
return strtolower($text);
}
function mb_convert_case($string, $transform, $locale){
switch($transform){
case 'MB_CASE_UPPER':
$string=mb_strtoupper($string);
break;
case 'MB_CASE_LOWER':
$string=mb_strtolower($string);
break;
case 'MB_CASE_TITLE':
$string=ucwords(mb_strtolower($string));
break;
}
return $string;
}
}
/*
* Transform text to uppercase, lowercase, initial caps, or do nothing based on system config
* 2nd parameter is optional to override the system default
*
*/
function transform($string,$method=null){
$config=new Config();
$method=(is_null($method))?$config->ParameterArray['LabelCase']:$method;
switch ($method){
case 'upper':
$string=mb_convert_case($string, MB_CASE_UPPER, "UTF-8");
break;
case 'lower':
$string=mb_convert_case($string, MB_CASE_LOWER, "UTF-8");
break;
case 'initial':
$string=mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
break;
default:
// Don't you touch my string.
}
return $string;
}
/*
* Convert ticks given back as uptime from devices into a human readable format
*/
function ticksToTime($ticks) {
$seconds=floor($ticks/100);
$dtF=new DateTime("@0");
$dtT=new DateTime("@$seconds");
$a=$dtF->diff($dtT)->format('%a');
$h=$dtF->diff($dtT)->format('%h');
$i=$dtF->diff($dtT)->format('%i');
$s=$dtF->diff($dtT)->format('%s');
if($a>0){
return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}else if($h>0){
return $dtF->diff($dtT)->format('%h hours, %i minutes and %s seconds');
}else if($i>0){
return $dtF->diff($dtT)->format(' %i minutes and %s seconds');
}else{
return $dtF->diff($dtT)->format('%s seconds');
}
}
/*
* Language internationalization slated for v2.0
*
*/
if(isset($_COOKIE["lang"])){
$locale=$_COOKIE["lang"];
}else{
$locale=$config->ParameterArray['Locale'];
}
if(extension_loaded('gettext')){
if(isset($locale)){
if ( ! setlocale(LC_ALL,$locale) ) {
if ( ! setlocale( LC_ALL, $locale . ".UTF8" ) ) {
error_log( "Gettext error loading locale $locale." );
}
}
putenv("LC_ALL=$locale");
putenv("LANGUAGE=$locale");
bindtextdomain("openDCIM","./locale");
$codeset='utf8';
if(isset($codeset)){
bind_textdomain_codeset("openDCIM",$codeset);
}
textdomain("openDCIM");
}
}
function GetValidTranslations() {
$path='./locale';
$dir=scandir($path);
$lang=array();
global $locale;
foreach($dir as $i => $d){
// get list of directories in locale that aren't . or ..
if(is_dir($path.DIRECTORY_SEPARATOR.$d) && $d!=".." && $d!="."){
// check the list of valid directories above to see if there is an openDCIM translation file present
if(file_exists($path.DIRECTORY_SEPARATOR.$d.DIRECTORY_SEPARATOR."LC_MESSAGES".DIRECTORY_SEPARATOR."openDCIM.mo")){
// build array of valid language choices
$lang[$d]=$d;
}
}
}
return $lang;
}
function __($string){
if(extension_loaded('gettext')){
return _($string);
}else{
return $string;
}
}
/**
* Parse a string which contains numeric or alpha repetition specifications. It
* returns an array of tokens or a message of the parsing exception encountered
* with the location of the failing character.
*
* @param string $pat
* @return mixed
*/
function parseGeneratorString($pat)
{
$result = array();
$cstr = '';
$escape = false;
$patLen = strlen($pat);
for ($i=0; $i < $patLen; $i++) {
if ($escape) {
$cstr .= $pat[$i];
$escape = false;
continue;
}
if ($pat[$i] == '\\') {
$escape = true;
continue;
}
if ($pat[$i] == '(') {
// current string complete, start of a pattern
$result[] = array('String', array($cstr));
$cstr = '';
list($i, $patSpec, $msg) = parsePatternSpec($pat, $patLen, ++$i);
if (! $patSpec) {
echo 'Error: Parse pattern return error - \'', $msg, '\' ', $i, PHP_EOL;
return array(null, $msg, $i);
}
$result[] = $patSpec;
continue;
}
$cstr .= $pat[$i];
}
if ($cstr != '') {
$result[] = array('String', array($cstr));
}
return array($result, '', $i);
}
/**
* Parse the numeric or alpha pattern specification and return the specification
* token or a the message explaining the exception encountered and the position
* of the character where the exception was detected.
*
* @param type $pat
* @param type $patLen
* @param type $idx
* @return type
*/
function parsePatternSpec(&$pat, $patLen, $idx) {
$stopChars = array(';', ')');
$patSpec = array();
$msg = 'Wrong pattern specification';
$ValueStr = '';
$token = 'StartValue';
$startValue = null;
$increment = 1;
$patType = '';
for ($i = $idx; $i < $patLen; ++$i) {
if (ctype_digit($pat[$i])) {
list($ValueStr, $i, $msg) = getNumericString($pat, $i, $stopChars);
$patType = 'numeric';
} elseif (ctype_alpha($pat[$i])) {
list($ValueStr, $i, $msg) = getAlphaString($pat, $i, $stopChars);
$patType = 'alpha';
} else {
if ($token == 'StartValue') {
$msg = 'No start value detected.';
} elseif ($token == 'Increment') {
$msg = 'Missing increment value.';
} else {
$msg = 'Unexpected character \'' . $pat[$i] . '\'';
}
return array($i, null, $msg);
}
if (($token == 'StartValue') and ($i >= $patLen)) {
$msg = 'Incomplete pattern specification, missing stop character [\''
. implode('\',\'', $stopChars) . '\']';
return array($i, null, $msg);
}
if (($token == 'StartValue') and (in_array($pat[$i], $stopChars))) {
if ($ValueStr === '') {
$msg = 'Missing start value';
return array($i, null, $msg);
}
if ($patType == 'numeric') {
$startValue = intval($ValueStr);
} else {
$startValue = $ValueStr;
}
$ValueStr = '';
if ($pat[$i] == ')') {
$token = 'right_parenthesis';
} elseif ($pat[$i] == ';') {
$token = 'Increment';
continue;
}
}
if (($token == 'Increment')) {
if ($patType == 'numeric') {
$increment = intval($ValueStr);
} else {
$msg = 'Increment must be a number, wrong value \'' . $ValueStr . '\'';
return array($i, null, $msg);
}
}
if ($pat[$i] == ')') {
$patSpec = array('Pattern', array($patType, $startValue, $increment));
break;
}
$msg = 'Unexpected character \'' . $pat[$i] . '\' for token \'' . $token . '\'.';
return array($i, null, $msg);
}
if ((! $patSpec) and ($token == 'Increment')) {
$msg = 'Incomplete increment specification';
return array($i, null, $msg);
}
return array($i, $patSpec, $msg);
}
/**
* Parse a numeric string.
*
* @param string $pat
* @param int $idx
* @param array $stopChars
* @return mixed
*/
function getNumericString(&$pat, $idx, &$stopChars) {
$strValue = '';
for ($i=$idx; $i < strlen($pat); $i++) {
$char = $pat[$i];
if (in_array($char, $stopChars)) {
return array(intval($strValue), $i, 'NumericValue');
}
if (ctype_digit($char)) {
$strValue .= $char;
} else {
$msg = 'Non-numeric character encountered \'' . $char . '\' ';
return array(null, $i, $msg);
}
}
$msg = 'Stop character not encountered [\'' . implode('\',\'', $stopChars) . '\'].';
return array(null, $i, $msg);
}
/**
* Parse an alpha string.
*
* @param string $pat
* @param int $idx
* @param array $stopChars
* @return mixed
*/
function getAlphaString(&$pat, $idx, &$stopChars) {
$strValue = '';
$patType = 'alpha';
$escaped = false;
for ($i=$idx; $i < strlen($pat); $i++) {
$char = $pat[$i];
if ($char == '\\') {
$escaped= true;
continue;
}
if ($escaped) {
$strValue .= $char;
$escaped = false;
continue;
}
if (in_array($char, $stopChars)) {
return array($strValue, $i, 'AlphaValue');
}
if (ctype_alpha($char)) {
$strValue .= $char;
} else {
$msg = 'Non-numeric character encountered \'' . $char . '\' ';
return array(null, $i, $msg);
}
}
$msg = 'Stop character not encountered \'' . implode(',', $stopChars) . '\'.';
return array(null, $i, $msg);
}
// Code provided for num2alpha and alpha2num in
// http://stackoverflow.com/questions/5554369/php-how-to-output-list-like-this-aa-ab-ac-all-the-way-to-zzzy-zzzz-zzzza
//function num2alpha($n, $shift=0) {
// for ($r = ''; $n >= 0; $n = intval($n / 26) - 1)
// $r = chr($n % 26 + 0x41 + $shift) . $r;
// return $r;
//}
/**
* Return the alpha represenation of the integer based on Excel offset.
*
* @param int $n
* @param int $offset
* @return string
*/
function num2alpha($n, $offset = 0x40) {
for ($r = ''; $n >= 0; $n = intval($n / 26) - 1) {
$r = chr($n % 26 + ($offset + 1)) . $r;
}
return $r;
}
/**
* Return the numeric representation the alpha string based on Excel offset.
* @param string $a
* @param int $offset
* @return int
*/
function alpha2num($a, $offset = 0x40)
{
$base = 26;
$l = strlen($a);
$n = 0;
for ($i = 0; $i < $l; $i++) {
$n = $n*$base + ord($a[$i]) - $offset;
}
return $n-1;
}
/**
* Take the generator string specification produced by parseGeneratorString and
* return a list of strings where the patterns are instantiated.
*
* @param array $patSpecs
* @param int $count
* @return array
*/
function generatePatterns($patSpecs, $count) {
$patternList = array();
for ($i=0; $i < $count; $i++) {
$str = '';
foreach ($patSpecs as $pat) {
if ($pat) {
if ($pat[0] == 'String') {
$str .= $pat[1][0];
} elseif ($pat[0] == 'Pattern') {
if ($pat[1][0] == 'numeric') {
$str .= (integer)($pat[1][1] + $i*$pat[1][2]);
} elseif ($pat[1][0] == 'alpha') {
$charIntVal = ord($pat[1][1]);
if (($charIntVal >= 65) and ($charIntVal <= 90)) {
$offset = 0x40;
$charIntVal = alpha2num($pat[1][1]);
} else {
$offset = 0x60;
$charIntVal = alpha2num($pat[1][1], $offset);
}
$str .= num2alpha(($charIntVal + $i*$pat[1][2]), $offset);
}
}
}
}
$patternList[] = $str;
}
return $patternList;
}
// Deal with pesky international number formats that mysql doesn't like
function float_sqlsafe($number){
$locale=localeconv();
if($locale['thousands_sep']=='.'){
$number=str_replace('.','',$number);
}
if($locale['decimal_point']==','){
$number=str_replace(',','.',$number);
}
return $number;
}
function locale_number( $number, $decimals=2 ) {
$locale = localeconv();
return number_format($number,$decimals,
$locale['decimal_point'],
$locale['thousands_sep']);
}
// This will build an array that can be json encoded to represent the makeup of
// the installations containers, zones, rows, etc. It didn't seem appropriate
// to be on any single class
if(!function_exists("buildNavTreeArray")){
function buildNavTreeArray(){
$con=new Container();
$cabs=Cabinet::ListCabinets();
$menu=array();
if(!function_exists("processcontainer")){
function processcontainer($container,$cabs){
$menu=array($container);
foreach($container->GetChildren() as $child){
if(get_class($child)=='Container'){
$menu[]=processcontainer($child,$cabs);
}elseif(get_class($child)=='DataCenter'){
$menu[]=processdatacenter($child,$cabs);
}
}
return $menu;
}
}
if(!function_exists("processdatacenter")){
function processdatacenter($dc,$cabs){
$menu=array($dc);
foreach($dc->GetChildren() as $child){
if(get_class($child)=='Zone'){
$menu[]=processzone($child,$cabs);
}elseif(get_class($child)=='CabRow'){
$menu[]=processcabrow($child,$cabs);
}else{
$menu[]=processcab($child,$cabs);
}
}
return $menu;
}
}
if(!function_exists("processzone")){
function processzone($zone,$cabs){
$menu=array($zone);
foreach($zone->GetChildren() as $child){
if(get_class($child)=='CabRow'){
$menu[]=processcabrow($child,$cabs);
}else{
$menu[]=processcab($child,$cabs);
}
}
return $menu;
}
}
if(!function_exists("processcabrow")){
function processcabrow($row,$cabs){
$menu=array($row);
foreach($cabs as $cab){
if($cab->CabRowID==$row->CabRowID){
$menu[]=processcab($cab,$cabs);
}
}
return $menu;
}
}
if(!function_exists("processcab")){
function processcab($cab,$cabs){
return $cab;
}
}
foreach($con->GetChildren() as $child){
if(get_class($child)=='Container'){
$menu[]=processcontainer($child,$cabs);
}elseif(get_class($child)=='DataCenter'){
$menu[]=processdatacenter($child,$cabs);
}
}
return $menu;
}
}
// This will format the array above into the format needed for the side bar navigation
// menu.
if(!function_exists("buildNavTreeHTML")) {
function buildNavTreeHTML(){
global $dbh;
$st = $dbh->prepare( "select * from fac_DataCache where ItemType='NavMenu'" );
$st->execute();
if ( $row = $st->fetch() ) {
return $row["Value"];
} else {
// Oops, there's no tree because this is the first time it's being run.
updateNavTreeHTML();
// Don't blindly risk a stuck forever loop (blank database) and only try to go one level deep
if ( $tree = buildNavTreeHTML() ) {
return $tree;
} else {
// Missing or blank database! We might be in the installer phase, so just chill.
return false;
}
}
}
}
if(!function_exists("updateNavTreeHTML")){
function updateNavTreeHTML($menu=null){
global $dbh;
$tl=1; //tree level
$menu=(is_null($menu))?buildNavTreeArray():$menu;
if(!function_exists("buildnavmenu")){
function buildnavmenu($ma,&$tl){
$menuCode = "";
foreach($ma as $i => $level){
if(is_object($level)){
if(isset($level->Name)){
$name=$level->Name;
}elseif(isset($level->Location)){
$name=$level->Location;
}else{
$name=$level->Description;
}
if($i==0){--$tl;}
foreach($level as $prop => $value){
if(preg_match("/id/i", $prop)){
$ObjectID=$value;
break;
}
}
$class=get_class($level);
$cabclose='';
if($class=="Container"){
$href="container_stats.php?container=";
$id="c$ObjectID";
}elseif($class=="Cabinet"){
$href="cabnavigator.php?cabinetid=";
$id="cab$ObjectID";
$cabclose="</li>";
}elseif($class=="Zone"){
$href="zone_stats.php?zone=";
$id="zone$ObjectID";
}elseif($class=="DataCenter"){
$href="dc_stats.php?dc=";
$id="dc$ObjectID";
}elseif($class=="CabRow"){
$href="rowview.php?row=";
$id="cr$ObjectID";
}
$menuCode .= str_repeat("\t",$tl).'<li class="liClosed" id="'.$id.'"><a class="'.$class.'" href="'.$href.$ObjectID."\">$name</a>$cabclose\n";
if($i==0){
++$tl;
$menuCode .= str_repeat("\t",$tl)."<ul>\n";
}
}else{
$tl++;
$menuCode .= buildnavmenu($level,$tl);
if(get_class($level[0])=="DataCenter"){
$menuCode .= str_repeat("\t",$tl).'<li id="dc-'.$level[0]->DataCenterID.'"><a href="storageroom.php?dc='.$level[0]->DataCenterID.'">Storage Room</a></li>'."\n";
}
$menuCode .= str_repeat("\t",$tl)."</ul>\n";
$tl--;
$menuCode .= str_repeat("\t",$tl)."</li>\n";
}
}
return $menuCode;
}
}
$menuCode = '<ul class="mktree" id="datacenters">'."\n";
$menuCode .= buildnavmenu($menu,$tl);
$menuCode .= '<li id="dc-1"><a href="storageroom.php">'.__("General Storage Room")."</a></li>\n</ul>";
$st = $dbh->prepare( "insert into fac_DataCache set ItemType='NavMenu', Value=:Value on duplicate key update Value=:Value" );
$st->execute( array( ":Value"=>$menuCode ));
}
}
/*
Check if we are doing a new install or an upgrade has been applied.
If found then force the user into only running that function.
To bypass the installer check from running, simply add
$devMode = true;
to the db.inc.php file.
*/
/*
If we are using Saml authentication, go ahead and figure out who
we are. It may be needed for the installation.
*/
if( AUTHENTICATION=="Saml" && !isset($_SESSION['userid']) && php_sapi_name()!="cli" && !isset($loginPage))
{
// Check for query parameters and add them if they exist, but don't leave a dangling question mark
if ( sanitize($_SERVER["QUERY_STRING"]) != "" ) {
$savedurl = $_SERVER['SCRIPT_NAME'] . "?" . sanitize($_SERVER['QUERY_STRING']);
} else {
$savedurl = $_SERVER["SCRIPT_NAME"];
}
$options = array ( 'expires' => time()+60, 'SameSite' => 'Strict' );
setcookie( 'targeturl', $savedurl, $options );
header("Location: ".redirect('saml/login.php'));
exit;
}
if(!(isset($devMode)&&$devMode)) {
$sql = "select Value from fac_Config where Parameter='Version'";
if ( $r = $dbh->query( $sql ))
$version = $r->fetchColumn();
else
$version = "";
if ( VERSION != $version ) {
if(file_exists("install.php") && basename($_SERVER['SCRIPT_NAME'])!="install.php" ){
error_log("Newer version codebase than database, redirecting to installation script.");
// new installs need to run the install first.
header("Location: ".redirect('install.php'));
exit;
}
}
}
/*
If we are using OIDC authentication, go ahead and figure out who
we are. It may be needed for the installation.
*/
if( AUTHENTICATION=="OIDC" && !isset($_SESSION['userid']) && php_sapi_name()!="cli" && !isset($loginPage)){
header("Location: ".redirect('login_oidc.php'));
exit;
}
// Just to keep things from getting extremely wonky and complicated, even though this COULD be in one giant
// if/then/else stanza, I'm breaking it into two
if( AUTHENTICATION=="LDAP" && $config->ParameterArray["LDAPSessionExpiration"] > 0 && isset($_SESSION['userid']) && ((time() - $_SESSION['LoginTime']) > $config->ParameterArray['LDAPSessionExpiration'])) {
session_unset();
session_destroy();
session_start();
}
if( AUTHENTICATION=="LDAP" && !isset($_SESSION['userid']) && php_sapi_name()!="cli" && !isset($loginPage)) {
$savedurl = $_SERVER['SCRIPT_NAME'] . "?" . $_SERVER['QUERY_STRING'];
$options = array ( 'expires' => time()+60, 'SameSite' => 'Strict' );
setcookie( 'targeturl', $savedurl, $options );
header("Location: ".redirect('login_ldap.php'));
exit;
}
// And just because you're logged in, it doesn't mean that we have your People record...
if(!People::Current()){
if(AUTHENTICATION=="OIDC" && !isset($loginPage) ) {
header("Location: ".redirect('login_oidc.php'));
exit;
} elseif ( AUTHENTICATION=="Saml" && !isset($loginPage) ){
header("Location: ".redirect('saml/login.php'));
exit;
} elseif ( AUTHENTICATION=="LDAP" && !isset($loginPage) ) {
header("Location: ".redirect('login_ldap.php'));
exit;
} elseif(AUTHENTICATION=="Apache"){
print "<h1>You must have some form of Authentication enabled to use openDCIM.</h1>";
exit;
}
}
/* This is used on every page so we might as well just init it once */
$person=People::Current();
// If we're in the process of logging in, just to get us through this, create an instance with all rights revoked
if ( isset( $loginPage ) ) {
$person = new People();
$person->revokeAll();
}
if(($person->Disabled || ($person->PersonID==0 && $person->UserID!="cli_admin")) && $config->ParameterArray["RequireDefinedUser"]=="enabled" && !isset($loginPage)){
header("Location: ".redirect('unauthorized.php'));
exit;
}
/*
* This is an attempt to be sane about the rights management and the menu.
* The menu will be built off a master array that is a merger of what options
* the user has available.
*
* Array structure:
* []->Top Level Menu Item
* [top level menu item]->Array(repeat previous structure)
*
*/
$menu=$rmenu=$rrmenu=$camenu=$wamenu=$samenu=$lmenu=array();
$rmenu[]='<a href="reports.php"><span>'.__("Reports").'</span></a>';
if($config->ParameterArray["WorkOrderBuilder"] == 'enabled' && isset($_COOKIE['workOrder']) && $_COOKIE['workOrder']!='[0]'){
array_unshift($rmenu , '<a href="workorder.php"><span>'.__("Work Order").'</span></a>');
}
if ( $config->ParameterArray["RackRequests"] == "enabled" && $person->RackRequest ) {
$rrmenu[]='<a href="rackrequest.php"><span>'.__("Rack Request Form").'</span></a>';
}
if ( $person->ContactAdmin ) {
$camenu[__("User Administration")][]='<a href="usermgr.php"><span>'.__("User Administration").'</span></a>';
$camenu[__("User Administration")][]='<a href="departments.php"><span>'.__("Dept. Administration").'</span></a>';
$camenu[__("Issue Escalation")][]='<a href="timeperiods.php"><span>'.__("Time Periods").'</span></a>';
$camenu[__("Issue Escalation")][]='<a href="escalations.php"><span>'.__("Escalation Rules").'</span></a>';
$camenu[]='<a href="project_mgr.php"><span>'.__("Project Catalog").'</span></a>';
}
if ( $person->WriteAccess ) {
$wamenu[__("Template Management")][]='<a href="device_templates.php"><span>'.__("Edit Device Templates").'</span></a>';
$wamenu[__("Infrastructure Management")][]='<a href="cabinets.php"><span>'.__("Edit Cabinets").'</span></a>';
$wamenu[__("Template Management")][]='<a href="image_management.php#pictures"><span>'.__("Device Image Management").'</span></a>';