exists() public method

Return TRUE if hive key is set (or return timestamp and TTL if cached)
public exists ( $key, &$val = NULL ) : boolean
$key string
$val mixed
return boolean
Beispiel #1
0
 /**
  * perform external authorisation, return access token
  * @param null $callback_url
  * @return array|bool
  */
 public function login($callback_url = NULL)
 {
     if (!$this->f3->exists('GET.oauth_token')) {
         $tokens = $this->requestToken();
         $this->setAuthToken($tokens['oauth_token'], $tokens['oauth_token_secret']);
         if (is_null($callback_url)) {
             $callback_url = $this->f3->get('SCHEME') . '://' . $this->f3->get('HOST') . $this->f3->get('URI');
         }
         $this->authorize($callback_url);
     } else {
         return $this->accessToken();
     }
 }
Beispiel #2
0
 /**
  * render asset group
  * @param array $assets
  * @return string
  */
 public function renderGroup($assets)
 {
     $out = array();
     foreach ($assets as $asset_type => $collection) {
         if ($this->f3->exists('ASSETS.filter.' . $asset_type, $filters)) {
             if (is_string($filters)) {
                 $filters = $this->f3->split($filters);
             }
             $filters = array_values(array_intersect_key($this->filter, array_flip($filters)));
             $collection = $this->f3->relay($filters, array($collection));
         }
         foreach ($collection as $asset) {
             $out[] = $this->f3->call($this->formatter[$asset_type], array($asset));
         }
     }
     return "\n\t" . implode("\n\t", $out) . "\n";
 }
Beispiel #3
0
 /**
  * check login state
  * @return bool
  */
 public static function isLoggedIn(\Base $f3)
 {
     $ip_db = sprintf("%u", ip2long($_SERVER['REMOTE_ADDR']));
     /*
     Session mask (bit-wise)
     
     - admin			   128
     - super mod			64
     - story mod 		32
     - lector			16
     - author (trusted)	 8
     - author (regular)	 4
     - user (trusted)	 2
     - user (active)		 1
     - guest				 0
     */
     if ($f3->exists('SESSION.session_id')) {
         $session_id = $f3->get('SESSION.session_id');
     } elseif (isset($_COOKIE['session_id'])) {
         $session_id = $_COOKIE['session_id'];
         $f3->set('SESSION.session_id', $session_id);
     } else {
         $session_id = \Model\Auth::instance()->createSession($ip_db);
     }
     //		echo "<br>old: ".$_SESSION['session_id'];
     if (isset($session_id) && ($user = \Model\Auth::instance()->validateSession($session_id, $ip_db)) and $user['userID'] > 0) {
         $_SESSION['groups'] = $user['groups'];
         $_SESSION['admin_active'] = $user['admin_active'];
         $_SESSION['userID'] = $user['userID'];
         $_SESSION['username'] = $user['nickname'];
         $_SESSION['mail'] = array($user['mail'], $user['unread']);
         $_SESSION['tpl'] = ["default", 1];
         return TRUE;
     } else {
         //getStats();
         //
         $_SESSION['groups'] = bindec('0');
         $_SESSION['admin_active'] = FALSE;
         $_SESSION['userID'] = FALSE;
         $_SESSION['username'] = "******";
         $_SESSION['mail'] = FALSE;
         $_SESSION['tpl'] = ["default", 1];
         return FALSE;
     }
 }
 public function database(\Base $f3)
 {
     $this->response->data['SUBPART'] = 'settings_database.html';
     $cfg = \Config::instance();
     if ($f3->get('VERB') == 'POST' && $f3->exists('POST.active_db')) {
         $type = $f3->get('POST.active_db');
         $cfg->{'DB_' . $type} = $f3->get('POST.DB_' . $type);
         $cfg->ACTIVE_DB = $type;
         $cfg->save();
         \Flash::instance()->addMessage('Config saved', 'success');
         $setup = new \Setup();
         $setup->install($type);
         // logout
         $f3->clear('SESSION.user_id');
     }
     $cfg->copyto('POST');
     $f3->set('JIG_format', array('JSON', 'Serialized'));
 }
