eZ Publish  [4.0]
ezbinaryfiletype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZBinaryFileType class
00004 //
00005 // Created on: <30-Apr-2002 13:06:21 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 eZBinaryFileType ezbinaryfiletype.php
00033   \ingroup eZDatatype
00034   \brief The class eZBinaryFileType handles files and association with content objects
00035 
00036 */
00037 
00038 //include_once( "kernel/classes/ezdatatype.php" );
00039 //include_once( "kernel/classes/datatypes/ezbinaryfile/ezbinaryfile.php" );
00040 //include_once( "kernel/classes/ezbinaryfilehandler.php" );
00041 //include_once( "lib/ezfile/classes/ezfile.php" );
00042 //include_once( "lib/ezutils/classes/ezsys.php" );
00043 //include_once( "lib/ezutils/classes/ezmimetype.php" );
00044 //include_once( "lib/ezutils/classes/ezhttpfile.php" );
00045 
00046 class eZBinaryFileType extends eZDataType
00047 {
00048     const MAX_FILESIZE_FIELD = 'data_int1';
00049 
00050     const MAX_FILESIZE_VARIABLE = '_ezbinaryfile_max_filesize_';
00051 
00052     const DATA_TYPE_STRING = "ezbinaryfile";
00053 
00054     function eZBinaryFileType()
00055     {
00056         $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', "File", 'Datatype name' ),
00057                            array( 'serialize_supported' => true ) );
00058     }
00059 
00060     /*!
00061      \return the binary file handler.
00062     */
00063     function fileHandler()
00064     {
00065         return eZBinaryFileHandler::instance();
00066     }
00067 
00068     /*!
00069      \reimp
00070      \return the template name which the handler decides upon.
00071     */
00072     function viewTemplate( $contentobjectAttribute )
00073     {
00074         $handler = $this->fileHandler();
00075         $handlerTemplate = $handler->viewTemplate( $contentobjectAttribute );
00076         $template = $this->DataTypeString;
00077         if ( $handlerTemplate !== false )
00078             $template .= '_' . $handlerTemplate;
00079         return $template;
00080     }
00081 
00082     /*!
00083      \return the template name to use for editing the attribute.
00084      \note Default is to return the datatype string which is OK
00085            for most datatypes, if you want dynamic templates
00086            reimplement this function and return a template name.
00087      \note The returned template name does not include the .tpl extension.
00088      \sa viewTemplate, informationTemplate
00089     */
00090     function editTemplate( $contentobjectAttribute )
00091     {
00092         $handler = $this->fileHandler();
00093         $handlerTemplate = $handler->editTemplate( $contentobjectAttribute );
00094         $template = $this->DataTypeString;
00095         if ( $handlerTemplate !== false )
00096             $template .= '_' . $handlerTemplate;
00097         return $template;
00098     }
00099 
00100     /*!
00101      \return the template name to use for information collection for the attribute.
00102      \note Default is to return the datatype string which is OK
00103            for most datatypes, if you want dynamic templates
00104            reimplement this function and return a template name.
00105      \note The returned template name does not include the .tpl extension.
00106      \sa viewTemplate, editTemplate
00107     */
00108     function informationTemplate( $contentobjectAttribute )
00109     {
00110         $handler = $this->fileHandler();
00111         $handlerTemplate = $handler->informationTemplate( $contentobjectAttribute );
00112         $template = $this->DataTypeString;
00113         if ( $handlerTemplate !== false )
00114             $template .= '_' . $handlerTemplate;
00115         return $template;
00116     }
00117 
00118     /*!
00119      Sets value according to current version
00120     */
00121     function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
00122     {
00123         if ( $currentVersion != false )
00124         {
00125             $contentObjectAttributeID = $originalContentObjectAttribute->attribute( "id" );
00126             $version = $contentObjectAttribute->attribute( "version" );
00127             $oldfile = eZBinaryFile::fetch( $contentObjectAttributeID, $currentVersion );
00128             if ( $oldfile != null )
00129             {
00130                 $oldfile->setAttribute( 'contentobject_attribute_id', $contentObjectAttribute->attribute( 'id' ) );
00131                 $oldfile->setAttribute( "version",  $version );
00132                 $oldfile->store();
00133             }
00134         }
00135     }
00136 
00137     /*!
00138      The object is being moved to trash, do any necessary changes to the attribute.
00139      Rename file and update db row with new name, so that access to the file using old links no longer works.
00140     */
00141     function trashStoredObjectAttribute( $contentObjectAttribute, $version = null )
00142     {
00143         $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
00144         $sys = eZSys::instance();
00145         $storage_dir = $sys->storageDirectory();
00146 
00147         if ( $version == null )
00148             $binaryFiles = eZBinaryFile::fetch( $contentObjectAttributeID );
00149         else
00150             $binaryFiles = array( eZBinaryFile::fetch( $contentObjectAttributeID, $version ) );
00151 
00152         foreach ( $binaryFiles as $binaryFile )
00153         {
00154             if ( $binaryFile == null )
00155                 continue;
00156             $mimeType =  $binaryFile->attribute( "mime_type" );
00157             list( $prefix, $suffix ) = split ('[/]', $mimeType );
00158             $orig_dir = $storage_dir . '/original/' . $prefix;
00159             $fileName = $binaryFile->attribute( "filename" );
00160 
00161             // Check if there are any other records in ezbinaryfile that point to that fileName.
00162             $binaryObjectsWithSameFileName = eZBinaryFile::fetchByFileName( $fileName );
00163 
00164             $filePath = $orig_dir . "/" . $fileName;
00165             $file = eZClusterFileHandler::instance( $filePath );
00166 
00167             if ( $file->exists() and count( $binaryObjectsWithSameFileName ) <= 1 )
00168             {
00169                 // create dest filename in the same manner as eZHTTPFile::store()
00170                 // grab file's suffix
00171                 $fileSuffix = eZFile::suffix( $fileName );
00172                 // prepend dot
00173                 if ( $fileSuffix )
00174                     $fileSuffix = '.' . $fileSuffix;
00175                 // grab filename without suffix
00176                 $fileBaseName = basename( $fileName, $fileSuffix );
00177                 // create dest filename
00178                 $newFileName = md5( $fileBaseName . microtime() . mt_rand() ) . $fileSuffix;
00179                 $newFilePath = $orig_dir . "/" . $newFileName;
00180 
00181                 // rename the file, and update the database data
00182                 $file->move( $newFilePath );
00183                 $binaryFile->setAttribute( 'filename', $newFileName );
00184                 $binaryFile->store();
00185             }
00186         }
00187     }
00188 
00189     /*!
00190      Delete stored attribute
00191     */
00192     function deleteStoredObjectAttribute( $contentObjectAttribute, $version = null )
00193     {
00194         $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
00195         $sys = eZSys::instance();
00196         $storage_dir = $sys->storageDirectory();
00197 
00198         require_once( 'kernel/classes/ezclusterfilehandler.php' );
00199         if ( $version == null )
00200         {
00201             $binaryFiles = eZBinaryFile::fetch( $contentObjectAttributeID );
00202             eZBinaryFile::removeByID( $contentObjectAttributeID, null );
00203 
00204             foreach ( $binaryFiles as  $binaryFile )
00205             {
00206                 $mimeType =  $binaryFile->attribute( "mime_type" );
00207                 list( $prefix, $suffix ) = split ('[/]', $mimeType );
00208                 $orig_dir = $storage_dir . '/original/' . $prefix;
00209                 $fileName = $binaryFile->attribute( "filename" );
00210 
00211                 // Check if there are any other records in ezbinaryfile that point to that fileName.
00212                 $binaryObjectsWithSameFileName = eZBinaryFile::fetchByFileName( $fileName );
00213 
00214                 $filePath = $orig_dir . "/" . $fileName;
00215                 $file = eZClusterFileHandler::instance( $filePath );
00216 
00217                 if ( $file->exists() and count( $binaryObjectsWithSameFileName ) < 1 )
00218                     $file->delete();
00219             }
00220         }
00221         else
00222         {
00223             $count = 0;
00224             $binaryFile = eZBinaryFile::fetch( $contentObjectAttributeID, $version );
00225             if ( $binaryFile != null )
00226             {
00227                 $mimeType =  $binaryFile->attribute( "mime_type" );
00228                 list( $prefix, $suffix ) = split ('[/]', $mimeType );
00229                 $orig_dir = $storage_dir . "/original/" . $prefix;
00230                 $fileName = $binaryFile->attribute( "filename" );
00231 
00232                 eZBinaryFile::removeByID( $contentObjectAttributeID, $version );
00233 
00234                 // Check if there are any other records in ezbinaryfile that point to that fileName.
00235                 $binaryObjectsWithSameFileName = eZBinaryFile::fetchByFileName( $fileName );
00236 
00237                 $filePath = $orig_dir . "/" . $fileName;
00238                 $file = eZClusterFileHandler::instance( $filePath );
00239 
00240                 if ( $file->exists() and count( $binaryObjectsWithSameFileName ) < 1 )
00241                     $file->delete();
00242             }
00243         }
00244     }
00245 
00246     /*!
00247      Checks if file uploads are enabled, if not it gives a warning.
00248     */
00249     function checkFileUploads()
00250     {
00251         $isFileUploadsEnabled = ini_get( 'file_uploads' ) != 0;
00252         if ( !$isFileUploadsEnabled )
00253         {
00254             $isFileWarningAdded = $GLOBALS['eZBinaryFileTypeWarningAdded'];
00255             if ( !isset( $isFileWarningAdded ) or
00256                  !$isFileWarningAdded )
00257             {
00258                 eZAppendWarningItem( array( 'error' => array( 'type' => 'kernel',
00259                                                               'number' => eZError::KERNEL_NOT_AVAILABLE ),
00260                                             'text' => ezi18n( 'kernel/classes/datatypes',
00261                                                               'File uploading is not enabled. Please contact the site administrator to enable it.' ) ) );
00262                 $GLOBALS['eZBinaryFileTypeWarningAdded'] = true;
00263             }
00264         }
00265     }
00266 
00267     /*!
00268      Validates the input and returns true if the input was
00269      valid for this datatype.
00270     */
00271     function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00272     {
00273         eZBinaryFileType::checkFileUploads();
00274         $classAttribute = $contentObjectAttribute->contentClassAttribute();
00275         $mustUpload = false;
00276         $httpFileName = $base . "_data_binaryfilename_" . $contentObjectAttribute->attribute( "id" );
00277         $maxSize = 1024 * 1024 * $classAttribute->attribute( self::MAX_FILESIZE_FIELD );
00278 
00279         if ( $contentObjectAttribute->validateIsRequired() )
00280         {
00281             $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
00282             $version = $contentObjectAttribute->attribute( "version" );
00283             $binary = eZBinaryFile::fetch( $contentObjectAttributeID, $version );
00284             if ( $binary === null )
00285             {
00286                 $mustUpload = true;
00287             }
00288         }
00289 
00290         $canFetchResult = eZHTTPFile::canFetch( $httpFileName, $maxSize );
00291         if ( $mustUpload && $canFetchResult == eZHTTPFile::UPLOADEDFILE_DOES_NOT_EXIST )
00292         {
00293             $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00294                                                                  'A valid file is required.' ) );
00295             return eZInputValidator::STATE_INVALID;
00296         }
00297         if ( $canFetchResult == eZHTTPFile::UPLOADEDFILE_EXCEEDS_PHP_LIMIT )
00298         {
00299             $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00300                 'The size of the uploaded file exceeds the limit set by the upload_max_filesize directive in php.ini.' ) );
00301             return eZInputValidator::STATE_INVALID;
00302         }
00303         if ( $canFetchResult == eZHTTPFile::UPLOADEDFILE_EXCEEDS_MAX_SIZE )
00304         {
00305             $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00306                                                                  'The size of the uploaded file exceeds the maximum upload size: %1 bytes.' ), $maxSize );
00307             return eZInputValidator::STATE_INVALID;
00308         }
00309         return eZInputValidator::STATE_ACCEPTED;
00310     }
00311 
00312     /*!
00313      Fetches the http post var integer input and stores it in the data instance.
00314     */
00315     function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00316     {
00317         eZBinaryFileType::checkFileUploads();
00318         if ( !eZHTTPFile::canFetch( $base . "_data_binaryfilename_" . $contentObjectAttribute->attribute( "id" ) ) )
00319             return false;
00320 
00321         $binaryFile = eZHTTPFile::fetch( $base . "_data_binaryfilename_" . $contentObjectAttribute->attribute( "id" ) );
00322 
00323         $contentObjectAttribute->setContent( $binaryFile );
00324 
00325         if ( $binaryFile instanceof eZHTTPFile )
00326         {
00327             $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
00328             $version = $contentObjectAttribute->attribute( "version" );
00329 
00330             /*
00331             $mimeObj = new  eZMimeType();
00332             $mimeData = $mimeObj->findByURL( $binaryFile->attribute( "original_filename" ), true );
00333             $mime = $mimeData['name'];
00334             */
00335 
00336             $mimeData = eZMimeType::findByFileContents( $binaryFile->attribute( "original_filename" ) );
00337             $mime = $mimeData['name'];
00338 
00339             if ( $mime == '' )
00340             {
00341                 $mime = $binaryFile->attribute( "mime_type" );
00342             }
00343             $extension = eZFile::suffix( $binaryFile->attribute( "original_filename" ) );
00344             $binaryFile->setMimeType( $mime );
00345             if ( !$binaryFile->store( "original", $extension ) )
00346             {
00347                 eZDebug::writeError( "Failed to store http-file: " . $binaryFile->attribute( "original_filename" ),
00348                                      "eZBinaryFileType" );
00349                 return false;
00350             }
00351 
00352             $binary = eZBinaryFile::fetch( $contentObjectAttributeID, $version );
00353             if ( $binary === null )
00354                 $binary = eZBinaryFile::create( $contentObjectAttributeID, $version );
00355 
00356             $orig_dir = $binaryFile->storageDir( "original" );
00357 
00358             $binary->setAttribute( "contentobject_attribute_id", $contentObjectAttributeID );
00359             $binary->setAttribute( "version", $version );
00360             $binary->setAttribute( "filename", basename( $binaryFile->attribute( "filename" ) ) );
00361             $binary->setAttribute( "original_filename", $binaryFile->attribute( "original_filename" ) );
00362             $binary->setAttribute( "mime_type", $mime );
00363 
00364             $binary->store();
00365 
00366 
00367             require_once( 'kernel/classes/ezclusterfilehandler.php' );
00368             $filePath = $binaryFile->attribute( 'filename' );
00369             $fileHandler = eZClusterFileHandler::instance();
00370             $fileHandler->fileStore( $filePath, 'binaryfile', true, $mime );
00371 
00372             $contentObjectAttribute->setContent( $binary );
00373         }
00374         return true;
00375     }
00376 
00377     /*!
00378      Does nothing, since the file has been stored. See fetchObjectAttributeHTTPInput for the actual storing.
00379     */
00380     function storeObjectAttribute( $contentObjectAttribute )
00381     {
00382     }
00383 
00384     function customObjectAttributeHTTPAction( $http, $action, $contentObjectAttribute, $parameters )
00385     {
00386         eZBinaryFileType::checkFileUploads();
00387         if( $action == "delete_binary" )
00388         {
00389             $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
00390             $version = $contentObjectAttribute->attribute( "version" );
00391             $this->deleteStoredObjectAttribute( $contentObjectAttribute, $version );
00392         }
00393     }
00394 
00395     /*!
00396      \reimp
00397      HTTP file insertion is supported.
00398     */
00399     function isHTTPFileInsertionSupported()
00400     {
00401         return true;
00402     }
00403 
00404     /*!
00405      \reimp
00406      HTTP file insertion is supported.
00407     */
00408     function isRegularFileInsertionSupported()
00409     {
00410         return true;
00411     }
00412 
00413     /*!
00414      \reimp
00415      Inserts the file using the eZBinaryFile class.
00416     */
00417     function insertHTTPFile( $object, $objectVersion, $objectLanguage,
00418                              $objectAttribute, $httpFile, $mimeData,
00419                              &$result )
00420     {
00421         $result = array( 'errors' => array(),
00422                          'require_storage' => false );
00423         $attributeID = $objectAttribute->attribute( 'id' );
00424 
00425         $binary = eZBinaryFile::fetch( $attributeID, $objectVersion );
00426         if ( $binary === null )
00427             $binary = eZBinaryFile::create( $attributeID, $objectVersion );
00428 
00429         $httpFile->setMimeType( $mimeData['name'] );
00430 
00431         $db = eZDB::instance();
00432         $db->begin();
00433 
00434         if ( !$httpFile->store( "original", false, false ) )
00435         {
00436             $result['errors'][] = array( 'description' => ezi18n( 'kernel/classes/datatypes/ezbinaryfile',
00437                                                         'Failed to store file %filename. Please contact the site administrator.', null,
00438                                                         array( '%filename' => $httpFile->attribute( "original_filename" ) ) ) );
00439             return false;
00440         }
00441 
00442 
00443         $filePath = $binary->attribute( 'filename' );
00444 
00445         $binary->setAttribute( "contentobject_attribute_id", $attributeID );
00446         $binary->setAttribute( "version", $objectVersion );
00447         $binary->setAttribute( "filename", basename( $httpFile->attribute( "filename" ) ) );
00448         $binary->setAttribute( "original_filename", $httpFile->attribute( "original_filename" ) );
00449         $binary->setAttribute( "mime_type", $mimeData['name'] );
00450 
00451         $binary->store();
00452 
00453 
00454         require_once( 'kernel/classes/ezclusterfilehandler.php' );
00455         $filePath = $httpFile->attribute( 'filename' );
00456         $fileHandler = eZClusterFileHandler::instance();
00457         $fileHandler->fileStore( $filePath, 'binaryfile', true, $mimeData['name'] );
00458         $objectAttribute->setContent( $binary );
00459 
00460         $db->commit();
00461 
00462         $objectAttribute->setContent( $binary );
00463 
00464         return true;
00465     }
00466 
00467     /*!
00468      \reimp
00469      Inserts the file using the eZBinaryFile class.
00470     */
00471     function insertRegularFile( $object, $objectVersion, $objectLanguage,
00472                                 $objectAttribute, $filePath,
00473                                 &$result )
00474     {
00475         $result = array( 'errors' => array(),
00476                          'require_storage' => false );
00477         $attributeID = $objectAttribute->attribute( 'id' );
00478 
00479         $binary = eZBinaryFile::fetch( $attributeID, $objectVersion );
00480         if ( $binary === null )
00481             $binary = eZBinaryFile::create( $attributeID, $objectVersion );
00482 
00483         $fileName = basename( $filePath );
00484         $mimeData = eZMimeType::findByFileContents( $filePath );
00485         $storageDir = eZSys::storageDirectory();
00486         list( $group, $type ) = explode( '/', $mimeData['name'] );
00487         $destination = $storageDir . '/original/' . $group;
00488 
00489         if ( !file_exists( $destination ) )
00490         {
00491             if ( !eZDir::mkdir( $destination, false, true ) )
00492             {
00493                 return false;
00494             }
00495         }
00496 
00497         // create dest filename in the same manner as eZHTTPFile::store()
00498         // grab file's suffix
00499         $fileSuffix = eZFile::suffix( $fileName );
00500         // prepend dot
00501         if( $fileSuffix )
00502             $fileSuffix = '.' . $fileSuffix;
00503         // grab filename without suffix
00504         $fileBaseName = basename( $fileName, $fileSuffix );
00505         // create dest filename
00506         $destFileName = md5( $fileBaseName . microtime() . mt_rand() ) . $fileSuffix;
00507         $destination = $destination . '/' . $destFileName;
00508 
00509         copy( $filePath, $destination );
00510 
00511         require_once( 'kernel/classes/ezclusterfilehandler.php' );
00512         $fileHandler = eZClusterFileHandler::instance();
00513         $fileHandler->fileStore( $destination, 'binaryfile', true, $mimeData['name'] );
00514 
00515 
00516         $binary->setAttribute( "contentobject_attribute_id", $attributeID );
00517         $binary->setAttribute( "version", $objectVersion );
00518         $binary->setAttribute( "filename", $destFileName );
00519         $binary->setAttribute( "original_filename", $fileName );
00520         $binary->setAttribute( "mime_type", $mimeData['name'] );
00521 
00522         $binary->store();
00523 
00524         $objectAttribute->setContent( $binary );
00525         return true;
00526     }
00527 
00528     /*!
00529       \reimp
00530       We support file information
00531     */
00532     function hasStoredFileInformation( $object, $objectVersion, $objectLanguage,
00533                                        $objectAttribute )
00534     {
00535         return true;
00536     }
00537 
00538     /*!
00539       \reimp
00540       Extracts file information for the binaryfile entry.
00541     */
00542     function storedFileInformation( $object, $objectVersion, $objectLanguage,
00543                                     $objectAttribute )
00544     {
00545         $binaryFile = eZBinaryFile::fetch( $objectAttribute->attribute( "id" ),
00546                                             $objectAttribute->attribute( "version" ) );
00547         if ( $binaryFile )
00548         {
00549             return $binaryFile->storedFileInfo();
00550         }
00551         return false;
00552     }
00553     /*!
00554       \reimp
00555       Updates download count for binary file.
00556     */
00557     function handleDownload( $object, $objectVersion, $objectLanguage,
00558                              $objectAttribute )
00559     {
00560         $binaryFile = eZBinaryFile::fetch( $objectAttribute->attribute( "id" ),
00561                                             $objectAttribute->attribute( "version" ) );
00562 
00563         $contentObjectAttributeID = $objectAttribute->attribute( 'id' );
00564         $version =  $objectAttribute->attribute( "version" );
00565 
00566         if ( $binaryFile )
00567         {
00568             $db = eZDB::instance();
00569             $db->query( "UPDATE ezbinaryfile SET download_count=(download_count+1)
00570                          WHERE
00571                          contentobject_attribute_id=$contentObjectAttributeID AND version=$version" );
00572             return true;
00573         }
00574         return false;
00575     }
00576 
00577     function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
00578     {
00579         $filesizeName = $base . self::MAX_FILESIZE_VARIABLE . $classAttribute->attribute( 'id' );
00580         if ( $http->hasPostVariable( $filesizeName ) )
00581         {
00582             $filesizeValue = $http->postVariable( $filesizeName );
00583             $classAttribute->setAttribute( self::MAX_FILESIZE_FIELD, $filesizeValue );
00584         }
00585     }
00586     /*!
00587      Returns the object title.
00588     */
00589     function title( $contentObjectAttribute,  $name = "original_filename" )
00590     {
00591         $value = false;
00592         $binaryFile = eZBinaryFile::fetch( $contentObjectAttribute->attribute( 'id' ),
00593                                            $contentObjectAttribute->attribute( 'version' ) );
00594         if ( is_object( $binaryFile ) )
00595             $value = $binaryFile->attribute( $name );
00596 
00597         return $value;
00598     }
00599 
00600     function hasObjectAttributeContent( $contentObjectAttribute )
00601     {
00602         $binaryFile = eZBinaryFile::fetch( $contentObjectAttribute->attribute( "id" ),
00603                                             $contentObjectAttribute->attribute( "version" ) );
00604         if ( !$binaryFile )
00605             return false;
00606         return true;
00607     }
00608 
00609     function objectAttributeContent( $contentObjectAttribute )
00610     {
00611         $binaryFile = eZBinaryFile::fetch( $contentObjectAttribute->attribute( "id" ),
00612                                             $contentObjectAttribute->attribute( "version" ) );
00613         if ( !$binaryFile )
00614         {
00615             $attrValue = false;
00616             return $attrValue;
00617         }
00618         return $binaryFile;
00619     }
00620 
00621     /*!
00622      \reimp
00623     */
00624     function isIndexable()
00625     {
00626         return true;
00627     }
00628 
00629     function metaData( $contentObjectAttribute )
00630     {
00631         $binaryFile = $contentObjectAttribute->content();
00632 
00633         $metaData = "";
00634         if ( $binaryFile instanceof eZBinaryFile )
00635         {
00636             $metaData = $binaryFile->metaData();
00637         }
00638         return $metaData;
00639     }
00640 
00641     /*!
00642      \reimp
00643     */
00644     function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00645     {
00646         $dom = $attributeParametersNode->ownerDocument;
00647         $maxSize = $classAttribute->attribute( self::MAX_FILESIZE_FIELD );
00648         $maxSizeNode = $dom->createElement( 'max-size' );
00649         $maxSizeNode->appendChild( $dom->createTextNode( $maxSize ) );
00650         $maxSizeNode->setAttribute( 'unit-size', 'mega' );
00651         $attributeParametersNode->appendChild( $maxSizeNode );
00652     }
00653 
00654     /*!
00655      \reimp
00656     */
00657     function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00658     {
00659         $sizeNode = $attributeParametersNode->getElementsByTagName( 'max-size' )->item( 0 );
00660         $maxSize = $sizeNode->textContent;
00661         $unitSize = $sizeNode->getAttribute( 'unit-size' );
00662         $classAttribute->setAttribute( self::MAX_FILESIZE_FIELD, $maxSize );
00663     }
00664 
00665     /*!
00666      \return string representation of an contentobjectattribute data for simplified export
00667 
00668     */
00669     function toString( $objectAttribute )
00670     {
00671         $binaryFile = $objectAttribute->content();
00672 
00673         if ( is_object( $binaryFile ) )
00674         {
00675             return implode( '|', array( $binaryFile->attribute( 'filepath' ), $binaryFile->attribute( 'original_filename' ) ) );
00676         }
00677         else
00678             return '';
00679     }
00680 
00681 
00682 
00683     function fromString( $objectAttribute, $string )
00684     {
00685         if( !$string )
00686             return true;
00687 
00688         $result = array();
00689         return $this->insertRegularFile( $objectAttribute->attribute( 'object' ),
00690                                          $objectAttribute->attribute( 'version' ),
00691                                          $objectAttribute->attribute( 'language_code' ),
00692                                          $objectAttribute,
00693                                          $string,
00694                                          $result );
00695     }
00696 
00697     /*!
00698      \param package
00699      \param content attribute
00700 
00701      \return a DOM representation of the content object attribute
00702     */
00703     function serializeContentObjectAttribute( $package, $objectAttribute )
00704     {
00705         $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00706 
00707         $binaryFile = $objectAttribute->attribute( 'content' );
00708         if ( is_object( $binaryFile ) )
00709         {
00710             $fileKey = md5( mt_rand() );
00711             $package->appendSimpleFile( $fileKey, $binaryFile->attribute( 'filepath' ) );
00712 
00713             $dom = $node->ownerDocument;
00714             $fileNode = $dom->createElement( 'binary-file' );
00715             $fileNode->setAttribute( 'filesize', $binaryFile->attribute( 'filesize' ) );
00716             $fileNode->setAttribute( 'filename', $binaryFile->attribute( 'filename' ) );
00717             $fileNode->setAttribute( 'original-filename', $binaryFile->attribute( 'original_filename' ) );
00718             $fileNode->setAttribute( 'mime-type', $binaryFile->attribute( 'mime_type' ) );
00719             $fileNode->setAttribute( 'filekey', $fileKey );
00720             $node->appendChild( $fileNode );
00721         }
00722 
00723         return $node;
00724     }
00725 
00726     /*!
00727      \reimp
00728      \param package
00729      \param contentobject attribute object
00730      \param domnode object
00731     */
00732     function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
00733     {
00734 
00735         $fileNode = $attributeNode->getElementsByTagName( 'binary-file' )->item( 0 );
00736         if ( !is_object( $fileNode ) or !$fileNode->hasAttributes() )
00737         {
00738             return;
00739         }
00740 
00741         $binaryFile = eZBinaryFile::create( $objectAttribute->attribute( 'id' ), $objectAttribute->attribute( 'version' ) );
00742 
00743         $sourcePath = $package->simpleFilePath( $fileNode->getAttribute( 'filekey' ) );
00744 
00745         if ( !file_exists( $sourcePath ) )
00746         {
00747             eZDebug::writeError( "The file '$sourcePath' does not exist, cannot initialize file attribute with it",
00748                                  'eZBinaryFileType::unserializeContentObjectAttribute' );
00749             return false;
00750         }
00751 
00752         //include_once( 'lib/ezfile/classes/ezdir.php' );
00753         $ini = eZINI::instance();
00754         $mimeType = $fileNode->getAttribute( 'mime-type' );
00755         list( $mimeTypeCategory, $mimeTypeName ) = explode( '/', $mimeType );
00756         $destinationPath = eZSys::storageDirectory() . '/original/' . $mimeTypeCategory . '/';
00757         if ( !file_exists( $destinationPath ) )
00758         {
00759             $oldumask = umask( 0 );
00760             if ( !eZDir::mkdir( $destinationPath, false, true ) )
00761             {
00762                 umask( $oldumask );
00763                 return false;
00764             }
00765             umask( $oldumask );
00766         }
00767 
00768         $basename = basename( $fileNode->getAttribute( 'filename' ) );
00769         while ( file_exists( $destinationPath . $basename ) )
00770         {
00771             $basename = substr( md5( mt_rand() ), 0, 8 ) . '.' . eZFile::suffix( $fileNode->getAttribute( 'filename' ) );
00772         }
00773 
00774         //include_once( 'lib/ezfile/classes/ezfilehandler.php' );
00775         eZFileHandler::copy( $sourcePath, $destinationPath . $basename );
00776         eZDebug::writeNotice( 'Copied: ' . $sourcePath . ' to: ' . $destinationPath . $basename,
00777                               'eZBinaryFileType::unserializeContentObjectAttribute()' );
00778 
00779         $binaryFile->setAttribute( 'contentobject_attribute_id', $objectAttribute->attribute( 'id' ) );
00780         $binaryFile->setAttribute( 'filename', $basename );
00781         $binaryFile->setAttribute( 'original_filename', $fileNode->getAttribute( 'original-filename' ) );
00782         $binaryFile->setAttribute( 'mime_type', $fileNode->getAttribute( 'mime-type' ) );
00783 
00784         $binaryFile->store();
00785 
00786 
00787         require_once( 'kernel/classes/ezclusterfilehandler.php' );
00788         $fileHandler = eZClusterFileHandler::instance();
00789         $fileHandler->fileStore( $destinationPath . $basename, 'binaryfile', true );
00790     }
00791 
00792 }
00793 
00794 eZDataType::register( eZBinaryFileType::DATA_TYPE_STRING, "eZBinaryFileType" );
00795 
00796 ?>