-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.php
8303 lines (7359 loc) · 277 KB
/
lib.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
/**
* lib.php
*
* Copyright (C) 2008-2023 Null Team
*
* This software is distributed under multiple licenses;
* see the COPYING file in the main directory for licensing
* information for this specific distribution.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.
*
* 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.
*/
?>
<?php
require_once("debug.php");
config_globals_from_session();
global $module, $method, $action, $vm_base, $limit, $db_true, $db_false, $limit, $page, $system_standard_timezone;
/**
* Include the classes for database objects.
* @param $path String. Path to the files to be included.
*/
function include_classes($path='')
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
$classes_dirs = array("classes/", "ansql/default_classes");
for ($i=0; $i<count($classes_dirs); $i++) {
if (!is_dir($path.$classes_dirs[$i]))
continue;
$handle = opendir($path.$classes_dirs[$i]);
while (false !== ($file = readdir($handle))) {
if (substr($file,-4) != '.php')
continue;
else {
if ($classes_dirs[$i] == "ansql/default_classes") {
$file_name = substr($file,0,strlen($file)-4);
global ${"custom_$file_name"};
if (isset(${"custom_$file_name"}) && ${"custom_$file_name"})
continue;
}
require_once($path.$classes_dirs[$i]."/$file");
}
}
}
}
/**
* Implementation of stripos function if it does not exist.
* Find the position of the first occurrence of a case-insensitive substring in a string.
*/
if (!function_exists("stripos")) {
// PHP 4 does not define stripos
function stripos($haystack,$needle,$offset=0)
{
return strpos(strtolower($haystack),strtolower($needle),$offset);
}
}
/**
* Implementation of array_column function if it does not exist.
* Return the values from a single column in the input array
*/
if (!function_exists("array_column")) {
// PHP <5.5 does not define array_column
function array_column($input, $column_key, $index_key=null) {
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
$err_mess="";
if ($index_key!==null || $column_key===null)
$err_mess = "This functionality was not yet implemented for your php version. The function does not support reindexing arrays or returning complete objects or arrays yet.";
else if (!is_array($input))
$err_mess = "Parameter input must be an array.";
else if (!ctype_digit(strval($column_key)) && !is_string($column_key))
$err_mess = "Parameter column_key must be either a string or an integer. ";
if ($err_mess!="") {
errormess("array_column(): Please contact an administrator." . $err_mess,"no");
Debug::trigger_report ("critical", $err_mess);
return array();
}
$result = array();
foreach ($input as $elem) {
if (is_object($elem)) {
$err_mess = "This functionality was not yet implemented for your php version. The parameter input array must not contain elements of type object.";
errormess("array_column(): Please contact an administrator." . $err_mess, "no");
Debug::trigger_report ("critical", $err_mess);
return array();
}
if (!isset($elem[$column_key]) || !is_array($elem))
continue;
$result[] = $elem[$column_key];
}
return $result;
}
}
escape_page_params();
if (!isset($system_standard_timezone))
$system_standard_timezone = "GMT".substr(date("O"),0,3);
/**
* Establish the name of the default function to be called using some predefind criteria.
* @param $module String. The Module defined for each project.
* @param $method String. The Method associated to the Module.
* @param $action String. The action associated with a method.
* @param $call String. The name of the default function to be called.
* @return string of type $call
*/
function get_default_function()
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $module, $method, $action;
Debug::debug_message(__FUNCTION__, "module:".$module." | method:".$method." | action:".$action);
if (!$method)
$method = $module;
if (substr($method,0,4) == "add_")
$method = str_replace("add_","edit_",$method);
if ($action)
$call = $method.'_'.$action;
else
$call = $method;
return $call;
}
/**
* Test a given Path.
* @param $path String.
* @return 403 Forbidden page only if $path matches a regular expression
*/
function testpath($path)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
if (preg_match("/[^A-Za-z0-9_]/",$path, $matches)) {
// Client tried to hack around the path naming rules - ALERT!
Debug::trigger_report('operational', "Preg_match function match path: ".print_r($matches)." session: ".print_r($_SESSION,true));
forbidden();
}
}
/**
* Send a raw HTTP header with 403 Forbidden. Clears the session data.
* Displays a page with Forbidden message.
* Terminates the current script.
*/
function forbidden()
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
header("403 Forbidden");
session_unset();
print '<html><body style="color:red">Forbidden</body></html>';
exit();
}
/**
* Builds the HTML <form> tag with all possible attributes:
* @param $action String. The action of the FORM
* @param $method String. Allowed values: post|get. Defaults to 'post'.
* @param $allow_upload Bool. If true allow the upload of files. Defaults to false.
* @param $form_name String. Fill the attribute name of the FORM.
* Defaults to global variable $module or 'current_form' if $module is not set or null
* @param $class String. Fill the attribute class. No default value set.
*/
function start_form($action = NULL, $method = "post", $allow_upload = false, $form_name = NULL, $class = NULL, $javascript = NULL)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $module;
if (!$method)
$method = "post";
$form = (!$module) ? "current_form" : $module;
if (!$form_name)
$form_name = $form;
if (!$action) {
if (isset($_SESSION["main"]))
$action = $_SESSION["main"];
else
$action = "index.php";
}
?><form action="<?php print $action;?>" name="<?php print $form_name;?>" id="<?php print $form_name;?>" <?php if ($class) print "class=\"$class\"";?> method="<?php print $method;?>" <?php if($allow_upload) print 'enctype="multipart/form-data"';?><?php if ($javascript) print $javascript;?>><?php
}
/**
* Ends a HTML FORM tag.
*/
function end_form()
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
?></form><?php
}
/**
* Displays a given text as a note.
* @param $note String Contains the note text.
* @param $encode Bool True to use htmlentities on message before displaying, false for not using htmlentities.
*/
function note($note, $encode=true)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
$note = ($encode) ? htmlentities($note) : $note;
print 'Note! '.$note.'<br/>';
}
/**
* Displays an error note with a predefined css
* @param $text String The message to be displayed.
* @param $encode Bool True to use htmlentities on message before displaying, false for not using htmlentities.
*/
function errornote($text, $encode=true)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
$text = ($encode) ? htmlentities($text) : $text;
print "<br/><font color=\"red\" style=\"font-weight:bold;\" > Error!</font> <font style=\"font-weight:bold;\">".$text."</font><br/>";
}
/**
* Displays a given text.
* @param $text String The text to be displayed
* @param $path String The path to use in link
* @param $return_text the link to return to requested Path
* @param $encode Bool True to use htmlentities on message before displaying, false for not using htmlentities.
*/
function message($text, $path=NULL, $return_text="Go back to application", $encode=true)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $module,$method;
$text = ($encode) ? htmlentities($text) : $text;
print '<div class="notice">'."\n";
print $text."\n";
if ($path == 'no') {
print '</div>';
return;
}
link_to_main_page($path, $return_text);
print '</div>';
}
/**
* Displays a given text.
* @param $text String The text to be displayed
* @param $path String The path to use in link. No link provided if value is "no"
* @param $return_text The link to return to requested Path
* @param $encode Bool True to use htmlentities on message before displaying, false for not using htmlentities.
* @param $details String Extra details to be displayed after click on "More details" link
* @param $more_details_id String If provided this will be the id of the html container holding the More Details text
* @param $extra_css String. Css class. If provided this css class will be aplied on the entire warning box
* @param $print_text Bool. If true prints the warning directly. Otherwise returns the html code for the warning message.
* @return string if $print_text is false
*/
function warning_mess($text, $path=NULL, $return_text="Go back to application", $encode=true, $details="", $more_details_id="", $extra_css="", $print_text = true)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $module,$method, $more_details_counter;
$text = ($encode) ? htmlentities($text) : $text;
$message = '<div class="warning '. $extra_css.'">'."\n";
$message .= '<div class="hold_err_mess">';
$message .= $text."\n";
if ($details) {
if (!$more_details_id) {
$more_details_counter = ($more_details_counter===null) ? 0 : $more_details_counter+1;
$more_details_id = "error_details".$more_details_counter;
}
$message .= ' <a class="llink" onclick="show_hide(\''.$more_details_id.'\');">More details<br/></a>';
$message .= '<div id="'.$more_details_id.'" class="error_details" style="display: none;">';
$message .= $details;
$message .= '</div>';
}
$message .= '</div>';
if ($path == 'no') {
$message .= '</div>';
if (!$print_text)
return $message;
print $message;
return;
}
$message .= " " . link_to_main_page($path, $return_text, false);
$message .= '</div>';
if (!$print_text)
return $message;
print $message;
}
/**
* Wrapper function for warning_mess()
* @param $text The text to be displayed
* @param $details String Extra details to be displayed after click on "More details" link
* @param $extra_css String. Css class. If provided this css class will be aplied on the entire warning box
* @param $print_text Bool. If true prints the warning directly. Otherwise returns the html code for the warning message.
* @param $path String The path to use in link. No link provided if value is "no"
* @param $return_text the link to return to requested Path
* @param $encode Bool True to use htmlentities on message before displaying, false for not using htmlentities.
* @param $more_details_id String If provided this will be the id of the html container holding the More Details text
* @return string if $print_text is false
*/
function warning_mess_with_details($text, $details, $extra_css="", $print_text = true, $path="no", $return_text="Go back to application", $encode=true, $more_details_id="")
{
return warning_mess($text, $path, $return_text, $encode, $details, $more_details_id, $extra_css, $print_text);
}
/**
* Displays a given text with a specific css for errors
* @param $text String The text to be displayed
* @param $path String The path to use in link. No link provided if value is "no"
* @param $return_text the link to return to requested Path
* @param $encode Bool True to use htmlentities on message before displaying, false for not using htmlentities.
* @param $details String Extra details to be displayed after click on "More details" link
* @param $more_details_id String If provided this will be the id of the html container holding the More Details text
* @param $extra_css String. Css class. If provided this css class will be aplied on the entire error box
* @param $print_text Bool. If true prints the error directly. Otherwise returns the html code for the error message.
* @return string if $print_text is false
*/
function errormess($text, $path=NULL, $return_text="Go back to application", $encode=true, $details="", $more_details_id="", $extra_css="", $print_text = true)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $module, $more_details_counter;
$text = ($encode) ? htmlentities($text) : $text;
$message = '<div class="notice error '. $extra_css.'">'."\n";
$message .= '<div class="hold_err_mess">';
$message .= "<font class=\"error\"> Error!</font>"."\n";
$message .= "<font style=\"font-weight:bold;\">". $text ."</font>"."\n";
if ($details) {
if (!$more_details_id) {
$more_details_counter = ($more_details_counter===null) ? 0 : $more_details_counter+1;
$more_details_id = "error_details".$more_details_counter;
}
$message .= ' <a class="llink" onclick="show_hide(\''.$more_details_id.'\');">More details<br/></a>';
$message .= '<div id="'.$more_details_id.'" class="error_details" style="display: none;">';
$message .= $details;
$message .= '</div>';
}
$message .= '</div>';
if ($path == 'no') {
$message .= '</div>';
if (!$print_text)
return $message;
print $message;
return;
}
$message .= " " . link_to_main_page($path, $return_text, false);
$message .= '</div>';
if (!$print_text)
return $message;
print $message;
}
/**
* Wrapper function for errormess()
* @param type $text String The text to be displayed
* @param type $details String Extra details to be displayed after click on "More details" link
* @param type $extra_css String. Css class. If provided this css class will be aplied on the entire error box
* @param type $print_text Bool. If true prints the error directly. Otherwise returns the html code for the error message.
* @param type $path String The path to use in link. No link provided if value is "no"
* @param type $return_text the link to return to requested Path
* @param type $encode Bool True to use htmlentities on message before displaying, false for not using htmlentities.
* @param $details String Extra details to be displayed after click on "More details" link
* @param $more_details_id String If provided this will be the id of the html container holding the More Details text
* @param $extra_css String. Css class. If provided this css class will be aplied on the entire error box
* @param $print_text Bool. If true prints the error directly. Otherwise returns the html code for the error message.
* @return String if $print_text is false
*/
function error_with_details($text, $details, $extra_css="", $print_text = true, $path="no", $return_text="Go back to application", $encode=true, $more_details_id="")
{
return errormess($text, $path, $return_text, $encode, $details, $more_details_id, $extra_css, $print_text);
}
/**
* Prints a message as a notice or an error type message and calls a function
* @param $message String The message to be displayed.
* @param $next_cb Callable/String. Setting it to 'no' stops the performing of the callback.
* @param $no_error Boolean If is true a message id displayed else an error type message is displayed. Defaults to true.
* @param $encode Bool True to use htmlentities on message before displaying, false for not using htmlentities.
* @param $details String Extra details to be displayed after click on "More details" link
* @param $more_details_id String If provided this will be the id of the html container holding the More Details text
* @param $extra_css String. Css class. If provided this css class will be aplied on the entire notice box
* @param $print_text Bool. If true prints the notice directly. Otherwise returns the html with the message.
* @return string if $print_text is false
*/
function notice($message, $next_cb=NULL, $no_error=true, $encode=true, $details="", $more_details_id="", $extra_css="", $print_text=true)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $module, $more_details_counter;
if (!$next_cb)
$next_cb = $module;
$message = ($encode) ? htmlentities($message) : $message;
$notice = "";
if ($no_error)
$notice = '<div class="notice '.$extra_css.'">'.$message;
else
$notice = '<div class="notice error '.$extra_css.'"><font class="error">Error! </font>'.$message;
if ($details) {
if (!$more_details_id) {
$more_details_counter = ($more_details_counter===null) ? 0 : $more_details_counter+1;
$more_details_id = "error_details".$more_details_counter;
}
$notice .= ' <a class="llink" onclick="show_hide(\''.$more_details_id.'\');">More details<br/></a>';
$notice .= '<div id="'.$more_details_id.'" class="error_details" style="display: none;">';
$notice .= $details;
$notice .= '</div>';
}
$notice .= "</div>";
if (!$print_text)
return $notice;
print $notice;
if ($next_cb != "no" && is_callable($next_cb))
call_user_func($next_cb);
}
/**
* Displays a text with a bold font style set.
* @param $text String The message to be displayed.
* @param $encode Bool True to use htmlentities on message before displaying, false for not using htmlentities.
*/
function plainmessage($text, $encode=true)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
$text = ($encode) ? htmlentities($text) : $text;
print "<br/><font style=\"font-weight:bold;\">".$text."</font><br/><br/>";
}
/**
* Displays a message or an error message depending on the data given in array
* @param $res Array Contains on key 0: true/false
* and on key 1: the message to be displayed
*/
function notify($res)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $path;
if ($res[0])
message($res[1],$path);
else
errormess($res[1],$path);
}
/**
* Displays a specific build link for application
*/
function link_to_main_page($path, $return_text, $print_text = true)
{
global $module;
if (isset($_SESSION["main"]))
$link = $_SESSION["main"];
else
$link = "main.php";
$link .= "?module=".$module;
if ($path)
$link .= "&method=".$path;
if ($print_text)
print '<a class="information" href="'.$link.'">'.$return_text.'</a>';
else
return '<a class="information" href="'.$link.'">'.$return_text.'</a>';
}
/**
* Escape the HTTP Request variables
*/
function escape_page_params()
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
foreach ($_POST as $param=>$value) {
if (is_array($value)) {
foreach ($value as $name=>$val) {
// not necesary because implementation of html_quotes_escape was changed
//$val = htmlspecialchars_decode($val, ENT_COMPAT);
$_POST[$param][$name] = escape_page_param($val);
}
} else {
// not necesary because implementation of html_quotes_escape was changed
//$value = htmlspecialchars_decode($value, ENT_COMPAT);
$_POST[$param] = escape_page_param($value);
}
}
foreach ($_GET as $param=>$value) {
$_GET[$param] = escape_page_param($value);
}
foreach ($_REQUEST as $param=>$value) {
$_REQUEST[$param] = escape_page_param($value);
}
}
/**
* Convert all applicable characters to HTML entities for a value or an array of values
* @param $value String / Array
* @return the modified $value
*/
function escape_page_param($value)
{
global $htmlentities_onall;
global $use_urldecode;
Debug::func_start(__FUNCTION__,func_get_args(),"paranoid");
Debug::xdebug("paranoid","Global in ".__FUNCTION__.": htmlentities_onall=$htmlentities_onall,use_urldecode=$use_urldecode");
if (!isset($htmlentities_onall))
$htmlentities_onall = true;
if (!isset($use_urldecode))
$use_urldecode = false;
if ($htmlentities_onall) {
if (!is_array($value))
$value = htmlentities($value);
else {
foreach ($value as $index=>$val)
$value[$index] = htmlentities($val);
}
}
if ($use_urldecode) {
if (!is_array($value))
$value = urldecode($value);
else {
foreach ($value as $index=>$val)
$value[$index] = urldecode($val);
}
}
return $value;
}
/**
* Return the $_GET OR $_POST value of a given parameter
* @param $param String
* @return the value of the $param set in $_GET or $_POST
* or NULL if is not set or if is a specific sql abreviation used in queries
*/
function getparam($param,$escape = true)
{
Debug::func_start(__FUNCTION__,func_get_args(),"paranoid");
global $no_trim;
if (!isset($no_trim))
$no_trim = false;
$ret = NULL;
if (isset($_POST[$param]))
$ret = $_POST[$param];
elseif (isset($_GET[$param]))
$ret = $_GET[$param];
else
return NULL;
if (is_array($ret)) {
foreach($ret as $index => $value) {
if ($no_trim)
$ret[$index] = escape_sql_param($ret[$index]);
else
$ret[$index] = escape_sql_param(trim($ret[$index]));
}
return $ret;
}
if ($no_trim)
$ret = escape_sql_param($ret);
else
$ret = escape_sql_param(trim($ret));
return $ret;
}
/**
* Return NULL if the value of a given param is specific to SQL abreviations
* or the value of the param
*/
function escape_sql_param($ret)
{
if (substr($ret,0,6) == "__sql_")
$ret = NULL;
if ($ret == "__empty")
$ret = NULL;
if ($ret == "__non_empty" || $ret == "__not_empty")
$ret = NULL;
if (substr($ret,0,6) == "__LIKE")
$ret = NULL;
if (substr($ret,0,10) == "__NOT LIKE")
$ret = NULL;
return $ret;
}
/**
* Returns the new string with "_" where were spaces.
* @param $value String
*/
function killspaces($value)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
return str_replace(' ','_',$value);
}
/**
* Verifies if a string is numeric.
* @param $num String the number to be checked
* @param $very_big Bool if true verifies if every digit is numeric
* @return NULL if given string is not numeric.
*/
function Numerify($num, $very_big = false)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
if ($num == '0')
$num = '0';
if ($very_big) {
for($i=0; $i<strlen($num); $i++) {
if(!is_numeric($num[$i]))
return "NULL";
}
} else {
if (!is_numeric($num) && strlen($num))
$num = "NULL";
}
return $num;
}
/**
* Build a full date string from parts
* @return false on failure, true on empty
*/
function dateCheck($year,$month,$day,$hour,$end)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
if ("$year$month$day" == "") {
if ($hour == "")
return true;
if (($hour<0) || ($hour>23))
return false;
$hour = sprintf(" %02u:%02u:%02u",$hour,$end,$end);
return gmdate("Y-m-d") . $hour;
}
if (!($year && $month && $day))
return false;
if ($hour == "")
$hour = $end ? 23 : 0;
if (!(is_numeric($year) && is_numeric($month) && is_numeric($day) && is_numeric($hour)))
return false;
if (($year<2000) || ($month<1) || ($month>12) || ($day<1) || ($day>31) || ($hour<0) || ($hour>23))
return false;
return sprintf("%04u-%02u-%02u %02u:%02u:%02u",$year,$month,$day,$hour,$end,$end);
}
/**
* Builds link from the parameters from the current REQUEST
* @param $exclude_params Array. Parameters to be excluded from built link
* @param $additional_url_elements Array. Parameters required into the built link
* @return String. The link from the current $_REQUEST
*/
function build_link_request($exclude_params=array(), $additional_url_elements=array())
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $module, $method, $action;
$link = (isset($_SESSION["main"]) && strlen($_SESSION["main"])) ? $_SESSION["main"] : "main.php";
$link .= "?";
foreach ($_REQUEST as $param=>$value) {
if ($param == "page" ||
$param == "PHPSESSID" ||
($param == "action" && $action) ||
($param == "method" && $method) ||
($param == "module" && $module) ||
in_array($param,$exclude_params) ||
(!is_array($value) && !strlen($value))
)
continue;
if (substr($link,-1) != "?")
$link .= "&";
if (is_array($additional_url_elements) && count($additional_url_elements)) {
foreach ($additional_url_elements as $k=>$element_name)
if ($element_name == $param)
$link .= "$param=".urlencode($value);
} else {
if (!is_array($value))
$link .= "$param=".urlencode($value);
else
foreach ($value as $arr_val)
$link .= "$param"."[]=".$arr_val;
}
}
if (substr($link,-1) != "?")
$link .= "&";
if ($module)
$link .= "module=$module";
if ($method)
$link .= "&method=$method";
if ($action) {
$call = get_default_function();
if (function_exists($call))
$link .= "&action=$action";
}
return $link;
}
/**
* Displays number on page that are links which will
* make a reload of page with a new limit request
* @param $total Integer contains the number of items
* @param $nrs Array contains the number of items to be displayed
* This function was adapted to work also for the old version when $total didn't exist
* Old function argument: items_on_page($nrs = array(20,50,100), $additional_url_elements=null)
*/
function items_on_page($total = null, $nrs = array(20,50,100), $additional_url_elements = null)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $limit;
//Detect if old argument is used
//Below are the cases where the old argument is used
//1. First parameter is an array -> Ex: items_on_page(array(60,120,240));
//2. First parameter is null and second parameter is an array and first element from array is string -> Ex: items_on_page(null, array("performer","operation"));
if (is_array($total) || (is_null($total) && is_array($nrs) && count($nrs) && is_string($nrs[0]))) {
$additional_url_elements = $nrs;
$nrs = $total;
$total = null;
}
if (!$nrs)
$nrs = array(20,50,100);
//Force total to integer
$total = (is_numeric($total)) ? intval($total) : null;
//Display limit links only if $total is bigger than the first limit
if (is_int($total) && $total<=$nrs[0])
return;
if ($additional_url_elements)
$additional_url_elements = array_merge($additional_url_elements, array("total"));
$link = build_link_request(array("limit"), $additional_url_elements);
print "<div class=\"items_on_page\">";
for($i=0; $i<count($nrs); $i++)
{
$option = $link."&limit=".$nrs[$i];
if ($i>0)
print '|';
print ' <a class="pagelink';
if ($nrs[$i]==$limit)
print " selected_pagelink";
print '" href="'.$option.'">'.$nrs[$i].'</a> ';
}
print "</div>";
}
/**
* Builds and prints pagination links. Ex: 1 2 3 >| or |< 1 2 3 4 5. Takes into account the total number of objects and limit of items to display on page
* @global string $go_to_prev_page_link. Holds previous page link if being on exception case** / Empty if correct page is displayed
* (**exception case: if the only object from last page is deleted $page will be equal with the total number of objects therefore that page will still be displayed)
* Used in table() / tableOfObjects() to load content from previous page if it was detected that table would be displayed without content (not applied if being on first page and deleting the only object from table)
* @param $total Integer. Total number of entities
* @param $additional_url_elements Array. Additional elements to add in links beside default ones(module, method, page, total)
* Ex: array("status")
* @param $jump_to Boolean. Default false. If true, 'jump to' input is displayed.
* @param $div_css String. If value is given, <center> tag won't be used, and specified css class will be used instead of the default one 'pages'.
*/
function pages($total = NULL, $additional_url_elements=array(), $jump_to=false, $div_css = NULL)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $limit, $page, $module, $method, $action, $go_to_prev_page_link;
if (!$limit)
$limit = 20;
$link = isset($_SESSION["main"]) ? $_SESSION["main"] : "main.php";
$link .= "?";
$slink = $link;
$page = 0;
if (isset($_REQUEST["page"]))
$page = $_REQUEST["page"];
if (isset($_REQUEST["total"]))
$total = $_REQUEST["total"];
if (!is_array($additional_url_elements) || !count($additional_url_elements))
$additional_url_elements = array("total");
elseif (!in_array("total", $additional_url_elements))
$additional_url_elements[] = "total";
$link = build_link_request(array("limit"), $additional_url_elements);
if (!$total)
$total = 0;
// fix page value (if the only object from last page is deleted $page would be equal with the total number of objects therfore that page would still be displayed)
if ($page && $page == $total) {
// fix page value so previous(correct) page and correct content will be displayed
$page = $page - $limit;
//used in table() / tableOfObjects() functions to load the previous page if no content for page
$go_to_prev_page_link = $link."&page=".$page;
}
if ($total <= $limit)
return;
$pages = floor($total/$limit);
if ($div_css) {
print '<div class="'.$div_css.'">';
} else {
print '<center>';
print '<div class="pages">';
}
if ($page != 0) {
/* jump to first page */
print '<a class="pagelink" href="'.$link.'&page=0">|<</a> ';
/* jump back 5 pages */
$prev5 = $page - 5*$limit;
if ($prev5>0)
print '<a class="pagelink" href="'.$link.'&page='.$prev5.'"><<</a> ';
/* jump to previous page */
/*$prev_page = $page - $limit;
print '<a class="pagelink" href="'.$link.'&page='.$prev_page.'"><</a> ';*/
$diff = floor(($total - ($page + $limit * 2))/$limit) * $limit;
$sp = $page - $limit * 2;
if ($diff < 0)
$sp = $sp - abs($diff);
while($sp<0)
$sp += $limit;
while($sp<$page) {
$pg_nr = floor($sp/$limit) + 1;
print '<a class="pagelink" href="'.$link.'&page='.$sp.'">'.$pg_nr.'</a> ';
$sp += $limit;
}
}
$pg_nr = floor($page/$limit)+1;
print '<font class="pagelink selected_pagelink" href="#">'.$pg_nr.'</font> ';
if (($page+$limit) < $total) {
if($pg_nr >= 3)
$stop_at = $pg_nr + 2;
else
$stop_at = $pg_nr + 5 - (floor($page/$limit)+1);
$next_page = $page + $limit;
while($next_page < $total && $pg_nr < $stop_at) {
$pg_nr++;
print '<a class="pagelink" href="'.$link.'&page='.$next_page.'">'.$pg_nr.'</a> ';
$next_page += $limit;
}
/* jump to next page */
/*$next_page = $page + $limit;
if($next_page<$total)
print '<a class="pagelink" href="'.$link.'&page='.$next_page.'">></a> ';*/
$next5 = $page + $limit*5;
$last_page = floor($total/$limit) * $limit;
if ($limit==1)
$last_page = $total - 1;
elseif (floor(($total/$limit))==$total/$limit)
$last_page = floor(($total-1)/$limit) * $limit;
/* jump 5 pages */
if ($next5 < $last_page)
print '<a class="pagelink" href="'.$link.'&page='.$next5.'">>></a> ';
/* jump to last page */
print '<a class="pagelink" href="'.$link.'&page='.$last_page.'">>|</a> ';
}
if ($jump_to) {
$no_pages = ceil($total/$limit);
print '('.$no_pages.') <font class="jump_to_text">Jump to</font> <input class="jump_to_inp" type="text" name="jump_to" id="jump_to" onkeyup="if (event.keyCode==13) '."jump_to('".$link."', $limit, $no_pages);".' "/> '. "<input type=\"button\" name=\"go\" value=\"Go\" onclick=\"jump_to('".$link."', $limit, $no_pages);\" />";
}
print '</div>';
if (!$div_css)
print '</center>';
}
/**
* Create links used for navigation between pages (previous / next)
*/
function navbuttons($params=array(),$class = "llink")
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $module, $method, $page;
$step = '';
$link="main.php?module=$module&method=$method&";
foreach($params as $key => $value) {
if ($key=="page" || $key=="tot")
continue;
$link="$link$key=$value&";
if ($key == "step")
$step = $value;
}
$total = $params["tot"];
if (!$step || $step == '')
$step = 10;
?>
<center>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="navbuttons">
<?php
$vl = $page-$step;
if ($vl >= 0) { ?>
<font size="-1"><a class="<?php print $class;?>" href="<?php print ("$link"."page"."=$vl");?>">Previous</a> </font>
<?php
}
?>
</td>
<td class="navbuttons">
<font size="-3">
<?php
$r = $page/$step+1;
print ("$r");
?>
</font>
</td>
<td class="navbuttons">
<?php
$vl = $page+$step;
if ($vl < $total) { ?>
<font size="-1"><a class="<?php print $class;?>" href="<?php print ("$link"."page"."=$vl");?>">Next</a> </font><?php
} ?>
</td>
</tr>
</table>
</center>
<?php
}
/**
* Validates an email address
* @param $mail String contains the email address string
* $return true if valid email and false if string is not in email pattern
*/
function check_valid_mail($mail)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
$pattern = '/^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i';
return preg_match($pattern,$mail);
}
/**
* Prints hidden type inputs used in page: module, method, action and additional parameters if set
* @param $action String contains the name of action in the page
* @param $additional Array contains the parameters and their values to be set as input hidden
* @param $empty_page_param Bool. If true it will set 'method' and 'module' hidden fields to existing value if they don't appear in $additional. Defaults to false
*/
function addHidden($action=NULL, $additional = array(), $empty_page_params=false, $skip_params=array())
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $method,$module;
if (($method || $empty_page_params) && !isset($additional["method"]))