1
0
mirror of https://github.com/owncloud/bookmarks.git synced 2024-11-28 04:24:09 +01:00

accept pages that return 401-403, show error message on other error codes

This commit is contained in:
Arthur Schiwon 2016-08-25 00:31:42 +02:00
parent 40f5f9836d
commit 32b2bae6ef
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
3 changed files with 25 additions and 11 deletions

View File

@ -26,6 +26,7 @@
namespace OCA\Bookmarks\Controller\Lib;
use GuzzleHttp\Exception\ClientException;
use \OCP\IDb;
class Bookmarks {
@ -560,7 +561,7 @@ class Bookmarks {
* @brief Load Url and receive Metadata (Title)
* @param string $url Url to load and analyze
* @return array Metadata for url;
* @throws \Exception
* @throws \Exception|ClientException
*/
public static function getURLMetadata($url) {
@ -572,6 +573,12 @@ class Bookmarks {
$request = \OC::$server->getHTTPClientService()->newClient()->get($url);
$page = $request->getBody();
$contentType = $request->getHeader('Content-Type');
} catch (ClientException $e) {
$errorCode = $e->getCode();
if(!($errorCode >= 401 && $errorCode <= 403)) {
// whitelist Unauthorized, Forbidden and Paid pages
throw $e;
}
} catch (\Exception $e) {
throw $e;
}

View File

@ -96,16 +96,20 @@ class BookmarkController extends ApiController {
if ($from_own == 0) {
// allow only http(s) and (s)ftp
$protocols = '/^(https?|s?ftp)\:\/\//i';
if (preg_match($protocols, $url)) {
$data = Bookmarks::getURLMetadata($url);
// if not (allowed) protocol is given, assume http and https (and fetch both)
} else {
// append https to url and fetch it
$url_https = 'https://' . $url;
$data_https = Bookmarks::getURLMetadata($url_https);
// append http to url and fetch it
$url_http = 'http://' . $url;
$data_http = Bookmarks::getURLMetadata($url_http);
try {
if (preg_match($protocols, $url)) {
$data = Bookmarks::getURLMetadata($url);
// if not (allowed) protocol is given, assume http and https (and fetch both)
} else {
// append https to url and fetch it
$url_https = 'https://' . $url;
$data_https = Bookmarks::getURLMetadata($url_https);
// append http to url and fetch it
$url_http = 'http://' . $url;
$data_http = Bookmarks::getURLMetadata($url_http);
}
} catch (\Exception $e) {
return new JSONResponse(array('status' => 'error'), Http::STATUS_BAD_REQUEST);
}
if ($title === '' && isset($data['title'])) { // prefer original url if working

View File

@ -221,6 +221,9 @@ function addBookmark(event) {
checkEmpty();
watchUrlField();
}
},
error: function () {
OC.Notification.showTemporary(t('bookmarks', 'Could not add bookmark.'));
}
});
}