aboutsummaryrefslogtreecommitdiffhomepage
path: root/index.php
diff options
context:
space:
mode:
Diffstat (limited to 'index.php')
-rw-r--r--index.php98
1 files changed, 31 insertions, 67 deletions
diff --git a/index.php b/index.php
index 3a63319c..a9264cbb 100644
--- a/index.php
+++ b/index.php
@@ -268,7 +268,7 @@ $GLOBALS['redirector'] = !empty($GLOBALS['redirector']) ? escape($GLOBALS['redir
268// a token depending of deployment salt, user password, and the current ip 268// a token depending of deployment salt, user password, and the current ip
269define('STAY_SIGNED_IN_TOKEN', sha1($GLOBALS['hash'].$_SERVER["REMOTE_ADDR"].$GLOBALS['salt'])); 269define('STAY_SIGNED_IN_TOKEN', sha1($GLOBALS['hash'].$_SERVER["REMOTE_ADDR"].$GLOBALS['salt']));
270 270
271autoLocale(); // Sniff browser language and set date format accordingly. 271autoLocale($_SERVER['HTTP_ACCEPT_LANGUAGE']); // Sniff browser language and set date format accordingly.
272header('Content-Type: text/html; charset=utf-8'); // We use UTF-8 for proper international characters handling. 272header('Content-Type: text/html; charset=utf-8'); // We use UTF-8 for proper international characters handling.
273 273
274//================================================================================================== 274//==================================================================================================
@@ -315,26 +315,6 @@ function setup_login_state() {
315} 315}
316$userIsLoggedIn = setup_login_state(); 316$userIsLoggedIn = setup_login_state();
317 317
318
319// ------------------------------------------------------------------------------------------
320// Sniff browser language to display dates in the right format automatically.
321// (Note that is may not work on your server if the corresponding local is not installed.)
322function autoLocale()
323{
324 $attempts = array('en_US'); // Default if browser does not send HTTP_ACCEPT_LANGUAGE
325 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) // e.g. "fr,fr-fr;q=0.8,en;q=0.5,en-us;q=0.3"
326 { // (It's a bit crude, but it works very well. Preferred language is always presented first.)
327 if (preg_match('/([a-z]{2})-?([a-z]{2})?/i',$_SERVER['HTTP_ACCEPT_LANGUAGE'],$matches)) {
328 $loc = $matches[1] . (!empty($matches[2]) ? '_' . strtoupper($matches[2]) : '');
329 $attempts = array($loc.'.UTF-8', $loc, str_replace('_', '-', $loc).'.UTF-8', str_replace('_', '-', $loc),
330 $loc . '_' . strtoupper($loc).'.UTF-8', $loc . '_' . strtoupper($loc),
331 $loc . '_' . $loc.'.UTF-8', $loc . '_' . $loc, $loc . '-' . strtoupper($loc).'.UTF-8',
332 $loc . '-' . strtoupper($loc), $loc . '-' . $loc.'.UTF-8', $loc . '-' . $loc);
333 }
334 }
335 setlocale(LC_TIME, $attempts); // LC_TIME = Set local for date/time format only.
336}
337
338// ------------------------------------------------------------------------------------------ 318// ------------------------------------------------------------------------------------------
339// PubSubHubbub protocol support (if enabled) [UNTESTED] 319// PubSubHubbub protocol support (if enabled) [UNTESTED]
340// (Source: http://aldarone.fr/les-flux-rss-shaarli-et-pubsubhubbub/ ) 320// (Source: http://aldarone.fr/les-flux-rss-shaarli-et-pubsubhubbub/ )
@@ -551,33 +531,6 @@ function getMaxFileSize()
551 return $maxsize; 531 return $maxsize;
552} 532}
553 533
554/* Converts a linkdate time (YYYYMMDD_HHMMSS) of an article to a timestamp (Unix epoch)
555 (used to build the ADD_DATE attribute in Netscape-bookmarks file)
556 PS: I could have used strptime(), but it does not exist on Windows. I'm too kind. */
557function linkdate2timestamp($linkdate)
558{
559 if(strcmp($linkdate, '_000000') !== 0 || !$linkdate){
560 $Y=$M=$D=$h=$m=$s=0;
561 $r = sscanf($linkdate,'%4d%2d%2d_%2d%2d%2d',$Y,$M,$D,$h,$m,$s);
562 return mktime($h,$m,$s,$M,$D,$Y);
563 }
564 return time();
565}
566
567/* Converts a linkdate time (YYYYMMDD_HHMMSS) of an article to a RFC822 date.
568 (used to build the pubDate attribute in RSS feed.) */
569function linkdate2rfc822($linkdate)
570{
571 return date('r',linkdate2timestamp($linkdate)); // 'r' is for RFC822 date format.
572}
573
574/* Converts a linkdate time (YYYYMMDD_HHMMSS) of an article to a ISO 8601 date.
575 (used to build the updated tags in ATOM feed.) */
576function linkdate2iso8601($linkdate)
577{
578 return date('c',linkdate2timestamp($linkdate)); // 'c' is for ISO 8601 date format.
579}
580
581// ------------------------------------------------------------------------------------------ 534// ------------------------------------------------------------------------------------------
582// Token management for XSRF protection 535// Token management for XSRF protection
583// Token should be used in any form which acts on data (create,update,delete,import...). 536// Token should be used in any form which acts on data (create,update,delete,import...).
@@ -769,14 +722,16 @@ function showRSS()
769 { 722 {
770 $link = $linksToDisplay[$keys[$i]]; 723 $link = $linksToDisplay[$keys[$i]];
771 $guid = $pageaddr.'?'.smallHash($link['linkdate']); 724 $guid = $pageaddr.'?'.smallHash($link['linkdate']);
772 $rfc822date = linkdate2rfc822($link['linkdate']); 725 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
773 $absurl = $link['url']; 726 $absurl = $link['url'];
774 if (startsWith($absurl,'?')) $absurl=$pageaddr.$absurl; // make permalink URL absolute 727 if (startsWith($absurl,'?')) $absurl=$pageaddr.$absurl; // make permalink URL absolute
775 if ($usepermalinks===true) 728 if ($usepermalinks===true)
776 echo '<item><title>'.$link['title'].'</title><guid isPermaLink="true">'.$guid.'</guid><link>'.$guid.'</link>'; 729 echo '<item><title>'.$link['title'].'</title><guid isPermaLink="true">'.$guid.'</guid><link>'.$guid.'</link>';
777 else 730 else
778 echo '<item><title>'.$link['title'].'</title><guid isPermaLink="false">'.$guid.'</guid><link>'.$absurl.'</link>'; 731 echo '<item><title>'.$link['title'].'</title><guid isPermaLink="false">'.$guid.'</guid><link>'.$absurl.'</link>';
779 if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) echo '<pubDate>'.escape($rfc822date)."</pubDate>\n"; 732 if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) {
733 echo '<pubDate>'.escape($date->format(DateTime::RSS))."</pubDate>\n";
734 }
780 if ($link['tags']!='') // Adding tags to each RSS entry (as mentioned in RSS specification) 735 if ($link['tags']!='') // Adding tags to each RSS entry (as mentioned in RSS specification)
781 { 736 {
782 foreach(explode(' ',$link['tags']) as $tag) { echo '<category domain="'.$pageaddr.'">'.$tag.'</category>'."\n"; } 737 foreach(explode(' ',$link['tags']) as $tag) { echo '<category domain="'.$pageaddr.'">'.$tag.'</category>'."\n"; }
@@ -857,8 +812,9 @@ function showATOM()
857 { 812 {
858 $link = $linksToDisplay[$keys[$i]]; 813 $link = $linksToDisplay[$keys[$i]];
859 $guid = $pageaddr.'?'.smallHash($link['linkdate']); 814 $guid = $pageaddr.'?'.smallHash($link['linkdate']);
860 $iso8601date = linkdate2iso8601($link['linkdate']); 815 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
861 $latestDate = max($latestDate,$iso8601date); 816 $iso8601date = $date->format(DateTime::ISO8601);
817 $latestDate = max($latestDate, $iso8601date);
862 $absurl = $link['url']; 818 $absurl = $link['url'];
863 if (startsWith($absurl,'?')) $absurl=$pageaddr.$absurl; // make permalink URL absolute 819 if (startsWith($absurl,'?')) $absurl=$pageaddr.$absurl; // make permalink URL absolute
864 $entries.='<entry><title>'.$link['title'].'</title>'; 820 $entries.='<entry><title>'.$link['title'].'</title>';
@@ -866,7 +822,10 @@ function showATOM()
866 $entries.='<link href="'.$guid.'" /><id>'.$guid.'</id>'; 822 $entries.='<link href="'.$guid.'" /><id>'.$guid.'</id>';
867 else 823 else
868 $entries.='<link href="'.$absurl.'" /><id>'.$guid.'</id>'; 824 $entries.='<link href="'.$absurl.'" /><id>'.$guid.'</id>';
869 if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) $entries.='<updated>'.escape($iso8601date).'</updated>'; 825
826 if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) {
827 $entries.='<updated>'.escape($iso8601date).'</updated>';
828 }
870 829
871 // Add permalink in description 830 // Add permalink in description
872 $descriptionlink = '(<a href="'.$guid.'">Permalink</a>)'; 831 $descriptionlink = '(<a href="'.$guid.'">Permalink</a>)';
@@ -972,8 +931,7 @@ function showDailyRSS() {
972 931
973 // For each day. 932 // For each day.
974 foreach ($days as $day => $linkdates) { 933 foreach ($days as $day => $linkdates) {
975 $daydate = linkdate2timestamp($day.'_000000'); // Full text date 934 $dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000');
976 $rfc822date = linkdate2rfc822($day.'_000000');
977 $absurl = escape(index_url($_SERVER).'?do=daily&day='.$day); // Absolute URL of the corresponding "Daily" page. 935 $absurl = escape(index_url($_SERVER).'?do=daily&day='.$day); // Absolute URL of the corresponding "Daily" page.
978 936
979 // Build the HTML body of this RSS entry. 937 // Build the HTML body of this RSS entry.
@@ -986,7 +944,8 @@ function showDailyRSS() {
986 $l = $LINKSDB[$linkdate]; 944 $l = $LINKSDB[$linkdate];
987 $l['formatedDescription'] = format_description($l['description'], $GLOBALS['redirector']); 945 $l['formatedDescription'] = format_description($l['description'], $GLOBALS['redirector']);
988 $l['thumbnail'] = thumbnail($l['url']); 946 $l['thumbnail'] = thumbnail($l['url']);
989 $l['timestamp'] = linkdate2timestamp($l['linkdate']); 947 $l_date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $l['linkdate']);
948 $l['timestamp'] = $l_date->getTimestamp();
990 if (startsWith($l['url'], '?')) { 949 if (startsWith($l['url'], '?')) {
991 $l['url'] = index_url($_SERVER) . $l['url']; // make permalink URL absolute 950 $l['url'] = index_url($_SERVER) . $l['url']; // make permalink URL absolute
992 } 951 }
@@ -996,10 +955,10 @@ function showDailyRSS() {
996 // Then build the HTML for this day: 955 // Then build the HTML for this day:
997 $tpl = new RainTPL; 956 $tpl = new RainTPL;
998 $tpl->assign('title', $GLOBALS['title']); 957 $tpl->assign('title', $GLOBALS['title']);
999 $tpl->assign('daydate', $daydate); 958 $tpl->assign('daydate', $dayDate->getTimestamp());
1000 $tpl->assign('absurl', $absurl); 959 $tpl->assign('absurl', $absurl);
1001 $tpl->assign('links', $links); 960 $tpl->assign('links', $links);
1002 $tpl->assign('rfc822date', escape($rfc822date)); 961 $tpl->assign('rssdate', escape($dayDate->format(DateTime::RSS)));
1003 $html = $tpl->draw('dailyrss', $return_string=true); 962 $html = $tpl->draw('dailyrss', $return_string=true);
1004 963
1005 echo $html . PHP_EOL; 964 echo $html . PHP_EOL;
@@ -1055,7 +1014,8 @@ function showDaily($pageBuilder)
1055 $linksToDisplay[$key]['taglist']=$taglist; 1014 $linksToDisplay[$key]['taglist']=$taglist;
1056 $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $GLOBALS['redirector']); 1015 $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $GLOBALS['redirector']);
1057 $linksToDisplay[$key]['thumbnail'] = thumbnail($link['url']); 1016 $linksToDisplay[$key]['thumbnail'] = thumbnail($link['url']);
1058 $linksToDisplay[$key]['timestamp'] = linkdate2timestamp($link['linkdate']); 1017 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
1018 $linksToDisplay[$key]['timestamp'] = $date->getTimestamp();
1059 } 1019 }
1060 1020
1061 /* We need to spread the articles on 3 columns. 1021 /* We need to spread the articles on 3 columns.
@@ -1080,11 +1040,12 @@ function showDaily($pageBuilder)
1080 $fill[$index]+=$length; 1040 $fill[$index]+=$length;
1081 } 1041 }
1082 1042
1043 $dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000');
1083 $data = array( 1044 $data = array(
1084 'linksToDisplay' => $linksToDisplay, 1045 'linksToDisplay' => $linksToDisplay,
1085 'linkcount' => count($LINKSDB), 1046 'linkcount' => count($LINKSDB),
1086 'cols' => $columns, 1047 'cols' => $columns,
1087 'day' => linkdate2timestamp($day.'_000000'), 1048 'day' => $dayDate->getTimestamp(),
1088 'previousday' => $previousday, 1049 'previousday' => $previousday,
1089 'nextday' => $nextday, 1050 'nextday' => $nextday,
1090 ); 1051 );
@@ -1238,11 +1199,12 @@ function renderPage()
1238 uksort($tags, function($a, $b) { 1199 uksort($tags, function($a, $b) {
1239 // Collator is part of PHP intl. 1200 // Collator is part of PHP intl.
1240 if (class_exists('Collator')) { 1201 if (class_exists('Collator')) {
1241 $c = new Collator(setlocale(LC_ALL, 0)); 1202 $c = new Collator(setlocale(LC_COLLATE, 0));
1242 return $c->compare($a, $b); 1203 if (!intl_is_failure(intl_get_error_code())) {
1243 } else { 1204 return $c->compare($a, $b);
1244 return strcasecmp($a, $b); 1205 }
1245 } 1206 }
1207 return strcasecmp($a, $b);
1246 }); 1208 });
1247 1209
1248 $tagList=array(); 1210 $tagList=array();
@@ -1588,7 +1550,7 @@ function renderPage()
1588 $link = array( 1550 $link = array(
1589 'title' => trim($_POST['lf_title']), 1551 'title' => trim($_POST['lf_title']),
1590 'url' => $url, 1552 'url' => $url,
1591 'description' => trim($_POST['lf_description']), 1553 'description' => $_POST['lf_description'],
1592 'private' => (isset($_POST['lf_private']) ? 1 : 0), 1554 'private' => (isset($_POST['lf_private']) ? 1 : 0),
1593 'linkdate' => $linkdate, 1555 'linkdate' => $linkdate,
1594 'tags' => str_replace(',', ' ', $tags) 1556 'tags' => str_replace(',', ' ', $tags)
@@ -1799,7 +1761,8 @@ HTML;
1799 ($exportWhat=='private' && $link['private']!=0) || 1761 ($exportWhat=='private' && $link['private']!=0) ||
1800 ($exportWhat=='public' && $link['private']==0)) 1762 ($exportWhat=='public' && $link['private']==0))
1801 { 1763 {
1802 echo '<DT><A HREF="'.$link['url'].'" ADD_DATE="'.linkdate2timestamp($link['linkdate']).'" PRIVATE="'.$link['private'].'"'; 1764 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
1765 echo '<DT><A HREF="'.$link['url'].'" ADD_DATE="'.$date->getTimestamp().'" PRIVATE="'.$link['private'].'"';
1803 if ($link['tags']!='') echo ' TAGS="'.str_replace(' ',',',$link['tags']).'"'; 1766 if ($link['tags']!='') echo ' TAGS="'.str_replace(' ',',',$link['tags']).'"';
1804 echo '>'.$link['title']."</A>\n"; 1767 echo '>'.$link['title']."</A>\n";
1805 if ($link['description']!='') echo '<DD>'.$link['description']."\n"; 1768 if ($link['description']!='') echo '<DD>'.$link['description']."\n";
@@ -2042,7 +2005,8 @@ function buildLinkList($PAGE,$LINKSDB)
2042 $link['description'] = format_description($link['description'], $GLOBALS['redirector']); 2005 $link['description'] = format_description($link['description'], $GLOBALS['redirector']);
2043 $classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight'; 2006 $classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight';
2044 $link['class'] = $link['private'] == 0 ? $classLi : 'private'; 2007 $link['class'] = $link['private'] == 0 ? $classLi : 'private';
2045 $link['timestamp'] = linkdate2timestamp($link['linkdate']); 2008 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
2009 $link['timestamp'] = $date->getTimestamp();
2046 $taglist = explode(' ', $link['tags']); 2010 $taglist = explode(' ', $link['tags']);
2047 uasort($taglist, 'strcasecmp'); 2011 uasort($taglist, 'strcasecmp');
2048 $link['taglist'] = $taglist; 2012 $link['taglist'] = $taglist;