PluginProbe
Elementor Website Builder – more than just a page builder / 3.16.0-dev1
Elementor Website Builder – more than just a page builder v3.16.0-dev1
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 / uploads-manager.php

uploads-manager.php in Elementor Website Builder – more than just a page builder 3.16.0-dev1, at core/files/uploads-manager.php

657 lines 17.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Files;
3
4 use Elementor\Core\Base\Base_Object;
5 use Elementor\Core\Common\Modules\Ajax\Module as Ajax;
6 use Elementor\Core\Files\File_Types\Base as File_Type_Base;
7 use Elementor\Core\Files\File_Types\Json;
8 use Elementor\Core\Files\File_Types\Svg;
9 use Elementor\Core\Files\File_Types\Zip;
10 use Elementor\Core\Utils\Exceptions;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly.
14 }
15
16 /**
17 * Elementor uploads manager.
18 *
19 * Elementor uploads manager handler class is responsible for handling file uploads that are not done with WP Media.
20 *
21 * @since 3.3.0
22 */
23 class Uploads_Manager extends Base_Object {
24
25 const UNFILTERED_FILE_UPLOADS_KEY = 'elementor_unfiltered_files_upload';
26 const INVALID_FILE_CONTENT = 'Invalid Content In File';
27
28 /**
29 * @var File_Type_Base[]
30 */
31 private $file_type_handlers = [];
32
33 private $allowed_file_extensions;
34
35 /**
36 * @var bool
37 */
38 private $is_elementor_upload = false;
39
40 /**
41 * @var string
42 */
43 private $temp_dir;
44
45 /**
46 * Register File Types
47 *
48 * To Add a new file type to Elementor, with its own handling logic, you need to add it to the $file_types array here.
49 *
50 * @since 3.3.0
51 * @access public
52 */
53 public function register_file_types() {
54 // All file types that have handlers should be included here.
55 $file_types = [
56 'json' => new Json(),
57 'zip' => new Zip(),
58 'svg' => new Svg(),
59 ];
60
61 foreach ( $file_types as $file_type => $file_handler ) {
62 $this->file_type_handlers[ $file_type ] = $file_handler;
63 }
64 }
65
66 /**
67 * Extract and Validate Zip
68 *
69 * This method accepts a $file array (which minimally should include a 'tmp_name')
70 *
71 * @since 3.3.0
72 * @access public
73 *
74 * @param string $file_path
75 * @param array $allowed_file_types
76 * @return array|\WP_Error
77 */
78 public function extract_and_validate_zip( $file_path, $allowed_file_types = null ) {
79 $result = [];
80
81 /** @var Zip $zip_handler - File Type */
82 $zip_handler = $this->file_type_handlers['zip'];
83
84 // Returns an array of file paths.
85 $extracted = $zip_handler->extract( $file_path, $allowed_file_types );
86
87 if ( is_wp_error( $extracted ) ) {
88 return $extracted;
89 }
90
91 // If there are no extracted file names, no files passed the extraction validation.
92 if ( empty( $extracted['files'] ) ) {
93 // TODO: Decide what to do if no files passed the extraction validation
94 return new \WP_Error( 'file_error', self::INVALID_FILE_CONTENT );
95 }
96
97 $result['extraction_directory'] = $extracted['extraction_directory'];
98
99 foreach ( $extracted['files'] as $extracted_file_path ) {
100 // Each file is an array with a 'name' (file path) property.
101 if ( ! is_wp_error( $this->validate_file( [ 'tmp_name' => $extracted_file_path ] ) ) ) {
102 $result['files'][] = $extracted_file_path;
103 }
104 }
105
106 return $result;
107 }
108
109 /**
110 * Handle Elementor Upload
111 *
112 * This method receives a $file array. If the received file is a Base64 string, the $file array should include a
113 * 'fileData' property containing the string, which is decoded and has its contents stored in a temporary file.
114 * If the $file parameter passed is a standard $file array, the 'name' and 'tmp_name' properties are used for
115 * validation.
116 *
117 * The file goes through validation; if it passes validation, the file is returned. Otherwise, an error is returned.
118 *
119 * @since 3.3.0
120 * @access public
121 *
122 * @param array $file
123 * @param array $allowed_file_extensions Optional. an array of file types that are allowed to pass validation for each
124 * upload.
125 * @return array|\WP_Error
126 */
127 public function handle_elementor_upload( array $file, $allowed_file_extensions = null ) {
128 // If $file['fileData'] is set, it signals that the passed file is a Base64 string that needs to be decoded and
129 // saved to a temporary file.
130 if ( isset( $file['fileData'] ) ) {
131 $file = $this->save_base64_to_tmp_file( $file );
132 }
133
134 $validation_result = $this->validate_file( $file, $allowed_file_extensions );
135
136 if ( is_wp_error( $validation_result ) ) {
137 return $validation_result;
138 }
139
140 return $file;
141 }
142
143 /**
144 * are Unfiltered Uploads Enabled
145 *
146 * @since 3.5.0
147 * @access public
148 *
149 * @return bool
150 */
151 final public static function are_unfiltered_uploads_enabled() {
152 $enabled = ! ! get_option( self::UNFILTERED_FILE_UPLOADS_KEY ) && Svg::file_sanitizer_can_run();
153
154 /**
155 * Allow Unfiltered Files Upload.
156 *
157 * Determines whether to enable unfiltered file uploads.
158 *
159 * @since 3.0.0
160 *
161 * @param bool $enabled Whether upload is enabled or not.
162 */
163 $enabled = apply_filters( 'elementor/files/allow_unfiltered_upload', $enabled );
164
165 return $enabled;
166 }
167
168 /**
169 * Handle Elementor WP Media Upload
170 *
171 * Runs on the 'wp_handle_upload_prefilter' filter.
172 *
173 * @since 3.2.0
174 * @access public
175 *
176 * @param $file
177 * @return mixed
178 */
179 public function handle_elementor_wp_media_upload( $file ) {
180 // If it isn't a file uploaded by Elementor, we do not intervene.
181 if ( ! $this->is_elementor_wp_media_upload() ) {
182 return $file;
183 }
184
185 $result = $this->validate_file( $file );
186
187 if ( is_wp_error( $result ) ) {
188 $file['error'] = $result->get_error_message();
189 }
190
191 return $file;
192 }
193
194 /**
195 * Get File Type Handler
196 *
197 * Initialize the proper file type handler according to the file extension
198 * and assign it to the file type handlers array.
199 *
200 * @since 3.3.0
201 * @access public
202 *
203 * @param string|null $file_extension - file extension
204 * @return File_Type_Base[]|File_Type_Base
205 */
206 public function get_file_type_handlers( $file_extension = null ) {
207 return self::get_items( $this->file_type_handlers, $file_extension );
208 }
209
210 /**
211 * Check filetype and ext
212 *
213 * A workaround for upload validation which relies on a PHP extension (fileinfo)
214 * with inconsistent reporting behaviour.
215 * ref: https://core.trac.wordpress.org/ticket/39550
216 * ref: https://core.trac.wordpress.org/ticket/40175
217 *
218 * @since 3.5.0
219 * @access public
220 *
221 * @param $data
222 * @param $file
223 * @param $filename
224 * @param $mimes
225 *
226 * @return mixed
227 */
228 public function check_filetype_and_ext( $data, $file, $filename, $mimes ) {
229 if ( ! empty( $data['ext'] ) && ! empty( $data['type'] ) ) {
230 return $data;
231 }
232
233 $wp_file_type = wp_check_filetype( $filename, $mimes );
234
235 $file_type_handlers = $this->get_file_type_handlers();
236
237 if ( isset( $file_type_handlers[ $wp_file_type['ext'] ] ) ) {
238 $file_type_handler = $file_type_handlers[ $wp_file_type['ext'] ];
239
240 $data['ext'] = $file_type_handler->get_file_extension();
241 $data['type'] = $file_type_handler->get_mime_type();
242 }
243
244 return $data;
245 }
246
247 /**
248 * Remove File Or Directory
249 *
250 * Directory is deleted recursively with all of its contents (subdirectories and files).
251 *
252 * @since 3.3.0
253 * @access public
254 *
255 * @param string $path
256 */
257 public function remove_file_or_dir( $path ) {
258 if ( is_dir( $path ) ) {
259 $this->remove_directory_with_files( $path );
260 } else {
261 unlink( $path );
262 }
263 }
264
265 /**
266 * Create Temp File
267 *
268 * Create a random temporary file.
269 *
270 * @since 3.3.0
271 * @access public
272 *
273 * @param string $file_content
274 * @param string $file_name
275 * @return string|\WP_Error
276 */
277 public function create_temp_file( $file_content, $file_name ) {
278 $temp_filename = $this->create_unique_dir() . $file_name;
279
280 /**
281 * Temp File Path
282 *
283 * Allows modifying the full path of the temporary file.
284 *
285 * @since 3.7.0
286 *
287 * @param string full path to file
288 */
289 $temp_filename = apply_filters( 'elementor/files/temp-file-path', $temp_filename );
290
291 file_put_contents( $temp_filename, $file_content ); // phpcs:ignore
292
293 return $temp_filename;
294 }
295
296 /**
297 * Get Temp Directory
298 *
299 * Get the temporary files directory path. If the directory does not exist, this method creates it.
300 *
301 * @since 3.3.0
302 * @access public
303 *
304 * @return string $temp_dir
305 */
306 public function get_temp_dir() {
307 if ( ! $this->temp_dir ) {
308 $wp_upload_dir = wp_upload_dir();
309
310 $temp_dir = implode( DIRECTORY_SEPARATOR, [ $wp_upload_dir['basedir'], 'elementor', 'tmp' ] ) . DIRECTORY_SEPARATOR;
311
312 /**
313 * Temp File Path
314 *
315 * Allows modifying the full path of the temporary file.
316 *
317 * @since 3.7.0
318 *
319 * @param string temporary directory
320 */
321 $this->temp_dir = apply_filters( 'elementor/files/temp-dir', $temp_dir );
322
323 if ( ! is_dir( $this->temp_dir ) ) {
324 wp_mkdir_p( $this->temp_dir );
325 }
326 }
327
328 return $this->temp_dir;
329 }
330
331 /**
332 * Create Unique Temp Dir
333 *
334 * Create a unique temporary directory
335 *
336 * @since 3.3.0
337 * @access public
338 *
339 * @return string the new directory path
340 */
341 public function create_unique_dir() {
342 $unique_dir_path = $this->get_temp_dir() . uniqid() . DIRECTORY_SEPARATOR;
343
344 wp_mkdir_p( $unique_dir_path );
345
346 return $unique_dir_path;
347 }
348
349 /**
350 * Register Ajax Actions
351 *
352 * Runs on the 'elementor/ajax/register_actions' hook. Receives the AJAX module as a parameter and registers
353 * callbacks for specified action IDs.
354 *
355 * @since 3.5.0
356 * @access public
357 *
358 * @param Ajax $ajax
359 */
360 public function register_ajax_actions( Ajax $ajax ) {
361 $ajax->register_ajax_action( 'enable_unfiltered_files_upload', [ $this, 'enable_unfiltered_files_upload' ] );
362 }
363
364 /**
365 * Set Unfiltered Files Upload
366 *
367 * @since 3.5.0
368 * @access public
369 */
370 public function enable_unfiltered_files_upload() {
371 if ( ! current_user_can( 'manage_options' ) ) {
372 return;
373 }
374
375 update_option( self::UNFILTERED_FILE_UPLOADS_KEY, 1 );
376 }
377
378 /**
379 * Support Unfiltered File Uploads
380 *
381 * When uploading a file within Elementor, this method adds the registered
382 * file types to WordPress' allowed mimes list. This will only happen if the user allowed unfiltered file uploads
383 * in Elementor's settings in the admin dashboard.
384 *
385 * @since 3.5.0
386 * @access public
387 *
388 * @param array $allowed_mimes
389 * @return array allowed mime types
390 */
391 final public function support_unfiltered_elementor_file_uploads( $allowed_mimes ) {
392 if ( $this->is_elementor_upload() && $this->are_unfiltered_uploads_enabled() ) {
393 foreach ( $this->file_type_handlers as $file_type_handler ) {
394 $allowed_mimes[ $file_type_handler->get_file_extension() ] = $file_type_handler->get_mime_type();
395 }
396 }
397
398 return $allowed_mimes;
399 }
400
401 /**
402 * Set Elementor Upload State
403 *
404 * @since 3.5.0
405 * @access public
406 *
407 * @param $state
408 */
409 public function set_elementor_upload_state( $state ) {
410 $this->is_elementor_upload = $state;
411 }
412
413 /**
414 * Is Elementor Upload
415 *
416 * This method checks if the current session includes a request to upload files made via Elementor.
417 *
418 * @since 3.5.0
419 * @access private
420 *
421 * @return bool
422 */
423 private function is_elementor_upload() {
424 return $this->is_elementor_upload || $this->is_elementor_media_upload() || $this->is_elementor_wp_media_upload();
425 }
426
427 /**
428 * Is Elementor Media Upload
429 *
430 * Checks whether the current request includes uploading files via Elementor which are not destined for the Media
431 * Library.
432 *
433 * @since 3.5.0
434 * @access public
435 *
436 * @return bool
437 */
438 public function is_elementor_media_upload() {
439 // Sometimes `uploadTypeCaller` passed as a GET parameter when using the WP Media Library REST API, where the
440 // whole request body is occupied by the uploaded file.
441 return isset( $_REQUEST['uploadTypeCaller'] ) && 'elementor-media-upload' === $_REQUEST['uploadTypeCaller']; // phpcs:ignore
442 }
443
444 /**
445 * Is Elementor WP Media Upload
446 *
447 * Checks whether the current request is a request to upload files into the WP Media Library via Elementor.
448 *
449 * @since 3.3.0
450 * @access private
451 *
452 * @return bool
453 */
454 private function is_elementor_wp_media_upload() {
455 return isset( $_REQUEST['uploadTypeCaller'] ) && 'elementor-wp-media-upload' === $_REQUEST['uploadTypeCaller']; // phpcs:ignore
456 }
457
458 /**
459 * Add File Extension To Allowed Extensions List
460 *
461 * @since 3.3.0
462 * @access private
463 *
464 * @param string $file_type
465 */
466 private function add_file_extension_to_allowed_extensions_list( $file_type ) {
467 $file_handler = $this->file_type_handlers[ $file_type ];
468
469 $file_extension = $file_handler->get_file_extension();
470
471 // Only add the file extension to the list if it doesn't already exist in it.
472 if ( ! in_array( $file_extension, $this->allowed_file_extensions, true ) ) {
473 $this->allowed_file_extensions[] = $file_extension;
474 }
475 }
476
477 /**
478 * Save Base64 as File
479 *
480 * Saves a Base64 string as a .tmp file in Elementor's temporary files directory.
481 *
482 * @since 3.3.0
483 * @access private
484 *
485 * @param $file
486 * @return array|\WP_Error
487 */
488 private function save_base64_to_tmp_file( $file ) {
489 $file_content = base64_decode( $file['fileData'] ); // phpcs:ignore
490
491 // If the decode fails
492 if ( ! $file_content ) {
493 return new \WP_Error( 'file_error', self::INVALID_FILE_CONTENT );
494 }
495
496 $temp_filename = $this->create_temp_file( $file_content, $file['fileName'] );
497
498 if ( is_wp_error( $temp_filename ) ) {
499 return $temp_filename;
500 }
501
502 return [
503 // the original uploaded file name
504 'name' => $file['fileName'],
505 // The path to the temporary file
506 'tmp_name' => $temp_filename,
507 ];
508 }
509
510 /**
511 * Validate File
512 *
513 * @since 3.3.0
514 * @access private
515 *
516 * @param array $file
517 * @param array $file_extensions Optional
518 * @return bool|\WP_Error
519 */
520 private function validate_file( array $file, $file_extensions = [] ) {
521 $uploaded_file_name = isset( $file['name'] ) ? $file['name'] : $file['tmp_name'];
522
523 $file_extension = pathinfo( $uploaded_file_name, PATHINFO_EXTENSION );
524
525 if ( ! $this->is_elementor_wp_media_upload() ) {
526 $is_file_type_allowed = $this->is_file_type_allowed( $file_extension, $file_extensions );
527
528 if ( is_wp_error( $is_file_type_allowed ) ) {
529 return $is_file_type_allowed;
530 }
531 }
532
533 $file_type_handler = $this->get_file_type_handlers( $file_extension );
534
535 // If Elementor does not have a handler for this file type, don't block it.
536 if ( ! $file_type_handler ) {
537 return true;
538 }
539
540 // If there is a File Type Handler for the uploaded file, it means it is a non-standard file type. In this case,
541 // we check if unfiltered file uploads are enabled or not before allowing it.
542 if ( ! self::are_unfiltered_uploads_enabled() ) {
543 return new \WP_Error( Exceptions::FORBIDDEN, esc_html__( 'This file is not allowed for security reasons.', 'elementor' ) );
544 }
545
546 // Here is each file type handler's chance to run its own specific validations
547 return $file_type_handler->validate_file( $file );
548 }
549
550 /**
551 * Is File Type Allowed
552 *
553 * Checks whether the passed file extension is allowed for upload.
554 *
555 * @since 3.5.0
556 * @access private
557 *
558 * @param $file_extension
559 * @param $filtered_file_extensions
560 * @return bool|\WP_Error
561 */
562 private function is_file_type_allowed( $file_extension, $filtered_file_extensions ) {
563 $allowed_file_extensions = $this->get_allowed_file_extensions();
564
565 if ( $filtered_file_extensions ) {
566 $allowed_file_extensions = array_intersect( $allowed_file_extensions, $filtered_file_extensions );
567 }
568
569 $is_allowed = false;
570
571 // Check if the file type (extension) is in the allowed extensions list. If it is a non-standard file type (not
572 // enabled by default in WordPress) and unfiltered file uploads are not enabled, it will not be in the allowed
573 // file extensions list.
574 foreach ( $allowed_file_extensions as $allowed_extension ) {
575 if ( preg_match( '/' . $allowed_extension . '/', $file_extension ) ) {
576 $is_allowed = true;
577
578 break;
579 }
580 }
581
582 if ( ! $is_allowed ) {
583 $is_allowed = new \WP_Error( Exceptions::FORBIDDEN, 'Uploading this file type is not allowed.' );
584 }
585
586 /**
587 * Elementor File Type Allowed
588 *
589 * Allows setting file types
590 *
591 * @since 3.5.0
592 *
593 * @param bool|\WP_Error $is_allowed
594 */
595 return apply_filters( 'elementor/files/allow-file-type/' . $file_extension, $is_allowed );
596 }
597
598 /**
599 * Remove Directory with Files
600 *
601 * @since 3.3.0
602 * @access private
603 *
604 * @param string $dir
605 * @return bool
606 */
607 private function remove_directory_with_files( $dir ) {
608 $dir_iterator = new \RecursiveDirectoryIterator( $dir, \RecursiveDirectoryIterator::SKIP_DOTS );
609
610 foreach ( new \RecursiveIteratorIterator( $dir_iterator, \RecursiveIteratorIterator::CHILD_FIRST ) as $name => $item ) {
611 if ( is_dir( $name ) ) {
612 rmdir( $name );
613 } else {
614 unlink( $name );
615 }
616 }
617
618 return rmdir( $dir );
619 }
620
621 /**
622 * Get Allowed File Extensions
623 *
624 * Retrieve an array containing the list of file extensions allowed for upload.
625 *
626 * @since 3.3.0
627 * @access private
628 *
629 * @return array file extension/s
630 */
631 private function get_allowed_file_extensions() {
632 if ( ! $this->allowed_file_extensions ) {
633 $this->allowed_file_extensions = array_keys( get_allowed_mime_types() );
634
635 foreach ( $this->get_file_type_handlers() as $file_type => $handler ) {
636 if ( $handler->is_upload_allowed() ) {
637 // Add the file extension to the allowed extensions list only if unfiltered files upload is enabled.
638 $this->add_file_extension_to_allowed_extensions_list( $file_type );
639 }
640 }
641 }
642
643 return $this->allowed_file_extensions;
644 }
645
646 public function __construct() {
647 $this->register_file_types();
648
649 add_filter( 'upload_mimes', [ $this, 'support_unfiltered_elementor_file_uploads' ] );
650 add_filter( 'wp_handle_upload_prefilter', [ $this, 'handle_elementor_wp_media_upload' ] );
651 add_filter( 'wp_check_filetype_and_ext', [ $this, 'check_filetype_and_ext' ], 10, 4 );
652
653 // Ajax.
654 add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] );
655 }
656 }
657