public function test() { $this->initContext(); $this->assertEquals(false, RestoUtil::UUIDv5('toto', 'toto')); $this->assertEquals('tototototiti', RestoUtil::superImplode('toto', array('toto', 'titi', null))); $this->assertEquals('http://localhost/titi/toto/tata.format?huhu=tty.json', RestoUtil::updateUrlFormat('http://localhost/titi/toto/tata?huhu=tty.json', 'format')); $this->assertEquals('http://localhost/tata.format?huhu=tty.json', RestoUtil::updateUrlFormat('http://localhost/tata.json?huhu=tty.json', 'format')); $this->assertEquals('1977-01-01T00:00:00Z', RestoUtil::toISO8601('1977')); $this->assertEquals('1977-02-01T00:00:00Z', RestoUtil::toISO8601('1977-02')); $this->assertEquals('1977-02-10T00:00:00Z', RestoUtil::toISO8601('1977-02-10')); $this->assertEquals('1977-02-10T12:11:30Z', RestoUtil::toISO8601('1977-02-10T12:11:30Z')); $this->assertEquals(array('toto', 'titi', 'tata', 'tet', ' tifi ', 'cdd'), RestoUtil::splitString('toto"titi"tata tet" tifi "cdd')); $this->assertEquals(false, RestoUtil::isUrl(null)); $this->assertEquals(false, RestoUtil::isUrl('tokijhfomqzcdna')); $this->assertEquals(true, RestoUtil::isUrl('http://tititit.com/foiherfoh')); $this->assertEquals(null, RestoUtil::sanitize(null)); $this->assertEquals(array('tototot', 'tydsnc'), RestoUtil::sanitize(array('tototot', 'tydsnc'))); $this->assertEquals('ogfdrhefvdjsncosd', RestoUtil::sanitize('ogfdrhefvdjsncosd')); $this->assertEquals('', RestoUtil::kvpsToQueryString('ogfdrhefvdjsncosd')); $this->assertEquals('?&toto=ttiti&frfr=trtr', RestoUtil::kvpsToQueryString(array('toto' => 'ttiti', 'frfr' => 'trtr'))); $this->assertEquals('?&toto=ttiti&frfr=trtr&arr[]=titi&arr[]=toto', RestoUtil::kvpsToQueryString(array('toto' => 'ttiti', 'frfr' => 'trtr', 'arr' => array('toto', 'titi')))); $this->assertEquals(array('totot_tiiti_titi' => ''), RestoUtil::queryStringToKvps('totot tiiti titi')); }
public function download() { /* * Not downloadable */ if (!isset($this->featureArray['properties']['services']) || !isset($this->featureArray['properties']['services']['download'])) { RestoLogUtil::httpError(404); } /* * Download hosted resource with support of Range and Partial Content * (See http://stackoverflow.com/questions/157318/resumable-downloads-when-using-php-to-send-the-file) */ if (isset($this->featureArray['properties']['resourceInfos'])) { if (!isset($this->featureArray['properties']['resourceInfos']['path']) || !is_file($this->featureArray['properties']['resourceInfos']['path'])) { RestoLogUtil::httpError(404); } return $this->streamLocalUrl(realpath($this->featureArray['properties']['resourceInfos']['path']), isset($this->featureArray['properties']['resourceInfos']['mimeType']) ? $this->featureArray['properties']['resourceInfos']['mimeType'] : 'application/octet-stream'); } else { if (RestoUtil::isUrl($this->featureArray['properties']['services']['download']['url'])) { return $this->streamExternalUrl(); } else { RestoLogUtil::httpError(404); } } }
/** * 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; }
/** * Generate items informations * * Get an array containing two lists : * -> items : Downloadable items with url * -> errors : Not downloadable items with explanations * * @return array */ private function generateItemsInformations() { /* * Features with errors */ $errors = array(); /* * Features without errors */ $items = array(); /* * Loop over items */ foreach ($this->order['items'] as $item) { /* * Check if item get an id */ if (!isset($item['id'])) { array_push($errors, array('type' => 'Feature', 'ErrorMessage' => 'Wrong item', 'ErrorCode' => 404, 'properties' => isset($item['properties']) ? $item['properties'] : null)); continue; } /* * Create RestoFeature */ $feature = new RestoFeature($this->context, $this->user, array('featureIdentifier' => $item['id'])); /* * Check if User is fullfilling license requirements */ if (!$feature->getLicense()->isApplicableToUser($this->user)) { array_push($errors, array('type' => 'Feature', 'id' => $feature->identifier, 'ErrorMessage' => 'User does not fulfill license requirements', 'ErrorCode' => 403, 'properties' => isset($item['properties']) ? $item['properties'] : null)); continue; } /* * Check if user has to sign license */ if ($feature->getLicense()->hasToBeSignedByUser($this->user)) { array_push($errors, array('type' => 'Feature', 'id' => $feature->identifier, 'ErrorMessage' => 'User has to sign license', 'ErrorCode' => 3002, 'license' => $feature->getLicense()->toArray(), 'properties' => isset($item['properties']) ? $item['properties'] : null)); continue; } /* * Check if item is valid */ if (!isset($item['properties']) || !isset($item['properties']['services']) || !isset($item['properties']['services']['download'])) { array_push($errors, array('type' => 'Feature', 'id' => $feature->identifier, 'ErrorMessage' => 'Invalid item', 'ErrorCode' => 404, 'properties' => isset($item['properties']) ? $item['properties'] : null)); continue; } /* * Check is item is downloadable */ if (!isset($item['properties']['services']['download']['url']) || !RestoUtil::isUrl($item['properties']['services']['download']['url'])) { array_push($errors, array('type' => 'Feature', 'id' => $feature->identifier, 'ErrorMessage' => 'Item not downloadable', 'ErrorCode' => 404, 'properties' => isset($item['properties']) ? $item['properties'] : null)); continue; } /* * Check user rights */ if (!$this->user->hasRightsTo(RestoUser::DOWNLOAD, array('collectionName' => $item['properties']['collection'], 'featureIdentifier' => $feature->identifier))) { array_push($errors, array('type' => 'Feature', 'id' => $feature->identifier, 'ErrorMessage' => "User hasn't enough rights. Please contact an administrator", 'ErrorCode' => 403, 'properties' => isset($item['properties']) ? $item['properties'] : null)); continue; } /* * Update local download url with a shared link */ $exploded = explode('?', $item['properties']['services']['download']['url']); if ($exploded[0] === $this->context->baseUrl . join('/', array('/collections', $item['properties']['collection'], $feature->identifier, 'download'))) { $item['properties']['services']['download']['url'] = $this->getSharedLink($item['properties']['services']['download']['url']); } /* * Add item to downloadable items list */ array_push($items, array('type' => 'Feature', 'id' => $feature->identifier, 'properties' => isset($item['properties']) ? $item['properties'] : null)); } /* * Return array containing errors and downloadable items */ return array('errors' => $errors, 'items' => $items); }
/** * Set download service * * @param array $properties * @param string $thisUrl * @param RestoCollection $collection */ private function setDownloadService(&$properties, $thisUrl, $collection) { $properties['services']['download'] = array('url' => RestoUtil::isUrl($properties['resource']) ? $properties['resource'] : $thisUrl . '/download'); $properties['services']['download']['mimeType'] = isset($properties['resourceMimeType']) ? $properties['resourceMimeType'] : 'application/unknown'; if (isset($properties['resourceSize']) && $properties['resourceSize']) { $properties['services']['download']['size'] = $properties['resourceSize']; } if (isset($properties['resourceChecksum'])) { $properties['services']['download']['checksum'] = $properties['resourceChecksum']; } /* * If resource is local (i.e. not external url), set resourceInfos array */ if (!RestoUtil::isUrl($properties['resource'])) { $properties['resourceInfos'] = array('path' => method_exists($collection->model, 'generateResourcePath') ? $collection->model->generateResourcePath($properties) : $properties['resource'], 'mimeType' => $properties['services']['download']['mimeType'], 'size' => isset($properties['services']['download']['size']) ? $properties['services']['download']['size'] : null, 'checksum' => isset($properties['services']['download']['checksum']) ? $properties['services']['download']['checksum'] : null); } }