This file is indexed.

/usr/share/z-push/lib/request/sync.php is in z-push-common 2.3.8-2ubuntu1.

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
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
<?php
/***********************************************
* File      :   sync.php
* Project   :   Z-Push
* Descr     :   Provides the SYNC command
*
* Created   :   16.02.2012
*
* Copyright 2007 - 2016 Zarafa Deutschland GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* Consult LICENSE file for details
************************************************/

class Sync extends RequestProcessor {
    // Ignored SMS identifier
    const ZPUSHIGNORESMS = "ZPISMS";
    private $importer;
    private $globallyExportedItems;
    private $singleFolder;
    private $multiFolderInfo;
    private $startTagsSent = false;
    private $startFolderTagSent = false;

    /**
     * Handles the Sync command
     * Performs the synchronization of messages
     *
     * @param int       $commandCode
     *
     * @access public
     * @return boolean
     */
    public function Handle($commandCode) {
        // Contains all requested folders (containers)
        $sc = new SyncCollections();
        $status = SYNC_STATUS_SUCCESS;
        $wbxmlproblem = false;
        $emptysync = false;
        $this->singleFolder = true;
        $this->multiFolderInfo = array();
        $this->globallyExportedItems = 0;


        // check if the hierarchySync was fully completed
        if (USE_PARTIAL_FOLDERSYNC) {
            if (self::$deviceManager->GetFolderSyncComplete() === false)  {
                ZLog::Write(LOGLEVEL_INFO, "Request->HandleSync(): Sync request aborted, as exporting of folders has not yet completed");
                self::$topCollector->AnnounceInformation("Aborted due incomplete folder sync", true);
                $status = SYNC_STATUS_FOLDERHIERARCHYCHANGED;
            }
            else
                ZLog::Write(LOGLEVEL_INFO, "Request->HandleSync(): FolderSync marked as complete");
        }

        // Start Synchronize
        if(self::$decoder->getElementStartTag(SYNC_SYNCHRONIZE)) {

            // AS 1.0 sends version information in WBXML
            if(self::$decoder->getElementStartTag(SYNC_VERSION)) {
                $sync_version = self::$decoder->getElementContent();
                ZLog::Write(LOGLEVEL_DEBUG, sprintf("WBXML sync version: '%s'", $sync_version));
                if(!self::$decoder->getElementEndTag())
                    return false;
            }

            // Synching specified folders
            // Android still sends heartbeat sync even if all syncfolders are disabled.
            // Check if Folders tag is empty (<Folders/>) and only sync if there are
            // some folders in the request. See ZP-172
            $startTag = self::$decoder->getElementStartTag(SYNC_FOLDERS);
            if(isset($startTag[EN_FLAGS]) && $startTag[EN_FLAGS]) {
                while(self::$decoder->getElementStartTag(SYNC_FOLDER)) {
                    $actiondata = array();
                    $actiondata["requested"] = true;
                    $actiondata["clientids"] = array();
                    $actiondata["modifyids"] = array();
                    $actiondata["removeids"] = array();
                    $actiondata["fetchids"] = array();
                    $actiondata["statusids"] = array();

                    // read class, synckey and folderid without SyncParameters Object for now
                    $class = $synckey = $folderid = false;

                    // if there are already collections in SyncCollections, this is min. the second folder
                    if ($sc->HasCollections()) {
                        $this->singleFolder = false;
                    }

                    //for AS versions < 2.5
                    if(self::$decoder->getElementStartTag(SYNC_FOLDERTYPE)) {
                        $class = self::$decoder->getElementContent();
                        ZLog::Write(LOGLEVEL_DEBUG, sprintf("Sync folder: '%s'", $class));

                        if(!self::$decoder->getElementEndTag())
                            return false;
                    }

                    // SyncKey
                    if(self::$decoder->getElementStartTag(SYNC_SYNCKEY)) {
                        $synckey = "0";
                        if (($synckey = self::$decoder->getElementContent()) !== false) {
                            if(!self::$decoder->getElementEndTag()) {
                                return false;
                            }
                        }
                    }
                    else
                        return false;

                    // FolderId
                    if(self::$decoder->getElementStartTag(SYNC_FOLDERID)) {
                        $folderid = self::$decoder->getElementContent();

                        if(!self::$decoder->getElementEndTag())
                            return false;
                    }

                    // compatibility mode AS 1.0 - get folderid which was sent during GetHierarchy()
                    if (! $folderid && $class) {
                        $folderid = self::$deviceManager->GetFolderIdFromCacheByClass($class);
                    }

                    // folderid HAS TO BE known by now, so we retrieve the correct SyncParameters object for an update
                    try {
                        $spa = self::$deviceManager->GetStateManager()->GetSynchedFolderState($folderid);

                        // TODO remove resync of folders for < Z-Push 2 beta4 users
                        // this forces a resync of all states previous to Z-Push 2 beta4
                        if (! $spa instanceof SyncParameters)
                            throw new StateInvalidException("Saved state are not of type SyncParameters");

                        // new/resync requested
                        if ($synckey == "0") {
                            $spa->RemoveSyncKey();
                            $spa->DelFolderStat();
                            $spa->SetMoveState(false);
                        }
                        else if ($synckey !== false) {
                            if (($synckey !== $spa->GetSyncKey() && $synckey !== $spa->GetNewSyncKey()) || !!$spa->GetMoveState()) {
                                ZLog::Write(LOGLEVEL_DEBUG, "HandleSync(): Synckey does not match latest saved for this folder or there is a move state, removing folderstat to force Exporter setup");
                                $spa->DelFolderStat();
                            }
                            $spa->SetSyncKey($synckey);
                        }
                    }
                    catch (StateInvalidException $stie) {
                        $spa = new SyncParameters();
                        $status = SYNC_STATUS_INVALIDSYNCKEY;
                        self::$topCollector->AnnounceInformation("State invalid - Resync folder", $this->singleFolder);
                        self::$deviceManager->ForceFolderResync($folderid);
                        $this->saveMultiFolderInfo("exception", "StateInvalidException");
                    }

                    // update folderid.. this might be a new object
                    $spa->SetFolderId($folderid);
                    $spa->SetBackendFolderId(self::$deviceManager->GetBackendIdForFolderId($folderid));

                    if ($class !== false)
                        $spa->SetContentClass($class);

                    // Get class for as versions >= 12.0
                    if (! $spa->HasContentClass()) {
                        try {
                            $spa->SetContentClass(self::$deviceManager->GetFolderClassFromCacheByID($spa->GetFolderId()));
                            ZLog::Write(LOGLEVEL_DEBUG, sprintf("GetFolderClassFromCacheByID from Device Manager: '%s' for id:'%s'", $spa->GetContentClass(), $spa->GetFolderId()));
                        }
                        catch (NoHierarchyCacheAvailableException $nhca) {
                            $status = SYNC_STATUS_FOLDERHIERARCHYCHANGED;
                            self::$deviceManager->ForceFullResync();
                        }
                    }

                    // determine if this is the KOE GAB folder so it can be prioritized by SyncCollections
                    if (KOE_CAPABILITY_GAB && self::$deviceManager->IsKoe() && $spa->GetBackendFolderId() == self::$deviceManager->GetKoeGabBackendFolderId()) {
                        $spa->SetKoeGabFolder(true);
                    }

                    // done basic SPA initialization/loading -> add to SyncCollection
                    $sc->AddCollection($spa);
                    $sc->AddParameter($spa, "requested", true);

                    if ($spa->HasContentClass())
                        self::$topCollector->AnnounceInformation(sprintf("%s request", $spa->GetContentClass()), $this->singleFolder);
                    else
                        ZLog::Write(LOGLEVEL_WARN, "Not possible to determine class of request. Request did not contain class and apparently there is an issue with the HierarchyCache.");

                    // SUPPORTED properties
                    if(($se = self::$decoder->getElementStartTag(SYNC_SUPPORTED)) !== false) {
                        // ZP-481: LG phones send an empty supported tag, so only read the contents if available here
                        // if <Supported/> is received, it's as no supported fields would have been sent at all.
                        // unsure if this is the correct approach, or if in this case some default list should be used
                        if ($se[EN_FLAGS] & EN_FLAGS_CONTENT) {
                            $supfields = array();
                            WBXMLDecoder::ResetInWhile("syncSupported");
                            while(WBXMLDecoder::InWhile("syncSupported")) {
                                $el = self::$decoder->getElement();

                                if($el[EN_TYPE] == EN_TYPE_ENDTAG)
                                    break;
                                else
                                    $supfields[] = $el[EN_TAG];
                            }
                            self::$deviceManager->SetSupportedFields($spa->GetFolderId(), $supfields);
                        }
                    }

                    // Deletes as moves can be an empty tag as well as have value
                    if(self::$decoder->getElementStartTag(SYNC_DELETESASMOVES)) {
                        $spa->SetDeletesAsMoves(true);
                        if (($dam = self::$decoder->getElementContent()) !== false) {
                            $spa->SetDeletesAsMoves((bool)$dam);
                            if(!self::$decoder->getElementEndTag()) {
                                return false;
                            }
                        }
                    }

                    // Get changes can be an empty tag as well as have value
                    // code block partly contributed by dw2412
                    if(self::$decoder->getElementStartTag(SYNC_GETCHANGES)) {
                        $sc->AddParameter($spa, "getchanges", true);
                        if (($gc = self::$decoder->getElementContent()) !== false) {
                            $sc->AddParameter($spa, "getchanges", $gc);
                            if(!self::$decoder->getElementEndTag()) {
                                return false;
                            }
                        }
                    }

                    if(self::$decoder->getElementStartTag(SYNC_WINDOWSIZE)) {
                        $ws = self::$decoder->getElementContent();
                        // normalize windowsize - see ZP-477
                        if ($ws == 0 || $ws > WINDOW_SIZE_MAX)
                            $ws = WINDOW_SIZE_MAX;

                        $spa->SetWindowSize($ws);

                        // also announce the currently requested window size to the DeviceManager
                        self::$deviceManager->SetWindowSize($spa->GetFolderId(), $spa->GetWindowSize());

                        if(!self::$decoder->getElementEndTag())
                            return false;
                    }

                    // conversation mode requested
                    if(self::$decoder->getElementStartTag(SYNC_CONVERSATIONMODE)) {
                        $spa->SetConversationMode(true);
                        if(($conversationmode = self::$decoder->getElementContent()) !== false) {
                            $spa->SetConversationMode((bool)$conversationmode);
                            if(!self::$decoder->getElementEndTag())
                            return false;
                        }
                    }

                    // Do not truncate by default
                    $spa->SetTruncation(SYNC_TRUNCATION_ALL);

                    // use default conflict handling if not specified by the mobile
                    $spa->SetConflict(SYNC_CONFLICT_DEFAULT);

                    // save the current filtertype because it might have been changed on the mobile
                    $currentFilterType = $spa->GetFilterType();

                    while(self::$decoder->getElementStartTag(SYNC_OPTIONS)) {
                        $firstOption = true;
                        WBXMLDecoder::ResetInWhile("syncOptions");
                        while(WBXMLDecoder::InWhile("syncOptions")) {
                            // foldertype definition
                            if(self::$decoder->getElementStartTag(SYNC_FOLDERTYPE)) {
                                $foldertype = self::$decoder->getElementContent();
                                ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): specified options block with foldertype '%s'", $foldertype));

                                // switch the foldertype for the next options
                                $spa->UseCPO($foldertype);

                                // save the current filtertype because it might have been changed on the mobile
                                $currentFilterType = $spa->GetFilterType();

                                // set to synchronize all changes. The mobile could overwrite this value
                                $spa->SetFilterType(SYNC_FILTERTYPE_ALL);

                                if(!self::$decoder->getElementEndTag())
                                    return false;
                            }
                            // if no foldertype is defined, use default cpo
                            else if ($firstOption){
                                $spa->UseCPO();
                                // save the current filtertype because it might have been changed on the mobile
                                $currentFilterType = $spa->GetFilterType();
                                // set to synchronize all changes. The mobile could overwrite this value
                                $spa->SetFilterType(SYNC_FILTERTYPE_ALL);
                            }
                            $firstOption = false;

                            if(self::$decoder->getElementStartTag(SYNC_FILTERTYPE)) {
                                $spa->SetFilterType(self::$decoder->getElementContent());
                                if(!self::$decoder->getElementEndTag())
                                    return false;
                            }
                            if(self::$decoder->getElementStartTag(SYNC_TRUNCATION)) {
                                $spa->SetTruncation(self::$decoder->getElementContent());
                                if(!self::$decoder->getElementEndTag())
                                    return false;
                            }
                            if(self::$decoder->getElementStartTag(SYNC_RTFTRUNCATION)) {
                                $spa->SetRTFTruncation(self::$decoder->getElementContent());
                                if(!self::$decoder->getElementEndTag())
                                    return false;
                            }

                            if(self::$decoder->getElementStartTag(SYNC_MIMESUPPORT)) {
                                $spa->SetMimeSupport(self::$decoder->getElementContent());
                                if(!self::$decoder->getElementEndTag())
                                    return false;
                            }

                            if(self::$decoder->getElementStartTag(SYNC_MIMETRUNCATION)) {
                                $spa->SetMimeTruncation(self::$decoder->getElementContent());
                                if(!self::$decoder->getElementEndTag())
                                    return false;
                            }

                            if(self::$decoder->getElementStartTag(SYNC_CONFLICT)) {
                                $spa->SetConflict(self::$decoder->getElementContent());
                                if(!self::$decoder->getElementEndTag())
                                    return false;
                            }

                            while (self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_BODYPREFERENCE)) {
                                if(self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_TYPE)) {
                                    $bptype = self::$decoder->getElementContent();
                                    $spa->BodyPreference($bptype);
                                    if(!self::$decoder->getElementEndTag()) {
                                        return false;
                                    }
                                }

                                if(self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_TRUNCATIONSIZE)) {
                                    $spa->BodyPreference($bptype)->SetTruncationSize(self::$decoder->getElementContent());
                                    if(!self::$decoder->getElementEndTag())
                                        return false;
                                }

                                if(self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_ALLORNONE)) {
                                    $spa->BodyPreference($bptype)->SetAllOrNone(self::$decoder->getElementContent());
                                    if(!self::$decoder->getElementEndTag())
                                        return false;
                                }

                                if(self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_PREVIEW)) {
                                    $spa->BodyPreference($bptype)->SetPreview(self::$decoder->getElementContent());
                                    if(!self::$decoder->getElementEndTag())
                                        return false;
                                }

                                if(!self::$decoder->getElementEndTag())
                                    return false;
                            }

