form.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 renderHourOptions($selected_value = null) {
  56. for($i = 0; $i <= 23; $i++) {
  57. $selected = ($selected_value === $i) ? "selected" : "";
  58. ?>
  59. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  60. <?php
  61. }
  62. }
  63. public static function renderMinuteOptions($selected_value = null) {
  64. for($i = 0; $i <= 59; $i++) {
  65. $selected = ($selected_value === $i) ? "selected" : "";
  66. ?>
  67. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  68. <?php
  69. }
  70. }
  71. public static function renderField($options) {
  72. switch($options->type) {
  73. case "header":
  74. self::renderHeader($options);
  75. break;
  76. case "dropdown":
  77. case "select":
  78. self::renderSelectField($options);
  79. break;
  80. case "text":
  81. self::renderTextField($options);
  82. break;
  83. case "number":
  84. self::renderNumberField($options);
  85. break;
  86. case "textarea":
  87. self::renderTextareaField($options);
  88. break;
  89. case "file":
  90. self::renderFileField($options);
  91. break;
  92. }
  93. }
  94. private static function renderHeader($options) {
  95. ?>
  96. <h3><?=htmlspecialchars($options->content)?></h3>
  97. <?php
  98. }
  99. private static function renderTextField($options) {
  100. $id = str_replace("_", "-", $options->name);
  101. $placeholder = "Enter " . strtolower($options->label);
  102. $value = $options->value ?? http::value($options->name);
  103. $required = ($options->required ?? false) ? "required" : "";
  104. $help_text = $options->helpText ?? false;
  105. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  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="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$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 renderNumberField($options) {
  123. $id = str_replace("_", "-", $options->name);
  124. $placeholder = "Enter " . strtolower($options->label);
  125. $value = $options->value ?? http::value($options->name);
  126. $required = ($options->required ?? false) ? "required" : "";
  127. $help_text = $options->helpText ?? false;
  128. $min = ($options->min ?? false) ? "min=\"$options->min\"" : "";
  129. $max = ($options->max ?? false) ? "max=\"$options->max\"" : "";
  130. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  131. $step = $options->step ?? 1;
  132. $prepend_text = $options->prependText ?? "";
  133. ?>
  134. <div class="row mb-1">
  135. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  136. <div class="col-sm">
  137. <div class="input-group">
  138. <?php
  139. if(!empty($prepend_text)) {
  140. ?>
  141. <div class="input-group-prepend">
  142. <span class="input-group-text"><?=htmlspecialchars($prepend_text)?></span>
  143. </div>
  144. <?php
  145. }
  146. ?>
  147. <input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" step="<?=$step?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
  148. </div>
  149. <?php
  150. if($help_text) {
  151. ?>
  152. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  153. <?php
  154. }
  155. ?>
  156. </div>
  157. </div>
  158. <?php
  159. }
  160. private static function renderSelectField($options) {
  161. $id = str_replace("_", "-", $options->name);
  162. $selected_value = $options->value ?? http::value($options->name);
  163. $required = ($options->required ?? false) ? "required" : "";
  164. $help_text = $options->helpText ?? false;
  165. ?>
  166. <div class="row mb-1">
  167. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  168. <div class="col-sm">
  169. <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
  170. <?php
  171. foreach($options->values as $index => $value) {
  172. $display = $options->displayStrings[$index];
  173. $selected = ($selected_value === strval($value)) ? "selected" : "";
  174. ?>
  175. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  176. <?php
  177. }
  178. ?>
  179. </select>
  180. <?php
  181. if($help_text) {
  182. ?>
  183. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  184. <?php
  185. }
  186. ?>
  187. </div>
  188. </div>
  189. <?php
  190. }
  191. private static function renderFileField($options) {
  192. $id = str_replace("_", "-", $options->name);
  193. $placeholder = "Enter " . strtolower($options->label);
  194. $required = ($options->required ?? false) ? "required" : "";
  195. $help_text = $options->helpText ?? false;
  196. ?>
  197. <div class="row mb-1">
  198. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  199. <div class="col-sm">
  200. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
  201. <?php
  202. if($help_text) {
  203. ?>
  204. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  205. <?php
  206. }
  207. ?>
  208. </div>
  209. </div>
  210. <?php
  211. }
  212. private static function renderTextareaField($options) {
  213. $id = str_replace("_", "-", $options->name);
  214. $placeholder = "Enter " . strtolower($options->label);
  215. $value = $options->value ?? http::value($options->name);
  216. $required = ($options->required ?? false) ? "required" : "";
  217. $help_text = $options->helpText ?? false;
  218. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  219. ?>
  220. <div class="row mb-1">
  221. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  222. <div class="col-sm">
  223. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
  224. <?php
  225. if($help_text) {
  226. ?>
  227. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  228. <?php
  229. }
  230. ?>
  231. </div>
  232. </div>
  233. <?php
  234. }
  235. }
  236. ?>