1
0

form.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. class Form {
  3. const BOOLEAN_OPTIONS = [
  4. 0 => NO,
  5. 1 => YES,
  6. ];
  7. public static function checked($value) {
  8. return !empty($value) ? "checked" : "";
  9. }
  10. public static function checkRecaptcha($required_score = 0.5) {
  11. if(!defined("GOOGLE_RECAPTCHA_SECRET")) return true;
  12. $json = http::post("https://www.google.com/recaptcha/api/siteverify", [
  13. "secret" => GOOGLE_RECAPTCHA_SECRET,
  14. "response" => http::value("g-recaptcha-response"),
  15. "remoteip" => $_SERVER["REMOTE_ADDR"]
  16. ]);
  17. if(!$json) return false;
  18. $response = json_decode($json);
  19. if(!$response) return false;
  20. if(isset($response->score)) {
  21. return $response->score >= $required_score;
  22. }
  23. else {
  24. return $response->success;
  25. }
  26. }
  27. public static function renderYearOptions($min = null, $max = null, $selected_value = null) {
  28. if(empty($min)) $min = date("Y") - 100;
  29. if(empty($max)) $max = date("Y") - 16;
  30. for($i = $min; $i <= $max; $i++) {
  31. $selected = ($selected_value == $i) ? "selected" : "";
  32. ?>
  33. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  34. <?php
  35. }
  36. }
  37. public static function renderYearOptionsDescending($min = null, $max = null, $selected_value = null) {
  38. if(empty($min)) $min = date("Y") - 100;
  39. if(empty($max)) $max = date("Y") - 16;
  40. for($i = $max; $i >= $min; $i--) {
  41. $selected = ($selected_value == $i) ? "selected" : "";
  42. ?>
  43. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  44. <?php
  45. }
  46. }
  47. public static function renderMonthOptions($selected_value = null) {
  48. for($i = 1; $i <= 12; $i++) {
  49. $selected = ($selected_value == $i) ? "selected" : "";
  50. $display = date("M", mktime(0, 0, 0, $i, 15));
  51. ?>
  52. <option value="<?=$i?>" <?=$selected?>><?=$display?></option>
  53. <?php
  54. }
  55. }
  56. public static function renderDayOptions($selected_value = null) {
  57. for($i = 1; $i <= 31; $i++) {
  58. $selected = ($selected_value == $i) ? "selected" : "";
  59. ?>
  60. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  61. <?php
  62. }
  63. }
  64. public static function renderHourOptions($selected_value = null) {
  65. for($i = 0; $i <= 23; $i++) {
  66. $selected = ($selected_value === $i) ? "selected" : "";
  67. ?>
  68. <option value="<?=$i?>" <?=$selected?>><?=str_pad($i, 2, "0", STR_PAD_LEFT)?></option>
  69. <?php
  70. }
  71. }
  72. public static function renderMinuteOptions($selected_value = null) {
  73. for($i = 0; $i <= 59; $i++) {
  74. $selected = ($selected_value === $i) ? "selected" : "";
  75. ?>
  76. <option value="<?=$i?>" <?=$selected?>><?=str_pad($i, 2, "0", STR_PAD_LEFT)?></option>
  77. <?php
  78. }
  79. }
  80. public static function renderSecondOptions($selected_value = null) {
  81. for($i = 0; $i <= 59; $i++) {
  82. $selected = ($selected_value === $i) ? "selected" : "";
  83. ?>
  84. <option value="<?=$i?>" <?=$selected?>><?=str_pad($i, 2, "0", STR_PAD_LEFT)?></option>
  85. <?php
  86. }
  87. }
  88. public static function renderCountryOptions($selected_value = null) {
  89. $file = ROOT_DIR . "/files/countries.json";
  90. $threshold = time() - (7 * 86400);
  91. if(file_exists($file) && filemtime($file) > $threshold) {
  92. $countries_json = @file_get_contents($file);
  93. $countries = json_decode($countries_json);
  94. }
  95. else {
  96. $url = "http://country.io/names.json";
  97. $countries_json = @file_get_contents($url);
  98. $countries = json_decode($countries_json);
  99. if($countries) {
  100. file_put_contents($file, $countries_json);
  101. }
  102. }
  103. if(!$countries) return false;
  104. $country_pairs = get_object_vars($countries);
  105. sort($country_pairs);
  106. $countries = array_values($country_pairs);
  107. ?>
  108. <option value="">Select Country</option>
  109. <?php
  110. foreach($countries as $country) {
  111. $selected = ($selected_value === $country) ? "selected" : "";
  112. ?>
  113. <option value="<?=htmlspecialchars($country)?>" <?=$selected?>><?=htmlspecialchars($country)?></option>
  114. <?php
  115. }
  116. }
  117. public static function renderField($options) {
  118. switch($options->type) {
  119. case "header":
  120. self::renderHeader($options);
  121. break;
  122. case "dropdown":
  123. case "select":
  124. self::renderSelectField($options);
  125. break;
  126. case "text":
  127. self::renderTextField($options);
  128. break;
  129. case "number":
  130. self::renderNumberField($options);
  131. break;
  132. case "textarea":
  133. self::renderTextareaField($options);
  134. break;
  135. case "file":
  136. self::renderFileField($options);
  137. break;
  138. }
  139. }
  140. private static function renderHeader($options) {
  141. ?>
  142. <h3><?=htmlspecialchars($options->content)?></h3>
  143. <?php
  144. }
  145. private static function renderTextField($options) {
  146. $id = str_replace("_", "-", $options->name);
  147. $placeholder = "Enter " . strtolower($options->label);
  148. $value = $options->value ?? http::value($options->name);
  149. $required = ($options->required ?? false) ? "required" : "";
  150. $help_text = $options->helpText ?? false;
  151. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  152. ?>
  153. <div class="row mb-1">
  154. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  155. <div class="col-sm">
  156. <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
  157. <?php
  158. if($help_text) {
  159. ?>
  160. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  161. <?php
  162. }
  163. ?>
  164. </div>
  165. </div>
  166. <?php
  167. }
  168. private static function renderNumberField($options) {
  169. $id = str_replace("_", "-", $options->name);
  170. $placeholder = "Enter " . strtolower($options->label);
  171. $value = $options->value ?? http::value($options->name);
  172. $required = ($options->required ?? false) ? "required" : "";
  173. $help_text = $options->helpText ?? false;
  174. $min = ($options->min ?? false) ? "min=\"$options->min\"" : "";
  175. $max = ($options->max ?? false) ? "max=\"$options->max\"" : "";
  176. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  177. $step = $options->step ?? 1;
  178. $prepend_text = $options->prependText ?? "";
  179. ?>
  180. <div class="row mb-1">
  181. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  182. <div class="col-sm">
  183. <div class="input-group">
  184. <?php
  185. if(!empty($prepend_text)) {
  186. ?>
  187. <div class="input-group-prepend">
  188. <span class="input-group-text"><?=htmlspecialchars($prepend_text)?></span>
  189. </div>
  190. <?php
  191. }
  192. ?>
  193. <input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" step="<?=$step?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
  194. </div>
  195. <?php
  196. if($help_text) {
  197. ?>
  198. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  199. <?php
  200. }
  201. ?>
  202. </div>
  203. </div>
  204. <?php
  205. }
  206. private static function renderSelectField($options) {
  207. $id = str_replace("_", "-", $options->name);
  208. $selected_value = $options->value ?? http::value($options->name);
  209. $required = ($options->required ?? false) ? "required" : "";
  210. $help_text = $options->helpText ?? false;
  211. ?>
  212. <div class="row mb-1">
  213. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  214. <div class="col-sm">
  215. <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
  216. <?php
  217. foreach($options->values as $index => $value) {
  218. $display = $options->displayStrings[$index];
  219. $selected = ($selected_value === strval($value)) ? "selected" : "";
  220. ?>
  221. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  222. <?php
  223. }
  224. ?>
  225. </select>
  226. <?php
  227. if($help_text) {
  228. ?>
  229. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  230. <?php
  231. }
  232. ?>
  233. </div>
  234. </div>
  235. <?php
  236. }
  237. private static function renderFileField($options) {
  238. $id = str_replace("_", "-", $options->name);
  239. $placeholder = "Enter " . strtolower($options->label);
  240. $required = ($options->required ?? false) ? "required" : "";
  241. $help_text = $options->helpText ?? false;
  242. ?>
  243. <div class="row mb-1">
  244. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  245. <div class="col-sm">
  246. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
  247. <?php
  248. if($help_text) {
  249. ?>
  250. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  251. <?php
  252. }
  253. ?>
  254. </div>
  255. </div>
  256. <?php
  257. }
  258. private static function renderTextareaField($options) {
  259. $id = str_replace("_", "-", $options->name);
  260. $placeholder = "Enter " . strtolower($options->label);
  261. $value = $options->value ?? http::value($options->name);
  262. $required = ($options->required ?? false) ? "required" : "";
  263. $help_text = $options->helpText ?? false;
  264. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  265. ?>
  266. <div class="row mb-1">
  267. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  268. <div class="col-sm">
  269. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
  270. <?php
  271. if($help_text) {
  272. ?>
  273. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  274. <?php
  275. }
  276. ?>
  277. </div>
  278. </div>
  279. <?php
  280. }
  281. }
  282. ?>