| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\helpers;
- /**
- * Klasa walidująca domenę oraz rozbijająca je na poszczególne cześci
- */
- class DomainHelper
- {
- private $fullName;
- private $subdomain;
- private $domain;
- private $tld;
- private static $list = array();
- /**
- *
- * @param string $domain
- */
- public function __construct($domain)
- {
- $this->fullName = trim(strtolower($domain));
- $this->split();
- }
- private function loadTldList() //TODO: dodać ładowanie
- {
- if(!empty(self::$list)) {
- return;
- }
- $path = \MGModule\DNSManager2\addon::getMainDIR() . DS . 'storage' . DS . 'tld.list';
- if(!file_exists($path)) {
- return;
- }
-
- $data = file($path);
- foreach($data as $line) {
- /* Ignore blank lines and comments. */
- if(preg_match('#(^//)|(^\s*$)#', $line))
- continue;
- self::$list[] = preg_replace('/[\r\n]/', '', $line);
- }
- $this->loadDomainExtensionFromWHMCS();
- }
- private function loadDomainExtensionFromWHMCS(){
- $extension = new \MGModule\DNSManager2\models\whmcs\domains\pricing\repository();
- foreach($extension->get() as $single){
- self::$list[] = substr($single->extension, 1);
- }
-
- }
- private function split()
- {
- $this->loadTldList();
- $components = array_reverse((explode('.', $this->fullName)));
- $last_match = '';
- $con = '';
- foreach($components as $part) {
- $con = "{$part}.{$con}";
- $con = trim($con, '.');
- if(in_array($con, self::$list)) {
- $last_match = $con;
- }
- }
-
- $this->tld = $last_match;
- $no_tld = preg_replace('/' . preg_quote($last_match) . '$/', '', $this->fullName);
- $no_tld = trim($no_tld, '.');
- $components = explode('.', $no_tld);
- $this->domain = array_pop($components);
- $this->subdomain = implode('.', $components);
- }
- /**
- * Pobieranie pełnej nazwy domeny
- * @return string
- */
- public function getFullName()
- {
- return $this->fullName;
- }
- /**
- * Pobieranie tld (bez kropki)
- * @return string
- */
- public function getTLD()
- {
- return $this->tld;
- }
- /**
- * Pobieranie nazwy głównej domeny (bez tld)
- * @return string
- */
- public function getDomain()
- {
- return $this->domain;
- }
-
- /**
- * Pobieranie nazwy głównej domeny z tld
- * @return string
- */
- public function getDomainWithTLD() {
- return $this->domain . '.' . $this->tld;
- }
- /**
- * Pobieranie subdomeny; zwraca pustry string jeżeli nie istnieje
- * @return string
- */
- public function getSubdomain()
- {
- return $this->subdomain;
- }
-
- /**
- * Sprawdzanie czy nazwa zawiera w sobie subdomenę
- * @return boolean
- */
- public function isSubdamain() {
- return !empty($this->subdomain);
- }
-
- /**
- * Sprawdzanie czy domena jest poprawna
- * @return boolean
- */
- public function isValid() {
- return !empty($this->domain) && !empty($this->tld);
- }
- }
|