DomainHelper.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\Helper;
  3. use ThurData\Servers\KerioEmail\Core\HandlerError\ErrorCodes\ErrorCodesLib;
  4. use ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception;
  5. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\DomainPricing;
  6. use ThurData\Servers\KerioEmail\Core\ModuleConstants;
  7. /**
  8. * Klasa walidująca domenę oraz rozbijająca je na poszczególne cześci
  9. */
  10. class DomainHelper
  11. {
  12. private $fullName;
  13. private $subdomain;
  14. private $domain;
  15. private $tld;
  16. private static $list = [];
  17. /**
  18. *
  19. * @param string $domain
  20. */
  21. public function __construct($domain)
  22. {
  23. $this->fullName = trim(strtolower($domain));
  24. $this->split();
  25. }
  26. private function loadTldList()
  27. {
  28. if (!empty(self::$list))
  29. {
  30. return;
  31. }
  32. $path = ModuleConstants::getFullPath("storage", "resources", "tld.list");
  33. if (!file_exists($path))
  34. {
  35. return;
  36. }
  37. $data = file($path);
  38. foreach ($data as $line)
  39. {
  40. /* Ignore blank lines and comments. */
  41. if (preg_match('#(^//)|(^\s*$)#', $line))
  42. {
  43. continue;
  44. }
  45. self::$list[] = preg_replace('/[\r\n]/', '', $line);
  46. }
  47. }
  48. private function split()
  49. {
  50. $this->loadTldList();
  51. $components = array_reverse((explode('.', $this->fullName)));
  52. $lastMatch = $this->getLastMatch($components, self::$list);
  53. if(empty($lastMatch))
  54. {
  55. $list = $this->getWhmcsTldList();
  56. $lastMatch = $this->getLastMatch($components, $list);
  57. if(empty($lastMatch))
  58. {
  59. throw new Exception(ErrorCodesLib::CORE_LIBS_DH_000001, ["domain" => $this->fullName]);
  60. }
  61. }
  62. $this->tld = $lastMatch;
  63. $noTld = preg_replace('/' . preg_quote($lastMatch) . '$/', '', $this->fullName);
  64. $noTld = trim($noTld, '.');
  65. $components = explode('.', $noTld);
  66. $this->domain = array_pop($components);
  67. $this->subdomain = implode('.', $components);
  68. }
  69. private function getLastMatch($components, $list)
  70. {
  71. $lastMatch = '';
  72. $con = '';
  73. foreach($components as $part)
  74. {
  75. $con = "{$part}.{$con}";
  76. $con = trim($con, '.');
  77. if(in_array($con, $list))
  78. {
  79. $lastMatch = $con;
  80. }
  81. }
  82. return $lastMatch;
  83. }
  84. private function getWhmcsTldList()
  85. {
  86. $end = substr($this->fullName, strrpos($this->fullName, "."));
  87. $domains = new DomainPricing();
  88. $tlds = $domains->where("extension", "LIKE", "%{$end}")->get()->pluck("extension");
  89. $list = array_map(function($value) {return trim($value, ".");}, $tlds->toArray());
  90. return $list;
  91. }
  92. /**
  93. * Pobieranie pełnej nazwy domeny
  94. * @return string
  95. */
  96. public function getFullName()
  97. {
  98. return $this->fullName;
  99. }
  100. /**
  101. * Pobieranie tld (bez kropki)
  102. * @return string
  103. */
  104. public function getTLD()
  105. {
  106. return $this->tld;
  107. }
  108. /**
  109. * Pobieranie tld
  110. * @return string
  111. */
  112. public function getTLDWithDot()
  113. {
  114. return ($this->tld != '') ? ("." . $this->tld) : "";
  115. }
  116. /**
  117. * Pobieranie nazwy głównej domeny (bez tld)
  118. * @return string
  119. */
  120. public function getDomain()
  121. {
  122. return $this->domain;
  123. }
  124. /**
  125. * Pobieranie nazwy głównej domeny z tld
  126. * @return string
  127. */
  128. public function getDomainWithTLD()
  129. {
  130. return $this->domain . '.' . $this->tld;
  131. }
  132. /**
  133. * Pobieranie subdomeny; zwraca pustry string jeżeli nie istnieje
  134. * @return string
  135. */
  136. public function getSubdomain()
  137. {
  138. return $this->subdomain;
  139. }
  140. /**
  141. * Sprawdzanie czy nazwa zawiera w sobie subdomenę
  142. * @return boolean
  143. */
  144. public function isSubdamain()
  145. {
  146. return !empty($this->subdomain);
  147. }
  148. /**
  149. * Sprawdzanie czy domena jest poprawna
  150. * @return boolean
  151. */
  152. public function isValid()
  153. {
  154. return !empty($this->domain) && !empty($this->tld);
  155. }
  156. }