1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/** |
5
|
|
|
* Part-DB Version 0.4+ "nextgen" |
6
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
7
|
|
|
* https://github.com/jbtronics. |
8
|
|
|
* |
9
|
|
|
* This program is free software; you can redistribute it and/or |
10
|
|
|
* modify it under the terms of the GNU General Public License |
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
12
|
|
|
* of the License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with this program; if not, write to the Free Software |
21
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace App\Entity\Attachments; |
25
|
|
|
|
26
|
|
|
use App\Entity\Base\DBElement; |
27
|
|
|
use App\Entity\Base\NamedDBElement; |
28
|
|
|
use App\Validator\Constraints\Selectable; |
29
|
|
|
use Doctrine\ORM\Mapping as ORM; |
30
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class Attachment. |
34
|
|
|
* |
35
|
|
|
* @ORM\Entity |
36
|
|
|
* @ORM\Table(name="`attachements`") |
37
|
|
|
* @ORM\InheritanceType("SINGLE_TABLE") |
38
|
|
|
* @ORM\DiscriminatorColumn(name="class_name", type="string") |
39
|
|
|
* @ORM\DiscriminatorMap({"PartDB\Part" = "PartAttachment", "Part" = "PartAttachment"}) |
40
|
|
|
* @ORM\EntityListeners({"App\EntityListeners\AttachmentDeleteListener"}) |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
|
|
abstract class Attachment extends NamedDBElement |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @var bool |
47
|
|
|
* @ORM\Column(type="boolean") |
48
|
|
|
*/ |
49
|
|
|
protected $show_in_table = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string The filename using the %BASE% variable |
53
|
|
|
* @ORM\Column(type="string", name="filename") |
54
|
|
|
*/ |
55
|
|
|
protected $path = ""; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* ORM mapping is done in sub classes (like PartAttachment) |
59
|
|
|
*/ |
60
|
|
|
protected $element; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var AttachmentType |
64
|
|
|
* @ORM\ManyToOne(targetEntity="AttachmentType", inversedBy="attachments") |
65
|
|
|
* @ORM\JoinColumn(name="type_id", referencedColumnName="id") |
66
|
|
|
* @Selectable() |
67
|
|
|
*/ |
68
|
|
|
protected $attachment_type; |
69
|
|
|
|
70
|
|
|
/*********************************************************** |
71
|
|
|
* Various function |
72
|
|
|
***********************************************************/ |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Check if this attachement is a picture (analyse the file's extension). |
76
|
|
|
* |
77
|
|
|
* @return bool * true if the file extension is a picture extension |
78
|
|
|
* * otherwise false |
79
|
|
|
*/ |
80
|
|
|
public function isPicture(): bool |
81
|
|
|
{ |
82
|
|
|
$extension = pathinfo($this->getPath(), PATHINFO_EXTENSION); |
83
|
|
|
|
84
|
|
|
// list all file extensions which are supported to display them by HTML code |
85
|
|
|
$picture_extensions = array('gif', 'png', 'jpg', 'jpeg', 'bmp', 'svg', 'tif'); |
86
|
|
|
|
87
|
|
|
return in_array(strtolower($extension), $picture_extensions, true); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Checks if the attachment file is externally saved (the database saves an URL) |
92
|
|
|
* @return bool true, if the file is saved externally |
93
|
|
|
*/ |
94
|
|
|
public function isExternal() : bool |
95
|
|
|
{ |
96
|
|
|
//return static::isUrl($this->getPath()); |
97
|
|
|
//Treat all pathes without a filepath as external |
98
|
|
|
return (strpos($this->getPath(), "%MEDIA%") === false) |
99
|
|
|
&& (strpos($this->getPath(), "%BASE") === false); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/******************************************************************************** |
103
|
|
|
* |
104
|
|
|
* Getters |
105
|
|
|
* |
106
|
|
|
*********************************************************************************/ |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the extension of the file referenced via the attachment. |
110
|
|
|
* For a path like %BASE/path/foo.bar, bar will be returned. |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getExtension() : string |
114
|
|
|
{ |
115
|
|
|
return pathinfo($this->getPath(), PATHINFO_EXTENSION); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the element, associated with this Attachement (for example a "Part" object). |
120
|
|
|
* |
121
|
|
|
* @return AttachmentContainingDBElement The associated Element. |
122
|
|
|
*/ |
123
|
|
|
public function getElement(): ?AttachmentContainingDBElement |
124
|
|
|
{ |
125
|
|
|
return $this->element; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* The URL to the external file. |
130
|
|
|
* Returns null, if the file is not external. |
131
|
|
|
* @return string|null |
132
|
|
|
*/ |
133
|
|
|
public function getURL(): ?string |
134
|
|
|
{ |
135
|
|
|
if (!$this->isExternal()) { |
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this->path; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns the hostname where the external file is stored. |
144
|
|
|
* Returns null, if the file is not external. |
145
|
|
|
* @return string|null |
146
|
|
|
*/ |
147
|
|
|
public function getHost(): ?string |
148
|
|
|
{ |
149
|
|
|
if (!$this->isExternal()) { |
150
|
|
|
return null; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return parse_url($this->getURL(), PHP_URL_HOST); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the filepath, relative to %BASE%. |
158
|
|
|
* |
159
|
|
|
* @return string A string like %BASE/path/foo.bar |
160
|
|
|
*/ |
161
|
|
|
public function getPath(): string |
162
|
|
|
{ |
163
|
|
|
return $this->path; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns the filename of the attachment. |
170
|
|
|
* For a path like %BASE/path/foo.bar, foo.bar will be returned. |
171
|
|
|
* |
172
|
|
|
* If the path is a URL (can be checked via isExternal()), null will be returned. |
173
|
|
|
* |
174
|
|
|
* @return string|null |
175
|
|
|
*/ |
176
|
|
|
public function getFilename(): ?string |
177
|
|
|
{ |
178
|
|
|
if ($this->isExternal()) { |
179
|
|
|
return null; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return pathinfo($this->getPath(), PATHINFO_BASENAME); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get the show_in_table attribute. |
187
|
|
|
* |
188
|
|
|
* @return bool true means, this attachement will be listed in the "Attachements" column of the HTML tables |
189
|
|
|
* false means, this attachement won't be listed in the "Attachements" column of the HTML tables |
190
|
|
|
*/ |
191
|
|
|
public function getShowInTable(): bool |
192
|
|
|
{ |
193
|
|
|
return (bool) $this->show_in_table; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get the type of this attachement. |
198
|
|
|
* |
199
|
|
|
* @return AttachmentType the type of this attachement |
200
|
|
|
* |
201
|
|
|
*/ |
202
|
|
|
public function getAttachmentType(): ?AttachmentType |
203
|
|
|
{ |
204
|
|
|
return $this->attachment_type; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Returns the ID as an string, defined by the element class. |
209
|
|
|
* This should have a form like P000014, for a part with ID 14. |
210
|
|
|
* |
211
|
|
|
* @return string The ID as a string; |
212
|
|
|
*/ |
213
|
|
|
public function getIDString(): string |
214
|
|
|
{ |
215
|
|
|
return 'A'.sprintf('%09d', $this->getID()); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/***************************************************************************************************** |
219
|
|
|
* Setters |
220
|
|
|
****************************************************************************************************/ |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param bool $show_in_table |
224
|
|
|
* |
225
|
|
|
* @return self |
226
|
|
|
*/ |
227
|
|
|
public function setShowInTable(bool $show_in_table): self |
228
|
|
|
{ |
229
|
|
|
$this->show_in_table = $show_in_table; |
230
|
|
|
|
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
abstract public function setElement(AttachmentContainingDBElement $element) : Attachment; |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param string $path |
238
|
|
|
* @return Attachment |
239
|
|
|
*/ |
240
|
|
|
public function setPath(string $path): Attachment |
241
|
|
|
{ |
242
|
|
|
$this->path = $path; |
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param AttachmentType $attachement_type |
248
|
|
|
* @return Attachment |
249
|
|
|
*/ |
250
|
|
|
public function setAttachmentType(AttachmentType $attachement_type): Attachment |
251
|
|
|
{ |
252
|
|
|
$this->attachment_type = $attachement_type; |
253
|
|
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Sets the url associated with this attachment. |
258
|
|
|
* If the url is empty nothing is changed, to not override the file path. |
259
|
|
|
* @param string|null $url |
260
|
|
|
* @return Attachment |
261
|
|
|
*/ |
262
|
|
|
public function setURL(?string $url) : Attachment |
263
|
|
|
{ |
264
|
|
|
//Only set if the URL is not empty |
265
|
|
|
if (!empty($url)) { |
266
|
|
|
if (strpos($url, '%BASE%') !== false || strpos($url, '%MEDIA%') !== false) { |
267
|
|
|
throw new \InvalidArgumentException("You can not reference internal files via the url field! But nice try!"); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$this->path = $url; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
|
277
|
|
|
/***************************************************************************************************** |
278
|
|
|
* Static functions |
279
|
|
|
*****************************************************************************************************/ |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Check if a string is a URL and is valid. |
283
|
|
|
* @param $string string The string which should be checked. |
284
|
|
|
* @param bool $path_required If true, the string must contain a path to be valid. (e.g. foo.bar would be invalid, foo.bar/test.php would be valid). |
285
|
|
|
* @param $only_http bool Set this to true, if only HTTPS or HTTP schemata should be allowed. |
286
|
|
|
* *Caution: When this is set to false, a attacker could use the file:// schema, to get internal server files, like /etc/passwd.* |
287
|
|
|
* @return bool True if the string is a valid URL. False, if the string is not an URL or invalid. |
288
|
|
|
*/ |
289
|
|
|
public static function isURL(string $string, bool $path_required = true, bool $only_http = true) : bool |
290
|
|
|
{ |
291
|
|
|
if ($only_http) { //Check if scheme is HTTPS or HTTP |
292
|
|
|
$scheme = parse_url($string, PHP_URL_SCHEME); |
293
|
|
|
if ($scheme !== 'http' && $scheme !== 'https') { |
294
|
|
|
return false; //All other schemes are not valid. |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
if ($path_required) { |
298
|
|
|
return (bool) filter_var($string, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return (bool) filter_var($string, FILTER_VALIDATE_URL); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|