openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

Local.class.php (5338B)


      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 Local 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 
     61 		// Sofort pruefen, ob das Zielverzeichnis ueberhaupt beschreibbar ist.
     62 		if   ( $this->localDestinationDirectory && $this->localDestinationDirectory[0] == '#')
     63 			$this->localDestinationDirectory = '';
     64 
     65 	}
     66 
     67 	/**
     68 	 * Copying a file to local filesystem.
     69 	 *
     70 	 * @param String Quelle
     71 	 * @param String Ziel
     72 	 */
     73 	public function put($source, $dest, $lastChangeDate)
     74 	{
     75 		// Is the output directory writable?
     76 		if   ( !is_writeable( $this->localDestinationDirectory ) )
     77 			throw new PublisherException('directory not writable: ' . $this->localDestinationDirectory);
     78 
     79 		$dest   = $this->localDestinationDirectory.'/'.$dest;
     80 
     81 		// Is the destination writable?
     82 		if   ( is_file($dest) && !is_writeable( $dest ) )
     83 			throw new PublisherException('file not writable: ' . $dest);
     84 
     85 		// Copy file to destination
     86 		if   (!@copy( $source,$dest ));
     87 		{
     88 			// Create directories, if necessary.
     89 			$this->mkdirs( dirname($dest) );
     90 
     91 			if   (!@copy( $source,$dest ))
     92 				throw new PublisherException( 'failed copying local file:' . "\n" .
     93 					'source     : ' . $source . "\n" .
     94 					'destination: ' . $dest);
     95 
     96 			// Das Änderungsdatum der Datei auch in der Zieldatei setzen.
     97 			if  ( Configuration::subset('publish')->is('set_modification_date',false ) )
     98 				if	( ! is_null($lastChangeDate) )
     99 					@touch( $dest,$lastChangeDate );
    100 
    101 			Logger::debug("published: $dest");
    102 		}
    103 
    104 		$chmod = Configuration::subset('security')->get('chmod','');
    105 
    106 		if	( $chmod  )
    107 		{
    108 			// CHMOD auf der Datei ausfuehren.
    109 			if	( ! @chmod($dest,octdec($chmod) ) )
    110 				throw new PublisherException('Unable to CHMOD file ' . $dest);
    111 		}
    112 
    113 	}
    114 
    115 
    116 
    117 	/**
    118 	 * Rekursives Anlagen von Verzeichnisse
    119 	 * Nett gemacht.
    120 	 * Quelle: http://de3.php.net/manual/de/function.mkdir.php
    121 	 * Thx to acroyear at io dot com
    122 	 *
    123 	 * @param String Verzeichnis
    124 	 * @return boolean
    125 	 */
    126 	private function mkdirs($path )
    127 	{
    128 		if	( is_dir($path) )
    129 			return;  // Path exists
    130 
    131 		$parentPath = dirname($path);
    132 
    133 		$this->mkdirs($parentPath);
    134 
    135 		//
    136 		if	( ! @mkdir($path) )
    137 			throw new PublisherException( 'Cannot create directory: ' . $path);
    138 
    139 		// CHMOD auf dem Verzeichnis ausgef�hren.
    140 		$chmod = Configuration::subset('security')->get('chmod_dir','');
    141 
    142 		if	( $chmod  )
    143 		{
    144 			if	( ! @chmod($path,octdec($chmod) ) )
    145 				throw new PublisherException('Unable to CHMOD directory ' . $path);
    146 		}
    147 	}
    148 
    149 
    150 
    151 
    152 
    153 
    154 	/**
    155 	 * Aufraeumen des Zielverzeichnisses.<br><br>
    156 	 * Es wird der komplette Zielordner samt Unterverzeichnissen durchsucht. Jede
    157 	 * Datei, die laenger existiert als der aktuelle Request alt ist, wird geloescht.<br>
    158 	 * Natuerlich darf diese Funktion nur nach einem Gesamt-Veroeffentlichen ausgefuehrt werden.
    159 	 */
    160 	public function clean()
    161 	{
    162 		if	( !empty($this->localDestinationDirectory) )
    163 			$this->cleanFolder($this->localDestinationDirectory);
    164 	}
    165 
    166 
    167 
    168 	/**
    169 	 * Aufr�umen eines Verzeichnisses.<br><br>
    170 	 * Dateien, die l�nger existieren als der aktuelle Request alt ist, werden gel�scht.<br>
    171 	 *
    172 	 * @param String Verzeichnis
    173 	 */
    174 	private function cleanFolder( $folderName )
    175 	{
    176 		$dh = opendir( $folderName );
    177 
    178 		while( $file = readdir($dh) )
    179 		{
    180 			if	( $file != '.' && $file != '..')
    181 			{
    182 				$fullpath = $folderName.'/'.$file;
    183 
    184 				// Wenn eine Datei beschreibbar und entsprechend alt
    185 				// ist, dann entfernen
    186 				if	( is_file($fullpath)     &&
    187 					is_writable($fullpath) &&
    188 					filemtime($fullpath) < Startup::getStartTime()  )
    189 					unlink($fullpath);
    190 
    191 				// Bei Ordnern rekursiv absteigen
    192 				if	( is_dir( $fullpath) )
    193 				{
    194 					$this->cleanFolder($fullpath);
    195 					@rmdir($fullpath);
    196 				}
    197 			}
    198 		}
    199 	}
    200 
    201 }