[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

/admin/includes/classes/ -> upload.php (source)

   1  <?php
   2  /*

   3  $Id: upload.php 3 2006-05-27 04:59:07Z user $

   4  

   5    osCMax Power E-Commerce

   6    http://oscdox.com

   7  

   8    Copyright 2006 osCMax

   9  

  10    Released under the GNU General Public License

  11  */
  12  
  13    class upload {
  14      var $file, $filename, $destination, $permissions, $extensions, $tmp_filename, $message_location;
  15  
  16      function upload($file = '', $destination = '', $permissions = '777', $extensions = '') {
  17        $this->set_file($file);
  18        $this->set_destination($destination);
  19        $this->set_permissions($permissions);
  20        $this->set_extensions($extensions);
  21  
  22        $this->set_output_messages('direct');
  23  
  24        if (tep_not_null($this->file) && tep_not_null($this->destination)) {
  25          $this->set_output_messages('session');
  26  
  27          if ( ($this->parse() == true) && ($this->save() == true) ) {
  28            return true;
  29          } else {
  30  
  31            return false;
  32          }
  33        }
  34      }
  35  
  36      function parse() {
  37        global $HTTP_POST_FILES, $messageStack;
  38  
  39        $file = array();
  40        if (isset($_FILES[$this->file])) {
  41          $file = array('name' => $_FILES[$this->file]['name'],
  42                        'type' => $_FILES[$this->file]['type'],
  43                        'size' => $_FILES[$this->file]['size'],
  44                        'tmp_name' => $_FILES[$this->file]['tmp_name']);
  45        } elseif (isset($HTTP_POST_FILES[$this->file])) {
  46          $file = array('name' => $HTTP_POST_FILES[$this->file]['name'],
  47                        'type' => $HTTP_POST_FILES[$this->file]['type'],
  48                        'size' => $HTTP_POST_FILES[$this->file]['size'],
  49                        'tmp_name' => $HTTP_POST_FILES[$this->file]['tmp_name']);
  50        }
  51  
  52        if ( tep_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') && is_uploaded_file($file['tmp_name']) ) {
  53          if (sizeof($this->extensions) > 0) {
  54            if (!in_array(strtolower(substr($file['name'], strrpos($file['name'], '.')+1)), $this->extensions)) {
  55              if ($this->message_location == 'direct') {
  56                $messageStack->add(ERROR_FILETYPE_NOT_ALLOWED, 'error');
  57              } else {
  58                $messageStack->add_session(ERROR_FILETYPE_NOT_ALLOWED, 'error');
  59              }
  60  
  61              return false;
  62            }
  63          }
  64  
  65          $this->set_file($file);
  66          $this->set_filename($file['name']);
  67          $this->set_tmp_filename($file['tmp_name']);
  68  
  69          return $this->check_destination();
  70        } else {
  71          if ($this->message_location == 'direct') {
  72            $messageStack->add(WARNING_NO_FILE_UPLOADED, 'warning');
  73          } else {
  74            $messageStack->add_session(WARNING_NO_FILE_UPLOADED, 'warning');
  75          }
  76  
  77          return false;
  78        }
  79      }
  80  
  81      function save() {
  82        global $messageStack;
  83  
  84        if (substr($this->destination, -1) != '/') $this->destination .= '/';
  85  
  86        if (move_uploaded_file($this->file['tmp_name'], $this->destination . $this->filename)) {
  87          chmod($this->destination . $this->filename, $this->permissions);
  88  
  89          if ($this->message_location == 'direct') {
  90            $messageStack->add(SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
  91          } else {
  92            $messageStack->add_session(SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
  93          }
  94  
  95          return true;
  96        } else {
  97          if ($this->message_location == 'direct') {
  98            $messageStack->add(ERROR_FILE_NOT_SAVED, 'error');
  99          } else {
 100            $messageStack->add_session(ERROR_FILE_NOT_SAVED, 'error');
 101          }
 102  
 103          return false;
 104        }
 105      }
 106  
 107      function set_file($file) {
 108        $this->file = $file;
 109      }
 110  
 111      function set_destination($destination) {
 112        $this->destination = $destination;
 113      }
 114  
 115      function set_permissions($permissions) {
 116        $this->permissions = octdec($permissions);
 117      }
 118  
 119      function set_filename($filename) {
 120        $this->filename = $filename;
 121      }
 122  
 123      function set_tmp_filename($filename) {
 124        $this->tmp_filename = $filename;
 125      }
 126  
 127      function set_extensions($extensions) {
 128        if (tep_not_null($extensions)) {
 129          if (is_array($extensions)) {
 130            $this->extensions = $extensions;
 131          } else {
 132            $this->extensions = array($extensions);
 133          }
 134        } else {
 135          $this->extensions = array();
 136        }
 137      }
 138  
 139      function check_destination() {
 140        global $messageStack;
 141  
 142        if (!is_writeable($this->destination)) {
 143          if (is_dir($this->destination)) {
 144            if ($this->message_location == 'direct') {
 145              $messageStack->add(sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error');
 146            } else {
 147              $messageStack->add_session(sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error');
 148            }
 149          } else {
 150            if ($this->message_location == 'direct') {
 151              $messageStack->add(sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error');
 152            } else {
 153              $messageStack->add_session(sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error');
 154            }
 155          }
 156  
 157          return false;
 158        } else {
 159          return true;
 160        }
 161      }
 162  
 163      function set_output_messages($location) {
 164        switch ($location) {
 165          case 'session':
 166            $this->message_location = 'session';
 167            break;
 168          case 'direct':
 169          default:
 170            $this->message_location = 'direct';
 171            break;
 172        }
 173      }
 174    }
 175  ?>


Generated: Fri Jan 1 13:43:16 2010 Cross-referenced by PHPXref 0.7