1
0
mirror of https://github.com/owncloud/bookmarks.git synced 2025-02-26 23:54:25 +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; namespace OCA\Bookmarks\Controller\Lib;
use GuzzleHttp\Exception\ClientException;
use \OCP\IDb; use \OCP\IDb;
class Bookmarks { class Bookmarks {
@ -560,7 +561,7 @@ class Bookmarks {
* @brief Load Url and receive Metadata (Title) * @brief Load Url and receive Metadata (Title)
* @param string $url Url to load and analyze * @param string $url Url to load and analyze
* @return array Metadata for url; * @return array Metadata for url;
* @throws \Exception * @throws \Exception|ClientException
*/ */
public static function getURLMetadata($url) { public static function getURLMetadata($url) {
@ -572,6 +573,12 @@ class Bookmarks {
$request = \OC::$server->getHTTPClientService()->newClient()->get($url); $request = \OC::$server->getHTTPClientService()->newClient()->get($url);
$page = $request->getBody(); $page = $request->getBody();
$contentType = $request->getHeader('Content-Type'); $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) { } catch (\Exception $e) {
throw $e; throw $e;
} }

View File

@ -96,6 +96,7 @@ class BookmarkController extends ApiController {
if ($from_own == 0) { if ($from_own == 0) {
// allow only http(s) and (s)ftp // allow only http(s) and (s)ftp
$protocols = '/^(https?|s?ftp)\:\/\//i'; $protocols = '/^(https?|s?ftp)\:\/\//i';
try {
if (preg_match($protocols, $url)) { if (preg_match($protocols, $url)) {
$data = Bookmarks::getURLMetadata($url); $data = Bookmarks::getURLMetadata($url);
// if not (allowed) protocol is given, assume http and https (and fetch both) // if not (allowed) protocol is given, assume http and https (and fetch both)
@ -107,6 +108,9 @@ class BookmarkController extends ApiController {
$url_http = 'http://' . $url; $url_http = 'http://' . $url;
$data_http = Bookmarks::getURLMetadata($url_http); $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 if ($title === '' && isset($data['title'])) { // prefer original url if working
$title = $data['title']; $title = $data['title'];

View File

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