Difference between revisions of "API CLASS FILE"

From VMPanel
Jump to: navigation, search
(Created page with " '''PHP Code:''' <?php ////////////////////////////////////////////////////////////// //=========================================================== // VMPanel_API.php...")
 
Line 3: Line 3:
 
'''PHP Code:'''
 
'''PHP Code:'''
  
<?php
+
<source lang="php" enclose="div">
  
//////////////////////////////////////////////////////////////
+
<?php
//===========================================================
+
// VMPanel_API.php
+
//===========================================================
+
// VMPanel - Cyber Group
+
// Version : 1.0
+
// ----------------------------------------------------------
+
// Started by: mahdi8492
+
// Date:      14th Sep 2016
+
// Time:      18:00 hrs
+
// Site:      http://www.vmpanel.ir/ (VMPanel)
+
// ----------------------------------------------------------
+
// Please Read the Terms of use at http://www.vmpanel.ir/
+
// ----------------------------------------------------------
+
//===========================================================
+
// (c)Cyber Group.
+
//===========================================================
+
//////////////////////////////////////////////////////////////
+
  
class VMPanel_API {
+
//////////////////////////////////////////////////////////////
 +
//===========================================================
 +
// VMPanel_API.php
 +
//===========================================================
 +
// VMPanel - Cyber Group
 +
// Version : 1.0
 +
// ----------------------------------------------------------
 +
// Started by: mahdi8492
 +
// Date:      14th Sep 2016
 +
// Time:      18:00 hrs
 +
// Site:      http://www.vmpanel.ir/ (VMPanel)
 +
// ----------------------------------------------------------
 +
// Please Read the Terms of use at http://www.vmpanel.ir/
 +
// ----------------------------------------------------------
 +
//===========================================================
 +
// (c)Cyber Group.
 +
//===========================================================
 +
//////////////////////////////////////////////////////////////
 +
 
