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

672 lines 17.7 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, $allowed_file_extensions );
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 $file_name = str_replace( ' ', '', sanitize_file_name( $file_name ) );
279
280 if ( empty( $file_name ) ) {
281 return new \WP_Error( 'invalid_file_name', esc_html__( 'Invalid file name.', 'elementor' ) );
282 }
283
284 $temp_filename = $this->create_unique_dir() . $file_name;
285
286 /**
287 * Temp File Path
288 *
289 * Allows modifying the full path of the temporary file.
290 *
291 * @since 3.7.0
292 *
293 * @param string full path to file
294 */
295 $temp_filename = apply_filters( 'elementor/files/temp-file-path', $temp_filename );
296
297 file_put_contents( $temp_filename, $file_content ); // phpcs:ignore
298
299 return $temp_filename;
300 }
301
302 /**
303 * Get Temp Directory
304 *
305 * Get the temporary files directory path. If the directory does not exist, this method creates it.
306 *
307 * @since 3.3.0
308 * @access public
309 *
310 * @return string $temp_dir
311 */
312 public function get_temp_dir() {
313 if ( ! $this->temp_dir ) {
314 $wp_upload_dir = wp_upload_dir();
315
316 $temp_dir = implode( DIRECTORY_SEPARATOR, [ $wp_upload_dir['basedir'], 'elementor', 'tmp' ] ) . DIRECTORY_SEPARATOR;
317
318 /**
319 * Temp File Path
320 *
321 * Allows modifying the full path of the temporary file.
322 *
323 * @since 3.7.0
324 *
325 * @param string temporary directory
326 */
327 $this->temp_dir = apply_filters( 'elementor/files/temp-dir', $temp_dir );
328
329 if ( ! is_dir( $this->temp_dir ) ) {
330 wp_mkdir_p( $this->temp_dir );
331 }
332 }
333
334 return $this->temp_dir;
335 }
336
337 /**
338 * Create Unique Temp Dir
339 *
340 * Create a unique temporary directory
341 *
342 * @since 3.3.0
343 * @access public
344 *
345 * @return string the new directory path
346 */
347 public function create_unique_dir() {
348 $unique_dir_path = $this->get_temp_dir() . uniqid() . DIRECTORY_SEPARATOR;
349
350 wp_mkdir_p( $unique_dir_path );
351
352 return $unique_dir_path;
353 }
354
355 /**
356 * Register Ajax Actions
357 *
358 * Runs on the 'elementor/ajax/register_actions' hook. Receives the AJAX module as a parameter and registers
359 * callbacks for specified action IDs.
360 *
361 * @since 3.5.0
362 * @access public
363 *
364 * @param Ajax $ajax
365 */
366 public function register_ajax_actions( Ajax $ajax ) {
367 $ajax->register_ajax_action( 'enable_unfiltered_files_upload', [ $this, 'enable_unfiltered_files_upload' ] );
368 }
369
370 /**
371 * Set Unfiltered Files Upload
372 *
373 * @since 3.5.0
374 * @access public
375 */
376 public function enable_unfiltered_files_upload() {
377 if ( ! current_user_can( 'manage_options' ) ) {
378 return;
379 }
380
381 update_option( self::UNFILTERED_FILE_UPLOADS_KEY, 1 );
382 }
383
384 /**
385 * Support Unfiltered File Uploads
386 *
387 * When uploading a file within Elementor, this method adds the registered
388 * file types to WordPress' allowed mimes list. This will only happen if the user allowed unfiltered file uploads
389 * in Elementor's settings in the admin dashboard.
390 *
391 * @since 3.5.0
392 * @access public
393 *
394 * @param array $allowed_mimes
395 * @return array allowed mime types
396 */
397 final public function support_unfiltered_elementor_file_uploads( $allowed_mimes ) {
398 if ( $this->is_elementor_upload() && $this->are_unfiltered_uploads_enabled() ) {
399 foreach ( $this->file_type_handlers as $file_type_handler ) {
400 $allowed_mimes[ $file_type_handler->get_file_extension() ] = $file_type_handler->get_mime_type();
401 }
402 }
403
404 return $allowed_mimes;
405 }
406
407 /**
408 * Set Elementor Upload State
409 *
410 * @since 3.5.0
411 * @access public
412 *
413 * @param $state
414 */
415 public function set_elementor_upload_state( $state ) {
416 $this->is_elementor_upload = $state;
417 }
418
419 /**
420 * Is Elementor Upload
421 *
422 * This method checks if the current session includes a request to upload files made via Elementor.
423 *
424 * @since 3.5.0
425 * @access private
426 *
427 * @return bool
428 */
429 private function is_elementor_upload() {
430 return $this->is_elementor_upload || $this->is_elementor_media_upload() || $this->is_elementor_wp_media_upload();
431 }
432
433 /**
434 * Is Elementor Media Upload
435 *
436 * Checks whether the current request includes uploading files via Elementor which are not destined for the Media
437 * Library.
438 *
439 * @since 3.5.0
440 * @access public
441 *
442 * @return bool
443 */
444 public function is_elementor_media_upload() {
445 // Sometimes `uploadTypeCaller` passed as a GET parameter when using the WP Media Library REST API, where the
446 // whole request body is occupied by the uploaded file.
447 return isset( $_REQUEST['uploadTypeCaller'] ) && 'elementor-media-upload' === $_REQUEST['uploadTypeCaller']; // phpcs:ignore
448 }
449
450 /**
451 * Is Elementor WP Media Upload
452 *
453 * Checks whether the current request is a request to upload files into the WP Media Library via Elementor.
454 *
455 * @since 3.3.0
456 * @access private
457 *
458 * @return bool
459 */
460 private function is_elementor_wp_media_upload() {
461 return isset( $_REQUEST['uploadTypeCaller'] ) && 'elementor-wp-media-upload' === $_REQUEST['uploadTypeCaller']; // phpcs:ignore
462 }
463
464 /**
465 * Add File Extension To Allowed Extensions List
466 *
467 * @since 3.3.0
468 * @access private
469 *
470 * @param string $file_type
471 */
472 private function add_file_extension_to_allowed_extensions_list( $file_type ) {
473 $file_handler = $this->file_type_handlers[ $file_type ];
474
475 $file_extension = $file_handler->get_file_extension();
476
477 // Only add the file extension to the list if it doesn't already exist in it.
478 if ( ! in_array( $file_extension, $this->allowed_file_extensions, true ) ) {
479 $this->allowed_file_extensions[] = $file_extension;
480 }
481 }
482
483 /**
484 * Save Base64 as File
485 *
486 * Saves a Base64 string as a .tmp file in Elementor's temporary files directory.
487 *
488 * @since 3.3.0
489 * @access private
490 *
491 * @param $file
492 * @param array|null $allowed_file_extensions
493 *
494 * @return array|\WP_Error
495 */
496 private function save_base64_to_tmp_file( $file, $allowed_file_extensions = null ) {
497 $file_extension = pathinfo( $file['fileName'], PATHINFO_EXTENSION );
498 $is_file_type_allowed = $this->is_file_type_allowed( $file_extension, $allowed_file_extensions );
499
500 if ( is_wp_error( $is_file_type_allowed ) ) {
501 return $is_file_type_allowed;
502 }
503
504 $file_content = base64_decode( $file['fileData'] ); // phpcs:ignore
505
506 // If the decode fails
507 if ( ! $file_content ) {
508 return new \WP_Error( 'file_error', self::INVALID_FILE_CONTENT );
509 }
510
511 $temp_filename = $this->create_temp_file( $file_content, $file['fileName'] );
512
513 if ( is_wp_error( $temp_filename ) ) {
514 return $temp_filename;
515 }
516
517 return [
518 // the original uploaded file name
519 'name' => $file['fileName'],
520 // The path to the temporary file
521 'tmp_name' => $temp_filename,
522 ];
523 }
524
525 /**
526 * Validate File
527 *
528 * @since 3.3.0
529 * @access private
530 *
531 * @param array $file
532 * @param array $file_extensions Optional
533 * @return bool|\WP_Error
534 */
535 private function validate_file( array $file, $file_extensions = [] ) {
536 $uploaded_file_name = isset( $file['name'] ) ? $file['name'] : $file['tmp_name'];
537
538 $file_extension = pathinfo( $uploaded_file_name, PATHINFO_EXTENSION );
539
540 if ( ! $this->is_elementor_wp_media_upload() ) {
541 $is_file_type_allowed = $this->is_file_type_allowed( $file_extension, $file_extensions );
542
543 if ( is_wp_error( $is_file_type_allowed ) ) {
544 return $is_file_type_allowed;
545 }
546 }
547
548 $file_type_handler = $this->get_file_type_handlers( $file_extension );
549
550 // If Elementor does not have a handler for this file type, don't block it.
551 if ( ! $file_type_handler ) {
552 return true;
553 }
554
555 // If there is a File Type Handler for the uploaded file, it means it is a non-standard file type. In this case,
556 // we check if unfiltered file uploads are enabled or not before allowing it.
557 if ( ! self::are_unfiltered_uploads_enabled() ) {
558 return new \WP_Error( Exceptions::FORBIDDEN, esc_html__( 'This file is not allowed for security reasons.', 'elementor' ) );
559 }
560
561 // Here is each file type handler's chance to run its own specific validations
562 return $file_type_handler->validate_file( $file );
563 }
564
565 /**
566 * Is File Type Allowed
567 *
568 * Checks whether the passed file extension is allowed for upload.
569 *
570 * @since 3.5.0
571 * @access private
572 *
573 * @param $file_extension
574 * @param $filtered_file_extensions
575 * @return bool|\WP_Error
576 */
577 private function is_file_type_allowed( $file_extension, $filtered_file_extensions ) {
578 $allowed_file_extensions = $this->get_allowed_file_extensions();
579
580 if ( $filtered_file_extensions ) {
581 $allowed_file_extensions = array_intersect( $allowed_file_extensions, $filtered_file_extensions );
582 }
583
584 $is_allowed = false;
585
586 // Check if the file type (extension) is in the allowed extensions list. If it is a non-standard file type (not
587 // enabled by default in WordPress) and unfiltered file uploads are not enabled, it will not be in the allowed
588 // file extensions list.
589 foreach ( $allowed_file_extensions as $allowed_extension ) {
590 if ( preg_match( '/' . $allowed_extension . '/', $file_extension ) ) {
591 $is_allowed = true;
592
593 break;
594 }
595 }
596
597 if ( ! $is_allowed ) {
598 $is_allowed = new \WP_Error( Exceptions::FORBIDDEN, 'Uploading this file type is not allowed.' );
599 }
600
601 /**
602 * Elementor File Type Allowed
603 *
604 * Allows setting file types
605 *
606 * @since 3.5.0
607 *
608 * @param bool|\WP_Error $is_allowed
609 */
610 return apply_filters( 'elementor/files/allow-file-type/' . $file_extension, $is_allowed );
611 }
612
613 /**
614 * Remove Directory with Files
615 *
616 * @since 3.3.0
617 * @access private
618 *
619 * @param string $dir
620 * @return bool
621 */
622 private function remove_directory_with_files( $dir ) {
623 $dir_iterator = new \RecursiveDirectoryIterator( $dir, \RecursiveDirectoryIterator::SKIP_DOTS );
624
625 foreach ( new \RecursiveIteratorIterator( $dir_iterator, \RecursiveIteratorIterator::CHILD_FIRST ) as $name => $item ) {
626 if ( is_dir( $name ) ) {
627 rmdir( $name );
628 } else {
629 unlink( $name );
630 }
631 }
632
633 return rmdir( $dir );
634 }
635
636 /**
637 * Get Allowed File Extensions
638 *
639 * Retrieve an array containing the list of file extensions allowed for upload.
640 *
641 * @since 3.3.0
642 * @access private
643 *
644 * @return array file extension/s
645 */
646 private function get_allowed_file_extensions() {
647 if ( ! $this->allowed_file_extensions ) {
648 $this->allowed_file_extensions = array_keys( get_allowed_mime_types() );
649
650 foreach ( $this->get_file_type_handlers() as $file_type => $handler ) {
651 if ( $handler->is_upload_allowed() ) {
652 // Add the file extension to the allowed extensions list only if unfiltered files upload is enabled.
653 $this->add_file_extension_to_allowed_extensions_list( $file_type );
654 }
655 }
656 }
657
658 return $this->allowed_file_extensions;
659 }
660
661 public function __construct() {
662 $this->register_file_types();
663
664 add_filter( 'upload_mimes', [ $this, 'support_unfiltered_elementor_file_uploads' ] );
665 add_filter( 'wp_handle_upload_prefilter', [ $this, 'handle_elementor_wp_media_upload' ] );
666 add_filter( 'wp_check_filetype_and_ext', [ $this, 'check_filetype_and_ext' ], 10, 4 );
667
668 // Ajax.
669 add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] );
670 }
671 }
672