form.php 9.8 KB

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