* @author Roland Käser * @version 0.9 * @copyright Copyright (c) 2021, Thurdata * @example ../test.php */ /** * cwp7_Admin class documentation */ /** * cwp7_Admin is a class which allows to manage cwp7 accounts via web-api * * You may create, modify, migrate, delete and get the attributes of a cwp7 account using this class * * For the usage examples of all class methods check the source code of test.php */ class cwp7_Admin { private $constructorSuccess; private $cwp7URL; private $cwp7ConType; private $cwp7Port; private $cwp7Secure; protected $cwp7Token; /** * Constructor * @param string $cwp7URL cwp7 URL (example: https://cwp7.my.lan) * @param string $username admin/user account's name * @param string $token api token * @param string $secure optional false to force unsecure (default true) */ function __construct($cwp7URL, $token, $secure=true) { if(!in_array('curl', get_loaded_extensions())) { $this->constructorSuccess = false; return array('error_msg' => 'Error: PHP curl extension not available'); } if (empty($cwp7URL) || empty($token)) { $this->constructorSuccess = false; return array('error_msg' => 'Error: Server login info missing, check server configuration'); } if(preg_match('/^https/', $cwp7URL)) { $this->cwp7ConType = 'https://'; if($secure) { $this->cwp7Secure = true; } else { $this->cwp7Secure = false; } } else { $this->cwp7ConType = 'http://'; $this->cwp7Secure = false; } $cwp7Hostname = str_replace(array('http://', 'https://'), array('',''), $cwp7URL); $cwp7Hostname = explode(':', $cwp7Hostname); if (gethostbyname($cwp7Hostname[0]) == $cwp7Hostname[0] && !filter_var($cwp7Hostname[0], FILTER_VALIDATE_IP)) { $this->constructorSuccess = false; return array('error_msg' => 'Error: Cannot resolve ' . $cwp7Hostname[0] . ', check server configuration'); } $this->cwp7Port = ($cwp7Hostname[1]) ? $cwp7Hostname[1] : '2304'; $this->cwp7URL = $this->cwp7ConType . $cwp7Hostname[0] . ':' . $this->cwp7Port; $this->cwp7Token = $token; $this->constructorSuccess = true; } public function constructorSuccess() { return $this->constructorSuccess; } /** * getAllAccounts * * @return array of cwp7 accounts array of informations or error message */ public function getAllAccounts() { if (!$this->constructorSuccess()) { return array('error_msg' => 'Error: not connect'); } $data = array("key" => $this->cwp7Token, "action" => 'list'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/account'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); if(!$this->cwp7Secure) { curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt ($ch, CURLOPT_POST, 1); $response = curl_exec($ch); if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) { curl_close($ch); return array('error_msg' => $response); }; curl_close($ch); return json_decode($response, true); } /** * getAccount * * @param string $email login e-mail * * @return array of account informations or error message */ public function getAccount($user) { if (!$this->constructorSuccess()) { return array('error_msg' => 'Error: not connect'); } $data = array("key" => $this->cwp7Token, "action"=>'list', "user" => $user); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/accountdetail'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); if(!$this->cwp7Secure) { curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt ($ch, CURLOPT_POST, 1); $response = curl_exec($ch); if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) { curl_close($ch); return array('error_msg' => $response); }; curl_close($ch); return json_decode($response, true); } /** * createAccount * * @param array $params avvount informations, email required. * * @return array of account informations or error message */ public function createAccount($params) { if (!$this->constructorSuccess()) { return array('error_msg' => 'Error: not connected'); } if(!isset($params['domain'])) { return array('error_msg' => 'Error: missing parameter domain'); } if(!isset($params['user'])) { return array('error_msg' => 'Error: missing parameter user'); } if(!isset($params['pass'])) { return array('error_msg' => 'Error: missing parameter pass'); } if(!isset($params['email'])) { return array('error_msg' => 'Error: missing parameter email'); } if(!isset($params['package'])) { return array('error_msg' => 'Error: missing parameter package'); } if(!isset($params['autossl'])) { $params['autossl'] = 0; } $data = array( "key" => $this->cwp7Token, "action" =>'add', "domain" => $params['domain'], "user" => $params['user'], "pass" => base64_encode($params['pass']), "email" => $params['email'], "package" => $params['package'], "autossl" => $params['autossl'], "encodepass" => true, ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/account'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); if(!$this->cwp7Secure) { curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt ($ch, CURLOPT_POST, 1); $response = curl_exec($ch); if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) { curl_close($ch); return array('error_msg' => $response); }; curl_close($ch); return json_decode($response, true); } /** * modifyAccount * * @param array $params account informations, user, e-mail & new package required. * * @see https://download.cwp7.com/published/web-api/v2.1-admin/accounts.md#user-content-Add%20User * * @return array status -> OK or error message */ public function modifyAccount($params) { if (!$this->constructorSuccess()) { return array('error_msg' => 'Error: not connected'); } if(!isset($params['user'])) { return array('error_msg' => 'Error: missing parameter user'); } if(!isset($params['email'])) { return array('error_msg' => 'Error: missing parameter email'); } if(!isset($params['package'])) { return array('error_msg' => 'Error: missing parameter package'); } $data = array( "key" => $this->cwp7Token, "action" =>'upd', "user" => $params['user'], "email" => $params['email'], "package" => $params['package'], ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/account'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); if(!$this->cwp7Secure) { curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt ($ch, CURLOPT_POST, 1); $response = curl_exec($ch); if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) { curl_close($ch); return array('error_msg' => $response); }; curl_close($ch); return json_decode($response, true); } /** * deleteAccount * * @param array user & e-mail required * * @return array success => true or error message */ public function deleteAccount($params) { if (!$this->constructorSuccess()) { return array('error_msg' => 'Error: not connected'); } if(!isset($params['user'])) { return array('error_msg' => 'Error: missing parameter user'); } if(!isset($params['email'])) { return array('error_msg' => 'Error: missing parameter email'); } $data = array( "key" => $this->cwp7Token, "action" =>'del', "user" => $params['user'], "email" => $params['email'], ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/account'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); if(!$this->cwp7Secure) { curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt ($ch, CURLOPT_POST, 1); $response = curl_exec($ch); if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) { curl_close($ch); return array('error_msg' => $response); }; curl_close($ch); return json_decode($response, true); } /** * suspendAccount * * @param string $user required * * @return array success => true or error message */ public function suspendAccount($user) { if (!$this->constructorSuccess()) { return array('error_msg' => 'Error: not connected'); } $data = array( "key" => $this->cwp7Token, "action" =>'susp', "user" => $user, ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/account'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); if(!$this->cwp7Secure) { curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt ($ch, CURLOPT_POST, 1); $response = curl_exec($ch); if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) { curl_close($ch); return array('error_msg' => $response); }; curl_close($ch); return json_decode($response, true); } /** * unsuspendAccount * * @param string $user required * * @return array success => true or error message */ public function unsuspendAccount($user) { if (!$this->constructorSuccess()) { return array('error_msg' => 'Error: not connected'); } $data = array( "key" => $this->cwp7Token, "action" =>'unsp', "user" => $user, ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/account'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); if(!$this->cwp7Secure) { curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt ($ch, CURLOPT_POST, 1); $response = curl_exec($ch); if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) { curl_close($ch); return array('error_msg' => $response); }; curl_close($ch); return json_decode($response, true); } }