bytic /
validator
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | function valid_url($input) |
||
| 6 | { |
||
| 7 | $input = (string) $input; |
||
| 8 | if (empty($input)) { |
||
| 9 | return false; |
||
| 10 | } |
||
| 11 | return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $input); |
||
| 12 | } |
||
| 13 | |||
| 14 | function valid_email($email) |
||
| 15 | { |
||
| 16 | $input = (string) $email; |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 17 | if (empty($email)) { |
||
| 18 | return false; |
||
| 19 | } |
||
| 20 | $isValid = true; |
||
| 21 | $atIndex = strrpos($email, '@'); |
||
| 22 | if (is_bool($atIndex) && !$atIndex) { |
||
|
0 ignored issues
–
show
|
|||
| 23 | $isValid = false; |
||
| 24 | } else { |
||
| 25 | $domain = substr($email, $atIndex + 1); |
||
| 26 | $local = substr($email, 0, $atIndex); |
||
| 27 | $localLen = strlen($local); |
||
| 28 | $domainLen = strlen($domain); |
||
| 29 | if ($localLen < 1 || $localLen > 64) { |
||
| 30 | // local part length exceeded |
||
| 31 | $isValid = false; |
||
| 32 | } else { |
||
| 33 | if ($domainLen < 1 || $domainLen > 255) { |
||
| 34 | // domain part length exceeded |
||
| 35 | $isValid = false; |
||
| 36 | } else { |
||
| 37 | if ('.' == $local[0] || '.' == $local[$localLen - 1]) { |
||
| 38 | // local part starts or ends with '.' |
||
| 39 | $isValid = false; |
||
| 40 | } else { |
||
| 41 | if (preg_match('/\.\./', $local)) { |
||
| 42 | // local part has two consecutive dots |
||
| 43 | $isValid = false; |
||
| 44 | } else { |
||
| 45 | if (!preg_match('/^[A-Za-z0-9\-\.]+$/', $domain)) { |
||
| 46 | // character not valid in domain part |
||
| 47 | $isValid = false; |
||
| 48 | } else { |
||
| 49 | if (preg_match('/\.\./', $domain)) { |
||
| 50 | // domain part has two consecutive dots |
||
| 51 | $isValid = false; |
||
| 52 | } else { |
||
| 53 | if (!preg_match('/^(\.|[A-Za-z0-9!#%&`_=\/$\'*+?^{}|~.-])+$/', |
||
| 54 | str_replace('\\', '', $local)) |
||
| 55 | ) { |
||
| 56 | // character not valid in local part unless |
||
| 57 | // local part is quoted |
||
| 58 | if (!preg_match('/^"(\"|[^"])+"$/', str_replace('\\', '', $local))) { |
||
| 59 | $isValid = false; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | if ($isValid && !(checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A'))) { |
||
| 69 | // domain not found in DNS |
||
| 70 | $isValid = false; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | return $isValid; |
||
| 75 | } |
||
| 76 | |||
| 77 | function valid_cc_number($cc_number) |
||
| 78 | { |
||
| 79 | /* Validate; return value is card type if valid. */ |
||
| 80 | $card_type = ''; |
||
| 81 | $card_regexes = [ |
||
| 82 | "/^4\d{12}(\d\d\d){0,1}$/" => 'visa', |
||
| 83 | "/^5[12345]\d{14}$/" => 'mastercard', |
||
| 84 | "/^3[47]\d{13}$/" => 'amex', |
||
| 85 | "/^6011\d{12}$/" => 'discover', |
||
| 86 | "/^30[012345]\d{11}$/" => 'diners', |
||
| 87 | "/^3[68]\d{12}$/" => 'diners', |
||
| 88 | ]; |
||
| 89 | |||
| 90 | foreach ($card_regexes as $regex => $type) { |
||
| 91 | if (preg_match($regex, $cc_number)) { |
||
| 92 | $card_type = $type; |
||
| 93 | break; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | if (!$card_type) { |
||
| 98 | return false; |
||
| 99 | } |
||
| 100 | |||
| 101 | /* mod 10 checksum algorithm */ |
||
| 102 | $revcode = strrev($cc_number); |
||
| 103 | $checksum = 0; |
||
| 104 | |||
| 105 | for ($i = 0; $i < strlen($revcode); ++$i) { |
||
| 106 | $current_num = (int) $revcode[$i]; |
||
| 107 | if ($i & 1) { /* Odd position */ |
||
| 108 | $current_num *= 2; |
||
| 109 | } |
||
| 110 | /* Split digits and add. */ |
||
| 111 | $checksum += $current_num % 10; |
||
| 112 | if ($current_num > 9 |
||
| 113 | ) { |
||
| 114 | ++$checksum; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | if (0 == $checksum % 10) { |
||
| 119 | return $card_type; |
||
| 120 | } else { |
||
| 121 | return false; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | function valid_cnp($cnp) |
||
| 126 | { |
||
| 127 | $const = '279146358279'; |
||
| 128 | $cnp = trim($cnp); |
||
| 129 | |||
| 130 | preg_match("|^([1256])(\d{2})(\d{2})(\d{2})(\d{6})$|ims", $cnp, $results); |
||
| 131 | if (count($results) < 1) { |
||
| 132 | return false; |
||
| 133 | } |
||
| 134 | |||
| 135 | $mf = $results[1] + 0; |
||
| 136 | if (5 == $mf || 6 == $mf) { |
||
| 137 | $year_add = 2000; |
||
| 138 | } else { |
||
| 139 | $year_add = 1900; |
||
| 140 | } |
||
| 141 | $year = $year_add + $results[2]; |
||
| 142 | $month = $results[3] + 0; |
||
| 143 | $day = $results[4] + 0; |
||
| 144 | |||
| 145 | if (!checkdate($month, $day, $year)) { |
||
| 146 | return false; |
||
| 147 | } |
||
| 148 | |||
| 149 | $suma = 0; |
||
| 150 | for ($i = 0; $i < 12; ++$i) { |
||
| 151 | $suma += $const[$i] * $cnp[$i]; |
||
| 152 | } |
||
| 153 | |||
| 154 | $rest = $suma % 11; |
||
| 155 | |||
| 156 | $c13 = $cnp[12] + 0; |
||
| 157 | |||
| 158 | if (!(($rest < 10 && $rest == $c13) || (10 == $rest && 1 == $c13))) { |
||
| 159 | return false; |
||
| 160 | } |
||
| 161 | |||
| 162 | return true; |
||
| 163 | } |
||
| 164 |