/** * (non-PHPdoc) * @see ActiveSync_Frontend_Abstract::toSyncrotonModel() */ public function toSyncrotonModel($entry, array $options = array()) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " email data " . print_r($entry->toArray(), true)); } if (!$this->_isMemoryLeft($entry->size)) { throw new Syncroton_Exception_MemoryExhausted('not enough memory left for: ' . $entry->getId() . ' Needed: ' . $entry->size); } $syncrotonEmail = new Syncroton_Model_Email(); foreach ($this->_mapping as $syncrotonProperty => $tine20Property) { if (empty($entry->{$tine20Property}) && $entry->{$tine20Property} != '0' || count($entry->{$tine20Property}) === 0) { continue; } switch ($tine20Property) { case 'from_email': $syncrotonEmail->{$syncrotonProperty} = $this->_createEmailAddress($entry->from_name, $entry->from_email); break; case 'to': case 'cc': $syncrotonEmail->{$syncrotonProperty} = implode(', ', $entry->{$tine20Property}); break; default: $syncrotonEmail->{$syncrotonProperty} = $entry->{$tine20Property}; break; } } $syncrotonEmail->body = $this->_getSyncrotonBody($entry, $options); if ($syncrotonEmail->body->type < 4) { $syncrotonEmail->nativeBodyType = $syncrotonEmail->body->type; } if ($syncrotonEmail->body->type == Syncroton_Command_Sync::BODY_TYPE_MIME) { $syncrotonEmail->messageClass = 'IPM.Note.SMIME'; } else { $syncrotonEmail->messageClass = 'IPM.Note'; } $syncrotonEmail->contentClass = 'urn:content-classes:message'; // read flag $syncrotonEmail->read = in_array(Zend_Mail_Storage::FLAG_SEEN, $entry->flags) ? 1 : 0; if (in_array(Zend_Mail_Storage::FLAG_ANSWERED, $entry->flags)) { $syncrotonEmail->lastVerbExecuted = Syncroton_Model_Email::LASTVERB_REPLYTOSENDER; $syncrotonEmail->lastVerbExecutionTime = new DateTime('now', new DateTimeZone('utc')); #} elseif (in_array('\Forwarded', $entry->flags)) { # $syncrotonEmail->lastVerbExecuted = Syncroton_Model_Email::LASTVERB_FORWARD; # $syncrotonEmail->lastVerbExecutionTime = new DateTime('now', new DateTimeZone('utc')); } $syncrotonEmail->flag = in_array('\\Flagged', $entry->flags) ? new Syncroton_Model_EmailFlag(array('status' => Syncroton_Model_EmailFlag::STATUS_ACTIVE, 'flagType' => 'FollowUp', 'reminderSet' => 0, 'startDate' => Tinebase_DateTime::now(), 'utcStartDate' => Tinebase_DateTime::now(), 'dueDate' => Tinebase_DateTime::now()->addWeek(1), 'utcDueDate' => Tinebase_DateTime::now()->addWeek(1))) : new Syncroton_Model_EmailFlag(array('status' => Syncroton_Model_EmailFlag::STATUS_CLEARED)); // attachments? if ($entry->has_attachment == true) { $syncrotonAttachments = array(); $attachments = $this->_contentController->getAttachments($entry); if (count($attachments) > 0) { foreach ($attachments as $attachment) { $syncrotonAttachment = new Syncroton_Model_EmailAttachment(array('displayName' => trim($attachment['filename']), 'fileReference' => $entry->getId() . ActiveSync_Frontend_Abstract::LONGID_DELIMITER . $attachment['partId'], 'method' => 1, 'estimatedDataSize' => $attachment['size'])); $syncrotonAttachments[] = $syncrotonAttachment; } } $syncrotonEmail->attachments = $syncrotonAttachments; } #$syncrotonEmail->categories = array('Test'); $syncrotonEmail->conversationId = $entry->getId(); $syncrotonEmail->conversationIndex = ""; return $syncrotonEmail; }
/** * append email data to xml element * * @param DOMElement $_domParrent the parrent xml node * @param string $_folderId the local folder id * @param string $_serverId the local entry id * @param boolean $_withBody retrieve body of entry */ public function appendXML(DOMElement $_domParrent, $_collectionData, $_serverId) { Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . " append email " . $_serverId); $data = $_serverId instanceof Tinebase_Record_Abstract ? $_serverId : $this->_contentController->get($_serverId); $_domParrent->ownerDocument->documentElement->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:Email', 'uri:Email'); foreach ($this->_mapping as $key => $value) { if (!empty($data->{$value}) || $data->{$value} == '0') { $nodeContent = null; switch ($value) { case 'received': if ($data->{$value} instanceof DateTime) { $nodeContent = $data->{$value}->toString('Y-m-d\\TH:i:s') . '.000Z'; } break; case 'from_email': $nodeContent = $this->_createEmailAddress($data->from_name, $data->from_email); break; case 'to': case 'cc': $nodeContent = implode(', ', $data->{$value}); break; default: $nodeContent = $data->{$value}; break; } // skip empty elements if ($nodeContent === null || $nodeContent == '') { //Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . " Value for $key is empty. Skip element."); continue; } // strip off any non printable control characters if (!ctype_print($nodeContent)) { $nodeContent = $this->removeControlChars($nodeContent); } $node = $_domParrent->ownerDocument->createElementNS('uri:Email', $key); $node->appendChild($_domParrent->ownerDocument->createTextNode($nodeContent)); $_domParrent->appendChild($node); } } // read flag if (in_array('\\Seen', $data->flags)) { $_domParrent->appendChild(new DOMElement('Read', 1, 'uri:Email')); } else { $_domParrent->appendChild(new DOMElement('Read', 0, 'uri:Email')); } // attachments? if ($data->has_attachment == true) { $attachments = $this->_contentController->getAttachments($data); if (count($attachments) > 0) { $tagAttachments = $_domParrent->appendChild(new DOMElement('Attachments', null, 'uri:AirSyncBase')); foreach ($attachments as $attachment) { $tagAttachment = $tagAttachments->appendChild(new DOMElement('Attachment', null, 'uri:AirSyncBase')); $filenameNode = $tagAttachment->appendChild(new DOMElement('DisplayName', null, 'uri:AirSyncBase')); $filenameNode->appendChild(new DOMText($this->removeControlChars(trim($attachment['filename'])))); $tagAttachment->appendChild(new DOMElement('FileReference', $data->getId() . '-' . $attachment['partId'], 'uri:AirSyncBase')); $tagAttachment->appendChild(new DOMElement('Method', 1, 'uri:AirSyncBase')); $tagAttachment->appendChild(new DOMElement('EstimatedDataSize', $this->removeControlChars($attachment['size']), 'uri:AirSyncBase')); } } } // get truncation $truncateAt = null; if (isset($_collectionData['mimeSupport']) && $_collectionData['mimeSupport'] == 2 && (version_compare($this->_device->acsversion, '12.0', '<=') || isset($_collectionData['bodyPreferences'][4]))) { if (isset($_collectionData['bodyPreferences'][4]) && isset($_collectionData['bodyPreferences'][4]['truncationSize'])) { $truncateAt = $_collectionData['bodyPreferences'][4]['truncationSize']; } $airSyncBaseType = 4; } elseif (isset($_collectionData['bodyPreferences'][2])) { if (isset($_collectionData['bodyPreferences'][2]['truncationSize'])) { $truncateAt = $_collectionData['bodyPreferences'][2]['truncationSize']; } $airSyncBaseType = 2; } else { if (isset($_collectionData['bodyPreferences'][1]) && isset($_collectionData['bodyPreferences'][1]['truncationSize'])) { $truncateAt = $_collectionData['bodyPreferences'][1]['truncationSize']; } $airSyncBaseType = 1; } if (isset($_collectionData['mimeTruncation']) && $_collectionData['mimeTruncation'] < 8) { switch ($_collectionData['mimeTruncation']) { case Syncope_Command_Sync::TRUNCATE_ALL: $truncateAt = 0; break; case Syncope_Command_Sync::TRUNCATE_4096: $truncateAt = 4096; break; case Syncope_Command_Sync::TRUNCATE_5120: $truncateAt = 5120; break; case Syncope_Command_Sync::TRUNCATE_7168: $truncateAt = 7168; break; case Syncope_Command_Sync::TRUNCATE_10240: $truncateAt = 10240; break; case Syncope_Command_Sync::TRUNCATE_20480: $truncateAt = 20480; break; case Syncope_Command_Sync::TRUNCATE_51200: $truncateAt = 51200; break; case Syncope_Command_Sync::TRUNCATE_102400: $truncateAt = 102400; break; } } if ($airSyncBaseType == 4) { // getMessagePart will return Zend_Mime_Part $messageBody = $this->_contentController->getMessagePart($_serverId); $messageBody = stream_get_contents($messageBody->getRawStream()); if (version_compare($this->_device->acsversion, '12.0', '<')) { // if the email contains non 7bit ascii characters we can't transfer them via MIMEData xml and we need to fall back to plain text if (preg_match('/(?:[^\\x00-\\x7F])/', $messageBody)) { $airSyncBaseType = 1; $messageBody = $this->_contentController->getMessageBody($_serverId, null, Zend_Mime::TYPE_TEXT, NULL, true); } } } else { $messageBody = $this->_contentController->getMessageBody($_serverId, null, $airSyncBaseType == 2 ? Zend_Mime::TYPE_HTML : Zend_Mime::TYPE_TEXT, NULL, true); } if ($truncateAt !== null && strlen($messageBody) > $truncateAt) { $messageBody = substr($messageBody, 0, $truncateAt); $isTruncacted = 1; } else { $isTruncacted = 0; } if (strlen($messageBody) > 0) { // remove control chars $messageBody = $this->removeControlChars($messageBody); // strip out any non utf-8 characters $messageBody = @iconv('utf-8', 'utf-8//IGNORE', $messageBody); if (version_compare($this->_device->acsversion, '12.0', '>=')) { $body = $_domParrent->appendChild(new DOMElement('Body', null, 'uri:AirSyncBase')); $body->appendChild(new DOMElement('Type', $airSyncBaseType, 'uri:AirSyncBase')); $body->appendChild(new DOMElement('Truncated', $isTruncacted, 'uri:AirSyncBase')); $body->appendChild(new DOMElement('EstimatedDataSize', $data->size, 'uri:AirSyncBase')); $dataTag = $body->appendChild(new DOMElement('Data', null, 'uri:AirSyncBase')); $dataTag->appendChild(new DOMText($messageBody)); $_domParrent->appendChild(new DOMElement('NativeBodyType', $airSyncBaseType, 'uri:AirSyncBase')); } else { if ($airSyncBaseType == 4) { $_domParrent->appendChild(new DOMElement('MIMETruncated', $isTruncacted, 'uri:Email')); $body = $_domParrent->appendChild(new DOMElement('MIMEData', null, 'uri:Email')); $body->appendChild(new DOMText($messageBody)); } else { $_domParrent->appendChild(new DOMElement('BodyTruncated', $isTruncacted, 'uri:Email')); $body = $_domParrent->appendChild(new DOMElement('Body', null, 'uri:Email')); $body->appendChild(new DOMText($messageBody)); } } } if ($airSyncBaseType == 4) { $_domParrent->appendChild(new DOMElement('MessageClass', 'IPM.Note.SMIME', 'uri:Email')); } else { $_domParrent->appendChild(new DOMElement('MessageClass', 'IPM.Note', 'uri:Email')); } $_domParrent->appendChild(new DOMElement('ContentClass', 'urn:content-classes:message', 'uri:Email')); return; /* foreach($message->getHeaders() as $headerName => $headerValue) { switch($headerName) { case 'importance': switch (strtolower($headerValue)) { case 'low': $_domParrent->appendChild($_xmlDocument->createElementNS('uri:Email', 'Importance', 0)); break; case 'high': $_domParrent->appendChild($_xmlDocument->createElementNS('uri:Email', 'Importance', 2)); break; } break; } } */ }