                            $e = self::$decoder->peek();
                            if($e[EN_TYPE] == EN_TYPE_ENDTAG) {
                                self::$decoder->getElementEndTag();
                                break;
                            }
                        }
                    }

                    // limit items to be synchronized to the mobiles if configured
                    if (defined('SYNC_FILTERTIME_MAX') && SYNC_FILTERTIME_MAX > SYNC_FILTERTYPE_ALL &&
                        (!$spa->HasFilterType() || $spa->GetFilterType() == SYNC_FILTERTYPE_ALL || $spa->GetFilterType() > SYNC_FILTERTIME_MAX)) {
                            ZLog::Write(LOGLEVEL_DEBUG, sprintf("SYNC_FILTERTIME_MAX defined. Filter set to value: %s", SYNC_FILTERTIME_MAX));
                            $spa->SetFilterType(SYNC_FILTERTIME_MAX);
                    }

                    // unset filtertype for KOE GAB folder
                    if ($spa->GetKoeGabFolder() === true) {
                        $spa->SetFilterType(SYNC_FILTERTYPE_ALL);
                        ZLog::Write(LOGLEVEL_DEBUG, "HandleSync(): KOE GAB folder - setting filter type to unlimited");
                    }

                    if ($currentFilterType != $spa->GetFilterType()) {
                        ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): filter type has changed (old: '%s', new: '%s'), removing folderstat to force Exporter setup", $currentFilterType, $spa->GetFilterType()));
                        $spa->DelFolderStat();
                    }

                    // Check if the hierarchycache is available. If not, trigger a HierarchySync
                    if (self::$deviceManager->IsHierarchySyncRequired()) {
                        $status = SYNC_STATUS_FOLDERHIERARCHYCHANGED;
                        ZLog::Write(LOGLEVEL_DEBUG, "HierarchyCache is also not available. Triggering HierarchySync to device");
                    }

                    if(($el = self::$decoder->getElementStartTag(SYNC_PERFORM)) && ($el[EN_FLAGS] & EN_FLAGS_CONTENT)) {
                        // We can not proceed here as the content class is unknown
                        if ($status != SYNC_STATUS_SUCCESS) {
                            ZLog::Write(LOGLEVEL_WARN, "Ignoring all incoming actions as global status indicates problem.");
                            $wbxmlproblem = true;
                            break;
                        }

                        $performaction = true;

                        // unset the importer
                        $this->importer = false;

                        $nchanges = 0;
                        WBXMLDecoder::ResetInWhile("syncActions");
                        while(WBXMLDecoder::InWhile("syncActions")) {
                            // ADD, MODIFY, REMOVE or FETCH
                            $element = self::$decoder->getElement();

                            if($element[EN_TYPE] != EN_TYPE_STARTTAG) {
                                self::$decoder->ungetElement($element);
                                break;
                            }

                            if ($status == SYNC_STATUS_SUCCESS)
                                $nchanges++;

                            // Foldertype sent when synching SMS
                            if(self::$decoder->getElementStartTag(SYNC_FOLDERTYPE)) {
                                $foldertype = self::$decoder->getElementContent();
                                ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): incoming data with foldertype '%s'", $foldertype));

                                if(!self::$decoder->getElementEndTag())
                                return false;
                            }
                            else
                                $foldertype = false;

                            $serverid = false;
                            if(self::$decoder->getElementStartTag(SYNC_SERVERENTRYID)) {
                                if (($serverid = self::$decoder->getElementContent()) !== false) {
                                    if(!self::$decoder->getElementEndTag()) { // end serverid
                                        return false;
                                    }
                                }
                            }

                            if(self::$decoder->getElementStartTag(SYNC_CLIENTENTRYID)) {
                                $clientid = self::$decoder->getElementContent();

                                if(!self::$decoder->getElementEndTag()) // end clientid
                                    return false;
                            }
                            else
                                $clientid = false;

                            // Get the SyncMessage if sent
                            if(($el = self::$decoder->getElementStartTag(SYNC_DATA)) && ($el[EN_FLAGS] & EN_FLAGS_CONTENT)) {
                                $message = ZPush::getSyncObjectFromFolderClass($spa->GetContentClass());

                                // KOE ZO-42: OL sends Notes as Appointments
                                if ($spa->GetContentClass() == "Notes" && KOE_CAPABILITY_NOTES && self::$deviceManager->IsKoe()) {
                                    ZLog::Write(LOGLEVEL_DEBUG, "HandleSync(): KOE sends Notes as Appointments, read as SyncAppointment and convert it into a SyncNote object.");
                                    $message = new SyncAppointment();
                                    $message->Decode(self::$decoder);

                                    $note = new SyncNote();
                                    if (isset($message->asbody))
                                        $note->asbody = $message->asbody;
                                    if (isset($message->categories))
                                        $note->categories = $message->categories;
                                    if (isset($message->subject))
                                        $note->subject = $message->subject;
                                    if (isset($message->dtstamp))
                                        $note->lastmodified = $message->dtstamp;

                                    // set SyncNote->Color from a color category
                                    $note->SetColorFromCategory();

                                    $message = $note;
                                }
                                else {
                                    $message->Decode(self::$decoder);

                                    // set Ghosted fields
                                    $message->emptySupported(self::$deviceManager->GetSupportedFields($spa->GetFolderId()));
                                }

                                if(!self::$decoder->getElementEndTag()) // end applicationdata
                                    return false;
                            }
                            else
                                $message = false;

                            switch($element[EN_TAG]) {
                                case SYNC_FETCH:
                                    array_push($actiondata["fetchids"], $serverid);
                                    break;
                                default:
                                    // get the importer
                                    if ($this->importer == false)
                                        $status = $this->getImporter($sc, $spa, $actiondata);

                                    if ($status == SYNC_STATUS_SUCCESS)
                                        $this->importMessage($spa, $actiondata, $element[EN_TAG], $message, $clientid, $serverid, $foldertype, $nchanges);
                                    else
                                        ZLog::Write(LOGLEVEL_WARN, "Ignored incoming change, global status indicates problem.");

                                    break;
                            }

                            if ($actiondata["fetchids"])
                                self::$topCollector->AnnounceInformation(sprintf("Fetching %d", $nchanges));
                            else
                                self::$topCollector->AnnounceInformation(sprintf("Incoming %d", $nchanges));

                            if(!self::$decoder->getElementEndTag()) // end add/change/delete/move
                                return false;
                        }

                        if ($status == SYNC_STATUS_SUCCESS && $this->importer !== false) {
                            ZLog::Write(LOGLEVEL_INFO, sprintf("Processed '%d' incoming changes", $nchanges));
                            if (!$actiondata["fetchids"]) {
                                self::$topCollector->AnnounceInformation(sprintf("%d incoming", $nchanges), $this->singleFolder);
                                $this->saveMultiFolderInfo("incoming", $nchanges);
                            }

                            try {
                                // Save the updated state, which is used for the exporter later
                                $sc->AddParameter($spa, "state", $this->importer->GetState());
                            }
                            catch (StatusException $stex) {
                               $status = $stex->getCode();
                            }
                        }

                        if(!self::$decoder->getElementEndTag()) // end PERFORM
                            return false;
                    }

                    // save the failsave state
                    if (!empty($actiondata["statusids"])) {
                        unset($actiondata["failstate"]);
                        $actiondata["failedsyncstate"] = $sc->GetParameter($spa, "state");
                        self::$deviceManager->GetStateManager()->SetSyncFailState($actiondata);
                    }

                    // save actiondata
                    $sc->AddParameter($spa, "actiondata", $actiondata);

                    if(!self::$decoder->getElementEndTag()) // end collection
                        return false;

                    // AS14 does not send GetChanges anymore. We should do it if there were no incoming changes
                    if (!isset($performaction) && !$sc->GetParameter($spa, "getchanges") && $spa->HasSyncKey())
                        $sc->AddParameter($spa, "getchanges", true);
                } // END FOLDER

                if(!$wbxmlproblem && !self::$decoder->getElementEndTag()) // end collections
                    return false;
            } // end FOLDERS

            if (self::$decoder->getElementStartTag(SYNC_HEARTBEATINTERVAL)) {
                $hbinterval = self::$decoder->getElementContent();
                if(!self::$decoder->getElementEndTag()) // SYNC_HEARTBEATINTERVAL
                    return false;
            }

            if (self::$decoder->getElementStartTag(SYNC_WAIT)) {
                $wait = self::$decoder->getElementContent();
                if(!self::$decoder->getElementEndTag()) // SYNC_WAIT
                    return false;

                // internally the heartbeat interval and the wait time are the same
                // heartbeat is in seconds, wait in minutes
                $hbinterval = $wait * 60;
            }

            if (self::$decoder->getElementStartTag(SYNC_WINDOWSIZE)) {
                $sc->SetGlobalWindowSize(self::$decoder->getElementContent());
                ZLog::Write(LOGLEVEL_DEBUG, "Sync(): Global WindowSize requested: ". $sc->GetGlobalWindowSize());
                if(!self::$decoder->getElementEndTag()) // SYNC_WINDOWSIZE
                    return false;
            }

            if(self::$decoder->getElementStartTag(SYNC_PARTIAL))
                $partial = true;
            else
                $partial = false;

            if(!$wbxmlproblem && !self::$decoder->getElementEndTag()) // end sync
                return false;
        }
        // we did not receive a SYNCHRONIZE block - assume empty sync
        else {
            $emptysync = true;
        }
        // END SYNCHRONIZE

        // check heartbeat/wait time
        if (isset($hbinterval)) {
            if ($hbinterval < 60 || $hbinterval > 3540) {
                $status = SYNC_STATUS_INVALIDWAITORHBVALUE;
                ZLog::Write(LOGLEVEL_WARN, sprintf("HandleSync(): Invalid heartbeat or wait value '%s'", $hbinterval));
            }
        }

        // Partial & Empty Syncs need saved data to proceed with synchronization
        if ($status == SYNC_STATUS_SUCCESS && ($emptysync === true || $partial === true) ) {
            ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): Partial or Empty sync requested. Retrieving data of synchronized folders."));

            // Load all collections - do not overwrite existing (received!), load states, check permissions and only load confirmed states!
            try {
                $sc->LoadAllCollections(false, true, true, true, true);
            }
            catch (StateInvalidException $siex) {
                $status = SYNC_STATUS_INVALIDSYNCKEY;
                self::$topCollector->AnnounceInformation("StateNotFoundException", $this->singleFolder);
                $this->saveMultiFolderInfo("exeption", "StateNotFoundException");
            }
            catch (StatusException $stex) {
               $status = SYNC_STATUS_FOLDERHIERARCHYCHANGED;
               self::$topCollector->AnnounceInformation(sprintf("StatusException code: %d", $status), $this->singleFolder);
               $this->saveMultiFolderInfo("exeption", "StatusException");
            }

            // update a few values
            foreach($sc as $folderid => $spa) {
                // manually set getchanges parameter for this collection if it is synchronized
                if ($spa->HasSyncKey()) {
                    $actiondata = $sc->GetParameter($spa, "actiondata");
                    // request changes if no other actions are executed
                    if (empty($actiondata["modifyids"]) && empty($actiondata["clientids"]) && empty($actiondata["removeids"])) {
                        $sc->AddParameter($spa, "getchanges", true);
                    }

                    // announce WindowSize to DeviceManager
                    self::$deviceManager->SetWindowSize($folderid, $spa->GetWindowSize());
                }
            }
            if (!$sc->HasCollections())
                $status = SYNC_STATUS_SYNCREQUESTINCOMPLETE;
        }
        else if (isset($hbinterval)) {
            // load the hierarchy data - there are no permissions to verify so we just set it to false
            if (!$sc->LoadCollection(false, true, false)) {
                $status = SYNC_STATUS_FOLDERHIERARCHYCHANGED;
                self::$topCollector->AnnounceInformation(sprintf("StatusException code: %d", $status), $this->singleFolder);
                $this->saveMultiFolderInfo("exeption", "StatusException");
            }
        }

        // HEARTBEAT
        if ($status == SYNC_STATUS_SUCCESS && isset($hbinterval)) {
            $interval = (defined('PING_INTERVAL') && PING_INTERVAL > 0) ? PING_INTERVAL : 30;

            if (isset($hbinterval))
                $sc->SetLifetime($hbinterval);

            // states are lazy loaded - we have to make sure that they are there!
            $loadstatus = SYNC_STATUS_SUCCESS;
            foreach($sc as $folderid => $spa) {
                // some androids do heartbeat on the OUTBOX folder, with weird results - ZP-362
                // we do not load the state so we will never get relevant changes on the OUTBOX folder
                if (self::$deviceManager->GetFolderTypeFromCacheById($folderid) == SYNC_FOLDER_TYPE_OUTBOX) {
                    ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): Heartbeat on Outbox folder not allowed"));
                    continue;
                }

                $fad = array();
                // if loading the states fails, we do not enter heartbeat, but we keep $status on SYNC_STATUS_SUCCESS
                // so when the changes are exported the correct folder gets an SYNC_STATUS_INVALIDSYNCKEY
                if ($loadstatus == SYNC_STATUS_SUCCESS)
                    $loadstatus = $this->loadStates($sc, $spa, $fad);
            }

            if ($loadstatus == SYNC_STATUS_SUCCESS) {
                $foundchanges = false;

                try {
                    // always check for changes
                    ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): Entering Heartbeat mode"));
                    $foundchanges = $sc->CheckForChanges($sc->GetLifetime(), $interval);
                }
                catch (StatusException $stex) {
                    if ($stex->getCode() == SyncCollections::OBSOLETE_CONNECTION) {
                        $status = SYNC_COMMONSTATUS_SYNCSTATEVERSIONINVALID;
                    }
                    else {
                        $status = SYNC_STATUS_FOLDERHIERARCHYCHANGED;
                        self::$topCollector->AnnounceInformation(sprintf("StatusException code: %d", $status), $this->singleFolder);
                        $this->saveMultiFolderInfo("exeption", "StatusException");
                    }
                }

                // update the waittime waited
                self::$waitTime = $sc->GetWaitedSeconds();

                // in case there are no changes and no other request has synchronized while we waited, we can reply with an empty response
                if (!$foundchanges && $status == SYNC_STATUS_SUCCESS) {
                    // if there were changes to the SPA or CPOs we need to save this before we terminate
                    // only save if the state was not modified by some other request, if so, return state invalid status
                    foreach($sc as $folderid => $spa) {
                        if (self::$deviceManager->CheckHearbeatStateIntegrity($spa->GetFolderId(), $spa->GetUuid(), $spa->GetUuidCounter())) {
                            $status = SYNC_COMMONSTATUS_SYNCSTATEVERSIONINVALID;
                        }
                        else {
                            $sc->SaveCollection($spa);
                        }
                    }

                    if ($status == SYNC_STATUS_SUCCESS) {
                        ZLog::Write(LOGLEVEL_DEBUG, "No changes found and no other process changed states. Replying with empty response and closing connection.");
                        self::$specialHeaders = array();
                        self::$specialHeaders[] = "Connection: close";
                        return true;
                    }
                }

                if ($foundchanges) {
                    foreach ($sc->GetChangedFolderIds() as $folderid => $changecount) {
                        // check if there were other sync requests for a folder during the heartbeat
                        $spa = $sc->GetCollection($folderid);
                        if ($changecount > 0 && $sc->WaitedForChanges() && self::$deviceManager->CheckHearbeatStateIntegrity($spa->GetFolderId(), $spa->GetUuid(), $spa->GetUuidCounter())) {
                            ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): heartbeat: found %d changes in '%s' which was already synchronized. Heartbeat aborted!", $changecount, $folderid));
                            $status = SYNC_COMMONSTATUS_SYNCSTATEVERSIONINVALID;
                        }
                        else
                            ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): heartbeat: found %d changes in '%s'", $changecount, $folderid));
                    }
                }
            }
        }

        // Start the output
        ZLog::Write(LOGLEVEL_DEBUG, "HandleSync(): Start Output");

        // global status
        // SYNC_COMMONSTATUS_* start with values from 101
        if ($status != SYNC_COMMONSTATUS_SUCCESS && ($status == SYNC_STATUS_FOLDERHIERARCHYCHANGED || $status > 100)) {
            self::$deviceManager->AnnounceProcessStatus($folderid, $status);
            $this->sendStartTags();
            self::$encoder->startTag(SYNC_STATUS);
                self::$encoder->content($status);
            self::$encoder->endTag();
            self::$encoder->endTag(); // SYNC_SYNCHRONIZE
            return true;
        }

        // Loop through requested folders
        foreach($sc as $folderid => $spa) {
            // get actiondata
            $actiondata = $sc->GetParameter($spa, "actiondata");

            if ($status == SYNC_STATUS_SUCCESS && (!$spa->GetContentClass() || !$spa->GetFolderId())) {
                ZLog::Write(LOGLEVEL_ERROR, sprintf("HandleSync(): no content class or folderid found for collection."));
                continue;
            }

            if (! $sc->GetParameter($spa, "requested")) {
                ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): partial sync for folder class '%s' with id '%s'", $spa->GetContentClass(), $spa->GetFolderId()));
                // reload state and initialize StateMachine correctly
                $sc->AddParameter($spa, "state", null);
                $status = $this->loadStates($sc, $spa, $actiondata);
            }

            // initialize exporter to get changecount
            $changecount = false;
            $exporter = false;
            $streamimporter = false;
            $newFolderStat = false;
            $setupExporter = true;

            // TODO we could check against $sc->GetChangedFolderIds() on heartbeat so we do not need to configure all exporter again
            if($status == SYNC_STATUS_SUCCESS && ($sc->GetParameter($spa, "getchanges") || ! $spa->HasSyncKey())) {

                // no need to run the exporter if the globalwindowsize is already full - if collection already has a synckey (ZP-1215)
                if ($sc->GetGlobalWindowSize() == $this->globallyExportedItems && $spa->HasSyncKey()) {
                    ZLog::Write(LOGLEVEL_DEBUG, sprintf("Sync(): no exporter setup for '%s' as GlobalWindowSize is full.", $spa->GetFolderId()));
                    $setupExporter = false;
                }
                // if the maximum request timeout is reached, stop processing other collections
                if (Request::IsRequestTimeoutReached()) {
                    ZLog::Write(LOGLEVEL_DEBUG, sprintf("Sync(): no exporter setup for '%s' as request timeout reached, omitting output for collection.", $spa->GetFolderId()));
                    $setupExporter = false;
                }

                // ZP-907: never send changes of UNKNOWN folders to an Outlook client
                if (Request::IsOutlook() && self::$deviceManager->GetFolderTypeFromCacheById($spa->GetFolderId()) == SYNC_FOLDER_TYPE_UNKNOWN && $spa->HasSyncKey()) {
                    ZLog::Write(LOGLEVEL_DEBUG, sprintf("Sync(): no exporter setup for '%s' as type is UNKNOWN.", $spa->GetFolderId()));
                    $setupExporter = false;
                }

                // compare the folder statistics if the backend supports this
                if ($setupExporter && self::$backend->HasFolderStats()) {
                    // check if the folder stats changed -> if not, don't setup the exporter, there are no changes!

                    $newFolderStat = self::$backend->GetFolderStat(ZPush::GetAdditionalSyncFolderStore($spa->GetBackendFolderId()), $spa->GetBackendFolderId());
                    if ($newFolderStat !== false && ! $spa->IsExporterRunRequired($newFolderStat, true)) {
                        $changecount = 0;
                        $setupExporter = false;
                    }
                }

                // Do a full Exporter setup if we can't avoid it
                if ($setupExporter) {
                    //make sure the states are loaded
                    $status = $this->loadStates($sc, $spa, $actiondata);

                    if($status == SYNC_STATUS_SUCCESS) {
                        try {
                            // if this is an additional folder the backend has to be setup correctly
                            if (!self::$backend->Setup(ZPush::GetAdditionalSyncFolderStore($spa->GetBackendFolderId())))
                                throw new StatusException(sprintf("HandleSync() could not Setup() the backend for folder id %s/%s", $spa->GetFolderId(), $spa->GetBackendFolderId()), SYNC_STATUS_FOLDERHIERARCHYCHANGED);

                            // Use the state from the importer, as changes may have already happened
                            $exporter = self::$backend->GetExporter($spa->GetBackendFolderId());

                            if ($exporter === false)
                                throw new StatusException(sprintf("HandleSync() could not get an exporter for folder id %s/%s", $spa->GetFolderId(), $spa->GetBackendFolderId()), SYNC_STATUS_FOLDERHIERARCHYCHANGED);
                        }
                        catch (StatusException $stex) {
                           $status = $stex->getCode();
                        }
                        try {
                            // Stream the messages directly to the PDA
                            $streamimporter = new ImportChangesStream(self::$encoder, ZPush::getSyncObjectFromFolderClass($spa->GetContentClass()));

                            if ($exporter !== false) {
                                $exporter->SetMoveStates($spa->GetMoveState());
                                $exporter->Config($sc->GetParameter($spa, "state"));
                                $exporter->ConfigContentParameters($spa->GetCPO());
                                $exporter->InitializeExporter($streamimporter);

                                $changecount = $exporter->GetChangeCount();
                            }
                        }
                        catch (StatusException $stex) {
                            if ($stex->getCode() === SYNC_FSSTATUS_CODEUNKNOWN && $spa->HasSyncKey())
                                $status = SYNC_STATUS_INVALIDSYNCKEY;
                            else
                                $status = $stex->getCode();
                        }

                        if (! $spa->HasSyncKey()) {
                            self::$topCollector->AnnounceInformation(sprintf("Exporter registered. %d objects queued.", $changecount), $this->singleFolder);
                            $this->saveMultiFolderInfo("queued", $changecount);
                            // update folder status as initialized
                            $spa->SetFolderSyncTotal($changecount);
                            $spa->SetFolderSyncRemaining($changecount);
                            if ($changecount > 0) {
                                self::$deviceManager->SetFolderSyncStatus($folderid, DeviceManager::FLD_SYNC_INITIALIZED);
                            }
                        }
                        else if ($status != SYNC_STATUS_SUCCESS) {
                            self::$topCollector->AnnounceInformation(sprintf("StatusException code: %d", $status), $this->singleFolder);
                            $this->saveMultiFolderInfo("exception", "StatusException");
                        }
                        self::$deviceManager->AnnounceProcessStatus($spa->GetFolderId(), $status);
                    }
                }
            }

            // Get a new sync key to output to the client if any changes have been send by the mobile or a new synckey is to be sent
            if (!empty($actiondata["modifyids"]) ||
                !empty($actiondata["clientids"]) ||
                !empty($actiondata["removeids"]) ||
                (! $spa->HasSyncKey() && $status == SYNC_STATUS_SUCCESS)) {
                    $spa->SetNewSyncKey(self::$deviceManager->GetStateManager()->GetNewSyncKey($spa->GetSyncKey()));
            }
            // get a new synckey only if we did not reach the global limit yet
            else {
                // when reaching the global limit for changes of all collections, stop processing other collections (ZP-697)
                if ($sc->GetGlobalWindowSize() <= $this->globallyExportedItems) {
                    ZLog::Write(LOGLEVEL_DEBUG, "Global WindowSize for amount of exported changes reached, omitting output for collection.");
                    continue;
                }

                // get a new synckey if there are changes are we did not reach the limit yet
                if ($changecount > 0) {
                    $spa->SetNewSyncKey(self::$deviceManager->GetStateManager()->GetNewSyncKey($spa->GetSyncKey()));
                }
            }

            // Fir AS 14.0+ omit output for folder, if there were no incoming or outgoing changes and no Fetch
            if (Request::GetProtocolVersion() >= 14.0 && ! $spa->HasNewSyncKey() && $changecount == 0 && empty($actiondata["fetchids"]) && $status == SYNC_STATUS_SUCCESS &&
                    ! $spa->HasConfirmationChanged() && ($newFolderStat === false || ! $spa->IsExporterRunRequired($newFolderStat))) {
                ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync: No changes found for %s folder id '%s'. Omitting output.", $spa->GetContentClass(), $spa->GetFolderId()));
                continue;
            }

            // if there are no other responses sent, we should end with a global status
            if ($status == SYNC_STATUS_FOLDERHIERARCHYCHANGED && $this->startTagsSent === false) {
                $this->sendStartTags();
                self::$encoder->startTag(SYNC_STATUS);
                self::$encoder->content($status);
                self::$encoder->endTag();
                self::$encoder->endTag(); // SYNC_SYNCHRONIZE
                return true;
            }

            // there is something to send here, sync folder to output
            $this->syncFolder($sc, $spa, $exporter, $changecount, $streamimporter, $status, $newFolderStat);

            // reset status for the next folder
            $status = SYNC_STATUS_SUCCESS;
        } // END foreach collection

        //SYNC_FOLDERS - only if the starttag was sent
        if ($this->startFolderTagSent)
            self::$encoder->endTag();

        // Check if there was any response - in case of an empty sync request, we shouldn't send an empty answer (ZP-1241)
        if (!$this->startTagsSent && $emptysync === true) {
            $this->sendStartTags();
            self::$encoder->startTag(SYNC_STATUS);
            self::$encoder->content(SYNC_STATUS_SYNCREQUESTINCOMPLETE);
            self::$encoder->endTag();
        }

        //SYNC_SYNCHRONIZE - only if the starttag was sent
        if ($this->startTagsSent)
            self::$encoder->endTag();

        // final top announcement for a multi-folder sync
        if ($sc->GetCollectionCount() > 1) {
            self::$topCollector->AnnounceInformation($this->getMultiFolderInfoLine($sc->GetCollectionCount()), true);
            ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync: Processed %d folders", $sc->GetCollectionCount()));
        }

        // update the waittime waited
        self::$waitTime = $sc->GetWaitedSeconds();

        return true;
    }

    /**
     * Sends the SYNC_SYNCHRONIZE once per request.
     *
     * @access private
     * @return void
     */
    private function sendStartTags() {
        if ($this->startTagsSent === false) {
            self::$encoder->startWBXML();
            self::$encoder->startTag(SYNC_SYNCHRONIZE);
            $this->startTagsSent = true;
        }
    }

    /**
     * Sends the SYNC_FOLDERS once per request.
     *
     * @access private
     * @return void
     */
    private function sendFolderStartTag() {
        $this->sendStartTags();
        if ($this->startFolderTagSent === false) {
            self::$encoder->startTag(SYNC_FOLDERS);
            $this->startFolderTagSent = true;
        }
    }

    /**
     * Synchronizes a folder to the output stream. Changes for this folders are expected.
     *
     * @param SyncCollections       $sc
     * @param SyncParameters        $spa
     * @param IExportChanges        $exporter             Fully configured exporter for this folder
     * @param int                   $changecount          Amount of changes expected
     * @param ImportChangesStream   $streamimporter       Output stream
     * @param int                   $status               current status of the folder processing
     * @param string                $newFolderStat        the new folder stat to be set if everything was exported
     *
     * @throws StatusException
     * @return int  sync status code
     */
    private function syncFolder($sc, $spa, $exporter, $changecount, $streamimporter, $status, $newFolderStat) {
        $actiondata = $sc->GetParameter($spa, "actiondata");

        // send the WBXML start tags (if not happened already)
        $this->sendFolderStartTag();
        self::$encoder->startTag(SYNC_FOLDER);

        if($spa->HasContentClass()) {
            ZLog::Write(LOGLEVEL_DEBUG, sprintf("Folder type: %s", $spa->GetContentClass()));
            // AS 12.0 devices require content class
            if (Request::GetProtocolVersion() < 12.1) {
                self::$encoder->startTag(SYNC_FOLDERTYPE);
                self::$encoder->content($spa->GetContentClass());
                self::$encoder->endTag();
            }
        }

        self::$encoder->startTag(SYNC_SYNCKEY);
        if($status == SYNC_STATUS_SUCCESS && $spa->HasNewSyncKey())
            self::$encoder->content($spa->GetNewSyncKey());
        else
            self::$encoder->content($spa->GetSyncKey());
        self::$encoder->endTag();

        self::$encoder->startTag(SYNC_FOLDERID);
        self::$encoder->content($spa->GetFolderId());
        self::$encoder->endTag();

        self::$encoder->startTag(SYNC_STATUS);
        self::$encoder->content($status);
        self::$encoder->endTag();

        // announce failing status to the process loop detection
        if ($status !== SYNC_STATUS_SUCCESS)
            self::$deviceManager->AnnounceProcessStatus($spa->GetFolderId(), $status);

        // Output IDs and status for incoming items & requests
        if($status == SYNC_STATUS_SUCCESS && (
                !empty($actiondata["clientids"]) ||
                !empty($actiondata["modifyids"]) ||
                !empty($actiondata["removeids"]) ||
                !empty($actiondata["fetchids"]) )) {

            self::$encoder->startTag(SYNC_REPLIES);
            // output result of all new incoming items
            foreach($actiondata["clientids"] as $clientid => $serverid) {
                self::$encoder->startTag(SYNC_ADD);
                self::$encoder->startTag(SYNC_CLIENTENTRYID);
                self::$encoder->content($clientid);
                self::$encoder->endTag();
                if ($serverid) {
                    self::$encoder->startTag(SYNC_SERVERENTRYID);
                    self::$encoder->content($serverid);
                    self::$encoder->endTag();
                }
                self::$encoder->startTag(SYNC_STATUS);
                self::$encoder->content((isset($actiondata["statusids"][$clientid])?$actiondata["statusids"][$clientid]:SYNC_STATUS_CLIENTSERVERCONVERSATIONERROR));
                self::$encoder->endTag();
                self::$encoder->endTag();
            }

            // loop through modify operations which were not a success, send status
            foreach($actiondata["modifyids"] as $serverid) {
                if (isset($actiondata["statusids"][$serverid]) && $actiondata["statusids"][$serverid] !== SYNC_STATUS_SUCCESS) {
                    self::$encoder->startTag(SYNC_MODIFY);
                    self::$encoder->startTag(SYNC_SERVERENTRYID);
                    self::$encoder->content($serverid);
                    self::$encoder->endTag();
                    self::$encoder->startTag(SYNC_STATUS);
                    self::$encoder->content($actiondata["statusids"][$serverid]);
                    self::$encoder->endTag();
                    self::$encoder->endTag();
                }
            }

            // loop through remove operations which were not a success, send status
            foreach($actiondata["removeids"] as $serverid) {
                if (isset($actiondata["statusids"][$serverid]) && $actiondata["statusids"][$serverid] !== SYNC_STATUS_SUCCESS) {
                    self::$encoder->startTag(SYNC_REMOVE);
                    self::$encoder->startTag(SYNC_SERVERENTRYID);
                    self::$encoder->content($serverid);
                    self::$encoder->endTag();
                    self::$encoder->startTag(SYNC_STATUS);
                    self::$encoder->content($actiondata["statusids"][$serverid]);
                    self::$encoder->endTag();
                    self::$encoder->endTag();
                }
            }

            if (!empty($actiondata["fetchids"])) {
                self::$topCollector->AnnounceInformation(sprintf("Fetching %d objects ", count($actiondata["fetchids"])), $this->singleFolder);
                $this->saveMultiFolderInfo("fetching", count($actiondata["fetchids"]));
            }

            foreach($actiondata["fetchids"] as $id) {
                $data = false;
                try {
                    $fetchstatus = SYNC_STATUS_SUCCESS;

                    // if this is an additional folder the backend has to be setup correctly
                    if (!self::$backend->Setup(ZPush::GetAdditionalSyncFolderStore($spa->GetBackendFolderId())))
                        throw new StatusException(sprintf("HandleSync(): could not Setup() the backend to fetch in folder id %s/%s", $spa->GetFolderId(), $spa->GetBackendFolderId()), SYNC_STATUS_OBJECTNOTFOUND);

                    $data = self::$backend->Fetch($spa->GetBackendFolderId(), $id, $spa->GetCPO());

                    // check if the message is broken
                    if (ZPush::GetDeviceManager(false) && ZPush::GetDeviceManager()->DoNotStreamMessage($id, $data)) {
                        ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): message not to be streamed as requested by DeviceManager, id = %s", $id));
                        $fetchstatus = SYNC_STATUS_CLIENTSERVERCONVERSATIONERROR;
                    }
                }
                catch (StatusException $stex) {
                    $fetchstatus = $stex->getCode();
                }

                self::$encoder->startTag(SYNC_FETCH);
                self::$encoder->startTag(SYNC_SERVERENTRYID);
                self::$encoder->content($id);
                self::$encoder->endTag();

                self::$encoder->startTag(SYNC_STATUS);
                self::$encoder->content($fetchstatus);
                self::$encoder->endTag();

                if($data !== false && $status == SYNC_STATUS_SUCCESS) {
                    self::$encoder->startTag(SYNC_DATA);
                    $data->Encode(self::$encoder);
                    self::$encoder->endTag();
                }
                else
                    ZLog::Write(LOGLEVEL_WARN, sprintf("Unable to Fetch '%s'", $id));
                self::$encoder->endTag();

            }
            self::$encoder->endTag();
        }

        if($sc->GetParameter($spa, "getchanges") && $spa->HasFolderId() && $spa->HasContentClass() && $spa->HasSyncKey()) {
            $moreAvailableSent = false;
            $windowSize = self::$deviceManager->GetWindowSize($spa->GetFolderId(), $spa->GetUuid(), $spa->GetUuidCounter(), $changecount);

            // limit windowSize to the max available limit of the global window size left
            $globallyAvailable = $sc->GetGlobalWindowSize() - $this->globallyExportedItems;
            if ($changecount > $globallyAvailable && $windowSize > $globallyAvailable) {
                ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): Limit window size to %d as the global window size limit will be reached", $globallyAvailable));
                $windowSize = $globallyAvailable;
            }
            // send <MoreAvailable/> if there are more changes than fit in the folder windowsize
            // or there is a move state (another sync should be done afterwards)
            if($changecount > $windowSize || $spa->GetMoveState() !== false) {
                self::$encoder->startTag(SYNC_MOREAVAILABLE, false, true);
                $moreAvailableSent = true;
                $spa->DelFolderStat();
            }
        }

        // Stream outgoing changes
        if($status == SYNC_STATUS_SUCCESS && $sc->GetParameter($spa, "getchanges") == true && $windowSize > 0 && !!$exporter) {
            self::$topCollector->AnnounceInformation(sprintf("Streaming data of %d objects", (($changecount > $windowSize)?$windowSize:$changecount)));

            // Output message changes per folder
            self::$encoder->startTag(SYNC_PERFORM);

            $n = 0;
            WBXMLDecoder::ResetInWhile("syncSynchronize");
            while(WBXMLDecoder::InWhile("syncSynchronize")) {
                try {
                    $progress = $exporter->Synchronize();
                    if(!is_array($progress))
                        break;
                    $n++;
                    if ($n % 10 == 0)
                        self::$topCollector->AnnounceInformation(sprintf("Streamed data of %d objects out of %d", $n, (($changecount > $windowSize)?$windowSize:$changecount)));
                }
                catch (SyncObjectBrokenException $mbe) {
                    $brokenSO = $mbe->GetSyncObject();
                    if (!$brokenSO) {
                        ZLog::Write(LOGLEVEL_ERROR, sprintf("HandleSync(): Catched SyncObjectBrokenException but broken SyncObject not available. This should be fixed in the backend."));
                    }
                    else {
                        if (!isset($brokenSO->id)) {
                            $brokenSO->id = "Unknown ID";
                            ZLog::Write(LOGLEVEL_ERROR, sprintf("HandleSync(): Catched SyncObjectBrokenException but no ID of object set. This should be fixed in the backend."));
                        }
                        self::$deviceManager->AnnounceIgnoredMessage($spa->GetFolderId(), $brokenSO->id, $brokenSO);
                    }
                }
                // something really bad happened while exporting changes
                catch (StatusException $stex) {
                    $status = $stex->getCode();
                    // during export we found out that the states should be thrown away (ZP-623)
                    if ($status == SYNC_STATUS_INVALIDSYNCKEY) {
                        self::$deviceManager->ForceFolderResync($spa->GetFolderId());
                        break;
                    }
                }

                if($n >= $windowSize || Request::IsRequestTimeoutReached()) {
                    ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): Exported maxItems of messages: %d / %d", $n, $changecount));
                    break;
                }
            }

            // $progress is not an array when exporting the last message
            // so we get the number to display from the streamimporter if it's available
            if (!!$streamimporter) {
                $n = $streamimporter->GetImportedMessages();
            }

            self::$encoder->endTag();

            // log the request timeout
            if (Request::IsRequestTimeoutReached()) {
                ZLog::Write(LOGLEVEL_DEBUG, "HandleSync(): Stopping export as maximum request timeout is almost reached!");
                // Send a <MoreAvailable/> tag if we reached the request timeout, there are more changes and a moreavailable was not already send
                if (!$moreAvailableSent && ($n > $windowSize)) {
                    self::$encoder->startTag(SYNC_MOREAVAILABLE, false, true);
                    $spa->DelFolderStat();
                    $moreAvailableSent = true;
                }
            }

            self::$topCollector->AnnounceInformation(sprintf("Outgoing %d objects%s", $n, ($n >= $windowSize)?" of ".$changecount:""), $this->singleFolder);
            $this->saveMultiFolderInfo("outgoing", $n);
            $this->saveMultiFolderInfo("queued", $changecount);

            $this->globallyExportedItems += $n;

            // update folder status, if there is something set
            if ($spa->GetFolderSyncRemaining() && $changecount > 0) {
                $spa->SetFolderSyncRemaining($changecount);
            }
            // changecount is initialized with 'false', so 0 means no changes!
            if ($changecount === 0 || ($changecount !== false && $changecount <= $windowSize)) {
                self::$deviceManager->SetFolderSyncStatus($spa->GetFolderId(), DeviceManager::FLD_SYNC_COMPLETED);

                // we should update the folderstat, but we recheck to see if it changed. If so, it's not updated to force another sync
                if (self::$backend->HasFolderStats()) {
                    $newFolderStatAfterExport = self::$backend->GetFolderStat(ZPush::GetAdditionalSyncFolderStore($spa->GetBackendFolderId()), $spa->GetBackendFolderId());
                    if ($newFolderStat === $newFolderStatAfterExport) {
                        $this->setFolderStat($spa, $newFolderStat);
                    }
                    else {
                        ZLog::Write(LOGLEVEL_DEBUG, "Sync() Folderstat differs after export, force another exporter run.");
                    }
                }
            }
            else
                self::$deviceManager->SetFolderSyncStatus($spa->GetFolderId(), DeviceManager::FLD_SYNC_INPROGRESS);
        }

        self::$encoder->endTag();

        // Save the sync state for the next time
        if($spa->HasNewSyncKey()) {
            self::$topCollector->AnnounceInformation("Saving state");

            try {
                if (isset($exporter) && $exporter) {
                    $state = $exporter->GetState();

                    // update the move state (it should be gone now)
                    list($moveState,) = $exporter->GetMoveStates();
                    $spa->SetMoveState($moveState);
                }

                // nothing exported, but possibly imported - get the importer state
                else if ($sc->GetParameter($spa, "state") !== null)
                    $state = $sc->GetParameter($spa, "state");

                // if a new request without state information (hierarchy) save an empty state
                else if (! $spa->HasSyncKey())
                    $state = "";
            }
            catch (StatusException $stex) {
                $status = $stex->getCode();
            }


            if (isset($state) && $status == SYNC_STATUS_SUCCESS)
                self::$deviceManager->GetStateManager()->SetSyncState($spa->GetNewSyncKey(), $state, $spa->GetFolderId());
            else
                ZLog::Write(LOGLEVEL_ERROR, sprintf("HandleSync(): error saving '%s' - no state information available", $spa->GetNewSyncKey()));
        }

        // save SyncParameters
        if ($status == SYNC_STATUS_SUCCESS && empty($actiondata["fetchids"]))
            $sc->SaveCollection($spa);

        return $status;
    }

    /**
     * Loads the states and writes them into the SyncCollection Object and the actiondata failstate
     *
     * @param SyncCollection    $sc             SyncCollection object
     * @param SyncParameters    $spa            SyncParameters object
     * @param array             $actiondata     Actiondata array
     * @param boolean           $loadFailsave   (opt) default false - indicates if the failsave states should be loaded
     *
     * @access private
     * @return status           indicating if there were errors. If no errors, status is SYNC_STATUS_SUCCESS
     */
    private function loadStates($sc, $spa, &$actiondata, $loadFailsave = false) {
        $status = SYNC_STATUS_SUCCESS;

        if ($sc->GetParameter($spa, "state") == null) {
            ZLog::Write(LOGLEVEL_DEBUG, sprintf("Sync->loadStates(): loading states for folder '%s'",$spa->GetFolderId()));

            try {
                $sc->AddParameter($spa, "state", self::$deviceManager->GetStateManager()->GetSyncState($spa->GetSyncKey()));

                if ($loadFailsave) {
                    // if this request was made before, there will be a failstate available
                    $actiondata["failstate"] = self::$deviceManager->GetStateManager()->GetSyncFailState();
                }

                // if this is an additional folder the backend has to be setup correctly
                if (!self::$backend->Setup(ZPush::GetAdditionalSyncFolderStore($spa->GetBackendFolderId())))
                    throw new StatusException(sprintf("HandleSync() could not Setup() the backend for folder id %s/%s", $spa->GetFolderId(), $spa->GetBackendFolderId()), SYNC_STATUS_FOLDERHIERARCHYCHANGED);
            }
            catch (StateNotFoundException $snfex) {
                $status = SYNC_STATUS_INVALIDSYNCKEY;
                self::$topCollector->AnnounceInformation("StateNotFoundException", $this->singleFolder);
                $this->saveMultiFolderInfo("exception", "StateNotFoundException");
            }
            catch (StatusException $stex) {
               $status = $stex->getCode();
               self::$topCollector->AnnounceInformation(sprintf("StatusException code: %d", $status), $this->singleFolder);
               $this->saveMultiFolderInfo("exception", "StateNotFoundException");

            }
        }

        return $status;
    }

    /**
     * Initializes the importer for the SyncParameters folder, loads necessary
     * states (incl. failsave states) and initializes the conflict detection
     *
     * @param SyncCollection    $sc             SyncCollection object
     * @param SyncParameters    $spa            SyncParameters object
     * @param array             $actiondata     Actiondata array
     *
     * @access private
     * @return status           indicating if there were errors. If no errors, status is SYNC_STATUS_SUCCESS
     */
    private function getImporter($sc, $spa, &$actiondata) {
        ZLog::Write(LOGLEVEL_DEBUG, "Sync->getImporter(): initialize importer");
        $status = SYNC_STATUS_SUCCESS;

        // load the states with failsave data
        $status = $this->loadStates($sc, $spa, $actiondata, true);

        try {
            if ($status == SYNC_STATUS_SUCCESS) {
                // Configure importer with last state
                $this->importer = self::$backend->GetImporter($spa->GetBackendFolderId());

                // if something goes wrong, ask the mobile to resync the hierarchy
                if ($this->importer === false)
                    throw new StatusException(sprintf("Sync->getImporter(): no importer for folder id %s/%s", $spa->GetFolderId(), $spa->GetBackendFolderId()), SYNC_STATUS_FOLDERHIERARCHYCHANGED);

                // set the move state so the importer is aware of previous made moves
                $this->importer->SetMoveStates($spa->GetMoveState());

                // if there is a valid state obtained after importing changes in a previous loop, we use that state
                if (isset($actiondata["failstate"]) && isset($actiondata["failstate"]["failedsyncstate"])) {
                    $this->importer->Config($actiondata["failstate"]["failedsyncstate"], $spa->GetConflict());
                }
                else
                    $this->importer->Config($sc->GetParameter($spa, "state"), $spa->GetConflict());

                // the CPO is also needed by the importer to check if imported changes are inside the sync window - see ZP-258
                $this->importer->ConfigContentParameters($spa->GetCPO());
                $this->importer->LoadConflicts($spa->GetCPO(), $sc->GetParameter($spa, "state"));
            }
        }
        catch (StatusException $stex) {
           $status = $stex->getCode();
        }

        return $status;
    }

    /**
     * Imports a message
     *
     * @param SyncParameters    $spa            SyncParameters object
     * @param array             $actiondata     Actiondata array
     * @param integer           $todo           WBXML flag indicating how message should be imported.
     *                                          Valid values: SYNC_ADD, SYNC_MODIFY, SYNC_REMOVE
     * @param SyncObject        $message        SyncObject message to be imported
     * @param string            $clientid       Client message identifier
     * @param string            $serverid       Server message identifier
     * @param string            $foldertype     On sms sync, this says "SMS", else false
     * @param integer           $messageCount   Counter of already imported messages
     *
     * @access private
     * @throws StatusException  in case the importer is not available
     * @return -                Message related status are returned in the actiondata.
     */
    private function importMessage($spa, &$actiondata, $todo, $message, $clientid, $serverid, $foldertype, $messageCount) {
        // the importer needs to be available!
        if ($this->importer == false)
            throw StatusException("Sync->importMessage(): importer not available", SYNC_STATUS_SERVERERROR);

        // mark this state as used, e.g. for HeartBeat
        self::$deviceManager->SetHeartbeatStateIntegrity($spa->GetFolderId(), $spa->GetUuid(), $spa->GetUuidCounter());

        // Detect incoming loop
        // messages which were created/removed before will not have the same action executed again
        // if a message is edited we perform this action "again", as the message could have been changed on the mobile in the meantime
        $ignoreMessage = false;
        if ($actiondata["failstate"]) {
            // message was ADDED before, do NOT add it again
            if ($todo == SYNC_ADD && isset($actiondata["failstate"]["clientids"][$clientid])) {
                $ignoreMessage = true;

                // make sure no messages are sent back
                self::$deviceManager->SetWindowSize($spa->GetFolderId(), 0);

                $actiondata["clientids"][$clientid] = $actiondata["failstate"]["clientids"][$clientid];
                $actiondata["statusids"][$clientid] = $actiondata["failstate"]["statusids"][$clientid];

                ZLog::Write(LOGLEVEL_WARN, sprintf("Mobile loop detected! Incoming new message '%s' was created on the server before. Replying with known new server id: %s", $clientid, $actiondata["clientids"][$clientid]));
            }

            // message was REMOVED before, do NOT attemp to remove it again
            if ($todo == SYNC_REMOVE && isset($actiondata["failstate"]["removeids"][$serverid])) {
                $ignoreMessage = true;

                // make sure no messages are sent back
                self::$deviceManager->SetWindowSize($spa->GetFolderId(), 0);

                $actiondata["removeids"][$serverid] = $actiondata["failstate"]["removeids"][$serverid];
                $actiondata["statusids"][$serverid] = $actiondata["failstate"]["statusids"][$serverid];

                ZLog::Write(LOGLEVEL_WARN, sprintf("Mobile loop detected! Message '%s' was deleted by the mobile before. Replying with known status: %s", $clientid, $actiondata["statusids"][$serverid]));
            }
        }

        if (!$ignoreMessage) {
            switch($todo) {
                case SYNC_MODIFY:
                    self::$topCollector->AnnounceInformation(sprintf("Saving modified message %d", $messageCount));
                    try {
                        $actiondata["modifyids"][] = $serverid;

                        // ignore sms messages
                        if ($foldertype == "SMS" || stripos($serverid, self::ZPUSHIGNORESMS) !== false) {
                            ZLog::Write(LOGLEVEL_DEBUG, "SMS sync are not supported. Ignoring message.");
                            // TODO we should update the SMS
                            $actiondata["statusids"][$serverid] = SYNC_STATUS_SUCCESS;
                        }
                        // check incoming message without logging WARN messages about errors
                        else if (!($message instanceof SyncObject) || !$message->Check(true)) {
                            $actiondata["statusids"][$serverid] = SYNC_STATUS_CLIENTSERVERCONVERSATIONERROR;
                        }
                        else {
                            if(isset($message->read)) {
                                // Currently, 'read' is only sent by the PDA when it is ONLY setting the read flag.
                                $this->importer->ImportMessageReadFlag($serverid, $message->read);
                            }
                            elseif (!isset($message->flag)) {
                                $this->importer->ImportMessageChange($serverid, $message);
                            }

                            // email todoflags - some devices send todos flags together with read flags,
                            // so they have to be handled separately
                            if (isset($message->flag)){
                                $this->importer->ImportMessageChange($serverid, $message);
                            }

                            $actiondata["statusids"][$serverid] = SYNC_STATUS_SUCCESS;
                        }
                    }
                    catch (StatusException $stex) {
                        $actiondata["statusids"][$serverid] = $stex->getCode();
                    }

                    break;
                case SYNC_ADD:
                    self::$topCollector->AnnounceInformation(sprintf("Creating new message from mobile %d", $messageCount));
                    try {
                        // mark the message as new message so SyncObject->Check() can differentiate
                        $message->flags = SYNC_NEWMESSAGE;

                        // ignore sms messages
                        if ($foldertype == "SMS") {
                            ZLog::Write(LOGLEVEL_DEBUG, "SMS sync are not supported. Ignoring message.");
                            // TODO we should create the SMS
                            // return a fake serverid which we can identify later
                            $actiondata["clientids"][$clientid] = self::ZPUSHIGNORESMS . $clientid;
                            $actiondata["statusids"][$clientid] = SYNC_STATUS_SUCCESS;
                        }
                        // check incoming message without logging WARN messages about errors
                        else if (!($message instanceof SyncObject) || !$message->Check(true)) {
                            $actiondata["clientids"][$clientid] = false;
                            $actiondata["statusids"][$clientid] = SYNC_STATUS_CLIENTSERVERCONVERSATIONERROR;
                        }
                        else {
                            $actiondata["clientids"][$clientid] = false;
                            $actiondata["clientids"][$clientid] = $this->importer->ImportMessageChange(false, $message);
                            $actiondata["statusids"][$clientid] = SYNC_STATUS_SUCCESS;
                        }
                    }
                    catch (StatusException $stex) {
                       $actiondata["statusids"][$clientid] = $stex->getCode();
                    }
                    break;
                case SYNC_REMOVE:
                    self::$topCollector->AnnounceInformation(sprintf("Deleting message removed on mobile %d", $messageCount));
                    try {
                        $actiondata["removeids"][] = $serverid;
                        // ignore sms messages
                        if ($foldertype == "SMS" || stripos($serverid, self::ZPUSHIGNORESMS) !== false) {
                            ZLog::Write(LOGLEVEL_DEBUG, "SMS sync are not supported. Ignoring message.");
                            // TODO we should delete the SMS
                            $actiondata["statusids"][$serverid] = SYNC_STATUS_SUCCESS;
                        }
                        else {
                            // if message deletions are to be moved, move them
                            if($spa->GetDeletesAsMoves()) {
                                $folderid = self::$backend->GetWasteBasket();

                                if($folderid) {
                                    $actiondata["statusids"][$serverid] = SYNC_STATUS_SUCCESS;
                                    $this->importer->ImportMessageMove($serverid, $folderid);
                                    break;
                                }
                                else
                                    ZLog::Write(LOGLEVEL_WARN, "Message should be moved to WasteBasket, but the Backend did not return a destination ID. Message is hard deleted now!");
                            }

                            $this->importer->ImportMessageDeletion($serverid);
                            $actiondata["statusids"][$serverid] = SYNC_STATUS_SUCCESS;
                        }
                    }
                    catch (StatusException $stex) {
                        if($stex->getCode() != SYNC_MOVEITEMSSTATUS_SUCCESS) {
                            $actiondata["statusids"][$serverid] = SYNC_STATUS_OBJECTNOTFOUND;
                        }
                    }
                    break;
            }
            ZLog::Write(LOGLEVEL_DEBUG, "Sync->importMessage(): message imported");
        }
    }

    /**
     * Keeps some interesting information about the sync process of several folders.
     *
     * @access private
     * @return
     */
    private function saveMultiFolderInfo($key, $value) {
        if ($key == "incoming" || $key == "outgoing" || $key == "queued" || $key == "fetching") {
            if (!isset($this->multiFolderInfo[$key])) {
                $this->multiFolderInfo[$key] = 0;
            }
            $this->multiFolderInfo[$key] += $value;
        }
        if ($key == "exception") {
            if (!isset($this->multiFolderInfo[$key])) {
                $this->multiFolderInfo[$key] = array();
            }
            $this->multiFolderInfo[$key][] = $value;
        }
    }

    /**
     * Returns a single string with information about the multi folder synchronization.
     *
     * @param int $amountOfFolders
     *
     * @access private
     * @return string
     */
    private function getMultiFolderInfoLine($amountOfFolders) {
        $s = $amountOfFolders . " folders";
        if (isset($this->multiFolderInfo["incoming"])) {
            $s .= ": ". $this->multiFolderInfo["incoming"] ." saved";
        }
        if (isset($this->multiFolderInfo["outgoing"]) && isset($this->multiFolderInfo["queued"]) && $this->multiFolderInfo["outgoing"] > 0) {
            $s .= sprintf(": Streamed %d out of %d", $this->multiFolderInfo["outgoing"], $this->multiFolderInfo["queued"]);
        }
        else if (!isset($this->multiFolderInfo["outgoing"]) && !isset($this->multiFolderInfo["queued"])) {
            $s .= ": no changes";
        }
        else {
            if (isset($this->multiFolderInfo["outgoing"])) {
                $s .= "/".$this->multiFolderInfo["outgoing"] ." streamed";
            }
            if (isset($this->multiFolderInfo["queued"])) {
                $s .= "/".$this->multiFolderInfo["queued"] ." queued";
            }
        }
        if (isset($this->multiFolderInfo["exception"])) {
            $exceptions = array_count_values($this->multiFolderInfo["exception"]);
            foreach ($exceptions as $name => $count) {
                $s .= sprintf("-%s(%d)", $name, $count);
            }
        }
        return $s;
    }

    /**
     * Sets the new folderstat and calculates & sets an expiration date for the folder stat.
     *
     * @param SyncParameters $spa
     * @param string $newFolderStat
     *
     * @access private
     * @return
     */
    private function setFolderStat($spa, $newFolderStat) {
        $spa->SetFolderStat($newFolderStat);
        $maxTimeout = 60 * 60 * 24 * 31; // one month

        $interval = Utils::GetFiltertypeInterval($spa->GetFilterType());
        $timeout = time() + (($interval && $interval < $maxTimeout) ? $interval : $maxTimeout);
        // randomize timout in 12h
        $timeout -= rand(0, 43200);
        ZLog::Write(LOGLEVEL_DEBUG, sprintf("Sync()->setFolderStat() on %s: %s expiring %s", $spa->getFolderId(), $newFolderStat, date('Y-m-d H:i:s', $timeout)));
        $spa->SetFolderStatTimeout($timeout);
    }
}