 +
class VMPanel_API {
 
 
 
var $apiusernme = '';
 
var $apiusernme = '';
Line 56: Line 58:
 
* @return      array The unserialized array on success OR false on failure
 
* @return      array The unserialized array on success OR false on failure
 
*/
 
*/
public function _unserialize($str){
+
public function _unserialize($str){
 
 
 
$var = @unserialize($str);
 
$var = @unserialize($str);
Line 80: Line 82:
  
 
 
public function cyberhttpPost($params)
+
public function cyberhttpPost($params)
 
{
 
{
 
$url = "http://".$this->ip.":".$this->port."/?loadapi=1&";
 
$url = "http://".$this->ip.":".$this->port."/?loadapi=1&";
Line 113: Line 115:
 
}
 
}
 
 
public function checklogin()
+
public function checklogin()
 
     {
 
     {
  
Line 131: Line 133:
 
     }
 
     }
 
 
public function isolist()
+
public function isolist()
 
     {
 
     {
 
        
 
        
Line 146: Line 148:
 
     }
 
     }
  
public function serverlist()
+
public function serverlist()
 
     {
 
     {
 
        
 
        
Line 162: Line 164:
  
  
public function createvps($useremail,$userpassword,$firstname,$lastname,$hostname,$server,$ram,$space,$cpumhz,$bandwidth,$vnc,$datastore,$useros,$cores='1',$nic_type='e1000',$osreinstall='0',$resouce_pools='')
+
public function createvps($useremail,$userpassword,$firstname,$lastname,$hostname,$server,$ram,$space,$cpumhz,$bandwidth,$vnc,$datastore,$useros,$cores='1',$nic_type='e1000',$osreinstall='0',$resouce_pools='')
 
     {
 
     {
  
Line 206: Line 208:
 
     }
 
     }
  
public function rebuildvps($vmid,$newos)
+
public function rebuildvps($vmid,$newos)
 
     {
 
     {
  
Line 229: Line 231:
 
 
 
 
public function getAllVmInfo()
+
public function getAllVmInfo()
 
     {
 
     {
 
        
 
        
Line 244: Line 246:
 
     }
 
     }
  
public function getSummaryInfo( $vmid )
+
public function getSummaryInfo( $vmid )
 
     {
 
     {
 
 
Line 261: Line 263:
 
     }
 
     }
  
public function getGuestInfo( $vmid )
+
public function getGuestInfo( $vmid )
 
     {
 
     {
 
 
Line 278: Line 280:
 
     }
 
     }
  
public function getDataStoreInfo( $vmid )
+
public function getDataStoreInfo( $vmid )
 
     {
 
     {
  
Line 295: Line 297:
 
     }
 
     }
 
//// Open VNC Ports On ESXI //////
 
//// Open VNC Ports On ESXI //////
public function gdbserver( $vmid )
+
public function gdbserver( $vmid )
 
     {
 
     {
 
 
Line 316: Line 318:
 
     }
 
     }
  
public function getvncconf( $vmid )
+
public function getvncconf( $vmid )
 
     {
 
     {
 
$postfilds = array();
 
$postfilds = array();
Line 332: Line 334:
 
     }
 
     }
  
public function getnovnc( $vmid )
+
public function getnovnc( $vmid )
 
     {
 
     {
 
 
Line 347: Line 349:
 
     }
 
     }
  
public function enablevnc( $vmid )
+
public function enablevnc( $vmid )
 
     {
 
     {
  
Line 387: Line 389:
 
     }
 
     }
  
public function poweroff( $vmid )
+
public function poweroff( $vmid )
 
     {
 
     {
  
Line 406: Line 408:
 
     }
 
     }
  
public function poweron( $vmid )
+
public function poweron( $vmid )
 
     {
 
     {
  
Line 425: Line 427:
 
     }
 
     }
  
public function resetvps( $vmid )
+
public function resetvps( $vmid )
 
     {
 
     {
  
Line 444: Line 446:
 
     }
 
     }
 
 
public function suspend( $vmid )
+
public function suspend( $vmid )
 
     {
 
     {
  
Line 463: Line 465:
 
     }
 
     }
 
 
public function unsuspend( $vmid )
+
public function unsuspend( $vmid )
 
     {
 
     {
  
Line 482: Line 484:
 
     }
 
     }
 
 
public function rebootos( $vmid )
+
public function rebootos( $vmid )
 
     {
 
     {
  
Line 502: Line 504:
  
 
 
public function shutdownos( $vmid )
+
public function shutdownos( $vmid )
 
     {
 
     {
  
Line 521: Line 523:
 
     }
 
     }
  
public function mountvmtool( $vmid )
+
public function mountvmtool( $vmid )
 
     {
 
     {
  
Line 540: Line 542:
 
     }
 
     }
  
public function getstate( $vmid )
+
public function getstate( $vmid )
 
     {
 
     {
  
Line 560: Line 562:
 
     }
 
     }
 
 
public function getvminfo( $vmid )
+
public function getvminfo( $vmid )
 
     {
 
     {
 
$postfilds = array();
 
$postfilds = array();
Line 601: Line 603:
 
     }
 
     }
  
public function terminatevps( $vmid )
+
public function terminatevps( $vmid )
 
     {
 
     {
  
Line 620: Line 622:
 
     }
 
     }
 
 
} // Class Ends
+
} // Class Ends
 +
 
 +
?>
  
?>
+
</source>

Revision as of 15:52, 14 September 2016


PHP Code:

<source lang="php" enclose="div">

<?php

////////////////////////////////////////////////////////////// //=========================================================== // VMPanel_API.php //=========================================================== // VMPanel - Cyber Group // Version : 1.0 // ---------------------------------------------------------- // Started by: mahdi8492 // Date: 14th Sep 2016 // Time: 18:00 hrs // Site: http://www.vmpanel.ir/ (VMPanel) // ---------------------------------------------------------- // Please Read the Terms of use at http://www.vmpanel.ir/ // ---------------------------------------------------------- //=========================================================== // (c)Cyber Group. //=========================================================== //////////////////////////////////////////////////////////////

class VMPanel_API {

var $apiusernme = ; var $apipassword = ; var $ip = ; var $port = 873; var $protocol = 'http'; var $error = array();

/** * Contructor * * @param string $ip IP of the VMPanel * @param string $apiusername The API Username of your VMPanel * @param string $apipassword The API Password of your VMPanel * @param int $port (Optional) The port to connect to. Port 873 is the default.(Other Ports : 2021 , 2022 , 2023 , 2082 , 2222 ) * @return NULL */ function VMPanel_API($ip, $apiusernme, $apipassword, $port = 873){ $this->apiusernme = $apiusernme; $this->apipassword = $apipassword; $this->ip = $ip; $this->port = $port;

}

/** * Unserializes a string * * @param string $str The serialized string * @return array The unserialized array on success OR false on failure */ 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;

}

}


public function cyberhttpPost($params) { $url = "http://".$this->ip.":".$this->port."/?loadapi=1&";

 	$postData = ;

//create name value pairs seperated by & $params['api'] = true ; $params['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'] ; $params['SERVER_ADDR'] = $_SERVER['SERVER_ADDR'] ; $params['HTTP_HOST'] = $_SERVER['HTTP_HOST'] ; $params['SERVER_NAME'] = $_SERVER['SERVER_NAME'] ; $params['MODULE_VER'] = '2.7' ;

foreach($params as $k => $v) {

     $postData .= $k . '='.$v.'&'; 

} $postData = rtrim($postData, '&');

$ch = curl_init();

   curl_setopt($ch,CURLOPT_URL,$url);
   curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
   curl_setopt($ch,CURLOPT_HEADER, false); 
   curl_setopt($ch, CURLOPT_POST, count($postData));
   curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    

   $output=curl_exec($ch);

   curl_close($ch);
   return $output;

}

public function checklogin()

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'check' ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

if( $data['ok'] = true ){ return true ; }

       return false ;
   }

public function isolist()

   {
      

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'isolist' ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

       return $data ;
   }

public function serverlist()

   {
      

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'serverlist' ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

       return $data ;
   }


public function createvps($useremail,$userpassword,$firstname,$lastname,$hostname,$server,$ram,$space,$cpumhz,$bandwidth,$vnc,$datastore,$useros,$cores='1',$nic_type='e1000',$osreinstall='0',$resouce_pools=)

   {


$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'create' ; $postfilds['prefix'] = 'API' ; // Prefix For Create VPS

$data['email'] = $useremail ; $data['userpassword'] = $userpassword ; $data['firstname'] = $firstname ; $data['lastname'] = $lastname ; $data['hostname'] = $hostname ; $data['server'] = $server ; $data['ram'] = $ram ; $data['space'] = $space ; $data['cpu'] = $cpumhz ; $data['bandwidth'] = $bandwidth ; $data['os'] =  ; $data['iso'] =  ; if(!empty($vnc)){ $data['vnc'] = 'on' ; }else{ $data['vnc'] =  ; } $data['ds'] = $datastore ; $data['useros'] = $useros ; $data['cores'] = $cores ; $data['nic_type'] = $nic_type ; $data['osreinstall'] = $osreinstall ; $data['resouce_pools'] = $resouce_pools ;

$postfilds['data'] = base64_encode(serialize($data)); ;


$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);


       return $data;
   }

public function rebuildvps($vmid,$newos)

   {


$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'rebuild' ; $postfilds['vmid'] = $vmid ;

$data['newos'] = $newos ;

$postfilds['data'] = base64_encode(serialize($data)); ;


$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);


       return $data;
   }


public function getAllVmInfo()

   {
      

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'vpslist' ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

       return $data ;
   }

public function getSummaryInfo( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'getsummary' ; $postfilds['datatype'] = 'array' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds);

$data = $this->_unserialize($data);

       return $data;
   }

public function getGuestInfo( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'getguestinfo' ; $postfilds['datatype'] = 'array' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds);

$data = $this->_unserialize($data);

       return $data ;
   }

public function getDataStoreInfo( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['datatype'] = 'array' ; $postfilds['action'] = 'getdatastore' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds);

$data = $this->_unserialize($data);

