diff options
-rwxr-xr-x[-rw-r--r--] | application/Url.php | 16 | ||||
-rwxr-xr-x | index.php | 80 | ||||
-rwxr-xr-x[-rw-r--r--] | tests/UrlTest.php | 16 |
3 files changed, 75 insertions, 37 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 | } |
@@ -1485,51 +1485,57 @@ function renderPage() | |||
1485 | $url->cleanup(); | 1485 | $url->cleanup(); |
1486 | 1486 | ||
1487 | $link_is_new = false; | 1487 | $link_is_new = false; |
1488 | $link = $LINKSDB->getLinkFromUrl($url); // Check if URL is not already in database (in this case, we will edit the existing link) | 1488 | // Check if URL is not already in database (in this case, we will edit the existing link) |
1489 | $link = $LINKSDB->getLinkFromUrl((string)$url); | ||
1489 | if (!$link) | 1490 | if (!$link) |
1490 | { | 1491 | { |
1491 | $link_is_new = true; // This is a new link | 1492 | $link_is_new = true; |
1492 | $linkdate = strval(date('Ymd_His')); | 1493 | $linkdate = strval(date('Ymd_His')); |
1493 | $title = (empty($_GET['title']) ? '' : $_GET['title'] ); // Get title if it was provided in URL (by the bookmarklet). | 1494 | // Get title if it was provided in URL (by the bookmarklet). |
1494 | $description = (empty($_GET['description']) ? '' : $_GET['description']); // Get description if it was provided in URL (by the bookmarklet). [Bronco added that] | 1495 | $title = (empty($_GET['title']) ? '' : $_GET['title'] ); |
1495 | $tags = (empty($_GET['tags']) ? '' : $_GET['tags'] ); // Get tags if it was provided in URL | 1496 | // Get description if it was provided in URL (by the bookmarklet). [Bronco added that] |
1496 | $private = (!empty($_GET['private']) && $_GET['private'] === "1" ? 1 : 0); // Get private if it was provided in URL | 1497 | $description = (empty($_GET['description']) ? '' : $_GET['description']); |
1497 | if (($url!='') && parse_url($url,PHP_URL_SCHEME)=='') $url = 'http://'.$url; | 1498 | $tags = (empty($_GET['tags']) ? '' : $_GET['tags'] ); |
1499 | $private = (!empty($_GET['private']) && $_GET['private'] === "1" ? 1 : 0); | ||
1498 | // 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.) | 1500 | // 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.) |
1499 | if (empty($title) && parse_url($url,PHP_URL_SCHEME)=='http') | 1501 | if (empty($title) && $url->getScheme() == 'http') { |
1500 | { | ||
1501 | list($status,$headers,$data) = getHTTP($url,4); // Short timeout to keep the application responsive. | 1502 | list($status,$headers,$data) = getHTTP($url,4); // Short timeout to keep the application responsive. |
1502 | // FIXME: Decode charset according to specified in either 1) HTTP response headers or 2) <head> in html | 1503 | // FIXME: Decode charset according to specified in either 1) HTTP response headers or 2) <head> in html |
1503 | if (strpos($status,'200 OK')!==false) | 1504 | if (strpos($status,'200 OK')!==false) { |
1504 | { | 1505 | // Look for charset in html header. |
1505 | // Look for charset in html header. | 1506 | preg_match('#<meta .*charset=.*>#Usi', $data, $meta); |
1506 | preg_match('#<meta .*charset=.*>#Usi', $data, $meta); | 1507 | |
1507 | 1508 | // If found, extract encoding. | |
1508 | // If found, extract encoding. | 1509 | if (!empty($meta[0])) { |
1509 | if (!empty($meta[0])) | 1510 | // Get encoding specified in header. |
1510 | { | 1511 | preg_match('#charset="?(.*)"#si', $meta[0], $enc); |
1511 | // Get encoding specified in header. | 1512 | // If charset not found, use utf-8. |
1512 | preg_match('#charset="?(.*)"#si', $meta[0], $enc); | 1513 | $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8'; |
1513 | // If charset not found, use utf-8. | 1514 | } |
1514 | $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8'; | 1515 | else { |
1515 | } | 1516 | $html_charset = 'utf-8'; |
1516 | else { $html_charset = 'utf-8'; } | 1517 | } |
1517 | 1518 | ||
1518 | // Extract title | 1519 | // Extract title |
1519 | $title = html_extract_title($data); | 1520 | $title = html_extract_title($data); |
1520 | if (!empty($title)) | 1521 | if (!empty($title)) { |
1521 | { | 1522 | // Re-encode title in utf-8 if necessary. |
1522 | // Re-encode title in utf-8 if necessary. | 1523 | $title = ($html_charset == 'iso-8859-1') ? utf8_encode($title) : $title; |
1523 | $title = ($html_charset == 'iso-8859-1') ? utf8_encode($title) : $title; | 1524 | } |
1524 | } | 1525 | } |
1525 | } | ||
1526 | } | 1526 | } |
1527 | if ($url=='') // In case of empty URL, this is just a text (with a link that points to itself) | 1527 | if ($url == '') { |
1528 | { | 1528 | $url = '?' . smallHash($linkdate); |
1529 | $url='?'.smallHash($linkdate); | 1529 | $title = 'Note: '; |
1530 | $title='Note: '; | ||
1531 | } | 1530 | } |
1532 | $link = array('linkdate'=>$linkdate,'title'=>$title,'url'=>$url,'description'=>$description,'tags'=>$tags,'private'=>$private); | 1531 | $link = array( |
1532 | 'linkdate' => $linkdate, | ||
1533 | 'title' => $title, | ||
1534 | 'url' => (string)$url, | ||
1535 | 'description' => $description, | ||
1536 | 'tags' => $tags, | ||
1537 | 'private' => $private | ||
1538 | ); | ||
1533 | } | 1539 | } |
1534 | 1540 | ||
1535 | $PAGE = new pageBuilder; | 1541 | $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 | } |