2008-04-06 10:20:13 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Manages uploads via HTML forms processed by PHP,
|
|
|
|
* uploads to Silverstripe's default upload directory,
|
|
|
|
* and either creates a new or uses an existing File-object
|
|
|
|
* for syncing with the database.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:04:27 +02:00
|
|
|
* <b>Validation</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:04:27 +02:00
|
|
|
* By default, a user can upload files without extension limitations,
|
|
|
|
* which can be a security risk if the webserver is not properly secured.
|
|
|
|
* Use {@link setAllowedExtensions()} to limit this list,
|
|
|
|
* and ensure the "assets/" directory does not execute scripts
|
|
|
|
* (see http://doc.silverstripe.org/secure-development#filesystem).
|
|
|
|
* {@link File::$allowed_extensions} provides a good start for a list of "safe" extensions.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-04-06 10:20:13 +02:00
|
|
|
* @subpackage filesystem
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-04-06 10:20:13 +02:00
|
|
|
* @todo Allow for non-database uploads
|
|
|
|
*/
|
|
|
|
class Upload extends Controller {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
private static $allowed_actions = array(
|
2011-02-13 23:14:51 +01:00
|
|
|
'index',
|
|
|
|
'load'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* A File object
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-04-06 10:20:13 +02:00
|
|
|
* @var File
|
|
|
|
*/
|
|
|
|
protected $file;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Validator for this upload field
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-05-25 05:42:52 +02:00
|
|
|
* @var Upload_Validator
|
|
|
|
*/
|
|
|
|
protected $validator;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* Information about the temporary file produced
|
|
|
|
* by the PHP-runtime.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $tmpFile;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-02-05 19:10:15 +01:00
|
|
|
/**
|
|
|
|
* Replace an existing file rather than renaming the new one.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @var boolean
|
2013-02-05 19:10:15 +01:00
|
|
|
*/
|
|
|
|
protected $replaceFile;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* Processing errors that can be evaluated,
|
|
|
|
* e.g. by Form-validation.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $errors = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* A foldername relative to /assets,
|
|
|
|
* where all uploaded files are stored by default.
|
|
|
|
*
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2008-04-06 10:20:13 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
2014-08-15 08:53:05 +02:00
|
|
|
private static $uploads_folder = "Uploads";
|
|
|
|
|
2015-05-29 18:12:02 +02:00
|
|
|
/**
|
|
|
|
* A prefix for the version number added to an uploaded file
|
|
|
|
* when a file with the same name already exists.
|
|
|
|
* Example using no prefix: IMG001.jpg becomes IMG2.jpg
|
|
|
|
* Example using '-v' prefix: IMG001.jpg becomes IMG001-v2.jpg
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $version_prefix = ''; // a default value will be introduced in SS4.0
|
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
2014-07-28 06:28:38 +02:00
|
|
|
$this->validator = Injector::inst()->create('Upload_Validator');
|
2013-05-27 05:22:59 +02:00
|
|
|
$this->replaceFile = self::config()->replaceFile;
|
2010-05-25 05:42:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 06:03:46 +02:00
|
|
|
/**
|
|
|
|
* Get current validator
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return Upload_Validator $validator
|
2010-05-25 06:03:46 +02:00
|
|
|
*/
|
|
|
|
public function getValidator() {
|
|
|
|
return $this->validator;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
/**
|
|
|
|
* Set a different instance than {@link Upload_Validator}
|
|
|
|
* for this upload session.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-05-25 05:42:52 +02:00
|
|
|
* @param object $validator
|
|
|
|
*/
|
|
|
|
public function setValidator($validator) {
|
|
|
|
$this->validator = $validator;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* Save an file passed from a form post into this object.
|
2011-08-26 17:57:05 +02:00
|
|
|
* File names are filtered through {@link FileNameFilter}, see class documentation
|
|
|
|
* on how to influence this behaviour.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-04-06 10:20:13 +02:00
|
|
|
* @param $tmpFile array Indexed array that PHP generated for every file it uploads.
|
|
|
|
* @param $folderPath string Folder path relative to /assets
|
|
|
|
* @return Boolean|string Either success or error-message.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function load($tmpFile, $folderPath = false) {
|
2008-04-06 10:20:13 +02:00
|
|
|
$this->clearErrors();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
if(!$folderPath) $folderPath = $this->config()->uploads_folder;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
if(!is_array($tmpFile)) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Upload::load() Not passed an array. Most likely, the form hasn't got the right enctype",
|
|
|
|
E_USER_ERROR);
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
if(!$tmpFile['size']) {
|
2016-03-29 02:38:24 +02:00
|
|
|
$this->errors[] = _t('File.NOFILESIZE', 'File size is zero bytes.');
|
2008-04-06 10:20:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
$valid = $this->validate($tmpFile);
|
|
|
|
if(!$valid) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
// @TODO This puts a HUGE limitation on files especially when lots
|
|
|
|
// have been uploaded.
|
|
|
|
$base = Director::baseFolder();
|
2011-12-06 01:56:24 +01:00
|
|
|
$parentFolder = Folder::find_or_make($folderPath);
|
2008-04-06 10:20:13 +02:00
|
|
|
|
|
|
|
// Generate default filename
|
2012-04-04 16:59:30 +02:00
|
|
|
$nameFilter = FileNameFilter::create();
|
2011-08-26 17:57:05 +02:00
|
|
|
$file = $nameFilter->filter($tmpFile['name']);
|
|
|
|
$fileName = basename($file);
|
2008-04-06 10:20:13 +02:00
|
|
|
|
2014-03-04 13:43:08 +01:00
|
|
|
$relativeFolderPath = $parentFolder
|
|
|
|
? $parentFolder->getRelativePath()
|
|
|
|
: ASSETS_DIR . '/';
|
|
|
|
$relativeFilePath = $relativeFolderPath . $fileName;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-02-05 19:10:15 +01:00
|
|
|
// Create a new file record (or try to retrieve an existing one)
|
|
|
|
if(!$this->file) {
|
|
|
|
$fileClass = File::get_class_for_file_extension(pathinfo($tmpFile['name'], PATHINFO_EXTENSION));
|
2014-03-04 13:10:48 +01:00
|
|
|
$this->file = new $fileClass();
|
|
|
|
}
|
|
|
|
if(!$this->file->ID && $this->replaceFile) {
|
|
|
|
$fileClass = $this->file->class;
|
|
|
|
$file = File::get()
|
|
|
|
->filter(array(
|
|
|
|
'ClassName' => $fileClass,
|
|
|
|
'Name' => $fileName,
|
|
|
|
'ParentID' => $parentFolder ? $parentFolder->ID : 0
|
|
|
|
))->First();
|
|
|
|
if($file) {
|
|
|
|
$this->file = $file;
|
2013-02-05 19:10:15 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-04 13:43:08 +01:00
|
|
|
// if filename already exists, version the filename (e.g. test.gif to test2.gif, test2.gif to test3.gif)
|
2013-02-05 19:10:15 +01:00
|
|
|
if(!$this->replaceFile) {
|
2014-03-04 13:43:08 +01:00
|
|
|
$fileSuffixArray = explode('.', $fileName);
|
|
|
|
$fileTitle = array_shift($fileSuffixArray);
|
|
|
|
$fileSuffix = !empty($fileSuffixArray)
|
|
|
|
? '.' . implode('.', $fileSuffixArray)
|
|
|
|
: null;
|
|
|
|
|
|
|
|
// make sure files retain valid extensions
|
|
|
|
$oldFilePath = $relativeFilePath;
|
|
|
|
$relativeFilePath = $relativeFolderPath . $fileTitle . $fileSuffix;
|
|
|
|
if($oldFilePath !== $relativeFilePath) {
|
|
|
|
user_error("Couldn't fix $relativeFilePath", E_USER_ERROR);
|
|
|
|
}
|
2013-03-08 04:02:38 +01:00
|
|
|
while(file_exists("$base/$relativeFilePath")) {
|
|
|
|
$i = isset($i) ? ($i+1) : 2;
|
|
|
|
$oldFilePath = $relativeFilePath;
|
2014-10-21 11:28:18 +02:00
|
|
|
|
2015-05-29 18:12:02 +02:00
|
|
|
$prefix = $this->config()->version_prefix;
|
|
|
|
$pattern = '/' . preg_quote($prefix) . '([0-9]+$)/';
|
|
|
|
if(preg_match($pattern, $fileTitle, $matches)) {
|
|
|
|
$fileTitle = preg_replace($pattern, $prefix . ($matches[1] + 1), $fileTitle);
|
2013-03-08 04:02:38 +01:00
|
|
|
} else {
|
2015-05-29 18:12:02 +02:00
|
|
|
$fileTitle .= $prefix . $i;
|
2013-03-08 04:02:38 +01:00
|
|
|
}
|
2014-03-04 13:43:08 +01:00
|
|
|
$relativeFilePath = $relativeFolderPath . $fileTitle . $fileSuffix;
|
2014-10-21 11:28:18 +02:00
|
|
|
|
2013-03-08 04:02:38 +01:00
|
|
|
if($oldFilePath == $relativeFilePath && $i > 2) {
|
|
|
|
user_error("Couldn't fix $relativeFilePath with $i tries", E_USER_ERROR);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2013-03-08 04:02:38 +01:00
|
|
|
} else {
|
|
|
|
//reset the ownerID to the current member when replacing files
|
|
|
|
$this->file->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(file_exists($tmpFile['tmp_name']) && copy($tmpFile['tmp_name'], "$base/$relativeFilePath")) {
|
2012-11-08 09:28:05 +01:00
|
|
|
$this->file->ParentID = $parentFolder ? $parentFolder->ID : 0;
|
2008-04-06 10:20:13 +02:00
|
|
|
// This is to prevent it from trying to rename the file
|
|
|
|
$this->file->Name = basename($relativeFilePath);
|
|
|
|
$this->file->write();
|
2014-04-02 02:30:12 +02:00
|
|
|
$this->file->onAfterUpload();
|
2016-02-22 13:11:26 +01:00
|
|
|
$this->extend('onAfterLoad', $this->file, $tmpFile); //to allow extensions to e.g. create a version after an upload
|
2008-04-06 10:20:13 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
2016-03-29 02:38:24 +02:00
|
|
|
$this->errors[] = _t('File.NOFILESIZE', 'File size is zero bytes.');
|
2008-04-06 10:20:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* Load temporary PHP-upload into File-object.
|
|
|
|
*
|
|
|
|
* @param array $tmpFile
|
|
|
|
* @param File $file
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
2008-11-05 04:56:22 +01:00
|
|
|
public function loadIntoFile($tmpFile, $file, $folderPath = false) {
|
2008-04-06 10:20:13 +02:00
|
|
|
$this->file = $file;
|
2008-11-05 04:56:22 +01:00
|
|
|
return $this->load($tmpFile, $folderPath);
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-02-05 19:10:15 +01:00
|
|
|
/**
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function setReplaceFile($bool) {
|
|
|
|
$this->replaceFile = $bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function getReplaceFile() {
|
|
|
|
return $this->replaceFile;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* Container for all validation on the file
|
|
|
|
* (e.g. size and extension restrictions).
|
|
|
|
* Is NOT connected to the {Validator} classes,
|
|
|
|
* please have a look at {FileField->validate()}
|
|
|
|
* for an example implementation of external validation.
|
|
|
|
*
|
|
|
|
* @param array $tmpFile
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function validate($tmpFile) {
|
2010-05-25 05:42:52 +02:00
|
|
|
$validator = $this->validator;
|
|
|
|
$validator->setTmpFile($tmpFile);
|
|
|
|
$isValid = $validator->validate();
|
|
|
|
if($validator->getErrors()) {
|
|
|
|
$this->errors = array_merge($this->errors, $validator->getErrors());
|
2009-03-17 23:26:33 +01:00
|
|
|
}
|
2010-05-25 05:42:52 +02:00
|
|
|
return $isValid;
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* Get file-object, either generated from {load()},
|
|
|
|
* or manually set.
|
|
|
|
*
|
|
|
|
* @return File
|
|
|
|
*/
|
|
|
|
public function getFile() {
|
|
|
|
return $this->file;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* Set a file-object (similiar to {loadIntoFile()})
|
|
|
|
*
|
|
|
|
* @param File $file
|
|
|
|
*/
|
|
|
|
public function setFile($file) {
|
|
|
|
$this->file = $file;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* Clear out all errors (mostly set by {loadUploaded()})
|
2014-09-20 16:09:48 +02:00
|
|
|
* including the validator's errors
|
2008-04-06 10:20:13 +02:00
|
|
|
*/
|
|
|
|
public function clearErrors() {
|
|
|
|
$this->errors = array();
|
2014-09-20 16:09:48 +02:00
|
|
|
$this->validator->clearErrors();
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* Determines wether previous operations caused an error.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-04-06 10:20:13 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isError() {
|
2014-08-15 08:53:05 +02:00
|
|
|
return (count($this->errors));
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* Return all errors that occurred while processing so far
|
|
|
|
* (mostly set by {loadUploaded()})
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getErrors() {
|
2013-05-27 05:22:59 +02:00
|
|
|
return $this->errors;
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2010-10-13 05:53:12 +02:00
|
|
|
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-10-13 05:53:12 +02:00
|
|
|
* @subpackage filesystem
|
|
|
|
*/
|
2010-05-25 05:42:52 +02:00
|
|
|
class Upload_Validator {
|
|
|
|
|
2015-03-31 13:27:32 +02:00
|
|
|
/**
|
|
|
|
* Contains a list of the max file sizes shared by
|
|
|
|
* all upload fields. This is then duplicated into the
|
|
|
|
* "allowedMaxFileSize" instance property on construct.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $default_max_file_size = array();
|
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
/**
|
|
|
|
* Information about the temporary file produced
|
|
|
|
* by the PHP-runtime.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $tmpFile;
|
|
|
|
|
|
|
|
protected $errors = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restrict filesize for either all filetypes
|
|
|
|
* or a specific extension, with extension-name
|
|
|
|
* as array-key and the size-restriction in bytes as array-value.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @var array
|
2010-05-25 05:42:52 +02:00
|
|
|
*/
|
|
|
|
public $allowedMaxFileSize = array();
|
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* @var array Collection of extensions.
|
2010-05-25 05:42:52 +02:00
|
|
|
* Extension-names are treated case-insensitive.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-05-25 05:42:52 +02:00
|
|
|
* Example:
|
|
|
|
* <code>
|
|
|
|
* array("jpg","GIF")
|
|
|
|
* </code>
|
|
|
|
*/
|
|
|
|
public $allowedExtensions = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all errors that occurred while validating
|
|
|
|
* the temporary file.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getErrors() {
|
2014-08-15 08:53:05 +02:00
|
|
|
return $this->errors;
|
2010-05-25 05:42:52 +02:00
|
|
|
}
|
|
|
|
|
2014-09-20 16:09:48 +02:00
|
|
|
/**
|
|
|
|
* Clear out all errors
|
|
|
|
*/
|
|
|
|
public function clearErrors() {
|
|
|
|
$this->errors = array();
|
|
|
|
}
|
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
/**
|
|
|
|
* Set information about temporary file produced by PHP.
|
|
|
|
* @param array $tmpFile
|
|
|
|
*/
|
|
|
|
public function setTmpFile($tmpFile) {
|
|
|
|
$this->tmpFile = $tmpFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get maximum file size for all or specified file extension.
|
|
|
|
*
|
|
|
|
* @param string $ext
|
|
|
|
* @return int Filesize in bytes
|
|
|
|
*/
|
|
|
|
public function getAllowedMaxFileSize($ext = null) {
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-03-31 13:27:32 +02:00
|
|
|
// Check if there is any defined instance max file sizes
|
|
|
|
if (empty($this->allowedMaxFileSize)) {
|
|
|
|
// Set default max file sizes if there isn't
|
|
|
|
$fileSize = Config::inst()->get('Upload_Validator', 'default_max_file_size');
|
2017-06-06 15:28:03 +02:00
|
|
|
if (!empty($fileSize)) {
|
2015-03-31 13:27:32 +02:00
|
|
|
$this->setAllowedMaxFileSize($fileSize);
|
|
|
|
} else {
|
|
|
|
// When no default is present, use maximum set by PHP
|
|
|
|
$maxUpload = File::ini2bytes(ini_get('upload_max_filesize'));
|
|
|
|
$maxPost = File::ini2bytes(ini_get('post_max_size'));
|
|
|
|
$this->setAllowedMaxFileSize(min($maxUpload, $maxPost));
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
$ext = strtolower($ext);
|
2015-03-31 13:27:32 +02:00
|
|
|
if ($ext) {
|
|
|
|
if (isset($this->allowedMaxFileSize[$ext])) {
|
|
|
|
return $this->allowedMaxFileSize[$ext];
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-03-31 13:27:32 +02:00
|
|
|
$category = File::get_app_category($ext);
|
|
|
|
if ($category && isset($this->allowedMaxFileSize['[' . $category . ']'])) {
|
|
|
|
return $this->allowedMaxFileSize['[' . $category . ']'];
|
|
|
|
}
|
2010-05-25 05:42:52 +02:00
|
|
|
}
|
2016-03-24 04:00:08 +01:00
|
|
|
|
|
|
|
return (isset($this->allowedMaxFileSize['*'])) ? $this->allowedMaxFileSize['*'] : false;
|
2010-05-25 05:42:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
/**
|
2015-03-31 13:27:32 +02:00
|
|
|
* Set filesize maximums (in bytes or INI format).
|
2010-05-25 05:42:52 +02:00
|
|
|
* Automatically converts extensions to lowercase
|
|
|
|
* for easier matching.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Example:
|
2010-05-25 05:42:52 +02:00
|
|
|
* <code>
|
2015-03-31 13:27:32 +02:00
|
|
|
* array('*' => 200, 'jpg' => 1000, '[doc]' => '5m')
|
2010-05-25 05:42:52 +02:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param array|int $rules
|
|
|
|
*/
|
|
|
|
public function setAllowedMaxFileSize($rules) {
|
|
|
|
if(is_array($rules) && count($rules)) {
|
|
|
|
// make sure all extensions are lowercase
|
|
|
|
$rules = array_change_key_case($rules, CASE_LOWER);
|
2015-03-31 13:27:32 +02:00
|
|
|
$finalRules = array();
|
|
|
|
$tmpSize = 0;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-03-31 13:27:32 +02:00
|
|
|
foreach ($rules as $rule => $value) {
|
|
|
|
if (is_numeric($value)) {
|
|
|
|
$tmpSize = $value;
|
|
|
|
} else {
|
|
|
|
$tmpSize = File::ini2bytes($value);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-03-31 13:27:32 +02:00
|
|
|
$finalRules[$rule] = (int)$tmpSize;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-03-31 13:27:32 +02:00
|
|
|
$this->allowedMaxFileSize = $finalRules;
|
|
|
|
} elseif(is_string($rules)) {
|
|
|
|
$this->allowedMaxFileSize['*'] = File::ini2bytes($rules);
|
2010-05-25 05:42:52 +02:00
|
|
|
} elseif((int) $rules > 0) {
|
|
|
|
$this->allowedMaxFileSize['*'] = (int)$rules;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAllowedExtensions() {
|
|
|
|
return $this->allowedExtensions;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
/**
|
2010-10-15 05:04:27 +02:00
|
|
|
* Limit allowed file extensions. Empty by default, allowing all extensions.
|
|
|
|
* To allow files without an extension, use an empty string.
|
|
|
|
* See {@link File::$allowed_extensions} to get a good standard set of
|
|
|
|
* extensions that are typically not harmful in a webserver context.
|
|
|
|
* See {@link setAllowedMaxFileSize()} to limit file size by extension.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:04:27 +02:00
|
|
|
* @param array $rules List of extensions
|
2010-05-25 05:42:52 +02:00
|
|
|
*/
|
|
|
|
public function setAllowedExtensions($rules) {
|
|
|
|
if(!is_array($rules)) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
// make sure all rules are lowercase
|
|
|
|
foreach($rules as &$rule) $rule = strtolower($rule);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
$this->allowedExtensions = $rules;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
/**
|
|
|
|
* Determines if the bytesize of an uploaded
|
|
|
|
* file is valid - can be defined on an
|
2010-10-15 05:04:27 +02:00
|
|
|
* extension-by-extension basis in {@link $allowedMaxFileSize}
|
2010-05-25 05:42:52 +02:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isValidSize() {
|
|
|
|
$pathInfo = pathinfo($this->tmpFile['name']);
|
|
|
|
$extension = isset($pathInfo['extension']) ? strtolower($pathInfo['extension']) : null;
|
|
|
|
$maxSize = $this->getAllowedMaxFileSize($extension);
|
|
|
|
return (!$this->tmpFile['size'] || !$maxSize || (int) $this->tmpFile['size'] < $maxSize);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
/**
|
|
|
|
* Determines if the temporary file has a valid extension
|
2010-10-04 06:42:54 +02:00
|
|
|
* An empty string in the validation map indicates files without an extension.
|
2010-05-25 05:42:52 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isValidExtension() {
|
|
|
|
$pathInfo = pathinfo($this->tmpFile['name']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:42:39 +02:00
|
|
|
// Special case for filenames without an extension
|
2010-10-04 06:42:13 +02:00
|
|
|
if(!isset($pathInfo['extension'])) {
|
2010-10-04 06:43:28 +02:00
|
|
|
return in_array('', $this->allowedExtensions, true);
|
2010-10-04 06:42:13 +02:00
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
return (!count($this->allowedExtensions)
|
|
|
|
|| in_array(strtolower($pathInfo['extension']), $this->allowedExtensions));
|
2010-10-04 06:42:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
/**
|
|
|
|
* Run through the rules for this validator checking against
|
|
|
|
* the temporary file set by {@link setTmpFile()} to see if
|
|
|
|
* the file is deemed valid or not.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-05-25 05:42:52 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function validate() {
|
|
|
|
// we don't validate for empty upload fields yet
|
|
|
|
if(!isset($this->tmpFile['name']) || empty($this->tmpFile['name'])) return true;
|
|
|
|
|
2010-11-30 06:13:09 +01:00
|
|
|
$isRunningTests = (class_exists('SapphireTest', false) && SapphireTest::is_running_test());
|
|
|
|
if(isset($this->tmpFile['tmp_name']) && !is_uploaded_file($this->tmpFile['tmp_name']) && !$isRunningTests) {
|
2010-05-25 05:42:52 +02:00
|
|
|
$this->errors[] = _t('File.NOVALIDUPLOAD', 'File is not a valid upload');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pathInfo = pathinfo($this->tmpFile['name']);
|
|
|
|
// filesize validation
|
|
|
|
if(!$this->isValidSize()) {
|
2010-10-04 06:44:03 +02:00
|
|
|
$ext = (isset($pathInfo['extension'])) ? $pathInfo['extension'] : '';
|
|
|
|
$arg = File::format_size($this->getAllowedMaxFileSize($ext));
|
2012-05-01 21:44:54 +02:00
|
|
|
$this->errors[] = _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'File.TOOLARGE',
|
2016-03-24 03:45:58 +01:00
|
|
|
'File size is too large, maximum {size} allowed',
|
|
|
|
'Argument 1: File size (e.g. 1MB)',
|
2012-05-01 21:44:54 +02:00
|
|
|
array('size' => $arg)
|
2010-05-25 05:42:52 +02:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// extension validation
|
|
|
|
if(!$this->isValidExtension()) {
|
2012-05-01 21:44:54 +02:00
|
|
|
$this->errors[] = _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'File.INVALIDEXTENSION',
|
2012-05-01 21:44:54 +02:00
|
|
|
'Extension is not allowed (valid: {extensions})',
|
|
|
|
'Argument 1: Comma-separated list of valid extensions',
|
|
|
|
array('extensions' => wordwrap(implode(', ', $this->allowedExtensions)))
|
2010-05-25 05:42:52 +02:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-27 22:14:02 +01:00
|
|
|
}
|