diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
commit | b6f678a5a1d15acf284ebcec16c905e976671ce1 (patch) | |
tree | 33c7da831482ed79c44896ef19c73c72ada84f2e /application/api/ApiUtils.php | |
parent | b14687036b9b800681197f51fdc47e62f0c88e2e (diff) | |
parent | 1c1520b6b98ab20201bfe15577782a52320339df (diff) | |
download | Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.gz Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.zst Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.zip |
Merge branch 'v0.12' into latest
Diffstat (limited to 'application/api/ApiUtils.php')
-rw-r--r-- | application/api/ApiUtils.php | 83 |
1 files changed, 40 insertions, 43 deletions
diff --git a/application/api/ApiUtils.php b/application/api/ApiUtils.php index 1e3ac02e..faebb8f5 100644 --- a/application/api/ApiUtils.php +++ b/application/api/ApiUtils.php | |||
@@ -2,6 +2,7 @@ | |||
2 | namespace Shaarli\Api; | 2 | namespace Shaarli\Api; |
3 | 3 | ||
4 | use Shaarli\Api\Exceptions\ApiAuthorizationException; | 4 | use Shaarli\Api\Exceptions\ApiAuthorizationException; |
5 | use Shaarli\Bookmark\Bookmark; | ||
5 | use Shaarli\Http\Base64Url; | 6 | use Shaarli\Http\Base64Url; |
6 | 7 | ||
7 | /** | 8 | /** |
@@ -15,6 +16,8 @@ class ApiUtils | |||
15 | * @param string $token JWT token extracted from the headers. | 16 | * @param string $token JWT token extracted from the headers. |
16 | * @param string $secret API secret set in the settings. | 17 | * @param string $secret API secret set in the settings. |
17 | * | 18 | * |
19 | * @return bool true on success | ||
20 | * | ||
18 | * @throws ApiAuthorizationException the token is not valid. | 21 | * @throws ApiAuthorizationException the token is not valid. |
19 | */ | 22 | */ |
20 | public static function validateJwtToken($token, $secret) | 23 | public static function validateJwtToken($token, $secret) |
@@ -45,33 +48,35 @@ class ApiUtils | |||
45 | ) { | 48 | ) { |
46 | throw new ApiAuthorizationException('Invalid JWT issued time'); | 49 | throw new ApiAuthorizationException('Invalid JWT issued time'); |
47 | } | 50 | } |
51 | |||
52 | return true; | ||
48 | } | 53 | } |
49 | 54 | ||
50 | /** | 55 | /** |
51 | * Format a Link for the REST API. | 56 | * Format a Link for the REST API. |
52 | * | 57 | * |
53 | * @param array $link Link data read from the datastore. | 58 | * @param Bookmark $bookmark Bookmark data read from the datastore. |
54 | * @param string $indexUrl Shaarli's index URL (used for relative URL). | 59 | * @param string $indexUrl Shaarli's index URL (used for relative URL). |
55 | * | 60 | * |
56 | * @return array Link data formatted for the REST API. | 61 | * @return array Link data formatted for the REST API. |
57 | */ | 62 | */ |
58 | public static function formatLink($link, $indexUrl) | 63 | public static function formatLink($bookmark, $indexUrl) |
59 | { | 64 | { |
60 | $out['id'] = $link['id']; | 65 | $out['id'] = $bookmark->getId(); |
61 | // Not an internal link | 66 | // Not an internal link |
62 | if (! is_note($link['url'])) { | 67 | if (! $bookmark->isNote()) { |
63 | $out['url'] = $link['url']; | 68 | $out['url'] = $bookmark->getUrl(); |
64 | } else { | 69 | } else { |
65 | $out['url'] = $indexUrl . $link['url']; | 70 | $out['url'] = rtrim($indexUrl, '/') . '/' . ltrim($bookmark->getUrl(), '/'); |
66 | } | 71 | } |
67 | $out['shorturl'] = $link['shorturl']; | 72 | $out['shorturl'] = $bookmark->getShortUrl(); |
68 | $out['title'] = $link['title']; | 73 | $out['title'] = $bookmark->getTitle(); |
69 | $out['description'] = $link['description']; | 74 | $out['description'] = $bookmark->getDescription(); |
70 | $out['tags'] = preg_split('/\s+/', $link['tags'], -1, PREG_SPLIT_NO_EMPTY); | 75 | $out['tags'] = $bookmark->getTags(); |
71 | $out['private'] = $link['private'] == true; | 76 | $out['private'] = $bookmark->isPrivate(); |
72 | $out['created'] = $link['created']->format(\DateTime::ATOM); | 77 | $out['created'] = $bookmark->getCreated()->format(\DateTime::ATOM); |
73 | if (! empty($link['updated'])) { | 78 | if (! empty($bookmark->getUpdated())) { |
74 | $out['updated'] = $link['updated']->format(\DateTime::ATOM); | 79 | $out['updated'] = $bookmark->getUpdated()->format(\DateTime::ATOM); |
75 | } else { | 80 | } else { |
76 | $out['updated'] = ''; | 81 | $out['updated'] = ''; |
77 | } | 82 | } |
@@ -79,7 +84,7 @@ class ApiUtils | |||
79 | } | 84 | } |
80 | 85 | ||
81 | /** | 86 | /** |
82 | * Convert a link given through a request, to a valid link for LinkDB. | 87 | * Convert a link given through a request, to a valid Bookmark for the datastore. |
83 | * | 88 | * |
84 | * If no URL is provided, it will generate a local note URL. | 89 | * If no URL is provided, it will generate a local note URL. |
85 | * If no title is provided, it will use the URL as title. | 90 | * If no title is provided, it will use the URL as title. |
@@ -87,50 +92,42 @@ class ApiUtils | |||
87 | * @param array $input Request Link. | 92 | * @param array $input Request Link. |
88 | * @param bool $defaultPrivate Request Link. | 93 | * @param bool $defaultPrivate Request Link. |
89 | * | 94 | * |
90 | * @return array Formatted link. | 95 | * @return Bookmark instance. |
91 | */ | 96 | */ |
92 | public static function buildLinkFromRequest($input, $defaultPrivate) | 97 | public static function buildLinkFromRequest($input, $defaultPrivate) |
93 | { | 98 | { |
94 | $input['url'] = ! empty($input['url']) ? cleanup_url($input['url']) : ''; | 99 | $bookmark = new Bookmark(); |
100 | $url = ! empty($input['url']) ? cleanup_url($input['url']) : ''; | ||
95 | if (isset($input['private'])) { | 101 | if (isset($input['private'])) { |
96 | $private = filter_var($input['private'], FILTER_VALIDATE_BOOLEAN); | 102 | $private = filter_var($input['private'], FILTER_VALIDATE_BOOLEAN); |
97 | } else { | 103 | } else { |
98 | $private = $defaultPrivate; | 104 | $private = $defaultPrivate; |
99 | } | 105 | } |
100 | 106 | ||
101 | $link = [ | 107 | $bookmark->setTitle(! empty($input['title']) ? $input['title'] : ''); |
102 | 'title' => ! empty($input['title']) ? $input['title'] : $input['url'], | 108 | $bookmark->setUrl($url); |
103 | 'url' => $input['url'], | 109 | $bookmark->setDescription(! empty($input['description']) ? $input['description'] : ''); |
104 | 'description' => ! empty($input['description']) ? $input['description'] : '', | 110 | $bookmark->setTags(! empty($input['tags']) ? $input['tags'] : []); |
105 | 'tags' => ! empty($input['tags']) ? implode(' ', $input['tags']) : '', | 111 | $bookmark->setPrivate($private); |
106 | 'private' => $private, | 112 | |
107 | 'created' => new \DateTime(), | 113 | return $bookmark; |
108 | ]; | ||
109 | return $link; | ||
110 | } | 114 | } |
111 | 115 | ||
112 | /** | 116 | /** |
113 | * Update link fields using an updated link object. | 117 | * Update link fields using an updated link object. |
114 | * | 118 | * |
115 | * @param array $oldLink data | 119 | * @param Bookmark $oldLink data |
116 | * @param array $newLink data | 120 | * @param Bookmark $newLink data |
117 | * | 121 | * |
118 | * @return array $oldLink updated with $newLink values | 122 | * @return Bookmark $oldLink updated with $newLink values |
119 | */ | 123 | */ |
120 | public static function updateLink($oldLink, $newLink) | 124 | public static function updateLink($oldLink, $newLink) |
121 | { | 125 | { |
122 | foreach (['title', 'url', 'description', 'tags', 'private'] as $field) { | 126 | $oldLink->setTitle($newLink->getTitle()); |
123 | $oldLink[$field] = $newLink[$field]; | 127 | $oldLink->setUrl($newLink->getUrl()); |
124 | } | 128 | $oldLink->setDescription($newLink->getDescription()); |
125 | $oldLink['updated'] = new \DateTime(); | 129 | $oldLink->setTags($newLink->getTags()); |
126 | 130 | $oldLink->setPrivate($newLink->isPrivate()); | |
127 | if (empty($oldLink['url'])) { | ||
128 | $oldLink['url'] = '?' . $oldLink['shorturl']; | ||
129 | } | ||
130 | |||
131 | if (empty($oldLink['title'])) { | ||
132 | $oldLink['title'] = $oldLink['url']; | ||
133 | } | ||
134 | 131 | ||
135 | return $oldLink; | 132 | return $oldLink; |
136 | } | 133 | } |
@@ -139,7 +136,7 @@ class ApiUtils | |||
139 | * Format a Tag for the REST API. | 136 | * Format a Tag for the REST API. |
140 | * | 137 | * |
141 | * @param string $tag Tag name | 138 | * @param string $tag Tag name |
142 | * @param int $occurrences Number of links using this tag | 139 | * @param int $occurrences Number of bookmarks using this tag |
143 | * | 140 | * |
144 | * @return array Link data formatted for the REST API. | 141 | * @return array Link data formatted for the REST API. |
145 | */ | 142 | */ |