PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/helpers/forms.php

https://github.com/MilkZoft/zan
PHP | 388 lines | 388 code | 0 blank | 0 comment | 1 complexity | 3ac734f12861ccf0c2e20f71948e82ae MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. if (!defined("ACCESS")) {
  3. die("Error: You don't have permission to access here...");
  4. }
  5. if (!function_exists("formCheckbox")) {
  6. function formCheckbox($attributes = false)
  7. {
  8. if (isset($attributes) and is_array($attributes)) {
  9. $attrs = null;
  10. foreach ($attributes as $attribute => $value) {
  11. if ($attribute !== "position" and $attribute !== "text" and $attribute !== "type" and $attribute !== "checked") {
  12. $attrs .= ' '. strtolower($attribute) .'="'. encode($value) .'"';
  13. } else {
  14. $$attribute = encode($value);
  15. }
  16. }
  17. $check = (isset($checked) and $checked) ? ' checked="checked"' : null;
  18. if (isset($position) and $position === "left" and isset($text)) {
  19. return ''. decode($text) .' <input'. $attrs .' type="checkbox"'. $check .' /> ';
  20. } elseif (isset($position) and $position === "right" and isset($text)) {
  21. return '<input'. $attrs .' type="checkbox"'. $check .' /> '. decode($text) .' ';
  22. } elseif (isset($text)) {
  23. return ''. decode($text) .' <input'. $attrs .' type="checkbox"'. $check .' /> ';
  24. } else {
  25. return '<input'. $attrs .' type="checkbox"'. $check .' /> ';
  26. }
  27. } else {
  28. return null;
  29. }
  30. }
  31. }
  32. if (!function_exists("formClose")) {
  33. function formClose()
  34. {
  35. $HTML = "</fieldset>";
  36. $HTML .= "</form>";
  37. return $HTML;
  38. }
  39. }
  40. if (!function_exists("formField")) {
  41. function formField($a = null, $text, $raquo = true)
  42. {
  43. $raquo = ($raquo === true) ? "&raquo; " : "";
  44. if (!is_null($a)) {
  45. $HTML = '<p class="field">';
  46. $HTML .= '<a '. $a .'>'. $raquo . $text .'</a>';
  47. $HTML .= '</p>';
  48. } else {
  49. $HTML = '<p class="field">';
  50. $HTML .= $raquo . $text;
  51. $HTML .= '</p>';
  52. }
  53. return $HTML;
  54. }
  55. }
  56. if (!function_exists("formInput")) {
  57. function formInput($attributes = false, $disabled = false)
  58. {
  59. if (isset($attributes) and is_array($attributes)) {
  60. $attrs = ($disabled) ? ' disabled ' : null;
  61. foreach ($attributes as $attribute => $value) {
  62. if ($attribute === "required") {
  63. $attrs .= ' required ';
  64. } elseif ($attribute === "events") {
  65. $attrs .= ' '. $value .' ';
  66. } elseif ($attribute !== "type" and $attribute !== "p" and $attribute !== "field" and $attribute !== "checked") {
  67. if (!preg_match('/"/', $value)) {
  68. $attrs .= ' '. strtolower($attribute) .'="'. $value .'"';
  69. } else {
  70. $attrs .= ' '. strtolower($attribute) ."='". $value ."'";
  71. }
  72. } else {
  73. $$attribute = $value;
  74. }
  75. }
  76. $check = (isset($checked) and $checked) ? ' checked="checked"' : null;
  77. if (isset($type)) {
  78. if ($type === "text") {
  79. $HTML = ' <input'. $attrs .' type="text" /> ';
  80. } elseif ($type === "password") {
  81. $HTML = ' <input'. $attrs .' type="password" /> ';
  82. } elseif ($type === "submit") {
  83. $HTML = ' <input'. $attrs .' type="submit" /> ';
  84. } elseif ($type === "button") {
  85. $HTML = ' <input'. $attrs .' type="button" /> ';
  86. } elseif ($type === "checkbox") {
  87. $HTML = ' <input'. $attrs .' type="checkbox"'. $check .'/> ';
  88. } elseif ($type === "radio") {
  89. $HTML = ' <input'. $attrs .' type="radio"'. $check .' /> ';
  90. } elseif ($type === "file") {
  91. $HTML = ' <input'. $attrs .' type="file" /> ';
  92. } elseif ($type === "hidden") {
  93. $HTML = ' <input'. $attrs .' type="hidden" /> ';
  94. } elseif ($type === "image") {
  95. $HTML = ' <input'. $attrs .' type="image" /> ';
  96. } elseif ($type === "reset") {
  97. $HTML = ' <input'. $attrs .' type="reset" /> ';
  98. } elseif ($type === "url") {
  99. $HTML = ' <input'. $attrs .' type="url" /> ';
  100. } elseif ($type === "email") {
  101. $HTML = ' <input'. $attrs .' type="email" /> ';
  102. } else {
  103. $HTML = ' <input'. $attrs .' type="text" /> ';
  104. }
  105. } else {
  106. $HTML = ' <input'. $attrs .' type="text" /> ';
  107. }
  108. if (isset($p) and $p and isset($field)) {
  109. $HTML = '<p><span class="field">&raquo; '. $field .'</span><br />'. $HTML .'</p>';
  110. } elseif (isset($p) and $p) {
  111. $HTML = '<p>'. $HTML .'</p>';
  112. } elseif (isset($field)) {
  113. $HTML = '<span class="field">&raquo; '. $field .'</span><br />'. $HTML .'';
  114. }
  115. return $HTML;
  116. } elseif ($attributes) {
  117. return ' <input name="'. $attributes .'" type="text" /> ';
  118. } else {
  119. return null;
  120. }
  121. }
  122. }
  123. if (!function_exists("formLabel")) {
  124. function formLabel($for, $text, $br = true)
  125. {
  126. $HTML = "<label for=\"$for\">$text: </label>";
  127. if ($br == true) {
  128. $HTML .= "<br />";
  129. }
  130. return $HTML;
  131. }
  132. }
  133. if (!function_exists("formOpen")) {
  134. function formOpen($action = null, $class = "forms", $ID = null, $legend = null, $method = "post", $enctype = null)
  135. {
  136. $ID = (isset($ID)) ? ' id="'. $ID .'"' : null;
  137. $legend = (isset($legend)) ? "<legend>$legend</legend>\n" : null;
  138. $action = (strstr($action, "http://")) ? $action : _get("webBase") ."/". $action;
  139. $enctype = (!is_null($enctype)) ? ' enctype="'. $enctype .'"' : null;
  140. $HTML = '<form'. $ID .' action="'. $action .'" method="'. $method .'" class="'. $class .'"'. $enctype .'>' . "\n\t";
  141. $HTML .= '<fieldset>' . "\n\t\t";
  142. $HTML .= $legend;
  143. return $HTML;
  144. }
  145. }
  146. if (!function_exists("formRadio")) {
  147. function formRadio($attributes, $options = false)
  148. {
  149. if (isset($attributes) and is_array($attributes)) {
  150. $attrs = null;
  151. foreach ($attributes as $attr => $value) {
  152. if ($attr !== "position" and $attr !== "text" and $attr !== "type" and $attr !== "p" and $attr !== "field" and $attr !== "checked") {
  153. $attrs .= ' '. strtolower($attr) .'="'. $value .'"';
  154. } else {
  155. $$attr = $value;
  156. }
  157. }
  158. if (is_array($options)) {
  159. $HTML = null;
  160. foreach ($options as $option) {
  161. if (is_array($option)) {
  162. foreach ($option as $attribute) {
  163. if ($attribute["default"]) {
  164. $check = ' checked="checked"';
  165. } else {
  166. $check = null;
  167. }
  168. $HTML .= ' <input '. $attrs .' value="'. $attribute["name"] .'" type="radio"'. $check .' />'. $attribute["value"];
  169. }
  170. }
  171. }
  172. } else {
  173. $check = (isset($checked) and $checked) ? ' checked="checked"' : null;
  174. if (isset($position) and $position === "left" and isset($text)) {
  175. $HTML = $text . ' <input'. $attrs .' type="radio"'. $check .' />';
  176. } elseif (isset($position) and $position === "right" and isset($text)) {
  177. $HTML = '<input'. $attrs .' type="radio"'. $check .' /> '. $text;
  178. } elseif (isset($text)) {
  179. $HTML = $text . ' <input'. $attrs .' type="radio"'. $check .' />';
  180. } else {
  181. $HTML = '<input'. $attrs .' type="radio"'. $check .' />';
  182. }
  183. }
  184. if (isset($p) and isset($field)) {
  185. $HTML = '<p><span class="field">&raquo; '. $field .'</span><br />'. $HTML .'</p>';
  186. }
  187. return $HTML;
  188. } else {
  189. return null;
  190. }
  191. }
  192. }
  193. if (!function_exists("formSelect")) {
  194. function formSelect($attributes = false, $options = false, $select = false)
  195. {
  196. if (isset($attributes) and is_array($attributes)) {
  197. $attrs = null;
  198. foreach ($attributes as $attribute => $value) {
  199. if ($attribute !== "p" and $attribute !== "field") {
  200. if ($attribute !== "disabled") {
  201. $attrs .= ' '. strtolower($attribute) .'="'. $value .'"';
  202. } elseif ($value) {
  203. $attrs .= ' disabled';
  204. }
  205. } else {
  206. $$attribute = $value;
  207. }
  208. }
  209. $HTML = '<select'. $attrs .' size="1">';
  210. if (is_array($options)) {
  211. foreach ($options as $option) {
  212. if ($select) {
  213. $HTML .= $select;
  214. $select = false;
  215. }
  216. if (is_string($option)) {
  217. $HTML .= '<option>'. $option .'</option>';
  218. } else {
  219. $selected = (isset($option["selected"]) and $option["selected"]) ? ' selected="selected"' : null;
  220. $value = (isset($option["value"])) ? $option["value"] : null;
  221. $text = (isset($option["option"])) ? $option["option"] : null;
  222. $HTML .= '<option value="'. $value .'"'. $selected .'>'. $text .'</option>';
  223. }
  224. }
  225. }
  226. $HTML .= '</select>';
  227. unset($options);
  228. if (isset($p) and isset($field)) {
  229. $HTML = '<p><span class="field">&raquo; '. $field .'</span><br />'. $HTML .'</p>';
  230. }
  231. return $HTML;
  232. } else {
  233. return null;
  234. }
  235. }
  236. }
  237. if (!function_exists("formTextarea")) {
  238. function formTextarea($attributes = false)
  239. {
  240. if (isset($attributes) and is_array($attributes)) {
  241. $attrs = null;
  242. foreach ($attributes as $attribute => $val) {
  243. if ($attribute !== "type" and $attribute !== "value" and $attribute !== "p" and $attribute !== "field") {
  244. $attrs .= ' '. strtolower($attribute) .'="'. $val .'"';
  245. } else {
  246. $$attribute = $val;
  247. }
  248. }
  249. $value = isset($value) ? $value : null;
  250. $HTML = '<textarea'. $attrs .'>'. $value .'</textarea>';
  251. if (isset($p) and isset($field)) {
  252. $HTML = '<p><span class="field">&raquo; '. $field .'</span><br />'. $HTML .'</p>';
  253. }
  254. return $HTML;
  255. } else {
  256. return null;
  257. }
  258. }
  259. }
  260. if (!function_exists("formAction")) {
  261. function formAction($action = null)
  262. {
  263. return '<p class="save-cancel">
  264. <input id="'. $action .'" name="'. $action .'" value="'. __(ucfirst($action)) .'" type="submit" class="btn btn-success">
  265. <input id="cancel" name="cancel" value="'. __("Cancel") .'" type="submit" class="btn btn-danger" />
  266. </p>';
  267. }
  268. }
  269. if (!function_exists("formCaptcha")) {
  270. function formCaptcha($attributes = false, $alphanumeric = false)
  271. {
  272. $hash = md5(getURL());
  273. $HTML = '<input type="hidden" name="captcha_token" value="'. $hash .'" />';
  274. $HTML .= '<input type="hidden" name="captcha_type" value="'. ($alphanumeric ? 'alphanumeric' : 'aritmethic') .'" />';
  275. if (!$alphanumeric) {
  276. $attributes["style"] = (isset($attributes["style"]) ? $attributes["style"] : '') ."max-width: 50px; text-align: center;";
  277. $attributes["type"] = "number";
  278. $num1 = rand(1, 9);
  279. $num2 = rand(1, 9);
  280. switch(rand(1, 3)) {
  281. case 1:
  282. $operation = '-';
  283. $answer = $num1 - $num2;
  284. break;
  285. default:
  286. $operation = '+';
  287. $answer = $num1 + $num2;
  288. }
  289. $HTML .= __("How much is ") . (rand(0, 1) === 0 ? $num1 : num2str($num1, true)) .' '. $operation .' '. (rand(0, 1) === 0 ? $num2 : num2str($num2, true)) .'? ';
  290. } else {
  291. $attributes["style"] = (isset($attributes["style"]) ? $attributes["style"] : '') . "max-width: 200px; text-align: center;";
  292. $answer = "";
  293. $characters = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
  294. for ($i = 0; $i < 5; $i++) {
  295. $answer .= $characters[rand(0, count($characters) - 1)];
  296. }
  297. $HTML .= '<img src="'. path("captcha/$hash") .'" /><br />';
  298. }
  299. SESSION("ZanCaptcha". $hash, $answer);
  300. if (isset($attributes) and is_array($attributes)) {
  301. $attrs = null;
  302. foreach ($attributes as $attribute => $value) {
  303. if ($attribute === "required") {
  304. $attrs .= ' required ';
  305. } elseif ($attribute === "events") {
  306. $attrs .= ' '. $value .' ';
  307. } elseif ($attribute !== "p" and $attribute !== "field") {
  308. if (!preg_match('/"/', $value)) {
  309. $attrs .= ' '. strtolower($attribute) .'="'. $value .'"';
  310. } else {
  311. $attrs .= ' '. strtolower($attribute) ."='". $value ."'";
  312. }
  313. } else {
  314. $$attribute = $value;
  315. }
  316. }
  317. $HTML .= '<input'. $attrs .' type="text" /> ';
  318. if (isset($p) and $p and isset($field)) {
  319. $HTML = '<p><span class="field">&raquo; '. $field .'</span><br />'. $HTML .'</p>';
  320. } elseif (isset($p) and $p) {
  321. $HTML = '<p>'. $HTML .'</p>';
  322. } elseif (isset($field)) {
  323. $HTML = '<span class="field">&raquo; '. $field .'</span><br />'. $HTML .'';
  324. }
  325. return $HTML;
  326. } elseif ($attributes) {
  327. return $HTML .'<input name="'. $attributes .'" type="text" />';
  328. } else {
  329. return null;
  330. }
  331. }
  332. }