Beispiel #5
0
 /**
  * render asset group
  * @param array $assets
  * @return string
  */
 public function renderGroup($assets)
 {
     $out = array();
     foreach ($assets as $asset_type => $collection) {
         if ($this->f3->exists('ASSETS.filter.' . $asset_type, $filters)) {
             if (is_string($filters)) {
                 $filters = $this->f3->split($filters);
             }
             $flist = array_flip($filters);
             $filters = array_values(array_intersect_key(array_replace($flist, $this->filter), $flist));
             $collection = $this->f3->relay($filters, array($collection));
         }
         foreach ($collection as $asset) {
             if (isset($asset['path'])) {
                 $path = $asset['path'];
                 $mtime = $this->f3->get('ASSETS.timestamps') && $asset['origin'] != 'external' && is_file($path) ? '?' . filemtime($path) : '';
                 $base = $this->f3->get('ASSETS.prepend_base') && $asset['origin'] != 'external' && is_file($path) ? $this->f3->get('BASE') . '/' : '';
                 $asset['path'] = $base . $path . $mtime;
             }
             $out[] = $this->f3->call($this->formatter[$asset_type], array($asset));
         }
     }
     return "\n\t" . implode("\n\t", $out) . "\n";
 }
Beispiel #6
0
 /**
  * update map data api
  * -> function is called continuously by any active client
  * @param \Base $f3
  */
 public function updateUserData(\Base $f3)
 {
     $return = (object) [];
     $return->error = [];
     $activeCharacter = $this->getCharacter(0);
     $postData = $f3->get('POST');
     if (!empty($mapIds = (array) $postData['mapIds'])) {
         // IMPORTANT for now -> just update a single map (save performance)
         $mapId = (int) reset($mapIds);
         // get map and check map access
         $map = $activeCharacter->getMap((int) $mapId);
         if (!is_null($map)) {
             $characterMapData = (array) $postData['characterMapData'];
             // check if data for specific system is requested
             $systemData = (array) $postData['systemData'];
             // if data is requested extend the cache key in order to get new data
             $requestSystemData = (object) [];
             $requestSystemData->mapId = isset($systemData['mapId']) ? (int) $systemData['mapId'] : 0;
             $requestSystemData->systemId = isset($systemData['systemData']['id']) ? (int) $systemData['systemData']['id'] : 0;
             // update current location
             // -> suppress temporary timeout errors
             $activeCharacter = $activeCharacter->updateLog(['suppressHTTPErrors' => true]);
             // check character log (current system) and manipulate map (e.g. add new system)
             if ((bool) $characterMapData['mapTracking']) {
                 $map = $this->updateMapData($activeCharacter, $map);
             }
             $cacheKey = $this->getUserDataCacheKey($mapId, $requestSystemData->systemId);
             if (!$f3->exists($cacheKey)) {
                 $return->mapUserData[] = $map->getUserData();
                 // request signature data for a system if user has map access!
                 if ($mapId === $requestSystemData->mapId) {
                     $system = $map->getSystemById($requestSystemData->systemId);
                     if (!is_null($system)) {
                         // data for currently selected system
                         $return->system = $system->getData();
                         $return->system->signatures = $system->getSignaturesData();
                     }
                 }
                 // cache time (seconds) should be equal or less than request trigger time
                 // prevent request flooding
                 $responseTTL = (int) $f3->get('PATHFINDER.TIMER.UPDATE_SERVER_USER_DATA.DELAY') / 1000;
                 // cache response
                 $f3->set($cacheKey, $return, $responseTTL);
             } else {
                 // get from cache
                 // this should happen if a user has multiple program instances running
                 // with the same main char
                 $return = $f3->get($cacheKey);
             }
         }
     }
     // get current user data -> this should not be cached because each user has different personal data
     // even if they have multiple characters using the same map!
     $return->userData = $activeCharacter->getUser()->getData();
     echo json_encode($return);
 }
Beispiel #7
0
 public function exists($channel, $strict = true)
 {
     if (!$strict) {
         $channel = $this->channelFromAlias($channel);
     }
     $chan = $this->channelFileName($channel);
     if (file_exists($chan)) {
         return true;
     }
     return parent::exists($channel, $strict);
 }
