| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP_File_Uploader_Handle |
| 4 |
* |
| 5 |
* Handle file uploads via regular form post (uses the $_FILES array) |
| 6 |
* |
| 7 |
* @see dropzone.js |
| 8 |
* |
| 9 |
* @package MainWP/Dashboard |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace MainWP\Dashboard; |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Class MainWP_File_Uploader_Handle |
| 21 |
* |
| 22 |
* @package MainWP\Dashboard |
| 23 |
*/ |
| 24 |
class MainWP_File_Uploader_Handle { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. |
| 25 |
|
| 26 |
/** |
| 27 |
* Save the file to the specified path. |
| 28 |
* |
| 29 |
* @param string $path Path to save file to. |
| 30 |
* @throws \MainWP_Exception Exception object. |
| 31 |
* |
| 32 |
* @return boolean TRUE on success|false on failure. |
| 33 |
* |
| 34 |
* @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system() |
| 35 |
* @uses \MainWP\Dashboard\MainWP_System_Utility::get_base_dir() |
| 36 |
*/ |
| 37 |
public function save( $path ) { |
| 38 |
$moved = false; |
| 39 |
$tmp_name = isset( $_FILES['qqfile']['tmp_name'] ) && MainWP_Utility::valid_file_check( $_FILES['qqfile']['tmp_name'] ) ? $_FILES['qqfile']['tmp_name'] : ''; //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- verify in caller. |
| 40 |
|
| 41 |
//phpcs:disable WordPress.WP.AlternativeFunctions -- custom process. |
| 42 |
if ( ! is_dir( dirname( $path ) ) ) { |
| 43 |
mkdir( dirname( $path ), 0750, true ); // MWP-1558: tightened from 0777. |
| 44 |
} |
| 45 |
|
| 46 |
if ( empty( $_REQUEST['dzchunkbyteoffset'] ) && file_exists( $path ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- verified. |
| 47 |
unlink( $path ); // first chuck, delete file if existed, to create new file. |
| 48 |
} |
| 49 |
|
| 50 |
// to append to file, wp_filesystem put_contents does not support. |
| 51 |
$str = file_get_contents( $tmp_name ); |
| 52 |
|
| 53 |
$moved = file_put_contents( $path, $str, FILE_APPEND ); |
| 54 |
|
| 55 |
if ( ! file_exists( $path ) ) { |
| 56 |
throw new MainWP_Exception( 'Unable to save the file to the MainWP upload directory, please check your system configuration.' ); |
| 57 |
} |
| 58 |
|
| 59 |
//phpcs:enable WordPress.WP.AlternativeFunctions |
| 60 |
|
| 61 |
if ( ! $moved ) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
/** Get the File Name. */ |
| 69 |
public function get_name() { |
| 70 |
return isset( $_FILES['qqfile']['name'] ) ? $_FILES['qqfile']['name'] : ''; //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- global variable. |
| 71 |
} |
| 72 |
|
| 73 |
/** Get the File Size. */ |
| 74 |
public function get_size() { |
| 75 |
return isset( $_FILES['qqfile']['size'] ) ? $_FILES['qqfile']['size'] : ''; //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- global variable. |
| 76 |
} |
| 77 |
} |
| 78 |
|