Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Email.php 7490 2010-03-29 19:53:27Z jwage $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Doctrine_Validator_Email
24
 *
25
 * @package     Doctrine
26
 * @subpackage  Validator
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28
 * @link        www.doctrine-project.org
29
 * @since       1.0
30
 * @version     $Revision: 7490 $
31
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
32
 */
33
class Doctrine_Validator_Email extends Doctrine_Validator_Driver
34
{
35
    /**
36
     * checks if given value is a valid email address
37
     *
38
     * @link http://iamcal.com/publish/articles/php/parsing_email/pdf/
39
     * @param mixed $value
40
     * @return boolean
41
     */
42
    public function validate($value)
43
    {
44
        if (is_null($value)) {
45
            return true;
46
        }
47
 
48
        if (isset($this->args) && (! isset($this->args['check_mx']) || $this->args['check_mx'] == true)) {
49
            $parts = explode('@', $value);
50
 
51
            if (isset($parts[1]) && $parts[1] && ! $this->_checkMX($parts[1])) {
52
                return false;
53
            }
54
        }
55
 
56
        $e = explode('.', $value);
57
        $tld = end($e);
58
 
59
        if (preg_match("/[^a-zA-Z]/", $tld)) {
60
            return false;
61
        }
62
 
63
        $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
64
        $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
65
        $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
66
        $quotedPair = '\\x5c[\\x00-\\x7f]';
67
        $domainLiteral = "\\x5b($dtext|$quotedPair)*\\x5d";
68
        $quotedString = "\\x22($qtext|$quotedPair)*\\x22";
69
        $domainRef = $atom;
70
        $subDomain = "($domainRef|$domainLiteral)";
71
        $word = "($atom|$quotedString)";
72
        $domain = "$subDomain(\\x2e$subDomain)+";
73
 
74
        /*
75
          following pseudocode to allow strict checking - ask pookey about this if you're puzzled
76
 
77
          if ($this->getValidationOption('strict_checking') == true) {
78
              $domain = "$sub_domain(\\x2e$sub_domain)*";
79
          }
80
        */
81
 
82
        $localPart = "$word(\\x2e$word)*";
83
        $addrSpec = "$localPart\\x40$domain";
84
 
85
        return (bool) preg_match("!^$addrSpec$!D", $value);
86
    }
87
 
88
    /**
89
     * Check DNS Records for MX type
90
     *
91
     * @param string $host Host name
92
     * @return boolean
93
     */
94
    private function _checkMX($host)
95
    {
96
        // We have different behavior here depending of OS and PHP version
97
        if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.3.0', '<')) {
98
            $output = array();
99
 
100
            @exec('nslookup -type=MX '.escapeshellcmd($host) . ' 2>&1', $output);
101
 
102
            if (empty($output)) {
103
                throw new Doctrine_Exception('Unable to execute DNS lookup. Are you sure PHP can call exec()?');
104
            }
105
 
106
            foreach ($output as $line) {
107
                if (preg_match('/^'.$host.'/', $line)) {
108
                    return true;
109
                }
110
            }
111
 
112
            return false;
113
        } else if (function_exists('checkdnsrr')) {
114
            return checkdnsrr($host, 'MX');
115
        }
116
 
117
        throw new Doctrine_Exception('Could not retrieve DNS record information. Remove check_mx = true to prevent this warning');
118
    }
119
}