form.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. class Form {
  3. public static function checkRecaptcha() {
  4. if(!defined("GOOGLE_RECAPTCHA_SECRET_KEY")) return true;
  5. $json = http::post("https://www.google.com/recaptcha/api/siteverify", [
  6. "secret" => GOOGLE_RECAPTCHA_SECRET_KEY,
  7. "response" => http::value("g-recaptcha-response"),
  8. "remoteip" => $_SERVER["REMOTE_ADDR"]
  9. ]);
  10. if($json) {
  11. $response = json_decode($json);
  12. if($response) {
  13. return $response->success;
  14. }
  15. }
  16. return false;
  17. }
  18. public static function renderYearOptions($min = null, $max = null, $selected_value = null) {
  19. if(empty($min)) $min = date("Y") - 100;
  20. if(empty($max)) $max = date("Y") - 16;
  21. for($i = $min; $i <= $max; $i++) {
  22. $selected = ($selected_value == $i) ? "selected" : "";
  23. ?>
  24. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  25. <?php
  26. }
  27. }
  28. public static function renderMonthOptions($selected_value = null) {
  29. for($i = 1; $i <= 12; $i++) {
  30. $selected = ($selected_value == $i) ? "selected" : "";
  31. $display = date("M", mktime(0, 0, 0, $i));
  32. ?>
  33. <option value="<?=$i?>" <?=$selected?>><?=$display?></option>
  34. <?php
  35. }
  36. }
  37. public static function renderDayOptions($selected_value = null) {
  38. for($i = 1; $i <= 31; $i++) {
  39. $selected = ($selected_value == $i) ? "selected" : "";
  40. ?>
  41. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  42. <?php
  43. }
  44. }
  45. public static function renderField($options) {
  46. switch($options->type) {
  47. case "header":
  48. self::renderHeader($options);
  49. break;
  50. case "dropdown":
  51. case "select":
  52. self::renderSelectField($options);
  53. break;
  54. case "text":
  55. self::renderTextField($options);
  56. break;
  57. case "textarea":
  58. self::renderTextareaField($options);
  59. break;
  60. case "file":
  61. self::renderFileField($options);
  62. break;
  63. }
  64. }
  65. private static function renderHeader($options) {
  66. ?>
  67. <h3><?=htmlspecialchars($options->content)?></h3>
  68. <?php
  69. }
  70. private static function renderTextField($options) {
  71. $id = str_replace("_", "-", $options->name);
  72. $placeholder = "Enter " . strtolower($options->label);
  73. $value = $options->value ?? http::value($options->name);
  74. $required = ($options->required ?? false) ? "required" : "";
  75. ?>
  76. <div class="row mb-1">
  77. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  78. <div class="col-sm">
  79. <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> />
  80. </div>
  81. </div>
  82. <?php
  83. }
  84. private static function renderSelectField($options) {
  85. $id = str_replace("_", "-", $options->name);
  86. $selected_value = $options->value ?? http::value($options->name);
  87. $required = ($options->required ?? false) ? "required" : "";
  88. ?>
  89. <div class="row mb-1">
  90. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  91. <div class="col-sm">
  92. <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
  93. <?php
  94. foreach($options->values as $index => $value) {
  95. $display = $options->displayStrings[$index];
  96. $selected = ($selected_value === strval($value)) ? "selected" : "";
  97. ?>
  98. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  99. <?php
  100. }
  101. ?>
  102. </select>
  103. </div>
  104. </div>
  105. <?php
  106. }
  107. private static function renderFileField($options) {
  108. $id = str_replace("_", "-", $options->name);
  109. $placeholder = "Enter " . strtolower($options->label);
  110. $required = ($options->required ?? false) ? "required" : "";
  111. ?>
  112. <div class="row mb-1">
  113. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  114. <div class="col-sm">
  115. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" capture="camera" <?=$required?> />
  116. </div>
  117. </div>
  118. <?php
  119. }
  120. private static function renderTextareaField($options) {
  121. $id = str_replace("_", "-", $options->name);
  122. $placeholder = "Enter " . strtolower($options->label);
  123. $value = $options->value ?? http::value($options->name);
  124. $required = ($options->required ?? false) ? "required" : "";
  125. ?>
  126. <div class="row mb-1">
  127. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  128. <div class="col-sm">
  129. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?>><?=htmlspecialchars($value)?></textarea>
  130. </div>
  131. </div>
  132. <?php
  133. }
  134. }
  135. ?>