1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264:
<?php
if (!defined('SMF'))
die('No direct access...');
function getMembersOnlineStats($membersOnlineOptions)
{
global $smcFunc, $scripturl, $user_info, $modSettings, $txt;
$allowed_sort_options = array(
'',
'log_time',
'real_name',
'show_online',
'online_color',
'group_name',
);
if (!isset($membersOnlineOptions['sort']))
{
$membersOnlineOptions['sort'] = 'log_time';
$membersOnlineOptions['reverse_sort'] = true;
}
elseif (!in_array($membersOnlineOptions['sort'], $allowed_sort_options))
trigger_error('Sort method for getMembersOnlineStats() function is not allowed', E_USER_NOTICE);
$membersOnlineStats = array(
'users_online' => array(),
'list_users_online' => array(),
'online_groups' => array(),
'num_guests' => 0,
'num_spiders' => 0,
'num_buddies' => 0,
'num_users_hidden' => 0,
'num_users_online' => 0,
);
$spiders = array();
$spider_finds = array();
if (!empty($modSettings['show_spider_online']) && ($modSettings['show_spider_online'] < 3 || allowedTo('admin_forum')) && !empty($modSettings['spider_name_cache']))
$spiders = $smcFunc['json_decode']($modSettings['spider_name_cache'], true);
$request = $smcFunc['db_query']('', '
SELECT
lo.id_member, lo.log_time, lo.id_spider, mem.real_name, mem.member_name, mem.show_online,
mg.online_color, mg.id_group, mg.group_name, mg.hidden, mg.group_type, mg.id_parent
FROM {db_prefix}log_online AS lo
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lo.id_member)
LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:reg_mem_group} THEN mem.id_post_group ELSE mem.id_group END)',
array(
'reg_mem_group' => 0,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if (empty($row['real_name']))
{
if ($row['id_spider'] && isset($spiders[$row['id_spider']]))
{
$spider_finds[$row['id_spider']] = isset($spider_finds[$row['id_spider']]) ? $spider_finds[$row['id_spider']] + 1 : 1;
$membersOnlineStats['num_spiders']++;
}
$membersOnlineStats['num_guests']++;
continue;
}
elseif (empty($row['show_online']) && empty($membersOnlineOptions['show_hidden']))
{
$membersOnlineStats['num_users_hidden']++;
continue;
}
if (!empty($row['online_color']))
$link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '" style="color: ' . $row['online_color'] . ';">' . $row['real_name'] . '</a>';
else
$link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>';
$is_buddy = in_array($row['id_member'], $user_info['buddies']);
if ($is_buddy)
{
$membersOnlineStats['num_buddies']++;
$link = '<strong>' . $link . '</strong>';
}
$membersOnlineStats['users_online'][$row[$membersOnlineOptions['sort']] . '_' . $row['member_name']] = array(
'id' => $row['id_member'],
'username' => $row['member_name'],
'name' => $row['real_name'],
'group' => $row['id_group'],
'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
'link' => $link,
'is_buddy' => $is_buddy,
'hidden' => empty($row['show_online']),
'is_last' => false,
);
$membersOnlineStats['list_users_online'][$row[$membersOnlineOptions['sort']] . '_' . $row['member_name']] = empty($row['show_online']) ? '<em>' . $link . '</em>' : $link;
if (!isset($membersOnlineStats['online_groups'][$row['id_group']]))
$membersOnlineStats['online_groups'][$row['id_group']] = array(
'id' => $row['id_group'],
'name' => $row['group_name'],
'color' => $row['online_color'],
'hidden' => $row['hidden'],
'type' => $row['group_type'],
'parent' => $row['id_parent'],
);
}
$smcFunc['db_free_result']($request);
if (!empty($spider_finds) && $modSettings['show_spider_online'] > 1)
{
$sort = $membersOnlineOptions['sort'] === 'log_time' && $membersOnlineOptions['reverse_sort'] ? 0 : 'zzz_';
foreach ($spider_finds as $id => $count)
{
$link = $spiders[$id] . ($count > 1 ? ' (' . $count . ')' : '');
$membersOnlineStats['users_online'][$sort . '_' . $spiders[$id]] = array(
'id' => 0,
'username' => $spiders[$id],
'name' => $link,
'group' => $txt['spiders'],
'href' => '',
'link' => $link,
'is_buddy' => false,
'hidden' => false,
'is_last' => false,
);
$membersOnlineStats['list_users_online'][$sort . '_' . $spiders[$id]] = $link;
}
}
if (!empty($membersOnlineStats['users_online']))
{
$sortFunction = empty($membersOnlineOptions['reverse_sort']) ? 'ksort' : 'krsort';
$sortFunction($membersOnlineStats['users_online']);
$sortFunction($membersOnlineStats['list_users_online']);
$userKeys = array_keys($membersOnlineStats['users_online']);
$membersOnlineStats['users_online'][end($userKeys)]['is_last'] = true;
}
ksort($membersOnlineStats['online_groups']);
$membersOnlineStats['num_users_online'] = count($membersOnlineStats['users_online']) + $membersOnlineStats['num_users_hidden'] - (isset($modSettings['show_spider_online']) && $modSettings['show_spider_online'] > 1 ? count($spider_finds) : 0);
return $membersOnlineStats;
}
function trackStatsUsersOnline($total_users_online)
{
global $modSettings, $smcFunc;
$settingsToUpdate = array();
if (!isset($modSettings['mostOnline']) || $total_users_online >= $modSettings['mostOnline'])
$settingsToUpdate = array(
'mostOnline' => $total_users_online,
'mostDate' => time()
);
$date = strftime('%Y-%m-%d', forum_time(false));
if (!isset($modSettings['mostOnlineUpdated']) || $modSettings['mostOnlineUpdated'] != $date)
{
$request = $smcFunc['db_query']('', '
SELECT most_on
FROM {db_prefix}log_activity
WHERE date = {date:date}
LIMIT 1',
array(
'date' => $date,
)
);
if ($smcFunc['db_num_rows']($request) === 0)
{
$smcFunc['db_insert']('ignore',
'{db_prefix}log_activity',
array('date' => 'date', 'most_on' => 'int'),
array($date, $total_users_online),
array('date')
);
}
else
{
list ($modSettings['mostOnlineToday']) = $smcFunc['db_fetch_row']($request);
if ($total_users_online > $modSettings['mostOnlineToday'])
trackStats(array('most_on' => $total_users_online));
$total_users_online = max($total_users_online, $modSettings['mostOnlineToday']);
}
$smcFunc['db_free_result']($request);
$settingsToUpdate['mostOnlineUpdated'] = $date;
$settingsToUpdate['mostOnlineToday'] = $total_users_online;
}
elseif ($total_users_online > $modSettings['mostOnlineToday'])
{
trackStats(array('most_on' => $total_users_online));
$settingsToUpdate['mostOnlineToday'] = $total_users_online;
}
if (!empty($settingsToUpdate))
updateSettings($settingsToUpdate);
}
?>