index.php 8.6 KB

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