-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdform.php
More file actions
1284 lines (1133 loc) · 63.7 KB
/
Copy pathmdform.php
File metadata and controls
1284 lines (1133 loc) · 63.7 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
// MDForm - Markdown Form Extension for Datenstrom Yellow
// https://github.com/goehte/yellow-mdform/
// This extension allows creating HTML forms from markdown-formatted files.
// NOTE: All v0.0.x are Alpha Versions
// Alpha Revision v0.0.6.4 - Date 29.04.2026 - Bug-fix & Enhancement: Error messages translated and new toggle feature [OFF/ON] to have preselected toggle switch
// Alpha Revision v0.0.7.2 - Date 30.04.2026 - Bug-fix & Styling
// Alpha Revision v0.0.8.1 - Date 30.04.2026 - Added Cookie to avoid resubmits
// Alpha Revision v0.0.8.2 - Date 01.05.2026 - Mail Header on Page YAML added
// Alpha Revision v0.0.8.3 - Date 05.05.2026 - E-Mail sending updated
// Alpha Revision v0.0.9.1 - Date 06.05.2026 - Image CAPTCHA added, pre-fill HTML during resubmit added
// Alpha Revision v0.0.9.2 - Date 06.05.2026 - Hidden fields added, output added
// Alpha Revision v0.0.9.3 - Date 13.05.2026 - Special function {_cc_email} added
// Alpha Revision v0.0.9.4 - Date 13.05.2026 - Special function {validate_email} added
class YellowMdform {
// Extension version number
const VERSION = "0.0.9";
// Reference to Yellow CMS API instance
public $yellow;
// Called when the extension loads to set up defaults
public function onLoad($yellow) {
$this->yellow = $yellow;
// Directory for storing form definition files
$this->yellow->system->setDefault("MDFormDirectory", "media/forms/");
// Directory for storing CSV output files
$this->yellow->system->setDefault("MDFormDirectoryCSVOutput", "media/tables/forms/");
// Directory for rate limiting files
$this->yellow->system->setDefault("MDFormRateLimitDirectory", "cache/mdform/ratelimit/");
// Passkey for CSRF hash generation
$saltPasskey = $this->yellow->system->get("coreSitename");
$this->yellow->system->setDefault("MDFormHashSaltPasskey", $saltPasskey);
// Default email and restriction settings
$this->yellow->system->setDefault("MDFormEmailRestriction", "0");
$this->yellow->system->setDefault("MDFormLinkRestriction", "0");
$this->yellow->system->setDefault("MDFormResubmitCookie", "0");
$this->yellow->system->setDefault("MDFormAllowedExtensions", "mdf, md, form");
$this->yellow->system->setDefault("MDFormStyleSheet", "mdform.css");
$this->yellow->system->setDefault("MDFormJavaScript", "mdform.js");
$this->yellow->system->setDefault("MDFormActivateJavaScript", "1"); // JS needed only needed for live output: {output:name}.
$this->yellow->system->setDefault("MDFormKeyValueSeperator", ": "); // How the key name and the value get shown in HTML and Email output.
// Language translations for UI messages
$this->yellow->language->setDefaults(array(
"Language: en",
"MDFormSubmitBtn: Submit",
"MDFormMandatory: *",
"MDFormSubmitted: <strong> ✅ Form successfully submitted.</strong>",
"MDFormHTMLOutput: You have provided the following data:",
"MDFormCSVSaved: Success! Data saved.",
"MDFormEmailSend: Success! Data send.",
"MDFormEmailCopySend: Copy of this from data send to: @sendermail.",
"MDFormEmailValidateSend: Please validate your data, check your mailbox @sendermail and click the validation link.",
"MDFormValidateSuccess: <strong> ✅ Success! Data validated.</strong>",
"MDFormValidateError: <div class=\"important\">Unable to validate data.</div>",
"MDFormMailHeader: Mail Header",
"MDFormMailFooter: Mail Footer",
"MDFormMailValidateText: Please click the Link below to validate your data:",
"MDFormNotifySendCopy: Please email me a copy of this form.",
"MDFormCaptchaFormTitle: <strong>Security query:</strong>",
"MDFormCaptchaForm: Please enter the numerical CAPTCHA from the picture in the form field.",
"MDFormCaptchaInvalid: <div class=\"important\">The entered CAPTCHA have been incorrect.</div>",
"MDFormWarningRateLimit: <div class=\"important\">Warning: Please wait a moment before submitting again.</div>",
"MDFormWarningResubmit: <div class=\"important\">Warning: Please do not resubmit successful sent forms.</div>",
"MDFormErrorMdfFileAccess: <div class=\"important\">[mdform] Error: Form file not found or access denied.</div>",
"MDFormErrorMdfFileNotFound: <div class=\"important\">[mdform] Error: File not found.</div>",
"MDFormErrorTokenInvalid: <div class=\"important\">[mdform] Error: Security token invalid or expired. Please refresh page.</div>",
"MDFormErrorCsvFileAccess: <div class=\"important\">[mdform] Error: Cannot open CSV file for writing.</div>",
"MDFormErrorEmailSetting: <div class=\"important\">[mdform] Error: Email address settings not valid.</div>",
"MDFormErrorEmailService: <div class=\"important\">[mdform] Error: Email not sent</div>",
"Language: de",
"MDFormSubmitBtn: Senden",
"MDFormMandatory: *",
"MDFormSubmitted: <strong> ✅ Formular erfolgreich abgesendet.</strong>",
"MDFormHTMLOutput: Sie haben die folgenden Daten übermittelt:",
"MDFormCSVSaved: Daten erfolgreich gespeichert.",
"MDFormEmailSent: Daten erfolgreich gesendet.",
"MDFormEmailCopySend: Eine Kopie der Formulardaten wurde gesendet an: @sendermail.",
"MDFormEmailValidateSend: Bitte validieren Sie die Daten, prüfen Sie Ihre Mailbox @sendermail und klicken Sie den Validierungslink.",
"MDFormValidateSuccess: <strong> ✅ Sie haben die Daten erfolgreich validiert.</strong>",
"MDFormValidateError: <div class=\"important\">Es war nicht möglich die Daten zu validieren.</div>",
"MDFormMailHeader: Mail Header",
"MDFormMailFooter: Mail Footer",
"MDFormMailValidateText: Bitte klicken Sie den Link unten um die Daten zu validieren:",
"MDFormNotifySendCopy: Bitte senden Sie mir eine Kopie dieses Formulars per E-Mail.",
"MDFormCaptchaFormTitle: <strong>Sicherheitsabfrage:</strong>",
"MDFormCaptchaForm: <small>Bitte geben sie das numerische CAPTCHA von dem Bild in das Formularfeld ein.</small>",
"MDFormCaptchaInvalid: <div class=\"important\">Das eingegebene CAPTCHA ist nicht korrekt.</div>",
"MDFormWarningRateLimit: <div class=\"important\">Warnung: Bitte warten Sie einen Moment, bevor Sie das Formular erneut absenden.</div>",
"MDFormWarningResubmit: <div class=\"important\">Warnung: Bitte senden Sie erfolgreich abgesendete Formulare nicht mehrfach ab..</div>",
"MDFormErrorMdfFileAccess: <div class=\"important\">[mdform] Fehler: Formulardatei nicht gefunden oder Zugriff verweigert.</div>",
"MDFormErrorMdfFileNotFound: <div class=\"important\">[mdform] Fehler: Datei nicht gefunden.</div>",
"MDFormErrorTokenInvalid: <div class=\"important\">[mdform] Fehler: Sicherheits-Token ungültig oder abgelaufen. Bitte Seite aktualisieren.</div>",
"MDFormErrorCsvFileAccess: <div class=\"important\">[mdform] Fehler: CSV-Datei konnte nicht zum Schreiben geöffnet werden.</div>",
"MDFormErrorEmailSetting: <div class=\"important\">[mdform] Fehler: E-Mail-Einstellungen sind ungültig.</div>",
"MDFormErrorEmailService: <div class=\"important\">[mdform] Fehler: E-Mail konnte nicht gesendet werden.</div>",
));
}
// Page YAML Options
// MDFormAutocomplete: OFF or ON
// MDFormMailHeader:
// MDFormMailFooter:
// Main entry point for form element parsing
public function onParseContentElement($page, $name, $text, $attributes, $type) {
$output = null;
// Only process elements named "mdform"
if ($name == "mdform" && ($type == "block" || $type == "inline")) {
list($fileName, $dispatchFormat, $formOptions) = $this->yellow->toolbox->getTextArguments($text);
$filePath = $this->yellow->system->get("MDFormDirectory");
// Strip all path components to prevent directory traversal
$fileName = basename(trim($fileName));
// Verify the resolved path is within the allowed base directory
$fullPath = realpath($filePath . $fileName);
$basePath = realpath($filePath);
// Validate path exists and is within base directory
if ($fullPath === false || strpos($fullPath, $basePath) !== 0) {
$output .= "<p>" . $this->yellow->language->getText("MDFormErrorMdfFileAccess") . "</p>\n ";
return $output;
}
// Process form if filename provided
if (!empty($fileName)) {
// Check if the form file exists on disk
if (file_exists($fullPath)) {
// Check if this is a form submission request
#if (($page->getRequest("mdform-status") === "send") && ($this->checkHashString($page->getRequest("mdform-file"), $fileName))) {
if (($page->getRequest("mdform-status") === "send") && ($this->checkHashString($page->getRequest("mdform-file"), $fileName))) {
$output .= $this->processSend($filePath, $fileName, $dispatchFormat, $formOptions);
}
// Validate Form
#elseif ($page->getRequest("mdform-status") === "validate") {
elseif (($page->getRequest("mdform-status") === "validate") && ($this->checkHashString(urldecode($page->getRequest("mdform-file")), $fileName))) {
#echo "Validate: " . urldecode($page->getRequest("mdform-file")) . " - ". $this->checkHashString(urldecode($page->getRequest("mdform-file")), $fileName);
$output = $this->processValidateCSV($fileName, urldecode($page->getRequest("mdform-hash")));
}
// Render the form for standard display
else {
$output = $this->getFormHTML($filePath, $fileName, $formOptions, false);
}
}
// Handle case where file is missing
else {
$output .= "<p>" . $this->yellow->language->getText("MDFormErrorMdfFileNotFound") . "</p>\n ";
}
}
}
return $output;
}
// Load custom style sheet CSS into the page header
public function onParsePageExtra($page, $name) {
$output = null;
// Check if header extra is being parsed
if ($name == "header") {
$assetLocation = $this->yellow->system->get("coreServerBase") . $this->yellow->system->get("coreAssetLocation");
// CSS
$style = $this->yellow->system->get("MDFormStyleSheet");
// Append link tag if stylesheet is defined
if ($style != "") {
$output .= "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"{$assetLocation}{$style}\" />\n";
}
// JS
$jsFile = $this->yellow->system->get("MDFormJavaScript");
// Add JavaScript libary
if (($jsFile != "") && ($this->yellow->system->get("MDFormActivateJavaScript"))) {
$output .= "<script src=\"{$assetLocation}{$jsFile}\" defer></script>\n";
}
}
return $output;
}
// Loads and validates form definition files
private function getFormHTML($filePath, $fileName, $formOptions, $prefillValues = false) {
$fileType = $this->yellow->toolbox->getFileType($filePath . $fileName);
$allowed = $this->yellow->system->get("MDFormAllowedExtensions");
$allowed = array_map('trim', explode(',', $allowed));
// Only process allowed file extensions
if (is_array($allowed) && in_array($fileType, $allowed)) {
$formData = $this->readMarkdown(file_get_contents($filePath . $fileName));
return $this->generateHTMLForm($formData, $fileName, $formOptions, $prefillValues);
}
return false;
}
// Parses markdown-formatted form definition into a structured array
private function readMarkdown($fileContent) {
$lines = explode("\n", $fileContent);
$formData = [];
$counters = ['radio' => 0, 'check' => 0, 'toggle' => 0, 'input' => 0];
// Process form markdown line by line
foreach ($lines as $line) {
$line = trim($line);
// Skip empty lines
if (empty($line)) {
continue;
}
$isMandatory = false;
$cleanLine = $line;
// Only strip trailing '*' if line contains form brackets
if (strpos($line, '[') !== false && strpos($line, ']') !== false) {
// Check if last character is mandatory marker
if (substr($line, -1) === '*') {
$isMandatory = true;
$cleanLine = rtrim($line, '*');
}
}
$labelPrefix = "";
$elementBody = $cleanLine;
// Output references: Label 1: {output:Name}
if (preg_match('/^(?:(.+?):\s*)?\{output:(.+)\}$/', $cleanLine, $outputMatches)) {
// If the label part matched, it's in index 1. Otherwise, it's an empty string.
$labelPrefix = !empty($outputMatches[1]) ? trim($outputMatches[1]) : '';
$outputName = trim($outputMatches[2]);
$entry['type'] = 'output';
$entry['label'] = $labelPrefix;
$entry['for'] = $this->sanitizeFromName($outputName);
$entry['name'] = 'output_' . $entry['for'];
$formData[] = $entry;
continue;
}
// Extract label and body for form fields
if (preg_match('/^(.+?):\s*(\[.*\].*)$/', $cleanLine, $matches)) {
$labelPrefix = trim($matches[1]);
$elementBody = trim($matches[2]);
}
#$entry = ['label' => $labelPrefix, 'required' => $isMandatory, 'name' => '', 'type' => '', 'options' => [], 'placeholder' => '', 'autocomplete' => '', 'min' => '', 'max' => ''];
$entry = ['label' => $labelPrefix, 'required' => $isMandatory, 'name' => '', 'type' => '', 'options' => [], 'placeholder' => '', 'autocomplete' => ''];
// Extract special attributes if present
if (preg_match('/^(.*)\{(\_[a-z0-9\-\_]+)\}\s*$/i', $elementBody, $attrMatches)) {
$elementBody = trim($attrMatches[1]);
$entry['special'] = strtolower(trim($attrMatches[2]));
}
// Extract autocomplete attributes if present
if (preg_match('/^(.*)\{([a-z0-9\-]+)\}\s*$/i', $elementBody, $attrMatches)) {
$elementBody = trim($attrMatches[1]);
$entry['autocomplete'] = strtolower(trim($attrMatches[2]));
}
// Select dropdown elements
if (preg_match('/^\[(.*?)\s*▼\s*;\s*(.*)\]$/u', $elementBody, $matches)) {
$entry['type'] = 'select';
$entry['name'] = $this->sanitizeFromName($labelPrefix ?: "dropdown_" . (++$counters['input']));
$entry['placeholder'] = trim($matches[1]);
$entry['options'] = array_map('trim', explode(',', $matches[2]));
}
// Radio button groups
elseif (preg_match('/^\[(\(\s*[xX ]?\s*\).*?)\]$/', $elementBody, $matches)) {
$entry['type'] = 'radio';
$entry['name'] = $this->sanitizeFromName($labelPrefix ?: "radio_group_" . (++$counters['radio']));
$innerContent = $matches[1];
$parts = preg_split('/,\s*(?=\()/', $innerContent);
// Parse each radio option
foreach ($parts as $p) {
// Match the (x) or ( ) part and label
if (preg_match('/\(\s*([xX ]?)\s*\)(.*)/', trim($p), $optionMatches)) {
$entry['options'][] = ['value' => trim($optionMatches[2]), 'checked' => (strtolower(trim($optionMatches[1])) === 'x')];
}
}
}
// Checkbox groups
elseif (preg_match('/^\[(\[\s*[xX ]?\s*\].*?)\]$/', $elementBody, $matches)) {
$entry['type'] = 'checkbox';
$entry['name'] = $this->sanitizeFromName($labelPrefix ?: "check_group_" . (++$counters['check']));
$innerContent = $matches[1];
$parts = preg_split('/,\s*(?=\[)/', $innerContent);
// Parse each checkbox option
foreach ($parts as $p) {
// Match the [x] or [ ] part and label
if (preg_match('/\[\s*([xX ]?)\s*\](.*)/', trim($p), $optionMatches)) {
$entry['options'][] = ['value' => trim($optionMatches[2]), 'checked' => (strtolower(trim($optionMatches[1])) === 'x')];
}
}
}
// Toggle inputs [ON/OFF]
elseif (preg_match('/^\[(?:(.*?):\s*)?ON\/OFF\]$/i', $elementBody, $matches)) {
$entry['type'] = 'toggle';
$toggleName = isset($matches[1]) ? trim($matches[1]) : "";
$entry['name'] = $this->sanitizeFromName($toggleName ?: $labelPrefix ?: "toggle_" . (++$counters['toggle']));
}
// Toggle inputs [OFF/ON]
elseif (preg_match('/^\[(?:(.*?):\s*)?OFF\/ON\]$/i', $elementBody, $matches)) {
$entry['type'] = 'toggle';
$toggleName = isset($matches[1]) ? trim($matches[1]) : "";
$entry['name'] = $this->sanitizeFromName($toggleName ?: $labelPrefix ?: "toggle_" . (++$counters['toggle']));
$entry['options'][] = ['checked' => true];
}
// Date inputs
elseif (preg_match('/^\[DD\/MM\/YYYY(?:;(\d{4}-\d{2}-\d{2}|TODAY)\.\.(\d{4}-\d{2}-\d{2}|TODAY))?\]$/', $elementBody, $dateMatches)) {
$entry['type'] = 'date';
$entry['name'] = $this->sanitizeFromName($labelPrefix ?: "date_" . (++$counters['input']));
$dateMin = isset($dateMatches[1]) ? $dateMatches[1] : null;
$dateMax = isset($dateMatches[2]) ? $dateMatches[2] : null;
// Convert TODAY keyword to current date
if ($dateMin === 'TODAY') {
$dateMin = date('Y-m-d');
}
// Convert TODAY keyword to current date
if ($dateMax === 'TODAY') {
$dateMax = date('Y-m-d');
}
$entry['min'] = $dateMin;
$entry['max'] = $dateMax;
}
// Logic for Number/Text/Textarea detection
elseif (preg_match('/^\[(.+)\]$/', $elementBody, $matches)) {
$rawContent = trim($matches[1]);
// Textarea check (prioritize to avoid misclassifying "..." as a range)
if (strpos($rawContent, '...') !== false) {
$dotsCount = strlen($rawContent) - strlen(rtrim($rawContent, '.'));
$entry['type'] = 'textarea';
$entry['placeholder'] = trim($rawContent, '. ');
$entry['name'] = $this->sanitizeFromName($labelPrefix ?: $entry['placeholder'] ?: "textarea_" . (++$counters['input']));
$entry['min'] = ($dotsCount >= 3) ? $dotsCount : 2;
}
// Number input: Check for digits, ranges, or floats
// Number input: [5], [5;1..10], [;1..10], [1..10], [5.0], [3.14;0..10]
// Number input: [Placeholder], [Placeholder;Min..Max], [;Min..Max]
elseif (preg_match('/^\[(?:(\d+(?:\.\d+)?))?(?:;(\d+(?:\.\d+)?)\.\.(\d+(?:\.\d+)?))?\]$/', $elementBody, $numberMatches)) {
$entry['type'] = 'number';
$entry['placeholder'] = !empty($numberMatches[1]) ? $numberMatches[1] : null;
$entry['min'] = $numberMatches[2] ?? null;
$entry['max'] = $numberMatches[3] ?? null;
$step = "1";
// Calculate step based on decimal precision
if (preg_match('/\.\d+/', $elementBody)) {
$decimals = [];
// Check placeholder for decimals
if ($entry['placeholder'] !== null && strpos($entry['placeholder'], '.') !== false) {
$decimals[] = strlen(explode('.', $entry['placeholder'])[1]);
}
// Check min value for decimals
if ($entry['min'] !== null && strpos($entry['min'], '.') !== false) {
$decimals[] = strlen(explode('.', $entry['min'])[1]);
}
// Check max value for decimals
if ($entry['max'] !== null && strpos($entry['max'], '.') !== false) {
$decimals[] = strlen(explode('.', $entry['max'])[1]);
}
// Set step to required precision
if (!empty($decimals)) {
$maxDecimals = max($decimals);
$step = "0." . str_repeat("0", $maxDecimals - 1) . "1";
}
}
$entry['step'] = $step;
$entry['name'] = $this->sanitizeFromName($labelPrefix ?: "number_" . (++$counters['input']));
}
// Hidden inputs: [~Name: Value]
elseif (preg_match('/^\[(~)(.+):\s*(.+)\]$/', $elementBody, $matches)) {
$entry['type'] = 'hidden';
$entry['name'] = $this->sanitizeFromName(trim($matches[2]));
$entry['value'] = trim($matches[3]);
$entry['required'] = false; // Hidden fields are never required
}
// Default to standard text field
else {
$entry['type'] = 'text';
$entry['placeholder'] = trim($rawContent, '. ');
$entry['name'] = $this->sanitizeFromName($labelPrefix ?: $entry['placeholder'] ?: "text_" . (++$counters['input']));
}
}
// Handle standalone markdown text lines
elseif (empty($labelPrefix) && !preg_match('/^\[.*\]$/', $cleanLine)) {
$entry['type'] = 'markdown';
$entry['content'] = $cleanLine;
$entry['name'] = 'markdown_' . count($formData);
}
// Add valid field definitions to the list
if ($entry['type']) {
$formData[] = $entry;
}
}
return $formData;
}
// Converts parsed form structure into HTML markup
private function generateHTMLForm($formData, $fileName, $formOptions, $prefillValues) {
$fileHash = $this->createHashString($fileName);
$autocompleteValue = strtolower($this->yellow->page->get("MDFormAutocomplete"));
$output = "<div class=\"mdform-container\">\n <form id=\"" . htmlspecialchars($fileHash) . "\" method=\"post\"";
// Allow to turn off autocomplete
if (in_array($autocompleteValue, ['on', 'off'])) {
$output .= " autocomplete=\"" . $autocompleteValue . "\"";
}
$output .= ">\n";
$output .= " <input type=\"hidden\" name=\"mdform-file\" value=\"" . htmlspecialchars($fileHash) . "\">\n";
#var_dump($formData); // Just for data structure debugging purpose
// Loop through each field to generate HTML
foreach ($formData as $field) {
// Render non-input markdown elements
if ($field['type'] === 'markdown') {
$parsedContent = trim($this->parseText($this->yellow->page, $field['content']));
$output .= " <div class=\"mdform-markdown\">$parsedContent</div>\n";
continue;
}
$req = $field['required'] ? "required" : "";
$star = $field['required'] ? $this->yellow->language->getText("MDFormMandatory") : "";
$output .= " <p class=\"mdform-group\">\n";
// Add label if it exists
if ($field['label']) {
$output .= " <strong>{$field['label']}: $star</strong><br />\n";
}
$htmlType = $field['type'];
// Apply specific autocomplete-based input types
if ($field['autocomplete'] === 'email' && $field['type'] === 'text') {
$htmlType = 'email';
}
// Map tel type
elseif ($field['autocomplete'] === 'tel' && $field['type'] === 'text') {
$htmlType = 'tel';
}
// Map url type
elseif ($field['autocomplete'] === 'url' && $field['type'] === 'text') {
$htmlType = 'url';
}
// Map password type
elseif ($field['autocomplete'] === 'password' && $field['type'] === 'text') {
$htmlType = 'password';
}
// Generate field markup based on type
switch ($field['type']) {
// Render output elements
case 'output':
$output .= " <output name=\"{$field['name']}\" id=\"{$field['name']}\" for=\"{$field['for']}\"></output>\n";
break;
// Render select dropdowns
case 'select':
$value = ($prefillValues === true) ? $this->yellow->page->getRequestHtml($field['name']) : "";
$output .= " <select name=\"{$field['name']}\" id=\"{$field['name']}\" class=\"form-control\" $req";
// Append autocomplete attribute
if ($field['autocomplete']) {
$output .= " autocomplete=\"{$field['autocomplete']}\"";
}
$output .= " style=\"width:100%\" >\n";
$output .= " <option value=\"\">{$field['placeholder']}</option>\n";
// Add dropdown options
foreach ($field['options'] as $option) {
$output .= " <option value=\"$option\"" . (($option === $value) ? " selected": "") . ">$option</option>\n";
}
$output .= " </select>\n";
break;
// Render radio buttons
case 'radio':
$value = ($prefillValues === true) ? $this->yellow->page->getRequest($field['name']) : "";
// Loop through radio options
foreach ($field['options'] as $option) {
$output .= " <label><input type=\"radio\" name=\"{$field['name']}\" class=\"form-control\" value=\"{$option['value']}\" $req";
// Add autocomplete
if ($field['autocomplete']) {
$output .= " autocomplete=\"{$field['autocomplete']}\"";
}
// Set checked state
if ($option['checked'] && ($prefillValues !== true)) {
$output .= " checked";
}
// Set checked state during resubmit
if ($prefillValues === true && ($value === $option['value'])) {
$output .= " checked";
}
$output .= "> {$option['value']}</label> \n";
}
break;
// Render checkboxes
case 'checkbox':
$value = ($prefillValues === true) ? $this->yellow->page->getRequest($field['name']) : "";
// Loop through checkbox options
foreach ($field['options'] as $option) {
$output .= " <label><input type=\"checkbox\" name=\"{$field['name']}[]\" class=\"form-control\" value=\"{$option['value']}\" $req";
// Add autocomplete
if ($field['autocomplete']) {
$output .= " autocomplete=\"{$field['autocomplete']}\"";
}
// Set checked state
if ($option['checked'] && ($prefillValues !== true)) {
$output .= " checked";
}
// Set checked state during resubmit
if ($prefillValues === true) {
foreach ($value as $val) {
($val === $option['value']) ? $output .= " checked" : $output .= "";
}
}
$output .= "> {$option['value']}</label> \n";
}
break;
// Render toggle switch
case 'toggle':
$value = ($prefillValues === true) ? $this->yellow->page->getRequest($field['name']) : "";
$output .= " <input type=\"checkbox\" name=\"{$field['name']}\" id=\"{$field['name']}\" class=\"form-control switch\" $req value=\"ON\"";
// Add autocomplete
if ($field['autocomplete']) {
$output .= " autocomplete=\"{$field['autocomplete']}\"";
}
// Set checked state
if (isset($field['options'][0]['checked']) && ($prefillValues !== true)) {
$output .= " checked";
}
elseif ($prefillValues === true && $value === "ON") {
$output .= " checked";
}
$output .= ">\n";
$output .= " <label class=\"switch\" for=\"{$field['name']}\">";
$toggleText = $field['name'];
// Format toggle text label
if (strpos($toggleText, "toggle_") !== 0) {
$toggleText = preg_replace('/_/', ' ', $toggleText);
$output .= "$toggleText $star\n";
}
$output .= "</label>\n";
break;
// Render date input
case 'date':
$value = ($prefillValues === true) ? $this->yellow->page->getRequestHtml($field['name']) : "";
$output .= " <input type=\"date\" name=\"{$field['name']}\" id=\"{$field['name']}\" value=\"$value\" class=\"form-control\"";
// Add min date limit
if (isset($field['min']) && $field['min'] !== null) {
$output .= " min=\"" . htmlspecialchars($field['min']) . "\"";
}
// Add max date limit
if (isset($field['max']) && $field['max'] !== null) {
$output .= " max=\"" . htmlspecialchars($field['max']) . "\"";
}
$output .= " $req style=\"width:100%\"";
// Add autocomplete
if ($field['autocomplete']) {
$output .= " autocomplete=\"{$field['autocomplete']}\"";
}
$output .= ">\n";
break;
// Render number input
case 'number':
$value = ($prefillValues === true) ? $this->yellow->page->getRequestHtml($field['name']) : "";
$output .= " <input type=\"number\" name=\"{$field['name']}\" id=\"{$field['name']}\" value=\"$value\" class=\"form-control\"";
// Add placeholder
if (!empty($field['placeholder'])) {
$output .= " placeholder=\"" . htmlspecialchars($field['placeholder']) . "\"";
}
// Add min number limit
if (isset($field['min']) && $field['min'] !== null) {
$output .= " min=\"" . htmlspecialchars($field['min']) . "\"";
}
// Add max number limit
if (isset($field['max']) && $field['max'] !== null) {
$output .= " max=\"" . htmlspecialchars($field['max']) . "\"";
}
// Add number step precision
if (isset($field['step']) && $field['step'] !== null) {
$output .= " step=\"" . htmlspecialchars($field['step']) . "\"";
}
$output .= " $req style=\"width:100%\"";
// Add autocomplete
if ($field['autocomplete']) {
$output .= " autocomplete=\"{$field['autocomplete']}\"";
}
$output .= ">\n";
break;
// Render textarea
case 'textarea':
$value = ($prefillValues === true) ? $this->yellow->page->getRequestHtml($field['name']) : "";
$output .= " <textarea name=\"{$field['name']}\" id=\"{$field['name']}\" class=\"form-control\" placeholder=\"{$field['placeholder']}\" $req style=\"width:100%\"";
// Add autocomplete
if ($field['autocomplete']) {
$output .= " autocomplete=\"{$field['autocomplete']}\"";
}
$output .= " rows=\"{$field['min']}\">$value</textarea>\n";
break;
// Render standard text inputs
case 'text':
$value = ($prefillValues === true) ? $this->yellow->page->getRequestHtml($field['name']) : "";
$output .= " <label><input type=\"$htmlType\" name=\"{$field['name']}\" id=\"{$field['name']}\" value=\"$value\" class=\"form-control\" placeholder=\"{$field['placeholder']}\" $req style=\"width:100%\"";
// Add autocomplete
if ($field['autocomplete']) {
$output .= " autocomplete=\"{$field['autocomplete']}\"";
}
$output .= "></label>\n";
break;
// Render hidden inputs
case 'hidden':
$value = isset($field['value']) ? htmlspecialchars($field['value']) : '';
$output .= " <input type=\"hidden\" name=\"{$field['name']}\" id=\"{$field['name']}\" value=\"$value\">\n";
break;
}
$output .= " </p>\n";
}
// Create CAPTCHA block
if (stripos($formOptions, "captcha") !== false) {
$captcha = $this->getRandomCaptchaString(); // Create an random captcha sting
$output .= " <blockquote><p>" . $this->yellow->language->getText("MDFormCaptchaFormTitle") . "<br />" . $this->getCaptcha($captcha) . "<br />↳<input type=\"number\" name=\"captcha\" placeholder=\"CAPTCHA\" pattern=\"[0-9]{6}\" size=\"6\" step=\"1\" min=\"0\" max=\"999999\" maxlength=\"6\"><br />";
$output .= $this->yellow->language->getText("MDFormCaptchaForm") . "</p>\n</blockquote>\n";
$output .= " <input type=\"hidden\" name=\"captcha_hash\" value=\"".$this->createCaptchaHash($captcha)."\" />\n";
}
$csrfToken = $this->createHashString($this->yellow->system->get("MDFormHashSaltPasskey"));
$output .= " <input type=\"hidden\" name=\"mdform-hash\" value=\"" . htmlspecialchars($csrfToken) . "\" />\n";
$output .= " <input type=\"hidden\" name=\"mdform-referer\" value=\"" . $this->yellow->toolbox->getServer("HTTP_REFERER") . "\" />\n";
$output .= " <input type=\"hidden\" name=\"mdform-status\" value=\"send\" />\n";
$output .= " <p><button type=\"submit\" class=\"btn\">" . $this->yellow->language->getText("MDFormSubmitBtn") . "</button> </p>\n </form>\n</div>\n";
return $output;
}
// Parses markdown text using the system's default parser
private function parseText($page, $text, $singleLine = true) {
$parser = $this->yellow->extension->get($this->yellow->system->get("parser"));
$output = $parser->onParseContentRaw($page, $text);
// Clean up paragraph tags for inline display
if ($singleLine) {
// Check for surrounding paragraph tags
if (substr($output, 0, 3) == "<p>" && substr($output, -5) == "</p>\n") {
$output = substr($output, 3, -5);
}
}
return $output;
}
// Extracts ONLY data-holding fields from MDF file for backend processing
private function getFormFileMetadata($fileContent) {
$formData = $this->readMarkdown($fileContent);
$formStructure = [];
// Loop through fields to extract names and requirements
foreach ($formData as $field) {
// SKIP display-only markdown elements
if (!($field['type'] === 'markdown')) {
// Define the base structure
$structure = [
'name' => $field['name'],
'required' => $field['required'],
'type' => $field['type'],
'autocomplete' => $field['autocomplete'],
];
// Only add 'special' if it exists and is not null
if (isset($field['special'])) {
$structure['special'] = $field['special'];
}
$formStructure[$field['name']] = $structure;
}
}
#var_dump($formStructure); // Just for data structure debugging purpose
return $formStructure;
}
// Handles form submission security and dispatch logic
private function processSend($filePath, $fileName, $dispatchFormat, $formOptions) {
// Validate CSRF token before processing
$receivedHash = $this->yellow->page->getRequest("mdform-hash");
if (!$this->checkHashString($receivedHash, $this->yellow->system->get("MDFormHashSaltPasskey"))) {
return "<p>" . $this->yellow->language->getText("MDFormErrorTokenInvalid") . "</p>\n " . $this->getFormHTML($filePath, $fileName, $formOptions, false);
}
// Verify if IP is being rate limited
if ($this->isRateLimited()) {
return "<p>" . $this->yellow->language->getText("MDFormWarningRateLimit") . "</p>\n " . $this->getFormHTML($filePath, $fileName, $formOptions, true);
}
// Validate Create CAPTCHA
if (stripos($formOptions, "captcha") !== false) {
if (!$this->checkCaptcha(trim($this->yellow->page->getRequest("captcha")), trim($this->yellow->page->getRequest("captcha_hash")))) {
return "<p>" . $this->yellow->language->getText("MDFormCaptchaInvalid") . "</p>\n " . $this->getFormHTML($filePath, $fileName, $formOptions, true);
}
}
if ($this->yellow->system->get("MDFormResubmitCookie")) {
// Validate resubmit with token
if ($this->yellow->toolbox->getCookie($receivedHash) === "yellowmdformhash") {
return "<p>" . $this->yellow->language->getText("MDFormWarningResubmit") . "</p>\n " . $this->getFormHTML($filePath, $fileName, $formOptions, true);
} else {
// Create a Cookie
$this->createCookie($receivedHash, "yellowmdformhash");
// Destroy Cookie
#$this->destroyCookie($receivedHash);
}
}
$output = "<div class=\"mdform-container\">\n ";
$output .= "<p>" . $this->yellow->language->getText("MDFormSubmitted") . "</p>\n ";
// Execute dispatch methods if defined
if (!is_string_empty($dispatchFormat)) {
$formStructure = $this->getFormFileMetadata(file_get_contents($filePath . $fileName));
#var_dump($formStructure); // Just for data structure debugging purpose
$dispatchCommands = preg_split('/[\s,]+/', $dispatchFormat, -1, PREG_SPLIT_NO_EMPTY);
// Execute each dispatch method in order
foreach ($dispatchCommands as $cmd) {
// Handle HTML display dispatch
if (strtolower($cmd) === "html") {
$output .= $this->subDispatchHtml($formStructure);
}
// Handle CSV export dispatch
elseif (strtolower($cmd) === "csv") {
$output .= $this->subDispatchCsv($formStructure, $fileName);
}
// Handle Email notification dispatch
elseif (strtolower($cmd) === "email") {
$output .= $this->subDispatchEmail($formStructure);
}
}
}
$output .= "</div>\n ";
return $output;
}
// Cleans field names by replacing special characters with underscores
private function sanitizeFromName($name) {
return str_replace([' ', '.', '[', ']', ':', '*'], '_', trim($name));
}
// Creates a time-limited hash token for CSRF protection
public function createHashString($salt) {
$ip = $this->yellow->toolbox->getServer("REMOTE_ADDR");
$hour = (int)(time() / 3600);
$hash = $this->yellow->toolbox->createHash($salt . $ip . $hour, "sha256");
// Handle potential hash failure
if (is_string_empty($hash)) {
$hash = "error-hash-algorithm-sha256";
}
return $hash;
}
// Verifies if the submitted CSRF token is valid
public function checkHashString($hash, $salt) {
$ip = $this->yellow->toolbox->getServer("REMOTE_ADDR");
$currentHour = (int)(time() / 3600);
$previousHour = $currentHour - 1;
// Allow tokens from current or previous hour block
return $this->yellow->toolbox->verifyHash($salt . $ip . $currentHour, "sha256", $hash) ||
$this->yellow->toolbox->verifyHash($salt . $ip . $previousHour, "sha256", $hash);
}
// Limits form submissions per IP/Session to prevent spam
private function isRateLimited() {
$limitDir = $this->yellow->system->get("MDFormRateLimitDirectory");
$ip = $this->yellow->toolbox->getServer("REMOTE_ADDR");
$userAgent = $this->yellow->toolbox->getServer("HTTP_USER_AGENT") ?? '';
// Create unique visitor fingerprint
$fingerprint = hash("sha256", $ip . '|' . $userAgent);
$fingerprintLimitFile = $limitDir . "fp_" . $fingerprint;
$ipLimitFile = $limitDir . "ip_" . hash("sha256", $ip);
// Initialize limit storage directory
if (!is_dir($limitDir)) {
mkdir($limitDir, 0700, true);
}
$this->cleanupOldRateLimitFiles($limitDir);
// Check session-based rate limit
if (file_exists($fingerprintLimitFile)) {
$lastSubmission = (int)file_get_contents($fingerprintLimitFile);
$waitTime = 20;
// Deny if too soon
if ((time() - $lastSubmission) < $waitTime) {
return true;
}
}
// Check hard IP-based rate limit
if (file_exists($ipLimitFile)) {
$lastIpSubmission = (int)file_get_contents($ipLimitFile);
$waitTime = 10;
// Deny if too soon
if ((time() - $lastIpSubmission) < $waitTime) {
return true;
}
}
// Store timestamp for current submission
file_put_contents($fingerprintLimitFile, time(), LOCK_EX);
file_put_contents($ipLimitFile, time(), LOCK_EX);
return false;
}
// Removes expired rate limit tracking files
private function cleanupOldRateLimitFiles($dir) {
$maxAge = 3600;
// Check if directory exists
if (!is_dir($dir)) {
return;
}
// Iterate through tracking files
foreach (glob($dir . '*') as $file) {
// Delete files older than one hour
if (is_file($file) && (time() - filemtime($file) > $maxAge)) {
@unlink($file);
}
}
}
// Create browser cookies
private function createCookie($name, $value) {
$expire = 0;
$scheme = $this->yellow->system->get("coreServerScheme");
$address = $this->yellow->system->get("coreServerAddress");
$base = $this->yellow->system->get("coreServerBase");
$path = $this->yellow->page->getLocation();
setcookie($name, $value, $expire, $base.$path, "", $scheme=="https", true);
}
// Destroy browser cookies
private function destroyCookie($name) {
$scheme = $this->yellow->system->get("coreServerScheme"); // Match creation logic
$base = $this->yellow->system->get("coreServerBase");
$path = $this->yellow->page->getLocation();
setcookie($name, "", 1, $base.$path, "", $scheme=="https", true);
}
// Dispatch: Formats submitted data as HTML
private function subDispatchHtml($formStructure) {
$output = "<p>" . $this->yellow->language->getText("MDFormHTMLOutput") . "</p>\n";
$output .= "<table> \n";
// Iterate through submission headers
foreach (array_keys($formStructure) as $header) {
$val = $this->yellow->page->getRequest($header);
$displayVal = is_array($val) ? implode(', ', $val) : $val;
$displayVal = ($formStructure[$header]['type'] === 'toggle' && empty($displayVal)) ? "OFF": $displayVal; // Toggle: Show OFF value if not ON value
$displayVal = htmlspecialchars(trim($displayVal));
$output .= "<tr><td>" . htmlspecialchars($header) . $this->yellow->system->get("MDFormKeyValueSeperator") . "</td><td><strong>" . $displayVal . "</strong></tr>\n";
}
$output .= "</table> \n";
return $output;
}
// Dispatch: Appends submitted data to a CSV file
private function subDispatchCsv($formStructure, $fileName) {
$output = "";
$delimiter = ",";
$enclosure = '"';
$escape = "\\";
$csvPath = $this->yellow->system->get("MDFormDirectoryCSVOutput") . $fileName . ".csv";
$expectedHeaders = array_merge(["mdform-timestamp"], ["mdform-hash"], ["mdform-validated"], array_keys($formStructure)); // Store also the mdform-hash within the CSV data
$csvDir = dirname($csvPath);
// Ensure CSV directory exists
if (!is_dir($csvDir)) {
mkdir($csvDir, 0755, true);
}
// Validate consistency of existing CSV files
if (file_exists($csvPath)) {
$handle = fopen($csvPath, 'r');
$existingHeader = fgetcsv($handle, 0, $delimiter, $enclosure, $escape);
fclose($handle);
// Backup and restart if structure has changed
if ($existingHeader !== $expectedHeaders) {
rename($csvPath, $csvPath . "." . date("YmdHis") . ".bak");
}
}
$dataRow = [];
// Map submitted values to expected headers
foreach ($expectedHeaders as $header) {
// Filing
if ($header === "mdform-timestamp") {
$val = date("Y-m-d H:i:s");
}
elseif ($header === "mdform-validated") {
$val = "0";
}
else {
$val = $this->yellow->page->getRequest($header);
}
// Join array values with semicolons
if (is_array($val)) {
$val = implode("; ", $val);
}
$dataRow[] = is_string($val) ? $val : (string)$val;
}
$isNew = !file_exists($csvPath);
$handle = fopen($csvPath, 'a');
// Handle CSV file write failures
if ($handle === false) {
return "<p>" . $this->yellow->language->getText("MDFormErrorCsvFileAccess") . "</p>\n ";
}
// Write CSV header for new files
if ($isNew) {
fputcsv($handle, $expectedHeaders, $delimiter, $enclosure, $escape);
}
fputcsv($handle, $dataRow, $delimiter, $enclosure, $escape);
fclose($handle);
$output = "<p>" . $this->yellow->language->getText("MDFormCSVSaved") . "</p>\n";
return $output;
}
// Validates a previously submitted form entry in the CSV
private function processValidateCSV($fileName, $formHash) {
$output = "";
$delimiter = ",";
$enclosure = '"';
$escape = "\\";
$found = false;
$csvPath = $this->yellow->system->get("MDFormDirectoryCSVOutput") . $fileName . ".csv";
if (!file_exists($csvPath)) {
return "<p>" . $this->yellow->language->getText("MDFormErrorMdfFileNotFound") . "</p>\n";
}
$rows = [];
$headers = [];
// 1. Read the file into memory
if (($handle = fopen($csvPath, "r")) !== false) {
// Get the header row first
$headers = fgetcsv($handle, 0, $delimiter, $enclosure, $escape);
if ($headers !== false) {
// Find the column indexes for hash and validation status
$hashIndex = array_search("mdform-hash", $headers);
$validatedIndex = array_search("mdform-validated", $headers);
if ($hashIndex !== false && $validatedIndex !== false) {
// Read data rows
while (($data = fgetcsv($handle, 0, $delimiter, $enclosure, $escape)) !== false) {
// Check if this row matches the hash
if ($data[$hashIndex] === $formHash) {
$data[$validatedIndex] = "1"; // Change "0" to "1"
$found = true;
}
$rows[] = $data;
}
}
}
fclose($handle);
}
// 2. If a match was found, write the updated data back to the CSV
if ($found) {
$handle = fopen($csvPath, 'w'); // 'w' truncates the file to overwrite
if ($handle !== false) {
fputcsv($handle, $headers, $delimiter, $enclosure, $escape);
foreach ($rows as $row) {
fputcsv($handle, $row, $delimiter, $enclosure, $escape);
}
fclose($handle);
$output = "<p>" . $this->yellow->language->getText("MDFormValidateSuccess") . "</p>\n";
} else {
$output = "<p>" . $this->yellow->language->getText("MDFormErrorCsvFileAccess") . "</p>\n";
}
} else {
// If hash is not found, it might be expired or already validated
$output = "<p>" . $this->yellow->language->getText("MDFormValidateError") . "</p>\n"; # . $this->getFormHTML($filePath, $fileName, $formOptions, true);
}
return $output;