Clean up the RPC script.
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 15 Jul 2010 21:36:11 +0000 (15:36 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 15 Jul 2010 21:56:32 +0000 (15:56 -0600)
Debugging was difficult for me with a bunch of duplicate code.  Try to
do the minimal amount possible before the Horde environment gets init'd.

horde/rpc.php

index 9ae3542..01a112e 100644 (file)
@@ -3,18 +3,22 @@
  * RPC processing script.
  *
  * Possible GET values:
- *
- *   'requestMissingAuthorization' -- Whether or not to request
- *   authentication credentials if they are not already present.
- *
- *   'wsdl' -- TODO
+ * <pre>
+ * 'requestMissingAuthorization' - Whether or not to request authentication
+ *                                 credentials if they are not already
+ *                                 present.
+ * 'wsdl' - TODO
+ * </pre>
  *
  * Copyright 2002-2010 The Horde Project (http://www.horde.org/)
  *
  * See the enclosed file COPYING for license information (LGPL). If you
  * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  *
- * @author Jan Schneider <jan@horde.org>
+ * @author   Jan Schneider <jan@horde.org>
+ * @category Horde
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @package  Horde
  */
 
 require_once dirname(__FILE__) . '/lib/Application.php';
@@ -32,104 +36,54 @@ $params = array();
 /* Look at the Content-type of the request, if it is available, to try
  * and determine what kind of request this is. */
 if ((!empty($_SERVER['CONTENT_TYPE']) &&
-     strpos($_SERVER['CONTENT_TYPE'], 'application/vnd.ms-sync.wbxml') !== false) ||
-    strpos($_SERVER['REQUEST_URI'], 'Microsoft-Server-ActiveSync') !== false) {
-
+     (strpos($_SERVER['CONTENT_TYPE'], 'application/vnd.ms-sync.wbxml') !== false)) ||
+   (strpos($_SERVER['REQUEST_URI'], 'Microsoft-Server-ActiveSync') !== false)) {
     /* ActiveSync Request */
     $serverType = 'ActiveSync';
-    $session_control = 'none';
     $nocompress = true;
-    Horde_Registry::appInit('horde', array('authentication' => 'none', 'nocompress' => $nocompress, 'session_control' => $session_control));
-
-    /* Check if we are even enabled for AS */
-    if (!empty($conf['activesync']['enabled'])) {
-        $request = new Horde_Controller_Request_Http(array('session_control' => $session_control));
-        if ($conf['activesync']['logging']['type'] == 'custom') {
-            $params['logger'] = new Horde_Log_Logger(new Horde_Log_Handler_Stream(fopen($conf['activesync']['logging']['path'], 'a')));
-        } else {
-            $params['logger'] = $injector->getInstance('Horde_Log_Logger');
-        }
-        $mailer = $injector->getInstance('Horde_Mail');
-
-        /* TODO: Probably want to bind a factory to injector for this? */
-        $params['registry'] = $registry;
-        $connector = new Horde_ActiveSync_Driver_Horde_Connector_Registry($params);
-        switch ($conf['activesync']['state']['driver']) {
-        case 'file':
-            $stateMachine = new Horde_ActiveSync_State_File($conf['activesync']['state']['params']);
-            break;
-        case 'history':
-            $state_params = $conf['activesync']['state']['params'];
-            $state_params['db'] = $injector->getInstance('Horde_Db_Adapter_Base');
-            $stateMachine = new Horde_ActiveSync_State_History($state_params);
-        }
-
-        $driver_params = array('connector' => $connector,
-                               'state_basic' => $stateMachine,
-                               'mail' => $mailer,
-                               'ping' => $conf['activesync']['ping']);
-
-        if ($params['provisioning'] = $conf['activesync']['securitypolicies']['provisioning']) {
-            $driver_params['policies'] = $conf['activesync']['securitypolicies'];
-        }
-        $params['backend'] = new Horde_ActiveSync_Driver_Horde($driver_params);
-        $params['server'] = new Horde_ActiveSync($params['backend'],
-                                                 new Horde_ActiveSync_Wbxml_Decoder(fopen('php://input', 'r')),
-                                                 new Horde_ActiveSync_Wbxml_Encoder(fopen('php://output', 'w+')),
-                                                 $request);
-        $params['server']->setLogger($params['logger']);
-    }
+    $session_control = 'none';
 } elseif (!empty($_SERVER['PATH_INFO']) ||
           in_array($_SERVER['REQUEST_METHOD'], array('DELETE', 'PROPFIND', 'PUT', 'OPTIONS'))) {
     $serverType = 'Webdav';
-    Horde_Registry::appInit('horde', array('authentication' => 'none', 'nocompress' => $nocompress, 'session_control' => $session_control));
-    $request = new Horde_Controller_Request_Http(array('session_control' => $session_control));
 } elseif ($_SERVER['CONTENT_TYPE']) {
     if (strpos($_SERVER['CONTENT_TYPE'], 'application/vnd.syncml+xml') !== false) {
         $serverType = 'Syncml';
-        $session_control = 'none';
         $nocompress = true;
+        $session_control = 'none';
     } elseif (strpos($_SERVER['CONTENT_TYPE'], 'application/vnd.syncml+wbxml') !== false) {
         $serverType = 'Syncml_Wbxml';
-        $session_control = 'none';
         $nocompress = true;
+        $session_control = 'none';
     } elseif (strpos($_SERVER['CONTENT_TYPE'], 'text/xml') !== false) {
         $input = Horde_Rpc::getInput();
         /* Check for SOAP namespace URI. */
-        if (strpos($input, 'http://schemas.xmlsoap.org/soap/envelope/') !== false) {
-            $serverType = 'Soap';
-        } else {
-            $serverType = 'Xmlrpc';
-        }
+        $serverType = (strpos($input, 'http://schemas.xmlsoap.org/soap/envelope/') !== false)
+            ? 'Soap'
+            : 'Xmlrpc';
     } elseif (strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) {
         $serverType = 'Jsonrpc';
     } else {
         header('HTTP/1.0 501 Not Implemented');
         exit;
     }
-    Horde_Registry::appInit('horde', array('authentication' => 'none', 'nocompress' => $nocompress, 'session_control' => $session_control));
-    $request = new Horde_Controller_Request_Http(array('session_control' => $session_control));
 } elseif ($_SERVER['QUERY_STRING'] && $_SERVER['QUERY_STRING'] == 'phpgw') {
     $serverType = 'Phpgw';
-    Horde_Registry::appInit('horde', array('authentication' => 'none', 'nocompress' => $nocompress, 'session_control' => $session_control));
-    $request = new Horde_Controller_Request_Http(array('session_control' => $session_control));
 } else {
     $serverType = 'Soap';
-    Horde_Registry::appInit('horde', array('authentication' => 'none', 'nocompress' => $nocompress, 'session_control' => $session_control));
-    $request = new Horde_Controller_Request_Http(array('session_control' => $session_control));
 }
 
-if ($serverType == 'Soap' &&
-    (!$request->getServer('REQUEST_METHOD') ||
-     $request->getServer('REQUEST_METHOD') != 'POST')) {
-    $session_control = 'none';
-    $params['requireAuthorization'] = false;
-    if (Horde_Util::getGet('wsdl') !== null) {
-        $input = 'wsdl';
-    } else {
-        $input = 'disco';
-    }
-}
+/* Initialize Horde environment. */
+Horde_Registry::appInit('horde', array(
+    'authentication' => 'none',
+    'nocompress' => $nocompress,
+    'session_control' => $session_control
+));
+
+$request = new Horde_Controller_Request_Http(array(
+    'session_control' => $session_control
+));
+
+$params['logger'] = $injector->getInstance('Horde_Log_Logger');
 
 /* Check to see if we want to exit if required credentials are not
  * present. */
@@ -137,9 +91,62 @@ if (($ra = Horde_Util::getGet('requestMissingAuthorization')) !== null) {
     $params['requestMissingAuthorization'] = $ra;
 }
 
-/* Make sure we have a logger */
-if (empty($params['logger'])) {
-    $params['logger'] = $injector->getInstance('Horde_Log_Logger');
+/* Driver specific tasks that require Horde environment. */
+switch ($serverType) {
+case 'ActiveSync':
+    /* Check if we are even enabled for AS */
+    if (empty($conf['activesync']['enabled'])) {
+        break;
+    }
+
+    if ($conf['activesync']['logging']['type'] == 'custom') {
+        $params['logger'] = new Horde_Log_Logger(new Horde_Log_Handler_Stream(fopen($conf['activesync']['logging']['path'], 'a')));
+    }
+
+    switch ($conf['activesync']['state']['driver']) {
+    case 'file':
+        $stateMachine = new Horde_ActiveSync_State_File($conf['activesync']['state']['params']);
+        break;
+
+    case 'history':
+        $state_params = $conf['activesync']['state']['params'];
+        $state_params['db'] = $injector->getInstance('Horde_Db_Adapter_Base');
+        $stateMachine = new Horde_ActiveSync_State_History($state_params);
+        break;
+    }
+
+    /* TODO: Probably want to bind a factory to injector for this? */
+    $params['registry'] = $registry;
+
+    $driver_params = array(
+        'connector' => new Horde_ActiveSync_Driver_Horde_Connector_Registry($params),
+        'mail' => $injector->getInstance('Horde_Mail'),
+        'ping' => $conf['activesync']['ping'],
+        'state_basic' => $stateMachine,
+    );
+
+    if ($params['provisioning'] = $conf['activesync']['securitypolicies']['provisioning']) {
+        $driver_params['policies'] = $conf['activesync']['securitypolicies'];
+    }
+    $params['backend'] = new Horde_ActiveSync_Driver_Horde($driver_params);
+    $params['server'] = new Horde_ActiveSync(
+        $params['backend'],
+        new Horde_ActiveSync_Wbxml_Decoder(fopen('php://input', 'r')),
+        new Horde_ActiveSync_Wbxml_Encoder(fopen('php://output', 'w+')),
+        $request
+    );
+    $params['server']->setLogger($params['logger']);
+    break;
+
+case 'Soap':
+    if (!$request->getServer('REQUEST_METHOD') ||
+        ($request->getServer('REQUEST_METHOD') != 'POST')) {
+        $params['requireAuthorization'] = false;
+        $input = (Horde_Util::getGet('wsdl') === null)
+            ? 'disco'
+            : 'wsdl';
+    }
+    break;
 }
 
 /* Load the RPC backend based on $serverType. */
@@ -152,12 +159,12 @@ $server->authorize();
 
 /* Get the server's response. We call $server->getInput() to allow
  * backends to handle input processing differently. */
-if ($input === null) {
+if (is_null($input)) {
     $input = $server->getInput();
 }
 
 $out = $server->getResponse($input);
-if (is_a($out, 'PEAR_Error')) {
+if ($out instanceof PEAR_Error) {
     header('HTTP/1.0 500 Internal Server Error');
     echo $out->getMessage();
     exit;
@@ -165,4 +172,3 @@ if (is_a($out, 'PEAR_Error')) {
 
 // Allow backends to determine how and when to send output.
 $server->sendOutput($out);
-