<?
class Upload {
    var $up_file;
	var $f;
    var $p;
	var $name;
	var $size;
	var $width;
	var $height;
	var $type;
	var $max_size;
	var $path;
	var $directory;
	var $errors;
	var $move;
	
	function Upload($input_name,$size,$width,$height,$dir) {
	    $this->f = $input_name;
		$this->max_size = $size;
		$this->directory = $dir;
		$this->max_width = $width;
		$this->max_height = $height;
	}
	
  function do_upload($audio=NULL) {
  //global $_FILES;
	    $this->up_file = $_FILES[$this->f]['tmp_name'];
		$this->name = $_FILES[$this->f]['name'];
		$this->size = $_FILES[$this->f]['size']/1000;
   	    $this->type = $_FILES[$this->f]['type'];
        if    (ereg(".gif$", $this->type) && ($audio == NULL)) $this->type="gif";
        elseif((ereg(".png$", $this->type)||
               ereg(".x-png$", $this->type)) && ($audio == NULL)) $this->type="png";
        elseif((ereg(".jpg$", $this->type)||
               ereg(".jpeg$", $this->type)||
               ereg(".pjpeg$", $this->type)) && ($audio == NULL)) $this->type="jpg";
        elseif(ereg(".mp3$", $this->type) && (($audio == '1') || ($audio == '3'))) $this->type="mp3";
        elseif(ereg(".wav$", $this->type) && (($audio == '1') || ($audio == '3'))) $this->type="wav";
        elseif(ereg(".mpeg$", $this->type) && (($audio == '1') || ($audio == '3'))) $this->type="mp3";
        elseif(ereg(".midi$", $this->type) && (($audio == '1') || ($audio == '3'))) $this->type="mid";
        elseif(ereg(".asf$", $this->type) && ($audio >= '2')) $this->type="asf";
        elseif(ereg(".wmv$", $this->type) && ($audio >= '2')) $this->type="wmv";
        elseif(ereg(".mpeg$", $this->type) && ($audio >= '2')) $this->type="mpg";
        elseif(ereg(".mpg$", $this->type) && ($audio >= '2')) $this->type="mpg";
        elseif(ereg(".msvideo$", $this->type) && ($audio >= '2')) $this->type="avi";
        elseif(ereg(".octet-stream$", $this->type) && ($audio >= '2')) $this->type="3gp";        
        else $this->type=":".$this->type;
		$this->path = $this->directory;
        $this->width = $this->height = '';
        if($audio != NULL) {
          if(!empty($this->up_file)&&(C_HACK3)) {
            $this->p = getimagesize($this->up_file);
            $this->width = $this->p[0];  
            $this->height = $this->p[1];
            }  
        }
		$this->errors = '';
		
	  if($this->move_file($audio)) return true;
    else return false;
	}
	
	function getName() {
	  return $this->name;
	}
	
	function getMaxSize() {
	  return $this->max_size;
	}
	
	function getSize() {
	  return $this->size;
	}

	function getType() {
	  return $this->type;
	}
	
	function move_file($audio=NULL) {
	  if($this->check_size() && $this->check_extension() && $this->check_uploaded() && $this->check_wh()) {
		$newpath = ($audio == NULL) ? $this->path : $this->path.'.'.$this->type;
		move_uploaded_file($this->up_file, $newpath);
    chmod($newpath, 0644);
		return true;
		} else {
		  return false;
		}
	}
	
	function check_size() {
    global $w; 
	  if(($this->up_file != "")&&($this->size <= $this->max_size)) {
      return true;
		} else {
		  if($this->errors == '') {
              $tm=array($this->max_size,$this->size);
			  $this->errors = $this->errors.template($w[220],$tm);
			}
		  return false;
		}
	}
	
	function check_wh() { // Check width and height
    global $w; 
	  if(($this->up_file != "")&&($this->width <= $this->max_width)&&($this->height <= $this->max_height)) {
      return true;
		} else {
		  if($this->errors == '') {
              $tm=array($this->max_width,$this->max_height);
			  $this->errors = $this->errors.template($w[221],$tm);
			}
		  return false;
		}
	}
	
	function check_uploaded() {
    global $w; 
	  if(is_uploaded_file($this->up_file)) {
			return true;
		} else {
		  if($this->errors == '') {
			  $this->errors = $this->errors.$w[1].'4';
			}
		  return false;
		}
	}
	
	function check_extension() {
    global $w; 
  		if (substr($this->type,0,1) == ":") {
  		    if($this->errors == '') {
  			   $this->errors = $this->errors.$w[222].$this->type;
  			}
  		  return false;
  		} else {
        return true;
		}
	}
	
	function getErrors() {
	  return $this->errors;
	}
	
}
?>