index.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 domains');
  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[] = _('ns1');
  85. }
  86. if (empty($_POST['v_ns2'])) {
  87. $errors[] = _('ns2');
  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(HESTIA_CMD."v-add-user-package ".$tmpfile." ".$v_package, $output, $return_var);
  179. check_return_code($return_var, $output);
  180. unset($output);
  181. fclose($fp);
  182. unlink($tmpfile);
  183. }
  184. // Flush field values on success
  185. if (empty($_SESSION['error_msg'])) {
  186. $_SESSION['ok_msg'] = sprintf(_('PACKAGE_CREATED_OK'), htmlentities($_POST['v_package']), htmlentities($_POST['v_package']));
  187. unset($v_package);
  188. }
  189. }
  190. // List web temmplates
  191. exec(HESTIA_CMD."v-list-web-templates json", $output, $return_var);
  192. $web_templates = json_decode(implode('', $output), true);
  193. unset($output);
  194. // List web templates for backend
  195. if (!empty($_SESSION['WEB_BACKEND'])) {
  196. exec(HESTIA_CMD."v-list-web-templates-backend json", $output, $return_var);
  197. $backend_templates = json_decode(implode('', $output), true);
  198. unset($output);
  199. }
  200. // List web templates for proxy
  201. if (!empty($_SESSION['PROXY_SYSTEM'])) {
  202. exec(HESTIA_CMD."v-list-web-templates-proxy json", $output, $return_var);
  203. $proxy_templates = json_decode(implode('', $output), true);
  204. unset($output);
  205. }
  206. // List DNS templates
  207. exec(HESTIA_CMD."v-list-dns-templates json", $output, $return_var);
  208. $dns_templates = json_decode(implode('', $output), true);
  209. unset($output);
  210. // List system shells
  211. exec(HESTIA_CMD."v-list-sys-shells json", $output, $return_var);
  212. $shells = json_decode(implode('', $output), true);
  213. unset($output);
  214. // Set default values
  215. if (empty($v_package)) {
  216. $v_package = '';
  217. }
  218. if (empty($v_web_template)) {
  219. $v_web_template = 'default';
  220. }
  221. if (empty($v_backend_template)) {
  222. $v_backend_template = 'default';
  223. }
  224. if (empty($v_proxy_template)) {
  225. $v_proxy_template = 'default';
  226. }
  227. if (empty($v_dns_template)) {
  228. $v_dns_template = 'default';
  229. }
  230. if (empty($v_shell)) {
  231. $v_shell = 'nologin';
  232. }
  233. if (empty($v_web_domains)) {
  234. $v_web_domains = "'1'";
  235. }
  236. if (empty($v_web_aliases)) {
  237. $v_web_aliases = "'5'";
  238. }
  239. if (empty($v_dns_domains)) {
  240. $v_dns_domains = "'1'";
  241. }
  242. if (empty($v_dns_records)) {
  243. $v_dns_records = "'unlimited'";
  244. }
  245. if (empty($v_mail_domains)) {
  246. $v_mail_domains = "'1'";
  247. }
  248. if (empty($v_mail_accounts)) {
  249. $v_mail_accounts = "'5'";
  250. }
  251. if (empty($v_databases)) {
  252. $v_databases = "'1'";
  253. }
  254. if (empty($v_cron_jobs)) {
  255. $v_cron_jobs = "'1'";
  256. }
  257. if (empty($v_backups)) {
  258. $v_backups = "'1'";
  259. }
  260. if (empty($v_disk_quota)) {
  261. $v_disk_quota = "'1000'";
  262. }
  263. if (empty($v_bandwidth)) {
  264. $v_bandwidth = "'1000'";
  265. }
  266. if (empty($v_ratelimit)) {
  267. $v_ratelimit = "'200'";
  268. }
  269. if (empty($v_ns1)) {
  270. $v_ns1 = 'ns1.example.ltd';
  271. }
  272. if (empty($v_ns2)) {
  273. $v_ns2 = 'ns2.example.ltd';
  274. }
  275. if (empty($v_ns3)) {
  276. $v_ns3 = '';
  277. }
  278. if (empty($v_ns4)) {
  279. $v_ns4 = '';
  280. }
  281. if (empty($v_ns5)) {
  282. $v_ns5 = '';
  283. }
  284. if (empty($v_ns6)) {
  285. $v_ns6 = '';
  286. }
  287. if (empty($v_ns7)) {
  288. $v_ns7 = '';
  289. }
  290. if (empty($v_ns8)) {
  291. $v_ns8 = '';
  292. }
  293. // Render page
  294. render_page($user, $TAB, 'add_package');
  295. // Flush session messages
  296. unset($_SESSION['error_msg']);
  297. unset($_SESSION['ok_msg']);