Further simplification and refactoring of device info handling.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 30 Mar 2010 04:47:56 +0000 (00:47 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 30 Mar 2010 04:50:05 +0000 (00:50 -0400)
Move some paramters around, simplify some method calls. Don't pass values
when we already have the means to get them etc...

Also, store device info even for devices that are not provisioned.

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/Options.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
framework/ActiveSync/lib/Horde/ActiveSync/State/Base.php
framework/ActiveSync/lib/Horde/ActiveSync/State/File.php

index 2da5150..3a919b9 100644 (file)
@@ -1531,26 +1531,39 @@ class Horde_ActiveSync
     }
 
     /**
-     * @param $cmd
-     * @param $devid
-     * @param $protocolversion
-     * @return unknown_type
+     * The heart of the server. Dispatch a request to the request object to
+     * handle.
+     *
+     * @param string $cmd    The command we are requesting.
+     * @param string $devId  The device id making the request.
+     *
+     * @return boolean
      */
     public function handleRequest($cmd, $devId)
     {
+        /* Check that this device is known */
+        $state = $this->_driver->getStateObject();
+        if (!empty($devId) && !$state->deviceExists($devId)) {
+            $get = $this->_request->getGetParams();
+            $device = new StdClass();
+            $device->userAgent = $this->_request->getHeader('User-Agent');
+            $device->deviceType = !empty($get['DeviceType']) ? $get['DeviceType'] : '';
+            $device->policykey = 0;
+            $state->setDeviceInfo($devId, $device);
+        }
+
+        /* Load the request handler to handle the request */
         $class = 'Horde_ActiveSync_Request_' . basename($cmd);
         $version = $this->getProtocolVersion();
         if (class_exists($class)) {
-            //@TODO: Remove version - get it in handle() since we pass self to it anyway
             $request = new $class($this->_driver,
                                   $this->_decoder,
                                   $this->_encoder,
                                   $this->_request,
-                                  $version,
-                                  $devId,
                                   $this->_provisioning);
             $request->setLogger($this->_logger);
-            return $request->handle($this);
+
+            return $request->handle($this, $devId);
         }
 
         // @TODO: Leave the following in place until all are refactored...then throw
index 65713ed..7987dc5 100644 (file)
@@ -66,9 +66,6 @@ abstract class Horde_ActiveSync_Request_Base
      */
     protected $_devId;
 
-    protected $_userAgent;
-    protected $_deviceType;
-
     /**
      * Used to track what error code to send back to PIM on failure
      *
@@ -95,7 +92,7 @@ abstract class Horde_ActiveSync_Request_Base
                                 Horde_ActiveSync_Wbxml_Decoder $decoder,
                                 Horde_ActiveSync_Wbxml_Encoder $encoder,
                                 Horde_Controller_Request_Http $request,
-                                $version, $devId, $provisioning)
+                                $provisioning)
     {
         /* Backend driver */
         $this->_driver = $driver;
@@ -107,22 +104,8 @@ abstract class Horde_ActiveSync_Request_Base
         /* The http request */
         $this->_request = $request;
 
-        /* Protocol Version */
-        $this->_version = $version;
-
-        /* Device Id */
-        $this->_devId = $devId;
-
         /* Provisioning support */
         $this->_provisioning = $provisioning;
-
-        /* Useragent and device identification */
-       $this->_userAgent = $request->getHeader('User-Agent');
-       $get = $request->getGetParams();
-       $this->_deviceType = $get['DeviceType'];
-
-        /* Logger */
-        $this->_logger;
     }
 
     /**
@@ -158,6 +141,10 @@ abstract class Horde_ActiveSync_Request_Base
      * @param string $version
      * @param string $devId
      */
-    abstract public function handle(Horde_ActiveSync $activeSync);
+    public function handle(Horde_ActiveSync $activeSync, $devId)
+    {
+        $this->_version = $activeSync->getProtocolVersion();
+        $this->_devId = $devId;
+    }
 
 }
