Ignore:
Timestamp:
Nov 17, 2005 3:00:00 AM (19 years ago)
Author:
scdev
Message:

Tons of little updates and bugfixes. CSS updates to templates and core css files. File upload ability to module_maker. Remade Upload interface to use setParam/getParam.

File:
1 edited

Legend:

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

    r19 r20  
    33 * Upload.inc.php
    44 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information
    5  */
    6 
    7 /**
     5 *
    86 * The Upload class provides an interface to deal with http uploaded files.
    97 *
    108 * @author  Quinn Comendant <quinn@strangecode.com>
    119 * @requires App.inc.php
    12  * @version 1.0
     10 * @version 1.2
    1311 */
    1412
     
    1917    // General object parameters.
    2018    var $_params = array(
     19   
     20        // Display message with raiseMsg?
    2121        'display_messages' => true,
     22       
     23        // Existing files will be overwritten when there is a name conflict?
    2224        'allow_overwriting' => false,
     25
     26        // The filesystem path to the final upload directory.
     27        'upload_path' => null,
     28
     29        // The file permissions of the uploaded files. Remember, files will be owned by the web server user.
     30        'dest_file_perms' => 0600,
     31
     32        // Require file to have one of the following file extentions.
     33        'valid_file_extensions' => array('jpg', 'jpeg', 'gif', 'png', 'pdf', 'txt', 'text', 'html', 'htm'),
    2334    );
    24 
    25     // Disk path where new image(s) will be uploaded.
    26     var $upload_directory_path = null;
    2735
    2836    // Array of files with errors.
    2937    var $errors = array();
    30    
    31     // Array of acceptable file extensions (lowercase).
    32     var $valid_file_extensions = array('jpg', 'jpeg', 'gif', 'png', 'pdf', 'txt', 'text', 'html', 'htm');
    33    
    34     // The uploaded files will normally be owned by user 'apache'. Set world-read/write
    35     // if the website admin needs to read/delete these files.
    36     var $dest_file_perms = 0600;
    37    
     38
    3839    // Array of file extensions and corresponding mime-types.
    3940    var $mime_extension_map = array(
     
    223224    {
    224225        if (isset($params) && is_array($params)) {
     226       
     227            // Enforce valid upload_path parameter.
     228            if (isset($params['upload_path'])) {
     229                $params['upload_path'] = realpath($params['upload_path']);
     230                // Must be directory.
     231                if (!is_dir($params['upload_path'])) {
     232                    App::logMsg(sprintf('Upload directory invalid: %s', $params['upload_path']), LOG_ERR, __FILE__, __LINE__);
     233                    trigger_error(sprintf('Upload directory invalid: %s', $params['upload_path']), E_USER_ERROR);
     234                }
     235                // Must be writable.
     236                if (!is_writable($params['upload_path'])) {
     237                    App::logMsg(sprintf('Upload directory not writable: %s', $params['upload_path']), LOG_ERR, __FILE__, __LINE__);
     238                    trigger_error(sprintf('Upload directory not writable: %s', $params['upload_path']), E_USER_ERROR);
     239                }
     240                // Set the default upload path, stripping any extra slashes if needed.
     241                $params['upload_path'] = preg_replace('!/+$!', '', $params['upload_path']);
     242            }
     243       
    225244            // Merge new parameters with old overriding only those passed.
    226245            $this->_params = array_merge($this->_params, $params);
     
    250269     *
    251270     */
    252     function setUploadPath($path)
    253     {
    254         $path = realpath($path);
    255        
    256         if (!is_dir($path)) {
    257             App::logMsg(sprintf('Upload directory invalid: %s', $path), LOG_ERR, __FILE__, __LINE__);
    258         }
    259         if (!is_writable($path)) {
    260             App::logMsg(sprintf('Upload directory not writable: %s', $path), LOG_ERR, __FILE__, __LINE__);
    261         }
    262        
    263         // Set the default upload path, stripping any extra slashes if needed.
    264         $this->upload_directory_path = preg_replace('!/+$!', '', $path);
    265     }
    266 
    267     /**
    268      *
    269      */
    270271    function process($form_name, $custom_file_name=null)
    271272    {
    272273        // Ensure we have a upload directory.
    273         if (!isset($this->upload_directory_path)) {
     274        if (!$this->getParam('upload_path')) {
    274275            App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);
    275276            $this->raiseMsg(_("There was a problem with the file upload. Please try again later."), MSG_ERR, __FILE__, __LINE__);
     
    364365           
    365366            // Check to be sure the file has a valid file extension.
    366             if (!in_array(strtolower($this->getFilenameExtension($files['name'][$i])), $this->valid_file_extensions)) {
    367                 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it is an unrecognized type. Files must have one of the following file extensions: %s."), $files['name'][$i], join(', ', $this->valid_file_extensions)), MSG_ERR, __FILE__, __LINE__);
     367            if (!in_array(strtolower($this->getFilenameExtension($files['name'][$i])), $this->getParam('valid_file_extensions'))) {
     368                $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it is an unrecognized type. Files must have one of the following file extensions: %s."), $files['name'][$i], join(', ', $this->getParam('valid_file_extensions'))), MSG_ERR, __FILE__, __LINE__);
    368369                App::logMsg(sprintf(_("The uploaded file %s has an unrecognized file extension."), $files['name'][$i]), LOG_WARNING, __FILE__, __LINE__);
    369370                $this->errors[] = $files['name'][$i];
     
    417418           
    418419            // Set the path and file name.
    419             $file_path_name = $this->upload_directory_path . '/' . $file_name;
     420            $file_path_name = $this->getParam('upload_path') . '/' . $file_name;
    420421           
    421422            // Move the file to the final place.
    422423            if (move_uploaded_file($files['tmp_name'][$i], $file_path_name)) {
    423                 chmod($file_path_name, $this->dest_file_perms);
     424                chmod($file_path_name, $this->getParam('dest_file_perms'));
    424425                $this->raiseMsg(sprintf(_("The file <strong>%s</strong> uploaded successfully."), $files['name'][$i]), MSG_SUCCESS, __FILE__, __LINE__);
    425426                if (!isset($custom_file_name) && $files['name'][$i] != $file_name) {
     
    449450    {
    450451        // Ensure we have a upload directory.
    451         if (!isset($this->upload_directory_path)) {
     452        if (!$this->getParam('upload_path')) {
    452453            App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);
    453454            return false;
    454455        }
    455456       
    456         $file_path_name = $this->upload_directory_path . '/' . $file_name;
     457        $file_path_name = $this->getParam('upload_path') . '/' . $file_name;
    457458
    458459        if (!is_file($file_path_name)) {
     
    474475    {
    475476        // Ensure we have an upload directory.
    476         if (!isset($this->upload_directory_path)) {
     477        if (!$this->getParam('upload_path')) {
    477478            App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);
    478479            return false;
    479480        }
    480481       
    481         $old_file_path_name = $this->upload_directory_path . '/' . $old_name;
    482         $new_file_path_name = $this->upload_directory_path . '/' . $new_name;
     482        $old_file_path_name = $this->getParam('upload_path') . '/' . $old_name;
     483        $new_file_path_name = $this->getParam('upload_path') . '/' . $new_name;
    483484        if (file_exists($old_file_path_name)) {
    484             if (!rename($old_file_path_name, $new_file_path_name)) {
     485            if (rename($old_file_path_name, $new_file_path_name)) {
     486                $this->raiseMsg(sprintf(_("The file <strong>%s</strong> has been renamed to <strong>%s</strong>."), basename($old_file_path_name), basename($new_file_path_name)), MSG_NOTICE, __FILE__, __LINE__);
     487                App::logMsg(sprintf('File renamed from %s to %s', $old_file_path_name, $new_file_path_name), LOG_DEBUG, __FILE__, __LINE__);
     488            } else {
    485489                $this->raiseMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), MSG_ERR, __FILE__, __LINE__);
    486490                App::logMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), LOG_ERR, __FILE__, __LINE__);
     
    500504    {
    501505        // Ensure we have a upload directory.
    502         if (!isset($this->upload_directory_path)) {
     506        if (!$this->getParam('upload_path')) {
    503507            App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);
    504508            return false;
    505509        }
    506510       
    507         return file_exists($this->upload_directory_path . '/' . $file_name);
     511        return file_exists($this->getParam('upload_path') . '/' . $file_name);
     512    }
     513
     514    /**
     515     * Get filename by glob pattern. Searches a directory for an image that matches the
     516     * specified glob pattern and returns the filename of the first file found.
     517     *
     518     * @access  public
     519     * @param   string  $pattern   Pattern to match filename.
     520     * @return  string filename on success, empty string on failure.
     521     * @author  Quinn Comendant <quinn@strangecode.com>
     522     * @since   15 Nov 2005 20:55:22
     523     */
     524    function getFilenameGlob($pattern)
     525    {
     526        $file_list = glob(sprintf('%s/%s', $this->getParam('upload_path'), $pattern));
     527        if (isset($file_list[0])) {
     528            return basename($file_list[0]);
     529        } else {
     530            return '';
     531        }
    508532    }
    509533
Note: See TracChangeset for help on using the changeset viewer.