/usr/share/php/Horde/Kolab/Storage/Data/Old.php is in php-horde-kolab-storage 2.2.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | <?php
/**
* Handles data objects in a Kolab storage folder.
*
* PHP version 5
*
* @category Kolab
* @package Kolab_Storage
* @author Stuart Binge <omicron@mighty.co.za>
* @author Thomas Jarosch <thomas.jarosch@intra2net.com>
* @author Gunnar Wrobel <wrobel@pardus.de>
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Kolab_Storage
*/
/**
* The Kolab_Data class represents a data type in a Kolab storage
* folder on the Kolab server.
*
* Copyright 2009-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Kolab
* @package Kolab_Storage
* @author Stuart Binge <omicron@mighty.co.za>
* @author Thomas Jarosch <thomas.jarosch@intra2net.com>
* @author Gunnar Wrobel <wrobel@pardus.de>
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Kolab_Storage
*/
class Horde_Kolab_Storage_Data_Old
{
/**
* The link to the parent folder object.
*
* @var Kolab_Folder
*/
private $_folder;
/**
* The folder type.
*
* @var string
*/
private $_type;
/**
* The driver for accessing the Kolab storage system.
*
* @var Horde_Kolab_Storage_Driver
*/
private $_driver;
/**
* The factory for generating additional resources.
*
* @var Horde_Kolab_Storage_Factory
*/
private $_factory;
/**
* The object type of the data.
*
* @var string
*/
private $_object_type;
/**
* The version of the data.
*
* @var int
*/
private $_data_version;
/**
* The data cache.
*
* @var Kolab_Cache
*/
private $_cache;
/**
* The Id of this data object in the cache.
*
* @var string
*/
private $_cache_key;
/**
* An addition to the cache key in case we are operating on
* something other than the default type.
*
* @var string
*/
private $_type_key;
/**
* Do we optimize for cyrus IMAPD?
*
* @var boolean
*/
private $_cache_cyrus_optimize = true;
/**
* Creates a Kolab Folder Data representation.
*
* @param string $folder Name/ID of the folder.
* @param string $type Type of the folder.
* @param Horde_Kolab_Storage_Driver $driver The primary connection driver.
* @param Horde_Kolab_Storage_Factory $factory The factory.
* @param string $object_type Type of the objects we want to read.
* @param int $data_version Format version of the object data.
*/
public function __construct($folder, $type, $driver, $factory, $object_type = null, $data_version = 1)
{
$this->_type = $type;
if (!empty($object_type)) {
$this->_object_type = $object_type;
} else {
$this->_object_type = $type;
}
$this->_data_version = $data_version;
if ($this->_object_type != $this->_type) {
$this->_type_key = '@' . $this->_object_type;
} else {
$this->_type_key = '';
}
}
/**
* Delete the specified message from this folder.
*
* @param string $object_uid Id of the message to be deleted.
*
* @return boolean|PEAR_Error True is successful, false if the
* message does not exist.
*/
public function delete($object_uid)
{
if (!$this->objectUidExists($object_uid)) {
return false;
}
// Find the storage ID
$id = $this->getStorageId($object_uid);
if ($id === false) {
return false;
}
$result = $this->_folder->deleteMessage($id);
if (is_a($result, 'PEAR_Error')) {
return $result;
}
unset($this->_cache->objects[$object_uid]);
unset($this->_cache->uids[$id]);
$this->_cache->save();
return true;
}
/**
* Delete all messages from the current folder.
*
* @return boolean|PEAR_Error True if successful.
*/
public function deleteAll()
{
$this->_cache->load($this->_cache_key, $this->_data_version);
if (empty($this->_cache->uids)) {
return true;
}
foreach ($this->_cache->uids as $id => $object_uid) {
$this->_folder->deleteMessage($id, false);
unset($this->_cache->objects[$object_uid]);
unset($this->_cache->uids[$id]);
}
$this->_cache->save();
$result = $this->_folder->trigger();
if (is_a($result, 'PEAR_Error')) {
Horde::log(sprintf('Failed triggering folder %s!', $this->_folder->name), 'ERR');
}
return true;
}
/**
* Move the specified message from the current folder into a new
* folder.
*
* @param string $object_uid ID of the message to be deleted.
* @param string $new_share ID of the target share.
*
* @return boolean|PEAR_Error True is successful, false if the
* object does not exist.
*/
public function move($object_uid, $new_share)
{
if (!$this->objectUidExists($object_uid)) {
return false;
}
// Find the storage ID
$id = $this->getStorageId($object_uid);
if ($id === false) {
return false;
}
$result = $this->_folder->moveMessageToShare($id, $new_share);
unset($this->_cache->objects[$object_uid]);
unset($this->_cache->uids[$id]);
$this->_cache->save();
return true;
}
/**
* Save an object.
*
* @param array $object The array that holds the data object.
* @param string $old_object_id The id of the object if it existed before.
*
* @return boolean True on success.
*
* @throws Horde_Kolab_Storage_Exception In case the given old object id
* is invalid or an error occured
* while saving the data.
*/
public function save($object, $old_object_id = null)
{
// update existing kolab object
if ($old_object_id != null) {
// check if object really exists
if (!$this->objectUidExists($old_object_id)) {
throw new Horde_Kolab_Storage_Exception(sprintf(Horde_Kolab_Storage_Translation::t("Old object %s does not exist."),
$old_object_id));
}
// get the storage ID
$id = $this->getStorageId($old_object_id);
if ($id === false) {
throw new Horde_Kolab_Storage_Exception(sprintf(Horde_Kolab_Storage_Translation::t("Old object %s does not map to a uid."),
$old_object_id));
}
$old_object = $this->getObject($old_object_id);
} else {
$id = null;
$old_object = null;
}
$this->_folder->saveObject($object, $this->_data_version,
$this->_object_type, $id, $old_object);
$this->synchronize($old_object_id);
return true;
}
/**
* 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');
}
}
}
}
}
/**
* Update the Horde history in case an element was modified
* outside of Horde.
*
* @param string $object_uid Object uid that should be updated.
* @param int $mod_ts Timestamp of the modification.
* @param string $action The action that was performed.
*
* @return NULL
*/
private function _updateHistory($object_uid, $mod_ts, $action)
{
global $registry;
if (!isset($registry)) {
return;
}
$app = $registry->getApp();
if (empty($app) || is_a($app, 'PEAR_Error')) {
/**
* Ignore the history if we are not in application
* context.
*/
return $app;
}
if (!class_exists('Horde_History')) {
return;
}
/* Log the action on this item in the history log. */
try {
$GLOBALS['injector']->getInstance('Horde_History')
->log($app . ':' . $this->_folder->getShareId() . ':' . $object_uid,
array('action' => $action, 'ts' => $mod_ts),
true);
} catch (Horde_Exception $e) {
}
}
/**
* Test if the storage ID exists.
*
* @param int $uid The storage ID.
*
* @return boolean True if the ID exists.
*/
public function storageIdExists($uid)
{
$this->_cache->load($this->_cache_key, $this->_data_version);
return array_key_exists($uid, $this->_cache->uids);
}
/**
* Generate a unique object id.
*
* @return string The unique id.
*/
public function generateUID()
{
do {
$key = md5(uniqid(mt_rand(), true));
} while ($this->objectUidExists($key));
return $key;
}
/**
* Return the specified attachment.
*
* @param string $attachment_id The attachment id.
*
* @return string|PEAR_Error The attachment data as a string.
*/
public function getAttachment($attachment_id)
{
$this->_cache->load($this->_cache_key, $this->_data_version);
return $this->_cache->loadAttachment($attachment_id);
}
/**
* Retrieve all objects in the current folder as an array.
*
* @return array The object data array.
*/
public function getObjectArray()
{
$this->_cache->load($this->_cache_key, $this->_data_version);
return $this->_cache->objects;
}
}
|