From 1c2fdb98b1c7fbd85aa086641dd47bc9bd03c8eb Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 10 Mar 2016 18:41:23 +0100 Subject: Add method assignAll() to pageBuilder to assign an array of data --- index.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'index.php') diff --git a/index.php b/index.php index 850b350e..9100044e 100644 --- a/index.php +++ b/index.php @@ -637,6 +637,29 @@ class pageBuilder $this->tpl->assign($what,$where); } + /** + * Assign an array of data to the template builder. + * + * @param array $data Data to assign. + * + * @return false if invalid data. + */ + public function assignAll($data) + { + // Lazy initialization + if ($this->tpl === false) { + $this->initialize(); + } + + if (empty($data) || !is_array($data)){ + return false; + } + + foreach ($data as $key => $value) { + $this->assign($key, $value); + } + } + // Render a specific page (using a template). // e.g. pb.renderPage('picwall') public function renderPage($page) -- 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 --- index.php | 125 +++++++++++++++++++++++++++++--------------------------------- 1 file changed, 59 insertions(+), 66 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index 9100044e..14811c56 100644 --- a/index.php +++ b/index.php @@ -790,14 +790,10 @@ function showRSS() // ------------------------------------------------------------------------------------------ // Output the last N links in ATOM format. -function showATOM() +function showATOM($pageBuilder, $linkDB) { header('Content-Type: application/atom+xml; charset=utf-8'); - // $usepermalink : If true, use permalink instead of final link. - // User just has to add 'permalink' in URL parameters. e.g. http://mysite.com/shaarli/?do=atom&permalinks - $usepermalinks = isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS']; - // Cache system $query = $_SERVER["QUERY_STRING"]; $cache = new CachedPage( @@ -811,97 +807,90 @@ function showATOM() exit; } - // If cached was not found (or not usable), then read the database and build the response: - // Read links from database (and filter private links if used it not logged in). - $LINKSDB = new LinkDB( - $GLOBALS['config']['DATASTORE'], - isLoggedIn(), - $GLOBALS['config']['HIDE_PUBLIC_LINKS'], - $GLOBALS['redirector'] - ); + // $usepermalink : If true, use permalink instead of final link. + // User just has to add 'permalink' in URL parameters. e.g. http://mysite.com/shaarli/?do=atom&permalinks + $usepermalinks = isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS']; // Optionally filter the results: $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; if (! empty($searchtags) && ! empty($searchterm)) { - $linksToDisplay = $LINKSDB->filter( + $linksToDisplay = $linkDB->filter( LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, array($searchtags, $searchterm) ); } elseif ($searchtags) { - $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $searchtags); + $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TAG, $searchtags); } elseif ($searchterm) { - $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); + $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); } else { - $linksToDisplay = $LINKSDB; + $linksToDisplay = $linkDB; } $nblinksToDisplay = 50; // Number of links to display. // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links. if (!empty($_GET['nb'])) { - $nblinksToDisplay = $_GET['nb']=='all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1); + $nblinksToDisplay = $_GET['nb'] == 'all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1); + } + + $keys = array(); + foreach ($linksToDisplay as $key=>$value) { + $keys[] = $key; // No, I can't use array_keys(). } - $pageaddr=escape(index_url($_SERVER)); + $pageaddr = escape(index_url($_SERVER)); $latestDate = ''; - $entries=''; - $i=0; - $keys=array(); foreach($linksToDisplay as $key=>$value) { $keys[]=$key; } // No, I can't use array_keys(). - while ($i<$nblinksToDisplay && $iformat(DateTime::ISO8601); - $latestDate = max($latestDate, $iso8601date); - $absurl = $link['url']; - if (startsWith($absurl,'?')) $absurl=$pageaddr.$absurl; // make permalink URL absolute - $entries.=''.$link['title'].''; - if ($usepermalinks===true) - $entries.=''.$guid.''; - else - $entries.=''.$guid.''; - - if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) { - $entries.=''.escape($iso8601date).''; + $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 ($usepermalinks) { + $permalink = 'Direct link'; + } else { + $permalink = 'Permalink'; } + $link['description'] = format_description($link['description']) . PHP_EOL .'
— '. $permalink; - // Add permalink in description - $descriptionlink = '(Permalink)'; - // If user wants permalinks first, put the final link in description - if ($usepermalinks===true) $descriptionlink = '(Link)'; - if (strlen($link['description'])>0) $descriptionlink = '
'.$descriptionlink; + $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); + $link['iso_date'] = $date->format(DateTime::ATOM); + $latestDate = max($latestDate, $link['iso_date']); + $taglist = array_filter(explode(' ', $link['tags']), 'strlen'); + uasort($taglist, 'strcasecmp'); + $link['taglist'] = $taglist; - $entries .= '\n"; - if ($link['tags']!='') // Adding tags to each ATOM entry (as mentioned in ATOM specification) - { - foreach(explode(' ',$link['tags']) as $tag) - { $entries.=''."\n"; } - } - $entries.="
\n"; + $linkDisp[$keys[$i]] = $link; $i++; } - $feed=''; - $feed.=''.$GLOBALS['title'].''; - if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) $feed.=''.escape($latestDate).''; - $feed.=''; - if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) - { - $feed.=''; - $feed.=''; - $feed.=''; + + $data = array(); + if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) { + $data['pubsubhub_url'] = escape($GLOBALS['config']['PUBSUBHUB_URL']); } - $feed.=''.$pageaddr.''.$pageaddr.''; - $feed.=''.$pageaddr.''."\n\n"; // Yes, I know I should use a real IRI (RFC3987), but the site URL will do. - $feed.=$entries; - $feed.=''; - echo $feed; + // Use the locale do define the language, if available. + $locale = strtolower(setlocale(LC_COLLATE, 0)); + if (! empty($locale) && preg_match('/^\w{2}[_\-]\w{2}/', $locale)) { + $data['language'] = substr($locale, 0, 2); + } else { + $data['language'] = 'en'; + } + $data['last_update'] = escape($latestDate); + $data['show_dates'] = !$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn(); + $data['self_link'] = escape($pageaddr . $_SERVER['REQUEST_URI']); + $data['index_url'] = escape($pageaddr); + $data['usepermalinks'] = $usepermalinks; + $data['links'] = $linkDisp; + $pageBuilder->assignAll($data); + $pageBuilder->renderPage('feed.atom', false); $cache->cache(ob_get_contents()); ob_end_flush(); exit; @@ -1283,6 +1272,11 @@ function renderPage() showDaily($PAGE); } + // ATOM feed. + if ($targetPage == Router::$PAGE_ATOM) { + showATOM($PAGE, $LINKSDB); + } + // Display openseach plugin (XML) if ($targetPage == Router::$PAGE_OPENSEARCH) { header('Content-Type: application/xml; charset=utf-8'); @@ -2608,7 +2602,6 @@ function resizeImage($filepath) if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=genthumbnail')) { genThumbnail(); exit; } // Thumbnail generation/cache does not need the link database. if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=rss')) { showRSS(); exit; } -if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=atom')) { showATOM(); exit; } if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=dailyrss')) { showDailyRSS(); exit; } if (!isset($_SESSION['LINKS_PER_PAGE'])) $_SESSION['LINKS_PER_PAGE']=$GLOBALS['config']['LINKS_PER_PAGE']; renderPage(); -- cgit v1.2.3 From e67712ba0f8896f0d7e676292f557b5999c2848d Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 12 Mar 2016 14:38:06 +0100 Subject: Refactor showRSS, and make it use the RSS template --- index.php | 120 +++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 67 insertions(+), 53 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index 14811c56..261c8a37 100644 --- a/index.php +++ b/index.php @@ -683,7 +683,7 @@ class pageBuilder // ------------------------------------------------------------------------------------------ // Output the last N links in RSS 2.0 format. -function showRSS() +function showRSS($pageBuilder, $linkDB) { header('Content-Type: application/rss+xml; charset=utf-8'); @@ -693,11 +693,11 @@ function showRSS() $usepermalinks = isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS']; // Cache system - $query = $_SERVER["QUERY_STRING"]; + $query = $_SERVER['QUERY_STRING']; $cache = new CachedPage( $GLOBALS['config']['PAGECACHE'], page_url($_SERVER), - startsWith($query,'do=rss') && !isLoggedIn() + startsWith($query, 'do=rss') && !isLoggedIn() ); $cached = $cache->cachedVersion(); if (! empty($cached)) { @@ -705,32 +705,23 @@ function showRSS() exit; } - // If cached was not found (or not usable), then read the database and build the response: - $LINKSDB = new LinkDB( - $GLOBALS['config']['DATASTORE'], - isLoggedIn(), - $GLOBALS['config']['HIDE_PUBLIC_LINKS'], - $GLOBALS['redirector'] - ); - // Read links from database (and filter private links if user it not logged in). - // Optionally filter the results: $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; if (! empty($searchtags) && ! empty($searchterm)) { - $linksToDisplay = $LINKSDB->filter( + $linksToDisplay = $linkDB->filter( LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, array($searchtags, $searchterm) ); } elseif ($searchtags) { - $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $searchtags); + $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TAG, $searchtags); } elseif ($searchterm) { - $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); + $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); } else { - $linksToDisplay = $LINKSDB; + $linksToDisplay = $linkDB; } $nblinksToDisplay = 50; // Number of links to display. @@ -739,50 +730,63 @@ function showRSS() $nblinksToDisplay = $_GET['nb'] == 'all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1); } + $keys = array(); + foreach ($linksToDisplay as $key=>$value) { + $keys[] = $key; // No, I can't use array_keys(). + } + $pageaddr = escape(index_url($_SERVER)); - echo ''; - echo ''.$GLOBALS['title'].''.$pageaddr.''; - echo 'Shared linksen-en'.$pageaddr.''."\n\n"; - if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) - { - echo ''; - echo ''; - echo ''; - echo ''; - } - $i=0; - $keys=array(); foreach($linksToDisplay as $key=>$value) { $keys[]=$key; } // No, I can't use array_keys(). - while ($i<$nblinksToDisplay && $i'.$link['title'].''.$guid.''.$guid.''; - else - echo ''.$link['title'].''.$guid.''.$absurl.''; - if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) { - echo ''.escape($date->format(DateTime::RSS))."\n"; - } - if ($link['tags']!='') // Adding tags to each RSS entry (as mentioned in RSS specification) - { - foreach(explode(' ',$link['tags']) as $tag) { echo ''.$tag.''."\n"; } + $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 ($usepermalinks) { + $permalink = 'Direct link'; + } else { + $permalink = 'Permalink'; + } + $link['description'] = format_description($link['description']) . PHP_EOL .'
— '. $permalink; + + $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); + $link['iso_date'] = $date->format(DateTime::RSS); + $latestDate = max($latestDate, $link['iso_date']); + $taglist = array_filter(explode(' ', $link['tags']), 'strlen'); + uasort($taglist, 'strcasecmp'); + $link['taglist'] = $taglist; - // Add permalink in description - $descriptionlink = '(Permalink)'; - // If user wants permalinks first, put the final link in description - if ($usepermalinks===true) $descriptionlink = '(Link)'; - if (strlen($link['description'])>0) $descriptionlink = '
'.$descriptionlink; - echo '' . "\n
\n"; + $linkDisp[$keys[$i]] = $link; $i++; } - echo '
'; + $data = array(); + if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) { + $data['pubsubhub_url'] = escape($GLOBALS['config']['PUBSUBHUB_URL']); + } + + // Use the locale do define the language, if available. + $locale = strtolower(setlocale(LC_COLLATE, 0)); + if (! empty($locale) && preg_match('/^\w{2}[_\-]\w{2}/', $locale)) { + $data['language'] = str_replace('_', '-', substr($locale, 0, 5)); + } else { + $data['language'] = 'en-en'; + } + $data['last_update'] = escape($latestDate); + $data['show_dates'] = !$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn(); + // Remove starting slash from REQUEST_URI. + $data['self_link'] = escape($pageaddr . substr($_SERVER['REQUEST_URI'], 1)); + $data['index_url'] = escape($pageaddr); + $data['usepermalinks'] = $usepermalinks; + $data['links'] = $linkDisp; + + $pageBuilder->assignAll($data); + $pageBuilder->renderPage('feed.rss', false); $cache->cache(ob_get_contents()); ob_end_flush(); exit; @@ -889,6 +893,12 @@ function showATOM($pageBuilder, $linkDB) $data['usepermalinks'] = $usepermalinks; $data['links'] = $linkDisp; + $pluginManager = PluginManager::getInstance(); + $pluginManager->executeHooks('render_feed', $data, array( + 'loggedin' => isLoggedIn(), + 'target' => Router::$PAGE_ATOM, + )); + $pageBuilder->assignAll($data); $pageBuilder->renderPage('feed.atom', false); $cache->cache(ob_get_contents()); @@ -1277,6 +1287,11 @@ function renderPage() showATOM($PAGE, $LINKSDB); } + // RSS feed. + if ($targetPage == Router::$PAGE_RSS) { + showRSS($PAGE, $LINKSDB); + } + // Display openseach plugin (XML) if ($targetPage == Router::$PAGE_OPENSEARCH) { header('Content-Type: application/xml; charset=utf-8'); @@ -2601,7 +2616,6 @@ function resizeImage($filepath) } if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=genthumbnail')) { genThumbnail(); exit; } // Thumbnail generation/cache does not need the link database. -if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=rss')) { showRSS(); exit; } if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=dailyrss')) { showDailyRSS(); exit; } if (!isset($_SESSION['LINKS_PER_PAGE'])) $_SESSION['LINKS_PER_PAGE']=$GLOBALS['config']['LINKS_PER_PAGE']; renderPage(); -- cgit v1.2.3 From bcd078bf0a5119fe0c8441b803b4fe05fdaa6d18 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 12 Mar 2016 14:39:06 +0100 Subject: Plugin: add render_feed hook and call it while generating ATOM and RSS feed. Create an example of the new hook in the demo plugin. --- index.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'index.php') diff --git a/index.php b/index.php index 261c8a37..73b83533 100644 --- a/index.php +++ b/index.php @@ -785,6 +785,12 @@ function showRSS($pageBuilder, $linkDB) $data['usepermalinks'] = $usepermalinks; $data['links'] = $linkDisp; + $pluginManager = PluginManager::getInstance(); + $pluginManager->executeHooks('render_feed', $data, array( + 'loggedin' => isLoggedIn(), + 'target' => Router::$PAGE_RSS, + )); + $pageBuilder->assignAll($data); $pageBuilder->renderPage('feed.rss', false); $cache->cache(ob_get_contents()); -- cgit v1.2.3 From e15f08d72a7e5f697fe4d64f1bd48465052ca115 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 21 Mar 2016 19:06:46 +0100 Subject: Use generateLocation to set the redirection in login (and don't escape the url) --- index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'index.php') diff --git a/index.php b/index.php index 850b350e..27db10ba 100644 --- a/index.php +++ b/index.php @@ -483,7 +483,7 @@ if (isset($_POST['login'])) if (isset($_POST['returnurl'])) { // Prevent loops over login screen. if (strpos($_POST['returnurl'], 'do=login') === false) { - header('Location: '. escape($_POST['returnurl'])); + header('Location: '. generateLocation($_POST['returnurl'], $_SERVER['HTTP_HOST'])); exit; } } -- 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. --- index.php | 279 +++++++++----------------------------------------------------- 1 file changed, 41 insertions(+), 238 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index 73b83533..6e14ff3f 100644 --- a/index.php +++ b/index.php @@ -154,6 +154,7 @@ if (is_file($GLOBALS['config']['CONFIG_FILE'])) { require_once 'application/ApplicationUtils.php'; require_once 'application/Cache.php'; require_once 'application/CachedPage.php'; +require_once 'application/FeedBuilder.php'; require_once 'application/FileUtils.php'; require_once 'application/HttpUtils.php'; require_once 'application/LinkDB.php'; @@ -681,237 +682,6 @@ class pageBuilder } } -// ------------------------------------------------------------------------------------------ -// Output the last N links in RSS 2.0 format. -function showRSS($pageBuilder, $linkDB) -{ - header('Content-Type: application/rss+xml; charset=utf-8'); - - // $usepermalink : If true, use permalink instead of final link. - // User just has to add 'permalink' in URL parameters. e.g. http://mysite.com/shaarli/?do=rss&permalinks - // Also enabled through a config option - $usepermalinks = isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS']; - - // Cache system - $query = $_SERVER['QUERY_STRING']; - $cache = new CachedPage( - $GLOBALS['config']['PAGECACHE'], - page_url($_SERVER), - startsWith($query, 'do=rss') && !isLoggedIn() - ); - $cached = $cache->cachedVersion(); - if (! empty($cached)) { - echo $cached; - exit; - } - - // Optionally filter the results: - $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; - $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; - if (! empty($searchtags) && ! empty($searchterm)) { - $linksToDisplay = $linkDB->filter( - LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, - array($searchtags, $searchterm) - ); - } - elseif ($searchtags) { - $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TAG, $searchtags); - } - elseif ($searchterm) { - $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); - } - else { - $linksToDisplay = $linkDB; - } - - $nblinksToDisplay = 50; // Number of links to display. - // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links. - if (!empty($_GET['nb'])) { - $nblinksToDisplay = $_GET['nb'] == 'all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1); - } - - $keys = array(); - foreach ($linksToDisplay as $key=>$value) { - $keys[] = $key; // No, I can't use array_keys(). - } - - $pageaddr = escape(index_url($_SERVER)); - $latestDate = ''; - $i = 0; - $linkDisp = array(); - while ($i < $nblinksToDisplay && $i < count($keys)) - { - $link = $linksToDisplay[$keys[$i]]; - $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 ($usepermalinks) { - $permalink = 'Direct link'; - } else { - $permalink = 'Permalink'; - } - $link['description'] = format_description($link['description']) . PHP_EOL .'
— '. $permalink; - - $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); - $link['iso_date'] = $date->format(DateTime::RSS); - $latestDate = max($latestDate, $link['iso_date']); - $taglist = array_filter(explode(' ', $link['tags']), 'strlen'); - uasort($taglist, 'strcasecmp'); - $link['taglist'] = $taglist; - - $linkDisp[$keys[$i]] = $link; - $i++; - } - - $data = array(); - if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) { - $data['pubsubhub_url'] = escape($GLOBALS['config']['PUBSUBHUB_URL']); - } - - // Use the locale do define the language, if available. - $locale = strtolower(setlocale(LC_COLLATE, 0)); - if (! empty($locale) && preg_match('/^\w{2}[_\-]\w{2}/', $locale)) { - $data['language'] = str_replace('_', '-', substr($locale, 0, 5)); - } else { - $data['language'] = 'en-en'; - } - $data['last_update'] = escape($latestDate); - $data['show_dates'] = !$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn(); - // Remove starting slash from REQUEST_URI. - $data['self_link'] = escape($pageaddr . substr($_SERVER['REQUEST_URI'], 1)); - $data['index_url'] = escape($pageaddr); - $data['usepermalinks'] = $usepermalinks; - $data['links'] = $linkDisp; - - $pluginManager = PluginManager::getInstance(); - $pluginManager->executeHooks('render_feed', $data, array( - 'loggedin' => isLoggedIn(), - 'target' => Router::$PAGE_RSS, - )); - - $pageBuilder->assignAll($data); - $pageBuilder->renderPage('feed.rss', false); - $cache->cache(ob_get_contents()); - ob_end_flush(); - exit; -} - -// ------------------------------------------------------------------------------------------ -// Output the last N links in ATOM format. -function showATOM($pageBuilder, $linkDB) -{ - header('Content-Type: application/atom+xml; charset=utf-8'); - - // Cache system - $query = $_SERVER["QUERY_STRING"]; - $cache = new CachedPage( - $GLOBALS['config']['PAGECACHE'], - page_url($_SERVER), - startsWith($query,'do=atom') && !isLoggedIn() - ); - $cached = $cache->cachedVersion(); - if (!empty($cached)) { - echo $cached; - exit; - } - - // $usepermalink : If true, use permalink instead of final link. - // User just has to add 'permalink' in URL parameters. e.g. http://mysite.com/shaarli/?do=atom&permalinks - $usepermalinks = isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS']; - - // Optionally filter the results: - $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; - $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; - if (! empty($searchtags) && ! empty($searchterm)) { - $linksToDisplay = $linkDB->filter( - LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, - array($searchtags, $searchterm) - ); - } - elseif ($searchtags) { - $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TAG, $searchtags); - } - elseif ($searchterm) { - $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); - } - else { - $linksToDisplay = $linkDB; - } - - $nblinksToDisplay = 50; // Number of links to display. - // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links. - if (!empty($_GET['nb'])) { - $nblinksToDisplay = $_GET['nb'] == 'all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1); - } - - $keys = array(); - foreach ($linksToDisplay as $key=>$value) { - $keys[] = $key; // No, I can't use array_keys(). - } - - $pageaddr = escape(index_url($_SERVER)); - $latestDate = ''; - $i = 0; - $linkDisp = array(); - while ($i < $nblinksToDisplay && $i < count($keys)) - { - $link = $linksToDisplay[$keys[$i]]; - $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 ($usepermalinks) { - $permalink = 'Direct link'; - } else { - $permalink = 'Permalink'; - } - $link['description'] = format_description($link['description']) . PHP_EOL .'
— '. $permalink; - - $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); - $link['iso_date'] = $date->format(DateTime::ATOM); - $latestDate = max($latestDate, $link['iso_date']); - $taglist = array_filter(explode(' ', $link['tags']), 'strlen'); - uasort($taglist, 'strcasecmp'); - $link['taglist'] = $taglist; - - $linkDisp[$keys[$i]] = $link; - $i++; - } - - $data = array(); - if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) { - $data['pubsubhub_url'] = escape($GLOBALS['config']['PUBSUBHUB_URL']); - } - // Use the locale do define the language, if available. - $locale = strtolower(setlocale(LC_COLLATE, 0)); - if (! empty($locale) && preg_match('/^\w{2}[_\-]\w{2}/', $locale)) { - $data['language'] = substr($locale, 0, 2); - } else { - $data['language'] = 'en'; - } - $data['last_update'] = escape($latestDate); - $data['show_dates'] = !$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn(); - $data['self_link'] = escape($pageaddr . $_SERVER['REQUEST_URI']); - $data['index_url'] = escape($pageaddr); - $data['usepermalinks'] = $usepermalinks; - $data['links'] = $linkDisp; - - $pluginManager = PluginManager::getInstance(); - $pluginManager->executeHooks('render_feed', $data, array( - 'loggedin' => isLoggedIn(), - 'target' => Router::$PAGE_ATOM, - )); - - $pageBuilder->assignAll($data); - $pageBuilder->renderPage('feed.atom', false); - $cache->cache(ob_get_contents()); - ob_end_flush(); - exit; -} - // ------------------------------------------------------------------------------------------ // Daily RSS feed: 1 RSS entry per day giving all the links on that day. // Gives the last 7 days (which have links). @@ -1288,14 +1058,47 @@ function renderPage() showDaily($PAGE); } - // ATOM feed. - if ($targetPage == Router::$PAGE_ATOM) { - showATOM($PAGE, $LINKSDB); - } + // ATOM and RSS feed. + if ($targetPage == Router::$PAGE_FEED_ATOM || $targetPage == Router::$PAGE_FEED_RSS) { + $feedType = $targetPage == Router::$PAGE_FEED_RSS ? FeedBuilder::$FEED_RSS : FeedBuilder::$FEED_ATOM; + header('Content-Type: application/'. $feedType .'+xml; charset=utf-8'); - // RSS feed. - if ($targetPage == Router::$PAGE_RSS) { - showRSS($PAGE, $LINKSDB); + // Cache system + $query = $_SERVER['QUERY_STRING']; + $cache = new CachedPage( + $GLOBALS['config']['PAGECACHE'], + page_url($_SERVER), + startsWith($query,'do='. $targetPage) && !isLoggedIn() + ); + $cached = $cache->cachedVersion(); + if (!empty($cached)) { + echo $cached; + exit; + } + + // Generate data. + $feedGenerator = new FeedBuilder($LINKSDB, $feedType, $_SERVER, $_GET, isLoggedIn()); + $feedGenerator->setLocale(strtolower(setlocale(LC_COLLATE, 0))); + $feedGenerator->setHideDates($GLOBALS['config']['HIDE_TIMESTAMPS'] && !isLoggedIn()); + $feedGenerator->setUsePermalinks(isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS']); + if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) { + $feedGenerator->setPubsubhubUrl($GLOBALS['config']['PUBSUBHUB_URL']); + } + $data = $feedGenerator->buildData(); + + // Process plugin hook. + $pluginManager = PluginManager::getInstance(); + $pluginManager->executeHooks('render_feed', $data, array( + 'loggedin' => isLoggedIn(), + 'target' => $targetPage, + )); + + // Render the template. + $PAGE->assignAll($data); + $PAGE->renderPage('feed.'. $feedType); + $cache->cache(ob_get_contents()); + ob_end_flush(); + exit; } // Display openseach plugin (XML) -- 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 --- index.php | 99 +++++++++++++++++---------------------------------------------- 1 file changed, 27 insertions(+), 72 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index 6e14ff3f..79c66648 100644 --- a/index.php +++ b/index.php @@ -816,7 +816,7 @@ function showDaily($pageBuilder) } try { - $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_DAY, $day); + $linksToDisplay = $LINKSDB->filterDay($day); } catch (Exception $exc) { error_log($exc); $linksToDisplay = array(); @@ -962,24 +962,7 @@ function renderPage() if ($targetPage == Router::$PAGE_PICWALL) { // Optionally filter the results: - $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; - $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; - if (! empty($searchtags) && ! empty($searchterm)) { - $links = $LINKSDB->filter( - LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, - array($searchtags, $searchterm) - ); - } - elseif ($searchtags) { - $links = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $searchtags); - } - elseif ($searchterm) { - $links = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); - } - else { - $links = $LINKSDB; - } - + $links = $LINKSDB->filterSearch($_GET); $linksToDisplay = array(); // Get only links which have a thumbnail. @@ -1071,7 +1054,7 @@ function renderPage() startsWith($query,'do='. $targetPage) && !isLoggedIn() ); $cached = $cache->cachedVersion(); - if (!empty($cached)) { + if (false && !empty($cached)) { echo $cached; exit; } @@ -1352,9 +1335,9 @@ function renderPage() // Delete a tag: if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) { - $needle=trim($_POST['fromtag']); + $needle = trim($_POST['fromtag']); // True for case-sensitive tag search. - $linksToAlter = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $needle, true); + $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true); foreach($linksToAlter as $key=>$value) { $tags = explode(' ',trim($value['tags'])); @@ -1369,9 +1352,9 @@ function renderPage() // Rename a tag: if (isset($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) { - $needle=trim($_POST['fromtag']); + $needle = trim($_POST['fromtag']); // True for case-sensitive tag search. - $linksToAlter = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $needle, true); + $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true); foreach($linksToAlter as $key=>$value) { $tags = explode(' ',trim($value['tags'])); @@ -1807,60 +1790,32 @@ function importFile() } } -// ----------------------------------------------------------------------------------------------- -// Template for the list of links (