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 //ini_set('error_reporting', 1);
15 * May 2017 - Ciro Scognamiglio <c.scognamiglio@cslash.net>
17 * xmlrpc php module is not compatible anymore with the PLCAPI class,
18 * if the package phpxmlrpc is installed in the same dir it will be used instead
20 * https://github.com/gggeek/phpxmlrpc
22 * If the package is not found the php module XML-RPC is used if available
26 if (file_exists(__DIR__ . '/phpxmlrpc/src/Autoloader.php')) {
27 include_once __DIR__ . '/phpxmlrpc/src/Autoloader.php';
28 PhpXmlRpc\Autoloader::register();
31 require_once 'plc_config.php';
44 function PLCAPI($auth = NULL,
45 $server = PLC_API_HOST,
50 $this->server = $server;
53 $this->cainfo = $cainfo;
54 $this->errors = array();
55 $this->trace = array();
56 $this->calls = array();
57 $this->multicall = false;
60 function rec_join ($arg) {
61 if ( is_array($arg) ) {
63 foreach ( $arg as $i ) {
64 $l = $this->rec_join($i);
66 if ( $l[0] != "<" ) { $ret .= $l . ", "; }
70 settype($arg, "string");
75 function backtrace_php () {
76 $backtrace = debug_backtrace();
78 $len = count($backtrace);
80 foreach( array_reverse($backtrace) as $line ) {
81 $msg .= "File '". $line['file'] . "' line " . $line['line'] . "\n";
82 $msg .= " " . $line['function'] . "( " . $this->rec_join($line['args']) . ")\n";
84 if ( $cnt == $len ) { break; }
89 function error_log($error_msg, $backtrace_level = 1) {
90 $backtrace = debug_backtrace();
91 $file = $backtrace[$backtrace_level]['file'];
92 $line = $backtrace[$backtrace_level]['line'];
94 $error_line='PLCAPI error: ' . $error_msg ;
95 if ($file) $error_line .= ' in file ' . $file;
96 if ($line) $error_line .= ' on line ' . $line;
97 $this->errors[] = $error_line;
98 # TODO: setup a config variable for more detailed stack traces, for API errors.
100 error_log($error_line);
102 error_log($this->backtrace_php());
107 if (empty($this->trace)) {
110 $last_trace = end($this->trace);
111 return implode("\\n", $last_trace['errors']);
119 function microtime_float() {
120 list($usec, $sec) = explode(" ", microtime());
121 return ((float) $usec + (float) $sec);
124 function call($method, $args = NULL) {
125 if ($this->multicall) {
126 $this->calls[] = array ('methodName' => $method,
130 return $this->internal_call($method, $args, 3);
135 * Use PhpXmlRpc\Value before encoding the request
137 function xmlrpcValue($value) {
138 switch(gettype($value)) {
141 foreach($value as $vk => $vv) {
142 $members[$vk] = $this->xmlrpcValue($vv);
145 if ((array_key_exists(0, $value)) || (empty($value))) {
146 return new PhpXmlRpc\Value(
151 return new PhpXmlRpc\Value(
159 return new PhpXmlRpc\Value($value, 'double');
162 return new PhpXmlRpc\Value($value, 'boolean');
166 return new PhpXmlRpc\Value(null, 'null');
169 return new PhpXmlRpc\Value($value, 'int');
172 return new PhpXmlRpc\Value($value);
177 function internal_call($method, $args = NULL, $backtrace_level = 2) {
178 if (class_exists('PhpXmlRpc\\PhpXmlRpc')) {
179 return $this->internal_call_phpxmlrpc($method, $args, $backtrace_level);
181 return $this->internal_call_xmlrpc($method, $args, $backtrace_level);
186 * the new internal call, will use PhpXmlRpc
188 function internal_call_phpxmlrpc($method, $args = NULL, $backtrace_level = 2) {
191 // var_dump($method);
195 PhpXmlRpc\PhpXmlRpc::$xmlrpc_null_extension = true;
197 if ($this->port == 443) {
203 // Set the URL for the request
204 $url .= $this->server . ':' . $this->port . '/' . $this->path;
206 $client = new PhpXmlRpc\Client($url);
207 $client->setSSLVerifyPeer(false);
210 * 2 -> verify CN (default)
212 $client->setSSLVerifyHost(1);
214 $values = $this->xmlrpcValue($args);
216 $response = $client->send(new PhpXmlRpc\Request($method, $values));
219 if (!$response->faultCode()) {
220 $encoder = new PhpXmlRpc\Encoder();
221 $v = $encoder->decode($response->value());
225 $this->error_log("An error occurred [" . $response->faultCode() . "] ".
226 $response->faultString());
232 * The original internal call that uses php XML-RPC
234 function internal_call_xmlrpc($method, $args = NULL, $backtrace_level = 2) {
237 // Verify peer certificate if talking over SSL
238 if ($this->port == 443) {
239 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
240 if (!empty($this->cainfo)) {
241 curl_setopt($curl, CURLOPT_CAINFO, $this->cainfo);
242 } elseif (defined('PLC_API_CA_SSL_CRT')) {
243 curl_setopt($curl, CURLOPT_CAINFO, PLC_API_CA_SSL_CRT);
250 // Set the URL for the request
251 $url .= $this->server . ':' . $this->port . '/' . $this->path;
252 curl_setopt($curl, CURLOPT_URL, $url);
254 // Marshal the XML-RPC request as a POST variable. <nil/> is an
255 // extension to the XML-RPC spec that is supported in our custom
256 // version of xmlrpc.so via the 'allow_null' output_encoding key.
257 $request = xmlrpc_encode_request($method, $args, array('null_extension'));
258 curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
260 // Construct the HTTP header
261 $header[] = 'Content-type: text/xml';
262 $header[] = 'Content-length: ' . strlen($request);
263 curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
265 // Set some miscellaneous options
266 curl_setopt($curl, CURLOPT_TIMEOUT, 180);
268 // Get the output of the request
269 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
270 $t0 = $this->microtime_float();
271 $output = curl_exec($curl);
272 $t1 = $this->microtime_float();
274 if (curl_errno($curl)) {
275 $this->error_log('curl: ' . curl_error($curl), true);
278 $ret = xmlrpc_decode($output);
279 if (is_array($ret) && xmlrpc_is_fault($ret)) {
280 $this->error_log('Fault Code ' . $ret['faultCode'] . ': ' .
281 $ret['faultString'], $backtrace_level, true);
288 $this->trace[] = array('method' => $method,
290 'runtime' => $t1 - $t0,
292 'errors' => $this->errors);
293 $this->errors = array();
299 if (!empty($this->calls)) {
300 $this->error_log ('Warning: multicall already in progress');
303 $this->multicall = true;
306 function xmlrpc_is_fault($arr) {
307 // check if xmlrpc_is_fault exists
308 return is_array($arr) && array_key_exists('faultCode', $arr) && array_key_exists('faultString', $arr);
312 if (!empty ($this->calls)) {
314 $results = $this->internal_call('system.multicall', array ($this->calls));
315 foreach ($results as $result) {
316 if (is_array($result)) {
317 if ($this->xmlrpc_is_fault($result)) {
318 $this->error_log('Fault Code ' . $result['faultCode'] . ': ' .
319 $result['faultString'], 1, true);
321 // Thierry - march 30 2007
322 // using $adm->error() is broken with begin/commit style
323 // this is because error() uses last item in trace and checks for ['errors']
324 // when using begin/commit we do run internal_call BUT internal_call checks for
325 // multicall's result globally, not individual results, so ['errors'] comes empty
326 // I considered hacking internal_call
327 // to *NOT* maintain this->trace at all when invoked with multicall
328 // but it is too complex to get all values right
329 // so let's go for the hacky way, and just record individual errors at the right place
330 $this->trace[count($this->trace)-1]['errors'][] = end($this->errors);
342 $this->calls = array();
343 $this->multicall = false;
352 function __call($name, $args) {
353 array_unshift($args, $this->auth);
354 return $this->call($name, $args);
360 $adm = new PLCAPI(array('AuthMethod' => "capability",
361 'Username' => PLC_API_MAINTENANCE_USER,
362 'AuthString' => PLC_API_MAINTENANCE_PASSWORD));