aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/LinkDB.php
diff options
context:
space:
mode:
Diffstat (limited to 'application/LinkDB.php')
-rw-r--r--application/LinkDB.php99
1 files changed, 88 insertions, 11 deletions
diff --git a/application/LinkDB.php b/application/LinkDB.php
index 1b505620..1cb70de0 100644
--- a/application/LinkDB.php
+++ b/application/LinkDB.php
@@ -66,21 +66,39 @@ class LinkDB implements Iterator, Countable, ArrayAccess
66 private $_redirector; 66 private $_redirector;
67 67
68 /** 68 /**
69 * Set this to `true` to urlencode link behind redirector link, `false` to leave it untouched.
70 *
71 * Example:
72 * anonym.to needs clean URL while dereferer.org needs urlencoded URL.
73 *
74 * @var boolean $redirectorEncode parameter: true or false
75 */
76 private $redirectorEncode;
77
78 /**
69 * Creates a new LinkDB 79 * Creates a new LinkDB
70 * 80 *
71 * Checks if the datastore exists; else, attempts to create a dummy one. 81 * Checks if the datastore exists; else, attempts to create a dummy one.
72 * 82 *
73 * @param string $datastore datastore file path. 83 * @param string $datastore datastore file path.
74 * @param boolean $isLoggedIn is the user logged in? 84 * @param boolean $isLoggedIn is the user logged in?
75 * @param boolean $hidePublicLinks if true all links are private. 85 * @param boolean $hidePublicLinks if true all links are private.
76 * @param string $redirector link redirector set in user settings. 86 * @param string $redirector link redirector set in user settings.
87 * @param boolean $redirectorEncode Enable urlencode on redirected urls (default: true).
77 */ 88 */
78 function __construct($datastore, $isLoggedIn, $hidePublicLinks, $redirector = '') 89 function __construct(
90 $datastore,
91 $isLoggedIn,
92 $hidePublicLinks,
93 $redirector = '',
94 $redirectorEncode = true
95 )
79 { 96 {
80 $this->_datastore = $datastore; 97 $this->_datastore = $datastore;
81 $this->_loggedIn = $isLoggedIn; 98 $this->_loggedIn = $isLoggedIn;
82 $this->_hidePublicLinks = $hidePublicLinks; 99 $this->_hidePublicLinks = $hidePublicLinks;
83 $this->_redirector = $redirector; 100 $this->_redirector = $redirector;
101 $this->redirectorEncode = $redirectorEncode === true;
84 $this->_checkDB(); 102 $this->_checkDB();
85 $this->_readDB(); 103 $this->_readDB();
86 } 104 }
@@ -278,7 +296,12 @@ You use the community supported version of the original Shaarli project, by Seba
278 296
279 // Do not use the redirector for internal links (Shaarli note URL starting with a '?'). 297 // Do not use the redirector for internal links (Shaarli note URL starting with a '?').
280 if (!empty($this->_redirector) && !startsWith($link['url'], '?')) { 298 if (!empty($this->_redirector) && !startsWith($link['url'], '?')) {
281 $link['real_url'] = $this->_redirector . urlencode($link['url']); 299 $link['real_url'] = $this->_redirector;
300 if ($this->redirectorEncode) {
301 $link['real_url'] .= urlencode(unescape($link['url']));
302 } else {
303 $link['real_url'] .= $link['url'];
304 }
282 } 305 }
283 else { 306 else {
284 $link['real_url'] = $link['url']; 307 $link['real_url'] = $link['url'];
@@ -341,17 +364,71 @@ You use the community supported version of the original Shaarli project, by Seba
341 } 364 }
342 365
343 /** 366 /**
344 * Filter links. 367 * Returns the shaare corresponding to a smallHash.
368 *
369 * @param string $request QUERY_STRING server parameter.
345 * 370 *
346 * @param string $type Type of filter. 371 * @return array $filtered array containing permalink data.
347 * @param mixed $request Search request, string or array. 372 *
373 * @throws LinkNotFoundException if the smallhash is malformed or doesn't match any link.
374 */
375 public function filterHash($request)
376 {
377 $request = substr($request, 0, 6);
378 $linkFilter = new LinkFilter($this->_links);
379 return $linkFilter->filter(LinkFilter::$FILTER_HASH, $request);
380 }
381
382 /**
383 * Returns the list of articles for a given day.
384 *
385 * @param string $request day to filter. Format: YYYYMMDD.
386 *
387 * @return array list of shaare found.
388 */
389 public function filterDay($request) {
390 $linkFilter = new LinkFilter($this->_links);
391 return $linkFilter->filter(LinkFilter::$FILTER_DAY, $request);
392 }
393
394 /**
395 * Filter links according to search parameters.
396 *
397 * @param array $filterRequest Search request content. Supported keys:
398 * - searchtags: list of tags
399 * - searchterm: term search
348 * @param bool $casesensitive Optional: Perform case sensitive filter 400 * @param bool $casesensitive Optional: Perform case sensitive filter
349 * @param bool $privateonly Optional: Returns private links only if true. 401 * @param bool $privateonly Optional: Returns private links only if true.
350 * 402 *
351 * @return array filtered links 403 * @return array filtered links, all links if no suitable filter was provided.
352 */ 404 */
353 public function filter($type = '', $request = '', $casesensitive = false, $privateonly = false) 405 public function filterSearch($filterRequest = array(), $casesensitive = false, $privateonly = false)
354 { 406 {
407 // Filter link database according to parameters.
408 $searchtags = !empty($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : '';
409 $searchterm = !empty($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : '';
410
411 // Search tags + fullsearch.
412 if (empty($type) && ! empty($searchtags) && ! empty($searchterm)) {
413 $type = LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT;
414 $request = array($searchtags, $searchterm);
415 }
416 // Search by tags.
417 elseif (! empty($searchtags)) {
418 $type = LinkFilter::$FILTER_TAG;
419 $request = $searchtags;
420 }
421 // Fulltext search.
422 elseif (! empty($searchterm)) {
423 $type = LinkFilter::$FILTER_TEXT;
424 $request = $searchterm;
425 }
426 // Otherwise, display without filtering.
427 else {
428 $type = '';
429 $request = '';
430 }
431
355 $linkFilter = new LinkFilter($this->_links); 432 $linkFilter = new LinkFilter($this->_links);
356 return $linkFilter->filter($type, $request, $casesensitive, $privateonly); 433 return $linkFilter->filter($type, $request, $casesensitive, $privateonly);
357 } 434 }