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