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

Source for file file.php

Documentation is available at file.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Adam Daniel <adaniel1@eesus.jnj.com>                        |
  17. // |          Bertrand Mansion <bmansion@mamasam.com>                     |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: file.php,v 1.22 2006/10/07 20:12:17 avb Exp $
  21.  
  22. require_once("HTML/QuickForm/input.php");
  23.  
  24. // register file-related rules
  25. if (class_exists('HTML_QuickForm')) {
  26.     HTML_QuickForm::registerRule('uploadedfile''callback''_ruleIsUploadedFile''HTML_QuickForm_file');
  27.     HTML_QuickForm::registerRule('maxfilesize''callback''_ruleCheckMaxFileSize''HTML_QuickForm_file');
  28.     HTML_QuickForm::registerRule('mimetype''callback''_ruleCheckMimeType''HTML_QuickForm_file');
  29.     HTML_QuickForm::registerRule('filename''callback''_ruleCheckFileName''HTML_QuickForm_file');
  30. }
  31.  
  32. /**
  33.  * HTML class for a file type element
  34.  * 
  35.  * @author       Adam Daniel <adaniel1@eesus.jnj.com>
  36.  * @author       Bertrand Mansion <bmansion@mamasam.com>
  37.  * @version      1.0
  38.  * @since        PHP4.04pl1
  39.  * @access       public
  40.  */
  41. {
  42.     // {{{ properties
  43.  
  44.    /**
  45.     * Uploaded file data, from $_FILES
  46.     * @var array 
  47.     */
  48.     var $_value = null;
  49.  
  50.     // }}}
  51.     // {{{ constructor
  52.  
  53.     /**
  54.      * Class constructor
  55.      * 
  56.      * @param     string    Input field name attribute
  57.      * @param     string    Input field label
  58.      * @param     mixed     (optional)Either a typical HTML attribute string
  59.      *                       or an associative array
  60.      * @since     1.0
  61.      * @access    public
  62.      */
  63.     function HTML_QuickForm_file($elementName=null$elementLabel=null$attributes=null)
  64.     {
  65.         HTML_QuickForm_input::HTML_QuickForm_input($elementName$elementLabel$attributes);
  66.         $this->setType('file');
  67.     //end constructor
  68.     
  69.     // }}}
  70.     // {{{ setSize()
  71.  
  72.     /**
  73.      * Sets size of file element
  74.      * 
  75.      * @param     int    Size of file element
  76.      * @since     1.0
  77.      * @access    public
  78.      */
  79.     function setSize($size)
  80.     {
  81.         $this->updateAttributes(array('size' => $size));
  82.     //end func setSize
  83.     
  84.     // }}}
  85.     // {{{ getSize()
  86.  
  87.     /**
  88.      * Returns size of file element
  89.      * 
  90.      * @since     1.0
  91.      * @access    public
  92.      * @return    int 
  93.      */
  94.     function getSize()
  95.     {
  96.         return $this->getAttribute('size');
  97.     //end func getSize
  98.  
  99.     // }}}
  100.     // {{{ freeze()
  101.  
  102.     /**
  103.      * Freeze the element so that only its value is returned
  104.      * 
  105.      * @access    public
  106.      * @return    bool 
  107.      */
  108.     function freeze()
  109.     {
  110.         return false;
  111.     //end func freeze
  112.  
  113.     // }}}
  114.     // {{{ setValue()
  115.  
  116.     /**
  117.      * Sets value for file element.
  118.      * 
  119.      * Actually this does nothing. The function is defined here to override
  120.      * HTML_Quickform_input's behaviour of setting the 'value' attribute. As
  121.      * no sane user-agent uses <input type="file">'s value for anything
  122.      * (because of security implications) we implement file's value as a
  123.      * read-only property with a special meaning.
  124.      * 
  125.      * @param     mixed    Value for file element
  126.      * @since     3.0
  127.      * @access    public
  128.      */
  129.     function setValue($value)
  130.     {
  131.         return null;
  132.     //end func setValue
  133.     
  134.     // }}}
  135.     // {{{ getValue()
  136.  
  137.     /**
  138.      * Returns information about the uploaded file
  139.      *
  140.      * @since     3.0
  141.      * @access    public
  142.      * @return    array 
  143.      */
  144.     function getValue()
  145.     {
  146.         return $this->_value;
  147.     // end func getValue
  148.  
  149.     // }}}
  150.     // {{{ onQuickFormEvent()
  151.  
  152.     /**
  153.      * Called by HTML_QuickForm whenever form event is made on this element
  154.      *
  155.      * @param     string    Name of event
  156.      * @param     mixed     event arguments
  157.      * @param     object    calling object
  158.      * @since     1.0
  159.      * @access    public
  160.      * @return    bool 
  161.      */
  162.     function onQuickFormEvent($event$arg&$caller)
  163.     {
  164.         switch ($event{
  165.             case 'updateValue':
  166.                 if ($caller->getAttribute('method'== 'get'{
  167.                     return PEAR::raiseError('Cannot add a file upload field to a GET method form');
  168.                 }
  169.                 $this->_value $this->_findValue();
  170.                 $caller->updateAttributes(array('enctype' => 'multipart/form-data'));
  171.                 $caller->setMaxFileSize();
  172.                 break;
  173.             case 'addElement':
  174.                 $this->onQuickFormEvent('createElement'$arg$caller);
  175.                 return $this->onQuickFormEvent('updateValue'null$caller);
  176.                 break;
  177.             case 'createElement':
  178.                 $className get_class($this);
  179.                 $this->$className($arg[0]$arg[1]$arg[2]);
  180.                 break;
  181.         }
  182.         return true;
  183.     // end func onQuickFormEvent
  184.  
  185.     // }}}
  186.     // {{{ moveUploadedFile()
  187.  
  188.     /**
  189.      * Moves an uploaded file into the destination
  190.      * 
  191.      * @param    string  Destination directory path
  192.      * @param    string  New file name
  193.      * @access   public
  194.      * @return   bool    Whether the file was moved successfully
  195.      */
  196.     function moveUploadedFile($dest$fileName '')
  197.     {
  198.         if ($dest != ''  && substr($dest-1!= '/'{
  199.             $dest .= '/';
  200.         }
  201.         $fileName ($fileName != ''$fileName basename($this->_value['name']);
  202.         return move_uploaded_file($this->_value['tmp_name']$dest $fileName)
  203.     // end func moveUploadedFile
  204.     
  205.     // }}}
  206.     // {{{ isUploadedFile()
  207.  
  208.     /**
  209.      * Checks if the element contains an uploaded file
  210.      *
  211.      * @access    public
  212.      * @return    bool      true if file has been uploaded, false otherwise
  213.      */
  214.     function isUploadedFile()
  215.     {
  216.         return $this->_ruleIsUploadedFile($this->_value);
  217.     // end func isUploadedFile
  218.  
  219.     // }}}
  220.     // {{{ _ruleIsUploadedFile()
  221.  
  222.     /**
  223.      * Checks if the given element contains an uploaded file
  224.      *
  225.      * @param     array     Uploaded file info (from $_FILES)
  226.      * @access    private
  227.      * @return    bool      true if file has been uploaded, false otherwise
  228.      */
  229.     function _ruleIsUploadedFile($elementValue)
  230.     {
  231.         if ((isset($elementValue['error']&& $elementValue['error'== 0||
  232.             (!empty($elementValue['tmp_name']&& $elementValue['tmp_name'!= 'none')) {
  233.             return is_uploaded_file($elementValue['tmp_name']);
  234.         else {
  235.             return false;
  236.         }
  237.     // end func _ruleIsUploadedFile
  238.     
  239.     // }}}
  240.     // {{{ _ruleCheckMaxFileSize()
  241.  
  242.     /**
  243.      * Checks that the file does not exceed the max file size
  244.      *
  245.      * @param     array     Uploaded file info (from $_FILES)
  246.      * @param     int       Max file size
  247.      * @access    private
  248.      * @return    bool      true if filesize is lower than maxsize, false otherwise
  249.      */
  250.     function _ruleCheckMaxFileSize($elementValue$maxSize)
  251.     {
  252.         if (!empty($elementValue['error']&& 
  253.             (UPLOAD_ERR_FORM_SIZE == $elementValue['error'|| UPLOAD_ERR_INI_SIZE == $elementValue['error'])) {
  254.             return false;
  255.         }
  256.         if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
  257.             return true;
  258.         }
  259.         return ($maxSize >= @filesize($elementValue['tmp_name']));
  260.     // end func _ruleCheckMaxFileSize
  261.  
  262.     // }}}
  263.     // {{{ _ruleCheckMimeType()
  264.  
  265.     /**
  266.      * Checks if the given element contains an uploaded file of the right mime type
  267.      *
  268.      * @param     array     Uploaded file info (from $_FILES)
  269.      * @param     mixed     Mime Type (can be an array of allowed types)
  270.      * @access    private
  271.      * @return    bool      true if mimetype is correct, false otherwise
  272.      */
  273.     function _ruleCheckMimeType($elementValue$mimeType)
  274.     {
  275.         if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
  276.             return true;
  277.         }
  278.         if (is_array($mimeType)) {
  279.             return in_array($elementValue['type']$mimeType);
  280.         }
  281.         return $elementValue['type'== $mimeType;
  282.     // end func _ruleCheckMimeType
  283.  
  284.     // }}}
  285.     // {{{ _ruleCheckFileName()
  286.  
  287.     /**
  288.      * Checks if the given element contains an uploaded file of the filename regex
  289.      *
  290.      * @param     array     Uploaded file info (from $_FILES)
  291.      * @param     string    Regular expression
  292.      * @access    private
  293.      * @return    bool      true if name matches regex, false otherwise
  294.      */
  295.     function _ruleCheckFileName($elementValue$regex)
  296.     {
  297.         if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
  298.             return true;
  299.         }
  300.         return preg_match($regex$elementValue['name']);
  301.     // end func _ruleCheckFileName
  302.     
  303.     // }}}
  304.     // {{{ _findValue()
  305.  
  306.    /**
  307.     * Tries to find the element value from the values array
  308.     * 
  309.     * Needs to be redefined here as $_FILES is populated differently from
  310.     * other arrays when element name is of the form foo[bar]
  311.     * 
  312.     * @access    private
  313.     * @return    mixed 
  314.     */
  315.     function _findValue()
  316.     {
  317.         if (empty($_FILES)) {
  318.             return null;
  319.         }
  320.         $elementName $this->getName();
  321.         if (isset($_FILES[$elementName])) {
  322.             return $_FILES[$elementName];
  323.         elseif (false !== ($pos strpos($elementName'['))) {
  324.             $base  str_replace(
  325.                         array('\\''\'')array('\\\\''\\\''),
  326.                         substr($elementName0$pos)
  327.                     )
  328.             $idx   "['" str_replace(
  329.                         array('\\''\''']''[')array('\\\\''\\\''''"']['"),
  330.                         substr($elementName$pos + 1-1)
  331.                      "']";
  332.             $props = array('name''type''size''tmp_name''error');
  333.             $code  = "if (!isset(\$_FILES['{$base}']['name']{$idx})) {\n" .
  334.                      "    return null;\n" .
  335.                      "} else {\n" .
  336.                      "    \$value = array();\n";
  337.             foreach ($props as $prop{
  338.                 $code .= "    \$value['{$prop}'] = \$_FILES['{$base}']['{$prop}']{$idx};\n";
  339.             }
  340.             return eval($code "    return \$value;\n}\n");
  341.         else {
  342.             return null;
  343.         }
  344.     }
  345.  
  346.     // }}}
  347. // end class HTML_QuickForm_file
  348. ?>

Documentation generated on Mon, 11 Mar 2019 14:46:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.