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