/usr/share/php/kohana3.1/system/classes/kohana/valid.php is in libkohana3.1-core-php 3.1.5-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | <?php defined('SYSPATH') OR die('No direct script access.');
/**
* Validation rules.
*
* @package Kohana
* @category Security
* @author Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Valid {
/**
* Checks if a field is not empty.
*
* @return boolean
*/
public static function not_empty($value)
{
if (is_object($value) AND $value instanceof ArrayObject)
{
// Get the array from the ArrayObject
$value = $value->getArrayCopy();
}
// Value cannot be NULL, FALSE, '', or an empty array
return ! in_array($value, array(NULL, FALSE, '', array()), TRUE);
}
/**
* Checks a field against a regular expression.
*
* @param string $value value
* @param string $expression regular expression to match (including delimiters)
* @return boolean
*/
public static function regex($value, $expression)
{
return (bool) preg_match($expression, (string) $value);
}
/**
* Checks that a field is long enough.
*
* @param string $value value
* @param integer $length minimum length required
* @return boolean
*/
public static function min_length($value, $length)
{
return UTF8::strlen($value) >= $length;
}
/**
* Checks that a field is short enough.
*
* @param string $value value
* @param integer $length maximum length required
* @return boolean
*/
public static function max_length($value, $length)
{
return UTF8::strlen($value) <= $length;
}
/**
* Checks that a field is exactly the right length.
*
* @param string $value value
* @param integer $length exact length required
* @return boolean
*/
public static function exact_length($value, $length)
{
return UTF8::strlen($value) === $length;
}
/**
* Checks that a field is exactly the value required.
*
* @param string $value value
* @param string $required required value
* @return boolean
*/
public static function equals($value, $required)
{
return ($value === $required);
}
/**
* Check an email address for correct format.
*
* @link http://www.iamcal.com/publish/articles/php/parsing_email/
* @link http://www.w3.org/Protocols/rfc822/
*
* @param string $email email address
* @param boolean $strict strict RFC compatibility
* @return boolean
*/
public static function email($email, $strict = FALSE)
{
if ($strict === TRUE)
{
$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
$pair = '\\x5c[\\x00-\\x7f]';
$domain_literal = "\\x5b($dtext|$pair)*\\x5d";
$quoted_string = "\\x22($qtext|$pair)*\\x22";
$sub_domain = "($atom|$domain_literal)";
$word = "($atom|$quoted_string)";
$domain = "$sub_domain(\\x2e$sub_domain)*";
$local_part = "$word(\\x2e$word)*";
$expression = "/^$local_part\\x40$domain$/D";
}
else
{
$expression = '/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD';
}
return (bool) preg_match($expression, (string) $email);
}
/**
* Validate the domain of an email address by checking if the domain has a
* valid MX record.
*
* @link http://php.net/checkdnsrr not added to Windows until PHP 5.3.0
*
* @param string $email email address
* @return boolean
*/
public static function email_domain($email)
{
// Check if the email domain has a valid MX record
return (bool) checkdnsrr(preg_replace('/^[^@]++@/', '', $email), 'MX');
}
/**
* Validate a URL.
*
* @param string $url URL
* @return boolean
*/
public static function url($url)
{
// Based on http://www.apps.ietf.org/rfc/rfc1738.html#sec-5
if ( ! preg_match(
'~^
# scheme
[-a-z0-9+.]++://
# username:password (optional)
(?:
[-a-z0-9$_.+!*\'(),;?&=%]++ # username
(?::[-a-z0-9$_.+!*\'(),;?&=%]++)? # password (optional)
@
)?
(?:
# ip address
\d{1,3}+(?:\.\d{1,3}+){3}+
| # or
# hostname (captured)
(
(?!-)[-a-z0-9]{1,63}+(?<!-)
(?:\.(?!-)[-a-z0-9]{1,63}+(?<!-)){0,126}+
)
)
# port (optional)
(?::\d{1,5}+)?
# path (optional)
(?:/.*)?
$~iDx', $url, $matches))
return FALSE;
// We matched an IP address
if ( ! isset($matches[1]))
return TRUE;
// Check maximum length of the whole hostname
// http://en.wikipedia.org/wiki/Domain_name#cite_note-0
if (strlen($matches[1]) > 253)
return FALSE;
// An extra check for the top level domain
// It must start with a letter
$tld = ltrim(substr($matches[1], (int) strrpos($matches[1], '.')), '.');
return ctype_alpha($tld[0]);
}
/**
* Validate an IP.
*
* @param string $ip IP address
* @param boolean $allow_private allow private IP networks
* @return boolean
*/
public static function ip($ip, $allow_private = TRUE)
{
// Do not allow reserved addresses
$flags = FILTER_FLAG_NO_RES_RANGE;
if ($allow_private === FALSE)
{
// Do not allow private or reserved addresses
$flags = $flags | FILTER_FLAG_NO_PRIV_RANGE;
}
return (bool) filter_var($ip, FILTER_VALIDATE_IP, $flags);
}
/**
* Validates a credit card number, with a Luhn check if possible.
*
* @param integer $number credit card number
* @param string|array $type card type, or an array of card types
* @return boolean
* @uses Valid::luhn
*/
public static function credit_card($number, $type = NULL)
{
// Remove all non-digit characters from the number
if (($number = preg_replace('/\D+/', '', $number)) === '')
return FALSE;
if ($type == NULL)
{
// Use the default type
$type = 'default';
}
elseif (is_array($type))
{
foreach ($type as $t)
{
// Test each type for validity
if (Valid::credit_card($number, $t))
return TRUE;
}
return FALSE;
}
$cards = Kohana::config('credit_cards');
// Check card type
$type = strtolower($type);
if ( ! isset($cards[$type]))
return FALSE;
// Check card number length
$length = strlen($number);
// Validate the card length by the card type
if ( ! in_array($length, preg_split('/\D+/', $cards[$type]['length'])))
return FALSE;
// Check card number prefix
if ( ! preg_match('/^'.$cards[$type]['prefix'].'/', $number))
return FALSE;
// No Luhn check required
if ($cards[$type]['luhn'] == FALSE)
return TRUE;
return Valid::luhn($number);
}
/**
* Validate a number against the [Luhn](http://en.wikipedia.org/wiki/Luhn_algorithm)
* (mod10) formula.
*
* @param string $number number to check
* @return boolean
*/
public static function luhn($number)
{
// Force the value to be a string as this method uses string functions.
// Converting to an integer may pass PHP_INT_MAX and result in an error!
$number = (string) $number;
if ( ! ctype_digit($number))
{
// Luhn can only be used on numbers!
return FALSE;
}
// Check number length
$length = strlen($number);
// Checksum of the card number
$checksum = 0;
for ($i = $length - 1; $i >= 0; $i -= 2)
{
// Add up every 2nd digit, starting from the right
$checksum += substr($number, $i, 1);
}
for ($i = $length - 2; $i >= 0; $i -= 2)
{
// Add up every 2nd digit doubled, starting from the right
$double = substr($number, $i, 1) * 2;
// Subtract 9 from the double where value is greater than 10
$checksum += ($double >= 10) ? ($double - 9) : $double;
}
// If the checksum is a multiple of 10, the number is valid
return ($checksum % 10 === 0);
}
/**
* Checks if a phone number is valid.
*
* @param string $number phone number to check
* @param array $lengths
* @return boolean
*/
public static function phone($number, $lengths = NULL)
{
if ( ! is_array($lengths))
{
$lengths = array(7,10,11);
}
// Remove all non-digit characters from the number
$number = preg_replace('/\D+/', '', $number);
// Check if the number is within range
return in_array(strlen($number), $lengths);
}
/**
* Tests if a string is a valid date string.
*
* @param string $str date to check
* @return boolean
*/
public static function date($str)
{
return (strtotime($str) !== FALSE);
}
/**
* Checks whether a string consists of alphabetical characters only.
*
* @param string $str input string
* @param boolean $utf8 trigger UTF-8 compatibility
* @return boolean
*/
public static function alpha($str, $utf8 = FALSE)
{
$str = (string) $str;
if ($utf8 === TRUE)
{
return (bool) preg_match('/^\pL++$/uD', $str);
}
else
{
return ctype_alpha($str);
}
}
/**
* Checks whether a string consists of alphabetical characters and numbers only.
*
* @param string $str input string
* @param boolean $utf8 trigger UTF-8 compatibility
* @return boolean
*/
public static function alpha_numeric($str, $utf8 = FALSE)
{
if ($utf8 === TRUE)
{
return (bool) preg_match('/^[\pL\pN]++$/uD', $str);
}
else
{
return ctype_alnum($str);
}
}
/**
* Checks whether a string consists of alphabetical characters, numbers, underscores and dashes only.
*
* @param string $str input string
* @param boolean $utf8 trigger UTF-8 compatibility
* @return boolean
*/
public static function alpha_dash($str, $utf8 = FALSE)
{
if ($utf8 === TRUE)
{
$regex = '/^[-\pL\pN_]++$/uD';
}
else
{
$regex = '/^[-a-z0-9_]++$/iD';
}
return (bool) preg_match($regex, $str);
}
/**
* Checks whether a string consists of digits only (no dots or dashes).
*
* @param string $str input string
* @param boolean $utf8 trigger UTF-8 compatibility
* @return boolean
*/
public static function digit($str, $utf8 = FALSE)
{
if ($utf8 === TRUE)
{
return (bool) preg_match('/^\pN++$/uD', $str);
}
else
{
return (is_int($str) AND $str >= 0) OR ctype_digit($str);
}
}
/**
* Checks whether a string is a valid number (negative and decimal numbers allowed).
*
* Uses {@link http://www.php.net/manual/en/function.localeconv.php locale conversion}
* to allow decimal point to be locale specific.
*
* @param string $str input string
* @return boolean
*/
public static function numeric($str)
{
// Get the decimal point for the current locale
list($decimal) = array_values(localeconv());
// A lookahead is used to make sure the string contains at least one digit (before or after the decimal point)
return (bool) preg_match('/^-?+(?=.*[0-9])[0-9]*+'.preg_quote($decimal).'?+[0-9]*+$/D', (string) $str);
}
/**
* Tests if a number is within a range.
*
* @param string $number number to check
* @param integer $min minimum value
* @param integer $max maximum value
* @return boolean
*/
public static function range($number, $min, $max)
{
return ($number >= $min AND $number <= $max);
}
/**
* Checks if a string is a proper decimal format. Optionally, a specific
* number of digits can be checked too.
*
* @param string $str number to check
* @param integer $places number of decimal places
* @param integer $digits number of digits
* @return boolean
*/
public static function decimal($str, $places = 2, $digits = NULL)
{
if ($digits > 0)
{
// Specific number of digits
$digits = '{'.( (int) $digits).'}';
}
else
{
// Any number of digits
$digits = '+';
}
// Get the decimal point for the current locale
list($decimal) = array_values(localeconv());
return (bool) preg_match('/^[0-9]'.$digits.preg_quote($decimal).'[0-9]{'.( (int) $places).'}$/D', $str);
}
/**
* Checks if a string is a proper hexadecimal HTML color value. The validation
* is quite flexible as it does not require an initial "#" and also allows for
* the short notation using only three instead of six hexadecimal characters.
*
* @param string $str input string
* @return boolean
*/
public static function color($str)
{
return (bool) preg_match('/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/iD', $str);
}
/**
* Checks if a field matches the value of another field.
*
* @param array $array array of values
* @param string $field field name
* @param string $match field name to match
* @return boolean
*/
public static function matches($array, $field, $match)
{
return ($array[$field] === $array[$match]);
}
} // End Valid
|