6b3dd2c27675f014a3ab82d496a426b0752cdc87
[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: gen_php_api.py,v 1.13 2006/03/23 04:29:08 mlhuang Exp $
12 //
13 //
14
15 require_once 'plc_config.php';
16
17 class PLCAPI
18 {
19   var $auth;
20   var $server;
21   var $port;
22   var $path;
23   var $errors;
24   var $trace;
25   var $calls;
26   var $multicall;
27
28   function PLCAPI ($auth,
29                    $server = PLC_API_HOST,
30                    $port = 8000, # PLC_API_PORT,
31                    $path = PLC_API_PATH,
32                    $cainfo = NULL)
33   {
34     $this->auth = $auth;
35     $this->server = $server;
36     $this->port = $port;
37     $this->path = $path;
38     $this->cainfo = $cainfo;
39     $this->errors = array();
40     $this->trace = array();
41     $this->calls = array ();
42     $this->multicall = false;
43   }
44
45   function error_log($error_msg, $backtrace_level = 1)
46   {
47     $backtrace = debug_backtrace();
48     $file = $backtrace[$backtrace_level]['file'];
49     $line = $backtrace[$backtrace_level]['line'];
50
51     $this->errors[] = 'PLCAPI error:  ' . $error_msg . ' in ' . $file . ' on line ' . $line;
52     error_log(end($this->errors));
53   }
54
55   function error ()
56   {
57     if (empty($this->trace)) {
58       return NULL;
59     } else {
60       $last_trace = end($this->trace);
61       return implode("\\n", $last_trace['errors']);
62     }
63   }
64
65   function trace ()
66   {
67     return $this->trace;
68   }
69
70   function microtime_float()
71   {
72     list($usec, $sec) = explode(" ", microtime());
73     return ((float) $usec + (float) $sec);
74   }
75
76   function call($method, $args = NULL)
77   {
78     if ($this->multicall) {
79         $this->calls[] = array ('methodName' => $method,
80                                 'params' => $args);
81         return NULL;
82     } else {
83         return $this->internal_call ($method, $args, 3);
84     }
85   }
86
87   function internal_call ($method, $args = NULL, $backtrace_level = 2)
88   {
89     $curl = curl_init();
90
91     // Verify peer certificate if talking over SSL
92     if ($this->port == 443) {
93       curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
94       if (!empty($this->cainfo)) {
95         curl_setopt($curl, CURLOPT_CAINFO, $this->cainfo);
96       } elseif (defined('PLC_API_SSL_CRT')) {
97         curl_setopt($curl, CURLOPT_CAINFO, PLC_API_SSL_CRT);
98       }
99       $url = 'https://';
100     } else {
101       $url = 'http://';
102     }
103
104     // Set the URL for the request
105     $url .= $this->server . ':' . $this->port . '/' . $this->path;
106     curl_setopt($curl, CURLOPT_URL, $url);
107
108     // Marshal the XML-RPC request as a POST variable
109     $request = xmlrpc_encode_request($method, $args);
110     curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
111
112     // Construct the HTTP header
113     $header[] = 'Content-type: text/xml';
114     $header[] = 'Content-length: ' . strlen($request);
115     curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
116
117     // Set some miscellaneous options
118     curl_setopt($curl, CURLOPT_TIMEOUT, 30);
119
120     // Get the output of the request
121     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
122     $t0 = $this->microtime_float();
123     $output = curl_exec($curl);
124     $t1 = $this->microtime_float();
125
126     if (curl_errno($curl)) {
127       $this->error_log('curl: ' . curl_error($curl), true);
128       $ret = NULL;
129     } else {
130       $ret = xmlrpc_decode($output);
131       if (is_array($ret) && xmlrpc_is_fault($ret)) {
132         $this->error_log('Fault Code ' . $ret['faultCode'] . ': ' .
133                          $ret['faultString'], $backtrace_level, true);
134         $ret = NULL;
135       }
136     }
137
138     curl_close($curl);
139
140     $this->trace[] = array('method' => $method,
141                            'args' => $args,
142                            'runtime' => $t1 - $t0,
143                            'return' => $ret,
144                            'errors' => $this->errors);
145     $this->errors = array();
146
147     return $ret;
148   }
149
150   function begin()
151   {
152     if (!empty($this->calls)) {
153       $this->error_log ('Warning: multicall already in progress');
154     }
155
156     $this->multicall = true;
157   }
158
159   function commit ()
160   {
161     if (!empty ($this->calls)) {
162       $ret = array();
163       $results = $this->internal_call ('system.multicall', array ($this->calls));
164       foreach ($results as $result) {
165         if (is_array($result)) {
166           if (xmlrpc_is_fault($result)) {
167             $this->error_log('Fault Code ' . $result['faultCode'] . ': ' .
168                              $result['faultString'], 1, true);
169             $ret[] = NULL;
170           } else {
171             $ret[] = $result[0];
172           }
173         } else {
174           $ret[] = $result;
175         }
176       }
177     } else {
178       $ret = NULL;
179     }
180
181     $this->calls = array ();
182     $this->multicall = false;
183
184     return $ret;
185   }
186
187   //
188   // PLCAPI Methods
189   //
190