Example #1
0
 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);
     }
 }
Example #2
0
 /**
  * decodes the tags 
  *
  * @return DOMDocument the decoded xml
  */
 public function decode()
 {
     $openTags = NULL;
     $node = NULL;
     $this->_codePage = $this->_dtd->getCurrentCodePage();
     while (!feof($this->_stream)) {
         $byte = $this->_getByte();
         switch ($byte) {
             case Wbxml_Abstract::END:
                 $node = $node->parentNode;
                 $openTags--;
                 break;
             case Wbxml_Abstract::OPAQUE:
                 $length = $this->_getMultibyteUInt();
                 if ($length > 0) {
                     $opaque = $this->_getOpaque($length);
                     try {
                         // let see if we can decode it. maybe the opaque data is wbxml encoded content
                         $opaqueDataStream = fopen("php://temp", 'r+');
                         fputs($opaqueDataStream, $opaque);
                         rewind($opaqueDataStream);
                         $opaqueContentDecoder = new Wbxml_Decoder($opaqueDataStream);
                         $dom = $opaqueContentDecoder->decode();
                         fclose($opaqueDataStream);
                         foreach ($dom->childNodes as $newNode) {
                             if ($newNode instanceof DOMElement) {
                                 $newNode = $this->_dom->importNode($newNode, true);
                                 $node->appendChild($newNode);
                             }
                         }
                     } catch (Exception $e) {
                         // if not, just treat it as a string
                         $node->appendChild($this->_dom->createTextNode($opaque));
                     }
                 }
                 break;
             case Wbxml_Abstract::STR_I:
                 $string = $this->_getTerminatedString();
                 $node->appendChild($this->_dom->createTextNode($string));
                 break;
             case Wbxml_Abstract::SWITCH_PAGE:
                 $page = $this->_getByte();
                 $this->_codePage = $this->_dtd->switchCodePage($page);
                 #echo "switched to codepage $page\n";
                 break;
             default:
                 $tagHasAttributes = ($byte & 0x80) != 0;
                 $tagHasContent = ($byte & 0x40) != 0;
                 // get rid of bit 7+8
                 $tagHexCode = $byte & 0x3f;
                 $tag = $this->_codePage->getTag($tagHexCode);
                 $nameSpace = $this->_codePage->getNameSpace();
                 $codePageName = $this->_codePage->getCodePageName();
                 #echo "Tag: $nameSpace:$tag\n";
                 if ($node === NULL) {
                     // create the domdocument
                     $node = $this->_createDomDocument($nameSpace, $tag);
                     $newNode = $node->documentElement;
                 } else {
                     if (!$this->_dom->isDefaultNamespace($nameSpace)) {
                         $this->_dom->documentElement->setAttribute('xmlns:' . $codePageName, $nameSpace);
                     }
                     $newNode = $node->appendChild($this->_dom->createElementNS('uri:' . $codePageName, $tag));
                 }
                 if ($tagHasAttributes) {
                     $attributes = $this->_getAttributes();
                 }
                 if ($tagHasContent == true) {
                     $node = $newNode;
                     $openTags++;
                 }
                 break;
         }
     }
     return $this->_dom;
 }
Example #3
0
 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());
 }