eZ Publish  [trunk]
ezbinaryfilehandler.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZBinaryFileHandler 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  \defgroup eZBinaryHandlers Binary file handlers
00013 */
00014 
00015 /*!
00016   \class eZBinaryFileHandler ezbinaryfilehandler.php
00017   \ingroup eZKernel
00018   \brief Interface for all binary file handlers
00019 
00020 */
00021 
00022 class eZBinaryFileHandler
00023 {
00024     const HANDLE_UPLOAD = 0x1;
00025     const HANDLE_DOWNLOAD = 0x2;
00026 
00027     const HANDLE_ALL = 0x3; // HANDLE_UPLOAD | HANDLE_DOWNLOAD
00028 
00029     const TYPE_FILE = 'file';
00030     const TYPE_MEDIA = 'media';
00031 
00032     const RESULT_OK = 1;
00033     const RESULT_UNAVAILABLE = 2;
00034 
00035     function eZBinaryFileHandler( $identifier, $name, $handleType )
00036     {
00037         $this->Info = array();
00038         $this->Info['identifier'] = $identifier;
00039         $this->Info['name'] = $name;
00040         $this->Info['handle-type'] = $handleType;
00041     }
00042 
00043     function attributes()
00044     {
00045         return array_keys( $this->Info );
00046     }
00047 
00048     function hasAttribute( $attribute )
00049     {
00050         return isset( $this->Info[$attribute] );
00051     }
00052 
00053     function attribute( $attribute )
00054     {
00055         if ( isset( $this->Info[$attribute] ) )
00056         {
00057             return $this->Info[$attribute];
00058         }
00059 
00060         eZDebug::writeError( "Attribute '$attribute' does not exist", __METHOD__ );
00061         return null;
00062     }
00063 
00064     /*!
00065      \return the suffix for the template name which will be used for attribute viewing.
00066      \note Default returns false which means no special template.
00067     */
00068     function viewTemplate( $contentobjectAttribute )
00069     {
00070         $retVal = false;
00071         return $retVal;
00072     }
00073 
00074     /*!
00075      \return the suffix for the template name which will be used for attribute viewing.
00076      \note Default returns false which means no special template.
00077     */
00078     function editTemplate( $contentobjectAttribute )
00079     {
00080         $retVal = false;
00081         return $retVal;
00082     }
00083 
00084     /*!
00085      \return the suffix for the template name which will be used for attribute viewing.
00086      \note Default returns false which means no special template.
00087     */
00088     function informationTemplate( $contentobjectAttribute )
00089     {
00090         $retVal = false;
00091         return $retVal;
00092     }
00093 
00094     /*!
00095      Figures out the filename from the binary object \a $binary.
00096      Currently supports eZBinaryFile, eZMedia and eZImageAliasHandler.
00097      \return \c false if no file was found.
00098      \param $returnMimeData If this is set to \c true then it will return a mime structure, otherwise it returns the filename.
00099      \deprecated
00100     */
00101     function storedFilename( &$binary, $returnMimeData = false )
00102     {
00103 
00104         $origDir = eZSys::storageDirectory() . '/original';
00105 
00106         $class = get_class( $binary );
00107         $fileName = false;
00108         $originalFilename = false;
00109         if ( in_array( $class, array( 'eZBinaryFile', 'eZMedia' ) ) )
00110         {
00111             $fileName = $origDir . "/" . $binary->attribute( 'mime_type_category' ) . '/'.  $binary->attribute( "filename" );
00112             $originalFilename = $binary->attribute( 'original_filename' );
00113         }
00114         else if ( $class == 'eZImageAliasHandler' )
00115         {
00116             $alias = $binary->attribute( 'original' );
00117             if ( $alias )
00118                 $fileName = $alias['url'];
00119             $originalFilename = $binary->attribute( 'original_filename' );
00120         }
00121         if ( $fileName )
00122         {
00123             $mimeData = eZMimeType::findByFileContents( $fileName );
00124             $mimeData['original_filename'] = $originalFilename;
00125 
00126             if ( !isSet( $mimeData['name'] ) )
00127                 $mimeData['name'] = 'application/octet-stream';
00128 
00129             if ( $returnMimeData )
00130                 return $mimeData;
00131             else
00132                 return $mimeData['url'];
00133         }
00134         return false;
00135     }
00136 
00137     function handleUpload()
00138     {
00139         return false;
00140     }
00141 
00142     /*!
00143      \return the file object which corresponds to \a $contentObject and \a $contentObjectAttribute.
00144     */
00145     function downloadFileObject( $contentObject, $contentObjectAttribute )
00146     {
00147         $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00148         $version = $contentObject->attribute( 'current_version' );
00149         $fileObject = eZBinaryFile::fetch( $contentObjectAttributeID, $version );
00150         if ( $fileObject )
00151             return $fileObject;
00152         $fileObject = eZMedia::fetch( $contentObjectAttributeID, $version );
00153         return $fileObject;
00154     }
00155 
00156     /*!
00157      \return the file object type which corresponds to \a $contentObject and \a $contentObjectAttribute.
00158      \deprecated
00159     */
00160     function downloadType( $contentObject, $contentObjectAttribute )
00161     {
00162         $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00163         $version = $contentObject->attribute( 'current_version' );
00164         $fileObject = eZBinaryFile::fetch( $contentObjectAttributeID, $version );
00165         if ( $fileObject )
00166             return self::TYPE_FILE;
00167         $fileObject = eZMedia::fetch( $contentObjectAttributeID, $version );
00168         if ( $fileObject )
00169             return self::TYPE_MEDIA;
00170         return false;
00171     }
00172 
00173     /*!
00174      \return the download url for the file object which corresponds to \a $contentObject and \a $contentObjectAttribute.
00175      \deprecated
00176     */
00177     function downloadURL( $contentObject, $contentObjectAttribute )
00178     {
00179         $contentObjectID = $contentObject->attribute( 'id' );
00180         $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00181         $downloadType = eZBinaryFileHandler::downloadType( $contentObject, $contentObjectAttribute );
00182         $downloadObject = eZBinaryFileHandler::downloadFileObject( $contentObject, $contentObjectAttribute );
00183         $name = '';
00184         switch ( $downloadType )
00185         {
00186             case self::TYPE_FILE:
00187             {
00188                 $name = $downloadObject->attribute( 'original_filename' );
00189             } break;
00190             case self::TYPE_MEDIA:
00191             {
00192                 $name = $downloadObject->attribute( 'original_filename' );
00193             } break;
00194             default:
00195             {
00196                 eZDebug::writeWarning( "Unknown binary file type '$downloadType'", __METHOD__ );
00197             } break;
00198         }
00199         $url = "/content/download/$contentObjectID/$contentObjectAttributeID/$downloadType/$name";
00200         return $url;
00201     }
00202 
00203     function handleDownload( $contentObject, $contentObjectAttribute, $type )
00204     {
00205         $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00206         $version = $contentObject->attribute( 'current_version' );
00207 
00208 
00209 
00210         if ( !$contentObjectAttribute->hasStoredFileInformation( $contentObject, $version,
00211                                                                  $contentObjectAttribute->attribute( 'language_code' ) ) )
00212             return self::RESULT_UNAVAILABLE;
00213 
00214         $fileInfo = $contentObjectAttribute->storedFileInformation( $contentObject, $version,
00215                                                                     $contentObjectAttribute->attribute( 'language_code' ) );
00216         if ( !$fileInfo )
00217             return self::RESULT_UNAVAILABLE;
00218         if ( !$fileInfo['mime_type'] )
00219             return self::RESULT_UNAVAILABLE;
00220 
00221         $contentObjectAttribute->handleDownload( $contentObject, $version,
00222                                                  $contentObjectAttribute->attribute( 'language_code' ) );
00223 
00224         return $this->handleFileDownload( $contentObject, $contentObjectAttribute, $type, $fileInfo );
00225     }
00226 
00227     function handleFileDownload( $contentObject, $contentObjectAttribute, $type, $mimeData )
00228     {
00229         return false;
00230     }
00231 
00232     function repositories()
00233     {
00234         return array( 'kernel/classes/binaryhandlers' );
00235     }
00236 
00237     /**
00238      * Returns a shared instance of the eZBinaryFileHandler class
00239      * pr $handlerName as defined in file.ini[BinaryFileSettings]Handler
00240      *
00241      * @param string|false $identifier Uses file.ini[BinaryFileSettings]Handler if false
00242      * @return eZBinaryFileHandler
00243      */
00244     static function instance( $identifier = false )
00245     {
00246         if ( $identifier === false )
00247         {
00248             $fileINI = eZINI::instance( 'file.ini' );
00249             $identifier = $fileINI->variable( 'BinaryFileSettings', 'Handler' );
00250         }
00251         $instance =& $GLOBALS['eZBinaryFileHandlerInstance-' . $identifier];
00252         if ( !isset( $instance ) )
00253         {
00254             $optionArray = array( 'iniFile'     => 'file.ini',
00255                                   'iniSection'  => 'BinaryFileSettings',
00256                                   'iniVariable' => 'Handler'  );
00257 
00258             $options = new ezpExtensionOptions( $optionArray );
00259 
00260             $instance = eZExtension::getHandlerClass( $options );
00261 
00262             if( $instance === false )
00263             {
00264                 eZDebug::writeError( "Could not find binary file handler '$identifier'", __METHOD__ );
00265             }
00266         }
00267         return $instance;
00268     }
00269 
00270     /// \privatesection
00271     public $Info;
00272 }
00273 
00274 ?>