| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\reverse;
- use \Exception;
- use \MGModule\DNSManager2\mgLibs\custom\dns\utils\IP;
- use \MGModule\DNSManager2\mgLibs\custom\dns\utils\ReverseDNSHelper;
- use \MGModule\DNSManager2\mgLibs\custom\exceptions\ValidationException;
- use \MGModule\DNSManager2\mgLibs\custom\helpers\DomainHelper;
- use MGModule\DNSManager2\mgLibs\custom\helpers\ZoneLogger\Manager;
- use \MGModule\DNSManager2\models\custom\reverse\Reverse;
- use \MGModule\DNSManager2\models\custom\server\Server;
- use \MGModule\DNSManager2\models\custom\server\setting\ServerSettingEnum;
- use \MGModule\DNSManager2\models\custom\zone\Zone;
- use MGModule\DNSManager2\mgLibs\custom\dns\record\interfaces\ArrayPrinter;
- /**
- * Klasa pomagająca operować na rDNS
- */
- class ReverseDNS implements ArrayPrinter{
- /** @var Server */
- private $server;
- /** @var DomainHelper */
- private $domain;
- /** @var IP */
- private $ip;
- private $ttl = 14400;
- private $module;
- private $clientid;
- private $relid;
- private $type;
- /**
- *
- * @param mixed $obj Model Zone albo Reverse, pobiera z tych obiektów automatycznie potrzebne dane
- */
- function __construct($obj = false)
- {
- if($obj instanceof Zone) {
- $this->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;
- }
- }
|