index.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. "</a>",
  191. '<a class="u-text-bold" href="/edit/dns/?domain=' .
  192. htmlentities($_POST["v_domain"]) .
  193. '">',
  194. );
  195. unset($v_domain);
  196. }
  197. }
  198. // Check POST request for dns record
  199. if (!empty($_POST["ok_rec"])) {
  200. // Check token
  201. if (!isset($_POST["token"]) || $_SESSION["token"] != $_POST["token"]) {
  202. header("location: /login/");
  203. exit();
  204. }
  205. // Check empty fields
  206. if (empty($_POST["v_domain"])) {
  207. $errors[] = _("Domain");
  208. }
  209. if (empty($_POST["v_rec"])) {
  210. $errors[] = _("Record");
  211. }
  212. if (empty($_POST["v_type"])) {
  213. $errors[] = _("Type");
  214. }
  215. if (empty($_POST["v_val"])) {
  216. $errors[] = _("IP or Value");
  217. }
  218. if (!empty($errors[0])) {
  219. foreach ($errors as $i => $error) {
  220. if ($i == 0) {
  221. $error_msg = $error;
  222. } else {
  223. $error_msg = $error_msg . ", " . $error;
  224. }
  225. }
  226. $_SESSION["error_msg"] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  227. }
  228. // Protect input
  229. $v_domain = quoteshellarg($_POST["v_domain"]);
  230. $v_rec = quoteshellarg($_POST["v_rec"]);
  231. $v_type = quoteshellarg($_POST["v_type"]);
  232. $v_val = quoteshellarg($_POST["v_val"]);
  233. $v_priority = quoteshellarg($_POST["v_priority"]);
  234. $v_ttl = quoteshellarg($_POST["v_ttl"]);
  235. // Add dns record
  236. if (empty($_SESSION["error_msg"])) {
  237. exec(
  238. HESTIA_CMD .
  239. "v-add-dns-record " .
  240. $user .
  241. " " .
  242. $v_domain .
  243. " " .
  244. $v_rec .
  245. " " .
  246. $v_type .
  247. " " .
  248. $v_val .
  249. " " .
  250. $v_priority .
  251. " '' yes " .
  252. $v_ttl,
  253. $output,
  254. $return_var,
  255. );
  256. check_return_code($return_var, $output);
  257. unset($output);
  258. }
  259. $v_type = $_POST["v_type"];
  260. // Flush field values on success
  261. if (empty($_SESSION["error_msg"])) {
  262. $_SESSION["ok_msg"] = htmlify_trans(
  263. sprintf(
  264. _("Record {%s.%s} has been created successfully."),
  265. htmlentities($_POST["v_rec"]),
  266. htmlentities($_POST["v_domain"]),
  267. ),
  268. "</span>",
  269. "<span class='u-text-bold'>",
  270. );
  271. unset($v_domain);
  272. unset($v_rec);
  273. unset($v_val);
  274. unset($v_priority);
  275. unset($v_dnssec);
  276. }
  277. }
  278. if (empty($v_ns1)) {
  279. $v_ns1 = "";
  280. }
  281. if (empty($v_ns2)) {
  282. $v_ns2 = "";
  283. }
  284. if (empty($v_ns3)) {
  285. $v_ns3 = "";
  286. }
  287. if (empty($v_ns4)) {
  288. $v_ns4 = "";
  289. }
  290. if (empty($v_ns5)) {
  291. $v_ns5 = "";
  292. }
  293. if (empty($v_ns6)) {
  294. $v_ns6 = "";
  295. }
  296. if (empty($v_ns7)) {
  297. $v_ns7 = "";
  298. }
  299. if (empty($v_ns8)) {
  300. $v_ns8 = "";
  301. }
  302. $v_ns1 = str_replace("'", "", $v_ns1);
  303. $v_ns2 = str_replace("'", "", $v_ns2);
  304. $v_ns3 = str_replace("'", "", $v_ns3);
  305. $v_ns4 = str_replace("'", "", $v_ns4);
  306. $v_ns5 = str_replace("'", "", $v_ns5);
  307. $v_ns6 = str_replace("'", "", $v_ns6);
  308. $v_ns7 = str_replace("'", "", $v_ns7);
  309. $v_ns8 = str_replace("'", "", $v_ns8);
  310. if (empty($v_ip) && count($v_ips) > 0) {
  311. $ip = array_key_first($v_ips);
  312. $v_ip = empty($v_ips[$ip]["NAT"]) ? $ip : $v_ips[$ip]["NAT"];
  313. }
  314. // List dns templates
  315. exec(HESTIA_CMD . "v-list-dns-templates json", $output, $return_var);
  316. $templates = json_decode(implode("", $output), true);
  317. unset($output);
  318. exec(HESTIA_CMD . "v-list-user " . $user . " json", $output, $return_var);
  319. $user_config = json_decode(implode("", $output), true);
  320. unset($output);
  321. $v_template = $user_config[$user_plain]["DNS_TEMPLATE"];
  322. if (empty($_GET["domain"])) {
  323. // Display body for dns domain
  324. if (empty($v_domain)) {
  325. $v_domain = "";
  326. }
  327. if (empty($v_ttl)) {
  328. $v_ttl = 14400;
  329. }
  330. if (empty($v_exp)) {
  331. $v_exp = date("Y-m-d", strtotime("+1 year"));
  332. }
  333. if (empty($v_dnssec)) {
  334. $v_dnssec = "";
  335. }
  336. if (empty($v_ns1)) {
  337. exec(HESTIA_CMD . "v-list-user-ns " . $user . " json", $output, $return_var);
  338. $nameservers = json_decode(implode("", $output), true);
  339. for ($i = 0; $i < 8; $i++) {
  340. if (empty($nameservers[$i])) {
  341. $nameservers[$i] = "";
  342. }
  343. }
  344. $v_ns1 = str_replace("'", "", $nameservers[0]);
  345. $v_ns2 = str_replace("'", "", $nameservers[1]);
  346. $v_ns3 = str_replace("'", "", $nameservers[2]);
  347. $v_ns4 = str_replace("'", "", $nameservers[3]);
  348. $v_ns5 = str_replace("'", "", $nameservers[4]);
  349. $v_ns6 = str_replace("'", "", $nameservers[5]);
  350. $v_ns7 = str_replace("'", "", $nameservers[6]);
  351. $v_ns8 = str_replace("'", "", $nameservers[7]);
  352. unset($output);
  353. }
  354. $accept = $_GET["accept"] ?? "";
  355. render_page($user, $TAB, "add_dns");
  356. } else {
  357. // Display body for dns record
  358. $v_domain = $_GET["domain"];
  359. if (empty($v_rec)) {
  360. $v_rec = "@";
  361. }
  362. if (empty($v_type)) {
  363. $v_type = "";
  364. }
  365. if (empty($v_val)) {
  366. $v_val = "";
  367. }
  368. if (empty($v_priority)) {
  369. $v_priority = "";
  370. }
  371. if (empty($v_ttl)) {
  372. $v_ttl = "";
  373. }
  374. if (empty($v_dnssec)) {
  375. $v_dnssec = "";
  376. }
  377. render_page($user, $TAB, "add_dns_rec");
  378. }
  379. // Flush session messages
  380. unset($_SESSION["error_msg"]);
  381. unset($_SESSION["ok_msg"]);