/trunk/jfx-private/classes/JFX/Filter.php
PHP | 319 lines | 145 code | 60 blank | 114 comment | 63 complexity | 2d881510bc3c2e37d3acebd7ed2c28b1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
- <?php
- ############### COPYLEFT GPLv3 LICENSE ###############
- ##
- ## JFX Version 0.2.9
- ## Website Management Software
- ## www.jfxcms.com
- ##
- ## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
- ##
- ## Anthony Gallon
- ## oi_antz@hotmail.com
- ##
- ## Permission is hereby granted to any person having a copy of this software
- ## to freely use and modify as required so long as the copyright notices
- ## and branding remain intact.
- ##
- ## Full license details available at http://www.jfxcms.com/license
- ##
- ############### COPYLEFT GPLv3 LICENSE ###############
- /**
- * Perform checks to verify a particular data type is of valid format
- * @author Anthony Gallon
- * @package jfx-core-classes
- */
- /**
- * Perform checks to verify a particular data type is of valid format
- * @author Anthony Gallon
- * @package jfx-core-classes
- */
- class JFX_Filter
- {
- /**
- * Check whether an email address is a valid format. Uses Halolib.
- *
- * @param string $email
- * @return bool $validFormat
- */
- public static function checkEmail($email){
- $fail = 0;
- return hl_ValEmail($email, $fail);
- //return ereg("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]{2,20}$", $email);
- }
- /**
- * Checks PHP code to see if it validates.
- * Accepts: PHP code as string with no <?php ?> brackets
- * Returns: true if code evaluates, false on error
- * Sourced from /admin/edituserplugin.php 17 November 2008
- * @param string $code
- * @return bool $validCode
- */
- public static function checkPHPCode($code){
- $validinfo = true;
- if (strrpos($code, '{') !== FALSE)
- {
- $lastopenbrace = strrpos($code, '{');
- $lastclosebrace = strrpos($code, '}');
- if ($lastopenbrace > $lastclosebrace)
- {
- JFX::addError(lang('invalidcode'));
- JFX::addError(lang('invalidcode_brace_missing'));
- $validinfo = false;
- }
- }
- if ($validinfo)
- {
- srand();
- ob_start();
- if (eval('function testfunction'.rand().'() {'.$code.'}') === FALSE)
- {
- $error[] = JFX::addError(lang('invalidcode'));
- //catch the error
- //eval('function testfunction'.rand().'() {'.$code.'}');
- $buffer = ob_get_clean();
- //add error
- JFX::addError(str_replace(array('(', ')'), '', htmlentities(strip_tags($buffer ), ENT_QUOTES)));
- $validinfo = false;
- }
- else
- {
- ob_get_clean();
- }
- }
- return (bool) $validinfo;
- }
-
- /**
- * Checks if NZ cellphone number is a valid pattern, if so it will return the formatted number.
- *
- * @param string $number
- * @return string or false on error
- */
- public static function formatCellphoneNumber($number){
- // declare valid area codes
- $areacodes = array('027', '021', '029', '025');
- // get digits
- $number = preg_replace('/[^0-9]/', '', $number);
- // strip leading country code
- if(substr($number, 0, 2)=='00') $number = substr($number, 2);
- if(substr($number, 0, 2)=='64') $number = substr($number, 2);
- // ensure proper area code
- if(substr($number, 0, 1)==0) $number = substr($number, 1);
- $number = '0'.$number;
- $number = substr($number, 0, 3).' '.substr($number, 3);
- $arr = explode(' ', $number);
- // check if phone number is valid
- if(count($arr)!=2) return false;
- if(!in_array($arr[0], $areacodes)) return false;
- if(strlen($arr[1])<6 || strlen($arr[1])>8) return false;
- // format the nubmer to ### ### ####
- $arr[1] = substr($arr[1], 0, 3).' '.substr($arr[1], 3);
- $number = implode(' ', $arr);
- // add area code
- $number = '64 '.$number;
- return $number;
- }
- /**
- * Checks if the number is a valid pattern, if so it will return the formatted number.
- *
- * @param string $number
- * @return string or false on error
- */
- public static function formatPhoneNumber($number){
- // declare valid area codes
- $areacodes = array('03', '04', '06', '07', '09');
- // get digits
- $number = preg_replace('/[^0-9]/', '', $number);
- // strip leading country code
- if(substr($number, 0, 2)=='00') $number = substr($number, 2);
- if(substr($number, 0, 2)=='64') $number = substr($number, 2);
- // ensure proper area code
- if(substr($number, 0, 1)==0) $number = substr($number, 1);
- $number = '0'.$number;
- // seperate area code
- if(substr($number, 0, 4)=='0800' || substr($number, 0, 4)=='0508') $number = substr($number, 0, 4).' '.substr($number, 4);
- if(in_array(substr($number, 0, 2), $areacodes)) $number = substr($number, 0, 2).' '.substr($number, 2);
- // check if valid format
- $arr = split(' ', $number);
- if(count($arr)<2) return false;
- if((strlen($arr[0]==2) && strlen($arr[1])<>7) || (strlen($arr[0])==4 && (strlen($arr[1])<6 || strlen($arr[1])>7))) return false;
- // break number into 333 3333 format
- $arr[1] = substr($arr[1], 0, 3).' '.substr($arr[1], 3);
- $number = implode(' ', $arr);
- // add area code
- $number = '64 '.$number;
- return $number;
- }
- /**
- * Checks if a number is a valid format for a credit card number
- *
- * @param string $number
- * @return bool $validFormat
- */
- public static function formatCreditCardNumber($number){
- // strip non numeric characters
- $number = preg_replace('/[^0-9]/', '', $number);
- if(strlen($number)!=16) return false;
- else return $number;
- }
- /**
- * Transform parts into a string representing a bank account number
- *
- * @param int $bank
- * @param int $branch
- * @param int $acct
- * @param int $suffix
- * @return string or false on error
- */
- public static function formatBankAccountNumber($bank, $branch, $acct, $suffix){
- $bank = preg_replace('/[^0-9]/', '', $bank);
- $branch = preg_replace('/[^0-9]/', '', $branch);
- $acct = preg_replace('/[^0-9]/', '', $acct);
- $suffix = preg_replace('/[^0-9]/', '', $suffix);
- if(strlen($bank)!=2) return false;
- if(strlen($branch)!=4) return false;
- if(strlen($acct)<6 || strlen($bank)>7) return false;
- if(strlen($suffix)< 2 || strlen($suffix) >4) return false;
- return $bank.' '.$branch.' '.$acct.' '.$suffix;
- }
- }
- /**********************************************************************
- * Holotech Function Library
- * Copyright (c) 2000 - 2006 All rights reserved.
- *
- * This is free software, in the sense of "free beer" *and* "free
- * speech"! How cool is that? It costs nothing, and you may use and
- * modify it as desired. You may redistribute it freely, so long as it
- * is unmodified and has the documentation file attached.
- *
- ## --> See /classes/JFX/Filter/Halolib.txt <-- ##
- *
- * Questions, comments, praise, criticism and beer are welcome.
- * Email hololib@holotech.net
- *
- * This library requires PHP4
- *
- **********************************************************************/
- /*
- * Validate an email address
- * $Addr = The address to check
- * $Level = The level of checking to perform
- * $Fail = The level at which the validation failed
- * $Timeout = Optional timeout for mail server response
- */
- function hl_ValEmail($Addr, &$Fail, $Level = 2, $Timeout = 3) {
- // Valid Top-Level Domains
- $gTLDs = "com:net:org:edu:gov:mil:int:arpa:aero:biz:coop:info:museum:name:";
- $CCs =
- "ad:ae:af:ag:ai:al:am:an:ao:aq:ar:as:at:au:aw:az:ba:bb:bd:be:bf:bg:bh:".
- "bi:bj:bm:bn:bo:br:bs:bt:bv:bw:by:bz:ca:cc:cf:cd:cg:ch:ci:ck:cl:cm:cn:".
- "co:cr:cs:cu:cv:cx:cy:cz:de:dj:dk:dm:do:dz:ec:ee:eg:eh:er:es:et:fi:fj:".
- "fk:fm:fo:fr:fx:ga:gb:gd:ge:gf:gh:gi:gl:gm:gn:gp:gq:gr:gs:gt:gu:gw:gy:".
- "hk:hm:hn:hr:ht:hu:id:ie:il:in:io:iq:ir:is:it:jm:jo:jp:ke:kg:kh:ki:km:".
- "kn:kp:kr:kw:ky:kz:la:lb:lc:li:lk:lr:ls:lt:lu:lv:ly:ma:mc:md:mg:mh:mk:".
- "ml:mm:mn:mo:mp:mq:mr:ms:mt:mu:mv:mw:mx:my:mz:na:nc:ne:nf:ng:ni:nl:no:".
- "np:nr:nt:nu:nz:om:pa:pe:pf:pg:ph:pk:pl:pm:pn:pr:pt:pw:py:qa:re:ro:ru:".
- "rw:sa:sb:sc:sd:se:sg:sh:si:sj:sk:sl:sm:sn:so:sr:st:su:sv:sy:sz:tc:td:".
- "tf:tg:th:tj:tk:tm:tn:to:tp:tr:tt:tv:tw:tz:ua:ug:uk:um:us:uy:uz:va:vc:".
- "ve:vg:vi:vn:vu:wf:ws:ye:yt:yu:za:zm:zr:zw:";
- $cTLDs =
- "com:net:org:edu:gov:mil:co:ne:or:ed:go:mi:aero:biz:coop:info:museum:name:";
- $Fail = 0;
- $Addr = trim(strtolower($Addr));
- if (ereg(' ', $Addr)) $Fail = 1;
- $UD = explode('@', $Addr);
- if (sizeof($UD) != 2 || !$UD[0]) $Fail = 1;
- $Levels = explode('.', $UD[1]); $sLevels = sizeof($Levels);
- if (!$Levels[0] || !$Levels[1]) $Fail = 1;
- $tld = $Levels[$sLevels-1];
- $tld = ereg_replace('[>)}]$|]$', '', $tld);
- if (strlen($tld) < 2
- || (strlen($tld) > 3 && !ereg(":$tld:", ':arpa:aero:coop:info:museum:name:'))) $Fail = 1;
- $Level--;
- if ($Level && !$Fail) {
- $Level--;
- if (!ereg($tld.':', $gTLDs) && !ereg($tld.':', $CCs)) $Fail = 2;
- }
- if ($Level && !$Fail) {
- $cd = $sLevels - 2; $domain = $Levels[$cd].'.'.$tld;
- if (ereg($Levels[$cd].':', $cTLDs)) { $cd--; $domain = $Levels[$cd].'.'.$domain; }
- }
- if ($Level && !$Fail) {
- $Level--;
- if (!getmxrr($domain, $mxhosts, $weight)) $Fail = 3;
- }
- if ($Level && !$Fail) {
- $Level--;
- while (!$sh && list($nul, $mxhost) = each($mxhosts)) $sh = fsockopen($mxhost, 25);
- if (!$sh) $Fail=4;
- }
- if ($Level && !$Fail) {
- $Level--;
- $out = "";
- socket_set_blocking($sh, false);
- $WaitTil = time() + $Timeout;
- while ($WaitTil > time() && !$out) $out = fgets($sh, 256);
- if (!ereg('^220', $out)) $Fail = 5;
- }
- if ($sh) fclose($sh);
- if ($Fail) return false; else return true;
- }