index.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  15. header('location: /login/');
  16. exit();
  17. }
  18. // Check empty fields
  19. if (empty($_POST['v_domain'])) $errors[] = _('domain');
  20. if (empty($_POST['v_ip'])) $errors[] = _('ip');
  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'])) $errors[] = 'domain';
  96. if (empty($_POST['v_rec'])) $errors[] = 'record';
  97. if (empty($_POST['v_type'])) $errors[] = 'type';
  98. if (empty($_POST['v_val'])) $errors[] = 'value';
  99. if (!empty($errors[0])) {
  100. foreach ($errors as $i => $error) {
  101. if ( $i == 0 ) {
  102. $error_msg = $error;
  103. } else {
  104. $error_msg = $error_msg.", ".$error;
  105. }
  106. }
  107. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'),$error_msg);
  108. }
  109. // Protect input
  110. $v_domain = escapeshellarg($_POST['v_domain']);
  111. $v_rec = escapeshellarg($_POST['v_rec']);
  112. $v_type = escapeshellarg($_POST['v_type']);
  113. $v_val = escapeshellarg($_POST['v_val']);
  114. $v_priority = escapeshellarg($_POST['v_priority']);
  115. $v_ttl = escapeshellarg($_POST['v_ttl']);
  116. // Add dns record
  117. if (empty($_SESSION['error_msg'])) {
  118. exec (HESTIA_CMD."v-add-dns-record ".$user." ".$v_domain." ".$v_rec." ".$v_type." ".$v_val." ".$v_priority." '' false ".$v_ttl, $output, $return_var);
  119. check_return_code($return_var,$output);
  120. unset($output);
  121. }
  122. $v_type = $_POST['v_type'];
  123. // Flush field values on success
  124. if (empty($_SESSION['error_msg'])) {
  125. $_SESSION['ok_msg'] = sprintf(_('DNS_RECORD_CREATED_OK'),htmlentities($_POST['v_rec']),htmlentities($_POST['v_domain']));
  126. unset($v_domain);
  127. unset($v_rec);
  128. unset($v_val);
  129. unset($v_priority);
  130. }
  131. }
  132. $v_ns1 = str_replace("'", "", $v_ns1);
  133. $v_ns2 = str_replace("'", "", $v_ns2);
  134. $v_ns3 = str_replace("'", "", $v_ns3);
  135. $v_ns4 = str_replace("'", "", $v_ns4);
  136. $v_ns5 = str_replace("'", "", $v_ns5);
  137. $v_ns6 = str_replace("'", "", $v_ns6);
  138. $v_ns7 = str_replace("'", "", $v_ns7);
  139. $v_ns8 = str_replace("'", "", $v_ns8);
  140. if(empty($v_ip) && count($v_ips) > 0) {
  141. $ip = array_key_first($v_ips);
  142. $v_ip = (empty($v_ips[$ip]['NAT'])?$ip:$v_ips[$ip]['NAT']);
  143. }
  144. // List dns templates
  145. exec (HESTIA_CMD."v-list-dns-templates json", $output, $return_var);
  146. $templates = json_decode(implode('', $output), true);
  147. unset($output);
  148. exec (HESTIA_CMD."v-list-user ".$user." json", $output, $return_var);
  149. $user_config = json_decode(implode('', $output), true);
  150. unset($output);
  151. $v_template = $user_config[$user]['DNS_TEMPLATE'];
  152. if (empty($_GET['domain'])) {
  153. // Display body for dns domain
  154. if (empty($v_ttl)) $v_ttl = 14400;
  155. if (empty($v_exp)) $v_exp = date('Y-m-d', strtotime('+1 year'));
  156. if (empty($v_ns1)) {
  157. exec (HESTIA_CMD."v-list-user-ns ".$user." json", $output, $return_var);
  158. $nameservers = json_decode(implode('', $output), true);
  159. $v_ns1 = str_replace("'", "", $nameservers[0]);
  160. $v_ns2 = str_replace("'", "", $nameservers[1]);
  161. $v_ns3 = str_replace("'", "", $nameservers[2]);
  162. $v_ns4 = str_replace("'", "", $nameservers[3]);
  163. $v_ns5 = str_replace("'", "", $nameservers[4]);
  164. $v_ns6 = str_replace("'", "", $nameservers[5]);
  165. $v_ns7 = str_replace("'", "", $nameservers[6]);
  166. $v_ns8 = str_replace("'", "", $nameservers[7]);
  167. unset($output);
  168. }
  169. render_page($user, $TAB, 'add_dns');
  170. } else {
  171. // Display body for dns record
  172. $v_domain = $_GET['domain'];
  173. if (empty($v_rec)){
  174. $v_rec = '@';
  175. }
  176. render_page($user, $TAB, 'add_dns_rec');
  177. }
  178. // Flush session messages
  179. unset($_SESSION['error_msg']);
  180. unset($_SESSION['ok_msg']);