Advertisement
Brettflan

Sample SMF code

Sep 28th, 2017
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.88 KB | None | 0 0
  1. // Put this user in the online log.
  2. function writeLog($force = false)
  3. {
  4.     global $user_info, $user_settings, $context, $modSettings, $settings, $topic, $board, $smcFunc, $sourcedir;
  5.  
  6.     // 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.
  7.     if (!empty($settings['display_who_viewing']) && ($topic || $board))
  8.     {
  9.         // Take the opposite approach!
  10.         $force = true;
  11.         // Don't update for every page - this isn't wholly accurate but who cares.
  12.         if ($topic)
  13.         {
  14.             if (isset($_SESSION['last_topic_id']) && $_SESSION['last_topic_id'] == $topic)
  15.                 $force = false;
  16.             $_SESSION['last_topic_id'] = $topic;
  17.         }
  18.     }
  19.  
  20.     // Are they a spider we should be tracking? Mode = 1 gets tracked on its spider check...
  21.     if (!empty($user_info['possibly_robot']) && !empty($modSettings['spider_mode']) && $modSettings['spider_mode'] > 1)
  22.     {
  23.         require_once($sourcedir . '/ManageSearchEngines.php');
  24.         logSpider();
  25.     }
  26.  
  27.     // Don't mark them as online more than every so often.
  28.     if (!empty($_SESSION['log_time']) && $_SESSION['log_time'] >= (time() - 8) && !$force)
  29.         return;
  30.  
  31.     if (!empty($modSettings['who_enabled']))
  32.     {
  33.         $serialized = $_GET + array('USER_AGENT' => $_SERVER['HTTP_USER_AGENT']);
  34.  
  35.         // In the case of a dlattach action, session_var may not be set.
  36.         if (!isset($context['session_var']))
  37.             $context['session_var'] = $_SESSION['session_var'];
  38.  
  39.         unset($serialized['sesc'], $serialized[$context['session_var']]);
  40.         $serialized = serialize($serialized);
  41.     }
  42.     else
  43.         $serialized = '';
  44.  
  45.     // Guests use 0, members use their session ID.
  46.     $session_id = $user_info['is_guest'] ? 'ip' . $user_info['ip'] : session_id();
  47.  
  48.     // Grab the last all-of-SMF-specific log_online deletion time.
  49.     $do_delete = cache_get_data('log_online-update', 30) < time() - 30;
  50.  
  51.     // If the last click wasn't a long time ago, and there was a last click...
  52.     if (!empty($_SESSION['log_time']) && $_SESSION['log_time'] >= time() - $modSettings['lastActive'] * 20)
  53.     {
  54.         if ($do_delete)
  55.         {
  56.             $smcFunc['db_query']('delete_log_online_interval', '
  57.                 DELETE FROM {db_prefix}log_online
  58.                 WHERE log_time < {int:log_time}
  59.                     AND session != {string:session}',
  60.                 array(
  61.                     'log_time' => time() - $modSettings['lastActive'] * 60,
  62.                     'session' => $session_id,
  63.                 )
  64.             );
  65.  
  66.             // Cache when we did it last.
  67.             cache_put_data('log_online-update', time(), 30);
  68.         }
  69.  
  70.         $smcFunc['db_query']('', '
  71.             UPDATE {db_prefix}log_online
  72.             SET log_time = {int:log_time}, ip = IFNULL(INET_ATON({string:ip}), 0), url = {string:url}
  73.             WHERE session = {string:session}',
  74.             array(
  75.                 'log_time' => time(),
  76.                 'ip' => $user_info['ip'],
  77.                 'url' => $serialized,
  78.                 'session' => $session_id,
  79.             )
  80.         );
  81.  
  82.         // Guess it got deleted.
  83.         if ($smcFunc['db_affected_rows']() == 0)
  84.             $_SESSION['log_time'] = 0;
  85.     }
  86.     else
  87.         $_SESSION['log_time'] = 0;
  88.  
  89.     // Otherwise, we have to delete and insert.
  90.     if (empty($_SESSION['log_time']))
  91.     {
  92.         if ($do_delete || !empty($user_info['id']))
  93.             $smcFunc['db_query']('', '
  94.                 DELETE FROM {db_prefix}log_online
  95.                 WHERE ' . ($do_delete ? 'log_time < {int:log_time}' : '') . ($do_delete && !empty($user_info['id']) ? ' OR ' : '') . (empty($user_info['id']) ? '' : 'id_member = {int:current_member}'),
  96.                 array(
  97.                     'current_member' => $user_info['id'],
  98.                     'log_time' => time() - $modSettings['lastActive'] * 60,
  99.                 )
  100.             );
  101.  
  102.         $smcFunc['db_insert']($do_delete ? 'ignore' : 'replace',
  103.             '{db_prefix}log_online',
  104.             array('session' => 'string', 'id_member' => 'int', 'id_spider' => 'int', 'log_time' => 'int', 'ip' => 'raw', 'url' => 'string'),
  105.             array($session_id, $user_info['id'], empty($_SESSION['id_robot']) ? 0 : $_SESSION['id_robot'], time(), 'IFNULL(INET_ATON(\'' . $user_info['ip'] . '\'), 0)', $serialized),
  106.             array('session')
  107.         );
  108.     }
  109.  
  110.     // Mark your session as being logged.
  111.     $_SESSION['log_time'] = time();
  112.  
  113.     // Well, they are online now.
  114.     if (empty($_SESSION['timeOnlineUpdated']))
  115.         $_SESSION['timeOnlineUpdated'] = time();
  116.  
  117.     // Set their login time, if not already done within the last minute.
  118.     if (SMF != 'SSI' && !empty($user_info['last_login']) && $user_info['last_login'] < time() - 60)
  119.     {
  120.         // Don't count longer than 15 minutes.
  121.         if (time() - $_SESSION['timeOnlineUpdated'] > 60 * 15)
  122.             $_SESSION['timeOnlineUpdated'] = time();
  123.  
  124.         $user_settings['total_time_logged_in'] += time() - $_SESSION['timeOnlineUpdated'];
  125.         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']));
  126.  
  127.         if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2)
  128.             cache_put_data('user_settings-' . $user_info['id'], $user_settings, 60);
  129.  
  130.         $user_info['total_time_logged_in'] += time() - $_SESSION['timeOnlineUpdated'];
  131.         $_SESSION['timeOnlineUpdated'] = time();
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement