index.php 8.3 KB

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