index.php 8.6 KB

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