Example #1
0
/**
 * Do we think the current user is a spider?
 * 
 * @todo Should this not be... you know... in a different file?
 * @return int
 */
function SpiderCheck()
{
    global $modSettings;
    if (isset($_SESSION['id_robot'])) {
        unset($_SESSION['id_robot']);
    }
    $_SESSION['robot_check'] = time();
    // We cache the spider data for five minutes if we can.
    if (($spider_data = CacheAPI::getCache('spider_search', 300)) === null) {
        $request = smf_db_query('
			SELECT id_spider, user_agent, ip_info
			FROM {db_prefix}spiders', array());
        $spider_data = array();
        while ($row = mysql_fetch_assoc($request)) {
            $spider_data[] = $row;
        }
        mysql_free_result($request);
        CacheAPI::putCache('spider_search', $spider_data, 300);
    }
    if (empty($spider_data)) {
        return false;
    }
    // Only do these bits once.
    $ci_user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    // Always attempt IPv6 first.
    if (strpos($_SERVER['REMOTE_ADDR'], ':') !== false) {
        $ip_parts = convertIPv6toInts($_SERVER['REMOTE_ADDR']);
    } else {
        preg_match('/^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/', $_SERVER['REMOTE_ADDR'], $ip_parts);
    }
    foreach ($spider_data as $spider) {
        // User agent is easy.
        if (!empty($spider['user_agent']) && strpos($ci_user_agent, strtolower($spider['user_agent'])) !== false) {
            $_SESSION['id_robot'] = $spider['id_spider'];
        } elseif (!empty($ip_parts)) {
            $ips = explode(',', $spider['ip_info']);
            foreach ($ips as $ip) {
                $ip = ip2range($ip);
                if (!empty($ip)) {
                    foreach ($ip as $key => $value) {
                        if ($value['low'] > $ip_parts[$key + 1] || $value['high'] < $ip_parts[$key + 1]) {
                            break;
                        } elseif ($key == 7 && strpos($_SERVER['REMOTE_ADDR'], ':') !== false || $key == 3 && strpos($_SERVER['REMOTE_ADDR'], ':') === false) {
                            $_SESSION['id_robot'] = $spider['id_spider'];
                        }
                    }
                }
            }
        }
        if (isset($_SESSION['id_robot'])) {
            break;
        }
    }
    // If this is low server tracking then log the spider here as oppossed to the main logging function.
    if (!empty($modSettings['spider_mode']) && $modSettings['spider_mode'] == 1 && !empty($_SESSION['id_robot'])) {
        logSpider();
    }
    return !empty($_SESSION['id_robot']) ? $_SESSION['id_robot'] : 0;
}
function SpiderCheck()
{
    global $modSettings, $smcFunc;
    if (isset($_SESSION['id_robot'])) {
        unset($_SESSION['id_robot']);
    }
    $_SESSION['robot_check'] = time();
    // We cache the spider data for five minutes if we can.
    if (!empty($modSettings['cache_enable'])) {
        $spider_data = cache_get_data('spider_search', 300);
    }
    if (!isset($spider_data) || $spider_data === NULL) {
        $request = $smcFunc['db_query']('spider_check', '
			SELECT id_spider, user_agent, ip_info
			FROM {db_prefix}spiders', array());
        $spider_data = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $spider_data[] = $row;
        }
        $smcFunc['db_free_result']($request);
        if (!empty($modSettings['cache_enable'])) {
            cache_put_data('spider_search', $spider_data, 300);
        }
    }
    if (empty($spider_data)) {
        return false;
    }
    // Only do these bits once.
    $ci_user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    preg_match('/^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/', $_SERVER['REMOTE_ADDR'], $ip_parts);
    foreach ($spider_data as $spider) {
        // User agent is easy.
        if (!empty($spider['user_agent']) && strpos($ci_user_agent, strtolower($spider['user_agent'])) !== false) {
            $_SESSION['id_robot'] = $spider['id_spider'];
        } elseif (!empty($ip_parts)) {
            $ips = explode(',', $spider['ip_info']);
            foreach ($ips as $ip) {
                $ip = ip2range($ip);
                if (!empty($ip)) {
                    foreach ($ip as $key => $value) {
                        if ($value['low'] > $ip_parts[$key + 1] || $value['high'] < $ip_parts[$key + 1]) {
                            break;
                        } elseif ($key == 3) {
                            $_SESSION['id_robot'] = $spider['id_spider'];
                        }
                    }
                }
            }
        }
        if (isset($_SESSION['id_robot'])) {
            break;
        }
    }
    // If this is low server tracking then log the spider here as oppossed to the main logging function.
    if (!empty($modSettings['spider_mode']) && $modSettings['spider_mode'] == 1 && !empty($_SESSION['id_robot'])) {
        logSpider();
    }
    return !empty($_SESSION['id_robot']) ? $_SESSION['id_robot'] : 0;
}
Example #3
0
function BanEdit()
{
    global $txt, $modSettings, $context, $ban_request, $scripturl, $smcFunc;
    $_REQUEST['bg'] = empty($_REQUEST['bg']) ? 0 : (int) $_REQUEST['bg'];
    // Adding or editing a ban trigger?
    if (!empty($_POST['add_new_trigger']) || !empty($_POST['edit_trigger'])) {
        checkSession();
        $newBan = !empty($_POST['add_new_trigger']);
        $values = array('id_ban_group' => $_REQUEST['bg'], 'hostname' => '', 'email_address' => '', 'id_member' => 0, 'ip_low1' => 0, 'ip_high1' => 0, 'ip_low2' => 0, 'ip_high2' => 0, 'ip_low3' => 0, 'ip_high3' => 0, 'ip_low4' => 0, 'ip_high4' => 0);
        // Preset all values that are required.
        if ($newBan) {
            $insertKeys = array('id_ban_group' => 'int', 'hostname' => 'string', 'email_address' => 'string', 'id_member' => 'int', 'ip_low1' => 'int', 'ip_high1' => 'int', 'ip_low2' => 'int', 'ip_high2' => 'int', 'ip_low3' => 'int', 'ip_high3' => 'int', 'ip_low4' => 'int', 'ip_high4' => 'int');
        } else {
            $updateString = '
				hostname = {string:hostname}, email_address = {string:email_address}, id_member = {int:id_member},
				ip_low1 = {int:ip_low1}, ip_high1 = {int:ip_high1},
				ip_low2 = {int:ip_low2}, ip_high2 = {int:ip_high2},
				ip_low3 = {int:ip_low3}, ip_high3 = {int:ip_high3},
				ip_low4 = {int:ip_low4}, ip_high4 = {int:ip_high4}';
        }
        if ($_POST['bantype'] == 'ip_ban') {
            $ip = trim($_POST['ip']);
            $ip_parts = ip2range($ip);
            $ip_check = checkExistingTriggerIP($ip_parts, $ip);
            if (!$ip_check) {
                fatal_lang_error('invalid_ip', false);
            }
            $values = array_merge($values, $ip_check);
            $modlogInfo['ip_range'] = $_POST['ip'];
        } elseif ($_POST['bantype'] == 'hostname_ban') {
            if (preg_match('/[^\\w.\\-*]/', $_POST['hostname']) == 1) {
                fatal_lang_error('invalid_hostname', false);
            }
            // Replace the * wildcard by a MySQL compatible wildcard %.
            $_POST['hostname'] = str_replace('*', '%', $_POST['hostname']);
            $values['hostname'] = $_POST['hostname'];
            $modlogInfo['hostname'] = $_POST['hostname'];
        } elseif ($_POST['bantype'] == 'email_ban') {
            if (preg_match('/[^\\w.\\-\\+*@]/', $_POST['email']) == 1) {
                fatal_lang_error('invalid_email', false);
            }
            $_POST['email'] = strtolower(str_replace('*', '%', $_POST['email']));
            // Check the user is not banning an admin.
            $request = $smcFunc['db_query']('', '
				SELECT id_member
				FROM {db_prefix}members
				WHERE (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0)
					AND email_address LIKE {string:email}
				LIMIT 1', array('admin_group' => 1, 'email' => $_POST['email']));
            if ($smcFunc['db_num_rows']($request) != 0) {
                fatal_lang_error('no_ban_admin', 'critical');
            }
            $smcFunc['db_free_result']($request);
            $values['email_address'] = $_POST['email'];
            $modlogInfo['email'] = $_POST['email'];
        } elseif ($_POST['bantype'] == 'user_ban') {
            $_POST['user'] = preg_replace('~&amp;#(\\d{4,5}|[2-9]\\d{2,4}|1[2-9]\\d);~', '&#$1;', $smcFunc['htmlspecialchars']($_POST['user'], ENT_QUOTES));
            $request = $smcFunc['db_query']('', '
				SELECT id_member, (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0) AS isAdmin
				FROM {db_prefix}members
				WHERE member_name = {string:user_name} OR real_name = {string:user_name}
				LIMIT 1', array('admin_group' => 1, 'user_name' => $_POST['user']));
            if ($smcFunc['db_num_rows']($request) == 0) {
                fatal_lang_error('invalid_username', false);
            }
            list($memberid, $isAdmin) = $smcFunc['db_fetch_row']($request);
            $smcFunc['db_free_result']($request);
            if ($isAdmin && $isAdmin != 'f') {
                fatal_lang_error('no_ban_admin', 'critical');
            }
            $values['id_member'] = $memberid;
            $modlogInfo['member'] = $memberid;
        } else {
            fatal_lang_error('no_bantype_selected', false);
        }
        if ($newBan) {
            $smcFunc['db_insert']('', '{db_prefix}ban_items', $insertKeys, $values, array('id_ban'));
        } else {
            $smcFunc['db_query']('', '
				UPDATE {db_prefix}ban_items
				SET ' . $updateString . '
				WHERE id_ban = {int:ban_item}
					AND id_ban_group = {int:id_ban_group}', array_merge($values, array('ban_item' => (int) $_REQUEST['bi'])));
        }
        // Log the addion of the ban entry into the moderation log.
        logAction('ban', $modlogInfo + array('new' => $newBan, 'type' => $_POST['bantype']));
        // Register the last modified date.
        updateSettings(array('banLastUpdated' => time()));
        // Update the member table to represent the new ban situation.
        updateBanMembers();
    } elseif (!empty($_POST['remove_selection']) && !empty($_POST['ban_items']) && is_array($_POST['ban_items'])) {
        checkSession();
        // Making sure every deleted ban item is an integer.
        foreach ($_POST['ban_items'] as $key => $value) {
            $_POST['ban_items'][$key] = (int) $value;
        }
        $smcFunc['db_query']('', '
			DELETE FROM {db_prefix}ban_items
			WHERE id_ban IN ({array_int:ban_list})
				AND id_ban_group = {int:ban_group}', array('ban_list' => $_POST['ban_items'], 'ban_group' => $_REQUEST['bg']));
        // It changed, let the settings and the member table know.
        updateSettings(array('banLastUpdated' => time()));
        updateBanMembers();
    } elseif (!empty($_POST['modify_ban']) || !empty($_POST['add_ban'])) {
        checkSession();
        $addBan = !empty($_POST['add_ban']);
        if (empty($_POST['ban_name'])) {
            fatal_lang_error('ban_name_empty', false);
        }
        // Let's not allow HTML in ban names, it's more evil than beneficial.
        $_POST['ban_name'] = $smcFunc['htmlspecialchars']($_POST['ban_name'], ENT_QUOTES);
        // Check whether a ban with this name already exists.
        $request = $smcFunc['db_query']('', '
			SELECT id_ban_group
			FROM {db_prefix}ban_groups
			WHERE name = {string:new_ban_name}' . ($addBan ? '' : '
				AND id_ban_group != {int:ban_group}') . '
			LIMIT 1', array('ban_group' => $_REQUEST['bg'], 'new_ban_name' => $_POST['ban_name']));
        if ($smcFunc['db_num_rows']($request) == 1) {
            fatal_lang_error('ban_name_exists', false, array($_POST['ban_name']));
        }
        $smcFunc['db_free_result']($request);
        $_POST['reason'] = $smcFunc['htmlspecialchars']($_POST['reason'], ENT_QUOTES);
        $_POST['notes'] = $smcFunc['htmlspecialchars']($_POST['notes'], ENT_QUOTES);
        $_POST['notes'] = str_replace(array("\r", "\n", '  '), array('', '<br />', '&nbsp; '), $_POST['notes']);
        $_POST['expiration'] = $_POST['expiration'] == 'never' ? 'NULL' : ($_POST['expiration'] == 'expired' ? '0' : ($_POST['expire_date'] != $_POST['old_expire'] ? time() + 24 * 60 * 60 * (int) $_POST['expire_date'] : 'expire_time'));
        $_POST['full_ban'] = empty($_POST['full_ban']) ? '0' : '1';
        $_POST['cannot_post'] = !empty($_POST['full_ban']) || empty($_POST['cannot_post']) ? '0' : '1';
        $_POST['cannot_register'] = !empty($_POST['full_ban']) || empty($_POST['cannot_register']) ? '0' : '1';
        $_POST['cannot_login'] = !empty($_POST['full_ban']) || empty($_POST['cannot_login']) ? '0' : '1';
        if ($addBan) {
            // Adding some ban triggers?
            if ($addBan && !empty($_POST['ban_suggestion']) && is_array($_POST['ban_suggestion'])) {
                $ban_triggers = array();
                $ban_logs = array();
                if (in_array('main_ip', $_POST['ban_suggestion']) && !empty($_POST['main_ip'])) {
                    $ip = trim($_POST['main_ip']);
                    $ip_parts = ip2range($ip);
                    if (!checkExistingTriggerIP($ip_parts, $ip)) {
                        fatal_lang_error('invalid_ip', false);
                    }
                    $ban_triggers[] = array($ip_parts[0]['low'], $ip_parts[0]['high'], $ip_parts[1]['low'], $ip_parts[1]['high'], $ip_parts[2]['low'], $ip_parts[2]['high'], $ip_parts[3]['low'], $ip_parts[3]['high'], '', '', 0);
                    $ban_logs[] = array('ip_range' => $_POST['main_ip']);
                }
                if (in_array('hostname', $_POST['ban_suggestion']) && !empty($_POST['hostname'])) {
                    if (preg_match('/[^\\w.\\-*]/', $_POST['hostname']) == 1) {
                        fatal_lang_error('invalid_hostname', false);
                    }
                    // Replace the * wildcard by a MySQL wildcard %.
                    $_POST['hostname'] = str_replace('*', '%', $_POST['hostname']);
                    $ban_triggers[] = array(0, 0, 0, 0, 0, 0, 0, 0, substr($_POST['hostname'], 0, 255), '', 0);
                    $ban_logs[] = array('hostname' => $_POST['hostname']);
                }
                if (in_array('email', $_POST['ban_suggestion']) && !empty($_POST['email'])) {
                    if (preg_match('/[^\\w.\\-\\+*@]/', $_POST['email']) == 1) {
                        fatal_lang_error('invalid_email', false);
                    }
                    $_POST['email'] = strtolower(str_replace('*', '%', $_POST['email']));
                    $ban_triggers[] = array(0, 0, 0, 0, 0, 0, 0, 0, '', substr($_POST['email'], 0, 255), 0);
                    $ban_logs[] = array('email' => $_POST['email']);
                }
                if (in_array('user', $_POST['ban_suggestion']) && (!empty($_POST['bannedUser']) || !empty($_POST['user']))) {
                    // We got a username, let's find its ID.
                    if (empty($_POST['bannedUser'])) {
                        $_POST['user'] = preg_replace('~&amp;#(\\d{4,5}|[2-9]\\d{2,4}|1[2-9]\\d);~', '&#$1;', $smcFunc['htmlspecialchars']($_POST['user'], ENT_QUOTES));
                        $request = $smcFunc['db_query']('', '
							SELECT id_member, (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0) AS isAdmin
							FROM {db_prefix}members
							WHERE member_name = {string:username} OR real_name = {string:username}
							LIMIT 1', array('admin_group' => 1, 'username' => $_POST['user']));
                        if ($smcFunc['db_num_rows']($request) == 0) {
                            fatal_lang_error('invalid_username', false);
                        }
                        list($_POST['bannedUser'], $isAdmin) = $smcFunc['db_fetch_row']($request);
                        $smcFunc['db_free_result']($request);
                        if ($isAdmin && $isAdmin != 'f') {
                            fatal_lang_error('no_ban_admin', 'critical');
                        }
                    }
                    $ban_triggers[] = array(0, 0, 0, 0, 0, 0, 0, 0, '', '', (int) $_POST['bannedUser']);
                    $ban_logs[] = array('member' => $_POST['bannedUser']);
                }
                if (!empty($_POST['ban_suggestion']['ips']) && is_array($_POST['ban_suggestion']['ips'])) {
                    $_POST['ban_suggestion']['ips'] = array_unique($_POST['ban_suggestion']['ips']);
                    // Don't add the main IP again.
                    if (in_array('main_ip', $_POST['ban_suggestion'])) {
                        $_POST['ban_suggestion']['ips'] = array_diff($_POST['ban_suggestion']['ips'], array($_POST['main_ip']));
                    }
                    foreach ($_POST['ban_suggestion']['ips'] as $ip) {
                        $ip_parts = ip2range($ip);
                        // They should be alright, but just to be sure...
                        if (count($ip_parts) != 4) {
                            fatal_lang_error('invalid_ip', false);
                        }
                        $ban_triggers[] = array($ip_parts[0]['low'], $ip_parts[0]['high'], $ip_parts[1]['low'], $ip_parts[1]['high'], $ip_parts[2]['low'], $ip_parts[2]['high'], $ip_parts[3]['low'], $ip_parts[3]['high'], '', '', 0);
                        $ban_logs[] = array('ip_range' => $ip);
                    }
                }
            }
            // Yes yes, we're ready to add now.
            $smcFunc['db_insert']('', '{db_prefix}ban_groups', array('name' => 'string-20', 'ban_time' => 'int', 'expire_time' => 'raw', 'cannot_access' => 'int', 'cannot_register' => 'int', 'cannot_post' => 'int', 'cannot_login' => 'int', 'reason' => 'string-255', 'notes' => 'string-65534'), array($_POST['ban_name'], time(), $_POST['expiration'], $_POST['full_ban'], $_POST['cannot_register'], $_POST['cannot_post'], $_POST['cannot_login'], $_POST['reason'], $_POST['notes']), array('id_ban_group'));
            $_REQUEST['bg'] = $smcFunc['db_insert_id']('{db_prefix}ban_groups', 'id_ban_group');
            // Now that the ban group is added, add some triggers as well.
            if (!empty($ban_triggers) && !empty($_REQUEST['bg'])) {
                // Put in the ban group ID.
                foreach ($ban_triggers as $k => $trigger) {
                    array_unshift($ban_triggers[$k], $_REQUEST['bg']);
                }
                // Log what we are doing!
                foreach ($ban_logs as $log_details) {
                    logAction('ban', $log_details + array('new' => 1));
                }
                $smcFunc['db_insert']('', '{db_prefix}ban_items', array('id_ban_group' => 'int', 'ip_low1' => 'int', 'ip_high1' => 'int', 'ip_low2' => 'int', 'ip_high2' => 'int', 'ip_low3' => 'int', 'ip_high3' => 'int', 'ip_low4' => 'int', 'ip_high4' => 'int', 'hostname' => 'string-255', 'email_address' => 'string-255', 'id_member' => 'int'), $ban_triggers, array('id_ban'));
            }
        } else {
            $smcFunc['db_query']('', '
				UPDATE {db_prefix}ban_groups
				SET
					name = {string:ban_name},
					reason = {string:reason},
					notes = {string:notes},
					expire_time = {raw:expiration},
					cannot_access = {int:cannot_access},
					cannot_post = {int:cannot_post},
					cannot_register = {int:cannot_register},
					cannot_login = {int:cannot_login}
				WHERE id_ban_group = {int:id_ban_group}', array('expiration' => $_POST['expiration'], 'cannot_access' => $_POST['full_ban'], 'cannot_post' => $_POST['cannot_post'], 'cannot_register' => $_POST['cannot_register'], 'cannot_login' => $_POST['cannot_login'], 'id_ban_group' => $_REQUEST['bg'], 'ban_name' => $_POST['ban_name'], 'reason' => $_POST['reason'], 'notes' => $_POST['notes']));
        }
        // No more caching, we have something new here.
        updateSettings(array('banLastUpdated' => time()));
        updateBanMembers();
    }
    // If we're editing an existing ban, get it from the database.
    if (!empty($_REQUEST['bg'])) {
        $context['ban_items'] = array();
        $request = $smcFunc['db_query']('', '
			SELECT
				bi.id_ban, bi.hostname, bi.email_address, bi.id_member, bi.hits,
				bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4,
				bg.id_ban_group, bg.name, bg.ban_time, bg.expire_time, bg.reason, bg.notes, bg.cannot_access, bg.cannot_register, bg.cannot_login, bg.cannot_post,
				IFNULL(mem.id_member, 0) AS id_member, mem.member_name, mem.real_name
			FROM {db_prefix}ban_groups AS bg
				LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_ban_group = bg.id_ban_group)
				LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
			WHERE bg.id_ban_group = {int:current_ban}', array('current_ban' => $_REQUEST['bg']));
        if ($smcFunc['db_num_rows']($request) == 0) {
            fatal_lang_error('ban_not_found', false);
        }
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            if (!isset($context['ban'])) {
                $context['ban'] = array('id' => $row['id_ban_group'], 'name' => $row['name'], 'expiration' => array('status' => $row['expire_time'] === null ? 'never' : ($row['expire_time'] < time() ? 'expired' : 'still_active_but_we_re_counting_the_days'), 'days' => $row['expire_time'] > time() ? floor(($row['expire_time'] - time()) / 86400) : 0), 'reason' => $row['reason'], 'notes' => $row['notes'], 'cannot' => array('access' => !empty($row['cannot_access']), 'post' => !empty($row['cannot_post']), 'register' => !empty($row['cannot_register']), 'login' => !empty($row['cannot_login'])), 'is_new' => false);
            }
            if (!empty($row['id_ban'])) {
                $context['ban_items'][$row['id_ban']] = array('id' => $row['id_ban'], 'hits' => $row['hits']);
                if (!empty($row['ip_high1'])) {
                    $context['ban_items'][$row['id_ban']]['type'] = 'ip';
                    $context['ban_items'][$row['id_ban']]['ip'] = range2ip(array($row['ip_low1'], $row['ip_low2'], $row['ip_low3'], $row['ip_low4']), array($row['ip_high1'], $row['ip_high2'], $row['ip_high3'], $row['ip_high4']));
                } elseif (!empty($row['hostname'])) {
                    $context['ban_items'][$row['id_ban']]['type'] = 'hostname';
                    $context['ban_items'][$row['id_ban']]['hostname'] = str_replace('%', '*', $row['hostname']);
                } elseif (!empty($row['email_address'])) {
                    $context['ban_items'][$row['id_ban']]['type'] = 'email';
                    $context['ban_items'][$row['id_ban']]['email'] = str_replace('%', '*', $row['email_address']);
                } elseif (!empty($row['id_member'])) {
                    $context['ban_items'][$row['id_ban']]['type'] = 'user';
                    $context['ban_items'][$row['id_ban']]['user'] = array('id' => $row['id_member'], 'name' => $row['real_name'], 'href' => $scripturl . '?action=profile;u=' . $row['id_member'], 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>');
                } else {
                    unset($context['ban_items'][$row['id_ban']]);
                    $smcFunc['db_query']('', '
						DELETE FROM {db_prefix}ban_items
						WHERE id_ban = {int:current_ban}', array('current_ban' => $row['id_ban']));
                }
            }
        }
        $smcFunc['db_free_result']($request);
    } else {
        $context['ban'] = array('id' => 0, 'name' => '', 'expiration' => array('status' => 'never', 'days' => 0), 'reason' => '', 'notes' => '', 'ban_days' => 0, 'cannot' => array('access' => true, 'post' => false, 'register' => false, 'login' => false), 'is_new' => true);
        $context['ban_suggestions'] = array('main_ip' => '', 'hostname' => '', 'email' => '', 'member' => array('id' => 0));
        // Overwrite some of the default form values if a user ID was given.
        if (!empty($_REQUEST['u'])) {
            $request = $smcFunc['db_query']('', '
				SELECT id_member, real_name, member_ip, email_address
				FROM {db_prefix}members
				WHERE id_member = {int:current_user}
				LIMIT 1', array('current_user' => (int) $_REQUEST['u']));
            if ($smcFunc['db_num_rows']($request) > 0) {
                list($context['ban_suggestions']['member']['id'], $context['ban_suggestions']['member']['name'], $context['ban_suggestions']['main_ip'], $context['ban_suggestions']['email']) = $smcFunc['db_fetch_row']($request);
            }
            $smcFunc['db_free_result']($request);
            if (!empty($context['ban_suggestions']['member']['id'])) {
                $context['ban_suggestions']['href'] = $scripturl . '?action=profile;u=' . $context['ban_suggestions']['member']['id'];
                $context['ban_suggestions']['member']['link'] = '<a href="' . $context['ban_suggestions']['href'] . '">' . $context['ban_suggestions']['member']['name'] . '</a>';
                // Default the ban name to the name of the banned member.
                $context['ban']['name'] = $context['ban_suggestions']['member']['name'];
                // 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'])) {
                    $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');
            }
        }
    }
    // Template needs this to show errors using javascript
    loadLanguage('Errors');
    // If we're in wireless mode remove the admin template layer and use a special template.
    if (WIRELESS && WIRELESS_PROTOCOL != 'wap') {
        $context['sub_template'] = WIRELESS_PROTOCOL . '_ban_edit';
        foreach ($context['template_layers'] as $k => $v) {
            if (strpos($v, 'generic_menu') === 0) {
                unset($context['template_layers'][$k]);
            }
        }
    } else {
        $context['sub_template'] = 'ban_edit';
    }
}
Example #4
0
/**
 * This function validates the ban triggers
 *
 * @package Bans
 * @param mixed[] $triggers
 */
function validateTriggers(&$triggers)
{
    $db = database();
    $ban_errors = Error_Context::context('ban', 1);
    if (empty($triggers)) {
        $ban_errors->addError('ban_empty_triggers');
    }
    $ban_triggers = array();
    $log_info = array();
    // Go through each trigger and make sure its valid
    foreach ($triggers as $key => $value) {
        if (!empty($value)) {
            if ($key == 'member') {
                continue;
            }
            if ($key == 'main_ip') {
                $value = trim($value);
                $ip_parts = ip2range($value);
                if (!checkExistingTriggerIP($ip_parts, $value)) {
                    $ban_errors->addError('invalid_ip');
                } else {
                    $ban_triggers['main_ip'] = 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']);
                }
            } elseif ($key == 'hostname') {
                if (preg_match('/[^\\w.\\-*]/', $value) == 1) {
                    $ban_errors->addError('invalid_hostname');
                } else {
                    // Replace the * wildcard by a MySQL wildcard %.
                    $value = substr(str_replace('*', '%', $value), 0, 255);
                    $ban_triggers['hostname']['hostname'] = $value;
                }
            } elseif ($key == 'email') {
                if (preg_match('/[^\\w.\\-\\+*@]/', $value) == 1) {
                    $ban_errors->addError('invalid_email');
                }
                // Check the user is not banning an admin.
                $request = $db->query('', '
					SELECT id_member
					FROM {db_prefix}members
					WHERE (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0)
						AND email_address LIKE {string:email}
					LIMIT 1', array('admin_group' => 1, 'email' => $value));
                if ($db->num_rows($request) != 0) {
                    $ban_errors->addError('no_ban_admin');
                }
                $db->free_result($request);
                $value = substr(strtolower(str_replace('*', '%', $value)), 0, 255);
                $ban_triggers['email']['email_address'] = $value;
            } elseif ($key == 'user') {
                $user = preg_replace('~&amp;#(\\d{4,5}|[2-9]\\d{2,4}|1[2-9]\\d);~', '&#$1;', Util::htmlspecialchars($value, ENT_QUOTES));
                $request = $db->query('', '
					SELECT id_member, (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0) AS isAdmin
					FROM {db_prefix}members
					WHERE member_name = {string:username} OR real_name = {string:username}
					LIMIT 1', array('admin_group' => 1, 'username' => $user));
                if ($db->num_rows($request) == 0) {
                    $ban_errors->addError('invalid_username');
                }
                list($value, $isAdmin) = $db->fetch_row($request);
                $db->free_result($request);
                if ($isAdmin && strtolower($isAdmin) != 'f') {
                    unset($value);
                    $ban_errors->addError('no_ban_admin');
                } else {
                    $ban_triggers['user']['id_member'] = $value;
                }
            } elseif (in_array($key, array('ips_in_messages', 'ips_in_errors'))) {
                // 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)) {
                        $ban_errors->addError('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 {
                $ban_errors->addError('no_bantype_selected');
            }
            if (isset($value) && !is_array($value)) {
                $log_info[] = array('value' => $value, 'bantype' => $key);
            }
        }
    }
    return array('ban_triggers' => $ban_triggers, 'log_info' => $log_info);
}
Example #5
0
/**
 * Do we think the current user is a spider?
 *
 * @package SearchEngines
 * @return int
 */
function spiderCheck()
{
    global $modSettings;
    $db = database();
    if (isset($_SESSION['id_robot'])) {
        unset($_SESSION['id_robot']);
    }
    $_SESSION['robot_check'] = time();
    // We cache the spider data for five minutes if we can.
    if (($spider_data = cache_get_data('spider_search', 300)) === null) {
        $request = $db->query('', '
			SELECT id_spider, user_agent, ip_info
			FROM {db_prefix}spiders
			ORDER BY LENGTH(user_agent) DESC', array());
        $spider_data = array();
        while ($row = $db->fetch_assoc($request)) {
            $spider_data[] = $row;
        }
        $db->free_result($request);
        // Save it in the cache
        cache_put_data('spider_search', $spider_data, 300);
    }
    if (empty($spider_data)) {
        return false;
    }
    // We need the user agent
    $req = request();
    // Always attempt IPv6 first.
    if (strpos($_SERVER['REMOTE_ADDR'], ':') !== false) {
        $ip_parts = convertIPv6toInts($_SERVER['REMOTE_ADDR']);
    } else {
        preg_match('/^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/', $_SERVER['REMOTE_ADDR'], $ip_parts);
    }
    foreach ($spider_data as $spider) {
        // User agent is easy.
        if (!empty($spider['user_agent']) && strpos(strtolower($req->user_agent()), strtolower($spider['user_agent'])) !== false) {
            $_SESSION['id_robot'] = $spider['id_spider'];
        } elseif (!empty($ip_parts)) {
            $ips = explode(',', $spider['ip_info']);
            foreach ($ips as $ip) {
                $ip = ip2range($ip);
                if (!empty($ip)) {
                    foreach ($ip as $key => $value) {
                        if ($value['low'] > $ip_parts[$key + 1] || $value['high'] < $ip_parts[$key + 1]) {
                            break;
                        } elseif ($key == 7 && strpos($_SERVER['REMOTE_ADDR'], ':') !== false || $key == 3 && strpos($_SERVER['REMOTE_ADDR'], ':') === false) {
                            $_SESSION['id_robot'] = $spider['id_spider'];
                        }
                    }
                }
            }
        }
        if (isset($_SESSION['id_robot'])) {
            break;
        }
    }
    // If this is low server tracking then log the spider here as oppossed to the main logging function.
    if (!empty($modSettings['spider_mode']) && $modSettings['spider_mode'] == 1 && !empty($_SESSION['id_robot'])) {
        logSpider();
    }
    return !empty($_SESSION['id_robot']) ? $_SESSION['id_robot'] : 0;
}
Example #6
0
function BanEdit()
{
    global $txt, $db_prefix, $modSettings, $context, $ban_request, $scripturl;
    global $func;
    $_REQUEST['bg'] = empty($_REQUEST['bg']) ? 0 : (int) $_REQUEST['bg'];
    // Adding or editing a ban trigger?
    if (!empty($_POST['add_new_trigger']) || !empty($_POST['edit_trigger'])) {
        checkSession();
        $newBan = !empty($_POST['add_new_trigger']);
        // Preset all values that are required.
        if ($newBan) {
            $inserts = array('ID_BAN_GROUP' => $_REQUEST['bg'], 'hostname' => "''", 'email_address' => "''");
        }
        if ($_POST['bantype'] == 'ip_ban') {
            $ip_parts = ip2range($_POST['ip']);
            if (count($ip_parts) != 4) {
                fatal_lang_error('invalid_ip', false);
            }
            if ($newBan) {
                $inserts += 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']);
            } else {
                $update = '
					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'] . ',
					hostname = \'\', email_address = \'\', ID_MEMBER = 0';
            }
            $modlogInfo['ip_range'] = $_POST['ip'];
        } elseif ($_POST['bantype'] == 'hostname_ban') {
            if (preg_match("/[^\\w.\\-*]/", $_POST['hostname']) == 1) {
                fatal_lang_error('invalid_hostname', false);
            }
            // Replace the * wildcard by a MySQL compatible wildcard %.
            $_POST['hostname'] = str_replace('*', '%', $_POST['hostname']);
            if ($newBan) {
                $inserts['hostname'] = "'{$_POST['hostname']}'";
            } else {
                $update = "\n\t\t\t\t\tip_low1 = 0, ip_high1 = 0,\n\t\t\t\t\tip_low2 = 0, ip_high2 = 0,\n\t\t\t\t\tip_low3 = 0, ip_high3 = 0,\n\t\t\t\t\tip_low4 = 0, ip_high4 = 0,\n\t\t\t\t\thostname = '{$_POST['hostname']}', email_address = '', ID_MEMBER = 0";
            }
            $modlogInfo['hostname'] = stripslashes($_POST['hostname']);
        } elseif ($_POST['bantype'] == 'email_ban') {
            if (preg_match("/[^\\w.\\-*@]/", $_POST['email']) == 1) {
                fatal_lang_error('invalid_email', false);
            }
            $_POST['email'] = strtolower(str_replace('*', '%', $_POST['email']));
            // Check the user is not banning an admin.
            $request = db_query("\n\t\t\t\tSELECT ID_MEMBER\n\t\t\t\tFROM {$db_prefix}members\n\t\t\t\tWHERE (ID_GROUP = 1 OR FIND_IN_SET(1, additionalGroups))\n\t\t\t\t\tAND emailAddress LIKE '{$_POST['email']}'\n\t\t\t\tLIMIT 1", __FILE__, __LINE__);
            if (mysql_num_rows($request) != 0) {
                fatal_lang_error('no_ban_admin');
            }
            mysql_free_result($request);
            if ($newBan) {
                $inserts['email_address'] = "'{$_POST['email']}'";
            } else {
                $update = "\n\t\t\t\t\tip_low1 = 0, ip_high1 = 0,\n\t\t\t\t\tip_low2 = 0, ip_high2 = 0,\n\t\t\t\t\tip_low3 = 0, ip_high3 = 0,\n\t\t\t\t\tip_low4 = 0, ip_high4 = 0,\n\t\t\t\t\thostname = '', email_address = '{$_POST['email']}', ID_MEMBER = 0";
            }
            $modlogInfo['email'] = stripslashes($_POST['email']);
        } elseif ($_POST['bantype'] == 'user_ban') {
            $_POST['user'] = $func['htmlspecialchars']($_POST['user'], ENT_QUOTES);
            $request = db_query("\n\t\t\t\tSELECT ID_MEMBER, (ID_GROUP = 1 OR FIND_IN_SET(1, additionalGroups)) AS isAdmin\n\t\t\t\tFROM {$db_prefix}members\n\t\t\t\tWHERE memberName = '{$_POST['user']}' OR realName = '{$_POST['user']}'\n\t\t\t\tLIMIT 1", __FILE__, __LINE__);
            if (mysql_num_rows($request) == 0) {
                fatal_lang_error('invalid_username', false);
            }
            list($memberid, $isAdmin) = mysql_fetch_row($request);
            mysql_free_result($request);
            if ($isAdmin) {
                fatal_lang_error('no_ban_admin');
            }
            if ($newBan) {
                $inserts['ID_MEMBER'] = $memberid;
            } else {
                $update = "\n\t\t\t\t\tip_low1 = 0, ip_high1 = 0,\n\t\t\t\t\tip_low2 = 0, ip_high2 = 0,\n\t\t\t\t\tip_low3 = 0, ip_high3 = 0,\n\t\t\t\t\tip_low4 = 0, ip_high4 = 0,\n\t\t\t\t\thostname = '', email_address = '', ID_MEMBER = {$memberid}";
            }
            $modlogInfo['member'] = $memberid;
        } else {
            fatal_lang_error('no_bantype_selected', false);
        }
        if ($newBan) {
            db_query("\n\t\t\t\tINSERT INTO {$db_prefix}ban_items\n\t\t\t\t\t(" . implode(', ', array_keys($inserts)) . ")\n\t\t\t\tVALUES (" . implode(', ', $inserts) . ")", __FILE__, __LINE__);
        } else {
            db_query("\n\t\t\t\tUPDATE {$db_prefix}ban_items\n\t\t\t\tSET {$update}\n\t\t\t\tWHERE ID_BAN = " . (int) $_REQUEST['bi'] . "\n\t\t\t\t\tAND ID_BAN_GROUP = {$_REQUEST['bg']}\n\t\t\t\tLIMIT 1", __FILE__, __LINE__);
        }
        // Log the addion of the ban entry into the moderation log.
        logAction('ban', $modlogInfo + array('new' => $newBan, 'type' => $_POST['bantype']));
        // Register the last modified date.
        updateSettings(array('banLastUpdated' => time()));
        // Update the member table to represent the new ban situation.
        updateBanMembers();
    } elseif (!empty($_POST['remove_selection']) && !empty($_POST['ban_items']) && is_array($_POST['ban_items'])) {
        checkSession();
        // Making sure every deleted ban item is an integer.
        foreach ($_POST['ban_items'] as $key => $value) {
            $_POST['ban_items'][$key] = (int) $value;
        }
        db_query("\n\t\t\tDELETE FROM {$db_prefix}ban_items\n\t\t\tWHERE ID_BAN IN (" . implode(', ', $_POST['ban_items']) . ")\n\t\t\t\tAND ID_BAN_GROUP = {$_REQUEST['bg']}\n\t\t\tLIMIT " . count($_POST['ban_items']), __FILE__, __LINE__);
        // It changed, let the settings and the member table know.
        updateSettings(array('banLastUpdated' => time()));
        updateBanMembers();
    } elseif (!empty($_POST['modify_ban']) || !empty($_POST['add_ban'])) {
        checkSession();
        $addBan = !empty($_POST['add_ban']);
        if (empty($_POST['ban_name'])) {
            fatal_error($txt['ban_name_empty'], false);
        }
        // Check whether a ban with this name already exists.
        $request = db_query("\n\t\t\tSELECT ID_BAN_GROUP\n\t\t\tFROM {$db_prefix}ban_groups\n\t\t\tWHERE name = '{$_POST['ban_name']}'" . ($addBan ? '' : "\n\t\t\t\tAND ID_BAN_GROUP != {$_REQUEST['bg']}") . "\n\t\t\tLIMIT 1", __FILE__, __LINE__);
        // !!! Separate the sprintf?
        if (mysql_num_rows($request) == 1) {
            fatal_error(sprintf($txt['ban_name_exists'], $_POST['ban_name']), false);
        }
        mysql_free_result($request);
        $_POST['reason'] = htmlspecialchars($_POST['reason'], ENT_QUOTES);
        $_POST['notes'] = htmlspecialchars($_POST['notes'], ENT_QUOTES);
        $_POST['notes'] = str_replace(array("\r", "\n", '  '), array('', '<br />', '&nbsp; '), $_POST['notes']);
        $_POST['expiration'] = $_POST['expiration'] == 'never' ? 'NULL' : ($_POST['expiration'] == 'expired' ? '0' : ($_POST['expire_date'] != $_POST['old_expire'] ? time() + 24 * 60 * 60 * (int) $_POST['expire_date'] : 'expire_time'));
        $_POST['full_ban'] = empty($_POST['full_ban']) ? '0' : '1';
        $_POST['cannot_post'] = !empty($_POST['full_ban']) || empty($_POST['cannot_post']) ? '0' : '1';
        $_POST['cannot_register'] = !empty($_POST['full_ban']) || empty($_POST['cannot_register']) ? '0' : '1';
        $_POST['cannot_login'] = !empty($_POST['full_ban']) || empty($_POST['cannot_login']) ? '0' : '1';
        if ($addBan) {
            // Adding some ban triggers?
            if ($addBan && !empty($_POST['ban_suggestion']) && is_array($_POST['ban_suggestion'])) {
                $ban_triggers = array();
                if (in_array('main_ip', $_POST['ban_suggestion']) && !empty($_POST['main_ip'])) {
                    $ip_parts = ip2range($_POST['main_ip']);
                    if (count($ip_parts) != 4) {
                        fatal_lang_error('invalid_ip', false);
                    }
                    $ban_triggers[] = $ip_parts[0]['low'] . ', ' . $ip_parts[0]['high'] . ', ' . $ip_parts[1]['low'] . ', ' . $ip_parts[1]['high'] . ', ' . $ip_parts[2]['low'] . ', ' . $ip_parts[2]['high'] . ', ' . $ip_parts[3]['low'] . ', ' . $ip_parts[3]['high'] . ", '', '', 0";
                }
                if (in_array('hostname', $_POST['ban_suggestion']) && !empty($_POST['hostname'])) {
                    if (preg_match("/[^\\w.\\-*]/", $_POST['hostname']) == 1) {
                        fatal_lang_error('invalid_hostname', false);
                    }
                    // Replace the * wildcard by a MySQL wildcard %.
                    $_POST['hostname'] = str_replace('*', '%', $_POST['hostname']);
                    $ban_triggers[] = "0, 0, 0, 0, 0, 0, 0, 0, '" . substr($_POST['hostname'], 0, 255) . "', '', 0";
                }
                if (in_array('email', $_POST['ban_suggestion']) && !empty($_POST['email'])) {
                    if (preg_match("/[^\\w.\\-*@]/", $_POST['email']) == 1) {
                        fatal_lang_error('invalid_email', false);
                    }
                    $_POST['email'] = strtolower(str_replace('*', '%', $_POST['email']));
                    $ban_triggers[] = "0, 0, 0, 0, 0, 0, 0, 0, '', '" . substr($_POST['email'], 0, 255) . "', 0";
                }
                if (in_array('user', $_POST['ban_suggestion']) && (!empty($_POST['bannedUser']) || !empty($_POST['user']))) {
                    // We got a username, let's find its ID.
                    if (empty($_POST['bannedUser'])) {
                        $_POST['user'] = $func['htmlspecialchars']($_POST['user'], ENT_QUOTES);
                        $request = db_query("\n\t\t\t\t\t\t\tSELECT ID_MEMBER, (ID_GROUP = 1 OR FIND_IN_SET(1, additionalGroups)) AS isAdmin\n\t\t\t\t\t\t\tFROM {$db_prefix}members\n\t\t\t\t\t\t\tWHERE memberName = '{$_POST['user']}' OR realName = '{$_POST['user']}'\n\t\t\t\t\t\t\tLIMIT 1", __FILE__, __LINE__);
                        if (mysql_num_rows($request) == 0) {
                            fatal_lang_error('invalid_username', false);
                        }
                        list($_POST['bannedUser'], $isAdmin) = mysql_fetch_row($request);
                        mysql_free_result($request);
                        if ($isAdmin) {
                            fatal_lang_error('no_ban_admin');
                        }
                    }
                    $ban_triggers[] = "0, 0, 0, 0, 0, 0, 0, 0, '', '', " . (int) $_POST['bannedUser'];
                }
                if (!empty($_POST['ban_suggestion']['ips']) && is_array($_POST['ban_suggestion']['ips'])) {
                    $_POST['ban_suggestion']['ips'] = array_unique($_POST['ban_suggestion']['ips']);
                    // Don't add the main IP again.
                    if (in_array('main_ip', $_POST['ban_suggestion'])) {
                        $_POST['ban_suggestion']['ips'] = array_diff($_POST['ban_suggestion']['ips'], array($_POST['main_ip']));
                    }
                    foreach ($_POST['ban_suggestion']['ips'] as $ip) {
                        $ip_parts = ip2range($ip);
                        // They should be alright, but just to be sure...
                        if (count($ip_parts) != 4) {
                            fatal_lang_error('invalid_ip', false);
                        }
                        $ban_triggers[] = $ip_parts[0]['low'] . ', ' . $ip_parts[0]['high'] . ', ' . $ip_parts[1]['low'] . ', ' . $ip_parts[1]['high'] . ', ' . $ip_parts[2]['low'] . ', ' . $ip_parts[2]['high'] . ', ' . $ip_parts[3]['low'] . ', ' . $ip_parts[3]['high'] . ", '', '', 0";
                    }
                }
            }
            // Yes yes, we're ready to add now.
            db_query("\n\t\t\t\tINSERT INTO {$db_prefix}ban_groups\n\t\t\t\t\t(name, ban_time, expire_time, cannot_access, cannot_register, cannot_post, cannot_login, reason, notes)\n\t\t\t\tVALUES\n\t\t\t\t\t(SUBSTRING('{$_POST['ban_name']}', 1, 20), " . time() . ", {$_POST['expiration']}, {$_POST['full_ban']}, {$_POST['cannot_register']}, {$_POST['cannot_post']}, {$_POST['cannot_login']}, SUBSTRING('{$_POST['reason']}', 1, 255), SUBSTRING('{$_POST['notes']}', 1, 65534))", __FILE__, __LINE__);
            $_REQUEST['bg'] = db_insert_id();
            // Now that the ban group is added, add some triggers as well.
            if (!empty($ban_triggers) && !empty($_REQUEST['bg'])) {
                db_query("\n\t\t\t\t\tINSERT INTO {$db_prefix}ban_items\n\t\t\t\t\t\t(ID_BAN_GROUP, ip_low1, ip_high1, ip_low2, ip_high2, ip_low3, ip_high3, ip_low4, ip_high4, hostname, email_address, ID_MEMBER)\n\t\t\t\t\tVALUES ({$_REQUEST['bg']}, " . implode("), ({$_REQUEST['bg']}, ", $ban_triggers) . ')', __FILE__, __LINE__);
            }
        } else {
            db_query("\n\t\t\t\tUPDATE {$db_prefix}ban_groups\n\t\t\t\tSET\n\t\t\t\t\tname = '{$_POST['ban_name']}',\n\t\t\t\t\treason = '{$_POST['reason']}',\n\t\t\t\t\tnotes = '{$_POST['notes']}',\n\t\t\t\t\texpire_time = {$_POST['expiration']},\n\t\t\t\t\tcannot_access = {$_POST['full_ban']},\n\t\t\t\t\tcannot_post = {$_POST['cannot_post']},\n\t\t\t\t\tcannot_register = {$_POST['cannot_register']},\n\t\t\t\t\tcannot_login = {$_POST['cannot_login']}\n\t\t\t\tWHERE ID_BAN_GROUP = {$_REQUEST['bg']}\n\t\t\t\tLIMIT 1", __FILE__, __LINE__);
        }
        // No more caching, we have something new here.
        updateSettings(array('banLastUpdated' => time()));
        updateBanMembers();
    }
    // If we're editing an existing ban, get it from the database.
    if (!empty($_REQUEST['bg'])) {
        $context['ban_items'] = array();
        $request = db_query("\n\t\t\tSELECT\n\t\t\t\tbi.ID_BAN, bi.hostname, bi.email_address, bi.ID_MEMBER, bi.hits,\n\t\t\t\tbi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4,\n\t\t\t\tbg.ID_BAN_GROUP, bg.name, bg.ban_time, bg.expire_time, bg.reason, bg.notes, bg.cannot_access, bg.cannot_register, bg.cannot_login, bg.cannot_post,\n\t\t\t\tIFNULL(mem.ID_MEMBER, 0) AS ID_MEMBER, mem.memberName, mem.realName\n\t\t\tFROM {$db_prefix}ban_groups AS bg\n\t\t\t\tLEFT JOIN {$db_prefix}ban_items AS bi ON (bi.ID_BAN_GROUP = bg.ID_BAN_GROUP)\n\t\t\t\tLEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = bi.ID_MEMBER)\n\t\t\tWHERE bg.ID_BAN_GROUP = {$_REQUEST['bg']}", __FILE__, __LINE__);
        if (mysql_num_rows($request) == 0) {
            fatal_lang_error('ban_not_found', false);
        }
        while ($row = mysql_fetch_assoc($request)) {
            if (!isset($context['ban'])) {
                $context['ban'] = array('id' => $row['ID_BAN_GROUP'], 'name' => $row['name'], 'expiration' => array('status' => $row['expire_time'] === null ? 'never' : ($row['expire_time'] < time() ? 'expired' : 'still_active_but_we_re_counting_the_days'), 'days' => $row['expire_time'] > time() ? floor(($row['expire_time'] - time()) / 86400) : 0), 'reason' => $row['reason'], 'notes' => $row['notes'], 'cannot' => array('access' => !empty($row['cannot_access']), 'post' => !empty($row['cannot_post']), 'register' => !empty($row['cannot_register']), 'login' => !empty($row['cannot_login'])), 'is_new' => false);
            }
            if (!empty($row['ID_BAN'])) {
                $context['ban_items'][$row['ID_BAN']] = array('id' => $row['ID_BAN'], 'hits' => $row['hits']);
                if (!empty($row['ip_high1'])) {
                    $context['ban_items'][$row['ID_BAN']]['type'] = 'ip';
                    $context['ban_items'][$row['ID_BAN']]['ip'] = range2ip(array($row['ip_low1'], $row['ip_low2'], $row['ip_low3'], $row['ip_low4']), array($row['ip_high1'], $row['ip_high2'], $row['ip_high3'], $row['ip_high4']));
                } elseif (!empty($row['hostname'])) {
                    $context['ban_items'][$row['ID_BAN']]['type'] = 'hostname';
                    $context['ban_items'][$row['ID_BAN']]['hostname'] = str_replace('%', '*', $row['hostname']);
                } elseif (!empty($row['email_address'])) {
                    $context['ban_items'][$row['ID_BAN']]['type'] = 'email';
                    $context['ban_items'][$row['ID_BAN']]['email'] = str_replace('%', '*', $row['email_address']);
                } elseif (!empty($row['ID_MEMBER'])) {
                    $context['ban_items'][$row['ID_BAN']]['type'] = 'user';
                    $context['ban_items'][$row['ID_BAN']]['user'] = array('id' => $row['ID_MEMBER'], 'name' => $row['realName'], 'href' => $scripturl . '?action=profile;u=' . $row['ID_MEMBER'], 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['realName'] . '</a>');
                } else {
                    unset($context['ban_items'][$row['ID_BAN']]);
                    db_query("\n\t\t\t\t\t\tDELETE FROM {$db_prefix}ban_items\n\t\t\t\t\t\tWHERE ID_BAN = {$row['ID_BAN']}\n\t\t\t\t\t\tLIMIT 1", __FILE__, __LINE__);
                }
            }
        }
        mysql_free_result($request);
    } else {
        $context['ban'] = array('id' => 0, 'name' => '', 'expiration' => array('status' => 'never', 'days' => 0), 'reason' => '', 'notes' => '', 'ban_days' => 0, 'cannot' => array('access' => true, 'post' => false, 'register' => false, 'login' => false), 'is_new' => true);
        $context['ban_suggestions'] = array('main_ip' => '', 'hostname' => '', 'email' => '', 'member' => array('id' => 0));
        // Overwrite some of the default form values if a user ID was given.
        if (!empty($_REQUEST['u'])) {
            $request = db_query("\n\t\t\t\tSELECT ID_MEMBER, realName, memberIP, emailAddress\n\t\t\t\tFROM {$db_prefix}members\n\t\t\t\tWHERE ID_MEMBER = " . (int) $_REQUEST['u'] . "\n\t\t\t\tLIMIT 1", __FILE__, __LINE__);
            if (mysql_num_rows($request) > 0) {
                list($context['ban_suggestions']['member']['id'], $context['ban_suggestions']['member']['name'], $context['ban_suggestions']['main_ip'], $context['ban_suggestions']['email']) = mysql_fetch_row($request);
            }
            mysql_free_result($request);
            if (!empty($context['ban_suggestions']['member']['id'])) {
                $context['ban_suggestions']['href'] = $scripturl . '?action=profile;u=' . $context['ban_suggestions']['member']['id'];
                $context['ban_suggestions']['member']['link'] = '<a href="' . $context['ban_suggestions']['href'] . '">' . $context['ban_suggestions']['member']['name'] . '</a>';
                // Default the ban name to the name of the banned member.
                $context['ban']['name'] = $context['ban_suggestions']['member']['name'];
                // 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'])) {
                    $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 = db_query("\n\t\t\t\t\tSELECT DISTINCT posterIP\n\t\t\t\t\tFROM {$db_prefix}messages\n\t\t\t\t\tWHERE ID_MEMBER = " . (int) $_REQUEST['u'] . "\n\t\t\t\t\t\tAND posterIP RLIKE '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\$'\n\t\t\t\t\tORDER BY posterIP", __FILE__, __LINE__);
                while ($row = mysql_fetch_assoc($request)) {
                    $context['ban_suggestions']['message_ips'][] = $row['posterIP'];
                }
                mysql_free_result($request);
                $context['ban_suggestions']['error_ips'] = array();
                $request = db_query("\n\t\t\t\t\tSELECT DISTINCT ip\n\t\t\t\t\tFROM {$db_prefix}log_errors\n\t\t\t\t\tWHERE ID_MEMBER = " . (int) $_REQUEST['u'] . "\n\t\t\t\t\t\tAND ip RLIKE '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\$'\n\t\t\t\t\tORDER BY ip", __FILE__, __LINE__);
                while ($row = mysql_fetch_assoc($request)) {
                    $context['ban_suggestions']['error_ips'][] = $row['ip'];
                }
                mysql_free_result($request);
                // Borrowing a few language strings from profile.
                loadLanguage('Profile');
            }
        }
    }
    $context['sub_template'] = 'ban_edit';
}
 /**
  * Here we can add, and edit, spider info!
  */
 public function action_editspiders()
 {
     global $context, $txt;
     // Some standard stuff.
     $context['id_spider'] = !empty($_GET['sid']) ? (int) $_GET['sid'] : 0;
     $context['page_title'] = $context['id_spider'] ? $txt['spiders_edit'] : $txt['spiders_add'];
     $context['sub_template'] = 'spider_edit';
     require_once SUBSDIR . '/SearchEngines.subs.php';
     // Are we saving?
     if (!empty($_POST['save'])) {
         checkSession();
         validateToken('admin-ses');
         // Check the IP range is valid.
         $ips = array();
         $ip_sets = explode(',', $_POST['spider_ip']);
         foreach ($ip_sets as $set) {
             $test = ip2range(trim($set));
             if (!empty($test)) {
                 $ips[] = $set;
             }
         }
         $ips = implode(',', $ips);
         // Goes in as it is...
         updateSpider($context['id_spider'], $_POST['spider_name'], $_POST['spider_agent'], $ips);
         // Order by user agent length.
         sortSpiderTable();
         cache_put_data('spider_search', null, 300);
         recacheSpiderNames();
         redirectexit('action=admin;area=sengines;sa=spiders');
     }
     // The default is new.
     $context['spider'] = array('id' => 0, 'name' => '', 'agent' => '', 'ip_info' => '');
     // An edit?
     if ($context['id_spider']) {
         $context['spider'] = getSpiderDetails($context['id_spider']);
     }
     createToken('admin-ses');
 }
Example #8
0
function EditSpider()
{
    global $context, $txt;
    // Some standard stuff.
    $context['id_spider'] = !empty($_GET['sid']) ? (int) $_GET['sid'] : 0;
    $context['page_title'] = $context['id_spider'] ? $txt['spiders_edit'] : $txt['spiders_add'];
    $context['sub_template'] = 'spider_edit';
    // Are we saving?
    if (!empty($_POST['save'])) {
        checkSession();
        $ips = array();
        // Check the IP range is valid.
        $ip_sets = explode(',', $_POST['spider_ip']);
        foreach ($ip_sets as $set) {
            $test = ip2range(trim($set));
            if (!empty($test)) {
                $ips[] = $set;
            }
        }
        $ips = implode(',', $ips);
        // Goes in as it is...
        if ($context['id_spider']) {
            smf_db_query('
				UPDATE {db_prefix}spiders
				SET spider_name = {string:spider_name}, user_agent = {string:spider_agent},
					ip_info = {string:ip_info}
				WHERE id_spider = {int:current_spider}', array('current_spider' => $context['id_spider'], 'spider_name' => $_POST['spider_name'], 'spider_agent' => $_POST['spider_agent'], 'ip_info' => $ips));
        } else {
            smf_db_insert('insert', '{db_prefix}spiders', array('spider_name' => 'string', 'user_agent' => 'string', 'ip_info' => 'string'), array($_POST['spider_name'], $_POST['spider_agent'], $ips), array('id_spider'));
        }
        // Order by user agent length.
        sortSpiderTable();
        CacheAPI::putCache('spider_search', null, 300);
        recacheSpiderNames();
        redirectexit('action=admin;area=sengines;sa=spiders');
    }
    // The default is new.
    $context['spider'] = array('id' => 0, 'name' => '', 'agent' => '', 'ip_info' => '');
    // An edit?
    if ($context['id_spider']) {
        $request = smf_db_query('
			SELECT id_spider, spider_name, user_agent, ip_info
			FROM {db_prefix}spiders
			WHERE id_spider = {int:current_spider}', array('current_spider' => $context['id_spider']));
        if ($row = mysql_fetch_assoc($request)) {
            $context['spider'] = array('id' => $row['id_spider'], 'name' => $row['spider_name'], 'agent' => $row['user_agent'], 'ip_info' => $row['ip_info']);
        }
        mysql_free_result($request);
    }
}