index.php 12 KB

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