setServer($obj->getServer()); $this->setDomain($obj->name); $this->setClientID($obj->clientid); } if($obj instanceof Reverse) { $this->setServer($obj->getServer()); $this->setDomain($obj->getFullDomain()); $this->setIP($obj->ip); $this->setClientID($obj->clientid); $this->setTTL($obj->ttl); $this->setType($obj->type); $this->setRelId($obj->relid); } } public function setClientID($clientid) { $this->clientid = $clientid; } public function setType($type) { $this->type = $type; } public function setRelId($relid) { $this->relid = $relid; } /** * * @param mixed $server Server id lub model Server */ public function setServer($server) { if(!($server instanceof Server)) { $server = new Server($server); } $this->module = null; $this->server = $server; } /** * * @param mixed $domain nazwa domeny lub obiekt klasy DomainHelper */ public function setDomain($domain) { if(!($domain instanceof DomainHelper)) { $domain = new DomainHelper($domain); } $this->module = null; $this->domain = $domain; } /** * * @param mixed $ip IP jako string lub obiekt klasy dns\utils\IP */ public function setIP($ip) { if(!($ip instanceof IP)) { $ip = new IP($ip); } $this->ip = $ip; } public function setTTL($ttl) { $this->ttl = $ttl; } /** * Tworzenie rDNS na serwerze i w bazie * @throws ValidationException * @throws Exception */ public function create() { $this->validate(); if(Reverse::byServerIDAndIP($this->server->id, (string) $this->ip) !== FALSE) { throw new ValidationException('IP already taken', 103); } $this->updateRecordOnServer(); if($this->getPTRFromServer() == false) { throw new Exception("Something went wrong during PTR record creation", 1); } $this->createRecordInDB(); } /** * Usuwanie rDNS z serwera i bazy * @throws Exception */ public function remove() { $this->validate(); $this->removeRecordFromServer(); try{ if($this->getPTRFromServer() != false) { throw new Exception("Something went wrong during PTR record removing", 2); } } catch (Exception $ex) { //when getPTRFromServer() throw exception, all is correct; } $this->removeRecordFromDB(); } /** * Aktualizacja domeny na serwerze i w bazie */ public function update() { //domain only $this->validate(); $this->updateRecordOnServer(); $this->updateRecordInDB(); } public function updateRecordOnServer() { $this->getModule()->updateRDNS((string) $this->ip, $this->ttl, $this->domain->getFullName()); } public function removeRecordFromServer() { $this->getModule()->removeRDNS((string) $this->ip); } public function getPTRFromServer() { return $this->getModule()->getRDNSRecord((string) $this->ip); } public function createRecordInDB() { $reverse = new Reverse(); $reverse->serverid = $this->server->id; $reverse->from = $this->domain->getDomainWithTLD(); $reverse->sub = $this->domain->getSubdomain(); $reverse->name = ReverseDNSHelper::reverseZoneName($this->ip); $reverse->created_at= date('Y-m-d H:i:s'); $reverse->ip = (string) $this->ip; $reverse->ttl = $this->ttl; $reverse->clientid = $this->clientid; $reverse->type = $this->type; $reverse->relid = $this->relid; $reverse->save(); } public function removeRecordFromDB() { $reverse = Reverse::byServerIDAndIP($this->server->id, (string) $this->ip); if($reverse !== false) { $reverse->remove(); } } public function updateRecordInDB() { $reverse = Reverse::byServerIDAndIP($this->server->id, (string) $this->ip); if($reverse !== false) { $old = $reverse; $reverse->from = $this->domain->getDomainWithTLD(); $reverse->sub = $this->domain->getSubdomain(); $reverse->save(); $zoneLogger = new Manager($_SESSION['uid']); $zoneLogger->logEditRdns($old, $reverse); } } private function getModule() { if(!is_null($this->module)) { return $this->module; } $module = $this->server->getModule(); $module->setDomain($this->domain->getDomainWithTLD()); return $module; } private function validate() { if(is_null($this->server) || is_null($this->domain) || is_null($this->ip) || is_null($this->clientid)) { throw new ValidationException('Insufficient Data', 100); } if(!$this->ip->isValid()) { throw new ValidationException('Invalid IP', 1); } if(!$this->domain->isValid()) { throw new ValidationException('Invalid Domain', 2); } if(!$this->server->getModule()->isRDNSSupported()) { throw new ValidationException('rDNS is not supported by server', 101); } if($this->server->getSettings(ServerSettingEnum::ALLOW_RDNS) != 'on') { throw new ValidationException('rDNS is disabled on server', 102); } } /** * Remove rDNS from server and database * @throws Exception */ public function cleanRemove() { $this->validate(); if($this->removeRecordFromServer()) { $this->removeRecordFromServer(); if($this->getPTRFromServer() != false) { throw new Exception("Something went wrong during PTR record removing", 2); } } $this->removeRecordFromDB(); } public function toArray($uppercase = true) { $data = [ 'name' => $this->domain->getFullName(), 'ip' => $this->ip, 'ttl' => $this->ttl, 'server' => $this->server->name, ]; $out = []; foreach ($data as $key => $value) { $newKey = $key; if($uppercase === true) { $newKey = strtoupper($key); } $out[$newKey] = $value; } return $out; } }