Skip to content

Commit 3f1395c

Browse files
authored
Merge pull request #302 from Art4/improve-file-upload
Deprecate attachment upload by file path
2 parents 084296c + ae24dcc commit 3f1395c

File tree

4 files changed

+29
-22
lines changed

4 files changed

+29
-22
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919

2020
- Switched from Travis-CI to Github Actions
2121

22+
### Fixed
23+
24+
- Avoid warning if path of uploaded file is longer than the maximum allowed path length
25+
2226
### Deprecated
2327

2428
- `Redmine\Api\AbstractApi::lastCallFailed()` is deprecated, use `Redmine\Client\Client::getLastResponseStatusCode()` instead
29+
- Uploading an attachment using `Redmine\Api\Attachment::upload()` with filepath is deprectead, use `file_get_contents()` to upload the file content instead
2530

2631
## [v2.0.1](https://github.com/kbsali/php-redmine-api/compare/v2.0.0...v2.0.1) - 2021-09-22
2732

src/Redmine/Client/ClientApiTrait.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,20 @@ public function getApi(string $name): Api
5353

5454
return $this->apiInstances[$name];
5555
}
56+
57+
private function isUploadCall(string $path): bool
58+
{
59+
$path = strtolower($path);
60+
61+
return (false !== strpos($path, '/uploads.json')) || (false !== strpos($path, '/uploads.xml'));
62+
}
63+
64+
private function isValidFilePath(string $body): bool
65+
{
66+
return
67+
'' !== $body
68+
&& strlen($body) <= \PHP_MAXPATHLEN
69+
&& is_file(strval(str_replace("\0", '', $body)))
70+
;
71+
}
5672
}

src/Redmine/Client/NativeCurlClient.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@ private function createCurl(string $method, string $path, string $body = '')
271271
switch ($method) {
272272
case 'post':
273273
$curlOptions[CURLOPT_POST] = 1;
274-
if ($this->isUploadCall($path, $body)) {
274+
if ($this->isUploadCall($path) && $this->isValidFilePath($body)) {
275+
@trigger_error('Uploading an attachment by filepath is deprecated, use file_get_contents() to upload the file content instead.', E_USER_DEPRECATED);
276+
275277
$file = fopen($body, 'r');
276278
$size = filesize($body);
277279
$filedata = fread($file, $size);
@@ -311,15 +313,6 @@ private function createCurl(string $method, string $path, string $body = '')
311313
return $curl;
312314
}
313315

314-
private function isUploadCall(string $path, string $body): bool
315-
{
316-
return
317-
(preg_match('/\/uploads.(json|xml)/i', $path)) &&
318-
'' !== $body &&
319-
is_file(strval(str_replace("\0", '', $body)))
320-
;
321-
}
322-
323316
private function createHttpHeader(string $path): array
324317
{
325318
// Additional request headers
@@ -360,7 +353,7 @@ private function createHttpHeader(string $path): array
360353
// Content type headers
361354
$tmp = parse_url($this->url.$path);
362355

363-
if (preg_match('/\/uploads.(json|xml)/i', $path)) {
356+
if ($this->isUploadCall($path)) {
364357
$httpHeaders[] = 'Content-Type: application/octet-stream';
365358
} elseif ('json' === substr($tmp['path'], -4)) {
366359
$httpHeaders[] = 'Content-Type: application/json';

src/Redmine/Client/Psr18Client.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ private function createRequest(string $method, string $path, string $body = ''):
181181

182182
switch ($method) {
183183
case 'POST':
184-
if ($this->isUploadCall($path, $body)) {
184+
if ($this->isUploadCall($path) && $this->isValidFilePath($body)) {
185+
@trigger_error('Uploading an attachment by filepath is deprecated, use file_get_contents() to upload the file content instead.', E_USER_DEPRECATED);
186+
185187
$request = $request->withBody(
186188
$this->streamFactory->createStreamFromFile($body)
187189
);
@@ -203,7 +205,7 @@ private function createRequest(string $method, string $path, string $body = ''):
203205
// set Content-Type header
204206
$tmp = parse_url($this->url.$path);
205207

206-
if (preg_match('/\/uploads.(json|xml)/i', $path)) {
208+
if ($this->isUploadCall($path)) {
207209
$request = $request->withHeader('Content-Type', 'application/octet-stream');
208210
} elseif ('json' === substr($tmp['path'], -4)) {
209211
$request = $request->withHeader('Content-Type', 'application/json');
@@ -213,13 +215,4 @@ private function createRequest(string $method, string $path, string $body = ''):
213215

214216
return $request;
215217
}
216-
217-
private function isUploadCall(string $path, string $body): bool
218-
{
219-
return
220-
(preg_match('/\/uploads.(json|xml)/i', $path)) &&
221-
'' !== $body &&
222-
is_file(strval(str_replace("\0", '', $body)))
223-
;
224-
}
225218
}

0 commit comments

Comments
 (0)