| @@ -1,12 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | if (!defined('ABSPATH')) exit; |
| 3 | -if (!class_exists('WPRFSWriteCallback')) : | |
| 4 | +if (!class_exists('BVFSWriteCallback')) : | |
| 4 | 5 | |
| 5 | -class WPRFSWriteCallback extends WPRCallbackBase { | |
| 6 | +class BVFSWriteCallback extends BVCallbackBase { | |
| 6 | 7 | |
| 7 | 8 | const MEGABYTE = 1048576; |
| 8 | - const FS_WRITE_WING_VERSION = 1.2; | |
| 9 | + const FS_WRITE_WING_VERSION = 1.0; | |
| 9 | 10 | |
| 10 | 11 | public function __construct() { |
| 11 | 12 | } |
| 12 | 13 | |
| @@ -51,9 +52,9 @@ | ||
| 51 | 52 | $dir_result['error'] = "FILE_PRESENT_IN_PLACE_OF_DIR"; |
| 52 | 53 | } |
| 53 | 54 | |
| 54 | 55 | } else { |
| 55 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir -- Using mkdir() directly as there is no direct suport for recursion | |
| 56 | + | |
| 56 | 57 | $dir_result['status'] = mkdir($dir, $permissions, $recursive); |
| 57 | 58 | if ($dir_result['status'] === false) { |
| 58 | 59 | $dir_result['error'] = "MKDIR_FAILED"; |
| 59 | 60 | } |
| @@ -69,31 +70,31 @@ | ||
| 69 | 70 | |
| 70 | 71 | public function removeDirs($dirs) { |
| 71 | 72 | $result = array(); |
| 72 | 73 | |
| 73 | - foreach ($dirs as $dir) { | |
| 74 | + foreach($dirs as $dir) { | |
| 74 | 75 | $dir_result = array(); |
| 75 | 76 | |
| 76 | - if ((WPRWPFileSystem::getInstance()->isDir($dir) === true) && !is_link($dir)) { | |
| 77 | + if (is_dir($dir) && !is_link($dir)) { | |
| 78 | + | |
| 77 | 79 | if ($this->isEmptyDir($dir)) { |
| 78 | - $dir_result['status'] = WPRWPFileSystem::getInstance()->rmdir($dir); | |
| 80 | + | |
| 81 | + $dir_result['status'] = rmdir($dir); | |
| 79 | 82 | if ($dir_result['status'] === false) { |
| 80 | 83 | $dir_result['error'] = "RMDIR_FAILED"; |
| 81 | - $fs_error = WPRWPFileSystem::getInstance()->checkForErrors(); | |
| 82 | - if (isset($fs_error)) { | |
| 83 | - $dir_result['fs_error'] = $fs_error; | |
| 84 | - } | |
| 85 | 84 | } |
| 85 | + | |
| 86 | 86 | } else { |
| 87 | 87 | $dir_result['status'] = false; |
| 88 | 88 | $dir_result['error'] = "NOT_EMPTY"; |
| 89 | 89 | } |
| 90 | + | |
| 90 | 91 | } else { |
| 91 | 92 | $dir_result['status'] = false; |
| 92 | 93 | $dir_result['error'] = "NOT_DIR"; |
| 93 | 94 | } |
| 94 | 95 | |
| 95 | - $result[$dir] = $dir_result; | |
| 96 | + $result[$dir] = $dir_result; | |
| 96 | 97 | } |
| 97 | 98 | |
| 98 | 99 | $result['status'] = true; |
| 99 | 100 | return $result; |
| @@ -115,20 +116,18 @@ | ||
| 115 | 116 | |
| 116 | 117 | public function doChmod($path_infos) { |
| 117 | 118 | $result = array(); |
| 118 | 119 | |
| 119 | - foreach ($path_infos as $path => $mode) { | |
| 120 | + foreach($path_infos as $path => $mode) { | |
| 120 | 121 | $path_result = array(); |
| 121 | 122 | |
| 122 | - if (WPRWPFileSystem::getInstance()->exists($path) === true) { | |
| 123 | - $path_result['status'] = WPRWPFileSystem::getInstance()->chmod($path, $mode); | |
| 123 | + if (file_exists($path)) { | |
| 124 | + | |
| 125 | + $path_result['status'] = chmod($path, $mode); | |
| 124 | 126 | if ($path_result['status'] === false) { |
| 125 | 127 | $path_result['error'] = "CHMOD_FAILED"; |
| 126 | - $fs_error = WPRWPFileSystem::getInstance()->checkForErrors(); | |
| 127 | - if (isset($fs_error)) { | |
| 128 | - $path_result['fs_error'] = $fs_error; | |
| 129 | - } | |
| 130 | 128 | } |
| 129 | + | |
| 131 | 130 | } else { |
| 132 | 131 | $path_result['status'] = false; |
| 133 | 132 | $path_result['error'] = "NOT_FOUND"; |
| 134 | 133 | } |
| @@ -139,12 +138,8 @@ | ||
| 139 | 138 | $result['status'] = true; |
| 140 | 139 | return $result; |
| 141 | 140 | } |
| 142 | 141 | |
| 143 | - // phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_fopen | |
| 144 | - // phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_fread | |
| 145 | - // phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_fwrite | |
| 146 | - // phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_fclose | |
| 147 | 142 | public function concatFiles($ifiles, $ofile, $bsize, $offset) { |
| 148 | 143 | if (($offset !== 0) && (!file_exists($ofile))) { |
| 149 | 144 | return array( |
| 150 | 145 | 'status' => false, |
| @@ -221,29 +216,23 @@ | ||
| 221 | 216 | } |
| 222 | 217 | |
| 223 | 218 | return $result; |
| 224 | 219 | } |
| 225 | - // phpcs:enable | |
| 226 | 220 | |
| 227 | 221 | public function renameFiles($path_infos) { |
| 228 | 222 | $result = array(); |
| 229 | 223 | |
| 230 | - foreach ($path_infos as $oldpath => $newpath) { | |
| 224 | + foreach($path_infos as $oldpath => $newpath) { | |
| 231 | 225 | $action_result = array(); |
| 226 | + $failed = array(); | |
| 232 | 227 | |
| 233 | - if (WPRWPFileSystem::getInstance()->exists($oldpath)) { | |
| 234 | - $action_result['status'] = WPRWPFileSystem::getInstance()->move($oldpath, $newpath, true); | |
| 228 | + if (file_exists($oldpath)) { | |
| 229 | + | |
| 230 | + $action_result['status'] = rename($oldpath, $newpath); | |
| 235 | 231 | if ($action_result['status'] === false) { |
| 236 | 232 | $action_result['error'] = "RENAME_FAILED"; |
| 237 | - $fs_error = WPRWPFileSystem::getInstance()->checkForErrors(); | |
| 238 | - if (isset($fs_error)) { | |
| 239 | - $action_result['fs_error'] = $fs_error; | |
| 240 | - } | |
| 241 | - } else { | |
| 242 | - if (function_exists('opcache_invalidate')) { | |
| 243 | - $action_result['opcache'] = opcache_invalidate($newpath, true); | |
| 244 | - } | |
| 245 | 233 | } |
| 234 | + | |
| 246 | 235 | } else { |
| 247 | 236 | $action_result['status'] = false; |
| 248 | 237 | $action_result['error'] = "NOT_FOUND"; |
| 249 | 238 | } |
| @@ -255,9 +244,8 @@ | ||
| 255 | 244 | return $result; |
| 256 | 245 | } |
| 257 | 246 | |
| 258 | 247 | public function curlFile($ifile_url, $ofile, $timeout) { |
| 259 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen | |
| 260 | 248 | $fp = fopen($ofile, "wb+"); |
| 261 | 249 | if ($fp === false) { |
| 262 | 250 | return array( |
| 263 | 251 | 'error' => 'FOPEN_FAILED_FOR_TEMP_OFILE' |
| @@ -264,11 +252,10 @@ | ||
| 264 | 252 | ); |
| 265 | 253 | } |
| 266 | 254 | |
| 267 | 255 | $result = array(); |
| 268 | - | |
| 269 | - // phpcs:disable WordPress.WP.AlternativeFunctions.curl_curl_init, WordPress.WP.AlternativeFunctions.curl_curl_exec, WordPress.WP.AlternativeFunctions.curl_curl_setopt, WordPress.WP.AlternativeFunctions.curl_curl_close, WordPress.WP.AlternativeFunctions.curl_curl_error, WordPress.WP.AlternativeFunctions.curl_curl_errno | |
| 270 | 256 | $ch = curl_init($ifile_url); |
| 257 | + curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); | |
| 271 | 258 | curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); |
| 272 | 259 | curl_setopt($ch, CURLOPT_HEADER, 0); |
| 273 | 260 | curl_setopt($ch, CURLOPT_FILE, $fp); |
| 274 | 261 | |
| @@ -277,21 +264,15 @@ | ||
| 277 | 264 | $result['errorno'] = curl_errno($ch); |
| 278 | 265 | } |
| 279 | 266 | |
| 280 | 267 | curl_close($ch); |
| 281 | - | |
| 282 | - // phpcs:enable WordPress.WP.AlternativeFunctions.curl_curl_init, WordPress.WP.AlternativeFunctions.curl_curl_exec, WordPress.WP.AlternativeFunctions.curl_curl_setopt, WordPress.WP.AlternativeFunctions.curl_curl_close, WordPress.WP.AlternativeFunctions.curl_curl_error, WordPress.WP.AlternativeFunctions.curl_curl_errno | |
| 283 | - | |
| 284 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose | |
| 285 | 268 | fclose($fp); |
| 286 | 269 | |
| 287 | - | |
| 288 | 270 | return $result; |
| 289 | 271 | } |
| 290 | 272 | |
| 291 | 273 | public function streamCopyFile($ifile_url, $ofile) { |
| 292 | 274 | $result = array(); |
| 293 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen | |
| 294 | 275 | $handle = fopen($ifile_url, "rb"); |
| 295 | 276 | |
| 296 | 277 | if ($handle === false) { |
| 297 | 278 | return array( |
| @@ -298,12 +279,11 @@ | ||
| 298 | 279 | 'error' => "UNABLE_TO_OPEN_REMOTE_FILE_STREAM" |
| 299 | 280 | ); |
| 300 | 281 | } |
| 301 | 282 | |
| 302 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen | |
| 303 | 283 | $fp = fopen($ofile, "wb+"); |
| 304 | 284 | if ($fp === false) { |
| 305 | - fclose($handle); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose | |
| 285 | + fclose($handle); | |
| 306 | 286 | |
| 307 | 287 | return array( |
| 308 | 288 | 'error' => 'FOPEN_FAILED_FOR_OFILE' |
| 309 | 289 | ); |
| @@ -312,10 +292,10 @@ | ||
| 312 | 292 | if (stream_copy_to_stream($handle, $fp) === false) { |
| 313 | 293 | $result['error'] = "UNABLE_TO_WRITE_TO_TMP_OFILE"; |
| 314 | 294 | } |
| 315 | 295 | |
| 316 | - fclose($handle); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose | |
| 317 | - fclose($fp); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose | |
| 296 | + fclose($handle); | |
| 297 | + fclose($fp); | |
| 318 | 298 | |
| 319 | 299 | return $result; |
| 320 | 300 | } |
| 321 | 301 | |
| @@ -321,16 +301,20 @@ | ||
| 321 | 301 | |
| 322 | 302 | public function writeContentToFile($content, $ofile) { |
| 323 | 303 | $result = array(); |
| 324 | 304 | |
| 325 | - if (WPRWPFileSystem::getInstance()->putContents($ofile, $content) === false) { | |
| 326 | - $result['error'] = 'UNABLE_TO_WRITE_TO_TMP_OFILE'; | |
| 327 | - $fs_error = WPRWPFileSystem::getInstance()->checkForErrors(); | |
| 328 | - if (isset($fs_error)) { | |
| 329 | - $result['fs_error'] = $fs_error; | |
| 330 | - } | |
| 305 | + $fp = fopen($ofile, "wb+"); | |
| 306 | + if ($fp === false) { | |
| 307 | + return array( | |
| 308 | + 'error' => 'FOPEN_FAILED_FOR_TEMP_OFILE' | |
| 309 | + ); | |
| 331 | 310 | } |
| 332 | 311 | |
| 312 | + if (fwrite($fp, $content) === false) { | |
| 313 | + $resp['error'] = "UNABLE_TO_WRITE_TO_TMP_OFILE"; | |
| 314 | + } | |
| 315 | + fclose($fp); | |
| 316 | + | |
| 333 | 317 | return $result; |
| 334 | 318 | } |
| 335 | 319 | |
| 336 | 320 | public function moveUploadedFile($ofile) { |
| @@ -335,17 +319,12 @@ | ||
| 335 | 319 | |
| 336 | 320 | public function moveUploadedFile($ofile) { |
| 337 | 321 | $result = array(); |
| 338 | 322 | |
| 339 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing | |
| 340 | 323 | if (isset($_FILES['myfile'])) { |
| 341 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing -- tmp_name is a path and nonce is ignored here | |
| 342 | 324 | $myfile = $_FILES['myfile']; |
| 343 | 325 | $is_upload_ok = false; |
| 344 | 326 | |
| 345 | - // Validate PHP upload errors manually | |
| 346 | - // This approach handles any file type (PHP, ZIP, SQL, etc.) without MIME restrictions | |
| 347 | - // Uses WordPress Filesystem API instead of wp_handle_upload() which is designed for media uploads | |
| 348 | 327 | switch ($myfile['error']) { |
| 349 | 328 | case UPLOAD_ERR_OK: |
| 350 | 329 | $is_upload_ok = true; |
| 351 | 330 | break; |
| @@ -365,28 +344,10 @@ | ||
| 365 | 344 | $is_upload_ok = false; |
| 366 | 345 | } |
| 367 | 346 | |
| 368 | 347 | if ($is_upload_ok) { |
| 369 | - $tmp_name = $myfile['tmp_name']; | |
| 370 | - | |
| 371 | - // Ensure target directory exists | |
| 372 | - $target_dir = dirname($ofile); | |
| 373 | - if (!file_exists($target_dir)) { | |
| 374 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir -- Using mkdir() directly as there is no direct support for recursion | |
| 375 | - if (!mkdir($target_dir, 0777, true)) { | |
| 376 | - $result['error'] = 'MKDIR_FAILED_FOR_TARGET'; | |
| 377 | - return $result; | |
| 378 | - } | |
| 379 | - } | |
| 380 | - | |
| 381 | - // Use WordPress Filesystem API to move the uploaded file | |
| 382 | - // This is WordPress.org compliant and handles any file type | |
| 383 | - if (WPRWPFileSystem::getInstance()->move($tmp_name, $ofile, true) === false) { | |
| 348 | + if (move_uploaded_file($myfile['tmp_name'], $ofile) === false) { | |
| 384 | 349 | $result['error'] = 'MOVE_UPLOAD_FILE_FAILED'; |
| 385 | - $fs_error = WPRWPFileSystem::getInstance()->checkForErrors(); | |
| 386 | - if (isset($fs_error)) { | |
| 387 | - $result['fs_error'] = $fs_error; | |
| 388 | - } | |
| 389 | 350 | } |
| 390 | 351 | } |
| 391 | 352 | |
| 392 | 353 | } else { |
| @@ -439,74 +400,8 @@ | ||
| 439 | 400 | |
| 440 | 401 | return $resp; |
| 441 | 402 | } |
| 442 | 403 | |
| 443 | - public function runFileCmd($cmd_key, $cmd_params) { | |
| 444 | - switch ($cmd_key) { | |
| 445 | - case "wrtfle": | |
| 446 | - return $this->uploadFile($cmd_params); | |
| 447 | - case "renmefle": | |
| 448 | - $from = $cmd_params['from']; | |
| 449 | - $to = $cmd_params['to']; | |
| 450 | - $rename_result = $this->renameFiles(array($from => $to)); | |
| 451 | - return isset($rename_result[$from]) ? $rename_result[$from] : array('status' => false, 'error' => 'RENAME_NO_RESULT'); | |
| 452 | - case "chmd": | |
| 453 | - $path = $cmd_params['path']; | |
| 454 | - $chmod_result = $this->doChmod(array($path => $cmd_params['mode'])); | |
| 455 | - return isset($chmod_result[$path]) ? $chmod_result[$path] : array('status' => false, 'error' => 'CHMOD_NO_RESULT'); | |
| 456 | - case "mkdr": | |
| 457 | - $path = $cmd_params['path']; | |
| 458 | - $perms = isset($cmd_params['perms']) ? $cmd_params['perms'] : 0777; | |
| 459 | - $rec = isset($cmd_params['rec']) ? (bool) $cmd_params['rec'] : true; | |
| 460 | - $mkdir_result = $this->makeDirs(array($path), $perms, $rec); | |
| 461 | - return isset($mkdir_result[$path]) ? $mkdir_result[$path] : array('status' => false, 'error' => 'MKDIR_NO_RESULT'); | |
| 462 | - case "rmfle": | |
| 463 | - $files = $cmd_params['files']; | |
| 464 | - $rm_result = $this->removeFiles($files); | |
| 465 | - $first = reset($files); | |
| 466 | - return isset($rm_result[$first]) ? $rm_result[$first] : array('status' => false, 'error' => 'RMFLE_NO_RESULT'); | |
| 467 | - case "rmdr": | |
| 468 | - $dirs = $cmd_params['dirs']; | |
| 469 | - $rmdr_result = $this->removeDirs($dirs); | |
| 470 | - $first = reset($dirs); | |
| 471 | - return isset($rmdr_result[$first]) ? $rmdr_result[$first] : array('status' => false, 'error' => 'RMDR_NO_RESULT'); | |
| 472 | - default: | |
| 473 | - return array('status' => false, 'error' => 'UNKNOWN_CMD'); | |
| 474 | - } | |
| 475 | - } | |
| 476 | - | |
| 477 | - public function executeFileOps($ops, $all_required = false) { | |
| 478 | - $result = array(); | |
| 479 | - $all_success = true; | |
| 480 | - | |
| 481 | - foreach ($ops as $op) { | |
| 482 | - $identifier = $op['identifier']; | |
| 483 | - $cmds = $op['cmds']; | |
| 484 | - $op_result = array(); | |
| 485 | - | |
| 486 | - foreach ($cmds as $cmd) { | |
| 487 | - foreach ($cmd as $cmd_key => $cmd_params) { | |
| 488 | - $cmd_result = $this->runFileCmd($cmd_key, $cmd_params); | |
| 489 | - $op_result[$cmd_key] = $cmd_result; | |
| 490 | - | |
| 491 | - if (isset($cmd_result['status']) && $cmd_result['status'] === false) { | |
| 492 | - $all_success = false; | |
| 493 | - break 2; | |
| 494 | - } | |
| 495 | - } | |
| 496 | - } | |
| 497 | - | |
| 498 | - $result[$identifier] = $op_result; | |
| 499 | - | |
| 500 | - if ($all_required && !$all_success) { | |
| 501 | - break; | |
| 502 | - } | |
| 503 | - } | |
| 504 | - | |
| 505 | - $result['status'] = $all_success; | |
| 506 | - return $result; | |
| 507 | - } | |
| 508 | - | |
| 509 | 404 | public function process($request) { |
| 510 | 405 | $params = $request->params; |
| 511 | 406 | |
| 512 | 407 | switch ($request->method) { |
| @@ -527,14 +422,10 @@ | ||
| 527 | 422 | break; |
| 528 | 423 | case "wrtfle": |
| 529 | 424 | $resp = $this->uploadFile($params); |
| 530 | 425 | break; |
| 531 | - case "fleops": | |
| 532 | - $all_required = isset($params['all_required']) ? (bool) $params['all_required'] : false; | |
| 533 | - $resp = $this->executeFileOps($params['ops'], $all_required); | |
| 534 | - break; | |
| 535 | 426 | case "cncatfls": |
| 536 | - $bsize = (isset($params['bsize'])) ? $params['bsize'] : (8 * WPRFSWriteCallback::MEGABYTE); | |
| 427 | + $bsize = (isset($params['bsize'])) ? $params['bsize'] : (8 * BVFSWriteCallback::MEGABYTE); | |
| 537 | 428 | $offset = (isset($params['offset'])) ? $params['offset'] : 0; |
| 538 | 429 | $resp = $this->concatFiles($params['infiles'], $params['ofile'], $bsize, $offset); |
| 539 | 430 | break; |
| 540 | 431 | default: |