[144] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[457] | 6 | * |
---|
[362] | 7 | * This file is part of The Strangecode Codebase. |
---|
| 8 | * |
---|
| 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
[457] | 13 | * |
---|
[362] | 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
[457] | 18 | * |
---|
[362] | 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
[144] | 24 | * Validator.inc.php |
---|
| 25 | * |
---|
[457] | 26 | * The Validator class provides a methods for validating input against different criteria. |
---|
[144] | 27 | * All functions return true if the input passes the test. |
---|
| 28 | * |
---|
| 29 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 30 | * @version 1.0 |
---|
| 31 | */ |
---|
| 32 | |
---|
[502] | 33 | class Validator |
---|
| 34 | { |
---|
[144] | 35 | |
---|
[468] | 36 | // Known credit card types. |
---|
| 37 | const CC_TYPE_VISA = 1; |
---|
| 38 | const CC_TYPE_MASTERCARD = 2; |
---|
| 39 | const CC_TYPE_AMEX = 3; |
---|
| 40 | const CC_TYPE_DISCOVER = 4; |
---|
| 41 | const CC_TYPE_DINERS = 5; |
---|
| 42 | const CC_TYPE_JCB = 6; |
---|
[144] | 43 | |
---|
[468] | 44 | // Validator::validateEmail() return types. |
---|
| 45 | const EMAIL_SUCCESS = 0; |
---|
| 46 | const EMAIL_REGEX_FAIL = 1; |
---|
| 47 | const EMAIL_LENGTH_FAIL = 2; |
---|
| 48 | const EMAIL_MX_FAIL = 3; |
---|
[144] | 49 | |
---|
[468] | 50 | // Validator::validatePhone() return types. |
---|
| 51 | const PHONE_SUCCESS = 0; |
---|
| 52 | const PHONE_REGEX_FAIL = 1; |
---|
| 53 | const PHONE_LENGTH_FAIL = 2; |
---|
| 54 | |
---|
[144] | 55 | /** |
---|
[550] | 56 | * Check if a value is not empty (the opposite of isEmpty()). |
---|
| 57 | * |
---|
| 58 | * @param string $val The input data to validate. |
---|
| 59 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 60 | * @param string $file Filename to log (usually __FILE__) |
---|
| 61 | * @param int $line Line number to log (usually __LINE__) |
---|
| 62 | * @return bool true if form is not empty, false otherwise. |
---|
| 63 | */ |
---|
| 64 | static public function notEmpty($val, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[144] | 65 | { |
---|
[550] | 66 | $app =& App::getInstance(); |
---|
[490] | 67 | if (is_array($val)) { |
---|
[550] | 68 | if (!empty($val)) { |
---|
| 69 | return true; |
---|
| 70 | } else { |
---|
| 71 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
| 72 | return false; |
---|
| 73 | } |
---|
[490] | 74 | } else { |
---|
[550] | 75 | if ('' != trim((string)$val)) { |
---|
| 76 | return true; |
---|
| 77 | } else { |
---|
| 78 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
| 79 | return false; |
---|
| 80 | } |
---|
[490] | 81 | } |
---|
[144] | 82 | } |
---|
| 83 | |
---|
[550] | 84 | /* |
---|
| 85 | * We were using the isEmpty method *wrong* for years and should have been using notEmpty becuase it is more grammatically correct. |
---|
| 86 | * Because the only use is to ensure a value is not empty, we're simply going to alias this method to notEmpty(). |
---|
| 87 | * |
---|
| 88 | * @param string $val The input data to validate. |
---|
| 89 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 90 | * @param string $file Filename to log (usually __FILE__) |
---|
| 91 | * @param int $line Line number to log (usually __LINE__) |
---|
| 92 | * @return bool true if form is empty, false otherwise. |
---|
| 93 | */ |
---|
| 94 | static public function isEmpty($val, $type=LOG_NOTICE, $file=null, $line=null) |
---|
| 95 | { |
---|
| 96 | return !self::notEmpty($val, $type, $file, $line); |
---|
| 97 | } |
---|
| 98 | |
---|
[144] | 99 | /** |
---|
[550] | 100 | * Check whether input is a string. |
---|
| 101 | * |
---|
| 102 | * @param string $val The input data to validate. |
---|
| 103 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 104 | * @param string $file Filename to log (usually __FILE__) |
---|
| 105 | * @param int $line Line number to log (usually __LINE__) |
---|
| 106 | * @return bool true if form is a string, false otherwise. |
---|
| 107 | */ |
---|
| 108 | static public function isString($val, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[144] | 109 | { |
---|
[550] | 110 | $app =& App::getInstance(); |
---|
| 111 | if ('' == trim((string)$val) || is_string($val)) { |
---|
| 112 | return true; |
---|
| 113 | } else { |
---|
| 114 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
| 115 | return false; |
---|
| 116 | } |
---|
[144] | 117 | } |
---|
| 118 | |
---|
| 119 | /** |
---|
[550] | 120 | * Check whether input is a number. Allows negative numbers. |
---|
| 121 | * |
---|
| 122 | * @param string $val The input data to validate. |
---|
| 123 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 124 | * @param string $file Filename to log (usually __FILE__) |
---|
| 125 | * @param int $line Line number to log (usually __LINE__) |
---|
| 126 | * @return bool True if no errors found, false otherwise. |
---|
| 127 | */ |
---|
| 128 | static public function isNumber($val, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[144] | 129 | { |
---|
[550] | 130 | $app =& App::getInstance(); |
---|
| 131 | if ('' == trim((string)$val) || is_numeric($val)) { |
---|
| 132 | return true; |
---|
| 133 | } else { |
---|
| 134 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
| 135 | return false; |
---|
| 136 | } |
---|
[144] | 137 | } |
---|
| 138 | |
---|
| 139 | /** |
---|
[550] | 140 | * addError if input is NOT an integer. Don't just use is_int() because the |
---|
| 141 | * data coming from the user is *really* a string. |
---|
| 142 | * |
---|
| 143 | * @param string $val The input data to validate. |
---|
| 144 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 145 | * @param string $file Filename to log (usually __FILE__) |
---|
| 146 | * @param int $line Line number to log (usually __LINE__) |
---|
| 147 | * @return bool true if value is an integer |
---|
| 148 | */ |
---|
| 149 | static public function isInteger($val, $negative_ok=false, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[144] | 150 | { |
---|
[550] | 151 | $app =& App::getInstance(); |
---|
[144] | 152 | $pattern = $negative_ok ? '/^-?[[:digit:]]+$/' : '/^[[:digit:]]+$/'; |
---|
[550] | 153 | if ('' == trim((string)$val) || (is_numeric($val) && preg_match($pattern, $val))) { |
---|
| 154 | return true; |
---|
| 155 | } else { |
---|
| 156 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
| 157 | return false; |
---|
| 158 | } |
---|
[144] | 159 | } |
---|
| 160 | |
---|
| 161 | /** |
---|
[550] | 162 | * Check whether input is a float. Don't just use is_float() because the |
---|
| 163 | * data coming from the user is *really* a string. Integers will also |
---|
| 164 | * pass this test. |
---|
| 165 | * |
---|
| 166 | * @param string $val The input data to validate. |
---|
| 167 | * @param bool $negative_ok If the value can be unsigned. |
---|
| 168 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 169 | * @param string $file Filename to log (usually __FILE__) |
---|
| 170 | * @param int $line Line number to log (usually __LINE__) |
---|
| 171 | * @return bool true if value is a float |
---|
| 172 | */ |
---|
| 173 | static public function isFloat($val, $negative_ok=false, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[144] | 174 | { |
---|
[550] | 175 | $app =& App::getInstance(); |
---|
[144] | 176 | $pattern = $negative_ok ? '/^-?[[:digit:]]*(?:\.?[[:digit:]]+)$/' : '/^[[:digit:]]*(?:\.?[[:digit:]]+)$/'; |
---|
[550] | 177 | if ('' == trim((string)$val) || (is_numeric($val) && preg_match($pattern, $val))) { |
---|
| 178 | return true; |
---|
| 179 | } else { |
---|
| 180 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
| 181 | return false; |
---|
| 182 | } |
---|
[144] | 183 | } |
---|
| 184 | |
---|
| 185 | /** |
---|
[550] | 186 | * Check whether input is a Decimal or Fixed type. Check values to be stored in mysql decimal, numeric, num, or fixed types. |
---|
| 187 | * Note: some integers and floats will also pass this test. |
---|
| 188 | * https://dev.mysql.com/doc/refman/5.5/en/fixed-point-types.html |
---|
| 189 | * |
---|
| 190 | * @param string $val The input data to validate. |
---|
| 191 | * @param bool $negative_ok If the value can be unsigned. |
---|
| 192 | * @param int $max Total max number of digits (for mysql max is 65). |
---|
| 193 | * @param int $dec Total max number of digits after the decimal place (for mysql max is 30). |
---|
| 194 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 195 | * @param string $file Filename to log (usually __FILE__) |
---|
| 196 | * @param int $line Line number to log (usually __LINE__) |
---|
| 197 | * @return bool true if value is a float |
---|
| 198 | */ |
---|
| 199 | static public function isDecimal($val, $max=10, $dec=2, $negative_ok=false, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[534] | 200 | { |
---|
[550] | 201 | $app =& App::getInstance(); |
---|
[534] | 202 | if ('' == trim((string)$val)) { |
---|
| 203 | return true; |
---|
| 204 | } |
---|
| 205 | if (!$negative_ok && is_numeric($val) && $val < 0) { |
---|
[550] | 206 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
[534] | 207 | return false; |
---|
| 208 | } |
---|
| 209 | // Get the length of the part after any decimal point, or zero. |
---|
| 210 | $num_parts = explode('.', $val); |
---|
| 211 | $dec_count = sizeof($num_parts) <= 1 ? 0 : mb_strlen(end($num_parts)); |
---|
| 212 | // Must be numeric, total digits <= $max, dec digits <= $dec. |
---|
[550] | 213 | if (is_numeric($val) && mb_strlen(str_replace(['-', '.'], '', $val)) <= $max && $dec_count <= $dec) { |
---|
| 214 | return true; |
---|
| 215 | } else { |
---|
| 216 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
| 217 | return false; |
---|
| 218 | } |
---|
[534] | 219 | } |
---|
| 220 | |
---|
| 221 | /** |
---|
[550] | 222 | * Check whether input is an array. |
---|
| 223 | * |
---|
| 224 | * @param string $val The input data to validate. |
---|
| 225 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 226 | * @param string $file Filename to log (usually __FILE__) |
---|
| 227 | * @param int $line Line number to log (usually __LINE__) |
---|
| 228 | * @return bool true if value is a float |
---|
| 229 | */ |
---|
| 230 | static public function isArray($val, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[144] | 231 | { |
---|
[550] | 232 | $app =& App::getInstance(); |
---|
| 233 | if ((is_string($val) && '' == trim((string)$val)) || is_array($val)) { |
---|
| 234 | return true; |
---|
| 235 | } else { |
---|
| 236 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
| 237 | return false; |
---|
| 238 | } |
---|
[144] | 239 | } |
---|
| 240 | |
---|
| 241 | /** |
---|
[550] | 242 | * Check whether input matches the specified perl regular expression |
---|
| 243 | * pattern. |
---|
| 244 | * |
---|
| 245 | * @param string $val The input data to validate. |
---|
| 246 | * @param int $regex PREG that the string must match |
---|
| 247 | * @param bool $valid_on_match Set to true to be valid if match, or false to be valid if the match fails. |
---|
| 248 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 249 | * @param string $file Filename to log (usually __FILE__) |
---|
| 250 | * @param int $line Line number to log (usually __LINE__) |
---|
| 251 | * @return bool true if value passes regex test |
---|
| 252 | */ |
---|
| 253 | static public function checkRegex($val, $regex, $valid_on_match=true, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[144] | 254 | { |
---|
[550] | 255 | $app =& App::getInstance(); |
---|
| 256 | if ($valid_on_match ? preg_match($regex, $val) : !preg_match($regex, $val)) { |
---|
| 257 | return true; |
---|
| 258 | } else { |
---|
| 259 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
| 260 | return false; |
---|
| 261 | } |
---|
[144] | 262 | } |
---|
| 263 | |
---|
| 264 | /** |
---|
[550] | 265 | * Tests if the string length is between specified values. Whitespace excluded for min. |
---|
| 266 | * |
---|
| 267 | * @param string $val The input data to validate. |
---|
| 268 | * @param int $min minimum length of string, inclusive |
---|
| 269 | * @param int $max maximum length of string, inclusive |
---|
| 270 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 271 | * @param string $file Filename to log (usually __FILE__) |
---|
| 272 | * @param int $line Line number to log (usually __LINE__) |
---|
| 273 | * @return bool true if string length is within given boundaries |
---|
| 274 | */ |
---|
| 275 | static public function stringLength($val, $min, $max, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[144] | 276 | { |
---|
[550] | 277 | $app =& App::getInstance(); |
---|
| 278 | if (mb_strlen((string)$val) >= $min && mb_strlen((string)$val) <= $max) { |
---|
| 279 | return true; |
---|
| 280 | } else { |
---|
| 281 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
| 282 | return false; |
---|
| 283 | } |
---|
[144] | 284 | } |
---|
| 285 | |
---|
| 286 | /** |
---|
[550] | 287 | * Check whether input is within a valid numeric range. |
---|
| 288 | * |
---|
| 289 | * @param string $val The input data to validate. |
---|
| 290 | * @param int $min minimum value of number, inclusive |
---|
| 291 | * @param int $max maximum value of number, inclusive |
---|
| 292 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 293 | * @param string $file Filename to log (usually __FILE__) |
---|
| 294 | * @param int $line Line number to log (usually __LINE__) |
---|
| 295 | * @return bool True if no errors found, false otherwise. |
---|
| 296 | */ |
---|
| 297 | static public function numericRange($val, $min, $max, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[144] | 298 | { |
---|
[550] | 299 | $app =& App::getInstance(); |
---|
| 300 | if ('' == trim((string)$val) || (is_numeric($val) && $val >= $min && $val <= $max)) { |
---|
| 301 | return true; |
---|
| 302 | } else { |
---|
| 303 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
| 304 | return false; |
---|
| 305 | } |
---|
[144] | 306 | } |
---|
| 307 | |
---|
| 308 | /** |
---|
[550] | 309 | * Validates an email address based on the recommendations in RFC 3696. |
---|
| 310 | * Is more loose than restrictive, to allow the many valid variants of |
---|
| 311 | * email addresses while catching the most common mistakes. |
---|
| 312 | * http://www.faqs.org/rfcs/rfc822.html |
---|
| 313 | * http://www.faqs.org/rfcs/rfc2822.html |
---|
| 314 | * http://www.faqs.org/rfcs/rfc3696.html |
---|
| 315 | * http://www.faqs.org/rfcs/rfc1035.html |
---|
| 316 | * |
---|
| 317 | * @access public |
---|
| 318 | * @param string $val The input data to validate.. |
---|
| 319 | * @param bool $strict Run strict tests (check if the domain exists and has an MX record assigned) |
---|
| 320 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 321 | * @param string $file Filename to log (usually __FILE__) |
---|
| 322 | * @param int $line Line number to log (usually __LINE__) |
---|
| 323 | * @return const One of the constant values: Validate::EMAIL_SUCCESS|Validate::EMAIL_REGEX_FAIL|Validate::EMAIL_LENGTH_FAIL|Validate::EMAIL_MX_FAIL |
---|
| 324 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 325 | */ |
---|
| 326 | static public function validateEmail($val, $strict=false, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[144] | 327 | { |
---|
[550] | 328 | $app =& App::getInstance(); |
---|
[144] | 329 | require_once 'codebase/lib/Email.inc.php'; |
---|
| 330 | $e = new Email(); |
---|
| 331 | |
---|
| 332 | // Test email address format. |
---|
| 333 | if (!preg_match($e->getParam('regex'), $val, $e_parts)) { |
---|
[550] | 334 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
[468] | 335 | return self::EMAIL_REGEX_FAIL; |
---|
[144] | 336 | } |
---|
[457] | 337 | |
---|
[144] | 338 | // We have a match! Here are the captured subpatterns, on which further tests are run. |
---|
[457] | 339 | // The part before the @. |
---|
[144] | 340 | $local = $e_parts[2]; |
---|
| 341 | |
---|
[457] | 342 | // The part after the @. |
---|
[144] | 343 | // If domain is an IP [XXX.XXX.XXX.XXX] strip off the brackets. |
---|
[247] | 344 | $domain = $e_parts[3]{0} == '[' ? mb_substr($e_parts[3], 1, -1) : $e_parts[3]; |
---|
[144] | 345 | |
---|
| 346 | // Test length. |
---|
[247] | 347 | if (mb_strlen($local) > 64 || mb_strlen($domain) > 191) { |
---|
[550] | 348 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
[468] | 349 | return self::EMAIL_LENGTH_FAIL; |
---|
[144] | 350 | } |
---|
| 351 | |
---|
[550] | 352 | if ($strict) { |
---|
| 353 | // Strict tests. |
---|
| 354 | if (ip2long($domain) === false && function_exists('checkdnsrr') && !checkdnsrr($domain . '.', 'MX') && gethostbyname($domain) == $domain) { |
---|
| 355 | // Check domain exists: It's a domain if ip2long fails; checkdnsrr ensures a MX record exists; gethostbyname() ensures the domain exists. |
---|
| 356 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
| 357 | return self::EMAIL_MX_FAIL; |
---|
| 358 | } |
---|
[144] | 359 | } |
---|
| 360 | |
---|
[468] | 361 | return self::EMAIL_SUCCESS; |
---|
[144] | 362 | } |
---|
| 363 | |
---|
| 364 | /** |
---|
[550] | 365 | * Check whether input is a valid phone number. Notice: it is now set |
---|
| 366 | * to allow characters like - or () or + so people can type in a phone |
---|
| 367 | * number that looks like: +1 (530) 555-1212 |
---|
| 368 | * |
---|
| 369 | * @param string $form_name the name of the incoming form variable |
---|
| 370 | * |
---|
| 371 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 372 | * @param string $file Filename to log (usually __FILE__) |
---|
| 373 | * @param int $line Line number to log (usually __LINE__) |
---|
| 374 | * @return bool true if no errors found, false otherwise |
---|
| 375 | */ |
---|
| 376 | static public function validatePhone($val, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[468] | 377 | { |
---|
[550] | 378 | $app =& App::getInstance(); |
---|
| 379 | if (!self::checkRegex($val, '/^[0-9 +().-]*$/', true, $type, $file, $line)) { |
---|
| 380 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
[468] | 381 | return self::PHONE_REGEX_FAIL; |
---|
| 382 | } |
---|
[550] | 383 | if (!self::stringLength($val, 0, 25, $type, $file, $line)) { |
---|
| 384 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
[468] | 385 | return self::PHONE_LENGTH_FAIL; |
---|
| 386 | } |
---|
| 387 | return self::PHONE_SUCCESS; |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | /** |
---|
[550] | 391 | * Verifies that date can be processed by the strtotime function. |
---|
| 392 | * Empty strings are considered valid. Other values are tested on their return value from strtotime(). Null values will fail. |
---|
| 393 | * |
---|
| 394 | * @param string $val The input data to validate. |
---|
| 395 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 396 | * @param string $file Filename to log (usually __FILE__) |
---|
| 397 | * @param int $line Line number to log (usually __LINE__) |
---|
| 398 | * @return bool True if no errors found, false otherwise. |
---|
| 399 | */ |
---|
| 400 | static public function validateStrDate($val, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[144] | 401 | { |
---|
[550] | 402 | $app =& App::getInstance(); |
---|
[479] | 403 | if (is_string($val) && '' === trim($val)) { |
---|
| 404 | // Don't be too bothered about empty strings. |
---|
| 405 | return true; |
---|
| 406 | } |
---|
[281] | 407 | |
---|
[144] | 408 | $timestamp = strtotime($val); |
---|
[457] | 409 | if (!$timestamp || $timestamp < 1) { |
---|
[550] | 410 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
[144] | 411 | return false; |
---|
| 412 | } else { |
---|
| 413 | return true; |
---|
| 414 | } |
---|
| 415 | } |
---|
| 416 | |
---|
[523] | 417 | /* |
---|
| 418 | * Checks if value is a "zero" SQL DATE, DATETIME, or TIMESTAMP value (or simply empty). |
---|
| 419 | * |
---|
| 420 | * @access public |
---|
| 421 | * @param string $val String to check. |
---|
[550] | 422 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 423 | * @param string $file Filename to log (usually __FILE__) |
---|
| 424 | * @param int $line Line number to log (usually __LINE__) |
---|
[523] | 425 | * @return bool True if value is an empty date. |
---|
| 426 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 427 | * @version 1.0 |
---|
| 428 | * @since 19 May 2015 09:57:27 |
---|
| 429 | */ |
---|
[550] | 430 | static public function isEmptyDate($val, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[523] | 431 | { |
---|
[550] | 432 | $app =& App::getInstance(); |
---|
[523] | 433 | if (empty($val) || '0000-00-00 00:00:00' == $val || '0000-00-00' == $val || '00:00:00' == $val) { |
---|
| 434 | return true; |
---|
| 435 | } |
---|
[550] | 436 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
[523] | 437 | return false; |
---|
| 438 | } |
---|
| 439 | |
---|
[144] | 440 | /** |
---|
[550] | 441 | * Verifies credit card number using the Luhn (mod 10) algorithm. |
---|
| 442 | * http://en.wikipedia.org/wiki/Luhn_algorithm |
---|
| 443 | * |
---|
| 444 | * @param string $val The input data to validate.. |
---|
| 445 | * @param string $cc_num Card number to verify. |
---|
| 446 | * @param string $cc_type Optional, card type to do specific checks. |
---|
| 447 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 448 | * @param string $file Filename to log (usually __FILE__) |
---|
| 449 | * @param int $line Line number to log (usually __LINE__) |
---|
| 450 | * @return bool True if no errors found, false otherwise. |
---|
| 451 | */ |
---|
| 452 | static public function validateCCNumber($val, $cc_type=null, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[468] | 453 | { |
---|
[550] | 454 | $app =& App::getInstance(); |
---|
[468] | 455 | // Get rid of any non-digits |
---|
| 456 | $cc_num = preg_replace('/[^\d]/', '', $val); |
---|
[144] | 457 | |
---|
[468] | 458 | // Perform card-specific checks, if applicable |
---|
| 459 | switch ($cc_type) { |
---|
| 460 | case self::CC_TYPE_VISA : |
---|
| 461 | $regex = '/^4\d{15}$|^4\d{12}$/'; |
---|
| 462 | break; |
---|
| 463 | case self::CC_TYPE_MASTERCARD : |
---|
| 464 | $regex = '/^5[1-5]\d{14}$/'; |
---|
| 465 | break; |
---|
| 466 | case self::CC_TYPE_AMEX : |
---|
| 467 | $regex = '/^3[47]\d{13}$/'; |
---|
| 468 | break; |
---|
| 469 | case self::CC_TYPE_DISCOVER : |
---|
| 470 | $regex = '/^6011\d{12}$/'; |
---|
| 471 | break; |
---|
| 472 | case self::CC_TYPE_DINERS : |
---|
| 473 | $regex = '/^30[0-5]\d{11}$|^3[68]\d{12}$/'; |
---|
| 474 | break; |
---|
| 475 | case self::CC_TYPE_JCB : |
---|
| 476 | $regex = '/^3\d{15}$|^2131|1800\d{11}$/'; |
---|
| 477 | break; |
---|
| 478 | default : |
---|
[470] | 479 | $regex = '/\d{13,}/'; |
---|
[468] | 480 | break; |
---|
| 481 | } |
---|
[457] | 482 | |
---|
[468] | 483 | if ('' != $regex && !preg_match($regex, $cc_num)) { |
---|
| 484 | // Invalid format. |
---|
[550] | 485 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
[468] | 486 | return false; |
---|
| 487 | } |
---|
[144] | 488 | |
---|
[468] | 489 | // The Luhn formula works right to left, so reverse the number. |
---|
| 490 | $cc_num = strrev($cc_num); |
---|
[144] | 491 | |
---|
[468] | 492 | $luhn_total = 0; |
---|
[144] | 493 | |
---|
[468] | 494 | $num = mb_strlen($cc_num); |
---|
| 495 | for ($i=0; $i<$num; $i++) { |
---|
| 496 | // Get each digit. |
---|
| 497 | $digit = mb_substr($cc_num, $i, 1); |
---|
[144] | 498 | |
---|
[468] | 499 | // If it's an odd digit, double it. |
---|
| 500 | if ($i / 2 != floor($i / 2)) { |
---|
| 501 | $digit *= 2; |
---|
| 502 | } |
---|
[144] | 503 | |
---|
[468] | 504 | // If the result is two digits, add them. |
---|
| 505 | if (mb_strlen($digit) == 2) { |
---|
| 506 | $digit = mb_substr($digit, 0, 1) + mb_substr($digit, 1, 1); |
---|
| 507 | } |
---|
[144] | 508 | |
---|
[468] | 509 | // Add the current digit to the $luhn_total. |
---|
| 510 | $luhn_total += $digit; |
---|
| 511 | } |
---|
[144] | 512 | |
---|
[468] | 513 | // If the Total is evenly divisible by 10, it's cool! |
---|
[550] | 514 | if ($luhn_total % 10 == 0) { |
---|
| 515 | return true; |
---|
| 516 | } else { |
---|
| 517 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
| 518 | return false; |
---|
| 519 | } |
---|
[468] | 520 | } |
---|
[144] | 521 | |
---|
| 522 | /** |
---|
[550] | 523 | * Check whether a file was selected for uploading. If file is missing, it's an error. |
---|
| 524 | * |
---|
| 525 | * @param string $form_name The input data to validate. |
---|
| 526 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 527 | * @param string $file Filename to log (usually __FILE__) |
---|
| 528 | * @param int $line Line number to log (usually __LINE__) |
---|
| 529 | * @return bool True if no errors found, false otherwise. |
---|
| 530 | */ |
---|
| 531 | static public function fileUploaded($form_name, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[144] | 532 | { |
---|
[550] | 533 | $app =& App::getInstance(); |
---|
[200] | 534 | if (!isset($_FILES[$form_name]['name']) || empty($_FILES[$form_name]['name'])) { |
---|
[550] | 535 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, 'no _FILES'), $type, $file, $line); |
---|
[169] | 536 | return false; |
---|
| 537 | } |
---|
[457] | 538 | |
---|
[200] | 539 | if (is_array($_FILES[$form_name]['name'])) { |
---|
| 540 | foreach($_FILES[$form_name]['name'] as $f) { |
---|
[169] | 541 | if ('' == $f) { |
---|
[550] | 542 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($_FILES)), $type, $file, $line); |
---|
[169] | 543 | return false; |
---|
| 544 | } |
---|
| 545 | } |
---|
| 546 | } else { |
---|
[200] | 547 | if ('' == $_FILES[$form_name]['name']) { |
---|
[550] | 548 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($_FILES)), $type, $file, $line); |
---|
[169] | 549 | return false; |
---|
| 550 | } |
---|
| 551 | } |
---|
[457] | 552 | |
---|
[169] | 553 | return true; |
---|
[144] | 554 | } |
---|
| 555 | |
---|
[487] | 556 | /* |
---|
| 557 | * Check if the amount of content sent by the browser exceeds the upload_max_filesize value configured in php.ini. |
---|
| 558 | * http://stackoverflow.com/a/24202363 |
---|
| 559 | * |
---|
| 560 | * @access public |
---|
| 561 | * @param string $form_name The input data to validate. |
---|
[550] | 562 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 563 | * @param string $file Filename to log (usually __FILE__) |
---|
| 564 | * @param int $line Line number to log (usually __LINE__) |
---|
[487] | 565 | * @return bool True if no errors found, false otherwise. |
---|
| 566 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 567 | * @version 1.0 |
---|
| 568 | * @since 20 Aug 2014 14:44:23 |
---|
| 569 | */ |
---|
[550] | 570 | static public function fileUploadSize($form_name, $type=LOG_NOTICE, $file=null, $line=null) |
---|
[487] | 571 | { |
---|
[550] | 572 | $app =& App::getInstance(); |
---|
[487] | 573 | $upload_max_filesize = phpIniGetBytes('upload_max_filesize'); |
---|
| 574 | if (isset($_SERVER['CONTENT_LENGTH']) && 0 != $upload_max_filesize && $_SERVER['CONTENT_LENGTH'] > $upload_max_filesize) { |
---|
[550] | 575 | $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line); |
---|
[487] | 576 | return false; |
---|
| 577 | } |
---|
| 578 | return true; |
---|
| 579 | } |
---|
| 580 | |
---|
[144] | 581 | } // THE END |
---|
| 582 | |
---|