From 18c407948991848a5d9f271d1d43fd1b213a3961 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Thu, 23 Nov 2023 16:56:21 +0100 Subject: [PATCH] fix(MonkeyPatching) handle feel fopen --- src/MonkeyPatch/FileStreamWrapper.php | 12 +++++++++++- src/WordPress/FileRequests/FileRequest.php | 6 ++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/MonkeyPatch/FileStreamWrapper.php b/src/MonkeyPatch/FileStreamWrapper.php index c2d7f0d7e..2a7594150 100644 --- a/src/MonkeyPatch/FileStreamWrapper.php +++ b/src/MonkeyPatch/FileStreamWrapper.php @@ -483,6 +483,15 @@ private function patchFile(string $absPath): string */ private function openFile(string $absPath, string $mode, bool $useIncludePath): void { + if (!file_exists($absPath) && !is_dir(dirname($absPath))) { + /* + * The file open operation will never succeed, so we don't even try. + * The `w`, `c` and `x` modes will create the file if it does not exist, + * but will not create the directory structure to it + */ + return; + } + if (isset($this->context)) { $handle = fopen($absPath, $mode, $useIncludePath, $this->context); } else { @@ -490,7 +499,8 @@ private function openFile(string $absPath, string $mode, bool $useIncludePath): } if (!is_resource($handle)) { - throw new MonkeyPatchingException("Could not open file $absPath."); + return; + // throw new MonkeyPatchingException("Could not open file $absPath."); } $this->fileResource = $handle; diff --git a/src/WordPress/FileRequests/FileRequest.php b/src/WordPress/FileRequests/FileRequest.php index 78c7a4fe2..3fbf2445d 100644 --- a/src/WordPress/FileRequests/FileRequest.php +++ b/src/WordPress/FileRequests/FileRequest.php @@ -143,6 +143,12 @@ public function execute(): array if (($errno & $this->errorLevel) === 0) { return true; } + + // Ignore E_WARNING from `fopen` calls: they are used by some plugins to try and check if a file exists. + if ($errno === E_WARNING && str_starts_with($errstr, 'fopen')) { + return true; + } + throw new ErrorException($errstr, 0, $errno, $errfile, $errline); }, E_ALL);