From 67b3132de470a6be7dd9c52c26a91cff3a6be183 Mon Sep 17 00:00:00 2001 From: Mateusz Uzdowski Date: Thu, 4 Mar 2010 04:42:41 +0000 Subject: [PATCH] BUGFIX: replacing calls to deprecated Upload functions - using validator instead (related to r100057) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@100496 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- filesystem/Upload.php | 9 ++++++ forms/FileField.php | 62 +++++++++++++++++++++----------------- forms/FileIFrameField.php | 3 -- forms/SimpleImageField.php | 12 ++++++-- 4 files changed, 54 insertions(+), 32 deletions(-) diff --git a/filesystem/Upload.php b/filesystem/Upload.php index 038e6c133..b82a74a24 100644 --- a/filesystem/Upload.php +++ b/filesystem/Upload.php @@ -53,6 +53,15 @@ class Upload extends Controller { $this->validator = new Upload_Validator(); } + /** + * Get current validator + * + * @return object $validator + */ + public function getValidator() { + return $this->validator; + } + /** * Set a different instance than {@link Upload_Validator} * for this upload session. diff --git a/forms/FileField.php b/forms/FileField.php index b8027d0ad..6b62a3cef 100755 --- a/forms/FileField.php +++ b/forms/FileField.php @@ -18,7 +18,8 @@ class FileField extends FormField { * 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. - * + * + * @deprecated 2.5 * @var array */ public $allowedMaxFileSize = array(); @@ -31,7 +32,8 @@ class FileField extends FormField { * * array("jpg","GIF") * - * + * + * @deprecated 2.5 * @var array */ public $allowedExtensions = array(); @@ -95,7 +97,7 @@ class FileField extends FormField { array( "type" => "hidden", "name" => "MAX_FILE_SIZE", - "value" => $this->getAllowedMaxFileSize(), + "value" => $this->getValidator()->getAllowedMaxFileSize(), "tabindex" => $this->getTabIndex() ) ); @@ -113,8 +115,6 @@ class FileField extends FormField { $file = new File(); } - $this->upload->setAllowedExtensions($this->allowedExtensions); - $this->upload->setAllowedMaxFileSize($this->allowedMaxFileSize); $this->upload->loadIntoFile($_FILES[$this->name], $file, $this->folderName); if($this->upload->isError()) return false; @@ -132,21 +132,36 @@ class FileField extends FormField { return $_FILES[$this->Name()]; } + /** + * Get custom validator for this field + * + * @param object $validator + */ + public function getValidator() { + return $this->upload->getValidator(); + } + + /** + * Set custom validator for this field + * + * @param object $validator + */ + public function setValidator($validator) { + $this->upload->setValidator($validator); + } + /** * Get maximum file size for all or specified file extension. * Falls back to the default filesize restriction ('*') * if the extension was not found. * + * @deprecated 2.5 * @param string $ext * @return int Filesize in bytes (0 means no filesize set) */ public function getAllowedMaxFileSize($ext = null) { - $ext = strtolower($ext); - if(isset($ext) && isset($this->allowedMaxFileSize[$ext])) { - return $this->allowedMaxFileSize[$ext]; - } else { - return (isset($this->allowedMaxFileSize['*'])) ? $this->allowedMaxFileSize['*'] : 0; - } + user_error('Upload::getAllowedMaxFileSize() is deprecated. Please use Upload_Validator::getAllowedMaxFileSize() instead', E_USER_NOTICE); + $this->getValidator()->getAllowedMaxFileSize($ext); } /** @@ -159,35 +174,30 @@ class FileField extends FormField { * array('*' => 200, 'jpg' => 1000) * * + * @deprecated 2.5 * @param unknown_type $rules */ public function setAllowedMaxFileSize($rules) { - if(is_array($rules)) { - // make sure all extensions are lowercase - $rules = array_change_key_case($rules, CASE_LOWER); - $this->allowedMaxFileSize = $rules; - } else { - $this->allowedMaxFileSize['*'] = (int)$rules; - } + user_error('Upload::setAllowedMaxFileSize() is deprecated. Please use Upload_Validator::setAllowedMaxFileSize() instead', E_USER_NOTICE); + $this->getValidator()->setAllowedMaxFileSize($rules); } /** + * @deprecated 2.5 * @return array */ public function getAllowedExtensions() { - return $this->allowedExtensions; + user_error('Upload::getAllowedExtensions() is deprecated. Please use Upload_Validator::getAllowedExtensions() instead', E_USER_NOTICE); + return $this->getValidator()->getAllowedExtensions(); } /** + * @deprecated 2.5 * @param array $rules */ public function setAllowedExtensions($rules) { - if(!is_array($rules)) return false; - - // make sure all rules are lowercase - foreach($rules as &$rule) $rule = strtolower($rule); - - $this->allowedExtensions = $rules; + user_error('Upload::setAllowedExtensions() is deprecated. Please use Upload_Validator::setAllowedExtensions() instead', E_USER_NOTICE); + $this->getValidator()->setAllowedExtensions($rules); } /** @@ -208,8 +218,6 @@ class FileField extends FormField { if(!isset($_FILES[$this->name])) return true; $tmpFile = $_FILES[$this->name]; - $this->upload->setAllowedExtensions($this->allowedExtensions); - $this->upload->setAllowedMaxFileSize($this->allowedMaxFileSize); $valid = $this->upload->validate($tmpFile); if(!$valid) { diff --git a/forms/FileIFrameField.php b/forms/FileIFrameField.php index f674d43cc..5ccb337f4 100755 --- a/forms/FileIFrameField.php +++ b/forms/FileIFrameField.php @@ -167,9 +167,6 @@ class FileIFrameField extends FileField { if($data['FileSource'] == 'new') { $fileObject = Object::create($desiredClass); - $this->upload->setAllowedExtensions($this->allowedExtensions); - $this->upload->setAllowedMaxFileSize($this->allowedMaxFileSize); - $this->upload->loadIntoFile($_FILES['Upload'], $fileObject, $this->folderName); if($this->upload->isError()) { diff --git a/forms/SimpleImageField.php b/forms/SimpleImageField.php index e0eaf5257..d73e6d4dd 100755 --- a/forms/SimpleImageField.php +++ b/forms/SimpleImageField.php @@ -20,9 +20,17 @@ * @subpackage fields-files */ class SimpleImageField extends FileField { - + /** + * @deprecated 2.5 + */ public $allowedExtensions = array('jpg','gif','png'); + function __constructor($name, $title = null, $value = null, $form = null, $rightTitle = null, $folderName = null) { + parent::__constructor($name, $title, $value, $form, $rightTitle, $folderName); + + $this->getValidator()->setAllowedExtensions(array('jpg','gif','png')); + } + function Field() { if($this->form) $record = $this->form->getRecord(); $fieldName = $this->name; @@ -55,7 +63,7 @@ class SimpleImageField extends FileField { array( "type" => "hidden", "name" => "MAX_FILE_SIZE", - "value" => $this->getAllowedMaxFileSize(), + "value" => $this->getValidator()->getAllowedMaxFileSize(), "tabindex" => $this->getTabIndex() ) );