\ No newline at end of file
index f333a87..62af78c 100644 (file)
@@ -21,8 +21,10 @@ class Horde_ActiveSync_Request_FolderSync extends Horde_ActiveSync_Request_Base
     const STATUS_KEYMISM = 9;
     const STATUS_PROTOERR = 10;
 
-    public function handle(Horde_ActiveSync $activeSync)
+    public function handle(Horde_ActiveSync $activeSync, $devId)
     {
+        parent::handle($activeSync, $devId);
+
         /* Be optimistic */
         $this->_statusCode = self::STATUS_SUCCESS;
         $this->_logger->info('[Horde_ActiveSync::handleFolderSync] Beginning FOLDERSYNC');
index 264374d..319a0ea 100644 (file)
@@ -26,8 +26,9 @@ class Horde_ActiveSync_Request_GetItemEstimate extends Horde_ActiveSync_Request_
      * @return boolean
      * @throws Horde_ActiveSync_Exception
      */
-    public function handle(Horde_ActiveSync $activeSync)
+    public function handle(Horde_ActiveSync $activeSync, $devId)
     {
+        parent::handle($activeSync, $devId);
         $this->_logger->info('[Horde_ActiveSync::handleFolderSync] Beginning GETITEMESTIMATE');
 
         /* Check policy */
index b28cea8..923a2d1 100644 (file)
@@ -14,7 +14,7 @@
  */
 class Horde_ActiveSync_Request_Options extends Horde_ActiveSync_Request_Base
 {
-    public function handle(Horde_ActiveSync $activeSync)
+    public function handle(Horde_ActiveSync $activeSync, $devId)
     {
         Horde_ActiveSync::activeSyncHeader();
         Horde_ActiveSync::versionHeader();
index 162f0c3..86b677a 100644 (file)
@@ -39,8 +39,10 @@ class Horde_ActiveSync_Request_Ping extends Horde_ActiveSync_Request_Base
      *
      * @return boolean
      */
-    public function handle(Horde_ActiveSync $activeSync)
+    public function handle(Horde_ActiveSync $activeSync, $devId)
     {
+        parent::handle($activeSync, $devId);
+
         // FIXME
         $timeout = 3;
         $this->_logger->info('Ping received for: ' . $this->_devId);
index 240f9df..cc1b270 100644 (file)
@@ -54,6 +54,8 @@ class Horde_ActiveSync_Request_Provision extends Horde_ActiveSync_Request_Base
      */
     public function handle(Horde_ActiveSync $activeSync)
     {
+        parent::handle($activeSync, $devId);
+
         /* Get the policy key if it was sent */
         $policykey = $activeSync->getPolicyKey();
 
@@ -149,11 +151,9 @@ class Horde_ActiveSync_Request_Provision extends Horde_ActiveSync_Request_Base
             if ($state->getPolicyKey($this->_devId) != $policykey) {
                 $policyStatus = self::STATUS_POLKEYMISM;
             } else {
+                /* Set the final key */
                 $policykey = $this->_driver->generatePolicyKey();
-                $info = array('policykey' => $policykey,
-                              'useragent' => $this->_userAgent,
-                              'devicetype' => $this->_deviceType);
-                $state->setDeviceInfo($this->_devId, $info);
+                $state->setPolicyKey($this->_devId, $policykey);
             }
         } elseif (empty($policykey)) {
             // This is phase2 - we need to set the intermediate key
index 2e7596c..8274326 100644 (file)
@@ -26,8 +26,9 @@ class Horde_ActiveSync_Request_Sync extends Horde_ActiveSync_Request_Base
      * @return boolean
      * @throws Horde_ActiveSync_Exception
      */
-    public function handle(Horde_ActiveSync $activeSync)
+    public function handle(Horde_ActiveSync $activeSync, $devId)
     {
+        parent::handle($activeSync, $devId);
         $this->_logger->info('[Horde_ActiveSync::handleSync] Handling SYNC command.');
 
         /* Check policy */
index 8a3be98..1be7157 100644 (file)
@@ -176,25 +176,53 @@ abstract class Horde_ActiveSync_State_Base
     abstract public function isConflict($stat, $type);
 
     /**
-     * Get the specified device's policy key.
+     * Obtain the current policy key, if it exists.
      *
-     * @param string $devId     The device id to get key for.
+     * @param string $devId     The device id to obtain policy key for.
      *
-     * @return integer  The policy key
+     * @return integer  The current policy key for this device, or 0 if none
+     *                  exists.
      */
     abstract public function getPolicyKey($devId);
 
     /**
-     * Set the policy key for the specified device id
+     * Save a new device policy key to storage.
      *
-     * @param string $devId     The device id
-     * @param integer $key      The policy key
-     *
-     * @return void
+     * @param string $devId  The device id
+     * @param integer $key   The new policy key
      */
     abstract public function setPolicyKey($devId, $key);
 
     /**
+     * Obtain the device object.
+     *
+     * @param string $devId
+     *
+     * @return StdClass
+     */
+    abstract public function getDeviceInfo($devId);
+
+    /**
+     * Check that a given device id is known to the server. This is regardless
+     * of Provisioning status.
+     *
+     * @param string $devId
+     *
+     * @return boolean
+     */
+    abstract public function deviceExists($devId);
+
+    /**
+     * Set new device info
+     *
+     * @param string $devId   The device id.
+     * @param StdClass $data  The device information
+     *
+     * @return boolean
+     */
+    abstract public function setDeviceInfo($devId, $data);
+
+    /**
      * Set the backend driver
      * (should really only be called by a backend object when passing this
      * object to client code)
index 211f85e..d792c05 100644 (file)
@@ -341,9 +341,11 @@ class Horde_ActiveSync_State_File extends Horde_ActiveSync_State_Base
     }
 
     /**
-     * Obtain the device info array.
+     * Obtain the device object.
      *
-     * @param <type> $devId
+     * @param string $devId
+     *
+     * @return StdClass
      */
     public function getDeviceInfo($devId)
     {
@@ -353,15 +355,23 @@ class Horde_ActiveSync_State_File extends Horde_ActiveSync_State_Base
             return unserialize(file_get_contents($file));
         }
 
-        return array('policykey' => 0,
-                     'rwstatus' => 0,
-                     'devicetype' => '',
-                     'useragent' => '');
+        /* Default structure */
+        $device = new StdClass();
+        $device->policykey = 0;
+        $device->rwstatus = 0; // ??
+        $device->deviceType = '';
+        $device->userAgent = '';
+
+        return $device;
     }
 
     /**
      * Set new device info
      *
+     * @param string $devId   The device id.
+     * @param StdClass $data  The device information
+     *
+     * @return boolean
      */
     public function setDeviceInfo($devId, $data)
     {
@@ -371,6 +381,19 @@ class Horde_ActiveSync_State_File extends Horde_ActiveSync_State_Base
     }
 
     /**
+     * Check that a given device id is known to the server. This is regardless
+     * of Provisioning status.
+     *
+     * @param string $devId
+     *
+     * @return boolean
+     */
+    public function deviceExists($devId)
+    {
+        return file_exists($this->_stateDir . '/' . $this->_backend->getUser() . '/info-' . $devId);
+    }
+
+    /**
      * Load a specific collection's ping state. Ping state must already have
      * been loaded.
      *
@@ -489,7 +512,7 @@ class Horde_ActiveSync_State_File extends Horde_ActiveSync_State_Base
     public function getPolicyKey($devId)
     {
         $info = $this->getDeviceInfo($devId);
-        return $info['policykey'];
+        return $info->policykey;
     }
 
     /**
@@ -501,7 +524,7 @@ class Horde_ActiveSync_State_File extends Horde_ActiveSync_State_Base
     public function setPolicyKey($devId, $key)
     {
         $info = $this->getDeviceInfo($devId);
-        $info['policykey'] = $key;
+        $info->policykey = $key;
         $this->setDeviceInfo($devId, $info);
     }