index.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. use function Hestiacp\quoteshellarg\quoteshellarg;
  3. ob_start();
  4. $TAB = "PACKAGE";
  5. // Main include
  6. include $_SERVER["DOCUMENT_ROOT"] . "/inc/main.php";
  7. // Check user
  8. if ($_SESSION["userContext"] != "admin") {
  9. header("Location: /list/user");
  10. exit();
  11. }
  12. // Check package argument
  13. if (empty($_GET["package"])) {
  14. header("Location: /list/package/");
  15. exit();
  16. }
  17. // Prevent editing of system package
  18. if ($_GET["package"] === "system") {
  19. header("Location: /list/package/");
  20. exit();
  21. }
  22. // List package
  23. $v_package = quoteshellarg($_GET["package"]);
  24. exec(HESTIA_CMD . "v-list-user-package " . $v_package . " 'json'", $output, $return_var);
  25. check_return_code_redirect($return_var, $output, "/list/package/");
  26. $data = json_decode(implode("", $output), true);
  27. unset($output);
  28. // Parse package
  29. $v_package = $_GET["package"];
  30. $v_package_new = $_GET["package"];
  31. $v_web_template = $data[$v_package]["WEB_TEMPLATE"];
  32. $v_backend_template = $data[$v_package]["BACKEND_TEMPLATE"];
  33. $v_proxy_template = $data[$v_package]["PROXY_TEMPLATE"];
  34. $v_dns_template = $data[$v_package]["DNS_TEMPLATE"];
  35. $v_web_domains = $data[$v_package]["WEB_DOMAINS"];
  36. $v_web_aliases = $data[$v_package]["WEB_ALIASES"];
  37. $v_dns_domains = $data[$v_package]["DNS_DOMAINS"];
  38. $v_dns_records = $data[$v_package]["DNS_RECORDS"];
  39. $v_mail_domains = $data[$v_package]["MAIL_DOMAINS"];
  40. $v_mail_accounts = $data[$v_package]["MAIL_ACCOUNTS"];
  41. $v_ratelimit = $data[$v_package]["RATE_LIMIT"];
  42. $v_databases = $data[$v_package]["DATABASES"];
  43. $v_cron_jobs = $data[$v_package]["CRON_JOBS"];
  44. $v_disk_quota = $data[$v_package]["DISK_QUOTA"];
  45. $v_bandwidth = $data[$v_package]["BANDWIDTH"];
  46. $v_shell = $data[$v_package]["SHELL"];
  47. $v_shell_jail_enabled = $data[$v_package]["SHELL_JAIL_ENABLED"];
  48. $v_ns = $data[$v_package]["NS"];
  49. $nameservers = explode(",", $v_ns);
  50. if (empty($nameservers[0])) {
  51. $v_ns1 = "";
  52. } else {
  53. $v_ns1 = $nameservers[0];
  54. }
  55. if (empty($nameservers[1])) {
  56. $v_ns2 = "";
  57. } else {
  58. $v_ns2 = $nameservers[1];
  59. }
  60. if (empty($nameservers[2])) {
  61. $v_ns3 = "";
  62. } else {
  63. $v_ns3 = $nameservers[2];
  64. }
  65. if (empty($nameservers[3])) {
  66. $v_ns4 = "";
  67. } else {
  68. $v_ns4 = $nameservers[3];
  69. }
  70. if (empty($nameservers[4])) {
  71. $v_ns5 = "";
  72. } else {
  73. $v_ns5 = $nameservers[4];
  74. }
  75. if (empty($nameservers[5])) {
  76. $v_ns6 = "";
  77. } else {
  78. $v_ns6 = $nameservers[5];
  79. }
  80. if (empty($nameservers[6])) {
  81. $v_ns7 = "";
  82. } else {
  83. $v_ns7 = $nameservers[6];
  84. }
  85. if (empty($nameservers[7])) {
  86. $v_ns8 = "";
  87. } else {
  88. $v_ns8 = $nameservers[7];
  89. }
  90. $v_backups = $data[$v_package]["BACKUPS"];
  91. $v_date = $data[$v_package]["DATE"];
  92. $v_time = $data[$v_package]["TIME"];
  93. $v_status = "active";
  94. // List web templates
  95. exec(HESTIA_CMD . "v-list-web-templates json", $output, $return_var);
  96. $web_templates = json_decode(implode("", $output), true);
  97. unset($output);
  98. // List backend templates
  99. if (!empty($_SESSION["WEB_BACKEND"])) {
  100. exec(HESTIA_CMD . "v-list-web-templates-backend json", $output, $return_var);
  101. $backend_templates = json_decode(implode("", $output), true);
  102. unset($output);
  103. }
  104. // List proxy templates
  105. if (!empty($_SESSION["PROXY_SYSTEM"])) {
  106. exec(HESTIA_CMD . "v-list-web-templates-proxy json", $output, $return_var);
  107. $proxy_templates = json_decode(implode("", $output), true);
  108. unset($output);
  109. }
  110. // List dns templates
  111. exec(HESTIA_CMD . "v-list-dns-templates json", $output, $return_var);
  112. $dns_templates = json_decode(implode("", $output), true);
  113. unset($output);
  114. // List shels
  115. exec(HESTIA_CMD . "v-list-sys-shells json", $output, $return_var);
  116. $shells = json_decode(implode("", $output), true);
  117. unset($output);
  118. // Check POST request
  119. if (!empty($_POST["save"])) {
  120. // Check token
  121. verify_csrf($_POST);
  122. // Check empty fields
  123. if (empty($_POST["v_package"])) {
  124. $errors[] = _("Package");
  125. }
  126. if (empty($_POST["v_web_template"])) {
  127. $errors[] = _("Web Template");
  128. }
  129. if (!empty($_SESSION["WEB_BACKEND"])) {
  130. if (empty($_POST["v_backend_template"])) {
  131. $errors[] = _("Backend Template");
  132. }
  133. }
  134. if (!empty($_SESSION["PROXY_SYSTEM"])) {
  135. if (empty($_POST["v_proxy_template"])) {
  136. $errors[] = _("Proxy Template");
  137. }
  138. }
  139. if (empty($_POST["v_dns_template"])) {
  140. $errors[] = _("DNS Template");
  141. }
  142. if (empty($_POST["v_shell"])) {
  143. $errrors[] = _("Shell");
  144. }
  145. if (!isset($_POST["v_web_domains"])) {
  146. $errors[] = _("Web Domains");
  147. }
  148. if (!isset($_POST["v_web_aliases"])) {
  149. $errors[] = _("Web Aliases");
  150. }
  151. if (!isset($_POST["v_dns_domains"])) {
  152. $errors[] = _("DNS Zones");
  153. }
  154. if (!isset($_POST["v_dns_records"])) {
  155. $errors[] = _("DNS Records");
  156. }
  157. if (!isset($_POST["v_mail_domains"])) {
  158. $errors[] = _("Mail Domains");
  159. }
  160. if (!isset($_POST["v_mail_accounts"])) {
  161. $errors[] = _("Mail Accounts");
  162. }
  163. if (!isset($_POST["v_ratelimit"])) {
  164. $errors[] = _("Rate Limit");
  165. }
  166. if (!isset($_POST["v_databases"])) {
  167. $errors[] = _("Databases");
  168. }
  169. if (!isset($_POST["v_cron_jobs"])) {
  170. $errors[] = _("Cron Jobs");
  171. }
  172. if (!isset($_POST["v_backups"])) {
  173. $errors[] = _("Backups");
  174. }
  175. if (!isset($_POST["v_disk_quota"])) {
  176. $errors[] = _("Quota");
  177. }
  178. if (!isset($_POST["v_bandwidth"])) {
  179. $errors[] = _("Bandwidth");
  180. }
  181. // Check if name server entries are blank if DNS server is installed
  182. if (isset($_SESSION["DNS_SYSTEM"]) && !empty($_SESSION["DNS_SYSTEM"])) {
  183. if (empty($_POST["v_ns1"])) {
  184. $errors[] = _("Nameserver 1");
  185. }
  186. if (empty($_POST["v_ns2"])) {
  187. $errors[] = _("Nameserver 2");
  188. }
  189. }
  190. if (
  191. isset($_POST["v_shell"]) &&
  192. isset($_POST["v_shell_jail_enabled"]) &&
  193. in_array($_POST["v_shell"], ["nologin", "rssh"]) &&
  194. $_POST["v_shell_jail_enabled"] == "yes"
  195. ) {
  196. $_SESSION["error_msg"] = _("Cannot combine nologin and rssh shell with jailed shell.");
  197. }
  198. if (!empty($errors[0])) {
  199. foreach ($errors as $i => $error) {
  200. if ($i == 0) {
  201. $error_msg = $error;
  202. } else {
  203. $error_msg = $error_msg . ", " . $error;
  204. }
  205. }
  206. $_SESSION["error_msg"] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  207. }
  208. // Protect input
  209. $v_package = quoteshellarg($_POST["v_package"]);
  210. $v_package_new = quoteshellarg($_POST["v_package_new"]);
  211. $v_web_template = quoteshellarg($_POST["v_web_template"]);
  212. if (!empty($_SESSION["WEB_BACKEND"])) {
  213. $v_backend_template = quoteshellarg($_POST["v_backend_template"]);
  214. }
  215. if (!empty($_SESSION["PROXY_SYSTEM"])) {
  216. $v_proxy_template = quoteshellarg($_POST["v_proxy_template"]);
  217. }
  218. $v_dns_template = quoteshellarg($_POST["v_dns_template"]);
  219. if (!empty($_POST["v_shell"])) {
  220. $v_shell = quoteshellarg($_POST["v_shell"]);
  221. } else {
  222. $v_shell = "nologin";
  223. }
  224. if (!empty($_POST["v_shell_jail_enabled"])) {
  225. $v_shell_jail_enabled = quoteshellarg($_POST["v_shell_jail_enabled"]);
  226. } else {
  227. $v_shell_jail_enabled = "no";
  228. }
  229. $v_web_domains = quoteshellarg($_POST["v_web_domains"]);
  230. $v_web_aliases = quoteshellarg($_POST["v_web_aliases"]);
  231. $v_dns_domains = quoteshellarg($_POST["v_dns_domains"]);
  232. $v_dns_records = quoteshellarg($_POST["v_dns_records"]);
  233. $v_mail_domains = quoteshellarg($_POST["v_mail_domains"]);
  234. $v_mail_accounts = quoteshellarg($_POST["v_mail_accounts"]);
  235. $v_ratelimit = quoteshellarg($_POST["v_ratelimit"]);
  236. $v_databases = quoteshellarg($_POST["v_databases"]);
  237. $v_cron_jobs = quoteshellarg($_POST["v_cron_jobs"]);
  238. $v_backups = quoteshellarg($_POST["v_backups"]);
  239. $v_disk_quota = quoteshellarg($_POST["v_disk_quota"]);
  240. $v_bandwidth = quoteshellarg($_POST["v_bandwidth"]);
  241. $v_ns1 = !empty($_POST["v_ns1"]) ? trim($_POST["v_ns1"], ".") : "";
  242. $v_ns2 = !empty($_POST["v_ns2"]) ? trim($_POST["v_ns2"], ".") : "";
  243. $v_ns3 = !empty($_POST["v_ns3"]) ? trim($_POST["v_ns3"], ".") : "";
  244. $v_ns4 = !empty($_POST["v_ns4"]) ? trim($_POST["v_ns4"], ".") : "";
  245. $v_ns5 = !empty($_POST["v_ns5"]) ? trim($_POST["v_ns5"], ".") : "";
  246. $v_ns6 = !empty($_POST["v_ns6"]) ? trim($_POST["v_ns6"], ".") : "";
  247. $v_ns7 = !empty($_POST["v_ns7"]) ? trim($_POST["v_ns7"], ".") : "";
  248. $v_ns8 = !empty($_POST["v_ns8"]) ? trim($_POST["v_ns8"], ".") : "";
  249. $v_ns = $v_ns1 . "," . $v_ns2;
  250. if (!empty($v_ns3)) {
  251. $v_ns .= "," . $v_ns3;
  252. }
  253. if (!empty($v_ns4)) {
  254. $v_ns .= "," . $v_ns4;
  255. }
  256. if (!empty($v_ns5)) {
  257. $v_ns .= "," . $v_ns5;
  258. }
  259. if (!empty($v_ns6)) {
  260. $v_ns .= "," . $v_ns6;
  261. }
  262. if (!empty($v_ns7)) {
  263. $v_ns .= "," . $v_ns7;
  264. }
  265. if (!empty($v_ns8)) {
  266. $v_ns .= "," . $v_ns8;
  267. }
  268. $v_ns = quoteshellarg($v_ns);
  269. $v_time = quoteshellarg(date("H:i:s"));
  270. $v_date = quoteshellarg(date("Y-m-d"));
  271. // Save package file on a fs
  272. $pkg = "WEB_TEMPLATE=" . $v_web_template . "\n";
  273. $pkg .= "BACKEND_TEMPLATE=" . $v_backend_template . "\n";
  274. $pkg .= "PROXY_TEMPLATE=" . $v_proxy_template . "\n";
  275. $pkg .= "DNS_TEMPLATE=" . $v_dns_template . "\n";
  276. $pkg .= "WEB_DOMAINS=" . $v_web_domains . "\n";
  277. $pkg .= "WEB_ALIASES=" . $v_web_aliases . "\n";
  278. $pkg .= "DNS_DOMAINS=" . $v_dns_domains . "\n";
  279. $pkg .= "DNS_RECORDS=" . $v_dns_records . "\n";
  280. $pkg .= "MAIL_DOMAINS=" . $v_mail_domains . "\n";
  281. $pkg .= "MAIL_ACCOUNTS=" . $v_mail_accounts . "\n";
  282. $pkg .= "RATE_LIMIT=" . $v_ratelimit . "\n";
  283. $pkg .= "DATABASES=" . $v_databases . "\n";
  284. $pkg .= "CRON_JOBS=" . $v_cron_jobs . "\n";
  285. $pkg .= "DISK_QUOTA=" . $v_disk_quota . "\n";
  286. $pkg .= "BANDWIDTH=" . $v_bandwidth . "\n";
  287. $pkg .= "NS=" . $v_ns . "\n";
  288. $pkg .= "SHELL=" . $v_shell . "\n";
  289. $pkg .= "SHELL_JAIL_ENABLED=" . $v_shell_jail_enabled . "\n";
  290. $pkg .= "BACKUPS=" . $v_backups . "\n";
  291. $pkg .= "TIME=" . $v_time . "\n";
  292. $pkg .= "DATE=" . $v_date . "\n";
  293. $tmpfile = tempnam("/tmp/", "hst_");
  294. $fp = fopen($tmpfile, "w");
  295. fwrite($fp, $pkg);
  296. exec(
  297. HESTIA_CMD . "v-add-user-package " . $tmpfile . " " . $v_package . " yes",
  298. $output,
  299. $return_var,
  300. );
  301. check_return_code($return_var, $output);
  302. unset($output);
  303. fclose($fp);
  304. unlink($tmpfile);
  305. // Propagate new package
  306. exec(HESTIA_CMD . "v-update-user-package " . $v_package . " 'json'", $output, $return_var);
  307. check_return_code($return_var, $output);
  308. unset($output);
  309. if ($v_package_new != $v_package) {
  310. exec(
  311. HESTIA_CMD . "v-rename-user-package " . $v_package . " " . $v_package_new,
  312. $output,
  313. $return_var,
  314. );
  315. check_return_code($return_var, $output);
  316. unset($output);
  317. }
  318. // Set success message
  319. if (empty($_SESSION["error_msg"])) {
  320. $_SESSION["ok_msg"] = _("Changes have been saved.");
  321. }
  322. }
  323. // Render page
  324. render_page($user, $TAB, "edit_package");
  325. // Flush session messages
  326. unset($_SESSION["error_msg"]);
  327. unset($_SESSION["ok_msg"]);