aboutsummaryrefslogtreecommitdiffhomepage
path: root/index.php
diff options
context:
space:
mode:
Diffstat (limited to 'index.php')
-rwxr-xr-x[-rw-r--r--]index.php107
1 files changed, 49 insertions, 58 deletions
diff --git a/index.php b/index.php
index b4d9395f..0dd5829b 100644..100755
--- a/index.php
+++ b/index.php
@@ -1,6 +1,6 @@
1<?php 1<?php
2/** 2/**
3 * Shaarli v0.6.0 - Shaare your links... 3 * Shaarli v0.6.1 - Shaare your links...
4 * 4 *
5 * The personal, minimalist, super-fast, no-database Delicious clone. 5 * The personal, minimalist, super-fast, no-database Delicious clone.
6 * 6 *
@@ -92,7 +92,8 @@ $GLOBALS['config']['ENABLE_THUMBNAILS'] = true;
92$GLOBALS['config']['ENABLE_LOCALCACHE'] = true; 92$GLOBALS['config']['ENABLE_LOCALCACHE'] = true;
93 93
94// Update check frequency for Shaarli. 86400 seconds=24 hours 94// Update check frequency for Shaarli. 86400 seconds=24 hours
95$GLOBALS['config']['UPDATECHECK_INTERVAL'] = 86400 ; 95$GLOBALS['config']['UPDATECHECK_BRANCH'] = 'stable';
96$GLOBALS['config']['UPDATECHECK_INTERVAL'] = 86400;
96 97
97 98
98/* 99/*
@@ -118,7 +119,7 @@ $GLOBALS['config']['PUBSUBHUB_URL'] = '';
118/* 119/*
119 * PHP configuration 120 * PHP configuration
120 */ 121 */
121define('shaarli_version', '0.6.0'); 122define('shaarli_version', '0.6.1');
122 123
123// http://server.com/x/shaarli --> /shaarli/ 124// http://server.com/x/shaarli --> /shaarli/
124define('WEB_PATH', substr($_SERVER["REQUEST_URI"], 0, 1+strrpos($_SERVER["REQUEST_URI"], '/', 0))); 125define('WEB_PATH', substr($_SERVER["REQUEST_URI"], 0, 1+strrpos($_SERVER["REQUEST_URI"], '/', 0)));
@@ -305,56 +306,15 @@ function setup_login_state() {
305} 306}
306$userIsLoggedIn = setup_login_state(); 307$userIsLoggedIn = setup_login_state();
307 308
308// Checks if an update is available for Shaarli.
309// (at most once a day, and only for registered user.)
310// Output: '' = no new version.
311// other= the available version.
312function checkUpdate()
313{
314 if (!isLoggedIn()) return ''; // Do not check versions for visitors.
315 if (empty($GLOBALS['config']['ENABLE_UPDATECHECK'])) return ''; // Do not check if the user doesn't want to.
316
317 // Get latest version number at most once a day.
318 if (!is_file($GLOBALS['config']['UPDATECHECK_FILENAME']) || (filemtime($GLOBALS['config']['UPDATECHECK_FILENAME'])<time()-($GLOBALS['config']['UPDATECHECK_INTERVAL'])))
319 {
320 $version = shaarli_version;
321 list($headers, $data) = get_http_url('https://raw.githubusercontent.com/shaarli/Shaarli/master/shaarli_version.php', 2);
322 if (strpos($headers[0], '200 OK') !== false) {
323 $version = str_replace(' */ ?>', '', str_replace('<?php /* ', '', $data));
324 }
325 // If failed, never mind. We don't want to bother the user with that.
326 file_put_contents($GLOBALS['config']['UPDATECHECK_FILENAME'],$version); // touch file date
327 }
328 // Compare versions:
329 $newestversion=file_get_contents($GLOBALS['config']['UPDATECHECK_FILENAME']);
330 if (version_compare($newestversion,shaarli_version)==1) return $newestversion;
331 return '';
332}
333
334 309
335// ----------------------------------------------------------------------------------------------- 310// -----------------------------------------------------------------------------------------------
336// Log to text file 311// Log to text file
337function logm($message) 312function logm($message)
338{ 313{
339 $t = strval(date('Y/m/d_H:i:s')).' - '.$_SERVER["REMOTE_ADDR"].' - '.strval($message)."\n"; 314 $t = strval(date('Y/m/d_H:i:s')).' - '.$_SERVER["REMOTE_ADDR"].' - '.strval($message)."\n";
340 file_put_contents($GLOBAL['config']['LOG_FILE'], $t, FILE_APPEND); 315 file_put_contents($GLOBALS['config']['LOG_FILE'], $t, FILE_APPEND);
341}
342
343// In a string, converts URLs to clickable links.
344// Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722
345function text2clickable($url)
346{
347 $redir = empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector'];
348 return preg_replace('!(((?:https?|ftp|file)://|apt:|magnet:)\S+[[:alnum:]]/?)!si','<a href="'.$redir.'$1" rel="nofollow">$1</a>',$url);
349} 316}
350 317
351// This function inserts &nbsp; where relevant so that multiple spaces are properly displayed in HTML
352// even in the absence of <pre> (This is used in description to keep text formatting)
353function keepMultipleSpaces($text)
354{
355 return str_replace(' ',' &nbsp;',$text);
356
357}
358// ------------------------------------------------------------------------------------------ 318// ------------------------------------------------------------------------------------------
359// Sniff browser language to display dates in the right format automatically. 319// Sniff browser language to display dates in the right format automatically.
360// (Note that is may not work on your server if the corresponding local is not installed.) 320// (Note that is may not work on your server if the corresponding local is not installed.)
@@ -672,7 +632,25 @@ class pageBuilder
672 private function initialize() 632 private function initialize()
673 { 633 {
674 $this->tpl = new RainTPL; 634 $this->tpl = new RainTPL;
675 $this->tpl->assign('newversion', escape(checkUpdate())); 635
636 try {
637 $version = ApplicationUtils::checkUpdate(
638 shaarli_version,
639 $GLOBALS['config']['UPDATECHECK_FILENAME'],
640 $GLOBALS['config']['UPDATECHECK_INTERVAL'],
641 $GLOBALS['config']['ENABLE_UPDATECHECK'],
642 isLoggedIn(),
643 $GLOBALS['config']['UPDATECHECK_BRANCH']
644 );
645 $this->tpl->assign('newVersion', escape($version));
646 $this->tpl->assign('versionError', '');
647
648 } catch (Exception $exc) {
649 logm($exc->getMessage());
650 $this->tpl->assign('newVersion', '');
651 $this->tpl->assign('versionError', escape($exc->getMessage()));
652 }
653
676 $this->tpl->assign('feedurl', escape(index_url($_SERVER))); 654 $this->tpl->assign('feedurl', escape(index_url($_SERVER)));
677 $searchcrits = ''; // Search criteria 655 $searchcrits = ''; // Search criteria
678 if (!empty($_GET['searchtags'])) { 656 if (!empty($_GET['searchtags'])) {
@@ -746,7 +724,8 @@ function showRSS()
746 $LINKSDB = new LinkDB( 724 $LINKSDB = new LinkDB(
747 $GLOBALS['config']['DATASTORE'], 725 $GLOBALS['config']['DATASTORE'],
748 isLoggedIn(), 726 isLoggedIn(),
749 $GLOBALS['config']['HIDE_PUBLIC_LINKS'] 727 $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
728 $GLOBALS['redirector']
750 ); 729 );
751 // Read links from database (and filter private links if user it not logged in). 730 // Read links from database (and filter private links if user it not logged in).
752 731
@@ -797,7 +776,9 @@ function showRSS()
797 // If user wants permalinks first, put the final link in description 776 // If user wants permalinks first, put the final link in description
798 if ($usepermalinks===true) $descriptionlink = '(<a href="'.$absurl.'">Link</a>)'; 777 if ($usepermalinks===true) $descriptionlink = '(<a href="'.$absurl.'">Link</a>)';
799 if (strlen($link['description'])>0) $descriptionlink = '<br>'.$descriptionlink; 778 if (strlen($link['description'])>0) $descriptionlink = '<br>'.$descriptionlink;
800 echo '<description><![CDATA['.nl2br(keepMultipleSpaces(text2clickable($link['description']))).$descriptionlink.']]></description>'."\n</item>\n"; 779 echo '<description><![CDATA['.
780 format_description($link['description'], $GLOBALS['redirector']) .
781 $descriptionlink . ']]></description>' . "\n</item>\n";
801 $i++; 782 $i++;
802 } 783 }
803 echo '</channel></rss><!-- Cached version of '.escape(page_url($_SERVER)).' -->'; 784 echo '</channel></rss><!-- Cached version of '.escape(page_url($_SERVER)).' -->';
@@ -835,7 +816,8 @@ function showATOM()
835 $LINKSDB = new LinkDB( 816 $LINKSDB = new LinkDB(
836 $GLOBALS['config']['DATASTORE'], 817 $GLOBALS['config']['DATASTORE'],
837 isLoggedIn(), 818 isLoggedIn(),
838 $GLOBALS['config']['HIDE_PUBLIC_LINKS'] 819 $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
820 $GLOBALS['redirector']
839 ); 821 );
840 822
841 // Optionally filter the results: 823 // Optionally filter the results:
@@ -876,7 +858,9 @@ function showATOM()
876 if ($usepermalinks===true) $descriptionlink = '(<a href="'.$absurl.'">Link</a>)'; 858 if ($usepermalinks===true) $descriptionlink = '(<a href="'.$absurl.'">Link</a>)';
877 if (strlen($link['description'])>0) $descriptionlink = '<br>'.$descriptionlink; 859 if (strlen($link['description'])>0) $descriptionlink = '<br>'.$descriptionlink;
878 860
879 $entries.='<content type="html"><![CDATA['.nl2br(keepMultipleSpaces(text2clickable($link['description']))).$descriptionlink."]]></content>\n"; 861 $entries .= '<content type="html"><![CDATA['.
862 format_description($link['description'], $GLOBALS['redirector']) .
863 $descriptionlink . "]]></content>\n";
880 if ($link['tags']!='') // Adding tags to each ATOM entry (as mentioned in ATOM specification) 864 if ($link['tags']!='') // Adding tags to each ATOM entry (as mentioned in ATOM specification)
881 { 865 {
882 foreach(explode(' ',$link['tags']) as $tag) 866 foreach(explode(' ',$link['tags']) as $tag)
@@ -929,7 +913,8 @@ function showDailyRSS() {
929 $LINKSDB = new LinkDB( 913 $LINKSDB = new LinkDB(
930 $GLOBALS['config']['DATASTORE'], 914 $GLOBALS['config']['DATASTORE'],
931 isLoggedIn(), 915 isLoggedIn(),
932 $GLOBALS['config']['HIDE_PUBLIC_LINKS'] 916 $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
917 $GLOBALS['redirector']
933 ); 918 );
934 919
935 /* Some Shaarlies may have very few links, so we need to look 920 /* Some Shaarlies may have very few links, so we need to look
@@ -983,7 +968,7 @@ function showDailyRSS() {
983 // We pre-format some fields for proper output. 968 // We pre-format some fields for proper output.
984 foreach ($linkdates as $linkdate) { 969 foreach ($linkdates as $linkdate) {
985 $l = $LINKSDB[$linkdate]; 970 $l = $LINKSDB[$linkdate];
986 $l['formatedDescription'] = nl2br(keepMultipleSpaces(text2clickable($l['description']))); 971 $l['formatedDescription'] = format_description($l['description'], $GLOBALS['redirector']);
987 $l['thumbnail'] = thumbnail($l['url']); 972 $l['thumbnail'] = thumbnail($l['url']);
988 $l['timestamp'] = linkdate2timestamp($l['linkdate']); 973 $l['timestamp'] = linkdate2timestamp($l['linkdate']);
989 if (startsWith($l['url'], '?')) { 974 if (startsWith($l['url'], '?')) {
@@ -1016,7 +1001,8 @@ function showDaily()
1016 $LINKSDB = new LinkDB( 1001 $LINKSDB = new LinkDB(
1017 $GLOBALS['config']['DATASTORE'], 1002 $GLOBALS['config']['DATASTORE'],
1018 isLoggedIn(), 1003 isLoggedIn(),
1019 $GLOBALS['config']['HIDE_PUBLIC_LINKS'] 1004 $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
1005 $GLOBALS['redirector']
1020 ); 1006 );
1021 1007
1022 $day=Date('Ymd',strtotime('-1 day')); // Yesterday, in format YYYYMMDD. 1008 $day=Date('Ymd',strtotime('-1 day')); // Yesterday, in format YYYYMMDD.
@@ -1047,7 +1033,7 @@ function showDaily()
1047 $taglist = explode(' ',$link['tags']); 1033 $taglist = explode(' ',$link['tags']);
1048 uasort($taglist, 'strcasecmp'); 1034 uasort($taglist, 'strcasecmp');
1049 $linksToDisplay[$key]['taglist']=$taglist; 1035 $linksToDisplay[$key]['taglist']=$taglist;
1050 $linksToDisplay[$key]['formatedDescription']=nl2br(keepMultipleSpaces(text2clickable($link['description']))); 1036 $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $GLOBALS['redirector']);
1051 $linksToDisplay[$key]['thumbnail'] = thumbnail($link['url']); 1037 $linksToDisplay[$key]['thumbnail'] = thumbnail($link['url']);
1052 $linksToDisplay[$key]['timestamp'] = linkdate2timestamp($link['linkdate']); 1038 $linksToDisplay[$key]['timestamp'] = linkdate2timestamp($link['linkdate']);
1053 } 1039 }
@@ -1107,7 +1093,8 @@ function renderPage()
1107 $LINKSDB = new LinkDB( 1093 $LINKSDB = new LinkDB(
1108 $GLOBALS['config']['DATASTORE'], 1094 $GLOBALS['config']['DATASTORE'],
1109 isLoggedIn(), 1095 isLoggedIn(),
1110 $GLOBALS['config']['HIDE_PUBLIC_LINKS'] 1096 $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
1097 $GLOBALS['redirector']
1111 ); 1098 );
1112 1099
1113 $PAGE = new pageBuilder; 1100 $PAGE = new pageBuilder;
@@ -1781,7 +1768,8 @@ function importFile()
1781 $LINKSDB = new LinkDB( 1768 $LINKSDB = new LinkDB(
1782 $GLOBALS['config']['DATASTORE'], 1769 $GLOBALS['config']['DATASTORE'],
1783 isLoggedIn(), 1770 isLoggedIn(),
1784 $GLOBALS['config']['HIDE_PUBLIC_LINKS'] 1771 $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
1772 $GLOBALS['redirector']
1785 ); 1773 );
1786 $filename=$_FILES['filetoupload']['name']; 1774 $filename=$_FILES['filetoupload']['name'];
1787 $filesize=$_FILES['filetoupload']['size']; 1775 $filesize=$_FILES['filetoupload']['size'];
@@ -1932,8 +1920,7 @@ function buildLinkList($PAGE,$LINKSDB)
1932 while ($i<$end && $i<count($keys)) 1920 while ($i<$end && $i<count($keys))
1933 { 1921 {
1934 $link = $linksToDisplay[$keys[$i]]; 1922 $link = $linksToDisplay[$keys[$i]];
1935 $link['description']=nl2br(keepMultipleSpaces(text2clickable($link['description']))); 1923 $link['description'] = format_description($link['description'], $GLOBALS['redirector']);
1936 $title=$link['title'];
1937 $classLi = $i%2!=0 ? '' : 'publicLinkHightLight'; 1924 $classLi = $i%2!=0 ? '' : 'publicLinkHightLight';
1938 $link['class'] = ($link['private']==0 ? $classLi : 'private'); 1925 $link['class'] = ($link['private']==0 ? $classLi : 'private');
1939 $link['timestamp']=linkdate2timestamp($link['linkdate']); 1926 $link['timestamp']=linkdate2timestamp($link['linkdate']);
@@ -1974,6 +1961,10 @@ function buildLinkList($PAGE,$LINKSDB)
1974 'links' => $linkDisp, 1961 'links' => $linkDisp,
1975 'tags' => $LINKSDB->allTags(), 1962 'tags' => $LINKSDB->allTags(),
1976 ); 1963 );
1964 // FIXME! temporary fix - see #399.
1965 if (!empty($GLOBALS['pagetitle']) && count($linkDisp) == 1) {
1966 $data['pagetitle'] = $GLOBALS['pagetitle'];
1967 }
1977 1968
1978 $pluginManager = PluginManager::getInstance(); 1969 $pluginManager = PluginManager::getInstance();
1979 $pluginManager->executeHooks('render_linklist', $data, array('loggedin' => isLoggedIn())); 1970 $pluginManager->executeHooks('render_linklist', $data, array('loggedin' => isLoggedIn()));