index.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. error_reporting(NULL);
  3. ob_start();
  4. $TAB = 'PACKAGE';
  5. // Main include
  6. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  7. header('Content-Type: application/json');
  8. // Check user
  9. if ($_SESSION['user'] != 'admin') {
  10. exit;
  11. }
  12. // Check POST request
  13. if (!empty($_POST['ok'])) {
  14. // Check token
  15. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  16. exit();
  17. }
  18. // Check empty fields
  19. if (empty($_POST['v_package'])) $errors[] = __('package');
  20. if (empty($_POST['v_web_template'])) $errors[] = __('web template');
  21. if (!empty($_SESSION['WEB_BACKEND'])) {
  22. if (empty($_POST['v_backend_template'])) $errors[] = __('backend template');
  23. }
  24. if (!empty($_SESSION['PROXY_SYSTEM'])) {
  25. if (empty($_POST['v_proxy_template'])) $errors[] = __('proxy template');
  26. }
  27. if (empty($_POST['v_dns_template'])) $errors[] = __('dns template');
  28. if (empty($_POST['v_shell'])) $errrors[] = __('shell');
  29. if (!isset($_POST['v_web_domains'])) $errors[] = __('web domains');
  30. if (!isset($_POST['v_web_aliases'])) $errors[] = __('web aliases');
  31. if (!isset($_POST['v_dns_domains'])) $errors[] = __('dns domains');
  32. if (!isset($_POST['v_dns_records'])) $errors[] = __('dns records');
  33. if (!isset($_POST['v_mail_domains'])) $errors[] = __('mail domains');
  34. if (!isset($_POST['v_mail_accounts'])) $errors[] = __('mail accounts');
  35. if (!isset($_POST['v_databases'])) $errors[] = __('databases');
  36. if (!isset($_POST['v_cron_jobs'])) $errors[] = __('cron jobs');
  37. if (!isset($_POST['v_backups'])) $errors[] = __('backups');
  38. if (!isset($_POST['v_disk_quota'])) $errors[] = __('quota');
  39. if (!isset($_POST['v_bandwidth'])) $errors[] = __('bandwidth');
  40. if (empty($_POST['v_ns1'])) $errors[] = __('ns1');
  41. if (empty($_POST['v_ns2'])) $errors[] = __('ns2');
  42. if (!empty($errors[0])) {
  43. foreach ($errors as $i => $error) {
  44. if ( $i == 0 ) {
  45. $error_msg = $error;
  46. } else {
  47. $error_msg = $error_msg.", ".$error;
  48. }
  49. }
  50. $_SESSION['error_msg'] = __('Field "%s" can not be blank.',$error_msg);
  51. }
  52. // Protect input
  53. $v_package = escapeshellarg($_POST['v_package']);
  54. $v_web_template = escapeshellarg($_POST['v_web_template']);
  55. $v_backend_template = escapeshellarg($_POST['v_backend_template']);
  56. $v_proxy_template = escapeshellarg($_POST['v_proxy_template']);
  57. $v_dns_template = escapeshellarg($_POST['v_dns_template']);
  58. $v_shell = escapeshellarg($_POST['v_shell']);
  59. $v_web_domains = escapeshellarg($_POST['v_web_domains']);
  60. $v_web_aliases = escapeshellarg($_POST['v_web_aliases']);
  61. $v_dns_domains = escapeshellarg($_POST['v_dns_domains']);
  62. $v_dns_records = escapeshellarg($_POST['v_dns_records']);
  63. $v_mail_domains = escapeshellarg($_POST['v_mail_domains']);
  64. $v_mail_accounts = escapeshellarg($_POST['v_mail_accounts']);
  65. $v_databases = escapeshellarg($_POST['v_databases']);
  66. $v_cron_jobs = escapeshellarg($_POST['v_cron_jobs']);
  67. $v_backups = escapeshellarg($_POST['v_backups']);
  68. $v_disk_quota = escapeshellarg($_POST['v_disk_quota']);
  69. $v_bandwidth = escapeshellarg($_POST['v_bandwidth']);
  70. $v_ns1 = trim($_POST['v_ns1'], '.');
  71. $v_ns2 = trim($_POST['v_ns2'], '.');
  72. $v_ns3 = trim($_POST['v_ns3'], '.');
  73. $v_ns4 = trim($_POST['v_ns4'], '.');
  74. $v_ns5 = trim($_POST['v_ns5'], '.');
  75. $v_ns6 = trim($_POST['v_ns6'], '.');
  76. $v_ns7 = trim($_POST['v_ns7'], '.');
  77. $v_ns8 = trim($_POST['v_ns8'], '.');
  78. $v_ns = $v_ns1.",".$v_ns2;
  79. if (!empty($v_ns3)) $v_ns .= ",".$v_ns3;
  80. if (!empty($v_ns4)) $v_ns .= ",".$v_ns4;
  81. if (!empty($v_ns5)) $v_ns .= ",".$v_ns5;
  82. if (!empty($v_ns6)) $v_ns .= ",".$v_ns6;
  83. if (!empty($v_ns7)) $v_ns .= ",".$v_ns7;
  84. if (!empty($v_ns8)) $v_ns .= ",".$v_ns8;
  85. $v_ns = escapeshellarg($v_ns);
  86. $v_time = escapeshellarg(date('H:i:s'));
  87. $v_date = escapeshellarg(date('Y-m-d'));
  88. // Create temporary dir
  89. if (empty($_SESSION['error_msg'])) {
  90. exec ('mktemp -d', $output, $return_var);
  91. $tmpdir = $output[0];
  92. check_return_code($return_var,$output);
  93. unset($output);
  94. }
  95. // Create package file
  96. if (empty($_SESSION['error_msg'])) {
  97. $pkg = "WEB_TEMPLATE=".$v_web_template."\n";
  98. if (!empty($_SESSION['WEB_BACKEND'])) {
  99. $pkg .= "BACKEND_TEMPLATE=".$v_backend_template."\n";
  100. }
  101. if (!empty($_SESSION['PROXY_SYSTEM'])) {
  102. $pkg .= "PROXY_TEMPLATE=".$v_proxy_template."\n";
  103. }
  104. $pkg .= "DNS_TEMPLATE=".$v_dns_template."\n";
  105. $pkg .= "WEB_DOMAINS=".$v_web_domains."\n";
  106. $pkg .= "WEB_ALIASES=".$v_web_aliases."\n";
  107. $pkg .= "DNS_DOMAINS=".$v_dns_domains."\n";
  108. $pkg .= "DNS_RECORDS=".$v_dns_records."\n";
  109. $pkg .= "MAIL_DOMAINS=".$v_mail_domains."\n";
  110. $pkg .= "MAIL_ACCOUNTS=".$v_mail_accounts."\n";
  111. $pkg .= "DATABASES=".$v_databases."\n";
  112. $pkg .= "CRON_JOBS=".$v_cron_jobs."\n";
  113. $pkg .= "DISK_QUOTA=".$v_disk_quota."\n";
  114. $pkg .= "BANDWIDTH=".$v_bandwidth."\n";
  115. $pkg .= "NS=".$v_ns."\n";
  116. $pkg .= "SHELL=".$v_shell."\n";
  117. $pkg .= "BACKUPS=".$v_backups."\n";
  118. $pkg .= "TIME=".$v_time."\n";
  119. $pkg .= "DATE=".$v_date."\n";
  120. $fp = fopen($tmpdir."/".$_POST['v_package'].".pkg", 'w');
  121. fwrite($fp, $pkg);
  122. fclose($fp);
  123. }
  124. // Add new package
  125. if (empty($_SESSION['error_msg'])) {
  126. exec (VESTA_CMD."v-add-user-package ".$tmpdir." ".$v_package, $output, $return_var);
  127. check_return_code($return_var,$output);
  128. unset($output);
  129. }
  130. // Remove tmpdir
  131. exec ('rm -rf '.$tmpdir, $output, $return_var);
  132. unset($output);
  133. // Flush field values on success
  134. if (empty($_SESSION['error_msg'])) {
  135. $_SESSION['ok_msg'] = __('PACKAGE_CREATED_OK',htmlentities($_POST['v_package']),htmlentities($_POST['v_package']));
  136. unset($v_package);
  137. }
  138. }
  139. // List web temmplates
  140. exec (VESTA_CMD."v-list-web-templates json", $output, $return_var);
  141. $web_templates = json_decode(implode('', $output), true);
  142. unset($output);
  143. // List web templates for backend
  144. if (!empty($_SESSION['WEB_BACKEND'])) {
  145. exec (VESTA_CMD."v-list-web-templates-backend json", $output, $return_var);
  146. $backend_templates = json_decode(implode('', $output), true);
  147. unset($output);
  148. }
  149. // List web templates for proxy
  150. if (!empty($_SESSION['PROXY_SYSTEM'])) {
  151. exec (VESTA_CMD."v-list-web-templates-proxy json", $output, $return_var);
  152. $proxy_templates = json_decode(implode('', $output), true);
  153. unset($output);
  154. }
  155. // List DNS templates
  156. exec (VESTA_CMD."v-list-dns-templates json", $output, $return_var);
  157. $dns_templates = json_decode(implode('', $output), true);
  158. unset($output);
  159. // List system shells
  160. exec (VESTA_CMD."v-list-sys-shells json", $output, $return_var);
  161. $shells = json_decode(implode('', $output), true);
  162. unset($output);
  163. // Set default values
  164. if (empty($v_web_template)) $v_web_template = 'default';
  165. if (empty($v_backend_template)) $v_backend_template = 'default';
  166. if (empty($v_proxy_template)) $v_proxy_template = 'default';
  167. if (empty($v_dns_template)) $v_dns_template = 'default';
  168. if (empty($v_shell)) $v_shell = 'nologin';
  169. if (empty($v_web_domains)) $v_web_domains = "'1'";
  170. if (empty($v_web_aliases)) $v_web_aliases = "'1'";
  171. if (empty($v_dns_domains)) $v_dns_domains = "'1'";
  172. if (empty($v_dns_records)) $v_dns_records = "'1'";
  173. if (empty($v_mail_domains)) $v_mail_domains = "'1'";
  174. if (empty($v_mail_accounts)) $v_mail_accounts = "'1'";
  175. if (empty($v_databases)) $v_databases = "'1'";
  176. if (empty($v_cron_jobs)) $v_cron_jobs = "'1'";
  177. if (empty($v_backups)) $v_backups = "'1'";
  178. if (empty($v_disk_quota)) $v_disk_quota = "'1000'";
  179. if (empty($v_bandwidth)) $v_bandwidth = "'1000'";
  180. if (empty($v_ns1)) $v_ns1 = 'ns1.example.ltd';
  181. if (empty($v_ns2)) $v_ns2 = 'ns2.example.ltd';
  182. // Render page
  183. // render_page($user, $TAB, 'add_package');
  184. $result = array(
  185. 'web_system' => $_SESSION['WEB_SYSTEM'],
  186. 'web_templates' => $web_templates,
  187. 'web_backend' => $_SESSION['WEB_BACKEND'],
  188. 'backend_templates' => $backend_templates,
  189. 'proxy_system' => $_SESSION['PROXY_SYSTEM'],
  190. 'proxy_templates' => $proxy_templates,
  191. 'dns_system' => $_SESSION['DNS_SYSTEM'],
  192. 'dns_templates' => $dns_templates,
  193. 'ssh_access' => $shells,
  194. 'ok_msg' => $_SESSION['ok_msg'],
  195. 'error_msg' => $_SESSION['error_msg']
  196. );
  197. echo json_encode($result);
  198. // Flush session messages
  199. unset($_SESSION['error_msg']);
  200. unset($_SESSION['ok_msg']);