Beispiel #1
0
 /**
  * Activate user
  * @param RestoUser $user
  */
 private function activateUser($user)
 {
     if (isset($user) && isset($this->context->query['act'])) {
         if ($user->activate($this->context->query['act'])) {
             /*
              * Close database handler and redirect to a human readable page...
              */
             if (isset($this->context->query['redirect'])) {
                 if (isset($this->context->dbDriver)) {
                     $this->context->dbDriver->closeDbh();
                 }
                 header('Location: ' . $this->context->query['redirect']);
                 exit;
             } else {
                 RestoLogUtil::success('User activated');
             }
         } else {
             RestoLogUtil::error('User not activated');
         }
     } else {
         RestoLogUtil::httpError(400);
     }
 }
Beispiel #2
0
 /**
  * Search locations from input query
  * 
  * Toponyms return order is :
  *      - fclass priority chain is P, A, the rest 
  *      - for 'P', fcode priority chain is PPLC, PPLG, PPLA, PPLA2, PPLA3, PPLA4, PPL, the rest
  *
  * (See http://www.geonames.org/export/codes.html for class and code explanation)
  * 
  * 
  * Query structure :
  * 
  *    array(
  *      'q' => // location to search form (e.g. Paris or Paris, France) - MANDATORY
  *      'type' => // force search type (i.e. 'toponym, country or state) - OPTIONAL
  *      'bbox' => // bounding box to restrict the search on - OPTIONAL
  *      'wkt' => // if true return geometry as wkt - OPTIONAL
  *    )
  * Gazetteer tables format :
  * 
  *  CREATE TABLE geoname (
  *      geonameid   int,
  *      name varchar(200),
  *      asciiname varchar(200),
  *      alternatenames varchar(8000),
  *      latitude float,
  *      longitude float,
  *      fclass char(1),
  *      fcode varchar(10),
  *      country varchar(2),
  *      cc2 varchar(60),
  *      admin1 varchar(20),
  *      admin2 varchar(80),
  *      admin3 varchar(20),
  *      admin4 varchar(20),
  *      population bigint,
  *      elevation int,
  *      gtopo30 int,
  *      timezone varchar(40),
  *      moddate date,
  *      geom
  *  );
  * 
  * @param array $params
  * @param boolean $normalize
  * @return array
  * 
  */
 public function search($params, $normalize = true)
 {
     if (!$this->dbh || !isset($params) || !isset($params['q'])) {
         return RestoLogUtil::httpError(400);
     }
     /*
      * Set output type - GeoJSON (default) or WKT
      */
     $this->outputAsWKT = isset($params['wkt']) ? filter_var($params['wkt'], FILTER_VALIDATE_BOOLEAN) : false;
     /*
      * Remove accents from query and split it into 'toponym' and 'modifier'
      */
     $query = $this->splitQuery($normalize ? $this->context->dbDriver->normalize($params['q']) : $params['q']);
     /*
      * Limit search to input type
      */
     $type = isset($params['type']) ? $params['type'] : null;
     switch ($type) {
         /*
          * State only
          */
         case 'state':
             $this->results = $this->getStates($query['toponym'], Gazetteer::STATE_PRECISION);
             break;
             /*
              * Region only
              */
         /*
          * Region only
          */
         case 'region':
             $this->results = $this->getRegions($query['toponym'], Gazetteer::REGION_PRECISION);
             break;
             /*
              * Country only
              */
         /*
          * Country only
          */
         case 'country':
             $this->results = $this->getCountries($query['toponym'], Gazetteer::COUNTRY_PRECISION);
             break;
             /*
              * Continent only
              */
         /*
          * Continent only
          */
         case 'continent':
             $this->results = $this->getContinents($query['toponym'], Gazetteer::CONTINENT_PRECISION);
             break;
             /*
              * Physical only
              */
         /*
          * Physical only
          */
         case 'physical':
             $this->results = $this->getPhysical($query['toponym'], Gazetteer::PHYSICAL_PRECISION);
             break;
             /*
              * Search for all
              */
         /*
          * Search for all
          */
         default:
             $this->results = $this->getToponyms($query['toponym'], array('bbox' => isset($params['bbox']) ? $params['bbox'] : null, 'modifier' => isset($query['modifier']) ? $query['modifier'] : null));
             if (!isset($query['modifier'])) {
                 $this->results = array_merge($this->results, $this->getContinents($query['toponym'], Gazetteer::CONTINENT_PRECISION));
                 $this->results = array_merge($this->results, $this->getCountries($query['toponym'], Gazetteer::COUNTRY_PRECISION));
                 $this->results = array_merge($this->results, $this->getRegions($query['toponym'], Gazetteer::REGION_PRECISION));
                 $this->results = array_merge($this->results, $this->getStates($query['toponym'], Gazetteer::STATE_PRECISION));
                 $this->results = array_merge($this->results, $this->getPhysical($query['toponym'], Gazetteer::PHYSICAL_PRECISION));
             }
     }
     /*
      * Close database handler
      */
     if ($this->closeDbh) {
         pg_close($this->dbh);
     }
     return RestoLogUtil::success(count($this->results) . ' toponym(s) found', array('query' => $params['q'], 'lang' => $this->context->dictionary->language, 'results' => $this->results));
 }
