host = $params['hostname'] ? $params['hostname'] : $params['default_ip']; $this->username = $params['username']; $this->password = $params['hash'] ? trim($params['hash']) : $params['password']; $this->headers[] = $params['hash'] ? 'Authorization: WHM ' . $this->username . ':' . str_replace(array("\r", "\n"), "", $this->password) : "Authorization: Basic " . base64_encode($this->username . ":" . $this->password); if($params['ssl'] == 'on') $this->ssl = true; else $this->ssl = false; $this->port = $params['serverport'] ? $params['serverport'] : '2087'; $this->queryuser = $user; return $this; } public function createSession($service='cpaneld') { $query = ($this->ssl ? 'https' : 'http') . "://" . $this->host . ":" . $this->port . "/json-api/create_user_session?api.version=1&user=" . $this->queryuser . "&service=".$service; $this->ch = curl_init(); curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($this->ch, CURLOPT_HEADER, false); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers); curl_setopt($this->ch, CURLOPT_URL, $query); $response = curl_exec($this->ch); if (!$response) { $error = curl_error($this->ch); curl_close($this->ch); throw new exceptions\DNSSubmoduleException("Curl error: " . $error); } $jsonResponse = json_decode($response, true); if ($jsonResponse['cpanelresult']['error']) { throw new exceptions\DNSSubmoduleException("UAPI error: " . $jsonResponse['cpanelresult']['error']); }else if($jsonResponse['metadata']['result']===0){ throw new exceptions\DNSSubmoduleException("UAPI error: " . $jsonResponse['metadata']['reason']); } $this->initSession($jsonResponse); } private function initSession($jsonResponse) { $tab = explode(':', $jsonResponse['data']['url']); $sessionUrl = $tab[0] . '://' . $this->host . ':' . $tab[2]; $cookieJar = 'cookie.txt'; curl_setopt($this->ch, CURLOPT_URL, $sessionUrl); curl_setopt($this->ch, CURLOPT_HTTPHEADER, null); curl_setopt($this->ch, CURLOPT_COOKIESESSION, true); curl_setopt($this->ch, CURLOPT_COOKIEJAR, $cookieJar); curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookieJar); $response = curl_exec($this->ch); if (!$response) { $error = curl_error($this->ch); curl_close($this->ch); throw new exceptions\DNSSubmoduleException("Curl error: " . $error); } $this->sessionUrl = preg_replace('{/login(?:/)??.*}', '', $sessionUrl); } public function exec($name, $api, $request = array()) { $this->requestUrl = $this->sessionUrl . $api . $name; if ($request) { $this->requestUrl .= '?' . http_build_query($request); } curl_setopt($this->ch, CURLOPT_URL, $this->requestUrl); $this->lastResponse = curl_exec($this->ch); $this->debug(); if (!$this->lastResponse) { $error = curl_error($this->ch); curl_close($this->ch); throw new exceptions\DNSSubmoduleException("Curl error: " . $error); } return $this->processResponse(); } private function processResponse() { $replace = [ '\\\n' => "", '\\\u2014' => " - ", '"[{' => '[{', '}]"' => '}]', '\"' => '"', '\\\\' => '' ]; $response = str_replace(array_keys($replace), array_values($replace), $this->lastResponse); $this->lastJsonResponse = json_decode($response, true); if ($this->lastJsonResponse['errors']) { throw new exceptions\DNSSubmoduleException("UAPI error: " . trim(implode(", ", $this->lastJsonResponse['errors']))); } if ($this->lastJsonResponse['data'] && preg_match('/Warning/', $this->lastJsonResponse['data']) && !preg_match('/Success/', $this->lastJsonResponse['data'])) { throw new exceptions\DNSSubmoduleException("UAPI error: " . trim($this->lastJsonResponse['data'])); } return $this->lastJsonResponse; } private function debug() { if (!$this->loger || !method_exists($this->loger, 'log')) { return; } $action = print_r(sprintf("GET: %s\r\nPOST: %s\r\n", $this->requestUrl, print_r($this->post, true)), true); $this->loger->log(str_replace($this->sessionUrl, "",$this->requestUrl), $action, $this->lastResponse, $this->jsonResponse); } function getLastResponse() { return $this->lastResponse; } function setLastResponse($lastResponse) { $this->lastResponse = $lastResponse; } }