/** * Authenticate user from Bearer authentication * (i.e. Single Sign On request with oAuth2) * * Assume either a JSON Web Token encoded by resto or a token generated by an SSO issuer (e.g. google) * * @param string $token */ private function authenticateBearer($token) { try { /* * If issuer_id is specified in the request then assumes a third party token. * In this case, transform this third party token into a resto token */ if (isset($this->context->query['issuerId']) && isset($this->context->modules['Auth'])) { $auth = RestoUtil::instantiate($this->context->modules['Auth']['className'], $this->context, null); $token = $auth->getProfileToken($this->context->query['issuerId'], $token); } $payloadObject = json_decode(json_encode((array) $this->context->decodeJWT($token)), true); $this->user = new RestoUser($payloadObject['data'], $this->context); /* * Assign token to user */ $this->user->token = $token; } catch (Exception $ex) { } }
/** * Load all collections from RESTo database and add them to this object */ public function loadCollectionsFromStore() { $collectionsDescriptions = $this->context->dbDriver->get(RestoDatabaseDriver::COLLECTIONS_DESCRIPTIONS); foreach (array_keys($collectionsDescriptions) as $key) { $collection = new RestoCollection($key, $this->context, $this->user); $collection->model = RestoUtil::instantiate($collectionsDescriptions[$key]['model'], array($collection->context, $collection->user)); $collection->osDescription = $collectionsDescriptions[$key]['osDescription']; $collection->status = $collectionsDescriptions[$key]['status']; $collection->owner = $collectionsDescriptions[$key]['owner']; $collection->license = new RestoLicense($this->context, $collectionsDescriptions[$key]['license']['licenseId'], false); $collection->license->setDescription($collectionsDescriptions[$key]['license'], false); $collection->propertiesMapping = $collectionsDescriptions[$key]['propertiesMapping']; $this->collections[$collection->name] = $collection; } return $this; }
/** * Set dictionary from input language - default is english */ private function setDictionary() { $lang = filter_input(INPUT_GET, 'lang', FILTER_SANITIZE_STRING); if (!isset($lang) || !in_array($lang, $this->languages) || !class_exists('RestoDictionary_' . $lang)) { $lang = 'en'; } $this->dictionary = RestoUtil::instantiate('RestoDictionary_' . $lang, array($this->dbDriver)); }
/** * Launch module run() function if exist otherwise returns 404 Not Found * * @param array $segments - path (i.e. a/b/c/d) exploded as an array (i.e. array('a', 'b', 'c', 'd') * @param array $data - data (POST or PUT) */ protected function processModuleRoute($segments, $data = array()) { $module = null; foreach (array_keys($this->context->modules) as $moduleName) { if (isset($this->context->modules[$moduleName]['route'])) { $moduleSegments = explode('/', $this->context->modules[$moduleName]['route']); $routeIsTheSame = true; $count = 0; for ($i = 0, $l = count($moduleSegments); $i < $l; $i++) { $count++; if (!isset($segments[$i]) || $moduleSegments[$i] !== $segments[$i]) { $routeIsTheSame = false; break; } } if ($routeIsTheSame) { $module = RestoUtil::instantiate($moduleName, array($this->context, $this->user)); for ($i = $count; $i--;) { array_shift($segments); } return $module->run($segments, $data); } } } if (!isset($module)) { RestoLogUtil::httpError(404); } }
/** * Extend search params with gazetteer results * * @param RestoContext $context * @param RestoUser $user * @param array $params */ private function extendParamsWithGazetteer($params) { if (!isset($params['searchTerms']) && !isset($params['geo:lon']) && !isset($params['geo:geometry']) && !isset($params['geo:box'])) { if (isset($this->context->modules['GazetteerPro'])) { $gazetteer = RestoUtil::instantiate($this->context->modules['GazetteerPro']['className'], array($this->context, $this->user)); } else { if (isset($this->context->modules['Gazetteer'])) { $gazetteer = RestoUtil::instantiate($this->context->modules['Gazetteer']['className'], array($this->context, $this->user)); } } if ($gazetteer) { $location = $gazetteer->search(array('q' => $params['geo:name'], 'wkt' => true)); if (count($location['results']) > 0) { if (isset($location['results'][0]['hash'])) { $params['searchTerms'] = 'geohash:' . $location['results'][0]['hash']; } else { if (isset($location['results'][0]['geo:geometry'])) { $params['geo:geometry'] = $location['results'][0]['geo:geometry']; } else { if (isset($location['results'][0]['geo:lon'])) { $params['geo:lon'] = $location['results'][0]['geo:lon']; $params['geo:lat'] = $location['results'][0]['geo:lat']; } } } } } } return $params; }
/** * Returns location from geohash/geouid * * @param string $hashOrUid */ public function whereFromGeohashOrGeouid($hashOrUid) { if (isset($this->context->modules['GazetteerPro'])) { $gazetteerPro = RestoUtil::instantiate($this->context->modules['GazetteerPro']['className'], array($this->context, $this->user)); $location = $gazetteerPro->search(array('q' => $hashOrUid, 'wkt' => true, 'preserve' => true, 'tolerance' => 0.05, 'snap' => true, 'lang' => $this->context->dictionary->language === 'en' ? 'en' : $this->context->dictionary->language . ',en')); return $location['results']; } return array(); }
/** * Set model * * @param string $name */ private function setModel($name) { /* * Check that input file is for the current collection */ if (isset($this->model)) { if ($this->model->name !== $name) { RestoLogUtil::httpError(500, 'Property "model" and collection name differ'); } } else { $this->model = RestoUtil::instantiate($name, array($this->context, $this->user)); } }
/** * Store feature within {collection}.features table following the class model * * @param array $data : array (MUST BE GeoJSON in abstract Model) * @param RestoCollection $collection * */ public function storeFeature($data, $collection) { /* * Assume input file or stream is a JSON Feature */ if (!RestoGeometryUtil::isValidGeoJSONFeature($data)) { RestoLogUtil::httpError(500, 'Invalid feature description'); } /* * Remap properties between RESTo model and input * GeoJSON Feature file */ $properties = $this->mapInputProperties($data); /* * Add collection to $properties to initialize facet counts on collection */ $properties['collection'] = isset($properties['collection']) ? $properties['collection'] : $collection->name; /* * Compute unique identifier */ if (!isset($data['id']) || !RestoUtil::isValidUUID($data['id'])) { $featureIdentifier = $collection->toFeatureId(isset($properties['productIdentifier']) ? $properties['productIdentifier'] : md5(microtime() . rand())); } else { $featureIdentifier = $data['id']; } /* * First check if feature is already in database * (do this before getKeywords to avoid iTag process) */ if ($collection->context->dbDriver->check(RestoDatabaseDriver::FEATURE, array('featureIdentifier' => $featureIdentifier))) { RestoLogUtil::httpError(500, 'Feature ' . $featureIdentifier . ' already in database'); } /* * Tag module */ $keywords = array(); if (isset($collection->context->modules['Tag'])) { $tagger = RestoUtil::instantiate($collection->context->modules['Tag']['className'], array($collection->context, $collection->user)); $keywords = $tagger->getKeywords($properties, $data['geometry']); } /* * Store feature */ $collection->context->dbDriver->store(RestoDatabaseDriver::FEATURE, array('collection' => $collection, 'featureArray' => array('type' => 'Feature', 'id' => $featureIdentifier, 'geometry' => $data['geometry'], 'properties' => array_merge($properties, array('keywords' => $keywords))))); return new RestoFeature($collection->context, $collection->user, array('featureIdentifier' => $featureIdentifier)); }