Completed
Push — master ( 717b25...01a83d )
by Jan
03:55
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
nc 5
nop 3
dl 0
loc 12
rs 9.6111
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
     * Get the filepath, relative to %BASE%.
133
     *
134
     * @return string A string like %BASE/path/foo.bar
135
     */
136
    public function getPath(): string
137
    {
138
        return $this->path;
139
    }
140
141
    /**
142
     * Returns the filename of the attachment.
143
     * For a path like %BASE/path/foo.bar, foo.bar will be returned.
144
     *
145
     * If the path is a URL (can be checked via isExternal()), null will be returned.
146
     *
147
     * @return string|null
148
     */
149
    public function getFilename(): ?string
150
    {
151
        if ($this->isExternal()) {
152
            return null;
153
        }
154
155
        return pathinfo($this->getPath(), PATHINFO_BASENAME);
156
    }
157
158
    /**
159
     * Get the show_in_table attribute.
160
     *
161
     * @return bool true means, this attachement will be listed in the "Attachements" column of the HTML tables
162
     *              false means, this attachement won't be listed in the "Attachements" column of the HTML tables
163
     */
164
    public function getShowInTable(): bool
165
    {
166
        return (bool) $this->show_in_table;
167
    }
168
169
    /**
170
     *  Get the type of this attachement.
171
     *
172
     * @return AttachmentType the type of this attachement
173
     *
174
     */
175
    public function getType(): AttachmentType
176
    {
177
        return $this->attachement_type;
178
    }
179
180
    /**
181
     * Returns the ID as an string, defined by the element class.
182
     * This should have a form like P000014, for a part with ID 14.
183
     *
184
     * @return string The ID as a string;
185
     */
186
    public function getIDString(): string
187
    {
188
        return 'A'.sprintf('%09d', $this->getID());
189
    }
190
191
    /*****************************************************************************************************
192
     * Setters
193
     ****************************************************************************************************/
194
195
    /**
196
     * @param bool $show_in_table
197
     *
198
     * @return self
199
     */
200
    public function setShowInTable(bool $show_in_table): self
201
    {
202
        $this->show_in_table = $show_in_table;
203
204
        return $this;
205
    }
206
207
    /*****************************************************************************************************
208
     * Static functions
209
     *****************************************************************************************************/
210
211
    /**
212
     * Check if a string is a URL and is valid.
213
     * @param $string string The string which should be checked.
214
     * @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).
215
     * @param $only_http bool Set this to true, if only HTTPS or HTTP schemata should be allowed.
216
     *  *Caution: When this is set to false, a attacker could use the file:// schema, to get internal server files, like /etc/passwd.*
217
     * @return bool True if the string is a valid URL. False, if the string is not an URL or invalid.
218
     */
219
    public static function isURL(string $string, bool $path_required = true, bool $only_http = true) : bool
220
    {
221
        if ($only_http) {   //Check if scheme is HTTPS or HTTP
222
            $scheme = parse_url($string, PHP_URL_SCHEME);
223
            if ($scheme !== 'http' && $scheme !== 'https') {
224
                return false;   //All other schemes are not valid.
225
            }
226
        }
227
        if ($path_required) {
228
            return (bool) filter_var($string, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED);
229
        } else {
230
            return (bool) filter_var($string, FILTER_VALIDATE_URL);
231
        }
232
    }
233
}
234