[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/validator/ -> sfFileValidator.class.php (source)

   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: sfFileValidator.class.php 3233 2007-01-11 21:01:08Z fabien $
  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      // 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  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7