2016-12-22 14:36:45 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Shaarli\Api\Controllers;
|
|
|
|
|
|
|
|
use Shaarli\Api\ApiUtils;
|
|
|
|
use Shaarli\Api\Exceptions\ApiBadParametersException;
|
2016-12-24 10:30:21 +01:00
|
|
|
use Shaarli\Api\Exceptions\ApiLinkNotFoundException;
|
2016-12-22 14:36:45 +01:00
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Links
|
|
|
|
*
|
2019-05-25 15:52:27 +02:00
|
|
|
* REST API Controller: all services related to bookmarks collection.
|
2016-12-22 14:36:45 +01:00
|
|
|
*
|
|
|
|
* @package Api\Controllers
|
|
|
|
* @see http://shaarli.github.io/api-documentation/#links-links-collection
|
|
|
|
*/
|
|
|
|
class Links extends ApiController
|
|
|
|
{
|
|
|
|
/**
|
2019-05-25 15:52:27 +02:00
|
|
|
* @var int Number of bookmarks returned if no limit is provided.
|
2016-12-22 14:36:45 +01:00
|
|
|
*/
|
|
|
|
public static $DEFAULT_LIMIT = 20;
|
|
|
|
|
|
|
|
/**
|
2019-05-25 15:52:27 +02:00
|
|
|
* Retrieve a list of bookmarks, allowing different filters.
|
2016-12-22 14:36:45 +01:00
|
|
|
*
|
|
|
|
* @param Request $request Slim request.
|
|
|
|
* @param Response $response Slim response.
|
|
|
|
*
|
|
|
|
* @return Response response.
|
|
|
|
*
|
|
|
|
* @throws ApiBadParametersException Invalid parameters.
|
|
|
|
*/
|
|
|
|
public function getLinks($request, $response)
|
|
|
|
{
|
2017-01-17 18:51:40 +01:00
|
|
|
$private = $request->getParam('visibility');
|
2019-05-25 15:52:27 +02:00
|
|
|
$bookmarks = $this->bookmarkService->search(
|
2016-12-22 14:36:45 +01:00
|
|
|
[
|
|
|
|
'searchtags' => $request->getParam('searchtags', ''),
|
|
|
|
'searchterm' => $request->getParam('searchterm', ''),
|
|
|
|
],
|
2017-01-17 18:51:40 +01:00
|
|
|
$private
|
2016-12-22 14:36:45 +01:00
|
|
|
);
|
|
|
|
|
2019-05-25 15:52:27 +02:00
|
|
|
// Return bookmarks from the {offset}th link, starting from 0.
|
2016-12-22 14:36:45 +01:00
|
|
|
$offset = $request->getParam('offset');
|
|
|
|
if (! empty($offset) && ! ctype_digit($offset)) {
|
|
|
|
throw new ApiBadParametersException('Invalid offset');
|
|
|
|
}
|
|
|
|
$offset = ! empty($offset) ? intval($offset) : 0;
|
2019-05-25 15:52:27 +02:00
|
|
|
if ($offset > count($bookmarks)) {
|
2016-12-22 14:36:45 +01:00
|
|
|
return $response->withJson([], 200, $this->jsonStyle);
|
|
|
|
}
|
|
|
|
|
2019-05-25 15:52:27 +02:00
|
|
|
// limit parameter is either a number of bookmarks or 'all' for everything.
|
2016-12-22 14:36:45 +01:00
|
|
|
$limit = $request->getParam('limit');
|
|
|
|
if (empty($limit)) {
|
|
|
|
$limit = self::$DEFAULT_LIMIT;
|
2018-02-28 22:34:40 +01:00
|
|
|
} elseif (ctype_digit($limit)) {
|
2016-12-22 14:36:45 +01:00
|
|
|
$limit = intval($limit);
|
2018-02-28 22:34:40 +01:00
|
|
|
} elseif ($limit === 'all') {
|
2019-05-25 15:52:27 +02:00
|
|
|
$limit = count($bookmarks);
|
2016-12-22 14:36:45 +01:00
|
|
|
} else {
|
|
|
|
throw new ApiBadParametersException('Invalid limit');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'environment' is set by Slim and encapsulate $_SERVER.
|
2018-05-19 15:04:04 +02:00
|
|
|
$indexUrl = index_url($this->ci['environment']);
|
2016-12-22 14:36:45 +01:00
|
|
|
|
|
|
|
$out = [];
|
2018-05-19 15:04:04 +02:00
|
|
|
$index = 0;
|
2019-05-25 15:52:27 +02:00
|
|
|
foreach ($bookmarks as $bookmark) {
|
2016-12-22 14:36:45 +01:00
|
|
|
if (count($out) >= $limit) {
|
|
|
|
break;
|
|
|
|
}
|
2018-05-19 15:04:04 +02:00
|
|
|
if ($index++ >= $offset) {
|
2019-05-25 15:52:27 +02:00
|
|
|
$out[] = ApiUtils::formatLink($bookmark, $indexUrl);
|
2016-12-22 14:36:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response->withJson($out, 200, $this->jsonStyle);
|
|
|
|
}
|
2016-12-24 10:30:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a single formatted link by its ID.
|
|
|
|
*
|
|
|
|
* @param Request $request Slim request.
|
|
|
|
* @param Response $response Slim response.
|
|
|
|
* @param array $args Path parameters. including the ID.
|
|
|
|
*
|
|
|
|
* @return Response containing the link array.
|
|
|
|
*
|
|
|
|
* @throws ApiLinkNotFoundException generating a 404 error.
|
|
|
|
*/
|
|
|
|
public function getLink($request, $response, $args)
|
|
|
|
{
|
2019-05-25 15:52:27 +02:00
|
|
|
if (!$this->bookmarkService->exists($args['id'])) {
|
2016-12-24 10:30:21 +01:00
|
|
|
throw new ApiLinkNotFoundException();
|
|
|
|
}
|
|
|
|
$index = index_url($this->ci['environment']);
|
2019-05-25 15:52:27 +02:00
|
|
|
$out = ApiUtils::formatLink($this->bookmarkService->get($args['id']), $index);
|
2017-01-05 15:58:24 +01:00
|
|
|
|
2016-12-24 10:30:21 +01:00
|
|
|
return $response->withJson($out, 200, $this->jsonStyle);
|
|
|
|
}
|
2017-01-05 15:58:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new link from posted request body.
|
|
|
|
*
|
|
|
|
* @param Request $request Slim request.
|
|
|
|
* @param Response $response Slim response.
|
|
|
|
*
|
|
|
|
* @return Response response.
|
|
|
|
*/
|
|
|
|
public function postLink($request, $response)
|
|
|
|
{
|
|
|
|
$data = $request->getParsedBody();
|
2019-05-25 15:52:27 +02:00
|
|
|
$bookmark = ApiUtils::buildLinkFromRequest($data, $this->conf->get('privacy.default_private_links'));
|
2017-01-05 15:58:24 +01:00
|
|
|
// duplicate by URL, return 409 Conflict
|
2019-05-25 15:52:27 +02:00
|
|
|
if (! empty($bookmark->getUrl())
|
|
|
|
&& ! empty($dup = $this->bookmarkService->findByUrl($bookmark->getUrl()))
|
|
|
|
) {
|
2017-01-05 15:58:24 +01:00
|
|
|
return $response->withJson(
|
|
|
|
ApiUtils::formatLink($dup, index_url($this->ci['environment'])),
|
|
|
|
409,
|
|
|
|
$this->jsonStyle
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-25 15:52:27 +02:00
|
|
|
$this->bookmarkService->add($bookmark);
|
|
|
|
$out = ApiUtils::formatLink($bookmark, index_url($this->ci['environment']));
|
2020-10-29 03:57:40 +01:00
|
|
|
$redirect = $this->ci->router->pathFor('getLink', ['id' => $bookmark->getId()]);
|
2017-01-05 15:58:24 +01:00
|
|
|
return $response->withAddedHeader('Location', $redirect)
|
|
|
|
->withJson($out, 201, $this->jsonStyle);
|
|
|
|
}
|
2017-04-01 11:11:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates an existing link from posted request body.
|
|
|
|
*
|
|
|
|
* @param Request $request Slim request.
|
|
|
|
* @param Response $response Slim response.
|
|
|
|
* @param array $args Path parameters. including the ID.
|
|
|
|
*
|
|
|
|
* @return Response response.
|
|
|
|
*
|
|
|
|
* @throws ApiLinkNotFoundException generating a 404 error.
|
|
|
|
*/
|
|
|
|
public function putLink($request, $response, $args)
|
|
|
|
{
|
2019-05-25 15:52:27 +02:00
|
|
|
if (! $this->bookmarkService->exists($args['id'])) {
|
2017-04-01 11:11:25 +02:00
|
|
|
throw new ApiLinkNotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$index = index_url($this->ci['environment']);
|
|
|
|
$data = $request->getParsedBody();
|
|
|
|
|
2019-05-25 15:52:27 +02:00
|
|
|
$requestBookmark = ApiUtils::buildLinkFromRequest($data, $this->conf->get('privacy.default_private_links'));
|
2017-04-01 11:11:25 +02:00
|
|
|
// duplicate URL on a different link, return 409 Conflict
|
2019-05-25 15:52:27 +02:00
|
|
|
if (! empty($requestBookmark->getUrl())
|
|
|
|
&& ! empty($dup = $this->bookmarkService->findByUrl($requestBookmark->getUrl()))
|
|
|
|
&& $dup->getId() != $args['id']
|
2017-04-01 11:11:25 +02:00
|
|
|
) {
|
|
|
|
return $response->withJson(
|
|
|
|
ApiUtils::formatLink($dup, $index),
|
|
|
|
409,
|
|
|
|
$this->jsonStyle
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-25 15:52:27 +02:00
|
|
|
$responseBookmark = $this->bookmarkService->get($args['id']);
|
|
|
|
$responseBookmark = ApiUtils::updateLink($responseBookmark, $requestBookmark);
|
|
|
|
$this->bookmarkService->set($responseBookmark);
|
2017-04-01 11:11:25 +02:00
|
|
|
|
2019-05-25 15:52:27 +02:00
|
|
|
$out = ApiUtils::formatLink($responseBookmark, $index);
|
2017-04-01 11:11:25 +02:00
|
|
|
return $response->withJson($out, 200, $this->jsonStyle);
|
|
|
|
}
|
2017-05-06 17:32:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an existing link by its ID.
|
|
|
|
*
|
|
|
|
* @param Request $request Slim request.
|
|
|
|
* @param Response $response Slim response.
|
|
|
|
* @param array $args Path parameters. including the ID.
|
|
|
|
*
|
|
|
|
* @return Response response.
|
|
|
|
*
|
|
|
|
* @throws ApiLinkNotFoundException generating a 404 error.
|
|
|
|
*/
|
|
|
|
public function deleteLink($request, $response, $args)
|
|
|
|
{
|
2019-05-25 15:52:27 +02:00
|
|
|
if (! $this->bookmarkService->exists($args['id'])) {
|
2017-05-06 17:32:16 +02:00
|
|
|
throw new ApiLinkNotFoundException();
|
|
|
|
}
|
2019-05-25 15:52:27 +02:00
|
|
|
$bookmark = $this->bookmarkService->get($args['id']);
|
|
|
|
$this->bookmarkService->remove($bookmark);
|
2017-05-06 17:32:16 +02:00
|
|
|
|
|
|
|
return $response->withStatus(204);
|
|
|
|
}
|
2016-12-22 14:36:45 +01:00
|
|
|
}
|