zones.php 30 KB

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