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