From 8395d0b76145969f4b8940a415af5e46528f04a5 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 10 Mar 2016 18:48:21 +0100 Subject: Adds a route for ATOM and RSS feeds page --- application/Router.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'application') diff --git a/application/Router.php b/application/Router.php index 6185f08e..d98312b5 100644 --- a/application/Router.php +++ b/application/Router.php @@ -15,6 +15,10 @@ class Router public static $PAGE_DAILY = 'daily'; + public static $PAGE_FEED_ATOM = 'atom'; + + public static $PAGE_FEED_RSS = 'rss'; + public static $PAGE_TOOLS = 'tools'; public static $PAGE_CHANGEPASSWORD = 'changepasswd'; @@ -79,6 +83,14 @@ class Router return self::$PAGE_DAILY; } + if (startsWith($query, 'do='. self::$PAGE_FEED_ATOM)) { + return self::$PAGE_FEED_ATOM; + } + + if (startsWith($query, 'do='. self::$PAGE_FEED_RSS)) { + return self::$PAGE_FEED_RSS; + } + // At this point, only loggedin pages. if (!$loggedIn) { return self::$PAGE_LINKLIST; -- cgit v1.2.3 From 69c474b96612dc64fc2cb66f1196251cafa08445 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 10 Mar 2016 19:01:30 +0100 Subject: Refactor showAtom, and make it use the ATOM template Minor changes: * Fix the date which was in a invalid format. * Avoid empty categories (tags). * Use the locale to set the language --- application/Utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'application') diff --git a/application/Utils.php b/application/Utils.php index 3d819716..bcf5bdb5 100644 --- a/application/Utils.php +++ b/application/Utils.php @@ -226,7 +226,7 @@ function space2nbsp($text) * * @return string formatted description. */ -function format_description($description, $redirector) { +function format_description($description, $redirector = false) { return nl2br(space2nbsp(text2clickable($description, $redirector))); } -- cgit v1.2.3 From 82e3680203896f024958ae969e2c4fccee9682f4 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 12 Mar 2016 16:08:01 +0100 Subject: Create a FeedBuilder class which build data for both ATOM and RSS feed. --- application/FeedBuilder.php | 295 ++++++++++++++++++++++++++++++++++++++++++++ application/Router.php | 2 +- 2 files changed, 296 insertions(+), 1 deletion(-) create mode 100644 application/FeedBuilder.php (limited to 'application') diff --git a/application/FeedBuilder.php b/application/FeedBuilder.php new file mode 100644 index 00000000..50e09831 --- /dev/null +++ b/application/FeedBuilder.php @@ -0,0 +1,295 @@ +linkDB = $linkDB; + $this->feedType = $feedType; + $this->serverInfo = $serverInfo; + $this->userInput = $userInput; + $this->isLoggedIn = $isLoggedIn; + } + + /** + * Build data for feed templates. + * + * @return array Formatted data for feeds templates. + */ + public function buildData() + { + // Optionally filter the results: + $searchtags = !empty($this->userInput['searchtags']) ? escape($this->userInput['searchtags']) : ''; + $searchterm = !empty($this->userInput['searchterm']) ? escape($this->userInput['searchterm']) : ''; + if (! empty($searchtags) && ! empty($searchterm)) { + $linksToDisplay = $this->linkDB->filter( + LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, + array($searchtags, $searchterm) + ); + } + elseif ($searchtags) { + $linksToDisplay = $this->linkDB->filter(LinkFilter::$FILTER_TAG, $searchtags); + } + elseif ($searchterm) { + $linksToDisplay = $this->linkDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); + } + else { + $linksToDisplay = $this->linkDB; + } + + $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay)); + + // Can't use array_keys() because $link is a LinkDB instance and not a real array. + $keys = array(); + foreach ($linksToDisplay as $key => $value) { + $keys[] = $key; + } + + $pageaddr = escape(index_url($this->serverInfo)); + $linkDisplayed = array(); + for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++) { + $linkDisplayed[$keys[$i]] = $this->buildItem($linksToDisplay[$keys[$i]], $pageaddr); + } + + $data['language'] = $this->getTypeLanguage(); + $data['pubsubhub_url'] = $this->pubsubhubUrl; + $data['last_update'] = $this->getLatestDateFormatted(); + $data['show_dates'] = !$this->hideDates || $this->isLoggedIn; + // Remove leading slash from REQUEST_URI. + $data['self_link'] = $pageaddr . escape(ltrim($this->serverInfo['REQUEST_URI'], '/')); + $data['index_url'] = $pageaddr; + $data['usepermalinks'] = $this->usePermalinks === true; + $data['links'] = $linkDisplayed; + + return $data; + } + + /** + * Build a feed item (one per shaare). + * + * @param array $link Single link array extracted from LinkDB. + * @param string $pageaddr Index URL. + * + * @return array Link array with feed attributes. + */ + protected function buildItem($link, $pageaddr) + { + $link['guid'] = $pageaddr .'?'. smallHash($link['linkdate']); + // Check for both signs of a note: starting with ? and 7 chars long. + if ($link['url'][0] === '?' && strlen($link['url']) === 7) { + $link['url'] = $pageaddr . $link['url']; + } + if ($this->usePermalinks === true) { + $permalink = 'Direct link'; + } else { + $permalink = 'Permalink'; + } + $link['description'] = format_description($link['description']) . PHP_EOL .'
— '. $permalink; + + $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); + + if ($this->feedType == self::$FEED_RSS) { + $link['iso_date'] = $date->format(DateTime::RSS); + } else { + $link['iso_date'] = $date->format(DateTime::ATOM); + } + + // Save the more recent item. + if (empty($this->latestDate) || $this->latestDate < $date) { + $this->latestDate = $date; + } + + $taglist = array_filter(explode(' ', $link['tags']), 'strlen'); + uasort($taglist, 'strcasecmp'); + $link['taglist'] = $taglist; + + return $link; + } + + /** + * Assign PubSub hub URL. + * + * @param string $pubsubhubUrl PubSub hub url. + */ + public function setPubsubhubUrl($pubsubhubUrl) + { + $this->pubsubhubUrl = $pubsubhubUrl; + } + + /** + * Set this to true to use permalinks instead of direct links. + * + * @param boolean $usePermalinks true to force permalinks. + */ + public function setUsePermalinks($usePermalinks) + { + $this->usePermalinks = $usePermalinks; + } + + /** + * Set this to true to hide timestamps in feeds. + * + * @param boolean $hideDates true to enable. + */ + public function setHideDates($hideDates) + { + $this->hideDates = $hideDates; + } + + /** + * Set the locale. Used to show feed language. + * + * @param string $locale The locale (eg. 'fr_FR.UTF8'). + */ + public function setLocale($locale) + { + $this->locale = strtolower($locale); + } + + /** + * Get the language according to the feed type, based on the locale: + * + * - RSS format: en-us (default: 'en-en'). + * - ATOM format: fr (default: 'en'). + * + * @return string The language. + */ + public function getTypeLanguage() + { + // Use the locale do define the language, if available. + if (! empty($this->locale) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale)) { + $length = ($this->feedType == self::$FEED_RSS) ? 5 : 2; + return str_replace('_', '-', substr($this->locale, 0, $length)); + } + return ($this->feedType == self::$FEED_RSS) ? 'en-en' : 'en'; + } + + /** + * Format the latest item date found according to the feed type. + * + * Return an empty string if invalid DateTime is passed. + * + * @return string Formatted date. + */ + protected function getLatestDateFormatted() + { + if (empty($this->latestDate) || !$this->latestDate instanceof DateTime) { + return ''; + } + + $type = ($this->feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM; + return $this->latestDate->format($type); + } + + /** + * Returns the number of link to display according to 'nb' user input parameter. + * + * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS. + * If 'nb' is set to 'all', display all filtered links (max parameter). + * + * @param int $max maximum number of links to display. + * + * @return int number of links to display. + */ + public function getNbLinks($max) + { + if (empty($this->userInput['nb'])) { + return self::$DEFAULT_NB_LINKS; + } + + if ($this->userInput['nb'] == 'all') { + return $max; + } + + $intNb = intval($this->userInput['nb']); + if (! is_int($intNb) || $intNb == 0) { + return self::$DEFAULT_NB_LINKS; + } + + return $intNb; + } +} diff --git a/application/Router.php b/application/Router.php index d98312b5..a1e594a0 100644 --- a/application/Router.php +++ b/application/Router.php @@ -53,7 +53,7 @@ class Router * @param array $get $_SERVER['GET']. * @param bool $loggedIn true if authenticated user. * - * @return self::page found. + * @return string page found. */ public static function findPage($query, $get, $loggedIn) { -- cgit v1.2.3 From ee88a4bcc29da721cf43b750663aebeac4969517 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 20 Mar 2016 14:14:38 +0100 Subject: Makes escape a recursive function which handle array of strings --- application/Utils.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'application') diff --git a/application/Utils.php b/application/Utils.php index bcf5bdb5..5b8ca508 100644 --- a/application/Utils.php +++ b/application/Utils.php @@ -63,14 +63,22 @@ function endsWith($haystack, $needle, $case=true) /** * Htmlspecialchars wrapper + * Support multidimensional array of strings. * - * @param string $str the string to escape. + * @param mixed $input Data to escape: a single string or an array of strings. * * @return string escaped. */ -function escape($str) +function escape($input) { - return htmlspecialchars($str, ENT_COMPAT, 'UTF-8', false); + if (is_array($input)) { + $out = array(); + foreach($input as $key => $value) { + $out[$key] = escape($value); + } + return $out; + } + return htmlspecialchars($input, ENT_COMPAT, 'UTF-8', false); } /** -- cgit v1.2.3 From 528a6f8a232c060faf024008e4f8a09b4aa8dabc Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 21 Mar 2016 21:40:49 +0100 Subject: Refactor filter in LinkDB * search type now carried by LinkDB in order to factorize code between different search sources. * LinkDB->filter split in 3 method: filterSearch, filterHash, filterDay (we know what type of filter is needed). * filterHash now throw a LinkNotFoundException if it doesn't exist: internal implementation choice, still displays a 404. * Smallhash regex has been rewritten. * Unit tests update --- application/FeedBuilder.php | 18 +------------ application/LinkDB.php | 64 +++++++++++++++++++++++++++++++++++++++++---- application/LinkFilter.php | 14 +++++++++- application/Updater.php | 2 +- 4 files changed, 74 insertions(+), 24 deletions(-) (limited to 'application') diff --git a/application/FeedBuilder.php b/application/FeedBuilder.php index 50e09831..ddefe6ce 100644 --- a/application/FeedBuilder.php +++ b/application/FeedBuilder.php @@ -103,23 +103,7 @@ class FeedBuilder public function buildData() { // Optionally filter the results: - $searchtags = !empty($this->userInput['searchtags']) ? escape($this->userInput['searchtags']) : ''; - $searchterm = !empty($this->userInput['searchterm']) ? escape($this->userInput['searchterm']) : ''; - if (! empty($searchtags) && ! empty($searchterm)) { - $linksToDisplay = $this->linkDB->filter( - LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, - array($searchtags, $searchterm) - ); - } - elseif ($searchtags) { - $linksToDisplay = $this->linkDB->filter(LinkFilter::$FILTER_TAG, $searchtags); - } - elseif ($searchterm) { - $linksToDisplay = $this->linkDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); - } - else { - $linksToDisplay = $this->linkDB; - } + $linksToDisplay = $this->linkDB->filterSearch($this->userInput); $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay)); 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 } /** - * Filter links. + * Returns the shaare corresponding to a smallHash. * - * @param string $type Type of filter. - * @param mixed $request Search request, string or array. + * @param string $request QUERY_STRING server parameter. + * + * @return array $filtered array containing permalink data. + * + * @throws LinkNotFoundException if the smallhash is malformed or doesn't match any link. + */ + public function filterHash($request) + { + $request = substr($request, 0, 6); + $linkFilter = new LinkFilter($this->_links); + return $linkFilter->filter(LinkFilter::$FILTER_HASH, $request); + } + + /** + * Returns the list of articles for a given day. + * + * @param string $request day to filter. Format: YYYYMMDD. + * + * @return array list of shaare found. + */ + public function filterDay($request) { + $linkFilter = new LinkFilter($this->_links); + return $linkFilter->filter(LinkFilter::$FILTER_DAY, $request); + } + + /** + * Filter links according to search parameters. + * + * @param array $filterRequest Search request content. Supported keys: + * - searchtags: list of tags + * - searchterm: term search * @param bool $casesensitive Optional: Perform case sensitive filter * @param bool $privateonly Optional: Returns private links only if true. * - * @return array filtered links + * @return array filtered links, all links if no suitable filter was provided. */ - public function filter($type = '', $request = '', $casesensitive = false, $privateonly = false) + public function filterSearch($filterRequest = array(), $casesensitive = false, $privateonly = false) { + // Filter link database according to parameters. + $searchtags = !empty($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : ''; + $searchterm = !empty($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : ''; + + // Search tags + fullsearch. + if (empty($type) && ! empty($searchtags) && ! empty($searchterm)) { + $type = LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT; + $request = array($searchtags, $searchterm); + } + // Search by tags. + elseif (! empty($searchtags)) { + $type = LinkFilter::$FILTER_TAG; + $request = $searchtags; + } + // Fulltext search. + elseif (! empty($searchterm)) { + $type = LinkFilter::$FILTER_TEXT; + $request = $searchterm; + } + // Otherwise, display without filtering. + else { + $type = ''; + $request = ''; + } + $linkFilter = new LinkFilter($this->_links); return $linkFilter->filter($type, $request, $casesensitive, $privateonly); } 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 * Filter links according to parameters. * * @param string $type Type of filter (eg. tags, permalink, etc.). - * @param string $request Filter content. + * @param mixed $request Filter content. * @param bool $casesensitive Optional: Perform case sensitive filter if true. * @param bool $privateonly Optional: Only returns private links if true. * @@ -110,6 +110,8 @@ class LinkFilter * @param string $smallHash permalink hash. * * @return array $filtered array containing permalink data. + * + * @throws LinkNotFoundException if the smallhash doesn't match any link. */ private function filterSmallHash($smallHash) { @@ -121,6 +123,11 @@ class LinkFilter return $filtered; } } + + if (empty($filtered)) { + throw new LinkNotFoundException(); + } + return $filtered; } @@ -318,3 +325,8 @@ class LinkFilter return array_filter(explode(' ', trim($tagsOut)), 'strlen'); } } + +class LinkNotFoundException extends Exception +{ + protected $message = 'The link you are trying to reach does not exist or has been deleted.'; +} 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 */ public function updateMethodRenameDashTags() { - $linklist = $this->linkDB->filter(); + $linklist = $this->linkDB->filterSearch(); foreach ($linklist as $link) { $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']); $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true))); -- cgit v1.2.3