Fix version output when missing.
[plcapi.git] / php / header.php
index 6b3dd2c..54f9b1e 100644 (file)
@@ -8,7 +8,8 @@
 // Mark Huang <mlhuang@cs.princeton.edu>
 // Copyright (C) 2005-2006 The Trustees of Princeton University
 //
-// $Id: gen_php_api.py,v 1.13 2006/03/23 04:29:08 mlhuang Exp $
+// $Id$
+// $URL$
 //
 //
 
@@ -25,11 +26,11 @@ class PLCAPI
   var $calls;
   var $multicall;
 
-  function PLCAPI ($auth,
-                  $server = PLC_API_HOST,
-                  $port = 8000, # PLC_API_PORT,
-                  $path = PLC_API_PATH,
-                  $cainfo = NULL)
+  function PLCAPI($auth = NULL,
+                 $server = PLC_API_HOST,
+                 $port = PLC_API_PORT,
+                 $path = PLC_API_PATH,
+                 $cainfo = NULL)
   {
     $this->auth = $auth;
     $this->server = $server;
@@ -38,7 +39,7 @@ class PLCAPI
     $this->cainfo = $cainfo;
     $this->errors = array();
     $this->trace = array();
-    $this->calls = array ();
+    $this->calls = array();
     $this->multicall = false;
   }
 
@@ -52,7 +53,7 @@ class PLCAPI
     error_log(end($this->errors));
   }
 
-  function error ()
+  function error()
   {
     if (empty($this->trace)) {
       return NULL;
@@ -62,7 +63,7 @@ class PLCAPI
     }
   }
 
-  function trace ()
+  function trace()
   {
     return $this->trace;
   }
@@ -76,15 +77,15 @@ class PLCAPI
   function call($method, $args = NULL)
   {
     if ($this->multicall) {
-       $this->calls[] = array ('methodName' => $method,
+      $this->calls[] = array ('methodName' => $method,
                                'params' => $args);
-       return NULL;
+      return NULL;
     } else {
-       return $this->internal_call ($method, $args, 3);
+      return $this->internal_call ($method, $args, 3);
     }
   }
 
-  function internal_call ($method, $args = NULL, $backtrace_level = 2)
+  function internal_call($method, $args = NULL, $backtrace_level = 2)
   {
     $curl = curl_init();
 
@@ -93,8 +94,8 @@ class PLCAPI
       curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
       if (!empty($this->cainfo)) {
        curl_setopt($curl, CURLOPT_CAINFO, $this->cainfo);
-      } elseif (defined('PLC_API_SSL_CRT')) {
-        curl_setopt($curl, CURLOPT_CAINFO, PLC_API_SSL_CRT);
+      } elseif (defined('PLC_API_CA_SSL_CRT')) {
+        curl_setopt($curl, CURLOPT_CAINFO, PLC_API_CA_SSL_CRT);
       }
       $url = 'https://';
     } else {
@@ -105,8 +106,10 @@ class PLCAPI
     $url .= $this->server . ':' . $this->port . '/' . $this->path;
     curl_setopt($curl, CURLOPT_URL, $url);
 
-    // Marshal the XML-RPC request as a POST variable
-    $request = xmlrpc_encode_request($method, $args);
+    // Marshal the XML-RPC request as a POST variable. <nil/> is an
+    // extension to the XML-RPC spec that is supported in our custom
+    // version of xmlrpc.so via the 'allow_null' output_encoding key.
+    $request = xmlrpc_encode_request($method, $args, array('allow_null' => TRUE));
     curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
 
     // Construct the HTTP header
@@ -115,7 +118,7 @@ class PLCAPI
     curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
 
     // Set some miscellaneous options
-    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
+    curl_setopt($curl, CURLOPT_TIMEOUT, 180);
 
     // Get the output of the request
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
@@ -156,7 +159,7 @@ class PLCAPI
     $this->multicall = true;
   }
 
-  function commit ()
+  function commit()
   {
     if (!empty ($this->calls)) {
       $ret = array();
@@ -167,6 +170,16 @@ class PLCAPI
             $this->error_log('Fault Code ' . $result['faultCode'] . ': ' .
                              $result['faultString'], 1, true);
             $ret[] = NULL;
+           // Thierry - march 30 2007 
+           // using $adm->error() is broken with begin/commit style 
+           // this is because error() uses last item in trace and checks for ['errors']
+           // when using begin/commit we do run internal_call BUT internal_call checks for 
+           // multicall's result globally, not individual results, so ['errors'] comes empty
+           // I considered hacking internal_call 
+           // to *NOT* maintain this->trace at all when invoked with multicall
+           // but it is too complex to get all values right
+           // so let's go for the hacky way, and just record individual errors at the right place
+            $this->trace[count($this->trace)-1]['errors'][] = end($this->errors);
           } else {
             $ret[] = $result[0];
           }
@@ -178,7 +191,7 @@ class PLCAPI
       $ret = NULL;
     }
 
-    $this->calls = array ();
+    $this->calls = array();
     $this->multicall = false;
 
     return $ret;