home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / HTML / Progress / uploader.php < prev   
Encoding:
PHP Script  |  2004-03-24  |  11.6 KB  |  339 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2004 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 3.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/3_0.txt.                                  |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Author: Laurent Laville <pear@laurent-laville.org>                   |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: uploader.php,v 1.1 2004/02/14 22:03:27 farell Exp $
  19.  
  20. require_once ('HTML/Progress/Error/Raise.php');
  21. require_once ('HTML/Progress/FTP/upload.php');
  22. require_once ('HTML/Progress.php');
  23.  
  24. require_once ('HTML/QuickForm.php');
  25.  
  26. /**
  27.  *
  28.  * The HTML_Progress_Uploader class provides a GUI interface
  29.  * (with progress bar) to manage files to upload to a 
  30.  * ftp server via your web browser.
  31.  *
  32.  * @version    1.1
  33.  * @author     Laurent Laville <pear@laurent-laville.org>
  34.  * @access     public
  35.  * @category   HTML
  36.  * @package    HTML_Progress
  37.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  38.  */
  39.  
  40. class HTML_Progress_Uploader extends FTP_Upload
  41. {
  42.     /**#@+
  43.      * Attributes of upload form.
  44.      *
  45.      * @var        string
  46.      * @since      1.1
  47.      * @access     public
  48.      */
  49.     var $windowname;
  50.     var $captionMask;
  51.     var $buttonStart;
  52.     var $buttonCancel;
  53.     /**#@-*/
  54.     
  55.     /**
  56.      * The progress object renders into this uploader.
  57.      *
  58.      * @var        object
  59.      * @since      1.1
  60.      * @access     private
  61.      */
  62.     var $_progress;
  63.  
  64.     /**
  65.      * The quickform object that allows the presentation.
  66.      *
  67.      * @var        object
  68.      * @since      1.1
  69.      * @access     private
  70.      */
  71.     var $_form;
  72.  
  73.     
  74.     /**
  75.      * The progress uploader class constructor
  76.      *
  77.      * @param      string    $formName      (optional) Name of monitor dialog box (QuickForm)
  78.      * @param      array     $attributes    (optional) List of renderer options
  79.      *
  80.      * @since      1.1
  81.      * @access     public
  82.      * @throws     HTML_PROGRESS_ERROR_INVALID_INPUT
  83.      */
  84.     function HTML_Progress_Uploader($formName = 'ProgressUploader', $attributes = array())
  85.     {
  86.         $this->_package = 'HTML_Progress_Uploader';
  87.         Error_Raise::initialize($this->_package, array('HTML_Progress', '_getErrorMessage'));
  88.  
  89.         if (!is_string($formName)) {
  90.             return Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  91.                 array('var' => '$formName',
  92.                       'was' => gettype($formName),
  93.                       'expected' => 'string',
  94.                       'paramnum' => 1), PEAR_ERROR_TRIGGER);
  95.  
  96.         } elseif (!is_array($attributes)) {
  97.             return Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  98.                 array('var' => '$attributes',
  99.                       'was' => gettype($attributes),
  100.                       'expected' => 'array',
  101.                       'paramnum' => 2), PEAR_ERROR_TRIGGER);
  102.         }
  103.         parent::FTP_Upload();          // checks all necessary dependencies
  104.         
  105.         $this->_form = new HTML_QuickForm($formName);
  106.  
  107.         $this->windowname   = isset($attributes['title'])  ? $attributes['title']  : 'Upload ...';
  108.         $this->captionMask  = isset($attributes['mask'])   ? $attributes['mask']   : '%s';
  109.         $this->buttonStart  = isset($attributes['start'])  ? $attributes['start']  : 'Start';
  110.         $this->buttonCancel = isset($attributes['cancel']) ? $attributes['cancel'] : 'Cancel';
  111.         $buttonAttr         = isset($attributes['button']) ? $attributes['button'] : '';
  112.         
  113.         $this->_form->addElement('header', 'windowname', $this->windowname);
  114.         $this->_form->addElement('static', 'progressBar');
  115.         $this->_form->addElement('static', 'progressStatus');
  116.  
  117.         $style = $this->isStarted() ? array('disabled'=>'true') : null;
  118.         
  119.         $buttons[] =& $this->_form->createElement('submit', 'start',  $this->buttonStart, $style);
  120.         $buttons[] =& $this->_form->createElement('submit', 'cancel', $this->buttonCancel);
  121.  
  122.         $buttons[0]->updateAttributes($buttonAttr);
  123.         $buttons[1]->updateAttributes($buttonAttr);
  124.         
  125.         $this->_form->addGroup($buttons, 'buttons', '', ' ', false);
  126.  
  127.         
  128.         // default embedded progress element with look-and-feel
  129.         $this->_progress = new HTML_Progress();
  130.         $this->setProgressElement($this->_progress);
  131.  
  132.         $str =& $this->_form->getElement('progressStatus');
  133.         $str->setText('<div id="status" class="progressStatus"> </div>');
  134.     }
  135.  
  136.     /**
  137.      * Attach a progress bar to this uploader.
  138.      *
  139.      * @param      object    $bar           a html_progress instance
  140.      *
  141.      * @return     void
  142.      * @since      1.1
  143.      * @access     public
  144.      * @throws     HTML_PROGRESS_ERROR_INVALID_INPUT
  145.      */
  146.     function setProgressElement(&$bar)
  147.     {
  148.         if (!is_a($bar, 'HTML_Progress')) {
  149.             return Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  150.                 array('var' => '$bar',
  151.                       'was' => gettype($bar),
  152.                       'expected' => 'HTML_Progress object',
  153.                       'paramnum' => 1), PEAR_ERROR_TRIGGER);
  154.         }
  155.         $this->_progress =& $bar;
  156.  
  157.         $this->_progress->setStringPainted(true);     // get space for the string
  158.         $this->_progress->setString("");              // but don't paint it
  159.         $this->_progress->setIndeterminate(true);
  160.  
  161.         $bar =& $this->_form->getElement('progressBar');
  162.         $bar->setText( $this->_progress->toHtml() );
  163.     }
  164.  
  165.     /**
  166.      * Returns TRUE if progress was started by user, FALSE otherwise.
  167.      *
  168.      * @return     bool
  169.      * @since      1.1
  170.      * @access     public
  171.      */
  172.     function isStarted()
  173.     {
  174.         $action = $this->_form->getSubmitValues();
  175.         return isset($action['start']);
  176.     }
  177.  
  178.     /**
  179.      * Returns TRUE if progress was canceled by user, FALSE otherwise.
  180.      *
  181.      * @return     bool
  182.      * @since      1.1
  183.      * @access     public
  184.      */
  185.     function isCanceled()
  186.     {
  187.         $action = $this->_form->getSubmitValues();
  188.         return isset($action['cancel']);
  189.     }
  190.  
  191.     /**
  192.      * Returns progress styles (StyleSheet).
  193.      *
  194.      * @return     string
  195.      * @since      1.1
  196.      * @access     public
  197.      */
  198.     function getStyle()
  199.     {
  200.         return $this->_progress->getStyle();
  201.     }
  202.  
  203.     /**
  204.      * Returns progress javascript.
  205.      *
  206.      * @return     string
  207.      * @since      1.1
  208.      * @access     public
  209.      */
  210.     function getScript()
  211.     {
  212.         $js = "
  213. function setStatus(pString)
  214. {
  215.         if (isDom)
  216.             prog = document.getElementById('status');
  217.         if (isIE)
  218.             prog = document.all['status'];
  219.         if (isNS4)
  220.             prog = document.layers['status'];
  221.     if (prog != null) 
  222.         prog.innerHTML = pString;
  223. }";
  224.         return $this->_progress->getScript() . $js;
  225.     }
  226.  
  227.     /**
  228.      * Returns Uploader forms as a Html string.
  229.      *
  230.      * @return     string
  231.      * @since      1.1
  232.      * @access     public
  233.      */
  234.     function toHtml()
  235.     {
  236.         return $this->_form->toHtml();
  237.     }
  238.  
  239.     /**
  240.      * Accepts a renderer
  241.      *
  242.      * @param      object    $renderer      An HTML_QuickForm_Renderer object
  243.      *
  244.      * @return     void
  245.      * @since      1.1
  246.      * @access     public
  247.      */
  248.     function accept(&$renderer)
  249.     {
  250.         if (!is_a($renderer, 'HTML_QuickForm_Renderer')) {
  251.             return Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  252.                 array('var' => '$renderer',
  253.                       'was' => gettype($renderer),
  254.                       'expected' => 'HTML_QuickForm_Renderer object',
  255.                       'paramnum' => 1), PEAR_ERROR_TRIGGER);
  256.         }
  257.         $this->_form->accept($renderer);
  258.     }
  259.  
  260.     /**
  261.      * Uploads the files asynchronously, so the class can perform other operations 
  262.      * while files are being uploaded, such :
  263.      * display a progress bar in indeterminate mode. 
  264.      *
  265.      * @param      string    $dest          Changes from current to the specified directory.
  266.      * @param      boolean   $overwrite     (optional) overwrite existing files.
  267.      *
  268.      * @return     mixed                    a null array if all files transfered
  269.      * @since      1.1
  270.      * @access     public
  271.      * @see        FTP_Upload::setFiles()
  272.      */
  273.     function moveTo($dest, $overwrite = false)
  274.     {
  275.         if (!is_string($dest)) {
  276.             Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  277.                 array('var' => '$dest',
  278.                       'was' => gettype($dest),
  279.                       'expected' => 'string',
  280.                       'paramnum' => 1), PEAR_ERROR_TRIGGER);
  281.  
  282.         } elseif (!is_bool($overwrite)) {
  283.             Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  284.                 array('var' => '$overwrite',
  285.                       'was' => gettype($overwrite),
  286.                       'expected' => 'boolean',
  287.                       'paramnum' => 2), PEAR_ERROR_TRIGGER);
  288.         }
  289.  
  290.         $dir = parent::_changeDir($dest);
  291.         if (PEAR::isError($dir)) {
  292.             return $dir;
  293.         }
  294.         $remoteFiles = ftp_nlist($this->_conn, '.');
  295.         if ($remoteFiles === false) {
  296.             return PEAR::raiseError('Couldn\'t read directory ' . $dest); 
  297.         }
  298.         
  299.         $nomove = array();   // files not transfered on remote host
  300.         
  301.         foreach ($this->_files as $file) {
  302.             if (!$overwrite && in_array(basename($file), $remoteFiles)) {
  303.                 // file already exists, skip to next one
  304.                 continue;
  305.             }
  306.  
  307.             // writes file caption
  308.             $status  = ob_get_clean();
  309.             $status  = '<script type="text/javascript">self.setStatus(\'';
  310.             $status .= sprintf($this->captionMask, basename($file));
  311.             $status .= '\'); </script>';
  312.             echo $status;
  313.             ob_start();
  314.  
  315.             $ret = ftp_nb_put($this->_conn, basename($file), $file, FTP_BINARY);
  316.  
  317.             while ($ret == FTP_MOREDATA) {
  318.   
  319.                 $this->_progress->display();
  320.                 // sleep a bit ...
  321.                 for ($i=0; $i<($this->_progress->_anim_speed*1000); $i++) { }
  322.                  
  323.                 if ($this->_progress->getPercentComplete() == 1) {
  324.                     $this->_progress->setValue(0);
  325.                 } else {
  326.                     $this->_progress->incValue();
  327.                 }
  328.  
  329.                 // upload Continue ...
  330.                 $ret = ftp_nb_continue($this->_conn);
  331.             }
  332.             if ($ret != FTP_FINISHED) {
  333.                 $nomove[] = $file;
  334.             }
  335.         }
  336.         return $nomove;
  337.     }
  338. }
  339. ?>