Skip to content

Commit

Permalink
Online update optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
CaMer0n committed Feb 7, 2017
1 parent 423ba61 commit d5711fd
Showing 1 changed file with 85 additions and 19 deletions.
104 changes: 85 additions & 19 deletions e107_handlers/online_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,30 +81,42 @@ public function goOnline($online_tracking = false, $flood_control = false)
//global $members_online, $total_online; // Not needed as globals
global $listuserson; // FIXME - remove it, make it property, call e_online signleton - e107::getOnline()

$e107 = e107::getInstance();
if($online_tracking === false && $flood_control === false)
{
define('e_TRACKING_DISABLED', true); // Used in forum, online menu
define('TOTAL_ONLINE', '');
define('MEMBERS_ONLINE', '');
define('GUESTS_ONLINE', '');
define('ON_PAGE', '');
define('MEMBER_LIST', '');

return null;
}

$sql = e107::getDb();
$user = e107::getUser();

if($online_tracking || $flood_control)
{
$online_timeout = 300;
$online_timeout = 300;

list($ban_access_guest,$ban_access_member) = explode(',',e107::getPref('ban_max_online_access', '100,200'));
$online_bancount = max($ban_access_guest,50); // Safety net for incorrect values
if ($user->isUser())
{
$online_bancount = max($online_bancount,$ban_access_member);
}
list($ban_access_guest,$ban_access_member) = explode(',',e107::getPref('ban_max_online_access', '100,200'));
$online_bancount = max($ban_access_guest,50); // Safety net for incorrect values
if ($user->isUser())
{
$online_bancount = max($online_bancount,$ban_access_member);
}

$online_warncount = $online_bancount * 0.9; // Set warning threshold at 90% of ban threshold
$online_warncount = $online_bancount * 0.9; // Set warning threshold at 90% of ban threshold
//TODO Add support for all queries.
// $page = (strpos(e_SELF, 'forum_') !== FALSE) ? e_SELF.'.'.e_QUERY : e_SELF;
// $page = (strpos(e_SELF, 'comment') !== FALSE) ? e_SELF.'.'.e_QUERY : $page;
// $page = (strpos(e_SELF, 'content') !== FALSE) ? e_SELF.'.'.e_QUERY : $page;
$page = e_REQUEST_URI; // mod rewrite & single entry support
// FIXME parse url, trigger registered e_online callbacks
$page = e107::getParser()->toDB($page, true); /// @todo - try not to use toDB() - triggers prefilter
// $page = e107::getParser()->toDB($page, true); /// @todo - try not to use toDB() - triggers prefilter

$page = filter_var($page,FILTER_SANITIZE_URL);
$ip = e107::getIPHandler()->getIP(FALSE);

$udata = ($user->isUser() && USER ? $user->getId().'.'.$user->getName() : '0'); // USER check required to make sure they logged in without an error.
$agent = $_SERVER['HTTP_USER_AGENT'];

Expand All @@ -130,7 +142,7 @@ public function goOnline($online_tracking = false, $flood_control = false)
if ($user->isUser() && !$user->getParentId())
{
// Find record that matches IP or visitor, or matches user info
if ($sql->select('online', '*', "(`online_ip` = '{$ip}' AND `online_user_id` = '0') OR `online_user_id` = '{$udata}'"))
if ($sql->select('online', '*', "(`online_ip` = '{$ip}' AND `online_user_id` = '0') OR `online_user_id` = '{$udata}' LIMIT 1"))
{
$row = $sql->fetch();

Expand All @@ -141,7 +153,17 @@ public function goOnline($online_tracking = false, $flood_control = false)
{
//It has been at least 'online_timeout' seconds since this user's info last logged
//Update user record with timestamp, current IP, current page and set pagecount to 1
$query = "online_timestamp='".time()."', online_ip='{$ip}'{$update_page}, online_pagecount=1, `online_active` = 1 WHERE online_user_id='{$row['online_user_id']}'";
// $query = "online_timestamp='".time()."', online_ip='{$ip}'{$update_page}, online_pagecount=1, `online_active` = 1 WHERE online_user_id='{$row['online_user_id']}'";

$query = array(
'online_timestamp' => time(),
'online_ip' => $ip,
'online_pagecount' => 1,
'online_active' => 1,
'WHERE' => "online_user_id=".intval($row['online_user_id'])." LIMIT 1"
);


}
else
{
Expand All @@ -150,7 +172,17 @@ public function goOnline($online_tracking = false, $flood_control = false)
$row['online_pagecount'] ++;
}
// Update user record with current IP, current page and increment pagecount
$query = "online_ip='{$ip}'{$update_page}, `online_pagecount` = '".intval($row['online_pagecount'])."', `online_active` = 1 WHERE `online_user_id` = '{$row['online_user_id']}'";
// $query = "online_ip='{$ip}'{$update_page}, `online_pagecount` = '".intval($row['online_pagecount'])."', `online_active` = 1 WHERE `online_user_id` = '{$row['online_user_id']}'";


$query = array(

'online_ip' => $ip,
'online_pagecount' => intval($row['online_pagecount']),
'online_active' => 1,
'WHERE' => "online_user_id=".intval($row['online_user_id'])." LIMIT 1"
);

}
}
else
Expand All @@ -160,7 +192,17 @@ public function goOnline($online_tracking = false, $flood_control = false)
{
// It has been at least 'timeout' seconds since this user has connected
// Update record with timestamp, current IP, current page and set pagecount to 1
$query = "`online_timestamp` = '".time()."', `online_user_id` = '{$udata}'{$update_page}, `online_pagecount` = 1, `online_active` = 1 WHERE `online_ip` = '{$ip}' AND `online_user_id` = '0'";
// $query = "`online_timestamp` = '".time()."', `online_user_id` = '{$udata}'{$update_page}, `online_pagecount` = 1, `online_active` = 1 WHERE `online_ip` = '{$ip}' AND `online_user_id` = '0'";

$query = array(
'online_timestamp' => time(),
'online_user_id' => $udata,
'online_pagecount' => 1,
'online_active' => 1,
'WHERE' => "online_ip = '".$ip."' AND online_user_id = '0' LIMIT 1"
);


}
else
{ // Another visit within the timeout period
Expand All @@ -170,9 +212,27 @@ public function goOnline($online_tracking = false, $flood_control = false)
}
//Update record with current IP, current page and increment pagecount
$query = "`online_user_id` = '{$udata}'{$update_page}, `online_pagecount` = ".intval($row['online_pagecount']).", `online_active` =1 WHERE `online_ip` = '{$ip}' AND `online_user_id` = '0'";

$query = array(
// 'online_timestamp' => time(),
'online_user_id' => $udata,
'online_pagecount' => intval($row['online_pagecount']),
'online_active' => 1,
'WHERE' => "online_ip = '".$ip."' AND online_user_id = '0' LIMIT 1"
);

}
}


