index.php 11 KB

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