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 | // Known credit card types. |
---|
34 | define('CC_TYPE_VISA', 1); |
---|
35 | define('CC_TYPE_MASTERCARD', 2); |
---|
36 | define('CC_TYPE_AMEX', 3); |
---|
37 | define('CC_TYPE_DISCOVER', 4); |
---|
38 | define('CC_TYPE_DINERS', 5); |
---|
39 | define('CC_TYPE_JCB', 6); |
---|
40 | |
---|
41 | // validateEmail return types. |
---|
42 | define('VALIDATE_EMAIL_REGEX_FAIL', 1); |
---|
43 | define('VALIDATE_EMAIL_LENGTH_FAIL', 2); |
---|
44 | define('VALIDATE_EMAIL_MX_FAIL', 3); |
---|
45 | define('VALIDATE_EMAIL_SUCCESS', 4); |
---|
46 | |
---|
47 | class Validator { |
---|
48 | |
---|
49 | /** |
---|
50 | * Ensures a value is empty. |
---|
51 | * |
---|
52 | * @param string $val The input data to validate. |
---|
53 | * @return bool true if form is not empty, false otherwise. |
---|
54 | */ |
---|
55 | function notEmpty($val) |
---|
56 | { |
---|
57 | return '' != trim((string)$val); |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Ensures a value is blank. |
---|
62 | * |
---|
63 | * @param string $val The input data to validate. |
---|
64 | * @return bool true if form is empty, false otherwise. |
---|
65 | */ |
---|
66 | function isEmpty($val) |
---|
67 | { |
---|
68 | return '' == trim((string)$val); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Check whether input is a string. |
---|
73 | * |
---|
74 | * @param string $val The input data to validate. |
---|
75 | * @return bool true if form is a string, false otherwise. |
---|
76 | */ |
---|
77 | function isString($val) |
---|
78 | { |
---|
79 | return '' == trim((string)$val) || is_string($val); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Check whether input is a number. Allows negative numbers. |
---|
84 | * |
---|
85 | * @param string $val The input data to validate. |
---|
86 | * @return bool True if no errors found, false otherwise. |
---|
87 | */ |
---|
88 | function isNumber($val) |
---|
89 | { |
---|
90 | return '' == trim((string)$val) || is_numeric($val); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * addError if input is NOT an integer. Don't just use is_int() because the |
---|
95 | * data coming from the user is *really* a string. |
---|
96 | * |
---|
97 | * @param string $val The input data to validate. |
---|
98 | * @return bool true if value is an integer |
---|
99 | */ |
---|
100 | function isInteger($val, $negative_ok=false) |
---|
101 | { |
---|
102 | $pattern = $negative_ok ? '/^-?[[:digit:]]+$/' : '/^[[:digit:]]+$/'; |
---|
103 | return '' == trim((string)$val) || (is_numeric($val) && preg_match($pattern, $val)); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Check whether input is a float. Don't just use is_float() because the |
---|
108 | * data coming from the user is *really* a string. Integers will also |
---|
109 | * pass this test. |
---|
110 | * |
---|
111 | * @param string $val The input data to validate. |
---|
112 | * @return bool true if value is a float |
---|
113 | */ |
---|
114 | function isFloat($val, $negative_ok=false) |
---|
115 | { |
---|
116 | $pattern = $negative_ok ? '/^-?[[:digit:]]*(?:\.?[[:digit:]]+)$/' : '/^[[:digit:]]*(?:\.?[[:digit:]]+)$/'; |
---|
117 | return '' == trim((string)$val) || (is_numeric($val) && preg_match($pattern, $val)); |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * Check whether input is an array. |
---|
122 | * |
---|
123 | * @param string $val The input data to validate. |
---|
124 | * @return bool true if value is a float |
---|
125 | */ |
---|
126 | function isArray($val) |
---|
127 | { |
---|
128 | return (is_string($val) && '' == trim((string)$val)) || is_array($val); |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Check whether input matches the specified perl regular expression |
---|
133 | * pattern. |
---|
134 | * |
---|
135 | * @param string $val The input data to validate. |
---|
136 | * @param int $regex PREG that the string must match |
---|
137 | * @param bool $valid_on_match Set to true to be valid if match, or false to be valid if the match fails. |
---|
138 | * @return bool true if value passes regex test |
---|
139 | */ |
---|
140 | function checkRegex($val, $regex, $valid_on_match=true) |
---|
141 | { |
---|
142 | return $valid_on_match ? preg_match($regex, $val) : !preg_match($regex, $val); |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * Tests if the string length is between specified values. Whitespace excluded for min. |
---|
147 | * |
---|
148 | * @param string $val The input data to validate. |
---|
149 | * @param int $min minimum length of string, inclusive |
---|
150 | * @param int $max maximum length of string, inclusive |
---|
151 | * @return bool true if string length is within given boundaries |
---|
152 | */ |
---|
153 | function stringLength($val, $min, $max) |
---|
154 | { |
---|
155 | return mb_strlen(trim((string)$val)) >= $min && mb_strlen($val) <= $max; |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * Check whether input is within a valid numeric range. |
---|
160 | * |
---|
161 | * @param string $val The input data to validate. |
---|
162 | * @param int $min minimum value of number, inclusive |
---|
163 | * @param int $max maximum value of number, inclusive |
---|
164 | * @return bool True if no errors found, false otherwise. |
---|
165 | */ |
---|
166 | function numericRange($val, $min, $max) |
---|
167 | { |
---|
168 | return '' == trim((string)$val) || (is_numeric($val) && $val >= $min && $val <= $max); |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * Validates an email address based on the recommendations in RFC 3696. |
---|
173 | * Is more loose than restrictive, to allow the many valid variants of |
---|
174 | * email addresses while catching the most common mistakes. |
---|
175 | * http://www.faqs.org/rfcs/rfc822.html |
---|
176 | * http://www.faqs.org/rfcs/rfc2822.html |
---|
177 | * http://www.faqs.org/rfcs/rfc3696.html |
---|
178 | * http://www.faqs.org/rfcs/rfc1035.html |
---|
179 | * |
---|
180 | * @access public |
---|
181 | * @param string $val The input data to validate.. |
---|
182 | * @return bool Validity of address. |
---|
183 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
184 | */ |
---|
185 | function validateEmail($val) |
---|
186 | { |
---|
187 | require_once 'codebase/lib/Email.inc.php'; |
---|
188 | $e = new Email(); |
---|
189 | |
---|
190 | // Test email address format. |
---|
191 | if (!preg_match($e->getParam('regex'), $val, $e_parts)) { |
---|
192 | return VALIDATE_EMAIL_REGEX_FAIL; |
---|
193 | } |
---|
194 | |
---|
195 | // We have a match! Here are the captured subpatterns, on which further tests are run. |
---|
196 | // The part before the @. |
---|
197 | $local = $e_parts[2]; |
---|
198 | |
---|
199 | // The part after the @. |
---|
200 | // If domain is an IP [XXX.XXX.XXX.XXX] strip off the brackets. |
---|
201 | $domain = $e_parts[3]{0} == '[' ? mb_substr($e_parts[3], 1, -1) : $e_parts[3]; |
---|
202 | |
---|
203 | // Test length. |
---|
204 | if (mb_strlen($local) > 64 || mb_strlen($domain) > 191) { |
---|
205 | return VALIDATE_EMAIL_LENGTH_FAIL; |
---|
206 | } |
---|
207 | |
---|
208 | // Check domain exists: It's a domain if ip2long fails; Checkdnsrr ensures a MX record exists; Gethostbyname() ensures the domain exists. |
---|
209 | // Compare ip2long twice for php4 backwards compat. |
---|
210 | if ((ip2long($domain) == '-1' || ip2long($domain) === false) && function_exists('checkdnsrr') && !checkdnsrr($domain . '.', 'MX') && gethostbyname($domain) == $domain) { |
---|
211 | return VALIDATE_EMAIL_MX_FAIL; |
---|
212 | } |
---|
213 | |
---|
214 | return VALIDATE_EMAIL_SUCCESS; |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | * Verifies that date can be processed by the strtotime function. |
---|
219 | * |
---|
220 | * @param string $val The input data to validate. |
---|
221 | * @return bool True if no errors found, false otherwise. |
---|
222 | */ |
---|
223 | function validateStrDate($val) |
---|
224 | { |
---|
225 | $app =& App::getInstance(); |
---|
226 | |
---|
227 | if ('' == trim($val)) { |
---|
228 | // Don't be too bothered about empty strings. |
---|
229 | return true; |
---|
230 | } |
---|
231 | |
---|
232 | $timestamp = strtotime($val); |
---|
233 | // Return values change between php4 and php5. |
---|
234 | if ('' != trim($val) && ($timestamp === -1 || $timestamp === false)) { |
---|
235 | return false; |
---|
236 | } else { |
---|
237 | return true; |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | |
---|
242 | /** |
---|
243 | * Verifies credit card number using the Luhn (mod 10) algorithm. |
---|
244 | * http://en.wikipedia.org/wiki/Luhn_algorithm |
---|
245 | * |
---|
246 | * @param string $val The input data to validate.. |
---|
247 | * @param string $cc_num Card number to verify. |
---|
248 | * @param string $cc_type Optional, card type to do specific checks. |
---|
249 | * @return bool True if no errors found, false otherwise. |
---|
250 | */ |
---|
251 | function validateCCNumber($val, $cc_type=null) |
---|
252 | { |
---|
253 | // Get rid of any non-digits |
---|
254 | $cc_num = preg_replace('/[^\d]/', '', $val); |
---|
255 | |
---|
256 | // Perform card-specific checks, if applicable |
---|
257 | switch ($cc_type) { |
---|
258 | case CC_TYPE_VISA : |
---|
259 | $regex = '/^4\d{15}$|^4\d{12}$/'; |
---|
260 | break; |
---|
261 | case CC_TYPE_MASTERCARD : |
---|
262 | $regex = '/^5[1-5]\d{14}$/'; |
---|
263 | break; |
---|
264 | case CC_TYPE_AMEX : |
---|
265 | $regex = '/^3[47]\d{13}$/'; |
---|
266 | break; |
---|
267 | case CC_TYPE_DISCOVER : |
---|
268 | $regex = '/^6011\d{12}$/'; |
---|
269 | break; |
---|
270 | case CC_TYPE_DINERS : |
---|
271 | $regex = '/^30[0-5]\d{11}$|^3[68]\d{12}$/'; |
---|
272 | break; |
---|
273 | case CC_TYPE_JCB : |
---|
274 | $regex = '/^3\d{15}$|^2131|1800\d{11}$/'; |
---|
275 | break; |
---|
276 | default : |
---|
277 | $regex = ''; |
---|
278 | break; |
---|
279 | } |
---|
280 | |
---|
281 | if ('' != $regex && !preg_match($regex, $cc_num)) { |
---|
282 | // Invalid format. |
---|
283 | return false; |
---|
284 | } |
---|
285 | |
---|
286 | // The Luhn formula works right to left, so reverse the number. |
---|
287 | $cc_num = strrev($cc_num); |
---|
288 | |
---|
289 | $luhn_total = 0; |
---|
290 | |
---|
291 | $num = mb_strlen($cc_num); |
---|
292 | for ($i=0; $i<$num; $i++) { |
---|
293 | // Get each digit. |
---|
294 | $digit = mb_substr($cc_num, $i, 1); |
---|
295 | |
---|
296 | // If it's an odd digit, double it. |
---|
297 | if ($i / 2 != floor($i / 2)) { |
---|
298 | $digit *= 2; |
---|
299 | } |
---|
300 | |
---|
301 | // If the result is two digits, add them. |
---|
302 | if (mb_strlen($digit) == 2) { |
---|
303 | $digit = mb_substr($digit, 0, 1) + mb_substr($digit, 1, 1); |
---|
304 | } |
---|
305 | |
---|
306 | // Add the current digit to the $luhn_total. |
---|
307 | $luhn_total += $digit; |
---|
308 | } |
---|
309 | |
---|
310 | // If the Total is evenly divisible by 10, it's cool! |
---|
311 | return $luhn_total % 10 == 0; |
---|
312 | } |
---|
313 | |
---|
314 | /** |
---|
315 | * Check whether a file was selected for uploading. If file is missing, it's an error. |
---|
316 | * |
---|
317 | * @param string $form_name The input data to validate. |
---|
318 | * @return bool True if no errors found, false otherwise. |
---|
319 | */ |
---|
320 | function fileUploaded($form_name) |
---|
321 | { |
---|
322 | if (!isset($_FILES[$form_name]['name']) || empty($_FILES[$form_name]['name'])) { |
---|
323 | return false; |
---|
324 | } |
---|
325 | |
---|
326 | if (is_array($_FILES[$form_name]['name'])) { |
---|
327 | foreach($_FILES[$form_name]['name'] as $f) { |
---|
328 | if ('' == $f) { |
---|
329 | return false; |
---|
330 | } |
---|
331 | } |
---|
332 | } else { |
---|
333 | if ('' == $_FILES[$form_name]['name']) { |
---|
334 | return false; |
---|
335 | } |
---|
336 | } |
---|
337 | |
---|
338 | return true; |
---|
339 | } |
---|
340 | |
---|
341 | } // THE END |
---|
342 | |
---|
343 | ?> |
---|