Development

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

You must first sign up to be able to contribute.

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

Revision 7902, 3.3 kB (checked in by fabien, 7 years ago)

rename sfValidatorBase to sfValidator in sfCompat10Plugin and renamed the new sfValidator class to sfValidatorBase (closes #3103)

  • 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  * @package    symfony
26  * @subpackage validator
27  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
28  * @version    SVN: $Id$
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->context->getRequest();
43
44     // file too large?
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     // supported mime types formats
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     // initialize parent
76     parent::initialize($context);
77
78     // set defaults
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     // pre-defined categories
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
Note: See TracBrowser for help on using the browser.