PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/jfx-private/classes/JFX/Filter.php

http://jfxcms.googlecode.com/
PHP | 319 lines | 145 code | 60 blank | 114 comment | 63 complexity | 2d881510bc3c2e37d3acebd7ed2c28b1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. ############### COPYLEFT GPLv3 LICENSE ###############
  3. ##
  4. ## JFX Version 0.2.9
  5. ## Website Management Software
  6. ## www.jfxcms.com
  7. ##
  8. ## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
  9. ##
  10. ## Anthony Gallon
  11. ## oi_antz@hotmail.com
  12. ##
  13. ## Permission is hereby granted to any person having a copy of this software
  14. ## to freely use and modify as required so long as the copyright notices
  15. ## and branding remain intact.
  16. ##
  17. ## Full license details available at http://www.jfxcms.com/license
  18. ##
  19. ############### COPYLEFT GPLv3 LICENSE ###############
  20. /**
  21. * Perform checks to verify a particular data type is of valid format
  22. * @author Anthony Gallon
  23. * @package jfx-core-classes
  24. */
  25. /**
  26. * Perform checks to verify a particular data type is of valid format
  27. * @author Anthony Gallon
  28. * @package jfx-core-classes
  29. */
  30. class JFX_Filter
  31. {
  32. /**
  33. * Check whether an email address is a valid format. Uses Halolib.
  34. *
  35. * @param string $email
  36. * @return bool $validFormat
  37. */
  38. public static function checkEmail($email){
  39. $fail = 0;
  40. return hl_ValEmail($email, $fail);
  41. //return ereg("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]{2,20}$", $email);
  42. }
  43. /**
  44. * Checks PHP code to see if it validates.
  45. * Accepts: PHP code as string with no <?php ?> brackets
  46. * Returns: true if code evaluates, false on error
  47. * Sourced from /admin/edituserplugin.php 17 November 2008
  48. * @param string $code
  49. * @return bool $validCode
  50. */
  51. public static function checkPHPCode($code){
  52. $validinfo = true;
  53. if (strrpos($code, '{') !== FALSE)
  54. {
  55. $lastopenbrace = strrpos($code, '{');
  56. $lastclosebrace = strrpos($code, '}');
  57. if ($lastopenbrace > $lastclosebrace)
  58. {
  59. JFX::addError(lang('invalidcode'));
  60. JFX::addError(lang('invalidcode_brace_missing'));
  61. $validinfo = false;
  62. }
  63. }
  64. if ($validinfo)
  65. {
  66. srand();
  67. ob_start();
  68. if (eval('function testfunction'.rand().'() {'.$code.'}') === FALSE)
  69. {
  70. $error[] = JFX::addError(lang('invalidcode'));
  71. //catch the error
  72. //eval('function testfunction'.rand().'() {'.$code.'}');
  73. $buffer = ob_get_clean();
  74. //add error
  75. JFX::addError(str_replace(array('(', ')'), '', htmlentities(strip_tags($buffer ), ENT_QUOTES)));
  76. $validinfo = false;
  77. }
  78. else
  79. {
  80. ob_get_clean();
  81. }
  82. }
  83. return (bool) $validinfo;
  84. }
  85. /**
  86. * Checks if NZ cellphone number is a valid pattern, if so it will return the formatted number.
  87. *
  88. * @param string $number
  89. * @return string or false on error
  90. */
  91. public static function formatCellphoneNumber($number){
  92. // declare valid area codes
  93. $areacodes = array('027', '021', '029', '025');
  94. // get digits
  95. $number = preg_replace('/[^0-9]/', '', $number);
  96. // strip leading country code
  97. if(substr($number, 0, 2)=='00') $number = substr($number, 2);
  98. if(substr($number, 0, 2)=='64') $number = substr($number, 2);
  99. // ensure proper area code
  100. if(substr($number, 0, 1)==0) $number = substr($number, 1);
  101. $number = '0'.$number;
  102. $number = substr($number, 0, 3).' '.substr($number, 3);
  103. $arr = explode(' ', $number);
  104. // check if phone number is valid
  105. if(count($arr)!=2) return false;
  106. if(!in_array($arr[0], $areacodes)) return false;
  107. if(strlen($arr[1])<6 || strlen($arr[1])>8) return false;
  108. // format the nubmer to ### ### ####
  109. $arr[1] = substr($arr[1], 0, 3).' '.substr($arr[1], 3);
  110. $number = implode(' ', $arr);
  111. // add area code
  112. $number = '64 '.$number;
  113. return $number;
  114. }
  115. /**
  116. * Checks if the number is a valid pattern, if so it will return the formatted number.
  117. *
  118. * @param string $number
  119. * @return string or false on error
  120. */
  121. public static function formatPhoneNumber($number){
  122. // declare valid area codes
  123. $areacodes = array('03', '04', '06', '07', '09');
  124. // get digits
  125. $number = preg_replace('/[^0-9]/', '', $number);
  126. // strip leading country code
  127. if(substr($number, 0, 2)=='00') $number = substr($number, 2);
  128. if(substr($number, 0, 2)=='64') $number = substr($number, 2);
  129. // ensure proper area code
  130. if(substr($number, 0, 1)==0) $number = substr($number, 1);
  131. $number = '0'.$number;
  132. // seperate area code
  133. if(substr($number, 0, 4)=='0800' || substr($number, 0, 4)=='0508') $number = substr($number, 0, 4).' '.substr($number, 4);
  134. if(in_array(substr($number, 0, 2), $areacodes)) $number = substr($number, 0, 2).' '.substr($number, 2);
  135. // check if valid format
  136. $arr = split(' ', $number);
  137. if(count($arr)<2) return false;
  138. if((strlen($arr[0]==2) && strlen($arr[1])<>7) || (strlen($arr[0])==4 && (strlen($arr[1])<6 || strlen($arr[1])>7))) return false;
  139. // break number into 333 3333 format
  140. $arr[1] = substr($arr[1], 0, 3).' '.substr($arr[1], 3);
  141. $number = implode(' ', $arr);
  142. // add area code
  143. $number = '64 '.$number;
  144. return $number;
  145. }
  146. /**
  147. * Checks if a number is a valid format for a credit card number
  148. *
  149. * @param string $number
  150. * @return bool $validFormat
  151. */
  152. public static function formatCreditCardNumber($number){
  153. // strip non numeric characters
  154. $number = preg_replace('/[^0-9]/', '', $number);
  155. if(strlen($number)!=16) return false;
  156. else return $number;
  157. }
  158. /**
  159. * Transform parts into a string representing a bank account number
  160. *
  161. * @param int $bank
  162. * @param int $branch
  163. * @param int $acct
  164. * @param int $suffix
  165. * @return string or false on error
  166. */
  167. public static function formatBankAccountNumber($bank, $branch, $acct, $suffix){
  168. $bank = preg_replace('/[^0-9]/', '', $bank);
  169. $branch = preg_replace('/[^0-9]/', '', $branch);
  170. $acct = preg_replace('/[^0-9]/', '', $acct);
  171. $suffix = preg_replace('/[^0-9]/', '', $suffix);
  172. if(strlen($bank)!=2) return false;
  173. if(strlen($branch)!=4) return false;
  174. if(strlen($acct)<6 || strlen($bank)>7) return false;
  175. if(strlen($suffix)< 2 || strlen($suffix) >4) return false;
  176. return $bank.' '.$branch.' '.$acct.' '.$suffix;
  177. }
  178. }
  179. /**********************************************************************
  180. * Holotech Function Library
  181. * Copyright (c) 2000 - 2006 All rights reserved.
  182. *
  183. * This is free software, in the sense of "free beer" *and* "free
  184. * speech"! How cool is that? It costs nothing, and you may use and
  185. * modify it as desired. You may redistribute it freely, so long as it
  186. * is unmodified and has the documentation file attached.
  187. *
  188. ## --> See /classes/JFX/Filter/Halolib.txt <-- ##
  189. *
  190. * Questions, comments, praise, criticism and beer are welcome.
  191. * Email hololib@holotech.net
  192. *
  193. * This library requires PHP4
  194. *
  195. **********************************************************************/
  196. /*
  197. * Validate an email address
  198. * $Addr = The address to check
  199. * $Level = The level of checking to perform
  200. * $Fail = The level at which the validation failed
  201. * $Timeout = Optional timeout for mail server response
  202. */
  203. function hl_ValEmail($Addr, &$Fail, $Level = 2, $Timeout = 3) {
  204. // Valid Top-Level Domains
  205. $gTLDs = "com:net:org:edu:gov:mil:int:arpa:aero:biz:coop:info:museum:name:";
  206. $CCs =
  207. "ad:ae:af:ag:ai:al:am:an:ao:aq:ar:as:at:au:aw:az:ba:bb:bd:be:bf:bg:bh:".
  208. "bi:bj:bm:bn:bo:br:bs:bt:bv:bw:by:bz:ca:cc:cf:cd:cg:ch:ci:ck:cl:cm:cn:".
  209. "co:cr:cs:cu:cv:cx:cy:cz:de:dj:dk:dm:do:dz:ec:ee:eg:eh:er:es:et:fi:fj:".
  210. "fk:fm:fo:fr:fx:ga:gb:gd:ge:gf:gh:gi:gl:gm:gn:gp:gq:gr:gs:gt:gu:gw:gy:".
  211. "hk:hm:hn:hr:ht:hu:id:ie:il:in:io:iq:ir:is:it:jm:jo:jp:ke:kg:kh:ki:km:".
  212. "kn:kp:kr:kw:ky:kz:la:lb:lc:li:lk:lr:ls:lt:lu:lv:ly:ma:mc:md:mg:mh:mk:".
  213. "ml:mm:mn:mo:mp:mq:mr:ms:mt:mu:mv:mw:mx:my:mz:na:nc:ne:nf:ng:ni:nl:no:".
  214. "np:nr:nt:nu:nz:om:pa:pe:pf:pg:ph:pk:pl:pm:pn:pr:pt:pw:py:qa:re:ro:ru:".
  215. "rw:sa:sb:sc:sd:se:sg:sh:si:sj:sk:sl:sm:sn:so:sr:st:su:sv:sy:sz:tc:td:".
  216. "tf:tg:th:tj:tk:tm:tn:to:tp:tr:tt:tv:tw:tz:ua:ug:uk:um:us:uy:uz:va:vc:".
  217. "ve:vg:vi:vn:vu:wf:ws:ye:yt:yu:za:zm:zr:zw:";
  218. $cTLDs =
  219. "com:net:org:edu:gov:mil:co:ne:or:ed:go:mi:aero:biz:coop:info:museum:name:";
  220. $Fail = 0;
  221. $Addr = trim(strtolower($Addr));
  222. if (ereg(' ', $Addr)) $Fail = 1;
  223. $UD = explode('@', $Addr);
  224. if (sizeof($UD) != 2 || !$UD[0]) $Fail = 1;
  225. $Levels = explode('.', $UD[1]); $sLevels = sizeof($Levels);
  226. if (!$Levels[0] || !$Levels[1]) $Fail = 1;
  227. $tld = $Levels[$sLevels-1];
  228. $tld = ereg_replace('[>)}]$|]$', '', $tld);
  229. if (strlen($tld) < 2
  230. || (strlen($tld) > 3 && !ereg(":$tld:", ':arpa:aero:coop:info:museum:name:'))) $Fail = 1;
  231. $Level--;
  232. if ($Level && !$Fail) {
  233. $Level--;
  234. if (!ereg($tld.':', $gTLDs) && !ereg($tld.':', $CCs)) $Fail = 2;
  235. }
  236. if ($Level && !$Fail) {
  237. $cd = $sLevels - 2; $domain = $Levels[$cd].'.'.$tld;
  238. if (ereg($Levels[$cd].':', $cTLDs)) { $cd--; $domain = $Levels[$cd].'.'.$domain; }
  239. }
  240. if ($Level && !$Fail) {
  241. $Level--;
  242. if (!getmxrr($domain, $mxhosts, $weight)) $Fail = 3;
  243. }
  244. if ($Level && !$Fail) {
  245. $Level--;
  246. while (!$sh && list($nul, $mxhost) = each($mxhosts)) $sh = fsockopen($mxhost, 25);
  247. if (!$sh) $Fail=4;
  248. }
  249. if ($Level && !$Fail) {
  250. $Level--;
  251. $out = "";
  252. socket_set_blocking($sh, false);
  253. $WaitTil = time() + $Timeout;
  254. while ($WaitTil > time() && !$out) $out = fgets($sh, 256);
  255. if (!ereg('^220', $out)) $Fail = 5;
  256. }
  257. if ($sh) fclose($sh);
  258. if ($Fail) return false; else return true;
  259. }