/var/www/html/wp-content/plugins/elementor/core/files/uploads-manager.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
<?php
namespace Elementor\Core\Files;

use 
Elementor\Core\Base\Base_Object;
use 
Elementor\Core\Common\Modules\Ajax\Module as Ajax;
use 
Elementor\Core\Files\File_Types\Base as File_Type_Base;
use 
Elementor\Core\Files\File_Types\Json;
use 
Elementor\Core\Files\File_Types\Svg;
use 
Elementor\Core\Files\File_Types\Zip;
use 
Elementor\Core\Files\Fonts\Google_Font;
use 
Elementor\Core\Utils\Exceptions;
use 
Elementor\Fonts;
use 
Elementor\User;

if ( ! 
defined'ABSPATH' ) ) {
    exit; 
// Exit if accessed directly.
}

/**
 * Elementor uploads manager.
 *
 * Elementor uploads manager handler class is responsible for handling file uploads that are not done with WP Media.
 *
 * @since 3.3.0
 */
class Uploads_Manager extends Base_Object {

    const 
UNFILTERED_FILE_UPLOADS_KEY 'elementor_unfiltered_files_upload';
    const 
INVALID_FILE_CONTENT 'Invalid Content In File';

    
/**
     * @var File_Type_Base[]
     */
    
private $file_type_handlers = [];

    private 
$allowed_file_extensions;

    
/**
     * @var bool
     */
    
private $is_elementor_upload false;

    
/**
     * @var string
     */
    
private $temp_dir;

    
/**
     * Register File Types
     *
     * To Add a new file type to Elementor, with its own handling logic, you need to add it to the $file_types array here.
     *
     * @since 3.3.0
     * @access public
     */
    
public function register_file_types() {
        
// All file types that have handlers should be included here.
        
$file_types = [
            
'json' => new Json(),
            
'zip' => new Zip(),
            
'svg' => new Svg(),
        ];

        foreach ( 
$file_types as $file_type => $file_handler ) {
            
$this->file_type_handlers$file_type ] = $file_handler;
        }
    }

    
/**
     * Extract and Validate Zip
     *
     * This method accepts a $file array (which minimally should include a 'tmp_name')
     *
     * @since 3.3.0
     * @access public
     *
     * @param string $file_path
     * @param array $allowed_file_types
     * @return array|\WP_Error
     */
    
public function extract_and_validate_zip$file_path$allowed_file_types null ) {
        
$result = [];

        
/** @var Zip $zip_handler - File Type */
        
$zip_handler $this->file_type_handlers['zip'];

        
// Returns an array of file paths.
        
$extracted $zip_handler->extract$file_path$allowed_file_types );

        if ( 
is_wp_error$extracted ) ) {
            return 
$extracted;
        }

        
// If there are no extracted file names, no files passed the extraction validation.
        
if ( empty( $extracted['files'] ) ) {
            
// TODO: Decide what to do if no files passed the extraction validation
            
return new \WP_Error'file_error'self::INVALID_FILE_CONTENT );
        }

        
$result['extraction_directory'] = $extracted['extraction_directory'];

        foreach ( 
$extracted['files'] as $extracted_file_path ) {
            
// Each file is an array with a 'name' (file path) property.
            
if ( ! is_wp_error$this->validate_file( [ 'tmp_name' => $extracted_file_path ] ) ) ) {
                
$result['files'][] = $extracted_file_path;
            }
        }

        return 
$result;
    }

    
/**
     * Handle Elementor Upload
     *
     * This method receives a $file array. If the received file is a Base64 string, the $file array should include a
     * 'fileData' property containing the string, which is decoded and has its contents stored in a temporary file.
     * If the $file parameter passed is a standard $file array, the 'name' and 'tmp_name' properties are used for
     * validation.
     *
     * The file goes through validation; if it passes validation, the file is returned. Otherwise, an error is returned.
     *
     * @since 3.3.0
     * @access public
     *
     * @param array $data
     * @param array $allowed_file_extensions Optional. an array of file types that are allowed to pass validation for each
     * upload.
     * @return array|\WP_Error
     */
    
