Development

/branches/1.1/lib/plugins/sfCompat10Plugin/lib/validator/sfFileValidator.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/plugins/sfCompat10Plugin/lib/validator/sfFileValidator.class.php

Revision 10270, 3.4 kB (checked in by Carl.Vondrick, 6 years ago)

1.1: added deprecated warnings to all sfCompatPlugin classes (closes #3882)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfFileValidator allows you to apply constraints to file upload.
13  *
14  * <b>Optional parameters:</b>
15  *
16  * # <b>max_size</b>         - [none]               - Maximum file size length.
17  * # <b>max_size_error</b>   - [File is too large]  - An error message to use when
18  *                                                file is too large.
19  * # <b>mime_types</b>       - [none]               - An array of mime types the file
20  *                                                is allowed to match.
21  * # <b>mime_types_error</b> - [Invalid mime type]  - An error message to use when
22  *                                                file mime type does not match a value
23  *                                                listed in the mime types array.
24  *
25  * WARNING: This class is deprecated and will be removed in symfony 1.2.
26  *
27  * @package    symfony
28  * @subpackage validator
29  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
30  * @version    SVN: $Id$
31  * @deprecated Deprecated since symfony 1.1
32  */
33 class sfFileValidator extends sfValidator
34 {
35   /**
36    * Executes this validator.
37    *
38    * @param mixed A file or parameter value/array
39    * @param error An error message reference
40    *
41    * @return bool true, if this validator executes successfully, otherwise false
42    */
43   public function execute(&$value, &$error)
44   {
45     $request = $this->context->getRequest();
46
47     // file too large?
48     $max_size = $this->getParameter('max_size');
49     if ($max_size !== null && $max_size < $value['size'])
50     {
51       $error = $this->getParameter('max_size_error');
52
53       return false;
54     }
55
56     // supported mime types formats
57     $mime_types = $this->getParameter('mime_types');
58     if ($mime_types !== null && !in_array($value['type'], $mime_types))
59     {
60       $error = $this->getParameter('mime_types_error');
61
62       return false;
63     }
64
65     return true;
66   }
67
68   /**
69    * Initializes this validator.
70    *
71    * @param sfContext The current application context
72    * @param array   An associative array of initialization parameters
73    *
74    * @return bool true, if initialization completes successfully, otherwise false
75    */
76   public function initialize($context, $parameters = null)
77   {
78     // initialize parent
79     parent::initialize($context);
80
81     // set defaults
82     $this->getParameterHolder()->set('max_size',         null);
83     $this->getParameterHolder()->set('max_size_error',   'File is too large');
84     $this->getParameterHolder()->set('mime_types',        null);
85     $this->getParameterHolder()->set('mime_types_error', 'Invalid mime type');
86
87     $this->getParameterHolder()->add($parameters);
88
89     // pre-defined categories
90     $categories = array(
91       '@web_images' => array(
92         'image/jpeg',
93         'image/pjpeg',
94         'image/png',
95         'image/x-png',
96         'image/gif',
97       ),
98     );
99
100     if (!is_array($this->getParameter('mime_types')))
101     {
102       if (isset($categories[$this->getParameter('mime_types')]))
103       {
104         $this->setParameter('mime_types', $categories[$this->getParameter('mime_types')]);
105       }
106     }
107     elseif ($this->getParameter('mime_types', null))
108     {
109       $this->setParameter('mime_types', $this->getParameter('mime_types'));
110     }
111
112     return true;
113   }
114 }
115
Note: See TracBrowser for help on using the browser.