PageRenderTime 50ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/system/engine/engine_library/inputtools.enginelib.php

https://bitbucket.org/threetopia/thtech
PHP | 399 lines | 342 code | 18 blank | 39 comment | 42 complexity | 581317aebd14b5dfc5539e4b6a66c9b6 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * File name : inputtools.enignelib.php
  4. * Author : Tri Hartanto
  5. * Site : trihartanto.com
  6. * Framework : thtech
  7. * Library type : Engine Library
  8. * Version : 1.7
  9. * License : GPL
  10. * Create Date : 11 Dec 2013
  11. * Modified Date : 20 Feb 2014
  12. * File Description : This file contains inputtools class to be used by the framework and CMS system.
  13. *
  14. * For more license information please kindly open and read LICENSE.txt file
  15. */
  16. class InputTools_EngineLibrary extends Systems
  17. {
  18. /*
  19. * @filter
  20. * How to : (key,value,filter,title,source)
  21. * - key = name of the field {array}(name="[username][password][etc]")
  22. * - value = field value (value="[somevalue]"|option="[someoption]")
  23. * - filter = filter on field ('unique','email','required',etc)
  24. * - title = field title ([Username][Password])
  25. * - source = POST|GET|FILES
  26. */
  27. public function filter($key=NULL,$values=array(),$filter=array(),$title=array(),$method='_POST')
  28. {
  29. //var_dump($values);echo'<br><br>';
  30. $result = array();
  31. $validation = array();
  32. $message = array();
  33. $source = ($method==='_FILES' and !empty($_FILES))?$_FILES:(($method==='_POST' and !empty($_POST))?$_POST:(($method==='_GET' and !empty($_GET))?$_GET:$values));
  34. $check = (!empty($key))?((is_array($key))?$key:array($key)):array_keys($source);
  35. if(!empty($check))
  36. {
  37. foreach($check as $ck=>$cv)
  38. {
  39. $val = (isset($source[$cv]))?$source[$cv]:NULL;
  40. $filter = (isset($filter[$cv]))?$filter[$cv]:$filter;
  41. if(!empty($filter))
  42. {
  43. foreach($filter as $fk=>$fv)
  44. {
  45. $method = 'filter_'.((!is_numeric($fk))?$fk:$fv);
  46. $filtered = (method_exists($this,$method))?$this->{$method}(((isset($title[$cv]))?$title[$cv]:$cv),$val,((isset($values[$cv]))?$values[$cv]:$values),$filter,$fv,$source):array('val'=>$val,'validation'=>true);
  47. $val = $filtered['val'];
  48. $validation[$cv][] = $filtered['validation'];
  49. $message[$cv] = (!empty($filtered['message']) and empty($message[$cv]))?$filtered['message']:((isset($message[$cv]))?$message[$cv]:'');
  50. }
  51. $validation[$cv] = (!in_array(false,$validation[$cv]))?true:false;
  52. }
  53. $result[$cv] = $val;
  54. }
  55. }
  56. return array('result'=>$result,'validation'=>$validation,'message'=>$message);
  57. }
  58. /*
  59. * Below are library function used by filter
  60. * How to :
  61. * filter_{libraryname}(key,val,values,filter,fv)
  62. * - key = field key name
  63. * - val = field current/sended value or output
  64. * - values = field predifined values such option values in select
  65. * - filter = applied filter on field
  66. * - fv = filter value to be used for designed filter
  67. * - source = object source _POST or _GET
  68. * Sample :
  69. * filter_range('city','jakarta',array('jakarta','surabaya','semarang','medan'),array('required','range'),array('jakarta'=>array('barat')),$_POST)
  70. */
  71. private function filter_striptags($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  72. {
  73. $validation = true;
  74. $message = '';
  75. $tags = (isset($filter['striptags']))?$filter['striptags']:'';
  76. if(!empty($val))
  77. {
  78. if(is_array($val))
  79. {
  80. foreach($val as $key0=>$val0)
  81. {
  82. $val[$key0] = strip_tags($val0,$tags);
  83. }
  84. }
  85. else
  86. {
  87. $val = strip_tags($val,$tags);
  88. }
  89. }
  90. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  91. }
  92. private function filter_htmlentities($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  93. {
  94. $validation = true;
  95. $message = '';
  96. if(!empty($val))
  97. {
  98. if(is_array($val))
  99. {
  100. foreach($val as $key0=>$val0)
  101. {
  102. $val[$key0] = htmlentities($val0);
  103. }
  104. }
  105. else
  106. {
  107. $val = htmlentities($val);
  108. }
  109. }
  110. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  111. }
  112. private function filter_filetype($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  113. {
  114. $available_extension = array();
  115. $validation = true;
  116. $message = '';
  117. //echo count($val['name']);
  118. if(!empty($val['type']) and !empty($val['name'][0]))
  119. {
  120. foreach($val['type'] as $ktype=>$vtype)
  121. {
  122. $validation = (!in_array($vtype,$fv) and $validation==true)?false:$validation;
  123. }
  124. $message = ($validation===false)?$key.' field supposed to be '.implode(', ',$fv):NULL;
  125. }
  126. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  127. }
  128. private function filter_filemaxsize($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  129. {
  130. $available_extension = array();
  131. $validation = true;
  132. $message = '';
  133. $fv = str_replace(array('kb','mb','gb'),array('000','000000','000000000'),strtolower($fv));
  134. if(!empty($val['size']) and !empty($val['name'][0]))
  135. {
  136. foreach($val['size'] as $ksize=>$vsize)
  137. {
  138. $validation = ($vsize>$fv)?false:$validation;
  139. }
  140. $fv = str_replace(array('000000000','000000','000'),array('gb','mb','kb'),strtolower($fv));
  141. $message = ($validation===false)?$key.' file size supposed to be not more than '.$fv:NULL;
  142. }
  143. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  144. }
  145. private function filter_fileminsize($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  146. {
  147. $available_extension = array();
  148. $validation = true;
  149. $message = '';
  150. $fv = str_replace(array('kb','mb','gb'),array('000','000000','000000000'),strtolower($fv));
  151. if(!empty($val['size']) and !empty($val['name'][0]))
  152. {
  153. foreach($val['size'] as $ksize=>$vsize)
  154. {
  155. $validation = ($vsize<$fv)?false:$validation;
  156. }
  157. $message = ($validation===false)?$key.' file size supposed to be more than '.$fv:NULL;
  158. }
  159. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  160. }
  161. private function filter_disabled($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  162. {
  163. $validation = true;
  164. $message = '';
  165. if(is_array($fv))
  166. {
  167. if(is_array($val) and array_intersect($val,$fv))
  168. {
  169. $validation = false;
  170. $message = 'Some of '.$key.' field supposed to be disabled';
  171. }
  172. else if(!is_array($val) and in_array($val,$fv))
  173. {
  174. $validation = false;
  175. $message = 'Some of '.$key.' field supposed to be disabled';
  176. }
  177. }
  178. else if(!is_array($fv))
  179. {
  180. if(!is_array($val) and !empty($val) and $val!=$values)
  181. {
  182. $validation = false;
  183. $message = $key.' field supposed to be disabled';
  184. }
  185. else
  186. {
  187. $val = NULL;
  188. }
  189. }
  190. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  191. }
  192. private function filter_required($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  193. {
  194. $validation = true;
  195. $message = '';
  196. if(($val=='' or (is_array($val) and (empty($val) or (isset($val['name'][0]) and empty($val['name'][0]))))) and !in_array('disabled',$filter))
  197. {
  198. $validation = false;
  199. $message = $key.' field is required';
  200. }
  201. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  202. }
  203. private function filter_email($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  204. {
  205. $validation = true;
  206. $message = '';
  207. $tempval = $val;
  208. $val = filter_var($val,FILTER_VALIDATE_EMAIL);
  209. if($val===false)
  210. {
  211. $val = $tempval;
  212. $validation = false;
  213. $message = $key.' field have an incorrect format';
  214. }
  215. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  216. }
  217. private function filter_range($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  218. {
  219. //print_r($val);print_r($values);echo '<br>'.$key.'<br><br>';
  220. $validation = true;
  221. $message = '';
  222. if(is_array($values))
  223. {
  224. $values = array_keys(array_filter($values));
  225. if(((!is_array($val) and !in_array($val,$values)) or (is_array($val) and !array_intersect($val,$values))) and $val!='')
  226. {
  227. $validation = false;
  228. $message = $key.' field value is out of range';
  229. }
  230. }
  231. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  232. }
  233. private function filter_date($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  234. {
  235. $validation = true;
  236. $message = '';
  237. $date = explode('/',$val);
  238. if(!empty($date) and $date[0]>12 or $date[0]<1 or !is_numeric($date[0]))
  239. {
  240. $validation = false;
  241. $message = 'There is somethi\'n wrong with a month value';
  242. }
  243. else if(!empty($date) and $date[1]>((!empty($date[0])?date('t',$date[0]):date('t'))) or $date[1]<1 or !is_numeric($date[0]))
  244. {
  245. $validation = false;
  246. $message = 'There is somethi\'n wrong with a day value';
  247. }
  248. else if(!empty($date) and $date[0]>12 or $date[0]<1 or !is_numeric($date[0]))
  249. {
  250. $validation = false;
  251. $message = 'There is somethi\'n wrong with a year value';
  252. }
  253. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  254. }
  255. private function filter_max($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  256. {
  257. $validation = true;
  258. $message = '';
  259. if($val>$fv)
  260. {
  261. $validation = false;
  262. $message = $key.' field must lower than '.$fv;
  263. }
  264. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  265. }
  266. private function filter_min($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  267. {
  268. $validation = true;
  269. $message = '';
  270. if($val<$fv)
  271. {
  272. $validation = false;
  273. $message = $key.' field must greater than '.$fv;
  274. }
  275. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  276. }
  277. private function filter_sameas($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  278. {
  279. $validation = true;
  280. $message = '';
  281. if(isset($source[$fv]) and $val!=$source[$fv])
  282. {
  283. $validation = false;
  284. $message = $key.' must be same with '.$fv;
  285. }
  286. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  287. }
  288. private function filter_unique($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  289. {
  290. $validation = true;
  291. $message = '';
  292. $unique = (!is_array($fv))?array($fv):$fv;
  293. if(!empty($unique) and !empty($val) and !empty($fv))
  294. {
  295. foreach($unique as $k=>$v)
  296. {
  297. if(!empty($v) and is_numeric($k))
  298. {
  299. $Explode = explode('.',$v);
  300. $CountExplode = count($Explode);
  301. if($CountExplode==2)
  302. {
  303. $SQLSelect[] = $Explode[1];
  304. $SQLFrom[] = $Explode[0];
  305. $SQLWhere[$Explode[1]] = $val;
  306. }
  307. else if($CountExplode==1)
  308. {
  309. $SQLWhere[] = $v;
  310. }
  311. }
  312. else if(!empty($v) and !is_numeric($k))
  313. {
  314. $SQLWhere[$k] = $v;
  315. }
  316. }
  317. $Execute = $this->_SysEngine->dbtools->Execute(array('SQL'=>array("SQLSelect"=>$SQLSelect,"SQLFrom"=>$SQLFrom,"SQLWhere"=>array("SQLBINARY"=>$SQLWhere))));
  318. if(!empty($Execute['result']))
  319. {
  320. $validation = false;
  321. $message = $key.' field must be unique';
  322. }
  323. }
  324. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  325. }
  326. private function filter_maxlength($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  327. {
  328. $validation = true;
  329. $message = '';
  330. if(strlen($val) > $fv)
  331. {
  332. $validation = false;
  333. $message = $key.' field length is exceed of alowed length ('.$fv.')';
  334. }
  335. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  336. }
  337. private function filter_minlength($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  338. {
  339. $validation = true;
  340. $message = '';
  341. if(strlen($val) < $fv)
  342. {
  343. $validation = false;
  344. $message = $key.'field length is exceed of alowed length ('.$fv.')';
  345. }
  346. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  347. }
  348. private function filter_isnumeric($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  349. {
  350. $validation = true;
  351. $message = '';
  352. if(!is_array($val) and $val!='' and !is_numeric($val))
  353. {
  354. $validation = false;
  355. $message = $key.' field must be numeric';
  356. }
  357. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  358. }
  359. private function filter_recaptcha($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  360. {
  361. $recaptcha_challenge_field = (isset($source['recaptcha_challenge_field']))?$source['recaptcha_challenge_field']:'';
  362. $validation = $this->_SysEngine->recaptchatools->checkanswer($recaptcha_challenge_field,$val,NULL)->is_valid;
  363. $message = '';
  364. if($validation==false)
  365. {
  366. $message = $key.' field is not correct';
  367. }
  368. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  369. }
  370. private function filter_lowercase($key=NULL,$val=NULL,$values=NULL,$filter=NULL,$fv=NULL,$source=NULL)
  371. {
  372. $validation = true;
  373. $message = '';
  374. if(!empty($val))
  375. {
  376. $val = strtolower($val);
  377. }
  378. return array('val'=>$val,'validation'=>$validation,'message'=>$message);
  379. }
  380. }
  381. ?>