| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\manager;
- use MGModule\DNSManager2\mgLibs\custom\dns;
- use MGModule\DNSManager2\models\custom\set;
- use MGModule\DNSManager2\models\custom\zone\Zone;
- class RecordHelper {
- private $record = false;
- private $zone;
- private $serverConfiguration;
- private $nameservers;
- public function __construct(set\record\SetRecord $record, Zone $zone, $nameservers = null) {
- $this->record = $record;
- $this->zone = $zone;
- $this->serverConfiguration = $zone->getServer()->getModuleConfiguration();
- $this->nameservers = $nameservers;
- }
-
- /**
- *
- * @param type $domain
- * @param type $ip
- * @return \MGModule\DNSManager2\mgLibs\custom\dns\record\Record
- */
- public function toDNSRecord($domain, $ip = '') {
- $record = new dns\record\Record();
- $record->name = $this->getNameWithReplacedValues($domain, $ip);
- $record->type = $this->record->type;
- $record->ttl = $this->record->ttl;
- $record->createRDATAObject();
- $record->rdata->setDataFromArray($this->getRDATAWithReplacedValues($domain, $ip));
- return $record;
- }
-
- public function getNameWithReplacedValues($domain, $ip = '') {
- return $this->replaceValues($this->record->name, $domain, $ip);
- }
-
- public function getRDATAWithReplacedValues($domain, $ip = '') {
- $out = array();
- foreach($this->record->rdata as $k => $v) {
- $out[$k] = $this->replaceValues($v, $domain, $ip);
- }
- return $out;
- }
-
- private function replaceValues($value, $domain, $ip = '') {
- $splitDomain = $this->splitDomain($domain);
- $a = str_replace('{$domain}', $domain, $value);
- $b = str_replace('{$domainname}', $splitDomain['domainname'], $a);
- $c = str_replace('{$domainextension}', $splitDomain['extension'], $b);
- $d = str_replace('{$serverhostname}', $this->serverConfiguration['hostname'], $c);
- foreach ( range(1, 5) as $i )
- {
- $search = '{$ns' . $i . '}';
- $replace = (is_array($this->nameservers) && $this->nameservers[$i - 1]) ? $this->nameservers[$i - 1] : '';
- $d = str_replace($search, $replace, $d);
- }
- return str_replace('{$ip}', $ip?:'', $d);
- }
- private function splitDomain($domain)
- {
- $domain = explode('.', $domain);
- if(count($domain) > 2)
- {
- $newDomain = array();
- $newDomain['domainname'] = $domain[0];
- foreach($domain as $k => $v)
- {
- if($domain[$k+1])
- {
- $newDomain['extension'] .= $newDomain['extension'] ? '.' . $domain[$k+1] : $domain[$k+1];
- }
- }
-
- return $newDomain;
- }
-
- return array(
- 'domainname'=> $domain[0],
- 'extension' => $domain[1]);
- }
-
- /** @return dns\Record */
- public function obj() {
- return $this->record;
- }
- }
|