ea-Geiercode-base
[ class tree: ea-Geier ] [ index: ea-Geier ] [ all elements ]

Source for file check.class.php

Documentation is available at check.class.php

  1. <?php 
  2. /**
  3.  * validator class
  4.  *
  5.  * LICENSE:
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
  13.  * GNU General Public License for more details.
  14.  * You should have received a copy of the GNU General Public License along
  15.  * with this program; if not, write to the Free Software Foundation, Inc.,
  16.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17.  *
  18.  * @package       ea-Geier
  19.  * @subpackage code/base
  20.  * @author       m2mtech <tech@m2m.at>
  21.  * @copyright  2007 m2m server software gmbh
  22.  * @license       http://www.gnu.org/licenses/gpl.html GNU General Public License Version 2
  23.  * @version       $Id: check.class.php 133 2007-08-24 13:59:14Z m2mtech $
  24.  * @link       http://www.ea-geier.at/
  25.  */
  26.  
  27. /**
  28.  * validator class
  29.  *
  30.  * @package       ea-Geier
  31.  */
  32. class eaCheck {
  33.  
  34.     /**
  35.      * error message
  36.      *
  37.      * @var string 
  38.      */
  39.     var $error = '';
  40.  
  41.     /**
  42.      * constructor
  43.      */
  44.     function eaInput(
  45.         setlocale(LC_CTYPE'de_DE')// enable umlauts
  46.         return true
  47.     }
  48.             
  49.     /**
  50.      * store error message and throw error
  51.      *
  52.      * @param    string    $message    error message
  53.      * @return    boolean false
  54.      */
  55.     function _error($message{
  56.         $this->error = $message;
  57.         return false
  58.     }
  59.  
  60.     /**
  61.      * validate text
  62.      *
  63.      * @param    string    &$value test value
  64.      * @return    boolean test result
  65.      */
  66.     function txt(&$value{
  67.         $value trim(html_entity_decode($value));
  68.         if (empty($value&& ($value != '0')) return $this->_error('empty');
  69.         $escValue strip_tags($value);
  70.         if (strcmp($value$escValue!= 0{
  71.             $value $escValue
  72.             return $this->_error('badformat');
  73.         }
  74.         return true;
  75.     }
  76.  
  77.     /**
  78.      * validate numbers
  79.      *
  80.      * allowed format [-]nnn[(.|,)[nnn]]
  81.      *
  82.      * @param    string    &$value test value
  83.      * @return    boolean test result
  84.      */
  85.     function num(&$value{
  86.         $value trim($value);
  87.         if ($value == ''return $this->_error('empty');
  88.         $match false;
  89.         if (!preg_match('/(-?(\d+([\,\.]\d*)?|([\,\.]\d+)))/'$value$match)) {
  90.             $value ''
  91.             return $this->_error('badformat');
  92.  
  93.         };
  94.         if ($value != $match[0]{
  95.             $value $match[0]
  96.             return $this->_error('badformat');
  97.         }
  98.         $value $match[0];     
  99.         return true;
  100.     }
  101.  
  102.     /**
  103.      * validate email address
  104.      *
  105.      * according to RFC2822, ignoring IDN
  106.      *
  107.      * @param    string    &$value test value
  108.      * @return    boolean test result
  109.      */
  110.     function mail(&$value{
  111.         $value trim($value);
  112.         if (empty($value)) return $this->_error('empty')
  113.         $match array();
  114.         if (!preg_match('/^((?:[^@]{1,64}|\".{1,62}\"))@([^@]{1,255})$/'$value$match)) {
  115.             $value strip_tags($value);
  116.             return $this->_error('badformat')
  117.         }
  118.         $atext '[a-z0-9!#$%&\'*+\-\/=?\^_`{|}~]';
  119.         $dotatom $atext '+(\.' $atext '+)*';
  120.         $quotedstring '\"[^\\\")]+\"';
  121.         list($mail$localpart$domain$match;
  122.         if (!preg_match('/^(' $dotatom '|' $quotedstring ')$/i'$localpart)) 
  123.             $value strip_tags($value);
  124.             return $this->_error('badformat')
  125.         }
  126.         if (!preg_match('/^[a-z0-9]+((\.|-)[a-z0-9]+)*$/i'$domain)) 
  127.             $value strip_tags($value);
  128.             return $this->_error('badformat')
  129.         }
  130.         // now we are RFC2822 compliant
  131.         // why should somebody use tags in the quoted string of the local part?
  132.         $notags strip_tags($localpart);
  133.         if (strcmp($notags$localpart!= 0{
  134.             $value strip_tags($value);
  135.             return $this->_error('badformat')
  136.         }
  137.         // why should someboedy want to add strings which can be used to spam?
  138.         $spam array('content-type:''mime-version:''multipart/mixed''bcc:');
  139.         foreach ($spam as $pattern
  140.             if (preg_match('/' preg_quote($pattern'/''/i'$localpart))
  141.                 return $this->_error('badformat');            
  142.         $value strtolower($value)// not compliant, but more user friendly
  143.         return true;
  144.     }
  145.  
  146.     /**
  147.      * validate character string
  148.      *
  149.      * @param    string    &$value test value
  150.      * @return    boolean test result
  151.      */
  152.     function abc(&$value{
  153.         $value trim($value);
  154.         if ($value == ''return $this->_error('empty');
  155.         $extValue preg_replace('/[^a-z]/i'''$value)// remove invalid characters
  156.         if (strcmp($value$extValue!= 0{
  157.             $value $extValue
  158.             return $this->_error('badformat');
  159.         }
  160.         return true;
  161.     }
  162.  
  163.     /**
  164.      * validate lowercase character string
  165.      *
  166.      * @param    string    &$value test value
  167.      * @return    boolean test result
  168.      */
  169.     function abcLower(&$value{
  170.         $value strtolower($value);
  171.         return $this->abc($value);
  172.     }
  173.  
  174.     /**
  175.      * validate uppercase character string
  176.      *
  177.      * @param    string    &$value test value
  178.      * @return    boolean test result
  179.      */
  180.     function abcUpper(&$value{
  181.         $value strtoupper($value);
  182.         return $this->abc($value);
  183.     }
  184.  
  185.     /**
  186.      * validate ip address string
  187.      *
  188.      * @param    string    &$value test value
  189.      * @param    string    $mask    ip mask (255.255.255.255)
  190.      * @return    boolean test result
  191.      */
  192.     function ip(&$value$mask '255.255.255.255'{
  193.         $pattern '/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/';
  194.         $ipArray array();
  195.         if (!preg_match($pattern$value$ipArray)) {
  196.             $value strip_tags($value);
  197.             return $this->_error('badformat')
  198.         }
  199.         unset($ipArray[0]);
  200.         foreach ($ipArray as $ipNibble
  201.             if ($ipNibble 255)  return $this->_error('badformat')
  202.         }
  203.         if ($mask == '255.255.255.255'return true;
  204.         $maskArray array();
  205.         if (!preg_match($pattern$mask$maskArray)) {
  206.             $value strip_tags($value);
  207.             return $this->_error('badformat')
  208.         }
  209.         unset($maskArray[0]);
  210.         foreach ($maskArray as $key => $maskNibble
  211.             if ($maskNibble == 255continue;
  212.             if ($maskNibble == 0{
  213.               $ipArray[$key0;
  214.               continue;
  215.             }
  216.             if ($maskNibble 255)    return $this->_error('badformat')
  217.             $ipArray[$key= (int)$ipArray[$key(int)$maskNibble;
  218.  
  219.         }
  220.         $value $ipArray[1'.' $ipArray[2'.' $ipArray[3'.' $ipArray[4];        
  221.         return true;
  222.     }
  223.  
  224.     /**
  225.      * validate database dsn
  226.      *
  227.      * allowed format: driver://user:password@server/database
  228.      *
  229.      * @param    string    test value
  230.      * @return    boolean result
  231.      */
  232.     function dsn(&$value{
  233.         $value trim(html_entity_decode($value));
  234.         if (empty($value)) return $this->_error('empty');
  235.         $escValue strip_tags($value);
  236.         if (strcmp($value$escValue!= 0{
  237.             $value $escValue
  238.             return $this->_error('badformat');
  239.         }
  240.         $pattern '/^[^:]+:\/\/[^:]+:[^@]+@[^\/]+\/.+$/';
  241.         if (!preg_match($pattern$value)) return $this->_error('badformat');
  242.         return true;
  243.     }
  244.  
  245. }
  246.         
  247. ?>

Documentation generated on Sun, 09 Sep 2007 17:08:08 +0200 by phpDocumentor 1.3.1