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
12 require_once 'plc_config.php';
25 function PLCAPI($auth = NULL,
26 $server = PLC_API_HOST,
32 $this->server = $server;
35 $this->cainfo = $cainfo;
36 $this->errors = array();
37 $this->trace = array();
38 $this->calls = array();
39 $this->multicall = false;
42 function rec_join ($arg) {
43 if ( is_array($arg) ) {
45 foreach ( $arg as $i ) {
46 $l = $this->rec_join($i);
48 if ( $l[0] != "<" ) { $ret .= $l . ", "; }
52 settype($arg, "string");
57 function backtrace_php () {
58 $backtrace = debug_backtrace();
60 $len = count($backtrace);
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";
66 if ( $cnt == $len ) { break; }
71 function error_log($error_msg, $backtrace_level = 1)
73 $backtrace = debug_backtrace();
74 $file = $backtrace[$backtrace_level]['file'];
75 $line = $backtrace[$backtrace_level]['line'];
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.
83 error_log($error_line);
85 error_log($this->backtrace_php());
91 if (empty($this->trace)) {
94 $last_trace = end($this->trace);
95 return implode("\\n", $last_trace['errors']);
104 function microtime_float()
106 list($usec, $sec) = explode(" ", microtime());
107 return ((float) $usec + (float) $sec);
110 function call($method, $args = NULL)
112 if ($this->multicall) {
113 $this->calls[] = array ('methodName' => $method,
117 return $this->internal_call ($method, $args, 3);
121 function internal_call($method, $args = NULL, $backtrace_level = 2)
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);
138 // Set the URL for the request
139 $url .= $this->server . ':' . $this->port . '/' . $this->path;
140 curl_setopt($curl, CURLOPT_URL, $url);
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);
148 // Construct the HTTP header
149 $header[] = 'Content-type: text/xml';
150 $header[] = 'Content-length: ' . strlen($request);
151 curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
153 // Set some miscellaneous options
154 curl_setopt($curl, CURLOPT_TIMEOUT, 180);
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();
162 if (curl_errno($curl)) {
163 $this->error_log('curl: ' . curl_error($curl), true);
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);
176 $this->trace[] = array('method' => $method,
178 'runtime' => $t1 - $t0,
180 'errors' => $this->errors);
181 $this->errors = array();
188 if (!empty($this->calls)) {
189 $this->error_log ('Warning: multicall already in progress');
192 $this->multicall = true;
197 if (!empty ($this->calls)) {
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);
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);
227 $this->calls = array();
228 $this->multicall = false;
237 function __call($name, $args)
239 array_unshift($args, $this->auth);
240 return $this->call($name, $args);
246 $adm = new PLCAPI(array('AuthMethod' => "capability",
247 'Username' => PLC_API_MAINTENANCE_USER,
248 'AuthString' => PLC_API_MAINTENANCE_PASSWORD));