Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

helpers.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /******************************************************************************/
  3. /* POST array processing */
  4. /******************************************************************************/
  5. /* Validate variables in the POST array */
  6. /* Creating a new array with data from the POST array */
  7. function post_array_check($arr) {
  8. $new_arr = array();
  9. $new_val = '';
  10. foreach ($arr as $key => $value) {
  11. if ( !is_array($value) ) {
  12. $new_val = strip_tags(trim($value));
  13. $new_arr[$key] = htmlspecialchars($new_val, ENT_QUOTES, 'UTF-8');
  14. continue;
  15. }
  16. $str = '';
  17. foreach ($value as $v) {
  18. $str .= strip_tags(trim($v)) . ", ";
  19. $str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
  20. }
  21. $str = substr($str, 0, -2);
  22. $new_arr[$key] = $str;
  23. }
  24. return $new_arr;
  25. }
  26. /******************************************************************************/
  27. /* end POST array processing */
  28. /******************************************************************************/
  29. /******************************************************************************/
  30. /* Token processing */
  31. /******************************************************************************/
  32. function token_check($value, $rules) {
  33. foreach ($rules as $rule => $val) {
  34. // "required" validation rule
  35. if ($rule === "required") {
  36. if ($val === true) {
  37. // Create new token instance
  38. $token = new CSRF($rules["prefix"]);
  39. // Check a token
  40. return (!$token->check_token($value)) ? true : false;
  41. }
  42. }
  43. }
  44. }
  45. /******************************************************************************/
  46. /* end Token processing */
  47. /******************************************************************************/
  48. /******************************************************************************/
  49. /* File processing */
  50. /******************************************************************************/
  51. function file_check($file, $rules, $messages, $default_mime_types){
  52. $allowedTypes = array();
  53. $extensionArr = explode( "|", strtolower($rules["extension"]) );
  54. $required = $rules["required"];
  55. $validate = $rules["validate"];
  56. $size = $rules["size"] * 1024 * 1024;
  57. /* Validation */
  58. if ($validate || $required) {
  59. // Add required valid types
  60. foreach ($extensionArr as $type) {
  61. if ( !array_key_exists($type, $default_mime_types) ){
  62. continue;
  63. }
  64. if ( is_array($default_mime_types[$type]) ){
  65. $count = count($default_mime_types[$type]);
  66. for($i = 0; $i < $count; $i++) {
  67. $allowedTypes[] = $default_mime_types[$type][$i];
  68. }
  69. continue;
  70. }
  71. $allowedTypes[] = $default_mime_types[$type];
  72. }
  73. // if file is required
  74. if ($required) {
  75. // if file is empty
  76. if (empty($_FILES[$file]["name"])) {
  77. return $messages["required"];
  78. }
  79. }
  80. // if file is not required
  81. // validate file only if it exists
  82. if (!empty($_FILES[$file]["name"])) {
  83. if (!in_array($_FILES[$file]["type"], $allowedTypes)) {
  84. return $messages["size_extension"];
  85. }
  86. if ($_FILES[$file]["size"] > $size) {
  87. return $messages["size_extension"];
  88. }
  89. if (!is_uploaded_file($_FILES[$file]["tmp_name"])) {
  90. return FILE_ERROR_MESSAGE;
  91. }
  92. }
  93. }
  94. return false;
  95. }
  96. /* Upload file */
  97. function upload_file($file, $directory){
  98. $file_name = FILE_DEFAULT_NAME;
  99. if (!empty($_FILES[$file]["name"])) {
  100. $file_name = generate_file_name($file);
  101. move_uploaded_file( $_FILES[$file]["tmp_name"], $directory.$file_name );
  102. }
  103. return $file_name;
  104. }
  105. /* Generate uniq name for file */
  106. function generate_file_name($file){
  107. return date("Ymd_His")."_".mt_rand(1000,9999).'_'.strtolower($_FILES[$file]["name"]);
  108. }
  109. /******************************************************************************/
  110. /* end File processing */
  111. /******************************************************************************/
  112. /******************************************************************************/
  113. /* Validation functions */
  114. /******************************************************************************/
  115. /* Email validation */
  116. function email_check($email){
  117. $email_template = '/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/i';
  118. return (preg_match($email_template, $email) !== 1) ? true : false;
  119. }
  120. /* Url validation */
  121. function url_check($url){
  122. $url_template = "%^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?$%iu";
  123. return (preg_match($url_template, $url) !== 1) ? true : false;
  124. }
  125. /* EqualTo validation */
  126. function equal_to_check($value, $target){
  127. return ($value !== $target) ? true : false;
  128. }
  129. /* Min length validation */
  130. function min_length_check($value, $len) {
  131. return (mb_strlen(trim($value), 'UTF-8') < $len) ? true : false;
  132. }
  133. /* Max length validation */
  134. function max_length_check($value, $len) {
  135. return (mb_strlen(trim($value), 'UTF-8') > $len) ? true : false;
  136. }
  137. /* Range length validation */
  138. function range_length_check($value, $len) {
  139. $value_len = mb_strlen($value, 'UTF-8');
  140. return ( $value_len < $len[0] || $value_len > $len[1] ) ? true : false;
  141. }
  142. /* Integer validation */
  143. function integer_сheck($value){
  144. $item_template = "/^-?\d+$/";
  145. return (preg_match($item_template, $value) !== 1) ? true : false;
  146. }
  147. /* Number validation */
  148. function number_сheck($value){
  149. $item_template = "/^-?\d+(?:\.\d+)?$/";
  150. return (preg_match($item_template, $value) !== 1) ? true : false;
  151. }
  152. /* Min value validation */
  153. function min_value_сheck($value, $val){
  154. if ( !number_сheck($value) || !integer_сheck($value) ) {
  155. return ( $value < $val ) ? true : false;
  156. }
  157. return true;
  158. }
  159. /* Max value validation */
  160. function max_value_сheck($value, $val){
  161. if ( !number_сheck($value) || !integer_сheck($value) ) {
  162. return ( $value > $val ) ? true : false;
  163. }
  164. return true;
  165. }
  166. /* Range value validation */
  167. function range_value_сheck($value, $val){
  168. if ( !number_сheck($value) || !integer_сheck($value) ) {
  169. return ( $value < $val[0] || $value > $val[1] ) ? true : false;
  170. }
  171. return true;
  172. }
  173. /******************************************************************************/
  174. /* end Validation functions */
  175. /******************************************************************************/
  176. /******************************************************************************/
  177. /* Debugger */
  178. /******************************************************************************/
  179. function debugger($data){
  180. if (count($data) === 1) {
  181. echo json_encode(array("error" => $data[0]));
  182. return false;
  183. }
  184. $result = $data[0]."<ul>";
  185. if ( is_string($data[1]) ){
  186. $result .= "<li>".$data[1]."</li></ul>";
  187. echo json_encode(array("error" => $result));
  188. return false;
  189. }
  190. if ( is_array($data[1]) ){
  191. foreach ($data[1] as $msg) {
  192. $result .= "<li>".$msg."</li>";
  193. }
  194. $result .= "</ul>";
  195. echo json_encode(array("error" => $result));
  196. return false;
  197. }
  198. }
  199. /******************************************************************************/
  200. /* end Debugger */
  201. /******************************************************************************/
  202. /******************************************************************************/
  203. /* Email template processing */
  204. /******************************************************************************/
  205. function row_name($name){
  206. return str_replace( "_", " ", ucfirst($name) );
  207. }
  208. /******************************************************************************/
  209. /* end Email template processing */
  210. /******************************************************************************/
  211. /******************************************************************************/
  212. /* Directory processing */
  213. /******************************************************************************/
  214. function directory_check($dir_name){
  215. clearstatcache();
  216. $result = ROOT.DIRECTORY_SEPARATOR.DEFAULT_DIRECTORY;
  217. $skip = array(".", "..");
  218. $files = scandir(ROOT);
  219. foreach($files as $file) {
  220. if (in_array($file, $skip)){
  221. continue;
  222. }
  223. if( $file === $dir_name && is_dir(ROOT.DIRECTORY_SEPARATOR.$file) ){
  224. $result = ROOT.DIRECTORY_SEPARATOR.$file;
  225. }
  226. }
  227. return $result.DIRECTORY_SEPARATOR;
  228. }
  229. function get_file($root, $target){
  230. clearstatcache();
  231. $result = false;
  232. $skip = array(".", "..", "php");
  233. $files = scandir($root);
  234. foreach($files as $file) {
  235. if (in_array($file, $skip)){
  236. continue;
  237. }
  238. if ( $file === $target && is_file($root.DIRECTORY_SEPARATOR.$file) ){
  239. $result = $root.DIRECTORY_SEPARATOR.$file;
  240. break;
  241. }
  242. if ( $result === false && is_dir($root.DIRECTORY_SEPARATOR.$file) ){
  243. $result = get_file($root.DIRECTORY_SEPARATOR.$file, $target);
  244. }
  245. }
  246. return $result;
  247. }
  248. /******************************************************************************/
  249. /* end Directory processing */
  250. /******************************************************************************/
  251. /******************************************************************************/
  252. /* Config validation */
  253. /******************************************************************************/
  254. function config_check($config){
  255. $result = array();
  256. if (!isset($config["rules"]) || !isset($config["messages"])){
  257. $result[] = CONFIG_NOT_FOUND_ERROR_MESSAGE;
  258. return $result;
  259. }
  260. /* Check validation rules */
  261. foreach ($config["rules"] as $field => $rules) {
  262. foreach ($rules as $rule => $value) {
  263. switch($rule) {
  264. case "required":
  265. case "alternative_mode":
  266. case "email":
  267. case "url":
  268. case "integer":
  269. case "number":
  270. case "validate":
  271. if (!is_bool($value)){
  272. $result[] = error_message($field, $rule, $value);
  273. }
  274. break;
  275. case "minlength":
  276. case "maxlength":
  277. if (!is_int($value)) {
  278. $result[] = error_message($field, $rule, $value);
  279. }
  280. break;
  281. case "minvalue":
  282. case "maxvalue":
  283. if (!is_numeric($value)) {
  284. $result[] = error_message($field, $rule, $value);
  285. }
  286. break;
  287. case "rangelength":
  288. if (!is_array($value) ||
  289. count($value) !== 2 ||
  290. !is_int($value[0]) ||
  291. !is_int($value[1]) ||
  292. $value[0] >= $value[1]) {
  293. $result[] = error_message($field, $rule, $value);
  294. }
  295. break;
  296. case "rangevalue":
  297. if (!is_array($value) ||
  298. count($value) !== 2 ||
  299. !is_numeric($value[0]) ||
  300. !is_numeric($value[1]) ||
  301. $value[0] >= $value[1]) {
  302. $result[] = error_message($field, $rule, $value);
  303. }
  304. break;
  305. case "size":
  306. if (!is_numeric($value) || $value <= 0) {
  307. $result[] = error_message($field, $rule, $value);
  308. }
  309. break;
  310. case "prefix":
  311. case "extension":
  312. case "equalTo":
  313. if (!is_string($value)) {
  314. $result[] = error_message($field, $rule, $value);
  315. }
  316. break;
  317. case "requiredFromGroup":
  318. if (!is_array($value) ||
  319. !is_int($value[0]) ||
  320. !is_array($value[1]) ||
  321. count($value) !== 2 ||
  322. $value[0] > count($value[1])) {
  323. $result[] = error_message($field, $rule, $value);
  324. }
  325. break;
  326. default:
  327. $result[] = CONFIG_UNKNOWN_RULE_ERROR_MESSAGE.$field." => ".$rule;
  328. }
  329. }
  330. }
  331. /* Check an accordance of the rules and messages */
  332. if ( array_diff_key($config["rules"], $config["messages"]) ||
  333. array_diff_key($config["rules"], $config["messages"])){
  334. $result[] = CONFIG_RULES_MESSAGES_ERROR_MESSAGE;
  335. }
  336. return $result;
  337. }
  338. function error_message($field, $rule, $value){
  339. $result = is_array($value) ? "Array" : $value;
  340. return CONFIG_NOT_ALLOWED_VALUE_ERROR_MESSAGE.$field.": ".$rule." => ".$result;
  341. }
  342. /******************************************************************************/
  343. /* end Config validation */
  344. /******************************************************************************/
  345. ?>