]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Merge pull request #556 from ArthurHoaro/login-refill
authorArthur <arthur@hoa.ro>
Sat, 14 May 2016 09:09:39 +0000 (11:09 +0200)
committerArthur <arthur@hoa.ro>
Sat, 14 May 2016 09:09:39 +0000 (11:09 +0200)
Prefill the login field when the authentication has failed

README.md
application/HttpUtils.php
application/NetscapeBookmarkUtils.php
application/Router.php
application/Utils.php
index.php
plugins/markdown/markdown.css
tests/NetscapeBookmarkUtilsTest.php
tpl/export.html
tpl/page.footer.html

index 8577fbfe5948132439293f0c84abd0083df75bc4..d67c10acbd63f7c0fc385f5792829db14d88a49e 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
 ![Shaarli logo](doc/images/doc-logo.png)
 
-The personal, minimalist, super-fast, no-database delicious clone.
+The personal, minimalist, super-fast, database free, bookmarking service.
 
 _Do you want to share the links you discover?_
 _Shaarli is a minimalist delicious clone that you can install on your own server._
index c84ba6f05746d404b195607715556a67ccce8eba..2e0792f97a5d2fdbfdf2b8978e2012300f519e54 100644 (file)
@@ -193,7 +193,7 @@ function server_url($server)
 function index_url($server)
 {
     $scriptname = $server['SCRIPT_NAME'];
-    if (endswith($scriptname, 'index.php')) {
+    if (endsWith($scriptname, 'index.php')) {
         $scriptname = substr($scriptname, 0, -9);
     }
     return server_url($server) . $scriptname;
index 8a296705f469a29c28ea267584a248179d3f540f..fdbb0ad7be393df486c6e4b0d0d4f0195e1a6c27 100644 (file)
@@ -13,17 +13,19 @@ class NetscapeBookmarkUtils
      * - timestamp  link addition date, using the Unix epoch format
      * - taglist    comma-separated tag list
      *
-     * @param LinkDB $linkDb    The link datastore
-     * @param string $selection Which links to export: (all|private|public)
+     * @param LinkDB $linkDb         Link datastore
+     * @param string $selection      Which links to export: (all|private|public)
+     * @param bool   $prependNoteUrl Prepend note permalinks with the server's URL
+     * @param string $indexUrl       Absolute URL of the Shaarli index page
      *
      * @throws Exception Invalid export selection
      *
      * @return array The links to be exported, with additional fields
      */
-    public static function filterAndFormat($linkDb, $selection)
+    public static function filterAndFormat($linkDb, $selection, $prependNoteUrl, $indexUrl)
     {
         // see tpl/export.html for possible values
-        if (! in_array($selection, array('all','public','private'))) {
+        if (! in_array($selection, array('all', 'public', 'private'))) {
             throw new Exception('Invalid export selection: "'.$selection.'"');
         }
 
@@ -39,6 +41,11 @@ class NetscapeBookmarkUtils
             $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
             $link['timestamp'] = $date->getTimestamp();
             $link['taglist'] = str_replace(' ', ',', $link['tags']);
+
+            if (startsWith($link['url'], '?') && $prependNoteUrl) {
+                $link['url'] = $indexUrl . $link['url'];
+            }
+
             $bookmarkLinks[] = $link;
         }
 
index a1e594a0846b529eedb06dd5ee02c1701b2dc095..2c3934b0079d909af6522ad2b4464ea3a1dedad2 100644 (file)
@@ -63,19 +63,19 @@ class Router
             return self::$PAGE_LINKLIST;
         }
 
-        if (startswith($query, 'do='. self::$PAGE_LOGIN) && $loggedIn === false) {
+        if (startsWith($query, 'do='. self::$PAGE_LOGIN) && $loggedIn === false) {
             return self::$PAGE_LOGIN;
         }
 
-        if (startswith($query, 'do='. self::$PAGE_PICWALL)) {
+        if (startsWith($query, 'do='. self::$PAGE_PICWALL)) {
             return self::$PAGE_PICWALL;
         }
 
-        if (startswith($query, 'do='. self::$PAGE_TAGCLOUD)) {
+        if (startsWith($query, 'do='. self::$PAGE_TAGCLOUD)) {
             return self::$PAGE_TAGCLOUD;
         }
 
-        if (startswith($query, 'do='. self::$PAGE_OPENSEARCH)) {
+        if (startsWith($query, 'do='. self::$PAGE_OPENSEARCH)) {
             return self::$PAGE_OPENSEARCH;
         }
 
@@ -96,23 +96,23 @@ class Router
             return self::$PAGE_LINKLIST;
         }
 
-        if (startswith($query, 'do='. self::$PAGE_TOOLS)) {
+        if (startsWith($query, 'do='. self::$PAGE_TOOLS)) {
             return self::$PAGE_TOOLS;
         }
 
-        if (startswith($query, 'do='. self::$PAGE_CHANGEPASSWORD)) {
+        if (startsWith($query, 'do='. self::$PAGE_CHANGEPASSWORD)) {
             return self::$PAGE_CHANGEPASSWORD;
         }
 
-        if (startswith($query, 'do='. self::$PAGE_CONFIGURE)) {
+        if (startsWith($query, 'do='. self::$PAGE_CONFIGURE)) {
             return self::$PAGE_CONFIGURE;
         }
 
-        if (startswith($query, 'do='. self::$PAGE_CHANGETAG)) {
+        if (startsWith($query, 'do='. self::$PAGE_CHANGETAG)) {
             return self::$PAGE_CHANGETAG;
         }
 
-        if (startswith($query, 'do='. self::$PAGE_ADDLINK)) {
+        if (startsWith($query, 'do='. self::$PAGE_ADDLINK)) {
             return self::$PAGE_ADDLINK;
         }
 
@@ -120,19 +120,19 @@ class Router
             return self::$PAGE_EDITLINK;
         }
 
-        if (startswith($query, 'do='. self::$PAGE_EXPORT)) {
+        if (startsWith($query, 'do='. self::$PAGE_EXPORT)) {
             return self::$PAGE_EXPORT;
         }
 
-        if (startswith($query, 'do='. self::$PAGE_IMPORT)) {
+        if (startsWith($query, 'do='. self::$PAGE_IMPORT)) {
             return self::$PAGE_IMPORT;
         }
 
-        if (startswith($query, 'do='. self::$PAGE_PLUGINSADMIN)) {
+        if (startsWith($query, 'do='. self::$PAGE_PLUGINSADMIN)) {
             return self::$PAGE_PLUGINSADMIN;
         }
 
-        if (startswith($query, 'do='. self::$PAGE_SAVE_PLUGINSADMIN)) {
+        if (startsWith($query, 'do='. self::$PAGE_SAVE_PLUGINSADMIN)) {
             return self::$PAGE_SAVE_PLUGINSADMIN;
         }
 
index 5b8ca508576a94b0bafd0a6ee957a99fd3a7588e..da521cceff728376178f11fe9fea0dae121d46eb 100644 (file)
@@ -41,8 +41,14 @@ function smallHash($text)
 
 /**
  * Tells if a string start with a substring
+ *
+ * @param string $haystack Given string.
+ * @param string $needle   String to search at the beginning of $haystack.
+ * @param bool   $case     Case sensitive.
+ *
+ * @return bool True if $haystack starts with $needle.
  */
-function startsWith($haystack, $needle, $case=true)
+function startsWith($haystack, $needle, $case = true)
 {
     if ($case) {
         return (strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
@@ -52,8 +58,14 @@ function startsWith($haystack, $needle, $case=true)
 
 /**
  * Tells if a string ends with a substring
+ *
+ * @param string $haystack Given string.
+ * @param string $needle   String to search at the end of $haystack.
+ * @param bool   $case     Case sensitive.
+ *
+ * @return bool True if $haystack ends with $needle.
  */
-function endsWith($haystack, $needle, $case=true)
+function endsWith($haystack, $needle, $case = true)
 {
     if ($case) {
         return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
index dd876a660528d4f5e98b728fd4011e058db8d23c..1eaf70cefbc0d898b7bcc20062613f354ccea1b8 100644 (file)
--- a/index.php
+++ b/index.php
@@ -690,7 +690,7 @@ class pageBuilder
 // This RSS feed cannot be filtered.
 function showDailyRSS() {
     // Cache system
-    $query = $_SERVER["QUERY_STRING"];
+    $query = $_SERVER['QUERY_STRING'];
     $cache = new CachedPage(
         $GLOBALS['config']['PAGECACHE'],
         page_url($_SERVER),
@@ -951,7 +951,7 @@ function renderPage()
         exit;
     }
     // -------- User wants to logout.
-    if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=logout'))
+    if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=logout'))
     {
         invalidateCaches($GLOBALS['config']['PAGECACHE']);
         logout();
@@ -1202,12 +1202,6 @@ function renderPage()
             exit;
         }
 
-               // Same case as above except that user tried to access ?do=addlink without being logged in
-               // Note: passing empty parameters makes Shaarli generate default URLs and descriptions.
-               if (isset($_GET['do']) && $_GET['do'] === 'addlink') {
-                       header('Location: ?do=login&post=');
-                       exit;
-               }
         showLinkList($PAGE, $LINKSDB);
         if (isset($_GET['edit_link'])) {
             header('Location: ?do=login&edit_link='. escape($_GET['edit_link']));
@@ -1593,8 +1587,9 @@ function renderPage()
         exit;
     }
 
-    // -------- Export as Netscape Bookmarks HTML file.
     if ($targetPage == Router::$PAGE_EXPORT) {
+        // Export links as a Netscape Bookmarks file
+
         if (empty($_GET['selection'])) {
             $PAGE->assign('linkcount',count($LINKSDB));
             $PAGE->renderPage('export');
@@ -1603,10 +1598,21 @@ function renderPage()
 
         // export as bookmarks_(all|private|public)_YYYYmmdd_HHMMSS.html
         $selection = $_GET['selection'];
+        if (isset($_GET['prepend_note_url'])) {
+            $prependNoteUrl = $_GET['prepend_note_url'];
+        } else {
+            $prependNoteUrl = false;
+        }
+
         try {
             $PAGE->assign(
                 'links',
-                NetscapeBookmarkUtils::filterAndFormat($LINKSDB, $selection)
+                NetscapeBookmarkUtils::filterAndFormat(
+                    $LINKSDB,
+                    $selection,
+                    $prependNoteUrl,
+                    index_url($_SERVER)
+                )
             );
         } catch (Exception $exc) {
             header('Content-Type: text/plain; charset=utf-8');
@@ -1627,7 +1633,7 @@ function renderPage()
     }
 
     // -------- User is uploading a file for import
-    if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=upload'))
+    if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=upload'))
     {
         // If file is too big, some form field may be missing.
         if (!isset($_POST['token']) || (!isset($_FILES)) || (isset($_FILES['filetoupload']['size']) && $_FILES['filetoupload']['size']==0))
@@ -1730,7 +1736,7 @@ function importFile($LINKSDB)
         {
             $link = array('linkdate'=>'','title'=>'','url'=>'','description'=>'','tags'=>'','private'=>0);
             $d = explode('<DD>',$html);
-            if (startswith($d[0],'<A '))
+            if (startsWith($d[0], '<A '))
             {
                 $link['description'] = (isset($d[1]) ? html_entity_decode(trim($d[1]),ENT_QUOTES,'UTF-8') : '');  // Get description (optional)
                 preg_match('!<A .*?>(.*?)</A>!i',$d[0],$matches); $link['title'] = (isset($matches[1]) ? trim($matches[1]) : '');  // Get title
@@ -2201,7 +2207,7 @@ function genThumbnail()
 
         // Is this a link to an image, or to a flickr page ?
         $imageurl='';
-        if (endswith(parse_url($url,PHP_URL_PATH),'.jpg'))
+        if (endsWith(parse_url($url, PHP_URL_PATH), '.jpg'))
         {  // This is a direct link to an image. e.g. http://farm1.staticflickr.com/5/5921913_ac83ed27bd_o.jpg
             preg_match('!(http://farm\d+\.staticflickr\.com/\d+/\d+_\w+_)\w.jpg!',$url,$matches);
             if (!empty($matches[1])) $imageurl=$matches[1].'m.jpg';
@@ -2378,8 +2384,8 @@ function resizeImage($filepath)
     return true;
 }
 
-if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=genthumbnail')) { genThumbnail(); exit; }  // Thumbnail generation/cache does not need the link database.
-if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=dailyrss')) { showDailyRSS(); exit; }
+if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=genthumbnail')) { genThumbnail(); exit; }  // Thumbnail generation/cache does not need the link database.
+if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=dailyrss')) { showDailyRSS(); exit; }
 if (!isset($_SESSION['LINKS_PER_PAGE'])) $_SESSION['LINKS_PER_PAGE']=$GLOBALS['config']['LINKS_PER_PAGE'];
 renderPage();
 ?>
index 3c1b2aeb2bd90a414addb9dde31c3f63b88a8a69..6789ce841cbd7e5394ee73fb238d5bc7254213a4 100644 (file)
     hyphens: none;  
 }
 
+.markdown :not(pre) code {
+    background-color: #eee;
+    padding: 1px 3px;
+    border-radius: 1px;
+    box-shadow: 0 -1px 0 #e5e5e5,0 0 1px rgba(0,0,0,0.12),0 1px 1px rgba(0,0,0,0.24);
+}
+
 .md_help {
     color: white;
 }
index b7472d92e38412455d2a8c8e9514f51d64099a92..41e6d84c22cf103e405f83d4595e6ea0db9e8817 100644 (file)
@@ -39,7 +39,7 @@ class NetscapeBookmarkUtilsTest extends PHPUnit_Framework_TestCase
      */
     public function testFilterAndFormatInvalid()
     {
-        NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'derp');
+        NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'derp', false, '');
     }
 
     /**
@@ -47,7 +47,7 @@ class NetscapeBookmarkUtilsTest extends PHPUnit_Framework_TestCase
      */
     public function testFilterAndFormatAll()
     {
-        $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all');
+        $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all', false, '');
         $this->assertEquals(self::$refDb->countLinks(), sizeof($links));
         foreach ($links as $link) {
             $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
@@ -67,7 +67,7 @@ class NetscapeBookmarkUtilsTest extends PHPUnit_Framework_TestCase
      */
     public function testFilterAndFormatPrivate()
     {
-        $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private');
+        $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private', false, '');
         $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links));
         foreach ($links as $link) {
             $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
@@ -87,7 +87,7 @@ class NetscapeBookmarkUtilsTest extends PHPUnit_Framework_TestCase
      */
     public function testFilterAndFormatPublic()
     {
-        $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public');
+        $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, '');
         $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links));
         foreach ($links as $link) {
             $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
@@ -101,4 +101,34 @@ class NetscapeBookmarkUtilsTest extends PHPUnit_Framework_TestCase
             );
         }
     }
+
+    /**
+     * Do not prepend notes with the Shaarli index's URL
+     */
+    public function testFilterAndFormatDoNotPrependNoteUrl()
+    {
+        $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, '');
+        $this->assertEquals(
+            '?WDWyig',
+            $links[0]['url']
+        );
+    }
+
+    /**
+     * Prepend notes with the Shaarli index's URL
+     */
+    public function testFilterAndFormatPrependNoteUrl()
+    {
+        $indexUrl = 'http://localhost:7469/shaarli/';
+        $links = NetscapeBookmarkUtils::filterAndFormat(
+            self::$linkDb,
+            'public',
+            true,
+            $indexUrl
+        );
+        $this->assertEquals(
+            $indexUrl . '?WDWyig',
+            $links[0]['url']
+        );
+    }
 }
index 9582627a2e4d617c6b9e7ccba117186ecaad64ed..67c3d05fb8a4926260d32f4a5ca09b8f2e9bca4c 100644 (file)
@@ -5,15 +5,21 @@
   <div id="pageheader">
     {include="page.header"}
     <div id="toolsdiv">
-      <a href="?do=export&amp;selection=all">
-        <b>Export all</b><span>: Export all links</span>
-      </a><br>
-      <a href="?do=export&amp;selection=public">
-        <b>Export public</b><span>: Only export public links</span>
-      </a><br>
-      <a href="?do=export&amp;selection=private">
-        <b>Export private</b><span>: Only export private links</span>
-      </a>
+      <form method="GET">
+        <input type="hidden" name="do" value="export">
+        Selection:<br>
+        <input type="radio" name="selection" value="all" checked="true"> All<br>
+        <input type="radio" name="selection" value="private"> Private<br>
+        <input type="radio" name="selection" value="public"> Public<br>
+        <br>
+        <input type="checkbox" name="prepend_note_url" id="prepend_note_url">
+        <label for="prepend_note_url">
+          Prepend note permalinks with this Shaarli instance's URL
+          <em>(useful to import bookmarks in a web browser)</em>
+        </label>
+        <br><br>
+        <input class="bigbutton" type="submit" value="Export">
+      </form>
       <div class="clear"></div>
     </div>
   </div>
index 195dada049cdbd9fb3642dba25a4cee16a888fdf..006d1d683b465d303ae56c4332d659bedab0533b 100644 (file)
@@ -1,5 +1,7 @@
 <div id="footer">
-    <b><a href="https://github.com/shaarli/Shaarli">Shaarli</a></b> - The personal, minimalist, super-fast, no-database delicious clone by the <a href="https://github.com/shaarli/Shaarli">Shaarli</a> community - <a href="doc/Home.html">Help/documentation</a>
+  <strong><a href="https://github.com/shaarli/Shaarli">Shaarli</a></strong>
+  - The personal, minimalist, super-fast, database free, bookmarking service by the Shaarli community
+  - <a href="doc/Home.html" rel="nofollow">Help/documentation</a>
     {loop="$plugins_footer.text"}
         {$value}
     {/loop}