diff --git a/app/main/controller/api/user.php b/app/main/controller/api/user.php index 7b2bc8fc..c4d24e9a 100644 --- a/app/main/controller/api/user.php +++ b/app/main/controller/api/user.php @@ -11,6 +11,7 @@ use Controller; use controller\MailController; use Model; use Exception; +use DB; class User extends Controller\Controller{ @@ -70,10 +71,12 @@ class User extends Controller\Controller{ // set Session login $dateTime = new \DateTime(); - $this->f3->set('SESSION.user.time', $dateTime->getTimestamp()); - $this->f3->set('SESSION.user.name', $user->name); - $this->f3->set('SESSION.user.id', $user->id); + $this->f3->set('SESSION.user', [ + 'time' => $dateTime->getTimestamp(), + 'name' => $user->name, + 'id' => $user->id + ]); // save user login information $user->touch('lastLogin'); @@ -144,19 +147,9 @@ class User extends Controller\Controller{ if($characterLog = $character->getLog()){ $characterLog->erase(); - $characterLog->save(); - - $character->clearCacheData(); - - // delete log cache key as well - $f3->clear('LOGGED.user.character.id_' . $characterLog->characterId->id . '.systemId'); - $f3->clear('LOGGED.user.character.id_' . $characterLog->characterId->id . '.shipId'); - } } } - - } /** diff --git a/app/main/controller/controller.php b/app/main/controller/controller.php index 94ce84c2..b205247f 100644 --- a/app/main/controller/controller.php +++ b/app/main/controller/controller.php @@ -79,20 +79,72 @@ class Controller { protected function _getUser($ttl = 5){ $user = false; - $userId = $this->f3->get('SESSION.user.id'); - if($userId > 0){ - $userModel = Model\BasicModel::getNew('UserModel'); - $userModel->getById($userId, $ttl); + if( $this->f3->exists('SESSION.user.id') ){ + $userId = (int)$this->f3->get('SESSION.user.id'); - if( !$userModel->dry() ){ - $user = $userModel; + if($userId > 0){ + $userModel = Model\BasicModel::getNew('UserModel'); + $userModel->getById($userId, $ttl); + + if( !$userModel->dry() ){ + $user = $userModel; + } } } return $user; } + /** + * log the current user out + * @param $f3 + */ + public function logOut($f3){ + + // destroy session + $f3->clear('SESSION'); + + if( !$f3->get('AJAX') ){ + // redirect to landing page + $f3->reroute('@landing'); + }else{ + $return = (object) []; + $return->reroute = self::getEnvironmentData('URL') . $f3->alias('landing'); + $return->error[] = $this->getUserLoggedOffError(); + + echo json_encode($return); + die(); + } + } + + /** + * verifies weather a given username and password is valid + * @param $userName + * @param $password + * @return Model\UserModel|null + */ + protected function _verifyUser($userName, $password) { + + $validUser = null; + + $user = Model\BasicModel::getNew('UserModel', 0); + + $user->getByName($userName); + + // check userName is valid + if( !$user->dry() ){ + // check if password is valid + $isValid = $user->verify($password); + + if($isValid === true){ + $validUser = $user; + } + } + + return $validUser; + } + /** * check weather the page is IGB trusted or not * @return mixed @@ -152,55 +204,6 @@ class Controller { return $isIGB; } - /** - * verifies weather a given username and password is valid - * @param $userName - * @param $password - * @return Model\UserModel|null - */ - protected function _verifyUser($userName, $password) { - - $validUser = null; - - $user = Model\BasicModel::getNew('UserModel', 0); - - $user->getByName($userName); - - // check userName is valid - if( !$user->dry() ){ - // check if password is valid - $isValid = $user->verify($password); - - if($isValid === true){ - $validUser = $user; - } - } - - return $validUser; - } - - /** - * log the current user out - * @param $f3 - */ - public function logOut($f3){ - - // destroy session - $f3->clear('SESSION.user'); - $f3->sync('SESSION'); - - if( !$f3->get('AJAX') ){ - // redirect to landing page - $f3->reroute('@landing'); - }else{ - $return = (object) []; - $return->reroute = self::getEnvironmentData('URL') . $f3->alias('landing'); - $return->error[] = $this->getUserLoggedOffError(); - - echo json_encode($return); - die(); - } - } /** * get error object is a user is not found/logged of diff --git a/app/main/cron/characterupdate.php b/app/main/cron/characterupdate.php index 243ac1ef..6a46bf14 100644 --- a/app/main/cron/characterupdate.php +++ b/app/main/cron/characterupdate.php @@ -9,6 +9,8 @@ namespace cron; use Controller; use DB; +use Model; + class CharacterUpdate { @@ -21,8 +23,24 @@ class CharacterUpdate { DB\Database::instance()->setDB('PF'); - $sqlDeleteCharacterLogs = "TRUNCATE TABLE character_log"; - $f3->get('DB')->exec($sqlDeleteCharacterLogs); + $characterLogModel = Model\BasicModel::getNew('CharacterLogModel'); + + // find "old" character logs + $characterLogs = $characterLogModel->find([ + 'TIMESTAMPDIFF(SECOND, updated, NOW() ) > :lifetime', + ':lifetime' => (int)$f3->get('PATHFINDER.CACHE.CHARACTER_LOG') + ]); + + if(is_object($characterLogs)){ + foreach($characterLogs as $characterLog){ + // delete log and all cached values + $characterLog->erase(); + } + } + + + + } } \ No newline at end of file diff --git a/app/main/model/characterlogmodel.php b/app/main/model/characterlogmodel.php index 8424f676..d67c3af4 100644 --- a/app/main/model/characterlogmodel.php +++ b/app/main/model/characterlogmodel.php @@ -19,6 +19,16 @@ class CharacterLogModel extends BasicModel { ] ]; + public function __construct($db = NULL, $table = NULL, $fluid = NULL, $ttl = 0){ + + parent::__construct($db, $table, $fluid, $ttl); + + // events ----------------------------------------- + $this->beforeerase(function($self){ + $self->clearCacheData(); + }); + } + /** * get all character log data * @return object @@ -38,5 +48,19 @@ class CharacterLogModel extends BasicModel { return $logData; } + /** + * see parent + */ + public function clearCacheData(){ + parent::clearCacheData(); + // delete log cache key as well + $f3 = self::getF3(); + $character = $this->characterId; + + $character->clearCacheData(); + $f3->clear('LOGGED.user.character.id_' . $character->id); + + return true; + } } \ No newline at end of file diff --git a/app/main/model/usermodel.php b/app/main/model/usermodel.php index 74cb2e81..5d812cd5 100644 --- a/app/main/model/usermodel.php +++ b/app/main/model/usermodel.php @@ -431,36 +431,43 @@ class UserModel extends BasicModel { * @throws \Exception */ public function updateCharacterLog($ttl = 0){ - $apiController = Controller\CcpApiController::getIGBHeaderData(); + $headerData = Controller\CcpApiController::getIGBHeaderData(); // check if IGB Data is available - if( !empty($apiController->values) ){ + if( !empty($headerData->values) ){ $f3 = self::getF3(); // check if system has changed since the last call - // current location is stored in session to avoid unnecessary DB calls - $sessionCharacterKey = 'LOGGED.user.character.id_' . $apiController->values['charid']; + // current location is stored (global) to avoid unnecessary DB calls + $sessionCharacterKey = 'LOGGED.user.character.id_' . $headerData->values['charid']; + + if($f3->exists($sessionCharacterKey)){ + // cache data exists + $cacheData = $f3->get($sessionCharacterKey); + }else{ + // new cache data + $cacheData = [ + 'systemId' => 0, + 'shipId' => 0 + ]; + } if( - !$f3->exists($sessionCharacterKey) || - $f3->get($sessionCharacterKey . '.systemId') != $apiController->values['solarsystemid'] || - $f3->get($sessionCharacterKey . '.shipId') != $apiController->values['shiptypeid'] + $cacheData['systemId'] != $headerData->values['solarsystemid'] || + $cacheData['shipId'] != $headerData->values['shiptypeid'] ){ - - $cacheData = [ - 'systemId' => $apiController->values['solarsystemid'], - 'shipId' => $apiController->values['shiptypeid'] - ]; + $cacheData['systemId'] = (int)$headerData->values['solarsystemid']; + $cacheData['shipId'] = (int)$headerData->values['shiptypeid']; // character has changed system, or character just logged on $character = self::getNew('CharacterModel'); - $character->getById( (int)$apiController->values['charid'] ); + $character->getById( (int)$headerData->values['charid'] ); if( $character->dry() ){ // this can happen if a valid user plays the game with a not registered character // whose API is not registered -> save new character or update character data - $corporationId = array_key_exists('corpid', $apiController->values) ? $apiController->values['corpid'] : null; - $allianceId = array_key_exists('allianceid', $apiController->values) ? $apiController->values['allianceid'] : null; + $corporationId = array_key_exists('corpid', $headerData->values) ? $headerData->values['corpid'] : null; + $allianceId = array_key_exists('allianceid', $headerData->values) ? $headerData->values['allianceid'] : null; // check if corp exists if( !is_null($corporationId) ){ @@ -468,7 +475,7 @@ class UserModel extends BasicModel { $corporation->getById( (int)$corporationId ); if( $corporation->dry() ){ $corporation->id = $corporationId; - $corporation->name = $apiController->values['corpname']; + $corporation->name = $headerData->values['corpname']; $corporation->save(); } } @@ -479,13 +486,13 @@ class UserModel extends BasicModel { $alliance->getById( (int)$allianceId ); if( $alliance->dry() ){ $alliance->id = $allianceId; - $alliance->name = $apiController->values['alliancename']; + $alliance->name = $headerData->values['alliancename']; $alliance->save(); } } - $character->id = (int) $apiController->values['charid']; - $character->name = $apiController->values['charname']; + $character->id = (int) $headerData->values['charid']; + $character->name = $headerData->values['charname']; $character->corporationId = $corporationId; $character->allianceId = $allianceId; $character->save(); @@ -498,11 +505,11 @@ class UserModel extends BasicModel { // set character log values $characterLog->characterId = $character; - $characterLog->systemId = $apiController->values['solarsystemid']; - $characterLog->systemName = $apiController->values['solarsystemname']; - $characterLog->shipId = $apiController->values['shiptypeid']; - $characterLog->shipName = $apiController->values['shipname']; - $characterLog->shipTypeName = $apiController->values['shiptypename']; + $characterLog->systemId = (int)$headerData->values['solarsystemid']; + $characterLog->systemName = $headerData->values['solarsystemname']; + $characterLog->shipId = (int)$headerData->values['shiptypeid']; + $characterLog->shipName = $headerData->values['shipname']; + $characterLog->shipTypeName = $headerData->values['shiptypename']; $characterLog->save(); @@ -512,7 +519,6 @@ class UserModel extends BasicModel { // cache character log information $f3->set($sessionCharacterKey, $cacheData, $ttl); } - } } diff --git a/app/pathfinder.ini b/app/pathfinder.ini index b14df42c..1cf1dc98 100644 --- a/app/pathfinder.ini +++ b/app/pathfinder.ini @@ -98,7 +98,7 @@ INVITE = 0 INVITE_LIMIT = 50 ; ====================================================================================================== -; Lifetime for map types +; Lifetime for map types (days) [PATHFINDER.MAP.PRIVATE] LIFETIME = 2 @@ -111,7 +111,7 @@ LIFETIME = 99999 [PATHFINDER.CACHE] ; cache character log informations in seconds. This is ignored if ship/system switch was detected -CHARACTER_LOG = 600 +CHARACTER_LOG = 300 ; cache time for all system data within a constellation (this will never change) 30d CONSTELLATION_SYSTEMS = 2592000 diff --git a/public/templates/dialog/settings.html b/public/templates/dialog/settings.html index 3117d91b..15798c85 100644 --- a/public/templates/dialog/settings.html +++ b/public/templates/dialog/settings.html @@ -224,10 +224,10 @@

API Key(s) are required to use pathfinder. - Don't have one? - Create a new key with an empty 'Access Mask'. + Don't have one? - Create a new key with an empty 'Access Mask' (0).

Get your new/custom API Key from - here + here and come back to this page.