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