[phpBB Debug] PHP Warning: in file [ROOT]/includes/crs/crs_misc_functions.php on line 37: mime_content_type(): Empty filename or path
[phpBB Debug] PHP Warning: in file [ROOT]/includes/crs/crs_misc_functions.php on line 37: mime_content_type(): Empty filename or path
Zen Cart 源代码 upload.php

Zen Cart 源代码 upload.php




下载文件

文件名: upload.php
文件类型: PHP文件
文件大小: 7.62 KiB
MD5: 6d7da0057240e5aca3f60a9b45684269

upload.php - 关闭高亮
  1. <?php
  2. /**
  3.  * upload Class.
  4.  *
  5.  * @package classes
  6.  * @copyright Copyright 2003-2011 Zen Cart Development Team
  7.  * @copyright Portions Copyright 2003 osCommerce
  8.  * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
  9.  * @version $Id: upload.php 18697 2011-05-04 14:35:20Z wilt $
  10.  */
  11. if (!defined('IS_ADMIN_FLAG')) {
  12.   die('Illegal Access');
  13. }
  14. /**
  15.  * upload Class.
  16.  * This class is used to manage file uploads
  17.  *
  18.  * @package classes
  19.  */
  20. class upload extends base {
  21.   var $file, $filename, $destination, $permissions, $extensions, $tmp_filename, $message_location;
  22.  
  23.   function upload($file = '', $destination = '', $permissions = '644', $extensions = array() ) {
  24.     $this->set_file($file);
  25.     $this->set_destination($destination);
  26.     $this->set_permissions($permissions);
  27.  
  28.     if (!zen_not_null($extensions)) {
  29.       if (!defined(UPLOAD_FILENAME_EXTENSIONS)) define ('UPLOAD_FILENAME_EXTENSIONS','jpg,jpeg,gif,png,eps,cdr,ai,pdf,tif,tiff,bmp,zip');
  30.       $extensions=explode(" ",preg_replace('/[.,;\s]+/',' ',UPLOAD_FILENAME_EXTENSIONS));
  31.     }
  32.     $this->set_extensions($extensions);
  33.  
  34.     $this->set_output_messages('direct');
  35.  
  36.     if (zen_not_null($this->file) && zen_not_null($this->destination)) {
  37.       $this->set_output_messages('session');
  38.  
  39.       if ( ($this->parse() == true) && ($this->save() == true) ) {
  40.         return true;
  41.       } else {
  42.         // self destruct
  43.         while(list($key,) = each($this)) {
  44.           $this->$key = null;
  45.         }
  46.  
  47.         return false;
  48.       }
  49.     }
  50.   }
  51.  
  52.   // iii Added: $key to differentiate between different files uploaded
  53.   function parse($key = '') {
  54.     global $messageStack;
  55.  
  56.     if (isset($_FILES[$this->file])) {
  57.       if (zen_not_null($key)) {
  58.         $file = array('name' => $_FILES[$this->file]['name'][$key],
  59.         'type' => $_FILES[$this->file]['type'][$key],
  60.         'size' => $_FILES[$this->file]['size'][$key],
  61.         'tmp_name' => $_FILES[$this->file]['tmp_name'][$key]);
  62.       } else {
  63.         $file = array('name' => $_FILES[$this->file]['name'],
  64.         'type' => $_FILES[$this->file]['type'],
  65.         'size' => $_FILES[$this->file]['size'],
  66.         'tmp_name' => $_FILES[$this->file]['tmp_name']);
  67.       }
  68.     } elseif (isset($GLOBALS['HTTP_POST_FILES'][$this->file])) {
  69.       global $HTTP_POST_FILES;
  70.  
  71.       $file = array('name' => $HTTP_POST_FILES[$this->file]['name'],
  72.       'type' => $HTTP_POST_FILES[$this->file]['type'],
  73.       'size' => $HTTP_POST_FILES[$this->file]['size'],
  74.       'tmp_name' => $HTTP_POST_FILES[$this->file]['tmp_name']);
  75.     } else {
  76.       $file = array('name' => (isset($GLOBALS[$this->file . '_name']) ? $GLOBALS[$this->file . '_name'] : ''),
  77.       'type' => (isset($GLOBALS[$this->file . '_type']) ? $GLOBALS[$this->file . '_type'] : ''),
  78.       'size' => (isset($GLOBALS[$this->file . '_size']) ? $GLOBALS[$this->file . '_size'] : ''),
  79.       'tmp_name' => (isset($GLOBALS[$this->file]) ? $GLOBALS[$this->file] : ''));
  80.     }
  81.     //if (!zen_not_null($file['tmp_name'])) return false;
  82.     //if ($file['tmp_name'] == 'none') return false;
  83.     //if (!is_uploaded_file($file['tmp_name'])) return false;
  84.  
  85. // not working at this time to test for server limit error
  86.     if (!is_uploaded_file($file['tmp_name'])) {
  87.       if ($this->message_location == 'direct') {
  88.         $messageStack->add_session('header', WARNING_NO_FILE_UPLOADED, 'warning');
  89.       } else {
  90.         $messageStack->add_session('upload', WARNING_NO_FILE_UPLOADED, 'warning');
  91.       }
  92.       return false;
  93.     }
  94.  
  95.     if ( zen_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') && is_uploaded_file($file['tmp_name']) ) {
  96.       if (zen_not_null($file['size']) and ($file['size'] > MAX_FILE_UPLOAD_SIZE)) {
  97.         if ($this->message_location == 'direct') {
  98.           $messageStack->add_session('header', ERROR_FILE_TOO_BIG, 'error');
  99.         } else {
  100.           $messageStack->add_session('upload', ERROR_FILE_TOO_BIG, 'error');
  101.         }
  102.         return false;
  103.       }
  104.       if (substr($file['name'], -9) == '.htaccess' || (sizeof($this->extensions) > 0 && !in_array(strtolower(substr($file['name'], strrpos($file['name'], '.')+1)), $this->extensions))) {
  105.           if ($this->message_location == 'direct') {
  106.             $messageStack->add_session('header', ERROR_FILETYPE_NOT_ALLOWED . ' ' . UPLOAD_FILENAME_EXTENSIONS, 'error');
  107.           } else {
  108.             $messageStack->add_session('upload', ERROR_FILETYPE_NOT_ALLOWED . ' - ' . UPLOAD_FILENAME_EXTENSIONS, 'error');
  109.           }
  110.           return false;
  111.       }
  112.  
  113.       $this->set_file($file);
  114.       $this->set_filename($file['name']);
  115.       $this->set_tmp_filename($file['tmp_name']);
  116.  
  117.       return $this->check_destination();
  118.     } else {
  119.       if ($this->message_location == 'direct') {
  120.         $messageStack->add_session('header', WARNING_NO_FILE_UPLOADED, 'warning');
  121.       } else {
  122.         $messageStack->add_session('upload', WARNING_NO_FILE_UPLOADED, 'warning');
  123.       }
  124.       return false;
  125.     }
  126.   }
  127.  
  128.   function save() {
  129.     global $messageStack;
  130.  
  131.     if (substr($this->destination, -1) != '/') $this->destination .= '/';
  132.  
  133.     if (@move_uploaded_file($this->file['tmp_name'], $this->destination . $this->filename)) {
  134.       chmod($this->destination . $this->filename, $this->permissions);
  135.  
  136.       if ($this->message_location == 'direct') {
  137.         $messageStack->add_session('header', SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
  138.       } else {
  139.         $messageStack->add_session('upload', SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
  140.       }
  141.  
  142.       return true;
  143.     } else {
  144.       if ($this->message_location == 'direct') {
  145.         $messageStack->add_session('header', ERROR_FILE_NOT_SAVED, 'error');
  146.       } else {
  147.         $messageStack->add_session('upload', ERROR_FILE_NOT_SAVED, 'error');
  148.       }
  149.  
  150.       return false;
  151.     }
  152.   }
  153.  
  154.   function set_file($file) {
  155.     $this->file = $file;
  156.   }
  157.  
  158.   function set_destination($destination) {
  159.     $this->destination = $destination;
  160.   }
  161.  
  162.   function set_permissions($permissions) {
  163.     $this->permissions = octdec($permissions);
  164.   }
  165.  
  166.   function set_filename($filename) {
  167.     $this->filename = $filename;
  168.   }
  169.  
  170.   function set_tmp_filename($filename) {
  171.     $this->tmp_filename = $filename;
  172.   }
  173.  
  174.   function set_extensions($extensions) {
  175.     if (zen_not_null($extensions)) {
  176.       if (is_array($extensions)) {
  177.         $this->extensions = $extensions;
  178.       } else {
  179.         $this->extensions = array($extensions);
  180.       }
  181.     } else {
  182.       $this->extensions = array();
  183.     }
  184.   }
  185.  
  186.   function check_destination() {
  187.     global $messageStack;
  188.  
  189.     if (!is_writeable($this->destination)) {
  190.       if (is_dir($this->destination)) {
  191.         if ($this->message_location == 'direct') {
  192.           $messageStack->add_session('header', sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error');
  193.         } else {
  194.           $messageStack->add_session('upload', sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error');
  195.         }
  196.       } else {
  197.         if ($this->message_location == 'direct') {
  198.           $messageStack->add_session('header', sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error');
  199.         } else {
  200.           $messageStack->add_session('upload', sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error');
  201.         }
  202.       }
  203.  
  204.       return false;
  205.     } else {
  206.       return true;
  207.     }
  208.   }
  209.  
  210.   function set_output_messages($location) {
  211.     switch ($location) {
  212.       case 'session':
  213.       $this->message_location = 'session';
  214.       break;
  215.       case 'direct':
  216.       default:
  217.       $this->message_location = 'direct';
  218.       break;
  219.     }
  220.   }
  221. }
  222.