eZ Publish  [trunk]
ezfilepassthroughhandler.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZFilePassthroughHandler class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 /*!
00012   \class eZFilePassthroughHandler ezfilepassthroughhandler.php
00013   \ingroup eZBinaryHandlers
00014   \brief Handles file downloading by passing the file through PHP
00015 
00016 */
00017 class eZFilePassthroughHandler extends eZBinaryFileHandler
00018 {
00019     const HANDLER_ID = 'ezfilepassthrough';
00020 
00021     function eZFilePassthroughHandler()
00022     {
00023         $this->eZBinaryFileHandler( self::HANDLER_ID, "PHP passthrough", eZBinaryFileHandler::HANDLE_DOWNLOAD );
00024     }
00025 
00026     function handleFileDownload( $contentObject, $contentObjectAttribute, $type,
00027                                  $fileInfo )
00028     {
00029         $fileName = $fileInfo['filepath'];
00030 
00031         $file = eZClusterFileHandler::instance( $fileName );
00032 
00033         if ( $fileName != "" and $file->exists() )
00034         {
00035             $fileSize = $file->size();
00036             if ( isset( $_SERVER['HTTP_RANGE'] ) && preg_match( "/^bytes=(\d+)-(\d+)?$/", trim( $_SERVER['HTTP_RANGE'] ), $matches ) )
00037             {
00038                 $fileOffset = $matches[1];
00039                 $contentLength = isset( $matches[2] ) ? $matches[2] - $matches[1] + 1 : $fileSize - $matches[1];
00040             }
00041             else
00042             {
00043                 $fileOffset = 0;
00044                 $contentLength = $fileSize;
00045             }
00046             // Figure out the time of last modification of the file right way to get the file mtime ... the
00047             $fileModificationTime = $file->mtime();
00048 
00049             // stop output buffering, and stop the session so that browsing can be continued while downloading
00050             eZSession::stop();
00051             ob_end_clean();
00052 
00053             eZFile::downloadHeaders(
00054                 $fileName,
00055                 self::dispositionType( $fileInfo['mime_type'] ) === 'attachment',
00056                 false,
00057                 $fileOffset,
00058                 $contentLength,
00059                 $fileSize
00060             );
00061 
00062             try
00063             {
00064                 $file->passthrough( $fileOffset, $contentLength );
00065             }
00066             catch ( eZClusterFileHandlerNotFoundException $e )
00067             {
00068                 eZDebug::writeError( $e->getMessage, __METHOD__ );
00069                 header( $_SERVER["SERVER_PROTOCOL"] . ' 500 Internal Server Error' );
00070             }
00071             catch ( eZClusterFileHandlerGeneralException $e )
00072             {
00073                 eZDebug::writeError( $e->getMessage, __METHOD__ );
00074                 header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found' );
00075             }
00076 
00077             eZExecution::cleanExit();
00078         }
00079         return eZBinaryFileHandler::RESULT_UNAVAILABLE;
00080     }
00081 
00082     /**
00083      * Checks if a file should be downloaded to disk or displayed inline in
00084      * the browser.
00085      *
00086      * This method returns "attachment" if no setting for the mime type is found.
00087      *
00088      * @param string $mimetype
00089      * @return string "attachment" or "inline"
00090      */
00091     protected static function dispositionType( $mimeType )
00092     {
00093         $ini = eZINI::instance( 'file.ini' );
00094 
00095         $mimeTypes = $ini->variable( 'PassThroughSettings', 'ContentDisposition', array() );
00096         if ( isset( $mimeTypes[$mimeType] ) )
00097         {
00098             return $mimeTypes[$mimeType];
00099         }
00100 
00101         return "attachment";
00102     }
00103 }
00104 
00105 ?>