make state a class member, pass the activesync server instance to the request
authorMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 26 Apr 2010 16:48:42 +0000 (12:48 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 26 Apr 2010 16:48:42 +0000 (12:48 -0400)
framework/ActiveSync/lib/Horde/ActiveSync.php
framework/ActiveSync/lib/Horde/ActiveSync/Request/Base.php
framework/ActiveSync/lib/Horde/ActiveSync/Request/FolderSync.php
framework/ActiveSync/lib/Horde/ActiveSync/Request/GetItemEstimate.php
framework/ActiveSync/lib/Horde/ActiveSync/Request/Ping.php
framework/ActiveSync/lib/Horde/ActiveSync/Request/Provision.php
framework/ActiveSync/lib/Horde/ActiveSync/Request/Sync.php

index 619a6f9..e0f083f 100644 (file)
@@ -1012,6 +1012,7 @@ class Horde_ActiveSync
                                   $this->_decoder,
                                   $this->_encoder,
                                   $this->_request,
+                                  $this,
                                   $this->_provisioning);
             $request->setLogger($this->_logger);
 
index 06f2245..9d25dd2 100644 (file)
@@ -73,6 +73,25 @@ abstract class Horde_ActiveSync_Request_Base
      */
     protected $_statusCode = 0;
 
+    /**
+     * State object
+     *
+     * @var Horde_ActiveSync_State_Base
+     */
+    protected $_state;
+
+    /**
+     * ActiveSync server
+     * 
+     * @var Horde_ActiveSync 
+     */
+    protected $_activeSync;
+
+    /**
+     * Logger
+     *
+     * @var Horde_Log_Logger
+     */
     protected $_logger;
 
     /**
@@ -90,11 +109,15 @@ abstract class Horde_ActiveSync_Request_Base
                                 Horde_ActiveSync_Wbxml_Decoder $decoder,
                                 Horde_ActiveSync_Wbxml_Encoder $encoder,
                                 Horde_Controller_Request_Http $request,
+                                Horde_ActiveSync $as,
                                 $provisioning)
     {
         /* Backend driver */
         $this->_driver = $driver;
 
+        /* server */
+        $this->_activeSync = $as;
+
         /* Wbxml handlers */
         $this->_encoder = $encoder;
         $this->_decoder = $decoder;
