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.19 2004/10/09 19:26:00 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.      */
  195.     function moveUploadedFile($dest$fileName '')
  196.     {
  197.         if ($dest != ''  && substr($dest-1!= '/'{
  198.             $dest .= '/';
  199.         }
  200.         $fileName ($fileName != ''$fileName basename($this->_value['name']);
  201.         if (move_uploaded_file($this->_value['tmp_name']$dest $fileName)) {
  202.             return true;
  203.         else {
  204.             return false;
  205.         }
  206.     // end func moveUploadedFile
  207.     
  208.     // }}}
  209.     // {{{ isUploadedFile()
  210.  
  211.     /**
  212.      * Checks if the element contains an uploaded file
  213.      *
  214.      * @access    public
  215.      * @return    bool      true if file has been uploaded, false otherwise
  216.      */
  217.     function isUploadedFile()
  218.     {
  219.         return $this->_ruleIsUploadedFile($this->_value);
  220.     // end func isUploadedFile
  221.  
  222.     // }}}
  223.     // {{{ _ruleIsUploadedFile()
  224.  
  225.     /**
  226.      * Checks if the given element contains an uploaded file
  227.      *
  228.      * @param     array     Uploaded file info (from $_FILES)
  229.      * @access    private
  230.      * @return    bool      true if file has been uploaded, false otherwise
  231.      */
  232.     function _ruleIsUploadedFile($elementValue)
  233.     {
  234.         if ((isset($elementValue['error']&& $elementValue['error'== 0||
  235.             (!empty($elementValue['tmp_name']&& $elementValue['tmp_name'!= 'none')) {
  236.             return is_uploaded_file($elementValue['tmp_name']);
  237.         else {
  238.             return false;
  239.         }
  240.     // end func _ruleIsUploadedFile
  241.     
  242.     // }}}
  243.     // {{{ _ruleCheckMaxFileSize()
  244.  
  245.     /**
  246.      * Checks that the file does not exceed the max file size
  247.      *
  248.      * @param     array     Uploaded file info (from $_FILES)
  249.      * @param     int       Max file size
  250.      * @access    private
  251.      * @return    bool      true if filesize is lower than maxsize, false otherwise
  252.      */
  253.     function _ruleCheckMaxFileSize($elementValue$maxSize)
  254.     {
  255.         if (!empty($elementValue['error']&& 
  256.             (UPLOAD_ERR_FORM_SIZE == $elementValue['error'|| UPLOAD_ERR_INI_SIZE == $elementValue['error'])) {
  257.             return false;
  258.         }
  259.         if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
  260.             return true;
  261.         }
  262.         return ($maxSize >= @filesize($elementValue['tmp_name']));
  263.     // end func _ruleCheckMaxFileSize
  264.  
  265.     // }}}
  266.     // {{{ _ruleCheckMimeType()
  267.  
  268.     /**
  269.      * Checks if the given element contains an uploaded file of the right mime type
  270.      *
  271.      * @param     array     Uploaded file info (from $_FILES)
  272.      * @param     mixed     Mime Type (can be an array of allowed types)
  273.      * @access    private
  274.      * @return    bool      true if mimetype is correct, false otherwise
  275.      */
  276.     function _ruleCheckMimeType($elementValue$mimeType)
  277.     {
  278.         if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
  279.             return true;
  280.         }
  281.         if (is_array($mimeType)) {
  282.             return in_array($elementValue['type']$mimeType);
  283.         }
  284.         return $elementValue['type'== $mimeType;
  285.     // end func _ruleCheckMimeType
  286.  
  287.     // }}}
  288.     // {{{ _ruleCheckFileName()
  289.  
  290.     /**
  291.      * Checks if the given element contains an uploaded file of the filename regex
  292.      *
  293.      * @param     array     Uploaded file info (from $_FILES)
  294.      * @param     string    Regular expression
  295.      * @access    private
  296.      * @return    bool      true if name matches regex, false otherwise
  297.      */
  298.     function _ruleCheckFileName($elementValue$regex)
  299.     {
  300.         if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
  301.             return true;
  302.         }
  303.         return preg_match($regex$elementValue['name']);
  304.     // end func _ruleCheckFileName
  305.     
  306.     // }}}
  307.     // {{{ _findValue()
  308.  
  309.    /**
  310.     * Tries to find the element value from the values array
  311.     * 
  312.     * Needs to be redefined here as $_FILES is populated differently from
  313.     * other arrays when element name is of the form foo[bar]
  314.     * 
  315.     * @access    private
  316.     * @return    mixed 
  317.     */
  318.     function _findValue()
  319.     {
  320.         if (empty($_FILES)) {
  321.             return null;
  322.         }
  323.         $elementName $this->getName();
  324.         if (isset($_FILES[$elementName])) {
  325.             return $_FILES[$elementName];
  326.         elseif (false !== ($pos strpos($elementName'['))) {
  327.             $base  substr($elementName0$pos);
  328.             $idx   "['" str_replace(array(']''[')array(''"']['")substr($elementName$pos + 1-1)) "']";
  329.             $props = array('name''type''size''tmp_name''error');
  330.             $code  = "if (!isset(\$_FILES['{$base}']['name']{$idx})) {\n" .
  331.                      "    return null;\n" .
  332.                      "} else {\n" .
  333.                      "    \$value = array();\n";
  334.             foreach ($props as $prop{
  335.                 $code .= "    \$value['{$prop}'] = \$_FILES['{$base}']['{$prop}']{$idx};\n";
  336.             }
  337.             return eval($code "    return \$value;\n}\n");
  338.         else {
  339.             return null;
  340.         }
  341.     }
  342.  
  343.     // }}}
  344. // end class HTML_QuickForm_file
  345. ?>

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