Browse Source

discovered ban_suggestions[ips] + other fixes (testing pays!)

Signed-off-by: emanuele <[email protected]>
emanuele 13 years ago
parent
commit
a5d65994de
3 changed files with 145 additions and 94 deletions
  1. 120 69
      Sources/ManageBans.php
  2. 17 17
      Themes/default/ManageBans.template.php
  3. 8 8
      Themes/default/Wireless.template.php

+ 120 - 69
Sources/ManageBans.php

@@ -378,27 +378,6 @@ function BanEdit()
 	{
 		foreach ($context['ban_errors'] as $error)
 			$context['error_messages'][$error] = $txt[$error];
-/*
-		$context['ban']['expiration'] = array(
-				'status' => 'never',
-				'days' => 0
-		);
-		$context['ban']['ban_days'] = 0;
-		$context['ban']['cannot'] = array(
-				'access' => true,
-				'post' => false,
-				'register' => false,
-				'login' => false,
-		);*/
-/*
-		$context['ban_suggestions'] = array(
-			'main_ip' => '',
-			'hostname' => '',
-			'email' => '',
-			'member' => array(
-				'id' => 0,
-			),
-		);*/
 	}
 	else
 	{
@@ -547,47 +526,14 @@ function BanEdit()
 
 					// Default the ban name to the name of the banned member.
 					$context['ban']['name'] = $context['ban_suggestions']['member']['name'];
-					$context['ban']['from_user'] = true; // @todo: there should be a better solution
+					// @todo: there should be a better solution...used to lock the "Ban on Username" input when banning from profile
+					$context['ban']['from_user'] = true;
 
 					// Would be nice if we could also ban the hostname.
-					if (preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $context['ban_suggestions']['main_ip']) == 1 && empty($modSettings['disableHostnameLookup']))
+					if ((preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $context['ban_suggestions']['main_ip']) == 1 || isValidIPv6($context['ban_suggestions']['main_ip'])) && empty($modSettings['disableHostnameLookup']))
 						$context['ban_suggestions']['hostname'] = host_from_ip($context['ban_suggestions']['main_ip']);
 
-					// Find some additional IP's used by this member.
-					$context['ban_suggestions']['message_ips'] = array();
-					$request = $smcFunc['db_query']('ban_suggest_message_ips', '
-						SELECT DISTINCT poster_ip
-						FROM {db_prefix}messages
-						WHERE id_member = {int:current_user}
-							AND poster_ip RLIKE {string:poster_ip_regex}
-						ORDER BY poster_ip',
-						array(
-							'current_user' => (int) $_REQUEST['u'],
-							'poster_ip_regex' => '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$',
-						)
-					);
-					while ($row = $smcFunc['db_fetch_assoc']($request))
-						$context['ban_suggestions']['message_ips'][] = $row['poster_ip'];
-					$smcFunc['db_free_result']($request);
-
-					$context['ban_suggestions']['error_ips'] = array();
-					$request = $smcFunc['db_query']('ban_suggest_error_ips', '
-						SELECT DISTINCT ip
-						FROM {db_prefix}log_errors
-						WHERE id_member = {int:current_user}
-							AND ip RLIKE {string:poster_ip_regex}
-						ORDER BY ip',
-						array(
-							'current_user' => (int) $_REQUEST['u'],
-							'poster_ip_regex' => '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$',
-						)
-					);
-					while ($row = $smcFunc['db_fetch_assoc']($request))
-						$context['ban_suggestions']['error_ips'][] = $row['ip'];
-					$smcFunc['db_free_result']($request);
-
-					// Borrowing a few language strings from profile.
-					loadLanguage('Profile');
+					banLoadAdditionalIPs();
 				}
 			}
 		}
@@ -607,6 +553,47 @@ function BanEdit()
 	createToken('admin-bet');
 }
 
