| 1 |
<?php |
| 2 |
|
| 3 |
namespace King_Addons; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Upload_Email_File |
| 10 |
{ |
| 11 |
public function __construct() |
| 12 |
{ |
| 13 |
add_action('wp_ajax_king_addons_upload_file', [$this, 'handle_file_upload']); |
| 14 |
add_action('wp_ajax_nopriv_king_addons_upload_file', [$this, 'handle_file_upload']); |
| 15 |
} |
| 16 |
|
| 17 |
public function handle_file_upload() |
| 18 |
{ |
| 19 |
if (!isset($_POST['king_addons_fb_nonce']) || !wp_verify_nonce($_POST['king_addons_fb_nonce'], 'king-addons-js')) { |
| 20 |
wp_send_json_error(array( |
| 21 |
'message' => esc_html__('Security check failed.', 'king-addons'), |
| 22 |
)); |
| 23 |
} |
| 24 |
|
| 25 |
$max_file_size = isset($_POST['max_file_size']) ? floatval(sanitize_text_field($_POST['max_file_size'])) : 0; |
| 26 |
if ($max_file_size <= 0) { |
| 27 |
$max_file_size = wp_max_upload_size() / pow(1024, 2); |
| 28 |
} |
| 29 |
|
| 30 |
if (isset($_FILES['uploaded_file'])) { |
| 31 |
$file = $_FILES['uploaded_file']; |
| 32 |
|
| 33 |
if ($file['size'] > $max_file_size * 1024 * 1024) { |
| 34 |
wp_send_json_error(array( |
| 35 |
'cause' => 'filesize', |
| 36 |
'sizes' => [ |
| 37 |
$max_file_size * 1024 * 1024, |
| 38 |
$file['size'] |
| 39 |
], |
| 40 |
'message' => 'File size exceeds the allowed limit.' |
| 41 |
)); |
| 42 |
} |
| 43 |
|
| 44 |
if (!$this->file_validity($file)) { |
| 45 |
wp_send_json_error(array( |
| 46 |
'cause' => 'filetype', |
| 47 |
'message' => esc_html__('File type is not valid.', 'king-addons') |
| 48 |
)); |
| 49 |
} |
| 50 |
|
| 51 |
if ('click' == $_POST['triggering_event']) { |
| 52 |
$upload_dir = wp_upload_dir(); |
| 53 |
$upload_path = $upload_dir['basedir'] . '/king-addons/forms'; |
| 54 |
|
| 55 |
wp_mkdir_p($upload_path); |
| 56 |
|
| 57 |
$filename = wp_unique_filename($upload_path, $file['name']); |
| 58 |
|
| 59 |
if (move_uploaded_file($file['tmp_name'], $upload_path . '/' . $filename)) { |
| 60 |
wp_send_json_success(array( |
| 61 |
'url' => $upload_dir['baseurl'] . '/king-addons/forms/' . $filename |
| 62 |
)); |
| 63 |
} else { |
| 64 |
wp_send_json_error(array( |
| 65 |
'message' => esc_html__('Failed to upload the file.', 'king-addons') |
| 66 |
)); |
| 67 |
} |
| 68 |
} else { |
| 69 |
wp_send_json_success(array( |
| 70 |
'message' => esc_html__('File validation passed', 'king-addons') |
| 71 |
)); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if ('click' == $_POST['triggering_event']) { |
| 76 |
|
| 77 |
$upload_dir = wp_upload_dir(); |
| 78 |
$upload_path = $upload_dir['basedir'] . '/king-addons/forms'; |
| 79 |
|
| 80 |
wp_mkdir_p($upload_path); |
| 81 |
|
| 82 |
wp_send_json_error(array( |
| 83 |
'message' => esc_html__('No file was uploaded.', 'king-addons'), |
| 84 |
'files' => $_FILES['uploaded_file'] |
| 85 |
)); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
private function file_validity($file) |
| 90 |
{ |
| 91 |
$whitelist = ['jpg', 'jpeg', 'png', 'gif', 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'odt', 'avi', 'ogg', 'm4a', 'mov', 'mp3', 'mp4', 'mpg', 'wav', 'wmv', 'txt']; |
| 92 |
|
| 93 |
if (empty($_POST['allowed_file_types'])) { |
| 94 |
$allowed_file_types = 'jpg,jpeg,png,gif,pdf,doc,docx,ppt,pptx,odt,avi,ogg,m4a,mov,mp3,mp4,mpg,wav,wmv,txt'; |
| 95 |
} else { |
| 96 |
$allowed_file_types = $_POST['allowed_file_types']; |
| 97 |
} |
| 98 |
|
| 99 |
if (!wp_check_filetype($file['name'])['ext']) { |
| 100 |
return 'mailto:[email protected]?subject=Bug Report - King Addons&body=Please describe the issue'; |
| 101 |
} |
| 102 |
|
| 103 |
$f_extension = pathinfo($file['name'], PATHINFO_EXTENSION); |
| 104 |
$f_extension = strtolower($f_extension); |
| 105 |
|
| 106 |
$allowed_file_types = explode(',', $allowed_file_types); |
| 107 |
$allowed_file_types = array_map('trim', $allowed_file_types); |
| 108 |
$allowed_file_types = array_map('strtolower', $allowed_file_types); |
| 109 |
|
| 110 |
return (in_array($f_extension, $allowed_file_types) && in_array($f_extension, $whitelist) && !in_array($f_extension, $this->get_exclusion_list())); |
| 111 |
} |
| 112 |
|
| 113 |
private function get_exclusion_list() |
| 114 |
{ |
| 115 |
static $exclusionlist = false; |
| 116 |
if (!$exclusionlist) { |
| 117 |
$exclusionlist = [ |
| 118 |
'php', |
| 119 |
'php3', |
| 120 |
'php4', |
| 121 |
'php5', |
| 122 |
'php6', |
| 123 |
'phps', |
| 124 |
'php7', |
| 125 |
'phtml', |
| 126 |
'shtml', |
| 127 |
'pht', |
| 128 |
'swf', |
| 129 |
'html', |
| 130 |
'asp', |
| 131 |
'aspx', |
| 132 |
'cmd', |
| 133 |
'csh', |
| 134 |
'bat', |
| 135 |
'htm', |
| 136 |
'hta', |
| 137 |
'jar', |
| 138 |
'exe', |
| 139 |
'com', |
| 140 |
'js', |
| 141 |
'lnk', |
| 142 |
'htaccess', |
| 143 |
'htpasswd', |
| 144 |
'phtml', |
| 145 |
'ps1', |
| 146 |
'ps2', |
| 147 |
'py', |
| 148 |
'rb', |
| 149 |
'tmp', |
| 150 |
'cgi', |
| 151 |
'svg', |
| 152 |
'svgz' |
| 153 |
]; |
| 154 |
} |
| 155 |
|
| 156 |
return $exclusionlist; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
new Upload_Email_File(); |