]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Fixes #325 - Shaarli does not recognize saved links
authorArthurHoaro <arthur@hoa.ro>
Thu, 20 Aug 2015 17:47:01 +0000 (19:47 +0200)
committerArthurHoaro <arthur@hoa.ro>
Mon, 31 Aug 2015 10:26:38 +0000 (12:26 +0200)
PHP doesn't seem to autoconvert objects to strings when they're use as array indexes.

Fixes regression introduced in d9d776af19fd0a191f82525991dafbb56e1bcfcb

application/Url.php [changed mode: 0644->0755]
index.php
tests/UrlTest.php [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 23356f3..02a4395
@@ -81,6 +81,10 @@ class Url
     public function __construct($url)
     {
         $this->parts = parse_url($url);
+
+        if (!empty($url) && empty($this->parts['scheme'])) {
+            $this->parts['scheme'] = 'http';
+        }
     }
 
     /**
@@ -147,4 +151,16 @@ class Url
         $this->cleanupFragment();
         return $this->__toString();
     }
+
+    /**
+     * Get URL scheme.
+     *
+     * @return string the URL scheme or false if none is provided.
+     */
+    public function getScheme() {
+        if (!isset($this->parts['scheme'])) {
+            return false;
+        }
+        return $this->parts['scheme'];
+    }
 }
index 8e04fa3ef5724667056f022ef67bf17df77eee60..ba70b29c966a0221a657aac2f5f5cdde1765b355 100755 (executable)
--- a/index.php
+++ b/index.php
@@ -1485,51 +1485,57 @@ function renderPage()
         $url->cleanup();
 
         $link_is_new = false;
-        $link = $LINKSDB->getLinkFromUrl($url); // Check if URL is not already in database (in this case, we will edit the existing link)
+        // Check if URL is not already in database (in this case, we will edit the existing link)
+        $link = $LINKSDB->getLinkFromUrl((string)$url);
         if (!$link)
         {
-            $link_is_new = true;  // This is a new link
+            $link_is_new = true;
             $linkdate = strval(date('Ymd_His'));
-            $title = (empty($_GET['title']) ? '' : $_GET['title'] ); // Get title if it was provided in URL (by the bookmarklet).
-            $description = (empty($_GET['description']) ? '' : $_GET['description']); // Get description if it was provided in URL (by the bookmarklet). [Bronco added that]
-            $tags = (empty($_GET['tags']) ? '' : $_GET['tags'] ); // Get tags if it was provided in URL
-            $private = (!empty($_GET['private']) && $_GET['private'] === "1" ? 1 : 0); // Get private if it was provided in URL
-            if (($url!='') && parse_url($url,PHP_URL_SCHEME)=='') $url = 'http://'.$url;
+            // Get title if it was provided in URL (by the bookmarklet).
+            $title = (empty($_GET['title']) ? '' : $_GET['title'] );
+            // Get description if it was provided in URL (by the bookmarklet). [Bronco added that]
+            $description = (empty($_GET['description']) ? '' : $_GET['description']);
+            $tags = (empty($_GET['tags']) ? '' : $_GET['tags'] );
+            $private = (!empty($_GET['private']) && $_GET['private'] === "1" ? 1 : 0);
             // If this is an HTTP link, we try go get the page to extract the title (otherwise we will to straight to the edit form.)
-            if (empty($title) && parse_url($url,PHP_URL_SCHEME)=='http')
-            {
+            if (empty($title) && $url->getScheme() == 'http') {
                 list($status,$headers,$data) = getHTTP($url,4); // Short timeout to keep the application responsive.
                 // FIXME: Decode charset according to specified in either 1) HTTP response headers or 2) <head> in html
-                if (strpos($status,'200 OK')!==false)
-                                        {
-                        // Look for charset in html header.
-                                               preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
-
-                                               // If found, extract encoding.
-                                               if (!empty($meta[0]))
-                                               {
-                                                       // Get encoding specified in header.
-                                                       preg_match('#charset="?(.*)"#si', $meta[0], $enc);
-                                                       // If charset not found, use utf-8.
-                                                       $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8';
-                                               }
-                                               else { $html_charset = 'utf-8'; }
-
-                                               // Extract title
-                                               $title = html_extract_title($data);
-                                               if (!empty($title))
-                                               {
-                                                       // Re-encode title in utf-8 if necessary.
-                                                       $title = ($html_charset == 'iso-8859-1') ? utf8_encode($title) : $title;
-                                               }
-                                       }
+                if (strpos($status,'200 OK')!==false) {
+                    // Look for charset in html header.
+                    preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
+
+                    // If found, extract encoding.
+                    if (!empty($meta[0])) {
+                        // Get encoding specified in header.
+                        preg_match('#charset="?(.*)"#si', $meta[0], $enc);
+                        // If charset not found, use utf-8.
+                        $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8';
+                    }
+                    else {
+                        $html_charset = 'utf-8';
+                    }
+
+                    // Extract title
+                    $title = html_extract_title($data);
+                    if (!empty($title)) {
+                        // Re-encode title in utf-8 if necessary.
+                        $title = ($html_charset == 'iso-8859-1') ? utf8_encode($title) : $title;
+                    }
+                }
             }
-            if ($url=='') // In case of empty URL, this is just a text (with a link that points to itself)
-            {
-                $url='?'.smallHash($linkdate);
-                $title='Note: ';
+            if ($url == '') {
+                $url = '?' . smallHash($linkdate);
+                $title = 'Note: ';
             }
-            $link = array('linkdate'=>$linkdate,'title'=>$title,'url'=>$url,'description'=>$description,'tags'=>$tags,'private'=>$private);
+            $link = array(
+                'linkdate' => $linkdate,
+                'title' => $title,
+                'url' => (string)$url,
+                'description' => $description,
+                'tags' => $tags,
+                'private' => $private
+            );
         }
 
         $PAGE = new pageBuilder;
old mode 100644 (file)
new mode 100755 (executable)
index a39630f..c848e88
@@ -151,4 +151,20 @@ class UrlTest extends PHPUnit_Framework_TestCase
             $url->cleanup()
         );
     }
+
+    /**
+     * Test default http scheme.
+     */
+    public function testDefaultScheme() {
+        $url = new Url(self::$baseUrl);
+        $this->assertEquals('http', $url->getScheme());
+        $url = new Url('domain.tld');
+        $this->assertEquals('http', $url->getScheme());
+        $url = new Url('ssh://domain.tld');
+        $this->assertEquals('ssh', $url->getScheme());
+        $url = new Url('ftp://domain.tld');
+        $this->assertEquals('ftp', $url->getScheme());
+        $url = new Url('git://domain.tld/push?pull=clone#checkout');
+        $this->assertEquals('git', $url->getScheme());
+    }
 }