<?php

class ImageUpload extends CI_Upload {

	// declare disallowed types variable
	var $disallowed_types = '';

	// add in the 'disallowed_types' default during initialization
	function initialize($config = array()) {
		$defaults = array(
			'max_size'         => 0,
			'max_width'        => 0,
			'max_height'       => 0,
			'max_filename'     => 0,
			'allowed_types'    => "",
			'disallowed_types' => "",
			'file_temp'        => "",
			'file_name'        => "",
			'orig_name'        => "",
			'file_type'        => "",
			'file_size'        => "",
			'file_ext'         => "",
			'upload_path'      => "",
			'overwrite'        => FALSE,
			'encrypt_name'     => FALSE,
			'is_image'         => FALSE,
			'image_width'      => '',
			'image_height'     => '',
			'image_type'       => '',
			'image_size_str'   => '',
			'error_msg'        => array(),
			'mimes'            => array(),
			'remove_spaces'    => TRUE,
			'xss_clean'        => FALSE,
			'temp_prefix'      => "temp_file_"
		);

		foreach ($defaults as $key => $val) {
			if (isset($config[$key])) {
				$method = 'set_'.$key;

				if (method_exists($this, $method)) {
					$this->$method($config[$key]);
				}
				else {
					$this->$key = $config[$key];
				}
			}
			else {
				$this->$key = $val;
			}
		}
	}

	// set disallowed filetypes
	function set_disallowed_types($types) {
		$this->disallowed_types = explode('|', $types);
	}

	// adapted to not require allowed_types and to check for disallowed types if it exists
	function is_allowed_filetype() {
		// if allowed file type list is not defined
		if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types)) {
			// if disallowed file type list is not defined
			if (count($this->disallowed_types) == 0 OR ! is_array($this->disallowed_types))
				return TRUE;

			// check for disallowed file types and return
			// negated because is_disallowed_filetype returns opposite result as this function
			return ! $this->is_disallowed_filetype();
		}

		// proceed as usual with allowed file type list check
		return parent::is_allowed_filetype();
	}

	// check for disallowed file types
	function is_disallowed_filetype() {
		// no file types provided
		if (count($this->disallowed_types) == 0 OR ! is_array($this->disallowed_types))
			return FALSE;

		// search through disallowed for this file type
		foreach ($this->disallowed_types as $val) {
			$mime = $this->mimes_types(strtolower($val));

			if (is_array($mime)) {
				if (in_array($this->file_type, $mime, TRUE)) {
					return TRUE;
				}
			}
			else {
				if ($mime == $this->file_type) {
					return TRUE;
				}
			}
		}

		return FALSE;
	}



	function do_upload($field = 'userfile', $new_name='') {


		echo "aa";
		exit();
		if ( ! isset($_FILES[$field])) {
			$this->set_error('upload_no_file_selected');
			return FALSE;
		}

		if ( ! $this->validate_upload_path()) {
			return FALSE;
		}

		if ( ! is_uploaded_file($_FILES[$field]['tmp_name'])) {
			$error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];

			switch($error) {
				case 1: // UPLOAD_ERR_INI_SIZE
					$this->set_error('upload_file_exceeds_limit');
					break;
				case 2: // UPLOAD_ERR_FORM_SIZE
					$this->set_error('upload_file_exceeds_form_limit');
					break;
				case 3: // UPLOAD_ERR_PARTIAL
					$this->set_error('upload_file_partial');
					break;
				case 4: // UPLOAD_ERR_NO_FILE
					$this->set_error('upload_no_file_selected');
					break;
				case 6: // UPLOAD_ERR_NO_TMP_DIR
					$this->set_error('upload_no_temp_directory');
					break;
				case 7: // UPLOAD_ERR_CANT_WRITE
					$this->set_error('upload_unable_to_write_file');
					break;
				case 8: // UPLOAD_ERR_EXTENSION
					$this->set_error('upload_stopped_by_extension');
					break;
				default :
					$this->set_error('upload_no_file_selected');
					break;
			}

			return FALSE;
		}

		$this->file_temp = $_FILES[$field]['tmp_name'];
		$this->file_size = $_FILES[$field]['size'];
		$this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']);
		$this->file_type = strtolower($this->file_type);
		$this->file_ext  = $this->get_extension($_FILES[$field]['name']);

		// check if a name has been specified, if so set it
		if ($new_name != '') {
			$this->file_name = $this->_prep_filename($new_name . $this->file_ext);
		}
		else {
			$this->file_name = $this->_prep_filename($_FILES[$field]['name']);
		}

		if ($this->file_size > 0) {
			$this->file_size = round($this->file_size/1024, 2);
		}

		if ( ! $this->is_allowed_filetype()) {
			$this->set_error('upload_invalid_filetype');
			return FALSE;
		}

		if ( ! $this->is_allowed_filesize()) {
			$this->set_error('upload_invalid_filesize');
			return FALSE;
		}

		if ( ! $this->is_allowed_dimensions()) {
			$this->set_error('upload_invalid_dimensions');
			return FALSE;
		}

		$this->file_name = $this->clean_file_name($this->file_name);

		if ($this->max_filename > 0) {
			$this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
		}

		if ($this->remove_spaces == TRUE) {
			$this->file_name = preg_replace("/\s+/", "_", $this->file_name);
		}

		$this->orig_name = $this->file_name;

		if ($this->overwrite == FALSE) {
			$this->file_name = $this->set_filename($this->upload_path, $this->file_name);

			if ($this->file_name === FALSE)
			{
				return FALSE;
			}
		}

		if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name)) {
			if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
			{
				$this->set_error('upload_destination_error');
				return FALSE;
			}
		}

		if ($this->xss_clean == TRUE) {
			$this->do_xss_clean();
		}

		$this->set_image_properties($this->upload_path.$this->file_name);

		return TRUE;

	}


}
