form.php 9.2 KB

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