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 / Progress2 / Upload.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  9.8 KB  |  287 lines

  1. <?php
  2. /**
  3.  * Copyright (c) 2005-2008, Laurent Laville <pear@laurent-laville.org>
  4.  *
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  *     * Redistributions of source code must retain the above copyright
  12.  *       notice, this list of conditions and the following disclaimer.
  13.  *     * Redistributions in binary form must reproduce the above copyright
  14.  *       notice, this list of conditions and the following disclaimer in the
  15.  *       documentation and/or other materials provided with the distribution.
  16.  *     * Neither the name of the authors nor the names of its contributors
  17.  *       may be used to endorse or promote products derived from this software
  18.  *       without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  24.  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * PHP versions 4 and 5
  33.  *
  34.  * @category  HTML
  35.  * @package   HTML_Progress2
  36.  * @author    Laurent Laville <pear@laurent-laville.org>
  37.  * @copyright 2007-2008 Laurent Laville
  38.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  39.  * @version   CVS: $Id: Upload.php,v 1.7 2008/04/21 22:38:25 farell Exp $
  40.  * @link      http://pear.php.net/package/HTML_Progress2
  41.  * @since     File available since Release 2.3.0RC1
  42.  */
  43.  
  44.  
  45. /**
  46.  * Backend for AJAX uploading bar.
  47.  *
  48.  * @category  HTML
  49.  * @package   HTML_Progress2
  50.  * @author    Laurent Laville <pear@laurent-laville.org>
  51.  * @copyright 2007-2008 Laurent Laville
  52.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  53.  * @version   Release: 2.4.0
  54.  * @link      http://pear.php.net/package/HTML_Progress2
  55.  * @since     Class available since Release 2.3.0RC1
  56.  */
  57.  
  58. class HTML_Progress2_Upload
  59. {
  60.     /**
  61.      * Backend identifier are either :
  62.      * 'none' - if nothing else below match
  63.      * 'apc5' - APC php extension with PHP5.2.0 or greater
  64.      * 'upm5' - upload progress meter php extension with PHP5.2.0 or greater
  65.      *
  66.      * @var        string
  67.      * @since      2.3.0RC1
  68.      * @access     public
  69.      */
  70.     var $backend;
  71.  
  72.     /**
  73.      * Formatted strings for Progress bar labels that will display upload info.
  74.      *
  75.      * Each conversion specification consists of a percent sign (%), followed
  76.      * by one or more of these elements.
  77.      *  T - for display total file size
  78.      *  C - for display current file size uploaded
  79.      *  P - for display percentage of file size uploaded
  80.      *  F - for display file name to upload
  81.      *  E - for estimate time left
  82.      *  S - for speed limit
  83.      *
  84.      * @var        string
  85.      * @since      2.3.0RC1
  86.      * @access     public
  87.      * @see        HTML_Progress2_Upload::sprintf()
  88.      */
  89.     var $format;
  90.  
  91.     /**
  92.      * Constructor (ZE2) Summary
  93.      *
  94.      * @param string $format (optional) conversion specifications
  95.      *                                  for all progress bar labels
  96.      *
  97.      * @since      version 2.3.0RC1 (2007-02-12)
  98.      * @access     protected
  99.      * @link       http://pdoru.from.ro/upload-progress-meter/  Patch for PHP 4
  100.      */
  101.     function __construct($format = null)
  102.     {
  103.         $php5 = (version_compare(phpversion(), '5.2.0', 'ge') < 0) ? false : true;
  104.  
  105.         if (extension_loaded('uploadprogress') && $php5) {
  106.             $this->backend = 'upm5';
  107.         } elseif (extension_loaded('apc') && $php5) {
  108.             $this->backend = 'apc5';
  109.         } else {
  110.             $this->backend = 'none';
  111.         }
  112.  
  113.         if (!is_array($format)) {
  114.             $format = array('uplStatus' => '%C / %T (%P%)');
  115.         }
  116.         $this->format = $format;
  117.     }
  118.  
  119.     /**
  120.      * Return hash of data with current file upload information, depending of
  121.      * backend used.
  122.      *
  123.      * @param string $Id upload identifier
  124.      *
  125.      * @return     bool|array   false if info unavailable, hash of data otherwise
  126.      * @since      version 2.3.0RC1 (2007-02-12)
  127.      * @access     public
  128.      */
  129.     function getInfo($Id)
  130.     {
  131.         $info = array('time_start' => 0, 'time_last' => 0,
  132.             'speed_average' => 0, 'speed_last' => 0,
  133.             'est_sec' => -1,
  134.             'bytes_uploaded' => 0,  'bytes_total' => 0,
  135.             'files_uploaded' => 0,
  136.             'cancel_upload' => 0
  137.             );
  138.  
  139.         switch ($this->backend) {
  140.         case 'upm5':
  141.             $tmp = uploadprogress_get_info($Id);
  142.             if (is_array($tmp)) {
  143.                 $info                 = array_merge($info, $tmp);
  144.                 $info['current_file'] = $tmp['filename'];
  145.             } else {
  146.                 $info = false;
  147.             }
  148.             break;
  149.         case 'apc5':
  150.             $tmp = apc_fetch('upload_' . $Id);
  151.             if (is_array($tmp)) {
  152.                 $info['bytes_uploaded'] = $tmp['current'];
  153.                 $info['bytes_total']    = $tmp['total'];
  154.                 $info['files_uploaded'] = $tmp['done'];
  155.                 $info['current_file']   = $tmp['filename'];
  156.                 if (isset($tmp['cancel_upload'])) {
  157.                     $info['cancel_upload'] = $tmp['cancel_upload'];
  158.                 }
  159.             } else {
  160.                 $info = false;
  161.             }
  162.             break;
  163.         default:
  164.         }
  165.  
  166.         return $info;
  167.     }
  168.  
  169.     /**
  170.      * Return a formatted string with current upload information
  171.      *
  172.      * Each conversion specification consists of a percent sign (%), followed
  173.      * by one or more of these elements.
  174.      *  T - for display total file size
  175.      *  C - for display current file size uploaded
  176.      *  P - for display percentage of file size uploaded
  177.      *  F - for display file name to upload
  178.      *  E - for estimate time left
  179.      *  S - for speed limit
  180.      *
  181.      * @param string $format conversion specification
  182.      * @param array  $info   current upload information
  183.      *
  184.      * @return     bool|string   false on error, formatted string with current info
  185.      * @since      version 2.3.0RC1 (2007-02-12)
  186.      * @access     public
  187.      */
  188.     function sprintf($format, $info)
  189.     {
  190.         if (!is_array($info)) {
  191.             return false;
  192.         }
  193.  
  194.         $pos = strpos($format, '%T');
  195.         if ($pos !== false) {
  196.             $format = str_replace('%T', $this->formatBytes($info['bytes_total']),
  197.                                   $format);
  198.         }
  199.         $pos = strpos($format, '%C');
  200.         if ($pos !== false) {
  201.             $format = str_replace('%C', $this->formatBytes($info['bytes_uploaded']),
  202.                                   $format);
  203.         }
  204.         $pos = strpos($format, '%P');
  205.         if ($pos !== false) {
  206.             if ($info['bytes_total'] < 1) {
  207.                 $percent = 100;
  208.             } else {
  209.                 $percent = round($info['bytes_uploaded'] /
  210.                                  $info['bytes_total'] * 100);
  211.             }
  212.             $format = str_replace('%P', $percent, $format);
  213.         }
  214.         $pos = strpos($format, '%F');
  215.         if ($pos !== false) {
  216.             $format = str_replace('%F', $info['current_file'], $format);
  217.         }
  218.         $pos = strpos($format, '%E');
  219.         if ($pos !== false
  220.             && $info['est_sec'] > 0) {
  221.             $eta    = sprintf("%02d:%02d",
  222.                               $info['est_sec'] / 60, $info['est_sec'] % 60);
  223.             $format = str_replace('%E', $eta, $format);
  224.         }
  225.  
  226.         $pos = strpos($format, '%S');
  227.         if ($pos !== false) {
  228.             $speed  = $this->formatBytes($info['speed_average']);
  229.             $format = str_replace('%S', $speed, $format);
  230.         }
  231.         return $format;
  232.     }
  233.  
  234.     /**
  235.      * Get the status of an upload passed in
  236.      *
  237.      * @param string $uplId (optional) upload identifier
  238.      *
  239.      * @return     array
  240.      * @since      version 2.3.0RC1 (2007-02-12)
  241.      * @access     public
  242.      */
  243.     function getStatus($uplId = null)
  244.     {
  245.         if (!isset($uplId)) {
  246.             $uplId = $_GET['upload_identifier'];
  247.         }
  248.         $tmp = $this->getInfo($uplId);
  249.         if (!is_array($tmp)) {
  250.             $ret = array('percentage' => 100, 'labels' => array());
  251.             return $ret;
  252.         }
  253.  
  254.         $percent = $this->sprintf('%P', $tmp);
  255.         $labels  = array();
  256.         foreach ($this->format as $lbl => $fmt) {
  257.             $labels[$lbl] = $this->sprintf($fmt, $tmp);
  258.         }
  259.  
  260.         $ret = array('percentage' => $percent, 'labels' => $labels,
  261.             'bytes_uploaded' => $tmp['bytes_uploaded'],
  262.             'bytes_total' => $tmp['bytes_total']
  263.         );
  264.         return $ret;
  265.     }
  266.  
  267.     /**
  268.      * Function to convert bytes to something larger
  269.      *
  270.      * @param int $x file size in bytes
  271.      *
  272.      * @return     string
  273.      * @since      version 2.3.0RC1 (2007-02-12)
  274.      * @access     public
  275.      */
  276.     function formatBytes($x)
  277.     {
  278.         if ($x < 1024) {
  279.             return "$x bytes";
  280.         } elseif ($x < (1024 * 1024)) {
  281.             return sprintf("%d KB", $x/1024);
  282.         } else {
  283.             return sprintf("%d MB", $x/(1024 * 1024));
  284.         }
  285.     }
  286. }
  287. ?>