@@ -115,6 +138,12 @@ abstract class Horde_ActiveSync_Request_Base
      */
     public function checkPolicyKey($sentKey)
     {
+        /* Android devices don't support provisioning, but also send a policykey
+         * header - which is against the specification. Check the user agent
+         * for Android (maybe need version sniffing in the future) and set the
+         * policykey to null for those devices. */
+
+        
         /* Don't attempt if we don't care */
         if ($this->_provisioning !== false) {
             $state = $this->_driver->getStateObject();
index 6d911d3..4494e69 100644 (file)
@@ -70,20 +70,20 @@ class Horde_ActiveSync_Request_FolderSync extends Horde_ActiveSync_Request_Base
         $this->_logger->debug('[Horde_ActiveSync::handleFolderSync] syncKey: ' . $synckey);
 
         /* Initialize state engine */
-        $state = &$this->_driver->getStateObject(array('synckey' => $synckey));
-        $state->getDeviceInfo($devId);
+        $this->_state = &$this->_driver->getStateObject(array('synckey' => $synckey));
+        $this->_state->getDeviceInfo($devId);
         try {
             /* Get folders that we know about already */
-            $state->loadState($synckey, 'foldersync');
+            $this->_state->loadState($synckey, 'foldersync');
 
             /* Get new synckey to send back */
-            $newsynckey = $state->getNewSyncKey($synckey);
+            $newsynckey = $this->_state->getNewSyncKey($synckey);
         } catch (Horde_ActiveSync_Exception $e) {
             $this->_statusCode = self::STATUS_KEYMISM;
             $this->_handleError();
             exit;
         }
-        $seenfolders = $state->getKnownFolders();
+        $seenfolders = $this->_state->getKnownFolders();
         $this->_logger->debug('[Horde_ActiveSync::handleFolderSync] newSyncKey: ' . $newsynckey);
 
         /* Deal with folder hierarchy changes */
@@ -108,7 +108,7 @@ class Horde_ActiveSync_Request_FolderSync extends Horde_ActiveSync_Request_Base
 
             /* Configure importer with last state */
             $importer = $this->_driver->getImporter();
-            $importer->init($state, false);
+            $importer->init($this->_state, false);
 
             while (1) {
                 $folder = new Horde_ActiveSync_Message_Folder(array('logger' => $this->_logger));
@@ -153,7 +153,7 @@ class Horde_ActiveSync_Request_FolderSync extends Horde_ActiveSync_Request_Base
         // count before sending the actual data.
         $exporter = new Horde_ActiveSync_Connector_Exporter();
         $sync = $this->_driver->GetSyncObject();
-        $sync->init($state, $exporter, array('synckey' => $synckey));
+        $sync->init($this->_state, $exporter, array('synckey' => $synckey));
 
         /* Perform the actual sync operation */
         while(is_array($sync->syncronize()));
@@ -203,8 +203,8 @@ class Horde_ActiveSync_Request_FolderSync extends Horde_ActiveSync_Request_Base
         $this->_encoder->endTag();
 
         /* Save the state as well as the known folder cache */
-        $state->setNewSyncKey($newsynckey);
-        $state->save();
+        $this->_state->setNewSyncKey($newsynckey);
+        $this->_state->save();
 
         return true;
     }
index 871fa01..2ee07ff 100644 (file)
@@ -115,9 +115,9 @@ class Horde_ActiveSync_Request_GetItemEstimate extends Horde_ActiveSync_Request_
 
             /* compatibility mode - get id from state */
             if (!isset($collectionid)) {
-                $state = &$this->_driver->getStateObject();
-                $state->getDeviceInfo($devId);
-                $collectionid = $state>getFolderData($this->_devid, $collection['class']);
+                $this->_state = &$this->_driver->getStateObject();
+                $this->_state->getDeviceInfo($devId);
+                $collectionid = $this->_state>getFolderData($this->_devid, $collection['class']);
             }
             $collection['id'] = $collectionid;
             $status[$collection['id']] = $cStatus;
@@ -143,10 +143,10 @@ class Horde_ActiveSync_Request_GetItemEstimate extends Horde_ActiveSync_Request_
             $this->_encoder->endTag();
             $this->_encoder->startTag(self::ESTIMATE);
 
-            $state = $this->_driver->getStateObject($collection);
-            $state->loadState($collection['synckey']);
+            $this->_state = $this->_driver->getStateObject($collection);
+            $this->_state->loadState($collection['synckey']);
             $sync = $this->_driver->getSyncObject();
-            $sync->init($state, null, $collection);
+            $sync->init($this->_state, null, $collection);
 
             $this->_encoder->content($sync->GetChangeCount());
             $this->_encoder->endTag();
index 7e58688..e9d4b7c 100644 (file)
@@ -37,7 +37,6 @@ class Horde_ActiveSync_Request_Ping extends Horde_ActiveSync_Request_Base
     const FOLDERTYPE =  'Ping:FolderType';
 
     protected $_ping_settings;
-    protected $_state;
 
     protected function _checkHeartbeat($lifetime)
     {
index 12fde60..6444a31 100644 (file)
@@ -69,8 +69,8 @@ class Horde_ActiveSync_Request_Provision extends Horde_ActiveSync_Request_Base
         }
 
         /* Get state object */
-        $state = $this->_driver->getStateObject();
-        $state->getDeviceInfo($devId);
+        $this->_state = $this->_driver->getStateObject();
+        $this->_state->getDeviceInfo($devId);
 
         /* Handle android remote wipe */
         if ($this->_decoder->getElementStartTag(Horde_ActiveSync::PROVISION_REMOTEWIPE)) {
@@ -83,7 +83,7 @@ class Horde_ActiveSync_Request_Provision extends Horde_ActiveSync_Request_Base
                 return $this->_globalError(self::STATUS_PROTERROR);
             }
             if ($status == self::STATUS_CLIENT_SUCCESS) {
-                $state->setDeviceRWStatus($this->_devId, Horde_ActiveSync::RWSTATUS_WIPED);
+                $this->_state->setDeviceRWStatus($this->_devId, Horde_ActiveSync::RWSTATUS_WIPED);
             }
 
             /* Need to send *something* in the policytype field even if wiping */
@@ -146,7 +146,7 @@ class Horde_ActiveSync_Request_Provision extends Horde_ActiveSync_Request_Base
                     return $this->_globalError(self::STATUS_PROTERROR);
                 }
                 if ($status == self::STATUS_CLIENT_SUCCESS) {
-                    $state->setDeviceRWStatus($this->_devId, Horde_ActiveSync::RWSTATUS_WIPED);
+                    $this->_state->setDeviceRWStatus($this->_devId, Horde_ActiveSync::RWSTATUS_WIPED);
                 }
             }
         }