Beispiel #8
0
 /**
  * search multiple route between two systems
  * @param \Base $f3
  */
 public function search($f3)
 {
     $requestData = (array) $f3->get('POST');
     $activeCharacter = $this->getCharacter();
     $return = (object) [];
     $return->error = [];
     $return->routesData = [];
     if (!empty($requestData['routeData'])) {
         $routesData = (array) $requestData['routeData'];
         //  map data where access was already checked -> cached data
         $validMaps = [];
         /**
          * @var $map Model\MapModel
          */
         $map = Model\BasicModel::getNew('MapModel');
         foreach ($routesData as $key => $routeData) {
             // mapIds are optional. If mapIds is empty or not set
             // route search is limited to CCPs static data
             $mapData = (array) $routeData['mapIds'];
             $mapData = array_flip(array_map('intval', $mapData));
             // check map access (filter requested mapIDs and format) --------------------
             array_walk($mapData, function (&$item, &$key, $data) {
                 if (isset($data[1][$key])) {
                     // character has mas access -> do not check again
                     $item = $data[1][$key];
                 } else {
                     // check map access for current character
                     $data[0]->getById($key);
                     if ($data[0]->hasAccess($data[2])) {
                         $item = ['id' => $key, 'name' => $data[0]->name];
                     } else {
                         $item = false;
                     }
                     $data[0]->reset();
                 }
             }, [$map, $validMaps, $activeCharacter]);
             // filter maps with NO access right
             $mapData = array_filter($mapData);
             $mapIds = array_column($mapData, 'id');
             // add map data to cache array
             $validMaps += $mapData;
             // search route with filter options
             $filterData = ['stargates' => (bool) $routeData['stargates'], 'jumpbridges' => (bool) $routeData['jumpbridges'], 'wormholes' => (bool) $routeData['wormholes'], 'wormholesReduced' => (bool) $routeData['wormholesReduced'], 'wormholesCritical' => (bool) $routeData['wormholesCritical']];
             $returnRoutData = ['systemFromData' => $routeData['systemFromData'], 'systemToData' => $routeData['systemToData'], 'maps' => $mapData, 'mapIds' => $mapIds];
             // add filter options for each route as well
             $returnRoutData += $filterData;
             if (count($mapIds) > 0) {
                 $systemFrom = $routeData['systemFromData']['name'];
                 $systemTo = $routeData['systemToData']['name'];
                 $cacheKey = $this->getRouteCacheKey($mapIds, $systemFrom, $systemTo, $filterData);
                 if ($f3->exists($cacheKey)) {
                     // get data from cache
                     $returnRoutData = $f3->get($cacheKey);
                 } else {
                     // max search depth for search
                     $searchDepth = $f3->get('PATHFINDER.ROUTE.SEARCH_DEPTH');
                     // set jump data for following route search
                     $this->initJumpData($mapIds, $filterData);
                     // no cached route data found
                     $foundRoutData = $this->findRoute($systemFrom, $systemTo, $searchDepth);
                     $returnRoutData = array_merge($returnRoutData, $foundRoutData);
                     // cache if route was found
                     if (isset($returnRoutData['routePossible']) && $returnRoutData['routePossible'] === true) {
                         $f3->set($cacheKey, $returnRoutData, $this->dynamicJumpDataCacheTime);
                     }
                 }
             }
             $return->routesData[] = $returnRoutData;
         }
     }
     echo json_encode($return);
 }
Beispiel #9
0
 /**
  * get system data for all systems within a constellation
  * @param \Base $f3
  * @param array $params
  */
 public function constellationData(\Base $f3, $params)
 {
     $return = (object) [];
     $return->error = [];
     $return->systemData = [];
     $constellationId = 0;
     // check for search parameter
     if (isset($params['arg1'])) {
         $constellationId = (int) $params['arg1'];
     }
     $cacheKey = 'CACHE_CONSTELLATION_SYSTEMS_' . self::formatHiveKey($constellationId);
     if ($f3->exists($cacheKey)) {
         $return->systemData = $f3->get($cacheKey);
     } else {
         if ($constellationId > 0) {
             $systemModels = $this->getSystemModelByIds([$constellationId], 'constellationID');
             foreach ($systemModels as $systemModel) {
                 $return->systemData[] = $systemModel->getData();
             }
             $f3->set($cacheKey, $return->systemData, $f3->get('PATHFINDER.CACHE.CONSTELLATION_SYSTEMS'));
         }
     }
     echo json_encode($return);
 }
