FastLookup.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Libs\Api\OpenSRS;
  3. use ModulesGarden\ProxmoxAddon\Core\Helper;
  4. class FastLookup
  5. {
  6. private $_socket = false;
  7. private $_socketErrorNum = false;
  8. private $_socketErrorMsg = false;
  9. private $_socketTimeout = 120;
  10. private $dataObject;
  11. private $dataFormat;
  12. public function checkDomain($domain)
  13. {
  14. if (!$this->init_socket())
  15. {
  16. throw new APIException('oSRS Error - Unable to establish socket: (' . $this->_socketErrorNum . ') ' . $this->_socketErrorMsg);
  17. }
  18. $callCheck = 'check_domain ' . $domain;
  19. $callLength = strlen($callCheck);
  20. fputs($this->_socket, $callCheck, $callLength);
  21. $result = fread($this->_socket, 1024);
  22. $data = $this->parseResult($result);
  23. // Close the socket
  24. $this->close_socket();
  25. return $data;
  26. }
  27. private function init_socket()
  28. {
  29. $register = Helper\sl('register');
  30. $this->_socket = pfsockopen($register->registry('OSRS_HOST'), $register->registry('OSRS_FASTLOOKUP_PORT'), $this->_socketErrorNum, $this->_socketErrorMsg, $this->_socketTimeout);
  31. if (!$this->_socket)
  32. {
  33. return false;
  34. }
  35. else
  36. {
  37. return true;
  38. }
  39. }
  40. private function parseResult($resString)
  41. {
  42. if ($resString != '')
  43. {
  44. $temArra = explode(' ', $resString);
  45. if ($temArra[0] == 210)
  46. {
  47. $resultReturn = 'Available';
  48. }
  49. elseif ($temArra[0] == 211)
  50. {
  51. $resultReturn = 'Taken';
  52. }
  53. elseif ($temArra[0] == 701)
  54. {
  55. $resultReturn = 'Unknown TLD';
  56. }
  57. else
  58. {
  59. $resultReturn = 'Syntax error - ' . $temArra[0];
  60. }
  61. }
  62. else
  63. {
  64. $resultReturn = 'Read error';
  65. }
  66. return $resultReturn | '';
  67. }
  68. private function close_socket()
  69. {
  70. fclose($this->_socket);
  71. $this->_socket = false;
  72. }
  73. public function send($tlds = [])
  74. {
  75. $result = $this->checkDomainBunch($this->getDomain(), $tlds);
  76. $this->resultFullRaw = $result;
  77. $this->resultRaw = $result;
  78. $this->resultFullFormatted = $this->convertArray2Formatted($this->_formatHolder, $this->resultFullRaw);
  79. $this->resultFormatted = $this->convertArray2Formatted($this->_formatHolder, $this->resultRaw);
  80. }
  81. public function checkDomainBunch($domain, $tlds)
  82. {
  83. if (!$this->init_socket())
  84. {
  85. throw new APIException('oSRS Error - Unable to establish socket: (' . $this->_socketErrorNum . ') ' . $this->_socketErrorMsg);
  86. }
  87. if (preg_match("/(^.+)\.(.+)\.(.+)/", $domain, $matches) > 0)
  88. {
  89. $domain = $matches[1];
  90. }
  91. elseif (preg_match("/(^.+)\.(.+)/", $domain, $matches) > 0)
  92. {
  93. $domain = $matches[1];
  94. }
  95. $resultArray = [];
  96. foreach ($tlds as $tld)
  97. {
  98. $callCheck = 'check_domain ' . $domain . $tld;
  99. $callLength = strlen($callCheck);
  100. fputs($this->_socket, $callCheck, $callLength);
  101. $result = fread($this->_socket, 1024);
  102. $checkRes = $this->parseResult($result);
  103. $loopAray = [];
  104. $loopAray['domain'] = $domain;
  105. $loopAray['tld'] = $tld;
  106. $loopAray['result'] = $checkRes;
  107. array_push($resultArray, $loopAray);
  108. }
  109. $this->close_socket();
  110. return $resultArray;
  111. }
  112. /**
  113. * Get the domain from the dataObject.
  114. */
  115. public function getDomain()
  116. {
  117. return $this->dataObject->data->domain;
  118. }
  119. public function convertArray2Formatted($type = '', $data = '')
  120. {
  121. $resultString = '';
  122. if ($type == 'json')
  123. {
  124. $resultString = json_encode($data);
  125. }
  126. if ($type == 'yaml')
  127. {
  128. $resultString = Spyc::YAMLDump($data);
  129. }
  130. return $resultString;
  131. }
  132. public function setDataObject($format, $dataObject)
  133. {
  134. $this->dataObject = $dataObject;
  135. $this->dataFormat = $format;
  136. }
  137. /**
  138. * Does the dataObject have a domain set?
  139. *
  140. * @return bool
  141. */
  142. public function hasDomain()
  143. {
  144. return isset($this->dataObject->data->domain);
  145. }
  146. }