openrat-cms

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

FileAction.class.php (16344B)


      1 <?php
      2 
      3 namespace cms\action;
      4 
      5 use cms\model\Folder;
      6 use cms\model\BaseObject;
      7 use cms\model\File;
      8 
      9 use cms\publish\PublishPreview;
     10 use cms\publish\PublishPublic;
     11 use Http;
     12 use \Html;
     13 use Upload;
     14 use ValidationException;
     15 
     16 // OpenRat Content Management System
     17 // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
     18 //
     19 // This program is free software; you can redistribute it and/or
     20 // modify it under the terms of the GNU General Public License
     21 // as published by the Free Software Foundation; either version 2
     22 // of the License, or (at your option) any later version.
     23 //
     24 // This program is distributed in the hope that it will be useful,
     25 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     26 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     27 // GNU General Public License for more details.
     28 //
     29 // You should have received a copy of the GNU General Public License
     30 // along with this program; if not, write to the Free Software
     31 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     32 
     33 
     34 /**
     35  * Action-Klasse zum Bearbeiten einer Datei
     36  *
     37  * @author Jan Dankert
     38  */
     39 class FileAction extends ObjectAction
     40 {
     41 	public $security = Action::SECURITY_USER;
     42 
     43     /**
     44      * @var File
     45      */
     46 	private $file;
     47 
     48 	/**
     49 	 * Konstruktor
     50 	 */
     51 	function __construct()
     52     {
     53         parent::__construct();
     54     }
     55 
     56 
     57     public function init()
     58     {
     59 		$file = new File( $this->getRequestId() );
     60 		$file->languageid = $this->getRequestVar(REQ_PARAM_LANGUAGE_ID);
     61 		$file->load();
     62 
     63         $this->setBaseObject( $file );
     64 	}
     65 
     66 
     67 	protected function setBaseObject( $file ) {
     68 		$this->file = $file;
     69 
     70 		parent::setBaseObject( $file );
     71 	}
     72 
     73 
     74 	/**
     75 	 * Ersetzt den Inhalt der Datei.
     76 	 */
     77 	public function editPost()
     78 	{
     79 		$upload = new Upload();
     80 
     81 		if   ( $upload->isAvailable() )
     82         {
     83             // File received as attachement.
     84             try
     85             {
     86                 $upload->processUpload();
     87             }
     88             catch( \Exception $e )
     89             {
     90                 throw $e;
     91             }
     92 
     93             $this->file->filename  = $upload->filename;
     94             $this->file->extension = $upload->extension;
     95             $this->file->size      = $upload->size;
     96             $this->file->save();
     97 
     98             $this->file->value = $upload->value;
     99             $this->file->saveValue();
    100         }
    101 		elseif( $this->hasRequestVar('value') )
    102         {
    103             // File value received
    104             $this->file->value = $this->getRequestVar('value');
    105 
    106             if   ( strtolower($this->getRequestVar('encoding')) == 'base64')
    107                 // file value is base64-encoded
    108                 $this->file->value = base64_decode($this->file->value);
    109 
    110             $this->file->saveValue();
    111         }
    112         else
    113         {
    114             // No file received.
    115             throw new ValidationException('value');
    116         }
    117 
    118         $this->file->setTimestamp();
    119 
    120 		$this->addNotice($this->file->getType(),$this->file->filename,'VALUE_SAVED','ok');
    121 	}
    122 
    123 
    124     /**
    125      * Abspeichern der Eigenschaften zu dieser Datei.
    126      *
    127      */
    128     function advancedPost()
    129     {
    130         $this->file->extension = $this->getRequestVar('extension'  ,OR_FILTER_FILENAME);
    131 
    132 		$typeid = $this->getRequestVar('type',OR_FILTER_NUMBER  );
    133 
    134 		if   ( ! in_array($typeid,[BaseObject::TYPEID_FILE,BaseObject::TYPEID_IMAGE,BaseObject::TYPEID_TEXT]))
    135 			throw new ValidationException('type');
    136 
    137         $this->file->typeid = $typeid;
    138         $this->file->updateType();
    139         $this->file->save();
    140 
    141         $this->addNotice($this->file->getType(),$this->file->filename,'PROP_SAVED','ok');
    142     }
    143 
    144 
    145 
    146     /**
    147 	 * Anzeigen des Inhaltes, der Inhalt wird samt Header direkt
    148 	 * auf die Standardausgabe geschrieben
    149 	 */
    150 	function previewView()
    151 	{
    152 	    $this->file->publisher = new PublishPreview();
    153 		$url = Html::url($this->file->getType(),'show',$this->file->objectid,array('target'=>'none') );
    154 		$this->setTemplateVar('preview_url',$url );
    155 	}
    156 
    157 
    158 	/**
    159 	 * Anzeigen des Inhaltes, der Inhalt wird samt Header direkt
    160 	 * auf die Standardausgabe geschrieben
    161 	 */
    162 	function showView()
    163 	{
    164 	    $this->file->publisher = new PublishPreview();
    165 		$this->lastModified( $this->file->lastchangeDate );
    166 
    167 		if	( $this->file->extension == 'gz' )
    168 		{
    169 			global $conf;
    170 			$mime_types = $conf['mime-types'];
    171 
    172 			$pos = strrpos($this->file->filename,'.');
    173 			if	( $pos === false )
    174 				$ext = '';
    175 			else
    176 				$ext = substr($this->file->filename,$pos+1);
    177 
    178 			$ext = strtolower($ext);
    179 
    180 			if	( !empty($mime_types[$ext]) )
    181 				$mime_type = $mime_types[$ext];
    182 			else
    183 				// Wenn kein Mime-Type gefunden, dann Standardwert setzen
    184 				$mime_type = OR_FILE_DEFAULT_MIMETYPE;
    185 
    186 			header('Content-Type: '.$mime_type );
    187 			header('Content-Encoding: gzip' );
    188 		}
    189 		else
    190 		{
    191 			// Angabe Content-Type
    192 			header('Content-Type: '.$this->file->mimeType() );
    193 		}
    194 
    195 		header('X-File-Id: '   .$this->file->fileid     );
    196 		header('X-Id: '        .$this->file->id         );
    197 
    198 		// Angabe Content-Disposition
    199 		// - Bild soll "inline" gezeigt werden
    200 		// - Dateiname wird benutzt, wenn der Browser das Bild speichern moechte
    201 		header('Content-Disposition: inline; filename='.$this->file->filename() );
    202 		header('Content-Transfer-Encoding: binary' );
    203 		header('Content-Description: '.$this->file->filename() );
    204 
    205 		$this->file->write(); // Bild aus Datenbank laden
    206 
    207 		// Groesse des Bildes in Bytes
    208 		// Der Browser hat so die Moeglichkeit, einen Fortschrittsbalken zu zeigen
    209 		header('Content-Length: '.filesize($this->file->getCache()->getFilename()) );
    210 
    211 
    212 		if	( $this->request->getRequestVar('encoding') == 'base64')
    213 		{
    214 		    $encodingFunction = function($value) {
    215 		        return base64_encode($value);
    216             };
    217 			$this->setTemplateVar('encoding', 'base64');
    218 		}
    219 		else {
    220             $encodingFunction = function($value) {
    221                 return $value;
    222             };
    223             $this->setTemplateVar('encoding', 'none');
    224         }
    225 
    226 
    227 		// Unterscheidung, ob PHP-Code in der Datei ausgefuehrt werden soll.
    228         $phpActive = ( config('publish','enable_php_in_file_content')=='auto' && $this->file->getRealExtension()=='php') ||
    229             config('publish','enable_php_in_file_content')===true;
    230 
    231         if	(  $phpActive ) {
    232 
    233             // PHP-Code ausfuehren
    234             ob_start();
    235             require( $this->file->getCache()->getFilename() );
    236             $this->setTemplateVar('value',$encodingFunction(ob_get_contents()) );
    237             ob_end_clean();
    238         }
    239         else
    240             $this->setTemplateVar('value',$encodingFunction(file_get_contents( $this->file->getCache()->getFilename()) ) );
    241         // Maybe we want some gzip-encoding?
    242 	}
    243 
    244 
    245 
    246 
    247 	public function advancedView()
    248 	{
    249 		// Eigenschaften der Datei uebertragen
    250 		$this->setTemplateVar( 'extension',$this->file->extension );
    251 		$this->setTemplateVar( 'type'     ,$this->file->type      );
    252 		$this->setTemplateVar( 'types'    ,[
    253 			BaseObject::TYPEID_FILE  => lang('file' ),
    254 			BaseObject::TYPEID_IMAGE => lang('image'),
    255 			BaseObject::TYPEID_TEXT  => lang('text' )
    256 		] );
    257 	}
    258 
    259 
    260 
    261 
    262 	/**
    263 	 * Anzeigen des Inhaltes
    264 	 */
    265 	function editView()
    266 	{
    267 		global $conf;
    268 		// MIME-Types aus Datei lesen
    269 		$this->setTemplateVars( $this->file->getProperties() );
    270 	}
    271 
    272 
    273 	/**
    274 	 * Anzeigen des Inhaltes
    275 	 */
    276 	function upload()
    277 	{
    278 	}
    279 
    280 
    281 	/**
    282 	 * Anzeigen des Inhaltes
    283 	 */
    284 	function valueView()
    285 	{
    286 		global $conf;
    287 		// MIME-Types aus Datei lesen
    288 		$this->setTemplateVars( $this->file->getProperties() );
    289 		$this->setTemplateVar('value',$this->file->loadValue());
    290 	}
    291 
    292 
    293 	/**
    294 	 * Anzeigen des Inhaltes
    295 	 */
    296 	function extractView()
    297 	{
    298 		$this->setTemplateVars( $this->file->getProperties() );
    299 	}
    300 
    301 
    302 	/**
    303 	 * Anzeigen des Inhaltes
    304 	 */
    305 	function uncompressView()
    306 	{
    307 	}
    308 
    309 
    310 	/**
    311 	 * Anzeigen des Inhaltes
    312 	 */
    313 	function uncompressPost()
    314 	{
    315 		switch( $this->file->extension )
    316 		{
    317 			case 'gz':
    318 				if	( $this->getRequestVar('replace') )
    319 				{
    320 					if	( strcmp(substr($this->file->loadValue(),0,2),"\x1f\x8b"))
    321 					{
    322 						throw new \LogicException("Not GZIP format (See RFC 1952)");
    323 					}
    324 					$method = ord(substr($this->file->loadValue(),2,1));
    325 					if	( $method != 8 )
    326 					{
    327 						throw new \LogicException("Unknown GZIP method: $method");
    328 					}
    329 					$this->file->value = gzinflate( substr($this->file->loadValue(),10));
    330 					$this->file->parse_filename( $this->file->filename );
    331 					$this->file->save();
    332 					$this->file->saveValue();
    333 				}
    334 				else
    335 				{
    336 					$newFile = new File();
    337 					$newFile->name     = $this->file->name;
    338 					$newFile->parentid = $this->file->parentid;
    339 					$newFile->value    = gzinflate( substr($this->file->loadValue(),10));
    340 					$newFile->parse_filename( $this->file->filename );
    341 					$newFile->add();
    342 				}
    343 				
    344 				break;
    345 
    346 			case 'bz2':
    347 				if	( $this->getRequestVar('replace') )
    348 				{
    349 					$this->file->value = bzdecompress($this->file->loadValue());
    350 					$this->file->parse_filename( $this->file->filename );
    351 					$this->file->save();
    352 					$this->file->saveValue();
    353 				}
    354 				else
    355 				{
    356 					$newFile = new File();
    357 					$newFile->name     = $this->file->name;
    358 					$newFile->parentid = $this->file->parentid;
    359 					$newFile->value    = bzdecompress( $this->file->loadValue() );
    360 					$newFile->parse_filename( $this->file->filename );
    361 					$newFile->add();
    362 				}
    363 				
    364 				break;
    365 
    366 			default:
    367 				throw new \OpenRatException( 'cannot uncompress file with extension: '.$this->file->extension );
    368 		}
    369 
    370 		$this->addNotice('file',$this->file->name,'DONE',OR_NOTICE_OK);
    371 		$this->callSubAction('edit');
    372 	}
    373 
    374 
    375 
    376 	/**
    377 	 * Anzeigen des Inhaltes
    378 	 */
    379 	function extractPost()
    380 	{
    381 		switch( $this->file->extension )
    382 		{
    383 			case 'tar':
    384 				$folder = new Folder();
    385 				$folder->parentid = $this->file->parentid;
    386 				$folder->name     = $this->file->name;
    387 				$folder->filename = $this->file->filename;
    388 				$folder->add();
    389 				
    390 				$tar = new ArchiveTar();
    391 				$tar->openTAR( $this->file->loadValue() );
    392 				
    393 				foreach( $tar->files as $file )
    394 				{
    395 					$newFile = new File();
    396 					$newFile->name     = $file['name'];
    397 					$newFile->parentid = $folder->objectid;
    398 					$newFile->value    = $file['file'];
    399 					$newFile->parse_filename( $file['name'] );
    400 					$newFile->lastchangeDate = $file['time'];
    401 					$newFile->add();
    402 					
    403 					$this->addNotice('file',$newFile->name,'ADDED');
    404 				}
    405 				
    406 				unset($tar);
    407 				
    408 				break;
    409 
    410 			case 'zip':
    411 			
    412 				$folder = new Folder();
    413 				$folder->parentid    = $this->file->parentid;
    414 				$folder->name        = $this->file->name;
    415 				$folder->filename    = $this->file->filename;
    416 				$folder->description = $this->file->fullFilename;
    417 				$folder->add();
    418 				
    419 				$zip = new ArchiveUnzip();
    420 				$zip->open( $this->file->loadValue() );
    421 
    422 				$lista = $zip->getList();
    423 
    424 				if(sizeof($lista)) foreach($lista as $fileName=>$trash){
    425 					
    426 
    427 					$newFile = new File();
    428 					$newFile->name        = basename($fileName);
    429 					$newFile->description = 'Extracted: '.$this->file->fullFilename.' -> '.$fileName;
    430 					$newFile->parentid    = $folder->objectid;
    431 					$newFile->parse_filename( basename($fileName) );
    432 
    433 					$newFile->value       = $zip->unzip($fileName);
    434 					$newFile->add();
    435 					
    436 					$this->addNotice('file',$newFile->name,'ADDED');
    437 					unset($newFile);
    438 				}
    439 
    440 				$zip->close();
    441 				unset($zip);
    442 				
    443 				break;
    444 
    445 			default:
    446 				throw new \OpenRatException( 'cannot extract file with extension: '.$this->file->extension );
    447 		}
    448 		$this->callSubAction('edit');
    449 	}
    450 
    451 
    452 
    453 	/**
    454 	 * Anzeigen des Inhaltes
    455 	 */
    456 	function compressView()
    457 	{
    458 		$formats = array();
    459 		foreach( $this->getCompressionTypes() as $t )
    460 			$formats[$t] = lang('compression_'.$t);
    461 
    462 		$this->setTemplateVar('formats'       ,$formats    );
    463 	}
    464 
    465 	
    466 
    467 	/**
    468 	 * Anzeigen des Inhaltes
    469 	 */
    470 	function compressPost()
    471 	{
    472 		$format = $this->getRequestVar('format',OR_FILTER_ALPHANUM);
    473 		
    474 		switch( $format )
    475 		{
    476 			case 'gz':
    477 				if	( $this->getRequestVar('replace',OR_FILTER_NUMBER)=='1' )
    478 				{
    479 					$this->file->value = gzencode( $this->file->loadValue(),1 );
    480 					$this->file->parse_filename( $this->file->filename.'.'.$this->file->extension.'.gz',FORCE_GZIP );
    481 					$this->file->save();
    482 					$this->file->saveValue();
    483 					
    484 				}
    485 				else
    486 				{
    487 					$newFile = new File();
    488 					$newFile->name     = $this->file->name;
    489 					$newFile->parentid = $this->file->parentid;
    490 					$newFile->value    = gzencode( $this->file->loadValue(),1 );
    491 					$newFile->parse_filename( $this->file->filename.'.'.$this->file->extension.'.gz',FORCE_GZIP );
    492 					$newFile->add();
    493 				}
    494 				
    495 				break;
    496 
    497 			case 'bzip2':
    498 				if	( $this->getRequestVar('replace')=='1' )
    499 				{
    500 					$this->file->value = bzcompress( $this->file->loadValue() );
    501 					$this->file->parse_filename( $this->file->filename.'.'.$this->file->extension.'.bz2' );
    502 					$this->file->save();
    503 					$this->file->saveValue();
    504 					
    505 				}
    506 				else
    507 				{
    508 					$newFile = new File();
    509 					$newFile->name     = $this->file->name;
    510 					$newFile->parentid = $this->file->parentid;
    511 					$newFile->value    = bzcompress( $this->file->loadValue() );
    512 					$newFile->parse_filename( $this->file->filename.'.'.$this->file->extension.'.bz2' );
    513 					$newFile->add();
    514 				}
    515 				
    516 				break;
    517 			default:
    518 				throw new \OpenRatException( 'unknown compress type: '.$format );
    519 		}
    520 
    521 		$this->addNotice('file',$this->file->name,'DONE',OR_NOTICE_OK);
    522 		$this->callSubAction('edit');
    523 	}
    524 
    525 
    526 	/**
    527 	 * Datei veroeffentlichen
    528 	 */
    529 	function pubView()
    530 	{
    531 	}
    532 
    533 
    534 	/**
    535 	 * Datei veroeffentlichen
    536 	 */
    537 	function pubPost()
    538 	{
    539 	    $this->file->publisher = new PublishPublic( $this->file->projectid );
    540 		$this->file->publish();
    541 		$this->file->publisher->close();
    542 		
    543 		$this->addNotice('file',$this->file->fullFilename,'PUBLISHED',OR_NOTICE_OK,array(),$this->file->publisher->log);
    544 	}
    545 
    546 
    547 
    548 	function getCompressionTypes()
    549 	{
    550 		$compressionTypes = array();
    551 		if	( function_exists('gzencode'    ) ) $compressionTypes[] = 'gz';
    552 		//if	( function_exists('gzencode'    ) ) $compressionTypes[] = 'zip';
    553 		if	( function_exists('bzipcompress') ) $compressionTypes[] = 'bz2';
    554 		return $compressionTypes;
    555 	}
    556 
    557 	function getArchiveTypes()
    558 	{
    559 		$archiveTypes = array();
    560 		$archiveTypes[] = 'tar';
    561 		$archiveTypes[] = 'zip';
    562 		return $archiveTypes;
    563 	}
    564 
    565 
    566 
    567 	function checkMenu( $name )
    568 	{
    569 		$archiveTypes     = $this->getArchiveTypes();
    570 		$compressionTypes = $this->getCompressionTypes();
    571 		
    572 		switch( $name )
    573 		{
    574 			case 'uncompress':
    575 				return !readonly() && in_array($this->file->extension,$compressionTypes);
    576 
    577 			case 'compress':
    578 				return !readonly() && !in_array($this->file->extension,$compressionTypes);
    579 
    580 			case 'extract':
    581 				return !readonly() && in_array($this->file->extension,$archiveTypes);
    582 
    583 			case 'size':
    584 				return !readonly() && $this->file->isImage();
    585 
    586 			case 'editvalue':
    587 				return !readonly() && substr($this->file->mimeType(),0,5)=='text/';
    588 
    589 			case 'aclform':
    590 				return !readonly();
    591 				
    592 			default:
    593 				return true;
    594 		}
    595 	}
    596 	
    597 	
    598 	
    599 	/**
    600 	 * Liefert die Struktur zu diesem Ordner:
    601 	 * - Mit den ├╝bergeordneten Ordnern und
    602 	 * - den in diesem Ordner enthaltenen Objekten
    603 	 * 
    604 	 * Beispiel:
    605 	 * <pre>
    606 	 * - A
    607 	 *   - B
    608 	 *     - C (dieser Ordner)
    609 	 *       - Unterordner
    610 	 *       - Seite
    611 	 *       - Seite
    612 	 *       - Datei
    613 	 * </pre> 
    614 	 */
    615 	public function structureView()
    616 	{
    617 
    618 		$structure = array();
    619 		$tmp = &$structure;
    620 		$nr  = 0;
    621 		
    622 		$folder = new Folder( $this->file->parentid );
    623 		$parents = $folder->parentObjectNames(false,true);
    624 		
    625 		foreach( $parents as $id=>$name)
    626 		{
    627 			unset($children);
    628 			unset($o);
    629 			$children = array();
    630 			$o = array('id'=>$id,'name'=>$name,'type'=>'folder','level'=>++$nr,'children'=>&$children);
    631 			
    632 			$tmp[$id] = &$o;;
    633 			
    634 			unset($tmp);
    635 			
    636 			$tmp = &$children; 
    637 		}
    638 		
    639 		
    640 		
    641 		unset($children);
    642 		unset($id);
    643 		unset($name);
    644 		
    645 		$elementChildren = array();
    646 		
    647 		$tmp[ $this->file->objectid ] = array('id'=>$this->file->objectid,'name'=>$this->file->name,'type'=>'file','self'=>true,'children'=>&$elementChildren);
    648 		
    649 		
    650 		//Html::debug($structure);
    651 		
    652 		$this->setTemplateVar('outline',$structure);
    653 	}
    654 
    655 
    656 	public function removeView()
    657     {
    658         $this->setTemplateVar( 'name',$this->file->filename );
    659     }
    660 
    661 
    662     public function removePost()
    663     {
    664         if   ( $this->getRequestVar('delete') != '' )
    665         {
    666             $this->file->delete();
    667             $this->addNotice('template',$this->file->filename,'DELETED',OR_NOTICE_OK);
    668         }
    669         else
    670         {
    671             $this->addNotice('template',$this->file->filename,'CANCELED',OR_NOTICE_WARN);
    672         }
    673     }
    674 }
    675 
    676 ?>