index.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. error_reporting(null);
  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 = escapeshellarg($_POST['v_package']);
  93. $v_web_template = escapeshellarg($_POST['v_web_template']);
  94. $v_backend_template = escapeshellarg($_POST['v_backend_template']);
  95. $v_proxy_template = escapeshellarg($_POST['v_proxy_template']);
  96. $v_dns_template = escapeshellarg($_POST['v_dns_template']);
  97. $v_shell = escapeshellarg($_POST['v_shell']);
  98. $v_web_domains = escapeshellarg($_POST['v_web_domains']);
  99. $v_web_aliases = escapeshellarg($_POST['v_web_aliases']);
  100. $v_dns_domains = escapeshellarg($_POST['v_dns_domains']);
  101. $v_dns_records = escapeshellarg($_POST['v_dns_records']);
  102. $v_mail_domains = escapeshellarg($_POST['v_mail_domains']);
  103. $v_mail_accounts = escapeshellarg($_POST['v_mail_accounts']);
  104. $v_databases = escapeshellarg($_POST['v_databases']);
  105. $v_cron_jobs = escapeshellarg($_POST['v_cron_jobs']);
  106. $v_backups = escapeshellarg($_POST['v_backups']);
  107. $v_disk_quota = escapeshellarg($_POST['v_disk_quota']);
  108. $v_bandwidth = escapeshellarg($_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 = escapeshellarg($v_ns);
  137. $v_time = escapeshellarg(date('H:i:s'));
  138. $v_date = escapeshellarg(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. }
  172. // Flush field values on success
  173. if (empty($_SESSION['error_msg'])) {
  174. $_SESSION['ok_msg'] = sprintf(_('PACKAGE_CREATED_OK'), htmlentities($_POST['v_package']), htmlentities($_POST['v_package']));
  175. unset($v_package);
  176. }
  177. }
  178. // List web temmplates
  179. exec(HESTIA_CMD."v-list-web-templates json", $output, $return_var);
  180. $web_templates = json_decode(implode('', $output), true);
  181. unset($output);
  182. // List web templates for backend
  183. if (!empty($_SESSION['WEB_BACKEND'])) {
  184. exec(HESTIA_CMD."v-list-web-templates-backend json", $output, $return_var);
  185. $backend_templates = json_decode(implode('', $output), true);
  186. unset($output);
  187. }
  188. // List web templates for proxy
  189. if (!empty($_SESSION['PROXY_SYSTEM'])) {
  190. exec(HESTIA_CMD."v-list-web-templates-proxy json", $output, $return_var);
  191. $proxy_templates = json_decode(implode('', $output), true);
  192. unset($output);
  193. }
  194. // List DNS templates
  195. exec(HESTIA_CMD."v-list-dns-templates json", $output, $return_var);
  196. $dns_templates = json_decode(implode('', $output), true);
  197. unset($output);
  198. // List system shells
  199. exec(HESTIA_CMD."v-list-sys-shells json", $output, $return_var);
  200. $shells = json_decode(implode('', $output), true);
  201. unset($output);
  202. // Set default values
  203. if (empty($v_web_template)) {
  204. $v_web_template = 'default';
  205. }
  206. if (empty($v_backend_template)) {
  207. $v_backend_template = 'default';
  208. }
  209. if (empty($v_proxy_template)) {
  210. $v_proxy_template = 'default';
  211. }
  212. if (empty($v_dns_template)) {
  213. $v_dns_template = 'default';
  214. }
  215. if (empty($v_shell)) {
  216. $v_shell = 'nologin';
  217. }
  218. if (empty($v_web_domains)) {
  219. $v_web_domains = "'1'";
  220. }
  221. if (empty($v_web_aliases)) {
  222. $v_web_aliases = "'1'";
  223. }
  224. if (empty($v_dns_domains)) {
  225. $v_dns_domains = "'1'";
  226. }
  227. if (empty($v_dns_records)) {
  228. $v_dns_records = "'1'";
  229. }
  230. if (empty($v_mail_domains)) {
  231. $v_mail_domains = "'1'";
  232. }
  233. if (empty($v_mail_accounts)) {
  234. $v_mail_accounts = "'1'";
  235. }
  236. if (empty($v_databases)) {
  237. $v_databases = "'1'";
  238. }
  239. if (empty($v_cron_jobs)) {
  240. $v_cron_jobs = "'1'";
  241. }
  242. if (empty($v_backups)) {
  243. $v_backups = "'1'";
  244. }
  245. if (empty($v_disk_quota)) {
  246. $v_disk_quota = "'1000'";
  247. }
  248. if (empty($v_bandwidth)) {
  249. $v_bandwidth = "'1000'";
  250. }
  251. if (empty($v_ns1)) {
  252. $v_ns1 = 'ns1.example.ltd';
  253. }
  254. if (empty($v_ns2)) {
  255. $v_ns2 = 'ns2.example.ltd';
  256. }
  257. // Render page
  258. render_page($user, $TAB, 'add_package');
  259. // Flush session messages
  260. unset($_SESSION['error_msg']);
  261. unset($_SESSION['ok_msg']);