PluginProbe
AliNext – WooCommerce Dropshipping Plugin for AliExpress / trunk
AliNext – WooCommerce Dropshipping Plugin for AliExpress vtrunk
ali2woo-lite / includes / classes / model / Attachment.php

Attachment.php in AliNext – WooCommerce Dropshipping Plugin for AliExpress trunk, at includes/classes/model/Attachment.php

661 lines 30.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Description of Attachment
5 *
6 * @author Ali2Woo Team
7 */
8
9 // phpcs:ignoreFile WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
10
11 namespace AliNext_Lite;;
12
13 use DOMDocument;
14 use Exception;
15 use function WP_Error;
16
17 class Attachment
18 {
19
20 public const KEY_ATTACHED_FILE = '_wp_a2w_attached_file';
21
22 /**
23 * External image host patterns detected by the "load external images locally" feature.
24 *
25 * @var string[]
26 */
27 public const EXTERNAL_IMAGE_DOMAINS = [
28 '.alicdn.com',
29 '.aliexpress-media.com',
30 ];
31
32 private $utils;
33 private $use_external_image_urls = false;
34
35 public function __construct($use_external = 'cfg')
36 {
37 $this->utils = new Utils();
38 if ('external' === $use_external || 'local' === $use_external) {
39 $this->use_external_image_urls = ('external' === $use_external);
40 } else {
41 $this->use_external_image_urls = get_setting('use_external_image_urls');
42 }
43 }
44
45 /**
46 * @return string[]
47 */
48 private static function externalImageLikePatterns(): array
49 {
50 return array_map(static function (string $domain): string {
51 return '%' . $domain . '%';
52 }, self::EXTERNAL_IMAGE_DOMAINS);
53 }
54
55 private static function isExternalImageUrl(string $url): bool
56 {
57 foreach (self::EXTERNAL_IMAGE_DOMAINS as $domain) {
58 if (str_contains($url, $domain)) {
59 return true;
60 }
61 }
62
63 return false;
64 }
65
66 public function create_attachment($post_id, $image_path, $params = [])
67 {
68 global $wpdb;
69
70 require_once ABSPATH . 'wp-admin/includes/file.php';
71 require_once ABSPATH . 'wp-admin/includes/media.php';
72 require_once ABSPATH . 'wp-admin/includes/image.php';
73
74 $title = $params['title'] ?? "";
75 $alt = $params['alt'] ?? "";
76 $check_duplicate = !isset($params['check_duplicate']) || $params['check_duplicate'];
77 $edit_images = $params['edit_images'] ?? [];
78 $use_external_image_urls = $params['use_external_image_urls'] ?? $this->use_external_image_urls;
79
80 $image_path = Utils::clear_image_url($image_path);
81 // remove _640x640.jpg from image url filename.jpg_640x640.jpg
82 //$image_path = preg_replace("/(.+)(.jpg)(_[0-9]+x[0-9]+.jpg)/", "$1$2", $image_path);
83 // $image_path = preg_replace("/(.+?)(.jpg|.jpeg)(.*)/", "$1$2", $image_path);
84 $image_id = Utils::buildImageIdFromPath($image_path);
85 $image_name = preg_replace('/\.[^.]+$/', '', basename($image_path));
86
87 if (!empty($edit_images[$image_id])) {
88 $attachment_id = $edit_images[$image_id]['attachment_id'];
89
90 wp_update_post([
91 'ID' => $attachment_id,
92 'post_title' => empty($title) ? $image_name : $title,
93 'post_excerpt' => empty($title) ? $image_name : $title,
94 'meta_input' => ['_a2w_external_image_url' => $edit_images[$image_id]['external_image_url']],
95 ]);
96
97 if (!empty($alt)) {
98 update_post_meta($attachment_id, '_wp_attachment_image_alt', $alt);
99 }
100
101 return $attachment_id;
102 }
103
104 if ($check_duplicate) {
105 if ($use_external_image_urls) {
106 $query = "SELECT ID FROM $wpdb->posts p INNER JOIN $wpdb->postmeta pm ON (p.ID = pm.post_id) " .
107 "WHERE p.post_parent=%d AND pm.meta_key='_a2w_external_image_url' AND pm.meta_value=%s " .
108 "LIMIT 1";
109 $old_attachment_id = $wpdb->get_var(
110 $wpdb->prepare($query, $post_id, $image_path)
111 );
112 } else {
113 $query = "SELECT ID FROM $wpdb->posts p INNER JOIN $wpdb->postmeta pm ON (p.ID = pm.post_id) " .
114 "LEFT JOIN $wpdb->postmeta pm1 ON (p.ID = pm1.post_id AND pm1.meta_key=%s AND pm1.meta_value='1') " .
115 "WHERE p.post_parent=%d AND pm.meta_key='_a2w_external_image_url' " .
116 "AND pm.meta_value=%s AND pm1.meta_id is null LIMIT 1";
117
118 $old_attachment_id = $wpdb->get_var(
119 $wpdb->prepare($query, self::KEY_ATTACHED_FILE, $post_id, $image_path)
120 );
121 }
122
123 if ($old_attachment_id) {
124 return $old_attachment_id;
125 }
126 }
127
128 if ($use_external_image_urls) {
129 // attach image as remote url
130 if (empty($post_id) || empty($image_path)) {
131 return false;
132 }
133
134 // remove _640x640.jpg from image url filename.jpg_640x640.jpg
135 //$image_path = preg_replace("/(.+)(.jpg)(_[0-9]+x[0-9]+.jpg)/", "$1$2", $image_path);
136 //$image_path = preg_replace("/(.+?)(.jpg|.jpeg)(.*)/", "$1$2", $image_path);
137
138 $wp_filetype = wp_check_filetype(basename($image_path), null);
139
140 //$image_name = preg_replace('/\.[^.]+$/', '', basename($image_path));
141 $image_name = sanitize_file_name($image_name);
142 $image_name = preg_replace("/[^a-zA-Z0-9-]/", "", $image_name);
143 $image_name = substr($image_name, 0, 200);
144
145 $attachment = array(
146 'guid' => $image_path,
147 'post_mime_type' => $wp_filetype['type'],
148 'post_title' => empty($title) ? $image_name : $title,
149 'post_excerpt' => empty($title) ? $image_name : $title,
150 'post_content' => '',
151 'post_status' => 'inherit',
152 );
153 $attach_id = wp_insert_attachment($attachment, $image_path, $post_id);
154
155 if (!$attach_id) {
156 return false;
157 }
158
159 $this->set_attachment_metadata($attach_id, $image_path);
160
161 update_post_meta($attach_id, '_a2w_external_image_url', $image_path);
162
163 if (!empty($alt)) {
164 update_post_meta($attach_id, '_wp_attachment_image_alt', $alt);
165 }
166
167 return $attach_id;
168 } else {
169 // attach image as upload
170 $image = $this->download_url($image_path);
171 if ($image) {
172 $file_array = [
173 'name' => basename($image),
174 'size' => filesize($image),
175 'tmp_name' => $image,
176 ];
177
178 if (isset($params['inner_post_id'])) {
179 $file_array['inner_post_id'] = $params['inner_post_id'];
180 }
181
182 if (isset($params['inner_attach_type'])) {
183 $file_array['inner_attach_type'] = $params['inner_attach_type'];
184 }
185
186 $attach_id = media_handle_sideload($file_array, $post_id, $title);
187
188 if ($attach_id) {
189 update_post_meta($attach_id, '_a2w_external_image_url', $image_path);
190 if (!empty($title)) {
191 wp_update_post([
192 'ID' => $attach_id,
193 'post_excerpt' => $title
194 ]);
195 }
196 if (!empty($alt)) {
197 update_post_meta($attach_id, '_wp_attachment_image_alt', $alt);
198 }
199 }
200
201 return $attach_id;
202 } else {
203 return false;
204 }
205 }
206 }
207
208 public function create_attachment_from_data($post_id, $file_data, $params = array())
209 {
210 require_once ABSPATH . 'wp-admin/includes/file.php';
211 require_once ABSPATH . 'wp-admin/includes/media.php';
212 require_once ABSPATH . 'wp-admin/includes/image.php';
213
214 $title = isset($params['title']) ? $params['title'] : "";
215 $alt = isset($params['alt']) ? $params['alt'] : "";
216
217 $matches = array();
218 preg_match('/data:(.*?);/', $file_data, $matches);
219 $ext = Attachment::mime2ext($matches[1]);
220 if (!$ext) {
221 return WP_Error('upload_error', "Can't find file ext");
222 }
223 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
224 $image_data = file_get_contents($file_data);
225
226 $wp_upload_dir = wp_upload_dir();
227 $image_filename = wp_unique_filename($wp_upload_dir['path'], wp_rand() . "." . $ext);
228 $image = $wp_upload_dir['path'] . '/' . $image_filename;
229
230 file_put_contents($image, $image_data);
231
232 if (!file_exists($image)) {
233 return WP_Error('upload_error', 'File not created');
234 }
235
236 $file_array = array('name' => basename($image), 'size' => filesize($image), 'tmp_name' => $image);
237
238 if (isset($params['inner_post_id'])) {
239 $file_array['inner_post_id'] = $params['inner_post_id'];
240 }
241
242 if (isset($params['inner_attach_type'])) {
243 $file_array['inner_attach_type'] = $params['inner_attach_type'];
244 }
245
246 $attach_id = media_handle_sideload($file_array, $post_id, $title);
247
248 if ($attach_id) {
249 if (!empty($title)) {
250 wp_update_post(array('ID' => $attach_id, 'post_excerpt' => $title));
251 }
252 if (!empty($alt)) {
253 update_post_meta($attach_id, '_wp_attachment_image_alt', $alt);
254 }
255 }
256
257 return $attach_id;
258 }
259
260 public function create_attachment_from_file($post_id, $file, $params = [])
261 {
262 require_once ABSPATH . 'wp-admin/includes/file.php';
263 require_once ABSPATH . 'wp-admin/includes/media.php';
264 require_once ABSPATH . 'wp-admin/includes/image.php';
265
266 $file_temp = $file['tmp_name'];
267 $file_mime = $file['type'];
268
269 $title = $params['title'] ?? "";
270 $alt = $params['alt'] ?? "";
271
272 $ext = Attachment::mime2ext($file_mime);
273 if (!$ext) {
274 return WP_Error('upload_error', "Can't find file ext");
275 }
276 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
277 $image_data = file_get_contents($file_temp);
278
279 $wp_upload_dir = wp_upload_dir();
280 $image_filename = wp_unique_filename($wp_upload_dir['path'], wp_rand() . "." . $ext);
281 $image = $wp_upload_dir['path'] . '/' . $image_filename;
282
283 file_put_contents($image, $image_data);
284
285 if (!file_exists($image)) {
286 return WP_Error('upload_error', 'File not created');
287 }
288
289 $file_array = [
290 'name' => basename($image),
291 'size' => filesize($image),
292 'tmp_name' => $image
293 ];
294
295 if (isset($params['inner_post_id'])) {
296 $file_array['inner_post_id'] = $params['inner_post_id'];
297 }
298
299 if (isset($params['inner_attach_type'])) {
300 $file_array['inner_attach_type'] = $params['inner_attach_type'];
301 }
302
303 $attach_id = media_handle_sideload($file_array, $post_id, $title);
304
305 if ($attach_id) {
306 if (!empty($title)) {
307 wp_update_post([
308 'ID' => $attach_id,
309 'post_excerpt' => $title
310 ]);
311 }
312 if (!empty($alt)) {
313 update_post_meta($attach_id, '_wp_attachment_image_alt', $alt);
314 }
315 }
316
317 return $attach_id;
318 }
319
320 private function download_url($url)
321 {
322 $wp_upload_dir = wp_upload_dir();
323 $parsed_url = wp_parse_url($url);
324 $pathinfo = pathinfo($parsed_url['path']);
325 if (!$pathinfo || !isset($pathinfo['extension'])) {
326 return false;
327 }
328 $dest_filename = wp_unique_filename($wp_upload_dir['path'], wp_rand() . "." . $pathinfo['extension']);
329
330 $dest_path = $wp_upload_dir['path'] . '/' . $dest_filename;
331
332 $response = a2wl_remote_get($url);
333 if (is_wp_error($response)) {
334 return false;
335 } elseif (!in_array($response['response']['code'], array(404, 403))) {
336 file_put_contents($dest_path, $response['body']);
337 }
338
339 if (!file_exists($dest_path)) {
340 return false;
341 } else {
342 return $dest_path;
343 }
344 }
345
346 private function set_attachment_metadata($attach_id, $image_url)
347 {
348 update_post_meta($attach_id, self::KEY_ATTACHED_FILE, 1);
349
350 $pi = pathinfo($image_url);
351
352 $image_sizes = array();
353 if (!empty($pi['extension'])) {
354 $image_sizes['thumbnail'] = array('url' => $image_url . '_50x50.' . $pi['extension'], 'width' => 50, 'height' => 50);
355 $image_sizes['small1'] = array('url' => $image_url . '_100x100.' . $pi['extension'], 'width' => 100, 'height' => 100);
356 $image_sizes['small2'] = array('url' => $image_url . '_200x200.' . $pi['extension'], 'width' => 200, 'height' => 200);
357 $image_sizes['medium'] = array('url' => $image_url . '_350x350.' . $pi['extension'], 'width' => 350, 'height' => 350);
358 $image_sizes['medium_large'] = array('url' => $image_url . '_640x640.' . $pi['extension'], 'width' => 640, 'height' => 640);
359 }
360
361 $image_sizes['large'] = array('url' => $image_url, 'width' => 800, 'height' => 800);
362
363 $attach_data = array(
364 'file' => 0,
365 'width' => 0,
366 'height' => 0,
367 'sizes' => array(),
368 'image_meta' => array(
369 'aperture' => '0',
370 'credit' => '',
371 'camera' => '',
372 'caption' => '',
373 'created_timestamp' => '0',
374 'copyright' => '',
375 'focal_length' => '0',
376 'iso' => '0',
377 'shutter_speed' => '0',
378 'title' => '',
379 'orientation' => '0',
380 'keywords' => array(),
381 ),
382 );
383
384 $attach_data = array_replace_recursive($attach_data, array(
385 'file' => $image_sizes['large']['url'],
386 'width' => $image_sizes['large']['width'],
387 'height' => $image_sizes['large']['height'],
388 ));
389
390 $wp_sizes = $this->utils->get_image_sizes();
391 foreach ($wp_sizes as $size => $props) {
392 $found_size = $this->_choose_image_size_from_aliexpress($props, $image_sizes);
393
394 if (!empty($found_size)) {
395 $wp_filetype = wp_check_filetype(basename($found_size['url']), null);
396 $attach_data['sizes']["$size"] = array(
397 'file' => basename($found_size['url']),
398 'width' => $found_size['width'],
399 'height' => $found_size['height'],
400 'mime-type' => $wp_filetype['type'],
401 );
402 }
403 }
404
405 wp_update_attachment_metadata($attach_id, $attach_data);
406 }
407
408 private function _choose_image_size_from_aliexpress($size, $image_sizes = array())
409 {
410 if (empty($image_sizes)) {
411 return false;
412 }
413
414 $min_size = $max_size = false;
415 foreach ($image_sizes as $props) {
416 if ((int) $size['width'] == (int) $props['width']) {
417 return $props;
418 }
419
420 if (intval($size['width']) < intval($props['width']) && (!$min_size || intval($min_size['width']) > intval($props['width']))) {
421 $min_size = $props;
422 }
423
424 if (!$max_size || (intval($max_size['width']) < intval($props['width']))) {
425 $max_size = $props;
426 }
427 }
428
429 return !$min_size ? $max_size : $min_size;
430 }
431
432 public static function find_products_with_external_images()
433 {
434 global $wpdb;
435
436 $result_ids = [];
437 $query = "SELECT DISTINCT IF(p.post_parent = 0, p.id, p.post_parent) AS id FROM $wpdb->posts p, " .
438 "(SELECT DISTINCT p1.post_parent AS id FROM $wpdb->posts p1 " .
439 "LEFT JOIN $wpdb->postmeta pm1 ON (p1.id = pm1.post_id) " .
440 "WHERE p1.post_type = 'attachment' AND pm1.meta_key=%s " .
441 "AND pm1.meta_value='1') pp " .
442 "WHERE p.ID = pp.id";
443 $tmp_product_ids = $wpdb->get_results(
444 $wpdb->prepare($query, self::KEY_ATTACHED_FILE),
445 ARRAY_N
446 );
447 foreach ($tmp_product_ids as $row) {
448 $result_ids[] = $row[0];
449 }
450 return $result_ids;
451 }
452
453 public static function calc_total_external_images()
454 {
455 global $wpdb;
456
457 $query = "SELECT count(id) FROM (SELECT DISTINCT p1.id AS id FROM $wpdb->posts p1 " .
458 "LEFT JOIN $wpdb->postmeta pm1 ON (p1.id = pm1.post_id) WHERE p1.post_type = 'attachment' " .
459 "AND pm1.meta_key=%s AND pm1.meta_value='1') AS pp";
460
461 $cnt = $wpdb->get_var($wpdb->prepare($query, self::KEY_ATTACHED_FILE));
462
463 $patterns = self::externalImageLikePatterns();
464 $likeConditions = implode(' OR ', array_fill(0, count($patterns), 'post_content LIKE %s'));
465 $query = "SELECT count(ID) FROM $wpdb->posts WHERE post_type = 'product' AND (" . $likeConditions . ")";
466 $cnt += $wpdb->get_var($wpdb->prepare($query, ...$patterns));
467
468 return $cnt;
469 }
470
471 public static function find_external_images(int $page_size = 1000, $post_id = false): array
472 {
473 global $wpdb;
474
475 $result_ids = [];
476 //1. find external images in the attachments
477 $post_filter = $post_id && intval($post_id) > 0 ? " AND p1.post_parent=" . intval($post_id) . " " : "";
478 $sql = "SELECT DISTINCT p1.id AS id FROM $wpdb->posts p1 " .
479 "LEFT JOIN $wpdb->postmeta pm1 ON(p1.id = pm1.post_id) " .
480 "WHERE p1.post_type = 'attachment' AND pm1.meta_key=%s AND pm1.meta_value='1' " .
481 $post_filter . " LIMIT %d";
482
483 $tmp_product_ids = $wpdb->get_results(
484 $wpdb->prepare(
485 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
486 $sql,
487 self::KEY_ATTACHED_FILE,
488 $page_size
489 ),
490 ARRAY_N
491 );
492
493 foreach ($tmp_product_ids as $row) {
494 $result_ids[] = $row[0];
495 }
496
497 $posts_limit = $page_size - count($result_ids);
498 if ($posts_limit > 0) {
499 //2. find products with external images in the product description
500 $post_filter = $post_id && intval($post_id) > 0 ? " AND ID=" . intval($post_id) . " " : "";
501 $patterns = self::externalImageLikePatterns();
502 $likeConditions = implode(' OR ', array_fill(0, count($patterns), 'post_content LIKE %s'));
503 $sql = "SELECT ID FROM $wpdb->posts " .
504 "WHERE post_type = 'product' AND ($likeConditions) $post_filter" . " " .
505 "LIMIT %d";
506 $tmp_product_ids = $wpdb->get_results(
507 $wpdb->prepare(
508 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
509 $sql,
510 ...array_merge($patterns, [$posts_limit])
511 ),
512 ARRAY_N
513 );
514
515 foreach ($tmp_product_ids as $row) {
516 $result_ids[] = $row[0];
517 }
518 }
519
520 return $result_ids;
521 }
522
523 /**
524 * @throws Exception
525 */
526 public function load_external_image($post_id): void
527 {
528 global $wpdb;
529
530 if ($post_id) {
531 $post_id = intval($post_id);
532 $post = get_post($post_id);
533 if ($post->post_type === 'attachment') {
534 //load external images for attachments
535 $tmp = get_post_meta($post_id, self::KEY_ATTACHED_FILE, true);
536 if ($tmp && intval($tmp) === 1) {
537
538 $new_image_id = $this->create_attachment($post->post_parent, $post->guid, array('inner_post_id' => $post_id, 'title' => $post->post_title, 'alt' => $post->post_title));
539 if ($new_image_id) {
540 $wpdb->query(
541 $wpdb->prepare(
542 "UPDATE $wpdb->postmeta SET meta_value = %s " .
543 "WHERE meta_key = '_thumbnail_id' AND meta_value = %d",
544 $new_image_id,
545 $post_id
546 )
547 );
548
549 $res = $wpdb->get_results(
550 $wpdb->prepare(
551 "SELECT meta_id, post_id, meta_key, meta_value FROM $wpdb->postmeta " .
552 "WHERE meta_key=%s",
553 '_product_image_gallery'
554 ),
555 ARRAY_A);
556 foreach ($res as $row) {
557 $tmp_id_list = explode(',', $row['meta_value']);
558 $tmp_id_list_res = array();
559 foreach ($tmp_id_list as $id_str) {
560 if (intval($id_str) > 0) {
561 if (intval($id_str) === $post_id) {
562 $tmp_id_list_res[] = $new_image_id;
563 } else {
564 $tmp_id_list_res[] = intval($id_str);
565 }
566 }
567 }
568
569 $wpdb->query(
570 $wpdb->prepare(
571 "UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_id = %s",
572 implode(',', $tmp_id_list_res),
573 $row['meta_id']
574 )
575 );
576 }
577
578 // update swatch
579 $sql = "SELECT meta_id, post_id, meta_key, meta_value FROM $wpdb->postmeta " .
580 "WHERE meta_key='_swatch_type_options' AND meta_value like '%\"" . $post_id . "\"%'";
581
582 $res = $wpdb->get_results(
583 $wpdb->prepare(
584 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
585 $sql
586 ),
587 ARRAY_A
588 );
589 foreach ($res as $row) {
590 $swatch_type_options = unserialize($row['meta_value']);
591 foreach ($swatch_type_options as $k => $v) {
592 foreach ($v['attributes'] as $ak => $a) {
593 if (isset($a['image']) && $a['image'] == $post_id) {
594 $swatch_type_options[$k]['attributes'][$ak]['image'] = $new_image_id;
595 }
596 }
597 }
598 delete_post_meta($row['post_id'], '_swatch_type_options');
599 update_post_meta($row['post_id'], "_swatch_type_options", $swatch_type_options);
600 }
601
602 Utils::delete_attachment($post_id, true);
603 $wpdb->query($wpdb->prepare(
604 "DELETE FROM {$wpdb->postmeta} WHERE post_id = %d",
605 $post_id
606 ));
607 wp_delete_post($post_id, true);
608 }
609 }
610 } else if ($post->post_content && class_exists('DOMDocument')) {
611 //load external images from the product description
612 if (function_exists('libxml_use_internal_errors')) {
613 libxml_use_internal_errors(true);
614 }
615 $dom = new DOMDocument();
616 @$dom->loadHTML($post->post_content);
617 $dom->formatOutput = true;
618
619 $elements = $dom->getElementsByTagName('img');
620 $replace_map = [];
621 for ($i = $elements->length; --$i >= 0;) {
622 $e = $elements->item($i);
623 $old_url = $e->getAttribute('src');
624
625 if (self::isExternalImageUrl($old_url)) {
626 $attachment_id = $this->create_attachment(
627 $post_id,
628 $e->getAttribute('src'),
629 ['inner_post_id' => $post_id, 'title' => $post->post_title, 'alt' => $post->post_title]
630 );
631 $new_url = wp_get_attachment_url($attachment_id);
632 $replace_map[$old_url] = $new_url;
633 }
634 }
635 $post->post_content = str_replace(
636 array_keys($replace_map),
637 array_values($replace_map),
638 $post->post_content
639 );
640 wp_update_post(['ID' => $post_id, 'post_content' => $post->post_content]);
641 }
642 } else {
643 throw new Exception("load_external_image: waiting for ID...");
644 }
645 }
646
647 public static function mime2ext($mime)
648 {
649 $all_mimes = '{"png":["image\/png","image\/x-png"],"bmp":["image\/bmp","image\/x-bmp","image\/x-bitmap","image\/x-xbitmap","image\/x-win-bitmap","image\/x-windows-bmp","image\/ms-bmp","image\/x-ms-bmp","application\/bmp","application\/x-bmp","application\/x-win-bitmap"],"gif":["image\/gif"],"jpeg":["image\/jpeg","image\/pjpeg"],"xspf":["application\/xspf+xml"],"vlc":["application\/videolan"],"wmv":["video\/x-ms-wmv","video\/x-ms-asf"],"au":["audio\/x-au"],"ac3":["audio\/ac3"],"flac":["audio\/x-flac"],"ogg":["audio\/ogg","video\/ogg","application\/ogg"],"kmz":["application\/vnd.google-earth.kmz"],"kml":["application\/vnd.google-earth.kml+xml"],"rtx":["text\/richtext"],"rtf":["text\/rtf"],"jar":["application\/java-archive","application\/x-java-application","application\/x-jar"],"zip":["application\/x-zip","application\/zip","application\/x-zip-compressed","application\/s-compressed","multipart\/x-zip"],"7zip":["application\/x-compressed"],"xml":["application\/xml","text\/xml"],"svg":["image\/svg+xml"],"3g2":["video\/3gpp2"],"3gp":["video\/3gp","video\/3gpp"],"mp4":["video\/mp4"],"m4a":["audio\/x-m4a"],"f4v":["video\/x-f4v"],"flv":["video\/x-flv"],"webm":["video\/webm"],"aac":["audio\/x-acc"],"m4u":["application\/vnd.mpegurl"],"pdf":["application\/pdf","application\/octet-stream"],"pptx":["application\/vnd.openxmlformats-officedocument.presentationml.presentation"],"ppt":["application\/powerpoint","application\/vnd.ms-powerpoint","application\/vnd.ms-office","application\/msword"],"docx":["application\/vnd.openxmlformats-officedocument.wordprocessingml.document"],"xlsx":["application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet","application\/vnd.ms-excel"],"xl":["application\/excel"],"xls":["application\/msexcel","application\/x-msexcel","application\/x-ms-excel","application\/x-excel","application\/x-dos_ms_excel","application\/xls","application\/x-xls"],"xsl":["text\/xsl"],"mpeg":["video\/mpeg"],"mov":["video\/quicktime"],"avi":["video\/x-msvideo","video\/msvideo","video\/avi","application\/x-troff-msvideo"],"movie":["video\/x-sgi-movie"],"log":["text\/x-log"],"txt":["text\/plain"],"css":["text\/css"],"html":["text\/html"],"wav":["audio\/x-wav","audio\/wave","audio\/wav"],"xhtml":["application\/xhtml+xml"],"tar":["application\/x-tar"],"tgz":["application\/x-gzip-compressed"],"psd":["application\/x-photoshop","image\/vnd.adobe.photoshop"],"exe":["application\/x-msdownload"],"js":["application\/x-javascript"],"mp3":["audio\/mpeg","audio\/mpg","audio\/mpeg3","audio\/mp3"],"rar":["application\/x-rar","application\/rar","application\/x-rar-compressed"],"gzip":["application\/x-gzip"],"hqx":["application\/mac-binhex40","application\/mac-binhex","application\/x-binhex40","application\/x-mac-binhex40"],"cpt":["application\/mac-compactpro"],"bin":["application\/macbinary","application\/mac-binary","application\/x-binary","application\/x-macbinary"],"oda":["application\/oda"],"ai":["application\/postscript"],"smil":["application\/smil"],"mif":["application\/vnd.mif"],"wbxml":["application\/wbxml"],"wmlc":["application\/wmlc"],"dcr":["application\/x-director"],"dvi":["application\/x-dvi"],"gtar":["application\/x-gtar"],"php":["application\/x-httpd-php","application\/php","application\/x-php","text\/php","text\/x-php","application\/x-httpd-php-source"],"swf":["application\/x-shockwave-flash"],"sit":["application\/x-stuffit"],"z":["application\/x-compress"],"mid":["audio\/midi"],"aif":["audio\/x-aiff","audio\/aiff"],"ram":["audio\/x-pn-realaudio"],"rpm":["audio\/x-pn-realaudio-plugin"],"ra":["audio\/x-realaudio"],"rv":["video\/vnd.rn-realvideo"],"jp2":["image\/jp2","video\/mj2","image\/jpx","image\/jpm"],"tiff":["image\/tiff"],"eml":["message\/rfc822"],"pem":["application\/x-x509-user-cert","application\/x-pem-file"],"p10":["application\/x-pkcs10","application\/pkcs10"],"p12":["application\/x-pkcs12"],"p7a":["application\/x-pkcs7-signature"],"p7c":["application\/pkcs7-mime","application\/x-pkcs7-mime"],"p7r":["application\/x-pkcs7-certreqresp"],"p7s":["application\/pkcs7-signature"],"crt":["application\/x-x509-ca-cert","application\/pkix-cert"],"crl":["application\/pkix-crl","application\/pkcs-crl"],"pgp":["application\/pgp"],"gpg":["application\/gpg-keys"],"rsa":["application\/x-pkcs7"],"ics":["text\/calendar"],"zsh":["text\/x-scriptzsh"],"cdr":["application\/cdr","application\/coreldraw","application\/x-cdr","application\/x-coreldraw","image\/cdr","image\/x-cdr","zz-application\/zz-winassoc-cdr"],"wma":["audio\/x-ms-wma"],"vcf":["text\/x-vcard"],"srt":["text\/srt"],"vtt":["text\/vtt"],"ico":["image\/x-icon","image\/x-ico","image\/vnd.microsoft.icon"],"csv":["text\/x-comma-separated-values","text\/comma-separated-values","application\/vnd.msexcel"],"json":["application\/json","text\/json"]}';
650 $all_mimes = json_decode($all_mimes, true);
651 foreach ($all_mimes as $key => $value) {
652 if (array_search($mime, $value) !== false) {
653 return $key;
654 }
655
656 }
657 return false;
658 }
659
660 }
661