CheckMoreDomains.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Libs\Api\ENom\Method;
  3. use ModulesGarden\ProxmoxAddon\Core\Helper\DomainHelper;
  4. /**
  5. * Description of CheckMoreDomains
  6. *
  7. * @author Rafał Ossowski <rafal.os@modulesgarden.com>
  8. */
  9. class CheckMoreDomains extends AbstractMethod
  10. {
  11. public function execute($records)
  12. {
  13. $returnData = [];
  14. $domainList = $this->getDomainList($records);
  15. if (count($domainList) > 30)
  16. {
  17. while (!empty($domainList))
  18. {
  19. $returnData = array_merge(
  20. $returnData, $this->checkDomainList(array_slice($domainList, 0, 30))
  21. );
  22. array_splice($domainList, 0, 30);
  23. }
  24. }
  25. else
  26. {
  27. $returnData = $this->checkDomainList($domainList);
  28. }
  29. return $returnData;
  30. }
  31. protected function getDomainList($records = [])
  32. {
  33. $domainList = [];
  34. foreach ($records as $record)
  35. {
  36. $domainList[] = $this->idnaConvert->encode(strtolower($record['sld'])) . "."
  37. . $this->idnaConvert->encode(strtolower($record['tld']));
  38. }
  39. return $domainList;
  40. }
  41. protected function checkDomainList($domainList)
  42. {
  43. $response = $this->request->post([
  44. 'Command' => 'check',
  45. 'DomainList' => str_replace(' ', '', implode(',', $domainList)),
  46. 'ResponseType' => 'xml',
  47. 'UID' => $this->settings['username'],
  48. 'PW' => $this->settings['password']
  49. ]);
  50. if ($response && $response->isSuccess())
  51. {
  52. $response = $response->getBody();
  53. if ($response->Done == 'true')
  54. {
  55. return $this->formatResponse($response);
  56. }
  57. }
  58. return false;
  59. }
  60. protected function formatResponse($response)
  61. {
  62. $result = [];
  63. foreach ($response as $key => $value)
  64. {
  65. switch ($key)
  66. {
  67. case 'DomainCount':
  68. case 'IsPremiumName':
  69. case 'PremiumPrice':
  70. case 'PremiumAboveThresholdPrice':
  71. case 'Command':
  72. case 'ErrCount':
  73. case 'ErrX':
  74. case 'Done':
  75. continue;
  76. default :
  77. $this->parserResponse($result, $this->convertDomain($key, $value));
  78. }
  79. }
  80. return $result;
  81. }
  82. protected function parserResponse(&$result, $data)
  83. {
  84. switch ($data['key'])
  85. {
  86. case 'Domain':
  87. $result[$data['name']]['sld'] = $data['value']->getDomain();
  88. $result[$data['name']]['tld'] = $data['value']->getTLDWithDot();
  89. break;
  90. case 'RRPCode':
  91. $result[$data['name']]['status'] = $data['value'];
  92. break;
  93. }
  94. }
  95. protected function convertDomain($key, $value)
  96. {
  97. $name = $this->getName($key);
  98. if (strpos($key, 'Domain') !== false)
  99. {
  100. $key = 'Domain';
  101. $value = new DomainHelper($this->idnaConvert->decode($value));
  102. }
  103. elseif (strpos($key, 'RRPCode') !== false)
  104. {
  105. $key = 'RRPCode';
  106. $value = $this->choseResult($value);
  107. }
  108. else
  109. {
  110. $key = null;
  111. }
  112. return [
  113. 'name' => $name,
  114. 'key' => $key,
  115. 'value' => $value
  116. ];
  117. }
  118. protected function getName($key)
  119. {
  120. return substr($key, (strpos($key, 'Domain') !== false) ? 6 : 7);
  121. }
  122. protected function choseResult($value)
  123. {
  124. switch ($value)
  125. {
  126. case 210:
  127. return self::RESPONSE_RESULT_FREE;
  128. case 211:
  129. return self::RESPONSE_RESULT_TAKEN;
  130. default :
  131. return self::RESPONSE_RESULT_ERROR;
  132. }
  133. }
  134. }