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
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class Attachment. |
33
|
|
|
* |
34
|
|
|
* @ORM\Entity |
35
|
|
|
* @ORM\Table(name="`attachements`") |
36
|
|
|
* @ORM\InheritanceType("SINGLE_TABLE") |
37
|
|
|
* @ORM\DiscriminatorColumn(name="class_name", type="string") |
38
|
|
|
* @ORM\DiscriminatorMap({"PartDB\Part" = "PartAttachment", "Part" = "PartAttachment"}) |
39
|
|
|
* |
40
|
|
|
*/ |
41
|
|
|
abstract class Attachment extends NamedDBElement |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var bool |
45
|
|
|
* @ORM\Column(type="boolean") |
46
|
|
|
*/ |
47
|
|
|
protected $show_in_table = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string The filename using the %BASE% variable |
51
|
|
|
* @ORM\Column(type="string", name="filename") |
52
|
|
|
*/ |
53
|
|
|
protected $path = ""; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* ORM mapping is done in sub classes (like PartAttachment) |
57
|
|
|
*/ |
58
|
|
|
protected $element; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var AttachmentType |
62
|
|
|
* @ORM\ManyToOne(targetEntity="AttachmentType", inversedBy="attachments") |
63
|
|
|
* @ORM\JoinColumn(name="type_id", referencedColumnName="id") |
64
|
|
|
* @Selectable() |
65
|
|
|
*/ |
66
|
|
|
protected $attachment_type; |
67
|
|
|
|
68
|
|
|
/*********************************************************** |
69
|
|
|
* Various function |
70
|
|
|
***********************************************************/ |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Check if this attachement is a picture (analyse the file's extension). |
74
|
|
|
* |
75
|
|
|
* @return bool * true if the file extension is a picture extension |
76
|
|
|
* * otherwise false |
77
|
|
|
*/ |
78
|
|
|
public function isPicture(): bool |
79
|
|
|
{ |
80
|
|
|
$extension = pathinfo($this->getPath(), PATHINFO_EXTENSION); |
81
|
|
|
|
82
|
|
|
// list all file extensions which are supported to display them by HTML code |
83
|
|
|
$picture_extensions = array('gif', 'png', 'jpg', 'jpeg', 'bmp', 'svg', 'tif'); |
84
|
|
|
|
85
|
|
|
return in_array(strtolower($extension), $picture_extensions, true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Checks if the attachment file is externally saved (the database saves an URL) |
90
|
|
|
* @return bool true, if the file is saved externally |
91
|
|
|
*/ |
92
|
|
|
public function isExternal() : bool |
93
|
|
|
{ |
94
|
|
|
return static::isUrl($this->getPath()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/******************************************************************************** |
98
|
|
|
* |
99
|
|
|
* Getters |
100
|
|
|
* |
101
|
|
|
*********************************************************************************/ |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the extension of the file referenced via the attachment. |
105
|
|
|
* For a path like %BASE/path/foo.bar, bar will be returned. |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function getExtension() : string |
109
|
|
|
{ |
110
|
|
|
return pathinfo($this->getPath(), PATHINFO_EXTENSION); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the element, associated with this Attachement (for example a "Part" object). |
115
|
|
|
* |
116
|
|
|
* @return AttachmentContainingDBElement The associated Element. |
117
|
|
|
*/ |
118
|
|
|
public function getElement(): ?AttachmentContainingDBElement |
119
|
|
|
{ |
120
|
|
|
return $this->element; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Checks if the file in this attachement is existing. This works for files on the HDD, and for URLs |
125
|
|
|
* (it's not checked if the ressource behind the URL is really existing). |
126
|
|
|
* |
127
|
|
|
* @return bool True if the file is existing. |
128
|
|
|
*/ |
129
|
|
|
public function isFileExisting(): bool |
130
|
|
|
{ |
131
|
|
|
return file_exists($this->getPath()) || static::isURL($this->getPath()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* The URL to the external file. |
136
|
|
|
* Returns null, if the file is not external. |
137
|
|
|
* @return string|null |
138
|
|
|
*/ |
139
|
|
|
public function getURL(): ?string |
140
|
|
|
{ |
141
|
|
|
if (!$this->isExternal()) { |
142
|
|
|
return null; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this->path; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Returns the hostname where the external file is stored. |
150
|
|
|
* Returns null, if the file is not external. |
151
|
|
|
* @return string|null |
152
|
|
|
*/ |
153
|
|
|
public function getHost(): ?string |
154
|
|
|
{ |
155
|
|
|
if (!$this->isExternal()) { |
156
|
|
|
return null; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return parse_url($this->getURL(), PHP_URL_HOST); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get the filepath, relative to %BASE%. |
164
|
|
|
* |
165
|
|
|
* @return string A string like %BASE/path/foo.bar |
166
|
|
|
*/ |
167
|
|
|
public function getPath(): string |
168
|
|
|
{ |
169
|
|
|
return $this->path; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Returns the filename of the attachment. |
176
|
|
|
* For a path like %BASE/path/foo.bar, foo.bar will be returned. |
177
|
|
|
* |
178
|
|
|
* If the path is a URL (can be checked via isExternal()), null will be returned. |
179
|
|
|
* |
180
|
|
|
* @return string|null |
181
|
|
|
*/ |
182
|
|
|
public function getFilename(): ?string |
183
|
|
|
{ |
184
|
|
|
if ($this->isExternal()) { |
185
|
|
|
return null; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return pathinfo($this->getPath(), PATHINFO_BASENAME); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get the show_in_table attribute. |
193
|
|
|
* |
194
|
|
|
* @return bool true means, this attachement will be listed in the "Attachements" column of the HTML tables |
195
|
|
|
* false means, this attachement won't be listed in the "Attachements" column of the HTML tables |
196
|
|
|
*/ |
197
|
|
|
public function getShowInTable(): bool |
198
|
|
|
{ |
199
|
|
|
return (bool) $this->show_in_table; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get the type of this attachement. |
204
|
|
|
* |
205
|
|
|
* @return AttachmentType the type of this attachement |
206
|
|
|
* |
207
|
|
|
*/ |
208
|
|
|
public function getAttachmentType(): ?AttachmentType |
209
|
|
|
{ |
210
|
|
|
return $this->attachment_type; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns the ID as an string, defined by the element class. |
215
|
|
|
* This should have a form like P000014, for a part with ID 14. |
216
|
|
|
* |
217
|
|
|
* @return string The ID as a string; |
218
|
|
|
*/ |
219
|
|
|
public function getIDString(): string |
220
|
|
|
{ |
221
|
|
|
return 'A'.sprintf('%09d', $this->getID()); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/***************************************************************************************************** |
225
|
|
|
* Setters |
226
|
|
|
****************************************************************************************************/ |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param bool $show_in_table |
230
|
|
|
* |
231
|
|
|
* @return self |
232
|
|
|
*/ |
233
|
|
|
public function setShowInTable(bool $show_in_table): self |
234
|
|
|
{ |
235
|
|
|
$this->show_in_table = $show_in_table; |
236
|
|
|
|
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
abstract public function setElement(AttachmentContainingDBElement $element) : Attachment; |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string $path |
244
|
|
|
* @return Attachment |
245
|
|
|
*/ |
246
|
|
|
public function setPath(string $path): Attachment |
247
|
|
|
{ |
248
|
|
|
$this->path = $path; |
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param AttachmentType $attachement_type |
254
|
|
|
* @return Attachment |
255
|
|
|
*/ |
256
|
|
|
public function setAttachmentType(AttachmentType $attachement_type): Attachment |
257
|
|
|
{ |
258
|
|
|
$this->attachment_type = $attachement_type; |
259
|
|
|
return $this; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public function setURL(?string $url) : Attachment |
263
|
|
|
{ |
264
|
|
|
//Only set if the URL is not empty |
265
|
|
|
if (!empty($url)) { |
266
|
|
|
$this->path = $url; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
|
273
|
|
|
/***************************************************************************************************** |
274
|
|
|
* Static functions |
275
|
|
|
*****************************************************************************************************/ |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Check if a string is a URL and is valid. |
279
|
|
|
* @param $string string The string which should be checked. |
280
|
|
|
* @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). |
281
|
|
|
* @param $only_http bool Set this to true, if only HTTPS or HTTP schemata should be allowed. |
282
|
|
|
* *Caution: When this is set to false, a attacker could use the file:// schema, to get internal server files, like /etc/passwd.* |
283
|
|
|
* @return bool True if the string is a valid URL. False, if the string is not an URL or invalid. |
284
|
|
|
*/ |
285
|
|
|
public static function isURL(string $string, bool $path_required = true, bool $only_http = true) : bool |
286
|
|
|
{ |
287
|
|
|
if ($only_http) { //Check if scheme is HTTPS or HTTP |
288
|
|
|
$scheme = parse_url($string, PHP_URL_SCHEME); |
289
|
|
|
if ($scheme !== 'http' && $scheme !== 'https') { |
290
|
|
|
return false; //All other schemes are not valid. |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
if ($path_required) { |
294
|
|
|
return (bool) filter_var($string, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
return (bool) filter_var($string, FILTER_VALIDATE_URL); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|