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