DomainHelper.php 3.4 KB

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