HTML_QuickForm2
[ class tree: HTML_QuickForm2 ] [ index: HTML_QuickForm2 ] [ all elements ]

Source for file InputFile.php

Documentation is available at InputFile.php

  1. <?php
  2. /**
  3.  * Class for <input type="file" /> elements
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * LICENSE:
  8.  *
  9.  * Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>,
  10.  *                           Bertrand Mansion <golgote@mamasam.com>
  11.  * All rights reserved.
  12.  *
  13.  * Redistribution and use in source and binary forms, with or without
  14.  * modification, are permitted provided that the following conditions
  15.  * are met:
  16.  *
  17.  *    * Redistributions of source code must retain the above copyright
  18.  *      notice, this list of conditions and the following disclaimer.
  19.  *    * Redistributions in binary form must reproduce the above copyright
  20.  *      notice, this list of conditions and the following disclaimer in the
  21.  *      documentation and/or other materials provided with the distribution.
  22.  *    * The names of the authors may not be used to endorse or promote products
  23.  *      derived from this software without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  33.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.  *
  37.  * @category   HTML
  38.  * @package    HTML_QuickForm2
  39.  * @author     Alexey Borzov <avb@php.net>
  40.  * @author     Bertrand Mansion <golgote@mamasam.com>
  41.  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
  42.  * @version    CVS: $Id: InputFile.php,v 1.6 2007/10/22 16:04:57 avb Exp $
  43.  * @link       http://pear.php.net/package/HTML_QuickForm2
  44.  */
  45.  
  46. /**
  47.  * Base class for <input> elements
  48.  */
  49. require_once 'HTML/QuickForm2/Element/Input.php';
  50.  
  51. /**
  52.  * Class for <input type="file" /> elements
  53.  *
  54.  * @category   HTML
  55.  * @package    HTML_QuickForm2
  56.  * @author     Alexey Borzov <avb@php.net>
  57.  * @author     Bertrand Mansion <golgote@mamasam.com>
  58.  * @version    Release: 0.2.0
  59.  */
  60. {
  61.    /**
  62.     * Default language for error messages
  63.     */
  64.     const DEFAULT_LANGUAGE = 'en';
  65.  
  66.    /**
  67.     * Localized error messages for PHP's file upload errors
  68.     * @var  array 
  69.     */
  70.     protected $errorMessages = array(
  71.         'en' => array(
  72.             UPLOAD_ERR_INI_SIZE   => 'The uploaded file exceeds size permitted by PHP configuration (%d bytes)',
  73.             UPLOAD_ERR_FORM_SIZE  => 'The uploaded file exceeds the MAX_FILE_SIZE directive in HTML form (%d bytes)',
  74.             UPLOAD_ERR_PARTIAL    => 'The file was only partially uploaded',
  75.             UPLOAD_ERR_NO_TMP_DIR => 'Server error: temporary directory is missing',
  76.             UPLOAD_ERR_CANT_WRITE => 'Server error: failed to write the file to disk',
  77.             UPLOAD_ERR_EXTENSION  => 'File upload was stopped by extension'
  78.         ),
  79.         'fr' => array(
  80.             UPLOAD_ERR_INI_SIZE   => 'Le fichier envoy&eacute; exc&egrave;de la taille autoris&eacute;e par la configuration de PHP (%d octets)',
  81.             UPLOAD_ERR_FORM_SIZE  => 'Le fichier envoy&eacute; exc&egrave;de la taille de MAX_FILE_SIZE sp&eacute;cifi&eacute;e dans le formulaire HTML (%d octets)',
  82.             UPLOAD_ERR_PARTIAL    => 'Le fichier n\'a &eacute;t&eacute; que partiellement t&eacute;l&eacute;charg&eacute;',
  83.             UPLOAD_ERR_NO_TMP_DIR => 'Erreur serveur: le r&eacute;pertoire temporaire est manquant',
  84.             UPLOAD_ERR_CANT_WRITE => 'Erreur serveur: &eacute;chec de l\'&eacute;criture du fichier sur le disque',
  85.             UPLOAD_ERR_EXTENSION  => 'L\'envoi de fichier est arr&ecirc;t&eacute; par l\'extension'
  86.         ),
  87.         'ru' => array(
  88.             UPLOAD_ERR_INI_SIZE   => '&#x420;&#x430;&#x437;&#x43c;&#x435;&#x440; &#x437;&#x430;&#x433;&#x440;&#x443;&#x436;&#x435;&#x43d;&#x43d;&#x43e;&#x433;&#x43e; &#x444;&#x430;&#x439;&#x43b;&#x430; &#x43f;&#x440;&#x435;&#x432;&#x43e;&#x441;&#x445;&#x43e;&#x434;&#x438;&#x442; &#x43c;&#x430;&#x43a;&#x441;&#x438;&#x43c;&#x430;&#x43b;&#x44c;&#x43d;&#x43e; &#x440;&#x430;&#x437;&#x440;&#x435;&#x448;&#x451;&#x43d;&#x43d;&#x44b;&#x439; &#x43d;&#x430;&#x441;&#x442;&#x440;&#x43e;&#x439;&#x43a;&#x430;&#x43c;&#x438; PHP (%d &#x431;&#x430;&#x439;&#x442;)',
  89.             UPLOAD_ERR_FORM_SIZE  => '&#x420;&#x430;&#x437;&#x43c;&#x435;&#x440; &#x437;&#x430;&#x433;&#x440;&#x443;&#x436;&#x435;&#x43d;&#x43d;&#x43e;&#x433;&#x43e; &#x444;&#x430;&#x439;&#x43b;&#x430; &#x43f;&#x440;&#x435;&#x432;&#x43e;&#x441;&#x445;&#x43e;&#x434;&#x438;&#x442; &#x434;&#x438;&#x440;&#x435;&#x43a;&#x442;&#x438;&#x432;&#x443; MAX_FILE_SIZE, &#x443;&#x43a;&#x430;&#x437;&#x430;&#x43d;&#x43d;&#x443;&#x44e; &#x432; &#x444;&#x43e;&#x440;&#x43c;&#x435; (%d &#x431;&#x430;&#x439;&#x442;)',
  90.             UPLOAD_ERR_PARTIAL    => '&#x424;&#x430;&#x439;&#x43b; &#x431;&#x44b;&#x43b; &#x437;&#x430;&#x433;&#x440;&#x443;&#x436;&#x435;&#x43d; &#x43d;&#x435; &#x43f;&#x43e;&#x43b;&#x43d;&#x43e;&#x441;&#x442;&#x44c;&#x44e;',
  91.             UPLOAD_ERR_NO_TMP_DIR => '&#x41e;&#x448;&#x438;&#x431;&#x43a;&#x430; &#x43d;&#x430; &#x441;&#x435;&#x440;&#x432;&#x435;&#x440;&#x435;: &#x43e;&#x442;&#x441;&#x443;&#x442;&#x441;&#x442;&#x432;&#x443;&#x435;&#x442; &#x43a;&#x430;&#x442;&#x430;&#x43b;&#x43e;&#x433; &#x434;&#x43b;&#x44f; &#x432;&#x440;&#x435;&#x43c;&#x435;&#x43d;&#x43d;&#x44b;&#x445; &#x444;&#x430;&#x439;&#x43b;&#x43e;&#x432;',
  92.             UPLOAD_ERR_CANT_WRITE => '&#x41e;&#x448;&#x438;&#x431;&#x43a;&#x430; &#x43d;&#x430; &#x441;&#x435;&#x440;&#x432;&#x435;&#x440;&#x435;: &#x43d;&#x435; &#x443;&#x434;&#x430;&#x43b;&#x43e;&#x441;&#x44c; &#x437;&#x430;&#x43f;&#x438;&#x441;&#x430;&#x442;&#x44c; &#x444;&#x430;&#x439;&#x43b; &#x43d;&#x430; &#x434;&#x438;&#x441;&#x43a;',
  93.             UPLOAD_ERR_EXTENSION  => '&#x417;&#x430;&#x433;&#x440;&#x443;&#x437;&#x43a;&#x430; &#x444;&#x430;&#x439;&#x43b;&#x430; &#x431;&#x44b;&#x43b;&#x430; &#x43e;&#x441;&#x442;&#x430;&#x43d;&#x43e;&#x432;&#x43b;&#x435;&#x43d;&#x430; &#x440;&#x430;&#x441;&#x448;&#x438;&#x440;&#x435;&#x43d;&#x438;&#x435;&#x43c;'
  94.         )
  95.     );
  96.  
  97.    /**
  98.     * Language to display error messages in
  99.     * @var  string 
  100.     */
  101.     protected $language;
  102.  
  103.    /**
  104.     * Information on uploaded file, from submit data source
  105.     * @var array 
  106.     */
  107.     protected $value = null;
  108.  
  109.     protected $attributes = array('type' => 'file');
  110.  
  111.  
  112.    /**
  113.     * Class constructor
  114.     *
  115.     * Possible keys in $data array are:
  116.     *  - 'language': language to display error messages in, it should either be
  117.     *    already available in the class or provided in 'errorMessages'
  118.     *  - 'errorMessages': an array of error messages with the following format
  119.     *    <pre>
  120.     *      'language code 1' => array(
  121.     *         UPLOAD_ERR_... => 'message',
  122.     *         ...
  123.     *         UPLOAD_ERR_... => 'message'
  124.     *      ),
  125.     *      ...
  126.     *      'language code N' => array(
  127.     *         ...
  128.     *      )
  129.     *    </pre>
  130.     *    Note that error messages for UPLOAD_ERR_INI_SIZE and UPLOAD_ERR_FORM_SIZE
  131.     *    may contain '%d' placeholders that will be automatically replaced by the
  132.     *    appropriate size limits. Note also that you don't need to provide messages
  133.     *    for every possible error code in the arrays, you may e.g. override just
  134.     *    one error message for a particular language.
  135.     *
  136.     * @param    string  Element name
  137.     * @param    mixed   Attributes (either a string or an array)
  138.     * @param    array   Data used to set up error messages for PHP's file
  139.     *                    upload errors.
  140.     */
  141.     public function __construct($name = null$attributes = nullarray $data = array())
  142.     {
  143.         if (isset($data['errorMessages'])) {
  144.             // neither array_merge() nor array_merge_recursive will do
  145.             foreach ($data['errorMessages'as $lang => $ary{
  146.                 foreach ($ary as $code => $message{
  147.                     $this->errorMessages[$lang][$code$message;
  148.                 }
  149.             }
  150.             unset($data['errorMessages']);
  151.         }
  152.         if (!isset($data['language'])) {
  153.             $this->language = self::DEFAULT_LANGUAGE;
  154.         else {
  155.             $this->language = isset($this->errorMessages[$data['language']])?
  156.                               $data['language']: self::DEFAULT_LANGUAGE;
  157.             unset($data['language']);
  158.         }
  159.         parent::__construct($name$attributes$data);
  160.     }
  161.  
  162.  
  163.    /**
  164.     * File upload elements cannot be frozen
  165.     *
  166.     * To properly "freeze" a file upload element one has to store the uploaded
  167.     * file somewhere and store the file info in session. This is way outside
  168.     * the scope of this class.
  169.     *
  170.     * @param    bool    Whether element should be frozen or editable. This
  171.     *                    parameter is ignored in case of file uploads
  172.     * @return   bool    Always returns false
  173.     */
  174.     public function toggleFrozen($freeze = null)
  175.     {
  176.         return false;
  177.     }
  178.  
  179.    /**
  180.     * Returns the information on uploaded file
  181.     *
  182.     * @return   array|null
  183.     */
  184.     public function getValue()
  185.     {
  186.         return $this->value;
  187.     }
  188.  
  189.    /**
  190.     * File upload's value cannot be set here
  191.     *
  192.     * @param     mixed    Value for file element, this parameter is ignored
  193.     * @return    HTML_QuickForm2_Element_InputFile 
  194.     */
  195.     public function setValue($value)
  196.     {
  197.         return $this;
  198.     }
  199.  
  200.     protected function updateValue()
  201.     {
  202.         foreach ($this->getDataSources(as $ds{
  203.             if ($ds instanceof HTML_QuickForm2_DataSource_Submit{
  204.                 $value $ds->getUpload($this->getName());
  205.                 if (null !== $value{
  206.                     $this->value = $value;
  207.                     return;
  208.                 }
  209.             }
  210.         }
  211.         $this->value = null;
  212.     }
  213.  
  214.    /**
  215.     * Performs the server-side validation
  216.     *
  217.     * Before the Rules added to the element kick in, the element checks the
  218.     * error code added to the $_FILES array by PHP. If the code isn't
  219.     * UPLOAD_ERR_OK or UPLOAD_ERR_NO_FILE then a built-in error message will be
  220.     * displayed and no further validation will take place.
  221.     *
  222.     * @return   boolean     Whether the element is valid
  223.     */
  224.     protected function validate()
  225.     {
  226.         if (strlen($this->error)) {
  227.             return false;
  228.         }
  229.         if (isset($this->value['error']&&
  230.             !in_array($this->value['error']array(UPLOAD_ERR_OKUPLOAD_ERR_NO_FILE)))
  231.         {
  232.             if (isset($this->errorMessages[$this->language][$this->value['error']])) {
  233.                 $errorMessage $this->errorMessages[$this->language][$this->value['error']];
  234.             else {
  235.                 $errorMessage $this->errorMessages[self::DEFAULT_LANGUAGE][$this->value['error']];
  236.             }
  237.             if (UPLOAD_ERR_INI_SIZE == $this->value['error']{
  238.                 $iniSize ini_get('upload_max_filesize');
  239.                 $size    intval($iniSize);
  240.                 switch (strtoupper(substr($iniSize-1))) {
  241.                     case 'G'$size *= 1024;
  242.                     case 'M'$size *= 1024;
  243.                     case 'K'$size *= 1024;
  244.                 }
  245.  
  246.             elseif (UPLOAD_ERR_FORM_SIZE == $this->value['error']{
  247.                 foreach ($this->getDataSources(as $ds{
  248.                     if ($ds instanceof HTML_QuickForm2_DataSource_Submit{
  249.                         $size intval($ds->getValue('MAX_FILE_SIZE'));
  250.                         break;
  251.                     }
  252.                 }
  253.             }
  254.             $this->error = isset($size)sprintf($errorMessage$size)$errorMessage;
  255.             return false;
  256.         }
  257.         return parent::validate();
  258.     }
  259. }
  260. ?>

Documentation generated on Mon, 22 Oct 2007 12:30:17 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.