-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
4728 lines (4260 loc) · 144 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
* This file is part of the FreeSentral Project http://freesentral.com
*
* FreeSentral - is a Web Graphical User Interface for easy configuration of the Yate PBX software
* Copyright (C) 2008-2014 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.
*/
require_once("debug.php");
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);
}
}
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;
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.
* Displayes 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)
{
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
}
/**
* Ends a HTML FORM tag.
*/
function end_form()
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
?></form><?php
}
/**
* Displayes a given text as a note.
* @param $note String Contains the note text.
*/
function note($note)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
print 'Note!! '.$note.'<br/>';
}
/**
* Displayes an error note with a predefined css
*/
function errornote($text)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
print "<br/><font color=\"red\" style=\"font-weight:bold;\" > Error!!</font> <font style=\"font-weight:bold;\">$text</font><br/>";
}
/**
* Displayes 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
*/
function message($text, $path=NULL, $return_text="Go back to application")
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $module,$method;
print '<div class="notice">'."\n";
print "$text\n";
if ($path == 'no') {
print '</div>';
return;
}
link_to_main_page($path, $return_text);
print '</div>';
}
/**
* Displayes 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
* @param $return_text the link to return to requested Path
*/
function errormess($text, $path=NULL, $return_text="Go back to application")
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $module;
print '<div class="notice error">'."\n";
print "<font class=\"error\"> Error!!</font>"."\n";
print "<font style=\"font-weight:bold;\">$text</font>"."\n";
if ($path == 'no') {
print '</div>';
return;
}
link_to_main_page($path, $return_text);
print '</div>';
}
/**
* Displayes a specific build link for application
*/
function link_to_main_page($path, $return_text)
{
global $module;
if (isset($_SESSION["main"]))
$link = $_SESSION["main"];
else
$link = "main.php";
$link .= "?module=".$module;
if ($path)
$link .= "&method=".$path;
print '<a class="information" href="'.$link.'">'.$return_text.'</a>';
}
/**
* 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.
*/
function notice($message, $next_cb=NULL, $no_error = true)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $module;
if (!$next_cb)
$next_cb = $module;
if ($no_error)
print '<div class="notice">'.$message.'</div>';
else
print '<div class="notice error"><font class="error">Error!! </font>'.$message.'</div>';
if ($next_cb != "no")
call_user_func($next_cb);
}
/**
* Displayes a text with a bold font style set.
*/
function plainmessage($text)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
print "<br/><font style=\"font-weight:bold;\">$text</font><br/><br/>";
}
/**
* Displayes 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);
}
/**
* Escape the HTTP Request variables
*/
function escape_page_params()
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
foreach ($_POST as $param=>$value)
$_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)
{
Debug::func_start(__FUNCTION__,func_get_args(),"paranoid");
if (!is_array($value))
return htmlentities($value);
else {
foreach ($value as $index=>$val)
$value[$index] = htmlentities($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");
$ret = NULL;
if (isset($_POST[$param]))
$ret = $_POST[$param];
else if (isset($_GET[$param]))
$ret = $_GET[$param];
else
return NULL;
if (is_array($ret)) {
foreach($ret as $index => $value)
$ret[$index] = escape_sql_param($ret[$index]);
return $ret;
}
$ret = escape_sql_param($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 (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 $nrs Array contains the number of items to be displayed
*/
function items_on_page($nrs = array(20,50,100), $additional_url_elements=null)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $limit;
if (!$nrs)
$nrs = array(20,50,100);
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
* @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")
*/
function pages($total = NULL, $additional_url_elements=array())
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $limit, $page, $module, $method, $action;
if (!$limit)
$limit = 20;
$link = $_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 (!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;
if ($total < $limit)
return;
$pages = floor($total/$limit);
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> ';
}
print '</div>';
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"]))
print "<input type=\"hidden\" name=\"method\" id=\"method\" value=\"$method\" />\n";
if (is_array($module) && !isset($additional["module"]))
print "<input type=\"hidden\" name=\"module\" id=\"module\" value=\"$module[0]\" />\n";
elseif (($module || $empty_page_params) && !isset($additional["module"]))
print "<input type=\"hidden\" name=\"module\" id=\"module\" value=\"$module\" />\n";
print "<input type=\"hidden\" name=\"action\" id=\"action\" value=\"$action\" />\n";
if (count($additional))
foreach($additional as $key=>$value)
print '<input type="hidden" id="' . $key . '" name="' . $key . '" value="' . $value . '">';
if (isset($_SESSION["previous_page"])) {
foreach ($_SESSION["previous_page"] as $param=>$value)
if (!isset($additional[$param]) && $param!="module" && $param!="method" && $param!="action" && !in_array($param,$skip_params))
print '<input type="hidden" id="'.$param.'" name="' . $param . '" value="' . $value . '">';
}
}
/**
* Creates a form for editing an object
* @param $object Object that will be edited or NULL if fields don't belong to an object
* @param $fields Array of type field_name=>field_formats
* Ex: $fields = array(
"username"=>array("display"=>"fixed", "compulsory"=>true),
// if index 0 in the array is not set then this field will correspond to variable username of @ref $object
// the field will be marked with a *(compulsory)
"description"=>array("display"=>"textarea", "comment"=>"short description"),
// "comment" is used for inserting a comment under the html element
"password"=>array("display"=>"password", "compulsory"=>"yes"),
"birthday"=>array("date", "display"=>"include_date"),
// will call function include_date
"category"=>array($categories, "display"=>"select")
// $categories is an array like
// $categories = array(array("category_id"=>"4", "category"=>"Nature"), array("category_id"=>"5", "category"=>"Movies")); when select category 'Nature' $_POST["category"] will be 4
// or $categories = array("Nature", "Movies");
"sex"=>array($sex, "display"=>"radio")
// $sex = array("male","female","don't want to answer");
);
* instead of "compulsory", "requited" can be also used
* possible values for "display" are "textarea", "password", "fileselect", "text", "select", "radio", "radios", "checkbox", "fixed"
* If not specified display is "text"
* If the field corresponds to a bool field in the object given display is ignored and display is set to "checkbox"
* @param $title Text representing the title of the form
* @param $submit Text representing the value of the submit button or Array of values that will appear as more submit buttons
* @param $compulsory_notice Bool true for using default notice, Text representing a notice that will be printed under the form if other notice is desired or NULL or false for no notice
* @param $no_reset When set to true the reset button won't be displayed, Default value is false
* @param $css Name of the css to be used when generating the elements. Default value is 'edit'
* @param $form_identifier Text. Used to make the current fields unique(Used when this function is called more than once inside the same form with fields that can have the same name when being displayed)
* @param $td_width Array or by default NULL. If Array("left"=>$value_left, "right"=>$value_right), force the widths to the ones provided. $value_left could be 20px or 20%.
* @param $hide_advanced Bool default false. When true advanced fields will be always hidden when displaying form
*/
function editObject($object, $fields, $title, $submit="Submit", $compulsory_notice=NULL, $no_reset=false, $css=NULL, $form_identifier='', $td_width=NULL, $hide_advanced=false)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
if(!$css)
$css = "edit";
print '<table class="'.$css.'" cellspacing="0" cellpadding="0">';
if($title) {
print '<tr class="'.$css.'">';
print '<th class="'.$css.'" colspan="2">'.$title.'</th>';
print '</tr>';
}
$show_advanced = false;
$have_advanced = false;
$custom_submit = array();
//find if there are any fields marked as advanced that have a value(if so then all advanced fields should be displayed)
foreach($fields as $field_name=>$field_format)
{
if(!isset($field_format["advanced"]))
continue;
if($field_format["advanced"] != true)
continue;
$have_advanced = true;
if($object)
$value = (!is_array($field_name) && isset($object->{$field_name})) ? $object->{$field_name} : NULL;
else
$value = NULL;
if(isset($field_format["value"]))
$value = $field_format["value"];
if (!$object || !is_object($object))
break;
$variable = $object->variable($field_name);
if((!$variable && $value && !$hide_advanced))
{
$show_advanced = true;
break;
}
if(!$variable)
continue;
if (($value && $variable->_type!="bool" && !$hide_advanced) || ($variable->_type=="bool" && bool_value($value) && !$hide_advanced))
{
$show_advanced = true;
break;
}
}
//if found errors in advanced fields, display the fields
foreach($fields as $field_name=>$field_format) {
if(!isset($field_format["advanced"]))
continue;
if (isset($field_format["error"]) && $field_format["error"]===true) {
$show_advanced = true;
break;
}
}
foreach($fields as $field_name=>$field_format) {
if (!isset($field_format["display"]) || $field_format["display"]!="custom_submit")
display_pair($field_name, $field_format, $object, $form_identifier, $css, $show_advanced, $td_width);
else
$custom_submit[$field_name] = $field_format;
}
if($have_advanced && !$compulsory_notice)
{
print '<tr class="'.$css.'">';
print '<td class="'.$css.' left_td advanced"> </th>';
print '<td class="'.$css.' left_right advanced"><img id="'.$form_identifier.'xadvanced"';
if(!$show_advanced)
print " src=\"images/advanced.jpg\" title=\"Show advanced fields\"";
else
print " src=\"images/basic.jpg\" title=\"Hide advanced fields\"";
print ' onClick="advanced(\''.$form_identifier.'\');"/></th></tr>';
}
if($compulsory_notice && $compulsory_notice !== true)
{
if($have_advanced) {
print '<tr class="'.$css.'">';
print '<td class="'.$css.' left_td" colspan="2">';
print '<img class="advanced" id="'.$form_identifier.'advanced" ';
if(!$show_advanced)
print "src=\"images/advanced.jpg\" title=\"Show advanced fields\"";
else
print "src=\"images/basic.jpg\" title=\"Hide advanced fields\"";
print ' onClick="advanced(\''.$form_identifier.'\');"/>'.$compulsory_notice.'</td>';
print '</tr>';
}
}elseif($compulsory_notice === true){
print '<tr class="'.$css.'">';
print '<td class="'.$css.' left_td" colspan="2">';
if($have_advanced) {
print '<img id="'.$form_identifier.'xadvanced"';
if(!$show_advanced)
print " class=\"advanced\" src=\"images/advanced.jpg\" title=\"Show advanced fields\"";
else
print " class=\"advanced\" src=\"images/basic.jpg\" title=\"Hide advanced fields\"";
print ' onClick="advanced(\''.$form_identifier.'\');"/>';
}
print 'Fields marked with <font class="compulsory">*</font> are required.</td>';
print '</tr>';
}
if(count($custom_submit))
foreach($custom_submit as $field_name=>$field_format)
display_pair($field_name, $field_format, $object, $form_identifier, $css, $show_advanced, $td_width);
if($submit != "no" && $submit != "no_submit")
{
print '<tr class="'.$css.'">';
print '<td class="'.$css.' trailer" colspan="2">';
if(is_array($submit))
{
for($i=0; $i<count($submit); $i++)
{
print ' ';
print '<input class="'.$css.'" type="submit" name="'.$submit[$i].'" value="'.$submit[$i].'"/>';
}
}else
print '<input class="'.$css.'" type="submit" name="'.$submit.'" value="'.$submit.'"/>';
if(!$no_reset) {
print ' <input class="'.$css.'" type="reset" value="Reset"/>';
$cancel_but = cancel_button($css);
if ($cancel_but)
print " $cancel_but";
}
print '</td>';
print '</tr>';
}
print '</table>';
}
/**
* Creates an input cancel button with build onclick link
* to return to the previous page
*/
function cancel_button($css="", $name="Cancel")
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
$res = null;
if (isset($_SESSION["previous_page"])) {
$link = $_SESSION["main"]."?";
foreach ($_SESSION["previous_page"] as $param=>$value) {
if (is_array($value) || is_object ($value))
continue;
$link.= "$param=".urlencode($value)."&";
}
$res = '<input class="'.$css.'" type="button" value="'.$name.'" onClick="location.href=\''.$link.'\'"/>';
}
return $res;
}
/**
* Returns a string with the link build from previous page data session variable
*/
function cancel_params()
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
$link = "";
foreach ($_SESSION["previous_page"] as $param=>$value)
$link.= "$param=".urlencode($value)."&";
return $link;
}
/**
* Builds the HTML data for FORM
*/
function display_pair($field_name, $field_format, $object, $form_identifier, $css, $show_advanced, $td_width, $category_id=null)
{
global $allow_code_comment, $use_comments_docs, $method;
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
if (!isset($allow_code_comment))
$allow_code_comment = true;
if (!isset($use_comments_docs))
$use_comments_docs = false;
$q_mark = false;
if (isset($field_format["advanced"]))
$have_advanced = true;
if (isset($field_format["triggered_by"]))
$needs_trigger = true;
if ($object) {
if (is_array($object))
$value = (!is_array($field_name) && isset($object[$field_name])) ? $object[$field_name] : NULL;
elseif (is_object($object))
$value = (!is_array($field_name) && isset($object->{$field_name})) ? $object->{$field_name} : NULL;
} else
$value = NULL;
if (isset($field_format["value"]))
$value = $field_format["value"];
if (!is_array($value) && !strlen($value) && isset($field_format["cb_for_value"]) && isset($field_format["cb_for_value"]["name"]) && is_callable($field_format["cb_for_value"]["name"])) {
if (count($field_format["cb_for_value"])==2)
$value = call_user_func_array($field_format["cb_for_value"]["name"],$field_format["cb_for_value"]["params"]);
else
$value = call_user_func($field_format["cb_for_value"]["name"]);
}
print '<tr id="tr_'.$form_identifier.$field_name.'"';
// if($needs_trigger == true)
// print 'name="'.$form_identifier.$field_name.'triggered'.$field_format["triggered_by"].'"';
if (isset($field_format["error"]) && $field_format["error"]===true)
$css .= " error_field";
print ' class="'.$css.'"';
if(isset($field_format["advanced"]))
{
if(!$show_advanced) {
print ' style="display:none;" advanced="true" ';
if (isset($field_format["triggered_by"]))
print " trigger=\"true\" ";
} elseif(isset($field_format["triggered_by"])){
if($needs_trigger)
print ' style="display:none;" trigger=\"true\" ';
else
print ' style="display:table-row;" trigger=\"true\" ';
} else
print ' style="display:table-row;"';
} elseif (isset($field_format["triggered_by"])) {
if ($needs_trigger)
print ' style="display:none;" trigger=\"true\" ';
else
print ' style="display:table-row;" trigger=\"true\" ';
}
print '>';
// if $var_name is an array we won't use it
$var_name = (isset($field_format[0])) ? $field_format[0] : $field_name;
$display = (isset($field_format["display"])) ? $field_format["display"] : "text";
if ($object && !is_array($object)) {
$variable = (!is_array($var_name)) ? $object->variable($var_name) : NULL;
if ($variable) {
if ($variable->_type == "bool" && $display!="text")
$display = "checkbox";