source: trunk/lib/Validator.inc.php @ 768

Last change on this file since 768 was 768, checked in by anonymous, 2 years ago

Minor improvements

File size: 23.4 KB
RevLine 
[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]33class 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    */
[575]64    static public function notEmpty($val, $type=LOG_DEBUG, $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                return false;
72            }
[490]73        } else {
[550]74            if ('' != trim((string)$val)) {
75                return true;
76            } else {
77                return false;
78            }
[490]79        }
[144]80    }
81
[550]82    /*
[597]83    * We were using the isEmpty method *wrong* for years and should have been using notEmpty because it is more grammatically correct.
[550]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    */
[575]92    static public function isEmpty($val, $type=LOG_DEBUG, $file=null, $line=null)
[550]93    {
94        return !self::notEmpty($val, $type, $file, $line);
95    }
96
[144]97    /**
[550]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    */
[575]106    static public function isString($val, $type=LOG_DEBUG, $file=null, $line=null)
[144]107    {
[550]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        }
[144]115    }
116
117    /**
[550]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    */
[575]126    static public function isNumber($val, $type=LOG_DEBUG, $file=null, $line=null)
[144]127    {
[550]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        }
[144]135    }
136
137    /**
[550]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    */
[575]147    static public function isInteger($val, $negative_ok=false, $type=LOG_DEBUG, $file=null, $line=null)
[144]148    {
[550]149        $app =& App::getInstance();
[144]150        $pattern = $negative_ok ? '/^-?[[:digit:]]+$/' : '/^[[:digit:]]+$/';
[550]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        }
[144]157    }
158
159    /**
[550]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    */
[575]171    static public function isFloat($val, $negative_ok=false, $type=LOG_DEBUG, $file=null, $line=null)
[144]172    {
[550]173        $app =& App::getInstance();
[144]174        $pattern = $negative_ok ? '/^-?[[:digit:]]*(?:\.?[[:digit:]]+)$/' : '/^[[:digit:]]*(?:\.?[[:digit:]]+)$/';
[550]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        }
[144]181    }
182
183    /**
[550]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    */
[575]197    static public function isDecimal($val, $max=10, $dec=2, $negative_ok=false, $type=LOG_DEBUG, $file=null, $line=null)
[534]198    {
[550]199        $app =& App::getInstance();
[534]200        if ('' == trim((string)$val)) {
201            return true;
202        }
203        if (!$negative_ok && is_numeric($val) && $val < 0) {
[550]204            $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line);
[534]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.
[550]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        }
[534]217    }
218
219    /**
[550]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    */
[575]228    static public function isArray($val, $type=LOG_DEBUG, $file=null, $line=null)
[144]229    {
[550]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        }
[144]237    }
238
239    /**
[550]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    */
[575]251    static public function checkRegex($val, $regex, $valid_on_match=true, $type=LOG_DEBUG, $file=null, $line=null)
[144]252    {
[550]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        }
[144]260    }
261
262    /**
[550]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    */
[575]273    static public function stringLength($val, $min, $max, $type=LOG_DEBUG, $file=null, $line=null)
[144]274    {
[550]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        }
[144]282    }
283
284    /**
[550]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    */
[575]295    static public function numericRange($val, $min, $max, $type=LOG_DEBUG, $file=null, $line=null)
[144]296    {
[550]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        }
[144]304    }
305
306    /**
[550]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__)
[635]321    * @return  const           One of the constant values: Validator::EMAIL_SUCCESS|Validator::EMAIL_REGEX_FAIL|Validator::EMAIL_LENGTH_FAIL|Validator::EMAIL_MX_FAIL
[550]322    * @author  Quinn Comendant <quinn@strangecode.com>
323    */
[575]324    static public function validateEmail($val, $strict=false, $type=LOG_DEBUG, $file=null, $line=null)
[144]325    {
[550]326        $app =& App::getInstance();
[144]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)) {
[550]332            $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line);
[468]333            return self::EMAIL_REGEX_FAIL;
[144]334        }
[457]335
[144]336        // We have a match! Here are the captured subpatterns, on which further tests are run.
[457]337        // The part before the @.
[144]338        $local = $e_parts[2];
339
[457]340        // The part after the @.
[144]341        // If domain is an IP [XXX.XXX.XXX.XXX] strip off the brackets.
[737]342        $domain = $e_parts[3][0] == '[' ? mb_substr($e_parts[3], 1, -1) : $e_parts[3];
[144]343
344        // Test length.
[247]345        if (mb_strlen($local) > 64 || mb_strlen($domain) > 191) {
[550]346            $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line);
[468]347            return self::EMAIL_LENGTH_FAIL;
[144]348        }
349
[768]350        if ($strict && getmxrr('checkdnsrr') && !getmxrr($domain)) {
351            // Strict tests: check if MX record exists.
352            $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line);
353            return self::EMAIL_MX_FAIL;
[144]354        }
355
[468]356        return self::EMAIL_SUCCESS;
[144]357    }
358
359    /**
[550]360    * Check whether input is a valid phone number. Notice: it is now set
361    * to allow characters like - or () or + so people can type in a phone
362    * number that looks like: +1 (530) 555-1212
363    *
364    * @param  string  $form_name the name of the incoming form variable
365    *
366    * @param  const  $type  A LOG_* constant (see App->logMsg())
367    * @param  string $file  Filename to log (usually __FILE__)
368    * @param  int    $line  Line number to log (usually __LINE__)
369    * @return bool    true if no errors found, false otherwise
370    */
[575]371    static public function validatePhone($val, $type=LOG_DEBUG, $file=null, $line=null)
[468]372    {
[550]373        $app =& App::getInstance();
374        if (!self::checkRegex($val, '/^[0-9 +().-]*$/', true, $type, $file, $line)) {
375            $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line);
[468]376            return self::PHONE_REGEX_FAIL;
377        }
[550]378        if (!self::stringLength($val, 0, 25, $type, $file, $line)) {
379            $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line);
[468]380            return self::PHONE_LENGTH_FAIL;
381        }
382        return self::PHONE_SUCCESS;
383    }
384
385    /**
[550]386    * Verifies that date can be processed by the strtotime function.
387    * Empty strings are considered valid. Other values are tested on their return value from strtotime(). Null values will fail.
388    *
389    * @param  string  $val The input data to validate.
390    * @param  const  $type  A LOG_* constant (see App->logMsg())
391    * @param  string $file  Filename to log (usually __FILE__)
392    * @param  int    $line  Line number to log (usually __LINE__)
393    * @return bool    True if no errors found, false otherwise.
394    */
[575]395    static public function validateStrDate($val, $type=LOG_DEBUG, $file=null, $line=null)
[144]396    {
[550]397        $app =& App::getInstance();
[479]398        if (is_string($val) && '' === trim($val)) {
399            // Don't be too bothered about empty strings.
400            return true;
401        }
[281]402
[767]403        if (false === strtotime($val)) {
[550]404            $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line);
[144]405            return false;
406        } else {
407            return true;
408        }
409    }
410
[523]411    /*
412    * Checks if value is a "zero" SQL DATE, DATETIME, or TIMESTAMP value (or simply empty).
413    *
414    * @access   public
415    * @param    string  $val    String to check.
[550]416    * @param  const  $type  A LOG_* constant (see App->logMsg())
417    * @param  string $file  Filename to log (usually __FILE__)
418    * @param  int    $line  Line number to log (usually __LINE__)
[523]419    * @return   bool            True if value is an empty date.
420    * @author   Quinn Comendant <quinn@strangecode.com>
421    * @version  1.0
422    * @since    19 May 2015 09:57:27
423    */
[575]424    static public function isEmptyDate($val, $type=LOG_DEBUG, $file=null, $line=null)
[523]425    {
[550]426        $app =& App::getInstance();
[601]427
428        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) {
[523]429            return true;
430        }
[550]431        $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line);
[523]432        return false;
433    }
434
[144]435    /**
[550]436    * Verifies credit card number using the Luhn (mod 10) algorithm.
437    * http://en.wikipedia.org/wiki/Luhn_algorithm
438    *
[635]439    * @param  string  $val   The input data to validate.
[550]440    * @param  string  $cc_num      Card number to verify.
441    * @param  string  $cc_type     Optional, card type to do specific checks.
442    * @param  const  $type  A LOG_* constant (see App->logMsg())
443    * @param  string $file  Filename to log (usually __FILE__)
444    * @param  int    $line  Line number to log (usually __LINE__)
445    * @return bool    True if no errors found, false otherwise.
446    */
[575]447    static public function validateCCNumber($val, $cc_type=null, $type=LOG_DEBUG, $file=null, $line=null)
[468]448    {
[550]449        $app =& App::getInstance();
[724]450
[468]451        // Get rid of any non-digits
[724]452        $cc_num = preg_replace('/[^\d]/' . $app->getParam('preg_u'), '', $val);
[144]453
[468]454        // Perform card-specific checks, if applicable
455        switch ($cc_type) {
456        case self::CC_TYPE_VISA :
457            $regex = '/^4\d{15}$|^4\d{12}$/';
458            break;
459        case self::CC_TYPE_MASTERCARD :
460            $regex = '/^5[1-5]\d{14}$/';
461            break;
462        case self::CC_TYPE_AMEX :
463            $regex = '/^3[47]\d{13}$/';
464            break;
465        case self::CC_TYPE_DISCOVER :
466            $regex = '/^6011\d{12}$/';
467            break;
468        case self::CC_TYPE_DINERS :
469            $regex = '/^30[0-5]\d{11}$|^3[68]\d{12}$/';
470            break;
471        case self::CC_TYPE_JCB :
472            $regex = '/^3\d{15}$|^2131|1800\d{11}$/';
473            break;
474        default :
[470]475            $regex = '/\d{13,}/';
[468]476            break;
477        }
[457]478
[468]479        if ('' != $regex && !preg_match($regex, $cc_num)) {
480            // Invalid format.
[550]481            $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line);
[468]482            return false;
483        }
[144]484
[468]485        // The Luhn formula works right to left, so reverse the number.
486        $cc_num = strrev($cc_num);
[144]487
[468]488        $luhn_total = 0;
[144]489
[468]490        $num = mb_strlen($cc_num);
491        for ($i=0; $i<$num; $i++) {
492            // Get each digit.
493            $digit = mb_substr($cc_num, $i, 1);
[144]494
[468]495            //  If it's an odd digit, double it.
496            if ($i / 2 != floor($i / 2)) {
497                $digit *= 2;
498            }
[144]499
[468]500            //  If the result is two digits, add them.
501            if (mb_strlen($digit) == 2) {
502                $digit = mb_substr($digit, 0, 1) + mb_substr($digit, 1, 1);
503            }
[144]504
[468]505            //  Add the current digit to the $luhn_total.
506            $luhn_total += $digit;
507        }
[144]508
[468]509        // If the Total is evenly divisible by 10, it's cool!
[550]510        if ($luhn_total % 10 == 0) {
511            return true;
512        } else {
513            $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line);
514            return false;
515        }
[468]516    }
[144]517
518    /**
[550]519    * Check whether a file was selected for uploading. If file is missing, it's an error.
520    *
521    * @param  string $form_name The input data to validate.
522    * @param  const  $type  A LOG_* constant (see App->logMsg())
523    * @param  string $file  Filename to log (usually __FILE__)
524    * @param  int    $line  Line number to log (usually __LINE__)
525    * @return bool   True if no errors found, false otherwise.
526    */
[575]527    static public function fileUploaded($form_name, $type=LOG_DEBUG, $file=null, $line=null)
[144]528    {
[550]529        $app =& App::getInstance();
[200]530        if (!isset($_FILES[$form_name]['name']) || empty($_FILES[$form_name]['name'])) {
[550]531            $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, 'no _FILES'), $type, $file, $line);
[169]532            return false;
533        }
[457]534
[200]535        if (is_array($_FILES[$form_name]['name'])) {
536            foreach($_FILES[$form_name]['name'] as $f) {
[169]537                if ('' == $f) {
[550]538                    $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($_FILES)), $type, $file, $line);
[169]539                    return false;
540                }
541            }
542        } else {
[200]543            if ('' == $_FILES[$form_name]['name']) {
[550]544                $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($_FILES)), $type, $file, $line);
[169]545                return false;
546            }
547        }
[457]548
[169]549        return true;
[144]550    }
551
[487]552    /*
553    * Check if the amount of content sent by the browser exceeds the upload_max_filesize value configured in php.ini.
554    * http://stackoverflow.com/a/24202363
555    *
556    * @access   public
557    * @param    string $form_name The input data to validate.
[550]558    * @param  const  $type  A LOG_* constant (see App->logMsg())
559    * @param  string $file  Filename to log (usually __FILE__)
560    * @param  int    $line  Line number to log (usually __LINE__)
[487]561    * @return   bool   True if no errors found, false otherwise.
562    * @author   Quinn Comendant <quinn@strangecode.com>
563    * @version  1.0
564    * @since    20 Aug 2014 14:44:23
565    */
[575]566    static public function fileUploadSize($form_name, $type=LOG_DEBUG, $file=null, $line=null)
[487]567    {
[550]568        $app =& App::getInstance();
[487]569        $upload_max_filesize = phpIniGetBytes('upload_max_filesize');
570        if (isset($_SERVER['CONTENT_LENGTH']) && 0 != $upload_max_filesize && $_SERVER['CONTENT_LENGTH'] > $upload_max_filesize) {
[593]571            $app->logMsg(sprintf('%s (line %s) failed: filesize %s exceeds limit of %s', __METHOD__, __LINE__, $_SERVER['CONTENT_LENGTH'], $upload_max_filesize), $type, $file, $line);
[487]572            return false;
573        }
574        return true;
575    }
576
[144]577} // THE END
578
Note: See TracBrowser for help on using the repository browser.