index.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. use function Hestiacp\quoteshellarg\quoteshellarg;
  3. ob_start();
  4. $TAB = 'DNS';
  5. // Main include
  6. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  7. // List ip addresses
  8. exec(HESTIA_CMD."v-list-user-ips ".$user." json", $output, $return_var);
  9. $v_ips = json_decode(implode('', $output), true);
  10. unset($output);
  11. // Check POST request for dns domain
  12. if (!empty($_POST['ok'])) {
  13. // Check token
  14. verify_csrf($_POST);
  15. // Check empty fields
  16. if (empty($_POST['v_domain'])) {
  17. $errors[] = _('domain');
  18. }
  19. if (empty($_POST['v_ip'])) {
  20. $errors[] = _('ip');
  21. }
  22. if (!empty($errors[0])) {
  23. foreach ($errors as $i => $error) {
  24. if ($i == 0) {
  25. $error_msg = $error;
  26. } else {
  27. $error_msg = $error_msg.", ".$error;
  28. }
  29. }
  30. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  31. }
  32. // Protect input
  33. $v_domain = preg_replace("/^www./i", "", $_POST['v_domain']);
  34. $v_domain = quoteshellarg($v_domain);
  35. $v_domain = strtolower($v_domain);
  36. $v_ip = $_POST['v_ip'];
  37. // Change NameServers
  38. if (empty($_POST['v_ns1'])) {
  39. $_POST['v_ns1'] = '';
  40. }
  41. if (empty($_POST['v_ns2'])) {
  42. $_POST['v_ns2'] = '';
  43. }
  44. if (empty($_POST['v_ns3'])) {
  45. $_POST['v_ns3'] = '';
  46. }
  47. if (empty($_POST['v_ns4'])) {
  48. $_POST['v_ns4'] = '';
  49. }
  50. if (empty($_POST['v_ns5'])) {
  51. $_POST['v_ns5'] = '';
  52. }
  53. if (empty($_POST['v_ns6'])) {
  54. $_POST['v_ns6'] = '';
  55. }
  56. if (empty($_POST['v_ns7'])) {
  57. $_POST['v_ns7'] = '';
  58. }
  59. if (empty($_POST['v_ns8'])) {
  60. $_POST['v_ns8'] = '';
  61. }
  62. if (empty($_POST['v_dnssec'])) {
  63. $_POST['v_dnssec'] = 'no';
  64. }
  65. $v_ns1 = quoteshellarg($_POST['v_ns1']);
  66. $v_ns2 = quoteshellarg($_POST['v_ns2']);
  67. $v_ns3 = quoteshellarg($_POST['v_ns3']);
  68. $v_ns4 = quoteshellarg($_POST['v_ns4']);
  69. $v_ns5 = quoteshellarg($_POST['v_ns5']);
  70. $v_ns6 = quoteshellarg($_POST['v_ns6']);
  71. $v_ns7 = quoteshellarg($_POST['v_ns7']);
  72. $v_ns8 = quoteshellarg($_POST['v_ns8']);
  73. $v_dnssec = quoteshellarg($_POST['v_dnssec']);
  74. // Add dns domain
  75. if (empty($_SESSION['error_msg'])) {
  76. exec(HESTIA_CMD."v-add-dns-domain ".$user." ".$v_domain." ".quoteshellarg($v_ip)." ".$v_ns1." ".$v_ns2." ".$v_ns3." ".$v_ns4." ".$v_ns5." ".$v_ns6." ".$v_ns7." ".$v_ns8." no ".$v_dnssec, $output, $return_var);
  77. check_return_code($return_var, $output);
  78. unset($output);
  79. }
  80. exec(HESTIA_CMD."v-list-user ".$user." json", $output, $return_var);
  81. $user_config = json_decode(implode('', $output), true);
  82. unset($output);
  83. $v_template = $user_config[$user_plain]['DNS_TEMPLATE'];
  84. if (($v_template != $_POST['v_template']) && (empty($_SESSION['error_msg']))) {
  85. $v_template = quoteshellarg($_POST['v_template']);
  86. exec(HESTIA_CMD."v-change-dns-domain-tpl ".$user." ".$v_domain." ".$v_template." 'no'", $output, $return_var);
  87. check_return_code($return_var, $output);
  88. unset($output);
  89. }
  90. // Set expiration date
  91. if (empty($_SESSION['error_msg'])) {
  92. if ((!empty($_POST['v_exp'])) && ($_POST['v_exp'] != date('Y-m-d', strtotime('+1 year')))) {
  93. $v_exp = quoteshellarg($_POST['v_exp']);
  94. exec(HESTIA_CMD."v-change-dns-domain-exp ".$user." ".$v_domain." ".$v_exp." no", $output, $return_var);
  95. check_return_code($return_var, $output);
  96. unset($output);
  97. }
  98. }
  99. // Set ttl
  100. if (empty($_SESSION['error_msg'])) {
  101. if ((!empty($_POST['v_ttl'])) && ($_POST['v_ttl'] != '14400') && (empty($_SESSION['error_msg']))) {
  102. $v_ttl = quoteshellarg($_POST['v_ttl']);
  103. exec(HESTIA_CMD."v-change-dns-domain-ttl ".$user." ".$v_domain." ".$v_ttl." no", $output, $return_var);
  104. check_return_code($return_var, $output);
  105. unset($output);
  106. }
  107. }
  108. // Restart dns server
  109. if (empty($_SESSION['error_msg'])) {
  110. exec(HESTIA_CMD."v-restart-dns", $output, $return_var);
  111. check_return_code($return_var, $output);
  112. unset($output);
  113. }
  114. // Flush field values on success
  115. if (empty($_SESSION['error_msg'])) {
  116. $_SESSION['ok_msg'] = sprintf(_('DNS_DOMAIN_CREATED_OK'), htmlentities($_POST['v_domain']), htmlentities($_POST['v_domain']));
  117. unset($v_domain);
  118. }
  119. }
  120. // Check POST request for dns record
  121. if (!empty($_POST['ok_rec'])) {
  122. // Check token
  123. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  124. header('location: /login/');
  125. exit();
  126. }
  127. // Check empty fields
  128. if (empty($_POST['v_domain'])) {
  129. $errors[] = 'domain';
  130. }
  131. if (empty($_POST['v_rec'])) {
  132. $errors[] = 'record';
  133. }
  134. if (empty($_POST['v_type'])) {
  135. $errors[] = 'type';
  136. }
  137. if (empty($_POST['v_val'])) {
  138. $errors[] = 'value';
  139. }
  140. if (!empty($errors[0])) {
  141. foreach ($errors as $i => $error) {
  142. if ($i == 0) {
  143. $error_msg = $error;
  144. } else {
  145. $error_msg = $error_msg.", ".$error;
  146. }
  147. }
  148. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  149. }
  150. // Protect input
  151. $v_domain = quoteshellarg($_POST['v_domain']);
  152. $v_rec = quoteshellarg($_POST['v_rec']);
  153. $v_type = quoteshellarg($_POST['v_type']);
  154. $v_val = quoteshellarg($_POST['v_val']);
  155. $v_priority = quoteshellarg($_POST['v_priority']);
  156. $v_ttl = quoteshellarg($_POST['v_ttl']);
  157. // Add dns record
  158. if (empty($_SESSION['error_msg'])) {
  159. exec(HESTIA_CMD."v-add-dns-record ".$user." ".$v_domain." ".$v_rec." ".$v_type." ".$v_val." ".$v_priority." '' yes ".$v_ttl, $output, $return_var);
  160. check_return_code($return_var, $output);
  161. unset($output);
  162. }
  163. $v_type = $_POST['v_type'];
  164. // Flush field values on success
  165. if (empty($_SESSION['error_msg'])) {
  166. $_SESSION['ok_msg'] = sprintf(_('DNS_RECORD_CREATED_OK'), htmlentities($_POST['v_rec']), htmlentities($_POST['v_domain']));
  167. unset($v_domain);
  168. unset($v_rec);
  169. unset($v_val);
  170. unset($v_priority);
  171. unset($v_dnssec);
  172. }
  173. }
  174. if (empty($v_ns1)) {
  175. $v_ns1 = '';
  176. }
  177. if (empty($v_ns2)) {
  178. $v_ns2 = '';
  179. }
  180. if (empty($v_ns3)) {
  181. $v_ns3 = '';
  182. }
  183. if (empty($v_ns4)) {
  184. $v_ns4 = '';
  185. }
  186. if (empty($v_ns5)) {
  187. $v_ns5 = '';
  188. }
  189. if (empty($v_ns6)) {
  190. $v_ns6 = '';
  191. }
  192. if (empty($v_ns7)) {
  193. $v_ns7 = '';
  194. }
  195. if (empty($v_ns8)) {
  196. $v_ns8 = '';
  197. }
  198. $v_ns1 = str_replace("'", "", $v_ns1);
  199. $v_ns2 = str_replace("'", "", $v_ns2);
  200. $v_ns3 = str_replace("'", "", $v_ns3);
  201. $v_ns4 = str_replace("'", "", $v_ns4);
  202. $v_ns5 = str_replace("'", "", $v_ns5);
  203. $v_ns6 = str_replace("'", "", $v_ns6);
  204. $v_ns7 = str_replace("'", "", $v_ns7);
  205. $v_ns8 = str_replace("'", "", $v_ns8);
  206. if (empty($v_ip) && count($v_ips) > 0) {
  207. $ip = array_key_first($v_ips);
  208. $v_ip = (empty($v_ips[$ip]['NAT']) ? $ip : $v_ips[$ip]['NAT']);
  209. }
  210. // List dns templates
  211. exec(HESTIA_CMD."v-list-dns-templates json", $output, $return_var);
  212. $templates = json_decode(implode('', $output), true);
  213. unset($output);
  214. exec(HESTIA_CMD."v-list-user ".$user." json", $output, $return_var);
  215. $user_config = json_decode(implode('', $output), true);
  216. unset($output);
  217. $v_template = $user_config[$user_plain]['DNS_TEMPLATE'];
  218. if (empty($_GET['domain'])) {
  219. // Display body for dns domain
  220. if (empty($v_ttl)) {
  221. $v_ttl = 14400;
  222. }
  223. if (empty($v_exp)) {
  224. $v_exp = date('Y-m-d', strtotime('+1 year'));
  225. }
  226. if (empty($v_ns1)) {
  227. exec(HESTIA_CMD."v-list-user-ns ".$user." json", $output, $return_var);
  228. $nameservers = json_decode(implode('', $output), true);
  229. for ($i = 0; $i < 8; $i++) {
  230. if (empty($nameservers[$i])) {
  231. $nameservers[$i] = '';
  232. }
  233. }
  234. $v_ns1 = str_replace("'", "", $nameservers[0]);
  235. $v_ns2 = str_replace("'", "", $nameservers[1]);
  236. $v_ns3 = str_replace("'", "", $nameservers[2]);
  237. $v_ns4 = str_replace("'", "", $nameservers[3]);
  238. $v_ns5 = str_replace("'", "", $nameservers[4]);
  239. $v_ns6 = str_replace("'", "", $nameservers[5]);
  240. $v_ns7 = str_replace("'", "", $nameservers[6]);
  241. $v_ns8 = str_replace("'", "", $nameservers[7]);
  242. unset($output);
  243. }
  244. render_page($user, $TAB, 'add_dns');
  245. } else {
  246. // Display body for dns record
  247. $v_domain = $_GET['domain'];
  248. if (empty($v_rec)) {
  249. $v_rec = '@';
  250. }
  251. if (empty($v_type)) {
  252. $v_type = '';
  253. }
  254. if (empty($v_val)) {
  255. $v_val = '';
  256. }
  257. if (empty($v_priority)) {
  258. $v_priority = '';
  259. }
  260. if (empty($v_ttl)) {
  261. $v_ttl = '';
  262. }
  263. if (empty($v_dnssec)) {
  264. $v_dnssec = '';
  265. }
  266. render_page($user, $TAB, 'add_dns_rec');
  267. }
  268. // Flush session messages
  269. unset($_SESSION['error_msg']);
  270. unset($_SESSION['ok_msg']);