Beispiel #10
0
 /**
  * callback handler for CCP SSO user Auth
  * -> see requestAuthorization()
  * @param \Base $f3
  */
 public function callbackAuthorization($f3)
 {
     $getParams = (array) $f3->get('GET');
     // users can log in either from @login (new user) or @map (existing user) root alias
     // -> in case login fails, users should be redirected differently
     $authFromMapAlias = false;
     if ($f3->exists(self::SESSION_KEY_SSO_STATE)) {
         // check response and validate 'state'
         if (isset($getParams['code']) && isset($getParams['state']) && !empty($getParams['code']) && !empty($getParams['state']) && $f3->get(self::SESSION_KEY_SSO_STATE) === $getParams['state']) {
             // check if user came from map (for redirect)
             if ($f3->get(self::SESSION_KEY_SSO_FROM_MAP)) {
                 $authFromMapAlias = true;
             }
             // clear 'state' for new next login request
             $f3->clear(self::SESSION_KEY_SSO_STATE);
             $f3->clear(self::SESSION_KEY_SSO_FROM_MAP);
             $accessData = $this->getCrestAccessData($getParams['code']);
             if (isset($accessData->accessToken) && isset($accessData->refreshToken)) {
                 // login succeeded -> get basic character data for current login
                 $verificationCharacterData = $this->verifyCharacterData($accessData->accessToken);
                 if (!is_null($verificationCharacterData)) {
                     // check if login is restricted to a characterID
                     // verification available data. Data is needed for "ownerHash" check
                     // get character data from CREST
                     $characterData = $this->getCharacterData($accessData->accessToken);
                     if (isset($characterData->character)) {
                         // add "ownerHash" and CREST tokens
                         $characterData->character['ownerHash'] = $verificationCharacterData->CharacterOwnerHash;
                         $characterData->character['crestAccessToken'] = $accessData->accessToken;
                         $characterData->character['crestRefreshToken'] = $accessData->refreshToken;
                         // add/update static character data
                         $characterModel = $this->updateCharacter($characterData);
                         if (!is_null($characterModel)) {
                             // check if character is authorized to log in
                             if ($characterModel->isAuthorized()) {
                                 // character is authorized to log in
                                 // -> update character log (current location,...)
                                 $characterModel = $characterModel->updateLog();
                                 // check if there is already an active user logged in
                                 if ($activeCharacter = $this->getCharacter()) {
                                     // connect character with current user
                                     $user = $activeCharacter->getUser();
                                 } elseif (is_null($user = $characterModel->getUser())) {
                                     // no user found (new character) -> create new user and connect to character
                                     $user = Model\BasicModel::getNew('UserModel');
                                     $user->name = $characterModel->name;
                                     $user->save();
                                 }
                                 /**
                                  * @var $userCharactersModel Model\UserCharacterModel
                                  */
                                 if (is_null($userCharactersModel = $characterModel->userCharacter)) {
                                     $userCharactersModel = Model\BasicModel::getNew('UserCharacterModel');
                                     $userCharactersModel->characterId = $characterModel;
                                 }
                                 // user might have changed
                                 $userCharactersModel->userId = $user;
                                 $userCharactersModel->save();
                                 // get updated character model
                                 $characterModel = $userCharactersModel->getCharacter();
                                 // login by character
                                 $loginCheck = $this->loginByCharacter($characterModel);
                                 if ($loginCheck) {
                                     // set "login" cookie
                                     $this->setLoginCookie($characterModel);
                                     // route to "map"
                                     $f3->reroute('@map');
                                 } else {
                                     $f3->set(self::SESSION_KEY_SSO_ERROR, sprintf(self::ERROR_LOGIN_FAILED, $characterModel->name));
                                 }
                             } else {
                                 // character is not authorized to log in
                                 $f3->set(self::SESSION_KEY_SSO_ERROR, sprintf(self::ERROR_CHARACTER_FORBIDDEN, $characterModel->name));
                             }
                         }
                     }
                 } else {
                     // failed to verify character by CREST
                     $f3->set(self::SESSION_KEY_SSO_ERROR, self::ERROR_CHARACTER_VERIFICATION);
                 }
             } else {
                 // CREST "accessData" missing (e.g. timeout)
                 $f3->set(self::SESSION_KEY_SSO_ERROR, sprintf(self::ERROR_SERVICE_TIMEOUT, self::CREST_TIMEOUT));
             }
         } else {
             // invalid CREST response
             $f3->set(self::SESSION_KEY_SSO_ERROR, sprintf(self::ERROR_LOGIN_FAILED, 'Invalid response'));
         }
     }
     if ($authFromMapAlias) {
         // on error -> route back to map
         $f3->reroute('@map');
     } else {
         // on error -> route back to login form
         $f3->reroute('@login');
     }
 }