form.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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, 15));
  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. $prepend_text = $options->prependText ?? "";
  107. ?>
  108. <div class="row mb-1">
  109. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  110. <div class="col-sm">
  111. <div class="input-group">
  112. <?php
  113. if(!empty($prepend_text)) {
  114. ?>
  115. <div class="input-group-prepend">
  116. <span class="input-group-text"><?=htmlspecialchars($prepend_text)?></span>
  117. </div>
  118. <?php
  119. }
  120. ?>
  121. <input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" step="<?=$step?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
  122. </div>
  123. <?php
  124. if($help_text) {
  125. ?>
  126. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  127. <?php
  128. }
  129. ?>
  130. </div>
  131. </div>
  132. <?php
  133. }
  134. private static function renderSelectField($options) {
  135. $id = str_replace("_", "-", $options->name);
  136. $selected_value = $options->value ?? http::value($options->name);
  137. $required = ($options->required ?? false) ? "required" : "";
  138. $help_text = $options->helpText ?? false;
  139. ?>
  140. <div class="row mb-1">
  141. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  142. <div class="col-sm">
  143. <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
  144. <?php
  145. foreach($options->values as $index => $value) {
  146. $display = $options->displayStrings[$index];
  147. $selected = ($selected_value === strval($value)) ? "selected" : "";
  148. ?>
  149. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  150. <?php
  151. }
  152. ?>
  153. </select>
  154. <?php
  155. if($help_text) {
  156. ?>
  157. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  158. <?php
  159. }
  160. ?>
  161. </div>
  162. </div>
  163. <?php
  164. }
  165. private static function renderFileField($options) {
  166. $id = str_replace("_", "-", $options->name);
  167. $placeholder = "Enter " . strtolower($options->label);
  168. $required = ($options->required ?? false) ? "required" : "";
  169. $help_text = $options->helpText ?? false;
  170. ?>
  171. <div class="row mb-1">
  172. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  173. <div class="col-sm">
  174. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
  175. <?php
  176. if($help_text) {
  177. ?>
  178. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  179. <?php
  180. }
  181. ?>
  182. </div>
  183. </div>
  184. <?php
  185. }
  186. private static function renderTextareaField($options) {
  187. $id = str_replace("_", "-", $options->name);
  188. $placeholder = "Enter " . strtolower($options->label);
  189. $value = $options->value ?? http::value($options->name);
  190. $required = ($options->required ?? false) ? "required" : "";
  191. $help_text = $options->helpText ?? false;
  192. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  193. ?>
  194. <div class="row mb-1">
  195. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  196. <div class="col-sm">
  197. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
  198. <?php
  199. if($help_text) {
  200. ?>
  201. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  202. <?php
  203. }
  204. ?>
  205. </div>
  206. </div>
  207. <?php
  208. }
  209. }
  210. ?>