protected function _handlePost() { if (count($_GET) == 1) { $arrayKeys = array_keys($_GET); $requestParameters = $this->_decodeRequestParameters($arrayKeys[0]); } else { $requestParameters = $this->_getRequestParameters(); } if ($this->_logger instanceof Zend_Log) { $this->_logger->debug(__METHOD__ . '::' . __LINE__ . ' REQUEST ' . print_r($requestParameters, true)); } $userAgent = $this->_request->getServer('HTTP_USER_AGENT', $requestParameters['deviceType']); $policyKey = $this->_request->getServer('HTTP_X_MS_POLICYKEY'); if ($this->_logger instanceof Zend_Log) { $this->_logger->debug(__METHOD__ . '::' . __LINE__ . " Agent: {$userAgent} PolicyKey: {$policyKey} ASVersion: {$requestParameters['protocolVersion']} Command: {$requestParameters['command']}"); } $className = 'Syncope_Command_' . $requestParameters['command']; if (!class_exists($className)) { throw new Syncope_Exception_CommandNotFound('unsupported command ' . $requestParameters['command']); } // get user device $device = $this->_getUserDevice($this->_userId, $requestParameters['deviceId'], $requestParameters['deviceType'], $userAgent, $requestParameters['protocolVersion']); if ($this->_request->getServer('CONTENT_TYPE') == 'application/vnd.ms-sync.wbxml') { // decode wbxml request try { $decoder = new Wbxml_Decoder($this->_body); $requestBody = $decoder->decode(); if ($this->_logger instanceof Zend_Log) { $this->_logger->debug(__METHOD__ . '::' . __LINE__ . " xml request: " . $requestBody->saveXML()); } } catch (Wbxml_Exception_UnexpectedEndOfFile $e) { $requestBody = NULL; } } else { $requestBody = $this->_body; } try { $command = new $className($requestBody, $device, $policyKey); $command->handle(); if (PHP_SAPI !== 'cli') { header("MS-Server-ActiveSync: 8.3"); } $response = $command->getResponse(); } catch (Syncope_Exception_PolicyKeyMissing $sepkm) { if ($this->_logger instanceof Zend_Log) { $this->_logger->warn(__METHOD__ . '::' . __LINE__ . " X-MS-POLICYKEY missing (" . $_command . ')'); } header("HTTP/1.1 400 header X-MS-POLICYKEY not found"); return; } catch (Syncope_Exception_ProvisioningNeeded $sepn) { if ($this->_logger instanceof Zend_Log) { $this->_logger->info(__METHOD__ . '::' . __LINE__ . " provisioning needed"); } header("HTTP/1.1 449 Retry after sending a PROVISION command"); return; } catch (Exception $e) { if ($this->_logger instanceof Zend_Log) { $this->_logger->crit(__METHOD__ . '::' . __LINE__ . " unexpected exception occured: " . get_class($e)); } if ($this->_logger instanceof Zend_Log) { $this->_logger->crit(__METHOD__ . '::' . __LINE__ . " exception message: " . $e->getMessage()); } if ($this->_logger instanceof Zend_Log) { $this->_logger->info(__METHOD__ . '::' . __LINE__ . " " . $e->getTraceAsString()); } header("HTTP/1.1 500 Internal server error"); return; } if ($response instanceof DOMDocument) { if ($this->_logger instanceof Zend_Log) { $this->_logger->debug(__METHOD__ . '::' . __LINE__ . " xml response: " . $response->saveXML()); } $outputStream = fopen("php://temp", 'r+'); $encoder = new Wbxml_Encoder($outputStream, 'UTF-8', 3); $encoder->encode($response); header("Content-Type: application/vnd.ms-sync.wbxml"); rewind($outputStream); fpassthru($outputStream); } }
/** * test xml generation for IPhone * * birthday must have 12 hours added */ public function testAppendXmlIPhone() { $imp = new DOMImplementation(); $dtd = $imp->createDocumentType('AirSync', "-//AIRSYNC//DTD AirSync//EN", "http://www.microsoft.com/"); $testDom = $imp->createDocument('uri:AirSync', 'Sync', $dtd); $testDom->formatOutput = true; $testDom->encoding = 'utf-8'; $testDom->documentElement->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:Contacts', 'uri:Contacts'); $collections = $testDom->documentElement->appendChild($testDom->createElementNS('uri:AirSync', 'Collections')); $collection = $collections->appendChild($testDom->createElementNS('uri:AirSync', 'Collection')); $commands = $collection->appendChild($testDom->createElementNS('uri:AirSync', 'Commands')); $add = $commands->appendChild($testDom->createElementNS('uri:AirSync', 'Add')); $appData = $add->appendChild($testDom->createElementNS('uri:AirSync', 'ApplicationData')); $controller = new ActiveSync_Controller_Contacts($this->objects['deviceIPhone'], new Tinebase_DateTime(null, null, 'de_DE')); $controller->appendXML($appData, null, $this->objects['contact']->getId(), array()); // no offset and namespace === uri:Contacts $this->assertEquals('1975-01-02T03:00:00.000Z', @$testDom->getElementsByTagNameNS('uri:Contacts', 'Birthday')->item(0)->nodeValue, $testDom->saveXML()); #echo $testDom->saveXML(); // try to encode XML until we have wbxml tests $outputStream = fopen("php://temp", 'r+'); $encoder = new Wbxml_Encoder($outputStream, 'UTF-8', 3); $encoder->encode($testDom); #rewind($outputStream); #fpassthru($outputStream); }
/** * test xml generation for IPhone */ public function testAppendXml() { $imp = new DOMImplementation(); $dtd = $imp->createDocumentType('AirSync', "-//AIRSYNC//DTD AirSync//EN", "http://www.microsoft.com/"); $testDom = $imp->createDocument('uri:AirSync', 'Sync', $dtd); $testDom->formatOutput = true; $testDom->encoding = 'utf-8'; $collections = $testDom->documentElement->appendChild($testDom->createElementNS('uri:AirSync', 'Collections')); $collection = $collections->appendChild($testDom->createElementNS('uri:AirSync', 'Collection')); $commands = $collection->appendChild($testDom->createElementNS('uri:AirSync', 'Commands')); $add = $commands->appendChild($testDom->createElementNS('uri:AirSync', 'Add')); $appData = $add->appendChild($testDom->createElementNS('uri:AirSync', 'ApplicationData')); $controller = new ActiveSync_Controller_Calendar($this->objects['deviceIPhone'], new Tinebase_DateTime(null, null, 'de_DE')); $controller->appendXML($appData, null, $this->objects['event']->getId(), array()); // namespace === uri:Calendar $endTime = $this->objects['event']->dtend->format("Ymd\\THis") . 'Z'; $this->assertEquals($endTime, @$testDom->getElementsByTagNameNS('uri:Calendar', 'EndTime')->item(0)->nodeValue, $testDom->saveXML()); $this->assertEquals($this->objects['event']->uid, @$testDom->getElementsByTagNameNS('uri:Calendar', 'UID')->item(0)->nodeValue, $testDom->saveXML()); // try to encode XML until we have wbxml tests $outputStream = fopen("php://temp", 'r+'); $encoder = new Wbxml_Encoder($outputStream, 'UTF-8', 3); $encoder->encode($testDom); }
public function testCreateContact() { $personalContainer = Tinebase_Container::getInstance()->getPersonalContainer(Tinebase_Core::getUser(), 'Addressbook', Tinebase_Core::getUser(), Tinebase_Model_Grants::GRANT_EDIT)->getFirstRecord(); $this->testSyncOfContacts(); // lets add one contact $doc = new DOMDocument(); $doc->preserveWhiteSpace = false; $doc->loadXML('<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE AirSync PUBLIC "-//AIRSYNC//DTD AirSync//EN" "http://www.microsoft.com/"> <Sync xmlns="uri:AirSync" xmlns:Contacts="uri:Contacts" xmlns:AirSyncBase="uri:AirSyncBase"><Collections> <Collection> <Class>Contacts</Class><SyncKey>2</SyncKey><CollectionId>' . $personalContainer->getId() . '</CollectionId><DeletesAsMoves/><GetChanges/><WindowSize>100</WindowSize> <Options><AirSyncBase:BodyPreference><AirSyncBase:Type>1</AirSyncBase:Type><AirSyncBase:TruncationSize>5120</AirSyncBase:TruncationSize></AirSyncBase:BodyPreference><Conflict>1</Conflict></Options> <Commands><Add><ClientId>42</ClientId><ApplicationData><Contacts:FirstName>aaaadde</Contacts:FirstName><Contacts:LastName>aaaaade</Contacts:LastName></ApplicationData></Add></Commands> </Collection> </Collections></Sync>'); // decode to wbxml and back again to test the wbxml en-/decoder $xmlStream = fopen("php://temp", 'r+'); $encoder = new Wbxml_Encoder($xmlStream, 'UTF-8', 3); $encoder->encode($doc); rewind($xmlStream); $decoder = new Wbxml_Decoder($xmlStream); $doc = $decoder->decode(); #$doc->formatOutput = true; echo $doc->saveXML(); $sync = new Syncope_Command_Sync($doc, $this->_device, $this->_device->policykey); $sync->handle(); $syncDoc = $sync->getResponse(); #$syncDoc->formatOutput = true; echo $syncDoc->saveXML(); $xpath = new DomXPath($syncDoc); $xpath->registerNamespace('AirSync', 'uri:AirSync'); $nodes = $xpath->query('//AirSync:Sync/AirSync:Collections/AirSync:Collection/AirSync:Class'); $this->assertEquals(1, $nodes->length, $syncDoc->saveXML()); $this->assertEquals('Contacts', $nodes->item(0)->nodeValue, $syncDoc->saveXML()); $nodes = $xpath->query('//AirSync:Sync/AirSync:Collections/AirSync:Collection/AirSync:SyncKey'); $this->assertEquals(1, $nodes->length, $syncDoc->saveXML()); $this->assertEquals(3, $nodes->item(0)->nodeValue, $syncDoc->saveXML()); $nodes = $xpath->query('//AirSync:Sync/AirSync:Collections/AirSync:Collection/AirSync:Status'); $this->assertEquals(1, $nodes->length, $syncDoc->saveXML()); $this->assertEquals(Syncope_Command_Sync::STATUS_SUCCESS, $nodes->item(0)->nodeValue, $syncDoc->saveXML()); $nodes = $xpath->query('//AirSync:Sync/AirSync:Collections/AirSync:Collection/AirSync:Responses/AirSync:Add/AirSync:ServerId'); $this->assertEquals(1, $nodes->length, $syncDoc->saveXML()); $this->assertFalse(empty($nodes->item(0)->nodeValue), $syncDoc->saveXML()); $nodes = $xpath->query('//AirSync:Sync/AirSync:Collections/AirSync:Collection/AirSync:Responses/AirSync:Add/AirSync:Status'); $this->assertEquals(1, $nodes->length, $syncDoc->saveXML()); $this->assertEquals(Syncope_Command_Sync::STATUS_SUCCESS, $nodes->item(0)->nodeValue, $syncDoc->saveXML()); }
/** * test xml generation for sync to client */ public function testAppendXml() { $dom = $this->_getOutputDOMDocument(); $appData = $dom->getElementsByTagNameNS('uri:AirSync', 'ApplicationData')->item(0); $controller = $this->_getController($this->_getDevice(Syncope_Model_Device::TYPE_WEBOS)); $task = Tasks_TestCase::getTestRecord(); $task->description = "Hello\r\nTask\nLars"; $task = Tasks_Controller_Task::getInstance()->create($task); $this->objects['tasks']['appendxml'] = $task; $controller->appendXML($appData, null, $task, array()); #$dom->formatOutput = true; echo $dom->saveXML(); $dom->formatOutput = false; // namespace === uri:Calendar $dueDate = $task->due->format("Y-m-d\\TH:i:s") . '.000Z'; $this->assertEquals($dueDate, @$dom->getElementsByTagNameNS('uri:Tasks', 'DueDate')->item(0)->nodeValue, $dom->saveXML()); $this->assertEquals("Hello\r\nTask\r\nLars", @$dom->getElementsByTagNameNS('uri:AirSyncBase', 'Data')->item(0)->nodeValue, $dom->saveXML()); // try to encode XML until we have wbxml tests $outputStream = fopen("php://temp", 'r+'); $encoder = new Wbxml_Encoder($outputStream, 'UTF-8', 3); $encoder->encode($dom); #rewind($outputStream); #fpassthru($outputStream); }
/** * test xml generation for IPhone * * birthday must have 12 hours added */ public function testAppendXmlIPhone() { $imp = new DOMImplementation(); $dtd = $imp->createDocumentType('AirSync', "-//AIRSYNC//DTD AirSync//EN", "http://www.microsoft.com/"); $testDoc = $imp->createDocument('uri:AirSync', 'Sync', $dtd); $testDoc->formatOutput = true; $testDoc->encoding = 'utf-8'; $testDoc->documentElement->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:Contacts', 'uri:Contacts'); $collections = $testDoc->documentElement->appendChild($testDoc->createElementNS('uri:AirSync', 'Collections')); $collection = $collections->appendChild($testDoc->createElementNS('uri:AirSync', 'Collection')); $commands = $collection->appendChild($testDoc->createElementNS('uri:AirSync', 'Commands')); $add = $commands->appendChild($testDoc->createElementNS('uri:AirSync', 'Add')); $appData = $add->appendChild($testDoc->createElementNS('uri:AirSync', 'ApplicationData')); $device = $this->_deviceBackend->create(Syncope_Backend_DeviceTests::getTestDevice(Syncope_Model_Device::TYPE_IPHONE)); $dataController = Syncope_Data_Factory::factory(Syncope_Data_Factory::CLASS_CONTACTS, $device, new DateTime(null, new DateTimeZone('UTC'))); $dataController->appendXML($appData, array('collectionId' => 'addressbookFolderId'), 'contact1'); // no offset and namespace === uri:Contacts #$this->assertEquals('1975-01-02T03:00:00.000Z', @$testDoc->getElementsByTagNameNS('uri:Contacts', 'Birthday')->item(0)->nodeValue, $testDoc->saveXML()); #echo $testDoc->saveXML(); // try to encode XML until we have wbxml tests $outputStream = fopen("php://temp", 'r+'); $encoder = new Wbxml_Encoder($outputStream, 'UTF-8', 3); $encoder->encode($testDoc); #rewind($outputStream); #fpassthru($outputStream); }