<?php
/* ====================
[BEGIN_COT_EXT]
Hooks=header.main
Tags=header.tpl:{HEADER_WHOSONLINE}
[END_COT_EXT]
==================== */

/**
 * Header part
 *
 * @package WhosOnline
 * @copyright (c) Cotonti Team
 * @license https://github.com/Cotonti/Cotonti/blob/master/License.txt
 */

defined('COT_CODE') or die('Wrong URL');

if (!defined('WHOSONLINE_UPDATED'))
{
  require_once cot_incfile('whosonline', 'plug');
  global $db_online;

	if ($usr['id'] > 0)
	{
    $whsupd = (empty($sys['online_location']) ? array(
							'online_ip' => $usr['ip'],
							'online_name' => $usr['name'],
							'online_lastseen' => (int)$sys['now'],
							'online_location' => $env['location'],
							'online_subloc' => (string) $sys['sublocation'],
							'online_userid' => (int)$usr['id'],
							'online_shield' => 0,
							'online_hammer' => 0
  			 ) : array(
  						'online_lastseen' => $sys['now'],
  						'online_location' => $env['location'],
  						'online_subloc' => (string) $sys['sublocation'],
  						'online_hammer' => (int)$sys['online_hammer']
  			 ));

		if ($db->query("SELECT COUNT(*) FROM $db_online WHERE online_userid=".$usr['id'].' LIMIT 1')->fetchColumn() > 0)
		{
      $db->update($db_online, $whsupd, "online_userid=".$usr['id']);
		}
		else
		{
      $db->insert($db_online, $whsupd);
		}
	}
	elseif(!$cfg['plugin']['whosonline']['disable_guests'])
	{
    $whsupd = (empty($sys['online_location']) ? array(
							'online_ip' => $usr['ip'],
							'online_name' => 'v',
							'online_lastseen' => (int)$sys['now'],
							'online_location' => $env['location'],
							'online_subloc' => (string) $sys['sublocation'],
							'online_userid' => -1,
							'online_shield' => 0,
							'online_hammer' => 0
  			 ) : array(
							'online_lastseen' => $sys['now'],
							'online_location' => $env['location'],
							'online_subloc' => (string)$sys['sublocation'],
							'online_hammer' => (int)$sys['online_hammer']
  			 ));

		if ($db->query("SELECT COUNT(*) FROM $db_online WHERE online_ip='".$usr['ip']."' AND online_userid < 0 LIMIT 1")->fetchColumn() > 0)
		{
      $db->update($db_online, $whsupd, "online_ip='".$usr['ip']."' AND online_userid < 0");
		}
		else
		{
      $db->insert($db_online, $whsupd);
		}
	}

  define('WHOSONLINE_UPDATED', true);
}

$t->assign(array(
  'HEADER_WHOSONLINE' => $out['whosonline'],
  'HEADER_WHOSONLINE_REG_COUNT' => $sys['whosonline_reg_count'],
  'HEADER_WHOSONLINE_ALL_COUNT' => $sys['whosonline_all_count']
));
