-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTemplate.php.orig
More file actions
1274 lines (1134 loc) · 58.2 KB
/
Template.php.orig
File metadata and controls
1274 lines (1134 loc) · 58.2 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
namespace BCCHR\CustomTemplateEngine;
/**
* Require Smarty class.
*/
require_once "vendor/autoload.php";
use REDCap;
use Smarty;
use DOMDocument;
require_once "./ExportRights.php"; // class to manage instrument-level rights
class Template
{
/**
* Class properties.
*
* @var Smarty $smarty Instance of Smarty object.
* @var Array $redcap Stores variables that will be used by Smarty template engine to fill data.
*
* @var Bool $show_label_and_row Whether to show a label or row or not in template.
* @var String $de_identified_replacement What to replace de-identified data with.
* @var Array $logical_operators Allowed logical operators.
* @var Array $date_formats Array of date formats.
*/
private $smarty;
private $redcap;
private $dictionary = array();
private $instruments = array();
private $date_formats = array(); // added default value for php8 compatibility to avoid passing null to in_array(). Never populated.
private $show_label_and_row = true;
private $de_identified_replacement = "[DE-IDENTIFIED]"; // rights value 2
private $removed_replacement = "[ID REMOVED]"; // rights value 3
private $no_rights_replacement = "[NO RIGHTS]"; // rights value 0
private $logical_operators = array("eq", "ne", "neq", "gt", "lt", "ge", "gte", "lte", "le", "not", "or", "and");
/**
* Class constructor.
*
* @param String $templates_dir Directory where templates are stored.
* @param String $compiled_dir Directory where templates compiled by Smarty are stored.
*/
function __construct($templates_dir, $compiled_dir)
{
$this->dictionary = REDCap::getDataDictionary('array', false);
$this->instruments = REDCap::getInstrumentNames();
$this->smarty = new Smarty();
$this->smarty->setTemplateDir($templates_dir);
$this->smarty->setCompileDir($compiled_dir);
$this->smarty->assign("showLabelAndRow", $this->show_label_and_row);
}
/**
* Checks whether all the siblings that come before or after an html element are empty
*
* @access private
* @param DOMNode $elem Root element.
* @param String $whichSibling Which sibling/direction to check. Can be either "previous" or "next".
* @return Bool True if the given element and all its siblings are empty, false otherwise.
*/
private function areSiblingsEmpty($elem, $whichSibling)
{
if ($elem == null)
{
return true;
}
else if ($whichSibling == "previous")
{
return $this->areSiblingsEmpty($elem->previousSibling, "previous") && empty($elem->nodeValue);
}
return $this->areSiblingsEmpty($elem->nextSibling, "next") && empty($elem->nodeValue);
}
/**
* Retrieves empty child nodes within given element.
*
* @access private
* @see Template::areSiblingsEmpty() For checking whether a table row is empty of data.
* @param DOMNode $elem Root element.
* @return Array An array of all empty nodes.
*/
private function getEmptyNodes($elem)
{
$empty_elems = array();
$isEmptyOrWhitespace = ctype_space($elem->nodeValue) || str_replace(array(" ", "\xC2\xA0"), "", $elem->nodeValue) == "";
if ($elem->hasChildNodes())
{
foreach($elem->childNodes as $child)
{
$empty_child_elems = $this->getEmptyNodes($child);
$empty_elems = array_merge($empty_elems, $empty_child_elems);
}
}
/**
* Checks for special whitespace characters, and tags that don't contain children.
*/
else if ($isEmptyOrWhitespace && $elem->tagName != "img" && $elem->tagName != "body" && $elem->tagName != "hr" && $elem->tagName != "br")
{
/**
* Empty <td> and <th> elements may pad out other data in table.
* Make sure the entire row is empty, before removing.
*/
if ($elem->tagName == "td" || $elem->tagName == "th")
{
if ($this->areSiblingsEmpty($elem->previousSibling, "previous") && $this->areSiblingsEmpty($elem->nextSibling, "next") && empty($elem->nodeValue))
{
$empty_elems[] = $elem;
}
}
else
{
$empty_elems[] = $elem;
}
}
return $empty_elems;
}
/**
* Parses event data into an assosiative array that will be used by Smarty to fill templates
* with data.
*
* Builds the array used in filling template data. Values are parsed according to the user's
* data rights. i.e. Identifiers removed, no unvalidated text fields, etc... Values are associated with
* field names, and field names are associated with event names (if project is longitudinal).
*
* @access private
* @param Array $event_data Event data for a REDcap record.
* @return Array An associative array of fields mapped to values, or events mapped to an array of fields mapped to values (if longitudinal).
*/
private function parseEventData($event_data)
{
$user = strtolower(USERID);
$rights = REDCap::getUserRights($user);
$rights_object = new ExportRights($rights); // populate a rights object with this user's instrument-level rights
$external_fields = array();
$this->instruments = REDCap::getInstrumentNames();
foreach ($this->instruments as $unique_name => $label)
{
$external_fields[] = "{$unique_name}_complete";
$external_fields[] = "{$unique_name}_timestamp";
}
$event_fields_and_vals = array();
foreach($event_data as $field_name => $value)
{
$value = trim(strip_tags($value));
if (in_array($field_name, $external_fields))
{
$event_fields_and_vals[$field_name] = $value;
}
// else if ($field_name !== "redcap_event_name")
else if ($field_name !== "redcap_event_name"
&& $field_name !== "redcap_repeat_instrument"
&& $field_name !== "redcap_repeat_instance")
{
if ($this->dictionary[$field_name]["field_type"] === "checkbox") { // modify checkbox values
/*
* Check user's data rights
* Value 0 = no rights
* Value 1 = full rights
* Value 2 = de-identified rights
* Value 3 = remove all identifiers rights
* De-identified rights: All unvalidated text fields & notes will be removed, as well as any date/time fields
* Remove all identifiers rights: hide data from all tagged identifier fields.
*/
$event_fields_and_vals[$field_name] = array();
if ($rights_object->field_to_rights_value[$field_name] !== "1") { // check if data needs to be hidden
if (($rights_object->field_to_rights_value[$field_name] === "3") && ($this->dictionary[$field_name]["identifier"] === "y")) { // remove all identifiers, and this is an identifier
$event_fields_and_vals[$field_name]["allValues"] = $this->removed_replacement;
} else if ($rights_object->field_to_rights_value[$field_name] === "2") { // de-identified rights, so remove marked identifiers, freetext and date/time fields
$event_fields_and_vals[$field_name]["allValues"] = $this->de_identified_replacement;
} else { // no rights, so remove everything
$event_fields_and_vals[$field_name]["allValues"] = $this->no_rights_replacement;
} // end else
} else { // full rights, so treat this data normally
$all_choices = explode("|", $this->dictionary[$field_name]["select_choices_or_calculations"]);
$all_choices = array_map(function ($v) {
$v = strip_tags($v);
$first_comma = strpos($v, ",");
return trim(substr($v, $first_comma + 1));
}, $all_choices);
foreach($all_choices as $choice)
{
if (strpos($value, $choice) !== FALSE)
{
$event_fields_and_vals[$field_name][] = $choice;
}
}
$event_fields_and_vals[$field_name]["allValues"] = implode(", ", explode(",", $value));
} // end else
} else { // non-checkbox fields, so check more thorougly
/*
* Check user's data rights
* Value 0 = no rights
* Value 1 = full rights
* Value 2 = de-identified rights
* Value 3 = remove all identifiers rights
* De-identified rights: All unvalidated text fields & notes will be removed, as well as any date/time fields
* Remove all identifiers rights: hide data from all tagged identifier fields.
*/
$event_fields_and_vals[$field_name] = $value;
if ($rights_object->field_to_rights_value[$field_name] !== "1") { // check if value needs to be modified
if ($rights_object->field_to_rights_value[$field_name] === "3") { // remove identifiers right, and this is an identifier
if ($this->dictionary[$field_name]["identifier"] === "y") {
$event_fields_and_vals[$field_name] = $this->removed_replacement;
} // end if
} else if ($rights_object->field_to_rights_value[$field_name] === "2") { // de-identified rights, so remove freetext fields, marked identifiers, and date/time fields
if ((($this->dictionary[$field_name]["field_type"] === "text") && (in_array($this->dictionary[$field_name]["text_validation_type_or_show_slider_number"], $this->date_formats) ||
empty($this->dictionary[$field_name]["text_validation_type_or_show_slider_number"]))) // non-validated text fields, meaning free short text, but not calc fields or decimal/date/int
|| ($this->dictionary[$field_name]["field_type"] === "notes")
|| ($this->dictionary[$field_name]["identifier"] === "y")) {
$event_fields_and_vals[$field_name] = $this->de_identified_replacement;
} // end if
} else { // rights value of 0, so remove all data from these fields
$event_fields_and_vals[$field_name] = $this->no_rights_replacement;
} // end else
} else { // treat the data normally
if ($this->dictionary[$field_name]["field_type"] === "notes") { // make HTML entities
$event_fields_and_vals[$field_name] = str_replace("\r\n", "<br/>", htmlentities($value));
} else { // return without modification
$event_fields_and_vals[$field_name] = $value;
} // end else
} // end else
} // end else
} // end else if
} // end foreach
return $event_fields_and_vals;
} // end function
/**
* Replaces given text with replacement.
*
* @access private
* @param String $text The text to replace.
* @param String $replacement The replacement text.
* @return String A string with the replaced text.
*/
private function replaceStrings($text, $replacement)
{
preg_match_all("/'/", $text, $quotes, PREG_OFFSET_CAPTURE);
$quotes = $quotes[0];
if (sizeof($quotes) % 2 === 0)
{
$i = 0;
$to_replace = array();
while ($i < sizeof($quotes))
{
$to_replace[] = substr($text, $quotes[$i][1], $quotes[$i + 1][1] - $quotes[$i][1] + 1);
$i = $i + 2;
}
$text = str_replace($to_replace, $replacement, $text);
}
return $text;
}
/**
* Parses a syntax string into blocks.
*
* @access private
* @param String $syntax The syntax to parse.
* @return Array An array of blocks that make up the syntax passed.
*/
private function getSyntaxParts($syntax)
{
$syntax = str_replace(array("['", "']"), array("[", "]"), $syntax);
$syntax = $this->replaceStrings(trim($syntax), "''"); //Replace strings with ''
$parts = array();
$previous = array();
$i = 0;
while($i < strlen($syntax))
{
$char = $syntax[$i];
switch($char)
{
case ",":
case "(":
case ")":
case "]":
$part = trim(implode("", $previous));
$previous = array();
if ($part !== "")
{
$parts[] = $part;
}
$parts[] = $char;
$i++;
break;
case "[":
if ($syntax[$i-1] == " ")
{
$parts[] = " ";
}
$part = trim(implode("", $previous));
if ($part !== "")
{
$parts[] = $part;
}
$parts[] = $char;
$previous = array();
$i++;
break;
case " ":
$part = trim(implode("", $previous));
$previous = array();
if ($part !== "")
{
$parts[] = $part;
}
$i++;
break;
default:
$previous[] = $char;
if ($i == strlen($syntax) - 1)
{
$part = trim(implode("", $previous));
if ($part !== "")
{
$parts[] = $part;
}
}
$i++;
break;
}
}
return $parts;
}
/**
* Checks whether fields and events exist within project
*
* @access private
* @param String $var The field name/event name to check
* @return Bool True if $var is a valid REDCap field or event on the project, false otherwise
*/
private function isValidFieldOrEvent($var)
{
$var = trim($var, "'");
$events = REDCap::getEventNames(true, true); // If there are no events (the project is classical), the method will return false
/**
* Get REDCap completion fields
*/
$external_fields = array();
foreach ($this->instruments as $unique_name => $label)
{
$external_fields[] = "{$unique_name}_complete";
$external_fields[] = "{$unique_name}_timestamp";
}
if ($var !== "allValues" && !in_array($var, $external_fields))
{
$dictionary = $this->dictionary[$var];
if (($events === FALSE && empty($dictionary)) ||
($events !== FALSE && !in_array($var, $events) && empty($dictionary)))
{
return false;
}
}
return true;
}
/**
* Checks whether fields and events are being queried correctly.
*
* @access private
* @param String $text The line of text to validate.
* @param Integer $line_num The current line number in the template.
* @return Array An array of errors, with the line number appended to indicate where it occured.
*/
private function validateFieldQueries($text, $line_num)
{
$errors = array();
// Get all occurences of an opening square bracket "["
preg_match_all('/\[/', $text, $opening_brackets, PREG_OFFSET_CAPTURE);
if (!empty($opening_brackets[0]))
{
// Get the event/field name between each opening and closing bracket, and check if it exists
foreach($opening_brackets[0] as $bracket)
{
$start_pos = $bracket[1]+1;
$closing_bracket = strpos($text, "]", $start_pos);
if ($closing_bracket !== FALSE)
{
$var = substr($text, $start_pos, $closing_bracket - $start_pos);
if (substr($var, 0, 1) !== "'" && substr($var, strlen($var)-1, 1) !== "'")
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] <strong>'$var'</strong> must be enclosed with single quotes.";
}
$var = trim($var, "'\"");
if ($var !== "allValues")
{
$dictionary = $this->dictionary[$var];
if (!empty($dictionary))
{
if ($dictionary["field_type"] === "checkbox")
{
// Check if checkbox is queried via in_array()
$in_array = false;
$in_array_start_pos = 0;
while (($in_array_start_pos = strpos($text, "in_array('", $in_array_start_pos)) !== FALSE)
{
$end_of_check_value = strpos($text, "', \$redcap", $in_array_start_pos+1);
$in_array_end_pos = strpos($text, ")", $end_of_check_value+1);
if (($in_array_start_pos < $start_pos) && ($in_array_end_pos > $start_pos))
{
$in_array = true;
break;
}
$in_array_start_pos = $in_array_start_pos + 1;
}
// check if checkbox is queried via ['allValues']
$all_values_str = substr($text, $closing_bracket+1, 13);
if ($all_values_str !== "['allValues']" && !$in_array)
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] <strong>'$var'</strong> is a checkbox and can only be queried using in_array or \$redcap['$var']['allValues']";
}
}
}
}
}
}
}
return $errors;
}
/**
* Validate syntax.
*
* @access private
* @see Template::validateFieldQueries() For checking whether fields and events are queried correctly.
* @see Template::getSyntaxParts() For retreiving blocks of syntax from the given syntax string.
* @param String $syntax The syntax to validate.
* @param Integer $line_num The current line number in the template.
* @return Array An array of errors, with the line number appended to indicate where it occured.
*/
private function validateSyntax($syntax, $line_num)
{
if (!empty($syntax))
{
$errors = $this->validateFieldQueries($syntax, $line_num);
if ((sizeof(explode("'", $syntax)) - 1) % 2 > 0)
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Odd number of single quotes exist. You've either added an extra quote, forgotten to close one, or forgotten to escape one.";
}
else if ($syntax != strip_tags($syntax))
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Report logic cannot have any HTML between {}";
}
else if (preg_match("/{/", $syntax) !== 0 || preg_match("/}/", $syntax) !== 0)
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] There is either a '{' or '}' within {}. They are special characters and can only denote the beginning and end of syntax.";
}
// Check symmetry of ()
if ($parts == null) { $parts = array();} // PHP8 compatability patch Dan Evans, 2023-06-09
if (sizeof(array_keys($parts, "(")) != sizeof(array_keys($parts, ")")))
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Odd number of parenthesis (. You've either added an extra parenthesis, or forgot to close one.";
}
// Check symmetry of []
if (sizeof(array_keys($parts, "[")) != sizeof(array_keys($parts, "]")))
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Odd number of square brackets [. You've either added an extra bracket, or forgot to close one.";
}
$parts = $this->getSyntaxParts($syntax);
foreach($parts as $index => $part)
{
switch ($part) {
case "if":
case "elseif":
// Must have either a ( or ) or $redcap or $showLabelAndRow or in_array after
if ($index != sizeof($parts) - 1)
{
if ($index !== 0)
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Mal-formed <strong>$part</strong> condition. <strong>$part</strong> clause must be first part of syntax.";
}
else
{
$next_part = $parts[$index + 1];
if (($next_part !== "("
&& ($next_part != "''" && $previous == "in_array")
&& $next_part !== ")"
&& $next_part !== "\$redcap"
&& $next_part !== "\$showLabelAndRow"
&& $next_part !== "in_array"
&& $next_part !== "''"))
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Invalid <strong>$next_part</strong> after <strong>$part</strong>.";
}
}
}
else
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Mal-formed <strong>$part</strong> condition. You cannot have an empty <strong>$part</strong> clause.";
}
break;
case "(":
$previous = $parts[$index - 1];
$next_part = $parts[$index + 1];
if (($next_part !== "("
&& ($next_part != "''" && $previous == "in_array")
&& $next_part !== ")"
&& $next_part !== "\$redcap"
&& $next_part !== "\$showLabelAndRow"
&& $next_part !== "in_array"
&& $next_part !== "''"))
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Invalid <strong>$next_part</strong> after <strong>(</strong>.";
}
else if ($next_part == "(" && $previous == "in_array")
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Malformed <strong>in_array()</strong> function.";
}
break;
case ")":
// Must have either a ) or logical operator after, if not the last part of syntax
if ($index != sizeof($parts) - 1)
{
$next_part = $parts[$index + 1];
if ($next_part !== ")" && !in_array($next_part, $this->logical_operators))
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Invalid <strong>$next_part</strong> after <strong>)</strong>.";
}
}
break;
case "eq":
case "ne":
case "neq":
case "gt":
case "ge":
case "gte":
case "lt":
case "le":
case "lte":
// Must have either a ( or $redcap or $showLabelAndRow or in_array or string or not after
// If there's another logical operator two spaces before, is illegal.
if ($index == 0)
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Cannot have a comparison operator <strong>$part</strong> as the first part in syntax.";
}
else if ($index != sizeof($parts) - 1)
{
$previous = $parts[$index - 2];
$next_part = $parts[$index + 1];
if (in_array($previous, $this->logical_operators) && $previous !== "or" && $previous !== "and")
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Invalid <strong>$part</strong>. You cannot chain comparison operators together, you must use an <strong>and</strong> or an <strong>or</strong>";
}
if (!empty($next_part)
&& $next_part !== "("
&& $next_part !== "\$redcap"
&& $next_part !== "\$showLabelAndRow"
&& $next_part !== "''"
&& $next_part !== "in_array"
&& $next_part !== "not"
&& !is_numeric($next_part))
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Invalid <strong>$next_part</strong> after <strong>$part</strong>.";
}
}
else
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Cannot have a comparison operator <strong>$part</strong> as the last part in syntax.";
}
break;
case "not":
case "or":
case "and":
// Must have either a ( or $redcap or $showLabelAndRow or in_array or string or not after
if ($index == 0)
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Cannot have a logical operator <strong>$part</strong> as the first part in syntax.";
}
else if ($index != sizeof($parts) - 1)
{
$next_part = $parts[$index + 1];
if (!empty($next_part)
&& $next_part !== "("
&& $next_part !== "\$redcap"
&& $next_part !== "\$showLabelAndRow"
&& $next_part !== "''"
&& $next_part !== "in_array"
&& $next_part !== "not"
&& !is_numeric($next_part))
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Invalid <strong>$next_part</strong> after <strong>$part</strong>.";
}
}
else
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Cannot have a logical operator <strong>$part</strong> as the last part in syntax.";
}
break;
case "''":
// Must have either a logical operator or ) or , or ]
if ($index != sizeof($parts) - 1)
{
$next_part = $parts[$index + 1];
if ($next_part !== ")"
&& $next_part != ","
&& $next_part != "]"
&& !in_array($next_part, $this->logical_operators))
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Invalid <strong>$next_part</strong> after string value within ''.";
}
}
break;
case "\$redcap":
// Must have a [
if ($index != sizeof($parts) - 1)
{
$next_part = $parts[$index + 1];
if ($next_part !== "[")
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Invalid <strong>\$redcap</strong> field query.";
}
}
else
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Invalid <strong>\$redcap</strong> field query at end of syntax.";
}
break;
case "[":
// Must have a ''
if ($index != sizeof($parts) - 1)
{
$previous = $parts[$index - 1];
$next_part = $parts[$index + 1];
if ($previous !== "\$redcap" && $previous !== "]")
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Each field and events query must be preceeded by <strong>\$redcap</strong>";
}
if ($next_part == "]")
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Cannot have empty [] brackets";
}
}
else
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Cannot have opening <strong>[</strong> as end of syntax.";
}
break;
case "\$showLabelAndRow":
// Must have either a logical operator or ) after, if not last item in syntax
if ($index != sizeof($parts) - 1)
{
$next_part = $parts[$index + 1];
if ($next_part !== ")"
&& !in_array($next_part, $this->logical_operators))
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Invalid <strong>$next_part</strong> after <strong>$part</strong>.";
}
}
break;
case "]":
// Must have either a logical operator or ) or [ after, if not last item in syntax
if ($index != sizeof($parts) - 1)
{
$previous_2 = $parts[$index - 2];
$next_part = $parts[$index + 1];
if ($previous_2 !== "[")
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Unclosed or empty <strong>]</strong> bracket.";
}
if ($next_part !== ")"
&& $next_part !== "["
&& !in_array($next_part, $this->logical_operators))
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Invalid <strong>'$next_part'</strong> after <strong>$part</strong>.";
}
}
break;
case ",":
// Must have a $redcap field query after, has to be part of in_array function call
if ($index != sizeof($parts) - 1)
{
$previous = $parts[$index - 1];
$previous_2 = $parts[$index - 2];
$previous_3 = $parts[$index - 3];
if ($previous_3 !== "in_array" || $previous_2 !== "(" || $previous !== "''")
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Improper use of <strong>,</strong> in syntax. Are you trying to call in_array()?";
}
$next_part = $parts[$index + 1];
if ($next_part !== "\$redcap")
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num]. Invalid <strong>$next_part</strong> after <strong>,</strong>.";
}
}
else
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] <strong>$part</strong> cannot be end of syntax";
}
break;
case "in_array":
// Must have a ( after
if ($index != sizeof($parts) - 1)
{
$next_part = $parts[$index + 1];
if ($next_part !== "(")
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Malformed <strong>in_array()</strong> function.";
}
}
else
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Malformed <strong>in_array()</strong> function.";
}
break;
case "else":
case "/if":
// Must be the only clause in syntax
if ($index != sizeof($parts) - 1)
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Mal-formed <strong>$part</strong> clause. Must be of form <strong>{{$part}}</strong>.";
}
break;
default:
// If it's a number, must have ) or logical operator after, if not last item in syntax
if (is_numeric($part))
{
if ($index != sizeof($parts) - 1)
{
$next_part = $parts[$index + 1];
if (!empty($next_part) && $next_part !== ")" && !in_array($next_part, $this->logical_operators))
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Invalid <strong>$next_part</strong> after <strong>$part</strong>.";
}
}
}
// Check if it's a string
else if (!empty($part) &&
$part[0] != "'" &&
$part[0] != "\"" &&
$part[strlen($part) - 1] != "'" &&
$part[strlen($part) - 1] != "\"" &&
!$this->isValidFieldOrEvent($part))
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] <strong>$part</strong> is not a valid event/field/syntax in this project";
}
break;
}
}
}
return $errors;
}
/**
* Validate if statements, by checking the following:
*
* - If statements have matching opening and closing statements.
* - Elseifs are associated with an if statement.
* - Every if statement has at most one else clause.
*
* @access private
* @param Array $lines An array of lines within template.
* @return Array An array of errors, with their line numbers appended to indicate where it occured.
*/
private function validateIfStatements($lines)
{
$errors = array();
// Find all occurences of opening {if, {/if}
$opening_ifs = array();
$closing_ifs = array();
foreach($lines as $index => $line)
{
// Could be multiple statements on same line
$last_pos = 0;
while(($last_pos = strpos($line, '{if', $last_pos)) !== FALSE)
{
$line_num = $index + 1;
if ($line[$last_pos + 3] !== " ")
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Mal-formed if/elseif statement. Need a space after 'if'";
}
$opening_ifs[] = array(
"line_num" => $line_num,
"line_pos" => $last_pos
);
$last_pos++;
}
$last_pos = 0;
while(($last_pos = strpos($line, '{/if}', $last_pos)) !== FALSE)
{
$closing_ifs[] = array(
"line_num" => $index + 1,
"line_pos" => $last_pos
);
$last_pos++;
}
}
$num_opening_ifs = sizeof($opening_ifs);
$num_closing_ifs = sizeof($closing_ifs);
$opening_ifs_copy = array_reverse($opening_ifs);
$closing_ifs_copy = array_reverse($closing_ifs);
foreach($opening_ifs_copy as $index => $opening_if)
{
$key = null;
for ($i = 0; $i < sizeof($closing_ifs_copy); $i++)
{
$closing_if = $closing_ifs_copy[$i];
if (($opening_if["line_num"] < $closing_if["line_num"]) ||
($opening_if["line_num"] == $closing_if["line_num"] && $opening_if["line_pos"] < $closing_if["line_pos"]))
{
$key = $i;
break;
}
}
if (!is_null($key))
{
unset($closing_ifs_copy[$key]);
$closing_ifs_copy = array_values($closing_ifs_copy);
}
else
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [" . $opening_if["line_num"] . "] Missing {/if}";
}
}
foreach($closing_ifs_copy as $closing_if)
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [" . $closing_if["line_num"] . "] Extra {/if}";
}
if (empty($errors))
{
// Find all occurences of {elseif, {else}
$elseifs = array();
$elses = array();
foreach($lines as $index => $line)
{
$last_pos = 0;
while(($last_pos = strpos($line, '{elseif', $last_pos)) !== FALSE)
{
$line_num = $index + 1;
if ($line[$last_pos + 7] !== " ")
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Mal-formed if/elseif statement. Need a space after 'elseif'";
}
$elseifs[] = array(
"line_num" => $line_num,
"line_pos" => $last_pos
);
$last_pos++;
}
$last_pos = 0;
while(($last_pos = strpos($line, '{else}', $last_pos)) !== FALSE)
{
$elses[] = array(
"line_num" => $index + 1,
"line_pos" => $last_pos
);
$last_pos++;
}
}
$num_elses = sizeof($elses);
$num_elseifs = sizeof($elseifs);
// Check that every elseif clause is associated with an if statement.
foreach($elseifs as $elseif)
{
$line_num = $elseif["line_num"];
$line_pos = $elseif["line_pos"];
if ($num_opening_ifs == 0)
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Every {elseif} clause must be associated with an {if} statement";
}
else
{
for ($i = 0; $i < $num_opening_ifs; $i++)
{
$opening_if = $opening_ifs[$i];
$closing_if = $closing_ifs[$i];
// Check if there's an else clause between the elseif and the opening if
$arr = array_filter($elses, function($v) use ($opening_if, $closing_if, $line_num, $line_pos) {
$v_line_num = $v["line_num"];
$v_line_pos = $v["line_pos"];
if ($opening_if["line_num"] < $v_line_num && $v_line_num < $closing_if["line_num"] && $v_line_num < $line_num)
{
return true;
}
else if (($opening_if["line_num"] == $v_line_num && $v_line_num == $closing_if["line_num"] && $v_line_num == $line_num) ||
($opening_if["line_num"] == $v_line_num && $v_line_num < $closing_if["line_num"] && $v_line_num == $line_num) ||
($opening_if["line_num"] < $v_line_num && $v_line_num == $closing_if["line_num"] && $v_line_num == $line_num) ||
($opening_if["line_num"] < $v_line_num && $v_line_num < $closing_if["line_num"] && $v_line_num == $line_num))
{
return $v_line_pos < $line_pos;
}
});
if (($opening_if["line_num"] < $line_num && $line_num < $closing_if["line_num"] && empty($arr)) ||
($opening_if["line_num"] == $line_num && $line_num == $closing_if["line_num"] && $opening_if["line_pos"] < $line_pos && $line_pos < $closing_if["line_pos"] && empty($arr)) ||
($opening_if["line_num"] == $line_num && $line_num < $closing_if["line_num"] && $opening_if["line_pos"] < $line_pos && empty($arr)) ||
($opening_if["line_num"] < $line_num && $line_num == $closing_if["line_num"] && $line_pos < $closing_if["line_pos"] && empty($arr)))
{
break;
}
else if ($i == $num_opening_ifs - 1)
{
$errors[] = "<b>ERROR</b> [EDITOR] LINE [$line_num] Every {elseif} clause must be associated with an {if} statement";
}
}
}
}
// Check that every if statement has at most one else clause, and that every else clause is associated with an if statement.
foreach($elses as $index => $else)
{
$key = null;
$line_num = $else["line_num"];
$line_pos = $else["line_pos"];
for ($i = 0; $i < sizeof($opening_ifs); $i++)
{