| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP_QQ2_File_Uploader |
| 4 |
* |
| 5 |
* DO NOT TOUCH - part of http://github.com/valums/file-uploader ! (@see js/fileuploader.js) |
| 6 |
* |
| 7 |
* @package MainWP/Dashboard |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace MainWP\Dashboard; |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Class MainWP_QQ2_File_Uploader |
| 19 |
* |
| 20 |
* @package MainWP\Dashboard |
| 21 |
*/ |
| 22 |
class MainWP_QQ2_File_Uploader { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. |
| 23 |
|
| 24 |
/** |
| 25 |
* Private variable to hold allowed file extensions. |
| 26 |
* |
| 27 |
* @var array Allowed extension. |
| 28 |
*/ |
| 29 |
private $allowedExtensions = array(); |
| 30 |
|
| 31 |
/** |
| 32 |
* Private variable to hold allowed file size. |
| 33 |
* |
| 34 |
* @var int Size limit. |
| 35 |
*/ |
| 36 |
private $sizeLimit = 8388608; |
| 37 |
|
| 38 |
/** |
| 39 |
* Private variable to hold the file to upload. |
| 40 |
* |
| 41 |
* @var mixed The file. |
| 42 |
*/ |
| 43 |
private $file; |
| 44 |
|
| 45 |
/** |
| 46 |
* MainWP_QQ2_File_Uploader constructor. |
| 47 |
* |
| 48 |
* Run each time the class is called. |
| 49 |
* |
| 50 |
* @param array $allowedExtensions Array of allowed Extensions. |
| 51 |
* @param int $sizeLimit Maximum allowed file size. |
| 52 |
* |
| 53 |
* @uses \MainWP\Dashboard\MainWP_QQ2_Uploaded_File_Form |
| 54 |
* @uses \MainWP\Dashboard\MainWP_QQ2_Uploaded_File_Xhr |
| 55 |
*/ |
| 56 |
public function __construct( array $allowedExtensions = array(), $sizeLimit = 8388608 ) { |
| 57 |
$allowedExtensions = array_map( 'strtolower', $allowedExtensions ); |
| 58 |
|
| 59 |
$this->allowedExtensions = $allowedExtensions; |
| 60 |
|
| 61 |
/** |
| 62 |
* Filter: 'mainwp_file_uploader_size_limit' |
| 63 |
* |
| 64 |
* Filters the maximum upload file size. Default: 8388608 Bytes (B) = 8 Megabytes (MB) |
| 65 |
* |
| 66 |
* @since 4.1 |
| 67 |
*/ |
| 68 |
$this->sizeLimit = apply_filters( 'mainwp_file_uploader_size_limit', $sizeLimit ); |
| 69 |
|
| 70 |
if ( isset( $_GET['qqfile'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 71 |
$this->file = new MainWP_QQ2_Uploaded_File_Xhr(); |
| 72 |
} elseif ( isset( $_FILES['qqfile'] ) && isset( $_REQUEST['dzuuid'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized --- do not use dzChunkIndex. |
| 73 |
$this->file = new MainWP_File_Uploader_Handle(); |
| 74 |
} elseif ( isset( $_FILES['qqfile'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 75 |
$this->file = new MainWP_QQ2_Uploaded_File_Form(); |
| 76 |
} else { |
| 77 |
$this->file = false; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Convert file size into bytes. |
| 83 |
* |
| 84 |
* @param int $str Original File. |
| 85 |
* |
| 86 |
* @return int $val File Size in Bytes. |
| 87 |
*/ |
| 88 |
private function to_bytes( $str ) { |
| 89 |
$val = trim( $str ); |
| 90 |
$last = strtolower( $str[ strlen( $str ) - 1 ] ); |
| 91 |
switch ( $last ) { |
| 92 |
case 'g': |
| 93 |
$val = substr( $str, 0, strlen( $str ) - 1 ) * 1024 * 1024 * 1024; |
| 94 |
break; |
| 95 |
case 'm': |
| 96 |
$val = substr( $str, 0, strlen( $str ) - 1 ) * 1024 * 1024; |
| 97 |
break; |
| 98 |
case 'k': |
| 99 |
$val = substr( $str, 0, strlen( $str ) - 1 ) * 1024; |
| 100 |
break; |
| 101 |
default: |
| 102 |
break; |
| 103 |
} |
| 104 |
|
| 105 |
return $val; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Handle the file upload. |
| 110 |
* |
| 111 |
* @param mixed $uploadDirectory File Upload directory. |
| 112 |
* @param bool $replaceOldFile True|False Weather or not to replace the orignal file or not. |
| 113 |
* |
| 114 |
* @return array success'=>true|error'=>'error message' |
| 115 |
*/ |
| 116 |
public function handle_upload( $uploadDirectory, $replaceOldFile = false ) { // phpcs:ignore --NOSONAR -complex. |
| 117 |
|
| 118 |
if ( ! $this->file ) { |
| 119 |
return array( 'error' => 'No files were uploaded!' ); |
| 120 |
} |
| 121 |
|
| 122 |
$size = $this->file->get_size(); |
| 123 |
|
| 124 |
if ( empty( $size ) ) { |
| 125 |
return array( 'error' => 'File is empty!' ); |
| 126 |
} |
| 127 |
|
| 128 |
$postSize = $this->to_bytes( ini_get( 'post_max_size' ) ); |
| 129 |
$uploadSize = $this->to_bytes( ini_get( 'upload_max_filesize' ) ); |
| 130 |
if ( $postSize < $size || $uploadSize < $size ) { |
| 131 |
return array( |
| 132 |
'error' => esc_html__( 'File is too large, increase post_max_size and/or upload_max_filesize', 'mainwp' ), |
| 133 |
'size' => esc_html( $size ), |
| 134 |
); |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
$pathinfo = pathinfo( $this->file->get_name() ); |
| 139 |
$filename = $pathinfo['filename']; |
| 140 |
$ext = $pathinfo['extension']; |
| 141 |
|
| 142 |
if ( $this->allowedExtensions && ! in_array( strtolower( $ext ), $this->allowedExtensions ) ) { |
| 143 |
$these = implode( ', ', $this->allowedExtensions ); |
| 144 |
|
| 145 |
return array( 'error' => esc_html__( 'File has an invalid extension, it should be one of ', 'mainwp' ) . $these . '.' ); |
| 146 |
} |
| 147 |
|
| 148 |
if ( ! $replaceOldFile ) { |
| 149 |
// don't overwrite previous files that were uploaded. |
| 150 |
while ( file_exists( $uploadDirectory . $filename . '.' . $ext ) ) { |
| 151 |
$filename .= wp_rand( 10, 99 ); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
try { |
| 156 |
// phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 157 |
$success = false; |
| 158 |
$tmp_name = ''; |
| 159 |
if ( isset( $_FILES['qqfile'] ) ) { |
| 160 |
// Multipart upload: validate before saving. |
| 161 |
if ( isset( $_FILES['qqfile']['error'] ) && isset( $_FILES['qqfile']['tmp_name'] ) && UPLOAD_ERR_OK === $_FILES['qqfile']['error'] && is_uploaded_file( $_FILES['qqfile']['tmp_name'] ) && $this->file->save( $uploadDirectory . $filename . '.' . $ext ) ) { |
| 162 |
$success = true; |
| 163 |
$tmp_name = $_FILES['qqfile']['tmp_name']; |
| 164 |
} |
| 165 |
} elseif ( $this->file->save( $uploadDirectory . $filename . '.' . $ext ) ) { |
| 166 |
$success = true; |
| 167 |
} |
| 168 |
// phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 169 |
|
| 170 |
if ( $success ) { |
| 171 |
return array( |
| 172 |
'success' => true, |
| 173 |
'path' => $uploadDirectory . $filename . '.' . $ext, |
| 174 |
'tmp' => $tmp_name, |
| 175 |
); |
| 176 |
} |
| 177 |
return array( |
| 178 |
'error' => esc_html__( 'Could not save uploaded file!', 'mainwp' ) . ' ' . |
| 179 |
esc_html__( 'The upload was cancelled, or server error encountered.', 'mainwp' ), |
| 180 |
); |
| 181 |
} catch ( \Exception $e ) { |
| 182 |
return array( 'error' => $e->getMessage() ); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Wrap of the method handle_upload() for compatible. |
| 188 |
* |
| 189 |
* @param mixed $uploadDirectory File Upload directory. |
| 190 |
* @param bool $replaceOldFile True|False Weather or not to replace the orignal file or not. |
| 191 |
* |
| 192 |
* @return array success'=>true|error'=>'error message' |
| 193 |
*/ |
| 194 |
public function handleUpload( $uploadDirectory, $replaceOldFile = false ) { // phpcs:ignore -- for compatible |
| 195 |
return $this->handle_upload( $uploadDirectory, $replaceOldFile ); |
| 196 |
} |
| 197 |
} |
| 198 |
|