]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Shaarli; | |
4 | ||
5 | use Shaarli\Config\ConfigManager; | |
6 | use WebThumbnailer\Application\ConfigManager as WTConfigManager; | |
7 | use WebThumbnailer\Exception\WebThumbnailerException; | |
8 | use WebThumbnailer\WebThumbnailer; | |
9 | ||
10 | /** | |
11 | * Class Thumbnailer | |
12 | * | |
13 | * Utility class used to retrieve thumbnails using web-thumbnailer dependency. | |
14 | */ | |
15 | class Thumbnailer | |
16 | { | |
17 | const COMMON_MEDIA_DOMAINS = [ | |
18 | 'imgur.com', | |
19 | 'flickr.com', | |
20 | 'youtube.com', | |
21 | 'wikimedia.org', | |
22 | 'redd.it', | |
23 | 'gfycat.com', | |
24 | 'media.giphy.com', | |
25 | 'twitter.com', | |
26 | 'twimg.com', | |
27 | 'instagram.com', | |
28 | 'pinterest.com', | |
29 | 'pinterest.fr', | |
30 | 'tumblr.com', | |
31 | 'deviantart.com', | |
32 | ]; | |
33 | ||
34 | const MODE_ALL = 'all'; | |
35 | const MODE_COMMON = 'common'; | |
36 | const MODE_NONE = 'none'; | |
37 | ||
38 | /** | |
39 | * @var WebThumbnailer instance. | |
40 | */ | |
41 | protected $wt; | |
42 | ||
43 | /** | |
44 | * @var ConfigManager instance. | |
45 | */ | |
46 | protected $conf; | |
47 | ||
48 | /** | |
49 | * Thumbnailer constructor. | |
50 | * | |
51 | * @param ConfigManager $conf instance. | |
52 | */ | |
53 | public function __construct($conf) | |
54 | { | |
55 | $this->conf = $conf; | |
56 | ||
57 | if (! $this->checkRequirements()) { | |
58 | $this->conf->set('thumbnails.mode', Thumbnailer::MODE_NONE); | |
59 | $this->conf->write(true); | |
60 | // TODO: create a proper error handling system able to catch exceptions... | |
61 | die(t( | |
62 | 'php-gd extension must be loaded to use thumbnails. ' | |
63 | .'Thumbnails are now disabled. Please reload the page.' | |
64 | )); | |
65 | } | |
66 | ||
67 | $this->wt = new WebThumbnailer(); | |
68 | WTConfigManager::addFile('inc/web-thumbnailer.json'); | |
69 | $this->wt->maxWidth($this->conf->get('thumbnails.width')) | |
70 | ->maxHeight($this->conf->get('thumbnails.height')) | |
71 | ->crop(true) | |
72 | ->debug($this->conf->get('dev.debug', false)); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Retrieve a thumbnail for given URL | |
77 | * | |
78 | * @param string $url where to look for a thumbnail. | |
79 | * | |
80 | * @return bool|string The thumbnail relative cache file path, or false if none has been found. | |
81 | */ | |
82 | public function get($url) | |
83 | { | |
84 | if ($this->conf->get('thumbnails.mode') === self::MODE_COMMON | |
85 | && ! $this->isCommonMediaOrImage($url) | |
86 | ) { | |
87 | return false; | |
88 | } | |
89 | ||
90 | try { | |
91 | return $this->wt->thumbnail($url); | |
92 | } catch (WebThumbnailerException $e) { | |
93 | // Exceptions are only thrown in debug mode. | |
94 | error_log(get_class($e) . ': ' . $e->getMessage()); | |
95 | } | |
96 | return false; | |
97 | } | |
98 | ||
99 | /** | |
100 | * We check weather the given URL is from a common media domain, | |
101 | * or if the file extension is an image. | |
102 | * | |
103 | * @param string $url to check | |
104 | * | |
105 | * @return bool true if it's an image or from a common media domain, false otherwise. | |
106 | */ | |
107 | public function isCommonMediaOrImage($url) | |
108 | { | |
109 | foreach (self::COMMON_MEDIA_DOMAINS as $domain) { | |
110 | if (strpos($url, $domain) !== false) { | |
111 | return true; | |
112 | } | |
113 | } | |
114 | ||
115 | if (endsWith($url, '.jpg') || endsWith($url, '.png') || endsWith($url, '.jpeg')) { | |
116 | return true; | |
117 | } | |
118 | ||
119 | return false; | |
120 | } | |
121 | ||
122 | /** | |
123 | * Make sure that requirements are match to use thumbnails: | |
124 | * - php-gd is loaded | |
125 | */ | |
126 | protected function checkRequirements() | |
127 | { | |
128 | return extension_loaded('gd'); | |
129 | } | |
130 | } |