public function handle_elementor_upload( array $data$allowed_file_extensions null ) {
        
// If $file['fileData'] is set, it signals that the passed file is a Base64 string that needs to be decoded and
        // saved to a temporary file.
        
if ( isset( $data['fileData'] ) ) {
            
$data $this->save_base64_to_tmp_file$data$allowed_file_extensions );
        }

        if ( 
is_wp_error$data ) ) {
            return 
$data;
        }

        
$validation_result $this->validate_file$data$allowed_file_extensions );

        if ( 
is_wp_error$validation_result ) ) {
            return 
$validation_result;
        }

        return 
$data;
    }

    
/**
     * are Unfiltered Uploads Enabled
     *
     * @since 3.5.0
     * @access public
     *
     * @return bool
     */
    
final public static function are_unfiltered_uploads_enabled() {
        
$enabled = ! ! get_optionself::UNFILTERED_FILE_UPLOADS_KEY )
            && 
Svg::file_sanitizer_can_run()
            && 
User::is_current_user_can_upload_json();

        
/**
         * Allow Unfiltered Files Upload.
         *
         * Determines whether to enable unfiltered file uploads.
         *
         * @since 3.0.0
         *
         * @param bool $enabled Whether upload is enabled or not.
         */
        
$enabled apply_filters'elementor/files/allow_unfiltered_upload'$enabled );

        return 
$enabled;
    }

    
/**
     * Handle Elementor WP Media Upload
     *
     * Runs on the 'wp_handle_upload_prefilter' filter.
     *
     * @since 3.2.0
     * @access public
     *
     * @param $file
     * @return mixed
     */
    
public function handle_elementor_wp_media_upload$file ) {
        
// If it isn't a file uploaded by Elementor, we do not intervene.
        
if ( ! $this->is_elementor_wp_media_upload() ) {
            return 
$file;
        }

        
$result $this->validate_file$file );

        if ( 
is_wp_error$result ) ) {
            
$file['error'] = $result->get_error_message();
        }

        return 
$file;
    }

    
/**
     * Get File Type Handler
     *
     * Initialize the proper file type handler according to the file extension
     * and assign it to the file type handlers array.
     *
     * @since 3.3.0
     * @access public
     *
     * @param string|null $file_extension - file extension
     * @return File_Type_Base[]|File_Type_Base
     */
    
public function get_file_type_handlers$file_extension null ) {
        return 
self::get_items$this->file_type_handlers$file_extension );
    }

    
/**
     * Check filetype and ext
     *
     * A workaround for upload validation which relies on a PHP extension (fileinfo)
     * with inconsistent reporting behaviour.
     * ref: https://core.trac.wordpress.org/ticket/39550
     * ref: https://core.trac.wordpress.org/ticket/40175
     *
     * @since 3.5.0
     * @access public
     *
     * @param $data
     * @param $file
     * @param $filename
     * @param $mimes
     *
     * @return mixed
     */
    
public function check_filetype_and_ext$data$file$filename$mimes ) {
        if ( ! empty( 
$data['ext'] ) && ! empty( $data['type'] ) ) {
            return 
$data;
        }

        
$wp_file_type wp_check_filetype$filename$mimes );

        
$file_type_handlers $this->get_file_type_handlers();

        if ( isset( 
$file_type_handlers$wp_file_type['ext'] ] ) ) {
            
$file_type_handler $file_type_handlers$wp_file_type['ext'] ];

            
$data['ext'] = $file_type_handler->get_file_extension();
            
$data['type'] = $file_type_handler->get_mime_type();
        }

        return 
$data;
    }

    
/**
     * Remove File Or Directory
     *
     * Directory is deleted recursively with all of its contents (subdirectories and files).
     *
     * @since 3.3.0
     * @access public
     *
     * @param string $path
     */
    
public function remove_file_or_dir$path ) {
        if ( 
is_dir$path ) ) {
            
$this->remove_directory_with_files$path );
        } elseif ( 
is_file$path ) ) {
            
unlink$path );
        }
    }

    
/**
     * Create Temp File
     *
     * Create a random temporary file.
     *
     * @since 3.3.0
     * @access public
     *
     * @param string $file_content
     * @param string $file_name
     * @return string|\WP_Error
     */
    
public function create_temp_file$file_content$file_name ) {
        
$file_name str_replace' '''sanitize_file_name$file_name ) );

        if ( empty( 
$file_name ) ) {
            return new 
\WP_Error'invalid_file_name'esc_html__'Invalid file name.''elementor' ) );
        }

        
