openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

Upload.class.php (3064B)


      1 <?php
      2 // OpenRat Content Management System
      3 // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
      4 //
      5 // This program is free software; you can redistribute it and/or
      6 // modify it under the terms of the GNU General Public License
      7 // as published by the Free Software Foundation; either version 2
      8 // of the License, or (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU General Public License
     16 // along with this program; if not, write to the Free Software
     17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     18 
     19 namespace util;
     20 use InvalidArgumentException;
     21 use Request;
     22 
     23 /**
     24  * Methoden fuer den Upload einer Datei
     25  *
     26  * @author $Author$
     27  * @version $Revision$
     28  * @package openrat.services
     29  */
     30 class Upload
     31 {
     32 	public $filename;
     33 	public $extension;
     34 	public $value;
     35 	public $size;
     36 	public $mimeType;
     37 
     38 	public $parameterName;
     39 
     40 	public static $DEFAULT_PARAM_NAME = 'file';
     41 
     42 
     43 	/**
     44 	 * Bearbeitet den Upload einer Datei.<br>
     45 	 *
     46 	 * @return Upload
     47 	 */
     48 	function __construct($name = 'file') // Konstruktor
     49 	{
     50 		$this->parameterName = $name;
     51 	}
     52 
     53 	/**
     54 	 * Provision of an uploaded file.
     55 	 */
     56 	public function processUpload()
     57 	{
     58 		$name = $this->parameterName;
     59 
     60 		if (!$this->isAvailable())
     61 			throw new InvalidArgumentException('No file received under the key "' . $name . '"');
     62 
     63 		$uFile = $_FILES[$name];
     64 
     65 		if (!isset($uFile['tmp_name']))
     66 			throw new InvalidArgumentException('No temporary filename found for uploaded file key "' . $name . '"');
     67 
     68 		if (!is_file($uFile['tmp_name']))
     69 			throw new InvalidArgumentException('Not a file: ' . $uFile['tmp_name']);
     70 
     71 		switch ($uFile['error']) {
     72 			case UPLOAD_ERR_OK:
     73 				break;
     74 
     75 			case UPLOAD_ERR_INI_SIZE:
     76 				throw new InvalidArgumentException('Uploaded file is bigger than allowed in server configuration');
     77 
     78 			case UPLOAD_ERR_FORM_SIZE:
     79 				throw new InvalidArgumentException('Uploaded file is bigger than allowed in form');
     80 
     81 			default:
     82 				throw new InvalidArgumentException('Error code while uploading file: ' . $uFile['error']);
     83 		}
     84 
     85 		$this->mimeType = $uFile['type'];
     86 
     87 		$this->size = filesize($uFile['tmp_name']);
     88 
     89 		$fh = fopen($uFile['tmp_name'], 'r');
     90 
     91 		$this->value = fread($fh, $this->size);
     92 		fclose($fh);
     93 
     94 		$this->filename = $uFile['name'];
     95 		$this->extension = '';
     96 
     97 		$p = strrpos($this->filename, '.'); // Letzten Punkt suchen
     98 
     99 		if ($p !== false) // Wennn letzten Punkt gefunden, dann dort aufteilen
    100 		{
    101 			$this->extension = substr($this->filename, $p + 1);
    102 			$this->filename = substr($this->filename, 0, $p);
    103 		}
    104 	}
    105 
    106 	/**
    107 	 * Is this upload available?
    108 	 * @param $name Request variable name
    109 	 * @return bool <code>true</code> if upload is available
    110 	 */
    111 	public function isAvailable()
    112 	{
    113 		$name = $this->parameterName;
    114 
    115 		return isset($_FILES[$name]) && is_array($_FILES[$name]);
    116 	}
    117 }