Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / imagemagick / src / ImagemagickFormatMapper.php
1 <?php
2
3 namespace Drupal\imagemagick;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Cache\Cache;
7 use Drupal\Core\Cache\CacheBackendInterface;
8 use Drupal\Core\Config\ConfigFactoryInterface;
9 use Drupal\Core\Config\Schema\SchemaCheckTrait;
10 use Drupal\Core\Config\TypedConfigManagerInterface;
11 use Drupal\Core\StringTranslation\StringTranslationTrait;
12
13 /**
14  * Provides the ImageMagick format mapper.
15  */
16 class ImagemagickFormatMapper implements ImagemagickFormatMapperInterface {
17
18   use SchemaCheckTrait;
19   use StringTranslationTrait;
20
21   /**
22    * The cache service.
23    *
24    * @var \Drupal\Core\Cache\CacheBackendInterface
25    */
26   protected $cache;
27
28   /**
29    * The MIME type guessing service.
30    *
31    * @var \Drupal\imagemagick\MimeTypeMapper
32    */
33   protected $mimeTypeMapper;
34
35   /**
36    * The config factory service.
37    *
38    * @var \Drupal\Core\Config\ConfigFactoryInterface
39    */
40   protected $configFactory;
41
42   /**
43    * The typed config service.
44    *
45    * @var \Drupal\Core\Config\TypedConfigManagerInterface
46    */
47   protected $typedConfig;
48
49   /**
50    * Constructs an ImagemagickFormatMapper object.
51    *
52    * @param \Drupal\Core\Cache\CacheBackendInterface $cache_service
53    *   The cache service.
54    * @param \Drupal\imagemagick\ImagemagickMimeTypeMapper $mime_type_mapper
55    *   The MIME type mapping service.
56    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
57    *   The config factory.
58    * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
59    *   The typed config service.
60    */
61   public function __construct(CacheBackendInterface $cache_service, ImagemagickMimeTypeMapper $mime_type_mapper, ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typed_config) {
62     $this->cache = $cache_service;
63     $this->mimeTypeMapper = $mime_type_mapper;
64     $this->configFactory = $config_factory;
65     $this->typedConfig = $typed_config;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function validateMap(array $map) {
72     $errors = [];
73
74     // Get current config object and change the format map.
75     $data = $this->configFactory->get('imagemagick.settings')->get();
76     $data['image_formats'] = $map;
77
78     // Validates against schema.
79     $schema_errors = $this->checkConfigSchema($this->typedConfig, 'imagemagick.settings', $data);
80     if ($schema_errors !== TRUE) {
81       foreach ($schema_errors as $key => $value) {
82         list(, $path) = explode(':', $key);
83         $components = explode('.', $path);
84         if ($components[0] === 'image_formats') {
85           if (isset($components[2])) {
86             $errors[$components[1]]['variables'][$components[2]][] = $value;
87           }
88           else {
89             $errors[$components[1]]['format'][] = $value;
90           }
91         }
92       }
93     }
94
95     // Other checks.
96     foreach ($map as $key => $value) {
97       if (Unicode::strtoupper($key) != $key) {
98         // Formats must be typed in uppercase.
99         $errors[$key]['format'][] = $this->t("The format (@key) must be entered in all uppercase characters.", ['@key' => $key])->render();
100       }
101       if (!isset($value['mime_type'])) {
102         // Formats must have a MIME type mapped.
103         $errors[$key]['format'][] = $this->t("Missing mime_type variable.")->render();
104       }
105       elseif (!in_array($value['mime_type'], $this->mimeTypeMapper->getMimeTypes())) {
106         // MIME type must exist.
107         $errors[$key]['variables']['mime_type'][] = $this->t("MIME type (@mime_type) not found.", ['@mime_type' => $value['mime_type']])->render();
108       }
109     }
110
111     return $errors;
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   public function isFormatEnabled($format) {
118     $format = Unicode::strtoupper($format);
119     return $format ? isset($this->resolveEnabledFormats()[$format]) : FALSE;
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   public function getMimeTypeFromFormat($format) {
126     $format = Unicode::strtoupper($format);
127     if ($this->isFormatEnabled($format)) {
128       return $this->resolveEnabledFormats()[$format];
129     }
130     return NULL;
131   }
132
133   /**
134    * {@inheritdoc}
135    */
136   public function getFormatFromExtension($extension) {
137     $extension = Unicode::strtolower($extension);
138     $enabled_extensions = $this->resolveEnabledExtensions();
139     return $extension ? (isset($enabled_extensions[$extension]) ? $enabled_extensions[$extension] : NULL) : NULL;
140   }
141
142   /**
143    * {@inheritdoc}
144    */
145   public function getEnabledFormats() {
146     return array_keys($this->resolveEnabledFormats());
147   }
148
149   /**
150    * {@inheritdoc}
151    */
152   public function getEnabledExtensions() {
153     return array_keys($this->resolveEnabledExtensions());
154   }
155
156   /**
157    * Returns the enabled image formats, processing the config map.
158    *
159    * Results are cached for subsequent access. Saving the config will
160    * invalidate the cache.
161    *
162    * @return array
163    *   An associative array with ImageMagick formats as keys and their MIME
164    *   type as values.
165    */
166   protected function resolveEnabledFormats() {
167     if ($cache = $this->cache->get("imagemagick:enabled_formats")) {
168       $enabled_image_formats = $cache->data;
169     }
170     else {
171       $config = $this->configFactory->get('imagemagick.settings');
172       $image_formats = $config->get('image_formats');
173       $enabled_image_formats = [];
174       foreach ($image_formats as $format => $data) {
175         if (!isset($data['enabled']) || (isset($data['enabled']) && $data['enabled'])) {
176           if (isset($data['mime_type']) && in_array($data['mime_type'], $this->mimeTypeMapper->getMimeTypes())) {
177             $enabled_image_formats[$format] = $data['mime_type'];
178           }
179         }
180       }
181       ksort($enabled_image_formats);
182       $this->cache->set("imagemagick:enabled_formats", $enabled_image_formats, Cache::PERMANENT, $config->getCacheTags());
183     }
184     return $enabled_image_formats;
185   }
186
187   /**
188    * Returns the enabled image file extensions, processing the config map.
189    *
190    * Results are cached for subsequent access. Saving the config will
191    * invalidate the cache.
192    *
193    * @return array
194    *   An associative array with file extensions as keys and their ImageMagick
195    *   format as values.
196    */
197   protected function resolveEnabledExtensions() {
198     if ($cache = $this->cache->get("imagemagick:enabled_extensions")) {
199       $extensions = $cache->data;
200     }
201     else {
202       // Get configured image formats.
203       $image_formats = $this->configFactory->get('imagemagick.settings')->get('image_formats');
204
205       // Get only enabled formats.
206       $enabled_image_formats = array_keys($this->resolveEnabledFormats());
207
208       // Apply defaults.
209       foreach ($enabled_image_formats as $format) {
210         if (isset($image_formats[$format]) && is_array($image_formats[$format])) {
211           $image_formats[$format] += [
212             'mime_type' => NULL,
213             'weight' => 0,
214             'exclude_extensions' => NULL,
215           ];
216         }
217       }
218
219       // Scans the enabled formats to determine enabled file extensions and
220       // their mapping to the internal Image/GraphicsMagick format.
221       $extensions = [];
222       $excluded_extensions = [];
223       foreach ($enabled_image_formats as $format) {
224         $format_extensions = $this->mimeTypeMapper->getExtensionsForMimeType($image_formats[$format]['mime_type']);
225         $weight_checked_extensions = [];
226         foreach ($format_extensions as $ext) {
227           if (!isset($extensions[$ext])) {
228             $weight_checked_extensions[$ext] = $format;
229           }
230           else {
231             // Extension is already present in the array, lower weight format
232             // prevails.
233             if ($image_formats[$format]['weight'] < $image_formats[$extensions[$ext]]['weight']) {
234               $weight_checked_extensions[$ext] = $format;
235             }
236           }
237         }
238         $extensions = array_merge($extensions, $weight_checked_extensions);
239         // Accumulate excluded extensions.
240         if ($image_formats[$format]['exclude_extensions']) {
241           $exclude_extensions_string = Unicode::strtolower(preg_replace('/\s+/', '', $image_formats[$format]['exclude_extensions']));
242           $excluded_extensions = array_merge($excluded_extensions, array_intersect($format_extensions, explode(',', $exclude_extensions_string)));
243         }
244       }
245
246       // Remove the excluded extensions.
247       $excluded_extensions = array_unique($excluded_extensions);
248       $excluded_extensions = array_combine($excluded_extensions, $excluded_extensions);
249       $extensions = array_diff_key($extensions, $excluded_extensions);
250
251       ksort($extensions);
252       $this->cache->set("imagemagick:enabled_extensions", $extensions, Cache::PERMANENT, $this->configFactory->get('imagemagick.settings')->getCacheTags());
253     }
254
255     return $extensions;
256   }
257
258 }