aboutsummaryrefslogtreecommitdiffhomepage
path: root/index.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2015-09-18 13:26:36 +0200
committerArthurHoaro <arthur@hoa.ro>2015-11-26 20:14:38 +0100
commit90e5bd65c9d4a5d3d5cedfeaa1314f2a15df5227 (patch)
tree7a085b9aecaa92aa5076df01df78c39edd2acff5 /index.php
parent986afb752bc57271e76935da9ed2df6ef8713cb7 (diff)
downloadShaarli-90e5bd65c9d4a5d3d5cedfeaa1314f2a15df5227.tar.gz
Shaarli-90e5bd65c9d4a5d3d5cedfeaa1314f2a15df5227.tar.zst
Shaarli-90e5bd65c9d4a5d3d5cedfeaa1314f2a15df5227.zip
URL encode links when a redirector is set.
Fixes #328 - URL encode links when a redirector is set * WARNING - template edit - new variable available : "real_url" Contains the final real url (redirected or any other change on original URL) * Don't redirect shaares link in RSS/Atom. * Affects links shaared in description. * Move text2clickable and keepMultipleSpaces to Utils.php + unit test UPDATE: * keepMultipleSpaces renamed to space2nbsp * space2nbsp improved to handle single space at line beginning * links in text description aren't 'nofollow' anymore
Diffstat (limited to 'index.php')
-rw-r--r--index.php48
1 files changed, 21 insertions, 27 deletions
diff --git a/index.php b/index.php
index b4d9395f..62d29f2c 100644
--- a/index.php
+++ b/index.php
@@ -340,21 +340,6 @@ function logm($message)
340 file_put_contents($GLOBAL['config']['LOG_FILE'], $t, FILE_APPEND); 340 file_put_contents($GLOBAL['config']['LOG_FILE'], $t, FILE_APPEND);
341} 341}
342 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}
350
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// ------------------------------------------------------------------------------------------ 343// ------------------------------------------------------------------------------------------
359// Sniff browser language to display dates in the right format automatically. 344// 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.) 345// (Note that is may not work on your server if the corresponding local is not installed.)
@@ -746,7 +731,8 @@ function showRSS()
746 $LINKSDB = new LinkDB( 731 $LINKSDB = new LinkDB(
747 $GLOBALS['config']['DATASTORE'], 732 $GLOBALS['config']['DATASTORE'],
748 isLoggedIn(), 733 isLoggedIn(),
749 $GLOBALS['config']['HIDE_PUBLIC_LINKS'] 734 $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
735 $GLOBALS['redirector']
750 ); 736 );
751 // Read links from database (and filter private links if user it not logged in). 737 // Read links from database (and filter private links if user it not logged in).
752 738
@@ -797,7 +783,9 @@ function showRSS()
797 // If user wants permalinks first, put the final link in description 783 // If user wants permalinks first, put the final link in description
798 if ($usepermalinks===true) $descriptionlink = '(<a href="'.$absurl.'">Link</a>)'; 784 if ($usepermalinks===true) $descriptionlink = '(<a href="'.$absurl.'">Link</a>)';
799 if (strlen($link['description'])>0) $descriptionlink = '<br>'.$descriptionlink; 785 if (strlen($link['description'])>0) $descriptionlink = '<br>'.$descriptionlink;
800 echo '<description><![CDATA['.nl2br(keepMultipleSpaces(text2clickable($link['description']))).$descriptionlink.']]></description>'."\n</item>\n"; 786 echo '<description><![CDATA['.
787 format_description($link['description'], $GLOBALS['redirector']) .
788 $descriptionlink . ']]></description>' . "\n</item>\n";
801 $i++; 789 $i++;
802 } 790 }
803 echo '</channel></rss><!-- Cached version of '.escape(page_url($_SERVER)).' -->'; 791 echo '</channel></rss><!-- Cached version of '.escape(page_url($_SERVER)).' -->';
@@ -835,7 +823,8 @@ function showATOM()
835 $LINKSDB = new LinkDB( 823 $LINKSDB = new LinkDB(
836 $GLOBALS['config']['DATASTORE'], 824 $GLOBALS['config']['DATASTORE'],
837 isLoggedIn(), 825 isLoggedIn(),
838 $GLOBALS['config']['HIDE_PUBLIC_LINKS'] 826 $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
827 $GLOBALS['redirector']
839 ); 828 );
840 829
841 // Optionally filter the results: 830 // Optionally filter the results:
@@ -876,7 +865,9 @@ function showATOM()
876 if ($usepermalinks===true) $descriptionlink = '(<a href="'.$absurl.'">Link</a>)'; 865 if ($usepermalinks===true) $descriptionlink = '(<a href="'.$absurl.'">Link</a>)';
877 if (strlen($link['description'])>0) $descriptionlink = '<br>'.$descriptionlink; 866 if (strlen($link['description'])>0) $descriptionlink = '<br>'.$descriptionlink;
878 867
879 $entries.='<content type="html"><![CDATA['.nl2br(keepMultipleSpaces(text2clickable($link['description']))).$descriptionlink."]]></content>\n"; 868 $entries .= '<content type="html"><![CDATA['.
869 format_description($link['description'], $GLOBALS['redirector']) .
870 $descriptionlink . "]]></content>\n";
880 if ($link['tags']!='') // Adding tags to each ATOM entry (as mentioned in ATOM specification) 871 if ($link['tags']!='') // Adding tags to each ATOM entry (as mentioned in ATOM specification)
881 { 872 {
882 foreach(explode(' ',$link['tags']) as $tag) 873 foreach(explode(' ',$link['tags']) as $tag)
@@ -929,7 +920,8 @@ function showDailyRSS() {
929 $LINKSDB = new LinkDB( 920 $LINKSDB = new LinkDB(
930 $GLOBALS['config']['DATASTORE'], 921 $GLOBALS['config']['DATASTORE'],
931 isLoggedIn(), 922 isLoggedIn(),
932 $GLOBALS['config']['HIDE_PUBLIC_LINKS'] 923 $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
924 $GLOBALS['redirector']
933 ); 925 );
934 926
935 /* Some Shaarlies may have very few links, so we need to look 927 /* Some Shaarlies may have very few links, so we need to look
@@ -983,7 +975,7 @@ function showDailyRSS() {
983 // We pre-format some fields for proper output. 975 // We pre-format some fields for proper output.
984 foreach ($linkdates as $linkdate) { 976 foreach ($linkdates as $linkdate) {
985 $l = $LINKSDB[$linkdate]; 977 $l = $LINKSDB[$linkdate];
986 $l['formatedDescription'] = nl2br(keepMultipleSpaces(text2clickable($l['description']))); 978 $l['formatedDescription'] = format_description($l['description'], $GLOBALS['redirector']);
987 $l['thumbnail'] = thumbnail($l['url']); 979 $l['thumbnail'] = thumbnail($l['url']);
988 $l['timestamp'] = linkdate2timestamp($l['linkdate']); 980 $l['timestamp'] = linkdate2timestamp($l['linkdate']);
989 if (startsWith($l['url'], '?')) { 981 if (startsWith($l['url'], '?')) {
@@ -1016,7 +1008,8 @@ function showDaily()
1016 $LINKSDB = new LinkDB( 1008 $LINKSDB = new LinkDB(
1017 $GLOBALS['config']['DATASTORE'], 1009 $GLOBALS['config']['DATASTORE'],
1018 isLoggedIn(), 1010 isLoggedIn(),
1019 $GLOBALS['config']['HIDE_PUBLIC_LINKS'] 1011 $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
1012 $GLOBALS['redirector']
1020 ); 1013 );
1021 1014
1022 $day=Date('Ymd',strtotime('-1 day')); // Yesterday, in format YYYYMMDD. 1015 $day=Date('Ymd',strtotime('-1 day')); // Yesterday, in format YYYYMMDD.
@@ -1047,7 +1040,7 @@ function showDaily()
1047 $taglist = explode(' ',$link['tags']); 1040 $taglist = explode(' ',$link['tags']);
1048 uasort($taglist, 'strcasecmp'); 1041 uasort($taglist, 'strcasecmp');
1049 $linksToDisplay[$key]['taglist']=$taglist; 1042 $linksToDisplay[$key]['taglist']=$taglist;
1050 $linksToDisplay[$key]['formatedDescription']=nl2br(keepMultipleSpaces(text2clickable($link['description']))); 1043 $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $GLOBALS['redirector']);
1051 $linksToDisplay[$key]['thumbnail'] = thumbnail($link['url']); 1044 $linksToDisplay[$key]['thumbnail'] = thumbnail($link['url']);
1052 $linksToDisplay[$key]['timestamp'] = linkdate2timestamp($link['linkdate']); 1045 $linksToDisplay[$key]['timestamp'] = linkdate2timestamp($link['linkdate']);
1053 } 1046 }
@@ -1107,7 +1100,8 @@ function renderPage()
1107 $LINKSDB = new LinkDB( 1100 $LINKSDB = new LinkDB(
1108 $GLOBALS['config']['DATASTORE'], 1101 $GLOBALS['config']['DATASTORE'],
1109 isLoggedIn(), 1102 isLoggedIn(),
1110 $GLOBALS['config']['HIDE_PUBLIC_LINKS'] 1103 $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
1104 $GLOBALS['redirector']
1111 ); 1105 );
1112 1106
1113 $PAGE = new pageBuilder; 1107 $PAGE = new pageBuilder;
@@ -1781,7 +1775,8 @@ function importFile()
1781 $LINKSDB = new LinkDB( 1775 $LINKSDB = new LinkDB(
1782 $GLOBALS['config']['DATASTORE'], 1776 $GLOBALS['config']['DATASTORE'],
1783 isLoggedIn(), 1777 isLoggedIn(),
1784 $GLOBALS['config']['HIDE_PUBLIC_LINKS'] 1778 $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
1779 $GLOBALS['redirector']
1785 ); 1780 );
1786 $filename=$_FILES['filetoupload']['name']; 1781 $filename=$_FILES['filetoupload']['name'];
1787 $filesize=$_FILES['filetoupload']['size']; 1782 $filesize=$_FILES['filetoupload']['size'];
@@ -1932,8 +1927,7 @@ function buildLinkList($PAGE,$LINKSDB)
1932 while ($i<$end && $i<count($keys)) 1927 while ($i<$end && $i<count($keys))
1933 { 1928 {
1934 $link = $linksToDisplay[$keys[$i]]; 1929 $link = $linksToDisplay[$keys[$i]];
1935 $link['description']=nl2br(keepMultipleSpaces(text2clickable($link['description']))); 1930 $link['description'] = format_description($link['description'], $GLOBALS['redirector']);
1936 $title=$link['title'];
1937 $classLi = $i%2!=0 ? '' : 'publicLinkHightLight'; 1931 $classLi = $i%2!=0 ? '' : 'publicLinkHightLight';
1938 $link['class'] = ($link['private']==0 ? $classLi : 'private'); 1932 $link['class'] = ($link['private']==0 ? $classLi : 'private');
1939 $link['timestamp']=linkdate2timestamp($link['linkdate']); 1933 $link['timestamp']=linkdate2timestamp($link['linkdate']);