| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules\SimpleDNSV8;
- use MGModule\DNSManager2\mgLibs\lang;
- class Curl
- {
- private $login;
- private $password;
- protected $hostUrl;
- protected $ssl;
- protected $curl;
- public function __construct(array $params)
- {
- $this->setParams($params)
- ->setHostUrl($params);
- }
- protected function setParams(array $params)
- {
- if (!$params['login'] || !$params['password'])
- {
- throw new \Exception('Login details are insufficient');
- }
- $this->login = $params['login'];
- $this->password = $params['password'];
- $this->httpauth = $params['httpauth'];
- return $this;
- }
- protected function setHostUrl(array $params)
- {
- if (!$params['hostname'] || !$params['port'])
- {
- throw new \Exception('Server details are insufficient');
- }
- $hostnameParts = $this->prepareHostnameParts($params['hostname']);
- $protocol = $params['ssl'] == 'on' ? 'https://' : 'http://';
- $this->hostUrl = $protocol . $hostnameParts['firstPart'] . ':' . $params['port'] . $hostnameParts['secondPart']; // API ver.2
- return $this;
- }
- protected function prepareHostnameParts($hostname)
- {
- $hostnameParts = explode('/', $hostname); // for incase of such hostnames simpledns.com/api
- $firstPart = $hostnameParts[0];
- unset($hostnameParts[0]);
- $secondPart = '';
- if(count($hostnameParts) > 0)
- {
- $secondPart = '/' . implode('/' ,$hostnameParts);
- }
- return [
- 'firstPart' => $firstPart,
- 'secondPart' => $secondPart
- ];
- }
- protected function init()
- {
- $this->curl = curl_init();
- return $this;
- }
- protected function prepareOptions()
- {
- curl_setopt($this->curl,CURLOPT_RETURNTRANSFER, true);
- curl_setopt($this->curl,CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($this->curl,CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($this->curl,CURLOPT_TIMEOUT, 30);
- curl_setopt($this->curl,CURLOPT_HTTPAUTH, $this->httpauth);
- curl_setopt($this->curl,CURLOPT_TIMEOUT, 30);
- return $this;
- }
- protected function setCurlUrl($url)
- {
- curl_setopt($this->curl,CURLOPT_URL, $url);
- return $this;
- }
- protected function setCurlMethod($method)
- {
- curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, strtoupper($method));
- return $this;
- }
- protected function setCurlPostFields($data)
- {
- curl_setopt($this->curl, CURLOPT_POST, true);
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
- return $this;
- }
- protected function setCurlLoginDetails($login, $password)
- {
- curl_setopt($this->curl,CURLOPT_USERPWD, $login . ':' . $password);
- return $this;
- }
- public function get($path)
- {
- return $this->execute(__FUNCTION__, $path);
- }
- public function post($path, $data)
- {
- return $this->execute(__FUNCTION__,$path, $data);
- }
- public function put($path, $data)
- {
- return $this->execute(__FUNCTION__, $path, $data);
- }
- public function patch($path, $data)
- {
- return $this->execute(__FUNCTION__, $path, $data);
- }
- public function delete($path, $data = null)
- {
- return $this->execute(__FUNCTION__, $path, $data);
- }
- public function isJson($json)
- {
- json_decode($json);
- return json_last_error() == JSON_ERROR_NONE;
- }
- protected function execute($method, $path, $data = [])
- {
- $url = $this->hostUrl . $path;
- $this->init()
- ->prepareOptions()
- ->setCurlLoginDetails($this->login, $this->password)
- ->setCurlUrl($url)
- ->setCurlMethod($method);
- if ($data)
- {
- $this->setCurlPostFields(json_encode($data));
- }
- $result = curl_exec($this->curl);
- \logModuleCall('dnsmanager', $url, $data, $result);
- if ($result === false)
- {
- throw new \Exception(curl_error($this->curl));
- }
- if($result == '')
- {
- $errorInfo = curl_getinfo($this->curl);
- if(!in_array($errorInfo['http_code'], [200, 204]))
- {
- throw new \Exception('HTTP Error: '.$errorInfo['http_code']);
- }
- }
- if ($result && !$this->isJson($result))
- {
- throw new \Exception($result);
- }
- return json_decode($result);
- }
- }
|