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