Exemplo n.º 1
0
 /**
  * Synchronize the data cache for the current folder.
  *
  * @param string $history_ignore Object uid that should not be
  *                               updated in the History
  *
  * @return NULL
  */
 public function synchronize($history_ignore = null)
 {
     $this->_cache->load($this->_cache_key, $this->_data_version);
     $result = $this->_folder->getStatus();
     list($validity, $nextid, $ids) = $result;
     $changes = $this->_folderChanged($validity, $nextid, array_keys($this->_cache->uids), $ids);
     if ($changes) {
         $modified = array();
         $recent_uids = array_diff($ids, array_keys($this->_cache->uids));
         $formats = $this->_folder->getFormats();
         $handler = Horde_Kolab_Format::factory('Xml', $this->_object_type, $this->_data_version);
         $count = 0;
         foreach ($recent_uids as $id) {
             if ($this->_type == 'annotation' && $id != 1) {
                 continue;
             }
             try {
                 $mime = $this->_folder->parseMessage($id, $handler->getMimeType(), false);
                 $text = $mime[0];
             } catch (Horde_Kolab_Storage_Exception $e) {
                 Horde::log($mime, 'WARN');
                 $text = false;
             }
             if ($text) {
                 $object = $handler->load($text);
                 if (is_a($object, 'PEAR_Error')) {
                     $this->_cache->ignore($id);
                     $object->addUserInfo('STORAGE ID: ' . $id);
                     Horde::log($object, 'WARN');
                     continue;
                 }
             } else {
                 $object = false;
             }
             if ($object !== false) {
                 $message =& $mime[2];
                 $handler_type = $handler->getMimeType();
                 foreach ($message->getParts() as $part) {
                     $name = $part->getName();
                     $type = $part->getType();
                     $dp = $part->getDispositionParameter('x-kolab-type');
                     if (!empty($name) && $type != $handler_type || !empty($dp) && in_array($dp, $formats)) {
                         $object['_attachments'][$name]['type'] = $type;
                         $object['_attachments'][$name]['key'] = $this->_cache_key . '/' . $object['uid'] . ':' . $name;
                         //@todo: Check what to do with this call
                         //$part->transferDecodeContents();
                         $result = $this->_cache->storeAttachment($object['_attachments'][$name]['key'], $part->getContents());
                         if (is_a($result, 'PEAR_Error')) {
                             Horde::log(sprintf('Failed storing attachment of object %s: %s', $id, $result->getMessage()), 'ERR');
                             $object = false;
                             break;
                         }
                     }
                 }
             }
             if ($object !== false) {
                 $this->_cache->store($id, $object['uid'], $object);
                 $mod_ts = time();
                 if (is_array($changes) && in_array($object['uid'], $changes) && $object['uid'] != $history_ignore) {
                     $this->_updateHistory($object['uid'], $mod_ts, 'modify');
                     $modified[] = $object['uid'];
                 } else {
                     $this->_updateHistory($object['uid'], $mod_ts, 'add');
                 }
             } else {
                 $this->_cache->ignore($id);
             }
             // write out cache once in a while so if the browser times out
             // we don't have to start from the beginning.
             if ($count > 500) {
                 $count = 0;
                 $this->_cache->save();
             }
             $count++;
         }
         $this->_cache->save();
         if (is_array($changes)) {
             $deleted = array_diff($changes, $modified);
             foreach ($deleted as $deleted_oid) {
                 if ($deleted_oid != $history_ignore) {
                     $this->_updateHistory($deleted_oid, time(), 'delete');
                 }
             }
         }
     }
 }