-// ------------------------------------------------------------------------------------------
-/* This class is in charge of building the final page.
- (This is basically a wrapper around RainTPL which pre-fills some fields.)
- p = new pageBuilder;
- p.assign('myfield','myvalue');
- p.renderPage('mytemplate');
-
-*/
-class pageBuilder
-{
- private $tpl; // RainTPL template
-
- function __construct()
- {
- $this->tpl = false;
- }
-
- /**
- * Initialize all default tpl tags.
- */
- private function initialize()
- {
- $this->tpl = new RainTPL;
- $this->tpl->assign('newversion', escape(checkUpdate()));
- $this->tpl->assign('feedurl', escape(index_url($_SERVER)));
- $searchcrits = ''; // Search criteria
- if (!empty($_GET['searchtags'])) {
- $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']);
- }
- elseif (!empty($_GET['searchterm'])) {
- $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
- }
- $this->tpl->assign('searchcrits', $searchcrits);
- $this->tpl->assign('source', index_url($_SERVER));
- $this->tpl->assign('version', shaarli_version);
- $this->tpl->assign('scripturl', index_url($_SERVER));
- $this->tpl->assign('pagetitle', 'Shaarli');
- $this->tpl->assign('privateonly', !empty($_SESSION['privateonly'])); // Show only private links?
- if (!empty($GLOBALS['title'])) {
- $this->tpl->assign('pagetitle', $GLOBALS['title']);
- }
- if (!empty($GLOBALS['titleLink'])) {
- $this->tpl->assign('titleLink', $GLOBALS['titleLink']);
- }
- if (!empty($GLOBALS['pagetitle'])) {
- $this->tpl->assign('pagetitle', $GLOBALS['pagetitle']);
- }
- $this->tpl->assign('shaarlititle', empty($GLOBALS['title']) ? 'Shaarli': $GLOBALS['title']);
- if (!empty($GLOBALS['plugin_errors'])) {
- $this->tpl->assign('plugin_errors', $GLOBALS['plugin_errors']);
- }
- }
-
- // The following assign() method is basically the same as RainTPL (except that it's lazy)
- public function assign($what,$where)
- {
- if ($this->tpl===false) $this->initialize(); // Lazy initialization
- $this->tpl->assign($what,$where);
- }
-
- // Render a specific page (using a template).
- // e.g. pb.renderPage('picwall')
- public function renderPage($page)
- {
- if ($this->tpl===false) $this->initialize(); // Lazy initialization
- $this->tpl->draw($page);
- }
-}
-
-// ------------------------------------------------------------------------------------------
-// Output the last N links in RSS 2.0 format.
-function showRSS()
-{
- 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;
- }
-
- // 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']
- );
- // Read links from database (and filter private links if user it not logged in).
-
- // Optionally filter the results:
- $linksToDisplay=array();
- if (!empty($_GET['searchterm'])) $linksToDisplay = $LINKSDB->filterFulltext($_GET['searchterm']);
- else if (!empty($_GET['searchtags'])) $linksToDisplay = $LINKSDB->filterTags(trim($_GET['searchtags']));
- else $linksToDisplay = $LINKSDB;
-
- $nblinksToDisplay = 50; // Number of links to display.
- if (!empty($_GET['nb'])) // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links.
- {
- $nblinksToDisplay = $_GET['nb']=='all' ? count($linksToDisplay) : max($_GET['nb']+0,1) ;
- }
-
- $pageaddr=escape(index_url($_SERVER));
- echo '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">';
- echo '<channel><title>'.$GLOBALS['title'].'</title><link>'.$pageaddr.'</link>';
- echo '<description>Shared links</description><language>en-en</language><copyright>'.$pageaddr.'</copyright>'."\n\n";
- if (!empty($GLOBALS['config']['PUBSUBHUB_URL']))
- {
- echo '<!-- PubSubHubbub Discovery -->';
- echo '<link rel="hub" href="'.escape($GLOBALS['config']['PUBSUBHUB_URL']).'" xmlns="http://www.w3.org/2005/Atom" />';
- echo '<link rel="self" href="'.$pageaddr.'?do=rss" xmlns="http://www.w3.org/2005/Atom" />';
- echo '<!-- End Of PubSubHubbub Discovery -->';
- }
- $i=0;
- $keys=array(); foreach($linksToDisplay as $key=>$value) { $keys[]=$key; } // No, I can't use array_keys().
- while ($i<$nblinksToDisplay && $i<count($keys))
- {
- $link = $linksToDisplay[$keys[$i]];
- $guid = $pageaddr.'?'.smallHash($link['linkdate']);
- $rfc822date = linkdate2rfc822($link['linkdate']);
- $absurl = $link['url'];
- if (startsWith($absurl,'?')) $absurl=$pageaddr.$absurl; // make permalink URL absolute
- if ($usepermalinks===true)
- echo '<item><title>'.$link['title'].'</title><guid isPermaLink="true">'.$guid.'</guid><link>'.$guid.'</link>';
- else
- echo '<item><title>'.$link['title'].'</title><guid isPermaLink="false">'.$guid.'</guid><link>'.$absurl.'</link>';
- if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) echo '<pubDate>'.escape($rfc822date)."</pubDate>\n";
- if ($link['tags']!='') // Adding tags to each RSS entry (as mentioned in RSS specification)
- {
- foreach(explode(' ',$link['tags']) as $tag) { echo '<category domain="'.$pageaddr.'">'.$tag.'</category>'."\n"; }
- }
-
- // Add permalink in description
- $descriptionlink = '(<a href="'.$guid.'">Permalink</a>)';
- // If user wants permalinks first, put the final link in description
- if ($usepermalinks===true) $descriptionlink = '(<a href="'.$absurl.'">Link</a>)';
- if (strlen($link['description'])>0) $descriptionlink = '<br>'.$descriptionlink;
- echo '<description><![CDATA['.nl2br(keepMultipleSpaces(text2clickable($link['description']))).$descriptionlink.']]></description>'."\n</item>\n";
- $i++;
- }
- echo '</channel></rss><!-- Cached version of '.escape(page_url($_SERVER)).' -->';
-
- $cache->cache(ob_get_contents());
- ob_end_flush();
- exit;
-}
-
-// ------------------------------------------------------------------------------------------
-// Output the last N links in ATOM format.
-function showATOM()
-{
- 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(
- $GLOBALS['config']['PAGECACHE'],
- page_url($_SERVER),
- startsWith($query,'do=atom') && !isLoggedIn()
- );
- $cached = $cache->cachedVersion();
- if (!empty($cached)) {
- echo $cached;
- 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']
- );
-
- // Optionally filter the results:
- $linksToDisplay=array();
- if (!empty($_GET['searchterm'])) $linksToDisplay = $LINKSDB->filterFulltext($_GET['searchterm']);
- else if (!empty($_GET['searchtags'])) $linksToDisplay = $LINKSDB->filterTags(trim($_GET['searchtags']));
- else $linksToDisplay = $LINKSDB;
-
- $nblinksToDisplay = 50; // Number of links to display.
- if (!empty($_GET['nb'])) // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links.
- {
- $nblinksToDisplay = $_GET['nb']=='all' ? count($linksToDisplay) : max($_GET['nb']+0,1) ;
- }
-
- $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 && $i<count($keys))
- {
- $link = $linksToDisplay[$keys[$i]];
- $guid = $pageaddr.'?'.smallHash($link['linkdate']);
- $iso8601date = linkdate2iso8601($link['linkdate']);
- $latestDate = max($latestDate,$iso8601date);
- $absurl = $link['url'];
- if (startsWith($absurl,'?')) $absurl=$pageaddr.$absurl; // make permalink URL absolute
- $entries.='<entry><title>'.$link['title'].'</title>';
- if ($usepermalinks===true)
- $entries.='<link href="'.$guid.'" /><id>'.$guid.'</id>';
- else
- $entries.='<link href="'.$absurl.'" /><id>'.$guid.'</id>';
- if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) $entries.='<updated>'.escape($iso8601date).'</updated>';
-
- // Add permalink in description
- $descriptionlink = '(<a href="'.$guid.'">Permalink</a>)';
- // If user wants permalinks first, put the final link in description
- if ($usepermalinks===true) $descriptionlink = '(<a href="'.$absurl.'">Link</a>)';
- if (strlen($link['description'])>0) $descriptionlink = '<br>'.$descriptionlink;
-
- $entries.='<content type="html"><![CDATA['.nl2br(keepMultipleSpaces(text2clickable($link['description']))).$descriptionlink."]]></content>\n";
- if ($link['tags']!='') // Adding tags to each ATOM entry (as mentioned in ATOM specification)
- {
- foreach(explode(' ',$link['tags']) as $tag)
- { $entries.='<category scheme="'.$pageaddr.'" term="'.$tag.'" />'."\n"; }
- }
- $entries.="</entry>\n";
- $i++;
- }
- $feed='<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/Atom">';
- $feed.='<title>'.$GLOBALS['title'].'</title>';
- if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) $feed.='<updated>'.escape($latestDate).'</updated>';
- $feed.='<link rel="self" href="'.escape(server_url($_SERVER).$_SERVER["REQUEST_URI"]).'" />';
- if (!empty($GLOBALS['config']['PUBSUBHUB_URL']))
- {
- $feed.='<!-- PubSubHubbub Discovery -->';
- $feed.='<link rel="hub" href="'.escape($GLOBALS['config']['PUBSUBHUB_URL']).'" />';
- $feed.='<!-- End Of PubSubHubbub Discovery -->';
- }
- $feed.='<author><name>'.$pageaddr.'</name><uri>'.$pageaddr.'</uri></author>';
- $feed.='<id>'.$pageaddr.'</id>'."\n\n"; // Yes, I know I should use a real IRI (RFC3987), but the site URL will do.
- $feed.=$entries;
- $feed.='</feed><!-- Cached version of '.escape(page_url($_SERVER)).' -->';
- echo $feed;
-
- $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).
-// This RSS feed cannot be filtered.
-function showDailyRSS() {