9a08ad2115c4c12121e2a8bdca4bd6553a2d4d89
[plcapi.git] / php / plc_api.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
12 require_once 'plc_config.php';
13
14 class PLCAPI
15 {
16   var $auth;
17   var $server;
18   var $port;
19   var $path;
20   var $errors;
21   var $trace;
22   var $calls;
23   var $multicall;
24
25   function PLCAPI($auth = NULL,
26                   $server = PLC_API_HOST,
27                   $port = PLC_API_PORT,
28                   $path = PLC_API_PATH,
29                   $cainfo = NULL)
30   {
31     $this->auth = $auth;
32     $this->server = $server;
33     $this->port = $port;
34     $this->path = $path;
35     $this->cainfo = $cainfo;
36     $this->errors = array();
37     $this->trace = array();
38     $this->calls = array();
39     $this->multicall = false;
40   }
41
42   function rec_join ($arg) {
43     if ( is_array($arg) ) {
44         $ret = "";
45         foreach ( $arg as $i ) {
46             $l = $this->rec_join($i);
47             # ignore html code.
48             if ( $l[0] != "<" ) { $ret .= $l . ", "; }
49         }
50         return $ret;
51     } else {
52         settype($arg, "string");
53         return $arg;
54     }
55   }
56
57   function backtrace_php () {
58     $backtrace = debug_backtrace();
59     $msg = "";
60     $len = count($backtrace);
61     $cnt = 1;
62     foreach( array_reverse($backtrace) as $line ) {
63         $msg .= "File '". $line['file'] . "' line " . $line['line'] . "\n";
64         $msg .= "    " . $line['function'] . "( "  . $this->rec_join($line['args']) . ")\n";
65         $cnt += 1;
66         if ( $cnt == $len ) { break; }
67     }
68     return $msg;
69   }
70
71   function error_log($error_msg, $backtrace_level = 1)
72   {
73     $backtrace = debug_backtrace();
74     $file = $backtrace[$backtrace_level]['file'];
75     $line = $backtrace[$backtrace_level]['line'];
76
77     $error_line='PLCAPI error:  ' . $error_msg ;
78     if ($file) $error_line .= ' in file ' . $file;
79     if ($line) $error_line .= ' on line ' . $line;
80     $this->errors[] = $error_line;
81     # TODO: setup a config variable for more detailed stack traces, for API errors.
82     if ( TRUE ){
83       error_log($error_line);
84     } else {
85        error_log($this->backtrace_php());
86     }
87   }
88
89   function error()
90   {
91     if (empty($this->trace)) {
92       return NULL;
93     } else {
94       $last_trace = end($this->trace);
95       return implode("\\n", $last_trace['errors']);
96     }
97   }
98
99   function trace()
100   {
101     return $this->trace;
102   }
103
104   function microtime_float()
105   {
106     list($usec, $sec) = explode(" ", microtime());
107     return ((float) $usec + (float) $sec);
108   }
109
110   function call($method, $args = NULL)
111   {
112     if ($this->multicall) {
113       $this->calls[] = array ('methodName' => $method,
114                                 'params' => $args);
115       return NULL;
116     } else {
117       return $this->internal_call ($method, $args, 3);
118     }
119   }
120
121   function internal_call($method, $args = NULL, $backtrace_level = 2)
122   {
123     $curl = curl_init();
124
125     // Verify peer certificate if talking over SSL
126     if ($this->port == 443) {
127       curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
128       if (!empty($this->cainfo)) {
129         curl_setopt($curl, CURLOPT_CAINFO, $this->cainfo);
130       } elseif (defined('PLC_API_CA_SSL_CRT')) {
131         curl_setopt($curl, CURLOPT_CAINFO, PLC_API_CA_SSL_CRT);
132       }
133       $url = 'https://';
134     } else {
135       $url = 'http://';
136     }
137
138     // Set the URL for the request
139     $url .= $this->server . ':' . $this->port . '/' . $this->path;
140     curl_setopt($curl, CURLOPT_URL, $url);
141
142     // Marshal the XML-RPC request as a POST variable. <nil/> is an
143     // extension to the XML-RPC spec that is supported in our custom
144     // version of xmlrpc.so via the 'allow_null' output_encoding key.
145     $request = xmlrpc_encode_request($method, $args, array('allow_null' => TRUE));
146     curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
147
148     // Construct the HTTP header
149     $header[] = 'Content-type: text/xml';
150     $header[] = 'Content-length: ' . strlen($request);
151     curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
152
153     // Set some miscellaneous options
154     curl_setopt($curl, CURLOPT_TIMEOUT, 180);
155
156     // Get the output of the request
157     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
158     $t0 = $this->microtime_float();
159     $output = curl_exec($curl);
160     $t1 = $this->microtime_float();
161
162     if (curl_errno($curl)) {
163       $this->error_log('curl: ' . curl_error($curl), true);
164       $ret = NULL;
165     } else {
166       $ret = xmlrpc_decode($output);
167       if (is_array($ret) && xmlrpc_is_fault($ret)) {
168         $this->error_log('Fault Code ' . $ret['faultCode'] . ': ' .
169                          $ret['faultString'], $backtrace_level, true);
170         $ret = NULL;
171       }
172     }
173
174     curl_close($curl);
175
176     $this->trace[] = array('method' => $method,
177                            'args' => $args,
178                            'runtime' => $t1 - $t0,
179                            'return' => $ret,
180                            'errors' => $this->errors);
181     $this->errors = array();
182
183     return $ret;
184   }
185
186   function begin()
187   {
188     if (!empty($this->calls)) {
189       $this->error_log ('Warning: multicall already in progress');
190     }
191
192     $this->multicall = true;
193   }
194
195   function commit()
196   {
197     if (!empty ($this->calls)) {
198       $ret = array();
199       $results = $this->internal_call ('system.multicall', array ($this->calls));
200       foreach ($results as $result) {
201         if (is_array($result)) {
202           if (xmlrpc_is_fault($result)) {
203             $this->error_log('Fault Code ' . $result['faultCode'] . ': ' .
204                              $result['faultString'], 1, true);
205             $ret[] = NULL;
206             // Thierry - march 30 2007 
207             // using $adm->error() is broken with begin/commit style 
208             // this is because error() uses last item in trace and checks for ['errors']
209             // when using begin/commit we do run internal_call BUT internal_call checks for 
210             // multicall's result globally, not individual results, so ['errors'] comes empty
211             // I considered hacking internal_call 
212             // to *NOT* maintain this->trace at all when invoked with multicall
213             // but it is too complex to get all values right
214             // so let's go for the hacky way, and just record individual errors at the right place
215             $this->trace[count($this->trace)-1]['errors'][] = end($this->errors);
216           } else {
217             $ret[] = $result[0];
218           }
219         } else {
220           $ret[] = $result;
221         }
222       }
223     } else {
224       $ret = NULL;
225     }
226
227     $this->calls = array();
228     $this->multicall = false;
229
230     return $ret;
231   }
232
233   //
234   // PLCAPI Methods
235   //
236
237   function __call($name, $args)
238   {
239      array_unshift($args, $this->auth);
240      return $this->call($name, $args);
241   }
242 }
243
244 global $adm;
245
246 $adm = new PLCAPI(array('AuthMethod' => "capability",
247                         'Username' => PLC_API_MAINTENANCE_USER,
248                         'AuthString' => PLC_API_MAINTENANCE_PASSWORD));
249
250 ?>