-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.form.php
More file actions
1461 lines (1390 loc) · 61.5 KB
/
functions.form.php
File metadata and controls
1461 lines (1390 loc) · 61.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* This file contains functions for forms
* @author Craig Spurrier <[email protected]>
* @package simms
*/
/**
* This function checks if any forms have been submitted by checking for post variables
* @author Craig Spurrier
* @version 0.4 Jan 2 2010 15:42EDT
* @return bool
*/
function form_submitted(){
if(count($_POST) > 0 OR count($_FILES) > 0){
return TRUE;
}else{
return FALSE;
}
}
/**
* This function builds the value portion of an input field
* @author Craig Spurrier
* @version 0.2 Nov 4 2010 11:30EDT
* @param string $id The name/id of the field
* @param string $value The default value. This will be overridden by any submitted user data
* @return string
*/
function f_value($id,$default_value=''){
if(!is_empty(get_var($id,'request'))){
$default_value = get_var($id,'request');
}
if(!is_empty($default_value)){
return "value='".htmlspecialchars($default_value, ENT_QUOTES)."'";
}
}
/**
* This function builds an input error area
* @author Craig Spurrier
* @version 0.3 Nov 24 2010 17:05EDT
* @param string $id The id of the input field
*/
function f_input_error($id,$output='echo'){
$return = "<div id='".$id."_error' class='input_error'";
if(!isset($GLOBALS['errors'][$id])){
$return .= " style='display:none;'";
}
$return .= ">";
if(isset($GLOBALS['errors'][$id])){
$return .= $GLOBALS['errors'][$id];
}
$return .= "</div>";
if($output == 'return'){
return $return;
}else{
echo $return;
}
}
function form_input($id,$type,$value='',$opts=array()){//wrapper for legacy support
if(isset($opts['selected'])){
$selected = $opts['selected'];
}else{
$selected = NULL;
}
if(isset($opts['class'])){
$class = $opts['class'];
}else{
$class = NULL;
}
if(isset($opts['output'])){
$output = $opts['output'];
}else{
$output = NULL;
}
f_input($id,$type,$value,$selected,$class,$output,$opts);
f_input_error($id,$output);
}
/**
* This function builds input fields
* @author Craig Spurrier
* @version 0.6 May 3 2012
* @param string $id The name and id of the field
* @param string $type The type of field. Supported types include: text, password, checkbox, select, multi_select, radio, hidden, wysiwyg, small_textarea, tiny_num, file
* @param string $value The default value. This will be overridden by any submitted user data
* @param bool $selected (FALSE|TRUE)
* @param string $class
* @param string $output
* @return mixed Output is determined by the last param
*/
function f_input($id,$type,$value='',$selected='0',$class='',$output='echo',$opts=array()){
global $require_return_over_echo, $files_url;
$name = $id;
if(isset($require_return_over_echo) AND $require_return_over_echo === true) {
$output = "return";
}
$id = str_replace(Array('[',']'),Array('_',''),$id);
switch($type){
case 'text':
$return = "<input type='text' name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id, ENT_QUOTES)."' ".f_value($id,$value);
if(!is_empty($class)){
$return .= " class='$class'";
}
if(isset($opts['placeholder'])){
$return .= " placeholder='".htmlspecialchars($opts['placeholder'], ENT_QUOTES)."'";
}
if(isset($opts['required'])){
$return .= " required='required'";
}
$return .= "/>\n";
break;
case 'password':
$return = "<input type='password' name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id, ENT_QUOTES)."' ";
if(!is_empty($class)){
$return .= " class='$class'";
}
if(isset($opts['placeholder'])){
$return .= " placeholder='".htmlspecialchars($opts['placeholder'], ENT_QUOTES)."'";
}
if(isset($opts['required'])){
$return .= " required='required'";
}
$return .= "/>\n";
break;
case 'checkbox':
$return = "";
if(is_array($value)){
foreach($value AS $value=>$option){
$return .= "<label for='".htmlspecialchars($id."_".$value, ENT_QUOTES)."'>".htmlspecialchars($option)."</label><input type='checkbox' name='".htmlspecialchars($name, ENT_QUOTES)."[]' id='".htmlspecialchars($id."_".$value, ENT_QUOTES)."' value='".htmlspecialchars($value, ENT_QUOTES)."'";
if(is_array($selected) AND isset($selected[$value])){
$return .= " checked='checked'";
}else{
if($selected == $value){
$return .= " checked='checked'";
}
}
$return .= " class='".htmlspecialchars($id, ENT_QUOTES)." ";
if(!is_empty($class)){
$return .= $class;
}
$return .= "'";
if(isset($opts['required'])){
$return .= " required='required'";
}
$return .= ">";
}
}else{
$return .= "<input type='checkbox' name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id, ENT_QUOTES)."' value='".htmlspecialchars($value, ENT_QUOTES)."'";
if($selected == $value){
$return .= " checked='checked'";
}
$return .= " class='".htmlspecialchars($id, ENT_QUOTES)." ";
if(!is_empty($class)){
$return .= $class;
}
$return .= "'";
if(isset($opts['required'])){
$return .= " required='required'";
}
$return .= ">";
}
$return .= "\n";
break;
case 'select':
if(!is_empty(get_var($id,'request'))){
$selected = get_var($id,'request');
}
$return = "<select name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id, ENT_QUOTES)."'";
if(!is_empty($class)){
$return .= " class='$class'";
}
if(isset($opts['required'])){
$return .= " required='required'";
}
$return .= ">";
foreach($value AS $value1=>$option){
if(is_array($option)){
$return .= "<optgroup label='".htmlspecialchars($value1, ENT_QUOTES)."'>";
foreach($option AS $valueA=>$optionA){
$return .= "<option value='".htmlspecialchars($valueA, ENT_QUOTES)."'";
if($selected == $valueA){
$return .= " selected='selected'";
}
$return .= ">".htmlspecialchars($optionA)."</option>\n";
}
$return .= " </optgroup>";
}else{
$return .= "<option value='".htmlspecialchars($value1, ENT_QUOTES)."'";
if($selected == $value1){
$return .= " selected='selected'";
}
$return .= ">".htmlspecialchars($option)."</option>\n";
}
}
$return .= "</select>";
$return .= "\n";
break;
case 'multi_select':
if(!is_empty(get_var($id,'request'))){
$selected = get_var($id,'request');
}
$return = "<select name='".htmlspecialchars($name, ENT_QUOTES)."[]' id='".htmlspecialchars($id, ENT_QUOTES)."' multiple='multiple'";
if(!is_empty($class)){
$return .= " class='$class'";
}
if(isset($opts['required'])){
$return .= " required='required'";
}
$return .= ">";
$values = $value;
foreach($values AS $value=>$option){
$return .= "<option value='".htmlspecialchars($value, ENT_QUOTES)."'";
if(array_key_exists($value,$selected)){
$return .= "selected='selected'";
}
$return .= ">".htmlspecialchars((string) $option)."</option>\n";
}
$return .= "</select>";
$return .= "\n";
break;
case 'radio':
if(!is_empty(get_var($id,'request'))){
$selected = get_var($id,'request');
}
$return = "";
foreach($value AS $value=>$option){
$return .= "<label for='".htmlspecialchars($id."_".$value, ENT_QUOTES)."'><input type='radio' name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id."_".$value, ENT_QUOTES)."' value='".htmlspecialchars($value, ENT_QUOTES)."'";
if($selected == $value){
$return .= " checked='checked'";
}
$return .= " class='".htmlspecialchars($id, ENT_QUOTES)." ";
if(!is_empty($class)){
$return .= $class;
}
$return .= "'";
$return .= ">".htmlspecialchars($option)."</label>";
}
$return .= "\n";
break;
case 'hidden':
$return = "<input type='hidden' name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id, ENT_QUOTES)."' ".f_value($id,$value);
if(!is_empty($class)){
$return .= " class='$class'";
}
$return .= "/>\n";
break;
case 'textarea':
if(!is_empty(get_var($id,'request'))){
$value= get_var($id,'request');
}
$return = "<textarea name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id, ENT_QUOTES)."' rows='12' cols='72'";
if(!is_empty($class)){
$return .= " class='$class'";
}
if(isset($opts['required'])){
$return .= " required='required'";
}
$return .= ">\n";
$return .= htmlspecialchars($value);
$return .= "</textarea>\n";
break;
case 'wysiwyg':
if(!is_empty(get_var($id,'request'))){
$value= get_var($id,'request');
}
$return = "<textarea name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id, ENT_QUOTES)."' rows='12' cols='35'";
if(!strpos($_SERVER['HTTP_USER_AGENT'],"iPhone") AND !strpos($_SERVER['HTTP_USER_AGENT'],"iPad")){
$return .= " class='wysiwyg";
}
if(!is_empty($class)){
$return .= " $class";
}
if(isset($opts['required'])){
$return .= " required='required'";
}
$return .= "'>\n";
$return .= htmlspecialchars($value);
$return .= "</textarea>\n";
break;
case 'small_textarea':
if(!is_empty(get_var($id,'request'))){
$value= get_var($id,'request');
}
$return = "<textarea name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id, ENT_QUOTES)."' rows='5' cols='30'";
if(!is_empty($class)){
$return .= " class='$class'";
}
if(isset($opts['required'])){
$return .= " required='required'";
}
$return .= ">\n";
$return .= htmlspecialchars($value);
$return .= "</textarea>\n";
break;
case 'tiny_num':
$return = "<input type='text' name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id, ENT_QUOTES)."' ".f_value($id,$value)." class='tiny_num";
if(!is_empty($class)){
$return .= " $class";
}
if(isset($opts['required'])){
$return .= " required='required'";
}
$return .= "' maxlength='4'/>\n";
break;
case 'file':
if($value) {
$return = "<span id='original-value-for-".htmlspecialchars($id, ENT_QUOTES)."'>
<input type='hidden' name='original-value-of-".htmlspecialchars($id, ENT_QUOTES)."' value=\"$value\" />";
if(substr($value, 0, strlen($files_url)) == $files_url OR (is_numeric($value) AND $value > 0)) {
if(substr($value, 0, strlen($files_url)) == $files_url) {
$file_id = substr($value, 27);
$url = $value;
} else {
$file_id = $value; //enables direct access to files tables for forms that edit files
$url = $files_url . $value;
}
$file = tcl_file_properties($file_id);
capitans_log("image file id: ".$file_id);
if(substr($file['page_file_type'], 0, 5) == "image") {
$return .= "<a href='$url' target='_blank' class='img-scaleable'><img src='$url?size=150' /></a>";
} else {
$return .= $value;
}
} else {
$return .= $value;
}
$return .= "
</span>
<button id='change-image-for-".htmlspecialchars($id, ENT_QUOTES)."'>Change</button>
<input type='file' name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id, ENT_QUOTES)."' />
<script type='text/javascript'>
$(document).ready(function () {
$('#".htmlspecialchars($id, ENT_QUOTES)."').hide();
$('#change-image-for-".htmlspecialchars($id, ENT_QUOTES)."').focus(function (e) {
e.preventDefault();
$('#change-image-for-".htmlspecialchars($id, ENT_QUOTES)."').hide();
$('#original-value-for-".htmlspecialchars($id, ENT_QUOTES)."').hide();
$('input#".htmlspecialchars($id, ENT_QUOTES)."').show();
$('input#".htmlspecialchars($id, ENT_QUOTES)."').trigger('click');
return false;
});
});
</script>
";
} else {
$return = "<input type='file' name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id, ENT_QUOTES)."' ".f_value($id,$value);
if(!is_empty($class)){
$return .= " class='$class'";
}
if(isset($opts['required'])){
$return .= " required='required'";
}
$return .= "/>\n";
}
break;
case 'datetime':
if(!$value) {
$value = date('m/d/Y h:i a');
}
$return = "<input type='text' name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id, ENT_QUOTES)."' value='".date("m/d/Y h:i a",strtotime($value))."' ";
if(!is_empty($class)){
$return .= " class='$class'";
}
if(isset($opts['required'])){
$return .= " required='required'";
}
$return .= "/>
<script type='text/javascript'>
if(typeof($('#".htmlspecialchars($id, ENT_QUOTES)."').datetimepicker) != 'undefined') {
$('#".htmlspecialchars($id, ENT_QUOTES)."').datetimepicker({
timeFormat: 'hh:mm TT',
stepMinute: 15,
showSecond: false
});
}
</script>";
break;
case 'date':
if(!$value) {
$value = date('m/d/Y');
}
$return = "<input type='text' name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id, ENT_QUOTES)."' value='".date("m/d/Y",strtotime($value))."' ";
if(!is_empty($class)){
$return .= " class='$class'";
}
if(isset($opts['required'])){
$return .= " required='required'";
}
$return .= "/>
<script type='text/javascript'>
if(typeof($('#".htmlspecialchars($id, ENT_QUOTES)."').datepicker) != 'undefined') {
$('#".htmlspecialchars($id, ENT_QUOTES)."').datepicker({ numberOfMonths: 3 });
}
</script>";
break;
case 'time':
$return = "<input type='text' name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id, ENT_QUOTES)."' ".f_value($id,$value);
if(!is_empty($class)){
$return .= " class='$class'";
}
if(isset($opts['required'])){
$return .= " required='required'";
}
$return .= "/>
<script type='text/javascript'>
if(typeof($('#".htmlspecialchars($id, ENT_QUOTES)."').timepicker) != 'undefined') {
$('#".htmlspecialchars($id, ENT_QUOTES)."').timepicker({
timeFormat: 'hh:mm tt',
stepMinute: 15
});
}
</script>";
break;
default:
$return = "<input type='$type' name='".htmlspecialchars($name, ENT_QUOTES)."' id='".htmlspecialchars($id, ENT_QUOTES)."' ".f_value($id,$value);
if(!is_empty($class)){
$return .= " class='$class'";
}
if(isset($opts['required'])){
$return .= " required='required'";
}
if(isset($opts['step'])){
$return .= " step='".htmlspecialchars($opts['step'], ENT_QUOTES)."'";
}
if(isset($opts['placeholder'])){
$return .= " placeholder='".htmlspecialchars($opts['placeholder'], ENT_QUOTES)."'";
}
$return .= "/>\n";
break;
}
if($output == 'return'){
return $return;
}else{
echo $return;
}
}
/**
* This function builds the list part of a multi select list
* @author Craig Spurrier
* @version 0.3 Nov 24 2010 17:05EDT
* @param string $id
* @param array $values
*/
function f_multi_select_list($id,$values,$hidden_x = 0){
echo "<ul id='",$id,"_multi_select_list'>";
foreach($values AS $key=>$value){
if(is_array($value)){
if(isset($value['vtip'])){
echo "<li class='vtip' title='".htmlspecialchars($value['vtip'])."'";
}else{
echo "<li";
}
if(isset($value['id'])) {
echo " id=\"".rawurlencode($value['id'])."\" ";
}
echo ">";
if(isset($value['url'])){
echo "<a href='".$value['url']."'";
if(isset($value['url_class'])){
echo " class='".htmlspecialchars($value['url_class'])."'";
}
echo ">".htmlspecialchars($value['value'])."</a>";
}else{
echo htmlspecialchars($value['value']);
}
if(isset($value['icon_url'])){
echo " <a href='".$value['icon_url']."'";
if(isset($value['icon_url_class'])){
echo " class='".htmlspecialchars($value['icon_url_class'])."'";
}
echo "><img src='".$value['icon']."' width='16' height='16' /></a>";
}
echo "<img src='images/remove.png' alt='remove' width='16' height='16' id='",$id,"_builder_",$key,"_remove' class='remove_button";
if($hidden_x){
echo " hidden";
}
echo "'/>";
}else{
echo "<li>";
echo htmlspecialchars($value);
echo "<img src='images/remove.png' alt='remove' width='16' height='16' id='",$id,"_builder_",$key,"_remove' class='remove_button";
if($hidden_x){
echo " hidden";
}
echo "'/>";
}
echo "</li>";
}
echo "</ul>";
}
/**
* This function checks that all fields in the array are not empty and returns an error via the error array if they are
* @author Craig Spurrier
* @version 0.2 Nov 5 2010 11:15EDT
* @param array $fields
*/
function required_fields($fields){
global $errors;
foreach($fields AS $field){
if(is_empty(get_var($field,'request'))){
$errors[$field] ="This field should not be empty";
}
}
}
/**
* This function validate an e-mail address via a regex and returns an error via the error array if it is incorrect
* @author Craig Spurrier
* @version 0.2 Nov 5 2010 11:30EDT
* @param string $email_field The name/id of the e-mail field
*/
function validate_email($email_field){
global $errors;
if (!preg_match('/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i', get_var($email_field,'request'))) {
$errors[$email_field] ="This does not appear to be a vaild e-mail address";
}
}
/**
* This function validates an e-mail address via a regex and returns true or false
* @author Craig Spurrier
* @version 0.2 Apr 5 2011 11:30EDT
* @param string $email_field The name/id of the e-mail field
*/
function valid_email($email){
if (preg_match('/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i', $email)) {
return TRUE;
}else{
return FALSE;
}
}
/**
* This function compares two fields and returns an error via the error array if they do not match
* @author Craig Spurrier
* @version 0.2 Nov 5 2010 11:40EDT
* @param string $field1 The name/id of the field to match to
* @param string $field2 The name/id of the e-mail to match from
*/
function match_fields($field1,$field2){
global $errors;
if(get_var($field1,'request') != get_var($field2,'request')){
$errors[$field2] ="$field1 does not match $field2";
}
}
/**
* This function validate a recaptcha and returns an error via the error array if it is incorrect
* @author Craig Spurrier
* @version 0.2 Nov 5 2010 11:45EDT
*/
function validate_captcha(){
global $errors,$recaptcha_private_key;
$resp = recaptcha_check_answer ($recaptcha_private_key,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
$errors['recaptcha_response_field'] = "The CAPTCHA solution was incorrect. Please try again.";
}
}
/**
* This function builds an add normalized area
* @author Craig Spurrier
* @version 0.3 Nov 24 2010 17:05EDT
* @param string $id The id of normalized field
* @param string $name The display name of this area
* @param array $fields An array of fields to include in the add area, each array should contain an array with the [0] as the id,[1] as the type, [2] as the default value/array of select options
* @param bool $hidden_button (0|1) Determines if the add button should be visible
*/
function f_input_add($id,$name,$fields,$hidden_button=0){
if(user_can('Add normalized')){
echo "
<img src='images/add.png' alt='Add' width='16' height='16' id='add_",$id,"_button' class='add_button";
if ($hidden_button){echo " hidden";}
echo "'/>
<fieldset id='add_",$id,"' class='add_area'>
<legend>New $name</legend>";
foreach($fields AS $name=>$field_opts){
echo " <p>
<label for='add_",$id,"_",$field_opts[0],"'>$name</label><br/>";
if(isset($field_opts[2])){
f_input("add_".$id."_".$field_opts[0],$field_opts[1],$field_opts[2]);
}else{
f_input("add_".$id."_".$field_opts[0],$field_opts[1]);
}
echo f_input_error("add_".$id."_",$field_opts[0]),
" </p>";
}
echo " <input type='button' value='Add' id='add_",$id,"_action' />
</fieldset>";
}
}
/**
* Creates a form that enables a one-to-many relationship to be created. If the
* form is submited to a file that also calls this function with the same
* parameters, this function will also process and insert/update/delete
* assignments based on user input. Unless in rare cases, this function will
* target the same page that is calling it so that user data from the form
* created by this function so that this function can correctly process it.
*
* This function will prefill selected values (check off the children that
* are already assigned to the parent) if a $first_key_id is supplied
*
* For Sanity's Sake: "Table 1" is the table that is being assigned to have many
* children. The children are chosen from available rows of "Table 2"
*
* @param PDO $conn a valid and connected PHP Database Object
* @param string $assignment_table The name of the relational database table that associates row_ids from table 1 to row_ids of table 2
* @param string $assignment_table_first_key_column The primary index column of table 1
* @param string $assignment_table_second_key_column The primary index column of table 2
* @param mixed $first_key_id The value of the key to assign as the "parent"
* @param stirng $second_table The name of the second table
* @param type $second_table_key_column The column in the second table to use as the key
* @param mixed $second_table_label_column The column in the second table to use as the human-readible "name". If this value is an enumerated array, we'll grab multiple columns as the label (i.e. array('name_last', 'name_first') )
* @param string $form_id The id of form (default: one_to_many). A hidden value is created with the same name to test if the form was actually submitted.
* @param string $second_table_where A string (included the " WHERE condition ") that is used filter the possible assignement values from the second table. Limits the possible children by some criteria.
* @param array $extra_fields_array An array of extra values that can be used as children (i.e array('0' => 'none', 'a' => 'all') ). These values are not stored in the database in a way that can be re-edited with this function by the user.
* @return string Returns either a string that is the HTML form with prefilled values.
*/
function f_data_assignments_one2many($conn, $assignment_table, $assignment_table_first_key_column, $assignment_table_second_key_column,
$first_key_id,
$second_table, $second_table_key_column, $second_table_label_column,
$form_id = 'one_to_many', $second_table_where = "",
$extra_fields_array=false) {
$form_name = "f_data2_".$form_id;
if(is_array($second_table_label_column)) {
$second_table_labels = $second_table_label_column;
$second_table_label_column = $second_table_labels[0];
}
$secondary_table_options = db_query($conn, "SELECT * FROM $second_table $second_table_where ORDER BY $second_table_label_column ASC ");
if(isset($_POST[$form_name])) {
db_exec($conn, "DELETE FROM $assignment_table WHERE $assignment_table_first_key_column = ".db_escape($first_key_id));
foreach($_POST as $key=>$val) {
if(!empty($val)) {
$insert_array = array(
$assignment_table_first_key_column => $first_key_id,
$assignment_table_second_key_column => (int)substr($key, 2)
);
if($extra_fields_array) {
foreach($extra_fields_array as $extra_field_label=>$extra_field_key) {
if(isset($_POST['o_'.substr($key, 2).$extra_field_key])) {
$insert_array[$extra_field_key] = $_POST['o_'.substr($key, 2).$extra_field_key];
}
}
}
db_exec($conn, build_insert_query($conn, $assignment_table, $insert_array));
}
}
}
$assignment_ids = array();
$assignments = db_query($conn, "SELECT * FROM $assignment_table WHERE $assignment_table_first_key_column = ".db_escape($first_key_id));
foreach($assignments as $assignment) {
$assignment_ids[$assignment[$assignment_table_second_key_column]] = $assignment;
}
$string = "<form name='$form_name' action='#' method='POST' id='$form_name'>";
foreach($secondary_table_options as $option) {
$string .= "\n<div class='form_row'>";
if($extra_fields_array AND array_key_exists($option[$second_table_key_column], $assignment_ids)) {
foreach($extra_fields_array as $extra_field_label=>$extra_field_key) {
$string .= "<input type='text' name='o_".$option[$second_table_key_column].$extra_field_key."' id='o_".$option[$second_table_key_column].$extra_field_key."' value=\"".htmlspecialchars($assignment_ids[$option[$second_table_key_column]]['subject_assignment_description'])."\" placeholder='".$extra_field_label."' />";
}
}
$string .= "<input type='checkbox' name='o_".$option[$second_table_key_column]."' id='o_".$option[$second_table_key_column]."' value='1' ";
if(array_key_exists($option[$second_table_key_column], $assignment_ids)) {
$string .= " checked='checked' ";
}
$string .= " /><label for='o_".$option[$second_table_key_column]."'>";
if(isset($second_table_labels) AND is_array($second_table_labels)) {
foreach($second_table_labels as $label) {
$string .= $option[$label]. " ";
}
} else {
$string .= $option[$second_table_label_column];
}
$string .= "</label><br />";
$string .= "</div>";
}
return $string . "<input type='submit'><input type='hidden' name='$form_name' value='1' /></form>";
}
/**
* Generates a form for file uploading. The result of this function, when the
* form is submitted, is passed to f_upload_file().
*
* The table needs to have data columns for name (varchar), contents (blob),
* size (int), type (varchar), restricted (boolean), title (varchar),
* update (datetime), user (varchar)
*
* If you're never going to use this function as part of user management system,
* you can save remove the parts of this function that relate to restricted and
* user. The restricted field is only set to 1 or 0 in the mysql database, it
* does not handle actual data restrictions, that needs to be handled by the
* file-display function.
*
* File upload are limited by the restrictions from the filesystem,
* apache (et al) handler, and network protocol.
*
* @param array $array array('name'=>'', 'contents'=>'', size'=>'', 'type'=>'', 'restricted'=>'', 'title'=>'', 'updated'=>'', 'user'=>'');
* @param PDO $conn a pdo connect resource
* @param string $data_table The table that is storing the data
* @param string $primary_key The primary index column name
* @param mixed $row_id (Usually an int) If this form is updating an existing data row, supply the primary index id value. Use FALSE to indicate you want to make a new row.
* @param string $form_id The id of the form. Used for CSS/jQuery targeting. A hidden input variable is created using the same value of the form's id to test when the form is actually submitted.
*/
function f_data_files($array, $conn, $data_table, $primary_key, $row_id=false, $form_id='someting_file_unique', $user=false) {
if(!isset($array['name'])) {
die('input_array must contain a database column for \'name\'');
}
global $errors;
$string = "";
if(isset($_FILES[$form_id.'file'])) {
$results = f_upload_file($array, $_FILES[$form_id.'file'], $conn, $data_table, $primary_key, $row_id, $_POST[$form_id.'restrict'], $_POST[$form_id.'title'], $user);
if($results) {
$string .= "Uploaded";
} else {
$string .= "Failed";
$errors[] = "Failed to upload file";
}
} else {
$string .= "<form action='#' id='$form_id' method='POST' enctype='multipart/form-data'>";
if(isset($array['title'])) {
$string .= "<label for=\"".$form_id."title\">File Title</label><input type='text' id=\"".$form_id."title\" name=\"".$form_id."title\" />";
}
if(isset($array['restricted'])) {
$string .= "<label for=\"".$form_id."restrict\">File Restriction</label><select id=\"".$form_id."restrict\" name=\"".$form_id."restrict\" /><option value='0'>No</option><option value='1'>Yes</option><select>";
}
$string .= "<label for=\"".$form_id."file\">File</label><input type='file' id=\"".$form_id."file\" name=\"".$form_id."file\" />
<input name='upload' type='submit' value='Upload'>
</form>";
}
return $string;
}
/**
* Inserts file data into a database. Commonly called when a user has submitted
* a form that contains a <input type='file' HTML element
*
* @param array $upload_table_array array('name'=>'', 'contents'=>'', size'=>'', 'type'=>'', 'restricted'=>'', 'title'=>'', 'updated'=>'', 'user'=>'');
* @param array $file_array_from_post The direct array from $_FILE['some_input_name']
* @param PDO $conn A valid and connected PHP Database Object
* @param string $data_table The name of the database table where values will be stored and edited
* @param string $data_table_primary_key The primary index column of the $data_table
* @param mixed $row_id When FALSE, we'll make a new data entry. Otherwise it will replace the values WHERE $data_table_primary_key = $row_id
* @param Boolean $restricted Determines if the row should be marked as "restricted". This function does nothing more than sets the value of the column "restricted". It does not actually do any data access restrictions. That should be handled by the display-data function.
* @param String $title An option file title supplied by the user when uploaded. Should be used as the 'name' of the file when displayed if the value is provided on ingest.
* @param mixed $user The user_id of the user that submitted the file.
* @return mixed array('last_insert_id' => int, 'rows_changed' => int) or FALSE if the file upload failed to reach PHP
*/
function f_upload_file($upload_table_array, $file_array_from_post, $conn, $data_table='page_files', $data_table_primary_key='page_file_id', $row_id=false, $restricted=false, $title='', $user=false){
if($file_array_from_post['size'] > 0) {
$fp = fopen($file_array_from_post['tmp_name'], 'r');
$content = fread($fp, filesize($file_array_from_post['tmp_name']));
$q = array(
$upload_table_array['name'] => $file_array_from_post['name'],
$upload_table_array['contents'] => $content
);
if(isset($upload_table_array['size'])) $q[$upload_table_array['size']] = $file_array_from_post['size'];
if(isset($upload_table_array['type'])) $q[$upload_table_array['type']] = $file_array_from_post['type'];
if(isset($upload_table_array['restricted'])) $q[$upload_table_array['restricted']] = $restricted ? '1' : '0';
if(isset($upload_table_array['title'])) $q[$upload_table_array['title']] = $title;
if(isset($upload_table_array['updated'])) $q[$upload_table_array['updated']] = date('Y-m-d H:i:s');
if(isset($upload_table_array['user']) AND $user) $q[$upload_table_array['user']] = $user;
if($row_id) {
$results = db_exec($conn, build_update_query($conn, $data_table, $q, " $data_table_primary_key = ".db_escape($row_id, $conn)));
} else {
$results = db_exec($conn, build_insert_query($conn, $data_table, $q));
}
return $results;
} else {
return false;
}
}
/**
* Makes a form with target='#' so that the form is submitted back to this
* function for processing and insertion into a database. If a row_id is
* provided, this form will prefill the input elements with the data from that
* row.
*
* @global string $errors
* @global boolean $require_return_over_echo Attacks f_input to always return string instead of immediately echo
* @global string $re_captcha_library the path and file name of the 1.11 recaptcha library
* @global string $re_captcha_key_private The private key for your recaptcha
* @global string $re_captcha_key_public The private key for your recaptcha
* @param array $input_array and array of f_data_element objects
* @param PDO $conn a valid and open PHP database object
* @param string $data_table The target database table where values should be stored from this form
* @param string $primary_key The primary index column inside $data_table
* @param mixed $row_id If FALSE, the form will insert a new entry; otherwise it will UPDATE ... WHERE $primary_key = $row_id
* @param string $form_id The value of ID of the Form used for targeting CSS/jQuery. A hidden input element is created with the same name to test if the form has been submitted
* @param string $method Default POST, optionally you can use GET, but I'm not sure why that's useful anymore. @todo Needs refactor
* @param function $insert_callback If set, we'll run insert_callback($new_id) after insertion
* @param function $update_callback If set, we'll run update_callback($id) after updating the datatables
* @param string $form_action Default #
* @param boolean $resource_var UNSUED, sorry @todo refactor
* @param function $validator_function runs before insertion for custom validation
* @param boolean $allow_delete default FALSE. To work, $conn needs to have DELETE privilges
* @param string $delete_url The url to load when a user clicks "delete row" (commonly, #)
* @param string $delete_callback_url A url to load AFTER deletion. Very useful to prevent reloading a page that's no longer valid
* @param function $delete_callback A function to run AFTER deletion
* @param boolean $use_captcha Default TRUE
* @return string The HTML form. Needs to be echoed somehow.
*/
function f_data($input_array, $conn, $data_table, $primary_key, $row_id=false,$form_id='something_unique', $method = "POST", $insert_callback = false, $update_callback = false, $form_action = "#", $resource_var=false, $validator_function=false, $allow_delete =false, $delete_url="", $delete_callback_url=false, $delete_callback=false, $use_captcha=false) {
global $errors, $require_return_over_echo, $re_captcha_library, $re_captcha_key_private, $re_captcha_key_public;
$require_return_over_echo = true;
$string = "";
$form_name = "f_data_". rawurlencode($form_id);
$confirmation = "";
$query_method = "";
if($use_captcha AND !logged_in()) {
require_once($re_captcha_library);
}
//prevent adding a new entry if the callback is an update form
$inserted = false;
if(isset($_POST[$form_name."_inserted"]) AND $_POST[$form_name."_inserted"]) {
$row_id = $_POST[$form_name."_inserted"];
}
if($allow_delete AND isset($_GET['this_delete'], $_GET['this_delete_row']) AND $_GET['this_delete_row'] > 0) {
db_exec($conn, "DELETE FROM $data_table WHERE $primary_key = ".db_escape($_GET['this_delete_row'],$conn)." LIMIT 1");
if($delete_callback) {
$delete_callback();
}
if($delete_callback_url) {
header('location: '.$delete_callback_url);
} else {
$row_id = false;
}
}
if(get_var($form_name, $method)) {
if($use_captcha AND !logged_in()) {
$response = recaptcha_check_answer($re_captcha_key_private, $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);
if(!$response->is_valid) {
$errors[] = "The recaptcha you submitted is invalid";
}
}
foreach($input_array as $element) {
if(is_a($element, 'f_data_element')) {
if($element->options and $element->options->required) {
required_fields($element->id);
}
if($element->type == 'datetime') {
$element->value = date("Y-m-d H:i:s",strtotime(get_var($element->id, $method)));
} else {
$element->value = get_var($element->id, $method);
}
}
}
if($errors) {
$string .= print_r($errors, true);
} else {
if($validator_function AND function_exists($validator_function) AND $validator_function() !== true) {
$string .= "<div class='input-error'>".($validator_function())."</div>";
} else {
if($row_id !== false) {
//update items
$query_method = "update";
foreach ($input_array as $element) {
if(is_a($element, 'f_data_element')) {
$element->set($conn, $primary_key, $row_id, $data_table);
}
}
$confirmation = "Updated";
if($update_callback !== false AND function_exists($update_callback)) {
$update_callback($row_id);
}
} else {
//insert items
$query_method = "insert";
$arr = array();
foreach ($input_array as $element) {
if($element->type == 'file') {
if(isset($element->options) AND is_object($element->options) AND $element->options->data_table) {
$upload_table_array = array(
'name' => $element->options->data_table_name,
'title' => $element->options->data_table_title,
'type' => $element->options->data_table_type,
'contents' => $element->options->data_table_contents,
'updated' => $element->options->data_table_updated,
'restricted' => $element->options->data_table_restricted,
'size' => $element->options->data_table_size,
'user' => $element->options->data_table_user,
);
$result = f_upload_file($upload_table_array, $_FILES[$element->id], $conn, $element->options->data_table, $element->options->data_table_key_column);
} else {
$upload_table_array = array(
'name' => 'page_file_name',
'title' => 'page_file_title',
'type' => 'page_file_type',
'contents' => 'page_file_contents',
'updated' => 'page_file_contents_date',
'restricted' => 'page_file_access_restricted',
'size' => 'page_file_size',
'user' => 'page_file_user',
);
$result = f_upload_file($upload_table_array, $_FILES[$element->id], $conn, 'sitewide.page_files', 'page_file_id');
}
if($result) {
$file_id = $conn->lastInsertId();
ul_new_category_assignment($file_id, 11);
$arr[$element->id] = 'http://library.sc.edu/file/'.$file_id;
} else {
$arr[$element->id] = "";
}
} else {
$arr[$element->id] = $element->value;
}
}
$result = db_exec($conn, build_insert_query($conn, $data_table, $arr));
$confirmation = "Inserted new record";
if($insert_callback !== false) {
$insert_callback($result['last_id']);
}
$row_id = $result['last_id'];
$inserted = true;
}
}
}
}
if(!get_var($form_name, $method) OR ($insert_callback === false AND $query_method == "insert") OR ($update_callback === false AND $query_method == "update") ) {
if(!$errors) {
$fields = "";
foreach($input_array as $element) {
if(is_a($element, 'f_data_element') AND $element->type != "password") {
$fields .= $element->id.", ";
}
}
if($fields != "") {
//having trouble here with rows who have an id of 0
$p_key_query_value = ($row_id === false ? "'-1'" : db_escape($row_id, $conn));
$q = "SELECT ".substr($fields, 0, -2)." FROM $data_table WHERE $primary_key = ".$p_key_query_value;
$original_values = db_query($conn, $q);
foreach($input_array as $element) {
if(is_a($element, 'f_data_element') AND $original_values) {
if($element->type != 'password' AND $element->type != 'select' AND $element->type != 'multi_select') {
$element->value = $original_values[0][$element->id];
}
if($element->type == 'select') {
$element->value = $element->values;
$element->selected = $original_values[0][$element->id];
}
if( $element->type == 'multi_select') {
$element->value = $element->values;
$element->selected = array($original_values[0][$element->id]=>$original_values[0][$element->id]);
}
}
if(is_a($element, 'f_data_element')) {
if($element->options AND $element->options->normalized_table_primary_label_column) {
if(is_array($element->options->normalized_table_primary_label_column)){
$select = "";
foreach($element->options->normalized_table_primary_label_column as $label_part) {
$select .= ", ".$label_part;
}
$order_by = $element->options->normalized_table_primary_label_column[0];
} else {
$order_by = $element->options->normalized_table_primary_label_column;
$select = ", ".$element->options->normalized_table_primary_key_column . ", ".$element->options->normalized_table_primary_label_column;
}
$these_options = db_query($conn, "SELECT ".$element->options->normalized_table_primary_key_column." $select FROM ".$element->options->normalized_table." ".($element->options->normalized_table_limit_condition)." ORDER BY $order_by ASC ");
$arr =array();
if($element->options->normalized_table_none_label) {
$arr[$element->options->normalized_table_none_value] = $element->options->normalized_table_none_label;
}
foreach($these_options as $option) {
if(is_array($element->options->normalized_table_primary_label_column)) {