Uapi.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules\cPanel;
  3. use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
  4. class Uapi
  5. {
  6. protected $ch = null;
  7. protected $token = null;
  8. protected $expires;
  9. protected $username;
  10. protected $password;
  11. protected $host;
  12. protected $port;
  13. protected $headers = [];
  14. protected $ssl;
  15. protected $sessionUrl;
  16. protected $queryuser;
  17. private $lastResponse;
  18. private $lastJsonResponse;
  19. public function setLogin($params, $user)
  20. {
  21. $this->host = $params['hostname'] ? $params['hostname'] : $params['default_ip'];
  22. $this->username = $params['username'];
  23. $this->password = $params['hash'] ? trim($params['hash']) : $params['password'];
  24. $this->headers[] = $params['hash'] ? 'Authorization: WHM ' . $this->username . ':' . str_replace(array("\r", "\n"), "", $this->password) : "Authorization: Basic " . base64_encode($this->username . ":" . $this->password);
  25. if($params['ssl'] == 'on')
  26. $this->ssl = true;
  27. else
  28. $this->ssl = false;
  29. $this->port = $params['serverport'] ? $params['serverport'] : '2087';
  30. $this->queryuser = $user;
  31. return $this;
  32. }
  33. public function createSession($service='cpaneld')
  34. {
  35. $query = ($this->ssl ? 'https' : 'http') . "://" . $this->host . ":" . $this->port
  36. . "/json-api/create_user_session?api.version=1&user=" . $this->queryuser . "&service=".$service;
  37. $this->ch = curl_init();
  38. curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
  39. curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
  40. curl_setopt($this->ch, CURLOPT_HEADER, false);
  41. curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  42. curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers);
  43. curl_setopt($this->ch, CURLOPT_URL, $query);
  44. $response = curl_exec($this->ch);
  45. if (!$response)
  46. {
  47. $error = curl_error($this->ch);
  48. curl_close($this->ch);
  49. throw new exceptions\DNSSubmoduleException("Curl error: " . $error);
  50. }
  51. $jsonResponse = json_decode($response, true);
  52. if ($jsonResponse['cpanelresult']['error'])
  53. {
  54. throw new exceptions\DNSSubmoduleException("UAPI error: " . $jsonResponse['cpanelresult']['error']);
  55. }else if($jsonResponse['metadata']['result']===0){
  56. throw new exceptions\DNSSubmoduleException("UAPI error: " . $jsonResponse['metadata']['reason']);
  57. }
  58. $this->initSession($jsonResponse);
  59. }
  60. private function initSession($jsonResponse)
  61. {
  62. $tab = explode(':', $jsonResponse['data']['url']);
  63. $sessionUrl = $tab[0] . '://' . $this->host . ':' . $tab[2];
  64. $cookieJar = 'cookie.txt';
  65. curl_setopt($this->ch, CURLOPT_URL, $sessionUrl);
  66. curl_setopt($this->ch, CURLOPT_HTTPHEADER, null);
  67. curl_setopt($this->ch, CURLOPT_COOKIESESSION, true);
  68. curl_setopt($this->ch, CURLOPT_COOKIEJAR, $cookieJar);
  69. curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookieJar);
  70. $response = curl_exec($this->ch);
  71. if (!$response)
  72. {
  73. $error = curl_error($this->ch);
  74. curl_close($this->ch);
  75. throw new exceptions\DNSSubmoduleException("Curl error: " . $error);
  76. }
  77. $this->sessionUrl = preg_replace('{/login(?:/)??.*}', '', $sessionUrl);
  78. }
  79. public function exec($name, $api, $request = array())
  80. {
  81. $this->requestUrl = $this->sessionUrl . $api . $name;
  82. if ($request)
  83. {
  84. $this->requestUrl .= '?' . http_build_query($request);
  85. }
  86. curl_setopt($this->ch, CURLOPT_URL, $this->requestUrl);
  87. $this->lastResponse = curl_exec($this->ch);
  88. $this->debug();
  89. if (!$this->lastResponse)
  90. {
  91. $error = curl_error($this->ch);
  92. curl_close($this->ch);
  93. throw new exceptions\DNSSubmoduleException("Curl error: " . $error);
  94. }
  95. return $this->processResponse();
  96. }
  97. private function processResponse()
  98. {
  99. $replace = [
  100. '\\\n' => "",
  101. '\\\u2014' => " - ",
  102. '"[{' => '[{',
  103. '}]"' => '}]',
  104. '\"' => '"',
  105. '\\\\' => ''
  106. ];
  107. $response = str_replace(array_keys($replace), array_values($replace), $this->lastResponse);
  108. $this->lastJsonResponse = json_decode($response, true);
  109. if ($this->lastJsonResponse['errors'])
  110. {
  111. throw new exceptions\DNSSubmoduleException("UAPI error: " . trim(implode(", ", $this->lastJsonResponse['errors'])));
  112. }
  113. if ($this->lastJsonResponse['data'] && preg_match('/Warning/', $this->lastJsonResponse['data']) && !preg_match('/Success/', $this->lastJsonResponse['data']))
  114. {
  115. throw new exceptions\DNSSubmoduleException("UAPI error: " . trim($this->lastJsonResponse['data']));
  116. }
  117. return $this->lastJsonResponse;
  118. }
  119. private function debug()
  120. {
  121. if (!$this->loger || !method_exists($this->loger, 'log'))
  122. {
  123. return;
  124. }
  125. $action = print_r(sprintf("GET: %s\r\nPOST: %s\r\n", $this->requestUrl, print_r($this->post, true)), true);
  126. $this->loger->log(str_replace($this->sessionUrl, "",$this->requestUrl), $action, $this->lastResponse, $this->jsonResponse);
  127. }
  128. function getLastResponse()
  129. {
  130. return $this->lastResponse;
  131. }
  132. function setLastResponse($lastResponse)
  133. {
  134. $this->lastResponse = $lastResponse;
  135. }
  136. }