diff --git a/.env-example b/.env-example new file mode 100644 index 0000000..d7e1bbb --- /dev/null +++ b/.env-example @@ -0,0 +1,5 @@ +SERVER_IP= +SERVER_PROTOCOL= #http | https +SERVER_PORT= +SERVER_KEY= #Get from side menu >Configuration>server info +SERVER_PASS= #Get from side menu >Configuration>server info \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..607642e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/vendor/ +.env +composer.lock \ No newline at end of file diff --git a/README.md b/README.md index 49a83b3..dd76311 100644 --- a/README.md +++ b/README.md @@ -1 +1,117 @@ -# Client_SDK \ No newline at end of file +# Admin virtualizor SDK + +Classified virtualizor admin sdk based on [APIStorm](https://github.com/yiiman-dev/apistorm) package + +# what is different? + +In this package, We have classified all the methods so that all the feedback is understandable for popular IDEs. + +## Usage + +for samples please clone package and install composer vendors + +then create `.env` file from `.env-example` file and fill your server data + +now execute ``index.php`` sample file in command line: + +`php index.php` + +# Composer install + +`composer require yiiman/virtualizor` + +## Methods list + +| method | classified | +| ----------------------------------------------------- | ----------- | +| addippool | ✓ | +| addips | ✗ | +| addiso | ✗ | +| addplan | ✗ | +| addserver | ✗ | +| addtemplate | ✗ | +| adduser | ✗ | +| addvs | ✓ | +| addiprange | ✗ | +| addsg | ✗ | +| addbackupserver | ✗ | +| addstorage | ✗ | +| adddnsplan | ✗ | +| add_admin_acl | ✗ | +| addmg | ✗ | +| add_distro | ✗ | +| addpdns | ✗ | +| adminindex | ✗ | +| apidoings | ✗ | +| backup | ✗ | +| bandwidth | ✗ | +| cluster | ✗ | +| config | ✗ | +| cpu | ✗ | +| createssl | ✗ | +| createtemplate | ✗ | +| databackup | ✗ | +| defaultvsconf | ✗ | +| delete_vs | ✓ | +| disk | ✗ | +| downloadiso | ✗ | +| editemailtemp | ✗ | +| editippool | ✗ | +| editips | ✗ | +| editplan | ✗ | +| editserver | ✗ | +| edittemplate | ✗ | +| edituser | ✓ | +| managevps | ✓ | +| editvs | ✗ | +| emailconfig | ✗ | +| emailtemp | ✗ | +| filemanager | ✗ | +| firewall | ✗ | +| giveos | ✗ | +| health | ✗ | +| hostname | ✗ | +| import | ✗ | +| ippool | ✗ | +| ips | ✓ | +| iso | ✗ | +| kernelconf | ✗ | +| license | ✗ | +| vps_list | ✓ | +| login | ✗ | +| loginlogs | ✗ | +| logs | ✗ | +| maintenance | ✗ | +| makeslave | ✗ | +| os | ✗ | +| ostemplates | ✓ | +| performance | ✗ | +| phpmyadmin | ✗ | +| plans | ✓ | +| poweroff | ✗ | +| processes | ✗ | +| ram | ✗ | +| stat | ✓ | +| rebuild | ✓ | +| restart | ✓ | +| restartservices | ✗ | +| serverinfo | ✗ | +| servers | ✓ | +| services | ✗ | +| start | ✓ | +| stop | ✓ | +| status | ✓ | +| suspend | ✓ | +| unsuspend | ✓ | +| ubc | ✗ | +| updates | ✗ | +| userlogs | ✗ | +| userSuspend | ✓ | +| userUnsuspend | ✓ | +| users | ✓ | +| vnc | ✗ | +| vpsbackupsettings | ✗ | +| vpsbackups | ✗ | +| vsbandwidth | ✗ | +| vscpu | ✗ | +| vsram | ✗ | diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..25a39be --- /dev/null +++ b/composer.json @@ -0,0 +1,28 @@ +{ + "name": "yiiman/virtualizor", + "description": "Tiny SDK for virtualizor", + "type": "library", + "license": "Apache-2.0", + "autoload": { + "psr-4": { + "YiiMan\\VirtualizorSdk\\": "src/" + } + }, + "authors": [ + { + "name": "YiiMan", + "email": "info@yiiman.ir", + "homepage": "https://yiiman.ir" + }, + { + "name": "AriaNet", + "homepage": "https://ariaservice.net", + "email": "develop@ariaservice.net" + } + ], + "minimum-stability": "stable", + "require": { + "yiiman/apistorm": "dev-main", + "vlucas/phpdotenv": "5.4.x-dev" + } +} diff --git a/enduser.php b/enduser.php deleted file mode 100644 index d7e97e3..0000000 --- a/enduser.php +++ /dev/null @@ -1,694 +0,0 @@ -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 '
';
-		print_r($re);
-		echo '
'; - } - - /** - * 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')); - -?> \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..14628f4 --- /dev/null +++ b/index.php @@ -0,0 +1,166 @@ + +{ + $dotenv = Dotenv\Dotenv::createImmutable(__DIR__); + $dotenv->load(); +} +// + + +// < Initialize SDK > +{ + $virtualizor = new VirtualizorAdmin(); + $virtualizor->Virtualizor_Admin_API + ( + $_ENV['SERVER_IP'], + $_ENV['SERVER_KEY'], + $_ENV['SERVER_PASS'], + $_ENV['SERVER_PORT'], + $_ENV['SERVER_PROTOCOL'] + ); + define('NEW_LINE',"\n\n\n\n\n") ; +} +// + + +echo 'Started API test'; +$u = $virtualizor->users(); +if ($u->isSuccess()) { + $u = $u->users[0]; + $userID = $u->uid; + $userEmail = $u->email; + + //create + $servers = $virtualizor->servers();//classified + if ($servers->isSuccess()) { + $plans = $virtualizor->plans();//classified + if ($plans->isSuccess()) { + $osTemplates=$virtualizor->ostemplates()->oslist[0]; + $sv = new CreateVS(); + $sv->virt0 = $sv::VIRTUALIZOR_TYPE_kvm; + $sv->user_email0 = $userEmail; + $sv->rootpass0 = 'YiiMan@@@22'; + $sv->hostname0 = 'yiiman'; + $sv->osid0 = 870; + + $sv->plid0 = (int) $plans->plans[0]->plid; + $sv->node_select0 = $servers->servers[0]->serid; + + + // < Create New VPS after validate data > + { + if ($sv->validate()) { + $created = $virtualizor->addvs($sv, []);//classified + echo 'Create new VPS was '.($created->isSuccess() ? 'Success' : 'not success because:'.$created->getError()->status_title).NEW_LINE; + } + } + // + + + } + + $vpsList = $virtualizor->vps_list();//classified + if ($vpsList->isSuccess()) { + $vpsID = $vpsList->vs[0]->vpsid; + // < Edit User > + { + $editPost = new EditUser(); + $editPost->fname = "YiiMan".uniqid(); + $editPost->newemail0 = 'test@gmail.com'; + if ($editPost->validate()) { + $edit = $virtualizor->edituser($editPost, $userID);//classified + echo 'Edit user was '.($edit->isSuccess() ? 'Success' : 'not success because:'.$edit->getError()->status_title).NEW_LINE; + + } else { + echo 'Edit User: your data has some errors <'.implode(',', $editPost->errors()).'>'.NEW_LINE; + } + } + // + + if (!empty($servers)) { + //suspend + $suspend = $virtualizor->userSuspend($userID);//classified + echo 'user Suspend was '.($suspend->isSuccess() ? 'Success' : 'not success because:'.$suspend->getError()->status_title).NEW_LINE; + + + //unsuspend + $unsuspend = $virtualizor->userUnsuspend($userID);//classified + echo 'user UnSuspend was '.($unsuspend->isSuccess() ? 'Success' : 'not success because:'.$unsuspend->getError()->status_title).NEW_LINE; + + //stop + $stopped = $virtualizor->stop($vpsID); + + //start + $start = $virtualizor->start($vpsID); + + //restart + + $sid = $servers->servers[0]->serid; + $restart = $virtualizor->restart($vpsID); + + + //reinstall|rebuild + $sid = $servers->servers[0]->serid; + + $rebuild = new Rebuild(); + $rebuild->osid = 870; + $rebuild = $virtualizor->rebuild($rebuild, $vpsID);//classified + echo 'VPS rebuild was '.($rebuild->isSuccess() ? 'Success' : 'not success because:'.$rebuild->getError()->status_title).NEW_LINE; + + + //checkStatus + $checkStatus = $virtualizor->status([$vpsID]);//classified + if (!$checkStatus->isSuccess()) { + echo 'VPS stats has error:<'.$checkStatus->getError()->status_title.'>('.$checkStatus->getError()->status_description.')'.NEW_LINE; + } else { + echo "VPS Stat: ".NEW_LINE; + var_export($checkStatus); + } + + + //checkUsage + $checkUsage = $virtualizor->stat($vpsID); + if (!$checkUsage->isSuccess()) { + echo 'VPS Usage has error:<'.$checkUsage->getError()->status_title.'>('.$checkUsage->getError()->status_description.')'.NEW_LINE; + } else { + echo "VPS Usage: ".NEW_LINE; + var_export($checkUsage); + } + + + //hasSpace + + + //reset_password + $manage = new ManageVPS(); + $manage->rootpass = 'amidikukuhkuh'; + $resetPassword = $virtualizor->managevps($manage, $vpsID); + + //get_service_details + + + //terminate + $deleted = $virtualizor->delete_vs($vpsID); + + + } + } + + } +} + + +echo NEW_LINE.'finish'.NEW_LINE; diff --git a/src/PostData/CreateVS.php b/src/PostData/CreateVS.php new file mode 100644 index 0000000..34d081a --- /dev/null +++ b/src/PostData/CreateVS.php @@ -0,0 +1,165 @@ + 'integer', + 'virt' => 'string', + 'uid' => 'integer', + 'user_email' => 'string', + 'user_pass' => 'string', + 'fname' => 'string', + 'lname' => 'string', + 'plid' => 'integer', + 'osid' => 'integer', + 'hostname' => 'string', + 'rootpass' => 'string', + 'ips' => 'array', + 'num_ips6' => 'integer', + 'num_ips6_subnet' => 'integer', + 'stid' => 'integer', + 'space' => 'integer', + 'ram' => 'integer', + 'swapram' => 'integer', + 'bandwidth' => 'integer', + 'network_speed' => 'integer', + 'cpu' => 'integer', + 'cores' => 'integer', + 'cpu_percent' => 'integer', + 'vnc' => 'integer', + 'vncpass' => 'string', + 'kvm_cache' => 'string', + 'io_mode' => 'string', + 'vnc_keymap' => 'string', + 'nic_type' => 'string', + 'osreinstall_limit' => 'integer', + 'node_select' => 'integer', + 'addvps' => 'integer' + ]; + } + + +} \ No newline at end of file diff --git a/src/PostData/EditUser.php b/src/PostData/EditUser.php new file mode 100644 index 0000000..d3d5470 --- /dev/null +++ b/src/PostData/EditUser.php @@ -0,0 +1,109 @@ + 'int', + 'priority' => 'int', + 'newpass' => 'string', + 'newemail' => 'string', + 'fname' => 'string', + 'lname' => 'string', + 'dnsplan_id' => 'int', + 'acl_id' => 'int', + 'num_vs' => 'int', + 'num_users' => 'int', + 'space' => 'int', + 'ram' => 'int', + 'burst' => 'int', + 'bandwidth' => 'int', + 'cpu' => 'int', + 'cores' => 'int', + 'cpu_percent' => 'int', + 'num_cores' => 'int', + 'num_ipv4' => 'int', + 'num_ipv6_subnet' => 'int', + 'num_ipv6' => 'int', + 'network_speed' => 'int', + 'upload_speed' => 'int', + 'band_suspend' => 'boolean', + 'service_period' => 'int', + 'allowed_virts' => 'array', + 'sgs' => 'array', + 'mgs' => 'array', + ]; + } +} \ No newline at end of file diff --git a/src/PostData/ManageVPS.php b/src/PostData/ManageVPS.php new file mode 100644 index 0000000..a5a9d07 --- /dev/null +++ b/src/PostData/ManageVPS.php @@ -0,0 +1,142 @@ + 'int', + 'rootpass' => 'string', + 'ips' => 'array', + 'hostname' => 'string', + 'ram' => 'int', + 'bandwidth' => 'int', + 'cores' => 'int', + 'cpupin' => 'int', + 'uid' => 'int', + 'network_speed' => 'int', + 'upload_speed' => 'int', + 'hvm' => 'int', + 'io' => 'int', + 'burst' => 'int', + 'cpu_percent' => 'int', + 'acpi' => 'int', + 'apic' => 'int', + 'pae' => 'int', + 'vnc' => 'int', + 'shadow' => 'int', + 'iso' => 'string', + 'boot' => 'string', + 'band_suspend' => 'int', + 'tuntap' => 'int', + 'pv_on_hvm' => 'int', + 'ppp' => 'int', + 'mac_status' => 'int', + 'osreinstall_limit' => 'int', + 'mgs' => 'string', + 'webuzo' => 'string', + 'cpu_mode' => 'string', + 'admin_managed' => 'int', + 'rdp' => 'int', + 'mac' => 'string', + 'notes' => 'string', + 'disable_nw_config' => 'int', + 'topology_sockets' => 'int', + 'topology_cores' => 'int', + 'topology_threads' => 'int', + ]; + } +} \ No newline at end of file diff --git a/src/PostData/Rebuild.php b/src/PostData/Rebuild.php new file mode 100644 index 0000000..7d2624f --- /dev/null +++ b/src/PostData/Rebuild.php @@ -0,0 +1,45 @@ + 'int', + 'osid' => 'int', + 'newpass' => 'string', + 'conf' => 'string', + 'format_primary' => 'bool', + 'eu_send_rebuild_email' => 'bool', + 'reos' => 'int', + ]; + } +} \ No newline at end of file diff --git a/src/PostData/Space.php b/src/PostData/Space.php new file mode 100644 index 0000000..abe8029 --- /dev/null +++ b/src/PostData/Space.php @@ -0,0 +1,12 @@ + $osData) { + $out[$osID] = new Os($osData); + } + return $out; + } +} \ No newline at end of file diff --git a/src/Responses/Admin/OsTemplate.php b/src/Responses/Admin/OsTemplate.php new file mode 100644 index 0000000..5953acc --- /dev/null +++ b/src/Responses/Admin/OsTemplate.php @@ -0,0 +1,42 @@ + $osData) { + $out[$osName] = new OsName($osData); + } + return $out; + } +} \ No newline at end of file diff --git a/src/VirtualizorAdmin.php b/src/VirtualizorAdmin.php new file mode 100644 index 0000000..34235d8 --- /dev/null +++ b/src/VirtualizorAdmin.php @@ -0,0 +1,1215 @@ +key = $key; + $this->pass = $pass; + $this->ip = $ip; + $this->port = $port; + $this->protocol = $prot; + if ($port != 4085) { + $this->protocol = 'http'; + } + } + + /** + * Dumps a variable + * @author Pulkit Gupta + * @param array $re The Array or any other variable. + * @return NULL + */ + function r($re) + { + echo '
';
+        print_r($re);
+        echo '
'; + } + + /** + * 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; + + } + + } + + /** + * Make an API Key + * @author Pulkit Gupta + * @param string $pass The API Password of your NODE + * @param string $key An 8 bit random string + * @return string The new APIKEY which will be used to query + */ + function make_apikey($key, $pass) + { + return $key.md5($pass.$key); + } + + /** + * Generates a random string for the given length + * @author Pulkit Gupta + * @param int $length The length of the random string to be generated + * @return string The generated random string + */ + function generateRandStr($length) + { + $randstr = ""; + for ($i = 0; $i < $length; $i++) { + $randnum = mt_rand(0, 61); + if ($randnum < 10) { + $randstr .= chr($randnum + 48); + } elseif ($randnum < 36) { + $randstr .= chr($randnum + 55); + } else { + $randstr .= chr($randnum + 61); + } + } + return strtolower($randstr); + } + + /** + * Makes an API request to the server to do a particular task + * @author Pulkit Gupta + * @param array $post An array of DATA that should be posted + * @param array $cookies An array FOR SENDING COOKIES + * @param string $path The action you want to do + * @return array The unserialized array on success OR false on failure + */ + function call($path, $data = [], $post = [], $cookies = []) + { + + $key = $this->generateRandStr(8); + $apikey = $this->make_apikey($key, $this->pass); + + $url = ($this->protocol).'://'.$this->ip.':'.$this->port.'/'.$path; + $url .= (strstr($url, '?') ? '' : '?'); + $url .= '&api=serialize&apikey='.rawurlencode($apikey); + + // Pass some data if there + if (!empty($data)) { + $url .= '&apidata='.rawurlencode(base64_encode(serialize($data))); + } + // 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, 'Softaculous'); + + // 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 = @unserialize($resp); + + if (empty($r)) { + return false; + } + + return $r; + + } + + /** + * Create a VPS + * @author Pulkit Gupta + * @param array $post An array of DATA that should be posted + * @param array $cookies An array FOR SENDING COOKIES + * @param string $path The action you want to do + * @return array The unserialized array on success OR false on failure + */ + function addippool($post) + { + $post['addippool'] = 1; + $path = 'index.php?act=addippool'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function addips($post) + { + $path = 'index.php?act=addips'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function addiso($post) + { + $path = 'index.php?act=addiso'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function addplan($post) + { + + $path = 'index.php?act=addplan'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function addserver($post) + { + $path = 'index.php?act=addserver'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function addtemplate($post) + { + $path = 'index.php?act=addtemplate'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function adduser($post = 0) + { + $path = 'index.php?act=adduser'; + $ret = $this->call($path, [], $post); + return $ret; + } + + /** + * @param string $path + * @param $data + * @param BasePostData|array $servedData + * @param array $cookies + * @return Res + */ + private function callStorm(string $path, $data = '', $servedData = null, $cookies = []) + { + + $key = $this->generateRandStr(8); + $apikey = $this->make_apikey($key, $this->pass); + + + $path .= '&api=serialize&apikey='.rawurlencode($apikey); + + // Pass some data if there + if (!empty($data)) { + $path .= '&apidata='.rawurlencode(base64_encode(serialize($data))); + } + $connection = new Connection(); + $connection->baseURL = $this->ip; + $connection->port = $this->port; + $connection->protocol = $this->protocol; + $connection->responseType = $connection::CONTENT_TYPE_SERIALIZED; + if (is_object($servedData)) { + $arrayData = $servedData->serve(); + } else { + if (empty($servedData)) { + $arrayData = []; + } else { + $arrayData = $servedData; + } + } + + return $connection->call($path, [], $arrayData, $cookies); + } + + /** + * Create a VPS + * @author Pulkit Gupta + * @param array $cookies An array FOR SENDING COOKIES + * @param CreateVS $fields An array of DATA that should be posted + * @return VS|Res|bool + */ + function addvs(CreateVS $fields, array $cookies = []) + { + $path = 'index.php?act=addvs'; + if ($fields->validate()) { + + $ret = $this->callStorm($path, 'addvs', $fields, $cookies); + if ($ret->isSuccess()) { + $data = $ret->getData(); + if (!empty($data['error'])) { + $ret->setError(500, $data['error']); + return $ret; + } else { + return new AddVs($ret); + } + } else { + return $ret; + } +// return [ +// 'title' => $ret->getData()['title'], +// 'error' => @empty($ret->getData()['error']) ? [] : $ret->getData()['error'], +// 'vs_info' => !empty($ret->getData()['newvs']) ? new VS($ret->getData()['newvs']) : $ret->getData()['newvs'], +// 'globals' => new GlobalResponse($ret['globals']) +// ]; + } else { + return false; + } + } + + function addiprange($post) + { + $path = 'index.php?act=addiprange'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function addsg($post) + { + $path = 'index.php?act=addsg'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function addbackupserver($post) + { + $path = 'index.php?act=addbackupserver'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function addstorage($post) + { + $path = 'index.php?act=addstorage'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function adddnsplan($post) + { + $path = 'index.php?act=adddnsplan'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function add_admin_acl($post) + { + $path = 'index.php?act=add_admin_acl'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function addmg($post) + { + $path = 'index.php?act=addmg'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function add_distro($post) + { + $path = 'index.php?act=add_distro'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function addpdns($post) + { + $path = 'index.php?act=addpdns'; + $ret = $this->call($path, [], $post); + return $ret; + } + + + function adminindex() + { + $path = 'index.php?act=adminindex'; + $res = $this->call($path); + return $res; + } + + function apidoings() + { + + } + + function backup($post) + { + $path = 'index.php?act=backup'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function bandwidth() + { + $path = 'index.php?act=backup'; + $ret = $this->call($path); + return $ret; + } + + /** + * Cleaning the POST variables + * @author Pulkit Gupta + * @param array $cookies An array FOR SENDING COOKIES + * @param array $post An array of DATA that should be posted + * @return array The unserialized array on success OR false on failure + */ + function clean_post(&$post, $edit = 0) + { + $post['serid'] = !isset($post['serid']) ? 0 : (int) $post['serid']; + $post['uid'] = !isset($post['uid']) ? 0 : (int) $post['uid']; + $post['plid'] = !isset($post['plid']) ? 0 : (int) $post['plid']; + $post['osid'] = !isset($post['osid']) ? 0 : (int) $post['osid']; + $post['iso'] = !isset($post['iso']) ? 0 : (int) $post['iso']; + $post['space'] = !isset($post['space']) ? 10 : (int) $post['space']; + $post['ram'] = !isset($post['ram']) ? 512 : (int) $post['ram']; + $post['swapram'] = !isset($post['swapram']) ? 1024 : (int) $post['swapram']; + $post['bandwidth'] = !isset($post['bandwidth']) ? 0 : (int) $post['bandwidth']; + $post['network_speed'] = !isset($post['network_speed']) ? 0 : (int) $post['network_speed']; + $post['cpu'] = !isset($post['cpu']) ? 1000 : (int) $post['cpu']; + $post['cores'] = !isset($post['cores']) ? 4 : (int) $post['cores']; + $post['cpu_percent'] = !isset($post['cpu_percent']) ? 100 : (int) $post['cpu_percent']; + $post['vnc'] = !isset($post['vnc']) ? 1 : (int) $post['vnc']; + $post['vncpass'] = !isset($post['vncpass']) ? 'test' : $post['vncpass']; + $post['sec_iso'] = !isset($post['sec_iso']) ? 0 : $post['sec_iso']; + $post['kvm_cache'] = !isset($post['kvm_cache']) ? 0 : $post['kvm_cache']; + $post['io_mode'] = !isset($post['io_mode']) ? 0 : $post['io_mode']; + $post['vnc_keymap'] = !isset($post['vnc_keymap']) ? 'en-us' : $post['vnc_keymap']; + $post['nic_type'] = !isset($post['nic_type']) ? 'default' : $post['nic_type']; + $post['osreinstall_limit'] = !isset($post['osreinstall_limit']) ? 0 : (int) $post['osreinstall_limit']; + $post['mgs'] = !isset($post['mgs']) ? 0 : $post['mgs']; + $post['tuntap'] = !isset($post['tuntap']) ? 0 : $post['tuntap']; + $post['virtio'] = !isset($post['virtio']) ? 0 : $post['virtio']; + if (isset($post['hvm'])) { + $post['hvm'] = $post['hvm']; + } + $post['noemail'] = !isset($post['noemail']) ? 0 : $post['noemail']; + $post['boot'] = !isset($post['boot']) ? 'dca' : $post['boot']; + $post['band_suspend'] = !isset($post['band_suspend']) ? 0 : $post['band_suspend']; + $post['vif_type'] = !isset($post['vif_type']) ? 'netfront' : $post['vif_type']; + if ($edit == 0) { + $post['addvps'] = !isset($post['addvps']) ? 1 : (int) $post['addvps']; + } else { + $post['editvps'] = !isset($post['editvps']) ? 1 : $post['editvps']; + $post['acpi'] = !isset($post['acpi']) ? 1 : $post['acpi']; + $post['apic'] = !isset($post['apic']) ? 1 : $post['apic']; + $post['pae'] = !isset($post['pae']) ? 1 : $post['pae']; + $post['dns'] = !isset($post['dns']) ? [ + '4.2.2.1', + '4.2.2.2' + ] : $post['dns']; + $post['editvps'] = !isset($post['editvps']) ? 1 : (int) $post['editvps']; + } + + return $post; + } + + function cluster() + { + + } + + function config($post) + { + $path = 'index.php?act=config'; + $ret = $this->call($path, [], $post); + return $ret; + } + + /** + * Get CPU usage details + * @author Pulkit Gupta + * @param + * @return array The unserialised array is returned on success or + * empty array is returned on failure + */ + function cpu($serverid = 0) + { + $path = 'index.php?act=manageserver&changeserid='.$serverid; + $ret = $this->call($path); + return $ret['usage']['cpu']; + } + + function createssl($post) + { + $path = 'index.php?act=createssl'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function createtemplate($post) + { + $path = 'index.php?act=createtemplate'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function databackup() + { + + } + + function defaultvsconf($post) + { + $path = 'index.php?act=defaultvsconf'; + $ret = $this->call($path, [], $post); + return $ret; + } + + /** + * Delete a VPS + * @author Pulkit Gupta + * @param int $vid An array of DATA that should be posted + * @return Res|DoneResponse + */ + function delete_vs(int $vid) + { + $path = 'index.php?act=vs&delete='.(int) $vid; + $res = $this->callStorm($path); + if ($res->isSuccess()) { + return new DoneResponse($res); + } else { + return $res; + } + } + + /** + * Get Disk usage details + * @author Pulkit Gupta + * @param int $serverid + * @return array The unserialised array is returned on success or + * empty array is returned on failure + */ + function disk($serverid = 0) + { + $path = 'index.php?act=manageserver&changeserid='.$serverid; + $ret = $this->call($path); + return $ret['usage']['disk']; + } + + function downloadiso() + { + + } + + function editemailtemp() + { + + } + + function editippool($post) + { + $path = 'index.php?act=editippool'; + $res = $this->call($path, [], $post); + return $res; + } + + function editips($post) + { + $path = 'index.php?act=editips'; + $res = $this->call($path, [], $post); + return $res; + } + + function editplan($post) + { + $path = 'index.php?act=editplan'; + $res = $this->call($path, [], $post); + return $res; + } + + function editserver($post) + { + $path = 'index.php?act=editserver'; + $res = $this->call($path, [], $post); + return $res; + } + + function edittemplate($post) + { + $path = 'index.php?act=edittemplate'; + $res = $this->call($path, [], $post); + return $res; + } + + /** + * @param UserEdit $post + * @return Res + */ + function edituser(EditUser $post, $uid) + { + $path = 'index.php?act=edituser&uid='.$uid; + $res = $this->callStorm($path, [], $post); + if ($res->isSuccess()) { + return new UserEdit($res); + } + return $res; + } + + /** + * @param ManageVPS $post + * @param int $vpsID + * @return Res|\YiiMan\VirtualizorSdk\Responses\Admin\ManageVps + */ + public function managevps(ManageVPS $post, int $vpsID) + { + $post->vpsid0 = $vpsID; + $path = 'index.php?act=managevps&vpsid='.$vpsID; + $res = $this->callStorm($path, '', $post); + if ($res->isSuccess()) { + return new \YiiMan\VirtualizorSdk\Responses\Admin\ManageVps($res); + } else { + return $res; + } + } + + /** + * Create a VPS + * @author Pulkit Gupta + * @param array $post An array of DATA that should be posted + * @return array The unserialized array on success OR false on failure + */ + function editvs($post, $cookies = []) + { + $path = 'index.php?act=editvs&vpsid='.$post['vpsid']; + $post = $this->clean_post($post, 1); + $ret = $this->call($path, '', $post, $cookies); + return [ + 'title' => $ret['title'], + 'error' => @empty($ret['error']) ? [] : $ret['error'], + 'vs_info' => $ret['vps'] + ]; + } + + function emailconfig($post) + { + $path = 'index.php?act=emailconfig'; + $res = $this->call($path, [], $post); + return $res; + } + + function emailtemp($post) + { + $path = 'index.php?act=emailtemp'; + $res = $this->call($path, [], $post); + return $res; + } + + function filemanager($post) + { + $path = 'index.php?act=filemanager'; + $res = $this->call($path, '', $post); + return $res; + } + + function firewall($post) + { + $path = 'index.php?act=firewall'; + $res = $this->call($path, [], $post); + return $res; + } + + function giveos() + { + + } + + function health() + { + + } + + function hostname($post) + { + $path = 'index.php?act=hostname'; + $res = $this->call($path, '', $post); + return $res; + } + + function import($post) + { + $path = 'index.php?act=import'; + $res = $this->call($path, [], $post); + return $res; + } + + function ippool($page = 1, $reslen = 50) + { + $path = 'index.php?act=ippool&page='.$page.'&reslen='.$reslen; + $res = $this->call($path); + return $res; + } + + /** + * Get list of IPs + * @author Pulkit Gupta + * @param + * @return Res|Ipsresponse The unserialised array on success. + */ + function ips($page = 1, $reslen = 50) + { + $path = 'index.php?act=ips&page='.$page.'&reslen='.$reslen; + $ret = $this->callStorm($path); + if ($ret->isSuccess()) { + return new Ipsresponse($ret); + } else { + return $ret; + } + + } + + function iso($page = 1, $reslen = 50) + { + $path = 'index.php?act=iso&page='.$page.'&reslen='.$reslen; + $ret = $this->call($path); + return $ret; + } + + function kernelconf($post = 0) + { + $path = 'index.php?act=kernelconf'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function license() + { + $path = 'index.php?act=license'; + $ret = $this->call($path); + return $ret; + } + + /** + * List VPS + * @author Pulkit Gupta + * @param int page number, if not specified then only 50 records are returned. + * @return ListVs|Res The ListVs class OR false on failure + */ + function vps_list($page = 1, $reslen = 50) + { + $path = 'index.php?act=vs&page='.$page.'&reslen='.$reslen; + $response = $this->callStorm($path); + if ($response->isSuccess()) { + return new ListVs($response); + } else { + return $response; + } + } + + function login() + { + + } + + function loginlogs($page = 1, $reslen = 50) + { + $path = 'index.php?act=loginlogs&page='.$page.'&reslen='.$reslen; + $ret = $this->call($path); + return $ret; + } + + function logs($page = 1, $reslen = 50) + { + $path = 'index.php?act=logs&page='.$page.'&reslen='.$reslen; + $ret = $this->call($path); + return $ret; + } + + function maintenance($post) + { + $path = 'index.php?act=maintenance'; + $ret = $this->call($path, [], $post); + return $ret; + } + + function makeslave() + { + + } + + function os($post) + { + $path = 'index.php?act=os'; + $ret = $this->call($path, [], $post); + return $ret; + } + + /** + * @param int $page + * @param int $reslen + * @return OsTemplates|Res + */ + function ostemplates($page = 1, $reslen = 50) + { + $path = 'index.php?act=ostemplates&page='.$page.'&reslen='.$reslen; + $ret = $this->callStorm($path); + if ($ret->isSuccess()) { + return new OsTemplates($ret); + } else { + return $ret; + } + } + + function performance() + { + $path = 'index.php?act=performance'; + $ret = $this->call($path); + return $ret; + } + + function phpmyadmin() + { + + } + + /** + * @param int $page + * @param int $reslen + * @return Res|Plans + */ + function plans($page = 1, $reslen = 50) + { + $path = 'index.php?act=plans&page='.$page.'&reslen='.$reslen; + $ret = $this->callStorm($path); + if ($ret->isSuccess()) { + return new Plans($ret); + } else { + return $ret; + } + } + + /** + * 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?act=vs&action=poweroff&serid=0&vpsid='.(int) $vid); + return $res; + } + + function processes() + { + $path = 'index.php?act=processes'; + $ret = $this->call($path); + return $ret; + } + + /** + * Get RAM details + * @author Pulkit Gupta + * @param + * @return array The unserialised array is returned on success or + * empty array is returned on failure + */ + function ram($serverid = 0) + { + $path = 'index.php?act=manageserver&changeserid='.$serverid; + $ret = $this->call($path); + return $ret['usage']['ram']; + } + + /** + * @param $vpsId + * @return Res|Response + */ + function stat($vpsId) + { + $path = 'index.php?act=vps_stats'; + $post = + [ + 'show' => 1, + 'vpsid' => $vpsId + ]; + $ret = $this->callStorm($path, '', $post); + if ($ret->isSuccess()) { + return new Response($ret); + } else { + return $ret; + } + } + + /** + * Rebuild a VPS + * @author Pulkit Gupta + * @param Rebuild $post An array of DATA that should be posted + * @param integer $vpsid + * @return Res|RebuildResponse The unserialized array on success OR false on failure + */ + function rebuild(Rebuild $post, int $vpsid) + { + $path = 'index.php?act=rebuild'; + $post->vpsid = $vpsid; + $ret = $this->callStorm($path, '', $post); + if ($ret->isSuccess()) { + return new RebuildResponse($ret); + } else { + return $ret; + } + } + + /** + * RESTART a Virtual Server + * @author Pulkit Gupta + * @param int $vid The VMs ID + * @return Res|DoneResponse + */ + function restart($vid) + { + // Make the Request + $res = $this->callStorm('index.php?act=vs&action=restart&serid=0&vpsid='.(int) $vid); + if ($res->isSuccess()) { + return new DoneResponse($res); + } else { + return $res; + } + } + + function restartservices($post) + { + $path = 'index.php?act=restartservices'; + $res = $this->call($path, [], $post); + return $res; + } + + /** + * Current server information + * @author Pulkit Gupta + * @param + * @return array The unserialized array on success OR false on failure + */ + function serverinfo() + { + $path = 'index.php?act=serverinfo'; + return $this->call($path); + } + + /** + * List Servers + * @author Pulkit Gupta + * @param + * @return Res|Server_list The unserialized array on success OR false on failure + */ + function servers() + { + $path = 'index.php?act=servers'; + $ret = $this->callStorm($path); + if ($ret->isSuccess()) { + return new Server_list($ret); + } else { + return $ret; + } + } + + function services($post = 0) + { + $path = 'index.php?act=services'; + $res = $this->call($path, '', $post); + return $res; + } + + function ssh() + { + /* $path = 'index.php?act=ssh'; + $res = $this->call($path); + return $res;*/ + } + + function ssl($post = 0) + { + /* $path = 'index.php?act=ssl'; + $res = $this->call($path, '', $post); + return $res;*/ + } + + function sslcert() + { + /* $path = 'index.php?act=sslcert'; + $res = $this->call($path); + return $res;*/ + } + + /** + * START a Virtual Server + * @author Pulkit Gupta + * @param int $vid The VMs ID + * @return Res|DoneResponse + */ + function start($vid) + { + + $res = $this->callStorm('index.php?act=vs&action=start&serid=0&vpsid='.(int) $vid); + if ($res->isSuccess()) { + return new DoneResponse($res); + } else { + return $res; + } + + } + + /** + * STOP a Virtual Server + * @author Pulkit Gupta + * @param int $vid The VMs ID + * @return Res|DoneResponse + */ + function stop($vid) + { + // Make the Request + $res = $this->callStorm('index.php?act=vs&action=stop&serid=0&vpsid='.(int) $vid); + if ($res->isSuccess()) { + return new DoneResponse($res); + } else { + return $res; + } + + } + + /** + * @param array $vids + * @return Res|StatusResponse + */ + function status($vids) + { + + // Make the Request + $res = $this->callStorm('index.php?act=vs&vs_status='.implode(',', $vids)); + if ($res->isSuccess()) { + return new StatusResponse($res); + } else { + return $res; + } + } + + /** + * Suspends a VM of a Virtual Server + * @author Pulkit Gupta + * @param int $vid The VMs ID + * @return Res|DoneResponse + */ + function suspend($vid) + { + $path = 'index.php?act=vs&suspend='.(int) $vid; + $res = $this->callStorm($path); + if ($res->isSuccess()) { + return new DoneResponse($res); + } else { + return $res; + } + } + + /** + * Unsuspends a VM of a Virtual Server + * @author Pulkit Gupta + * @param int $vid The VMs ID + * @return Res|DoneResponse + */ + function unsuspend($vid) + { + $path = 'index.php?act=vs&unsuspend='.(int) $vid; + $res = $this->callStorm($path); + if ($res->isSuccess()) { + return new DoneResponse($res); + } else { + return $res; + } + } + + function tools() + { + + } + + function ubc($post) + { + $path = 'index.php?act=ubc'; + $res = $this->call($path, [], $post); + return $res; + } + + function updates() + { + $path = 'index.php?act=updates'; + $res = $this->call($path); + return $res; + } + + function userlogs($page = 1, $reslen = 50) + { + $path = 'index.php?act=userlogs&page='.$page.'&reslen='.$reslen; + $res = $this->call($path); + return $res; + } + + function userSuspend($uid) + { + $path = 'index.php?act=users&page=1&reslen=3'; + $res = $this->callStorm($path, "", ['suspend' => $uid]); + if ($res->isSuccess()) { + return new DoneResponse($res); + } + return $res; + } + + function userUnsuspend($uid) + { + $path = 'index.php?act=users&page=1&reslen=3'; + $res = $this->callStorm($path, "", ['unsuspend' => $uid]); + $data = $res->getData(); + if (!is_object($data)) { + $data = str_replace('Success !a:', 'a:', $data); + $data = unserialize($data); + $res->setData($data); + } + + if ($res->isSuccess()) { + return new DoneResponse($res); + } + return $res; + } + + /** + * @param int $page + * @param int $reslen + * @return Res|UsersList + */ + function users($page = 1, $reslen = 50) + { + $path = 'index.php?act=users&page='.$page.'&reslen='.$reslen; + $res = $this->callStorm($path); + if ($res->isSuccess()) { + return new UsersList($res); + } else { + return $res; + } + + } + + function vnc($post) + { + $path = 'index.php?act=vnc'; + $res = $this->call($path, [], $post); + return $res; + } + + function vpsbackupsettings($post) + { + $path = 'index.php?act=vpsbackupsettings'; + $res = $this->call($path, [], $post); + return $res; + } + + function vpsbackups($post) + { + $path = 'index.php?act=vpsbackups'; + $res = $this->call($path, '', $post); + return $res; + } + + + function vsbandwidth() + { + $path = 'index.php?act=vsbandwidth'; + $res = $this->call($path); + return $res; + } + + function vscpu() + { + $path = 'index.php?act=vscpu'; + $res = $this->call($path); + return $res; + } + + function vsram() + { + $path = 'index.php?act=vsram'; + $res = $this->call($path); + return $res; + + } +} \ No newline at end of file diff --git a/src/VirtualizorClient.php b/src/VirtualizorClient.php new file mode 100644 index 0000000..42a407e --- /dev/null +++ b/src/VirtualizorClient.php @@ -0,0 +1,723 @@ +key = $key; + $this->pass = $pass; + $this->ip = $ip; + $this->port = $port; + if (!($port == 4083 || $port == 443)) { + $this->protocol = 'http'; + } + } + + /** + * Dumps a variable + * + * @param array $re The Array or any other variable. + * @return NULL + * @author Pulkit Gupta + */ + public function r($re) + { + echo '
';
+        print_r($re);
+        echo '
'; + } + + /** + * Unserializes a string + * + * @param string $str The serialized string + * @return array The unserialized array on success OR false on failure + * @author Pulkit Gupta + */ + public 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 + * + * @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 + * @author Pulkit Gupta + */ + public function call($path, $post = [], $cookies = []) + { + + $url = ($this->protocol).'://'.$this->ip.':'.$this->port.'/'.$path; + $url .= (strstr($url, '?') ? '' : '?'); + $url .= '&api=json&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 + * + * @return array The array containing a list of Virtual Servers one has in their account + * @author Pulkit Gupta + */ + public function listvs() + { + + $resp = $this->call('index.php?act=listvs'); + + return $resp['vs']; + + } + + + /** + * START a Virtual Server + * + * @param int $vid The VMs ID + * @return bool TRUE on success or FALSE on failure + * @author Pulkit Gupta + */ + public 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 + * + * @param int $vid The VMs ID + * @return bool TRUE on success or FALSE on failure + * @author Pulkit Gupta + */ + public 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 + * + * @param int $vid The VMs ID + * @return bool TRUE on success or FALSE on failure + * @author Pulkit Gupta + */ + public 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 + * + * @param int $vid The VMs ID + * @return bool TRUE on success or FALSE on failure + * @author Pulkit Gupta + */ + public 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 + * + * @param int $vid The VMs ID + * @return int 1 if the VM is ON, 0 if its OFF + * @author Pulkit Gupta + */ + public 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 + * + * @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 + * @author Pulkit Gupta + */ + public function hostname($vid, $newhostname = null) + { + + // Are we to change ? + if (!empty($newhostname)) { + + $post = [ + '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. + * + * @param int $vid The VMs ID + * @return array An array containing the details is returned. Usage details is available only in case of OpenVZ. + * @author Pulkit Gupta + */ + public 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. + * + * @param int $vid The VMs ID + * @return array An array containing the details is returned. Usage details is available only in case of OpenVZ. + * @author Pulkit Gupta + */ + public 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. + * + * @param int $vid The VMs ID + * @return array An array containing the details is returned. Usage details is available only in case of OpenVZ. + * @author Pulkit Gupta + */ + public 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. + * + * @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 + * @author Pulkit Gupta + */ + public 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 + * + * @param int $vid The VMs ID + * @return array An array containing all the processes is returned + * @author Pulkit Gupta + */ + public function processes($vid) + { + + $resp = $this->call('index.php?svs='.$vid.'&act=processes'); + + return $resp['processes']; + + } + + + /** + * List the Services in a VPS - Only OpenVZ + * + * @param int $vid The VMs ID + * @return array An array containing all the services is returned + * @author Pulkit Gupta + */ + public 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 + * + * @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 + * @author Pulkit Gupta + */ + public function changepassword($vid, $pass) + { + + $post = [ + '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. + * + * @param int $vid The VMs ID + * @return array An array containing all the VNC Details + * @author Pulkit Gupta + */ + public 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. + * + * @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 + * @author Pulkit Gupta + */ + public function vncpass($vid, $pass) + { + + $post = [ + '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. + * + * @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 + * @author Pulkit Gupta + */ + public 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 []; + + } + + if (!empty($newosid)) { + + // The POST Vars + $post = [ + '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 + * + * @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 + * @author Pulkit Gupta + */ + public 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')); + +?> \ No newline at end of file