PrimitiveFile.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: PrimitiveFile.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00018     class PrimitiveFile extends RangedPrimitive
00019     {
00020         private $originalName       = null;
00021         private $mimeType           = null;
00022 
00023         private $allowedMimeTypes   = array();
00024 
00025         public function getOriginalName()
00026         {
00027             return $this->originalName;
00028         }
00029 
00030         public function getMimeType()
00031         {
00032             return $this->mimeType;
00033         }
00034         
00038         public function clean()
00039         {
00040             $this->originalName = null;
00041             $this->mimeType = null;
00042             
00043             return parent::clean();
00044         }
00045         
00050         public function setAllowedMimeTypes($mimes)
00051         {
00052             Assert::isArray($mimes);
00053             
00054             $this->allowedMimeTypes = $mimes;
00055             
00056             return $this;
00057         }
00058 
00063         public function addAllowedMimeType($mime)
00064         {
00065             Assert::isString($mime);
00066             
00067             $this->allowedMimeTypes[] = $mime;
00068 
00069             return $this;
00070         }
00071         
00072         public function getAllowedMimeTypes()
00073         {
00074             return $this->allowedMimeType;
00075         }
00076 
00077         public function isAllowedMimeType()
00078         {
00079             if (count($this->allowedMimeTypes) > 0) {
00080                 return in_array($this->mimeType, $this->allowedMimeTypes);
00081             } else
00082                 return true;
00083         }
00084 
00085         public function copyTo($path, $name)
00086         {
00087             return $this->copyToPath($path.$name);
00088         }
00089         
00090         public function copyToPath($path)
00091         {
00092             if (is_readable($this->value) && is_writable(dirname($path))) {
00093                 return move_uploaded_file($this->value, $path);
00094             } else
00095                 throw new WrongArgumentException(
00096                     "can not move '{$this->value}' to '{$path}'"
00097                 );
00098         }
00099         
00100         public function import($scope)
00101         {
00102             if (
00103                 !BasePrimitive::import($scope)
00104                 || !is_array($scope[$this->name])
00105                 || (
00106                     isset($scope[$this->name], $scope[$this->name]['error'])
00107                     && $scope[$this->name]['error'] == UPLOAD_ERR_NO_FILE
00108                 )
00109             )
00110                 return null;
00111             
00112             if (isset($scope[$this->name]['tmp_name']))
00113                 $file = $scope[$this->name]['tmp_name'];
00114             else
00115                 return false;
00116                 
00117             if (is_readable($file) && is_uploaded_file($file))
00118                 $size = filesize($file);
00119             else
00120                 return false;
00121             
00122             $this->mimeType = $scope[$this->name]['type'];
00123             
00124             if (!$this->isAllowedMimeType())
00125                 return false;
00126             
00127             if (
00128                 isset($scope[$this->name])
00129                 && !($this->max && ($size > $this->max))
00130                 && !($this->min && ($size < $this->min))
00131             ) {
00132                 $this->value = $scope[$this->name]['tmp_name'];
00133                 $this->originalName = $scope[$this->name]['name'];
00134                 
00135                 return true;
00136             }
00137             
00138             return false;
00139         }
00140     }
00141 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4