+function banLoadAdditionalIPs()
+{
+	global $context, $smcFunc;
+
+	// Borrowing a few language strings from profile.
+	loadLanguage('Profile');
+
+	// Find some additional IP's used by this member.
+	$context['ban_suggestions']['message_ips'] = array();
+	$request = $smcFunc['db_query']('ban_suggest_message_ips', '
+		SELECT DISTINCT poster_ip
+		FROM {db_prefix}messages
+		WHERE id_member = {int:current_user}
+			AND poster_ip RLIKE {string:poster_ip_regex}
+		ORDER BY poster_ip',
+		array(
+			'current_user' => $context['ban_suggestions']['member']['id'],
+			'poster_ip_regex' => '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$',
+		)
+	);
+	while ($row = $smcFunc['db_fetch_assoc']($request))
+		$context['ban_suggestions']['message_ips'][] = $row['poster_ip'];
+	$smcFunc['db_free_result']($request);
+
+	$context['ban_suggestions']['error_ips'] = array();
+	$request = $smcFunc['db_query']('ban_suggest_error_ips', '
+		SELECT DISTINCT ip
+		FROM {db_prefix}log_errors
+		WHERE id_member = {int:current_user}
+			AND ip RLIKE {string:poster_ip_regex}
+		ORDER BY ip',
+		array(
+			'current_user' => $context['ban_suggestions']['member']['id'],
+			'poster_ip_regex' => '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$',
+		)
+	);
+	while ($row = $smcFunc['db_fetch_assoc']($request))
+		$context['ban_suggestions']['error_ips'][] = $row['ip'];
+	$smcFunc['db_free_result']($request);
+}
+
 function banEdit2()
 {
 	global $smcFunc, $context;
@@ -661,13 +648,18 @@ function banEdit2()
 			'hostname' => '',
 			'email' => '',
 			'member' => array(
-				'id' => 0,
-			),
+				'id' => isset($_REQUEST['u']) ? (int) $_REQUEST['u'] : 0,
+			)
 		);
 		$ban_triggers = array();
 
 		foreach ($_POST['ban_suggestions'] as $key => $value)
-			$triggers[$value] = !empty($_POST[$value]) ? $_POST[$value] : '';
+		{
+			if (is_array($value))
+				$triggers[$key] = $value;
+			else
+				$triggers[$value] = !empty($_POST[$value]) ? $_POST[$value] : '';
+		}
 
 		$ban_triggers = validateTriggers($triggers);
 
@@ -679,7 +671,7 @@ function banEdit2()
 			else
 				updateTriggers((int) $_REQUEST['bi'], $ban_info['id'], $ban_triggers['ban_triggers'][0], $ban_triggers['log_info'][0]);
 		}
-		elseif (!empty($ban_triggers['ban_triggers']) && !empty($context['ban_errors']))
+		if (!empty($context['ban_errors']))
 		{
 			$context['ban_suggestions'] = $triggers;
 		}
@@ -687,7 +679,12 @@ function banEdit2()
 
 	// Something went wrong somewhere... Oh well, let's go back.
 	if (!empty($context['ban_errors']))
+	{
+		// Not strictly necessary, but it's nice
+		if (!empty($context['ban_suggestions']['member']['id']))
+			banLoadAdditionalIPs();
 		return BanEdit();
+	}
 
 	if (isset($_POST['ban_items']))
 	{
@@ -848,6 +845,48 @@ function validateTriggers(&$triggers)
 				else
 					$ban_triggers['user']['id_member'] = $value;
 			}
+			elseif ($key == 'ips_m' || $key == 'ips_e')
+			{
+				// Special case, those two are arrays themselves
+				$values = array_unique($value);
+				// Don't add the main IP again.
+				if (isset($triggers['main_ip']))
+					$values = array_diff($values, array($triggers['main_ip']));
+				unset($value);
+				foreach ($values as $val)
+				{
+					$val = trim($val);
+					$ip_parts = ip2range($val);
+					if (!checkExistingTriggerIP($ip_parts, $val))
+						$context['ban_erros'][] = 'invalid_ip';
+					else
+					{
+						$ban_triggers[$key][] = array(
+							'ip_low1' => $ip_parts[0]['low'],
+							'ip_high1' => $ip_parts[0]['high'],
+							'ip_low2' => $ip_parts[1]['low'],
+							'ip_high2' => $ip_parts[1]['high'],
+							'ip_low3' => $ip_parts[2]['low'],
+							'ip_high3' => $ip_parts[2]['high'],
+							'ip_low4' => $ip_parts[3]['low'],
+							'ip_high4' => $ip_parts[3]['high'],
+							'ip_low5' => $ip_parts[4]['low'],
+							'ip_high5' => $ip_parts[4]['high'],
+							'ip_low6' => $ip_parts[5]['low'],
+							'ip_high6' => $ip_parts[5]['high'],
+							'ip_low7' => $ip_parts[6]['low'],
+							'ip_high7' => $ip_parts[6]['high'],
+							'ip_low8' => $ip_parts[7]['low'],
+							'ip_high8' => $ip_parts[7]['high'],
+						);
+
+						$log_info[] = array(
+							'value' => $val,
+							'bantype' => 'ip_range',
+						);
+					}
+				}
+			}
 			else
 				$context['ban_erros'][] = 'no_bantype_selected';
 
