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