index.php 8.5 KB

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