BUGFIX: replacing calls to deprecated Upload functions - using validator instead (related to r100057) (from r100496)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@105581 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-05-25 04:03:46 +00:00
parent 1e5944c690
commit ecb6c8cb3f
4 changed files with 55 additions and 31 deletions

View File

@ -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.

View File

@ -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,6 +32,9 @@ class FileField extends FormField {
* <code>
* array("jpg","GIF")
* </code>
*
* @deprecated 2.5
* @var array
*/
public $allowedExtensions = array();
@ -93,7 +97,7 @@ class FileField extends FormField {
array(
"type" => "hidden",
"name" => "MAX_FILE_SIZE",
"value" => $this->getAllowedMaxFileSize(),
"value" => $this->getValidator()->getAllowedMaxFileSize(),
"tabindex" => $this->getTabIndex()
)
);
@ -111,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;
@ -130,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);
}
/**
@ -157,35 +174,30 @@ class FileField extends FormField {
* array('*' => 200, 'jpg' => 1000)
* </code>
*
* @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);
}
/**
@ -206,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) {

View File

@ -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()) {

View File

@ -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()
)
);