zones.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. <?php
  2. namespace MGModule\DNSManager2\controllers\addon\admin;
  3. use Exception;
  4. use MGModule\DNSManager2 as main;
  5. use MGModule\DNSManager2\mgLibs\custom;
  6. use MGModule\DNSManager2\mgLibs\custom\AjaxResponse;
  7. use MGModule\DNSManager2\mgLibs\custom\dns\record\Record;
  8. use MGModule\DNSManager2\mgLibs\custom\dns\utils\IP;
  9. use MGModule\DNSManager2\mgLibs\custom\helpers\RecordSetHelper;
  10. use MGModule\DNSManager2\mgLibs\custom\helpers\ZoneLogger\Manager as ZoneLoggerManager;
  11. use MGModule\DNSManager2\mgLibs\custom\manager\ClientHelper;
  12. use MGModule\DNSManager2\mgLibs\custom\manager\DefaultNotifications;
  13. use MGModule\DNSManager2\mgLibs\custom\manager\EmailNotificationHelper;
  14. use MGModule\DNSManager2\mgLibs\custom\manager\GlobalSettingHelper;
  15. use MGModule\DNSManager2\mgLibs\custom\manager\LogHelper;
  16. use MGModule\DNSManager2\mgLibs\custom\manager\ZoneCreator;
  17. use MGModule\DNSManager2\models\custom\globalsetting\GlobalSettingEnum;
  18. use MGModule\DNSManager2\models\custom\reverse;
  19. use MGModule\DNSManager2\models\custom\set;
  20. use MGModule\DNSManager2\models\custom\zone;
  21. use \MGModule\DNSManager2\models\custom\package;
  22. use \MGModule\DNSManager2\models\custom\globalsetting;
  23. use MGModule\DNSManager2\models\custom\zone\Repository as ZoneRepository;
  24. use MGModule\DNSManager2\mgLibs\custom\manager\additionalMethods\DefaultIPHelper;
  25. use MGModule\DNSManager2\models\custom\zone\ZoneTypeEnum;
  26. class zones extends main\mgLibs\process\abstractController{
  27. public function indexJSON($input, $vars = array()) {
  28. return AjaxResponse::I()->refreshPage($this->indexHTML($input, $vars))->toArray();
  29. }
  30. function indexHTML($input, $vars = array()) {
  31. return array(
  32. 'tpl' => 'main',
  33. 'vars' => $vars
  34. );
  35. }
  36. function refreshZonesTableJSON($input, $vars = array()) {
  37. $query = "SELECT dns_manager2_zone.id AS id, dns_manager2_zone.name AS name, CONCAT(tblclients.firstname, ' ', tblclients.lastname) AS client,
  38. dns_manager2_zone.clientid AS clientid, dns_manager2_server.name AS server_name, dns_manager2_zone.type AS type,
  39. dns_manager2_zone.relid AS relid, dns_manager2_zone.serverid AS serverid, dns_manager2_zone.status AS status ,
  40. dns_manager2_zone.connected_with_type as connectedWithType, dns_manager2_zone.connected_with_relid as connectedWithRelid,
  41. dns_manager2_zone.is_locked as is_locked
  42. FROM dns_manager2_zone
  43. LEFT JOIN dns_manager2_server ON dns_manager2_server.id = dns_manager2_zone.serverid
  44. LEFT JOIN tblclients ON tblclients.id = dns_manager2_zone.clientid";
  45. $columns = array('id','name', 'client', 'type', 'server_name', 'status', 'connectedWithType', 'connectedWithRelid');
  46. $helper = new main\mgLibs\custom\RawQueryTableHelper($query, $input, $columns);
  47. $data = $helper->getDataTableArray();
  48. foreach($helper->get() as $zone)
  49. $data['data'][] = $this->dataTablesParseRow('row', ['zone' => $zone]);
  50. return $data;
  51. }
  52. public function editZoneJSON($input, $vars = array()) {
  53. $zone = new zone\Zone($input['id']);
  54. $module = $zone->getModule();
  55. $module->zoneExists();
  56. $vars['counter'] = 0;
  57. $vars['id'] = $zone->id;
  58. $vars['zone_name'] = $zone->name;
  59. $vars['records'] = array_merge($module->getRecords(), $this->getPTRRecords($zone));
  60. foreach($vars['records'] as &$record) {
  61. $record->encode();
  62. }
  63. usort($vars['records'], static function ( $a, $b )
  64. {
  65. if( $a->type === 'SOA' ) return -1;
  66. if( $b->type === 'SOA' ) return 1;
  67. return (int)($a->type > $b->type);
  68. });
  69. $vars['available_record_types'] = $module->getAvailableRecordTypes();
  70. $vars['ttl_disabled'] = !$module->isTTLEnabled();
  71. $vars['show_full_record_name'] = GlobalSettingHelper::getSetting(GlobalSettingEnum::SHOW_FULL_RECORD_NAME);
  72. return AjaxResponse::I()->modal('edit-zone', $vars)->toArray();
  73. }
  74. public function addNewRecordJSON($input, $vars = array()) {
  75. $class = 'MGModule\DNSManager2\mgLibs\custom\dns\record\type\\' . $input['new_record_type'];
  76. if(class_exists($class)) {
  77. $zone = new zone\Zone($input['id']);
  78. $packageSettings = $zone->getPackage()->getSettings('default_ttl');
  79. $packageSettings = unserialize($packageSettings);
  80. $vars['ttl_default_value'] = $packageSettings[$input['new_record_type']];
  81. if(empty($vars['ttl_default_value']))
  82. $vars['ttl_default_value'] = 14440;
  83. $vars['ttl_disabled'] = $input['ttl_disabled'] == 1;
  84. $vars['counter'] = $input['records_counter'];
  85. $vars['record_type'] = $input['new_record_type'];
  86. $vars['record_obj'] = new $class();
  87. $vars['record_fields'] = get_object_vars($vars['record_obj']);
  88. $settingRepo = new main\models\custom\package\setting\Repository();
  89. $vars['recordsAllowedIp'] = $settingRepo->byKey('records_allowed_ip')->get()[0] == 'on';
  90. $vars['recordsBlacklist'] = $settingRepo->byKey('records_blacklist')->get()[0] == 'on';
  91. $helper = new ClientHelper($_SESSION['uid']);
  92. if($vars['recordsAllowedIp']) {
  93. $allowedIps = $helper->getPackageAllowedIps(intval($zone->relid), intval($zone->type));
  94. $allowedIps = IP::getAvailableIpList(json_decode($allowedIps['allowed_ips']));
  95. $vars['allowed_ipv4'] = IP::getOnlyIpv4($allowedIps);
  96. $vars['allowed_ipv4_flag'] = is_array($vars['allowed_ipv4']) && !empty($vars['allowed_ipv4']) && $vars['allowed_ipv4'][0] != null;
  97. $vars['allowed_ipv6'] = IP::getOnlyIpv6($allowedIps);
  98. $vars['allowed_ipv6_flag'] = is_array($vars['allowed_ipv6']) && !empty($vars['allowed_ipv6']) && $vars['allowed_ipv6'][0] != null;
  99. }
  100. if($vars['recordsBlacklist']) {
  101. $ipBlacklist = $helper->getPackageIpBlacklist(intval($zone->relid), intval($zone->type));
  102. $ipBlacklist = IP::getAvailableIpList(json_decode($ipBlacklist['ip_blacklist']));
  103. $vars['ipv4_blacklist'] = IP::getOnlyIpv4($ipBlacklist);
  104. $vars['ipv4_blacklist_flag'] = is_array($vars['ipv4_blacklist']) && !empty($vars['ipv4_blacklist']) && $vars['ipv4_blacklist'][0] != null;
  105. $vars['ipv6_blacklist'] = IP::getOnlyIpv6($ipBlacklist);
  106. $vars['ipv6_blacklist_flag'] = is_array($vars['ipv6_blacklist']) && !empty($vars['ipv6_blacklist']) && $vars['ipv6_blacklist'][0] != null;
  107. }
  108. if($input['type'] == 'PTR') {
  109. $vars['value'] = $zone->name;
  110. }
  111. AjaxResponse::I()->new_record = main\mgLibs\smarty::I()->view('new-record',
  112. $vars,
  113. main\addon::getModuleTemplatesDir().DS.'pages' . DS . main\addon::I()->page);
  114. } else {
  115. AjaxResponse::I()->addRawError(main\mgLibs\lang::T('cannot_find_class'). ' ' .$class);
  116. }
  117. return AjaxResponse::I()->toArray();
  118. }
  119. public function addNewRecordSaveJSON($input, $vars = array()) {
  120. $module = zone\Zone::factory($input['id'])->getModule();
  121. try {
  122. $zone = new zone\Zone($input['id']);
  123. $helper = new ClientHelper($_SESSION['uid']);
  124. $ipBlacklist = IP::getAvailableIpList(json_decode($helper->getPackageIpBlacklist(intval($zone->relid), intval($zone->type))['ip_blacklist']));
  125. $type = $input['add_record'][$input['records_counter']]['type'];
  126. if($type == 'A' || $type == 'AAAA') {
  127. $input['add_record'][$input['records_counter']]['field']['address'] =
  128. $input['add_record'][$input['records_counter']]['field']['address'] ? : $input['add_record'][$input['records_counter']]['field']['0'];
  129. }
  130. $ip = $input['add_record'][$input['records_counter']]['field']['address'];
  131. //todo check if blacklist opt is on
  132. if($ip && !empty($ipBlacklist) && in_array($ip, $ipBlacklist) && $ipBlacklist[0] != null)
  133. return AjaxResponse::I()->addError('ip_on_blacklist')->toArray();
  134. $zoneLoggerManager = new ZoneLoggerManager();
  135. foreach($input['add_record'] as $counter => $record_data) {
  136. $record = $this->createRecordFromInput($record_data);
  137. $record->nameToAbsolute($zone->name);
  138. if($record->validateName() !== true) {
  139. return AjaxResponse::I()->addError($record->validateName())->toArray();
  140. }
  141. try {
  142. $record->rdata->validate();
  143. } catch (Exception $e) {
  144. return AjaxResponse::I()->addError($e->getMessage())->toArray();
  145. }
  146. $module->addRecord($record);
  147. $zoneLoggerManager->logAddRecordToZone($zone, $record);
  148. }
  149. EmailNotificationHelper::sendClientNotificationUsingZoneID(DefaultNotifications::GENERAL_ZONE_ALTERED_NOTIFICATION, $input['id'], ['zone_name' => $zone->name]);
  150. // LogHelper::addSuccessLogUsingZoneID('Edit Zone', '', $input['id']);
  151. return $this->editZoneJSON($input, $vars);
  152. } catch (Exception $exc) {
  153. LogHelper::addFailLogUsingZoneID('Edit Zone', $exc->getMessage(), $input['id']);
  154. AjaxResponse::I()->addRawError($exc->getMessage());
  155. return AjaxResponse::I()->toArray();
  156. }
  157. }
  158. public function editZoneSaveJSON($input, $vars = array())
  159. {
  160. $zone = zone\Zone::factory($input['id']);
  161. $module = $zone->getModule();
  162. try
  163. {
  164. $module->moduleConvertInputFormData($input);
  165. $oldRecords = RecordSetHelper::formatRecordsToIndexById($module->getRecords());
  166. if(!$module->customEditRecords($input))
  167. {
  168. foreach($input['edit_record'] as $counter => $record_data)
  169. {
  170. $record = $this->createRecordFromInput($record_data);
  171. $record->nameToAbsolute($zone->name);
  172. $module->editRecord($record);
  173. $editRecordHelper = new custom\helpers\ZoneLogger\Actions\EditRecord($record, $oldRecords[$record->line]);
  174. $zoneLoggerManager = new ZoneLoggerManager();
  175. $zoneLoggerManager->logEditRecordsInZone($zone, $editRecordHelper);
  176. }
  177. }
  178. if(count($input['edit_record']) > 0) {
  179. $zone = new zone\Zone($input['id']);
  180. EmailNotificationHelper::sendClientNotificationUsingZoneID(DefaultNotifications::GENERAL_ZONE_ALTERED_NOTIFICATION, $input['id'], ['zone_name' => $zone->name]);
  181. // LogHelper::addSuccessLogUsingZoneID('Edit Zone', '', $input['id']);
  182. }
  183. AjaxResponse::I()->addInfo('changes_saved');
  184. } catch (Exception $exc) {
  185. LogHelper::addFailLogUsingZoneID('Edit Zone', $exc->getMessage(), $input['id']);
  186. AjaxResponse::I()->addRawError($exc->getMessage());
  187. return AjaxResponse::I()->toArray();
  188. }
  189. return AjaxResponse::I()->toArray();
  190. }
  191. public function lockZoneJSON( $input, $vars = [] )
  192. {
  193. try
  194. {
  195. /** @var zone\Zone $zone */
  196. $zone = zone\Zone::factory($input['id']);
  197. $zone->setLocked();
  198. }
  199. catch (Exception $exc)
  200. {
  201. LogHelper::addFailLogUsingZoneID('Lock Zone', $exc->getMessage(), $input['id']);
  202. AjaxResponse::I()->addRawError($exc->getMessage());
  203. return AjaxResponse::I()->toArray();
  204. }
  205. AjaxResponse::I()->addInfo('Zone Locked');
  206. return AjaxResponse::I()->refreshPage($this->indexHTML($input, $vars))->toArray();
  207. }
  208. public function unlockZoneJSON( $input, $vars = [] )
  209. {
  210. try
  211. {
  212. /** @var zone\Zone $zone */
  213. $zone = zone\Zone::factory($input['id']);
  214. $zone->setUnlocked();
  215. }
  216. catch (Exception $exc)
  217. {
  218. LogHelper::addFailLogUsingZoneID('Lock Zone', $exc->getMessage(), $input['id']);
  219. AjaxResponse::I()->addRawError($exc->getMessage());
  220. return AjaxResponse::I()->toArray();
  221. }
  222. AjaxResponse::I()->addInfo('Zone Unlocked');
  223. return AjaxResponse::I()->refreshPage($this->indexHTML($input, $vars))->toArray();
  224. }
  225. public function removeZoneRecordJSON($input, $vars = array()) {
  226. $record_data = array_pop($input['record']);
  227. if(empty($record_data))
  228. {
  229. $record_data = array_pop($input['edit_record']);
  230. }
  231. $module = zone\Zone::factory($input['id'])->getModule();
  232. $record = $this->createRecordFromInput($record_data);
  233. try {
  234. $zone = new zone\Zone($input['id']);
  235. $module->deleteRecord($record);
  236. $zoneLoggerManager = new ZoneLoggerManager();
  237. $zoneLoggerManager->logRemoveRecordFromZone($zone, $record);
  238. EmailNotificationHelper::sendClientNotificationUsingZoneID(DefaultNotifications::GENERAL_ZONE_ALTERED_NOTIFICATION, $input['id'], ['zone_name' => $zone->name]);
  239. // LogHelper::addSuccessLogUsingZoneID('Edit Zone', '', $input['id']);
  240. } catch (Exception $exc) {
  241. LogHelper::addFailLogUsingZoneID('Edit Zone', $exc->getMessage(), $input['id']);
  242. throw $exc;
  243. }
  244. AjaxResponse::I()->addInfo('record_removed');
  245. return $this->editZoneJSON($input, $vars);
  246. }
  247. private function createRecordFromInput($input) {
  248. $record = Record::tryToCreateFromArray($input);
  249. $record->rdata->setDataFromArray($input['field']);
  250. $record->decode();
  251. return $record;
  252. }
  253. public function removeZoneJSON($input, $vars = array()) { //ZAPYTAC MARUSZA O SZCZEGOLY - KIEDYS
  254. $zone = new zone\Zone($input['id']);
  255. $module = $zone->getModule();
  256. custom\helpers\StatusHelper::removeZoneStatus($input['id']);
  257. try {
  258. $module->terminateZone();
  259. $zoneLoggerManager = new ZoneLoggerManager();
  260. $zoneLoggerManager->logTerminateZoneAction($zone);
  261. EmailNotificationHelper::sendClientNotificationUsingZoneID(DefaultNotifications::GENERAL_ZONE_REMOVED_NOTIFICATION, $input['id'], ['zone_name' => $zone->name]);
  262. } catch (Exception $exc) {
  263. LogHelper::addFailLogUsingZone('Remove Zone', $exc->getMessage(), $zone);
  264. throw $exc;
  265. }
  266. $zone->setThatNotExist();
  267. AjaxResponse::I()->addInfo("zone_removed_from_server");
  268. return AjaxResponse::I()->toArray();
  269. }
  270. public function createZoneJSON($input, $vars = array())
  271. {
  272. $zone = new zone\Zone($input['id']);
  273. try
  274. {
  275. $module = $zone->getModule();
  276. if(!$module->zoneExists())
  277. {
  278. $zone->getModule()->activateZone();
  279. $setHelper = new main\mgLibs\custom\helpers\RecordSetHelper($zone, new main\mgLibs\custom\helpers\ZoneLogger\Manager());
  280. $setHelper->addRecordSet();
  281. EmailNotificationHelper::sendClientNotificationUsingZoneID(DefaultNotifications::GENERAL_ZONE_CREATED_NOTIFICATION, $input['id'], ['zone_name' => $zone->name]);
  282. }
  283. $zone->setThatExist();
  284. // LogHelper::addSuccessLogUsingZoneID('Add Zone', '', $input['id']);
  285. }
  286. catch(Exception $exc)
  287. {
  288. LogHelper::addFailLogUsingZone('Add Zone', $exc->getMessage(), $zone);
  289. throw $exc;
  290. }
  291. AjaxResponse::I()->addInfo("zone_created");
  292. return AjaxResponse::I()->toArray();
  293. }
  294. public function synchronizeZoneJSON($input, $vars = array()) {
  295. $zone = new zone\Zone($input['id']);
  296. try {
  297. $module = $zone->getModule();
  298. if (!$module->zoneExists()) {
  299. $zone->setThatNotExist();
  300. //$module->activateZone();
  301. //EmailNotificationHelper::sendClientNotificationUsingZoneID(DefaultNotifications::GENERAL_ZONE_CREATED_NOTIFICATION, $input['id']);
  302. } else {
  303. $zone->setThatExist();
  304. }
  305. LogHelper::addSuccessLogUsingZoneID('Synchronize Zone', '', $input['id']);
  306. } catch (Exception $exc) {
  307. LogHelper::addFailLogUsingZone('Synchronize Zone', $exc->getMessage(), $zone);
  308. throw $exc;
  309. }
  310. AjaxResponse::I()->addInfo("zone_synchronized");
  311. return AjaxResponse::I()->toArray();
  312. }
  313. public function removeZoneFromWHMCSJSON($input, $vars = array()) {
  314. zone\Zone::factory($input['id'])->delete();
  315. custom\helpers\StatusHelper::removeZoneStatus($input['id']);
  316. AjaxResponse::I()->refreshPage($this->indexHTML($input, $vars));
  317. AjaxResponse::I()->addInfo("zone_removed_from_whmcs");
  318. return AjaxResponse::I()->toArray();
  319. }
  320. public function switchRelatedItemJSON($input, $vars = array()) {
  321. $zone = new zone\Zone($input['id']);
  322. $vars['id'] = $input['id'];
  323. $vars['items'] = $this->getClientRelatedItems($zone->clientid);
  324. $vars['type'] = $zone->type;
  325. $vars['relid'] = $zone->relid;
  326. return AjaxResponse::I()->modal('switch-item', $vars)->toArray();
  327. }
  328. public function switchRelatedItemSaveJSON($input, $vars = array()) {
  329. list($type, $relid) = explode('|', $input['relateditem']);
  330. $zone = new zone\Zone($input['id']);
  331. $zone->type = $type;
  332. $zone->relid = $relid;
  333. $zone->save();
  334. return AjaxResponse::I()->addInfo('success')->toArray();
  335. }
  336. private function getClientRelatedItems($clientid) {
  337. $helper = new ClientHelper($clientid);
  338. $groups = $helper->getGroups();
  339. $out = array();
  340. foreach($groups as $name => $group) {
  341. $item = current($group);
  342. $out[] = array(
  343. 'name' => $name,
  344. 'type' => $item['type'],
  345. 'relid' => $item['relid'],
  346. 'ips_available' => IP::getAvailableIpList(json_decode($item['ips_available'])),
  347. 'ip_blacklist' => IP::getAvailableIpList(json_decode($item['blacklist']))
  348. );
  349. }
  350. return $out;
  351. }
  352. public function exportZoneToFileJSON($input, $vars = array())
  353. {
  354. $zone = new zone\Zone($input['id']);
  355. $fileManager = new main\mgLibs\custom\FileManager('zonesFilesStorage'.DIRECTORY_SEPARATOR.'singleZones');
  356. $vars['id'] = $zone->id;
  357. $vars['zone_name'] = $zone->name;
  358. $vars['isWritable'] = $fileManager->isStorageWritable();
  359. $vars['isReadable'] = $fileManager->isStorageReadable();
  360. $vars['storagePath'] = $fileManager->getStoragePath();
  361. $vars['newFileName'] = main\mgLibs\custom\helpers\ImportExportFileHelper::generateNewExportFileName($zone->name);
  362. return AjaxResponse::I()->modal('export-zone', $vars)->toArray();
  363. }
  364. public function confirmExportToFileJSON($input, $vars = array())
  365. {
  366. try
  367. {
  368. main\mgLibs\custom\helpers\ImportExportFileHelper::exportZoneToFile($input['id'], 'zonesFilesStorage'.DIRECTORY_SEPARATOR.'singleZones', $input['fileName']);
  369. }
  370. catch(\Exception $exc)
  371. {
  372. LogHelper::addFailLogUsingZoneID('Export Zone', $exc->getMessage(), $input['id']);
  373. throw $exc;
  374. }
  375. return AjaxResponse::I()->addInfo('zoneSuccessfullyExported')->toArray();
  376. }
  377. public function importZoneFromFileJSON($input, $vars = array())
  378. {
  379. try
  380. {
  381. $zone = new zone\Zone($input['id']);
  382. $fileManager = new main\mgLibs\custom\FileManager('zonesFilesStorage'.DIRECTORY_SEPARATOR.'singleZones');
  383. $filesList = main\mgLibs\custom\helpers\ImportExportFileHelper::listFilesForZone($zone, $fileManager);
  384. $vars['isReadable'] = $fileManager->isStorageReadable();
  385. $vars['storagePath'] = $fileManager->getStoragePath();
  386. }
  387. catch(\Exception $exc)
  388. {
  389. LogHelper::addFailLogUsingZoneID('Import Zone', $exc->getMessage(), $input['id']);
  390. throw $exc;
  391. }
  392. $vars['id'] = $zone->id;
  393. $vars['filesList'] = $filesList;
  394. return AjaxResponse::I()->modal('import-zone', $vars)->toArray();
  395. }
  396. public function confirmImportFromFileJSON($input, $vars = array())
  397. {
  398. try
  399. {
  400. $zone = new zone\Zone($input['id']);
  401. $backupZone = main\mgLibs\custom\helpers\ImportExportFileHelper::loadFileContent('zonesFilesStorage'.DIRECTORY_SEPARATOR.'singleZones', $input['fileNameForImport']);
  402. $zoneUpdateHelper = new main\mgLibs\custom\helpers\ZoneUpdateHelper($zone, $backupZone, new ZoneLoggerManager($zone->clientid));
  403. $zoneUpdateHelper->update();
  404. }
  405. catch(\Exception $exc)
  406. {
  407. LogHelper::addFailLogUsingZoneID('Import Zone', $exc->getMessage(), $input['id']);
  408. throw $exc;
  409. }
  410. return AjaxResponse::I()->addInfo('zoneSuccessfullyImported')->toArray();
  411. }
  412. private function getClients()
  413. {
  414. $clients = ClientHelper::getAllClients();
  415. return $clients;
  416. }
  417. private function getAdminSets()
  418. {
  419. $adminSets = set\Repository::factory()->adminOnly()->get();
  420. return $adminSets;
  421. }
  422. private function getClientSets($clientid)
  423. {
  424. $clientSets = set\Repository::factory()->byUserId((int)$clientid)->get();
  425. return $clientSets;
  426. }
  427. public function addZoneJSON($input, $vars = array())
  428. {
  429. $adminSets = $this->getAdminSets();
  430. $vars['adminSets'] = $adminSets;
  431. return AjaxResponse::I()->modal('add-zone', $vars)->toArray();
  432. }
  433. public function addZoneSaveJSON($input, $vars = array())
  434. {
  435. $item = explode("|" , $input['relateditem']);
  436. $input['type'] = $item[0];
  437. $input['relid'] = $item[1];
  438. $ip = $input['zone_ip'];
  439. $helper = new ClientHelper($_SESSION['uid']);
  440. try
  441. {
  442. $allowedIps = IP::getAvailableIpList(json_decode($helper->getPackageAllowedIps($item[1], $item[0])['allowed_ips']));
  443. if ( is_array($allowedIps) && !empty($allowedIps) && $allowedIps[0] != null)
  444. {
  445. if ( !in_array($ip, $allowedIps) )
  446. {
  447. return AjaxResponse::I()->addError('not_allowed_ip')->toArray();
  448. }
  449. }
  450. $ipBlacklist = json_decode($helper->getPackageIpBlacklist($item[1], $item[0])['ip_blacklist']);
  451. if (in_array($ip, $ipBlacklist) && !empty($ipBlacklist) && $ipBlacklist[0] != null)
  452. {
  453. return AjaxResponse::I()->addError('ip_on_blacklist')->toArray();
  454. }
  455. $creator = new ZoneCreator($input['zone_name'], $input['type'], $input['relid'], $input['zone_ip'], $input['client']);
  456. $creator->setZoneLoggerManager(new ZoneLoggerManager($_SESSION['uid']));
  457. $ret = $creator->create($input['recordSet']);
  458. if(is_string($ret))
  459. {
  460. return AjaxResponse::I()->addRawError($ret)->toArray();
  461. }
  462. AjaxResponse::I()->addInfo('zone_created_successfully');
  463. }
  464. catch(Exception $exc)
  465. {
  466. return AjaxResponse::I()->addRawError($exc->getMessage())->toArray();
  467. }
  468. return AjaxResponse::I()->refreshPage($this->indexHTML($input))->toArray();
  469. }
  470. private function tryGetDefaultIP($domainID, $typeID){
  471. if($domainID == null)
  472. return '';
  473. switch($typeID){
  474. case ZoneTypeEnum::DOMAIN:
  475. $domain = new main\models\whmcs\domains\domain($domainID);
  476. break;
  477. case ZoneTypeEnum::HOSTING:
  478. $domain = new main\models\whmcs\service\service($domainID);
  479. break;
  480. case ZoneTypeEnum::ADDON:
  481. $domain = (new main\models\whmcs\hostingaddon\hostingaddon($domainID))->getHostingModel();
  482. break;
  483. }
  484. $defaultIPMethods = new DefaultIPHelper();
  485. $ip = $defaultIPMethods->getDefaultIpBySpecificDomain($domain, $typeID);
  486. if($ip)
  487. return $ip;
  488. else
  489. return '';
  490. }
  491. public function getClientServicesJSON($input, $vars = array())
  492. {
  493. $clientInput = explode("|" , $input['client']);
  494. $clientid = $clientInput[0];
  495. $currentName = $clientInput[1];
  496. $gropus = $this->getClientRelatedItems($clientid);
  497. if(empty($input['relateditem']))
  498. {
  499. $type = $gropus[0]['type'];
  500. $relid = $gropus[0]['relid'];
  501. $input['relateditem'] = [
  502. 'type' => $type ? $type : 0,
  503. 'relid' => $relid ? $relid : 0,
  504. ];
  505. $vars['defaultIP'] = $this->tryGetDefaultIP($relid, $type);
  506. }
  507. if(!empty($input['relateditem'])){
  508. $item = explode('|', $input['relateditem']);
  509. $zoneSetting = custom\helpers\ZoneSettings::getZoneItemSettingForType($item[0]);
  510. $vars['relateditem'] = [
  511. 'type' => $item[0],
  512. 'relid' => $item[1],
  513. ];
  514. $adminSets = $this->getReletedSets($item[1], $item[0]);
  515. if($item[1] != 0)
  516. $vars['defaultIP'] = $this->tryGetDefaultIP($item[1], $item[0]);
  517. }else{
  518. $adminSets = $this->getReletedSets($gropus[0]['relid'], $gropus[0]['type']);
  519. }
  520. $clientSets = $this->getClientSets($clientid);
  521. $sets = array_merge($adminSets,$clientSets);
  522. if($zoneSetting)
  523. {
  524. $relId = $vars['relateditem']['relid'];
  525. $relType = $vars['relateditem']['type'];
  526. $vars['zoneDomainItems'] = custom\helpers\ZoneSettings::getProperlyItems($clientid, $relId, $relType);
  527. }
  528. $vars['currentClient'] = $currentName;
  529. $vars['currentId'] = $clientid;
  530. $vars['items'] = $gropus;
  531. $vars['sets'] = $sets;
  532. AjaxResponse::I()->modal('add-zone', $vars);
  533. return AjaxResponse::I()->toArray();
  534. }
  535. private function getReletedSets($productID = null, $type = null){
  536. if(is_null($productID) || $productID == 0){
  537. return [];
  538. }
  539. $zone = new \MGModule\DNSManager2\models\custom\zone\Zone();
  540. $zone->relid = $productID;
  541. $zone->type = $type;
  542. $zone->clientid = $zone->getRelatedItem()->clientID();
  543. $item = $zone->getPackage()->getAdminSets();;
  544. if(is_array($item)){
  545. $setsArray = [];
  546. foreach($item as $itemID){
  547. $setsArray[] = \MGModule\DNSManager2\models\custom\set\Repository::factory()->byID($itemID)->one();
  548. }
  549. return $setsArray;
  550. }
  551. return [];
  552. }
  553. public function setRecordsJSON($input, $vars = array())
  554. {
  555. if(!array_filter($input['zone']['checked']))
  556. {
  557. return AjaxResponse::I()->addError('select_one_at_least')->toArray();
  558. }
  559. $adminSets = $this->getAdminSets();
  560. $vars['sets'] = $adminSets;
  561. return AjaxResponse::I()->modal('set-records', $vars)->toArray();
  562. }
  563. public function setRecordsSaveJSON($input, $vars = array())
  564. {
  565. if(!$input['setRecord'])
  566. {
  567. return AjaxResponse::I()->addError('no_record_set_selected')->toArray();
  568. }
  569. $zones = $input['zone']['checked'];
  570. $wipe = $input['wipe'];
  571. $mainTask = custom\TaskManager::addTask('DnsRecord:main',array('recordId' => $input['setRecord'] , 'zones' => $zones, 'wipe' => $wipe));
  572. AjaxResponse::I()->addInfo('set_added_successfully');
  573. return AjaxResponse::I()->refreshPage($this->indexHTML($input))->toArray();
  574. }
  575. //mass remove
  576. public function massRemoveZonesJSON($input, $vars = []){
  577. if(!array_filter($input['zone']['checked']))
  578. {
  579. return AjaxResponse::I()->addError('select_one_at_least')->toArray();
  580. }
  581. $sets = [];
  582. foreach($input['zone']['checked'] as $index => $value){
  583. if($value == "on"){
  584. $sets[] = $index;
  585. }
  586. }
  587. $vars['sets'] = json_encode($sets);
  588. return AjaxResponse::I()->modal('mass-remove-records', $vars)->toArray();
  589. }
  590. public function massRemoveZonesFromWHMCSJSON($input, $vars = []){
  591. if(!array_filter($input['zone']['checked']))
  592. {
  593. return AjaxResponse::I()->addError('select_one_at_least')->toArray();
  594. }
  595. $sets = [];
  596. foreach($input['zone']['checked'] as $index => $value){
  597. if($value == "on"){
  598. $sets[] = $index;
  599. }
  600. }
  601. $vars['sets'] = json_encode($sets);
  602. return AjaxResponse::I()->modal('mass-remove-for-whmcs-records', $vars)->toArray();
  603. }
  604. private function getPTRRecords($zone) {
  605. $out = array();
  606. foreach(reverse\Repository::factory()->from($zone->getServer()->id, $zone->name)->byClientID($zone->clientid)->get() as $data) {
  607. $ptr = new main\mgLibs\custom\dns\record\Record();
  608. $ptr->name = custom\dns\utils\ReverseDNSHelper::reverseRecordName($data->ip) . '.' . $data->name;
  609. $ptr->type = 'PTR';
  610. $ptr->ttl = $data->ttl;
  611. $ptr->ip = $data->ip;
  612. $ptr->createRDATAObject('PTR');
  613. $ptr->rdata->setFirstProperty(empty($data->sub)?$data->from:$data->sub . '.' . $data->from);
  614. $out[] = $ptr;
  615. }
  616. return $out;
  617. }
  618. }