diff --git a/CHANGELOG.md b/CHANGELOG.md index 3dbf5ed6..daa6452b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,9 +19,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Switched from Travis-CI to Github Actions +### Fixed + +- Avoid warning if path of uploaded file is longer than the maximum allowed path length + ### Deprecated - `Redmine\Api\AbstractApi::lastCallFailed()` is deprecated, use `Redmine\Client\Client::getLastResponseStatusCode()` instead +- Uploading an attachment using `Redmine\Api\Attachment::upload()` with filepath is deprectead, use `file_get_contents()` to upload the file content instead ## [v2.0.1](https://github.com/kbsali/php-redmine-api/compare/v2.0.0...v2.0.1) - 2021-09-22 diff --git a/src/Redmine/Client/ClientApiTrait.php b/src/Redmine/Client/ClientApiTrait.php index b8b47bb4..a426f7b5 100644 --- a/src/Redmine/Client/ClientApiTrait.php +++ b/src/Redmine/Client/ClientApiTrait.php @@ -53,4 +53,20 @@ public function getApi(string $name): Api return $this->apiInstances[$name]; } + + private function isUploadCall(string $path): bool + { + $path = strtolower($path); + + return (false !== strpos($path, '/uploads.json')) || (false !== strpos($path, '/uploads.xml')); + } + + private function isValidFilePath(string $body): bool + { + return + '' !== $body + && strlen($body) <= \PHP_MAXPATHLEN + && is_file(strval(str_replace("\0", '', $body))) + ; + } } diff --git a/src/Redmine/Client/NativeCurlClient.php b/src/Redmine/Client/NativeCurlClient.php index 4553a788..4ef910a1 100644 --- a/src/Redmine/Client/NativeCurlClient.php +++ b/src/Redmine/Client/NativeCurlClient.php @@ -271,7 +271,9 @@ private function createCurl(string $method, string $path, string $body = '') switch ($method) { case 'post': $curlOptions[CURLOPT_POST] = 1; - if ($this->isUploadCall($path, $body)) { + if ($this->isUploadCall($path) && $this->isValidFilePath($body)) { + @trigger_error('Uploading an attachment by filepath is deprecated, use file_get_contents() to upload the file content instead.', E_USER_DEPRECATED); + $file = fopen($body, 'r'); $size = filesize($body); $filedata = fread($file, $size); @@ -311,15 +313,6 @@ private function createCurl(string $method, string $path, string $body = '') return $curl; } - private function isUploadCall(string $path, string $body): bool - { - return - (preg_match('/\/uploads.(json|xml)/i', $path)) && - '' !== $body && - is_file(strval(str_replace("\0", '', $body))) - ; - } - private function createHttpHeader(string $path): array { // Additional request headers @@ -360,7 +353,7 @@ private function createHttpHeader(string $path): array // Content type headers $tmp = parse_url($this->url.$path); - if (preg_match('/\/uploads.(json|xml)/i', $path)) { + if ($this->isUploadCall($path)) { $httpHeaders[] = 'Content-Type: application/octet-stream'; } elseif ('json' === substr($tmp['path'], -4)) { $httpHeaders[] = 'Content-Type: application/json'; diff --git a/src/Redmine/Client/Psr18Client.php b/src/Redmine/Client/Psr18Client.php index b7c17509..f67a0a41 100644 --- a/src/Redmine/Client/Psr18Client.php +++ b/src/Redmine/Client/Psr18Client.php @@ -181,7 +181,9 @@ private function createRequest(string $method, string $path, string $body = ''): switch ($method) { case 'POST': - if ($this->isUploadCall($path, $body)) { + if ($this->isUploadCall($path) && $this->isValidFilePath($body)) { + @trigger_error('Uploading an attachment by filepath is deprecated, use file_get_contents() to upload the file content instead.', E_USER_DEPRECATED); + $request = $request->withBody( $this->streamFactory->createStreamFromFile($body) ); @@ -203,7 +205,7 @@ private function createRequest(string $method, string $path, string $body = ''): // set Content-Type header $tmp = parse_url($this->url.$path); - if (preg_match('/\/uploads.(json|xml)/i', $path)) { + if ($this->isUploadCall($path)) { $request = $request->withHeader('Content-Type', 'application/octet-stream'); } elseif ('json' === substr($tmp['path'], -4)) { $request = $request->withHeader('Content-Type', 'application/json'); @@ -213,13 +215,4 @@ private function createRequest(string $method, string $path, string $body = ''): return $request; } - - private function isUploadCall(string $path, string $body): bool - { - return - (preg_match('/\/uploads.(json|xml)/i', $path)) && - '' !== $body && - is_file(strval(str_replace("\0", '', $body))) - ; - } }