| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
class sfFileValidator extends sfValidator |
|---|
| 31 |
{ |
|---|
| 32 |
|
|---|
| 33 |
* Executes this validator. |
|---|
| 34 |
* |
|---|
| 35 |
* @param mixed A file or parameter value/array |
|---|
| 36 |
* @param error An error message reference |
|---|
| 37 |
* |
|---|
| 38 |
* @return bool true, if this validator executes successfully, otherwise false |
|---|
| 39 |
*/ |
|---|
| 40 |
public function execute(&$value, &$error) |
|---|
| 41 |
{ |
|---|
| 42 |
$request = $this->getContext()->getRequest(); |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
$max_size = $this->getParameter('max_size'); |
|---|
| 46 |
if ($max_size !== null && $max_size < $value['size']) |
|---|
| 47 |
{ |
|---|
| 48 |
$error = $this->getParameter('max_size_error'); |
|---|
| 49 |
|
|---|
| 50 |
return false; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
$mime_types = $this->getParameter('mime_types'); |
|---|
| 55 |
if ($mime_types !== null && !in_array($value['type'], $mime_types)) |
|---|
| 56 |
{ |
|---|
| 57 |
$error = $this->getParameter('mime_types_error'); |
|---|
| 58 |
|
|---|
| 59 |
return false; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
return true; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
* Initializes this validator. |
|---|
| 67 |
* |
|---|
| 68 |
* @param sfContext The current application context |
|---|
| 69 |
* @param array An associative array of initialization parameters |
|---|
| 70 |
* |
|---|
| 71 |
* @return bool true, if initialization completes successfully, otherwise false |
|---|
| 72 |
*/ |
|---|
| 73 |
public function initialize($context, $parameters = null) |
|---|
| 74 |
{ |
|---|
| 75 |
|
|---|
| 76 |
parent::initialize($context); |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
$this->getParameterHolder()->set('max_size', null); |
|---|
| 80 |
$this->getParameterHolder()->set('max_size_error', 'File is too large'); |
|---|
| 81 |
$this->getParameterHolder()->set('mime_types', null); |
|---|
| 82 |
$this->getParameterHolder()->set('mime_types_error', 'Invalid mime type'); |
|---|
| 83 |
|
|---|
| 84 |
$this->getParameterHolder()->add($parameters); |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
$categories = array( |
|---|
| 88 |
'@web_images' => array( |
|---|
| 89 |
'image/jpeg', |
|---|
| 90 |
'image/pjpeg', |
|---|
| 91 |
'image/png', |
|---|
| 92 |
'image/x-png', |
|---|
| 93 |
'image/gif', |
|---|
| 94 |
), |
|---|
| 95 |
); |
|---|
| 96 |
|
|---|
| 97 |
if (!is_array($this->getParameter('mime_types'))) |
|---|
| 98 |
{ |
|---|
| 99 |
if (isset($categories[$this->getParameter('mime_types')])) |
|---|
| 100 |
{ |
|---|
| 101 |
$this->setParameter('mime_types', $categories[$this->getParameter('mime_types')]); |
|---|
| 102 |
} |
|---|
| 103 |
} |
|---|
| 104 |
elseif ($this->getParameter('mime_types', null)) |
|---|
| 105 |
{ |
|---|
| 106 |
$this->setParameter('mime_types', $this->getParameter('mime_types')); |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
return true; |
|---|
| 110 |
} |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|