| Conditions | 236 |
| Paths | > 20000 |
| Total Lines | 1004 |
| Code Lines | 535 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 29 | public function Handle($commandCode) { |
||
| 30 | // Contains all requested folders (containers) |
||
| 31 | $sc = new SyncCollections(); |
||
| 32 | $status = SYNC_STATUS_SUCCESS; |
||
| 33 | $wbxmlproblem = false; |
||
| 34 | $emptysync = false; |
||
| 35 | $this->singleFolder = true; |
||
| 36 | $this->multiFolderInfo = []; |
||
| 37 | $this->globallyExportedItems = 0; |
||
| 38 | |||
| 39 | // check if the hierarchySync was fully completed |
||
| 40 | if (USE_PARTIAL_FOLDERSYNC) { |
||
| 41 | if (self::$deviceManager->GetFolderSyncComplete() === false) { |
||
| 42 | SLog::Write(LOGLEVEL_INFO, "Request->HandleSync(): Sync request aborted, as exporting of folders has not yet completed"); |
||
| 43 | self::$topCollector->AnnounceInformation("Aborted due incomplete folder sync", true); |
||
| 44 | $status = SYNC_STATUS_FOLDERHIERARCHYCHANGED; |
||
| 45 | } |
||
| 46 | else { |
||
| 47 | SLog::Write(LOGLEVEL_INFO, "Request->HandleSync(): FolderSync marked as complete"); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | // Start Synchronize |
||
| 52 | if (self::$decoder->getElementStartTag(SYNC_SYNCHRONIZE)) { |
||
| 53 | // AS 1.0 sends version information in WBXML |
||
| 54 | if (self::$decoder->getElementStartTag(SYNC_VERSION)) { |
||
| 55 | $sync_version = self::$decoder->getElementContent(); |
||
| 56 | SLog::Write(LOGLEVEL_DEBUG, sprintf("WBXML sync version: '%s'", $sync_version)); |
||
| 57 | if (!self::$decoder->getElementEndTag()) { |
||
| 58 | return false; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | // Syncing specified folders |
||
| 63 | // Android still sends heartbeat sync even if all syncfolders are disabled. |
||
| 64 | // Check if Folders tag is empty (<Folders/>) and only sync if there are |
||
| 65 | // some folders in the request. |
||
| 66 | $startTag = self::$decoder->getElementStartTag(SYNC_FOLDERS); |
||
| 67 | if (isset($startTag[EN_FLAGS]) && $startTag[EN_FLAGS]) { |
||
| 68 | while (self::$decoder->getElementStartTag(SYNC_FOLDER)) { |
||
| 69 | $actiondata = []; |
||
| 70 | $actiondata["requested"] = true; |
||
| 71 | $actiondata["clientids"] = []; |
||
| 72 | $actiondata["modifyids"] = []; |
||
| 73 | $actiondata["removeids"] = []; |
||
| 74 | $actiondata["fetchids"] = []; |
||
| 75 | $actiondata["statusids"] = []; |
||
| 76 | |||
| 77 | // read class, synckey and folderid without SyncParameters Object for now |
||
| 78 | $class = $synckey = $folderid = false; |
||
|
|
|||
| 79 | |||
| 80 | // if there are already collections in SyncCollections, this is min. the second folder |
||
| 81 | if ($sc->HasCollections()) { |
||
| 82 | $this->singleFolder = false; |
||
| 83 | } |
||
| 84 | |||
| 85 | // for AS versions < 2.5 |
||
| 86 | if (self::$decoder->getElementStartTag(SYNC_FOLDERTYPE)) { |
||
| 87 | $class = self::$decoder->getElementContent(); |
||
| 88 | SLog::Write(LOGLEVEL_DEBUG, sprintf("Sync folder: '%s'", $class)); |
||
| 89 | |||
| 90 | if (!self::$decoder->getElementEndTag()) { |
||
| 91 | return false; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | // SyncKey |
||
| 96 | if (self::$decoder->getElementStartTag(SYNC_SYNCKEY)) { |
||
| 97 | $synckey = "0"; |
||
| 98 | if (($synckey = self::$decoder->getElementContent()) !== false) { |
||
| 99 | if (!self::$decoder->getElementEndTag()) { |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | else { |
||
| 105 | return false; |
||
| 106 | } |
||
| 107 | |||
| 108 | // FolderId |
||
| 109 | if (self::$decoder->getElementStartTag(SYNC_FOLDERID)) { |
||
| 110 | $folderid = self::$decoder->getElementContent(); |
||
| 111 | |||
| 112 | if (!self::$decoder->getElementEndTag()) { |
||
| 113 | return false; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | // compatibility mode AS 1.0 - get folderid which was sent during GetHierarchy() |
||
| 118 | if (!$folderid && $class) { |
||
| 119 | $folderid = self::$deviceManager->GetFolderIdFromCacheByClass($class); |
||
| 120 | } |
||
| 121 | |||
| 122 | // folderid HAS TO BE known by now, so we retrieve the correct SyncParameters object for an update |
||
| 123 | try { |
||
| 124 | $spa = self::$deviceManager->GetStateManager()->GetSynchedFolderState($folderid); |
||
| 125 | |||
| 126 | // TODO remove resync of folders |
||
| 127 | // this forces a resync of all states |
||
| 128 | if (!$spa instanceof SyncParameters) { |
||
| 129 | throw new StateInvalidException("Saved state are not of type SyncParameters"); |
||
| 130 | } |
||
| 131 | |||
| 132 | // new/resync requested |
||
| 133 | if ($synckey == "0") { |
||
| 134 | $spa->RemoveSyncKey(); |
||
| 135 | $spa->DelFolderStat(); |
||
| 136 | $spa->SetMoveState(false); |
||
| 137 | } |
||
| 138 | elseif ($synckey !== false) { |
||
| 139 | if ($synckey !== $spa->GetSyncKey() && $synckey !== $spa->GetNewSyncKey()) { |
||
| 140 | SLog::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"); |
||
| 141 | $spa->DelFolderStat(); |
||
| 142 | } |
||
| 143 | $spa->SetSyncKey($synckey); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | catch (StateInvalidException) { |
||
| 147 | $spa = new SyncParameters(); |
||
| 148 | $status = SYNC_STATUS_INVALIDSYNCKEY; |
||
| 149 | self::$topCollector->AnnounceInformation("State invalid - Resync folder", $this->singleFolder); |
||
| 150 | self::$deviceManager->ForceFolderResync($folderid); |
||
| 151 | $this->saveMultiFolderInfo("exception", "StateInvalidException"); |
||
| 152 | } |
||
| 153 | |||
| 154 | // update folderid.. this might be a new object |
||
| 155 | $spa->SetFolderId($folderid); |
||
| 156 | $spa->SetBackendFolderId(self::$deviceManager->GetBackendIdForFolderId($folderid)); |
||
| 157 | |||
| 158 | if ($class !== false) { |
||
| 159 | $spa->SetContentClass($class); |
||
| 160 | } |
||
| 161 | |||
| 162 | // Get class for as versions >= 12.0 |
||
| 163 | if (!$spa->HasContentClass()) { |
||
| 164 | try { |
||
| 165 | $spa->SetContentClass(self::$deviceManager->GetFolderClassFromCacheByID($spa->GetFolderId())); |
||
| 166 | SLog::Write(LOGLEVEL_DEBUG, sprintf("GetFolderClassFromCacheByID from Device Manager: '%s' for id:'%s'", $spa->GetContentClass(), $spa->GetFolderId())); |
||
| 167 | } |
||
| 168 | catch (NoHierarchyCacheAvailableException) { |
||
| 169 | $status = SYNC_STATUS_FOLDERHIERARCHYCHANGED; |
||
| 170 | self::$deviceManager->ForceFullResync(); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | // done basic SPA initialization/loading -> add to SyncCollection |
||
| 175 | $sc->AddCollection($spa); |
||
| 176 | $sc->AddParameter($spa, "requested", true); |
||
| 177 | |||
| 178 | if ($spa->HasContentClass()) { |
||
| 179 | self::$topCollector->AnnounceInformation(sprintf("%s request", $spa->GetContentClass()), $this->singleFolder); |
||
| 180 | } |
||
| 181 | else { |
||
| 182 | SLog::Write(LOGLEVEL_WARN, "Not possible to determine class of request. Request did not contain class and apparently there is an issue with the HierarchyCache."); |
||
| 183 | } |
||
| 184 | |||
| 185 | // SUPPORTED properties |
||
| 186 | if (($se = self::$decoder->getElementStartTag(SYNC_SUPPORTED)) !== false) { |
||
| 187 | // LG phones send an empty supported tag, so only read the contents if available here |
||
| 188 | // if <Supported/> is received, it's as no supported fields would have been sent at all. |
||
| 189 | // unsure if this is the correct approach, or if in this case some default list should be used |
||
| 190 | if ($se[EN_FLAGS] & EN_FLAGS_CONTENT) { |
||
| 191 | $supfields = []; |
||
| 192 | WBXMLDecoder::ResetInWhile("syncSupported"); |
||
| 193 | while (WBXMLDecoder::InWhile("syncSupported")) { |
||
| 194 | $el = self::$decoder->getElement(); |
||
| 195 | |||
| 196 | if ($el[EN_TYPE] == EN_TYPE_ENDTAG) { |
||
| 197 | break; |
||
| 198 | } |
||
| 199 | |||
| 200 | $supfields[] = $el[EN_TAG]; |
||
| 201 | } |
||
| 202 | self::$deviceManager->SetSupportedFields($spa->GetFolderId(), $supfields); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | // Deletes as moves can be an empty tag as well as have value |
||
| 207 | if (self::$decoder->getElementStartTag(SYNC_DELETESASMOVES)) { |
||
| 208 | $spa->SetDeletesAsMoves(true); |
||
| 209 | if (($dam = self::$decoder->getElementContent()) !== false) { |
||
| 210 | $spa->SetDeletesAsMoves((bool) $dam); |
||
| 211 | if (!self::$decoder->getElementEndTag()) { |
||
| 212 | return false; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | // Get changes can be an empty tag as well as have value |
||
| 218 | // code block partly contributed by dw2412 |
||
| 219 | if ($starttag = self::$decoder->getElementStartTag(SYNC_GETCHANGES)) { |
||
| 220 | $sc->AddParameter($spa, "getchanges", true); |
||
| 221 | if (($gc = self::$decoder->getElementContent()) !== false) { |
||
| 222 | $sc->AddParameter($spa, "getchanges", $gc); |
||
| 223 | } |
||
| 224 | // read the endtag if SYNC_GETCHANGES wasn't an empty tag |
||
| 225 | if ($starttag[EN_FLAGS] & EN_FLAGS_CONTENT) { |
||
| 226 | if (!self::$decoder->getElementEndTag()) { |
||
| 227 | return false; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | if (self::$decoder->getElementStartTag(SYNC_WINDOWSIZE)) { |
||
| 233 | $ws = self::$decoder->getElementContent(); |
||
| 234 | // normalize windowsize |
||
| 235 | if ($ws == 0 || $ws > WINDOW_SIZE_MAX) { |
||
| 236 | $ws = WINDOW_SIZE_MAX; |
||
| 237 | } |
||
| 238 | |||
| 239 | $spa->SetWindowSize($ws); |
||
| 240 | |||
| 241 | // also announce the currently requested window size to the DeviceManager |
||
| 242 | self::$deviceManager->SetWindowSize($spa->GetFolderId(), $spa->GetWindowSize()); |
||
| 243 | |||
| 244 | if (!self::$decoder->getElementEndTag()) { |
||
| 245 | return false; |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | // conversation mode requested |
||
| 250 | if (self::$decoder->getElementStartTag(SYNC_CONVERSATIONMODE)) { |
||
| 251 | $spa->SetConversationMode(true); |
||
| 252 | if (($conversationmode = self::$decoder->getElementContent()) !== false) { |
||
| 253 | $spa->SetConversationMode((bool) $conversationmode); |
||
| 254 | if (!self::$decoder->getElementEndTag()) { |
||
| 255 | return false; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | // Do not truncate by default |
||
| 261 | $spa->SetTruncation(SYNC_TRUNCATION_ALL); |
||
| 262 | |||
| 263 | // use default conflict handling if not specified by the mobile |
||
| 264 | $spa->SetConflict(SYNC_CONFLICT_DEFAULT); |
||
| 265 | |||
| 266 | // save the current filtertype because it might have been changed on the mobile |
||
| 267 | $currentFilterType = $spa->GetFilterType(); |
||
| 268 | |||
| 269 | while (self::$decoder->getElementStartTag(SYNC_OPTIONS)) { |
||
| 270 | $firstOption = true; |
||
| 271 | WBXMLDecoder::ResetInWhile("syncOptions"); |
||
| 272 | while (WBXMLDecoder::InWhile("syncOptions")) { |
||
| 273 | // foldertype definition |
||
| 274 | if (self::$decoder->getElementStartTag(SYNC_FOLDERTYPE)) { |
||
| 275 | $foldertype = self::$decoder->getElementContent(); |
||
| 276 | SLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): specified options block with foldertype '%s'", $foldertype)); |
||
| 277 | |||
| 278 | // switch the foldertype for the next options |
||
| 279 | $spa->UseCPO($foldertype); |
||
| 280 | |||
| 281 | // save the current filtertype because it might have been changed on the mobile |
||
| 282 | $currentFilterType = $spa->GetFilterType(); |
||
| 283 | |||
| 284 | // set to synchronize all changes. The mobile could overwrite this value |
||
| 285 | $spa->SetFilterType(SYNC_FILTERTYPE_ALL); |
||
| 286 | |||
| 287 | if (!self::$decoder->getElementEndTag()) { |
||
| 288 | return false; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | // if no foldertype is defined, use default cpo |
||
| 292 | elseif ($firstOption) { |
||
| 293 | $spa->UseCPO(); |
||
| 294 | // save the current filtertype because it might have been changed on the mobile |
||
| 295 | $currentFilterType = $spa->GetFilterType(); |
||
| 296 | // set to synchronize all changes. The mobile could overwrite this value |
||
| 297 | $spa->SetFilterType(SYNC_FILTERTYPE_ALL); |
||
| 298 | } |
||
| 299 | $firstOption = false; |
||
| 300 | |||
| 301 | if (self::$decoder->getElementStartTag(SYNC_FILTERTYPE)) { |
||
| 302 | $spa->SetFilterType(self::$decoder->getElementContent()); |
||
| 303 | if (!self::$decoder->getElementEndTag()) { |
||
| 304 | return false; |
||
| 305 | } |
||
| 306 | } |
||
| 307 | if (self::$decoder->getElementStartTag(SYNC_TRUNCATION)) { |
||
| 308 | $spa->SetTruncation(self::$decoder->getElementContent()); |
||
| 309 | if (!self::$decoder->getElementEndTag()) { |
||
| 310 | return false; |
||
| 311 | } |
||
| 312 | } |
||
| 313 | if (self::$decoder->getElementStartTag(SYNC_RTFTRUNCATION)) { |
||
| 314 | $spa->SetRTFTruncation(self::$decoder->getElementContent()); |
||
| 315 | if (!self::$decoder->getElementEndTag()) { |
||
| 316 | return false; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | if (self::$decoder->getElementStartTag(SYNC_MIMESUPPORT)) { |
||
| 321 | $spa->SetMimeSupport(self::$decoder->getElementContent()); |
||
| 322 | if (!self::$decoder->getElementEndTag()) { |
||
| 323 | return false; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | if (self::$decoder->getElementStartTag(SYNC_MIMETRUNCATION)) { |
||
| 328 | $spa->SetMimeTruncation(self::$decoder->getElementContent()); |
||
| 329 | if (!self::$decoder->getElementEndTag()) { |
||
| 330 | return false; |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | if (self::$decoder->getElementStartTag(SYNC_CONFLICT)) { |
||
| 335 | $spa->SetConflict(self::$decoder->getElementContent()); |
||
| 336 | if (!self::$decoder->getElementEndTag()) { |
||
| 337 | return false; |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | while (self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_BODYPREFERENCE)) { |
||
| 342 | if (self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_TYPE)) { |
||
| 343 | $bptype = self::$decoder->getElementContent(); |
||
| 344 | $spa->BodyPreference($bptype); |
||
| 345 | if (!self::$decoder->getElementEndTag()) { |
||
| 346 | return false; |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | if (self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_TRUNCATIONSIZE)) { |
||
| 351 | $spa->BodyPreference($bptype)->SetTruncationSize(self::$decoder->getElementContent()); |
||
| 352 | if (!self::$decoder->getElementEndTag()) { |
||
| 353 | return false; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | if (self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_ALLORNONE)) { |
||
| 358 | $spa->BodyPreference($bptype)->SetAllOrNone(self::$decoder->getElementContent()); |
||
| 359 | if (!self::$decoder->getElementEndTag()) { |
||
| 360 | return false; |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | if (self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_PREVIEW)) { |
||
| 365 | $spa->BodyPreference($bptype)->SetPreview(self::$decoder->getElementContent()); |
||
| 366 | if (!self::$decoder->getElementEndTag()) { |
||
| 367 | return false; |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | if (!self::$decoder->getElementEndTag()) { |
||
| 372 | return false; |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | if (self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_BODYPARTPREFERENCE)) { |
||
| 377 | if (self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_TYPE)) { |
||
| 378 | $bpptype = self::$decoder->getElementContent(); |
||
| 379 | $spa->BodyPartPreference($bpptype); |
||
| 380 | if (!self::$decoder->getElementEndTag()) { |
||
| 381 | return false; |
||
| 382 | } |
||
| 383 | } |
||
| 384 | |||
| 385 | if (self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_TRUNCATIONSIZE)) { |
||
| 386 | $spa->BodyPartPreference($bpptype)->SetTruncationSize(self::$decoder->getElementContent()); |
||
| 387 | if (!self::$decoder->getElementEndTag()) { |
||
| 388 | return false; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | if (self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_ALLORNONE)) { |
||
| 393 | $spa->BodyPartPreference($bpptype)->SetAllOrNone(self::$decoder->getElementContent()); |
||
| 394 | if (!self::$decoder->getElementEndTag()) { |
||
| 395 | return false; |
||
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | if (self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_PREVIEW)) { |
||
| 400 | $spa->BodyPartPreference($bpptype)->SetPreview(self::$decoder->getElementContent()); |
||
| 401 | if (!self::$decoder->getElementEndTag()) { |
||
| 402 | return false; |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | if (!self::$decoder->getElementEndTag()) { |
||
| 407 | return false; |
||
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | if (self::$decoder->getElementStartTag(SYNC_RIGHTSMANAGEMENT_SUPPORT)) { |
||
| 412 | $spa->SetRmSupport(self::$decoder->getElementContent()); |
||
| 413 | if (!self::$decoder->getElementEndTag()) { |
||
| 414 | return false; |
||
| 415 | } |
||
| 416 | } |
||
| 417 | |||
| 418 | $e = self::$decoder->peek(); |
||
| 419 | if ($e[EN_TYPE] == EN_TYPE_ENDTAG) { |
||
| 420 | self::$decoder->getElementEndTag(); |
||
| 421 | |||
| 422 | break; |
||
| 423 | } |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | // limit items to be synchronized to the mobiles if configured |
||
| 428 | $maxAllowed = self::$deviceManager->GetFilterType($spa->GetFolderId(), $spa->GetBackendFolderId()); |
||
| 429 | if ($maxAllowed > SYNC_FILTERTYPE_ALL && |
||
| 430 | (!$spa->HasFilterType() || $spa->GetFilterType() == SYNC_FILTERTYPE_ALL || $spa->GetFilterType() > $maxAllowed)) { |
||
| 431 | SLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): FilterType applied globally or specifically, using value: %s", $maxAllowed)); |
||
| 432 | $spa->SetFilterType($maxAllowed); |
||
| 433 | } |
||
| 434 | |||
| 435 | if ($currentFilterType != $spa->GetFilterType()) { |
||
| 436 | SLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): FilterType has changed (old: '%s', new: '%s'), removing folderstat to force Exporter setup", $currentFilterType, $spa->GetFilterType())); |
||
| 437 | $spa->DelFolderStat(); |
||
| 438 | } |
||
| 439 | |||
| 440 | // Check if the hierarchycache is available. If not, trigger a HierarchySync |
||
| 441 | if (self::$deviceManager->IsHierarchySyncRequired()) { |
||
| 442 | $status = SYNC_STATUS_FOLDERHIERARCHYCHANGED; |
||
| 443 | SLog::Write(LOGLEVEL_DEBUG, "HierarchyCache is also not available. Triggering HierarchySync to device"); |
||
| 444 | } |
||
| 445 | |||
| 446 | // AS16: Check if this is a DRAFTS folder - if so, disable FilterType |
||
| 447 | if (Request::GetProtocolVersion() >= 16.0 && self::$deviceManager->GetFolderTypeFromCacheById($spa->GetFolderId()) == SYNC_FOLDER_TYPE_DRAFTS) { |
||
| 448 | $spa->SetFilterType(SYNC_FILTERTYPE_DISABLE); |
||
| 449 | SLog::Write(LOGLEVEL_DEBUG, "HandleSync(): FilterType has been disabled as this is a DRAFTS folder."); |
||
| 450 | } |
||
| 451 | |||
| 452 | if (($el = self::$decoder->getElementStartTag(SYNC_PERFORM)) && ($el[EN_FLAGS] & EN_FLAGS_CONTENT)) { |
||
| 453 | // We can not proceed here as the content class is unknown |
||
| 454 | if ($status != SYNC_STATUS_SUCCESS) { |
||
| 455 | SLog::Write(LOGLEVEL_WARN, "Ignoring all incoming actions as global status indicates problem."); |
||
| 456 | $wbxmlproblem = true; |
||
| 457 | |||
| 458 | break; |
||
| 459 | } |
||
| 460 | |||
| 461 | $performaction = true; |
||
| 462 | |||
| 463 | // unset the importer |
||
| 464 | $this->importer = false; |
||
| 465 | |||
| 466 | $nchanges = 0; |
||
| 467 | WBXMLDecoder::ResetInWhile("syncActions"); |
||
| 468 | while (WBXMLDecoder::InWhile("syncActions")) { |
||
| 469 | // ADD, MODIFY, REMOVE or FETCH |
||
| 470 | $element = self::$decoder->getElement(); |
||
| 471 | |||
| 472 | if ($element[EN_TYPE] != EN_TYPE_STARTTAG) { |
||
| 473 | self::$decoder->ungetElement($element); |
||
| 474 | |||
| 475 | break; |
||
| 476 | } |
||
| 477 | |||
| 478 | if ($status == SYNC_STATUS_SUCCESS) { |
||
| 479 | ++$nchanges; |
||
| 480 | } |
||
| 481 | |||
| 482 | // Foldertype sent when syncing SMS |
||
| 483 | if (self::$decoder->getElementStartTag(SYNC_FOLDERTYPE)) { |
||
| 484 | $foldertype = self::$decoder->getElementContent(); |
||
| 485 | SLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): incoming data with foldertype '%s'", $foldertype)); |
||
| 486 | |||
| 487 | if (!self::$decoder->getElementEndTag()) { |
||
| 488 | return false; |
||
| 489 | } |
||
| 490 | } |
||
| 491 | else { |
||
| 492 | $foldertype = false; |
||
| 493 | } |
||
| 494 | |||
| 495 | $serverid = false; |
||
| 496 | if (self::$decoder->getElementStartTag(SYNC_SERVERENTRYID)) { |
||
| 497 | if (($serverid = self::$decoder->getElementContent()) !== false) { |
||
| 498 | if (!self::$decoder->getElementEndTag()) { // end serverid |
||
| 499 | return false; |
||
| 500 | } |
||
| 501 | } |
||
| 502 | } |
||
| 503 | // get the instanceId if available |
||
| 504 | $instanceid = false; |
||
| 505 | if (self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_INSTANCEID)) { |
||
| 506 | if (($instanceid = self::$decoder->getElementContent()) !== false) { |
||
| 507 | if (!self::$decoder->getElementEndTag()) { // end instanceid |
||
| 508 | return false; |
||
| 509 | } |
||
| 510 | } |
||
| 511 | } |
||
| 512 | |||
| 513 | if (self::$decoder->getElementStartTag(SYNC_CLIENTENTRYID)) { |
||
| 514 | $clientid = self::$decoder->getElementContent(); |
||
| 515 | |||
| 516 | if (!self::$decoder->getElementEndTag()) { // end clientid |
||
| 517 | return false; |
||
| 518 | } |
||
| 519 | } |
||
| 520 | else { |
||
| 521 | $clientid = false; |
||
| 522 | } |
||
| 523 | |||
| 524 | // Get the SyncMessage if sent |
||
| 525 | if (($el = self::$decoder->getElementStartTag(SYNC_DATA)) && ($el[EN_FLAGS] & EN_FLAGS_CONTENT)) { |
||
| 526 | $message = GSync::getSyncObjectFromFolderClass($spa->GetContentClass()); |
||
| 527 | $message->Decode(self::$decoder); |
||
| 528 | |||
| 529 | // set Ghosted fields |
||
| 530 | $message->emptySupported(self::$deviceManager->GetSupportedFields($spa->GetFolderId())); |
||
| 531 | |||
| 532 | if (!self::$decoder->getElementEndTag()) { // end applicationdata |
||
| 533 | return false; |
||
| 534 | } |
||
| 535 | } |
||
| 536 | else { |
||
| 537 | $message = false; |
||
| 538 | } |
||
| 539 | |||
| 540 | // InstanceID sent: do action to a recurrency exception |
||
| 541 | if ($instanceid) { |
||
| 542 | // for delete actions we don't have an ASObject |
||
| 543 | if (!$message) { |
||
| 544 | $message = GSync::getSyncObjectFromFolderClass($spa->GetContentClass()); |
||
| 545 | $message->Decode(self::$decoder); |
||
| 546 | } |
||
| 547 | $message->instanceid = $instanceid; |
||
| 548 | if ($element[EN_TAG] == SYNC_REMOVE) { |
||
| 549 | $message->instanceiddelete = true; |
||
| 550 | $element[EN_TAG] = SYNC_MODIFY; |
||
| 551 | } |
||
| 552 | } |
||
| 553 | |||
| 554 | switch ($element[EN_TAG]) { |
||
| 555 | case SYNC_FETCH: |
||
| 556 | array_push($actiondata["fetchids"], $serverid); |
||
| 557 | break; |
||
| 558 | |||
| 559 | default: |
||
| 560 | // get the importer |
||
| 561 | if ($this->importer === false) { |
||
| 562 | $status = $this->getImporter($sc, $spa, $actiondata); |
||
| 563 | } |
||
| 564 | |||
| 565 | if ($status == SYNC_STATUS_SUCCESS) { |
||
| 566 | $this->importMessage($spa, $actiondata, $element[EN_TAG], $message, $clientid, $serverid, $foldertype, $nchanges); |
||
| 567 | } |
||
| 568 | else { |
||
| 569 | SLog::Write(LOGLEVEL_WARN, "Ignored incoming change, global status indicates problem."); |
||
| 570 | } |
||
| 571 | break; |
||
| 572 | } |
||
| 573 | |||
| 574 | if ($actiondata["fetchids"]) { |
||
| 575 | self::$topCollector->AnnounceInformation(sprintf("Fetching %d", $nchanges)); |
||
| 576 | } |
||
| 577 | else { |
||
| 578 | self::$topCollector->AnnounceInformation(sprintf("Incoming %d", $nchanges)); |
||
| 579 | } |
||
| 580 | |||
| 581 | if (!self::$decoder->getElementEndTag()) { // end add/change/delete/move |
||
| 582 | return false; |
||
| 583 | } |
||
| 584 | } |
||
| 585 | |||
| 586 | if ($status == SYNC_STATUS_SUCCESS && $this->importer !== false) { |
||
| 587 | SLog::Write(LOGLEVEL_INFO, sprintf("Processed '%d' incoming changes", $nchanges)); |
||
| 588 | if (!$actiondata["fetchids"]) { |
||
| 589 | self::$topCollector->AnnounceInformation(sprintf("%d incoming", $nchanges), $this->singleFolder); |
||
| 590 | $this->saveMultiFolderInfo("incoming", $nchanges); |
||
| 591 | } |
||
| 592 | |||
| 593 | try { |
||
| 594 | // Save the updated state, which is used for the exporter later |
||
| 595 | $sc->AddParameter($spa, "state", $this->importer->GetState()); |
||
| 596 | } |
||
| 597 | catch (StatusException $stex) { |
||
| 598 | $status = $stex->getCode(); |
||
| 599 | } |
||
| 600 | |||
| 601 | // Check if changes are requested - if not and there are no changes to be exported anyway, update folderstat! |
||
| 602 | if (!$sc->GetParameter($spa, "getchanges") && !$sc->CountChange($spa->GetFolderId())) { |
||
| 603 | SLog::Write(LOGLEVEL_DEBUG, "Incoming changes, no export requested: update folderstat"); |
||
| 604 | $newFolderStatAfterImport = self::$backend->GetFolderStat(GSync::GetAdditionalSyncFolderStore($spa->GetBackendFolderId()), $spa->GetBackendFolderId()); |
||
| 605 | $this->setFolderStat($spa, $newFolderStatAfterImport); |
||
| 606 | } |
||
| 607 | } |
||
| 608 | |||
| 609 | if (!self::$decoder->getElementEndTag()) { // end PERFORM |
||
| 610 | return false; |
||
| 611 | } |
||
| 612 | } |
||
| 613 | |||
| 614 | // save the failsafe state |
||
| 615 | if (!empty($actiondata["statusids"])) { |
||
| 616 | unset($actiondata["failstate"]); |
||
| 617 | $actiondata["failedsyncstate"] = $sc->GetParameter($spa, "state"); |
||
| 618 | self::$deviceManager->GetStateManager()->SetSyncFailState($actiondata); |
||
| 619 | } |
||
| 620 | |||
| 621 | // save actiondata |
||
| 622 | $sc->AddParameter($spa, "actiondata", $actiondata); |
||
| 623 | |||
| 624 | if (!self::$decoder->getElementEndTag()) { // end collection |
||
| 625 | return false; |
||
| 626 | } |
||
| 627 | |||
| 628 | // AS14 does not send GetChanges anymore. We should do it if there were no incoming changes |
||
| 629 | if (!isset($performaction) && !$sc->GetParameter($spa, "getchanges") && $spa->HasSyncKey()) { |
||
| 630 | $sc->AddParameter($spa, "getchanges", true); |
||
| 631 | } |
||
| 632 | } // END FOLDER |
||
| 633 | |||
| 634 | if (!$wbxmlproblem && !self::$decoder->getElementEndTag()) { // end collections |
||
| 635 | return false; |
||
| 636 | } |
||
| 637 | } // end FOLDERS |
||
| 638 | |||
| 639 | if (self::$decoder->getElementStartTag(SYNC_HEARTBEATINTERVAL)) { |
||
| 640 | $hbinterval = self::$decoder->getElementContent(); |
||
| 641 | if (!self::$decoder->getElementEndTag()) { // SYNC_HEARTBEATINTERVAL |
||
| 642 | return false; |
||
| 643 | } |
||
| 644 | } |
||
| 645 | |||
| 646 | if (self::$decoder->getElementStartTag(SYNC_WAIT)) { |
||
| 647 | $wait = self::$decoder->getElementContent(); |
||
| 648 | if (!self::$decoder->getElementEndTag()) { // SYNC_WAIT |
||
| 649 | return false; |
||
| 650 | } |
||
| 651 | |||
| 652 | // internally the heartbeat interval and the wait time are the same |
||
| 653 | // heartbeat is in seconds, wait in minutes |
||
| 654 | $hbinterval = $wait * 60; |
||
| 655 | } |
||
| 656 | |||
| 657 | if (self::$decoder->getElementStartTag(SYNC_WINDOWSIZE)) { |
||
| 658 | $sc->SetGlobalWindowSize(self::$decoder->getElementContent()); |
||
| 659 | SLog::Write(LOGLEVEL_DEBUG, "Sync(): Global WindowSize requested: " . $sc->GetGlobalWindowSize()); |
||
| 660 | if (!self::$decoder->getElementEndTag()) { // SYNC_WINDOWSIZE |
||
| 661 | return false; |
||
| 662 | } |
||
| 663 | } |
||
| 664 | |||
| 665 | if (self::$decoder->getElementStartTag(SYNC_PARTIAL)) { |
||
| 666 | $partial = true; |
||
| 667 | } |
||
| 668 | else { |
||
| 669 | $partial = false; |
||
| 670 | } |
||
| 671 | |||
| 672 | if (!$wbxmlproblem && !self::$decoder->getElementEndTag()) { // end sync |
||
| 673 | return false; |
||
| 674 | } |
||
| 675 | } |
||
| 676 | // we did not receive a SYNCHRONIZE block - assume empty sync |
||
| 677 | else { |
||
| 678 | $emptysync = true; |
||
| 679 | } |
||
| 680 | // END SYNCHRONIZE |
||
| 681 | |||
| 682 | // check heartbeat/wait time |
||
| 683 | if (isset($hbinterval)) { |
||
| 684 | if ($hbinterval < 60 || $hbinterval > 3540) { |
||
| 685 | $status = SYNC_STATUS_INVALIDWAITORHBVALUE; |
||
| 686 | SLog::Write(LOGLEVEL_WARN, sprintf("HandleSync(): Invalid heartbeat or wait value '%s'", $hbinterval)); |
||
| 687 | } |
||
| 688 | } |
||
| 689 | |||
| 690 | // Partial & Empty Syncs need saved data to proceed with synchronization |
||
| 691 | if ($status == SYNC_STATUS_SUCCESS && ($emptysync === true || $partial === true)) { |
||
| 692 | SLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): Partial or Empty sync requested. Retrieving data of synchronized folders.")); |
||
| 693 | |||
| 694 | // Load all collections - do not overwrite existing (received!), load states, check permissions and only load confirmed states! |
||
| 695 | try { |
||
| 696 | $sc->LoadAllCollections(false, true, true, true, true); |
||
| 697 | } |
||
| 698 | catch (StateInvalidException) { |
||
| 699 | $status = SYNC_STATUS_INVALIDSYNCKEY; |
||
| 700 | self::$topCollector->AnnounceInformation("StateNotFoundException", $this->singleFolder); |
||
| 701 | $this->saveMultiFolderInfo("exception", "StateNotFoundException"); |
||
| 702 | } |
||
| 703 | catch (StatusException) { |
||
| 704 | $status = SYNC_STATUS_FOLDERHIERARCHYCHANGED; |
||
| 705 | self::$topCollector->AnnounceInformation(sprintf("StatusException code: %d", $status), $this->singleFolder); |
||
| 706 | $this->saveMultiFolderInfo("exception", "StatusException"); |
||
| 707 | } |
||
| 708 | |||
| 709 | // update a few values |
||
| 710 | foreach ($sc as $folderid => $spa) { |
||
| 711 | // manually set getchanges parameter for this collection if it is synchronized |
||
| 712 | if ($spa->HasSyncKey()) { |
||
| 713 | $actiondata = $sc->GetParameter($spa, "actiondata"); |
||
| 714 | // request changes if no other actions are executed |
||
| 715 | if (empty($actiondata["modifyids"]) && empty($actiondata["clientids"]) && empty($actiondata["removeids"])) { |
||
| 716 | $sc->AddParameter($spa, "getchanges", true); |
||
| 717 | } |
||
| 718 | |||
| 719 | // announce WindowSize to DeviceManager |
||
| 720 | self::$deviceManager->SetWindowSize($folderid, $spa->GetWindowSize()); |
||
| 721 | } |
||
| 722 | } |
||
| 723 | if (!$sc->HasCollections()) { |
||
| 724 | $status = SYNC_STATUS_SYNCREQUESTINCOMPLETE; |
||
| 725 | } |
||
| 726 | } |
||
| 727 | elseif (isset($hbinterval)) { |
||
| 728 | // load the hierarchy data - there are no permissions to verify so we just set it to false |
||
| 729 | if (!$sc->LoadCollection(false, true, false)) { |
||
| 730 | $status = SYNC_STATUS_FOLDERHIERARCHYCHANGED; |
||
| 731 | self::$topCollector->AnnounceInformation(sprintf("StatusException code: %d", $status), $this->singleFolder); |
||
| 732 | $this->saveMultiFolderInfo("exception", "StatusException"); |
||
| 733 | } |
||
| 734 | } |
||
| 735 | |||
| 736 | // HEARTBEAT |
||
| 737 | if ($status == SYNC_STATUS_SUCCESS && isset($hbinterval)) { |
||
| 738 | $interval = (defined('PING_INTERVAL') && PING_INTERVAL > 0) ? PING_INTERVAL : 30; |
||
| 739 | $sc->SetLifetime($hbinterval); |
||
| 740 | |||
| 741 | // states are lazy loaded - we have to make sure that they are there! |
||
| 742 | $loadstatus = SYNC_STATUS_SUCCESS; |
||
| 743 | foreach ($sc as $folderid => $spa) { |
||
| 744 | // some androids do heartbeat on the OUTBOX folder, with weird results |
||
| 745 | // we do not load the state so we will never get relevant changes on the OUTBOX folder |
||
| 746 | if (self::$deviceManager->GetFolderTypeFromCacheById($folderid) == SYNC_FOLDER_TYPE_OUTBOX) { |
||
| 747 | SLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): Heartbeat on Outbox folder not allowed")); |
||
| 748 | |||
| 749 | continue; |
||
| 750 | } |
||
| 751 | |||
| 752 | $fad = []; |
||
| 753 | // if loading the states fails, we do not enter heartbeat, but we keep $status on SYNC_STATUS_SUCCESS |
||
| 754 | // so when the changes are exported the correct folder gets an SYNC_STATUS_INVALIDSYNCKEY |
||
| 755 | if ($loadstatus == SYNC_STATUS_SUCCESS) { |
||
| 756 | $loadstatus = $this->loadStates($sc, $spa, $fad); |
||
| 757 | } |
||
| 758 | } |
||
| 759 | |||
| 760 | if ($loadstatus == SYNC_STATUS_SUCCESS) { |
||
| 761 | $foundchanges = false; |
||
| 762 | |||
| 763 | try { |
||
| 764 | // always check for changes |
||
| 765 | SLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): Entering Heartbeat mode")); |
||
| 766 | $foundchanges = $sc->CheckForChanges($sc->GetLifetime(), $interval); |
||
| 767 | } |
||
| 768 | catch (StatusException $stex) { |
||
| 769 | if ($stex->getCode() == SyncCollections::OBSOLETE_CONNECTION) { |
||
| 770 | $status = SYNC_COMMONSTATUS_SYNCSTATEVERSIONINVALID; |
||
| 771 | } |
||
| 772 | else { |
||
| 773 | $status = SYNC_STATUS_FOLDERHIERARCHYCHANGED; |
||
| 774 | self::$topCollector->AnnounceInformation(sprintf("StatusException code: %d", $status), $this->singleFolder); |
||
| 775 | $this->saveMultiFolderInfo("exception", "StatusException"); |
||
| 776 | } |
||
| 777 | } |
||
| 778 | |||
| 779 | // update the waittime waited |
||
| 780 | self::$waitTime = $sc->GetWaitedSeconds(); |
||
| 781 | |||
| 782 | // in case there are no changes and no other request has synchronized while we waited, we can reply with an empty response |
||
| 783 | if (!$foundchanges && $status == SYNC_STATUS_SUCCESS) { |
||
| 784 | // if there were changes to the SPA or CPOs we need to save this before we terminate |
||
| 785 | // only save if the state was not modified by some other request, if so, return state invalid status |
||
| 786 | foreach ($sc as $folderid => $spa) { |
||
| 787 | if (self::$deviceManager->CheckHearbeatStateIntegrity($spa->GetFolderId(), $spa->GetUuid(), $spa->GetUuidCounter())) { |
||
| 788 | $status = SYNC_COMMONSTATUS_SYNCSTATEVERSIONINVALID; |
||
| 789 | } |
||
| 790 | else { |
||
| 791 | $sc->SaveCollection($spa); |
||
| 792 | } |
||
| 793 | } |
||
| 794 | |||
| 795 | if ($status == SYNC_STATUS_SUCCESS) { |
||
| 796 | SLog::Write(LOGLEVEL_DEBUG, "No changes found and no other process changed states. Replying with empty response and closing connection."); |
||
| 797 | self::$specialHeaders = []; |
||
| 798 | self::$specialHeaders[] = "Connection: close"; |
||
| 799 | |||
| 800 | return true; |
||
| 801 | } |
||
| 802 | } |
||
| 803 | |||
| 804 | if ($foundchanges) { |
||
| 805 | foreach ($sc->GetChangedFolderIds() as $folderid => $changecount) { |
||
| 806 | // check if there were other sync requests for a folder during the heartbeat |
||
| 807 | $spa = $sc->GetCollection($folderid); |
||
| 808 | if ($changecount > 0 && $sc->WaitedForChanges() && self::$deviceManager->CheckHearbeatStateIntegrity($spa->GetFolderId(), $spa->GetUuid(), $spa->GetUuidCounter())) { |
||
| 809 | SLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): heartbeat: found %d changes in '%s' which was already synchronized. Heartbeat aborted!", $changecount, $folderid)); |
||
| 810 | $status = SYNC_COMMONSTATUS_SYNCSTATEVERSIONINVALID; |
||
| 811 | } |
||
| 812 | else { |
||
| 813 | SLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): heartbeat: found %d changes in '%s'", $changecount, $folderid)); |
||
| 814 | } |
||
| 815 | } |
||
| 816 | } |
||
| 817 | } |
||
| 818 | } |
||
| 819 | |||
| 820 | // Start the output |
||
| 821 | SLog::Write(LOGLEVEL_DEBUG, "HandleSync(): Start Output"); |
||
| 822 | |||
| 823 | // global status |
||
| 824 | // SYNC_COMMONSTATUS_* start with values from 101 |
||
| 825 | if ($status != SYNC_COMMONSTATUS_SUCCESS && ($status == SYNC_STATUS_FOLDERHIERARCHYCHANGED || $status > 100)) { |
||
| 826 | self::$deviceManager->AnnounceProcessStatus($folderid, $status); |
||
| 827 | $this->sendStartTags(); |
||
| 828 | self::$encoder->startTag(SYNC_STATUS); |
||
| 829 | self::$encoder->content($status); |
||
| 830 | self::$encoder->endTag(); |
||
| 831 | self::$encoder->endTag(); // SYNC_SYNCHRONIZE |
||
| 832 | |||
| 833 | return true; |
||
| 834 | } |
||
| 835 | |||
| 836 | // Loop through requested folders |
||
| 837 | foreach ($sc as $folderid => $spa) { |
||
| 838 | // get actiondata |
||
| 839 | $actiondata = $sc->GetParameter($spa, "actiondata"); |
||
| 840 | |||
| 841 | if ($status == SYNC_STATUS_SUCCESS && (!$spa->GetContentClass() || !$spa->GetFolderId())) { |
||
| 842 | SLog::Write(LOGLEVEL_ERROR, sprintf("HandleSync(): no content class or folderid found for collection.")); |
||
| 843 | |||
| 844 | continue; |
||
| 845 | } |
||
| 846 | |||
| 847 | if (!$sc->GetParameter($spa, "requested")) { |
||
| 848 | SLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync(): partial sync for folder class '%s' with id '%s'", $spa->GetContentClass(), $spa->GetFolderId())); |
||
| 849 | // reload state and initialize StateMachine correctly |
||
| 850 | $sc->AddParameter($spa, "state", null); |
||
| 851 | $status = $this->loadStates($sc, $spa, $actiondata); |
||
| 852 | } |
||
| 853 | |||
| 854 | // initialize exporter to get changecount |
||
| 855 | $changecount = false; |
||
| 856 | $exporter = false; |
||
| 857 | $streamimporter = false; |
||
| 858 | $newFolderStat = false; |
||
| 859 | $setupExporter = true; |
||
| 860 | |||
| 861 | // TODO we could check against $sc->GetChangedFolderIds() on heartbeat so we do not need to configure all exporter again |
||
| 862 | if ($status == SYNC_STATUS_SUCCESS && ($sc->GetParameter($spa, "getchanges") || !$spa->HasSyncKey())) { |
||
| 863 | // no need to run the exporter if the globalwindowsize is already full - if collection already has a synckey |
||
| 864 | if ($sc->GetGlobalWindowSize() == $this->globallyExportedItems && $spa->HasSyncKey()) { |
||
| 865 | SLog::Write(LOGLEVEL_DEBUG, sprintf("Sync(): no exporter setup for '%s' as GlobalWindowSize is full.", $spa->GetFolderId())); |
||
| 866 | $setupExporter = false; |
||
| 867 | } |
||
| 868 | // if the maximum request timeout is reached, stop processing other collections |
||
| 869 | if (Request::IsRequestTimeoutReached()) { |
||
| 870 | SLog::Write(LOGLEVEL_DEBUG, sprintf("Sync(): no exporter setup for '%s' as request timeout reached, omitting output for collection.", $spa->GetFolderId())); |
||
| 871 | $setupExporter = false; |
||
| 872 | } |
||
| 873 | |||
| 874 | // if max memory allocation is reached, stop processing other collections |
||
| 875 | if (Request::IsRequestMemoryLimitReached()) { |
||
| 876 | SLog::Write(LOGLEVEL_DEBUG, sprintf("Sync(): no exporter setup for '%s' as max memory allocatation reached, omitting output for collection.", $spa->GetFolderId())); |
||
| 877 | $setupExporter = false; |
||
| 878 | } |
||
| 879 | |||
| 880 | // force exporter run if there is a saved status |
||
| 881 | if ($setupExporter && self::$deviceManager->HasFolderSyncStatus($spa->GetFolderId())) { |
||
| 882 | SLog::Write(LOGLEVEL_DEBUG, sprintf("Sync(): forcing exporter setup for '%s' as a sync status is saved - ignoring backend folder stats", $spa->GetFolderId())); |
||
| 883 | } |
||
| 884 | // compare the folder statistics if the backend supports this |
||
| 885 | elseif ($setupExporter && self::$backend->HasFolderStats()) { |
||
| 886 | // check if the folder stats changed -> if not, don't setup the exporter, there are no changes! |
||
| 887 | $newFolderStat = self::$backend->GetFolderStat(GSync::GetAdditionalSyncFolderStore($spa->GetBackendFolderId()), $spa->GetBackendFolderId()); |
||
| 888 | if ($newFolderStat !== false && !$spa->IsExporterRunRequired($newFolderStat, true)) { |
||
| 889 | $changecount = 0; |
||
| 890 | $setupExporter = false; |
||
| 891 | } |
||
| 892 | } |
||
| 893 | |||
| 894 | // Do a full Exporter setup if we can't avoid it |
||
| 895 | if ($setupExporter) { |
||
| 896 | // make sure the states are loaded |
||
| 897 | $status = $this->loadStates($sc, $spa, $actiondata); |
||
| 898 | |||
| 899 | if ($status == SYNC_STATUS_SUCCESS) { |
||
| 900 | try { |
||
| 901 | // if this is an additional folder the backend has to be setup correctly |
||
| 902 | if (!self::$backend->Setup(GSync::GetAdditionalSyncFolderStore($spa->GetBackendFolderId()))) { |
||
| 903 | throw new StatusException(sprintf("HandleSync() could not Setup() the backend for folder id %s/%s", $spa->GetFolderId(), $spa->GetBackendFolderId()), SYNC_STATUS_FOLDERHIERARCHYCHANGED); |
||
| 904 | } |
||
| 905 | |||
| 906 | // Use the state from the importer, as changes may have already happened |
||
| 907 | $exporter = self::$backend->GetExporter($spa->GetBackendFolderId()); |
||
| 908 | |||
| 909 | if ($exporter === false) { |
||
| 910 | throw new StatusException(sprintf("HandleSync() could not get an exporter for folder id %s/%s", $spa->GetFolderId(), $spa->GetBackendFolderId()), SYNC_STATUS_FOLDERHIERARCHYCHANGED); |
||
| 911 | } |
||
| 912 | } |
||
| 913 | catch (StatusException $stex) { |
||
| 914 | $status = $stex->getCode(); |
||
| 915 | } |
||
| 916 | |||
| 917 | try { |
||
| 918 | // Stream the messages directly to the PDA |
||
| 919 | $streamimporter = new ImportChangesStream(self::$encoder, GSync::getSyncObjectFromFolderClass($spa->GetContentClass())); |
||
| 920 | |||
| 921 | if ($exporter !== false) { |
||
| 922 | $exporter->Config($sc->GetParameter($spa, "state")); |
||
| 923 | $exporter->ConfigContentParameters($spa->GetCPO()); |
||
| 924 | $exporter->InitializeExporter($streamimporter); |
||
| 925 | |||
| 926 | $changecount = $exporter->GetChangeCount(); |
||
| 927 | } |
||
| 928 | } |
||
| 929 | catch (StatusException $stex) { |
||
| 930 | if ($stex->getCode() === SYNC_FSSTATUS_CODEUNKNOWN && $spa->HasSyncKey()) { |
||
| 931 | $status = SYNC_STATUS_INVALIDSYNCKEY; |
||
| 932 | } |
||
| 933 | else { |
||
| 934 | $status = $stex->getCode(); |
||
| 935 | } |
||
| 936 | } |
||
| 937 | |||
| 938 | if (!$spa->HasSyncKey()) { |
||
| 939 | self::$topCollector->AnnounceInformation(sprintf("Exporter registered. %d objects queued.", $changecount), $this->singleFolder); |
||
| 940 | $this->saveMultiFolderInfo("queued", $changecount); |
||
| 941 | // update folder status as initialized |
||
| 942 | $spa->SetFolderSyncTotal($changecount); |
||
| 943 | $spa->SetFolderSyncRemaining($changecount); |
||
| 944 | if ($changecount > 0) { |
||
| 945 | self::$deviceManager->SetFolderSyncStatus($folderid, DeviceManager::FLD_SYNC_INITIALIZED); |
||
| 946 | } |
||
| 947 | } |
||
| 948 | elseif ($status != SYNC_STATUS_SUCCESS) { |
||
| 949 | self::$topCollector->AnnounceInformation(sprintf("StatusException code: %d", $status), $this->singleFolder); |
||
| 950 | $this->saveMultiFolderInfo("exception", "StatusException"); |
||
| 951 | } |
||
| 952 | self::$deviceManager->AnnounceProcessStatus($spa->GetFolderId(), $status); |
||
| 953 | } |
||
| 954 | } |
||
| 955 | } |
||
| 956 | |||
| 957 | // 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 |
||
| 958 | if (!empty($actiondata["modifyids"]) || |
||
| 959 | !empty($actiondata["clientids"]) || |
||
| 960 | !empty($actiondata["removeids"]) || |
||
| 961 | (!$spa->HasSyncKey() && $status == SYNC_STATUS_SUCCESS)) { |
||
| 962 | $spa->SetNewSyncKey(self::$deviceManager->GetStateManager()->GetNewSyncKey($spa->GetSyncKey())); |
||
| 963 | } |
||
| 964 | // get a new synckey only if we did not reach the global limit yet |
||
| 965 | else { |
||
| 966 | // when reaching the global limit for changes of all collections, stop processing other collections |
||
| 967 | if ($sc->GetGlobalWindowSize() <= $this->globallyExportedItems) { |
||
| 968 | SLog::Write(LOGLEVEL_DEBUG, "Global WindowSize for amount of exported changes reached, omitting output for collection."); |
||
| 969 | |||
| 970 | continue; |
||
| 971 | } |
||
| 972 | |||
| 973 | // get a new synckey if there are changes are we did not reach the limit yet |
||
| 974 | if ($changecount > 0) { |
||
| 975 | $spa->SetNewSyncKey(self::$deviceManager->GetStateManager()->GetNewSyncKey($spa->GetSyncKey())); |
||
| 976 | } |
||
| 977 | } |
||
| 978 | |||
| 979 | // Fir AS 14.0+ omit output for folder, if there were no incoming or outgoing changes and no Fetch |
||
| 980 | if (Request::GetProtocolVersion() >= 14.0 && !$spa->HasNewSyncKey() && $changecount == 0 && empty($actiondata["fetchids"]) && $status == SYNC_STATUS_SUCCESS && |
||
| 981 | !$spa->HasConfirmationChanged() && ($newFolderStat === false || !$spa->IsExporterRunRequired($newFolderStat))) { |
||
| 982 | SLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync: No changes found for %s folder id '%s'. Omitting output.", $spa->GetContentClass(), $spa->GetFolderId())); |
||
| 983 | |||
| 984 | continue; |
||
| 985 | } |
||
| 986 | |||
| 987 | // if there are no other responses sent, we should end with a global status |
||
| 988 | if ($status == SYNC_STATUS_FOLDERHIERARCHYCHANGED && $this->startTagsSent === false) { |
||
| 989 | $this->sendStartTags(); |
||
| 990 | self::$encoder->startTag(SYNC_STATUS); |
||
| 991 | self::$encoder->content($status); |
||
| 992 | self::$encoder->endTag(); |
||
| 993 | self::$encoder->endTag(); // SYNC_SYNCHRONIZE |
||
| 994 | |||
| 995 | return true; |
||
| 996 | } |
||
| 997 | |||
| 998 | // there is something to send here, sync folder to output |
||
| 999 | $this->syncFolder($sc, $spa, $exporter, $changecount, $streamimporter, $status, $newFolderStat); |
||
| 1000 | |||
| 1001 | // reset status for the next folder |
||
| 1002 | $status = SYNC_STATUS_SUCCESS; |
||
| 1003 | } // END foreach collection |
||
| 1004 | |||
| 1005 | // SYNC_FOLDERS - only if the starttag was sent |
||
| 1006 | if ($this->startFolderTagSent) { |
||
| 1007 | self::$encoder->endTag(); |
||
| 1008 | } |
||
| 1009 | |||
| 1010 | // Check if there was any response - in case of an empty sync request, we shouldn't send an empty answer |
||
| 1011 | if (!$this->startTagsSent && $emptysync === true) { |
||
| 1012 | $this->sendStartTags(); |
||
| 1013 | self::$encoder->startTag(SYNC_STATUS); |
||
| 1014 | self::$encoder->content(SYNC_STATUS_SYNCREQUESTINCOMPLETE); |
||
| 1015 | self::$encoder->endTag(); |
||
| 1016 | } |
||
| 1017 | |||
| 1018 | // SYNC_SYNCHRONIZE - only if the starttag was sent |
||
| 1019 | if ($this->startTagsSent) { |
||
| 1020 | self::$encoder->endTag(); |
||
| 1021 | } |
||
| 1022 | |||
| 1023 | // final top announcement for a multi-folder sync |
||
| 1024 | if ($sc->GetCollectionCount() > 1) { |
||
| 1025 | self::$topCollector->AnnounceInformation($this->getMultiFolderInfoLine($sc->GetCollectionCount()), true); |
||
| 1026 | SLog::Write(LOGLEVEL_DEBUG, sprintf("HandleSync: Processed %d folders", $sc->GetCollectionCount())); |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | // update the waittime waited |
||
| 1030 | self::$waitTime = $sc->GetWaitedSeconds(); |
||
| 1031 | |||
| 1032 | return true; |
||
| 1033 | } |
||
| 1715 |