index.php 7.2 KB

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