$temp_filename $this->create_unique_dir() . $file_name;

        
/**
         * Temp File Path
         *
         * Allows modifying the full path of the temporary file.
         *
         * @since 3.7.0
         *
         * @param string full path to file
         */
        
$temp_filename apply_filters'elementor/files/temp-file-path'$temp_filename );

        
file_put_contents$temp_filename$file_content ); // phpcs:ignore

        
return $temp_filename;
    }

    
/**
     * Get Temp Directory
     *
     * Get the temporary files directory path. If the directory does not exist, this method creates it.
     *
     * @since 3.3.0
     * @access public
     *
     * @return string $temp_dir
     */
    
public function get_temp_dir() {
        if ( ! 
$this->temp_dir ) {
            
$wp_upload_dir wp_upload_dir();

            
$temp_dir implodeDIRECTORY_SEPARATOR, [ $wp_upload_dir['basedir'], 'elementor''tmp' ] ) . DIRECTORY_SEPARATOR;

            
/**
             * Temp File Path
             *
             * Allows modifying the full path of the temporary file.
             *
             * @since 3.7.0
             *
             * @param string temporary directory
             */
            
$this->temp_dir apply_filters'elementor/files/temp-dir'$temp_dir );

            if ( ! 
is_dir$this->temp_dir ) ) {
                
wp_mkdir_p$this->temp_dir );
            }
        }

        return 
$this->temp_dir;
    }

    
/**
     * Create Unique Temp Dir
     *
     * Create a unique temporary directory
     *
     * @since 3.3.0
     * @access public
     *
     * @return string the new directory path
     */
    
public function create_unique_dir() {
        
$unique_dir_path $this->get_temp_dir() . uniqid() . DIRECTORY_SEPARATOR;

        
wp_mkdir_p$unique_dir_path );

        return 
$unique_dir_path;
    }

    
/**
     * Register Ajax Actions
     *
     * Runs on the 'elementor/ajax/register_actions' hook. Receives the AJAX module as a parameter and registers
     * callbacks for specified action IDs.
     *
     * @since 3.5.0
     * @access public
     *
     * @param Ajax $ajax
     */
    
public function register_ajax_actionsAjax $ajax ) {
        
$ajax->register_ajax_action'enable_unfiltered_files_upload', [ $this'enable_unfiltered_files_upload' ] );
        
$ajax->register_ajax_action'enqueue_google_fonts', [ $this'ajax_enqueue_google_fonts' ] );
    }

    
/**
     * Set Unfiltered Files Upload
     *
     * @since 3.5.0
     * @access public
     */
    
public function enable_unfiltered_files_upload() {
        if ( ! 
current_user_can'manage_options' ) ) {
            return;
        }

        
update_optionself::UNFILTERED_FILE_UPLOADS_KEY);
    }

    public function 
ajax_enqueue_google_fonts$data ): bool {
        if ( empty( 
$data['font_name'] ) ) {
            return 
false;
        }

        
$font_type Fonts::get_font_type$data['font_name'] );

        if ( 
Fonts::GOOGLE !== $font_type ) {
            return 
false;
        }

        
Google_Font::enqueue$data['font_name'] );

        return 
true;
    }

    
/**
     * Support Unfiltered File Uploads
     *
     * When uploading a file within Elementor, this method adds the registered
     * file types to WordPress' allowed mimes list. This will only happen if the user allowed unfiltered file uploads
     * in Elementor's settings in the admin dashboard.
     *
     * @since 3.5.0
     * @access public
     *
     * @param array $allowed_mimes
     * @return array allowed mime types
     */
    
final public function support_unfiltered_elementor_file_uploads$allowed_mimes ) {
        if ( 
$this->is_elementor_upload() && $this->are_unfiltered_uploads_enabled() ) {
            foreach ( 
$this->file_type_handlers as $file_type_handler ) {
                
$allowed_mimes$file_type_handler->get_file_extension() ] = $file_type_handler->get_mime_type();
            }
        }

        return 
$allowed_mimes;
    }

    
/**
     * Set Elementor Upload State
     *
     * @since 3.5.0
     * @access public
     *
     * @param $state
     */
    
public function set_elementor_upload_state$state ) {
        
$this->is_elementor_upload $state;
    }

    
