<?php
require_once(LIBDIR."/mvcfronk/mfUpload/mfUpload_TmpFile.php");

class mfUpload {
	private $upload;
	private $size;
	private $savepath;
  private $original_filename;
	private $filename;
  private $dirmode;
  private $filemode;

  
	public function __construct($uplName,$randomFileName=true) {
    $this->log = mfLoghandler::singleton();
    
    $this->dirmode = 0777;
    $this->filemode = 0666;
    
    if(defined("MFUPLOAD_DIRMODE")) {
      $this->dirmode = MFUPLOAD_DIRMODE;
    }
    
    if(defined("MFUPLOAD_FILEMODE")) {
      $this->filemode = MFUPLOAD_FILEMODE;
    }
    
		if(!$this->upload=new mfUpload_TmpFile($uplName)) {
			throw new Exception($this->upload->errormessage);
		}
    
    $this->original_filename = $this->upload->filename;
		
		if(!$this->filename=$this->upload->getFilename()){
			throw new Exception($this->upload->errormessage);
		}
    
    if($randomFileName) {
      $this->filename=$this->getRandomFilename().'-'.$this->filename;
    }
		
		$this->size=$this->upload->getFileSize();
		if($this->size > MFUPLOAD_FILE_MAX_SIZE) {
			throw new Exception('File is too big. Maximum allowed filesize is '.(MFUPLOAD_FILE_MAX_SIZE).' MB');
		}
	}

  public function getSavepath() {
    return $this->savepath;
  }
  
  public function setSavepath($path) {
    $this->savepath = $path;
    return true;
  }
  
  public function getOriginalFilename() {
    return $this->original_filename;
  }
  
  public function getFilename() {
    return $this->filename;
  }
  
  public function setFilename($name) {
    $this->filename = $name;
  }
  
  public function getSize() {
    return $this->size;
  }
  
  public function getDirmode() {
    return $this->dirmode;
  }
  
  public function setDirmode($mode) {
    $this->dirmode=$mode;
  }
  
  public function getFilemode() {
    return $this->filemode;
  }
  
  public function setFilemode($mode) {
    $this->filemode = $mode;
  }
  
  public function save() {
    if(!$this->savepath) {
      if(!$this->buildSavepath()) {
        throw new Exception ("Not enough data to build savepath!",605);
      }
    }
    
    if(!$this->upload->move_upload($this->savepath."/".$this->filename)) {
			throw new Exception ("Unable to move temp file: ".$this->upload->errormessage,605);
		}
    
    chmod($this->savepath."/".$this->filename,$this->filemode);
    
    return true;
  }
  
  
	private function buildSavepath() {
		if(!$this->filename) {
			return false;
		}

		$savepath=MFUPLOAD_FILE_SAVE_PATH;
		if(!MFUPLOAD_FILE_SAVE_PATH) {
			$savepath="upload/";
		}
    
    $this->log->debug("dirmode: ".decoct($this->dirmode));
    if(!file_exists($savepath)) {
      if(!mkdir($savepath,$this->dirmode,true)) {
        throw new Exception("Error creating directory $savepath.");
      }
      chmod($savepath,$this->dirmode);
    }
    if(!is_dir($savepath)) {
      throw new Exception("Upload save path not a directory!");
    }

		$this->savepath=$savepath;
    return true;
	}
	
	public function getRandomFilename() {
		$length = 20;
    $characters = '0123456789abcdef';
    $string = '';    

    for ($p = 0; $p < $length; $p++) {
      $string .= $characters[mt_rand(0, strlen($characters))];
	  }

	  $rand = uniqid(md5(rand()), true);
	  list($rand) = explode('.',$rand);
    
	  $string=$rand.$string;
	  return $string;
	}
}
