index.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. // Init
  3. error_reporting(NULL);
  4. ob_start();
  5. session_start();
  6. $TAB = 'PACKAGE';
  7. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  8. if (empty($_SESSION['user'])) {
  9. header("Location: /login/");
  10. }
  11. // Header
  12. include($_SERVER['DOCUMENT_ROOT'].'/templates/header.html');
  13. // Panel
  14. top_panel($user,$TAB);
  15. // Are you admin?
  16. if ($_SESSION['user'] == 'admin') {
  17. if (!empty($_POST['ok'])) {
  18. // Check input
  19. if (empty($_POST['v_package'])) $errors[] = 'package';
  20. if (empty($_POST['v_template'])) $errors[] = 'template';
  21. if (empty($_POST['v_shell'])) $errrors[] = 'shell';
  22. if (!isset($_POST['v_web_domains'])) $errors[] = 'web domains';
  23. if (!isset($_POST['v_web_aliases'])) $errors[] = 'web aliases';
  24. if (!isset($_POST['v_dns_domains'])) $errors[] = 'dns domains';
  25. if (!isset($_POST['v_dns_records'])) $errors[] = 'dns records';
  26. if (!isset($_POST['v_mail_domains'])) $errors[] = 'mail domains';
  27. if (!isset($_POST['v_mail_accounts'])) $errors[] = 'mail accounts';
  28. if (!isset($_POST['v_databases'])) $errors[] = 'databases';
  29. if (!isset($_POST['v_cron_jobs'])) $errors[] = 'cron jobs';
  30. if (!isset($_POST['v_backups'])) $errors[] = 'backups';
  31. if (!isset($_POST['v_disk_quota'])) $errors[] = 'quota';
  32. if (!isset($_POST['v_bandwidth'])) $errors[] = 'bandwidth';
  33. if (empty($_POST['v_ns1'])) $errors[] = 'ns1';
  34. if (empty($_POST['v_ns2'])) $errors[] = 'ns2';
  35. // Protect input
  36. $v_package = escapeshellarg($_POST['v_package']);
  37. $v_template = escapeshellarg($_POST['v_template']);
  38. $v_shell = escapeshellarg($_POST['v_shell']);
  39. $v_web_domains = escapeshellarg($_POST['v_web_domains']);
  40. $v_web_aliases = escapeshellarg($_POST['v_web_aliases']);
  41. $v_dns_domains = escapeshellarg($_POST['v_dns_domains']);
  42. $v_dns_records = escapeshellarg($_POST['v_dns_records']);
  43. $v_mail_domains = escapeshellarg($_POST['v_mail_domains']);
  44. $v_mail_accounts = escapeshellarg($_POST['v_mail_accounts']);
  45. $v_databases = escapeshellarg($_POST['v_databases']);
  46. $v_cron_jobs = escapeshellarg($_POST['v_cron_jobs']);
  47. $v_backups = escapeshellarg($_POST['v_backups']);
  48. $v_disk_quota = escapeshellarg($_POST['v_disk_quota']);
  49. $v_bandwidth = escapeshellarg($_POST['v_bandwidth']);
  50. $v_ns1 = trim($_POST['v_ns1'], '.');
  51. $v_ns2 = trim($_POST['v_ns2'], '.');
  52. $v_ns3 = trim($_POST['v_ns3'], '.');
  53. $v_ns4 = trim($_POST['v_ns4'], '.');
  54. $v_ns = $v_ns1.",".$v_ns2;
  55. if (!empty($v_ns3)) $v_ns .= ",".$v_ns3;
  56. if (!empty($v_ns4)) $v_ns .= ",".$v_ns4;
  57. $v_ns = escapeshellarg($v_ns);
  58. $v_time = escapeshellarg(date('H:i:s'));
  59. $v_date = escapeshellarg(date('Y-m-d'));
  60. // Check for errors
  61. if (!empty($errors[0])) {
  62. foreach ($errors as $i => $error) {
  63. if ( $i == 0 ) {
  64. $error_msg = $error;
  65. } else {
  66. $error_msg = $error_msg.", ".$error;
  67. }
  68. }
  69. $_SESSION['error_msg'] = "Error: field ".$error_msg." can not be blank.";
  70. } else {
  71. exec ('mktemp -d', $output, $return_var);
  72. $tmpdir = $output[0];
  73. unset($output);
  74. // Create package
  75. $pkg = "TEMPLATE=".$v_template."\n";
  76. $pkg .= "WEB_DOMAINS=".$v_web_domains."\n";
  77. $pkg .= "WEB_ALIASES=".$v_web_aliases."\n";
  78. $pkg .= "DNS_DOMAINS=".$v_dns_domains."\n";
  79. $pkg .= "DNS_RECORDS=".$v_dns_records."\n";
  80. $pkg .= "MAIL_DOMAINS=".$v_mail_domains."\n";
  81. $pkg .= "MAIL_ACCOUNTS=".$v_mail_accounts."\n";
  82. $pkg .= "DATABASES=".$v_databases."\n";
  83. $pkg .= "CRON_JOBS=".$v_cron_jobs."\n";
  84. $pkg .= "DISK_QUOTA=".$v_disk_quota."\n";
  85. $pkg .= "BANDWIDTH=".$v_bandwidth."\n";
  86. $pkg .= "NS=".$v_ns."\n";
  87. $pkg .= "SHELL=".$v_shell."\n";
  88. $pkg .= "BACKUPS=".$v_backups."\n";
  89. $pkg .= "TIME=".$v_time."\n";
  90. $pkg .= "DATE=".$v_date."\n";
  91. // Write package
  92. $fp = fopen($tmpdir."/".$_POST['v_package'].".pkg", 'w');
  93. fwrite($fp, $pkg);
  94. fclose($fp);
  95. // Add new package
  96. if (empty($_SESSION['error_msg'])) {
  97. exec (VESTA_CMD."v-add-user-package ".$tmpdir." ".$v_package, $output, $return_var);
  98. if ($return_var != 0) {
  99. $error = implode('<br>', $output);
  100. if (empty($error)) $error = 'Error: vesta did not return any output.';
  101. $_SESSION['error_msg'] = $error;
  102. }
  103. unset($output);
  104. }
  105. // Remove tmpdir
  106. exec ('rm -rf '.$tmpdir, $output, $return_var);
  107. unset($output);
  108. // Check output
  109. if (empty($_SESSION['error_msg'])) {
  110. $_SESSION['ok_msg'] = "OK: package <a href='/edit/package/?package=".$_POST['v_package']."'><b>".$_POST['v_package']."</b></a> has been created successfully.";
  111. unset($v_package);
  112. }
  113. }
  114. }
  115. exec (VESTA_CMD."v-list-web-templates json", $output, $return_var);
  116. check_error($return_var);
  117. $templates = json_decode(implode('', $output), true);
  118. unset($output);
  119. exec (VESTA_CMD."v-list-sys-shells json", $output, $return_var);
  120. check_error($return_var);
  121. $shells = json_decode(implode('', $output), true);
  122. unset($output);
  123. // Set default values
  124. if (empty($v_template)) $v_template = 'default';
  125. if (empty($v_shell)) $v_shell = 'nologin';
  126. if (empty($v_web_domains)) $v_web_domains = "'0'";
  127. if (empty($v_web_aliases)) $v_web_aliases = "'0'";
  128. if (empty($v_dns_domains)) $v_dns_domains = "'0'";
  129. if (empty($v_dns_records)) $v_dns_records = "'0'";
  130. if (empty($v_mail_domains)) $v_mail_domains = "'0'";
  131. if (empty($v_mail_accounts)) $v_mail_accounts = "'0'";
  132. if (empty($v_databases)) $v_databases = "'0'";
  133. if (empty($v_cron_jobs)) $v_cron_jobs = "'0'";
  134. if (empty($v_backups)) $v_backups = "'0'";
  135. if (empty($v_disk_quota)) $v_disk_quota = "'0'";
  136. if (empty($v_bandwidth)) $v_bandwidth = "'0'";
  137. if (empty($v_ns1)) $v_ns1 = 'ns1.example.ltd';
  138. if (empty($v_ns2)) $v_ns2 = 'ns2.example.ltd';
  139. include($_SERVER['DOCUMENT_ROOT'].'/templates/admin/add_package.html');
  140. unset($_SESSION['error_msg']);
  141. unset($_SESSION['ok_msg']);
  142. }
  143. // Footer
  144. include($_SERVER['DOCUMENT_ROOT'].'/templates/footer.html');