index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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_cpu_quota = $data[$v_package]["CPU_QUOTA"];
  48. $v_cpu_quota_period = $data[$v_package]["CPU_QUOTA_PERIOD"];
  49. $v_memory_limit = $data[$v_package]["MEMORY_LIMIT"];
  50. $v_swap_limit = $data[$v_package]["SWAP_LIMIT"];
  51. $v_ns = $data[$v_package]["NS"];
  52. $nameservers = explode(",", $v_ns);
  53. if (empty($nameservers[0])) {
  54. $v_ns1 = "";
  55. } else {
  56. $v_ns1 = $nameservers[0];
  57. }
  58. if (empty($nameservers[1])) {
  59. $v_ns2 = "";
  60. } else {
  61. $v_ns2 = $nameservers[1];
  62. }
  63. if (empty($nameservers[2])) {
  64. $v_ns3 = "";
  65. } else {
  66. $v_ns3 = $nameservers[2];
  67. }
  68. if (empty($nameservers[3])) {
  69. $v_ns4 = "";
  70. } else {
  71. $v_ns4 = $nameservers[3];
  72. }
  73. if (empty($nameservers[4])) {
  74. $v_ns5 = "";
  75. } else {
  76. $v_ns5 = $nameservers[4];
  77. }
  78. if (empty($nameservers[5])) {
  79. $v_ns6 = "";
  80. } else {
  81. $v_ns6 = $nameservers[5];
  82. }
  83. if (empty($nameservers[6])) {
  84. $v_ns7 = "";
  85. } else {
  86. $v_ns7 = $nameservers[6];
  87. }
  88. if (empty($nameservers[7])) {
  89. $v_ns8 = "";
  90. } else {
  91. $v_ns8 = $nameservers[7];
  92. }
  93. $v_backups = $data[$v_package]["BACKUPS"];
  94. $v_backups_incremental = $data[$v_package]["BACKUPS_INCREMENTAL"];
  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_backups_incremental"])) {
  180. $errors[] = _("Incremental Backups");
  181. }
  182. if (!isset($_POST["v_disk_quota"])) {
  183. $errors[] = _("Quota");
  184. }
  185. if (!isset($_POST["v_bandwidth"])) {
  186. $errors[] = _("Bandwidth");
  187. }
  188. if ($_SESSION["RESOURCES_LIMIT"] == "yes") {
  189. if (!isset($_POST["v_cpu_quota"])) {
  190. $errors[] = _("CPU quota");
  191. }
  192. if (!isset($_POST["v_cpu_quota_period"])) {
  193. $errors[] = _("CPU quota period");
  194. }
  195. if (!isset($_POST["v_memory_limit"])) {
  196. $errors[] = _("Memory Limit");
  197. }
  198. if (!isset($_POST["v_swap_limit"])) {
  199. $errors[] = _("Swap Limit");
  200. }
  201. }
  202. // Check if name server entries are blank if DNS server is installed
  203. if (isset($_SESSION["DNS_SYSTEM"]) && !empty($_SESSION["DNS_SYSTEM"])) {
  204. if (empty($_POST["v_ns1"])) {
  205. $errors[] = _("Nameserver 1");
  206. }
  207. if (empty($_POST["v_ns2"])) {
  208. $errors[] = _("Nameserver 2");
  209. }
  210. }
  211. if (!empty($errors[0])) {
  212. foreach ($errors as $i => $error) {
  213. if ($i == 0) {
  214. $error_msg = $error;
  215. } else {
  216. $error_msg = $error_msg . ", " . $error;
  217. }
  218. }
  219. $_SESSION["error_msg"] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  220. }
  221. // Protect input
  222. $v_package = quoteshellarg($_POST["v_package"]);
  223. $v_package_new = quoteshellarg($_POST["v_package_new"]);
  224. $v_web_template = quoteshellarg($_POST["v_web_template"]);
  225. if (!empty($_SESSION["WEB_BACKEND"])) {
  226. $v_backend_template = quoteshellarg($_POST["v_backend_template"]);
  227. }
  228. if (!empty($_SESSION["PROXY_SYSTEM"])) {
  229. $v_proxy_template = quoteshellarg($_POST["v_proxy_template"]);
  230. }
  231. $v_dns_template = quoteshellarg($_POST["v_dns_template"]);
  232. if (!empty($_POST["v_shell"])) {
  233. $v_shell = quoteshellarg($_POST["v_shell"]);
  234. } else {
  235. $v_shell = "nologin";
  236. }
  237. $v_web_domains = quoteshellarg($_POST["v_web_domains"]);
  238. $v_web_aliases = quoteshellarg($_POST["v_web_aliases"]);
  239. $v_dns_domains = quoteshellarg($_POST["v_dns_domains"]);
  240. $v_dns_records = quoteshellarg($_POST["v_dns_records"]);
  241. $v_mail_domains = quoteshellarg($_POST["v_mail_domains"]);
  242. $v_mail_accounts = quoteshellarg($_POST["v_mail_accounts"]);
  243. $v_ratelimit = quoteshellarg($_POST["v_ratelimit"]);
  244. $v_databases = quoteshellarg($_POST["v_databases"]);
  245. $v_cron_jobs = quoteshellarg($_POST["v_cron_jobs"]);
  246. $v_backups = quoteshellarg($_POST["v_backups"]);
  247. $v_backups_incremental = quoteshellarg($_POST["v_backups_incremental"]);
  248. $v_disk_quota = quoteshellarg($_POST["v_disk_quota"]);
  249. $v_bandwidth = quoteshellarg($_POST["v_bandwidth"]);
  250. $v_cpu_quota =
  251. $_SESSION["RESOURCES_LIMIT"] == "yes" ? quoteshellarg($_POST["v_cpu_quota"]) : "";
  252. $v_cpu_quota_period =
  253. $_SESSION["RESOURCES_LIMIT"] == "yes" ? quoteshellarg($_POST["v_cpu_quota_period"]) : "";
  254. $v_memory_limit =
  255. $_SESSION["RESOURCES_LIMIT"] == "yes" ? quoteshellarg($_POST["v_memory_limit"]) : "";
  256. $v_swap_limit =
  257. $_SESSION["RESOURCES_LIMIT"] == "yes" ? quoteshellarg($_POST["v_swap_limit"]) : "";
  258. $v_ns1 = !empty($_POST["v_ns1"]) ? trim($_POST["v_ns1"], ".") : "";
  259. $v_ns2 = !empty($_POST["v_ns2"]) ? trim($_POST["v_ns2"], ".") : "";
  260. $v_ns3 = !empty($_POST["v_ns3"]) ? trim($_POST["v_ns3"], ".") : "";
  261. $v_ns4 = !empty($_POST["v_ns4"]) ? trim($_POST["v_ns4"], ".") : "";
  262. $v_ns5 = !empty($_POST["v_ns5"]) ? trim($_POST["v_ns5"], ".") : "";
  263. $v_ns6 = !empty($_POST["v_ns6"]) ? trim($_POST["v_ns6"], ".") : "";
  264. $v_ns7 = !empty($_POST["v_ns7"]) ? trim($_POST["v_ns7"], ".") : "";
  265. $v_ns8 = !empty($_POST["v_ns8"]) ? trim($_POST["v_ns8"], ".") : "";
  266. $v_ns = $v_ns1 . "," . $v_ns2;
  267. if (!empty($v_ns3)) {
  268. $v_ns .= "," . $v_ns3;
  269. }
  270. if (!empty($v_ns4)) {
  271. $v_ns .= "," . $v_ns4;
  272. }
  273. if (!empty($v_ns5)) {
  274. $v_ns .= "," . $v_ns5;
  275. }
  276. if (!empty($v_ns6)) {
  277. $v_ns .= "," . $v_ns6;
  278. }
  279. if (!empty($v_ns7)) {
  280. $v_ns .= "," . $v_ns7;
  281. }
  282. if (!empty($v_ns8)) {
  283. $v_ns .= "," . $v_ns8;
  284. }
  285. $v_ns = quoteshellarg($v_ns);
  286. $v_time = quoteshellarg(date("H:i:s"));
  287. $v_date = quoteshellarg(date("Y-m-d"));
  288. // Save package file on a fs
  289. $pkg = "WEB_TEMPLATE=" . $v_web_template . "\n";
  290. $pkg .= "BACKEND_TEMPLATE=" . $v_backend_template . "\n";
  291. $pkg .= "PROXY_TEMPLATE=" . $v_proxy_template . "\n";
  292. $pkg .= "DNS_TEMPLATE=" . $v_dns_template . "\n";
  293. $pkg .= "WEB_DOMAINS=" . $v_web_domains . "\n";
  294. $pkg .= "WEB_ALIASES=" . $v_web_aliases . "\n";
  295. $pkg .= "DNS_DOMAINS=" . $v_dns_domains . "\n";
  296. $pkg .= "DNS_RECORDS=" . $v_dns_records . "\n";
  297. $pkg .= "MAIL_DOMAINS=" . $v_mail_domains . "\n";
  298. $pkg .= "MAIL_ACCOUNTS=" . $v_mail_accounts . "\n";
  299. $pkg .= "RATE_LIMIT=" . $v_ratelimit . "\n";
  300. $pkg .= "DATABASES=" . $v_databases . "\n";
  301. $pkg .= "CRON_JOBS=" . $v_cron_jobs . "\n";
  302. $pkg .= "DISK_QUOTA=" . $v_disk_quota . "\n";
  303. $pkg .= "CPU_QUOTA=" . $v_cpu_quota . "\n";
  304. $pkg .= "CPU_QUOTA_PERIOD=" . $v_cpu_quota_period . "\n";
  305. $pkg .= "MEMORY_LIMIT=" . $v_memory_limit . "\n";
  306. $pkg .= "SWAP_LIMIT=" . $v_swap_limit . "\n";
  307. $pkg .= "BANDWIDTH=" . $v_bandwidth . "\n";
  308. $pkg .= "NS=" . $v_ns . "\n";
  309. $pkg .= "SHELL=" . $v_shell . "\n";
  310. $pkg .= "BACKUPS=" . $v_backups . "\n";
  311. $pkg .= "BACKUPS_INCREMENTAL=" . $v_backups_incremental . "\n";
  312. $pkg .= "TIME=" . $v_time . "\n";
  313. $pkg .= "DATE=" . $v_date . "\n";
  314. $tmpfile = tempnam("/tmp/", "hst_");
  315. $fp = fopen($tmpfile, "w");
  316. fwrite($fp, $pkg);
  317. exec(
  318. HESTIA_CMD . "v-add-user-package " . $tmpfile . " " . $v_package . " yes",
  319. $output,
  320. $return_var,
  321. );
  322. check_return_code($return_var, $output);
  323. unset($output);
  324. fclose($fp);
  325. unlink($tmpfile);
  326. // Propagate new package
  327. exec(HESTIA_CMD . "v-update-user-package " . $v_package . " 'json'", $output, $return_var);
  328. check_return_code($return_var, $output);
  329. unset($output);
  330. if ($v_package_new != $v_package) {
  331. exec(
  332. HESTIA_CMD . "v-rename-user-package " . $v_package . " " . $v_package_new,
  333. $output,
  334. $return_var,
  335. );
  336. check_return_code($return_var, $output);
  337. unset($output);
  338. }
  339. // Set success message
  340. if (empty($_SESSION["error_msg"])) {
  341. $_SESSION["ok_msg"] = _("Changes have been saved.");
  342. }
  343. }
  344. // Render page
  345. render_page($user, $TAB, "edit_package");
  346. // Flush session messages
  347. unset($_SESSION["error_msg"]);
  348. unset($_SESSION["ok_msg"]);