if(!empty($update_page))
{
$query['online_location'] = $page;
}

$sql->update('online', $query);


}
else
{
Expand Down Expand Up @@ -245,13 +305,18 @@ public function goOnline($online_tracking = false, $flood_control = false)

// FIXME - don't use constants below, save data in class vars, call e_online signleton - e107::getOnline()
// $total_online = $sql->db_Count('online'); // 1 less query! :-)
if ($total_online = $sql->gen('SELECT o.*,u.user_image FROM #online AS o LEFT JOIN #user AS u ON o.online_user_id = u.user_id WHERE o.online_pagecount > 0 ORDER BY o.online_timestamp DESC'))
if ($total_online = $sql->gen('SELECT o.*,u.user_image FROM `#online` AS o LEFT JOIN `#user` AS u ON o.online_user_id = u.user_id WHERE o.online_pagecount > 0 ORDER BY o.online_timestamp DESC'))
// if ($total_online = $sql->gen('SELECT o FROM `#online` WHERE o.online_pagecount > 0 ORDER BY o.online_timestamp DESC'))
{
$member_list = '';
$members_online = 0;
$listuserson = array();

while ($row = $sql->fetch())
{



$row['online_bot'] = $this->isBot($row['online_agent']);

// Sort into usable format and add bot field.
Expand Down Expand Up @@ -300,6 +365,7 @@ public function goOnline($online_tracking = false, $flood_control = false)
//update most ever online
$olCountPrefs = e107::getConfig('history'); // Get historic counts of members on line
$olCountPrefs->setParam('nologs', true);

if ($total_online > ($olCountPrefs->get('most_members_online') + $olCountPrefs->get('most_guests_online')))
{
$olCountPrefs->set('most_members_online', MEMBERS_ONLINE);
Expand All @@ -308,7 +374,7 @@ public function goOnline($online_tracking = false, $flood_control = false)
$olCountPrefs->save(false, true, false);
}
}
}
/*}
else
{
define('e_TRACKING_DISABLED', true); // Used in forum, online menu
Expand All @@ -317,7 +383,7 @@ public function goOnline($online_tracking = false, $flood_control = false)
define('GUESTS_ONLINE', '');
define('ON_PAGE', '');
define('MEMBER_LIST', '');
}
}*/
}


Expand Down

0 comments on commit d5711fd

Please sign in to comment.