index.php 8.5 KB

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