Ftp.class.php

gehe zur Dokumentation dieser Datei
00001 <?php
00002 #
00003 #  DaCMS Content Management System
00004 #  Copyright (C) 2002 Jan Dankert, jandankert@jandankert.de
00005 #
00006 #  This program is free software; you can redistribute it and/or
00007 #  modify it under the terms of the GNU General Public License
00008 #  as published by the Free Software Foundation; either version 2
00009 #  of the License, or (at your option) any later version.
00010 #
00011 #  This program is distributed in the hope that it will be useful,
00012 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 #  GNU General Public License for more details.
00015 #
00016 #  You should have received a copy of the GNU General Public License
00017 #  along with this program; if not, write to the Free Software
00018 #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 #
00020 
00030 class Ftp
00031 {
00032      var $verb;
00033      var $url;
00034      var $log = array();
00035      var $mode=FTP_ASCII;
00036      var $passive = false;
00037      
00038      var $ok    = true;
00039      
00040 
00041      // Konstruktor
00042      function Ftp( $url )
00043      {
00044           $this->connect( $url );
00045      }
00046 
00047      
00048      // Aufbauen der Verbindung
00049      function connect( $url )
00050      {
00051           $this->url = $url;
00052           
00053           global $conf;
00054      
00055           $conf_ftp = $conf['publish']['ftp'];
00056           $ftp = parse_url( $this->url );
00057           
00058           // Die projektspezifischen Werte gewinnen bei �berschneidungen mit den Default-Werten
00059           $ftp = array_merge($conf_ftp,$ftp);
00060      
00061           // Nur FTP und FTPS (seit PHP 4.3) erlaubt
00062           if   ( !ereg('^ftps?$',$ftp['scheme']) )
00063           {
00064                $this->log[] = 'Unknown scheme in FTP Url: '.$ftp['scheme'];
00065                $this->log[] = 'Only FTP (and FTPS, if compiled in) are supported';
00066                $this->ok  = false;
00067                return;
00068           }
00069           
00070           if   ( function_exists('ftp_ssl_connect') && $ftp['scheme'] == 'ftps' )
00071                $this->verb = @ftp_ssl_connect( $ftp['host'],$ftp['port'] );
00072           else
00073                $this->verb = @ftp_connect( $ftp['host'],$ftp['port'] );
00074 
00075           if   ( !$this->verb )
00076           {
00077                $this->log[] = 'Cannot connect to '.$ftp['scheme'].'-server: '.$ftp['host'].':'.$ftp['port'];
00078                $this->ok = false;
00079                
00080                Logger::error('Cannot connect to '.$ftp['host'].':'.$ftp['port']);
00081                return;
00082           }
00083 
00084           $this->log[] = 'Connected to FTP server '.$ftp['host'].':'.$ftp['port'];
00085           
00086           if   ( empty($ftp['user']) )
00087                $ftp['user'] = 'anonymous';
00088                
00089           if   ( ! ftp_login( $this->verb,$ftp['user'],$ftp['pass'] ) )
00090           {
00091                $this->log[] = 'Unable to login as user '.$ftp['user'];
00092                $this->ok = false;
00093                return;
00094           }
00095           
00096           $this->log[] = 'Logged in as user '.$ftp['user'];
00097 
00098           $pasv = (!empty($ftp['fragment']) && $ftp['fragment'] == 'passive' );
00099           
00100           $this->log[] = 'entering passive mode '.($pasv?'on':'off');
00101           if   ( ! ftp_pasv($this->verb,true) )
00102           {
00103                $this->log[] = 'cannot switch PASV mode';
00104                $this->ok = false;
00105                return;
00106           }
00107           
00108           if   ( !empty($ftp['query']) )
00109           {
00110                parse_str( $ftp['query'],$ftp_var );
00111                
00112                if   ( isset( $ftp_var['site'] ) )
00113                {
00114                     $site_commands = explode( ',',$ftp_var['site'] );
00115                     foreach( $site_commands as $cmd )
00116                     {
00117                          $this->log .= 'executing SITE command: '.$cmd;
00118 
00119                          if   ( ! @ftp_site( $this->verb,$cmd ) )
00120                          {
00121                               $this->log[] = 'unable to do SITE command: '.$cmd;
00122                               $this->ok = false;
00123                               return;
00124                          }
00125                     }
00126                }
00127           }
00128 
00129           $this->path = ereg_replace( '\/$','',$ftp['path']);
00130           
00131           $this->log[] = 'Changing directory to '.$this->path;
00132           
00133           if   ( ! @ftp_chdir( $this->verb,$this->path ) )
00134           {
00135                $this->log[] = 'unable CHDIR to directory: '.$this->path;
00136                $this->ok = false;
00137                return;
00138           }
00139      }
00140      
00141 
00149      function put( $source,$dest,$mode=FTP_BINARY )
00150      {
00151           if   ( ! $this->ok )
00152                return;
00153                
00154           $ftp = parse_url( $this->url );
00155 
00156           $dest = $this->path.'/'.$dest;
00157           
00158           $this->log .= "Copying file: $source -&gt; $dest ...\n";
00159 
00160           if   ( !@ftp_put( $this->verb,$dest,$source,$this->mode ) )
00161           {
00162                if   ( !$this->mkdirs( dirname($dest) ) )
00163                     return; // Fehler.
00164 
00165                ftp_chdir( $this->verb,$this->path );
00166 
00167                if   ( ! @ftp_put( $this->verb,$dest,$source,$mode ) )
00168                {
00169                     $this->ok = false;
00170                     $this->log[] = 'FTP PUT failed...';
00171                     $this->log[] = 'source     : '.$source;
00172                     $this->log[] = 'destination: '.$dest;
00173                     return;
00174                }
00175                
00176           }
00177      }
00178 
00179 
00180 
00187      function mkdirs( $strPath )
00188      {
00189           if   ( @ftp_chdir($this->verb,$strPath) )
00190                return true; // Verzeichnis existiert schon :)
00191       
00192           $pStrPath = dirname($strPath);
00193           
00194           if   ( !$this->mkdirs($pStrPath) )
00195                return false;
00196           
00197           if   ( ! @ftp_mkdir($this->verb,$strPath) )
00198           {
00199                $this->ok = false;
00200                $this->log[] = "failed to create remote directory: $strPath";
00201           }
00202           
00203           return $this->ok;
00204      }
00205      
00206      
00207      
00212      function close()
00213      {
00214           if   ( !$this->ok ) // Noch alles ok?
00215                return;
00216                
00217           if   ( ! @ftp_quit( $this->verb ) )
00218           {
00219                // Das Schlie�en der Verbindung hat nicht funktioniert.
00220                // Eigentlich k�nnten wir das ignorieren, aber wir sind anst�ndig und melden eine Fehler.
00221                $this->log[] = 'failed to close connection';
00222                $this->ok = false;
00223                return;
00224           }
00225      }
00226 }
00227 
00228 
00229 ?>

Erzeugt am Thu May 14 00:55:49 2009 für OpenRat von  doxygen 1.5.8