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: Creditcard.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_Creditcard
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_Creditcard extends Doctrine_Validator_Driver
34
{
35
    /**
36
     * checks if given value is a valid credit card number
37
     *
38
     * @link http://www.owasp.org/index.php/OWASP_Validation_Regex_Repository
39
     * @param mixed $value
40
     * @return boolean
41
     */
42
    public function validate($value)
43
    {
44
        if (is_null($value)) {
45
            return true;
46
        }
47
        $cardType = "";
48
        $card_regexes = array(
49
            "/^4\d{12}(\d\d\d){0,1}$/"      => 'visa',
50
            "/^5[12345]\d{14}$/"            => 'mastercard',
51
            "/^3[47]\d{13}$/"               => 'amex',
52
            "/^6011\d{12}$/"                => 'discover',
53
            "/^30[012345]\d{11}$/"          => 'diners',
54
            "/^3[68]\d{12}$/"               => 'diners',
55
        );
56
        foreach ($card_regexes as $regex => $type) {
57
            if (preg_match($regex, $value)) {
58
                 $cardType = $type;
59
                 break;
60
            }
61
        }
62
        if ( ! $cardType) {
63
            return false;
64
        }
65
        /* mod 10 checksum algorithm */
66
        $revcode = strrev($value);
67
        $checksum = 0;
68
        for ($i = 0; $i < strlen($revcode); $i++) {
69
            $currentNum = intval($revcode[$i]);
70
            if ($i & 1) {               /* Odd position */
71
                 $currentNum *= 2;
72
            }
73
            /* Split digits and add. */
74
            $checksum += $currentNum % 10;
75
            if ($currentNum > 9) {
76
                 $checksum += 1;
77
            }
78
        }
79
        if ($checksum % 10 == 0) {
80
            return true;
81
        } else {
82
            return false;
83
        }
84
    }
85
}