SubmoduleAbstract.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns;
  3. use InvalidArgumentException;
  4. use MGModule\DNSManager2 as main;
  5. use MGModule\DNSManager2\mgLibs\custom\dns\utils\IDNAConvert;
  6. use MGModule\DNSManager2\mgLibs\custom\dns\utils\ReverseDNSHelper;
  7. use MGModule\DNSManager2\mgLibs\custom\manager\GlobalSettingHelper;
  8. use MGModule\DNSManager2\models\custom\globalsetting\GlobalSettingEnum;
  9. use MGModule\DNSManager2\models\custom\server\Server;
  10. //TODO: optymalizacja dla multiple delete?
  11. abstract class SubmoduleAbstract implements interfaces\SubmoduleConfigurationInterface, interfaces\SubmoduleInterface
  12. {
  13. protected $hasTTL = false;
  14. protected $supportRDNS = false;
  15. protected $requireIP = false;
  16. protected $supportImport = false;
  17. protected $supportDNSSEC = false;
  18. protected $isRemoveableDefaultServerRecordsSet = false;
  19. protected $availableTypes = [];
  20. protected $configFields = [];
  21. protected $domain = false;
  22. protected $config;
  23. protected $debug = false;
  24. protected $ip = '';
  25. /**
  26. * @var Server
  27. */
  28. protected $server = false;
  29. /**
  30. * @var main\mgLibs\custom\helpers\ZoneLogger\Manager
  31. */
  32. protected $zoneLoggerManager;
  33. public function __construct()
  34. {
  35. $this->supportRDNS = $this instanceof interfaces\SubmoduleRDNSInterface;
  36. $this->requireIP = $this instanceof interfaces\SubmoduleIPInterface;
  37. $this->hasTTL = $this instanceof interfaces\SubmoduleTTLInterface;
  38. $this->supportImport = $this instanceof interfaces\SubmoduleImportInterface;
  39. $this->supportDNSSEC = $this instanceof interfaces\SubmoduleDNSSecInterface;
  40. $this->isRemoveableDefaultServerRecordsSet = $this instanceof interfaces\SubmoduleRemoveDefaultRecords;
  41. }
  42. /**
  43. * @return Server
  44. */
  45. public function getServer()
  46. {
  47. return $this->server;
  48. }
  49. /**
  50. * @param Server $server
  51. */
  52. public function setServer(Server $server)
  53. {
  54. $this->server = $server;
  55. }
  56. public function getNameServers($index = false)
  57. {
  58. $nameservers = $this->server->getNameservers($index);
  59. $out = [];
  60. foreach ($nameservers as $nameserver)
  61. {
  62. if ('' != $nameserver->name)
  63. {
  64. $out[] = $nameserver->name;
  65. }
  66. }
  67. return $out;
  68. }
  69. public function setDomain($domain)
  70. {
  71. $idna = new IDNAConvert();
  72. $this->domain = $idna->encode($domain);
  73. }
  74. //TODO: podstawowe parsowanie błędóww!
  75. //TODO: sprawdzic specjalne znaki kodowane przez WHMCSa
  76. public function setConfiguration($config)
  77. {
  78. if (isset($config['password']))
  79. {
  80. $config['password'] = html_entity_decode($config['password']);
  81. }
  82. $this->config = $config;
  83. }
  84. public function getConfiguration()
  85. {
  86. return $this->configFields;
  87. }
  88. public function getAvailableRecordTypes()
  89. {
  90. $array = $this->availableTypes;
  91. sort($array);
  92. return $array;
  93. }
  94. public function isIPRequired()
  95. {
  96. return $this->requireIP;
  97. }
  98. public function isRDNSSupported()
  99. {
  100. return $this->supportRDNS;
  101. }
  102. public function isTTLEnabled()
  103. {
  104. return $this->hasTTL;
  105. }
  106. public function isImportSupported()
  107. {
  108. return $this->supportImport;
  109. }
  110. /**
  111. * Return true if DNSSEC is supported
  112. * @return bool
  113. */
  114. public function isDNSSECSupported()
  115. {
  116. return $this->supportDNSSEC;
  117. }
  118. public function enableDebug()
  119. {
  120. $this->debug = true;
  121. }
  122. public function disableDebug()
  123. {
  124. $this->debug = false;
  125. }
  126. protected function log($message, $type = 'INFO')
  127. { //TODO: dodać interfejs + domyślna klasa logera
  128. if (!$this->debug)
  129. {
  130. return;
  131. }
  132. $class = explode('\\', get_class($this));
  133. $path = __DIR__ . DIRECTORY_SEPARATOR . 'submodules' . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . array_pop($class);
  134. if (!file_exists($path))
  135. {
  136. @mkdir($path, 0777, true);
  137. }
  138. $filepath = $path . DIRECTORY_SEPARATOR . date('Y-m-d') . '.log';
  139. $line = date('Y-m-d H:i:s') . "\t" . $type . "\t" . $message . "\r\n";
  140. @file_put_contents($filepath, $line, FILE_APPEND);
  141. }
  142. public function hasRecordSupport($recordType)
  143. {
  144. if (in_array($recordType, $this->availableTypes))
  145. {
  146. return true;
  147. }
  148. return false;
  149. }
  150. public function isDisabledPopulateNs()
  151. {
  152. return $this->disabledPopulateNs;
  153. }
  154. //===========================================================================================================
  155. //===========================================STANDARD rDNS===================================================
  156. //===========================================================================================================
  157. protected function getClone($ip)
  158. {
  159. $clone = clone $this;
  160. $clone->setDomain(ReverseDNSHelper::reverseZoneName($ip));
  161. return $clone;
  162. }
  163. public function getRDNSRecord($ip)
  164. {
  165. $clone = $this->getClone($ip);
  166. foreach ($clone->getRecords('PTR') as $record)
  167. {
  168. if ($record->name == ReverseDNSHelper::reverseRecordName($ip)
  169. || $record->nameToRelative($clone->domain) == ReverseDNSHelper::reverseRecordName($ip))
  170. {
  171. return $record;
  172. }
  173. }
  174. return false;
  175. }
  176. public function getRDNSRecordsNumber($ip)
  177. {
  178. $clone = $this->getClone($ip);
  179. $result = $clone->getRecords('PTR');
  180. return count($result);
  181. }
  182. public function updateRDNS($ip, $ttl = false, $value = false)
  183. {
  184. if (!$this->isRDNSSupported())
  185. {
  186. throw new exceptions\DNSSubmoduleException('rDNS is not supported', SubmoduleExceptionCodes::COMMAND_ERROR);
  187. }
  188. $clone = $this->getClone($ip);
  189. if (!$clone->zoneExists())
  190. {
  191. $clone->activateZone();
  192. $this->addNSRecordsRDNS($clone);
  193. }
  194. $record = $this->getRDNSRecord($ip);
  195. if ($record === false)
  196. {
  197. $record = ReverseDNSHelper::createPTRRecord($ip, $ttl, $value ?: $this->domain);
  198. $clone->addRecord($record);
  199. return;
  200. }
  201. $record->rdata->setFirstProperty($value);
  202. $clone->editRecord($record);
  203. }
  204. public function addNSRecordsRDNS($clone)
  205. {
  206. $zone = (new main\models\custom\zone\Repository())->byName($this->domain)->one();
  207. if ($zone)
  208. {
  209. $defaultSet = $zone->getPackage()->getDefaultSet();
  210. }
  211. // if default record set is set for package
  212. if ($defaultSet)
  213. {
  214. $records = $defaultSet->getRecords();
  215. foreach ($records as $r)
  216. {
  217. if ($r->type === 'NS')
  218. {
  219. $record = new record\Record();
  220. $record->name = $r->name;
  221. $record->type = $r->type;
  222. $record->ttl = $r->ttl;
  223. $record->createRDATAObject('NS');
  224. $record->rdata->setFirstProperty($r->rdata['nsdname']);
  225. $clone->addRecord($record);
  226. $this->getLoggerManager()->logAddRecordToZone($zone, $record);
  227. }
  228. }
  229. }
  230. }
  231. public function removeRDNS($ip)
  232. {
  233. if (!$this->isRDNSSupported())
  234. {
  235. throw new exceptions\DNSSubmoduleException('rDNS is not supported', SubmoduleExceptionCodes::COMMAND_ERROR);
  236. }
  237. $clone = $this->getClone($ip);
  238. if (!$clone->zoneExists())
  239. {
  240. return;
  241. }
  242. $record = $this->getRDNSRecord($ip);
  243. if ($record)
  244. {
  245. $clone->deleteRecord($record);
  246. }
  247. $rdnsNumber = $this->getRDNSRecordsNumber($ip);
  248. if ($rdnsNumber < 1 && GlobalSettingHelper::getSetting(GlobalSettingEnum::DO_NOT_REMOVE_EMPTY_PTR_ZONES) !== 'on') // 1 cause w just deleted one record
  249. {
  250. $clone->terminateZone();
  251. }
  252. }
  253. public function wipeRecords()
  254. {
  255. $records = $this->getRecords();
  256. $cannotDelete = [];
  257. foreach ($records as &$record)
  258. {
  259. if ($record->type === 'SOA')
  260. {
  261. continue;
  262. }
  263. $recordName = $record->name;
  264. try
  265. {
  266. $this->deleteRecord($record);
  267. $records = $this->getRecords();
  268. }
  269. catch (\Exception $ex)
  270. {
  271. $record->name = $recordName;
  272. $cannotDelete[] = $record;
  273. continue;
  274. }
  275. }
  276. return $cannotDelete;
  277. }
  278. public function wipeRecordsArray(array $wipe)
  279. {
  280. $records = $this->getRecords();
  281. foreach ($wipe as $r)
  282. {
  283. foreach ($records as &$record)
  284. {
  285. if ($r->name == $record->name && $r->type == $record->type && $r->rdata == $record->rdata)
  286. {
  287. try
  288. {
  289. $r->line = $record->line;
  290. $this->deleteRecord($r);
  291. $records = $this->getRecords();
  292. }
  293. catch (\Exception $ex)
  294. {
  295. continue;
  296. }
  297. }
  298. }
  299. }
  300. return;
  301. }
  302. //===========================================================================================================
  303. //===========================================IP DEFAULT======================================================
  304. //===========================================================================================================
  305. public function setIP($ip)
  306. {
  307. $this->ip = $ip;
  308. }
  309. public function removeDefaultServerRecordsSet($defaultModuleRecords)
  310. {
  311. if ($this->isRemoveableDefaultServerRecordsSet)
  312. {
  313. $this->removeDefaultServerRecords($defaultModuleRecords);
  314. }
  315. }
  316. public function isRemoveableDefaultServerRecordsSet()
  317. {
  318. return $this->isRemoveableDefaultServerRecordsSet;
  319. }
  320. public function hasCustomEditRecords()
  321. {
  322. return $this instanceof interfaces\SubmoduleCustomEditRecords;
  323. }
  324. public function getLoggerManager()
  325. {
  326. if (!$this->zoneLoggerManager)
  327. {
  328. $this->zoneLoggerManager = new main\mgLibs\custom\helpers\ZoneLogger\Manager($_SESSION['uid']);
  329. }
  330. return $this->zoneLoggerManager;
  331. }
  332. public function getDomain()
  333. {
  334. return $this->domain;
  335. }
  336. public function validateRecord($record)
  337. {
  338. foreach ($this->validators() as $validatorName => $validatingFunction)
  339. {
  340. if (!$validatingFunction($record->{$validatorName}, $record->type))
  341. {
  342. $langName = strtolower($record->type) . ucfirst($validatorName) . "ValidationError";
  343. $errorMessage = main\mgLibs\lang::absoluteT('addonAA', $langName);
  344. $errorMessage = (!$errorMessage || $langName == $errorMessage)
  345. ? main\mgLibs\lang::absoluteT('addonAA', 'defaultNameValidationError')
  346. : $errorMessage;
  347. throw new InvalidArgumentException($errorMessage);
  348. }
  349. }
  350. $record->rdata->validate();
  351. }
  352. protected function validators(): array
  353. {
  354. return [
  355. 'name' =>
  356. function (string $name, string $type = '')
  357. {
  358. $idna = new IDNAConvert();
  359. $name = $name == '@' ? $name : $idna->encode($name);
  360. switch (strtolower($type))
  361. {
  362. case 'srv':
  363. return preg_match("(_\w+\._\w+\.*)", $name) == 1;
  364. default:
  365. return preg_match("/^[@\w.-]+$/", $name) == 1;
  366. }
  367. }
  368. ];
  369. }
  370. }