index.php 8.0 KB

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