/usr/share/php/Horde/ActiveSync/State/Base.php is in php-horde-activesync 2.19.2-2.
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 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 | <?php
/**
* Horde_ActiveSync_State_Base::
*
* @license http://www.horde.org/licenses/gpl GPLv2
* NOTE: According to sec. 8 of the GENERAL PUBLIC LICENSE (GPL),
* Version 2, the distribution of the Horde_ActiveSync module in or
* to the United States of America is excluded from the scope of this
* license.
* @copyright 2009-2014 Horde LLC (http://www.horde.org)
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @package ActiveSync
*/
/**
* Base class for managing everything related to device state
*
* @license http://www.horde.org/licenses/gpl GPLv2
* NOTE: According to sec. 8 of the GENERAL PUBLIC LICENSE (GPL),
* Version 2, the distribution of the Horde_ActiveSync module in or
* to the United States of America is excluded from the scope of this
* license.
* @copyright 2009-2014 Horde LLC (http://www.horde.org)
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @package ActiveSync
*/
abstract class Horde_ActiveSync_State_Base
{
/**
* Configuration parameters
*
* @var array
*/
protected $_params;
/**
* Caches the current state(s) in memory
*
* @var mixed Horde_ActiveSync_Folder_Base if request is not a FOLDERSYNC
* otherwise an array containing all FOLDERSYNC state.
*/
protected $_folder;
/**
* The syncKey for the current request.
*
* @var string
*/
protected $_syncKey;
/**
* The backend driver
*
* @param Horde_ActiveSync_Driver_Base
*/
protected $_backend;
/**
* The process id (used for logging).
*
* @var integer
*/
protected $_procid;
/**
* The timestamp for the last syncKey
*
* @var timestamp
*/
protected $_lastSyncStamp = 0;
/**
* The current sync timestamp
*
* @var timestamp
*/
protected $_thisSyncStamp = 0;
/**
* The collection array for the collection we are currently syncing.
* Keys include:
* - class: The collection class Contacts, Calendar etc...
* - synckey: The current synckey
* - newsynckey: The new synckey sent back to the PIM
* - id: Server folder id
* - filtertype: Filter
* - conflict: Conflicts
* - truncation: Truncation
*
* @var array
*/
protected $_collection;
/**
* Logger instance
*
* @var Horde_Log_Logger
*/
protected $_logger;
/**
* Device object.
*
* @var Horde_ActiveSync_Device
*/
protected $_deviceInfo;
/**
* Local cache for changes to *send* to PIM
* (Will remain null until getChanges() is called)
*
* @var array
*/
protected $_changes;
/**
* The type of request we are handling.
*
* @var string
*/
protected $_type;
/**
* A map of backend folderids to UIDs
*
* @var array
*/
protected $_folderUidMap;
/**
* Const'r
*
* @param array $params All configuration parameters.
*/
public function __construct(array $params = array())
{
$this->_params = $params;
if (empty($params['logger'])) {
$this->_logger = new Horde_Support_Stub();
} else {
$this->_logger = $params['logger'];
}
$this->_procid = getmypid();
}
/**
* Destructor
*/
public function __destruct()
{
unset($this->_backend);
}
/**
* Update the $oldKey syncState to $newKey.
*
* @param string $newKey
*
* @return void
*/
public function setNewSyncKey($newKey)
{
$this->_syncKey = $newKey;
}
/**
* Get the current synckey
*
* @return string The synkey we last retrieved state for
*/
public function getCurrentSyncKey()
{
return $this->_syncKey;
}
/**
* Generate a random 10 digit policy key
*
* @return unknown
*/
public function generatePolicyKey()
{
return mt_rand(1000000000, 9999999999);
}
/**
* Obtain the current policy key, if it exists.
*
* @param string $devId The device id to obtain policy key for.
*
* @return integer The current policy key for this device, or 0 if none
* exists.
*/
public function getPolicyKey($devId)
{
/* See if we have it already */
if (empty($this->_deviceInfo) || $this->_deviceInfo->id != $devId) {
throw new Horde_ActiveSync_Exception('Device not loaded.');
}
return $this->_deviceInfo->policykey;
}
/**
* Return a device wipe status
*
* @param string $devId
*
* @return integer
*/
public function getDeviceRWStatus($devId)
{
/* See if we have it already */
if (empty($this->_deviceInfo) || $this->_deviceInfo->id != $devId) {
throw new Horde_ActiveSync_Exception('Device not loaded.');
}
return $this->_deviceInfo->rwstatus;
}
/**
* Set the backend driver
* (should really only be called by a backend object when passing this
* object to client code)
*
* @param Horde_ActiveSync_Driver_Base $backend The backend driver
*
* @return void
*/
public function setBackend(Horde_ActiveSync_Driver_Base $backend)
{
$this->_backend = $backend;
}
/**
* Set the logger instance for this object.
*
* @param Horde_Log_Logger $logger
*/
public function setLogger($logger)
{
$this->_logger = $logger;
}
/**
* Get the number of server changes.
*
* @return integer
*/
public function getChangeCount()
{
if (!isset($this->_changes)) {
$this->getChanges();
}
return count($this->_changes);
}
/**
* Determines if the server version of the message represented by $stat
* conflicts with the PIM version of the message. For this driver, this is
* true whenever $lastSyncTime is older then $stat['mod']. Method is only
* called from the Importer during an import of a non-new change from the
* PIM.
*
* @param array $stat A message stat array
* @param string $type The type of change (change, delete, add)
*
* @return boolean
*/
public function isConflict($stat, $type)
{
// $stat == server's message information
if ($stat['mod'] > $this->_lastSyncStamp &&
($type == Horde_ActiveSync::CHANGE_TYPE_DELETE ||
$type == Horde_ActiveSync::CHANGE_TYPE_CHANGE)) {
// changed here - deleted there
// changed here - changed there
return true;
}
return false;
}
/**
* Return an array of known folders. This is essentially the state for a
* FOLDERSYNC request. AS uses a seperate synckey for FOLDERSYNC requests
* also, so need to treat it as any other collection.
*
* @return array An array of folder uids.
*/
public function getKnownFolders()
{
if (!isset($this->_folder)) {
throw new Horde_ActiveSync_Exception('Sync state not loaded');
}
$folders = array();
foreach ($this->_folder as $folder) {
$folders[] = $folder['id'];
}
return $folders;
}
/**
* Return the mapping of folder uids to backend folderids.
*
* @return array An array of backend folderids -> uids.
* @since 2.9.0
*/
public function getFolderUidToBackendIdMap()
{
if (!isset($this->_folderUidMap)) {
$this->_folderUidMap = array();
$cache = $this->getSyncCache(
$this->_deviceInfo->id,
$this->_deviceInfo->user,
array('folders'));
foreach ($cache['folders'] as $id => $folder) {
$this->_folderUidMap[$folder['serverid']] = $id;
}
}
return $this->_folderUidMap;
}
/**
* Get a EAS Folder Uid for the given backend server id.
*
* @param string $serverid The backend server id. E.g., 'INBOX'.
*
* @return string|boolean The EAS UID for the requested serverid, or false
* if it is not found.
* @since 2.4.0
* @deprecated Use self::getFolderUidToBackendIdMap() instead.
*/
public function getFolderUidForBackendId($serverid)
{
$cache = $this->getSyncCache(
$this->_deviceInfo->id,
$this->_deviceInfo->user);
$folders = $cache['folders'];
foreach ($folders as $id => $folder) {
if ($folder['serverid'] == $serverid) {
$this->_logger->info(sprintf(
'[%s] Found serverid for %s: %s',
$this->_procid,
$serverid,
$id));
return $id;
}
}
$this->_logger->info(sprintf(
'[%s] No folderid found for %s',
$this->_procid,
$serverid));
return false;
}
/**
* Get all items that have changed since the last sync time
*
* @param array $options An options array:
* - ping: (boolean) Only detect if there is a change, do not build
* any messages.
* DEFAULT: false (Build full change array).
*
* @return array An array of hashes describing each change:
* - id: The id of the item being changed.
* - type: The type of change. a Horde_ActiveSync::CHANGE_TYPE_*
* constant.
* - flags: Used to transport email message flags when type is
* Horde_ActiveSync::CHANGE_TYPE_FLAGS or set to
* Horde_ActiveSync::FLAG_NEWMESSAGE when type is
* Horde_ActiveSync::CHANGE_TYPE_CHANGE and the message
* represents a new message, as opposed to a change in an
* existing message.
* - ignore: Set to true when the change should be ignored, and not sent
* to the client by the exporter. Usually due to the change
* being the result of a client originated change.
*
* @throws Horde_ActiveSync_Exception_StaleState
*/
public function getChanges(array $options = array())
{
if (!empty($this->_collection['id'])) {
// How far back to sync (for those collections that use this)
$cutoffdate = self::_getCutOffDate(!empty($this->_collection['filtertype'])
? $this->_collection['filtertype']
: 0);
$this->_logger->info(sprintf(
'[%s] Initializing message diff engine for %s (%s)',
$this->_procid,
$this->_collection['id'],
$this->_folder->serverid()
));
if (!empty($this->_changes)) {
$this->_logger->info(sprintf(
'[%s] Returning previously found changes.',
$this->_procid));
return $this->_changes;
}
// Get the current syncStamp from the backend.
$this->_thisSyncStamp = $this->_backend->getSyncStamp(
$this->_folder->serverid(),
$this->_lastSyncStamp);
if ($this->_thisSyncStamp === false) {
throw new Horde_ActiveSync_Exception_StaleState(
'Detecting a change in timestamp or modification sequence. Reseting state.');
}
$this->_logger->info(sprintf(
'[%s] Using SYNCSTAMP %s for %s.',
$this->_procid,
$this->_thisSyncStamp,
$this->_collection['id']));
// No existing changes, poll the backend
$changes = $this->_backend->getServerChanges(
$this->_folder,
(int)$this->_lastSyncStamp,
(int)$this->_thisSyncStamp,
$cutoffdate,
!empty($options['ping']),
$this->_folder->haveInitialSync,
!empty($options['maxitems']) ? $options['maxitems'] : 100
);
// Only update the folderstate if we are not PINGing.
if (empty($options['ping'])) {
$this->_folder->updateState();
}
$this->_logger->info(sprintf(
'[%s] Found %d message changes in %s.',
$this->_procid,
count($changes),
$this->_collection['id']));
// Check changes for mirrored client chagnes, but only if we KNOW
// we have some client changes.
$this->_changes = array();
if (count($changes) && $this->_havePIMChanges()) {
$this->_logger->info(sprintf(
'[%s] Checking for PIM initiated changes.',
$this->_procid));
switch ($this->_collection['class']) {
case Horde_ActiveSync::CLASS_EMAIL:
$mailmap = $this->_getMailMapChanges($changes);
$flag_map = array(
Horde_ActiveSync::CHANGE_TYPE_FLAGS => 'flag change',
Horde_ActiveSync::CHANGE_TYPE_DELETE => 'deletion',
Horde_ActiveSync::CHANGE_TYPE_CHANGE => 'move'
);
foreach ($changes as $change) {
if (!empty($mailmap[$change['id']][$change['type']])) {
// @todo For 3.0, create a Changes and
// ChangeFilter classes to abstract out a bunch of
// this stuff. (Needs BC breaking changes in
// storage/state classes).
//
// OL2013 is broken and duplicates the destination
// email during MOVEITEMS requests (instead it
// reassigns the existing email the new UID). Don't
// send the ADD command for these changes.
if ($change['type'] == Horde_ActiveSync::CHANGE_TYPE_CHANGE &&
$change['flags'] == Horde_ActiveSync::FLAG_NEWMESSAGE &&
$this->_deviceInfo->deviceType != 'WindowsOutlook15') {
$this->_changes[] = $change;
continue;
}
$this->_logger->info(sprintf(
'[%s] Ignoring PIM initiated %s for %s',
$this->_procid,
$flag_map[$change['type']],
$change['id']));
$change['ignore'] = true;
}
$this->_changes[] = $change;
}
break;
default:
$pim_timestamps = $this->_getPIMChangeTS($changes);
foreach ($changes as $change) {
if (empty($pim_timestamps[$change['id']])) {
$this->_changes[] = $change;
continue;
}
if ($change['type'] == Horde_ActiveSync::CHANGE_TYPE_DELETE) {
// If we have a delete, don't bother stating the message,
// the entry should already be deleted on the client.
$stat['mod'] = 0;
} else {
// stat only returns MODIFY times, not deletion times,
// so will return (int)0 for ADD or DELETE.
$stat = $this->_backend->statMessage($this->_folder->serverid(), $change['id']);
}
if ($pim_timestamps[$change['id']] >= $stat['mod']) {
$this->_logger->info(sprintf(
'[%s] Ignoring PIM initiated change for %s (PIM TS: %s Stat TS: %s)',
$this->_procid,
$change['id'], $pim_timestamps[$change['id']], $stat['mod']));
} else {
$this->_changes[] = $change;
}
}
}
} elseif (count($changes)) {
$this->_logger->info(sprintf(
'[%s] No PIM changes present, returning all messages.',
$this->_procid));
$this->_changes = $changes;
}
} else {
$this->_getFolderChanges();
}
return $this->_changes;
}
/**
* Get folder changes. Populates $this->_changes with an array of change
* entries each containing 'type', 'id' and possibly 'flags'.
*/
protected function _getFolderChanges()
{
$this->_logger->info(sprintf(
'[%s] Initializing folder diff engine',
$this->_procid));
$folderlist = $this->_backend->getFolderList();
if ($folderlist === false) {
return false;
}
// @TODO Remove in H6. We need to ensure we have 'serverid' in the
// returned stat data.
foreach ($folderlist as &$folder) {
// Either the backend populates this or not. So, if we have it, we
// can stop checking.
if (!empty($folder['serverid'])) {
break;
} else {
$folder['serverid'] = $folder['id'];
}
}
$this->_changes = $this->_getDiff(
(empty($this->_folder) ? array() : $this->_folder),
$folderlist);
if (!count($this->_changes)) {
$this->_logger->info(sprintf(
'[%s] No folder changes found.',
$this->_procid));
} else {
$this->_logger->info(sprintf(
'[%s] Found %d folder changes.',
$this->_procid,
count($this->_changes)));
}
}
/**
* Gets the new sync key for a specified sync key. You must save the new
* sync state under this sync key when done sync'ing by calling
* setNewSyncKey(), then save().
*
* @param string $syncKey The old syncKey
*
* @return string The new synckey
* @throws Horde_ActiveSync_Exception
*/
static public function getNewSyncKey($syncKey)
{
if (empty($syncKey)) {
return '{' . new Horde_Support_Uuid() . '}' . '1';
} else {
if (preg_match('/^\{([a-fA-F0-9-]+)\}([0-9]+)$/', $syncKey, $matches)) {
$n = $matches[2];
$n++;
return '{' . $matches[1] . '}' . $n;
}
throw new Horde_ActiveSync_Exception('Invalid SyncKey format passed to getNewSyncKey()');
}
}
/**
* Return the counter for the specified syncKey.
*
* @param string $syncKey The synckey to obtain the counter for.
*
* @return mixed integer|boolean The increment counter or false if failed.
*/
static public function getSyncKeyCounter($syncKey)
{
if (preg_match('/^\{([a-fA-F0-9-]+)\}([0-9]+)$/', $syncKey, $matches)) {
$n = $matches[2];
return $n;
}
return false;
}
/**
* Return the UID portion of a synckey.
*
* @param string $syncKey The synckey
*
* @return string The UID.
*/
static public function getSyncKeyUid($syncKey)
{
if (preg_match('/^(\{[a-fA-F0-9-]+\})([0-9]+)$/', $syncKey, $matches)) {
return $matches[1];
}
}
/**
* Returns the timestamp of the earliest modification time to consider
*
* @param integer $restrict The time period to restrict to
*
* @return integer
*/
static protected function _getCutOffDate($restrict)
{
// @todo Should just pass the filtertype to the driver instead
// of parsing it here, let the driver figure out what to do with it.
if ($restrict == Horde_ActiveSync::FILTERTYPE_INCOMPLETETASKS) {
return $restrict;
}
switch($restrict) {
case Horde_ActiveSync::FILTERTYPE_1DAY:
$back = 86400;
break;
case Horde_ActiveSync::FILTERTYPE_3DAYS:
$back = 259200;
break;
case Horde_ActiveSync::FILTERTYPE_1WEEK:
$back = 604800;
break;
case Horde_ActiveSync::FILTERTYPE_2WEEKS:
$back = 1209600;
break;
case Horde_ActiveSync::FILTERTYPE_1MONTH:
$back = 2419200;
break;
case Horde_ActiveSync::FILTERTYPE_3MONTHS:
$back = 7257600;
break;
case Horde_ActiveSync::FILTERTYPE_6MONTHS:
$back = 14515200;
break;
default:
break;
}
if (isset($back)) {
$date = time() - $back;
return $date;
} else {
return 0; // unlimited
}
}
/**
* Helper function that performs the actual diff between PIM state and
* server state FOLDERSYNC arrays.
*
* @param array $old The PIM state
* @param array $new The current server state
*
* @return unknown_type
*/
protected function _getDiff($old, $new)
{
$changes = array();
// Sort both arrays in the same way by ID
usort($old, array(__CLASS__, 'RowCmp'));
usort($new, array(__CLASS__, 'RowCmp'));
$inew = 0;
$iold = 0;
// Get changes by comparing our list of folders with
// our previous state
while (1) {
$change = array();
if ($iold >= count($old) || $inew >= count($new)) {
break;
}
// If ids are the same, but mod is different, a folder was
// renamed on the client, but the server keeps it's id.
if ($old[$iold]['id'] == $new[$inew]['id']) {
// Both folders are still available compare mod
if ($old[$iold]['mod'] != $new[$inew]['mod']) {
$change['type'] = Horde_ActiveSync::CHANGE_TYPE_CHANGE;
$change['id'] = $new[$inew]['id'];
$change['serverid'] = $new[$inew]['serverid'];
$changes[] = $change;
}
$inew++;
$iold++;
} else {
if ($old[$iold]['id'] > $new[$inew]['id']) {
// Messesge in device state has disappeared
$change['type'] = Horde_ActiveSync::CHANGE_TYPE_DELETE;
$change['id'] = $old[$iold]['id'];
$change['serverid'] = $old[$iold]['serverid'];
$changes[] = $change;
$iold++;
} else {
// Message in $new is new
$change['type'] = Horde_ActiveSync::CHANGE_TYPE_CHANGE;
$change['id'] = $new[$inew]['id'];
$change['serverid'] = $new[$inew]['serverid'];
$changes[] = $change;
$inew++;
}
}
}
while ($iold < count($old)) {
// All data left in _syncstate have been deleted
$change['type'] = Horde_ActiveSync::CHANGE_TYPE_DELETE;
$change['id'] = $old[$iold]['id'];
$change['serverid'] = $old[$iold]['serverid'];
$changes[] = $change;
$iold++;
}
// New folders added on server.
while ($inew < count($new)) {
// All data left in new have been added
$change['type'] = Horde_ActiveSync::CHANGE_TYPE_CHANGE;
$change['flags'] = Horde_ActiveSync::FLAG_NEWMESSAGE;
$change['id'] = $new[$inew]['id'];
$change['serverid'] = $new[$inew]['serverid'];
$changes[] = $change;
$inew++;
}
return $changes;
}
/**
* Helper function for the _diff method
*
* @param $a
* @param $b
* @return unknown_type
*/
static public function RowCmp($a, $b)
{
return $a['id'] < $b['id'] ? 1 : -1;
}
/**
* Load and initialize the sync state
*
* @param array $collection The collection array for the collection, if
* a FOLDERSYNC, pass an empty array.
* @param string $syncKey The synckey of the state to load. If empty will
* force a reset of the state for the class
* specified in $id
* @param string $type The type of state a
* Horde_ActiveSync::REQUEST_TYPE constant.
* @param string $id The folder id this state represents. If empty
* assumed to be a foldersync state.
*
* @throws Horde_ActiveSync_Exception, Horde_ActiveSync_Exception_StateGone
*/
public function loadState(array $collection, $syncKey, $type = null, $id = null)
{
// Initialize the local members.
$this->_collection = $collection;
$this->_changes = null;
$this->_type = $type;
// If this is a FOLDERSYNC, mock the device id.
if ($type == Horde_ActiveSync::REQUEST_TYPE_FOLDERSYNC && empty($id)) {
$id = Horde_ActiveSync::REQUEST_TYPE_FOLDERSYNC;
}
// synckey == 0 is an initial sync or reset.
if (empty($syncKey)) {
$this->_logger->notice(sprintf(
'[%s] %s::loadState: clearing folder state.',
$this->_procid,
__CLASS__));
if ($type == Horde_ActiveSync::REQUEST_TYPE_FOLDERSYNC) {
$this->_folder = array();
} else {
// Create a new folder object.
$this->_folder = ($this->_collection['class'] == Horde_ActiveSync::CLASS_EMAIL) ?
new Horde_ActiveSync_Folder_Imap($this->_collection['serverid'], Horde_ActiveSync::CLASS_EMAIL) :
($this->_collection['serverid'] == 'RI' ? new Horde_ActiveSync_Folder_RI('RI', 'RI') : new Horde_ActiveSync_Folder_Collection($this->_collection['serverid'], $this->_collection['class']));
}
$this->_syncKey = '0';
$this->_resetDeviceState($id);
return;
}
$this->_logger->info(
sprintf('[%s] Loading state for synckey %s',
$this->_procid,
$syncKey)
);
// Check if synckey is allowed
if (!preg_match('/^\{([0-9A-Za-z-]+)\}([0-9]+)$/', $syncKey, $matches)) {
throw new Horde_ActiveSync_Exception('Invalid sync key');
}
$this->_syncKey = $syncKey;
// Cleanup older syncstates
$this->_gc($syncKey);
// Load the state
$this->_loadState($type);
}
/**
* Return the most recently seen synckey for the given collection.
*
* @param string $collection_id The activesync collection id.
*
* @return string|integer The synckey or 0 if none found.
*/
public function getLatestSynckeyForCollection($collection_id)
{
// For now, pull in the raw cache_data. Will change when each bit of
// data gets it's own field.
$data = $this->getSyncCache($this->_deviceInfo->id, $this->_deviceInfo->user);
return !empty($data['collections'][$collection_id]['lastsynckey'])
? $data['collections'][$collection_id]['lastsynckey']
: 0;
}
/**
* Load the state represented by $syncKey from storage.
*
* @param string $type The type of state a
* Horde_ActiveSync::REQUEST_TYPE constant.
*
* @throws Horde_ActiveSync_Exception, Horde_ActiveSync_Exception_StateGone
*/
protected function _loadState($type)
{
throw new Horde_ActiveSync_Exception('Must be implemented in concrete class.');
}
/**
* Check for the existence of ANY entries in the map table for this device
* and user.
*
* An extra database query for each sync, but the payoff is that we avoid
* having to stat every message change we send to the PIM if there are no
* PIM generated changes for this sync period.
*
* @return boolean
* @throws Horde_ActiveSync_Exception
*/
protected function _havePIMChanges()
{
throw new Horde_ActiveSync_Exception('Must be implemented in concrete class.');
}
/**
* Return all available mailMap changes for the current folder.
*
* @param array $changes The chagnes array
*
* @return array An array of hashes, each in the form of
* {uid} => array(
* Horde_ActiveSync::CHANGE_TYPE_FLAGS => true|false,
* Horde_ActiveSync::CHANGE_TYPE_DELETE => true|false
* )
*/
protected function _getMailMapChanges(array $changes)
{
throw new Horde_ActiveSync_Exception('Must be implemented in concrete class.');
}
/**
* Save the current syncstate to storage
*/
abstract public function save();
/**
* Update the state to reflect changes
*
* @param string $type The type of change (change, delete, flags or
* foldersync)
* @param array $change A stat/change hash describing the change.
* Contains:
* - id: The message uid the change applies to
* - parent: The parent of the message, normally the folder id.
* - flags: If this is a flag change, the state of the read flag.
* - mod: The modtime of this change for collections that use it.
*
* @param integer $origin Flag to indicate the origin of the change:
* Horde_ActiveSync::CHANGE_ORIGIN_NA - Not applicapble/not important
* Horde_ActiveSync::CHANGE_ORIGIN_PIM - Change originated from PIM
*
* @param string $user The current sync user, only needed if change
* origin is CHANGE_ORIGIN_PIM
* @param string $clientid PIM clientid sent when adding a new message
*/
abstract public function updateState(
$type, array $change, $origin = Horde_ActiveSync::CHANGE_ORIGIN_NA,
$user = null, $clientid = '');
/**
* Save a new device policy key to storage.
*
* @param string $devId The device id
* @param integer $key The new policy key
*/
abstract public function setPolicyKey($devId, $key);
/**
* Reset ALL device policy keys. Used when server policies have changed
* and you want to force ALL devices to pick up the changes. This will
* cause all devices that support provisioning to be reprovisioned.
*
* @throws Horde_ActiveSync_Exception
*/
abstract public function resetAllPolicyKeys();
/**
* Set a new remotewipe status for the device
*
* @param string $devId The device id.
* @param string $status A Horde_ActiveSync::RWSTATUS_* constant.
*
* @throws Horde_ActiveSync_Exception
*/
abstract public function setDeviceRWStatus($devId, $status);
/**
* Obtain the device object.
*
* @param object $device
* @param string $user
*
* @return Horde_ActiveSync_Device
*/
abstract public function loadDeviceInfo($device, $user = null);
/**
* Check that a given device id is known to the server. This is regardless
* of Provisioning status.
*
* @param string $devId The device id to check
* @param string $user The device should be owned by this user.
*
* @return boolean
*/
abstract public function deviceExists($devId, $user = null);
/**
* Set new device info
*
* @param object $data The device information
* @param array $dirty Array of dirty properties. @since 2.9.0
*/
abstract public function setDeviceInfo(Horde_ActiveSync_Device $data, array $dirty = array());
/**
* Set the device's properties as sent by a SETTINGS request.
*
* @param array $data The device settings
* @param string $deviceId The device id.
*
* @throws Horde_ActiveSync_Exception
*/
abstract public function setDeviceProperties(array $data, $deviceId);
/**
* Explicitly remove a state from storage.
*
* @param array $options An options array containing:
* - synckey: (string) Remove only the state associated with this synckey.
* - devId: (string) Remove all information for this device.
* - user: (string) When removing device info, restrict to removing data
* for this user only.
* - id: (string) When removing device state, restrict ro removing data
* only for this collection.
*
* @throws Horde_ActiveSyncException
*/
abstract public function removeState(array $options);
/**
* List all devices that we know about.
*
* @return array An array of device hashes
* @throws Horde_ActiveSync_Exception
*/
abstract public function listDevices();
/**
* Get the last time the currently loaded device issued a SYNC request.
*
* @return integer The timestamp of the last sync, regardless of collection
* @throws Horde_ActiveSync_Exception
*/
abstract public function getLastSyncTimestamp();
/**
* Return the sync cache.
*
* @param string $devid The device id.
* @param string $user The user id.
* @param array $fields An array of fields to return. Default is to return
* the full cache. @since 2.9.0
*
* @return array The current sync cache for the user/device combination.
* @throws Horde_ActiveSync_Exception
*/
abstract public function getSyncCache($devid, $user, array $fields = null);
/**
* Save the provided sync_cache.
*
* @param array $cache The cache to save.
* @param string $devid The device id.
* @param string $user The userid.
* @param array $dirty An array of dirty properties. @since 2.9.0
*
* @throws Horde_ActiveSync_Exception
*/
abstract public function saveSyncCache(array $cache, $devid, $user, array $dirty = null);
/**
* Delete a complete sync cache
*
* @param string $devid The device id
* @param string $user The user name.
*
* @throws Horde_ActiveSync_Exception
*/
abstract public function deleteSyncCache($devid, $user);
/**
* Check and see that we didn't already see the incoming change from the PIM.
* This would happen e.g., if the PIM failed to receive the server response
* after successfully importing new messages.
*
* @param string $id The client id sent during message addition.
*
* @return string The UID for the given clientid, null if none found.
* @throws Horde_ActiveSync_Exception
*/
abstract public function isDuplicatePIMAddition($id);
}
|