form.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "number":
  58. self::renderNumberField($options);
  59. break;
  60. case "textarea":
  61. self::renderTextareaField($options);
  62. break;
  63. case "file":
  64. self::renderFileField($options);
  65. break;
  66. }
  67. }
  68. private static function renderHeader($options) {
  69. ?>
  70. <h3><?=htmlspecialchars($options->content)?></h3>
  71. <?php
  72. }
  73. private static function renderTextField($options) {
  74. $id = str_replace("_", "-", $options->name);
  75. $placeholder = "Enter " . strtolower($options->label);
  76. $value = $options->value ?? http::value($options->name);
  77. $required = ($options->required ?? false) ? "required" : "";
  78. $help_text = $options->helpText ?? false;
  79. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  80. ?>
  81. <div class="row mb-1">
  82. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  83. <div class="col-sm">
  84. <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
  85. <?php
  86. if($help_text) {
  87. ?>
  88. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  89. <?php
  90. }
  91. ?>
  92. </div>
  93. </div>
  94. <?php
  95. }
  96. private static function renderNumberField($options) {
  97. $id = str_replace("_", "-", $options->name);
  98. $placeholder = "Enter " . strtolower($options->label);
  99. $value = $options->value ?? http::value($options->name);
  100. $required = ($options->required ?? false) ? "required" : "";
  101. $help_text = $options->helpText ?? false;
  102. $min = ($options->min ?? false) ? "min=\"$options->min\"" : "";
  103. $max = ($options->max ?? false) ? "max=\"$options->max\"" : "";
  104. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  105. $step = $options->step ?? 1;
  106. ?>
  107. <div class="row mb-1">
  108. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  109. <div class="col-sm">
  110. <input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" step="<?=$step?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
  111. <?php
  112. if($help_text) {
  113. ?>
  114. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  115. <?php
  116. }
  117. ?>
  118. </div>
  119. </div>
  120. <?php
  121. }
  122. private static function renderSelectField($options) {
  123. $id = str_replace("_", "-", $options->name);
  124. $selected_value = $options->value ?? http::value($options->name);
  125. $required = ($options->required ?? false) ? "required" : "";
  126. $help_text = $options->helpText ?? false;
  127. ?>
  128. <div class="row mb-1">
  129. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  130. <div class="col-sm">
  131. <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
  132. <?php
  133. foreach($options->values as $index => $value) {
  134. $display = $options->displayStrings[$index];
  135. $selected = ($selected_value === strval($value)) ? "selected" : "";
  136. ?>
  137. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  138. <?php
  139. }
  140. ?>
  141. </select>
  142. <?php
  143. if($help_text) {
  144. ?>
  145. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  146. <?php
  147. }
  148. ?>
  149. </div>
  150. </div>
  151. <?php
  152. }
  153. private static function renderFileField($options) {
  154. $id = str_replace("_", "-", $options->name);
  155. $placeholder = "Enter " . strtolower($options->label);
  156. $required = ($options->required ?? false) ? "required" : "";
  157. $help_text = $options->helpText ?? false;
  158. ?>
  159. <div class="row mb-1">
  160. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  161. <div class="col-sm">
  162. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
  163. <?php
  164. if($help_text) {
  165. ?>
  166. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  167. <?php
  168. }
  169. ?>
  170. </div>
  171. </div>
  172. <?php
  173. }
  174. private static function renderTextareaField($options) {
  175. $id = str_replace("_", "-", $options->name);
  176. $placeholder = "Enter " . strtolower($options->label);
  177. $value = $options->value ?? http::value($options->name);
  178. $required = ($options->required ?? false) ? "required" : "";
  179. $help_text = $options->helpText ?? false;
  180. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  181. ?>
  182. <div class="row mb-1">
  183. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  184. <div class="col-sm">
  185. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
  186. <?php
  187. if($help_text) {
  188. ?>
  189. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  190. <?php
  191. }
  192. ?>
  193. </div>
  194. </div>
  195. <?php
  196. }
  197. }
  198. ?>