uid = $client_uid; $this->password = $client_password; } function reset() { $this->result=''; $this->error=false; $this->error_string=''; $this->license_key=''; } function exists($lid) { $licenses = $this->get_licenses($lid); if ($licenses === false) return false; return array_key_exists($lid, $licenses); } function get_all_licenses() { return $this->get_licenses(); } function get_all_active_licenses() { return $this->get_licenses('','','Y'); } function get_all_inactive_licenses() { return $this->get_licenses('','','N'); } // Licenses that have been recently downloaded function get_all_recent_licenses() { return $this->get_licenses('','','Y', '31'); } // Llicenses that have not been recently downloaded, or never downloaded. // This is what you'd use to find lincenses you can re-use. function get_all_non_recent_licenses() { return $this->get_licenses('','','Y', '-31'); } function get_license_from_lid($lid) { $licenses = $this->get_licenses($lid); if ($licenses === false) return false; if (array_key_exists($lid, $licenses)) return $licenses[$lid]; //if already have error, string is likely set. if (!$this->error) { $this->error_string .= "Cannot find LID $lid in result"; $this->error = true; } return false; } function get_license_from_ip($ip) { $licenses = $this->get_licenses('', $ip); if ($licenses === false) return false; foreach ($licenses as $license) { if (array_key_exists('ip', $license) && $license['ip'] == $ip) return $license; } //if already have error, string is likely set. if (!$this->error) { $this->error_string .= "Cannot find LID from IP '$ip' in result"; $this->error = true; } return false; } function get_license_sessions($lid, $opts=NULL) { $vars = [ 'lid' => $lid ]; if (!is_null($opts)) $vars = array_merge($opts, $vars); $ch = $this->get_curl("/clients/api/license_sessions.php", $vars); $this->run_curl($ch); if ($this->error) return false; if ($this->result_has_error()) return false; return json_decode($this->result); } //****************************************************************** //* CHANGE THE IP OF THE LICENSE(only for approved accounts) function change_ip($lid, $new_ip) { return $this->do_special($lid, 'saveip', 'ip', $new_ip); } //****************************************************************** //* CHANGE THE OS OF THE LICENSE function change_os($lid, $new_os) { return $this->do_special($lid, 'saveos', 'os', $new_os); } //****************************************************************** //* CHANGE THE NAME OF THE LICENSE function change_name($lid, $new_name) { return $this->do_special($lid, 'savename', 'name', $new_name); } //****************************************************************** //* CHANGE THE PAYMENT METHOD OF THE LICENSE //* 'balance' or 'mail' function change_payment($lid, $new_payment) { return $this->do_special($lid, 'savepayment', 'payment', $new_payment); } //****************************************************************** //* CHANGE THE SUSPEND/BLOCK STATUS OF THE LICENSE //* blocks client's usage, not related to billing Y or N. function change_suspended($lid, $Y_or_N) { return $this->do_special($lid, 'savesuspended', 'suspended', $Y_or_N); } //****************************************************************** //* CHANGE THE SUSPEND/BLOCK STATUS OF THE LICENSE //* List of PIDs: $this->get_products(); function change_product($lid, $pid) { return $this->do_special($lid, 'savepid', 'pid', $pid); } //****************************************************************** //* RESET THE LICENSE KEY HASH FOR THE LICENSE //* Makes currently used keys void/unuseable. Must re-download new value. function reset_license_key($lid) { return $this->do_special($lid, NULL, 'action', 'reset_license_key'); } //****************************************************************** //* CHANGE THE IP AUTO LOCK FOR THE LICENSE //* value=0|1 function change_autolock($lid, $value) { return $this->do_special($lid, 'save_auto_ip_lock', 'auto_ip_lock', $value); } //****************************************************************** //* LIST ALL OPERATING SYSTEMS //* active='' for all. 'yes' for actives, 'no' for inactives. //* Just use get_active_os_list() for the 'yes' case. function get_os_list($active='') { $vars = array(); if ($active != '') $vars['active'] = $active; $ch = $this->get_curl("/clients/api/os_list.php", $vars); $this->run_curl($ch); if ($this->error) return false; if ($this->result_has_error()) return false; $os_list = array(); $raw_lines = explode("\n", $this->result); foreach ($raw_lines as $line) { if ($line == '') continue; list($name, $text) = explode('=', $line); if ($name == '' || $text == '') continue; $os_list[$name] = $text; } return $os_list; } function get_active_os_list() { return $this->show_os_list('yes'); } //****************************************************************** //* List All Available Products and their pid numbers. function get_products() { $ch = $this->get_curl("/clients/api/products.php"); $this->run_curl($ch); if ($this->error) return false; if ($this->result_has_error()) return false; $product_list = array(); $raw_lines = explode("\n", $this->result); foreach ($raw_lines as $line) { if ($line == '') continue; list($name, $text) = explode('=', $line); if ($name == '' || $text == '') continue; $product_list[$name] = $text; } return $product_list; } //****************************************************************** //* List All Available Products and their pid numbers. function get_user_info() { $ch = $this->get_curl("/clients/api/user_info.php"); $this->run_curl($ch); if ($this->error) return false; if ($this->result_has_error()) return false; parse_str($this->result, $user_info); return $user_info; } //****************************************************************** //* Used to access list.php in it's various forms. function save_commments($lid, $tag, $desc) { $vars = array(); $vars['tag'] = $tag; $vars['desc'] = $desc; $vars['lid'] = $lid; $ch = $this->get_curl("/clients/api/savecomments.php", $vars, 'POST'); $this->run_curl($ch); if ($this->error) return false; if ($this->result_has_error()) return false; return true; } //****************************************************************** //* Used to create a license. //* Documention: https://directadmin.com/clients/api/#create //* $data should include: pid, name, payment, auto_ip_lock=0|1, ouput_key=0|1 function create_license($data) { $this->license_key = ''; if (!isset($data['ip']) || $data['ip'] == '') $data['ip'] = 'NULL'; $ch = $this->get_curl("/clients/api/createlicense.php", $data, 'POST'); curl_setopt($ch, CURLOPT_REFERER, "https://www.directadmin.com/clients/api/createlicense.php"); curl_setopt($ch, CURLOPT_HEADER, 1); //for X-lid if( !($response=curl_exec($ch)) ) { $this->error_string .= curl_error($ch); $this->error = true; curl_close($ch); return false; } $this->set_http_code($ch); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($response, 0, $header_size); $this->result = substr($response, $header_size); curl_close($ch); if ($this->result_has_error()) return false; //get X-lid header. $headers = $this->get_headers($header); if (!isset($headers['X-lid']) || !is_numeric($headers['X-lid'])) { $this->error = true; $this->error_string = "License might be created, but cannot get X-lid header from result: ".$this->result; return false; } $return_variables = []; parse_str($this->result, $return_variables); if (isset($return_variables['license_key'])) { $this->license_key = urldecode($return_variables['license_key']); } return $headers['X-lid']; } //****************************************************************** //* Used to delete a license, if you're allowed. (approved accounts only) function delete_license($lid) { $vars = array(); $vars['lid'] = $lid; $ch = $this->get_curl("/clients/api/deletelicense.php", $vars, 'POST'); curl_setopt($ch, CURLOPT_REFERER, "https://www.directadmin.com/clients/license.php"); $this->run_curl($ch); if ($this->error) return false; if ($this->result_has_error()) return false; return true; } //****************************************************************** //* Used to make a balance payment on a license, if you're allowed. (approved accounts only) /* Documentation: https://new.directadmin.com/clients/api/#makepayment Optional: $data['autorenew'] = 'N' : Sets payment to 'mail' instead of balance, prevent auto-rewnew next month. */ function make_payment($lid, $data = NULL) { $vars = array(); if (!is_null($data)) $vars = array_merge($vars, $data); $vars['lid'] = $lid; $ch = $this->get_curl("/clients/api/makepayment.php", $vars, 'POST'); curl_setopt($ch, CURLOPT_REFERER, "https://www.directadmin.com/clients/makepayment.php"); $this->run_curl($ch); if ($this->error) return false; $this->result = trim($this->result); //output has extra newline ahead of data. if ($this->result_has_error()) return false; return true; } //************************************************************************************************************************************ //* PRIVATE FUNCTIONS - nothing for you to call below this point. //************************************************************************************************************************************ //****************************************************************** //* Setup the connection handler. private function get_curl($url, $vars='', $method='GET') { $var_string = ''; $c=0; if ($vars != '') foreach($vars as $v_name=>$v_value) { if ($c++>0) $var_string.='&'; $var_string .= urlencode($v_name).'='.urlencode($v_value); } $get_vars = ''; if ($method == 'GET') $url .= '?'.$var_string; $ch = curl_init($this->remote_host.$url); curl_setopt($ch, CURLOPT_USERPWD, $this->uid.':'.$this->password); if ($method == 'POST') { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $var_string); } curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //1 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //2 curl_setopt($ch, CURLOPT_FORBID_REUSE, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); curl_setopt($ch, CURLOPT_TIMEOUT, 15); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close')); //curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); return $ch; } //****************************************************************** //* Set the class http_code var to the http response code, and set errors accordingly. //* Return false for bad state. Return true for good or unsure. private function set_http_code($ch) { $this->http_code= curl_getinfo($ch, CURLINFO_RESPONSE_CODE); switch ($this->http_code) { case 401 : $this->error = true; $this->error_string .= "Login failure."; return false; case 429 : $this->error = true; $this->error_string .= "Rate-limit reached. Try again later."; return false; } return true; } //****************************************************************** //* Pull the trigger private function run_curl($ch) { if( !($this->result=curl_exec($ch)) ) { $this->error_string .= curl_error($ch); $this->error = true; } $this->set_http_code($ch); curl_close($ch); return $this->result; } //****************************************************************** //* Used to access special.php in it's various forms. private function do_special($lid, $action, $name, $value) { $vars = array(); $vars[$name] = $value; if (!is_null($action)) $vars[$action] = 'yes'; $ch = $this->get_curl("/clients/api/special.php?lid=$lid", $vars, 'POST'); $this->run_curl($ch); if ($this->error) return false; if ($this->result_has_error()) return false; return true; } //****************************************************************** //* Used to access list.php in it's various forms. private function get_licenses($lid='', $ip='', $active='', $recent_download='') { $vars = array(); if ($lid != '') $vars['lid'] = $lid; if ($ip != '') $vars['ip'] = $ip; if ($active != '') $vars['active'] = $active; if ($recent_download != '') $vars['recent_download'] = $recent_download; $ch = $this->get_curl("/clients/api/list.php", $vars); $this->run_curl($ch); if ($this->error) return false; if ($this->result_has_error()) return false; $licenses = array(); $raw_lines = explode("\n", $this->result); foreach ($raw_lines as $line) { if ($line == '') continue; parse_str($line, $license); if (!array_key_exists('lid', $license) || !is_numeric($license['lid'])) { $this->error = true; $this->error_string .= 'Cannot find lid from: '.$line."\n"; continue; } $licenses[$license['lid']] = $license; } return $licenses; } //****************************************************************** //* Check the filled result for error=1, and set the text. private function result_has_error() { if ($this->error) return true; if (substr($this->result, 0, strlen(IS_ERROR)) === IS_ERROR) { parse_str($this->result, $return_variables); $this->error = true; $this->error_string .= urldecode($return_variables['text']); return true; } return false; } private function get_headers($header_text) { $headers = array(); $raw_lines = explode("\n", $header_text); $c=0; foreach ($raw_lines as $line) { if ($c++==0) continue; //skip HTTP/1.1 $line = trim($line); if ($line == '' || $line == "\n") continue; list($name, $value) = explode(':', $line); $headers[trim($name)] = trim($value); } return $headers; } } ?>