Overview

Packages

  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Datatype
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cValidatorAbstract
  • cValidatorEmail
  • cValidatorFactory
  • cValidatorRegex
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * This file contains the mail validator class.
  4:  *
  5:  * @package    Core
  6:  * @subpackage Validation
  7:  * @version    SVN Revision $Rev:$
  8:  *
  9:  * @author     Murat Purc <murat@purc.de>
 10:  * @copyright  four for business AG <www.4fb.de>
 11:  * @license    http://www.contenido.org/license/LIZENZ.txt
 12:  * @link       http://www.4fb.de
 13:  * @link       http://www.contenido.org
 14:  */
 15: 
 16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 17: 
 18: /**
 19:  * E-Mail validation.
 20:  *
 21:  * Supports following options:
 22:  * <pre>
 23:  * - disallow_tld   (array)  Optional, list of top level domains to disallow
 24:  * - disallow_host  (array)  Optional, list of hosts to disallow
 25:  * - mx_check       (bool)   Optional, flag to check DNS records for MX type
 26:  * </pre>
 27:  *
 28:  * Usage:
 29:  * <pre>
 30:  * $validator = cValidatorFactory::getInstance('email');
 31:  * if ($validator->isValid('user@contenido.org')) {
 32:  *     // email is valid
 33:  * } else {
 34:  *     $errors = $validator->getErrors();
 35:  *     foreach ($errors as $pos => $errItem) {
 36:  *         echo $errItem->code . ": " . $errItem->message . "\n";
 37:  *     }
 38:  * }
 39:  * </pre>
 40:  *
 41:  * @package    Core
 42:  * @subpackage Validation
 43:  */
 44: class cValidatorEmail extends cValidatorAbstract {
 45: 
 46:     /**
 47:      * Flag about existing flter_var function
 48:      * @var  bool
 49:      */
 50:     protected static $_filterVarExists;
 51: 
 52:     /**
 53:      * Constructor function, sets some predefined options
 54:      */
 55:     public function __construct() {
 56:         // Some default options to exclude tld or host
 57:         // RFC 2606 filter (<http://tools.ietf.org/html/rfc2606>)
 58:         $this->setOption('disallow_tld', array('.test', '.example', '.invalid', '.localhost'));
 59:         $this->setOption('disallow_host', array('example.com', 'example.org', 'example.net'));
 60:     }
 61: 
 62:     /**
 63:      * Filter variable function exists setter
 64:      * @param  bool  $exists
 65:      */
 66:     public static function setFilterVarExists($exists) {
 67:         self::$_filterVarExists = (bool) $exists;
 68:     }
 69: 
 70:     /**
 71:      * Unsets filter variable function state
 72:      */
 73:     public static function resetFilterVarExists() {
 74:         unset(self::$_filterVarExists);
 75:     }
 76: 
 77:     /**
 78:      * {@inheritdoc}
 79:      */
 80:     protected function _isValid($value) {
 81:         if (!is_string($value) || empty($value)) {
 82:             $this->addError('Invalid or empty value', 1);
 83:             return false;
 84:         }
 85: 
 86:         $host = substr($value, strpos($value, '@') + 1);
 87:         $tld = strrchr($value, '.');
 88: 
 89:         // do RFC 2606 or user defined filter if configured
 90:         if ($this->getOption('disallow_tld')) {
 91:             if (in_array($tld, $this->getOption('disallow_tld'))) {
 92:                 $this->addError('Disallowed top level domain', 2);
 93:                 return false;
 94:             }
 95:         }
 96:         if ($this->getOption('disallow_host')) {
 97:             if (in_array($host, $this->getOption('disallow_host'))) {
 98:                 $this->addError('Disallowed host name', 3);
 99:                 return false;
100:             }
101:         }
102: 
103:         if (!isset(self::$_filterVarExists)) {
104:             self::setFilterVarExists(function_exists('filter_var'));
105:         }
106: 
107:         // Use native filter_var function, if exists
108:         if (self::$_filterVarExists) {
109:             $isValid = (bool) filter_var($value, FILTER_VALIDATE_EMAIL);
110:         } else {
111:             // Fallback for not existing filter_var function
112:             // Taken over from PHP trunk ext/filter/logical_filters.c
113:             /*
114:              * The regex below is based on a regex by Michael Rushton.
115:              * However, it is not identical.  I changed it to only consider routeable
116:              * addresses as valid.  Michael's regex considers a@b a valid address
117:              * which conflicts with section 2.3.5 of RFC 5321 which states that:
118:              *
119:              *   Only resolvable, fully-qualified domain names (FQDNs) are permitted
120:              *   when domain names are used in SMTP.  In other words, names that can
121:              *   be resolved to MX RRs or address (i.e., A or AAAA) RRs (as discussed
122:              *   in Section 5) are permitted, as are CNAME RRs whose targets can be
123:              *   resolved, in turn, to MX or address RRs.  Local nicknames or
124:              *   unqualified names MUST NOT be used.
125:              *
126:              * This regex does not handle comments and folding whitespace.  While
127:              * this is technically valid in an email address, these parts aren't
128:              * actually part of the address itself.
129:              *
130:              * Michael's regex carries this copyright:
131:              *
132:              * Copyright � Michael Rushton 2009-10
133:              * http://squiloople.com/
134:              * Feel free to use and redistribute this code. But please keep this copyright notice.
135:              *
136:              */
137:             $regexp = "/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD";
138:             $isValid = (bool) preg_match($regexp, $value);
139:         }
140:         if (!$isValid) {
141:             $this->addError('Invalid email', 4);
142:             return false;
143:         }
144: 
145: 
146:         // Do DNS check for MX type
147:         if ($this->getOption('mx_check') && function_exists('checkdnsrr')) {
148:             $isValid = $this->_checkMx($host);
149:             if (!$isValid) {
150:                 $this->addError('MX check failed', 5);
151:                 return false;
152:             }
153:         }
154: 
155:         return true;
156:     }
157: 
158:     /**
159:      * Check DNS Records for MX type.
160:      *
161:      * @param string $host Host name
162:      *
163:      * @return bool
164:      */
165:     private function _checkMx($host) {
166:         return checkdnsrr($host, 'MX');
167:     }
168: 
169: }
170: 
CMS CONTENIDO 4.9.3 API documentation generated by ApiGen 2.8.0