From b302b3c584b84f22f0e6f187b072180ecbacdfab Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 5 Jul 2018 20:29:55 +0200 Subject: [PATCH] Thumbnails: add a common mode to only retrieve thumbs from popular media websites --- application/PageBuilder.php | 6 ++- application/Thumbnailer.php | 54 +++++++++++++++++++++++++- application/Updater.php | 5 ++- index.php | 22 ++++++----- tests/ThumbnailerTest.php | 43 ++++++++++++++++---- tests/Updater/UpdaterTest.php | 7 ++-- tests/utils/config/configJson.json.php | 2 +- tpl/default/configure.html | 13 ++++++- tpl/vintage/configure.html | 13 ++++++- 9 files changed, 135 insertions(+), 30 deletions(-) diff --git a/application/PageBuilder.php b/application/PageBuilder.php index 5da70811..b1abe0d0 100644 --- a/application/PageBuilder.php +++ b/application/PageBuilder.php @@ -1,6 +1,7 @@ tpl->assign('tags', $this->linkDB->linksCountPerTag()); } - $this->tpl->assign('thumbnails_enabled', $this->conf->get('thumbnails.enabled')); + $this->tpl->assign( + 'thumbnails_enabled', + $this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE + ); $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width')); $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height')); diff --git a/application/Thumbnailer.php b/application/Thumbnailer.php index d2284e79..7d0d9c33 100644 --- a/application/Thumbnailer.php +++ b/application/Thumbnailer.php @@ -14,6 +14,27 @@ use WebThumbnailer\Application\ConfigManager as WTConfigManager; */ class Thumbnailer { + const COMMON_MEDIA_DOMAINS = [ + 'imgur.com', + 'flickr.com', + 'youtube.com', + 'wikimedia.org', + 'redd.it', + 'gfycat.com', + 'media.giphy.com', + 'twitter.com', + 'twimg.com', + 'instagram.com', + 'pinterest.com', + 'pinterest.fr', + 'tumblr.com', + 'deviantart.com', + ]; + + const MODE_ALL = 'all'; + const MODE_COMMON = 'common'; + const MODE_NONE = 'none'; + /** * @var WebThumbnailer instance. */ @@ -57,13 +78,42 @@ class Thumbnailer */ public function get($url) { + if ($this->conf->get('thumbnails.mode') === self::MODE_COMMON + && ! $this->isCommonMediaOrImage($url) + ) { + return false; + } + try { return $this->wt->thumbnail($url); } catch (WebThumbnailerException $e) { // Exceptions are only thrown in debug mode. - error_log(get_class($e) .': '. $e->getMessage()); - return false; + error_log(get_class($e) . ': ' . $e->getMessage()); } + return false; + } + + /** + * We check weather the given URL is from a common media domain, + * or if the file extension is an image. + * + * @param string $url to check + * + * @return bool true if it's an image or from a common media domain, false otherwise. + */ + public function isCommonMediaOrImage($url) + { + foreach (self::COMMON_MEDIA_DOMAINS as $domain) { + if (strpos($url, $domain) !== false) { + return true; + } + } + + if (endsWith($url, '.jpg') || endsWith($url, '.png') || endsWith($url, '.jpeg')) { + return true; + } + + return false; } /** diff --git a/application/Updater.php b/application/Updater.php index f6b9e205..2a4c807c 100644 --- a/application/Updater.php +++ b/application/Updater.php @@ -2,6 +2,7 @@ use Shaarli\Config\ConfigJson; use Shaarli\Config\ConfigPhp; use Shaarli\Config\ConfigManager; +use Shaarli\Thumbnailer; /** * Class Updater. @@ -497,12 +498,12 @@ class Updater */ public function updateMethodWebThumbnailer() { - if ($this->conf->exists('thumbnails.enabled')) { + if ($this->conf->exists('thumbnails.mode')) { return true; } $thumbnailsEnabled = $this->conf->get('thumbnail.enable_thumbnails', true); - $this->conf->set('thumbnails.enabled', $thumbnailsEnabled); + $this->conf->set('thumbnails.mode', $thumbnailsEnabled ? Thumbnailer::MODE_ALL : Thumbnailer::MODE_NONE); $this->conf->set('thumbnails.width', 125); $this->conf->set('thumbnails.height', 90); $this->conf->remove('thumbnail'); diff --git a/index.php b/index.php index d5a3e93d..299d6d9b 100644 --- a/index.php +++ b/index.php @@ -603,7 +603,9 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, // -------- Picture wall if ($targetPage == Router::$PAGE_PICWALL) { - if (! $conf->get('thumbnails.enabled')) { + $PAGE->assign('pagetitle', t('Picture wall') .' - '. $conf->get('general.title', 'Shaarli')); + if (! $conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) === Thumbnailer::MODE_NONE) { + $PAGE->assign('linksToDisplay', []); $PAGE->renderPage('picwall'); exit; } @@ -630,7 +632,6 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, $PAGE->assign($key, $value); } - $PAGE->assign('pagetitle', t('Picture wall') .' - '. $conf->get('general.title', 'Shaarli')); $PAGE->renderPage('picwall'); exit; } @@ -1013,14 +1014,13 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, $conf->set('api.secret', escape($_POST['apiSecret'])); $conf->set('translation.language', escape($_POST['language'])); - $thumbnailsEnabled = extension_loaded('gd') && !empty($_POST['enableThumbnails']); - $conf->set('thumbnails.enabled', $thumbnailsEnabled); - - if (! $conf->get('thumbnails.enabled') && $thumbnailsEnabled) { + $thumbnailsMode = extension_loaded('gd') ? $_POST['enableThumbnails'] : Thumbnailer::MODE_NONE; + if ($conf->get('thumbnails.enabled', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE) { $_SESSION['warnings'][] = t( - 'You have enabled thumbnails. Please synchonize them.' + 'You have enabled or changed thumbnails mode. Please synchonize them.' ); } + $conf->set('thumbnails.mode', $thumbnailsMode); try { $conf->write($loginManager->isLoggedIn()); @@ -1061,6 +1061,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, $PAGE->assign('languages', Languages::getAvailableLanguages()); $PAGE->assign('language', $conf->get('translation.language')); $PAGE->assign('gd_enabled', extension_loaded('gd')); + $PAGE->assign('thumbnails_mode', $conf->get('thumbnails.mode', Thumbnailer::MODE_NONE)); $PAGE->assign('pagetitle', t('Configure') .' - '. $conf->get('general.title', 'Shaarli')); $PAGE->renderPage('configure'); exit; @@ -1162,7 +1163,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, $link['title'] = $link['url']; } - if ($conf->get('thumbnails.enabled')) { + if ($conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE) { $thumbnailer = new Thumbnailer($conf); $link['thumbnail'] = $thumbnailer->get($url); } @@ -1606,7 +1607,8 @@ function buildLinkList($PAGE, $LINKSDB, $conf, $pluginManager, $loginManager) $i = ($page-1) * $_SESSION['LINKS_PER_PAGE']; $end = $i + $_SESSION['LINKS_PER_PAGE']; - if ($conf->get('thumbnails.enabled')) { + $thumbnailsEnabled = $conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE; + if ($thumbnailsEnabled) { $thumbnailer = new Thumbnailer($conf); } @@ -1633,7 +1635,7 @@ function buildLinkList($PAGE, $LINKSDB, $conf, $pluginManager, $loginManager) // Thumbnails enabled, not a note, // and (never retrieved yet or no valid cache file) - if ($conf->get('thumbnails.enabled') && $link['url'][0] != '?' + if ($thumbnailsEnabled && $link['url'][0] != '?' && (! isset($link['thumbnail']) || ($link['thumbnail'] !== false && ! is_file($link['thumbnail']))) ) { $elem = $LINKSDB[$keys[$i]]; diff --git a/tests/ThumbnailerTest.php b/tests/ThumbnailerTest.php index c04b8fb5..08311545 100644 --- a/tests/ThumbnailerTest.php +++ b/tests/ThumbnailerTest.php @@ -25,14 +25,20 @@ class ThumbnailerTest extends TestCase */ protected $thumbnailer; + /** + * @var ConfigManager + */ + protected $conf; + public function setUp() { - $conf = new ConfigManager('tests/utils/config/configJson'); - $conf->set('thumbnails.width', self::WIDTH); - $conf->set('thumbnails.height', self::HEIGHT); - $conf->set('dev.debug', true); + $this->conf = new ConfigManager('tests/utils/config/configJson'); + $this->conf->set('thumbnails.mode', Thumbnailer::MODE_ALL); + $this->conf->set('thumbnails.width', self::WIDTH); + $this->conf->set('thumbnails.height', self::HEIGHT); + $this->conf->set('dev.debug', true); - $this->thumbnailer = new Thumbnailer($conf); + $this->thumbnailer = new Thumbnailer($this->conf); // cache files in the sandbox WTConfigManager::addFile('tests/utils/config/wt.json'); } @@ -43,9 +49,9 @@ class ThumbnailerTest extends TestCase } /** - * Test a thumbnail with a custom size. + * Test a thumbnail with a custom size in 'all' mode. */ - public function testThumbnailValid() + public function testThumbnailAllValid() { $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/'); $this->assertNotFalse($thumb); @@ -54,6 +60,29 @@ class ThumbnailerTest extends TestCase $this->assertEquals(self::HEIGHT, imagesy($image)); } + /** + * Test a thumbnail with a custom size in 'common' mode. + */ + public function testThumbnailCommonValid() + { + $this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON); + $thumb = $this->thumbnailer->get('https://imgur.com/jlFgGpe'); + $this->assertNotFalse($thumb); + $image = imagecreatefromstring(file_get_contents($thumb)); + $this->assertEquals(self::WIDTH, imagesx($image)); + $this->assertEquals(self::HEIGHT, imagesy($image)); + } + + /** + * Test a thumbnail in 'common' mode which isn't include in common websites. + */ + public function testThumbnailCommonInvalid() + { + $this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON); + $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/'); + $this->assertFalse($thumb); + } + /** * Test a thumbnail that can't be retrieved. */ diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index 92ff5690..05f4b8e1 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -2,6 +2,7 @@ use Shaarli\Config\ConfigJson; use Shaarli\Config\ConfigManager; use Shaarli\Config\ConfigPhp; +use Shaarli\Thumbnailer; require_once 'tests/Updater/DummyUpdater.php'; require_once 'inc/rain.tpl.class.php'; @@ -696,7 +697,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $updater = new Updater([], [], $this->conf, true, $_SESSION); $this->assertTrue($updater->updateMethodWebThumbnailer()); $this->assertFalse($this->conf->exists('thumbnail')); - $this->assertTrue($this->conf->get('thumbnails.enabled')); + $this->assertEquals(\Shaarli\Thumbnailer::MODE_ALL, $this->conf->get('thumbnails.mode')); $this->assertEquals(125, $this->conf->get('thumbnails.width')); $this->assertEquals(90, $this->conf->get('thumbnails.height')); $this->assertContains('You have enabled thumbnails', $_SESSION['warnings'][0]); @@ -712,7 +713,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $updater = new Updater([], [], $this->conf, true, $_SESSION); $this->assertTrue($updater->updateMethodWebThumbnailer()); $this->assertFalse($this->conf->exists('thumbnail')); - $this->assertFalse($this->conf->get('thumbnails.enabled')); + $this->assertEquals(Thumbnailer::MODE_NONE, $this->conf->get('thumbnails.mode')); $this->assertEquals(125, $this->conf->get('thumbnails.width')); $this->assertEquals(90, $this->conf->get('thumbnails.height')); $this->assertTrue(empty($_SESSION['warnings'])); @@ -726,7 +727,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $updater = new Updater([], [], $this->conf, true, $_SESSION); $this->assertTrue($updater->updateMethodWebThumbnailer()); $this->assertFalse($this->conf->exists('thumbnail')); - $this->assertTrue($this->conf->get('thumbnails.enabled')); + $this->assertEquals(Thumbnailer::MODE_COMMON, $this->conf->get('thumbnails.mode')); $this->assertEquals(90, $this->conf->get('thumbnails.width')); $this->assertEquals(53, $this->conf->get('thumbnails.height')); $this->assertTrue(empty($_SESSION['warnings'])); diff --git a/tests/utils/config/configJson.json.php b/tests/utils/config/configJson.json.php index a656b67c..1549ddfc 100644 --- a/tests/utils/config/configJson.json.php +++ b/tests/utils/config/configJson.json.php @@ -76,7 +76,7 @@ "extensions": [] }, "thumbnails": { - "enabled": true, + "mode": "common", "width": 90, "height": 53 } diff --git a/tpl/default/configure.html b/tpl/default/configure.html index dca9503b..9711d151 100644 --- a/tpl/default/configure.html +++ b/tpl/default/configure.html @@ -259,8 +259,17 @@
- +
diff --git a/tpl/vintage/configure.html b/tpl/vintage/configure.html index fc3a563b..9466c235 100644 --- a/tpl/vintage/configure.html +++ b/tpl/vintage/configure.html @@ -131,8 +131,17 @@ Enable thumbnails - +