DomainHelper.php 3.2 KB

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