eZ Publish  [4.0]
ezfilepassthroughhandler.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZFilePassthroughHandler class
00004 //
00005 // Created on: <30-Apr-2002 16:47:08 bf>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*!
00032   \class eZFilePassthroughHandler ezfilepassthroughhandler.php
00033   \ingroup eZBinaryHandlers
00034   \brief Handles file downloading by passing the file through PHP
00035 
00036 */
00037 //include_once( "kernel/classes/datatypes/ezbinaryfile/ezbinaryfile.php" );
00038 //include_once( "kernel/classes/ezbinaryfilehandler.php" );
00039 
00040 class eZFilePassthroughHandler extends eZBinaryFileHandler
00041 {
00042     const HANDLER_ID = 'ezfilepassthrough';
00043 
00044     function eZFilePassthroughHandler()
00045     {
00046         $this->eZBinaryFileHandler( self::HANDLER_ID, "PHP passthrough", eZBinaryFileHandler::HANDLE_DOWNLOAD );
00047     }
00048 
00049     function handleFileDownload( $contentObject, $contentObjectAttribute, $type,
00050                                  $fileInfo )
00051     {
00052         $fileName = $fileInfo['filepath'];
00053 
00054 
00055         require_once( 'kernel/classes/ezclusterfilehandler.php' );
00056         $file = eZClusterFileHandler::instance( $fileName );
00057 
00058         if ( $fileName != "" and $file->exists() )
00059         {
00060             $file->fetch( true );
00061             $fileSize = $file->size();
00062             $mimeType =  $fileInfo['mime_type'];
00063             $originalFileName = $fileInfo['original_filename'];
00064             $contentLength = $fileSize;
00065             $fileOffset = false;
00066             $fileLength = false;
00067             if ( isset( $_SERVER['HTTP_RANGE'] ) )
00068             {
00069                 $httpRange = trim( $_SERVER['HTTP_RANGE'] );
00070                 if ( preg_match( "/^bytes=([0-9]+)-$/", $httpRange, $matches ) )
00071                 {
00072                     $fileOffset = $matches[1];
00073                     header( "Content-Range: bytes $fileOffset-" . ( $fileSize - 1 ) . "/$fileSize" );
00074                     header( "HTTP/1.1 206 Partial content" );
00075                     $contentLength -= $fileOffset;
00076                 }
00077             }
00078             // Figure out the time of last modification of the file right way to get the file mtime ... the
00079             $fileModificationTime = filemtime( $fileName );
00080 
00081             ob_clean();
00082             header( "Pragma: " );
00083             header( "Cache-Control: " );
00084             /* Set cache time out to 10 minutes, this should be good enough to work around an IE bug */
00085             header( "Expires: ". gmdate('D, d M Y H:i:s', time() + 600) . ' GMT' );
00086             header( "Last-Modified: ". gmdate( 'D, d M Y H:i:s', $fileModificationTime ) . ' GMT' );
00087             header( "Content-Length: $contentLength" );
00088             header( "Content-Type: $mimeType" );
00089             header( "X-Powered-By: eZ Publish" );
00090 
00091             $dispositionType = self::dispositionType( $mimeType );
00092             header( "Content-disposition: $dispositionType; filename=\"$originalFileName\"" );
00093 
00094             header( "Content-Transfer-Encoding: binary" );
00095             header( "Accept-Ranges: bytes" );
00096 
00097             $fh = fopen( "$fileName", "rb" );
00098             if ( $fileOffset )
00099             {
00100                 fseek( $fh, $fileOffset );
00101             }
00102 
00103             ob_end_clean();
00104             fpassthru( $fh );
00105             fclose( $fh );
00106 
00107             eZExecution::cleanExit();
00108         }
00109         return eZBinaryFileHandler::RESULT_UNAVAILABLE;
00110     }
00111 
00112     /**
00113      * Checks if a file should be downloaded to disk or displayed inline in
00114      * the browser.
00115      *
00116      * This method returns "attachment" if no setting for the mime type is found.
00117      *
00118      * @param string $mimetype
00119      * @return string "attachment" or "inline"
00120      */
00121     protected static function dispositionType( $mimeType )
00122     {
00123         $ini = eZINI::instance( 'file.ini' );
00124 
00125         $mimeTypes = $ini->variable( 'PassThroughSettings', 'ContentDisposition', array() );
00126         if ( isset( $mimeTypes[$mimeType] ) )
00127         {
00128             return $mimeTypes[$mimeType];
00129         }
00130 
00131         return "attachment";
00132     }
00133 }
00134 
00135 ?>