home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / QuickForm / file.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  11.2 KB  |  359 lines

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