@@ -162,17 +162,17 @@ class Horde_ActiveSync_Request_Provision extends Horde_ActiveSync_Request_Base
         send it to the client. */
         if (!$phase2) {
             /* Verify intermediate key */
-            if ($state->getPolicyKey($this->_devId) != $policykey) {
+            if ($this->_state->getPolicyKey($this->_devId) != $policykey) {
                 $policyStatus = self::STATUS_POLKEYMISM;
             } else {
                 /* Set the final key */
-                $policykey = $state->generatePolicyKey();
-                $state->setPolicyKey($this->_devId, $policykey);
+                $policykey = $this->_state->generatePolicyKey();
+                $this->_state->setPolicyKey($this->_devId, $policykey);
             }
         } elseif (empty($policykey)) {
             // This is phase2 - we need to set the intermediate key
-            $policykey = $state->generatePolicyKey();
-            $state->setPolicyKey($this->_devId, $policykey);
+            $policykey = $this->_state->generatePolicyKey();
+            $this->_state->setPolicyKey($this->_devId, $policykey);
         }
 
         $this->_encoder->startTag(Horde_ActiveSync::PROVISION_PROVISION);
@@ -181,10 +181,10 @@ class Horde_ActiveSync_Request_Provision extends Horde_ActiveSync_Request_Base
         $this->_encoder->endTag();
 
         /* Wipe data if status is pending or wiped */
-        $rwstatus = $state->getDeviceRWStatus($this->_devId);
+        $rwstatus = $this->_state->getDeviceRWStatus($this->_devId);
         if ($rwstatus == Horde_ActiveSync::RWSTATUS_PENDING || $rwstatus == Horde_ActiveSync::RWSTATUS_WIPED) {
             $this->_encoder->startTag(Horde_ActiveSync::PROVISION_REMOTEWIPE, false, true);
-            $state->setDeviceRWStatus($this->_devId, Horde_ActiveSync::RWSTATUS_WIPED);
+            $this->_state->setDeviceRWStatus($this->_devId, Horde_ActiveSync::RWSTATUS_WIPED);
         } else {
             $this->_encoder->startTag(Horde_ActiveSync::PROVISION_POLICIES);
             $this->_encoder->startTag(Horde_ActiveSync::PROVISION_POLICY);
index 4842c9d..80832d9 100644 (file)
@@ -184,18 +184,18 @@ class Horde_ActiveSync_Request_Sync extends Horde_ActiveSync_Request_Base
 
             if ($this->_statusCode == self::STATUS_SUCCESS) {
                 /* Initialize the state */
-                $state = &$this->_driver->getStateObject($collection);
-                $device = $state->getDeviceInfo($devId);
+                $this->_state = &$this->_driver->getStateObject($collection);
+                $device = $this->_state->getDeviceInfo($devId);
                 if (!empty($collection['supported'])) {
                     /* Initial sync and we have SUPPORTED data - save it */
                     if (empty($device->supported)) {
                         $device->supported = array();
                     }
                     $device->supported[$collection['class']] = $collection['supported'];
-                    $state->setDeviceInfo($devId, $device);
+                    $this->_state->setDeviceInfo($devId, $device);
                 }
                 try {
-                    $state->loadState($collection['synckey'], 'sync');
+                    $this->_state->loadState($collection['synckey'], 'sync');
                 } catch (Horde_ActiveSync_Exception $e) {
                     $this->_statusCode = self::STATUS_KEYMISM;
                     $this->_handleError($collection);
@@ -204,7 +204,7 @@ class Horde_ActiveSync_Request_Sync extends Horde_ActiveSync_Request_Base
 
                 /* compatibility mode - get folderid from the state directory */
                 if (!isset($collection['id'])) {
-                    $collection['id'] = $state->getFolderData($this->_devId, $collection['class']);
+                    $collection['id'] = $this->_state->getFolderData($this->_devId, $collection['class']);
                 }
 
                 /* compatibility mode - set default conflict behavior if no
@@ -217,7 +217,7 @@ class Horde_ActiveSync_Request_Sync extends Horde_ActiveSync_Request_Base
             if ($this->_decoder->getElementStartTag(Horde_ActiveSync::SYNC_COMMANDS)) {
                 /* Configure importer with last state */
                 $importer = $this->_driver->getImporter();
-                $importer->init($state, $collection['id'], $collection['conflict']);
+                $importer->init($this->_state, $collection['id'], $collection['conflict']);
                 $nchanges = 0;
                 while (1) {
                     // MODIFY or REMOVE or ADD or FETCH
@@ -376,7 +376,7 @@ class Horde_ActiveSync_Request_Sync extends Horde_ActiveSync_Request_Base
                 isset($collection['getchanges']) ||
                 $collection['synckey'] == '0') {
                 try {
-                    $collection['newsynckey'] = $state->getNewSyncKey($collection['synckey']);
+                    $collection['newsynckey'] = $this->_state->getNewSyncKey($collection['synckey']);
                 } catch (Horde_ActiveSync_Exception $e) {
                     $this->_statusCode = self::STATUS_KEYMISM;
                 }
@@ -450,7 +450,7 @@ class Horde_ActiveSync_Request_Sync extends Horde_ActiveSync_Request_Base
                 $filtertype = isset($collection['filtertype']) ? $collection['filtertype'] : false;
                 $exporter = new Horde_ActiveSync_Connector_Exporter($this->_encoder, $collection['class']);
                 $sync = $this->_driver->getSyncObject();
-                $sync->init($state, $exporter, $collection);
+                $sync->init($this->_state, $exporter, $collection);
                 $changecount = $sync->getChangeCount();
                 if (!empty($collection['windowsize']) && $changecount > $collection['windowsize']) {
                     $this->_encoder->startTag(Horde_ActiveSync::SYNC_MOREAVAILABLE, false, true);
@@ -481,8 +481,8 @@ class Horde_ActiveSync_Request_Sync extends Horde_ActiveSync_Request_Base
             /* Save the sync state for the next time */
             if (isset($collection['newsynckey'])) {
                 if (!empty($sync) || !empty($importer) || !empty($exporter) || $collection['synckey'] == 0)  {
-                    $state->setNewSyncKey($collection['newsynckey']);
-                    $state->save();
+                    $this->_state->setNewSyncKey($collection['newsynckey']);
+                    $this->_state->save();
                 } else {
                     $this->_logger->err(sprintf('[%s] Error saving %s - no state information available.', $this->_devId, $collection['newsynckey']));
                 }
@@ -516,8 +516,8 @@ class Horde_ActiveSync_Request_Sync extends Horde_ActiveSync_Request_Base
 
             $collection['newsynckey'] = Horde_ActiveSync_State_Base::getNewSyncKey(($this->_statusCode == self::STATUS_KEYMISM) ? 0 : $collection['synckey']);
             if ($collection['synckey'] != 0) {
-                $state = &$this->_driver->getStateObject($collection);
-                $state->removeState($collection['synckey']);
+                $this->_state = &$this->_driver->getStateObject($collection);
+                $this->_state->removeState($collection['synckey']);
             }
         }