<?php
/*******************************************************************************
 *                    _ _ _ _
 *                   / _ _  /
 *            _ _   / /__/ /_ _   ____ _ _   _ _ _    __
 *           / _ ` /   _ _// _ ` / __// _ ` / _ \\ \ / /
 *          / _ _// /\ \  / _ _/ \_ \/ _ _// / \/ \ ` /
 *          \_ , /_/  \_\ \_ ,  /__ /\_ , /_/      \_/
 *
 *******************************************************************************
 *    Application:  eReserv
 *    Version:      6
 *    Author:       David Di Certo
 *    Website:      http://www.cogites.com
 *    Email:        contact@cogites.com
 *    Copyright:    2013 - David Di Certo
 *******************************************************************************
 *    == FR ==      La revente et la redistribution de l'application sont
 *                  strictement interdites.
 *
 *                  Vous êtes tenu d'avoir pris connaissance des
 *                  Conditions Générales de Vente disponibles sur Cogites.com
 *                  (y compris pour la version Free).
 *
 *    == EN ==      Resale and redistribution of the application are
 *                  Strictly prohibited.
 *
 *                  You are required to have read the Terms and Conditions
 *                  available on Cogites.com (including the Free version).
 *
 *******************************************************************************
*/

require_once("lib/upload/upload.inc.php");
include_once("config/Tagger.php");
include_once("config/Logger.php");

class MYUPLOAD extends upload
{
	var $id;
	var $deniedExtension = array();

	function __construct($id) {
		$this->id = $id;
		// Set Max Size 1Mb
		$this->set_max_size(UPLOAD_FILE_SIZE);
		// Do not change
		// Set Temp Name for upload, $_FILES['file']['tmp_name'] is automaticly get the temp name
		$this->set_tmp_name($_FILES[$this->id]['tmp_name']);
		// Do not change
		// Set file size, $_FILES['file']['size'] is automaticly get the size
		$this->set_file_size($_FILES[$this->id]['size']);
		// Do not change
		// Set File Type, $_FILES['file']['type'] is automaticly get the type
		$this->set_file_type($_FILES[$this->id]['type']);
		// Set File Name, $_FILES['file']['name'] is automaticly get the file name.. you can change
    }
	function SetDeniedExtension($ext){
	Logger::debug('		[upload] - ->Denied Extension: ' . print_r($ext,true));
		if (is_array($ext)){
			foreach ($ext as $val){
				$this->deniedExtension[] = array($val);
			}
		}else{
			$this->deniedExtension[] = array($ext);
		}
	}

	function copy_file() {
		Logger::debug('		[upload] - Starting Copy');
		Logger::debug('		[upload] - $_FILES: ' . print_r($_FILES,true));
		if (!empty($_FILES[$this->id]['name'])) {
			Logger::debug('		[upload] - name is: ' . $_FILES[$this->id]['name']);
			if ($_FILES[$this->id]['error'] === UPLOAD_ERR_OK) {
				Logger::debug('		[upload] OK: ' . $_FILES[$this->id]['error']);
				$extension_ok = true;
				$filename = basename($_FILES[$this->id]['name']);
				$fileext = substr($filename, strrpos($filename, '.') + 1);
				// TODO : BUG : $ext never used and it is an Array
				// BMP is hardcoded. To be adapted to match with $ext[0]
				foreach ($this->deniedExtension as $ext){
				Logger::debug('		[upload] - Check for extension: ' . print_r($ext, true));
					if ($fileext == "bmp"){
						Logger::debug('		[upload] - Extension not supported ! ' . print_r($ext, true));
						$extension_ok = false;
						break;
					}
				}
				if ($extension_ok){
					if (file_exists($this->user_full_name)){
						Logger::debug('		[upload] - File exists. It will be deleted: ' . $this->user_full_name);
						unlink($this->user_full_name);
					}
					Logger::debug('		[upload] - Ready to copy');
					parent::start_copy();
					Logger::debug('		[upload] - Copy done');
					// Control File is uploaded or not
					// If there is error write the error message
					if($this->is_ok()) {
						Logger::debug('		[upload] It is is_ok');
						$msgType = "success";
						$msg = "Status successfuly updated";
						return array($msgType, $msg);
					}else{
						Logger::err('		[upload] It is not_ok. Error: ' . $this->error());
						$msgType = "error";
						$msg = "Failed to update Status" . $this->error();
						return array($msgType, $msg);
					}
				}else{
					$msgType = "error";
					$msg = "File extension not supported";
					return array($msgType, $msg);
				}
			}else{
				Logger::debug('		[upload] upload ERROR.' . $_FILES[$this->id]['error']);
				$msgType = "error";
				$msg = "Failed to upload Image: ". $_FILES[$this->id]['error'];
				switch ($_FILES[$this->id]['error']) {
					case UPLOAD_ERR_INI_SIZE:
						$msg .= "The uploaded file exceeds the upload_max_filesize directive in php.ini";
						break;
					case UPLOAD_ERR_FORM_SIZE:
						$msg .= "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
						break;
					case UPLOAD_ERR_PARTIAL:
						$msg .= "The uploaded file was only partially uploaded";
						break;
					case UPLOAD_ERR_NO_FILE:
						$msg .= "No file was uploaded";
						break;
					case UPLOAD_ERR_NO_TMP_DIR:
						$msg .= "Missing a temporary folder";
						break;
					case UPLOAD_ERR_CANT_WRITE:
						$msg .= "Failed to write file to disk";
						break;
					case UPLOAD_ERR_EXTENSION:
						$msg .= "File upload stopped by extension";
						break;
					default:
						$msg .= "Unknown upload error";
						break;
				}
				Logger::debug('		[upload] upload ERROR.' . $msg);
				return array($msgType, $msg);
			}
		}else{
			Logger::debug('		[upload] Name is empty.');
			$msgType = "error";
			$msg = "Failed to upload Image: ";
		}
		return array($msgType, $msg);
	}

}

?>