/** * Imports a message * * @param SyncParameters $spa SyncParameters object * @param array $actiondata Actiondata array * @param integer $todo WBXML flag indicating how message should be imported. * Valid values: SYNC_ADD, SYNC_MODIFY, SYNC_REMOVE * @param SyncObject $message SyncObject message to be imported * @param string $clientid Client message identifier * @param string $serverid Server message identifier * @param string $foldertype On sms sync, this says "SMS", else false * @param integer $messageCount Counter of already imported messages * * @access private * @throws StatusException in case the importer is not available * @return - Message related status are returned in the actiondata. */ private function importMessage($spa, &$actiondata, $todo, $message, $clientid, $serverid, $foldertype, $messageCount) { // the importer needs to be available! if ($this->importer == false) { throw StatusException("Sync->importMessage(): importer not available", SYNC_STATUS_SERVERERROR); } // mark this state as used, e.g. for HeartBeat self::$deviceManager->SetHeartbeatStateIntegrity($spa->GetFolderId(), $spa->GetUuid(), $spa->GetUuidCounter()); // Detect incoming loop // messages which were created/removed before will not have the same action executed again // if a message is edited we perform this action "again", as the message could have been changed on the mobile in the meantime $ignoreMessage = false; if ($actiondata["failstate"]) { // message was ADDED before, do NOT add it again if ($todo == SYNC_ADD && isset($actiondata["failstate"]["clientids"][$clientid])) { $ignoreMessage = true; // make sure no messages are sent back self::$deviceManager->SetWindowSize($spa->GetFolderId(), 0); $actiondata["clientids"][$clientid] = $actiondata["failstate"]["clientids"][$clientid]; $actiondata["statusids"][$clientid] = $actiondata["failstate"]["statusids"][$clientid]; ZLog::Write(LOGLEVEL_WARN, sprintf("Mobile loop detected! Incoming new message '%s' was created on the server before. Replying with known new server id: %s", $clientid, $actiondata["clientids"][$clientid])); } // message was REMOVED before, do NOT attemp to remove it again if ($todo == SYNC_REMOVE && isset($actiondata["failstate"]["removeids"][$serverid])) { $ignoreMessage = true; // make sure no messages are sent back self::$deviceManager->SetWindowSize($spa->GetFolderId(), 0); $actiondata["removeids"][$serverid] = $actiondata["failstate"]["removeids"][$serverid]; $actiondata["statusids"][$serverid] = $actiondata["failstate"]["statusids"][$serverid]; ZLog::Write(LOGLEVEL_WARN, sprintf("Mobile loop detected! Message '%s' was deleted by the mobile before. Replying with known status: %s", $clientid, $actiondata["statusids"][$serverid])); } } if (!$ignoreMessage) { switch ($todo) { case SYNC_MODIFY: self::$topCollector->AnnounceInformation(sprintf("Saving modified message %d", $messageCount)); try { $actiondata["modifyids"][] = $serverid; // ignore sms messages if ($foldertype == "SMS" || stripos($serverid, self::ZPUSHIGNORESMS) !== false) { ZLog::Write(LOGLEVEL_DEBUG, "SMS sync are not supported. Ignoring message."); // TODO we should update the SMS $actiondata["statusids"][$serverid] = SYNC_STATUS_SUCCESS; } else { if (!$message instanceof SyncObject || !$message->Check(true)) { $actiondata["statusids"][$serverid] = SYNC_STATUS_CLIENTSERVERCONVERSATIONERROR; } else { if (isset($message->read)) { // Currently, 'read' is only sent by the PDA when it is ONLY setting the read flag. $this->importer->ImportMessageReadFlag($serverid, $message->read); } elseif (!isset($message->flag)) { $this->importer->ImportMessageChange($serverid, $message); } // email todoflags - some devices send todos flags together with read flags, // so they have to be handled separately if (isset($message->flag)) { $this->importer->ImportMessageChange($serverid, $message); } $actiondata["statusids"][$serverid] = SYNC_STATUS_SUCCESS; } } } catch (StatusException $stex) { $actiondata["statusids"][$serverid] = $stex->getCode(); } break; case SYNC_ADD: self::$topCollector->AnnounceInformation(sprintf("Creating new message from mobile %d", $messageCount)); try { // ignore sms messages if ($foldertype == "SMS") { ZLog::Write(LOGLEVEL_DEBUG, "SMS sync are not supported. Ignoring message."); // TODO we should create the SMS // return a fake serverid which we can identify later $actiondata["clientids"][$clientid] = self::ZPUSHIGNORESMS . $clientid; $actiondata["statusids"][$clientid] = SYNC_STATUS_SUCCESS; } else { if (!$message instanceof SyncObject || !$message->Check(true)) { $actiondata["clientids"][$clientid] = false; $actiondata["statusids"][$clientid] = SYNC_STATUS_CLIENTSERVERCONVERSATIONERROR; } else { $actiondata["clientids"][$clientid] = false; $actiondata["clientids"][$clientid] = $this->importer->ImportMessageChange(false, $message); $actiondata["statusids"][$clientid] = SYNC_STATUS_SUCCESS; } } } catch (StatusException $stex) { $actiondata["statusids"][$clientid] = $stex->getCode(); } break; case SYNC_REMOVE: self::$topCollector->AnnounceInformation(sprintf("Deleting message removed on mobile %d", $messageCount)); try { $actiondata["removeids"][] = $serverid; // ignore sms messages if ($foldertype == "SMS" || stripos($serverid, self::ZPUSHIGNORESMS) !== false) { ZLog::Write(LOGLEVEL_DEBUG, "SMS sync are not supported. Ignoring message."); // TODO we should delete the SMS $actiondata["statusids"][$serverid] = SYNC_STATUS_SUCCESS; } else { // if message deletions are to be moved, move them if ($spa->GetDeletesAsMoves()) { $folderid = self::$backend->GetWasteBasket(); if ($folderid) { $this->importer->ImportMessageMove($serverid, $folderid); $actiondata["statusids"][$serverid] = SYNC_STATUS_SUCCESS; break; } else { ZLog::Write(LOGLEVEL_WARN, "Message should be moved to WasteBasket, but the Backend did not return a destination ID. Message is hard deleted now!"); } } $this->importer->ImportMessageDeletion($serverid); $actiondata["statusids"][$serverid] = SYNC_STATUS_SUCCESS; } } catch (StatusException $stex) { $actiondata["statusids"][$serverid] = $stex->getCode(); } break; } ZLog::Write(LOGLEVEL_DEBUG, "Sync->importMessage(): message imported"); } }