PluginProbe
Elementor Website Builder – more than just a page builder / 3.1.0
Elementor Website Builder – more than just a page builder v3.1.0
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / files / assets / files-upload-handler.php

files-upload-handler.php in Elementor Website Builder – more than just a page builder 3.1.0, at core/files/assets/files-upload-handler.php

134 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Core\Files\Assets;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly.
7 }
8
9 abstract class Files_Upload_Handler {
10 const OPTION_KEY = 'elementor_unfiltered_files_upload';
11
12 public function __construct() {
13 add_filter( 'upload_mimes', [ $this, 'support_unfiltered_files_upload' ] );
14 add_filter( 'wp_handle_upload_prefilter', [ $this, 'handle_upload_prefilter' ] );
15 add_filter( 'wp_check_filetype_and_ext', [ $this, 'check_filetype_and_ext' ], 10, 4 );
16 }
17
18 abstract public function get_mime_type();
19
20 abstract public function get_file_type();
21
22 /**
23 * is_elementor_media_upload
24 * @return bool
25 */
26 private function is_elementor_media_upload() {
27 return isset( $_POST['uploadTypeCaller'] ) && 'elementor-editor-upload' === $_POST['uploadTypeCaller']; // phpcs:ignore
28 }
29
30 /**
31 * @return bool
32 */
33 final public static function is_enabled() {
34 $enabled = ! ! get_option( self::OPTION_KEY ) && self::file_sanitizer_can_run();
35
36 /**
37 * @deprecated 3.0.0 Use `elementor/document/urls/edit` filter instead.
38 */
39 $enabled = apply_filters( 'elementor/files/svg/enabled', $enabled );
40
41 /**
42 * Allow Unfiltered Files Upload.
43 *
44 * Determines whether to enable unfiltered file uploads.
45 *
46 * @since 3.0.0
47 *
48 * @param bool $enabled Weather upload is enabled or not.
49 */
50 $enabled = apply_filters( 'elementor/files/allow_unfiltered_upload', $enabled );
51
52 return $enabled;
53 }
54
55 final public function support_unfiltered_files_upload( $existing_mimes ) {
56 if ( $this->is_elementor_media_upload() ) {
57 $existing_mimes[ $this->get_file_type() ] = $this->get_mime_type();
58 }
59
60 return $existing_mimes;
61 }
62
63 /**
64 * handle_upload_prefilter
65 * @param $file
66 *
67 * @return mixed
68 */
69 public function handle_upload_prefilter( $file ) {
70 if ( ! $this->is_file_should_handled( $file ) ) {
71 return $file;
72 }
73
74 $ext = pathinfo( $file['name'], PATHINFO_EXTENSION );
75 $file_type = $this->get_file_type();
76 $display_type = strtoupper( $file_type );
77
78 if ( $file_type !== $ext ) {
79 $file['error'] = sprintf( __( 'The uploaded %1$s file is not supported. Please upload a valid %2$s file', 'elementor' ), $ext, $display_type );
80 return $file;
81 }
82
83 if ( ! self::is_enabled() ) {
84 $file['error'] = sprintf( __( '%1$s file is not allowed for security reasons', 'elementor' ), $display_type );
85 return $file;
86 }
87
88 return $file;
89 }
90
91 protected function is_file_should_handled( $file ) {
92 return $this->is_elementor_media_upload() && $this->get_mime_type() === $file['type'];
93 }
94
95 /**
96 * file_sanitizer_can_run
97 * @return bool
98 */
99 public static function file_sanitizer_can_run() {
100 return class_exists( 'DOMDocument' ) && class_exists( 'SimpleXMLElement' );
101 }
102
103 /**
104 * Check filetype and ext
105 *
106 * A workaround for upload validation which relies on a PHP extension (fileinfo)
107 * with inconsistent reporting behaviour.
108 * ref: https://core.trac.wordpress.org/ticket/39550
109 * ref: https://core.trac.wordpress.org/ticket/40175
110 *
111 * @param $data
112 * @param $file
113 * @param $filename
114 * @param $mimes
115 *
116 * @return mixed
117 */
118 public function check_filetype_and_ext( $data, $file, $filename, $mimes ) {
119 if ( ! empty( $data['ext'] ) && ! empty( $data['type'] ) ) {
120 return $data;
121 }
122
123 $wp_file_type = wp_check_filetype( $filename, $mimes );
124 $file_type = strtolower( $this->get_file_type() );
125
126 if ( $file_type === $wp_file_type['ext'] ) {
127 $data['ext'] = $file_type;
128 $data['type'] = $this->get_mime_type();
129 }
130
131 return $data;
132 }
133 }
134