Publish.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 
00034 class Publish
00035 {
00036      var $ftp;
00037      var $with_local          = false;
00038      var $with_ftp            = false;
00039      var $local_destdir       = '';
00040      var $content_negotiation = false;
00041      var $cut_index           = false;
00042      var $cmd_after_publish   = '';
00043      var $publishedObjects    = array();
00044      var $log                 = array();
00045      var $ok                  = true;
00046 
00054      function Publish()
00055      {
00056           global $conf;
00057           $conf_project = $conf['publish']['project']; 
00058           
00059           $project = Session::getProject();
00060 
00061           // Feststellen, ob FTP benutzt wird.
00062           // Dazu muss FTP aktiviert sein (enable=true) und eine URL vorhanden sein.
00063           if   ( @$conf['publish']['ftp']['enable'] && 
00064                   ( !empty($project->ftp_url) ||
00065                     isset($conf['publish']['ftp']['host']) ) )
00066           {
00067                $this->with_ftp = true;
00068                $this->ftp = new Ftp( $project->ftp_url ); // Aufbauen einer FTP-Verbindung
00069                
00070                if   ( ! $this->ftp->ok ) // FTP-Verbindung ok?
00071                {
00072                     $this->ok = false;
00073                     $this->log = $this->ftp->log;
00074                     return; // Ende. Ohne FTP brauchen wir nicht weitermachen.
00075                }
00076 
00077                $this->ftp->passive = ( $project->ftp_passive == '1' );
00078           }
00079           
00080           $localDir = ereg_replace( '\/$','',$project->target_dir);
00081           if   ( empty( $localDir))
00082                $localDir = $project->name;
00083                
00084           if   ( $conf_project['override_publish_dir'] && $localDir != basename($localDir) )
00085                $this->local_destdir = $localDir;
00086           else
00087                $this->local_destdir = $conf_project['publish_dir'].$localDir;
00088                
00089 
00090           // Sofort pruefen, ob das Zielverzeichnis ueberhaupt beschreibbar ist.
00091           if   ( $this->local_destdir != '' )
00092           {
00093                if   ( !is_writeable( $this->local_destdir ) )
00094                {
00095                     $this->ok = false;
00096                     $this->log[] = 'directory not writable: '.$this->local_destdir;
00097                     $this->log[] = 'please correct the file permissions.';
00098                     return;
00099                }
00100 
00101                $this->with_local = true;
00102           }
00103           
00104           $this->content_negotiation = ( $project->content_negotiation == '1' );
00105           $this->cut_index           = ( $project->cut_index           == '1' );
00106 
00107           if   ( $conf_project['override_system_command'] && !empty($project->cmd_after_publish) )
00108                $this->cmd_after_publish   = $project->cmd_after_publish;
00109           else
00110                $this->cmd_after_publish   = $conf_project['system_command'];
00111           
00112           // Im Systemkommando Variablen ersetzen
00113           $this->cmd_after_publish = str_replace('{name}'   ,$project->name                ,$this->cmd_after_publish);
00114           $this->cmd_after_publish = str_replace('{dir}'    ,$this->local_destdir          ,$this->cmd_after_publish);
00115           $this->cmd_after_publish = str_replace('{dirbase}',basename($this->local_destdir),$this->cmd_after_publish);
00116      }
00117 
00118      
00119      
00127      function copy( $tmp_filename,$dest_filename )
00128      {
00129           if   ( !$this->ok)
00130                return;
00131                     
00132           global $conf;
00133           $source = $tmp_filename;
00134 
00135           if   ( $this->with_local )
00136           {
00137                $dest   = $this->local_destdir.'/'.$dest_filename;
00138                
00139                // Nicht kopieren, wenn
00140                // - Quelldatei nicht neuer als die Zieldatei
00141                if   ( is_file($dest) &&
00142                       filemtime($source) <= filemtime($dest)   )
00143                       return;
00144                 
00145                if   (!@copy( $source,$dest ));
00146                {
00147                     if   ( ! $this->mkdirs( dirname($dest) ) )
00148                          return;
00149           
00150                     if   (!@copy( $source,$dest ))
00151                     {
00152                          $this->ok = false;
00153                          $this->log[] = 'failed copying local file:';
00154                          $this->log[] = 'source     : '.$source;
00155                          $this->log[] = 'destination: '.$dest;
00156                          return;
00157                     }
00158                }
00159                
00160                if   (!empty($conf['security']['chmod']))
00161                {
00162                     // CHMOD auf der Datei ausgeführen.
00163                     if   ( ! @chmod($dest,octdec($conf['security']['chmod'])) )
00164                     {
00165                          $this->ok = false;
00166                          $this->log[] = 'Unable to CHMOD file '.$dest;
00167                          return;
00168                     }
00169                }
00170           }
00171           
00172           if   ( $this->with_ftp ) // Falls FTP aktiviert
00173           {
00174                $dest = $dest_filename;
00175                $this->ftp->put( $source,$dest,FTP_ASCII );
00176 
00177                if   ( ! $this->ftp->ok )
00178                {
00179                     $this->ok = false;
00180                     $this->log[] = $this->ftp->log;
00181                }
00182           }
00183      }
00184      
00185      
00186 
00196      function mkdirs( $strPath )
00197      {
00198           global $conf;
00199           
00200           if   ( is_dir($strPath) )
00201                return true;
00202       
00203           $pStrPath = dirname($strPath);
00204           if   ( !$this->mkdirs($pStrPath) )
00205                return false;
00206           
00207           if   ( ! @mkdir($strPath,0777) )
00208           {
00209                $this->ok = false;
00210                $this->log[] = 'Cannot create directory: '.$strPath;
00211                return false;
00212           }
00213 
00214           // CHMOD auf dem Verzeichnis ausgeführen.
00215           if   (!empty($conf['security']['chmod_dir']))
00216           {
00217                if   ( ! @chmod($strPath,octdec($conf['security']['chmod_dir'])) )
00218                {
00219                     $this->ok = false;
00220                     $this->log[] = 'Unable to CHMOD directory: '.$strPath;
00221                     return false;
00222                }
00223           }
00224           
00225           
00226           return $this->ok;
00227      }
00228 
00229 
00230 
00236      function close()
00237      {
00238           if   ( $this->with_ftp )
00239           {
00240                $this->ftp->close();
00241           }
00242 
00243           // Ausführen des Systemkommandos.
00244           if   ( !empty($this->cmd_after_publish) && $this->ok )
00245           {
00246                $ausgabe = array();
00247                $rc      = false;
00248                exec( $this->cmd_after_publish,$ausgabe,$rc );
00249                
00250                if   ( $rc != 0 ) // Wenn Returncode ungleich 0, dann Ausgabe ins Log schreiben und Fehler melden.
00251                {
00252                     $this->log   = $ausgabe; 
00253                     $this->log[] = 'OpenRat: System command failed - returncode is '.$rc; 
00254                     $this->ok = false;
00255                }
00256           }
00257      }
00258      
00259      
00260      
00267      function clean()
00268      {
00269           if   ( $this->ok )
00270                return;
00271                
00272           if   ( !empty($this->local_destdir) )
00273                $this->cleanFolder($this->local_destdir);
00274      } 
00275 
00276      
00277 
00284      function cleanFolder( $folderName )
00285      {
00286           $dh = opendir( $folderName );
00287 
00288           while( $file = readdir($dh) )
00289           {
00290                if   ( $file != '.' && $file != '..')
00291                {
00292                     $fullpath = $folderName.'/'.$file;
00293 
00294                     // Wenn eine Datei beschreibbar und entsprechend alt
00295                     // ist, dann entfernen
00296                     if   ( is_file($fullpath)     &&
00297                            is_writable($fullpath) &&
00298                            filemtime($fullpath) < START_TIME  )
00299                          unlink($fullpath);
00300 
00301                     // Bei Ordnern rekursiv absteigen                 
00302                     if   ( is_dir( $fullpath) )
00303                     {
00304                          $this->cleanFolder($fullpath);
00305                          @rmdir($fullpath);
00306                     }
00307                }
00308           }
00309      }
00310 
00311 }
00312 
00313 ?>

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