OwlCyberSecurity - MANAGER
Edit File: whmapi.class.php
<?php class whmapi { const VERSION = '1.0.1'; private $curl = false; private $server = false; private $username = false; private $password = false; private $accesshash = false; private $ssl = true; private $connect_timeout = 10; private $useragent = ''; private $debug = false; private $error = false; public function __construct($settings=array()) { // Debug if(array_key_exists('debug', $settings)) $this->debug = $settings['debug']; // SSL if(array_key_exists('ssl', $settings)) $this->ssl = $settings['ssl']; // Server if(!array_key_exists('server', $settings)) throw new exception ('setting param server is missing'); $this->server = $settings['server']; // Username if(!array_key_exists('username', $settings)) throw new exception('setting param username is missing'); $this->username = $settings['username']; // Password or accesshash if(!array_key_exists('password', $settings) && !array_key_exists('accesshash', $settings)) throw new exception('setting param accesshash and password missing, one must be set'); if(array_key_exists('accesshash', $settings)) $this->accesshash = $settings['accesshash']; if(array_key_exists('password', $settings)) $this->password = $settings['password']; // Curl connect timeout if(array_key_exists('connect_timeout', $settings)) $this->connect_timeout = $settings['connect_timeout']; // Set additional useragent if(array_key_exists('useragent', $settings)) $this->useragent = ' ' . $settings['useragent']; // Init Curl if(!function_exists('curl_init')) throw new exception('Your php installation does not appear to support curl'); $this->curl = curl_init(); if($this->curl === false) throw new Exception('curl_init failed, ' . curl_error()); // Curl Options curl_setopt($this->curl, CURLOPT_USERAGENT, 'WHMAPI-PHP-Class/' . self::VERSION . $this->useragent); curl_setopt($this->curl, CURLOPT_POST, 1); curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout); curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->curl, CURLINFO_HEADER_OUT, 1); curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); if($this->accesshash !== false) { curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Authorization: WHM ' . $this->username . ':' . $this->accesshash)); } else { curl_setopt($this->curl, CURLOPT_USERPWD, $this->username . ':' . $this->password); } } public function __destruct() { if($this->curl !== false) curl_close($this->curl); } public function whmapi_error() { return $this->error; } public function request($func='',$post=array()) { // function check if(empty($func)) throw new exception('request method requires first param to be a whm api function name'); // Curl URL if($this->ssl === true) { curl_setopt($this->curl, CURLOPT_URL, 'https://' . $this->server . ':2087/json-api/' . $func); } else { curl_setopt($this->curl, CURLOPT_URL, 'https//' . $this->server . ':2086/json-api/' . $func); } // Curl Post if(count($post) > 0) curl_setopt($this->curl, CURLOPT_POSTFIELDS, $post); // Make request $curl_exec = curl_exec($this->curl); $curl_info = curl_getinfo($this->curl); $curl_error = curl_error($this->curl); // Debug if($this->debug === true) { $log = array( 'timestamp' => date(r), 'curl_exec' => $curl_exec, 'curl_info' => $curl_info, 'curl_error' => $curl_error, ); @file_put_contents('./whmapi-debug.log', print_r($log, true), FILE_APPEND | LOCK_EX ); } // Check for errors if($curl_error != '' || $curl_exec === false) { $this->error = $curl_error; return false; } // Accesss check if($curl_info['http_code'] == '403') { $this->error = 'Authorization with WHM failed, access denied'; return false; } // Decode JSON $response = @json_decode(utf8_encode($curl_exec), true); if(!is_array($response)) { $this->error = 'Failed to decode JSON response from server'; return false; } // Return Response return $response; } }