       return $data ;
   }

//// Open VNC Ports On ESXI ////// public function gdbserver( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'gdbserver' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds);

$data = $this->_unserialize($data);

if($data['status'] == 'action-success' ){ return true; }

      return false;
   }

public function getvncconf( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'getvncconf' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds);


$data = $this->_unserialize($data);

return $data ;

   }

public function getnovnc( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'novnc' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

return $data ;

   }

public function enablevnc( $vmid )

   {


$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'setvncconf' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

if($data['enabled'] == "TRUE"){ return true; } return false;

  	}


public function resetvncpass( $vmid )

   {


$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'setvncpass' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

if($data['enabled'] == "TRUE"){ return true; } return false;

   }

public function poweroff( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'poweroff' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

if($data['status'] == "action-success"){ return true ; }

       return false;
   }

public function poweron( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'poweron' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

if($data['status'] == "action-success"){ return true ; }

       return false;
   }

public function resetvps( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'reset' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

if($data['status'] == "action-success"){ return true ; }

       return false;
   }

public function suspend( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'suspend' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

if($data['status'] == "action-success"){ return true ; }

       return false;
   }

public function unsuspend( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'unsuspend' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

if($data['status'] == "action-success"){ return true ; }

       return false;
   }

public function rebootos( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'reboot' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

if($data['status'] == "action-success"){ return true ; }

       return false;
   }


