form.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. ?>
  106. <div class="row mb-1">
  107. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  108. <div class="col-sm">
  109. <input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" step="0.01" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
  110. <?php
  111. if($help_text) {
  112. ?>
  113. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  114. <?php
  115. }
  116. ?>
  117. </div>
  118. </div>
  119. <?php
  120. }
  121. private static function renderSelectField($options) {
  122. $id = str_replace("_", "-", $options->name);
  123. $selected_value = $options->value ?? http::value($options->name);
  124. $required = ($options->required ?? false) ? "required" : "";
  125. $help_text = $options->helpText ?? false;
  126. ?>
  127. <div class="row mb-1">
  128. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  129. <div class="col-sm">
  130. <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
  131. <?php
  132. foreach($options->values as $index => $value) {
  133. $display = $options->displayStrings[$index];
  134. $selected = ($selected_value === strval($value)) ? "selected" : "";
  135. ?>
  136. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  137. <?php
  138. }
  139. ?>
  140. </select>
  141. <?php
  142. if($help_text) {
  143. ?>
  144. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  145. <?php
  146. }
  147. ?>
  148. </div>
  149. </div>
  150. <?php
  151. }
  152. private static function renderFileField($options) {
  153. $id = str_replace("_", "-", $options->name);
  154. $placeholder = "Enter " . strtolower($options->label);
  155. $required = ($options->required ?? false) ? "required" : "";
  156. $help_text = $options->helpText ?? false;
  157. ?>
  158. <div class="row mb-1">
  159. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  160. <div class="col-sm">
  161. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
  162. <?php
  163. if($help_text) {
  164. ?>
  165. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  166. <?php
  167. }
  168. ?>
  169. </div>
  170. </div>
  171. <?php
  172. }
  173. private static function renderTextareaField($options) {
  174. $id = str_replace("_", "-", $options->name);
  175. $placeholder = "Enter " . strtolower($options->label);
  176. $value = $options->value ?? http::value($options->name);
  177. $required = ($options->required ?? false) ? "required" : "";
  178. $help_text = $options->helpText ?? false;
  179. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  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. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
  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. }
  197. ?>