/**
     * Is Elementor Upload
     *
     * This method checks if the current session includes a request to upload files made via Elementor.
     *
     * @since 3.5.0
     * @access private
     *
     * @return bool
     */
    
private function is_elementor_upload() {
        return 
$this->is_elementor_upload || $this->is_elementor_media_upload() || $this->is_elementor_wp_media_upload();
    }

    
/**
     * Is Elementor Media Upload
     *
     * Checks whether the current request includes uploading files via Elementor which are not destined for the Media
     * Library.
     *
     * @since 3.5.0
     * @access public
     *
     * @return bool
     */
    
public function is_elementor_media_upload() {
        
// Sometimes `uploadTypeCaller` passed as a GET parameter when using the WP Media Library REST API, where the
        // whole request body is occupied by the uploaded file.
        
return isset( $_REQUEST['uploadTypeCaller'] ) && 'elementor-media-upload' === $_REQUEST['uploadTypeCaller']; // phpcs:ignore
    
}

    
/**
     * Is Elementor WP Media Upload
     *
     * Checks whether the current request is a request to upload files into the WP Media Library via Elementor.
     *
     * @since 3.3.0
     * @access private
     *
     * @return bool
     */
    
private function is_elementor_wp_media_upload() {
        return isset( 
$_REQUEST['uploadTypeCaller'] ) && 'elementor-wp-media-upload' === $_REQUEST['uploadTypeCaller']; // phpcs:ignore
    
}

    
/**
     * Add File Extension To Allowed Extensions List
     *
     * @since 3.3.0
     * @access private
     *
     * @param string $file_type
     */
    
private function add_file_extension_to_allowed_extensions_list$file_type ) {
        
$file_handler $this->file_type_handlers$file_type ];

        
$file_extension $file_handler->get_file_extension();

        
// Only add the file extension to the list if it doesn't already exist in it.
        
if ( ! in_array$file_extension$this->allowed_file_extensionstrue ) ) {
            
$this->allowed_file_extensions[] = $file_extension;
        }
    }

    
/**
     * Save Base64 as File
     *
     * Saves a Base64 string as a .tmp file in Elementor's temporary files directory.
     *
     * @since 3.3.0
     * @access private
     *
     * @param $file
     * @param array|null $allowed_file_extensions
     *
     * @return array|\WP_Error
     */
    
private function save_base64_to_tmp_file$file$allowed_file_extensions null ) {
        if ( empty( 
$file['fileName'] ) || empty( $file['fileData'] ) ) {
            return new 
\WP_Error'file_error'self::INVALID_FILE_CONTENT );
        }

        
$file_extension pathinfo$file['fileName'], PATHINFO_EXTENSION );
        
$is_file_type_allowed $this->is_file_type_allowed$file_extension$allowed_file_extensions );

        if ( 
is_wp_error$is_file_type_allowed ) ) {
            return 
$is_file_type_allowed;
        }

        
$file_content base64_decode$file['fileData'] ); // phpcs:ignore

        // If the decode fails
        
if ( ! $file_content ) {
            return new 
\WP_Error'file_error'self::INVALID_FILE_CONTENT );
        }

        
$temp_filename $this->create_temp_file$file_content$file['fileName'] );

        if ( 
is_wp_error$temp_filename ) ) {
            return 
$temp_filename;
        }

        return [
            
// the original uploaded file name
            
'name' => $file['fileName'],
            
// The path to the temporary file
            
'tmp_name' => $temp_filename,
        ];
    }

    
/**
     * Validate File
     *
     * @since 3.3.0
     * @access private
     *
     * @param array $file
     * @param array $file_extensions Optional
     * @return bool|\WP_Error
     */
    
private function validate_file( array $file$file_extensions = [] ) {
        
$uploaded_file_name = isset( $file['name'] ) ? $file['name'] : $file['tmp_name'];

        
$file_extension pathinfo$uploaded_file_namePATHINFO_EXTENSION );

        if ( ! 
$this->is_elementor_wp_media_upload() ) {
            
$is_file_type_allowed $this->is_file_type_allowed$file_extension$file_extensions );

            if ( 
is_wp_error$is_file_type_allowed ) ) {
                return 
$is_file_type_allowed;
            }
        }

        
