index.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. ob_start();
  3. $TAB = 'DNS';
  4. // Main include
  5. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  6. // List ip addresses
  7. exec(HESTIA_CMD."v-list-user-ips ".$user." json", $output, $return_var);
  8. $v_ips = json_decode(implode('', $output), true);
  9. unset($output);
  10. // Check POST request for dns domain
  11. if (!empty($_POST['ok'])) {
  12. // Check token
  13. verify_csrf($_POST);
  14. // Check empty fields
  15. if (empty($_POST['v_domain'])) {
  16. $errors[] = _('domain');
  17. }
  18. if (empty($_POST['v_ip'])) {
  19. $errors[] = _('ip');
  20. }
  21. if (!empty($errors[0])) {
  22. foreach ($errors as $i => $error) {
  23. if ($i == 0) {
  24. $error_msg = $error;
  25. } else {
  26. $error_msg = $error_msg.", ".$error;
  27. }
  28. }
  29. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  30. }
  31. // Protect input
  32. $v_domain = preg_replace("/^www./i", "", $_POST['v_domain']);
  33. $v_domain = escapeshellarg($v_domain);
  34. $v_domain = strtolower($v_domain);
  35. $v_ip = $_POST['v_ip'];
  36. $v_ns1 = escapeshellarg($_POST['v_ns1']);
  37. $v_ns2 = escapeshellarg($_POST['v_ns2']);
  38. $v_ns3 = escapeshellarg($_POST['v_ns3']);
  39. $v_ns4 = escapeshellarg($_POST['v_ns4']);
  40. $v_ns5 = escapeshellarg($_POST['v_ns5']);
  41. $v_ns6 = escapeshellarg($_POST['v_ns6']);
  42. $v_ns7 = escapeshellarg($_POST['v_ns7']);
  43. $v_ns8 = escapeshellarg($_POST['v_ns8']);
  44. // Add dns domain
  45. if (empty($_SESSION['error_msg'])) {
  46. exec(HESTIA_CMD."v-add-dns-domain ".$user." ".$v_domain." ".escapeshellarg($v_ip)." ".$v_ns1." ".$v_ns2." ".$v_ns3." ".$v_ns4." ".$v_ns5." ".$v_ns6." ".$v_ns7." ".$v_ns8." no", $output, $return_var);
  47. check_return_code($return_var, $output);
  48. unset($output);
  49. }
  50. // Change domain template
  51. if (($v_template != $_POST['v_template']) && (empty($_SESSION['error_msg']))) {
  52. $v_template = escapeshellarg($_POST['v_template']);
  53. exec(HESTIA_CMD."v-change-dns-domain-tpl ".$user." ".$v_domain." ".$v_template." 'no'", $output, $return_var);
  54. check_return_code($return_var, $output);
  55. unset($output);
  56. }
  57. // Set expiriation date
  58. if (empty($_SESSION['error_msg'])) {
  59. if ((!empty($_POST['v_exp'])) && ($_POST['v_exp'] != date('Y-m-d', strtotime('+1 year')))) {
  60. $v_exp = escapeshellarg($_POST['v_exp']);
  61. exec(HESTIA_CMD."v-change-dns-domain-exp ".$user." ".$v_domain." ".$v_exp." no", $output, $return_var);
  62. check_return_code($return_var, $output);
  63. unset($output);
  64. }
  65. }
  66. // Set ttl
  67. if (empty($_SESSION['error_msg'])) {
  68. if ((!empty($_POST['v_ttl'])) && ($_POST['v_ttl'] != '14400') && (empty($_SESSION['error_msg']))) {
  69. $v_ttl = escapeshellarg($_POST['v_ttl']);
  70. exec(HESTIA_CMD."v-change-dns-domain-ttl ".$user." ".$v_domain." ".$v_ttl." no", $output, $return_var);
  71. check_return_code($return_var, $output);
  72. unset($output);
  73. }
  74. }
  75. // Restart dns server
  76. if (empty($_SESSION['error_msg'])) {
  77. exec(HESTIA_CMD."v-restart-dns", $output, $return_var);
  78. check_return_code($return_var, $output);
  79. unset($output);
  80. }
  81. // Flush field values on success
  82. if (empty($_SESSION['error_msg'])) {
  83. $_SESSION['ok_msg'] = sprintf(_('DNS_DOMAIN_CREATED_OK'), htmlentities($_POST['v_domain']), htmlentities($_POST['v_domain']));
  84. unset($v_domain);
  85. }
  86. }
  87. // Check POST request for dns record
  88. if (!empty($_POST['ok_rec'])) {
  89. // Check token
  90. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  91. header('location: /login/');
  92. exit();
  93. }
  94. // Check empty fields
  95. if (empty($_POST['v_domain'])) {
  96. $errors[] = 'domain';
  97. }
  98. if (empty($_POST['v_rec'])) {
  99. $errors[] = 'record';
  100. }
  101. if (empty($_POST['v_type'])) {
  102. $errors[] = 'type';
  103. }
  104. if (empty($_POST['v_val'])) {
  105. $errors[] = 'value';
  106. }
  107. if (!empty($errors[0])) {
  108. foreach ($errors as $i => $error) {
  109. if ($i == 0) {
  110. $error_msg = $error;
  111. } else {
  112. $error_msg = $error_msg.", ".$error;
  113. }
  114. }
  115. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  116. }
  117. // Protect input
  118. $v_domain = escapeshellarg($_POST['v_domain']);
  119. $v_rec = escapeshellarg($_POST['v_rec']);
  120. $v_type = escapeshellarg($_POST['v_type']);
  121. $v_val = escapeshellarg($_POST['v_val']);
  122. $v_priority = escapeshellarg($_POST['v_priority']);
  123. $v_ttl = escapeshellarg($_POST['v_ttl']);
  124. // Add dns record
  125. if (empty($_SESSION['error_msg'])) {
  126. exec(HESTIA_CMD."v-add-dns-record ".$user." ".$v_domain." ".$v_rec." ".$v_type." ".$v_val." ".$v_priority." '' false ".$v_ttl, $output, $return_var);
  127. check_return_code($return_var, $output);
  128. unset($output);
  129. }
  130. $v_type = $_POST['v_type'];
  131. // Flush field values on success
  132. if (empty($_SESSION['error_msg'])) {
  133. $_SESSION['ok_msg'] = sprintf(_('DNS_RECORD_CREATED_OK'), htmlentities($_POST['v_rec']), htmlentities($_POST['v_domain']));
  134. unset($v_domain);
  135. unset($v_rec);
  136. unset($v_val);
  137. unset($v_priority);
  138. }
  139. }
  140. $v_ns1 = str_replace("'", "", $v_ns1);
  141. $v_ns2 = str_replace("'", "", $v_ns2);
  142. $v_ns3 = str_replace("'", "", $v_ns3);
  143. $v_ns4 = str_replace("'", "", $v_ns4);
  144. $v_ns5 = str_replace("'", "", $v_ns5);
  145. $v_ns6 = str_replace("'", "", $v_ns6);
  146. $v_ns7 = str_replace("'", "", $v_ns7);
  147. $v_ns8 = str_replace("'", "", $v_ns8);
  148. if (empty($v_ip) && count($v_ips) > 0) {
  149. $ip = array_key_first($v_ips);
  150. $v_ip = (empty($v_ips[$ip]['NAT']) ? $ip : $v_ips[$ip]['NAT']);
  151. }
  152. // List dns templates
  153. exec(HESTIA_CMD."v-list-dns-templates json", $output, $return_var);
  154. $templates = json_decode(implode('', $output), true);
  155. unset($output);
  156. exec(HESTIA_CMD."v-list-user ".$user." json", $output, $return_var);
  157. $user_config = json_decode(implode('', $output), true);
  158. unset($output);
  159. $v_template = $user_config[$user_plain]['DNS_TEMPLATE'];
  160. if (empty($_GET['domain'])) {
  161. // Display body for dns domain
  162. if (empty($v_ttl)) {
  163. $v_ttl = 14400;
  164. }
  165. if (empty($v_exp)) {
  166. $v_exp = date('Y-m-d', strtotime('+1 year'));
  167. }
  168. if (empty($v_ns1)) {
  169. exec(HESTIA_CMD."v-list-user-ns ".$user." json", $output, $return_var);
  170. $nameservers = json_decode(implode('', $output), true);
  171. $v_ns1 = str_replace("'", "", $nameservers[0]);
  172. $v_ns2 = str_replace("'", "", $nameservers[1]);
  173. $v_ns3 = str_replace("'", "", $nameservers[2]);
  174. $v_ns4 = str_replace("'", "", $nameservers[3]);
  175. $v_ns5 = str_replace("'", "", $nameservers[4]);
  176. $v_ns6 = str_replace("'", "", $nameservers[5]);
  177. $v_ns7 = str_replace("'", "", $nameservers[6]);
  178. $v_ns8 = str_replace("'", "", $nameservers[7]);
  179. unset($output);
  180. }
  181. render_page($user, $TAB, 'add_dns');
  182. } else {
  183. // Display body for dns record
  184. $v_domain = $_GET['domain'];
  185. if (empty($v_rec)) {
  186. $v_rec = '@';
  187. }
  188. render_page($user, $TAB, 'add_dns_rec');
  189. }
  190. // Flush session messages
  191. unset($_SESSION['error_msg']);
  192. unset($_SESSION['ok_msg']);