index.php 9.2 KB

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