Beispiel #3
0
 /**
  * Add feature to collection 
  * 
  * @param RestoCollection $collection
  * @param array $data
  * 
  */
 private function addFeatureToCollection($collection, $data)
 {
     $feature = $collection->addFeature($data);
     /*
      * Store query
      */
     if ($this->context->storeQuery === true) {
         $this->user->storeQuery($this->context->method, 'insert', $collection->name, $feature->identifier, $this->context->query, $this->context->getUrl());
     }
     return RestoLogUtil::success('Feature ' . $feature->identifier . ' inserted within ' . $collection->name, array('featureIdentifier' => $feature->identifier));
 }
Beispiel #4
0
 /**
  * Return formated rights
  * 
  * @param RestoUser $user
  * @param string $collectionName
  * @param string $featureIdentifier
  */
 private function getRights($user, $collectionName, $featureIdentifier)
 {
     return RestoLogUtil::success('Rights for ' . $user->profile['email'], array('email' => $user->profile['email'], 'userid' => $user->profile['userid'], 'groups' => $user->profile['groups'], 'rights' => $user->getRights($collectionName, $featureIdentifier)));
 }
Beispiel #5
0
 /**
  * 
  * Process HTTP PUT request on users
  *
  *    user
  *    user/cart/{itemid}                            |  Modify item in user cart
  * 
  * @param array $segments
  * @param array $data
  */
 private function PUT_user($segments, $data)
 {
     /*
      * user
      */
     if (!isset($segments[1])) {
         /*
          * For normal user (i.e. non admin), some properties cannot be modified after validation
          */
         if (!$this->user->isAdmin()) {
             /*
              * Already validated => avoid updating administrative properties
              */
             if (isset($this->user->profile['validatedby'])) {
                 unset($data['activated'], $data['validatedby'], $data['validationdate'], $data['country'], $data['organization'], $data['organizationcountry'], $data['flags']);
             }
             /*
              * These properties can only be changed by admin
              */
             unset($data['groups']);
         }
         /*
          * Ensure that user can only update its profile
          */
         $data['email'] = $this->user->profile['email'];
         $this->context->dbDriver->update(RestoDatabaseDriver::USER_PROFILE, array('profile' => $data));
         return RestoLogUtil::success('Update profile for user ' . $this->user->profile['email']);
     } else {
         if ($segments[1] === 'cart' && isset($segments[2])) {
             if ($this->user->getCart()->update($segments[2], $data, true)) {
                 return RestoLogUtil::success('Item ' . $segments[2] . ' updated', array('itemId' => $segments[2], 'item' => $data));
             } else {
                 return RestoLogUtil::error('Cannot update item ' . $segments[2]);
             }
         } else {
             RestoLogUtil::httpError(404);
         }
     }
 }