$file_type_handler $this->get_file_type_handlers$file_extension );

        
// If Elementor does not have a handler for this file type, don't block it.
        
if ( ! $file_type_handler ) {
            return 
true;
        }

        
// If there is a File Type Handler for the uploaded file, it means it is a non-standard file type. In this case,
        // we check if unfiltered file uploads are enabled or not before allowing it.
        
if ( ! self::are_unfiltered_uploads_enabled() ) {
            
$error 'json' === $file_extension
                
esc_html__'You do not have permission to upload JSON files.''elementor' )
                : 
esc_html__'This file is not allowed for security reasons.''elementor' );
            return new 
\WP_ErrorExceptions::FORBIDDEN$error );
        }

        
// Here is each file type handler's chance to run its own specific validations
        
return $file_type_handler->validate_file$file );
    }

    
/**
     * Is File Type Allowed
     *
     * Checks whether the passed file extension is allowed for upload.
     *
     * @since 3.5.0
     * @access private
     *
     * @param $file_extension
     * @param $filtered_file_extensions
     * @return bool|\WP_Error
     */
    
private function is_file_type_allowed$file_extension$filtered_file_extensions ) {
        
$allowed_file_extensions $this->get_allowed_file_extensions();

        if ( 
$filtered_file_extensions ) {
            
$allowed_file_extensions array_intersect$allowed_file_extensions$filtered_file_extensions );
        }

        
$is_allowed false;

        
// Check if the file type (extension) is in the allowed extensions list. If it is a non-standard file type (not
        // enabled by default in WordPress) and unfiltered file uploads are not enabled, it will not be in the allowed
        // file extensions list.
        
foreach ( $allowed_file_extensions as $allowed_extension ) {
            if ( 
preg_match'/' $allowed_extension '/'$file_extension ) ) {
                
$is_allowed true;

                break;
            }
        }

        if ( ! 
$is_allowed ) {
            
$is_allowed = new \WP_ErrorExceptions::FORBIDDEN'Uploading this file type is not allowed.' );
        }

        
/**
         * Elementor File Type Allowed
         *
         * Allows setting file types
         *
         * @since 3.5.0
         *
         * @param bool|\WP_Error $is_allowed
         */
        
return apply_filters'elementor/files/allow-file-type/' $file_extension$is_allowed );
    }

    
/**
     * Remove Directory with Files
     *
     * @since 3.3.0
     * @access private
     *
     * @param string $dir
     * @return bool
     */
    
private function remove_directory_with_files$dir ) {
        
$dir_iterator = new \RecursiveDirectoryIterator$dir\RecursiveDirectoryIterator::SKIP_DOTS );

        foreach ( new 
\RecursiveIteratorIterator$dir_iterator\RecursiveIteratorIterator::CHILD_FIRST ) as $name => $item ) {
            if ( 
is_dir$name ) ) {
                
rmdir$name );
            } elseif ( 
is_file$name ) ) {
                
unlink$name );
            }
        }

        return 
rmdir$dir );
    }

    
/**
     * Get Allowed File Extensions
     *
     * Retrieve an array containing the list of file extensions allowed for upload.
     *
     * @since 3.3.0
     * @access private
     *
     * @return array file extension/s
     */
    
private function get_allowed_file_extensions() {
        if ( ! 
$this->allowed_file_extensions ) {
            
$this->allowed_file_extensions array_keysget_allowed_mime_types() );

            foreach ( 
$this->get_file_type_handlers() as $file_type => $handler ) {
                if ( 
$handler->is_upload_allowed() ) {
                    
// Add the file extension to the allowed extensions list only if unfiltered files upload is enabled.
                    
$this->add_file_extension_to_allowed_extensions_list$file_type );
                }
            }
        }

        return 
$this->allowed_file_extensions;
    }

    public function 
__construct() {
        
$this->register_file_types();

        
add_filter'upload_mimes', [ $this'support_unfiltered_elementor_file_uploads' ] );
        
add_filter'wp_handle_upload_prefilter', [ $this'handle_elementor_wp_media_upload' ] );
        
add_filter'wp_check_filetype_and_ext', [ $this'check_filetype_and_ext' ], 10);

        
// Ajax.
        
add_action'elementor/ajax/register_actions', [ $this'register_ajax_actions' ] );
    }
}