openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

LocalTarget.class.php (5626B)


      1 <?php
      2 // OpenRat Content Management System
      3 // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
      4 //
      5 // This program is free software; you can redistribute it and/or
      6 // modify it under the terms of the GNU General Public License
      7 // as published by the Free Software Foundation; either version 2
      8 // of the License, or (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU General Public License
     16 // along with this program; if not, write to the Free Software
     17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     18 namespace cms\generator\target;
     19 
     20 use cms\base\Configuration;
     21 use cms\base\Startup;
     22 use logger\Logger;
     23 use util\exception\PublisherException;
     24 use util\exception\UIException;
     25 use util\FileUtils;
     26 use util\Url;
     27 
     28 
     29 /**
     30  * Publishing to the local filesystem.
     31  *
     32  * @author Jan Dankert
     33  */
     34 class LocalTarget extends  BaseTarget
     35 {
     36 	/**
     37 	 * @var string
     38 	 */
     39 	private $localDestinationDirectory;
     40 
     41 	/**
     42 	 * @param $url Url
     43 	 */
     44 	public function open()
     45 	{
     46 		$fileSystemConfig = Configuration::subset(['publish','filesystem']);
     47 
     48 		$targetDir = rtrim( $this->url->path,'/' );
     49 
     50 		if	( FileUtils::isAbsolutePath($targetDir) && $fileSystemConfig->is('per_project',true ))
     51 		{
     52 			$this->localDestinationDirectory = FileUtils::toAbsolutePath([$targetDir]); // Projekteinstellung verwenden.
     53 		}
     54 		else
     55 		{
     56 			// Konfiguriertes Verzeichnis verwenden.
     57 			$this->localDestinationDirectory = FileUtils::toAbsolutePath([$fileSystemConfig->get('directory','/var/www'),$targetDir]);
     58 		}
     59 
     60 		// Sofort pruefen, ob das Zielverzeichnis ueberhaupt beschreibbar ist.
     61 		if   ( $this->localDestinationDirectory && $this->localDestinationDirectory[0] == '#')
     62 			$this->localDestinationDirectory = '';
     63 
     64 	}
     65 
     66 	/**
     67 	 * Copying a file to local filesystem.
     68 	 *
     69 	 * @param String Quelle
     70 	 * @param String Ziel
     71 	 * @throws PublisherException
     72 	 */
     73 	public function put($source, $dest, $lastChangeDate)
     74 	{
     75 		// Is the output directory existent?
     76 		if   ( !is_dir( $this->localDestinationDirectory ) )
     77 			if   ( ! @mkdir( $this->localDestinationDirectory ) )
     78 				throw new PublisherException('cannot create directory: ' . $this->localDestinationDirectory);
     79 
     80 		// Is the output directory writable?
     81 		if   ( !is_writeable( $this->localDestinationDirectory ) )
     82 			throw new PublisherException('directory not writable: ' . $this->localDestinationDirectory);
     83 
     84 
     85 		$dest   = $this->localDestinationDirectory.'/'.$dest;
     86 
     87 
     88 		// Is the destination writable?
     89 		if   ( is_file($dest) && !is_writeable( $dest ) )
     90 			throw new PublisherException('file not writable: ' . $dest);
     91 
     92 		// Copy file to destination
     93 		if   (!@copy( $source,$dest ));
     94 		{
     95 			// Create directories, if necessary.
     96 			$this->mkdirs( dirname($dest) );
     97 
     98 			if   (!@copy( $source,$dest ))
     99 				throw new PublisherException( 'failed copying local file:' . "\n" .
    100 					'source     : ' . $source . "\n" .
    101 					'destination: ' . $dest);
    102 
    103 			// Das Änderungsdatum der Datei auch in der Zieldatei setzen.
    104 			if  ( Configuration::subset('publish')->is('set_modification_date',false ) )
    105 				if	( ! is_null($lastChangeDate) )
    106 					@touch( $dest,$lastChangeDate );
    107 
    108 			Logger::debug("published: $dest");
    109 		}
    110 
    111 		$chmod = Configuration::subset('security')->get('chmod','');
    112 
    113 		if	( $chmod  )
    114 		{
    115 			// CHMOD auf der Datei ausfuehren.
    116 			if	( ! @chmod($dest,octdec($chmod) ) )
    117 				throw new PublisherException('Unable to CHMOD file ' . $dest);
    118 		}
    119 
    120 	}
    121 
    122 
    123 
    124 	/**
    125 	 * Rekursives Anlagen von Verzeichnisse
    126 	 * Nett gemacht.
    127 	 * Quelle: http://de3.php.net/manual/de/function.mkdir.php
    128 	 * Thx to acroyear at io dot com
    129 	 *
    130 	 * @param String Verzeichnis
    131 	 * @return boolean
    132 	 */
    133 	private function mkdirs($path )
    134 	{
    135 		if	( is_dir($path) )
    136 			return;  // Path exists
    137 
    138 		$parentPath = dirname($path);
    139 
    140 		$this->mkdirs($parentPath);
    141 
    142 		//
    143 		if	( ! @mkdir($path) )
    144 			throw new PublisherException( 'Cannot create directory: ' . $path);
    145 
    146 		// CHMOD auf dem Verzeichnis ausgef�hren.
    147 		$chmod = Configuration::subset('security')->get('chmod_dir','');
    148 
    149 		if	( $chmod  )
    150 		{
    151 			if	( ! @chmod($path,octdec($chmod) ) )
    152 				throw new PublisherException('Unable to CHMOD directory ' . $path);
    153 		}
    154 	}
    155 
    156 
    157 
    158 
    159 
    160 
    161 	/**
    162 	 * Aufraeumen des Zielverzeichnisses.<br><br>
    163 	 * Es wird der komplette Zielordner samt Unterverzeichnissen durchsucht. Jede
    164 	 * Datei, die laenger existiert als der aktuelle Request alt ist, wird geloescht.<br>
    165 	 * Natuerlich darf diese Funktion nur nach einem Gesamt-Veroeffentlichen ausgefuehrt werden.
    166 	 */
    167 	public function clean()
    168 	{
    169 		if	( !empty($this->localDestinationDirectory) )
    170 			$this->cleanFolder($this->localDestinationDirectory);
    171 	}
    172 
    173 
    174 
    175 	/**
    176 	 * Aufr�umen eines Verzeichnisses.<br><br>
    177 	 * Dateien, die l�nger existieren als der aktuelle Request alt ist, werden gel�scht.<br>
    178 	 *
    179 	 * @param String Verzeichnis
    180 	 */
    181 	private function cleanFolder( $folderName )
    182 	{
    183 		$dh = opendir( $folderName );
    184 
    185 		while( $file = readdir($dh) )
    186 		{
    187 			if	( $file != '.' && $file != '..')
    188 			{
    189 				$fullpath = $folderName.'/'.$file;
    190 
    191 				// Wenn eine Datei beschreibbar und entsprechend alt
    192 				// ist, dann entfernen
    193 				if	( is_file($fullpath)     &&
    194 					is_writable($fullpath) &&
    195 					filemtime($fullpath) < Startup::getStartTime()  )
    196 					unlink($fullpath);
    197 
    198 				// Bei Ordnern rekursiv absteigen
    199 				if	( is_dir( $fullpath) )
    200 				{
    201 					$this->cleanFolder($fullpath);
    202 					@rmdir($fullpath);
    203 				}
    204 			}
    205 		}
    206 	}
    207 
    208 }