]>
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 | 'soundcloud.com', | |
31 | 'tumblr.com', | |
32 | 'deviantart.com', | |
33 | ]; | |
34 | ||
35 | const MODE_ALL = 'all'; | |
36 | const MODE_COMMON = 'common'; | |
37 | const MODE_NONE = 'none'; | |
38 | ||
39 | /** | |
40 | * @var WebThumbnailer instance. | |
41 | */ | |
42 | protected $wt; | |
43 | ||
44 | /** | |
45 | * @var ConfigManager instance. | |
46 | */ | |
47 | protected $conf; | |
48 | ||
49 | /** | |
50 | * Thumbnailer constructor. | |
51 | * | |
52 | * @param ConfigManager $conf instance. | |
53 | */ | |
54 | public function __construct($conf) | |
55 | { | |
56 | $this->conf = $conf; | |
57 | ||
58 | if (! $this->checkRequirements()) { | |
59 | $this->conf->set('thumbnails.mode', Thumbnailer::MODE_NONE); | |
60 | $this->conf->write(true); | |
61 | // TODO: create a proper error handling system able to catch exceptions... | |
62 | die(t( | |
63 | 'php-gd extension must be loaded to use thumbnails. ' | |
64 | .'Thumbnails are now disabled. Please reload the page.' | |
65 | )); | |
66 | } | |
67 | ||
68 | $this->wt = new WebThumbnailer(); | |
69 | WTConfigManager::addFile('inc/web-thumbnailer.json'); | |
70 | $this->wt->maxWidth($this->conf->get('thumbnails.width')) | |
71 | ->maxHeight($this->conf->get('thumbnails.height')) | |
72 | ->crop(true) | |
73 | ->debug($this->conf->get('dev.debug', false)); | |
74 | } | |
75 | ||
76 | /** | |
77 | * Retrieve a thumbnail for given URL | |
78 | * | |
79 | * @param string $url where to look for a thumbnail. | |
80 | * | |
81 | * @return bool|string The thumbnail relative cache file path, or false if none has been found. | |
82 | */ | |
83 | public function get($url) | |
84 | { | |
85 | if ($this->conf->get('thumbnails.mode') === self::MODE_COMMON | |
86 | && ! $this->isCommonMediaOrImage($url) | |
87 | ) { | |
88 | return false; | |
89 | } | |
90 | ||
91 | try { | |
92 | return $this->wt->thumbnail($url); | |
93 | } catch (WebThumbnailerException $e) { | |
94 | // Exceptions are only thrown in debug mode. | |
95 | error_log(get_class($e) . ': ' . $e->getMessage()); | |
96 | } | |
97 | return false; | |
98 | } | |
99 | ||
100 | /** | |
101 | * We check weather the given URL is from a common media domain, | |
102 | * or if the file extension is an image. | |
103 | * | |
104 | * @param string $url to check | |
105 | * | |
106 | * @return bool true if it's an image or from a common media domain, false otherwise. | |
107 | */ | |
108 | public function isCommonMediaOrImage($url) | |
109 | { | |
110 | foreach (self::COMMON_MEDIA_DOMAINS as $domain) { | |
111 | if (strpos($url, $domain) !== false) { | |
112 | return true; | |
113 | } | |
114 | } | |
115 | ||
116 | if (endsWith($url, '.jpg') || endsWith($url, '.png') || endsWith($url, '.jpeg')) { | |
117 | return true; | |
118 | } | |
119 | ||
120 | return false; | |
121 | } | |
122 | ||
123 | /** | |
124 | * Make sure that requirements are match to use thumbnails: | |
125 | * - php-gd is loaded | |
126 | */ | |
127 | protected function checkRequirements() | |
128 | { | |
129 | return extension_loaded('gd'); | |
130 | } | |
131 | } |