@@ -877,17 +916,13 @@ function addTriggers($group_id = 0, $triggers = array(), $logs = array())
 
 	if (empty($group_id))
 		$context['ban_errors'][] = 'ban_group_id_empty';
-	if (empty($triggers))
-		$context['ban_errors'][] = 'ban_no_triggers';
-
-	if (!empty($context['ban_errors']))
-		return;
 
 	$logCorrel = array(
 		'main_ip' => 'ip_range',
 		'hostname' => 'email',
 		'email' => 'email',
 		'user' => 'member',
+		'ip_range' => 'ip_range',
 	);
 
 	// Preset all values that are required.
@@ -937,8 +972,22 @@ function addTriggers($group_id = 0, $triggers = array(), $logs = array())
 		'ip_high8' => 'int',
 	);
 
-	foreach ($triggers as $key => &$trigger)
-		$trigger = array_merge($values, $trigger);
+	$ins_triggers = array();
+	foreach ($triggers as $key => $trigger)
+	{
+		// Exceptions, exceptions, exceptions...always exceptions... :P
+		if (is_array($trigger))
+			foreach ($trigger as $real_trigger)
+				$ins_triggers[$key] = array_merge($values, $trigger);
+		else
+			$ins_triggers[$key] = array_merge($values, $trigger);
+	}
+
+	if (empty($triggers))
+		$context['ban_errors'][] = 'ban_no_triggers';
+
+	if (!empty($context['ban_errors']))
+		return false;
 
 	$smcFunc['db_insert']('',
 		'{db_prefix}ban_items',
@@ -954,6 +1003,8 @@ function addTriggers($group_id = 0, $triggers = array(), $logs = array())
 			'new' => 1,
 			'type' => $log['bantype'],
 		));
