index.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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[] = _("Incremental Backups");
  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 (!empty($errors[0])) {
  109. foreach ($errors as $i => $error) {
  110. if ($i == 0) {
  111. $error_msg = $error;
  112. } else {
  113. $error_msg = $error_msg . ", " . $error;
  114. }
  115. }
  116. $_SESSION["error_msg"] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  117. } else {
  118. // Protect input
  119. $v_package = quoteshellarg($_POST["v_package"]);
  120. $v_web_template = quoteshellarg($_POST["v_web_template"]);
  121. $v_backend_template = quoteshellarg($_POST["v_backend_template"]);
  122. $v_proxy_template = quoteshellarg($_POST["v_proxy_template"]);
  123. $v_dns_template = quoteshellarg($_POST["v_dns_template"]);
  124. $v_shell = quoteshellarg($_POST["v_shell"]);
  125. $v_web_domains = quoteshellarg($_POST["v_web_domains"]);
  126. $v_web_aliases = quoteshellarg($_POST["v_web_aliases"]);
  127. $v_dns_domains = quoteshellarg($_POST["v_dns_domains"]);
  128. $v_dns_records = quoteshellarg($_POST["v_dns_records"]);
  129. $v_mail_domains = quoteshellarg($_POST["v_mail_domains"]);
  130. $v_mail_accounts = quoteshellarg($_POST["v_mail_accounts"]);
  131. $v_databases = quoteshellarg($_POST["v_databases"]);
  132. $v_cron_jobs = quoteshellarg($_POST["v_cron_jobs"]);
  133. $v_backups = quoteshellarg($_POST["v_backups"]);
  134. $v_backups_incremental = quoteshellarg($_POST["v_backups_incremental"]);
  135. $v_disk_quota = quoteshellarg($_POST["v_disk_quota"]);
  136. $v_bandwidth = quoteshellarg($_POST["v_bandwidth"]);
  137. $v_ratelimit = quoteshellarg($_POST["v_ratelimit"]);
  138. $v_cpu_quota =
  139. $_SESSION["RESOURCES_LIMIT"] == "yes" ? quoteshellarg($_POST["v_cpu_quota"]) : "";
  140. $v_cpu_quota_period =
  141. $_SESSION["RESOURCES_LIMIT"] == "yes"
  142. ? quoteshellarg($_POST["v_cpu_quota_period"])
  143. : "";
  144. $v_memory_limit =
  145. $_SESSION["RESOURCES_LIMIT"] == "yes" ? quoteshellarg($_POST["v_memory_limit"]) : "";
  146. $v_swap_limit =
  147. $_SESSION["RESOURCES_LIMIT"] == "yes" ? quoteshellarg($_POST["v_swap_limit"]) : "";
  148. $v_ns1 = !empty($_POST["v_ns1"]) ? trim($_POST["v_ns1"], ".") : "";
  149. $v_ns2 = !empty($_POST["v_ns2"]) ? trim($_POST["v_ns2"], ".") : "";
  150. $v_ns3 = !empty($_POST["v_ns3"]) ? trim($_POST["v_ns3"], ".") : "";
  151. $v_ns4 = !empty($_POST["v_ns4"]) ? trim($_POST["v_ns4"], ".") : "";
  152. $v_ns5 = !empty($_POST["v_ns5"]) ? trim($_POST["v_ns5"], ".") : "";
  153. $v_ns6 = !empty($_POST["v_ns6"]) ? trim($_POST["v_ns6"], ".") : "";
  154. $v_ns7 = !empty($_POST["v_ns7"]) ? trim($_POST["v_ns7"], ".") : "";
  155. $v_ns8 = !empty($_POST["v_ns8"]) ? trim($_POST["v_ns8"], ".") : "";
  156. $v_ns = $v_ns1 . "," . $v_ns2;
  157. if (!empty($v_ns3)) {
  158. $v_ns .= "," . $v_ns3;
  159. }
  160. if (!empty($v_ns4)) {
  161. $v_ns .= "," . $v_ns4;
  162. }
  163. if (!empty($v_ns5)) {
  164. $v_ns .= "," . $v_ns5;
  165. }
  166. if (!empty($v_ns6)) {
  167. $v_ns .= "," . $v_ns6;
  168. }
  169. if (!empty($v_ns7)) {
  170. $v_ns .= "," . $v_ns7;
  171. }
  172. if (!empty($v_ns8)) {
  173. $v_ns .= "," . $v_ns8;
  174. }
  175. $v_ns = quoteshellarg($v_ns);
  176. $v_time = quoteshellarg(date("H:i:s"));
  177. $v_date = quoteshellarg(date("Y-m-d"));
  178. // Create package file
  179. if (empty($_SESSION["error_msg"])) {
  180. $pkg = "WEB_TEMPLATE=" . $v_web_template . "\n";
  181. if (!empty($_SESSION["WEB_BACKEND"])) {
  182. $pkg .= "BACKEND_TEMPLATE=" . $v_backend_template . "\n";
  183. }
  184. if (!empty($_SESSION["PROXY_SYSTEM"])) {
  185. $pkg .= "PROXY_TEMPLATE=" . $v_proxy_template . "\n";
  186. }
  187. $pkg .= "DNS_TEMPLATE=" . $v_dns_template . "\n";
  188. $pkg .= "WEB_DOMAINS=" . $v_web_domains . "\n";
  189. $pkg .= "WEB_ALIASES=" . $v_web_aliases . "\n";
  190. $pkg .= "DNS_DOMAINS=" . $v_dns_domains . "\n";
  191. $pkg .= "DNS_RECORDS=" . $v_dns_records . "\n";
  192. $pkg .= "MAIL_DOMAINS=" . $v_mail_domains . "\n";
  193. $pkg .= "MAIL_ACCOUNTS=" . $v_mail_accounts . "\n";
  194. $pkg .= "DATABASES=" . $v_databases . "\n";
  195. $pkg .= "CRON_JOBS=" . $v_cron_jobs . "\n";
  196. $pkg .= "DISK_QUOTA=" . $v_disk_quota . "\n";
  197. $pkg .= "CPU_QUOTA=" . $v_cpu_quota . "\n";
  198. $pkg .= "CPU_QUOTA_PERIOD=" . $v_cpu_quota_period . "\n";
  199. $pkg .= "MEMORY_LIMIT=" . $v_memory_limit . "\n";
  200. $pkg .= "SWAP_LIMIT=" . $v_swap_limit . "\n";
  201. $pkg .= "BANDWIDTH=" . $v_bandwidth . "\n";
  202. $pkg .= "RATE_LIMIT=" . $v_ratelimit . "\n";
  203. $pkg .= "NS=" . $v_ns . "\n";
  204. $pkg .= "SHELL=" . $v_shell . "\n";
  205. $pkg .= "BACKUPS=" . $v_backups . "\n";
  206. $pkg .= "BACKUPS_INCREMENTAL=" . $v_backups_incremental . "\n";
  207. $pkg .= "TIME=" . $v_time . "\n";
  208. $pkg .= "DATE=" . $v_date . "\n";
  209. $tmpfile = tempnam("/tmp/", "hst_");
  210. $fp = fopen($tmpfile, "w");
  211. fwrite($fp, $pkg);
  212. exec(
  213. HESTIA_CMD . "v-add-user-package " . $tmpfile . " " . $v_package,
  214. $output,
  215. $return_var,
  216. );
  217. check_return_code($return_var, $output);
  218. unset($output);
  219. fclose($fp);
  220. unlink($tmpfile);
  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. }
  236. // List web temmplates
  237. exec(HESTIA_CMD . "v-list-web-templates json", $output, $return_var);
  238. $web_templates = json_decode(implode("", $output), true);
  239. unset($output);
  240. // List web templates for backend
  241. if (!empty($_SESSION["WEB_BACKEND"])) {
  242. exec(HESTIA_CMD . "v-list-web-templates-backend json", $output, $return_var);
  243. $backend_templates = json_decode(implode("", $output), true);
  244. unset($output);
  245. }
  246. // List web templates for proxy
  247. if (!empty($_SESSION["PROXY_SYSTEM"])) {
  248. exec(HESTIA_CMD . "v-list-web-templates-proxy json", $output, $return_var);
  249. $proxy_templates = json_decode(implode("", $output), true);
  250. unset($output);
  251. }
  252. // List DNS templates
  253. exec(HESTIA_CMD . "v-list-dns-templates json", $output, $return_var);
  254. $dns_templates = json_decode(implode("", $output), true);
  255. unset($output);
  256. // List system shells
  257. exec(HESTIA_CMD . "v-list-sys-shells json", $output, $return_var);
  258. $shells = json_decode(implode("", $output), true);
  259. unset($output);
  260. // Set default values
  261. if (empty($v_package)) {
  262. $v_package = "";
  263. }
  264. if (empty($v_web_template)) {
  265. $v_web_template = "default";
  266. }
  267. if (empty($v_backend_template)) {
  268. $v_backend_template = "default";
  269. }
  270. if (empty($v_proxy_template)) {
  271. $v_proxy_template = "default";
  272. }
  273. if (empty($v_dns_template)) {
  274. $v_dns_template = "default";
  275. }
  276. if (empty($v_shell)) {
  277. $v_shell = "nologin";
  278. }
  279. if (empty($v_web_domains)) {
  280. $v_web_domains = "'1'";
  281. }
  282. if (empty($v_web_aliases)) {
  283. $v_web_aliases = "'5'";
  284. }
  285. if (empty($v_dns_domains)) {
  286. $v_dns_domains = "'1'";
  287. }
  288. if (empty($v_dns_records)) {
  289. $v_dns_records = "'unlimited'";
  290. }
  291. if (empty($v_mail_domains)) {
  292. $v_mail_domains = "'1'";
  293. }
  294. if (empty($v_mail_accounts)) {
  295. $v_mail_accounts = "'5'";
  296. }
  297. if (empty($v_databases)) {
  298. $v_databases = "'1'";
  299. }
  300. if (empty($v_cron_jobs)) {
  301. $v_cron_jobs = "'1'";
  302. }
  303. if (empty($v_backups)) {
  304. $v_backups = "'1'";
  305. }
  306. if (empty($v_disk_quota)) {
  307. $v_disk_quota = "'1000'";
  308. }
  309. if (empty($v_bandwidth)) {
  310. $v_bandwidth = "'1000'";
  311. }
  312. if (empty($v_ratelimit)) {
  313. $v_ratelimit = "'200'";
  314. }
  315. if (empty($v_cpu_quota)) {
  316. $v_cpu_quota = "'unlimited'";
  317. }
  318. if (empty($v_cpu_quota_period)) {
  319. $v_cpu_quota_period = "'unlimited'";
  320. }
  321. if (empty($v_memory_limit)) {
  322. $v_memory_limit = "'unlimited'";
  323. }
  324. if (empty($v_swap_limit)) {
  325. $v_swap_limit = "'unlimited'";
  326. }
  327. if (empty($v_ns1)) {
  328. $v_ns1 = "ns1.example.tld";
  329. }
  330. if (empty($v_ns2)) {
  331. $v_ns2 = "ns2.example.tld";
  332. }
  333. if (empty($v_ns3)) {
  334. $v_ns3 = "";
  335. }
  336. if (empty($v_ns4)) {
  337. $v_ns4 = "";
  338. }
  339. if (empty($v_ns5)) {
  340. $v_ns5 = "";
  341. }
  342. if (empty($v_ns6)) {
  343. $v_ns6 = "";
  344. }
  345. if (empty($v_ns7)) {
  346. $v_ns7 = "";
  347. }
  348. if (empty($v_ns8)) {
  349. $v_ns8 = "";
  350. }
  351. // Render page
  352. render_page($user, $TAB, "add_package");
  353. // Flush session messages
  354. unset($_SESSION["error_msg"]);
  355. unset($_SESSION["ok_msg"]);