function event_hook($event, &$bag, &$eventData, $addData = null)
 {
     global $serendipity;
     static $i = null;
     static $keys = array('audio' => array('channels' => 'AudioChannels', 'channelmode' => 'AudioChannelMode', 'sample_rate' => 'AudioSampleRate', 'bitrate_mode' => 'AudioBitrateMode', 'codec' => 'AudioCodec'), 'video' => array('bitrate_mode' => 'VideoBitrateMode', 'resolution_x' => 'VideoWidth', 'resolution_y' => 'VideoHeight', 'frame_rate' => 'VideoFrameRate', 'codec' => 'VideoCodec'), 'main' => array('playtime_seconds' => 'RunLength', 'bitrate' => 'Bitrate', 'mime_type' => 'Mime-Type'));
     $hooks =& $bag->get('event_hooks');
     if (isset($hooks[$event])) {
         switch ($event) {
             case 'media_getproperties_cached':
             case 'media_getproperties':
                 if ($i === null) {
                     // Try to find the getid3 library in the bundled-libs first:
                     if (file_exists(dirname(__FILE__) . '/../../bundled-libs/getid3/getid3.lib.php')) {
                         @define('GETID3_INCLUDEPATH', dirname(__FILE__) . '/../../bundled-libs/getid3/');
                     } else {
                         if (file_exists(dirname(__FILE__) . '/getid3/getid3.lib.php')) {
                             @define('GETID3_INCLUDEPATH', dirname(__FILE__) . '/getid3/');
                         } else {
                             $eventData['ID3']['ERROR'] = PLUGIN_GETID3_LIBNOTFOUND;
                             break;
                         }
                     }
                     require_once GETID3_INCLUDEPATH . 'getid3.php';
                     $i = new GetID3();
                     $i->encoding = LANG_CHARSET;
                 }
                 $id3 =& $i->analyze($addData);
                 getid3_lib::CopyTagsToComments($id3);
                 foreach ($keys['audio'] as $audiokey => $audiodesc) {
                     if (!empty($id3['audio'][$audiokey])) {
                         $eventData['ID3'][$audiodesc] = $id3['audio'][$audiokey];
                     }
                 }
                 foreach ($keys['video'] as $videokey => $videodesc) {
                     if (!empty($id3['video'][$videokey])) {
                         $eventData['ID3'][$videodesc] = $id3['video'][$videokey];
                     }
                 }
                 foreach ($keys['main'] as $mainkey => $maindesc) {
                     if (!empty($id3[$mainkey])) {
                         $eventData['ID3'][$maindesc] = $id3[$mainkey];
                     }
                 }
                 if (!empty($eventData['ID3']['RunLength'])) {
                     $eventData['ID3']['RunLength'] = $this->parseSec($eventData['ID3']['RunLength']);
                 }
                 if (!empty($eventData['ID3']['Bitrate'])) {
                     $eventData['ID3']['Bitrate'] = round($eventData['ID3']['Bitrate'] / 1000, 2) . ' kbit/sec';
                 }
                 break;
         }
     }
 }
 /**
  * adds a resource to the gallery when the resource is already stored on disk, instead of
  * it coming from an upload as it usually happens. This method is better than 
  * GalleryResources::addResource() when instead of dealing with uploaded files, the file
  * is already in disk and all that is left to do is to add it to the database.
  *
  * @param ownerId
  * @param albumId
  * @param description
  * @param fullFilePath The real path where the file is stored. This is expected to be
  * its final and permanent destination
  * @return It will return one of the following constants:
  * - GALLERY_ERROR_RESOURCE_TOO_BIG
  * - GALLERY_ERROR_RESOURCE_FORBIDDEN_EXTENSION
  * - GALLERY_ERROR_QUOTA_EXCEEDED
  * - GALLERY_ERROR_ADDING_RESOURCE
  * - GALLERY_ERROR_UPLOADS_NOT_ENABLED
  * or the identifier of the resource that was just added if the operation succeeded.
  */
 function addResourceFromDisk($ownerId, $albumId, $description, $fullFilePath)
 {
     // check if quotas are enabled, and if this file would make us go
     // over the quota
     if (GalleryResourceQuotas::isBlogOverResourceQuota($ownerId, File::getSize($fullFilePath))) {
         return GALLERY_ERROR_QUOTA_EXCEEDED;
     }
     $fileName = basename($fullFilePath);
     $filePath = dirname($fullFilePath);
     // get the metadata
     $getId3 = new GetID3();
     $metadata = $getId3->analyze($fullFilePath);
     // nifty helper method from the getid3 package
     getid3_lib::CopyTagsToComments($metadata);
     $resourceType = $this->_getResourceType($fullFilePath, $metadata);
     $info = $this->_filterMetadata($metadata, $resourceType);
     // set the flags
     $flags = 0;
     if ($resourceType == GALLERY_RESOURCE_IMAGE) {
         $flags = $flags | GALLERY_RESOURCE_PREVIEW_AVAILABLE;
     }
     // add the record to the database
     $resourceId = $this->addResourceToDatabase($ownerId, $albumId, $description, $flags, $resourceType, $filePath, $fileName, $metadata);
     if (!$resourceId) {
         return false;
     }
     // and finally move the file to the right place in disk
     // move the file to disk
     $storage = new GalleryResourceStorage();
     $resFile = $storage->storeFile($resourceId, $ownerId, $fullFilePath, RESOURCE_STORAGE_STORE_MOVE);
     // if the file cannot be read, we will also remove the record from the
     // database so that we don't screw up
     $fileReadable = File::isReadable($resFile);
     if (!$resFile || $resFile < 0 || !$fileReadable) {
         // if something went wrong, we should not keep the record in the db
         $query = "DELETE FROM " . $this->getPrefix() . "gallery_resources\n                          WHERE id = {$resourceId}";
         $this->Execute($query);
         return $resFile;
     }
     // and finally, we can generate the thumbnail only if the file is an image, of course :)
     if ($resourceType == GALLERY_RESOURCE_IMAGE) {
         $this->generateResourceThumbnail($resFile, $resourceId, $ownerId);
     }
     // return the id of the resource we just added
     return $resourceId;
 }