form.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. case "hr":
  152. self::renderHorizontalBar($options);
  153. break;
  154. }
  155. }
  156. private static function renderHeader($options) {
  157. ?>
  158. <h3><?=htmlspecialchars($options->content)?></h3>
  159. <?php
  160. }
  161. /**
  162. * @param object $options
  163. * @throws Exception
  164. */
  165. private static function renderTextField(object $options) {
  166. $id = str_replace("_", "-", $options->name);
  167. $placeholder = "Enter " . strtolower($options->label);
  168. $value = $options->value ?? http::value($options->name);
  169. $required = ($options->required ?? false) ? "required" : "";
  170. $help_text = $options->helpText ?? false;
  171. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  172. ?>
  173. <div class="row mb-1">
  174. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  175. <div class="col-sm">
  176. <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
  177. <?php
  178. if($help_text) {
  179. ?>
  180. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  181. <?php
  182. }
  183. ?>
  184. </div>
  185. </div>
  186. <?php
  187. }
  188. /**
  189. * @param object $options
  190. * @throws Exception
  191. */
  192. private static function renderNumberField(object $options) {
  193. $id = str_replace("_", "-", $options->name);
  194. $placeholder = "Enter " . strtolower($options->label);
  195. $value = $options->value ?? http::value($options->name);
  196. $required = ($options->required ?? false) ? "required" : "";
  197. $help_text = $options->helpText ?? false;
  198. $min = ($options->min ?? false) ? "min=\"$options->min\"" : "";
  199. $max = ($options->max ?? false) ? "max=\"$options->max\"" : "";
  200. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  201. $step = $options->step ?? 1;
  202. $prepend_text = $options->prependText ?? "";
  203. ?>
  204. <div class="row mb-1">
  205. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  206. <div class="col-sm">
  207. <div class="input-group">
  208. <?php
  209. if(!empty($prepend_text)) {
  210. ?>
  211. <div class="input-group-prepend">
  212. <span class="input-group-text"><?=htmlspecialchars($prepend_text)?></span>
  213. </div>
  214. <?php
  215. }
  216. ?>
  217. <input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" step="<?=$step?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
  218. </div>
  219. <?php
  220. if($help_text) {
  221. ?>
  222. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  223. <?php
  224. }
  225. ?>
  226. </div>
  227. </div>
  228. <?php
  229. }
  230. /**
  231. * @param object $options
  232. * @throws Exception
  233. */
  234. private static function renderSelectField(object $options) {
  235. $id = str_replace("_", "-", $options->name);
  236. $selected_value = $options->value ?? http::value($options->name);
  237. $required = ($options->required ?? false) ? "required" : "";
  238. $help_text = $options->helpText ?? false;
  239. ?>
  240. <div class="row mb-1">
  241. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  242. <div class="col-sm">
  243. <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
  244. <?php
  245. foreach($options->values as $index => $value) {
  246. $display = $options->displayStrings[$index];
  247. $selected = ($selected_value === strval($value)) ? "selected" : "";
  248. ?>
  249. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  250. <?php
  251. }
  252. ?>
  253. </select>
  254. <?php
  255. if($help_text) {
  256. ?>
  257. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  258. <?php
  259. }
  260. ?>
  261. </div>
  262. </div>
  263. <?php
  264. }
  265. /**
  266. * @param object $options
  267. */
  268. private static function renderFileField(object $options) {
  269. $id = str_replace("_", "-", $options->name);
  270. $required = ($options->required ?? false) ? "required" : "";
  271. $help_text = $options->helpText ?? false;
  272. ?>
  273. <div class="row mb-1">
  274. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  275. <div class="col-sm">
  276. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
  277. <?php
  278. if($help_text) {
  279. ?>
  280. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  281. <?php
  282. }
  283. ?>
  284. </div>
  285. </div>
  286. <?php
  287. }
  288. /**
  289. * @param object $options
  290. * @throws Exception
  291. */
  292. private static function renderTextareaField(object $options) {
  293. $id = str_replace("_", "-", $options->name);
  294. $placeholder = "Enter " . strtolower($options->label);
  295. $value = $options->value ?? http::value($options->name);
  296. $required = ($options->required ?? false) ? "required" : "";
  297. $help_text = $options->helpText ?? false;
  298. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  299. ?>
  300. <div class="row mb-1">
  301. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  302. <div class="col-sm">
  303. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
  304. <?php
  305. if($help_text) {
  306. ?>
  307. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  308. <?php
  309. }
  310. ?>
  311. </div>
  312. </div>
  313. <?php
  314. }
  315. private static function renderHorizontalBar(object $options) {
  316. ?>
  317. <hr />
  318. <?php
  319. }
  320. }
  321. ?>