/** * Updates visit level information such as date last active and the user's ip address. * * @param int $UserID * @param string|int|float $ClientHour */ function UpdateVisit($UserID, $ClientHour = FALSE) { $UserID = (int) $UserID; if (!$UserID) { throw new Exception('A valid User ID is required.'); } $User = Gdn::UserModel()->GetID($UserID, DATASET_TYPE_ARRAY); $Fields = array(); if (Gdn_Format::ToTimestamp($User['DateLastActive']) < strtotime('5 minutes ago')) { // We only update the last active date once every 5 minutes to cut down on DB activity. $Fields['DateLastActive'] = Gdn_Format::ToDateTime(); } // Update session level information if necessary. if ($UserID == Gdn::Session()->UserID) { $IP = Gdn::Request()->IpAddress(); $Fields['LastIPAddress'] = $IP; if (Gdn::Session()->NewVisit()) { $Fields['CountVisits'] = GetValue('CountVisits', $User, 0) + 1; } } // Generate the AllIPs field. $AllIPs = GetValue('AllIPAddresses', $User, array()); if (is_string($AllIPs)) { $AllIPs = explode(',', $AllIPs); SetValue('AllIPAddresses', $User, $AllIPs); } if (!is_array($AllIPs)) { $AllIPs = array(); } if ($IP = GetValue('InsertIPAddress', $User)) { $AllIPs[] = ForceIPv4($IP); } if ($IP = GetValue('LastIPAddress', $User)) { $AllIPs[] = $IP; } $AllIPs = array_unique($AllIPs); sort($AllIPs); $Fields['AllIPAddresses'] = $AllIPs; // Set the hour offset based on the client's clock. if (is_numeric($ClientHour) && $ClientHour >= 0 && $ClientHour < 24) { $HourOffset = $ClientHour - date('G', time()); $Fields['HourOffset'] = $HourOffset; } // See if the fields have changed. $Set = array(); foreach ($Fields as $Name => $Value) { if (GetValue($Name, $User) != $Value) { $Set[$Name] = $Value; } } if (!empty($Set)) { $this->EventArguments['Fields'] =& $Set; $this->FireEvent('UpdateVisit'); $this->SetField($UserID, $Set); } if ($User['LastIPAddress'] != $Fields['LastIPAddress']) { $User = $this->GetID($UserID, DATASET_TYPE_ARRAY); if (!BanModel::CheckUser($User, NULL, TRUE, $Bans)) { $BanModel = new BanModel(); $Ban = array_pop($Bans); $BanModel->SaveUser($User, TRUE, $Ban); $BanModel->SetCounts($Ban); } } }