/** * getApiUrl * Get the HostControl API URL based on the testing-mode of $params * @param array $params * @return mixed */ public static function getApiUrl($params = array()) { if (!empty($params['AlternativePort']) && $params['AlternativePort'] == "on") { return self::HOSTCONTROL_PRODUCTION_ALTERNATIVE_API_URL; } if (in_array($_SERVER['REMOTE_ADDR'], array("212.203.0.138", "192.168.0.185"))) { self::$production = false; return self::HOSTCONTROL_TEST_API_URL; } return self::HOSTCONTROL_PRODUCTION_API_URL; }
private function execute_request($method, $path, $parameters) { if (!in_array($method, $this->allowed_methods)) { throw new HostControlAPIClientError('Unsupported request method.'); } $handler = curl_init(); $url = $this->baseurl . $path; if ($method == self::HTTP_METHOD_GET) { if (is_array($parameters) && count($parameters) > 0) { $query_string = http_build_query($parameters); $url .= $query_string; } } else { $parameters = json_encode($parameters); if ($parameters === false) { throw new HostControlAPIClientError('Unable to encode parameters.'); } curl_setopt($handler, CURLOPT_POSTFIELDS, $parameters); } curl_setopt($handler, CURLOPT_URL, $url); curl_setopt($handler, CURLOPT_RETURNTRANSFER, true); curl_setopt($handler, CURLOPT_FRESH_CONNECT, true); curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($handler, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($handler, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($handler, CURLOPT_HTTPHEADER, array("X-APIKEY: {$this->apikey}")); $response = curl_exec($handler); if (strlen($response) == 0) { throw new HostControlAPIClientError('No response received.'); } $status_code = curl_getinfo($handler, CURLINFO_HTTP_CODE); if ($status_code != 200) { throw new HostControlAPIClientError("Received status code '{$status_code}' ."); } $response = json_decode($response); if (!is_object($response)) { throw new HostControlAPIClientError('Invalid response received.'); } if (!property_exists($response, 'success') || !$response->success) { if (is_object($response->error->message)) { $nested_errors = get_object_vars($response->error->message); $messages = array(); foreach ($nested_errors as $attribute => $error) { $messages[] = ucfirst($attribute) . ": " . join(', ', $error); } $error = join(", ", $messages); } else { if (!empty($response->error->message)) { $error = $response->error->message; } else { $error = HostControlHelper::get_human_friendly_error($response->error->code); } } throw new HostControlAPIClientError($error); } if (property_exists($response, 'result')) { return $response->result; } return null; }
/** * Sync the domainnames in WHMCS with the domainname (statuses) at HostControl * @param $params * @return array */ function hostcontrol_Sync($params) { $api_client = new HostControlAPIClient(HostControlHelper::getApiUrl($params), $params['ApiKey']); $domainname = strtolower($params["sld"] . "." . $params["tld"]); $domain_info = array(); try { $domain = $api_client->domain->get($domainname); $domain_info['expirydate'] = date('Y-m-d', strtotime($domain->expires)); } catch (HostControlAPIClientError $e) { HostControlHelper::debugLog($params, 'domain-get-single', array($domainname), $e); if (in_array($e->getMessage(), array('invalid_domain', 'invalid_resource'))) { $domain_info['active'] = false; $domain_info['expired'] = true; } } return $domain_info; }