/yii-1.1.4/framework/validators/CEmailValidator.php

https://github.com/sassman/django-benchmark · PHP · 94 lines · 32 code · 3 blank · 59 comment · 13 complexity · 7985bcdb90e735cf84f3bb8e2459289a MD5 · raw file

  1. <?php
  2. /**
  3. * CEmailValidator class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2010 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CEmailValidator validates that the attribute value is a valid email address.
  12. *
  13. * @author Qiang Xue <qiang.xue@gmail.com>
  14. * @version $Id: CEmailValidator.php 1840 2010-02-26 04:34:30Z qiang.xue $
  15. * @package system.validators
  16. * @since 1.0
  17. */
  18. class CEmailValidator extends CValidator
  19. {
  20. /**
  21. * @var string the regular expression used to validate the attribute value.
  22. * @see http://www.regular-expressions.info/email.html
  23. */
  24. public $pattern='/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/';
  25. /**
  26. * @var string the regular expression used to validate email addresses with the name part.
  27. * This property is used only when {@link allowName} is true.
  28. * @since 1.0.5
  29. * @see allowName
  30. */
  31. public $fullPattern='/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/';
  32. /**
  33. * @var boolean whether to allow name in the email address (e.g. "Qiang Xue <qiang.xue@gmail.com>"). Defaults to false.
  34. * @since 1.0.5
  35. * @see fullPattern
  36. */
  37. public $allowName=false;
  38. /**
  39. * @var boolean whether to check the MX record for the email address.
  40. * Defaults to false. To enable it, you need to make sure the PHP function 'checkdnsrr'
  41. * exists in your PHP installation.
  42. */
  43. public $checkMX=false;
  44. /**
  45. * @var boolean whether to check port 25 for the email address.
  46. * Defaults to false.
  47. * @since 1.0.4
  48. */
  49. public $checkPort=false;
  50. /**
  51. * @var boolean whether the attribute value can be null or empty. Defaults to true,
  52. * meaning that if the attribute is empty, it is considered valid.
  53. */
  54. public $allowEmpty=true;
  55. /**
  56. * Validates the attribute of the object.
  57. * If there is any error, the error message is added to the object.
  58. * @param CModel the object being validated
  59. * @param string the attribute being validated
  60. */
  61. protected function validateAttribute($object,$attribute)
  62. {
  63. $value=$object->$attribute;
  64. if($this->allowEmpty && $this->isEmpty($value))
  65. return;
  66. if(!$this->validateValue($value))
  67. {
  68. $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} is not a valid email address.');
  69. $this->addError($object,$attribute,$message);
  70. }
  71. }
  72. /**
  73. * Validates a static value to see if it is a valid email.
  74. * Note that this method does not respect {@link allowEmpty} property.
  75. * This method is provided so that you can call it directly without going through the model validation rule mechanism.
  76. * @param mixed the value to be validated
  77. * @return boolean whether the value is a valid email
  78. * @since 1.1.1
  79. */
  80. public function validateValue($value)
  81. {
  82. $valid=is_string($value) && (preg_match($this->pattern,$value) || $this->allowName && preg_match($this->fullPattern,$value));
  83. if($valid)
  84. $domain=rtrim(substr($value,strpos($value,'@')+1),'>');
  85. if($valid && $this->checkMX && function_exists('checkdnsrr'))
  86. $valid=checkdnsrr($domain,'MX');
  87. if($valid && $this->checkPort && function_exists('fsockopen'))
  88. $valid=fsockopen($domain,25)!==false;
  89. return $valid;
  90. }
  91. }