Completed
Push — master ( bcdba8...408d98 )
by Jan
03:54
created

Attachment::isURL()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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