ReverseDNS.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\reverse;
  3. use \Exception;
  4. use \MGModule\DNSManager2\mgLibs\custom\dns\utils\IP;
  5. use \MGModule\DNSManager2\mgLibs\custom\dns\utils\ReverseDNSHelper;
  6. use \MGModule\DNSManager2\mgLibs\custom\exceptions\ValidationException;
  7. use \MGModule\DNSManager2\mgLibs\custom\helpers\DomainHelper;
  8. use MGModule\DNSManager2\mgLibs\custom\helpers\ZoneLogger\Manager;
  9. use \MGModule\DNSManager2\models\custom\reverse\Reverse;
  10. use \MGModule\DNSManager2\models\custom\server\Server;
  11. use \MGModule\DNSManager2\models\custom\server\setting\ServerSettingEnum;
  12. use \MGModule\DNSManager2\models\custom\zone\Zone;
  13. use MGModule\DNSManager2\mgLibs\custom\dns\record\interfaces\ArrayPrinter;
  14. /**
  15. * Klasa pomagająca operować na rDNS
  16. */
  17. class ReverseDNS implements ArrayPrinter{
  18. /** @var Server */
  19. private $server;
  20. /** @var DomainHelper */
  21. private $domain;
  22. /** @var IP */
  23. private $ip;
  24. private $ttl = 14400;
  25. private $module;
  26. private $clientid;
  27. private $relid;
  28. private $type;
  29. /**
  30. *
  31. * @param mixed $obj Model Zone albo Reverse, pobiera z tych obiektów automatycznie potrzebne dane
  32. */
  33. function __construct($obj = false)
  34. {
  35. if($obj instanceof Zone) {
  36. $this->setServer($obj->getServer());
  37. $this->setDomain($obj->name);
  38. $this->setClientID($obj->clientid);
  39. }
  40. if($obj instanceof Reverse) {
  41. $this->setServer($obj->getServer());
  42. $this->setDomain($obj->getFullDomain());
  43. $this->setIP($obj->ip);
  44. $this->setClientID($obj->clientid);
  45. $this->setTTL($obj->ttl);
  46. $this->setType($obj->type);
  47. $this->setRelId($obj->relid);
  48. }
  49. }
  50. public function setClientID($clientid) {
  51. $this->clientid = $clientid;
  52. }
  53. public function setType($type)
  54. {
  55. $this->type = $type;
  56. }
  57. public function setRelId($relid)
  58. {
  59. $this->relid = $relid;
  60. }
  61. /**
  62. *
  63. * @param mixed $server Server id lub model Server
  64. */
  65. public function setServer($server) {
  66. if(!($server instanceof Server)) {
  67. $server = new Server($server);
  68. }
  69. $this->module = null;
  70. $this->server = $server;
  71. }
  72. /**
  73. *
  74. * @param mixed $domain nazwa domeny lub obiekt klasy DomainHelper
  75. */
  76. public function setDomain($domain) {
  77. if(!($domain instanceof DomainHelper)) {
  78. $domain = new DomainHelper($domain);
  79. }
  80. $this->module = null;
  81. $this->domain = $domain;
  82. }
  83. /**
  84. *
  85. * @param mixed $ip IP jako string lub obiekt klasy dns\utils\IP
  86. */
  87. public function setIP($ip) {
  88. if(!($ip instanceof IP)) {
  89. $ip = new IP($ip);
  90. }
  91. $this->ip = $ip;
  92. }
  93. public function setTTL($ttl) {
  94. $this->ttl = $ttl;
  95. }
  96. /**
  97. * Tworzenie rDNS na serwerze i w bazie
  98. * @throws ValidationException
  99. * @throws Exception
  100. */
  101. public function create() {
  102. $this->validate();
  103. if(Reverse::byServerIDAndIP($this->server->id, (string) $this->ip) !== FALSE) {
  104. throw new ValidationException('IP already taken', 103);
  105. }
  106. $this->updateRecordOnServer();
  107. if($this->getPTRFromServer() == false) {
  108. throw new Exception("Something went wrong during PTR record creation", 1);
  109. }
  110. $this->createRecordInDB();
  111. }
  112. /**
  113. * Usuwanie rDNS z serwera i bazy
  114. * @throws Exception
  115. */
  116. public function remove() {
  117. $this->validate();
  118. $this->removeRecordFromServer();
  119. try{
  120. if($this->getPTRFromServer() != false) {
  121. throw new Exception("Something went wrong during PTR record removing", 2);
  122. }
  123. } catch (Exception $ex) {
  124. //when getPTRFromServer() throw exception, all is correct;
  125. }
  126. $this->removeRecordFromDB();
  127. }
  128. /**
  129. * Aktualizacja domeny na serwerze i w bazie
  130. */
  131. public function update() { //domain only
  132. $this->validate();
  133. $this->updateRecordOnServer();
  134. $this->updateRecordInDB();
  135. }
  136. public function updateRecordOnServer() {
  137. $this->getModule()->updateRDNS((string) $this->ip, $this->ttl, $this->domain->getFullName());
  138. }
  139. public function removeRecordFromServer() {
  140. $this->getModule()->removeRDNS((string) $this->ip);
  141. }
  142. public function getPTRFromServer() {
  143. return $this->getModule()->getRDNSRecord((string) $this->ip);
  144. }
  145. public function createRecordInDB() {
  146. $reverse = new Reverse();
  147. $reverse->serverid = $this->server->id;
  148. $reverse->from = $this->domain->getDomainWithTLD();
  149. $reverse->sub = $this->domain->getSubdomain();
  150. $reverse->name = ReverseDNSHelper::reverseZoneName($this->ip);
  151. $reverse->created_at= date('Y-m-d H:i:s');
  152. $reverse->ip = (string) $this->ip;
  153. $reverse->ttl = $this->ttl;
  154. $reverse->clientid = $this->clientid;
  155. $reverse->type = $this->type;
  156. $reverse->relid = $this->relid;
  157. $reverse->save();
  158. }
  159. public function removeRecordFromDB() {
  160. $reverse = Reverse::byServerIDAndIP($this->server->id, (string) $this->ip);
  161. if($reverse !== false) {
  162. $reverse->remove();
  163. }
  164. }
  165. public function updateRecordInDB() {
  166. $reverse = Reverse::byServerIDAndIP($this->server->id, (string) $this->ip);
  167. if($reverse !== false) {
  168. $old = $reverse;
  169. $reverse->from = $this->domain->getDomainWithTLD();
  170. $reverse->sub = $this->domain->getSubdomain();
  171. $reverse->save();
  172. $zoneLogger = new Manager($_SESSION['uid']);
  173. $zoneLogger->logEditRdns($old, $reverse);
  174. }
  175. }
  176. private function getModule() {
  177. if(!is_null($this->module)) {
  178. return $this->module;
  179. }
  180. $module = $this->server->getModule();
  181. $module->setDomain($this->domain->getDomainWithTLD());
  182. return $module;
  183. }
  184. private function validate() {
  185. if(is_null($this->server) || is_null($this->domain) || is_null($this->ip) || is_null($this->clientid)) {
  186. throw new ValidationException('Insufficient Data', 100);
  187. }
  188. if(!$this->ip->isValid()) {
  189. throw new ValidationException('Invalid IP', 1);
  190. }
  191. if(!$this->domain->isValid()) {
  192. throw new ValidationException('Invalid Domain', 2);
  193. }
  194. if(!$this->server->getModule()->isRDNSSupported()) {
  195. throw new ValidationException('rDNS is not supported by server', 101);
  196. }
  197. if($this->server->getSettings(ServerSettingEnum::ALLOW_RDNS) != 'on') {
  198. throw new ValidationException('rDNS is disabled on server', 102);
  199. }
  200. }
  201. /**
  202. * Remove rDNS from server and database
  203. * @throws Exception
  204. */
  205. public function cleanRemove()
  206. {
  207. $this->validate();
  208. if($this->removeRecordFromServer())
  209. {
  210. $this->removeRecordFromServer();
  211. if($this->getPTRFromServer() != false)
  212. {
  213. throw new Exception("Something went wrong during PTR record removing", 2);
  214. }
  215. }
  216. $this->removeRecordFromDB();
  217. }
  218. public function toArray($uppercase = true)
  219. {
  220. $data = [
  221. 'name' => $this->domain->getFullName(),
  222. 'ip' => $this->ip,
  223. 'ttl' => $this->ttl,
  224. 'server' => $this->server->name,
  225. ];
  226. $out = [];
  227. foreach ($data as $key => $value)
  228. {
  229. $newKey = $key;
  230. if($uppercase === true)
  231. {
  232. $newKey = strtoupper($key);
  233. }
  234. $out[$newKey] = $value;
  235. }
  236. return $out;
  237. }
  238. }