|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2023 |
|
6
|
|
|
* @package MShop |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
namespace Aimeos\MShop; |
|
11
|
|
|
|
|
12
|
|
|
use \Psr\Http\Message\UploadedFileInterface; |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Upload trait |
|
17
|
|
|
* |
|
18
|
|
|
* @package MShop |
|
19
|
|
|
*/ |
|
20
|
|
|
trait Upload |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Returns the context object. |
|
24
|
|
|
* |
|
25
|
|
|
* @return \Aimeos\MShop\ContextIface Context object |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract protected function context() : \Aimeos\MShop\ContextIface; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Deletes a file from the file system |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $filepath Relative path to the file in the file system |
|
34
|
|
|
* @param string $fsname File system name |
|
35
|
|
|
* @return self Same object for fluent interface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function deleteFile( string $filepath, string $fsname = 'fs-media' ) : self |
|
38
|
|
|
{ |
|
39
|
|
|
$fs = $this->context()->fs( $fsname ); |
|
40
|
|
|
|
|
41
|
|
|
if( $fs->has( $filepath ) ) { |
|
42
|
|
|
$fs->rm( $filepath ); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns a save file name for the uploaded file |
|
51
|
|
|
* |
|
52
|
|
|
* @param \Psr\Http\Message\UploadedFileInterface $file Uploaded file object |
|
53
|
|
|
* @return string Sanitized file name |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function getFilename( UploadedFileInterface $file ) : string |
|
56
|
|
|
{ |
|
57
|
|
|
$filename = \Aimeos\Base\Str::slug( $file->getClientFilename() ); |
|
58
|
|
|
return substr( md5( $filename . getmypid() . microtime( true ) ), -8 ) . '_' . $filename; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns the file mime type for the uploaded file |
|
64
|
|
|
* |
|
65
|
|
|
* Caution: This method must be called before storeFile() to be able to |
|
66
|
|
|
* determine the file mime type! |
|
67
|
|
|
* |
|
68
|
|
|
* @param \Psr\Http\Message\UploadedFileInterface $file Uploaded file object |
|
69
|
|
|
* @return string File mime type |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getFilemime( UploadedFileInterface $file ) : string |
|
72
|
|
|
{ |
|
73
|
|
|
$stream = $file->getStream(); |
|
74
|
|
|
|
|
75
|
|
|
if( !$stream->isSeekable() ) { |
|
76
|
|
|
return ''; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$stream->rewind(); |
|
80
|
|
|
$content = $stream->read( 100 ); |
|
81
|
|
|
$stream->rewind(); |
|
82
|
|
|
|
|
83
|
|
|
$finfo = new \finfo( FILEINFO_MIME_TYPE ); |
|
84
|
|
|
return $finfo->buffer( $content ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Stores the uploaded file |
|
90
|
|
|
* |
|
91
|
|
|
* @param \Psr\Http\Message\UploadedFileInterface $file Uploaded file object |
|
92
|
|
|
* @param string $filepath Relative path to the file in the file system |
|
93
|
|
|
* @param string $fsname File system name |
|
94
|
|
|
* @return self Same object for fluent interface |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function storeFile( UploadedFileInterface $file, string $filepath, string $fsname = 'fs-media' ) : self |
|
97
|
|
|
{ |
|
98
|
|
|
if( $file->getError() !== UPLOAD_ERR_OK ) |
|
99
|
|
|
{ |
|
100
|
|
|
$errors = [ |
|
101
|
|
|
0 => 'There is no error, the file uploaded with success', |
|
102
|
|
|
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini', |
|
103
|
|
|
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', |
|
104
|
|
|
3 => 'The uploaded file was only partially uploaded', |
|
105
|
|
|
4 => 'No file was uploaded', |
|
106
|
|
|
6 => 'Missing a temporary folder', |
|
107
|
|
|
7 => 'Failed to write file to disk.', |
|
108
|
|
|
8 => 'A PHP extension stopped the file upload.', |
|
109
|
|
|
]; |
|
110
|
|
|
|
|
111
|
|
|
throw new \RuntimeException( $errors[$code] ?? sprintf( 'An unknown error occured with code %1$d', $code ) ); |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$fs = $this->context()->fs( $fsname ); |
|
115
|
|
|
|
|
116
|
|
|
if( ( $fs instanceof \Aimeos\Base\Filesystem\DirIface ) && !$fs->has( $dirname = dirname( $filepath ) ) ) { |
|
117
|
|
|
$fs->mkdir( $dirname ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$fs->writes( $filepath, $file->getStream()->detach() ); |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths