aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
Diffstat (limited to 'application')
-rw-r--r--application/FeedBuilder.php279
-rw-r--r--application/LinkDB.php64
-rw-r--r--application/LinkFilter.php14
-rw-r--r--application/LinkUtils.php2
-rw-r--r--application/Router.php14
-rw-r--r--application/Updater.php2
-rw-r--r--application/Utils.php16
7 files changed, 378 insertions, 13 deletions
diff --git a/application/FeedBuilder.php b/application/FeedBuilder.php
new file mode 100644
index 00000000..ddefe6ce
--- /dev/null
+++ b/application/FeedBuilder.php
@@ -0,0 +1,279 @@
1<?php
2
3/**
4 * FeedBuilder class.
5 *
6 * Used to build ATOM and RSS feeds data.
7 */
8class FeedBuilder
9{
10 /**
11 * @var string Constant: RSS feed type.
12 */
13 public static $FEED_RSS = 'rss';
14
15 /**
16 * @var string Constant: ATOM feed type.
17 */
18 public static $FEED_ATOM = 'atom';
19
20 /**
21 * @var string Default language if the locale isn't set.
22 */
23 public static $DEFAULT_LANGUAGE = 'en-en';
24
25 /**
26 * @var int Number of links to display in a feed by default.
27 */
28 public static $DEFAULT_NB_LINKS = 50;
29
30 /**
31 * @var LinkDB instance.
32 */
33 protected $linkDB;
34
35 /**
36 * @var string RSS or ATOM feed.
37 */
38 protected $feedType;
39
40 /**
41 * @var array $_SERVER.
42 */
43 protected $serverInfo;
44
45 /**
46 * @var array $_GET.
47 */
48 protected $userInput;
49
50 /**
51 * @var boolean True if the user is currently logged in, false otherwise.
52 */
53 protected $isLoggedIn;
54
55 /**
56 * @var boolean Use permalinks instead of direct links if true.
57 */
58 protected $usePermalinks;
59
60 /**
61 * @var boolean true to hide dates in feeds.
62 */
63 protected $hideDates;
64
65 /**
66 * @var string PubSub hub URL.
67 */
68 protected $pubsubhubUrl;
69
70 /**
71 * @var string server locale.
72 */
73 protected $locale;
74
75 /**
76 * @var DateTime Latest item date.
77 */
78 protected $latestDate;
79
80 /**
81 * Feed constructor.
82 *
83 * @param LinkDB $linkDB LinkDB instance.
84 * @param string $feedType Type of feed.
85 * @param array $serverInfo $_SERVER.
86 * @param array $userInput $_GET.
87 * @param boolean $isLoggedIn True if the user is currently logged in, false otherwise.
88 */
89 public function __construct($linkDB, $feedType, $serverInfo, $userInput, $isLoggedIn)
90 {
91 $this->linkDB = $linkDB;
92 $this->feedType = $feedType;
93 $this->serverInfo = $serverInfo;
94 $this->userInput = $userInput;
95 $this->isLoggedIn = $isLoggedIn;
96 }
97
98 /**
99 * Build data for feed templates.
100 *
101 * @return array Formatted data for feeds templates.
102 */
103 public function buildData()
104 {
105 // Optionally filter the results:
106 $linksToDisplay = $this->linkDB->filterSearch($this->userInput);
107
108 $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay));
109
110 // Can't use array_keys() because $link is a LinkDB instance and not a real array.
111 $keys = array();
112 foreach ($linksToDisplay as $key => $value) {
113 $keys[] = $key;
114 }
115
116 $pageaddr = escape(index_url($this->serverInfo));
117 $linkDisplayed = array();
118 for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++) {
119 $linkDisplayed[$keys[$i]] = $this->buildItem($linksToDisplay[$keys[$i]], $pageaddr);
120 }
121
122 $data['language'] = $this->getTypeLanguage();
123 $data['pubsubhub_url'] = $this->pubsubhubUrl;
124 $data['last_update'] = $this->getLatestDateFormatted();
125 $data['show_dates'] = !$this->hideDates || $this->isLoggedIn;
126 // Remove leading slash from REQUEST_URI.
127 $data['self_link'] = $pageaddr . escape(ltrim($this->serverInfo['REQUEST_URI'], '/'));
128 $data['index_url'] = $pageaddr;
129 $data['usepermalinks'] = $this->usePermalinks === true;
130 $data['links'] = $linkDisplayed;
131
132 return $data;
133 }
134
135 /**
136 * Build a feed item (one per shaare).
137 *
138 * @param array $link Single link array extracted from LinkDB.
139 * @param string $pageaddr Index URL.
140 *
141 * @return array Link array with feed attributes.
142 */
143 protected function buildItem($link, $pageaddr)
144 {
145 $link['guid'] = $pageaddr .'?'. smallHash($link['linkdate']);
146 // Check for both signs of a note: starting with ? and 7 chars long.
147 if ($link['url'][0] === '?' && strlen($link['url']) === 7) {
148 $link['url'] = $pageaddr . $link['url'];
149 }
150 if ($this->usePermalinks === true) {
151 $permalink = '<a href="'. $link['url'] .'" title="Direct link">Direct link</a>';
152 } else {
153 $permalink = '<a href="'. $link['guid'] .'" title="Permalink">Permalink</a>';
154 }
155 $link['description'] = format_description($link['description']) . PHP_EOL .'<br>&#8212; '. $permalink;
156
157 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
158
159 if ($this->feedType == self::$FEED_RSS) {
160 $link['iso_date'] = $date->format(DateTime::RSS);
161 } else {
162 $link['iso_date'] = $date->format(DateTime::ATOM);
163 }
164
165 // Save the more recent item.
166 if (empty($this->latestDate) || $this->latestDate < $date) {
167 $this->latestDate = $date;
168 }
169
170 $taglist = array_filter(explode(' ', $link['tags']), 'strlen');
171 uasort($taglist, 'strcasecmp');
172 $link['taglist'] = $taglist;
173
174 return $link;
175 }
176
177 /**
178 * Assign PubSub hub URL.
179 *
180 * @param string $pubsubhubUrl PubSub hub url.
181 */
182 public function setPubsubhubUrl($pubsubhubUrl)
183 {
184 $this->pubsubhubUrl = $pubsubhubUrl;
185 }
186
187 /**
188 * Set this to true to use permalinks instead of direct links.
189 *
190 * @param boolean $usePermalinks true to force permalinks.
191 */
192 public function setUsePermalinks($usePermalinks)
193 {
194 $this->usePermalinks = $usePermalinks;
195 }
196
197 /**
198 * Set this to true to hide timestamps in feeds.
199 *
200 * @param boolean $hideDates true to enable.
201 */
202 public function setHideDates($hideDates)
203 {
204 $this->hideDates = $hideDates;
205 }
206
207 /**
208 * Set the locale. Used to show feed language.
209 *
210 * @param string $locale The locale (eg. 'fr_FR.UTF8').
211 */
212 public function setLocale($locale)
213 {
214 $this->locale = strtolower($locale);
215 }
216
217 /**
218 * Get the language according to the feed type, based on the locale:
219 *
220 * - RSS format: en-us (default: 'en-en').
221 * - ATOM format: fr (default: 'en').
222 *
223 * @return string The language.
224 */
225 public function getTypeLanguage()
226 {
227 // Use the locale do define the language, if available.
228 if (! empty($this->locale) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale)) {
229 $length = ($this->feedType == self::$FEED_RSS) ? 5 : 2;
230 return str_replace('_', '-', substr($this->locale, 0, $length));
231 }
232 return ($this->feedType == self::$FEED_RSS) ? 'en-en' : 'en';
233 }
234
235 /**
236 * Format the latest item date found according to the feed type.
237 *
238 * Return an empty string if invalid DateTime is passed.
239 *
240 * @return string Formatted date.
241 */
242 protected function getLatestDateFormatted()
243 {
244 if (empty($this->latestDate) || !$this->latestDate instanceof DateTime) {
245 return '';
246 }
247
248 $type = ($this->feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM;
249 return $this->latestDate->format($type);
250 }
251
252 /**
253 * Returns the number of link to display according to 'nb' user input parameter.
254 *
255 * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS.
256 * If 'nb' is set to 'all', display all filtered links (max parameter).
257 *
258 * @param int $max maximum number of links to display.
259 *
260 * @return int number of links to display.
261 */
262 public function getNbLinks($max)
263 {
264 if (empty($this->userInput['nb'])) {
265 return self::$DEFAULT_NB_LINKS;
266 }
267
268 if ($this->userInput['nb'] == 'all') {
269 return $max;
270 }
271
272 $intNb = intval($this->userInput['nb']);
273 if (! is_int($intNb) || $intNb == 0) {
274 return self::$DEFAULT_NB_LINKS;
275 }
276
277 return $intNb;
278 }
279}
diff --git a/application/LinkDB.php b/application/LinkDB.php
index 1b505620..a62341fc 100644
--- a/application/LinkDB.php
+++ b/application/LinkDB.php
@@ -341,17 +341,71 @@ You use the community supported version of the original Shaarli project, by Seba
341 } 341 }
342 342
343 /** 343 /**
344 * Filter links. 344 * Returns the shaare corresponding to a smallHash.
345 * 345 *
346 * @param string $type Type of filter. 346 * @param string $request QUERY_STRING server parameter.
347 * @param mixed $request Search request, string or array. 347 *
348 * @return array $filtered array containing permalink data.
349 *
350 * @throws LinkNotFoundException if the smallhash is malformed or doesn't match any link.
351 */
352 public function filterHash($request)
353 {
354 $request = substr($request, 0, 6);
355 $linkFilter = new LinkFilter($this->_links);
356 return $linkFilter->filter(LinkFilter::$FILTER_HASH, $request);
357 }
358
359 /**
360 * Returns the list of articles for a given day.
361 *
362 * @param string $request day to filter. Format: YYYYMMDD.
363 *
364 * @return array list of shaare found.
365 */
366 public function filterDay($request) {
367 $linkFilter = new LinkFilter($this->_links);
368 return $linkFilter->filter(LinkFilter::$FILTER_DAY, $request);
369 }
370
371 /**
372 * Filter links according to search parameters.
373 *
374 * @param array $filterRequest Search request content. Supported keys:
375 * - searchtags: list of tags
376 * - searchterm: term search
348 * @param bool $casesensitive Optional: Perform case sensitive filter 377 * @param bool $casesensitive Optional: Perform case sensitive filter
349 * @param bool $privateonly Optional: Returns private links only if true. 378 * @param bool $privateonly Optional: Returns private links only if true.
350 * 379 *
351 * @return array filtered links 380 * @return array filtered links, all links if no suitable filter was provided.
352 */ 381 */
353 public function filter($type = '', $request = '', $casesensitive = false, $privateonly = false) 382 public function filterSearch($filterRequest = array(), $casesensitive = false, $privateonly = false)
354 { 383 {
384 // Filter link database according to parameters.
385 $searchtags = !empty($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : '';
386 $searchterm = !empty($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : '';
387
388 // Search tags + fullsearch.
389 if (empty($type) && ! empty($searchtags) && ! empty($searchterm)) {
390 $type = LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT;
391 $request = array($searchtags, $searchterm);
392 }
393 // Search by tags.
394 elseif (! empty($searchtags)) {
395 $type = LinkFilter::$FILTER_TAG;
396 $request = $searchtags;
397 }
398 // Fulltext search.
399 elseif (! empty($searchterm)) {
400 $type = LinkFilter::$FILTER_TEXT;
401 $request = $searchterm;
402 }
403 // Otherwise, display without filtering.
404 else {
405 $type = '';
406 $request = '';
407 }
408
355 $linkFilter = new LinkFilter($this->_links); 409 $linkFilter = new LinkFilter($this->_links);
356 return $linkFilter->filter($type, $request, $casesensitive, $privateonly); 410 return $linkFilter->filter($type, $request, $casesensitive, $privateonly);
357 } 411 }
diff --git a/application/LinkFilter.php b/application/LinkFilter.php
index 3fd803cb..5e0d8015 100644
--- a/application/LinkFilter.php
+++ b/application/LinkFilter.php
@@ -44,7 +44,7 @@ class LinkFilter
44 * Filter links according to parameters. 44 * Filter links according to parameters.
45 * 45 *
46 * @param string $type Type of filter (eg. tags, permalink, etc.). 46 * @param string $type Type of filter (eg. tags, permalink, etc.).
47 * @param string $request Filter content. 47 * @param mixed $request Filter content.
48 * @param bool $casesensitive Optional: Perform case sensitive filter if true. 48 * @param bool $casesensitive Optional: Perform case sensitive filter if true.
49 * @param bool $privateonly Optional: Only returns private links if true. 49 * @param bool $privateonly Optional: Only returns private links if true.
50 * 50 *
@@ -110,6 +110,8 @@ class LinkFilter
110 * @param string $smallHash permalink hash. 110 * @param string $smallHash permalink hash.
111 * 111 *
112 * @return array $filtered array containing permalink data. 112 * @return array $filtered array containing permalink data.
113 *
114 * @throws LinkNotFoundException if the smallhash doesn't match any link.
113 */ 115 */
114 private function filterSmallHash($smallHash) 116 private function filterSmallHash($smallHash)
115 { 117 {
@@ -121,6 +123,11 @@ class LinkFilter
121 return $filtered; 123 return $filtered;
122 } 124 }
123 } 125 }
126
127 if (empty($filtered)) {
128 throw new LinkNotFoundException();
129 }
130
124 return $filtered; 131 return $filtered;
125 } 132 }
126 133
@@ -318,3 +325,8 @@ class LinkFilter
318 return array_filter(explode(' ', trim($tagsOut)), 'strlen'); 325 return array_filter(explode(' ', trim($tagsOut)), 'strlen');
319 } 326 }
320} 327}
328
329class LinkNotFoundException extends Exception
330{
331 protected $message = 'The link you are trying to reach does not exist or has been deleted.';
332}
diff --git a/application/LinkUtils.php b/application/LinkUtils.php
index 26dd6b67..d8dc8b5e 100644
--- a/application/LinkUtils.php
+++ b/application/LinkUtils.php
@@ -9,7 +9,7 @@
9 */ 9 */
10function html_extract_title($html) 10function html_extract_title($html)
11{ 11{
12 if (preg_match('!<title>(.*)</title>!is', $html, $matches)) { 12 if (preg_match('!<title>(.*?)</title>!is', $html, $matches)) {
13 return trim(str_replace("\n", ' ', $matches[1])); 13 return trim(str_replace("\n", ' ', $matches[1]));
14 } 14 }
15 return false; 15 return false;
diff --git a/application/Router.php b/application/Router.php
index 6185f08e..a1e594a0 100644
--- a/application/Router.php
+++ b/application/Router.php
@@ -15,6 +15,10 @@ class Router
15 15
16 public static $PAGE_DAILY = 'daily'; 16 public static $PAGE_DAILY = 'daily';
17 17
18 public static $PAGE_FEED_ATOM = 'atom';
19
20 public static $PAGE_FEED_RSS = 'rss';
21
18 public static $PAGE_TOOLS = 'tools'; 22 public static $PAGE_TOOLS = 'tools';
19 23
20 public static $PAGE_CHANGEPASSWORD = 'changepasswd'; 24 public static $PAGE_CHANGEPASSWORD = 'changepasswd';
@@ -49,7 +53,7 @@ class Router
49 * @param array $get $_SERVER['GET']. 53 * @param array $get $_SERVER['GET'].
50 * @param bool $loggedIn true if authenticated user. 54 * @param bool $loggedIn true if authenticated user.
51 * 55 *
52 * @return self::page found. 56 * @return string page found.
53 */ 57 */
54 public static function findPage($query, $get, $loggedIn) 58 public static function findPage($query, $get, $loggedIn)
55 { 59 {
@@ -79,6 +83,14 @@ class Router
79 return self::$PAGE_DAILY; 83 return self::$PAGE_DAILY;
80 } 84 }
81 85
86 if (startsWith($query, 'do='. self::$PAGE_FEED_ATOM)) {
87 return self::$PAGE_FEED_ATOM;
88 }
89
90 if (startsWith($query, 'do='. self::$PAGE_FEED_RSS)) {
91 return self::$PAGE_FEED_RSS;
92 }
93
82 // At this point, only loggedin pages. 94 // At this point, only loggedin pages.
83 if (!$loggedIn) { 95 if (!$loggedIn) {
84 return self::$PAGE_LINKLIST; 96 return self::$PAGE_LINKLIST;
diff --git a/application/Updater.php b/application/Updater.php
index 773a1ffa..58c13c07 100644
--- a/application/Updater.php
+++ b/application/Updater.php
@@ -137,7 +137,7 @@ class Updater
137 */ 137 */
138 public function updateMethodRenameDashTags() 138 public function updateMethodRenameDashTags()
139 { 139 {
140 $linklist = $this->linkDB->filter(); 140 $linklist = $this->linkDB->filterSearch();
141 foreach ($linklist as $link) { 141 foreach ($linklist as $link) {
142 $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']); 142 $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']);
143 $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true))); 143 $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true)));
diff --git a/application/Utils.php b/application/Utils.php
index 3d819716..5b8ca508 100644
--- a/application/Utils.php
+++ b/application/Utils.php
@@ -63,14 +63,22 @@ function endsWith($haystack, $needle, $case=true)
63 63
64/** 64/**
65 * Htmlspecialchars wrapper 65 * Htmlspecialchars wrapper
66 * Support multidimensional array of strings.
66 * 67 *
67 * @param string $str the string to escape. 68 * @param mixed $input Data to escape: a single string or an array of strings.
68 * 69 *
69 * @return string escaped. 70 * @return string escaped.
70 */ 71 */
71function escape($str) 72function escape($input)
72{ 73{
73 return htmlspecialchars($str, ENT_COMPAT, 'UTF-8', false); 74 if (is_array($input)) {
75 $out = array();
76 foreach($input as $key => $value) {
77 $out[$key] = escape($value);
78 }
79 return $out;
80 }
81 return htmlspecialchars($input, ENT_COMPAT, 'UTF-8', false);
74} 82}
75 83
76/** 84/**
@@ -226,7 +234,7 @@ function space2nbsp($text)
226 * 234 *
227 * @return string formatted description. 235 * @return string formatted description.
228 */ 236 */
229function format_description($description, $redirector) { 237function format_description($description, $redirector = false) {
230 return nl2br(space2nbsp(text2clickable($description, $redirector))); 238 return nl2br(space2nbsp(text2clickable($description, $redirector)));
231} 239}
232 240