1 |
<?php |
2 |
/* |
3 |
* PHP XenAPI v1.0 |
4 |
* a class for XenServer API calls |
5 |
* |
6 |
* Copyright (C) 2010 Andy Goodwin <andyg@unf.net> |
7 |
* |
8 |
* This class requires xml-rpc, PHP5, and curl. |
9 |
* |
10 |
* Permission is hereby granted, free of charge, to any person obtaining |
11 |
* a copy of this software and associated documentation files (the |
12 |
* "Software"), to deal in the Software without restriction, including |
13 |
* without limitation the rights to use, copy, modify, merge, publish, |
14 |
* distribute, sublicense, and/or sell copies of the Software, and to |
15 |
* permit persons to whom the Software is furnished to do so, subject to |
16 |
* the following conditions: |
17 |
* |
18 |
* The above copyright notice and this permission notice shall be included |
19 |
* in all copies or substantial portions of the Software. |
20 |
* |
21 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
22 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
23 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
24 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
25 |
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
26 |
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
27 |
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
28 |
* |
29 |
*/ |
30 |
|
31 |
class XenApi { |
32 |
private $_url; |
33 |
|
34 |
private $_session_id; |
35 |
private $_user; |
36 |
private $_password; |
37 |
|
38 |
function __construct ($url, $user, $password) { |
39 |
$r = $this->xenrpc_request($url, $this->xenrpc_method('session.login_with_password', array($user, $password, '1.3'))); |
40 |
if (is_array($r) && $r['Status'] == 'Success') { |
41 |
$this->_session_id = $r['Value']; |
42 |
$this->_url = $url; |
43 |
$this->_user = $user; |
44 |
$this->_password = $password; |
45 |
} else { |
46 |
echo "API failure. (" . implode(' ', $r['ErrorDescription']) . ")\n"; exit; |
47 |
} |
48 |
} |
49 |
|
50 |
function __call($name, $args) { |
51 |
if (!is_array($args)) { |
52 |
$args = array(); |
53 |
} |
54 |
list($mod, $method) = explode('__', $name, 2); |
55 |
$ret = $this->xenrpc_parseresponse($this->xenrpc_request($this->_url, |
56 |
$this->xenrpc_method($mod . '.' . $method, array_merge(array($this->_session_id), $args)))); |
57 |
return $ret; |
58 |
} |
59 |
|
60 |
function get_id() { |
61 |
return $this->_session_id; |
62 |
} |
63 |
|
64 |
function xenrpc_parseresponse($response) { |
65 |
if (!@is_array($response) && !@$response['Status']) { |
66 |
echo "API failure. (500)\n"; exit; |
67 |
} else { |
68 |
if ($response['Status'] == 'Success') { |
69 |
$ret = $response['Value']; |
70 |
} else { |
71 |
if ($response['ErrorDescription'][0] == 'SESSION_INVALID') { |
72 |
$r = $this->xenrpc_request($url, $this->xenrpc_method('session.login_with_password', |
73 |
array($this->_user, $this->_password, '1.3'))); |
74 |
if (!is_array($r) && $r['Status'] == 'Success') { |
75 |
$this->_session_id = $r['Value']; |
76 |
} else { |
77 |
echo "API failure. (session)\n"; exit; |
78 |
} |
79 |
} else { |
80 |
echo "API failure. (" . implode(' ', $response['ErrorDescription']) . ")\n"; exit; |
81 |
} |
82 |
} |
83 |
} |
84 |
return $ret; |
85 |
} |
86 |
|
87 |
function xenrpc_method($name, $params) { |
88 |
$ret = xmlrpc_encode_request($name, $params); |
89 |
|
90 |
return $ret; |
91 |
} |
92 |
|
93 |
function xenrpc_request($url, $req) { |
94 |
$headers = array('Content-type: text/xml', 'Content-length: ' . strlen($req)); |
95 |
|
96 |
$ch = curl_init($url); |
97 |
|
98 |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); |
99 |
|
100 |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); |
101 |
curl_setopt($ch, CURLOPT_TIMEOUT, 60); |
102 |
|
103 |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
104 |
|
105 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
106 |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); |
107 |
|
108 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
109 |
|
110 |
curl_setopt($ch, CURLOPT_POSTFIELDS, $req); |
111 |
|
112 |
$resp = curl_exec($ch); |
113 |
curl_close($ch); |
114 |
|
115 |
$ret = xmlrpc_decode($resp); |
116 |
|
117 |
return $ret; |
118 |
} |
119 |
} |