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