-
Notifications
You must be signed in to change notification settings - Fork 3
/
enduser.php
694 lines (506 loc) · 16.6 KB
/
enduser.php
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
<?php
//////////////////////////////////////////////////////////////
//===========================================================
// enduser.php (API)
//===========================================================
// SOFTACULOUS VIRTUALIZOR
// Version : 1.0
// Inspired by the DESIRE to be the BEST OF ALL
// ----------------------------------------------------------
// Started by: Alons
// Date: 8th Mar 2010
// Time: 23:00 hrs
// Site: http://www.virtualizor.com/ (SOFTACULOUS VIRTUALIZOR)
// ----------------------------------------------------------
// Please Read the Terms of use at http://www.virtualizor.com
// ----------------------------------------------------------
//===========================================================
// (c)Softaculous Ltd.
//===========================================================
//////////////////////////////////////////////////////////////
class Virtualizor_Enduser_API {
var $key = '';
var $pass = '';
var $ip = '';
var $port = 4083;
var $protocol = 'https';
var $error = array();
/**
* Contructor
*
* @author Pulkit Gupta
* @param string $ip IP of the Control Panel
* @param string $key The API KEY of your account
* @param string $pass The API Password of your account
* @param int $port (Optional) The port to connect to. Port 4083 is the default. 4082 is non-SSL
* @return NULL
*/
function Virtualizor_Enduser_API($ip, $key, $pass, $port = 4083){
$this->key = $key;
$this->pass = $pass;
$this->ip = $ip;
$this->port = $port;
if(!($port == 4083 || $port == 443)){
$this->protocol = 'http';
}
}
/**
* Dumps a variable
*
* @author Pulkit Gupta
* @param array $re The Array or any other variable.
* @return NULL
*/
function r($re){
echo '<pre>';
print_r($re);
echo '</pre>';
}
/**
* Unserializes a string
*
* @author Pulkit Gupta
* @param string $str The serialized string
* @return array The unserialized array on success OR false on failure
*/
function _unserialize($str){
$var = @unserialize($str);
if(empty($var)){
$str = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'._strlen('$2').':\"$2\";'", $str);
$var = @unserialize($str);
}
//If it is still empty false
if(empty($var)){
return false;
}else{
return $var;
}
}
/**
* Makes an API request to the server to do a particular task
*
* @author Pulkit Gupta
* @param string $path The action you want to do
* @param array $post An array of DATA that should be posted
* @param array $cookies An array FOR SENDING COOKIES
* @return array The unserialized array on success OR false on failure
*/
function call($path, $post = array(), $cookies = array()){
$url = ($this->protocol).'://'.$this->ip.':'.$this->port.'/'.$path;
$url .= (strstr($url, '?') ? '' : '?');
$url .= '&api=serialize&apikey='.rawurlencode($this->key).'&apipass='.rawurlencode($this->pass);
// Set the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Time OUT
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
// 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, 'Virtualizor');
// Cookies
if(!empty($cookies)){
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIE, http_build_query($cookies, '', '; '));
}
if(!empty($post)){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Get response from the server.
$resp = curl_exec($ch);
curl_close($ch);
// The following line is a method to test
//if(preg_match('/sync/is', $url)) echo $resp;
if(empty($resp)){
return false;
}
$r = $this->_unserialize($resp);
if(empty($r)){
return false;
}
return $r;
}
/**
* List the Virtual Servers in your account
*
* @author Pulkit Gupta
* @return array The array containing a list of Virtual Servers one has in their account
*/
function listvs(){
$resp = $this->call('index.php?act=listvs');
return $resp['vs'];
}
/**
* START a Virtual Server
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @return bool TRUE on success or FALSE on failure
*/
function start($vid){
// Make the Request
$res = $this->call('index.php?svs='.$vid.'&act=start&do=1');
// Did it finish ?
if(!empty($res['done'])){
return true;
}else{
return false;
}
}
/**
* STOP a Virtual Server
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @return bool TRUE on success or FALSE on failure
*/
function stop($vid){
// Make the Request
$res = $this->call('index.php?svs='.$vid.'&act=stop&do=1');
// Did it finish ?
if(!empty($res['done'])){
return true;
}else{
return false;
}
}
/**
* RESTART a Virtual Server
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @return bool TRUE on success or FALSE on failure
*/
function restart($vid){
// Make the Request
$res = $this->call('index.php?svs='.$vid.'&act=restart&do=1');
// Did it finish ?
if(!empty($res['done'])){
return true;
}else{
return false;
}
}
/**
* POWER OFF a Virtual Server
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @return bool TRUE on success or FALSE on failure
*/
function poweroff($vid){
// Make the Request
$res = $this->call('index.php?svs='.$vid.'&act=poweroff&do=1');
// Did it finish ?
if(!empty($res['done'])){
return true;
}else{
return false;
}
}
/**
* STOP a Virtual Server
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @return int 1 if the VM is ON, 0 if its OFF
*/
function status($vid){
// Make the Request
$res = $this->call('index.php?svs='.$vid.'&act=start');
return $res['status'];
}
/**
* GET or SET the hostname of a VM. To get the current hostname dont pass the $newhostname parameter
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @param string $newhostname The new HOSTNAME of the virtual server.
* @return string The CURRENT hostname is returned if $newhostname is NULL.
* FALSE is returned if there was an error while setting the new hostname
* 'onboot' is returned if the new hostname will be set when the VPS is STOPPED and STARTED
* 'done' is returned if the new hostname has been set right now - Mainly OpenVZ
*/
function hostname($vid, $newhostname = NULL){
// Are we to change ?
if(!empty($newhostname)){
$post = array('newhost' => $newhostname,
'changehost' => 'Change Hostname');
$resp = $this->call('index.php?svs='.$vid.'&act=hostname', $post);
// Was there an error
if(!empty($resp['error'])){
$this->error = $resp['error'];
return false;
// Will it be done when the VPS is STOPPED and STARTED ?
}elseif(!empty($resp['onboot'])){
return 'onboot';
// It was done successfully
}elseif(!empty($resp['done'])){
return 'done';
}
// Just return the CURRENT HOSTNAME
}else{
$resp = $this->call('index.php?svs='.$vid.'&act=hostname');
return $resp['current'];
}
}
/**
* GET the CPU details of a VM. Incase of Xen / KVM, only information is available as usage cannot be sensed.
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @return array An array containing the details is returned. Usage details is available only in case of OpenVZ.
*/
function cpu($vid){
$resp = $this->call('index.php?svs='.$vid.'&act=cpu');
return $resp['cpu'];
}
/**
* GET the RAM details of a VM. Incase of Xen / KVM, only information is available as usage cannot be sensed.
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @return array An array containing the details is returned. Usage details is available only in case of OpenVZ.
*/
function ram($vid){
$resp = $this->call('index.php?svs='.$vid.'&act=ram');
return $resp['ram'];
}
/**
* GET the Disk details of a VM. Incase of Xen / KVM, only information is available as usage cannot be sensed.
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @return array An array containing the details is returned. Usage details is available only in case of OpenVZ.
*/
function disk($vid){
$resp = $this->call('index.php?svs='.$vid.'&act=disk');
$ret['disk'] = $resp['disk'];
$ret['inodes'] = $resp['inodes'];
return $ret;
}
/**
* GET the Bandwidth Usage of a VM.
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @param int $month The month in the format YYYYMM e.g. 201205 is for the Month of May, 2012
* @return array Returns an array of Bandwidth Information for the Month GIVEN.
* By Default the CURRENT MONTH details are returned
*/
function bandwidth($vid, $month = 0){
$resp = $this->call('index.php?svs='.$vid.'&act=bandwidth'.(!empty($month) ? '&show='.$month : ''));
return $resp['bandwidth'];
}
/**
* List the Processes in a VPS - Only OpenVZ
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @return array An array containing all the processes is returned
*/
function processes($vid){
$resp = $this->call('index.php?svs='.$vid.'&act=processes');
return $resp['processes'];
}
/**
* List the Services in a VPS - Only OpenVZ
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @return array An array containing all the services is returned
*/
function services($vid){
$resp = $this->call('index.php?svs='.$vid.'&act=services');
$ret['services'] = $resp['services'];
$ret['autostart'] = $resp['autostart'];
$ret['running'] = $resp['running'];
return $ret;
}
/**
* Changes the root password of a VPS
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @param string $pass The new password to set
* @return string FALSE is returned if there was an error while setting the new password
* 'onboot' is returned if the new password will be set when the VPS is STOPPED and STARTED
* 'done' is returned if the new password has been set right now - Mainly OpenVZ
*/
function changepassword($vid, $pass) {
$post = array('newpass' => $pass,
'conf' => $pass,
'changepass' => 'Change Password'
);
$resp = $this->call('index.php?svs='.$vid.'&act=changepassword', $post);
// Was there an error
if(!empty($resp['error'])){
$this->error = $resp['error'];
return false;
// Will it be done when the VPS is STOPPED and STARTED ?
}elseif(!empty($resp['onboot'])){
return 'onboot';
// It was done successfully
}elseif(!empty($resp['done'])){
return 'done';
}
}
/**
* Get the VNC Details like PORT, IP, VNC Password. Only available in case of Xen and KVM VPS if VNC is enabled.
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @return array An array containing all the VNC Details
*/
function vnc($vid){
$resp = $this->call('index.php?svs='.$vid.'&act=vnc');
return $resp['info'];
}
/**
* Change the VNC Password. Only available in case of Xen and KVM VPS if VNC is enabled.
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @param string $pass The new password to set
* @return string FALSE is returned if there was an error while setting the new password
* 'onboot' is returned if the new password will be set when the VPS is STOPPED and STARTED
*/
function vncpass($vid, $pass) {
$post = array('newpass' => $pass,
'conf' => $pass,
'vncpass' => 'Change Password'
);
$resp = $this->call('index.php?svs='.$vid.'&act=vncpass', $post);
// Was there an error
if(!empty($resp['error'])){
$this->error = $resp['error'];
return false;
// Will it be done when the VPS is STOPPED and STARTED ?
}elseif(!empty($resp['onboot']) || !empty($resp['done'])){
return 'onboot';
}
}
/**
* Re-installs a VPS if the $newosid is specified. If the $newosid is not passed,
* then this function will return an array of available templates.
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @param int $newosid The Operating System ID (you got from the list) that will be installed on the VPS.
* @param string $newpass The new root password to set
* @return string FALSE is returned if there was an error while setting the new password
* string 'onboot' is returned if the new password will be set when the VPS is STOPPED and STARTED
* string 'done' is returned if the new password has been set right now - Mainly OpenVZ
* array An array of the list of avvailable OS Templates is returned if $newosid is NULL
*/
function ostemplate($vid, $newosid = NULL, $newpass = NULL){
// Get the list of OS Templates
$resp = $this->call('index.php?svs='.$vid.'&act=ostemplate');
// Get a list of Virtual Servers
$listvs = $this->listvs();
// Is there such a VPS ?
if(!empty($listvs[$vid])){
$resp = $resp['oslist'][$listvs[$vid]['virt']];
// No such VPS. Return an EMPTY ARRAY
}else{
return array();
}
if(!empty($newosid)){
// The POST Vars
$post = array('newos' => $newosid,
'newpass' => $newpass,
'conf' => $newpass,
'reinsos' => 'Reinstall');
$resp = $this->call('index.php?svs='.$vid.'&act=ostemplate', $post);
// Was there an error
if(!empty($resp['error'])){
$this->error = $resp['error'];
return false;
// Will it be done when the VPS is STOPPED and STARTED ?
}elseif(!empty($resp['onboot'])){
return 'onboot';
// It was done successfully
}elseif(!empty($resp['done'])){
return 'done';
}
// Just return the OS List
}else{
return $resp;
}
}
/**
* Install a Control Panel
*
* @author Pulkit Gupta
* @param int $vid The VMs ID
* @param string $panel The Name of the Panel you want to install. Options - cpanel, plesk, webuzo, kloxo, webmin
* @return string FALSE is returned if there was an error while installing the control panel
* 'onboot' is returned if the control panel will be installed when the VPS is STOPPED and STARTED
* 'done' is returned if the control panel has been installed right now - Mainly OpenVZ
*/
function controlpanel($vid, $panel){
$post['ins'][$panel] = 1;
$resp = $this->call('index.php?svs='.$vid.'&act=controlpanel', $post);
// Was there an error
if(!empty($resp['error'])){
$this->error = $resp['error'];
return false;
// Will it be done when the VPS is STOPPED and STARTED ?
}elseif(!empty($resp['onboot'])){
return 'onboot';
// It was done successfully
}elseif(!empty($resp['done'])){
return 'done';
}
}
}
//////////////
// Examples
//////////////
//$v = new Virtualizor_Enduser_API('127.0.0.1', '16_BIT_API_KEY', '32_BIT_API_PASS');
// Get the list of the VPS
//$v->r($v->listvs());
// Start a VPS
//echo $v->start(3);
// Stop a VPS
//echo $v->stop(3);
// Restart a VPS
//echo $v->restart(3);
// Poweroff a VPS
//echo $v->poweroff(3);
// Get the Status of a VPS
//echo $v->status(3);
// Get the Hostname
//echo $v->hostname(4);
// Change the Hostname
//$v->r($v->hostname(4, 'NEWHOSTNAME'));
// CPU Details
//$v->r($v->cpu(4));
// Ram Details
//$v->r($v->ram(4));
// Disk Details
//$v->r($v->disk(4));
// Bandwidth Details for the Current Month
//$v->r($v->bandwidth(4));
// Bandwidth Details for the Month of May in 2012
//$v->r($v->bandwidth(4, 201205));
// List the processes - OpenVZ only
//$v->r($v->processes(4));
// List the services - OpenVZ only
//$v->r($v->services(4));
// Change the Root Password of a Virtual Server ?
//$v->r($v->changepassword(4, 'test'));
// Give the VNC Details - VNC must be enabled - Xen / KVM
//$v->r($v->vnc(4));
// Change the VNC Password - VNC must be enabled - Xen / KVM
//$v->r($v->vncpass(4, 'NEWpass'));
// List available OS Templates
//$v->r($v->ostemplate(2));
// Reinstall the OS
//$v->r($v->ostemplate(4, 1, 'test'));
// Install a Control Panel
//$v->r($v->controlpanel(4, 'cpanel'));
?>