index.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. $v_ns1 = quoteshellarg($_POST['v_ns1']);
  63. $v_ns2 = quoteshellarg($_POST['v_ns2']);
  64. $v_ns3 = quoteshellarg($_POST['v_ns3']);
  65. $v_ns4 = quoteshellarg($_POST['v_ns4']);
  66. $v_ns5 = quoteshellarg($_POST['v_ns5']);
  67. $v_ns6 = quoteshellarg($_POST['v_ns6']);
  68. $v_ns7 = quoteshellarg($_POST['v_ns7']);
  69. $v_ns8 = quoteshellarg($_POST['v_ns8']);
  70. // Add dns domain
  71. if (empty($_SESSION['error_msg'])) {
  72. 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", $output, $return_var);
  73. check_return_code($return_var, $output);
  74. unset($output);
  75. }
  76. exec(HESTIA_CMD."v-list-user ".$user." json", $output, $return_var);
  77. $user_config = json_decode(implode('', $output), true);
  78. unset($output);
  79. $v_template = $user_config[$user_plain]['DNS_TEMPLATE'];
  80. if (($v_template != $_POST['v_template']) && (empty($_SESSION['error_msg']))) {
  81. $v_template = quoteshellarg($_POST['v_template']);
  82. exec(HESTIA_CMD."v-change-dns-domain-tpl ".$user." ".$v_domain." ".$v_template." 'no'", $output, $return_var);
  83. check_return_code($return_var, $output);
  84. unset($output);
  85. }
  86. // Set expiration date
  87. if (empty($_SESSION['error_msg'])) {
  88. if ((!empty($_POST['v_exp'])) && ($_POST['v_exp'] != date('Y-m-d', strtotime('+1 year')))) {
  89. $v_exp = quoteshellarg($_POST['v_exp']);
  90. exec(HESTIA_CMD."v-change-dns-domain-exp ".$user." ".$v_domain." ".$v_exp." no", $output, $return_var);
  91. check_return_code($return_var, $output);
  92. unset($output);
  93. }
  94. }
  95. // Set ttl
  96. if (empty($_SESSION['error_msg'])) {
  97. if ((!empty($_POST['v_ttl'])) && ($_POST['v_ttl'] != '14400') && (empty($_SESSION['error_msg']))) {
  98. $v_ttl = quoteshellarg($_POST['v_ttl']);
  99. exec(HESTIA_CMD."v-change-dns-domain-ttl ".$user." ".$v_domain." ".$v_ttl." no", $output, $return_var);
  100. check_return_code($return_var, $output);
  101. unset($output);
  102. }
  103. }
  104. // Restart dns server
  105. if (empty($_SESSION['error_msg'])) {
  106. exec(HESTIA_CMD."v-restart-dns", $output, $return_var);
  107. check_return_code($return_var, $output);
  108. unset($output);
  109. }
  110. // Flush field values on success
  111. if (empty($_SESSION['error_msg'])) {
  112. $_SESSION['ok_msg'] = sprintf(_('DNS_DOMAIN_CREATED_OK'), htmlentities($_POST['v_domain']), htmlentities($_POST['v_domain']));
  113. unset($v_domain);
  114. }
  115. }
  116. // Check POST request for dns record
  117. if (!empty($_POST['ok_rec'])) {
  118. // Check token
  119. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  120. header('location: /login/');
  121. exit();
  122. }
  123. // Check empty fields
  124. if (empty($_POST['v_domain'])) {
  125. $errors[] = 'domain';
  126. }
  127. if (empty($_POST['v_rec'])) {
  128. $errors[] = 'record';
  129. }
  130. if (empty($_POST['v_type'])) {
  131. $errors[] = 'type';
  132. }
  133. if (empty($_POST['v_val'])) {
  134. $errors[] = 'value';
  135. }
  136. if (!empty($errors[0])) {
  137. foreach ($errors as $i => $error) {
  138. if ($i == 0) {
  139. $error_msg = $error;
  140. } else {
  141. $error_msg = $error_msg.", ".$error;
  142. }
  143. }
  144. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  145. }
  146. // Protect input
  147. $v_domain = quoteshellarg($_POST['v_domain']);
  148. $v_rec = quoteshellarg($_POST['v_rec']);
  149. $v_type = quoteshellarg($_POST['v_type']);
  150. $v_val = quoteshellarg($_POST['v_val']);
  151. $v_priority = quoteshellarg($_POST['v_priority']);
  152. $v_ttl = quoteshellarg($_POST['v_ttl']);
  153. // Add dns record
  154. if (empty($_SESSION['error_msg'])) {
  155. exec(HESTIA_CMD."v-add-dns-record ".$user." ".$v_domain." ".$v_rec." ".$v_type." ".$v_val." ".$v_priority." '' yes ".$v_ttl, $output, $return_var);
  156. check_return_code($return_var, $output);
  157. unset($output);
  158. }
  159. $v_type = $_POST['v_type'];
  160. // Flush field values on success
  161. if (empty($_SESSION['error_msg'])) {
  162. $_SESSION['ok_msg'] = sprintf(_('DNS_RECORD_CREATED_OK'), htmlentities($_POST['v_rec']), htmlentities($_POST['v_domain']));
  163. unset($v_domain);
  164. unset($v_rec);
  165. unset($v_val);
  166. unset($v_priority);
  167. }
  168. }
  169. if (empty($v_ns1)) {
  170. $v_ns1 = '';
  171. }
  172. if (empty($v_ns2)) {
  173. $v_ns2 = '';
  174. }
  175. if (empty($v_ns3)) {
  176. $v_ns3 = '';
  177. }
  178. if (empty($v_ns4)) {
  179. $v_ns4 = '';
  180. }
  181. if (empty($v_ns5)) {
  182. $v_ns5 = '';
  183. }
  184. if (empty($v_ns6)) {
  185. $v_ns6 = '';
  186. }
  187. if (empty($v_ns7)) {
  188. $v_ns7 = '';
  189. }
  190. if (empty($v_ns8)) {
  191. $v_ns8 = '';
  192. }
  193. $v_ns1 = str_replace("'", "", $v_ns1);
  194. $v_ns2 = str_replace("'", "", $v_ns2);
  195. $v_ns3 = str_replace("'", "", $v_ns3);
  196. $v_ns4 = str_replace("'", "", $v_ns4);
  197. $v_ns5 = str_replace("'", "", $v_ns5);
  198. $v_ns6 = str_replace("'", "", $v_ns6);
  199. $v_ns7 = str_replace("'", "", $v_ns7);
  200. $v_ns8 = str_replace("'", "", $v_ns8);
  201. if (empty($v_ip) && count($v_ips) > 0) {
  202. $ip = array_key_first($v_ips);
  203. $v_ip = (empty($v_ips[$ip]['NAT']) ? $ip : $v_ips[$ip]['NAT']);
  204. }
  205. // List dns templates
  206. exec(HESTIA_CMD."v-list-dns-templates json", $output, $return_var);
  207. $templates = json_decode(implode('', $output), true);
  208. unset($output);
  209. exec(HESTIA_CMD."v-list-user ".$user." json", $output, $return_var);
  210. $user_config = json_decode(implode('', $output), true);
  211. unset($output);
  212. $v_template = $user_config[$user_plain]['DNS_TEMPLATE'];
  213. if (empty($_GET['domain'])) {
  214. // Display body for dns domain
  215. if (empty($v_ttl)) {
  216. $v_ttl = 14400;
  217. }
  218. if (empty($v_exp)) {
  219. $v_exp = date('Y-m-d', strtotime('+1 year'));
  220. }
  221. if (empty($v_ns1)) {
  222. exec(HESTIA_CMD."v-list-user-ns ".$user." json", $output, $return_var);
  223. $nameservers = json_decode(implode('', $output), true);
  224. for ($i = 0; $i < 8; $i++) {
  225. if (empty($nameservers[$i])) {
  226. $nameservers[$i] = '';
  227. }
  228. }
  229. $v_ns1 = str_replace("'", "", $nameservers[0]);
  230. $v_ns2 = str_replace("'", "", $nameservers[1]);
  231. $v_ns3 = str_replace("'", "", $nameservers[2]);
  232. $v_ns4 = str_replace("'", "", $nameservers[3]);
  233. $v_ns5 = str_replace("'", "", $nameservers[4]);
  234. $v_ns6 = str_replace("'", "", $nameservers[5]);
  235. $v_ns7 = str_replace("'", "", $nameservers[6]);
  236. $v_ns8 = str_replace("'", "", $nameservers[7]);
  237. unset($output);
  238. }
  239. render_page($user, $TAB, 'add_dns');
  240. } else {
  241. // Display body for dns record
  242. $v_domain = $_GET['domain'];
  243. if (empty($v_rec)) {
  244. $v_rec = '@';
  245. }
  246. if (empty($v_type)) {
  247. $v_type = '';
  248. }
  249. if (empty($v_val)) {
  250. $v_val = '';
  251. }
  252. if (empty($v_priority)) {
  253. $v_priority = '';
  254. }
  255. if (empty($v_ttl)) {
  256. $v_ttl = '';
  257. }
  258. render_page($user, $TAB, 'add_dns_rec');
  259. }
  260. // Flush session messages
  261. unset($_SESSION['error_msg']);
  262. unset($_SESSION['ok_msg']);