Index: rest_server.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/rest_server/Attic/rest_server.info,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 rest_server.info
--- rest_server.info	5 Jan 2008 05:39:54 -0000	1.1.2.1
+++ rest_server.info	18 Oct 2008 22:13:53 -0000
@@ -1,5 +1,5 @@
-; $Id;
+; $Id$
 name = REST Server
 description = Provides a REST server.
 package = "Services - servers"
-dependancies = zend services
+dependencies = services
Index: rest_server.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/rest_server/Attic/rest_server.module,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 rest_server.module
--- rest_server.module	5 Jan 2008 05:39:54 -0000	1.1.2.1
+++ rest_server.module	18 Oct 2008 22:13:53 -0000
@@ -1,59 +1,139 @@
 <?php
-// $Id: rest_server.module,v 1.1.2.1 2008/01/05 05:39:54 robloach Exp $
+// $Id$
 
 /**
- * Implementation of hook_server_info()
+ * Implementation of hook_server_info().
  */
 function rest_server_server_info() {
   return array(
     '#name' => 'REST',
     '#path' => 'rest',
   );
-} // function rest_server_server_info
+} 
 
 /**
- * Implementation of hook_server()
+ * Implementation of hook_server().
  */
 function rest_server_server() {
-  // Initiate the server and the request
-  if (zend_initialize('Zend_Rest_Server')) {
-    $request = $_REQUEST;
-    $server = new Zend_Rest_Server();
-    
-    // Check for which service we're calling
-    foreach (services_get_all() as $method) {
-      if ($method['#method'] == $request['method']) {
-        $request['method'] = $method['#callback'];
-        $server->addFunction($method['#callback']);
-        
-        // Special use cases for arguments of certain types
-        foreach ($method['#args'] as $argument) {
-          $argname = $argument['#name'];
-          switch ($argument['#type']) {
-            case 'array' :
-              if (empty($request[$argname])) {
-                $request[$argname] = array();
-              }
-              else {
-                $request[$argname] = explode(',', $request[$argname]);
-              }
-              break;
-          }
+  $request = $_REQUEST;
+  $args = array();
+
+  // Check for which service we're calling
+  foreach (services_get_all() as $method) {
+    if ($method['#method'] == $request['method']) {
+      $method_name = $method['#method'];
+
+      // Special use cases for arguments of certain types
+      foreach ($method['#args'] as $argument) {
+        $argname = $argument['#name'];
+        switch ($argument['#type']) {
+          case 'array' :
+            if (empty($request[$argname])) {
+              $args[] = array();
+            }
+            else {
+              $args[] = explode(',', $request[$argname]);
+            }
+            break;
+          case 'string':
+            if(empty($request[$argname])) {
+              $args[] = '';
+            }
+            else {
+              $args[] = $request[$argname];
+            }
+            break;
         }
-        
-        // Found method to call, so exit
-        break;
       }
+      break;
     }
-    try {
-      echo $server->handle($request);
+  }
+
+  try {
+    $result = services_method_call($method_name, $args);
+    $output = rest_server_to_xml($result);
+    rest_server_respond($output);
+  }
+  catch (Exception $e) {
+    $result = rest_server_server_error($e->getMessage());
+    $output = rest_server_to_xml($result);
+    rest_server_respond($output);
+  }
+
+} 
+
+/**
+ * Implementation of hook_server_error
+ *
+ * @param string $message
+ * @return array
+ */
+function rest_server_server_error($message) {
+  return array(
+    'status' => 'error',
+    'message' => $message,
+  );
+}
+
+/**
+ * Print out the response to client.
+ */
+function rest_server_respond($data) {
+  header('Content-type: text/xml');
+  print $data;
+}
+
+/**
+ * Convert data array to xml representation.
+ *
+ * @param $data
+ *   Array or object with data to convert.
+ * @param $root_node
+ *   Response root node name.
+ * @param $xml
+ *   SimpleXML object, used in recursive calls.
+ * @param $depth
+ *   Node depth, used in recursive calls.
+ * 
+ * @return
+ *   XML string.
+ */
+function rest_server_to_xml($data, $root_node = 'response', $xml = NULL, $depth = 0)  {
+  if (!isset($xml)) {
+    $xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><rest generator="xml_writer" version="1.0" />');
+    $root = $xml->addChild($root_node);
+  }
+  else {
+    $root = $xml;
+  }
+
+  foreach($data as $key => $value) {
+    if (is_array($value)|| is_object($value)) {
+      $node = is_numeric($key) ? _rest_server_item_node($root, $key) : $root->addChild($key);
+      rest_server_to_xml($value, $root_node, $node, $depth + 1);
     }
-    catch (Exception $e) {
-      echo $e->getMessage();
+    else {
+      $value = htmlentities($value);
+      if (is_numeric($key)) {
+        _rest_server_item_node($root, $key, $value);
+      }
+      else {
+        $root->addChild($key, $value);
+      }
     }
   }
-  else {
-    drupal_set_message(t('The <em>REST Server</em> requires proper configuration of the <a href="@url" target="_blank">Zend Framework</a>. Please <a href="@configure">configure it</a> correctly and try again.', array('@url' => 'http://framework.zend.com', '@configure' => url('admin/settings/zend'))), 'error');
-    drupal_goto(variable_get('site_frontpage', 'node'));
+
+  if ($depth == 0) {
+    return $xml->asXML();
   }
-} // function rest_server_server
+}
+
+/**
+ * Helper function to generate 'item' nodes for arrays with numeric keys.
+ * E.g. <item id="0">.
+ */
+function _rest_server_item_node($root, $key, $value = NULL) {
+  $item = isset($value) ? $root->addChild('item', $value) : $root->addChild('item');
+  $item->addAttribute('id', $key);
+  return $item;
+}
