Completed
Push — master ( 87db25...ca4834 )
by Jan
04:01
created

Attachment::getHost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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