Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate attachment upload by file path #302

Merged
merged 8 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions src/Redmine/Client/ClientApiTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
;
}
}
15 changes: 4 additions & 11 deletions src/Redmine/Client/NativeCurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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';
Expand Down
15 changes: 4 additions & 11 deletions src/Redmine/Client/Psr18Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand All @@ -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');
Expand All @@ -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)))
;
}
}