public function shutdownos( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'shutdown' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

if($data['status'] == "action-success"){ return true ; }

       return false;
   }

public function mountvmtool( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'vmtool' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

if($data['status'] == "action-success"){ return true ; }

       return false;
   }

public function getstate( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'getstatus' ; $postfilds['datatype'] = 'array' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

if(!empty($data)){ return $data ; }

       return false;
   }

public function getvminfo( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'importantinfo' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

if(empty($data)){ return true ; } $vminfoArr = array( );

       $info = array( );
       $info['vmid'] = $vmid;
       $info['memorySize'] = intval( $data['ram'] );
       $info['memoryUsage'] = intval( $data['memoryUsage'] );
       $info['cpuMax'] = intval( $data['cpu'] );

$info['bootTime'] = $data['bootTime'];

       $info['os'] = $data['os'];
       $info['osFullName'] = trim( $data['os_name'] );
       $info['hostname'] = $data['hostname'];

$info['vmPathName'] = $data['vmPathName'];

       $info['ip'] = $data['ip'];
       $info['powerState'] = strtolower( trim( $data['powerstate'] ) );
       $info['cpuUsage'] = intval( $data['cpuUsage'] );
       $info['uptime'] = intval( $data['uptime'] );
       $info['hd'] = $data['disks']['size'];
       $info['hardUsage'] = $data['hardUsage'];
       $info['hardFree'] = round($info['hd']-$info['hardUsage']);
       $info['hardNums'] = $data['hardNums'];
       return $info;
   }

public function terminatevps( $vmid )

   {

$postfilds = array(); $postfilds['user'] = $this->apiusernme ; $postfilds['pass'] = $this->apipassword ; $postfilds['action'] = 'terminate' ; $postfilds['vmid'] = $vmid ;

$data = $this->cyberhttpPost($postfilds); $data = $this->_unserialize($data);

if($data['status'] == "action-success"){ return true ; }

       return $data['status'];
   }

} // Class Ends

?>

</source>