/
BxDolForm.php
1693 lines (1547 loc) · 68.8 KB
/
BxDolForm.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 defined('BX_DOL') or die('hack attempt');
/**
* Copyright (c) UNA, Inc - https://una.io
* MIT License - https://opensource.org/licenses/MIT
*
* @defgroup UnaCore UNA Core
* @{
*/
define('BX_DOL_FORM_METHOD_GET', 'get');
define('BX_DOL_FORM_METHOD_POST', 'post');
define('BX_DOL_FORM_METHOD_SPECIFIC', 'specific');
define('BX_DATA_LISTS_KEY_PREFIX', '#!');
define('BX_DATA_VALUES_DEFAULT', 'LKey'); ///< Use default values for data items, @see BxDolForm::getDataItems
define('BX_DATA_VALUES_ADDITIONAL', 'LKey2'); ///< Use additional values for data items, @see BxDolForm::getDataItems
/**
* Forms allows to display forms from data stored in DB tables, before it was possible to display forms from PHP arrays only.
*
*
* The Form Objects have the following main advantages:
* - Minimal coding is needed to create different forms
* - Easy forms alterations
* - Multiple representations of the same form
* - Automated data inserting into database
* - Automated data updating
* - Automated data checking
* - Automatic security checking
* - Automatic spam filter
*
*
* Forms API uses several definitions:
* - Form or Form Object - record in sys_objects_form table, or instance of Form class.
* - Form Display - set of some form inputs in particular order, displayed for some purpose; one form can have several displays, for example add and edit displays.
* - Form Input or Form Field - form input field, like textarea, checkbox or set of radio buttons.
*
*
* @section form_creating_object Creating the Form Object
*
* 1. Create Form Object, add record to sys_objects_form table:
* - object - name of the Form Object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing; for example: bx_group - for group data processing, like group adding or editing.
* - title - Form Object title to display in studio forms builder.
* - action - url to submit form to, if url is not full and not empty, then site url is added automatically.
* - form_attrs - serialized array of additional form attributes.
* - submit_name - name of form field with submit button, it is used to determine if form is submitted.
* - table - DB table name (for automatic saving/updating).
* - key - DB table field with unique ID (for automatic updating).
* - uri - DB table field with URI (for automatic URI generation, aling with uri_title).
* - uri_title - DB table field with data title (for automatic URI generation, aling with uri).
* - params - serialized array of additional form parameters:
* - checker_helper - name of custom BxDolFormCheckerHelper class.
* - csrf - array of Cross-site request forgery attack prevention parameters, for now only one boolean parameter is supported - disabled, so it can be disabled for particular form.
* - active - 1 or 0, if form is inactive then it can not be used anywhere.
* - override_class_name - user defined class name which is derived from BxTemplFormView.
* - override_class_file - the location of the user defined class, leave it empty if class is located in system folders.
*
*
* 2. Create Form Displays, add records to sys_form_displays table:
* - display_name - name of the Form Display, in the format:
* form object name, underscore, internal identifier or nothing;
* for example: bx_group_add - for displaying group adding form, bx_group_edit - for displaying group editing form
* - module - module name this display belongs to, it must be associated with name field in sys_modules table.
* - object - form object name from sys_objects_form table this Form Display belongs to.
* - title - Form Display title to display in studio forms builder.
* - view_mode - display form as read-only.
*
*
* 3. Create Form Fields, add records to sys_form_inputs table:
* - object - form object name from sys_objects_form table this Form Field belongs to.
* - module - module name this field belongs to.
* - name - unique Form Field name in particular From Object.
* - value - default value, or empty if there is no default value.
* - values - possible values, for certain form field types.
* - checked - 0 or 1, it determines if form field is checked, for certain form field types.
* - type - form field type, for now the following types are supported:
* - text - text input field.
* - password - password input field.
* - textarea - multiline input field.
* - number - number input field.
* - select - select one from all available values.
* - select_multiple - select one, multiple or all items from all available values.
* - switcher - on/off switcher.
* - checkbox - one checkbox.
* - checkbox_set - set of checkboxes.
* - radio_set - set of radio buttons.
* - slider - select some numeric value within the range using slider control.
* - doublerange - select range values within the range using slider control.
* - datepicker - date selection control.
* - datetime - date/time selection control.
* - captcha - image captcha.
* - hidden - hidden input field.
* - file - file upload input.
* - button - button control.
* - image - form image button.
* - reset - form reset button.
* - submit - form submit button.
* - value - just displaying value without any crontol.
* - block_header - start group of field.
* - custom - custom control.
* - input_set - set of other form controls.
* detailed description of every type will be described below.
* - caption - input title.
* - info - some info to help user to input data into the field, it's better to specify format and limits here.
* - required - indicate that the input is required by displaying asterisk near the field,
* please note that this field don't perform any checking automatically,
* since you mark field as required you need to specify checked function which will check entered value.
* - collapsed - display section as collapsed by default, for block_header field type only.
* - html - display visual editor of certain type, for textarea field type only.
* - 0 - no visual editor, leave textarea field as it is.
* - 1 - standard(default) visual editor, @see BxDolEditor.
* - 2 - full visual editor, @see BxDolEditor.
* - 3 - mini visual editor, @see BxDolEditor.
* - attrs - serialized array of additional input attributes.
* - attrs_tr - serialized array of additional attributes for the whole input row.
* - attrs_wrapper - serialized array of additional attributes for input wrapper.
* - checker_func - checked function, if you marked field as required in textarea field you need to point one of the following checked functions:
* - Length - check value length, additional params must contain min and/or max values for checking.
* - Date - check if date is entered correctly.
* - DateTime - check if datetime is entered correctly.
* - Preg - check value with provided regular expression in checker_params field.
* - Avail - just check if value isn't 0 or empty string, additional function parameters are not used.
* - Email - check if value is written in valid email format.
* - Captcha - check if captcha is entered correctly, for captcha field type only.
* You can inherit BxDolFormCheckerHelper class and add own checker functions, you will need to point your custom class in Form Object params array.
* - checker_params - serialized array of checker_func parameters.
* - checker_error - error message to show in case of checking function returns false.
* - db_pass - function to pass value through before saving to database and after restoring from database (for example when date need to be converted from/to timestamp value),
* available values are the following:
* - Int - convert value to integer.
* - Float - convert value to floating point number.
* - Date,DateUtc - convert value to timestamp value before saving to database, and convert from timespamp value after restoring from database.
* - DateTime,DateTimeUtc - convert value to timestamp value before saving to database, and convert from timespamp value after restoring from database.
* - Xss - it warns you that this text can contain XSS vulnerabilities and you need to be extra careful with this, and always use Forms engine to output string to the browser or use bx_process_output if going to output text manually.
* - XssHtml - this text cam have HTML tags, so perform XSS vulnerabilies cleaning before saving to database.
* - All - do not perform any conversion and pass text as it is, be careful with this, use it only when no other function can be used, and make all necessary security checking by yourself.
* - Preg - perform regular expression on the text before saving data to database, regular expression can be provided in db_params field.
* - Boolean - this is used for checkboxes with 'on' value which need to be converted into boolean value.
* - Set - convert set of values into bit integer before saving to database, and restore bit integer into array of values upon restoration from database, it can be used for select_multiple and checkbox_set field types.
* Please note that values for this field must be 1,2,4,8,... (values of power of 2); the max number of values are 31 for 32bit hardware and 63 for 64bit hardware.
* You can inherit BxDolFormCheckerHelper class and add own pass functions, you will need to point your custom class in Form Object params array.
* - db_params - serialized array of db_pass parameters.
* - editable - allow to edit this field from admin forms builder.
* - deletable - allow to delete this field from admin forms builder.
*
*
* 4. Add Form Fields and Form Displays associations, add records to sys_form_display_inputs table:
* - display_name - name of the Form Display from sys_form_displays table.
* - input_name - name of the Form Field from sys_form_inputs table.
* - visible_for_levels - bit field with set of member level ids. To use member level id in bit field the level id minus 1 is used as power of 2, for example:
* - user level id = 1 -> 2^(1-1) = 1
* - user level id = 2 -> 2^(2-1) = 2
* - user level id = 3 -> 2^(3-1) = 4
* - user level id = 4 -> 2^(4-1) = 8
* - active - 1 - form field displayed on form, or 0 - isn't displayed.
* - order - fields are displayed in this order.
*
*
* @section form_field_types Form Field Types
*
* Detailed description of Form Field Types.
*
* Almost all fields have the following common parameters:
* - object
* - name
* - type
* - caption
* - info
* - required
* - attrs
* - attrs_tr
* - attrs_wrapper
*
* We will not describe above list of parameters in every type, since they work the same way for all types.
*
* The list below are field types with their unique parameters, which are designed especially for this field, or some parameters which work differently for the specified field type.
*
* text - text input field. It is displayed as regular single line text input.
* Parameters:
* - value - default value, or empty if there is no default value.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func
* Can be used here: Length, Preg, Avail, Email
* Make no sense to use it here: Date, DateTime, Captcha
* - db_pass
* Can be used here: Int, Float, Xss, All, Preg
* Make no sense to use it here: Date, DateTime, XssHtml, Boolean, Set
*
* password - password input field. It is displayed as HTML input element with invisible input.
* Parameters:
* - value - default value, or empty if there is no default value.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func
* Can be used here: Length, Preg, Avail.
* Make no sense to use it here: Date, DateTime, Captcha, Email.
* - db_pass
* Can be used here: Xss, All.
* Make no sense to use it here: Int, Float, Date, DateTime, XssHtml, Boolean, Set, Preg.
*
* textarea - multiline input field. It can be displayed as regular textarea field or as visual HTML editor.
* Parameters:
* - value - default value, or empty if there is no default value.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - use visual editor or not.
* - 0 - no visual editor, leave textarea field as it is.
* - 1 - standard(default) visual editor, @see BxDolEditor.
* - 2 - full visual editor, @see BxDolEditor.
* - 3 - mini visual editor, @see BxDolEditor.
* - checker_func
* Can be used here: Length, Preg, Avail
* Make no sense to use it here: Email, Date, DateTime, Captcha
* - db_pass
* Can be used here: Int, Float, Xss, XssHtml, All, Preg
* Make no sense to use it here: Date, DateTime, Boolean, Set
*
* number - number input field. It is displayed as HTL text input, but with limited width. Also some browsers can add additional controls to this field.
* Parameters:
* - value - default value, or empty if there is no default value.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func
* Can be used here: Length, Preg, Avail
* Make no sense to use it here: Email, Date, DateTime, Captcha
* - db_pass
* Can be used here: Int, Float
* Make no sense to use it here: Xss, XssHtml, All, Preg, Date, DateTime, Boolean, Set
*
* select - select one from all available values. It is displayed as HTML combo-box.
* Parameters:
* - value - default value (array index of selected item from values array), or empty - if there is no default value.
* - values - serialized array of available values, or reference to predefined set of values.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func
* Can be used here: Length, Preg, Avail
* Make no sense to use it here: Email, Date, DateTime, Captcha
* - db_pass
* Can be used here: Int, Float, Xss, All, Preg
* Make no sense to use it here: Date, DateTime, XssHtml, Boolean, Set
*
* select_multiple - select one, multiple or all items from all available values. It is displayed as HTML multiple selection input.
* Parameters:
* - value - default value (bit integer of array indexes of selected items from values array), or empty - if there is no default value.
* - values - serialized array of available values, or reference to predefined set of values. Array index must be power of 2. Max number of values is 31 on 32bit hardware or 63 on 64bit hardware.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func
* Can be used here: Length, Preg, Avail
* Make no sense to use it here: Email, Date, DateTime, Captcha
* - db_pass
* Can be used here: Int
* Make no sense to use it here: Float, Xss, All, Preg, Date, DateTime, XssHtml, Boolean, Set
*
* switcher - on/off switcher. It is displayed as custom HTML element with own styles, but on background it works as regular HTML checkbox element.
* Parameters:
* - value - the value which will be submitted if switcher is on, if switcher is off - nothing is submitted.
* - values - not applicable here.
* - checked - if set to 1 then switcher is on by default, 0 - it is off by default.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func
* Can be used here: Length, Preg, Avail
* Make no sense to use it here: Email, Date, DateTime, Captcha
* - db_pass
* Can be used here: Int, Float, Xss, All, Preg, Boolean
* Make no sense to use it here: Date, DateTime, XssHtml, Set
*
* checkbox - one checkbox. Displayed as HTML checkbox input element.
* Parameters:
* - value - the value which will be submitted if checkbox is checked, if checkbox isn't checked - nothing is submitted.
* - values - not applicable here.
* - checked - if set to 1 then checkbox is checked by default, 0 - it is unchecked by default.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func
* Can be used here: Length, Preg, Avail
* Make no sense to use it here: Email, Date, DateTime, Captcha
* - db_pass
* Can be used here: Int, Float, Xss, All, Preg, Boolean
* Make no sense to use it here: Date, DateTime, XssHtml, Set
*
* checkbox_set - set of checkboxes. It is displayed as set of checkboxes. It is displayed in one row if number of items is equal or less than 3 or every item is displayed on new line if there is more than 3 items in the set.
* Parameters:
* - value - default value (bit integer of array indexes of selected items from values array), or empty - if there is no default value.
* - values - serialized array of available values, or reference to predefined set of values. Array index must be power of 2. Max number of values is 31 on 32bit hardware or 63 on 64bit hardware.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func
* Can be used here: Length, Preg, Avail
* Make no sense to use it here: Email, Date, DateTime, Captcha
* - db_pass
* Can be used here: Int
* Make no sense to use it here: Float, Xss, All, Preg, Date, DateTime, XssHtml, Boolean, Set
*
* radio_set - set of radio buttons. It is displayed as set of radio buttons. It is displayed in one row if number of items is equal or less than 3 or every item is displayed on new line if there is more than 3 items in the set.
* Parameters:
* - value - default value (array index of selected radio button from values array), or empty - if there is no default value.
* - values - serialized array of available values, or reference to predefined set of values.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func
* Can be used here: Length, Preg, Avail
* Make no sense to use it here: Email, Date, DateTime, Captcha
* - db_pass
* Can be used here: Int, Float, Xss, All, Preg
* Make no sense to use it here: Date, DateTime, XssHtml, Boolean, Set
*
* slider - select some numeric value within the range using slider control. It is displayed as jQuery UI HTML control, but on background it works as regular HTML text input element.
* Parameters:
* - value - default value in the format, or empty if there is no default value.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - attrs - the following additional attributes can be used here:
* - min - minimal value, default is 0.
* - max - maximal value, default is 100.
* - step - value can be changed by this step only, default is 1.
* - checker_func
* Can be used here: Length, Avail
* Make no sense to use it here: Preg, Email, Date, DateTime, Captcha
* - db_pass
* Can be used here: Int, Float
* Make no sense to use it here: Xss, XssHtml, All, Preg, Date, DateTime, Boolean, Set
*
* doublerange - select range values within the range using slider control.
* Parameters:
* - value - default value in the format [min value]-[max value], for example 16-99, or empty if there is no default value.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - attrs - the following additional attributes can be used here:
* - min - minimal value, default is 0.
* - max - maximal value, default is 100.
* - step - value can be changed by this step only, default is 1.
* - checker_func
* Can be used here: Length, Avail
* Make no sense to use it here: Preg, Email, Date, DateTime, Captcha
* - db_pass
* Can be used here: Xss, All, Preg
* Make no sense to use it here: Int, Float, XssHtml, Date, DateTime, Boolean, Set
*
* datepicker - date selection control. It is displayed as HTML text input control, when clicking on this input then popup with date selector control is appeared.
* Parameters:
* - value - default value, in the format YYYY-MMM-DD, or empty if there is no default value.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func
* Can be used here: Date
* Make no sense to use it here: Length, Preg, Avail, Email, DateTime, Captcha
* - db_pass
* Can be used here: Date, DateUtc
* Make no sense to use it here: Int, Float, Xss, All, Preg, DateTime, XssHtml, Boolean, Set
*
* datetime - date/time selection control.
* Parameters:
* - value - default value, in the format YYYY-MMM-DD HH:MM:SS, or empty if there is no default value.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func
* Can be used here: DateTime
* Make no sense to use it here: Length, Preg, Avail, Email, Date, Captcha
* - db_pass
* Can be used here: DateTime, DateTimeUtc
* Make no sense to use it here: Int, Float, Xss, All, Preg, Date, XssHtml, Boolean, Set
*
* captcha - image captcha. Displayed as image with some text along with HTML text input for entering displayed on the image text.
* Parameters:
* - value - not applicable here.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func
* Can be used here: Captcha
* Make no sense to use it here: Length, Preg, Avail, Email, Date, DateTime
* - db_pass
* Can be used here: Xss, All, Preg
* Make no sense to use it here: Int, Float, Date, DateTime, XssHtml, Boolean, Set
*
* hidden - hidden input field. Displayed as hidden HTML input.
* Parameters:
* - value - hidden input value.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func
* Can be used here: Length, Preg, Avail, Email, Date, DateTime
* Make no sense to use it here: Captcha
* - db_pass
* Can be used here: Int, Float, Xss, All, Preg, Date, DateUtc, DateTime, DateTimeUtc, XssHtml, Boolean
* Make no sense to use it here: Set
*
* file - file upload input. Displayed as file upload HTML input.
* Parameters:
* - value - not applicable here.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func
* File name is passed for checking.
* Can be used here: Avail, Length, Preg
* Make no sense to use it here: Email, Date, DateTime, Captcha
* - db_pass
* File can't be stored in the database, so this field isn't applicable here.
*
* files - files upload input. Displayed as complex uploading HTML control.
* This control is too complex to describe it using default set of database fields, you need to use custom class to display this control.
*
* button - button control. Displayes as HTML button element.
* Parameters:
* - value - translatable button caption.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func - not applicable here.
* - db_pass - not applicable here.
*
* image - form image button. It is displayed as HTML form image input element.
* Parameters:
* - value - not applicable here.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - attrs - the following mandatory attribute must be specified here:
* - src - image URL.
* - checker_func - not applicable here.
* - db_pass - not applicable here.
*
* reset - form reset button. Displayed as HTML form reset input button. By clicking on this button the form is reset to its default state.
* Parameters:
* - value - translatable button caption.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func - not applicable here.
* - db_pass - not applicable here.
*
* submit - form submit button. Displayed as HTML form submit input button. This button have the primary button style to distinguish it from other buttons. By clicking on this button the form is submitted.
* Parameters:
* - value - translatable button caption.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func - not applicable here.
* - db_pass - not applicable here.
*
* value - just displaying value without any control.
* Parameters:
* - value - the value to display.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func - not applicable here.
* - db_pass - not applicable here.
*
* block_header - start group of fields. Displayed as form fields divider with caption - then it can be collapsible or without caption - then it is just divider without any functionality.
* Parameters:
* - value - not applicable here.
* - values - not applicable here.
* - checked - not applicable here.
* - collapsed - display group of field collapsed by default, 1 - the group is collapsed, 0 - expanded (default value).
* - html - not applicable here.
* - checker_func - not applicable here.
* - db_pass - not applicable here.
*
* custom - custom control. You need custom class to display this control, so the exact used values are determined by particular realisation.
*
* input_set - set of other form controls.
* Parameters:
* - value - not applicable here.
* - values - comma separated list of field names (by name field) of fields to display here.
* - checked - not applicable here.
* - collapsed - not applicable here.
* - html - not applicable here.
* - checker_func - not applicable here.
* - db_pass - not applicable here.
*
*
* @section form_using_own_class Using own class for custom behavior
*
* It is possible to provide own class for displaying and processing the form.
* To do this you need to point it in override_class_name and override_class_file fields in sys_objects_form table.
* Your custom class must be inherited from BxTemplFormView class or its descendants.
*
*
* @section form_display_custom_control Displaying custom field control
*
* It is possible to leave form field with default caption and override only the form field control.
* To override some field you need to define the following function:
* @code
* protected function genCustomInput[field name] ($aInput).
* @endcode
* Where [field name] is form field name.
* For example:
*
* @code
* protected function genCustomInputCustom ($aInput) {
* return
* 'r: <input type="text" size="2" value="'.(isset($aInput['value'][0]) ? $aInput['value'][0] : '').'" name="'.$aInput['name'].'[]" />' .
* 'g: <input type="text" size="2" value="'.(isset($aInput['value'][1]) ? $aInput['value'][1] : '').'" name="'.$aInput['name'].'[]" />' .
* 'b: <input type="text" size="2" value="'.(isset($aInput['value'][2]) ? $aInput['value'][2] : '').'" name="'.$aInput['name'].'[]" />';
* }
* @endcode
*
*
* @section form_display_custom_row Displaying custom field row
*
* Form row consists of caption and control, by default it is displayed with default design and functionality.
* If you need to display some field with custom header and control you need to declare the following function:
* @code
* protected function genCustomRow[field name] ($aInput).
* @endcode
* Where [field name] is form field name.
*
*
* @section example Example of usage
*
* Printing the form for adding new record to the database:
*
* @code
* $oForm = BxDolForm::getObjectInstance('sample_form_objects', 'sample_form_objects_add'); // get form instance for specified form object and display
* if (!$oForm)
* die('"sample_form_objects_add" form object or "sample_form_objects_add" display is not defined');
* $oForm->initChecker(); // init form checker witout any data - adding new record
* if ($oForm->isSubmittedAndValid())
* echo 'inserted id: ' . $oForm->insert (); // add new record to the database
* echo $oForm->getCode(); // display form
* @endcode
*
* Printing the form for editing existing record in the database:
*
* @code
* // $iEditId - ID of edited row, for example from _GET parameter
* $oDb = BxDolDb::getInstance();
* $sQuery = $oDb->prepare("SELECT * FROM `sample_input_types` WHERE id = ?", $iEditId);
* $aRecord = $oDb->getRow();
* if (!$aRecord)
* die("$iEditId record wasn't found.");
*
* $oForm = BxDolForm::getObjectInstance('sample_form_objects', 'sample_form_objects_edit'); // get form instance for specified form object and display
* if (!$oForm)
* die('"sample_form_objects_edit" form object or "sample_form_objects_edit" display is not defined');
* $oForm->initChecker($aRecord); // init form checker with edited data
* if ($oForm->isSubmittedAndValid())
* echo 'updated: ' . $oForm->update ($iEditId); // update database
* echo $oForm->getCode(); // display form
* @endcode
*
* Example of custom form class and custom checking helper class:
*
* @code
* class BxSampleForm extends BxTemplFormView {
*
* public function __construct ($aInfo, $oTemplate = false) {
* parent::__construct ($aInfo, $oTemplate);
* }
*
*
* // display input with 'custom' name
* protected function genCustomInputCustom ($aInput) {
* return
* 'r: <input type="text" size="2" value="'.(isset($aInput['value'][0]) ? $aInput['value'][0] : '').'" name="'.$aInput['name'].'[]" />' .
* 'g: <input type="text" size="2" value="'.(isset($aInput['value'][1]) ? $aInput['value'][1] : '').'" name="'.$aInput['name'].'[]" />' .
* 'b: <input type="text" size="2" value="'.(isset($aInput['value'][2]) ? $aInput['value'][2] : '').'" name="'.$aInput['name'].'[]" />';
* }
*
* }
*
* class BxSampleFormCheckerHelper extends BxDolFormCheckerHelper {
*
* protected $_sDiv = ',';
*
* // prepare RBG values to save to the DB
* function passRgb ($s) {
* if (!is_array($s))
* return false;
*
* $sRet = '';
* foreach ($s as $k => $v)
* $sRet .= (int)trim($v) . $this->_sDiv;
*
* return trim($sRet, $this->_sDiv);
* }
*
* // prepare RGB values to output to the screen
* function displayRgb ($s) {
* return explode($this->_sDiv, $s);
* }
* }
* @endcode
*
* The recommended way is to define forms in database, if it is impossible for some reasons you can init form object from array, there is an example:
*
* @code
* $aForm = array(
* 'form_attrs' => array(
* 'name' => 'form_my',
* 'method' => 'post',
* ),
*
* 'params' => array (
* 'db' => array(
* 'table' => 'table_name', // table name
* 'key' => 'ID', // key field name
* 'uri' => 'EntryUri', // uri field name
* 'uri_title' => 'Title', // title field to generate uri from
* 'submit_name' => 'submit_form', // some filed name with non empty value to determine if the for was submitted,
* in most cases it is submit button name
* ),
* 'csrf' => array(
* 'disable' => true, //if it wasn't set or has some other value then CSRF checking is enabled for current form, take a look at sys_security_form_token_enable to disable CSRF checking completely.
* )
* ),
*
* 'inputs' => array(
*
* 'Title' => array(
* 'type' => 'text',
* 'name' => 'Title', // the same as key and database field name
* 'caption' => 'Some caption',
* 'required' => true,
*
* // checker params
* 'checker' => array (
* 'func' => 'length', // see BxDolFormCheckerHelper class for all check* functions
* 'params' => array(3,100),
* 'error' => 'length must be from 3 to 100 characters',
* ),
* // database params
* 'db' => array (
* 'pass' => 'Xss', // do XSS clear before getting this value, see BxDolFormCheckerHelper class for all pass* functions
* ),
* ),
*
* 'Description' => array(
* 'type' => 'textarea',
* 'name' => 'Description', // the same as key and database field name
* 'caption' => 'Some caption',
* 'required' => true,
*
* // checker params
* 'checker' => array (
* 'func' => 'length',
* 'error' => 'enter at least 3 characters',
* 'params' => array(3,64000),
* ),
* 'db' => array (
* 'pass' => 'XssHtml', // do XSS clear, but keep HTML before getting this value
* ),
* ),
* );
* @endcode
*
* Using of above array:
*
* @code
* $oForm = new BxTemplFormView ($aForm);
* $oForm->initChecker();
*
* if ($oForm->isSubmittedAndValid ()) {
*
* // add additional vars to database, in this case creation date field is added
* $aValsAdd = array (
* 'Date' => time(),
* );
*
* echo 'insert last id: ' . $oForm->insert ($aValsAdd); // insert validated data to database
*
* } else {
*
* echo $oForm->getCode (); // show form
*
* }
*
* @endcode
*
*/
class BxDolForm extends BxDol implements iBxDolReplaceable
{
static $TYPES_SKIP = array('files' => 1);
static $TYPES_CHECKBOX = array('checkbox' => 1, 'switcher' => 1);
static $TYPES_TEXT = array('text' => 1, 'textarea' => 1);
static $TYPES_FILE = array('file' => 1);
static $FUNC_SKIP_DOMAIN_CHECK = array('email' => 1, 'emails' => 1, 'emailexist' => 1, 'emailuniq' => 1, 'hostdomain' => 1, 'hostdomainchat' => 1);
protected $_aMarkers = array ();
protected $oTemplate;
protected $_isValid = true;
protected $_sCheckerHelper;
protected $_aSpecificValues;
public $aFormAttrs; ///< form html element attributes
public $aInputs; ///< form inputs
public $aParams; ///< additional form parameters
public $id; ///< Form element id
public function __construct ($aInfo, $oTemplate)
{
parent::__construct();
if ($oTemplate)
$this->oTemplate = $oTemplate;
else
$this->oTemplate = BxDolTemplate::getInstance();
$this->aFormAttrs = isset($aInfo['form_attrs']) ? $aInfo['form_attrs'] : array();
$this->aInputs = isset($aInfo['inputs']) ? $aInfo['inputs'] : array();
$this->aParams = isset($aInfo['params']) ? $aInfo['params'] : array();
// get form element id
$this->id = $this->aFormAttrs['id'] = (!empty($this->aFormAttrs['id']) ? $this->aFormAttrs['id'] : (!empty($this->aFormAttrs['name']) ? $this->aFormAttrs['name'] : 'form_advanced'));
// set default method
if (!isset($this->aFormAttrs['method']))
$this->aFormAttrs['method'] = BX_DOL_FORM_METHOD_POST;
// set default action
if (!isset($this->aFormAttrs['action']))
$this->aFormAttrs['action'] = '';
$this->_sCheckerHelper = isset($this->aParams['checker_helper']) ? $this->aParams['checker_helper'] : '';
BxDolForm::genCsrfToken();
}
/**
* Get form object instance by object name
* @param $sObject object name
* @return object instance or false on error
*/
static public function getObjectInstance($sObject, $sDisplayName, $oTemplate = false, $sParam = '')
{
$sKey = 'BxDolForm!'.$sObject.'!'.$sDisplayName.'!'.$sParam;
if (isset($GLOBALS['bxDolClasses'][$sKey]))
return $GLOBALS['bxDolClasses'][$sKey];
$aObject = BxDolFormQuery::getFormArray($sObject, $sDisplayName);
if (!$aObject || !is_array($aObject))
return false;
$sClass = 'BxTemplFormView';
if (!empty($aObject['override_class_name'])) {
$sClass = $aObject['override_class_name'];
if (!empty($aObject['override_class_file']))
require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
}
$o = new $sClass($aObject, $oTemplate);
return ($GLOBALS['bxDolClasses'][$sKey] = $o);
}
/**
* Get data items array
* @param $sKey data items identifier
* @param $isUseForSet convert data items keys to use in set fields, make it power of 2 (1,2,4,8,16,etc).
* @param $sUseValues use default(BX_DATA_VALUES_DEFAULT) or additional(BX_DATA_VALUES_ADDITIONAL) value titles, if additinal value title is missing default title is used
* @return data items array
*/
public static function getDataItems($sKey, $isUseForSet = false, $sUseValues = BX_DATA_VALUES_DEFAULT)
{
return BxDolFormQuery::getDataItems($sKey, $isUseForSet, $sUseValues);
}
function initChecker ($aValues = array (), $aSpecificValues = array())
{
$this->_aSpecificValues = $aSpecificValues;
$oChecker = new BxDolFormChecker($this->_sCheckerHelper);
$oChecker->setFormMethod($this->aFormAttrs['method'], $aSpecificValues);
$oChecker->setFormParams($this->aParams);
// init form with default values
$aValuesDef = array ();
foreach ($this->aInputs as $k => $a) {
if (!isset($a['value']) || !isset($a['db']['pass']) || isset(self::$TYPES_CHECKBOX[$a['type']]) || isset(self::$TYPES_SKIP[$a['type']]))
continue;
$aValuesDef[$k] = $a['value'];
}
$oChecker->fillWithValues($this->aInputs, $aValuesDef);
// init form with provided values
if ($aValues)
$oChecker->fillWithValues($this->aInputs, $aValues);
if ($this->isSubmitted ()) {
// init form with submitted data, overwrite prevously declared values
$oChecker->enableFormCsrfChecking(isset($this->aParams['csrf']['disable']) && $this->aParams['csrf']['disable'] === true ? false : true);
$this->_isValid = $oChecker->check($this->aInputs, $aValues);
if (!$this->_initCheckerNestedForms ())
$this->_isValid = false;
}
}
function getKeyValuesPair ()
{
$aRet = array ();
foreach ($this->aInputs as $k => $a) {
if (!isset($a['value']) || isset(self::$TYPES_CHECKBOX[$a['type']]) || isset(self::$TYPES_SKIP[$a['type']]))
continue;
$aRet[$k] = $a['value'];
}
return $aRet;
}
function insert ($aValsToAdd = array(), $isIgnore = false)
{
$oChecker = new BxDolFormChecker($this->_sCheckerHelper);
$oChecker->setFormMethod($this->aFormAttrs['method'], $this->_aSpecificValues);
$sSql = $oChecker->dbInsert($this->aParams['db'], $this->aInputs, $aValsToAdd, $isIgnore);
if (!$sSql)
return false;
$oDb = BxDolDb::getInstance();
if ($oDb->query($sSql))
return $oDb->lastId();
return false;
}
function update ($val, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
{
$oChecker = new BxDolFormChecker($this->_sCheckerHelper);
$oChecker->setFormMethod($this->aFormAttrs['method'], $this->_aSpecificValues);
$sSql = $oChecker->dbUpdate($val, $this->aParams['db'], $this->aInputs, $aValsToAdd, $aTrackTextFieldsChanges);
if (!$sSql)
return false;
return BxDolDb::getInstance()->query($sSql);
}
function delete ($val)
{
$oChecker = new BxDolFormChecker($this->_sCheckerHelper);
$oChecker->setFormMethod($this->aFormAttrs['method'], $this->_aSpecificValues);
$sSql = $oChecker->dbDelete($val, $this->aParams['db'], $this->aInputs);
if (!$sSql)
return false;
return BxDolDb::getInstance()->query($sSql);
}
function generateUri ()
{
$f = &$this->aParams['db'];
$sUri = $this->getCleanValue ($f['uri_title']);
return uriGenerate($sUri, $f['table'], $f['uri']);
}
function getCleanValue ($sName)
{
$oChecker = new BxDolFormChecker($this->_sCheckerHelper);
$oChecker->setFormMethod($this->aFormAttrs['method'], $this->_aSpecificValues);
$a = isset($this->aInputs[$sName]) ? $this->aInputs[$sName] : false;
if ($a && isset($a['db']['pass']))
return $oChecker->get ($a['name'], $a['db']['pass'], isset($a['db']['params']) && $a['db']['params'] ? $a['db']['params'] : array());
else
return $oChecker->get ($sName);
}
function isSubmitted ()
{
return BxDolForm::getSubmittedValue($this->aParams['db']['submit_name'], $this->aFormAttrs['method'], $this->_aSpecificValues) ? true : false;
}
function getId ()
{
return $this->id;
}
function setId ($sId)
{
$this->id = $sId;
$this->aFormAttrs['id'] = $sId;
}
function setName($sName)
{
$this->aFormAttrs['name'] = $sName;
}
function setValid ($isValid)
{
$this->_isValid = $isValid;
}
function isValid ()
{
return $this->_isValid;
}
function isSubmittedAndValid ()
{
return ($this->isSubmitted() && $this->isValid());
}
function getSpecificValues()
{
return $this->_aSpecificValues;
}
public static function getSubmittedValue($sKey, $sMethod, &$aSpecificValues = false)
{
$aData = array();
if($sMethod == BX_DOL_FORM_METHOD_GET) {
$aData = &$_GET;
} else if($sMethod == BX_DOL_FORM_METHOD_POST) {
$aData = &$_POST;
} else if($sMethod == BX_DOL_FORM_METHOD_SPECIFIC) {
$aData = &$aSpecificValues;
}
return isset($aData[$sKey]) ? $aData[$sKey] : false;
}
public static function setSubmittedValue($sKey, $mixedValue, $sMethod, &$aSpecificValues = false)
{
$aData = array();
if($sMethod == BX_DOL_FORM_METHOD_GET) {
$aData = &$_GET;
} else if($sMethod == BX_DOL_FORM_METHOD_POST) {
$aData = &$_POST;
} else if($sMethod == BX_DOL_FORM_METHOD_SPECIFIC) {
$aData = &$aSpecificValues;
}
return $aData[$sKey] = $mixedValue;
}
/**
* Check if field exists
* @param $sName field name
* @param $bCheckAllFields true - check all fields; false - all fields except: password, captcha, hidden, file, button, image, reset, submit, block_header, input_set
* @return true if field exists or false otherwise
*/
public function isFieldExist($sName, $bCheckAllFields = false)
{
if (!isset($this->aInputs[$sName]))
return false;
return $bCheckAllFields ? true : !in_array($this->aInputs[$sName]['type'], array('password', 'captcha', 'hidden', 'button', 'reset', 'submit', 'block_header', 'input_set', 'location'));
}
// Static Methods related to CSRF Tocken
public static function genCsrfToken($bReturn = false)
{
if (getParam('sys_security_form_token_enable') != 'on')
return false;
$oSession = BxDolSession::getInstance();
$iCsrfTokenLifetime = (int)getParam('sys_security_form_token_lifetime');
if($oSession->getValue('csrf_token') === false || ($iCsrfTokenLifetime != 0 && time() - (int)$oSession->getValue('csrf_token_time') > $iCsrfTokenLifetime)) {
$sToken = genRndPwd(20, true);
$oSession->setValue('csrf_token', $sToken);
$oSession->setValue('csrf_token_time', time());
} else
$sToken = $oSession->getValue('csrf_token');
if($bReturn)
return $sToken;
}
public static function getCsrfToken()
{