<?php
/*
 * Copyright (c) 2013, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

class WV_Uploader {
	const NO_FILE       = 1;
	const FILE_TOO_BIG  = 2;
	const WRONG_TYPE    = 3;
	const ERROR_COPYING = 4;

	protected $formKey;
	protected $maxSize;
	protected $allowedMimeTypes;
	protected $id = null;

	public function __construct($formKey, $maxSize = 0, $allowedMimeTypes = array(), $id = null) {
		$this->setFormKey($formKey);
		$this->setMaxSize($maxSize);
		$this->forceMimeType($allowedMimeTypes);
		$this->setId($id);
	}

	public function upload($destination) {
		$this->checkUploadConditions();
		return $this->moveFile($destination);
	}

	public function setFormKey($formKey) {
		$this->formKey = $formKey;
	}

	public function setId($id) {
		if (!$id && $id !== 0) return false;
		$this->id = $id;
	}

	public function setMaxSize($maxSize) {
		$this->maxSize = abs(intval($maxSize));
	}

	public function forceMimeType($mimeTypes) {
		$this->allowedMimeTypes = sly_makeArray($mimeTypes);
	}

	public function hasFile() {
		if (!isset($_FILES[$this->formKey]['error'])) return false;

		if (isset($_FILES[$this->formKey]['error'][$this->id])) {
			return $_FILES[$this->formKey]['error'][$this->id] == UPLOAD_ERR_OK;
		}
		else {
			return $_FILES[$this->formKey]['error'] == UPLOAD_ERR_OK;
		}
	}

	public function hasValidFile() {
		try {
			$this->checkUploadConditions();
			return true;
		}
		catch (WV_Exception_Uploader $e) {
			return false;
		}
	}

	protected function checkUploadConditions() {
		if (!$this->hasFile()) {
			throw new WV_Exception_Uploader(self::NO_FILE);
		}

		if ($this->maxSize > 0 && $this->getSize() > $this->maxSize) {
			throw new WV_Exception_Uploader(self::FILE_TOO_BIG);
		}

		if (!empty($this->allowedMimeTypes) && !in_array($this->getMimeType(), $this->allowedMimeTypes)) {
			throw new WV_Exception_Uploader(self::WRONG_TYPE);
		}
	}

	protected function moveFile($destination) {
		if (!is_dir(dirname($destination)) || !move_uploaded_file($this->getTempName(), $destination)) {
			throw new WV_Exception_Uploader(self::ERROR_COPYING);
		}

		return true;
	}

	public function getTempName() { return $this->getFileArrayKey('tmp_name'); }
	public function getName()     { return $this->getFileArrayKey('name');     }
	public function getSize()     { return $this->getFileArrayKey('size');     }
	public function getMimeType() { return $this->getFileArrayKey('type');     }

	protected function getFileArrayKey($keyname) {
		if (!isset($_FILES[$this->formKey][$keyname])) return null;

		if ($this->id !== null && isset($_FILES[$this->formKey][$keyname][$this->id])) {
			return $_FILES[$this->formKey][$keyname][$this->id];
		}
		else {
			return $_FILES[$this->formKey][$keyname];
		}
	}

	public function getExtension() {
		return sly_Util_String::getFileExtension($this->getName());
	}
}
