true ); protected $lang=array( 'english'=>array( 'Cpanelplan'=>'Plan Name', 'Cpanelreseller'=>'Reseller Account', 'Cpaneldisk-quota'=>'Disk Quota', 'Cpanelbandwidth-limit'=>'Bandwidth Limit', 'Cpanelmax-ftp-acc'=>'Max Ftp Accounts', 'Cpanelmax-mail-acc'=>'Max Mail Accounts', 'Cpanelmax-mail-lists'=>'Max Mail Lists', 'Cpanelmax-sql-db'=>'Max SQL Databases', 'Cpanelmax-subdomains'=>'Max Subdomains', 'Cpanelmax-parked-domains'=>'Max Parked Domains', 'Cpanelmax-addon-domains'=>'Max Addon Domains', 'Cpaneldedicated-ip'=>'Dedicated IP', 'Cpanelshell-access'=>'Shell Access', 'Cpanelfrontpage-ex'=>'Frontpage Extensions', 'Cpanelcgi-access'=>'CGI Access', 'Cpanelcpmod'=>'Cpanel Theme', 'Cpanellanguage'=>'Language', 'Cpanelacl'=>'ACL (reseller only)', 'CpanelDBUsage'=>'Disk & Bandwidth Usage', 'CpanelLoginToWebmail'=>'Login to WebMail', 'CpanelLoginTo'=>'Login to cPanel' ) ); protected $options = array( // plan 'option1' =>array ( 'name'=> 'plan', 'value' => false, 'type'=> 'loadable', 'default'=>"getPackages" ), 'option2' =>array ( 'name'=> 'reseller', 'value' => '0', 'type'=> 'check', 'default'=>false ), // ACL 'option18' =>array ( 'name'=> 'acl', 'value' => false, 'type'=> 'loadable', 'default'=>"getAcls", 'extended' => true, 'depends' => array('option2') ), 'option3' =>array ( 'name'=> 'disk-quota', 'value' => false, 'type'=> 'input', 'default'=>false, 'extended' => true ), // Limit bandwidth 'option4' =>array ( 'name'=> 'bandwidth-limit', 'value' => false, 'type'=> 'input', 'default'=>false, 'extended' => true ), 'option5' =>array ( 'name'=> 'max-ftp-acc', 'value' => false, 'type'=> 'input', 'default'=>false, 'extended' => true ), 'option6' =>array ( 'name'=> 'max-mail-acc', 'value' => false, 'type'=> 'input', 'default'=>false, 'extended' => true ), 'option7' =>array ( 'name'=> 'max-mail-lists', 'value' => false, 'type'=> 'input', 'default'=>false, 'extended' => true ), 'option8' =>array ( 'name'=> 'max-sql-db', 'value' => false, 'type'=> 'input', 'default'=>false, 'extended' => true ), 'option9' =>array ( 'name'=> 'max-subdomains', 'value' => false, 'type'=> 'input', 'default'=>false, 'extended' => true ), 'option10' =>array ( 'name'=> 'max-parked-domains', 'value' => false, 'type'=> 'input', 'default'=>false, 'extended' => true ), 'option11' =>array ( 'name'=> 'max-addon-domains', 'value' => false, 'type'=> 'input', 'default'=>false, 'extended' => true ), 'option12' =>array ( 'name'=> 'dedicated-ip', 'value' => '0', 'type'=> 'check', 'default'=>false, 'extended' => true ), // Shell access 'option13' =>array ( 'name'=> 'shell-access', 'value' => '0', 'type'=> 'check', 'default'=>false, 'extended' => true ), // Frontpage extensions 'option14' =>array ( 'name'=> 'frontpage-ex', 'value' => '0', 'type'=> 'check', 'default'=>false, 'extended' => true ), // CGI access 'option15' =>array ( 'name'=> 'cgi-access', 'value' => '0', 'type'=> 'check', 'default'=>false, 'extended' => true ), // Cpanel Theme 'option16' =>array ( 'name'=> 'cpmod', 'value' => false, 'type'=> 'input', 'default'=>'', 'extended' => true ), // 'option17' =>array ( // 'name'=> 'language', // 'value' => false, // 'type'=> 'input', // 'default'=>'', // 'extended' => true // ) ); public function addUser($username,$password, $oldpass=false) { $this->details['option1']['value']=$username; $this->details['option2']['value']=$password; } public function addDomain($domain) { $this->details['option4']['value']=$domain; } public function optionPackage($package) { $this->options['option1']['value'] = $package; } public function getPackage() { return $this->options['option1']['value']; } protected function addInfo($info) { if(empty($info) || $info == '') return false; parent::addInfo($info); } private $error_prefix = false; protected function addError($info) { if(empty($info) || $info == '') return false; if($this->error_prefix) { $info = $this->error_prefix.' - '.$info; $this->error_prefix = false; } parent::addError($info); } protected $details = array( // domena 'option4' =>array ( 'name'=> 'domain', 'value' => false, 'type'=> 'input', 'default'=>false ), // nazwa uzytkownika 'option1' =>array ( 'name'=> 'username', 'value' => false, 'type'=> 'input', 'default'=>false ), // haslo 'option2' =>array ( 'name'=> 'password', 'value' => false, 'type'=> 'input', 'default'=>false ), ); protected $commands = array( 'Suspend','Terminate','ChangePackage','ChangePassword','Create','Unsuspend' ); private function get($command,$params=false, $timeout = 90) { if(substr($command, 0, 1) != '/') $command='/'.$command; $url = $this->url.$command; if(is_array($params)) { $url .= '?'; foreach($params as $key=>$value) { $key = urlencode($key); $value = urlencode($value); $url .= "{$key}={$value}&"; } } $ch = curl_init(); $chOptions = array ( CURLOPT_URL => rtrim ($url, '&'), CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_TIMEOUT => $timeout ); if($this->hash) { $curlheader[] = $this->hash; curl_setopt($ch,CURLOPT_HTTPHEADER,$curlheader); } curl_setopt_array($ch, $chOptions); $out = curl_exec($ch); if($out === false) { $this->addError(ucwords(curl_error($ch))); } curl_close($ch); return $out; } public function testConnection() { if($this->is_connected(true)) return true; } private function checkErr($simpleXML) { switch ($this->action) { case "createacct" : if(!$simpleXML) { sleep(3); if($this->checkUser()) return true; $this->addError('No response from server. Please check manually in WHM if account has been created.'); return false; } if((integer)$simpleXML->result->status == 1) return True; if(isset($simpleXML->result->statusmsg)) $this->addError((string)$simpleXML->result->statusmsg); if(isset($simpleXML->statusmsg)) $this->addError((string)$simpleXML->statusmsg); return False; break; case "setacls": if((integer)$simpleXML->result->status == 1) return True; if(isset($simpleXML->result->statusmsg)) $this->addError('Error occured while setting up ACL for reseller: '.(string)$simpleXML->result->statusmsg); if(isset($simpleXML->statusmsg)) $this->addError('Error occured while setting up ACL for reseller: '.(string)$simpleXML->statusmsg); return False; case "suspendacct" : if(!$simpleXML) { sleep(3); if($this->suspended()) return true; $this->addError('No response from server. Please check manually in WHM if account has been suspended.'); return false; } if((integer)$simpleXML->result->status == 1) return True; if(isset($simpleXML->result->statusmsg)) $this->addError((string)$simpleXML->result->statusmsg); if(isset($simpleXML->statusmsg)) $this->addError((string)$simpleXML->statusmsg); return False; break; case "unsuspendacct" : if(!$simpleXML) { sleep(3); if(!$this->suspended()) return true; $this->addError('No response from server. Please check manually in WHM if account has been unsuspended.'); return false; } if((integer)$simpleXML->result->status == 1) return True; if(isset($simpleXML->result->statusmsg)) $this->addError((string)$simpleXML->result->statusmsg); if(isset($simpleXML->statusmsg)) $this->addError((string)$simpleXML->statusmsg); return False; break; case "removeacct" : if(!$simpleXML) { sleep(3); if(!$this->checkUser()) return true; $this->addError('No response from server. Please check manually in WHM if account has been created.'); return false; } if((integer)$simpleXML->result->status == 1) return True; if(isset($simpleXML->result->statusmsg)) $this->addError((string)$simpleXML->result->statusmsg); if(isset($simpleXML->statusmsg)) $this->addError((string)$simpleXML->statusmsg); return False; break; case "changepackage" : if((integer)$simpleXML->result->status == 1) return True; if(isset($simpleXML->result->statusmsg)) $this->addError((string)$simpleXML->result->statusmsg); if(isset($simpleXML->statusmsg)) $this->addError((string)$simpleXML->statusmsg); return False; break; case "passwd" : if((integer)$simpleXML->passwd->status == 1) return True; $this->addError((string)$simpleXML->passwd->statusmsg); if(isset($simpleXML->statusmsg)) $this->addError((string)$simpleXML->statusmsg); return False; break; case "modifyacct" : if((integer)$simpleXML->result->status == 1) { //$this->addInfo((string)$simpleXML->result->statusmsg); return True; } $this->addInfo('Can\'t modify user params: '.(string)$simpleXML->result->statusmsg); return False; break; case "limitbw" : if((integer)$simpleXML->result->status == 1) { //$this->addInfo((string)$simpleXML->result->statusmsg); return True; } $this->addInfo('Can\'t modify user bandwidth: '.(string)$simpleXML->result->statusmsg); break; case "editquota" : if(!$simpleXML) { $this->addInfo('Can\'t modify user quota: No response from the server. Please check manually in CPanel if quota has been changed.'); return false; } if((integer)$simpleXML->result->status == 1) { //$this->addInfo((string)$simpleXML->result->statusmsg); return True; } $this->addInfo('Can\'t modify user quota: '.(string)$simpleXML->result>statusmsg); break; case "unsetupreseller" : if((integer)$simpleXML->result->status == 1) { //$this->addInfo((string)$simpleXML->result->statusmsg); return True; } $this->addInfo('Can\'t Unsetup reseller privilleges for this account : '.(string)$simpleXML->result>statusmsg); break; case "setupreseller" : if((integer)$simpleXML->result->status == 1) { //$this->addInfo((string)$simpleXML->result->statusmsg); return True; } $this->addInfo('Can\'t Setup reseller privilleges for this account : '.(string)$simpleXML->result>statusmsg); break; } return False; } private function is_connected($test = false) { if($this->connected == true) return true; $out = $this->get("/xml-api/version",false,$test == true ? 15 : 90); if(!$out) { $this->addError(self::CONNECTION_PROBLEM); return False; } $out = $this->parseResponse($out); if(!$out || !isset($out->version)) { $this->addError(self::LOGIN_ERROR); return False; } $this->connected = true; return True; } private function checkUser() { if(!$this->is_connected()) return false; $params = array('user' => $this->details['option1']['value']); $out = $this->get("/xml-api/accountsummary", $params); $out = $this->parseResponse($out); if((integer)$out->status == 1) return true; } private function suspended() { if(!$this->is_connected()) return false; $params = array('user' => $this->details['option1']['value']); $out = $this->get("/xml-api/accountsummary", $params); $out = $this->parseResponse($out); if((integer)$out->status == 0) { $this->addError((string)$out->statusmsg); return False; } if((integer)$out->acct->suspended == 1) return True; return False; } private function parseResponse($response_string) { //zamienia string na SimpleXML if(!$response_string) return false; @$a = simplexml_load_string($response_string); if($a===FALSE) { return false; } else { $xml = new SimpleXMLElement($response_string); return $xml; } } private function getVersion() { if(!$this->is_connected()) return false; if($this->panel_version) return $this->panel_version; $out = $this->get("/xml-api/version"); $out = $this->parseResponse($out); $this->panel_version = (string)$out->version; return (string)$out->version; } private function checkVersion() { $version = $this->getVersion(); if(!$version) { $this->addError('Cannot connect to Control Panel.'); return false; } $number = str_replace("." , "" , $version); if(substr($number,0 , 5)<11240) { $this->addError('Server has an older version (' . $version . '). This module requires 1.24.0'); return False; } return true; } private $param = array(); private $action; const NOUSER = "User do not exists on the server"; const CONNECTION_PROBLEM = "Couldn't Connect To Host"; const LOGIN_ERROR = "Login to server unsuccessful"; const SUSPENDED = "User is already suspended"; const UNSUSPENDED = "User is already unsuspended"; // CONSTRUCT private $url; private $hash = false; private $secure=false; private $client_url=''; private $panel_version = false; public function connect($connect) { $ip = (!$connect['ip'] && $connect['host']) ? $connect['host'] : $connect['ip']; if($connect['hash']) { $this->hash = "Authorization: WHM {$connect['username']}:". str_replace(array("\r", "\n"),"",$connect['hash']); if($connect['secure']) $this->url = "https://{$ip}:2087"; else $this->url = "http://{$ip}:2086"; } else { if($connect['secure']) $this->url = 'https://'.urlencode($connect['username']).':'.urlencode($connect['password']).'@'.$ip.':2087'; else $this->url = 'http://'.urlencode($connect['username']).':'.urlencode($connect['password']).'@'.$ip.':2086'; } if($connect['secure']) $this->secure=true; $this->cp_username=$connect['username']; $this->client_url=$ip; } public function callCustomModule($data=array()) { //call custom cPanel module and return results, dont parse for errors! if(!$this->is_connected()) return false; if(!isset($data['cpanel_xmlapi_user'])) { $data['cpanel_xmlapi_user'] = $this->cp_username; } $out = $this->get("/xml-api/cpanel",$data); $out = $this->parseResponse($out); return $out; } public function getPackages($data = array()) { if(!$this->is_connected()) return False; $out = $this->get("/xml-api/listpkgs"); $out = $this->parseResponse($out); if((integer)$simpleXML->result->status == 1) { } elseif(isset($simpleXML->statusmsg)) $this->addError('Can\'t get packages: '.(string)$simpleXML->statusmsg); if(isset($out->package)) { if($data['import'] == true) { foreach($out->package as $key) $array[] = array('name' =>(string)$key->name); } else { foreach($out->package as $key) $array[] = (string)$key->name; } return $array; } else $this->addError('Unable to get packages from the server.'); } public function getAcls($data) { if($data['option2'] == '1') { if(!$this->is_connected()) return False; $out = $this->get("/xml-api/listacls"); $out = $this->parseResponse($out); if(isset($out->acls)) { if(empty($out->acls)) return false; $array = array(); $array[] = ''; $i=1; foreach($out->acls as $key) { $array[$i] = (string)$key->name; $i++; } return $array; } elseif(isset($out->statusmsg)) $this->addError('Can\'t get ACLs: '.(string)$out->statusmsg); } } public function Prepare($data=array()) { if(empty($data) || !is_array($data)) return false; if(isset($data['options'])) { foreach($data['options'] as $options => $value) $this->options[$options]['value'] = $value; } else { foreach($data as $options => $value) $this->options[$options]['value'] = $value; } } public function Send() { if(!$this->checkVersion()) return false; $out = $this->get("/xml-api/{$this->action}", $this->param); $out = $this->parseResponse($out); if($this->checkErr($out)) return True; return False; } private function setAcl() { $this->action = "setacls"; $this->param = array( 'reseller' => $this->details['option1']['value'], 'acllist' => $this->options['option18']['value'] ); $this->send(); } protected $clientCommands = array( 'LoginInfo','CpanelDBUsage','CpanelLoginToWebmail','CpanelLoginTo' ); public function getCustomTemplate($method) { //return path to custom template file if its supported by a method if( in_array($method,array('CpanelDBUsage','CpanelLoginToWebmail','CpanelLoginTo','LoginInfo'))) { return MAINDIR.'includes'.DS.'libs'.DS.'cpanel'.DS.'template.tpl'; } return ''; } public function LoginInfo() {} public function getLoginInfo() { return $this->getCpanelLoginTo(); } public function CpanelLoginTo() {} public function getCpanelLoginTo() { return array('hostname'=>$this->getPanelLoginUrl($this->client_url,$this->secure), 'username' => $this->details['option1']['value'], 'password' => $this->details['option2']['value']); } public function CpanelLoginToWebmail() {} public function getCpanelLoginToWebmail() { if($this->secure) { $hostname='https://'.$this->client_url.':2096'; } else { $hostname='http://'.$this->client_url.':2095'; } return array('hostname'=>$hostname, 'username' => $this->details['option1']['value'], 'password' => $this->details['option2']['value']); } public function CpanelDBUsage(){} public function getCpanelDBUsage() { return array( 'disk'=>$this->getHDD(), 'bandwidth'=>$this->getBandwidth() ); } private function getHDD() { if(!$this->is_connected()) return False; $r=$this->get("xml-api/accountsummary",array('user' => $this->details['option1']['value'])); $r = $this->parseResponse($r); if(isset($r->acct)) { $ret=array( 'usage'=>str_ireplace("M", "",(string)$r->acct->diskused), 'limit'=>str_replace("M", "",(string)$r->acct->disklimit) ); if($ret['limit']=='unlimited') $ret['ratio']=0; else { $ret['ratio']=ceil(($ret['usage']/$ret['limit'])*100); } return $ret; } return false; } private function getBandwidth() { if(!$this->is_connected()) return False; $r=$this->get("xml-api/showbw",array('search' => $this->details['option4']['value'], 'searchtype' => 'domain')); $r = $this->parseResponse($r); if(isset($r->bandwidth)) { $ret=array( 'usage'=>(int)ceil($r->bandwidth->acct->totalbytes/1024), 'limit'=>(string)$r->bandwidth->acct->limit ); if($ret['limit']=='unlimited') $ret['ratio']=0; else { $ret['limit']=$ret['limit']/(1024*1024); $ret['ratio']=ceil(($ret['usage']/$ret['limit'])*100); } return $ret; } return false; } public function Create() { if(!$this->is_connected()) return False; $this->action = "createacct"; $this->param = array( 'username' => $this->details['option1']['value'], 'password' => $this->details['option2']['value'], 'contactemail' => $this->client_data['email'], 'domain' => $this->details['option4']['value'], 'plan' => $this->options['option1']['value'], 'reseller' => $this->options['option2']['value'], 'quota' => $this->options['option3']['value'], 'bwlimit' => $this->options['option4']['value'], 'maxftp' => $this->options['option5']['value'], 'maxpop' => $this->options['option6']['value'], 'maxlst' => $this->options['option7']['value'], 'maxsql' => $this->options['option8']['value'], 'maxsub' => $this->options['option9']['value'], 'maxpark' => $this->options['option10']['value'], 'maxaddon' => $this->options['option11']['value'], 'ip' => $this->options['option12']['value'], 'hasshell' => $this->options['option13']['value'], 'frontpage' => $this->options['option14']['value'], 'cgi' => $this->options['option15']['value'], 'cpmod' => $this->options['option16']['value'], // 'language' => $this->options['option17']['value'], ); if($this->send()) { if($this->options['option2']['value'] == '1' && $this->options['option18']['value'] != '') { $this->setAcl(); } return true; } return false; } public function Suspend() { if(!$this->is_connected()) return False; if($this->suspended()) { $this->addError(self::SUSPENDED); return true; } $this->action = "suspendacct"; $this->param = array('user' => $this->details['option1']['value']); if($this->send()) return True; return false; } public function Unsuspend() { if(!$this->is_connected()) return False; if(!$this->suspended()) { if(isset($this->error) && !empty($this->error)) return False; $this->addError(self::UNSUSPENDED); return true; } $this->action = "unsuspendacct"; $this->param = array('user' => $this->details['option1']['value']); if($this->send()) return True; return false; } public function Terminate() { if(!$this->is_connected()) return False; $this->action = "removeacct"; $this->param = array('user' => $this->details['option1']['value']); if($this->send()) return true; return false; } public function ChangePackage() { if(!$this->is_connected()) return False; $this->action = "changepackage"; $this->param = array('user' => $this->details['option1']['value'], 'pkg' => $this->options['option1']['value']); $this->error_prefix = 'CPanel Plan change error'; if($this->send()) $this->addInfo('Plan has been changed'); $this->action = "limitbw"; $this->param = array('user' => $this->details['option1']['value'], 'bwlimit' => $this->options['option4']['value']); if($this->send()) $this->addInfo('Bandwidth limit updated'); $this->action = "editquota"; $this->param = array('user' => $this->details['option1']['value'], 'quota' => $this->options['option3']['value']); if($this->send()) $this->addInfo('Quota limit updated'); if($this->options['option2']['value'] == '1') { $this->action = "setupreseller"; $this->param = array('user' => $this->details['option1']['value']); if($this->send()) $this->addInfo('Reseller privileges applied'); } else { $this->action = "unsetupreseller"; $this->param = array('user' => $this->details['option1']['value']); if($this->send()) $this->addInfo('Reseller privileges removed'); } $this->action = "modifyacct"; $this->param = array( 'user' => $this->details['option1']['value'], 'domain' => $this->details['option4']['value'], 'HASCGI' => $this->options['option15']['value'], 'MAXFTP' => $this->options['option5']['value'], 'MAXPOP' => $this->options['option6']['value'], 'MAXLST' => $this->options['option7']['value'], 'MAXSQL' => $this->options['option8']['value'], 'MAXSUB' => $this->options['option9']['value'], 'MAXPARK' => $this->options['option10']['value'], 'MAXADDON' => $this->options['option11']['value'], 'shell' => $this->options['option13']['value'], 'cptheme' => $this->options['option16']['value'], 'lang' => $this->options['option17']['value'], ); if($this->send()) $this->addInfo('Account limits updated'); return true; } public function ChangePassword($newpassword) { if(!$this->is_connected()) return False; $this->action = "passwd"; $this->param = array('user' => $this->details['option1']['value'], 'pass' => $newpassword); if($this->send()) return true; return false; } /************** GET ACCOUNTS FOR IMPORT SCRIPT *************/ public function getAccounts() { if(!$this->is_connected()) return false; $out = $this->get("/xml-api/listaccts"); $out = $this->parseResponse($out); $return = array(); if(array($out->acct) && !empty($out->acct)) { foreach($out->acct as $acct) { $return[] = array( 'username' => (string)$acct->user, 'email' => (string)$acct->email, 'domain' => (string)$acct->domain, 'package' => (string)$acct->plan, 'status' => (int)$acct->suspended == 1 ? 'Suspended' : 'Active' ); } } return $return; } /************** SYNCHRONIZE *************/ public function getSynchInfo() { if(!$this->is_connected()) return false; $return = array(); $return['suspended'] = $this->suspended() ? '1' : '0'; $info = $this->checkInfo(); $return['domain'] = $info['domain']; $return['checkdomain'] = $info['checkdomain']; // if other user have this domain related. $return['checkpassword'] = '0'; $return['user'] = $info['is_user']; return $return; } private function listAccounts() { $out = $this->get("/xml-api/listaccts"); $out = $this->parseResponse($out); $return = array(); if(array($out->acct) && !empty($out->acct)) { foreach($out->acct as $acct) { $return['users'][] = (string)$acct->user; $name = (string)$acct->user; $val = (string)$acct->domain; $return['domains'][$val] = $name; } } return $return; } public function checkInfo() { $return = array('is_user' => '0', 'checkdomain' => '1', 'domain' => ''); $data = $this->listAccounts(); if(empty($data)) return $return; if(in_array($this->details['option1']['value'], $data['users'])) $return['is_user'] = '1'; if(isset($data['domains'][$this->details['option4']['value']]) && $data['domains'][$this->details['option4']['value']] != $this->details['option1']['value']) $return['checkdomain'] = '0'; $return['domain'] = array_search($this->details['option1']['value'], $data['domains']); return $return; } } ?>