Beispiel #6
0
 /**
  * Send reset password link to user email adress
  * 
  */
 public function sendResetPasswordLink()
 {
     /*
      * Only existing local user can change there password
      */
     if (!$this->context->dbDriver->check(RestoDatabaseDriver::USER, array('email' => $this->profile['email'])) || $this->context->dbDriver->get(RestoDatabaseDriver::USER_PASSWORD, array('email' => $this->profile['email'])) === str_repeat('*', 40)) {
         RestoLogUtil::httpError(3005);
     }
     /*
      * Send email with reset link
      */
     $shared = $this->context->dbDriver->get(RestoDatabaseDriver::SHARED_LINK, array('email' => $this->profile['email'], 'resourceUrl' => $this->context->resetPasswordUrl . '/' . base64_encode($this->profile['email']), 'duration' => isset($this->context->sharedLinkDuration) ? $this->context->sharedLinkDuration : null));
     $fallbackLanguage = isset($this->context->mail['resetPassword'][$this->context->dictionary->language]) ? $this->context->dictionary->language : 'en';
     if (!RestoUtil::sendMail(array('to' => $this->profile['email'], 'senderName' => $this->context->mail['senderName'], 'senderEmail' => $this->context->mail['senderEmail'], 'subject' => $this->context->dictionary->translate($this->context->mail['resetPassword'][$fallbackLanguage]['subject'], $this->context->title), 'message' => $this->context->dictionary->translate($this->context->mail['resetPassword'][$fallbackLanguage]['message'], $this->context->title, $shared['resourceUrl'] . '?_tk=' . $shared['token'])))) {
         RestoLogUtil::httpError(3003);
     }
     return RestoLogUtil::success('Reset link sent to ' . $this->profile['email']);
 }
Beispiel #7
0
 /**
  * 
  * Process user
  * 
  *    user/cart                                     |  Remove all cart items
  *    user/cart/{itemid}                            |  Remove {itemid} from user cart
  * 
  *  @SWG\Delete(
  *      tags={"user"},
  *      path="/user/cart/{itemId}",
  *      summary="Delete cart item(s)",
  *      description="Delete cart item {itemId}. Delete all items if no {itemId} is specified",
  *      operationId="deleteCartItem",
  *      produces={"application/json"},
  *      @SWG\Parameter(
  *          name="itemId",
  *          in="path",
  *          description="Cart item identifier",
  *          required=false,
  *          type="string",
  *          @SWG\Items(type="string")
  *      ),
  *      @SWG\Response(
  *          response="200",
  *          description="Acknowledgment on successful cart item(s) deletion"
  *      ),
  *      @SWG\Response(
  *          response="404",
  *          description="ItemId not found"
  *      ),
  *      @SWG\Response(
  *          response="403",
  *          description="Forbidden"
  *      )
  *  )
  * 
  * @param array $segments
  */
 private function DELETE_user($segments)
 {
     if (isset($segments[1]) && $segments[1] === 'cart') {
         /*
          * Clear all cart items
          */
         if (!isset($segments[2])) {
             return $this->user->getCart()->clear(true) ? RestoLogUtil::success('Cart cleared') : RestoLogUtil::error('Cannot clear cart');
         } else {
             return $this->user->getCart()->remove($segments[2], true) ? RestoLogUtil::success('Item removed from cart', array('itemid' => $segments[2])) : RestoLogUtil::error('Item cannot be removed', array('itemid' => $segments[2]));
         }
     } else {
         RestoLogUtil::httpError(404);
     }
 }
Beispiel #8
0
 /**
  *
  * Process HTTP PUT request on users
  *
  *      {featureid}   
  *
  * @param array $segments
  * @param array $data
  */
 private function processPUT($segments, $data)
 {
     /*
      * Check route pattern
      */
     if (!isset($segments[1]) || isset($segments[2])) {
         RestoLogUtil::httpError(404);
     }
     /*
      * First segment is the feature identifier
      */
     $feature = new RestoFeature($this->context, $this->user, array('featureIdentifier' => $segments[0]));
     if (!isset($feature)) {
         RestoLogUtil::httpError(404, 'Feature does not exist');
     }
     /*
      * Second segment is the action
      */
     switch ($segments[1]) {
         case 'refresh':
             $this->refresh($feature, $data);
             return RestoLogUtil::success('Recompute keywords for feature ' . $feature->identifier);
         default:
             RestoLogUtil::httpError(404);
     }
 }