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