openrat-cms

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

FileShowAction.class.php (3208B)


      1 <?php
      2 namespace cms\action\file;
      3 use cms\action\FileAction;
      4 use cms\action\Method;
      5 use cms\base\Configuration;
      6 use cms\generator\FileContext;
      7 use cms\generator\FileGenerator;
      8 use cms\generator\FileHistoryContext;
      9 use cms\generator\FileHistoryGenerator;
     10 use cms\generator\Producer;
     11 use cms\model\File;
     12 use cms\model\Value;
     13 use util\exception\SecurityException;
     14 
     15 
     16 class FileShowAction extends FileAction implements Method {
     17 
     18     public function view() {
     19 
     20 		$valueId = $this->request->getNumber('valueid');
     21 
     22 		if   ( $valueId ) {
     23 			$value = new Value();
     24 			$value->loadWithId( $valueId );
     25 			if   ( $value->contentid != $this->file->contentid )
     26 				throw new SecurityException('Content-Id does not match');
     27 
     28 			$fileHistoryContext = new FileHistoryContext($this->file->objectid, $valueId );
     29 			$generator          = new FileHistoryGenerator( $fileHistoryContext );
     30 		} else {
     31 			$fileContext = new FileContext($this->file->objectid, Producer::SCHEME_PREVIEW );
     32 			$generator   = new FileGenerator( $fileContext );
     33 		}
     34 
     35 
     36 		$this->lastModified( $this->file->lastchangeDate );
     37 
     38 		if	( $this->file->extension == 'gz' )
     39 		{
     40 			$pos = strrpos($this->file->filename,'.');
     41 			if	( $pos === false )
     42 				$ext = '';
     43 			else
     44 				$ext = substr($this->file->filename,$pos+1);
     45 
     46 			$ext = strtolower($ext);
     47 
     48 			$mime_type = File::$MIME_TYPES[$ext];
     49 
     50 			$this->setContentType( $mime_type );
     51 			$this->addHeader('Content-Encoding','gzip' );
     52 		}
     53 		else
     54 		{
     55 			// Angabe Content-Type
     56 			$this->setContentType($generator->getMimeType() );
     57 		}
     58 
     59 		// Image should be displayed inline.
     60 		// Filename is used if the user agent is saving the file.
     61 		$this->addHeader('Content-Disposition'      ,'inline; filename='.$this->file->filename() );
     62 		$this->addHeader('Content-Transfer-Encoding','binary' );
     63 		$this->addHeader('Content-Description'      ,$this->file->filename() );
     64 
     65 
     66 		// Groesse des Bildes in Bytes
     67 		// Der Browser hat so die Moeglichkeit, einen Fortschrittsbalken zu zeigen
     68 		$this->addHeader('Content-Length',$this->file->size );
     69 
     70 
     71 		if	( $this->request->getAlphanum('encoding') == 'base64')
     72 		{
     73 		    $encodingFunction = function($value) {
     74 		        return base64_encode($value);
     75             };
     76 			$this->setTemplateVar('encoding', 'base64');
     77 		}
     78 		else {
     79             $encodingFunction = function($value) {
     80                 return $value;
     81             };
     82             $this->setTemplateVar('encoding', 'none');
     83         }
     84 
     85 
     86 		// Unterscheidung, ob PHP-Code in der Datei ausgefuehrt werden soll.
     87 		$publishConfig = Configuration::subset('publish');
     88         $phpActive = ( $publishConfig->get('enable_php_in_file_content')=='auto' && $this->file->getRealExtension()=='php') ||
     89             $publishConfig->get('enable_php_in_file_content' )===true;
     90 
     91         if	(  $phpActive ) {
     92 
     93             // PHP-Code ausfuehren
     94             ob_start();
     95             require( $generator->getCache()->load()->getFilename() );
     96             $this->setTemplateVar('value',$encodingFunction(ob_get_contents()) );
     97             ob_end_clean();
     98         }
     99         else
    100             $this->setTemplateVar('value',$encodingFunction( $generator->getCache()->get() ) );
    101         // Maybe we want some gzip-encoding?
    102     }
    103 
    104 
    105     public function post() {
    106     }
    107 }