Fix version output when missing.
[plcapi.git] / php / header.php
1 <?php
2 //
3 // PlanetLab Central Slice API (PLCAPI) PHP interface
4 //
5 // DO NOT EDIT. This file was automatically generated at
6 // @DATE@.
7 //
8 // Mark Huang <mlhuang@cs.princeton.edu>
9 // Copyright (C) 2005-2006 The Trustees of Princeton University
10 //
11 // $Id$
12 // $URL$
13 //
14 //
15
16 require_once 'plc_config.php';
17
18 class PLCAPI
19 {
20   var $auth;
21   var $server;
22   var $port;
23   var $path;
24   var $errors;
25   var $trace;
26   var $calls;
27   var $multicall;
28
29   function PLCAPI($auth = NULL,
30                   $server = PLC_API_HOST,
31                   $port = PLC_API_PORT,
32                   $path = PLC_API_PATH,
33                   $cainfo = NULL)
34   {
35     $this->auth = $auth;
36     $this->server = $server;
37     $this->port = $port;
38     $this->path = $path;
39     $this->cainfo = $cainfo;
40     $this->errors = array();
41     $this->trace = array();
42     $this->calls = array();
43     $this->multicall = false;
44   }
45
46   function error_log($error_msg, $backtrace_level = 1)
47   {
48     $backtrace = debug_backtrace();
49     $file = $backtrace[$backtrace_level]['file'];
50     $line = $backtrace[$backtrace_level]['line'];
51
52     $this->errors[] = 'PLCAPI error:  ' . $error_msg . ' in ' . $file . ' on line ' . $line;
53     error_log(end($this->errors));
54   }
55
56   function error()
57   {
58     if (empty($this->trace)) {
59       return NULL;
60     } else {
61       $last_trace = end($this->trace);
62       return implode("\\n", $last_trace['errors']);
63     }
64   }
65
66   function trace()
67   {
68     return $this->trace;
69   }
70
71   function microtime_float()
72   {
73     list($usec, $sec) = explode(" ", microtime());
74     return ((float) $usec + (float) $sec);
75   }
76
77   function call($method, $args = NULL)
78   {
79     if ($this->multicall) {
80       $this->calls[] = array ('methodName' => $method,
81                                 'params' => $args);
82       return NULL;
83     } else {
84       return $this->internal_call ($method, $args, 3);
85     }
86   }
87
88   function internal_call($method, $args = NULL, $backtrace_level = 2)
89   {
90     $curl = curl_init();
91
92     // Verify peer certificate if talking over SSL
93     if ($this->port == 443) {
94       curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
95       if (!empty($this->cainfo)) {
96         curl_setopt($curl, CURLOPT_CAINFO, $this->cainfo);
97       } elseif (defined('PLC_API_CA_SSL_CRT')) {
98         curl_setopt($curl, CURLOPT_CAINFO, PLC_API_CA_SSL_CRT);
99       }
100       $url = 'https://';
101     } else {
102       $url = 'http://';
103     }
104
105     // Set the URL for the request
106     $url .= $this->server . ':' . $this->port . '/' . $this->path;
107     curl_setopt($curl, CURLOPT_URL, $url);
108
109     // Marshal the XML-RPC request as a POST variable. <nil/> is an
110     // extension to the XML-RPC spec that is supported in our custom
111     // version of xmlrpc.so via the 'allow_null' output_encoding key.
112     $request = xmlrpc_encode_request($method, $args, array('allow_null' => TRUE));
113     curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
114
115     // Construct the HTTP header
116     $header[] = 'Content-type: text/xml';
117     $header[] = 'Content-length: ' . strlen($request);
118     curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
119
120     // Set some miscellaneous options
121     curl_setopt($curl, CURLOPT_TIMEOUT, 180);
122
123     // Get the output of the request
124     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
125     $t0 = $this->microtime_float();
126     $output = curl_exec($curl);
127     $t1 = $this->microtime_float();
128
129     if (curl_errno($curl)) {
130       $this->error_log('curl: ' . curl_error($curl), true);
131       $ret = NULL;
132     } else {
133       $ret = xmlrpc_decode($output);
134       if (is_array($ret) && xmlrpc_is_fault($ret)) {
135         $this->error_log('Fault Code ' . $ret['faultCode'] . ': ' .
136                          $ret['faultString'], $backtrace_level, true);
137         $ret = NULL;
138       }
139     }
140
141     curl_close($curl);
142
143     $this->trace[] = array('method' => $method,
144                            'args' => $args,
145                            'runtime' => $t1 - $t0,
146                            'return' => $ret,
147                            'errors' => $this->errors);
148     $this->errors = array();
149
150     return $ret;
151   }
152
153   function begin()
154   {
155     if (!empty($this->calls)) {
156       $this->error_log ('Warning: multicall already in progress');
157     }
158
159     $this->multicall = true;
160   }
161
162   function commit()
163   {
164     if (!empty ($this->calls)) {
165       $ret = array();
166       $results = $this->internal_call ('system.multicall', array ($this->calls));
167       foreach ($results as $result) {
168         if (is_array($result)) {
169           if (xmlrpc_is_fault($result)) {
170             $this->error_log('Fault Code ' . $result['faultCode'] . ': ' .
171                              $result['faultString'], 1, true);
172             $ret[] = NULL;
173             // Thierry - march 30 2007 
174             // using $adm->error() is broken with begin/commit style 
175             // this is because error() uses last item in trace and checks for ['errors']
176             // when using begin/commit we do run internal_call BUT internal_call checks for 
177             // multicall's result globally, not individual results, so ['errors'] comes empty
178             // I considered hacking internal_call 
179             // to *NOT* maintain this->trace at all when invoked with multicall
180             // but it is too complex to get all values right
181             // so let's go for the hacky way, and just record individual errors at the right place
182             $this->trace[count($this->trace)-1]['errors'][] = end($this->errors);
183           } else {
184             $ret[] = $result[0];
185           }
186         } else {
187           $ret[] = $result;
188         }
189       }
190     } else {
191       $ret = NULL;
192     }
193
194     $this->calls = array();
195     $this->multicall = false;
196
197     return $ret;
198   }
199
200   //
201   // PLCAPI Methods
202   //
203