Rule
[ class tree: Rule ] [ index: Rule ] [ all elements ]

Source for file Email.php

Documentation is available at Email.php

  1. <?php
  2. /**
  3.  * Formagic
  4.  *
  5.  * LICENCE
  6.  *
  7.  * This source file is subject to the new BSD license that is bundled
  8.  * with this package in the file LICENSE.txt.
  9.  * It is also available through the world-wide-web at
  10.  * http://formagic.weasle.de/licence.txt
  11.  * If you did not receive a copy of the license and are unable to
  12.  * obtain it through the world-wide-web, please send an email
  13.  * to licence@weasle.de so we can send you a copy immediately.
  14.  *
  15.  * @category    Formagic
  16.  * @package     Rule
  17.  * @author      Marc Schrader
  18.  * @author      Florian Sonnenburg
  19.  * @copyright   Copyright (c) 2007 Florian Sonnenburg
  20.  * @license     http://formagic.weasle.de/licence.txt     New BSD License
  21.  * @revision    $Revision: 17 $
  22.  */
  23.  
  24. require_once('Rule.php');
  25.  
  26. /**
  27.  * Email rule checks if a valid email address is entered.
  28.  *
  29.  * The following arguments are supported:
  30.  *  - (boolean)'checkDns': Performs DNS lookup
  31.  *
  32.  * If only one argument is passed instead of array, it is interpreted as 'checkDns'
  33.  *
  34.  * @category    Formagic
  35.  * @package     Rule
  36.  * @author      Marc Schrader
  37.  * @author      Florian Sonnenburg
  38.  * @copyright   Copyright (c) 2007 Marc Schrader
  39.  * @version     $Id: Email.php 17 2007-08-19 20:40:10Z meweasle $
  40.  ***/
  41. {
  42.  
  43.     /**
  44.      * Default error message
  45.      * @var string 
  46.      ***/
  47.     protected $_errorMessage = 'Bitte eine Email-Adresse eingeben';
  48.  
  49.     /**
  50.      * Default error code
  51.      * @var integer 
  52.      ***/
  53.     protected $_errorCode = 1;
  54.  
  55.     /**
  56.      * Email Regex
  57.      * @var string 
  58.      ***/
  59.     private $_regex '/^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/';
  60.  
  61.     /**
  62.      * Performs rule check
  63.      *
  64.      * @param Formagic_Item $itemObj 
  65.      * @return boolean 
  66.      ***/
  67.     public function check($item)
  68.     {
  69.         $value $item->getValue();
  70.         if (!$value && !$item->hasRule('mandatory')) {
  71.             return true;
  72.         }
  73.  
  74.         // Check value with Regex
  75.         if (!preg_match($this->_regex$value)) {
  76.             return false;
  77.         }
  78.         // Check if DNS lookup is supported by PHP version and requested
  79.         // by user
  80.         if ($this->_args && !is_array($this->_args)) {
  81.             $this->_args = array('checkDns' => (boolean)$this->_args);
  82.         }
  83.         if (   empty($this->_args['checkDns'])
  84.             || !function_exists('checkdnsrr')
  85.         {
  86.             return true;
  87.         }
  88.         // Check DNS
  89.         $tokens explode('@'$value);
  90.         if (   !checkdnsrr($tokens[1]'MX')
  91.             && !checkdnsrr($tokens[1]'A')) {
  92.  
  93.             return false;
  94.         }
  95.  
  96.         return false;
  97.     }
  98. }

Documentation generated on Thu, 23 Aug 2007 00:29:36 +0200 by phpDocumentor 1.4.0