/*
[SOOBIC!] admin/includes/upload.php
Version: 1.0.0
Author: Soolan (soolan@hotmail.com)
Copyright: soolan (www.soobic.com)
Last Modified: 2005/4/1 10:00
*/
class upload {
var $file, $filename, $destination, $permissions, $extensions, $tmp_filename, $message_location,$im;
function upload($file = '', $destination = '', $permissions = '777', $extensions = '') {
$this->set_file($file);
$this->set_destination($destination);
$this->set_permissions($permissions);
$this->set_extensions($extensions);
$this->set_output_messages('direct');
if (tep_not_null($this->file) && tep_not_null($this->destination)) {
$this->set_output_messages('session');
}
}
function parse($filename) {
global $messageStack;
$filename=$filename=="" ? time() :$filename;
if (isset($_FILES[$this->file])) {
$file = array('name' => $_FILES[$this->file]['name'],
'type' => $_FILES[$this->file]['type'],
'size' => $_FILES[$this->file]['size'],
'tmp_name' => $_FILES[$this->file]['tmp_name']);
} elseif (isset($GLOBALS['HTTP_POST_FILES'][$this->file])) {
global $HTTP_POST_FILES;
$file = array('name' => $HTTP_POST_FILES[$this->file]['name'],
'type' => $HTTP_POST_FILES[$this->file]['type'],
'size' => $HTTP_POST_FILES[$this->file]['size'],
'tmp_name' => $HTTP_POST_FILES[$this->file]['tmp_name']);
} else {
$file = array('name' => (isset($GLOBALS[$this->file . '_name']) ? $GLOBALS[$this->file . '_name'] : ''),
'type' => (isset($GLOBALS[$this->file . '_type']) ? $GLOBALS[$this->file . '_type'] : ''),
'size' => (isset($GLOBALS[$this->file . '_size']) ? $GLOBALS[$this->file . '_size'] : ''),
'tmp_name' => (isset($GLOBALS[$this->file]) ? $GLOBALS[$this->file] : ''));
}
if ( tep_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') && is_uploaded_file($file['tmp_name']) ) {
if(CONFIG_CREATE_SMAILL_IMAGE=='True'){
if($file['type'] == "image/pjpeg"){
$this->im = imagecreatefromjpeg($file['tmp_name']);
}elseif($file['type'] == "image/x-png"){
$this->im = imagecreatefrompng($file['tmp_name']);
}elseif($file['type'] == "image/gif"){
$this->im = imagecreatefromgif($file['tmp_name']);
}
}
if (sizeof($this->extensions) > 0) {
if (!in_array(strtolower(substr($file['name'], strrpos($file['name'], '.')+1)), $this->extensions)) {
if ($this->message_location == 'direct') {
$messageStack->add(ERROR_FILETYPE_NOT_ALLOWED, 'error');
} else {
$messageStack->add_session(ERROR_FILETYPE_NOT_ALLOWED, 'error');
}
return false;
}
}
$extension = strtolower(substr(strrchr($file['name'], '.'), 1));
$this->set_file($file);
$this->set_filename($filename.'.'.$extension);
if(CONFIG_CREATE_SMAILL_IMAGE=='True'){
$this->set_filename_small('small_'.$filename);
}
$this->set_tmp_filename($file['tmp_name']);
return $this->check_destination();
} else {
if ($this->message_location == 'direct') {
$messageStack->add(WARNING_NO_FILE_UPLOADED, 'warning');
} else {
$messageStack->add_session(WARNING_NO_FILE_UPLOADED, 'warning');
}
return false;
}
}
function save($newwidth='',$newheight='') {
global $messageStack;
if (substr($this->destination, -1) != '/') $this->destination .= '/';
if (move_uploaded_file($this->file['tmp_name'], $this->destination . $this->filename)) {
if(CONFIG_CREATE_SMAILL_IMAGE=='True'&& $newwidth!=''&& $newheight!=''){
$this->ResizeImage($this->im,$newwidth,$newheight,$this->destination . $this->filename_small);
ImageDestroy ($this->im);
}
chmod($this->destination . $this->filename, $this->permissions);
if ($this->message_location == 'direct') {
$messageStack->add(SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
} else {
$messageStack->add_session(SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
}
return true;
} else {
if ($this->message_location == 'direct') {
$messageStack->add(ERROR_FILE_NOT_SAVED, 'error');
} else {
$messageStack->add_session(ERROR_FILE_NOT_SAVED, 'error');
}
return false;
}
}
function set_file($file) {
$this->file = $file;
}
function set_destination($destination) {
if(!is_dir($destination)) {
mkdir($destination, 0777);
}
$this->destination = $destination;
}
function set_permissions($permissions) {
$this->permissions = octdec($permissions);
}
function set_filename($filename) {
$this->filename = $filename;
}
function set_filename_small($filename) {
$this->filename_small = $filename;
}
function set_tmp_filename($filename) {
$this->tmp_filename = $filename;
}
function set_extensions($extensions) {
if (tep_not_null($extensions)) {
if (is_array($extensions)) {
$this->extensions = $extensions;
} else {
$this->extensions = array($extensions);
}
} else {
$this->extensions = array();
}
}
function check_destination() {
global $messageStack;
if (!is_writeable($this->destination)) {
if (is_dir($this->destination)) {
if ($this->message_location == 'direct') {
$messageStack->add(sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error');
} else {
$messageStack->add_session(sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error');
}
} else {
if ($this->message_location == 'direct') {
$messageStack->add(sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error');
} else {
$messageStack->add_session(sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error');
}
}
return false;
} else {
return true;
}
}
function set_output_messages($location) {
switch ($location) {
case 'session':
$this->message_location = 'session';
break;
case 'direct':
default:
$this->message_location = 'direct';
break;
}
}
function ResizeImage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$name . ".jpg");
}
}
}
?>