/** * 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; }
function writeLog($force = false) { global $user_info, $user_settings, $context, $modSettings, $settings, $topic, $board, $smcFunc, $sourcedir; // If we are showing who is viewing a topic, let's see if we are, and force an update if so - to make it accurate. if (!empty($settings['display_who_viewing']) && ($topic || $board)) { // Take the opposite approach! $force = true; // Don't update for every page - this isn't wholly accurate but who cares. if ($topic) { if (isset($_SESSION['last_topic_id']) && $_SESSION['last_topic_id'] == $topic) { $force = false; } $_SESSION['last_topic_id'] = $topic; } } // Are they a spider we should be tracking? Mode = 1 gets tracked on its spider check... if (!empty($user_info['possibly_robot']) && !empty($modSettings['spider_mode']) && $modSettings['spider_mode'] > 1) { require_once $sourcedir . '/ManageSearchEngines.php'; logSpider(); } // Don't mark them as online more than every so often. if (!empty($_SESSION['log_time']) && $_SESSION['log_time'] >= time() - 8 && !$force) { return; } if (!empty($modSettings['who_enabled'])) { $serialized = $_GET + array('USER_AGENT' => $_SERVER['HTTP_USER_AGENT']); // In the case of a dlattach action, session_var may not be set. if (!isset($context['session_var'])) { $context['session_var'] = $_SESSION['session_var']; } unset($serialized['sesc'], $serialized[$context['session_var']]); $serialized = serialize($serialized); } else { $serialized = ''; } // Guests use 0, members use their session ID. $session_id = $user_info['is_guest'] ? 'ip' . $user_info['ip'] : session_id(); // Grab the last all-of-SMF-specific log_online deletion time. $do_delete = cache_get_data('log_online-update', 30) < time() - 30; // If the last click wasn't a long time ago, and there was a last click... if (!empty($_SESSION['log_time']) && $_SESSION['log_time'] >= time() - $modSettings['lastActive'] * 20) { if ($do_delete) { $smcFunc['db_query']('delete_log_online_interval', ' DELETE FROM {db_prefix}log_online WHERE log_time < {int:log_time} AND session != {string:session}', array('log_time' => time() - $modSettings['lastActive'] * 60, 'session' => $session_id)); // Cache when we did it last. cache_put_data('log_online-update', time(), 30); } $smcFunc['db_query']('', ' UPDATE {db_prefix}log_online SET log_time = {int:log_time}, ip = IFNULL(INET_ATON({string:ip}), 0), url = {string:url} WHERE session = {string:session}', array('log_time' => time(), 'ip' => $user_info['ip'], 'url' => $serialized, 'session' => $session_id)); // Guess it got deleted. if ($smcFunc['db_affected_rows']() == 0) { $_SESSION['log_time'] = 0; } } else { $_SESSION['log_time'] = 0; } // Otherwise, we have to delete and insert. if (empty($_SESSION['log_time'])) { if ($do_delete || !empty($user_info['id'])) { $smcFunc['db_query']('', ' DELETE FROM {db_prefix}log_online WHERE ' . ($do_delete ? 'log_time < {int:log_time}' : '') . ($do_delete && !empty($user_info['id']) ? ' OR ' : '') . (empty($user_info['id']) ? '' : 'id_member = {int:current_member}'), array('current_member' => $user_info['id'], 'log_time' => time() - $modSettings['lastActive'] * 60)); } $smcFunc['db_insert']($do_delete ? 'ignore' : 'replace', '{db_prefix}log_online', array('session' => 'string', 'id_member' => 'int', 'id_spider' => 'int', 'log_time' => 'int', 'ip' => 'raw', 'url' => 'string'), array($session_id, $user_info['id'], empty($_SESSION['id_robot']) ? 0 : $_SESSION['id_robot'], time(), 'IFNULL(INET_ATON(\'' . $user_info['ip'] . '\'), 0)', $serialized), array('session')); } // Mark your session as being logged. $_SESSION['log_time'] = time(); // Well, they are online now. if (empty($_SESSION['timeOnlineUpdated'])) { $_SESSION['timeOnlineUpdated'] = time(); } // Set their login time, if not already done within the last minute. if (SMF != 'SSI' && !empty($user_info['last_login']) && $user_info['last_login'] < time() - 60) { // Don't count longer than 15 minutes. if (time() - $_SESSION['timeOnlineUpdated'] > 60 * 15) { $_SESSION['timeOnlineUpdated'] = time(); } $user_settings['total_time_logged_in'] += time() - $_SESSION['timeOnlineUpdated']; updateMemberData($user_info['id'], array('last_login' => time(), 'member_ip' => $user_info['ip'], 'member_ip2' => $_SERVER['BAN_CHECK_IP'], 'total_time_logged_in' => $user_settings['total_time_logged_in'])); if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2) { cache_put_data('user_settings-' . $user_info['id'], $user_settings, 60); } $user_info['total_time_logged_in'] += time() - $_SESSION['timeOnlineUpdated']; $_SESSION['timeOnlineUpdated'] = time(); } }
/** * 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; }