From 32c32a7e7afa8c473cdb6c73e5edd8c077078f03 Mon Sep 17 00:00:00 2001 From: Johannes Feichtner Date: Wed, 10 Feb 2016 00:21:07 +0100 Subject: [PATCH] Enable multiple standard IPs --- actions/admin/settings/120.system.php | 2 +- admin_customers.php | 24 ++++++++----- admin_ipsandports.php | 8 ++--- .../integrity/class.IntegrityCheck.php | 34 ++++++++++--------- .../admin/domains/formfield.domains_add.php | 2 +- .../function.storeSettingDefaultIp.php | 27 +++++++++++---- 6 files changed, 60 insertions(+), 37 deletions(-) diff --git a/actions/admin/settings/120.system.php b/actions/admin/settings/120.system.php index 7412f569..46faefcf 100644 --- a/actions/admin/settings/120.system.php +++ b/actions/admin/settings/120.system.php @@ -55,7 +55,7 @@ return array( 'settinggroup' => 'system', 'varname' => 'defaultip', 'type' => 'option', - 'option_mode' => 'one', + 'option_mode' => 'multiple', 'option_options_method' => 'getIpPortCombinations', 'default' => '', 'save_method' => 'storeSettingDefaultIp', diff --git a/admin_customers.php b/admin_customers.php index 6f0dcca0..7d2b9448 100644 --- a/admin_customers.php +++ b/admin_customers.php @@ -913,10 +913,13 @@ if ($page == 'customers' $domainid = Database::lastInsertId(); // set ip <-> domain connection - $ins_stmt = Database::prepare(" - INSERT INTO `".TABLE_DOMAINTOIP."` SET `id_domain` = :domainid, `id_ipandports` = :ipid" - ); - Database::pexecute($ins_stmt, array('domainid' => $domainid, 'ipid' => Settings::Get('system.defaultip'))); + $defaultips = explode(',', Settings::Get('system.defaultip')); + foreach ($defaultips as $defaultip) { + $ins_stmt = Database::prepare(" + INSERT INTO `" . TABLE_DOMAINTOIP . "` SET `id_domain` = :domainid, `id_ipandports` = :ipid" + ); + Database::pexecute($ins_stmt, array('domainid' => $domainid, 'ipid' => $defaultip)); + } $upd_stmt = Database::prepare(" UPDATE `" . TABLE_PANEL_CUSTOMERS . "` SET `standardsubdomain` = :domainid WHERE `customerid` = :customerid" @@ -937,7 +940,7 @@ if ($page == 'customers' SELECT ip, port FROM `".TABLE_PANEL_IPSANDPORTS."` WHERE `id` = :defaultip "); - $srv_ip = Database::pexecute_first($srv_ip_stmt, array('defaultip' => Settings::Get('system.defaultip'))); + $srv_ip = Database::pexecute_first($srv_ip_stmt, array('defaultip' => reset(explode(',', Settings::Get('system.defaultip'))))); $replace_arr = array( 'FIRSTNAME' => $firstname, @@ -1272,10 +1275,13 @@ if ($page == 'customers' $domainid = Database::lastInsertId(); // set ip <-> domain connection - $ins_stmt = Database::prepare(" - INSERT INTO `".TABLE_DOMAINTOIP."` SET `id_domain` = :domainid, `id_ipandports` = :ipid" - ); - Database::pexecute($ins_stmt, array('domainid' => $domainid, 'ipid' => Settings::Get('system.defaultip'))); + $defaultips = explode(',', Settings::Get('system.defaultip')); + foreach ($defaultips as $defaultip) { + $ins_stmt = Database::prepare(" + INSERT INTO `" . TABLE_DOMAINTOIP . "` SET `id_domain` = :domainid, `id_ipandports` = :ipid" + ); + Database::pexecute($ins_stmt, array('domainid' => $domainid, 'ipid' => $defaultip)); + } $upd_stmt = Database::prepare(" UPDATE `" . TABLE_PANEL_CUSTOMERS . "` SET `standardsubdomain` = :domainid WHERE `customerid` = :customerid" diff --git a/admin_ipsandports.php b/admin_ipsandports.php index 4e5cda9d..f03f6635 100644 --- a/admin_ipsandports.php +++ b/admin_ipsandports.php @@ -83,7 +83,7 @@ if ($page == 'ipsandports' $result_checkdomain = Database::pexecute_first($result_checkdomain_stmt, array('id' => $id)); if ($result_checkdomain['id'] == '') { - if ($result['id'] != Settings::Get('system.defaultip')) { + if (!in_array($result['id'], explode(',', Settings::Get('system.defaultip')))) { $result_sameipotherport_stmt = Database::prepare(" SELECT `id` FROM `" . TABLE_PANEL_IPSANDPORTS . "` @@ -324,7 +324,7 @@ if ($page == 'ipsandports' $ssl_ca_file = ''; $ssl_cert_chainfile = ''; } - + if ($listen_statement != '1') { $listen_statement = '0'; } @@ -344,7 +344,7 @@ if ($page == 'ipsandports' if ($ssl != '1') { $ssl = '0'; } - + if ($ssl_cert_file != '') { $ssl_cert_file = makeCorrectFile($ssl_cert_file); } @@ -426,7 +426,7 @@ if ($page == 'ipsandports' $ipsandports_edit_data = include_once dirname(__FILE__).'/lib/formfields/admin/ipsandports/formfield.ipsandports_edit.php'; $ipsandports_edit_form = htmlform::genHTMLForm($ipsandports_edit_data); - + $title = $ipsandports_edit_data['ipsandports_edit']['title']; $image = $ipsandports_edit_data['ipsandports_edit']['image']; diff --git a/lib/classes/integrity/class.IntegrityCheck.php b/lib/classes/integrity/class.IntegrityCheck.php index 7ab536a1..c7fea0f0 100644 --- a/lib/classes/integrity/class.IntegrityCheck.php +++ b/lib/classes/integrity/class.IntegrityCheck.php @@ -15,7 +15,7 @@ * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt * @package Integrity * - * IntegrityCheck - class + * IntegrityCheck - class */ class IntegrityCheck { @@ -28,7 +28,7 @@ class IntegrityCheck { /** * Constructor - * Parses all available checks into $this->available + * Parses all available checks into $this->available */ public function __construct() { global $userinfo; @@ -41,7 +41,7 @@ class IntegrityCheck { unset($this->available[array_search('checkAll', $this->available)]); unset($this->available[array_search('fixAll', $this->available)]); sort($this->available); - + } /** @@ -130,9 +130,9 @@ class IntegrityCheck { while ($row = $adm_stmt->fetch(PDO::FETCH_ASSOC)) { if ($row['ip'] < 0 || is_null($row['ip']) || empty($row['ip'])) { // Admin uses default-IP - $admips[$row['adminid']] = Settings::Get('system.defaultip'); + $admips[$row['adminid']] = explode(',', Settings::Get('system.defaultip')); } else { - $admips[$row['adminid']] = $row['ip']; + $admips[$row['adminid']] = [ $row['ip'] ]; } } } @@ -143,19 +143,19 @@ class IntegrityCheck { while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { $ips[$row['id']] = $row['ip'] . ':' . $row['port']; } - + // Cache all configured domains $result_stmt = Database::prepare("SELECT `id`, `adminid` FROM `" . TABLE_PANEL_DOMAINS . "` ORDER BY `id` ASC"); Database::pexecute($result_stmt); while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { $domains[$row['id']] = $row['adminid']; } - + // Check if every domain to ip/port - association is valid in TABLE_DOMAINTOIP $result_stmt = Database::prepare("SELECT `id_domain`, `id_ipandports` FROM `" . TABLE_DOMAINTOIP . "`"); Database::pexecute($result_stmt); while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { - if (!array_key_exists($row['id_ipandports'], $ips)) { + if (!array_key_exists($row['id_ipandports'], $ips)) { if ($fix) { Database::pexecute($del_stmt, array('domainid' => $row['id_domain'], 'ipandportid' => $row['id_ipandports'])); $this->_log->logAction(ADM_ACTION, LOG_WARNING, "found an ip/port-id in domain <> ip table which does not exist, integrity check fixed this"); @@ -170,18 +170,20 @@ class IntegrityCheck { $this->_log->logAction(ADM_ACTION, LOG_WARNING, "found a domain-id in domain <> ip table which does not exist, integrity check fixed this"); } else { $this->_log->logAction(ADM_ACTION, LOG_NOTICE, "found a domain-id in domain <> ip table which does not exist, integrity check can fix this"); - return false; + return false; } } // Save one IP/Port combination per domain, so we know, if one domain is missing an IP $ipstodomains[$row['id_domain']] = $row['id_ipandports']; } - + // Check that all domains have at least one IP/Port combination foreach ($domains as $domainid => $adminid) { if (!array_key_exists($domainid, $ipstodomains)) { if ($fix) { - Database::pexecute($ins_stmt, array('domainid' => $domainid, 'ipandportid' => $admips[$adminid])); + foreach ($admips[$adminid] as $defaultip) { + Database::pexecute($ins_stmt, array('domainid' => $domainid, 'ipandportid' => $defaultip)); + } $this->_log->logAction(ADM_ACTION, LOG_WARNING, "found a domain-id with no entry in domain <> ip table, integrity check fixed this"); } else { $this->_log->logAction(ADM_ACTION, LOG_NOTICE, "found a domain-id with no entry in domain <> ip table, integrity check can fix this"); @@ -220,7 +222,7 @@ class IntegrityCheck { while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { $ips[$row['id']] = $row['ip'] . ':' . $row['port']; } - + // Cache all configured domains $result_stmt = Database::prepare("SELECT `id`, `parentdomainid`, `ssl_redirect` FROM `" . TABLE_PANEL_DOMAINS . "` ORDER BY `id` ASC"); $ip_stmt = Database::prepare("SELECT `id_domain`, `id_ipandports` FROM `" . TABLE_DOMAINTOIP . "` WHERE `id_domain` = :domainid"); @@ -232,7 +234,7 @@ class IntegrityCheck { Database::pexecute($ip_stmt, array('domainid' => $row['id'])); while ($iprow = $ip_stmt->fetch(PDO::FETCH_ASSOC)) { // If the parentdomain has an ip/port assigned which we know is SSL enabled, set the parentdomain to "true" - if (array_key_exists($iprow['id_ipandports'], $ips)) { $parentdomains[$row['id']] = true; } + if (array_key_exists($iprow['id_ipandports'], $ips)) { $parentdomains[$row['id']] = true; } } } elseif ($row['ssl_redirect'] == 1) { // All subdomains with enabled ssl_redirect enabled are stored @@ -240,14 +242,14 @@ class IntegrityCheck { $subdomains[$row['parentdomainid']][] = $row['id']; } } - + // Check if every parentdomain with enabled ssl_redirect as SSL enabled foreach ($parentdomains as $id => $sslavailable) { // This parentdomain has no subdomains if (!isset($subdomains[$id])) { continue; } // This parentdomain has SSL enabled, doesn't matter what status the subdomains have if ($sslavailable) { continue; } - + // At this point only parentdomains reside which have ssl_redirect enabled subdomains if ($fix) { // We make a blanket update to all subdomains of this parentdomain, doesn't matter which one is wrong, all have to be disabled @@ -259,7 +261,7 @@ class IntegrityCheck { return false; } } - + if ($fix) { return $this->SubdomainSslRedirect(); } else { diff --git a/lib/formfields/admin/domains/formfield.domains_add.php b/lib/formfields/admin/domains/formfield.domains_add.php index 46a04dc5..94876c1b 100644 --- a/lib/formfields/admin/domains/formfield.domains_add.php +++ b/lib/formfields/admin/domains/formfield.domains_add.php @@ -97,7 +97,7 @@ return array( 'desc' => $lng['domains']['ipandport_multi']['description'], 'type' => 'checkbox', 'values' => $ipsandports, - 'value' => array(Settings::Get('system.defaultip')), + 'value' => explode(',', Settings::Get('system.defaultip')), 'is_array' => 1, 'mandatory' => true ), diff --git a/lib/functions/settings/function.storeSettingDefaultIp.php b/lib/functions/settings/function.storeSettingDefaultIp.php index 01aa5c8a..1d527f48 100644 --- a/lib/functions/settings/function.storeSettingDefaultIp.php +++ b/lib/functions/settings/function.storeSettingDefaultIp.php @@ -17,6 +17,7 @@ * */ function storeSettingDefaultIp($fieldname, $fielddata, $newfieldvalue) { + $defaultips_old = Settings::Get('system.defaultip'); $returnvalue = storeSettingField($fieldname, $fielddata, $newfieldvalue); @@ -40,13 +41,27 @@ function storeSettingDefaultIp($fieldname, $fielddata, $newfieldvalue) { } if (count($ids) > 0) { - $upd_stmt = Database::prepare(" - UPDATE `" . TABLE_DOMAINTOIP . "` SET - `id_ipandports` = :newval - WHERE `id_domain` IN ('" . implode(', ', $ids) . "') - AND `id_ipandports` = :defaultip + $defaultips_new = explode(',', $newfieldvalue); + + // Delete the existing mappings linking to default IPs + $del_stmt = Database::prepare(" + DELETE FROM `" . TABLE_DOMAINTOIP . "` + WHERE `id_domain` IN (" . implode(', ', $ids) . ") + AND `id_ipandports` IN (" . $defaultips_old . ") "); - Database::pexecute($upd_stmt, array('newval' => $newfieldvalue, 'defaultip' => Settings::Get('system.defaultip'))); + Database::pexecute($del_stmt); + + // Insert the new mappings + $ins_stmt = Database::prepare(" + INSERT INTO `" . TABLE_DOMAINTOIP . "` + SET `id_domain` = :domainid, `id_ipandports` = :ipandportid + "); + + foreach ($ids as $id) { + foreach ($defaultips_new as $defaultip_new) { + Database::pexecute($ins_stmt, array('domainid' => $id, 'ipandportid' => $defaultip_new)); + } + } } }