index.php 8.7 KB

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