index.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. if (empty($_POST["v_dnssec"])) {
  63. $_POST["v_dnssec"] = "no";
  64. }
  65. $v_ns1 = quoteshellarg($_POST["v_ns1"]);
  66. $v_ns2 = quoteshellarg($_POST["v_ns2"]);
  67. $v_ns3 = quoteshellarg($_POST["v_ns3"]);
  68. $v_ns4 = quoteshellarg($_POST["v_ns4"]);
  69. $v_ns5 = quoteshellarg($_POST["v_ns5"]);
  70. $v_ns6 = quoteshellarg($_POST["v_ns6"]);
  71. $v_ns7 = quoteshellarg($_POST["v_ns7"]);
  72. $v_ns8 = quoteshellarg($_POST["v_ns8"]);
  73. $v_dnssec = quoteshellarg($_POST["v_dnssec"]);
  74. // Add dns domain
  75. if (empty($_SESSION["error_msg"])) {
  76. exec(
  77. HESTIA_CMD .
  78. "v-add-dns-domain " .
  79. $user .
  80. " " .
  81. $v_domain .
  82. " " .
  83. quoteshellarg($v_ip) .
  84. " " .
  85. $v_ns1 .
  86. " " .
  87. $v_ns2 .
  88. " " .
  89. $v_ns3 .
  90. " " .
  91. $v_ns4 .
  92. " " .
  93. $v_ns5 .
  94. " " .
  95. $v_ns6 .
  96. " " .
  97. $v_ns7 .
  98. " " .
  99. $v_ns8 .
  100. " no " .
  101. $v_dnssec,
  102. $output,
  103. $return_var,
  104. );
  105. check_return_code($return_var, $output);
  106. unset($output);
  107. }
  108. exec(HESTIA_CMD . "v-list-user " . $user . " json", $output, $return_var);
  109. $user_config = json_decode(implode("", $output), true);
  110. unset($output);
  111. $v_template = $user_config[$user_plain]["DNS_TEMPLATE"];
  112. if ($v_template != $_POST["v_template"] && empty($_SESSION["error_msg"])) {
  113. $v_template = quoteshellarg($_POST["v_template"]);
  114. exec(
  115. HESTIA_CMD .
  116. "v-change-dns-domain-tpl " .
  117. $user .
  118. " " .
  119. $v_domain .
  120. " " .
  121. $v_template .
  122. " 'no'",
  123. $output,
  124. $return_var,
  125. );
  126. check_return_code($return_var, $output);
  127. unset($output);
  128. }
  129. // Set expiration date
  130. if (empty($_SESSION["error_msg"])) {
  131. if (!empty($_POST["v_exp"]) && $_POST["v_exp"] != date("Y-m-d", strtotime("+1 year"))) {
  132. $v_exp = quoteshellarg($_POST["v_exp"]);
  133. exec(
  134. HESTIA_CMD .
  135. "v-change-dns-domain-exp " .
  136. $user .
  137. " " .
  138. $v_domain .
  139. " " .
  140. $v_exp .
  141. " no",
  142. $output,
  143. $return_var,
  144. );
  145. check_return_code($return_var, $output);
  146. unset($output);
  147. }
  148. }
  149. // Set ttl
  150. if (empty($_SESSION["error_msg"])) {
  151. if (
  152. !empty($_POST["v_ttl"]) &&
  153. $_POST["v_ttl"] != "14400" &&
  154. empty($_SESSION["error_msg"])
  155. ) {
  156. $v_ttl = quoteshellarg($_POST["v_ttl"]);
  157. exec(
  158. HESTIA_CMD .
  159. "v-change-dns-domain-ttl " .
  160. $user .
  161. " " .
  162. $v_domain .
  163. " " .
  164. $v_ttl .
  165. " no",
  166. $output,
  167. $return_var,
  168. );
  169. check_return_code($return_var, $output);
  170. unset($output);
  171. }
  172. }
  173. // Restart dns server
  174. if (empty($_SESSION["error_msg"])) {
  175. exec(HESTIA_CMD . "v-restart-dns", $output, $return_var);
  176. check_return_code($return_var, $output);
  177. unset($output);
  178. }
  179. // Flush field values on success
  180. if (empty($_SESSION["error_msg"])) {
  181. $_SESSION["ok_msg"] = sprintf(
  182. _("DNS_DOMAIN_CREATED_OK"),
  183. htmlentities($_POST["v_domain"]),
  184. htmlentities($_POST["v_domain"]),
  185. );
  186. unset($v_domain);
  187. }
  188. }
  189. // Check POST request for dns record
  190. if (!empty($_POST["ok_rec"])) {
  191. // Check token
  192. if (!isset($_POST["token"]) || $_SESSION["token"] != $_POST["token"]) {
  193. header("location: /login/");
  194. exit();
  195. }
  196. // Check empty fields
  197. if (empty($_POST["v_domain"])) {
  198. $errors[] = "domain";
  199. }
  200. if (empty($_POST["v_rec"])) {
  201. $errors[] = "record";
  202. }
  203. if (empty($_POST["v_type"])) {
  204. $errors[] = "type";
  205. }
  206. if (empty($_POST["v_val"])) {
  207. $errors[] = "value";
  208. }
  209. if (!empty($errors[0])) {
  210. foreach ($errors as $i => $error) {
  211. if ($i == 0) {
  212. $error_msg = $error;
  213. } else {
  214. $error_msg = $error_msg . ", " . $error;
  215. }
  216. }
  217. $_SESSION["error_msg"] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  218. }
  219. // Protect input
  220. $v_domain = quoteshellarg($_POST["v_domain"]);
  221. $v_rec = quoteshellarg($_POST["v_rec"]);
  222. $v_type = quoteshellarg($_POST["v_type"]);
  223. $v_val = quoteshellarg($_POST["v_val"]);
  224. $v_priority = quoteshellarg($_POST["v_priority"]);
  225. $v_ttl = quoteshellarg($_POST["v_ttl"]);
  226. // Add dns record
  227. if (empty($_SESSION["error_msg"])) {
  228. exec(
  229. HESTIA_CMD .
  230. "v-add-dns-record " .
  231. $user .
  232. " " .
  233. $v_domain .
  234. " " .
  235. $v_rec .
  236. " " .
  237. $v_type .
  238. " " .
  239. $v_val .
  240. " " .
  241. $v_priority .
  242. " '' yes " .
  243. $v_ttl,
  244. $output,
  245. $return_var,
  246. );
  247. check_return_code($return_var, $output);
  248. unset($output);
  249. }
  250. $v_type = $_POST["v_type"];
  251. // Flush field values on success
  252. if (empty($_SESSION["error_msg"])) {
  253. $_SESSION["ok_msg"] = sprintf(
  254. _("DNS_RECORD_CREATED_OK"),
  255. htmlentities($_POST["v_rec"]),
  256. htmlentities($_POST["v_domain"]),
  257. );
  258. unset($v_domain);
  259. unset($v_rec);
  260. unset($v_val);
  261. unset($v_priority);
  262. unset($v_dnssec);
  263. }
  264. }
  265. if (empty($v_ns1)) {
  266. $v_ns1 = "";
  267. }
  268. if (empty($v_ns2)) {
  269. $v_ns2 = "";
  270. }
  271. if (empty($v_ns3)) {
  272. $v_ns3 = "";
  273. }
  274. if (empty($v_ns4)) {
  275. $v_ns4 = "";
  276. }
  277. if (empty($v_ns5)) {
  278. $v_ns5 = "";
  279. }
  280. if (empty($v_ns6)) {
  281. $v_ns6 = "";
  282. }
  283. if (empty($v_ns7)) {
  284. $v_ns7 = "";
  285. }
  286. if (empty($v_ns8)) {
  287. $v_ns8 = "";
  288. }
  289. $v_ns1 = str_replace("'", "", $v_ns1);
  290. $v_ns2 = str_replace("'", "", $v_ns2);
  291. $v_ns3 = str_replace("'", "", $v_ns3);
  292. $v_ns4 = str_replace("'", "", $v_ns4);
  293. $v_ns5 = str_replace("'", "", $v_ns5);
  294. $v_ns6 = str_replace("'", "", $v_ns6);
  295. $v_ns7 = str_replace("'", "", $v_ns7);
  296. $v_ns8 = str_replace("'", "", $v_ns8);
  297. if (empty($v_ip) && count($v_ips) > 0) {
  298. $ip = array_key_first($v_ips);
  299. $v_ip = empty($v_ips[$ip]["NAT"]) ? $ip : $v_ips[$ip]["NAT"];
  300. }
  301. // List dns templates
  302. exec(HESTIA_CMD . "v-list-dns-templates json", $output, $return_var);
  303. $templates = json_decode(implode("", $output), true);
  304. unset($output);
  305. exec(HESTIA_CMD . "v-list-user " . $user . " json", $output, $return_var);
  306. $user_config = json_decode(implode("", $output), true);
  307. unset($output);
  308. $v_template = $user_config[$user_plain]["DNS_TEMPLATE"];
  309. if (empty($_GET["domain"])) {
  310. // Display body for dns domain
  311. if (empty($v_ttl)) {
  312. $v_ttl = 14400;
  313. }
  314. if (empty($v_exp)) {
  315. $v_exp = date("Y-m-d", strtotime("+1 year"));
  316. }
  317. if (empty($v_ns1)) {
  318. exec(HESTIA_CMD . "v-list-user-ns " . $user . " json", $output, $return_var);
  319. $nameservers = json_decode(implode("", $output), true);
  320. for ($i = 0; $i < 8; $i++) {
  321. if (empty($nameservers[$i])) {
  322. $nameservers[$i] = "";
  323. }
  324. }
  325. $v_ns1 = str_replace("'", "", $nameservers[0]);
  326. $v_ns2 = str_replace("'", "", $nameservers[1]);
  327. $v_ns3 = str_replace("'", "", $nameservers[2]);
  328. $v_ns4 = str_replace("'", "", $nameservers[3]);
  329. $v_ns5 = str_replace("'", "", $nameservers[4]);
  330. $v_ns6 = str_replace("'", "", $nameservers[5]);
  331. $v_ns7 = str_replace("'", "", $nameservers[6]);
  332. $v_ns8 = str_replace("'", "", $nameservers[7]);
  333. unset($output);
  334. }
  335. render_page($user, $TAB, "add_dns");
  336. } else {
  337. // Display body for dns record
  338. $v_domain = $_GET["domain"];
  339. if (empty($v_rec)) {
  340. $v_rec = "@";
  341. }
  342. if (empty($v_type)) {
  343. $v_type = "";
  344. }
  345. if (empty($v_val)) {
  346. $v_val = "";
  347. }
  348. if (empty($v_priority)) {
  349. $v_priority = "";
  350. }
  351. if (empty($v_ttl)) {
  352. $v_ttl = "";
  353. }
  354. if (empty($v_dnssec)) {
  355. $v_dnssec = "";
  356. }
  357. render_page($user, $TAB, "add_dns_rec");
  358. }
  359. // Flush session messages
  360. unset($_SESSION["error_msg"]);
  361. unset($_SESSION["ok_msg"]);