+
+	return true;
 }
 
 /**

+ 17 - 17
Themes/default/ManageBans.template.php

@@ -46,7 +46,7 @@ function template_ban_edit()
 
 	echo '
 			<div class="content">
-				<form action="', $scripturl, '?action=admin;area=ban;sa=edit" method="post" accept-charset="', $context['character_set'], '" onsubmit="if (this.ban_name.value == \'\') {alert(\'', $txt['ban_name_empty'], '\'); return false;} if (this.partial_ban.checked &amp;&amp; !(this.cannot_post.checked || this.cannot_register.checked || this.cannot_login.checked)) {alert(\'', $txt['ban_restriction_empty'], '\'); return false;}">
+				<form action="', $scripturl, '?action=admin;area=ban;sa=edit', !empty($context['ban_suggestions']['member']['id']) ? ';u=' . $context['ban_suggestions']['member']['id'] : '', '" method="post" accept-charset="', $context['character_set'], '" onsubmit="if (this.ban_name.value == \'\') {alert(\'', $txt['ban_name_empty'], '\'); return false;} if (this.partial_ban.checked &amp;&amp; !(this.cannot_post.checked || this.cannot_register.checked || this.cannot_login.checked)) {alert(\'', $txt['ban_restriction_empty'], '\'); return false;}">
 					<dl class="settings">
 						<dt id="ban_name_label">
 							<strong>', $txt['ban_name'], ':</strong>
@@ -100,7 +100,7 @@ function template_ban_edit()
 						</legend>
 						<dl class="settings">
 							<dt>
-								<input type="checkbox" name="ban_suggestions[]" id="main_ip_check" value="main_ip" class="input_check" ', !empty($context['ban_suggestions']['main_ip']) ? 'checked="checked ' : '', '/>
+								<input type="checkbox" name="ban_suggestions[]" id="main_ip_check" value="main_ip" class="input_check" ', !empty($context['ban_suggestions']['main_ip']) ? 'checked="checked" ' : '', '/>
 								<label for="main_ip_check">', $txt['ban_on_ip'], '</label>
 							</dt>
 							<dd>
@@ -110,7 +110,7 @@ function template_ban_edit()
 		if (empty($modSettings['disableHostnameLookup']))
 			echo '
 							<dt>
-								<input type="checkbox" name="ban_suggestions[]" id="hostname_check" value="hostname" class="input_check" ', !empty($context['ban_suggestions']['hostname']) ? 'checked="checked ' : '', '/>
+								<input type="checkbox" name="ban_suggestions[]" id="hostname_check" value="hostname" class="input_check" ', !empty($context['ban_suggestions']['hostname']) ? 'checked="checked" ' : '', '/>
 								<label for="hostname_check">', $txt['ban_on_hostname'], '</label>
 							</dt>
 							<dd>
@@ -119,14 +119,14 @@ function template_ban_edit()
 
 		echo '
 							<dt>
-								<input type="checkbox" name="ban_suggestions[]" id="email_check" value="email" class="input_check" ', !empty($context['ban_suggestions']['email']) ? 'checked="checked ' : '', '/>
+								<input type="checkbox" name="ban_suggestions[]" id="email_check" value="email" class="input_check" ', !empty($context['ban_suggestions']['email']) ? 'checked="checked" ' : '', '/>
 								<label for="email_check">', $txt['ban_on_email'], '</label>
 							</dt>
 							<dd>
 								<input type="text" name="email" value="', $context['ban_suggestions']['email'], '" size="44" onfocus="document.getElementById(\'email_check\').checked = true;" class="input_text" />
 							</dd>
 							<dt>
-								<input type="checkbox" name="ban_suggestions[]" id="user_check" value="user" class="input_check" ', !empty($context['ban_suggestions']['user']) ? 'checked="checked' : '', '/>
+								<input type="checkbox" name="ban_suggestions[]" id="user_check" value="user" class="input_check" ', !empty($context['ban_suggestions']['user']) ? 'checked="checked" ' : '', '/>
 								<label for="user_check">', $txt['ban_on_username'], '</label>:
 							</dt>
 							<dd>
@@ -142,12 +142,12 @@ function template_ban_edit()
 
 			foreach ($context['ban_suggestions']['message_ips'] as $ip)
 				echo '
-						<dt>
-							<input type="checkbox" name="ban_suggestion[ips][]" value="', $ip, '" class="input_check" />
-						</dt>
-						<dd>
-							', $ip, '
-						</dd>';
+							<dt>
+								<input type="checkbox" name="ban_suggestions[ips_m][]" ', !empty($context['ban_suggestions']['ips_m']) && in_array($ip, $context['ban_suggestions']['ips_m']) ? 'checked="checked" ' : '', 'value="', $ip, '" class="input_check" />
+							</dt>
+							<dd>
+								', $ip, '
+							</dd>';
 		}
 
 		if (!empty($context['ban_suggestions']['error_ips']))
@@ -159,12 +159,12 @@ function template_ban_edit()
 
 			foreach ($context['ban_suggestions']['error_ips'] as $ip)
 				echo '
-						<dt>
-							<input type="checkbox" name="ban_suggestion[ips][]" value="', $ip, '" class="input_check" />
-						</dt>
-						<dd>
-							', $ip, '
-						</dd>';
+							<dt>
+								<input type="checkbox" name="ban_suggestions[ips_e][]" ', !empty($context['ban_suggestions']['ips_e']) && in_array($ip, $context['ban_suggestions']['ips_e']) ? 'checked="checked" ' : '', 'value="', $ip, '" class="input_check" />
+							</dt>
+							<dd>
+								', $ip, '
+							</dd>';
 		}
 
 		echo '

+ 8 - 8
Themes/default/Wireless.template.php

@@ -832,24 +832,24 @@ function template_imode_ban_edit()
 		echo '
 			<tr bgcolor="#b6dbff"><td>', $txt['ban_triggers'], '</td></tr>
 			<tr><td>
-				<input type="checkbox" name="ban_suggestion[]" value="main_ip" class="input_check" /> <strong>', $txt['wireless_ban_ip'], ':</strong><br />
+				<input type="checkbox" name="ban_suggestions[]" value="main_ip" class="input_check" /> <strong>', $txt['wireless_ban_ip'], ':</strong><br />
 				&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="main_ip" value="', $context['ban_suggestions']['main_ip'], '" size="20" class="input_text" />
 			</td></tr>';
 
 		if (empty($modSettings['disableHostnameLookup']))
 			echo '
 			<tr><td>
-				<input type="checkbox" name="ban_suggestion[]" value="hostname" class="input_check" /> <strong>', $txt['wireless_ban_hostname'], ':</strong><br />
+				<input type="checkbox" name="ban_suggestions[]" value="hostname" class="input_check" /> <strong>', $txt['wireless_ban_hostname'], ':</strong><br />
 				&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="hostname" value="', $context['ban_suggestions']['hostname'], '" size="20" class="input_text" />
 			</td></tr>';
 
 		echo '
 			<tr><td>
-				<input type="checkbox" name="ban_suggestion[]" value="email" class="input_check" /> <strong>', $txt['wireless_ban_email'], ':</strong><br />
+				<input type="checkbox" name="ban_suggestions[]" value="email" class="input_check" /> <strong>', $txt['wireless_ban_email'], ':</strong><br />
 				&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="email" value="', $context['ban_suggestions']['email'], '" size="20" class="input_text" />
 			</td></tr>
 			<tr><td>
-				<input type="checkbox" name="ban_suggestion[]" value="user" class="input_check" /> <strong>', $txt['ban_on_username'], ':</strong><br />';
+				<input type="checkbox" name="ban_suggestions[]" value="user" class="input_check" /> <strong>', $txt['ban_on_username'], ':</strong><br />';
 
 		if (empty($context['ban_suggestions']['member']['id']))
 			echo '
@@ -1464,24 +1464,24 @@ function template_wap2_ban_edit()
 		echo '
 		<p class="titlebg">', $txt['ban_triggers'], '</p>
 		<p class="windowbg">
-			<input type="checkbox" name="ban_suggestion[]" value="main_ip" class="input_check" /> <strong>', $txt['wireless_ban_ip'], ':</strong><br />
+			<input type="checkbox" name="ban_suggestions[]" value="main_ip" class="input_check" /> <strong>', $txt['wireless_ban_ip'], ':</strong><br />
 			&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="main_ip" value="', $context['ban_suggestions']['main_ip'], '" size="20" class="input_text" />
 		</p>';
 
 		if (empty($modSettings['disableHostnameLookup']))
 			echo '
 		<p class="windowbg">
-			<input type="checkbox" name="ban_suggestion[]" value="hostname" class="input_check" /> <strong>', $txt['wireless_ban_hostname'], ':</strong><br />
+			<input type="checkbox" name="ban_suggestions[]" value="hostname" class="input_check" /> <strong>', $txt['wireless_ban_hostname'], ':</strong><br />
 			&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="hostname" value="', $context['ban_suggestions']['hostname'], '" size="20" class="input_text" />
 		<p>';
 
 		echo '
 		<p class="windowbg">
-			<input type="checkbox" name="ban_suggestion[]" value="email" class="input_check" /> <strong>', $txt['wireless_ban_email'], ':</strong><br />
+			<input type="checkbox" name="ban_suggestions[]" value="email" class="input_check" /> <strong>', $txt['wireless_ban_email'], ':</strong><br />
 			&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="email" value="', $context['ban_suggestions']['email'], '" size="20" class="input_text" />
 		</p>
 		<p class="windowbg">
-			<input type="checkbox" name="ban_suggestion[]" value="user" class="input_check" /> <strong>', $txt['ban_on_username'], ':</strong><br />';
+			<input type="checkbox" name="ban_suggestions[]" value="user" class="input_check" /> <strong>', $txt['ban_on_username'], ':</strong><br />';
 
 		if (empty($context['ban_suggestions']['member']['id']))
 			echo '