PluginProbe
Elementor Website Builder – more than just a page builder / 3.35.0-dev2
Elementor Website Builder – more than just a page builder v3.35.0-dev2
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.35.0-dev2, at core/files/uploads-manager.php

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