]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - index.php
Display notes as absolute urls
[github/shaarli/Shaarli.git] / index.php
index 7795a5974388e20b8a7f11b10ae5691950748fb7..83c507bbd64581800c664d6ff62eaa8271233651 100644 (file)
--- a/index.php
+++ b/index.php
@@ -302,17 +302,12 @@ function keepMultipleSpaces($text)
 // (Note that is may not work on your server if the corresponding local is not installed.)
 function autoLocale()
 {
-    $attempts = array('en_US'); // Default if browser does not send HTTP_ACCEPT_LANGUAGE
+    $loc='en_US'; // Default if browser does not send HTTP_ACCEPT_LANGUAGE
     if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) // e.g. "fr,fr-fr;q=0.8,en;q=0.5,en-us;q=0.3"
     {   // (It's a bit crude, but it works very well. Preferred language is always presented first.)
-        if (preg_match('/([a-z]{2})-?([a-z]{2})?/i',$_SERVER['HTTP_ACCEPT_LANGUAGE'],$matches)) {
-            $loc = $matches[1] . (!empty($matches[2]) ? '_' . strtoupper($matches[2]) : '');
-            $attempts = array($loc, str_replace('_', '-', $loc),
-                $loc . '_' . strtoupper($loc), $loc . '_' . $loc,
-                $loc . '-' . strtoupper($loc), $loc . '-' . $loc);
-        }
+        if (preg_match('/([a-z]{2}(-[a-z]{2})?)/i',$_SERVER['HTTP_ACCEPT_LANGUAGE'],$matches)) $loc=$matches[1];
     }
-    setlocale(LC_TIME, $attempts);  // LC_TIME = Set local for date/time format only.
+    setlocale(LC_TIME,$loc);  // LC_TIME = Set local for date/time format only.
 }
 
 // ------------------------------------------------------------------------------------------
@@ -554,7 +549,7 @@ function endsWith($haystack,$needle,$case=true)
 function linkdate2timestamp($linkdate)
 {
     $Y=$M=$D=$h=$m=$s=0;
-    sscanf($linkdate,'%4d%2d%2d_%2d%2d%2d',$Y,$M,$D,$h,$m,$s);
+    $r = sscanf($linkdate,'%4d%2d%2d_%2d%2d%2d',$Y,$M,$D,$h,$m,$s);
     return mktime($h,$m,$s,$M,$D,$Y);
 }
 
@@ -572,6 +567,16 @@ function linkdate2iso8601($linkdate)
     return date('c',linkdate2timestamp($linkdate)); // 'c' is for ISO 8601 date format.
 }
 
+/*  Converts a linkdate time (YYYYMMDD_HHMMSS) of an article to a localized date format.
+    (used to display link date on screen)
+    The date format is automatically chosen according to locale/languages sniffed from browser headers (see autoLocale()). */
+function linkdate2locale($linkdate)
+{
+    return utf8_encode(strftime('%c',linkdate2timestamp($linkdate))); // %c is for automatic date format according to locale.
+    // Note that if you use a locale which is not installed on your webserver,
+    // the date will not be displayed in the chosen locale, but probably in US notation.
+}
+
 // Parse HTTP response headers and return an associative array.
 function http_parse_headers_shaarli( $headers )
 {
@@ -1137,7 +1142,7 @@ function showDailyRSS()
             $l = $LINKSDB[$linkdate];
             $l['formatedDescription']=nl2br(keepMultipleSpaces(text2clickable(htmlspecialchars($l['description']))));
             $l['thumbnail'] = thumbnail($l['url']);
-            $l['timestamp'] = linkdate2timestamp($l['linkdate']);
+            $l['localdate']=linkdate2locale($l['linkdate']);
             if (startsWith($l['url'],'?')) $l['url']=indexUrl().$l['url'];  // make permalink URL absolute
             $links[$linkdate]=$l;
         }
@@ -1185,7 +1190,7 @@ function showDaily()
         $linksToDisplay[$key]['taglist']=$taglist;
         $linksToDisplay[$key]['formatedDescription']=nl2br(keepMultipleSpaces(text2clickable(htmlspecialchars($link['description']))));
         $linksToDisplay[$key]['thumbnail'] = thumbnail($link['url']);
-        $linksToDisplay[$key]['timestamp'] = linkdate2timestamp($link['linkdate']);
+        $linksToDisplay[$key]['localdate'] = linkdate2locale($link['linkdate']);
     }
 
     /* We need to spread the articles on 3 columns.
@@ -1935,14 +1940,21 @@ function buildLinkList($PAGE,$LINKSDB)
     while ($i<$end && $i<count($keys))
     {
         $link = $linksToDisplay[$keys[$i]];
+        $title               = $link['title'];
+        $taglist             = explode(' ',$link['tags']);
+        uasort($taglist, 'strcasecmp');
+        $classLi             = $i%2!=0 ? '' : 'publicLinkHightLight'; // This could really be done with just a css pseudoclass.
         $link['description']=nl2br(keepMultipleSpaces(text2clickable(htmlspecialchars($link['description']))));
-        $title=$link['title'];
-        $classLi =  $i%2!=0 ? '' : 'publicLinkHightLight';
         $link['class'] = ($link['private']==0 ? $classLi : 'private');
         $link['timestamp']=linkdate2timestamp($link['linkdate']);
-        $taglist = explode(' ',$link['tags']);
-        uasort($taglist, 'strcasecmp');
         $link['taglist']=$taglist;
+
+        // Convert notes to absolute URLs
+        if ($link["url"][0]      === '?' && // Check for both signs of a note: starting with ? and 7 chars long. I doubt that you'll post any links that look like this.
+            strlen($link["url"]) === 7) {
+            $link["url"] = indexUrl() . $link["url"];
+        }
+
         $linkDisp[$keys[$i]] = $link;
         $i++;
     }