1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* part-db version 0.1 |
5
|
|
|
* Copyright (C) 2005 Christoph Lechner |
6
|
|
|
* http://www.cl-projects.de/ |
7
|
|
|
* |
8
|
|
|
* part-db version 0.2+ |
9
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
10
|
|
|
* http://code.google.com/p/part-db/ |
11
|
|
|
* |
12
|
|
|
* Part-DB Version 0.4+ |
13
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
14
|
|
|
* https://github.com/jbtronics |
15
|
|
|
* |
16
|
|
|
* This program is free software; you can redistribute it and/or |
17
|
|
|
* modify it under the terms of the GNU General Public License |
18
|
|
|
* as published by the Free Software Foundation; either version 2 |
19
|
|
|
* of the License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU General Public License |
27
|
|
|
* along with this program; if not, write to the Free Software |
28
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
namespace App\Services\Attachments; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
36
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* This service converts the relative pathes for attachments saved in database (like %MEDIA%/img.jpg) to real pathes |
40
|
|
|
* an vice versa. |
41
|
|
|
* @package App\Services\Attachments |
42
|
|
|
*/ |
43
|
|
|
class AttachmentPathResolver |
44
|
|
|
{ |
45
|
|
|
protected $project_dir; |
46
|
|
|
|
47
|
|
|
protected $media_path; |
48
|
|
|
protected $footprints_path; |
49
|
|
|
protected $models_path; |
50
|
|
|
protected $secure_path; |
51
|
|
|
|
52
|
|
|
protected $placeholders; |
53
|
|
|
protected $pathes; |
54
|
|
|
protected $placeholders_regex; |
55
|
|
|
protected $pathes_regex; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* AttachmentPathResolver constructor. |
59
|
|
|
* @param string $project_dir The kernel that should be used to resolve the project dir. |
60
|
|
|
* @param string $media_path The path where uploaded attachments should be stored. |
61
|
|
|
* @param string|null $footprints_path The path where builtin attachments are stored. |
62
|
|
|
* Set to null if this ressource should be disabled. |
63
|
|
|
* @param string|null $models_path Set to null if this ressource should be disabled. |
64
|
|
|
*/ |
65
|
|
|
public function __construct(string $project_dir, string $media_path, string $secure_path, ?string $footprints_path, ?string $models_path) |
66
|
|
|
{ |
67
|
|
|
$this->project_dir = $project_dir; |
68
|
|
|
|
69
|
|
|
//Determine the path for our ressources |
70
|
|
|
$this->media_path = $this->parameterToAbsolutePath($media_path); |
71
|
|
|
$this->footprints_path = $this->parameterToAbsolutePath($footprints_path); |
72
|
|
|
$this->models_path = $this->parameterToAbsolutePath($models_path); |
73
|
|
|
$this->secure_path = $this->parameterToAbsolutePath($secure_path); |
74
|
|
|
|
75
|
|
|
//Here we define the valid placeholders and their replacement values |
76
|
|
|
$this->placeholders = ['%MEDIA%', '%BASE%/data/media', '%FOOTPRINTS%', '%FOOTPRINTS_3D%', '%SECURE%']; |
77
|
|
|
$this->pathes = [$this->media_path, $this->media_path, $this->footprints_path, $this->models_path, $this->secure_path]; |
78
|
|
|
|
79
|
|
|
//Remove all disabled placeholders |
80
|
|
|
foreach ($this->pathes as $key => $path) { |
81
|
|
|
if ($path === null) { |
82
|
|
|
unset($this->placeholders[$key], $this->pathes[$key]); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
//Create the regex arrays |
87
|
|
|
$this->placeholders_regex = $this->arrayToRegexArray($this->placeholders); |
88
|
|
|
$this->pathes_regex = $this->arrayToRegexArray($this->pathes); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Converts a path passed by parameter from services.yaml (which can be an absolute path or relative to project dir) |
93
|
|
|
* to an absolute path. When a relative path is passed, the directory must exist or null is returned. |
94
|
|
|
* @internal |
95
|
|
|
* @param string|null $param_path The parameter value that should be converted to a absolute path |
96
|
|
|
* @return string|null |
97
|
|
|
*/ |
98
|
|
|
public function parameterToAbsolutePath(?string $param_path) : ?string |
99
|
|
|
{ |
100
|
|
|
if ($param_path === null) { |
101
|
|
|
return null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$fs = new Filesystem(); |
105
|
|
|
//If current string is already an absolute path, then we have nothing to do |
106
|
|
|
if ($fs->isAbsolutePath($param_path)) { |
107
|
|
|
$tmp = realpath($param_path); |
108
|
|
|
//Disable ressource if path is not existing |
109
|
|
|
if ($tmp === false) { |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
return $tmp; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
//Otherwise prepend the project path |
116
|
|
|
$tmp = realpath($this->project_dir . DIRECTORY_SEPARATOR . $param_path); |
117
|
|
|
|
118
|
|
|
//If path does not exist then disable the placeholder |
119
|
|
|
if ($tmp === false) { |
120
|
|
|
return null; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
//Otherwise return resolved path |
124
|
|
|
return $tmp; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Create an array usable for preg_replace out of an array of placeholders or pathes. |
129
|
|
|
* Slashes and other chars become escaped. |
130
|
|
|
* For example: '%TEST%' becomes '/^%TEST%/'. |
131
|
|
|
* @param array $array |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
protected function arrayToRegexArray(array $array) : array |
135
|
|
|
{ |
136
|
|
|
$ret = []; |
137
|
|
|
|
138
|
|
|
foreach ($array as $item) { |
139
|
|
|
$item = str_replace(['\\'], ['/'], $item); |
140
|
|
|
$ret[] = '/' . preg_quote($item, '/') . '/'; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $ret; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Converts an relative placeholder filepath (with %MEDIA% or older %BASE%) to an absolute filepath on disk. |
149
|
|
|
* The directory separator is always /. Relative pathes are not realy possible (.. is striped) |
150
|
|
|
* @param string $placeholder_path The filepath with placeholder for which the real path should be determined. |
151
|
|
|
* @return string|null The absolute real path of the file, or null if the placeholder path is invalid |
152
|
|
|
*/ |
153
|
|
|
public function placeholderToRealPath(string $placeholder_path) : ?string |
154
|
|
|
{ |
155
|
|
|
//The new attachments use %MEDIA% as placeholders, which is the directory set in media_directory |
156
|
|
|
//Older path entries are given via %BASE% which was the project root |
157
|
|
|
|
158
|
|
|
$count = 0; |
159
|
|
|
$placeholder_path = preg_replace($this->placeholders_regex, $this->pathes, $placeholder_path,-1,$count); |
160
|
|
|
|
161
|
|
|
//A valid placeholder can have only one |
162
|
|
|
if ($count !== 1) { |
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
//If we have now have a placeholder left, the string is invalid: |
167
|
|
|
if (preg_match('/%\w+%/', $placeholder_path)) { |
168
|
|
|
return null; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
//Path is invalid if path is directory traversal |
172
|
|
|
if (strpos($placeholder_path, '..') !== false) { |
173
|
|
|
return null; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
//Normalize path and remove .. (to prevent directory traversal attack) |
177
|
|
|
$placeholder_path = str_replace(['\\'], ['/'], $placeholder_path); |
178
|
|
|
|
179
|
|
|
return $placeholder_path; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Converts an real absolute filepath to a placeholder version. |
184
|
|
|
* @param string $real_path The absolute path, for which the placeholder version should be generated. |
185
|
|
|
* @param bool $old_version By default the %MEDIA% placeholder is used, which is directly replaced with the |
186
|
|
|
* media directory. If set to true, the old version with %BASE% will be used, which is the project directory. |
187
|
|
|
* @return string The placeholder version of the filepath |
188
|
|
|
*/ |
189
|
|
|
public function realPathToPlaceholder(string $real_path, bool $old_version = false) : ?string |
190
|
|
|
{ |
191
|
|
|
$count = 0; |
192
|
|
|
|
193
|
|
|
//Normalize path |
194
|
|
|
$real_path = str_replace('\\', '/', $real_path); |
195
|
|
|
|
196
|
|
|
if ($old_version) { |
197
|
|
|
//We need to remove the %MEDIA% placeholder (element 0) |
198
|
|
|
$pathes = $this->pathes_regex; |
199
|
|
|
$placeholders = $this->placeholders; |
200
|
|
|
unset($pathes[0], $placeholders[0]); |
201
|
|
|
$real_path = preg_replace($pathes, $placeholders, $real_path, -1, $count); |
202
|
|
|
} else { |
203
|
|
|
$real_path = preg_replace($this->pathes_regex, $this->placeholders, $real_path, -1, $count); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if ($count !== 1) { |
207
|
|
|
return null; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
//If the new string does not begin with a placeholder, it is invalid |
211
|
|
|
if (!preg_match('/^%\w+%/', $real_path)) { |
212
|
|
|
return null; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $real_path; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* The path where uploaded attachments is stored. |
220
|
|
|
* @return string The absolute path to the media folder. |
221
|
|
|
*/ |
222
|
|
|
public function getMediaPath() : string |
223
|
|
|
{ |
224
|
|
|
return $this->media_path; |
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* The path where secured attachments are stored. Must not be located in public/ folder, so it can only be accessed |
229
|
|
|
* via the attachment controller. |
230
|
|
|
* @return string The absolute path to the secure path. |
231
|
|
|
*/ |
232
|
|
|
public function getSecurePath() : string |
233
|
|
|
{ |
234
|
|
|
return $this->secure_path; |
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* The string where the builtin footprints are stored |
239
|
|
|
* @return string|null The absolute path to the footprints folder. Null if built footprints were disabled. |
240
|
|
|
*/ |
241
|
|
|
public function getFootprintsPath() : ?string |
242
|
|
|
{ |
243
|
|
|
return $this->footprints_path; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* The string where the builtin 3D models are stored |
248
|
|
|
* @return string|null The absolute path to the models folder. Null if builtin models were disabled. |
249
|
|
|
*/ |
250
|
|
|
public function getModelsPath() : ?string |
251
|
|
|
{ |
252
|
|
|
return $this->models_path; |
253
|
|
|
} |
254
|
|
|
} |