| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Filesystem; |
| 4 |
|
| 5 |
use ImportWP\Common\Http\Http; |
| 6 |
use ImportWP\Common\Util\Logger; |
| 7 |
use ImportWP\Common\Util\Singleton; |
| 8 |
use ImportWP\Container; |
| 9 |
use ImportWP\EventHandler; |
| 10 |
|
| 11 |
class Filesystem |
| 12 |
{ |
| 13 |
use Singleton; |
| 14 |
|
| 15 |
/** |
| 16 |
* @var ImportWP\Container\Container |
| 17 |
*/ |
| 18 |
private $container; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var EventHandler $event_handler |
| 22 |
*/ |
| 23 |
private $event_handler; |
| 24 |
|
| 25 |
public function __construct(EventHandler $event_handler) |
| 26 |
{ |
| 27 |
$this->event_handler = $event_handler; |
| 28 |
$this->container = Container::getInstance(); |
| 29 |
} |
| 30 |
|
| 31 |
public function copy($source, $destination, $allowed_mimes = null) |
| 32 |
{ |
| 33 |
|
| 34 |
if (!file_exists($source)) { |
| 35 |
Logger::write('File doesn`t exist on local filesystem: ' . $source); |
| 36 |
return new \WP_Error('IWP_FS_7', 'File doesn`t exist on local filesystem.'); |
| 37 |
} |
| 38 |
|
| 39 |
$filetype = $this->get_filetype($source); |
| 40 |
|
| 41 |
if (!is_null($allowed_mimes)) { |
| 42 |
|
| 43 |
if (!$filetype) { |
| 44 |
Logger::write('Unable to determine filetype: ' . $source); |
| 45 |
return new \WP_Error('IWP_FS_6', 'Unable to determine filetype.'); |
| 46 |
} |
| 47 |
|
| 48 |
if (!in_array($filetype, $allowed_mimes, true)) { |
| 49 |
Logger::write('Invalid filetype: ' . $filetype . ', allowed(' . implode(', ', $allowed_mimes) . ')'); |
| 50 |
return new \WP_Error('IWP_FS_4', 'Invalid filetype.'); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
if (!copy($source, $destination)) { |
| 55 |
return new \WP_Error('IWP_FS_4', 'Unable to copy file: ' . $source . '.'); |
| 56 |
} |
| 57 |
|
| 58 |
$type = $this->get_file_mime($source); |
| 59 |
Logger::write('Copied file: ' . $destination . ' -type=' . $filetype . ' -mime=' . $type); |
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
public function upload_file($attachment, $allowed_mimes = null) |
| 64 |
{ |
| 65 |
// use built in wordpress file upload |
| 66 |
if (!function_exists('wp_handle_upload')) { |
| 67 |
require_once ABSPATH . "wp-admin" . '/includes/file.php'; |
| 68 |
} |
| 69 |
|
| 70 |
$uploaded_file = wp_handle_upload($attachment, ['test_form' => false, 'test_type' => false]); |
| 71 |
|
| 72 |
if (isset($uploaded_file['error'])) { |
| 73 |
Logger::write($uploaded_file['error']); |
| 74 |
return new \WP_Error('IWP_FS_UF', $uploaded_file['error']); |
| 75 |
} |
| 76 |
|
| 77 |
$file = $uploaded_file['file']; |
| 78 |
$type = $uploaded_file['type']; |
| 79 |
|
| 80 |
$filetype = $this->check_mime_header($uploaded_file['type']); |
| 81 |
|
| 82 |
// if header doesnt match check for file extension. |
| 83 |
if (!$filetype) { |
| 84 |
$filetype = $this->get_filetype_from_ext($attachment['name']); |
| 85 |
} |
| 86 |
|
| 87 |
// determine file type from mimetype. |
| 88 |
if (!is_null($allowed_mimes) && !in_array($filetype, $allowed_mimes, true)) { |
| 89 |
Logger::write('Invalid filetype: ' . $filetype . ', allowed(' . implode(', ', $allowed_mimes) . ')'); |
| 90 |
return new \WP_Error('IWP_FS_4', 'Invalid filetype.'); |
| 91 |
} |
| 92 |
|
| 93 |
Logger::write('Uploaded file: ' . $file . ' -type=' . $filetype . ' -mime=' . $type); |
| 94 |
|
| 95 |
return array( |
| 96 |
'dest' => $file, |
| 97 |
'type' => $filetype, |
| 98 |
'mime' => $type |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
public function download_file($remote_url, $filetype = null, $allowed_mimes = null, $override_filename = null, $prefix = '') |
| 103 |
{ |
| 104 |
$remote_url_temp = strtok($remote_url, '?'); |
| 105 |
|
| 106 |
if (!is_null($allowed_mimes)) { |
| 107 |
if (is_null($filetype)) { |
| 108 |
$filetype = $this->get_filetype_from_ext($remote_url_temp); |
| 109 |
} |
| 110 |
if (!in_array($filetype, $allowed_mimes, true)) { |
| 111 |
Logger::write('Invalid filetype: ' . $filetype . ', allowed(' . implode(', ', $allowed_mimes) . ')'); |
| 112 |
return new \WP_Error('IWP_FS_4', 'Invalid filetype.'); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
$wp_upload_dir = wp_upload_dir(); |
| 117 |
$filename = !empty($override_filename) ? $override_filename : $prefix . basename($remote_url_temp); |
| 118 |
|
| 119 |
// force extension of file if it doesnt match |
| 120 |
if ($filetype === 'xml') { |
| 121 |
if (preg_match('/\.(xml|zip|gz)$/', $filename) === 0) { |
| 122 |
$filename .= '.xml'; |
| 123 |
} |
| 124 |
} elseif ($filetype === 'csv') { |
| 125 |
if (preg_match('/\.(csv|zip|gz)$/', $filename) === 0) { |
| 126 |
$filename .= '.csv'; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
$dest = wp_unique_filename($wp_upload_dir['path'], $filename); |
| 131 |
$wp_dest = $wp_upload_dir['path'] . '/' . $dest; |
| 132 |
touch($wp_dest); |
| 133 |
|
| 134 |
/** |
| 135 |
* @var Http $http |
| 136 |
*/ |
| 137 |
$http = Container::getInstance()->get('http'); |
| 138 |
|
| 139 |
$headers = []; |
| 140 |
if ($filetype === 'xml') { |
| 141 |
$headers['Content-Type'] = 'text/xml'; |
| 142 |
$headers['Accept'] = 'text/xml'; |
| 143 |
} elseif ($filetype === 'csv') { |
| 144 |
$headers['Content-Type'] = 'text/csv'; |
| 145 |
$headers['Accept'] = 'text/csv'; |
| 146 |
} |
| 147 |
|
| 148 |
$result = $http->download_file_stream($remote_url, $wp_dest, $headers); |
| 149 |
if (is_wp_error($result)) { |
| 150 |
@unlink($wp_dest); |
| 151 |
Logger::write($result->get_error_message()); |
| 152 |
return $result; |
| 153 |
} |
| 154 |
|
| 155 |
if (is_string($result)) { |
| 156 |
$filename = !empty($override_filename) ? $override_filename : $prefix . basename($result); |
| 157 |
$dest = wp_unique_filename($wp_upload_dir['path'], $filename); |
| 158 |
$wp_tmp_dest = $wp_upload_dir['path'] . '/' . $dest; |
| 159 |
|
| 160 |
if (copy($wp_dest, $wp_tmp_dest)) { |
| 161 |
Logger::write('Rename file: ' . $wp_dest . ' -output=' . $wp_tmp_dest); |
| 162 |
unlink($wp_dest); |
| 163 |
$wp_dest = $wp_tmp_dest; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
$exists = $this->file_exists($wp_dest); |
| 168 |
if (is_wp_error($exists)) { |
| 169 |
return $exists; |
| 170 |
} |
| 171 |
|
| 172 |
$type = $this->get_file_mime($wp_dest); |
| 173 |
Logger::write('Downloaded file: ' . $wp_dest . ' -type=' . $filetype . ' -mime=' . $type); |
| 174 |
|
| 175 |
return array( |
| 176 |
'dest' => $wp_dest, |
| 177 |
'type' => $filetype, |
| 178 |
'mime' => $type |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
public function file_exists($src) |
| 183 |
{ |
| 184 |
try { |
| 185 |
if (!file_exists($src)) { |
| 186 |
throw new \Exception("File not found: " . $src); |
| 187 |
} |
| 188 |
|
| 189 |
$size = filesize($src); |
| 190 |
if ($size == 0) { |
| 191 |
unlink($src); |
| 192 |
throw new \Exception("File not found or empty: " . $src); |
| 193 |
} |
| 194 |
} catch (\Exception $e) { |
| 195 |
return new \WP_Error('IWP_FS_8', $e->getMessage()); |
| 196 |
} |
| 197 |
|
| 198 |
return true; |
| 199 |
} |
| 200 |
|
| 201 |
public function copy_file($remote_url, $allowed_mimes = null, $override_filename = null, $prefix = '') |
| 202 |
{ |
| 203 |
$remote_url = strtok($remote_url, '?'); |
| 204 |
|
| 205 |
$wp_upload_dir = wp_upload_dir(); |
| 206 |
|
| 207 |
$filename = !empty($override_filename) ? $override_filename : $prefix . basename($remote_url); |
| 208 |
$dest = wp_unique_filename($wp_upload_dir['path'], $filename); |
| 209 |
$wp_dest = $wp_upload_dir['path'] . '/' . $dest; |
| 210 |
|
| 211 |
$result = $this->copy($remote_url, $wp_dest, $allowed_mimes); |
| 212 |
if (is_wp_error($result)) { |
| 213 |
return $result; |
| 214 |
} |
| 215 |
|
| 216 |
return array( |
| 217 |
'dest' => $wp_dest, |
| 218 |
'type' => $this->get_filetype($wp_dest), |
| 219 |
'mime' => $this->get_file_mime($wp_dest) |
| 220 |
); |
| 221 |
} |
| 222 |
|
| 223 |
public function string_to_file($string, $filename) |
| 224 |
{ |
| 225 |
$wp_upload_dir = wp_upload_dir(); |
| 226 |
$dest = wp_unique_filename($wp_upload_dir['path'], $filename); |
| 227 |
$wp_dest = $wp_upload_dir['path'] . '/' . $dest; |
| 228 |
|
| 229 |
if (file_put_contents($wp_dest, $string) === false) { |
| 230 |
return new \WP_Error('IWP_FS_SF', "Unable to write string to file"); |
| 231 |
} |
| 232 |
|
| 233 |
return array( |
| 234 |
'dest' => $wp_dest, |
| 235 |
'type' => $this->get_filetype($wp_dest), |
| 236 |
'mime' => $this->get_file_mime($wp_dest) |
| 237 |
); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
/** |
| 243 |
* Get and/or create the plugins tmp directory |
| 244 |
* |
| 245 |
* @return string |
| 246 |
*/ |
| 247 |
public function get_temp_directory($url = false) |
| 248 |
{ |
| 249 |
$base = $url ? WP_CONTENT_URL : WP_CONTENT_DIR; |
| 250 |
$ds = $url ? '/' : DIRECTORY_SEPARATOR; |
| 251 |
$path = $base . $ds . 'uploads'; |
| 252 |
if (!is_dir($path)) { |
| 253 |
mkdir($path); |
| 254 |
} |
| 255 |
|
| 256 |
$path .= $ds . 'importwp'; |
| 257 |
if (!is_dir($path)) { |
| 258 |
mkdir($path); |
| 259 |
} |
| 260 |
|
| 261 |
return $path; |
| 262 |
} |
| 263 |
|
| 264 |
public function check_mime_header($mime) |
| 265 |
{ |
| 266 |
switch ($mime) { |
| 267 |
case 'text/comma-separated-values': |
| 268 |
case 'text/csv': |
| 269 |
case 'application/csv': |
| 270 |
case 'application/excel': |
| 271 |
case 'application/vnd.ms-excel': |
| 272 |
case 'application/vnd.msexcel': |
| 273 |
case 'text/anytext': |
| 274 |
case 'text/plain': |
| 275 |
return 'csv'; |
| 276 |
case 'text/xml': |
| 277 |
case 'application/xml': |
| 278 |
case 'application/x-xml': |
| 279 |
return 'xml'; |
| 280 |
} |
| 281 |
|
| 282 |
return $this->event_handler->run('importer.allowed_mime_types', [false, $mime]); |
| 283 |
} |
| 284 |
|
| 285 |
public function get_filetype($file) |
| 286 |
{ |
| 287 |
$mime_type = $this->get_file_mime($file); |
| 288 |
if ($mime_type) { |
| 289 |
return $this->check_mime_header($mime_type); |
| 290 |
} |
| 291 |
|
| 292 |
return $this->get_filetype_from_ext($file); |
| 293 |
} |
| 294 |
|
| 295 |
public function get_file_mime($file) |
| 296 |
{ |
| 297 |
$check = wp_check_filetype_and_ext($file, basename($file)); |
| 298 |
return $check['type']; |
| 299 |
} |
| 300 |
|
| 301 |
public function get_filetype_from_ext($file) |
| 302 |
{ |
| 303 |
|
| 304 |
$filetype = null; |
| 305 |
if (stripos($file, '.csv')) { |
| 306 |
$filetype = 'csv'; |
| 307 |
} elseif (stripos($file, '.xml')) { |
| 308 |
$filetype = 'xml'; |
| 309 |
} |
| 310 |
|
| 311 |
$filetype = apply_filters('iwp/get_filetype_from_ext', $filetype, $file); |
| 312 |
|
| 313 |
return $filetype; |
| 314 |
} |
| 315 |
} |
| 316 |
|