/** * Update ONLINE_USERS collection * @todo exit if useragent is of known Crawler * * @todo make logging guests online configurable option via Ini * */ protected function run() { $Viewer = $this->Registry->Viewer; $ip = Request::getIP(); $uid = $Viewer->getUid(); d('uid: ' . $uid); $aData = array('ip' => $ip, 'i_ts' => time(), 'ua' => Request::getUserAgent(), 'action' => 'request_' . $this->Registry->Request->get('a', 's', 'home'), 'uri' => $_SERVER['REQUEST_URI'], 'title' => $this->title, 'category' => $this->category, 'a_kw' => !empty($this->aInfo['keywords']) ? explode(', ', $this->aInfo['keywords']) : array()); if ($uid > 0) { $aData['i_uid'] = $uid; $aData['username'] = $Viewer->getDisplayName(); $aData['avtr'] = $Viewer->getAvatarSrc(); $aData['profile'] = $Viewer->getProfileUrl(); $aData['role'] = $Viewer->getRoleId(); $aData['i_pp'] = $Viewer->getProfitPoint(); } $Mongo = $this->Registry->Mongo->getDb(); $Geo = $this->Registry->Geo; $func = function () use($aData, $Mongo, $Geo) { $aGeo = $Geo->getLocation($aData['ip'])->toArray(); $aData = $aData + $aGeo; /** * Need unique index uid * */ if (array_key_exists('i_uid', $aData)) { $coll = $Mongo->ONLINE; $coll->ensureIndex(array('i_uid' => 1), array('unique' => true)); $coll->ensureIndex(array('i_ts' => 1)); $coll->update(array('i_uid' => $aData['i_uid']), $aData, array('upsert' => true)); } else { /** * For guests the value of ip2long (int) * will be used as uid */ $aData['i_uid'] = ip2long($aData['ip']); $coll = $Mongo->GUESTS; $coll->ensureIndex(array('i_uid' => 1), array('unique' => true)); $coll->ensureIndex(array('i_ts' => 1)); $coll->update(array('ip' => $aData['ip']), $aData, array('upsert' => true)); } /** * Remove old records * Cleanup runs 10% of requests * removes records older than 24 hours */ if (1 === rand(0, 10)) { $offset = time() - 60 * 60 * 24; $coll->remove(array('i_ts' => array('$lt' => $offset))); } }; \Lampcms\runLater($func); }
/** * Update LOGIN_LOG collection * */ protected function run() { $Viewer = $this->Registry->Viewer; if (!is_object($Viewer)) { d('Could not get Viewer object'); return; } $ip = Request::getIP(); $uid = $Viewer->getUid(); d('uid: ' . $uid); if ($uid > 0) { $aData = array('ip' => $ip, 'i_uid' => $uid, 'i_ts' => time(), 'ua' => Request::getUserAgent(), 'login_method' => $this->loginMethod); $Mongo = $this->Registry->Mongo->getDb(); //$Geo = $this->Registry->Geo; $func = function () use($aData, $Mongo) { //$aGeo = $Geo->getLocation($aData['ip'])->toArray(); //$aData = $aData + $aGeo; $coll = $Mongo->LOGIN_LOG; $coll->ensureIndex(array('i_uid' => 1)); $coll->ensureIndex(array('ip' => 1)); $coll->insert($aData); }; \Lampcms\runLater($func); } }