3 // PlanetLab Central Slice API (PLCAPI) PHP interface
5 // DO NOT EDIT. This file was automatically generated at
8 // Mark Huang <mlhuang@cs.princeton.edu>
9 // Copyright (C) 2005-2006 The Trustees of Princeton University
16 require_once 'plc_config.php';
29 function PLCAPI($auth = NULL,
30 $server = PLC_API_HOST,
36 $this->server = $server;
39 $this->cainfo = $cainfo;
40 $this->errors = array();
41 $this->trace = array();
42 $this->calls = array();
43 $this->multicall = false;
46 function rec_join ($arg) {
47 if ( is_array($arg) ) {
49 foreach ( $arg as $i ) {
50 $l = $this->rec_join($i);
52 if ( $l[0] != "<" ) { $ret .= $l . ", "; }
56 settype($arg, "string");
61 function backtrace_php () {
62 $backtrace = debug_backtrace();
64 $len = count($backtrace);
66 foreach( array_reverse($backtrace) as $line ) {
67 $msg .= "File '". $line['file'] . "' line " . $line['line'] . "\n";
68 $msg .= " " . $line['function'] . "( " . $this->rec_join($line['args']) . ")\n";
70 if ( $cnt == $len ) { break; }
75 function error_log($error_msg, $backtrace_level = 1)
77 $backtrace = debug_backtrace();
78 $file = $backtrace[$backtrace_level]['file'];
79 $line = $backtrace[$backtrace_level]['line'];
81 $error_line='PLCAPI error: ' . $error_msg ;
82 if ($file) $error_line .= ' in file ' . $file;
83 if ($line) $error_line .= ' on line ' . $line;
84 $this->errors[] = $error_line;
85 # TODO: setup a config variable for more detailed stack traces, for API errors.
87 error_log($error_line);
89 error_log($this->backtrace_php());
95 if (empty($this->trace)) {
98 $last_trace = end($this->trace);
99 return implode("\\n", $last_trace['errors']);
108 function microtime_float()
110 list($usec, $sec) = explode(" ", microtime());
111 return ((float) $usec + (float) $sec);
114 function call($method, $args = NULL)
116 if ($this->multicall) {
117 $this->calls[] = array ('methodName' => $method,
121 return $this->internal_call ($method, $args, 3);
125 function internal_call($method, $args = NULL, $backtrace_level = 2)
129 // Verify peer certificate if talking over SSL
130 if ($this->port == 443) {
131 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
132 if (!empty($this->cainfo)) {
133 curl_setopt($curl, CURLOPT_CAINFO, $this->cainfo);
134 } elseif (defined('PLC_API_CA_SSL_CRT')) {
135 curl_setopt($curl, CURLOPT_CAINFO, PLC_API_CA_SSL_CRT);
142 // Set the URL for the request
143 $url .= $this->server . ':' . $this->port . '/' . $this->path;
144 curl_setopt($curl, CURLOPT_URL, $url);
146 // Marshal the XML-RPC request as a POST variable. <nil/> is an
147 // extension to the XML-RPC spec that is supported in our custom
148 // version of xmlrpc.so via the 'allow_null' output_encoding key.
149 $request = xmlrpc_encode_request($method, $args, array('allow_null' => TRUE));
150 curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
152 // Construct the HTTP header
153 $header[] = 'Content-type: text/xml';
154 $header[] = 'Content-length: ' . strlen($request);
155 curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
157 // Set some miscellaneous options
158 curl_setopt($curl, CURLOPT_TIMEOUT, 180);
160 // Get the output of the request
161 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
162 $t0 = $this->microtime_float();
163 $output = curl_exec($curl);
164 $t1 = $this->microtime_float();
166 if (curl_errno($curl)) {
167 $this->error_log('curl: ' . curl_error($curl), true);
170 $ret = xmlrpc_decode($output);
171 if (is_array($ret) && xmlrpc_is_fault($ret)) {
172 $this->error_log('Fault Code ' . $ret['faultCode'] . ': ' .
173 $ret['faultString'], $backtrace_level, true);
180 $this->trace[] = array('method' => $method,
182 'runtime' => $t1 - $t0,
184 'errors' => $this->errors);
185 $this->errors = array();
192 if (!empty($this->calls)) {
193 $this->error_log ('Warning: multicall already in progress');
196 $this->multicall = true;
201 if (!empty ($this->calls)) {
203 $results = $this->internal_call ('system.multicall', array ($this->calls));
204 foreach ($results as $result) {
205 if (is_array($result)) {
206 if (xmlrpc_is_fault($result)) {
207 $this->error_log('Fault Code ' . $result['faultCode'] . ': ' .
208 $result['faultString'], 1, true);
210 // Thierry - march 30 2007
211 // using $adm->error() is broken with begin/commit style
212 // this is because error() uses last item in trace and checks for ['errors']
213 // when using begin/commit we do run internal_call BUT internal_call checks for
214 // multicall's result globally, not individual results, so ['errors'] comes empty
215 // I considered hacking internal_call
216 // to *NOT* maintain this->trace at all when invoked with multicall
217 // but it is too complex to get all values right
218 // so let's go for the hacky way, and just record individual errors at the right place
219 $this->trace[count($this->trace)-1]['errors'][] = end($this->errors);
231 $this->calls = array();
232 $this->multicall = false;
241 function __call($name, $args)
243 array_unshift($args, $this->auth);
244 return $this->call($name, $args);
250 $adm = new PLCAPI(array('AuthMethod' => "capability",
251 'Username' => PLC_API_MAINTENANCE_USER,
252 'AuthString' => PLC_API_MAINTENANCE_PASSWORD));