form.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 renderYearOptionsDescending($min = null, $max = null, $selected_value = null) {
  29. if(empty($min)) $min = date("Y") - 100;
  30. if(empty($max)) $max = date("Y") - 16;
  31. for($i = $max; $i >= $min; $i--) {
  32. $selected = ($selected_value == $i) ? "selected" : "";
  33. ?>
  34. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  35. <?php
  36. }
  37. }
  38. public static function renderMonthOptions($selected_value = null) {
  39. for($i = 1; $i <= 12; $i++) {
  40. $selected = ($selected_value == $i) ? "selected" : "";
  41. $display = date("M", mktime(0, 0, 0, $i, 15));
  42. ?>
  43. <option value="<?=$i?>" <?=$selected?>><?=$display?></option>
  44. <?php
  45. }
  46. }
  47. public static function renderDayOptions($selected_value = null) {
  48. for($i = 1; $i <= 31; $i++) {
  49. $selected = ($selected_value == $i) ? "selected" : "";
  50. ?>
  51. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  52. <?php
  53. }
  54. }
  55. public static function renderField($options) {
  56. switch($options->type) {
  57. case "header":
  58. self::renderHeader($options);
  59. break;
  60. case "dropdown":
  61. case "select":
  62. self::renderSelectField($options);
  63. break;
  64. case "text":
  65. self::renderTextField($options);
  66. break;
  67. case "number":
  68. self::renderNumberField($options);
  69. break;
  70. case "textarea":
  71. self::renderTextareaField($options);
  72. break;
  73. case "file":
  74. self::renderFileField($options);
  75. break;
  76. }
  77. }
  78. private static function renderHeader($options) {
  79. ?>
  80. <h3><?=htmlspecialchars($options->content)?></h3>
  81. <?php
  82. }
  83. private static function renderTextField($options) {
  84. $id = str_replace("_", "-", $options->name);
  85. $placeholder = "Enter " . strtolower($options->label);
  86. $value = $options->value ?? http::value($options->name);
  87. $required = ($options->required ?? false) ? "required" : "";
  88. $help_text = $options->helpText ?? false;
  89. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  90. ?>
  91. <div class="row mb-1">
  92. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  93. <div class="col-sm">
  94. <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
  95. <?php
  96. if($help_text) {
  97. ?>
  98. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  99. <?php
  100. }
  101. ?>
  102. </div>
  103. </div>
  104. <?php
  105. }
  106. private static function renderNumberField($options) {
  107. $id = str_replace("_", "-", $options->name);
  108. $placeholder = "Enter " . strtolower($options->label);
  109. $value = $options->value ?? http::value($options->name);
  110. $required = ($options->required ?? false) ? "required" : "";
  111. $help_text = $options->helpText ?? false;
  112. $min = ($options->min ?? false) ? "min=\"$options->min\"" : "";
  113. $max = ($options->max ?? false) ? "max=\"$options->max\"" : "";
  114. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  115. $step = $options->step ?? 1;
  116. $prepend_text = $options->prependText ?? "";
  117. ?>
  118. <div class="row mb-1">
  119. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  120. <div class="col-sm">
  121. <div class="input-group">
  122. <?php
  123. if(!empty($prepend_text)) {
  124. ?>
  125. <div class="input-group-prepend">
  126. <span class="input-group-text"><?=htmlspecialchars($prepend_text)?></span>
  127. </div>
  128. <?php
  129. }
  130. ?>
  131. <input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" step="<?=$step?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
  132. </div>
  133. <?php
  134. if($help_text) {
  135. ?>
  136. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  137. <?php
  138. }
  139. ?>
  140. </div>
  141. </div>
  142. <?php
  143. }
  144. private static function renderSelectField($options) {
  145. $id = str_replace("_", "-", $options->name);
  146. $selected_value = $options->value ?? http::value($options->name);
  147. $required = ($options->required ?? false) ? "required" : "";
  148. $help_text = $options->helpText ?? false;
  149. ?>
  150. <div class="row mb-1">
  151. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  152. <div class="col-sm">
  153. <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
  154. <?php
  155. foreach($options->values as $index => $value) {
  156. $display = $options->displayStrings[$index];
  157. $selected = ($selected_value === strval($value)) ? "selected" : "";
  158. ?>
  159. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  160. <?php
  161. }
  162. ?>
  163. </select>
  164. <?php
  165. if($help_text) {
  166. ?>
  167. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  168. <?php
  169. }
  170. ?>
  171. </div>
  172. </div>
  173. <?php
  174. }
  175. private static function renderFileField($options) {
  176. $id = str_replace("_", "-", $options->name);
  177. $placeholder = "Enter " . strtolower($options->label);
  178. $required = ($options->required ?? false) ? "required" : "";
  179. $help_text = $options->helpText ?? false;
  180. ?>
  181. <div class="row mb-1">
  182. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  183. <div class="col-sm">
  184. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
  185. <?php
  186. if($help_text) {
  187. ?>
  188. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  189. <?php
  190. }
  191. ?>
  192. </div>
  193. </div>
  194. <?php
  195. }
  196. private static function renderTextareaField($options) {
  197. $id = str_replace("_", "-", $options->name);
  198. $placeholder = "Enter " . strtolower($options->label);
  199. $value = $options->value ?? http::value($options->name);
  200. $required = ($options->required ?? false) ? "required" : "";
  201. $help_text = $options->helpText ?? false;
  202. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  203. ?>
  204. <div class="row mb-1">
  205. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  206. <div class="col-sm">
  207. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
  208. <?php
  209. if($help_text) {
  210. ?>
  211. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  212. <?php
  213. }
  214. ?>
  215. </div>
  216. </div>
  217. <?php
  218. }
  219. }
  220. ?>