index.php 9.2 KB

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