|
ZenCart_Documentation
1.5.0
http://www.collinsharper.com
|
00001 <?php 00012 class upload { 00013 var $file, $filename, $destination, $permissions, $extensions, $tmp_filename, $message_location; 00014 00015 function upload($file = '', $destination = '', $permissions = '644', $extensions = '') { 00016 $this->set_file($file); 00017 $this->set_destination($destination); 00018 $this->set_permissions($permissions); 00019 $this->set_extensions($extensions); 00020 00021 $this->set_output_messages('direct'); 00022 00023 if (zen_not_null($this->file) && zen_not_null($this->destination)) { 00024 $this->set_output_messages('session'); 00025 00026 if ( ($this->parse() == true) && ($this->save() == true) ) { 00027 return true; 00028 } else { 00029 // self destruct 00030 while(list($key,) = each($this)) { 00031 $this->$key = null; 00032 } 00033 00034 return false; 00035 } 00036 } 00037 } 00038 00039 function parse() { 00040 global $messageStack; 00041 00042 if (isset($_FILES[$this->file])) { 00043 $file = array('name' => $_FILES[$this->file]['name'], 00044 'type' => $_FILES[$this->file]['type'], 00045 'size' => $_FILES[$this->file]['size'], 00046 'tmp_name' => $_FILES[$this->file]['tmp_name']); 00047 } elseif (isset($GLOBALS['HTTP_POST_FILES'][$this->file])) { 00048 global $HTTP_POST_FILES; 00049 00050 $file = array('name' => $HTTP_POST_FILES[$this->file]['name'], 00051 'type' => $HTTP_POST_FILES[$this->file]['type'], 00052 'size' => $HTTP_POST_FILES[$this->file]['size'], 00053 'tmp_name' => $HTTP_POST_FILES[$this->file]['tmp_name']); 00054 } else { 00055 $file = array('name' => (isset($GLOBALS[$this->file . '_name']) ? $GLOBALS[$this->file . '_name'] : ''), 00056 'type' => (isset($GLOBALS[$this->file . '_type']) ? $GLOBALS[$this->file . '_type'] : ''), 00057 'size' => (isset($GLOBALS[$this->file . '_size']) ? $GLOBALS[$this->file . '_size'] : ''), 00058 'tmp_name' => (isset($GLOBALS[$this->file]) ? $GLOBALS[$this->file] : '')); 00059 } 00060 00061 if ( zen_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') && is_uploaded_file($file['tmp_name']) ) { 00062 if (sizeof($this->extensions) > 0 || substr($file['name'], -9) == '.htaccess') { 00063 if (!in_array(strtolower(substr($file['name'], strrpos($file['name'], '.')+1)), $this->extensions) || substr($file['name'], -9) == '.htaccess') { 00064 if ($this->message_location == 'direct') { 00065 $messageStack->add(sprintf(ERROR_FILETYPE_NOT_ALLOWED, strtolower(substr($file['name'], strrpos($file['name'], '.')+1))), 'error'); 00066 } else { 00067 $messageStack->add_session(sprintf(ERROR_FILETYPE_NOT_ALLOWED, strtolower(substr($file['name'], strrpos($file['name'], '.')+1))), 'error'); 00068 } 00069 00070 return false; 00071 } 00072 } 00073 00074 $this->set_file($file); 00075 $this->set_filename($file['name']); 00076 $this->set_tmp_filename($file['tmp_name']); 00077 00078 return $this->check_destination(); 00079 } else { 00080 if ($file['name'] !='' && $file['tmp_name'] !='') { 00081 if ($this->message_location == 'direct') { 00082 $messageStack->add(WARNING_NO_FILE_UPLOADED, 'warning'); 00083 } else { 00084 $messageStack->add_session(WARNING_NO_FILE_UPLOADED, 'warning'); 00085 } 00086 } 00087 return false; 00088 } 00089 } 00090 00091 function save($overwrite=true) { 00092 global $messageStack; 00093 00094 if (!$overwrite and file_exists($this->destination . $this->filename)) { 00095 $messageStack->add_session(TEXT_IMAGE_OVERWRITE_WARNING . $this->filename, 'caution'); 00096 return true; 00097 } else { 00098 00099 if (substr($this->destination, -1) != '/') $this->destination .= '/'; 00100 00101 if (move_uploaded_file($this->file['tmp_name'], $this->destination . $this->filename)) { 00102 chmod($this->destination . $this->filename, $this->permissions); 00103 00104 if ($this->message_location == 'direct') { 00105 $messageStack->add(sprintf(SUCCESS_FILE_SAVED_SUCCESSFULLY,$this->filename), 'success'); 00106 } else { 00107 $messageStack->add_session(sprintf(SUCCESS_FILE_SAVED_SUCCESSFULLY,$this->filename), 'success'); 00108 } 00109 00110 return true; 00111 } else { 00112 if ($this->message_location == 'direct') { 00113 $messageStack->add(ERROR_FILE_NOT_SAVED, 'error'); 00114 } else { 00115 $messageStack->add_session(ERROR_FILE_NOT_SAVED, 'error'); 00116 } 00117 00118 return false; 00119 } 00120 } 00121 } 00122 00123 function set_file($file) { 00124 $this->file = $file; 00125 } 00126 00127 function set_destination($destination) { 00128 $this->destination = $destination; 00129 } 00130 00131 function set_permissions($permissions) { 00132 $this->permissions = octdec($permissions); 00133 } 00134 00135 function set_filename($filename) { 00136 $this->filename = $filename; 00137 } 00138 00139 function set_tmp_filename($filename) { 00140 $this->tmp_filename = $filename; 00141 } 00142 00143 function set_extensions($extensions) { 00144 if (zen_not_null($extensions)) { 00145 if (is_array($extensions)) { 00146 $this->extensions = $extensions; 00147 } else { 00148 $this->extensions = array($extensions); 00149 } 00150 } else { 00151 $this->extensions = array(); 00152 } 00153 } 00154 00155 function check_destination() { 00156 global $messageStack; 00157 00158 if (!is_writeable($this->destination)) { 00159 if (is_dir($this->destination)) { 00160 if ($this->message_location == 'direct') { 00161 $messageStack->add(sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error'); 00162 } else { 00163 $messageStack->add_session(sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error'); 00164 } 00165 } else { 00166 if ($this->message_location == 'direct') { 00167 $messageStack->add(sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error'); 00168 } else { 00169 $messageStack->add_session(sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error'); 00170 } 00171 } 00172 00173 return false; 00174 } else { 00175 return true; 00176 } 00177 } 00178 00179 function set_output_messages($location) { 00180 switch ($location) { 00181 case 'session': 00182 $this->message_location = 'session'; 00183 break; 00184 case 'direct': 00185 default: 00186 $this->message_location = 'direct'; 00187 break; 00188 } 00189 } 00190 } 00191