File "class.ftphp.inc"

Full Path: /home/analogde/www/FTP/Code03/inc/class.ftphp.inc
File size: 5.75 KB
MIME-type: text/x-php
Charset: utf-8

<?php
//
// $Id: class.ftphp.inc,v1.0 2004-12-18 12:46a EST Onion Exp $
//

/*
 * FTPhp Class
 *
 * A simple wrapper for stuffs.
 */
class FTPhp {

 /*
  * Public Property
  */
 public $current_directory = '';

 /*
  * Static Property
  */
 //
 // God damn it, why can't everyone just conform to a standard? ~_~
 // This bit ripped off from the Filezilla 2.2.9 source (FtpListResult.cpp to be exact).
 //
 private static $file_list_patterns = array (
                                             // common UNIX formats
                                             //permissions        ??   owner group  filesize              last modified date                                      filename
                                      /*0*/  '#([\-dlwrx]{10})\s+\d+\s+(.+)\s+(.+)\s+(\d+)\s+((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d+\s+[\d\:]+)\s+(.+)\r\n#Ui',
                                      /*1*/  '#([\-dlwrx]{10})\s+\d+\s+(.+)\s+(.+)\s+(\d+)\s+([\d\-\:]+\s+[\d\-\:]+)\s+(.+)\r\n#Ui',
                                             // netware
                                             //perms?     wtf?             owner  size         last modified date                                               filename
                                      /*2*/  '#([\-dl])\s+(\[[\w\-]+)\])\s+(.+)\s+(\d+)\s+((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d+\s+[\d\:]+)\s+(.+)#Ui',
                                             // MSDOS
                                             // last modified   directory or filesize   filename
                                      /*3*/  '#([\d\-]+\s+[\w\:]+)\s+(\<DIR\>|\d+)\s+(.+)#Ui'
                                            );

 /*
  * Private Properties
  */
 private $ftp_obj;
 private $options = array ();
 
 /*
  * Constructor
  *
  * void __construct ( string host, int port, string user, string pass )
  */
 public function __construct ( $host, $port, $user, $pass ) {
  $this->options = parse_ini_file ('./inc/options.ini');

  $this->ftp_obj = new FTP ($host, $port, $user, $pass, $this->options['show_pass']);
  if (!$this->ftp_obj->is_ok ()) {
   self::trigger_error ($this->ftp_obj->get_last_response_str ());
   exit;
  }
  
  $this->current_directory = $this->ftp_obj->pwd ();
 }

 /*
  * Static Method
  */
 /*
  * FTPhp::trigger_error ()
  *
  * Displays a formatted error.
  *
  * void trigger_error ( [ string error ] )
  */
 public static function trigger_error ( $error = '' ) {
  echo '  <div class="error"><strong>Error:</strong> ', htmlentities ($error), '</div>',"\n";
 }

 /*
  * Public Methods
  */
 /*
  * FTPhp::perform_action ()
  *
  * Invokes a method from the FTP object.
  *
  * void perform_action ( callback action, string arg )
  */
 public function perform_action ( $action, $args ) {
  call_user_func_array (array (&$this->ftp_obj, $action), $args) or self::trigger_error ($this->ftp_obj->get_last_response_str ());
  $this->current_directory = $this->ftp_obj->pwd ();
 }

 /*
  * FTPhp::download ()
  *
  * Downloads a file.
  *
  * void download ( string filename )
  */
 public function download ( $filename ) {
  $file_contents = $this->ftp_obj->retr ($filename);
  if ($file_contents === false) {
   self::trigger_error ($this->ftp_obj->get_last_response_str ());
  } else {
   ob_clean ();
   echo $file_contents;
   $dots = explode ('.', $filename);
   header ('Content-Type: '.(is_array ($dots) ? get_type_by_extension (end ($dots)) : 'application/octet-stream'));
   if ($this->options['download_as'] == 'attachment') {
    header ('Content-Disposition: attachment; filename="'.$filename.'"');
   }
   header ('Content-Length: '.strlen (ob_get_contents ()));
   exit;
  }
 }

 /*
  * FTPhp::upload ()
  *
  * Uploads a file.
  *
  * void upload ( string local_filename, string remote_filename )
  */
 public function upload ( $local_filename, $remote_filename ) {
  if (file_exists ($local_filename)) {
   $this->ftp_obj->stor ($local_filename, $remote_filename) or self::trigger_error ($this->ftp_obj->get_last_response_str ());
  } else {
   self::trigger_error ('"'.$local_filename.'" doesn\'t exist.');
  }
 }

 /*
  * FTPhp::file_list ()
  *
  * Returns the current directory's file listing as an array.
  *
  * array file_list ( void )
  */
 public function file_list ( ) {
  $list = $this->ftp_obj->ls ();
  if ($list === false) {
   self::trigger_error ($this->ftp_obj->get_last_response_str ());
  } else {
   foreach (self::$file_list_patterns as $id => $pattern) {
    if (preg_match_all ($pattern, $list, $matches_array, PREG_PATTERN_ORDER)) {
     switch ($id) {
      case 0:
      case 1:
       return array ('permissions'   => $matches_array[1],
                     'owner'         => $matches_array[2],
                     'group'         => $matches_array[3],
                     'filesize'      => $matches_array[4],
                     'last_modified' => $matches_array[5],
                     'filename'      => $matches_array[7]
                    );
      case 2:
       return array ('permissions'   => $matches_array[1],
                     'owner'         => $matches_array[3],
                     'filesize'      => $matches_array[4],
                     'last_modified' => $matches_array[5],
                     'filename'      => $matches_array[7]
                    );
      case 3:
       return array ('last_modified' => $matches_array[1],
                     'filesize'      => $matches_array[2],
                     'filename'      => $matches_array[3]
                    );
     }
    }
   }
   // debugger crap
   $debug = new Debugger (__FILE__, __CLASS__, end (explode ('::', __METHOD__)), __LINE__, 'thing to get parsed into array', 'that didn\'t happen', array ('this' => $this, 'file_list_patterns' => self::$file_list_patterns, 'list' => $list));
   foot ('<pre>'.htmlentities ($debug->__toString ()).'</pre>');
  }
 }
}