From: Michael J. Rubinsky Date: Tue, 30 Mar 2010 04:47:56 +0000 (-0400) Subject: Further simplification and refactoring of device info handling. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=531448f4f685b4ee4e09e2638851aab614912e0e;p=horde.git Further simplification and refactoring of device info handling. 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. --- diff --git a/framework/ActiveSync/lib/Horde/ActiveSync.php b/framework/ActiveSync/lib/Horde/ActiveSync.php index 2da5150ad..3a919b95d 100644 --- a/framework/ActiveSync/lib/Horde/ActiveSync.php +++ b/framework/ActiveSync/lib/Horde/ActiveSync.php @@ -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 diff --git a/framework/ActiveSync/lib/Horde/ActiveSync/Request/Base.php b/framework/ActiveSync/lib/Horde/ActiveSync/Request/Base.php index 65713ed47..7987dc54d 100644 --- a/framework/ActiveSync/lib/Horde/ActiveSync/Request/Base.php +++ b/framework/ActiveSync/lib/Horde/ActiveSync/Request/Base.php @@ -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 diff --git a/framework/ActiveSync/lib/Horde/ActiveSync/Request/FolderSync.php b/framework/ActiveSync/lib/Horde/ActiveSync/Request/FolderSync.php index f333a878b..62af78cbf 100644 --- a/framework/ActiveSync/lib/Horde/ActiveSync/Request/FolderSync.php +++ b/framework/ActiveSync/lib/Horde/ActiveSync/Request/FolderSync.php @@ -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'); diff --git a/framework/ActiveSync/lib/Horde/ActiveSync/Request/GetItemEstimate.php b/framework/ActiveSync/lib/Horde/ActiveSync/Request/GetItemEstimate.php index 264374d80..319a0ea7d 100644 --- a/framework/ActiveSync/lib/Horde/ActiveSync/Request/GetItemEstimate.php +++ b/framework/ActiveSync/lib/Horde/ActiveSync/Request/GetItemEstimate.php @@ -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 */ diff --git a/framework/ActiveSync/lib/Horde/ActiveSync/Request/Options.php b/framework/ActiveSync/lib/Horde/ActiveSync/Request/Options.php index b28cea8f6..923a2d1a3 100644 --- a/framework/ActiveSync/lib/Horde/ActiveSync/Request/Options.php +++ b/framework/ActiveSync/lib/Horde/ActiveSync/Request/Options.php @@ -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(); diff --git a/framework/ActiveSync/lib/Horde/ActiveSync/Request/Ping.php b/framework/ActiveSync/lib/Horde/ActiveSync/Request/Ping.php index 162f0c30b..86b677a74 100644 --- a/framework/ActiveSync/lib/Horde/ActiveSync/Request/Ping.php +++ b/framework/ActiveSync/lib/Horde/ActiveSync/Request/Ping.php @@ -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); diff --git a/framework/ActiveSync/lib/Horde/ActiveSync/Request/Provision.php b/framework/ActiveSync/lib/Horde/ActiveSync/Request/Provision.php index 240f9dfc5..cc1b270d1 100644 --- a/framework/ActiveSync/lib/Horde/ActiveSync/Request/Provision.php +++ b/framework/ActiveSync/lib/Horde/ActiveSync/Request/Provision.php @@ -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 diff --git a/framework/ActiveSync/lib/Horde/ActiveSync/Request/Sync.php b/framework/ActiveSync/lib/Horde/ActiveSync/Request/Sync.php index 2e7596c14..82743260f 100644 --- a/framework/ActiveSync/lib/Horde/ActiveSync/Request/Sync.php +++ b/framework/ActiveSync/lib/Horde/ActiveSync/Request/Sync.php @@ -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 */ diff --git a/framework/ActiveSync/lib/Horde/ActiveSync/State/Base.php b/framework/ActiveSync/lib/Horde/ActiveSync/State/Base.php index 8a3be982a..1be7157ff 100644 --- a/framework/ActiveSync/lib/Horde/ActiveSync/State/Base.php +++ b/framework/ActiveSync/lib/Horde/ActiveSync/State/Base.php @@ -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) diff --git a/framework/ActiveSync/lib/Horde/ActiveSync/State/File.php b/framework/ActiveSync/lib/Horde/ActiveSync/State/File.php index 211f85ef8..d792c05fc 100644 --- a/framework/ActiveSync/lib/Horde/ActiveSync/State/File.php +++ b/framework/ActiveSync/lib/Horde/ActiveSync/State/File.php @@ -341,9 +341,11 @@ class Horde_ActiveSync_State_File extends Horde_ActiveSync_State_Base } /** - * Obtain the device info array. + * Obtain the device object. * - * @param $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); }