Changeset 487


Ignore:
Timestamp:
Aug 20, 2014 11:56:27 AM (10 years ago)
Author:
anonymous
Message:

Added Validator::fileUploadSize(), phpIniGetBytes(), and fixed php version error checking.

Location:
trunk/lib
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/App.inc.php

    r484 r487  
    4242class App {
    4343
     44    // Minimum version of PHP required for this version of the Codebase.
     45    const CODEBASE_MIN_PHP_VERSION = '5.3.0';
     46
    4447    // A place to keep an object instance for the singleton pattern.
    4548    protected static $instance = null;
     
    179182        $this->timer = new ScriptTimer();
    180183        $this->timer->start('_app');
    181 
    182         // The codebase now requires a minimum PHP version.
    183         $codebase_minimum_php_version = '5.3.0';
    184         if (version_compare(PHP_VERSION, $codebase_minimum_php_version, '<')) {
    185             $this->logMsg(sprintf('Codebase minimum PHP version of %s not satisfied (you have %s). ', $codebase_minimum_php_version, phpversion()), LOG_NOTICE, __FILE__, __LINE__);
    186         }
    187184    }
    188185
     
    382379        // Set the version of the codebase we're using.
    383380        $codebase_version_file = dirname(__FILE__) . '/../docs/version.txt';
     381        $codebase_version = '';
    384382        if (is_readable($codebase_version_file)) {
    385383            $codebase_version = trim(file_get_contents($codebase_version_file));
     
    388386                header('X-Codebase-Version: ' . $codebase_version);
    389387            }
     388        }
     389
     390        if (version_compare(PHP_VERSION, self::CODEBASE_MIN_PHP_VERSION, '<')) {
     391            $this->logMsg(sprintf('PHP %s required for Codebase %s, using %s; some things will break.', self::CODEBASE_MIN_PHP_VERSION, $codebase_version, PHP_VERSION), LOG_NOTICE, __FILE__, __LINE__);
    390392        }
    391393
  • trunk/lib/FormValidator.inc.php

    r484 r487  
    595595        }
    596596    }
     597    /**
     598     * Check whether a file was selected for uploading. If file is missing, it's an error.
     599     *
     600     * @param  string $form_name the name of the incoming form variable
     601     * @param  string $msg       the message to display on error
     602     *
     603     * @return bool   true if no errors found, false otherwise
     604     */
     605    public function fileUploadSize($form_name, $msg='')
     606    {
     607        if (Validator::fileUploadSize($form_name)) {
     608            return true;
     609        } else {
     610            $msg = '' == $msg ? sprintf(_("Maximum filesize exceeded. Got %s, but limit is %s."), humanFileSize($_SERVER['CONTENT_LENGTH']), humanFileSize(phpIniGetBytes('upload_max_filesize'))) : $msg;
     611            $this->addError($form_name, $msg);
     612            return false;
     613        }
     614    }
    597615
    598616} // THE END
  • trunk/lib/Utilities.inc.php

    r486 r487  
    503503}
    504504
     505/*
     506* Convert a php.ini value (8M, 512K, etc), into integer value of bytes.
     507*
     508* @access   public
     509* @param    string  $val    Value from php config, e.g., upload_max_filesize.
     510* @return   int             Value converted to bytes as an integer.
     511* @author   Quinn Comendant <quinn@strangecode.com>
     512* @version  1.0
     513* @since    20 Aug 2014 14:32:41
     514*/
     515function phpIniGetBytes($val)
     516{
     517    $val = trim(ini_get($val));
     518    if ($val != '') {
     519        $last = strtolower($val{strlen($val) - 1});
     520    } else {
     521        $last = '';
     522    }
     523    switch ($last) {
     524        // The 'G' modifier is available since PHP 5.1.0
     525        case 'g':
     526            $val *= 1024;
     527        case 'm':
     528            $val *= 1024;
     529        case 'k':
     530            $val *= 1024;
     531    }
     532
     533    return (int)$val;
     534}
     535
    505536/**
    506537 * Tests the existence of a file anywhere in the include path.
  • trunk/lib/Validator.inc.php

    r479 r487  
    363363    }
    364364
     365    /*
     366    * Check if the amount of content sent by the browser exceeds the upload_max_filesize value configured in php.ini.
     367    * http://stackoverflow.com/a/24202363
     368    *
     369    * @access   public
     370    * @param    string $form_name The input data to validate.
     371    * @return   bool   True if no errors found, false otherwise.
     372    * @author   Quinn Comendant <quinn@strangecode.com>
     373    * @version  1.0
     374    * @since    20 Aug 2014 14:44:23
     375    */
     376    static public function fileUploadSize($form_name)
     377    {
     378        $upload_max_filesize = phpIniGetBytes('upload_max_filesize');
     379        if (isset($_SERVER['CONTENT_LENGTH']) && 0 != $upload_max_filesize && $_SERVER['CONTENT_LENGTH'] > $upload_max_filesize) {
     380            return false;
     381        }
     382        return true;
     383    }
     384
    365385} // THE END
    366386
Note: See TracChangeset for help on using the changeset viewer.