aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2015-08-31 20:31:41 +0200
committerVirtualTam <virtualtam@flibidi.net>2015-08-31 20:31:41 +0200
commit6211c498f6e0bdc6d86152e9777bcc75955a5ec4 (patch)
treef7b48317044c151609b9d8e7d476cc5014916245
parentce8e248ab04a035c2824bee6af91aed49d623a6a (diff)
parent26c503460cd2b50c2c122dba73d62e09ee04b9c8 (diff)
downloadShaarli-6211c498f6e0bdc6d86152e9777bcc75955a5ec4.tar.gz
Shaarli-6211c498f6e0bdc6d86152e9777bcc75955a5ec4.tar.zst
Shaarli-6211c498f6e0bdc6d86152e9777bcc75955a5ec4.zip
Merge pull request #326 from ArthurHoaro/bug-url
Fixes #325 - Shaarli does not recognize saved links
-rwxr-xr-x[-rw-r--r--]application/Url.php16
-rwxr-xr-xindex.php82
-rwxr-xr-x[-rw-r--r--]tests/UrlTest.php16
3 files changed, 76 insertions, 38 deletions
diff --git a/application/Url.php b/application/Url.php
index 23356f39..02a4395d 100644..100755
--- a/application/Url.php
+++ b/application/Url.php
@@ -81,6 +81,10 @@ class Url
81 public function __construct($url) 81 public function __construct($url)
82 { 82 {
83 $this->parts = parse_url($url); 83 $this->parts = parse_url($url);
84
85 if (!empty($url) && empty($this->parts['scheme'])) {
86 $this->parts['scheme'] = 'http';
87 }
84 } 88 }
85 89
86 /** 90 /**
@@ -147,4 +151,16 @@ class Url
147 $this->cleanupFragment(); 151 $this->cleanupFragment();
148 return $this->__toString(); 152 return $this->__toString();
149 } 153 }
154
155 /**
156 * Get URL scheme.
157 *
158 * @return string the URL scheme or false if none is provided.
159 */
160 public function getScheme() {
161 if (!isset($this->parts['scheme'])) {
162 return false;
163 }
164 return $this->parts['scheme'];
165 }
150} 166}
diff --git a/index.php b/index.php
index a093a283..f4c7e781 100755
--- a/index.php
+++ b/index.php
@@ -1500,51 +1500,57 @@ function renderPage()
1500 $url->cleanup(); 1500 $url->cleanup();
1501 1501
1502 $link_is_new = false; 1502 $link_is_new = false;
1503 $link = $LINKSDB->getLinkFromUrl($url); // Check if URL is not already in database (in this case, we will edit the existing link) 1503 // Check if URL is not already in database (in this case, we will edit the existing link)
1504 $link = $LINKSDB->getLinkFromUrl((string)$url);
1504 if (!$link) 1505 if (!$link)
1505 { 1506 {
1506 $link_is_new = true; // This is a new link 1507 $link_is_new = true;
1507 $linkdate = strval(date('Ymd_His')); 1508 $linkdate = strval(date('Ymd_His'));
1508 $title = (empty($_GET['title']) ? '' : $_GET['title'] ); // Get title if it was provided in URL (by the bookmarklet). 1509 // Get title if it was provided in URL (by the bookmarklet).
1509 $description = (empty($_GET['description']) ? '' : $_GET['description']); // Get description if it was provided in URL (by the bookmarklet). [Bronco added that] 1510 $title = (empty($_GET['title']) ? '' : $_GET['title'] );
1510 $tags = (empty($_GET['tags']) ? '' : $_GET['tags'] ); // Get tags if it was provided in URL 1511 // Get description if it was provided in URL (by the bookmarklet). [Bronco added that]
1511 $private = (!empty($_GET['private']) && $_GET['private'] === "1" ? 1 : 0); // Get private if it was provided in URL 1512 $description = (empty($_GET['description']) ? '' : $_GET['description']);
1512 if (($url!='') && parse_url($url,PHP_URL_SCHEME)=='') $url = 'http://'.$url; 1513 $tags = (empty($_GET['tags']) ? '' : $_GET['tags'] );
1513 // 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.) 1514 $private = (!empty($_GET['private']) && $_GET['private'] === "1" ? 1 : 0);
1514 if (empty($title) && parse_url($url,PHP_URL_SCHEME)=='http') 1515 // If this is an HTTP(S) link, we try go get the page to extract the title (otherwise we will to straight to the edit form.)
1515 { 1516 if (empty($title) && strpos($url->getScheme(), 'http') !== false) {
1516 list($status,$headers,$data) = getHTTP($url,4); // Short timeout to keep the application responsive. 1517 list($status,$headers,$data) = getHTTP($url,4); // Short timeout to keep the application responsive.
1517 // FIXME: Decode charset according to specified in either 1) HTTP response headers or 2) <head> in html 1518 // FIXME: Decode charset according to specified in either 1) HTTP response headers or 2) <head> in html
1518 if (strpos($status,'200 OK')!==false) 1519 if (strpos($status,'200 OK')!==false) {
1519 { 1520 // Look for charset in html header.
1520 // Look for charset in html header. 1521 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
1521 preg_match('#<meta .*charset=.*>#Usi', $data, $meta); 1522
1522 1523 // If found, extract encoding.
1523 // If found, extract encoding. 1524 if (!empty($meta[0])) {
1524 if (!empty($meta[0])) 1525 // Get encoding specified in header.
1525 { 1526 preg_match('#charset="?(.*)"#si', $meta[0], $enc);
1526 // Get encoding specified in header. 1527 // If charset not found, use utf-8.
1527 preg_match('#charset="?(.*)"#si', $meta[0], $enc); 1528 $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8';
1528 // If charset not found, use utf-8. 1529 }
1529 $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8'; 1530 else {
1530 } 1531 $html_charset = 'utf-8';
1531 else { $html_charset = 'utf-8'; } 1532 }
1532 1533
1533 // Extract title 1534 // Extract title
1534 $title = html_extract_title($data); 1535 $title = html_extract_title($data);
1535 if (!empty($title)) 1536 if (!empty($title)) {
1536 { 1537 // Re-encode title in utf-8 if necessary.
1537 // Re-encode title in utf-8 if necessary. 1538 $title = ($html_charset == 'iso-8859-1') ? utf8_encode($title) : $title;
1538 $title = ($html_charset == 'iso-8859-1') ? utf8_encode($title) : $title; 1539 }
1539 } 1540 }
1540 }
1541 } 1541 }
1542 if ($url=='') // In case of empty URL, this is just a text (with a link that points to itself) 1542 if ($url == '') {
1543 { 1543 $url = '?' . smallHash($linkdate);
1544 $url='?'.smallHash($linkdate); 1544 $title = 'Note: ';
1545 $title='Note: ';
1546 } 1545 }
1547 $link = array('linkdate'=>$linkdate,'title'=>$title,'url'=>$url,'description'=>$description,'tags'=>$tags,'private'=>$private); 1546 $link = array(
1547 'linkdate' => $linkdate,
1548 'title' => $title,
1549 'url' => (string)$url,
1550 'description' => $description,
1551 'tags' => $tags,
1552 'private' => $private
1553 );
1548 } 1554 }
1549 1555
1550 $PAGE = new pageBuilder; 1556 $PAGE = new pageBuilder;
diff --git a/tests/UrlTest.php b/tests/UrlTest.php
index a39630f1..c848e88e 100644..100755
--- a/tests/UrlTest.php
+++ b/tests/UrlTest.php
@@ -151,4 +151,20 @@ class UrlTest extends PHPUnit_Framework_TestCase
151 $url->cleanup() 151 $url->cleanup()
152 ); 152 );
153 } 153 }
154
155 /**
156 * Test default http scheme.
157 */
158 public function testDefaultScheme() {
159 $url = new Url(self::$baseUrl);
160 $this->assertEquals('http', $url->getScheme());
161 $url = new Url('domain.tld');
162 $this->assertEquals('http', $url->getScheme());
163 $url = new Url('ssh://domain.tld');
164 $this->assertEquals('ssh', $url->getScheme());
165 $url = new Url('ftp://domain.tld');
166 $this->assertEquals('ftp', $url->getScheme());
167 $url = new Url('git://domain.tld/push?pull=clone#checkout');
168 $this->assertEquals('git', $url->getScheme());
169 }
154} 170}