/** * Generate a unique cache fileName from input array * * @param array $arr */ private function getCacheFileName($arr) { if (!isset($arr) || !is_array($arr) || count($arr) === 0) { return null; } return RestoUtil::encrypt(serialize($arr)) . '.cache'; }
/** * Add items to cart * * $data should be an array of array. * * Structure : * array( * array( * 'id' => //featureidentifier * 'properties' => array( * * ) * ), * array( * * ), * ... * ) * * @param array $data * @param boolean $synchronize : true to synchronize with database */ public function add($data, $synchronize = false) { if (!is_array($data)) { return false; } $items = array(); for ($i = count($data); $i--;) { if (!isset($data[$i]['id'])) { continue; } /* * Same resource cannot be added twice */ $itemId = RestoUtil::encrypt((isset($this->user->profile['email']) ? $this->user->profile['email'] : '') . $data[$i]['id']); if (isset($this->items[$itemId])) { continue; } if ($synchronize && $this->user->profile['userid'] !== -1) { if (!$this->user->context->dbDriver->store(RestoDatabaseDriver::CART_ITEM, array('email' => $this->user->profile['email'], 'item' => $data[$i]))) { return false; } } $this->items[$itemId] = $data[$i]; $items[$itemId] = $data[$i]; } return $items; }
/** * Place order for user from cart - empty cart afterward * * @param string $identifier * @param array $items * * @return array * @throws exception */ private function storeOrder($identifier, $items) { /* * Do not create empty orders */ if (!isset($items) || count($items) === 0) { return -1; } try { $orderId = RestoUtil::encrypt($identifier . microtime()); $values = array('\'' . pg_escape_string($orderId) . '\'', '\'' . pg_escape_string($identifier) . '\'', '\'' . pg_escape_string(json_encode($items)) . '\'', 'now()'); $this->dbDriver->query('INSERT INTO usermanagement.orders (orderid, email, items, querytime) VALUES (' . join(',', $values) . ')'); } catch (Exception $e) { RestoLogUtil::httpError($e->getCode(), $e->getMessage()); } return $orderId; }
/** * Create a shared resource and return it * * @param string $identifier * @param string $resourceUrl * @param integer $duration * @return array */ public function createSharedLink($identifier, $resourceUrl, $duration = 86400) { if (!isset($resourceUrl) || !RestoUtil::isUrl($resourceUrl)) { return null; } if (!is_int($duration)) { $duration = 86400; } $results = $this->dbDriver->fetch($this->dbDriver->query('INSERT INTO usermanagement.sharedlinks (url, token, email, validity) VALUES (\'' . pg_escape_string($resourceUrl) . '\',\'' . RestoUtil::encrypt(mt_rand() . microtime()) . '\',\'' . pg_escape_string($identifier) . '\',now() + ' . $duration . ' * \'1 second\'::interval) RETURNING token', 500, 'Cannot share link')); if (count($results) === 1) { return array('resourceUrl' => $resourceUrl, 'token' => $results[0]['token']); } return null; }
/** * Update user profile to database * * @param array $profile * @return integer (userid) * @throws exception */ public function updateUserProfile($profile) { if (!is_array($profile) || !isset($profile['email'])) { RestoLogUtil::httpError(500, 'Cannot update user profile - invalid user identifier'); } /* * The following parameters cannot be updated : * - email * - userid * - activationcode * - registrationdate */ $values = array(); foreach (array_values(array('username', 'givenname', 'lastname', 'groups', 'country', 'organization', 'topics', 'organizationcountry', 'flags')) as $field) { if (isset($profile[$field])) { switch ($field) { case 'password': $values[] = 'password=\'' . RestoUtil::encrypt($profile['password']) . '\''; break; case 'activated': $values[] = 'activated=' . $profile['activated']; break; default: $values[] = $field . '=\'' . pg_escape_string($profile[$field]) . '\''; } } } $results = array(); if (count($values) > 0) { $results = $this->dbDriver->fetch($this->dbDriver->query('UPDATE usermanagement.users SET ' . join(',', $values) . ' WHERE email=\'' . pg_escape_string(trim(strtolower($profile['email']))) . '\' RETURNING userid')); } return count($results) === 1 ? $results[0]['userid'] : null; }