-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebuzoApi.php
More file actions
314 lines (249 loc) · 9 KB
/
WebuzoApi.php
File metadata and controls
314 lines (249 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/**
* Provides an interface to issue commands to a remote WHM server.
* @link https://webuzo.com/docs/api/
* @author Brijesh Kothari
* @version 1.0
*/
class WebuzoApi
{
protected $host;
protected $username;
protected $apikey;
protected $ssl = false;
var $port;
var $schema;
var $type = 'json';
var $result;
var $request;
var $url;
/**
* Let's start her up!
* @param string $host Host name of server
* @param string $username Username with Webuzo admin privileges
* @param string $apikey API Key
* @param boolean $ssl Use an SSL connection
* @param string $output Output type
*/
public function __construct($host, $username, $apikey, $ssl = false, $output = 'json')
{
$this->host = $host;
$this->username = $username;
$this->apikey = $apikey;
$this->ssl = $ssl;
$this->port = ( $ssl == true ) ? 2005 : 2004;
$this->enduser_port = ( $ssl == true ) ? 2003 : 2002;
$this->schema = ( $ssl == true ) ? 'https://' : 'http://';
$this->output = 'json';
}
/**
* Makes a request through the Webuzo API (Admin)
* @link https://webuzo.com/docs/api/
* @param string $act
* @param array $params
* @return boolean
*/
function call($act, $params = array(), $cookies = array()){
global $webuzo_conf;
$this->url = $this->schema.$this->host.':'.$this->port.'/index.php?act='.$act.'&api='.$this->output.'&apiuser='.$this->username.'&apikey='.rawurlencode($this->apikey).'&skip_callback=1';
// Set the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
// Time OUT
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
// Turn off the server and peer verification (TrustManager Concept).
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// UserAgent
curl_setopt($ch, CURLOPT_USERAGENT, 'Softaculous');
// Cookies
if(!empty($cookies)){
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIE, http_build_query($cookies, '', '; '));
}
if(!empty($params)){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Get response from the server.
$data = curl_exec($ch);
if ($data === false) {
$errorDesc = curl_error($ch);
if ($errorDesc != '') {
$error = "Webuzo API Request (".$act.") / cURL Error: ". $errorDesc.' - URL : '.$this->url;
} else {
$errorNumber = curl_errno($ch);
if ($errorNumber == 3) {
$error = "Webuzo API Request (".$act.") / cURL Error: Server Hostname is not valid.";
}
}
CE_Lib::log(1, $error);
throw new Exception($error);
}
$data = str_replace('“', "'", $data);
$data = str_replace('”', "'", $data);
// As a security prevention measure - Though this cannot happen
$data = str_replace($pass, '12345678901234567890123456789012', $data);
if($this->output == 'serialize'){
$result = $this->result = unserialize($data);
}else{
$result = $this->result = json_decode(utf8_encode($data), true);
}
$this->request = array ( 'url' => $this->url, 'act' => $act, 'params' => $params, 'raw' => $data);
$this->verbose_request = array ( 'url' => $this->url, 'act' => $act, 'params' => $params, 'raw' => $data, 'json' => $result);
curl_close($ch);
// The following line is a method to test
//if(preg_match('/sync/is', $url)) echo $data;
if(empty($data)){
return false;
}
// As a security prevention measure - Though this cannot happen
$data = str_replace($pass, '12345678901234567890123456789012', $data);
if($this->output == 'serialize'){
$result = unserialize($data);
}else{
$result = json_decode($data, true);
}
CE_Lib::log(4, 'Webuzo API Request: '.print_r($this->request, true));
CE_Lib::log(5, 'Webuzo API Request: '.print_r($this->verbose_request, true));
if(!is_array($result)){
throw new Exception("Failed to connect to Webuzo Server");
}elseif(isset($result['error'])) {
throw new CE_Exception("Webuzo returned an error: ".implode('<br />', $result['error']));
}
return $result;
}
/**
* Makes a request through the Webuzo API (Enduser)
* @link https://webuzo.com/docs/api/
* @param string $act
* @param array $params
* @return boolean
*/
function enduser_call($act, $params = array(), $cookies = array()){
global $webuzo_conf;
$this->url = $this->schema.$this->host.':'.$this->enduser_port.'/index.php?act='.$act.'&api='.$this->output.'&apiuser='.$this->username.'&apikey='.rawurlencode($this->apikey).'&skip_callback=1';
// Set the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
// Time OUT
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
// Turn off the server and peer verification (TrustManager Concept).
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// UserAgent
curl_setopt($ch, CURLOPT_USERAGENT, 'Softaculous');
// Cookies
if(!empty($cookies)){
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIE, http_build_query($cookies, '', '; '));
}
if(!empty($params)){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Get response from the server.
$data = curl_exec($ch);
if ($data === false) {
$errorDesc = curl_error($ch);
if ($errorDesc != '') {
$error = "Webuzo API Enduser Request (".$act.") / cURL Error: ". $errorDesc;
} else {
$errorNumber = curl_errno($ch);
if ($errorNumber == 3) {
$error = "Webuzo API Enduser Request (".$act.") / cURL Error: Server Hostname is not valid.";
}
}
CE_Lib::log(1, $error);
throw new Exception($error);
}
$data = str_replace('“', "'", $data);
$data = str_replace('”', "'", $data);
// As a security prevention measure - Though this cannot happen
$data = str_replace($pass, '12345678901234567890123456789012', $data);
if($this->output == 'serialize'){
$result = $this->result = unserialize($data);
}else{
$result = $this->result = json_decode(utf8_encode($data), true);
}
$this->request = array ( 'url' => $this->url, 'act' => $act, 'params' => $params, 'raw' => $data);
$this->verbose_request = array ( 'url' => $this->url, 'act' => $act, 'params' => $params, 'raw' => $data, 'json' => $result);
curl_close($ch);
// The following line is a method to test
//if(preg_match('/sync/is', $url)) echo $data;
if(empty($data)){
return false;
}
// As a security prevention measure - Though this cannot happen
$data = str_replace($pass, '12345678901234567890123456789012', $data);
if($this->output == 'serialize'){
$result = unserialize($data);
}else{
$result = json_decode($data, true);
}
CE_Lib::log(4, 'Webuzo API Enduser Request : '.print_r($this->request, true));
CE_Lib::log(5, 'Webuzo API Enduser Request : '.print_r($this->verbose_request, true));
if(!is_array($result)){
throw new Exception("Failed to connect to Webuzo Server");
}elseif(isset($result['error'])) {
throw new CE_Exception("Webuzo returned an error: ".implode('<br />', $result['error']));
}
return $result;
}
/**
* Gets plans/packages
* @return Array of packages (key = package name, index = package array)
*/
public function packages()
{
$result = $this->call('plans');
$packages = array();
if(!empty($result['plans'])){
$packages = $result['plans'];
}
return $packages;
}
/**
* Gets all the accounts
* @return Array of accounts (key = account username, index = account array)
*/
public function accounts()
{
$result = $this->call('users');
$accounts = array();
if(!empty($result['users'])){
$accounts = $result['users'];
}
return $accounts;
}
/**
* Gets all suspended accounts
*/
public function suspended()
{
$result = $this->call('users');
$accounts = array();
if(!empty($result['users'])){
foreach ($result['users'] as $a) {
if(!empty($a['status']) && $a['status'] == 'suspended'){
$accounts[$a['user']] = $a;
}
}
}
return $accounts;
}
/**
* Gets the current Webuzo version on the server.
* @return string Current Webuzo version
*/
public function version()
{
$result = $this->call();
if(!empty($response['version'])){
return $response['version'];
}
return '';
}
}