2019-03-20 23:16:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
|
|
|
* Part-DB Version 0.4+ "nextgen"
|
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer
|
2019-03-20 23:16:07 +01:00
|
|
|
* https://github.com/jbtronics.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
namespace App\Entity\Attachments;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
use App\Entity\Base\NamedDBElement;
|
2019-08-12 21:47:25 +02:00
|
|
|
use App\Validator\Constraints\Selectable;
|
2019-02-23 22:41:13 +01:00
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Class Attachment.
|
|
|
|
*
|
2019-02-23 22:41:13 +01:00
|
|
|
* @ORM\Entity
|
2019-08-29 13:06:04 +02:00
|
|
|
* @ORM\Table(name="`attachments`")
|
2019-08-06 13:18:29 +02:00
|
|
|
* @ORM\InheritanceType("SINGLE_TABLE")
|
|
|
|
* @ORM\DiscriminatorColumn(name="class_name", type="string")
|
2019-09-24 13:39:49 +02:00
|
|
|
* @ORM\DiscriminatorMap({
|
|
|
|
* "PartDB\Part" = "PartAttachment", "Part" = "PartAttachment",
|
|
|
|
* "PartDB\Device" = "DeviceAttachment", "Device" = "DeviceAttachment",
|
|
|
|
* "AttachmentType" = "AttachmentTypeAttachment", "Category" = "CategoryAttachment",
|
|
|
|
* "Footprint" = "FootprintAttachment", "Manufacturer" = "ManufacturerAttachment",
|
|
|
|
* "Currency" = "CurrencyAttachment", "Group" = "GroupAttachment",
|
|
|
|
* "MeasurementUnit" = "MeasurementUnitAttachment", "Storelocation" = "StorelocationAttachment",
|
|
|
|
* "Supplier" = "SupplierAttachment", "User" = "UserAttachment"
|
|
|
|
* })
|
2019-08-27 22:24:56 +02:00
|
|
|
* @ORM\EntityListeners({"App\EntityListeners\AttachmentDeleteListener"})
|
2019-08-06 13:18:29 +02:00
|
|
|
*
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-08-06 13:18:29 +02:00
|
|
|
abstract class Attachment extends NamedDBElement
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2019-09-22 21:25:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of file extensions, that browsers can show directly as image.
|
|
|
|
* Based on: https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types
|
|
|
|
* It will be used to determine if a attachment is a picture and therefore will be shown to user as preview.
|
|
|
|
*/
|
|
|
|
const PICTURE_EXTS = ['apng', 'bmp', 'gif', 'ico', 'cur', 'jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp', 'png',
|
|
|
|
'svg', 'webp'];
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
* @ORM\Column(type="boolean")
|
|
|
|
*/
|
2019-08-26 23:30:35 +02:00
|
|
|
protected $show_in_table = false;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
|
|
|
/**
|
2019-09-24 13:39:49 +02:00
|
|
|
* @var string The path to the file relative to a placeholder path like %MEDIA%
|
|
|
|
* @ORM\Column(type="string", name="path")
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-09-16 22:04:59 +02:00
|
|
|
protected $path = '';
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2019-09-24 13:39:49 +02:00
|
|
|
/**
|
|
|
|
* @var string The original filenamethe file had, when the user uploaded it.
|
|
|
|
* @ORM\Column(type="string", nullable=true)
|
|
|
|
*/
|
|
|
|
protected $original_filename;
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
2019-08-06 13:18:29 +02:00
|
|
|
* ORM mapping is done in sub classes (like PartAttachment)
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
|
|
|
protected $element;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var AttachmentType
|
2019-09-24 13:39:49 +02:00
|
|
|
* @ORM\ManyToOne(targetEntity="AttachmentType", inversedBy="attachments_with_type")
|
2019-02-23 22:41:13 +01:00
|
|
|
* @ORM\JoinColumn(name="type_id", referencedColumnName="id")
|
2019-08-12 21:47:25 +02:00
|
|
|
* @Selectable()
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-08-26 23:30:35 +02:00
|
|
|
protected $attachment_type;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
|
|
|
/***********************************************************
|
|
|
|
* Various function
|
|
|
|
***********************************************************/
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Check if this attachement is a picture (analyse the file's extension).
|
2019-09-02 23:09:58 +02:00
|
|
|
* If the link is external, it is assumed that this is false.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-03-20 23:16:07 +01:00
|
|
|
* @return bool * true if the file extension is a picture extension
|
|
|
|
* * otherwise false
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function isPicture(): bool
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2019-09-02 23:09:58 +02:00
|
|
|
//We can not check if a external link is a picture, so just assume this is false
|
|
|
|
if ($this->isExternal()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-06 18:47:09 +02:00
|
|
|
$extension = pathinfo($this->getPath(), PATHINFO_EXTENSION);
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2019-09-22 21:25:06 +02:00
|
|
|
return in_array(strtolower($extension), static::PICTURE_EXTS, true);
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
2019-08-06 18:47:09 +02:00
|
|
|
/**
|
|
|
|
* Checks if the attachment file is externally saved (the database saves an URL)
|
|
|
|
* @return bool true, if the file is saved externally
|
|
|
|
*/
|
|
|
|
public function isExternal() : bool
|
|
|
|
{
|
2019-08-27 13:15:18 +02:00
|
|
|
//Treat all pathes without a filepath as external
|
2019-09-22 23:47:40 +02:00
|
|
|
return (strpos($this->getPath(), '%MEDIA%') !== 0)
|
|
|
|
&& (strpos($this->getPath(), '%BASE%') !== 0);
|
2019-08-06 18:47:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/********************************************************************************
|
|
|
|
*
|
|
|
|
* Getters
|
|
|
|
*
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-08-06 18:47:09 +02:00
|
|
|
/**
|
|
|
|
* Returns the extension of the file referenced via the attachment.
|
|
|
|
* For a path like %BASE/path/foo.bar, bar will be returned.
|
2019-09-22 23:47:40 +02:00
|
|
|
* If this attachment is external null is returned.
|
|
|
|
* @return string|null The file extension in lower case.
|
2019-08-06 18:47:09 +02:00
|
|
|
*/
|
2019-09-22 23:47:40 +02:00
|
|
|
public function getExtension() : ?string
|
2019-08-06 18:47:09 +02:00
|
|
|
{
|
2019-09-22 23:47:40 +02:00
|
|
|
if ($this->isExternal()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return strtolower(pathinfo($this->getPath(), PATHINFO_EXTENSION));
|
2019-08-06 18:47:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Get the element, associated with this Attachement (for example a "Part" object).
|
|
|
|
*
|
2019-08-20 18:39:57 +02:00
|
|
|
* @return AttachmentContainingDBElement The associated Element.
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-04-05 17:49:02 +02:00
|
|
|
public function getElement(): ?AttachmentContainingDBElement
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
|
|
|
return $this->element;
|
|
|
|
}
|
|
|
|
|
2019-08-10 19:16:56 +02:00
|
|
|
/**
|
|
|
|
* The URL to the external file.
|
|
|
|
* Returns null, if the file is not external.
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getURL(): ?string
|
|
|
|
{
|
|
|
|
if (!$this->isExternal()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the hostname where the external file is stored.
|
|
|
|
* Returns null, if the file is not external.
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getHost(): ?string
|
|
|
|
{
|
|
|
|
if (!$this->isExternal()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parse_url($this->getURL(), PHP_URL_HOST);
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
2019-08-06 18:47:09 +02:00
|
|
|
* Get the filepath, relative to %BASE%.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-08-06 18:47:09 +02:00
|
|
|
* @return string A string like %BASE/path/foo.bar
|
|
|
|
*/
|
|
|
|
public function getPath(): string
|
|
|
|
{
|
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
2019-08-10 19:16:56 +02:00
|
|
|
|
|
|
|
|
2019-08-06 18:47:09 +02:00
|
|
|
/**
|
|
|
|
* Returns the filename of the attachment.
|
|
|
|
* For a path like %BASE/path/foo.bar, foo.bar will be returned.
|
|
|
|
*
|
|
|
|
* If the path is a URL (can be checked via isExternal()), null will be returned.
|
|
|
|
*
|
|
|
|
* @return string|null
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-08-06 18:47:09 +02:00
|
|
|
public function getFilename(): ?string
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2019-08-06 18:47:09 +02:00
|
|
|
if ($this->isExternal()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pathinfo($this->getPath(), PATHINFO_BASENAME);
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Get the show_in_table attribute.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-03-20 23:16:07 +01:00
|
|
|
* @return bool true means, this attachement will be listed in the "Attachements" column of the HTML tables
|
|
|
|
* false means, this attachement won't be listed in the "Attachements" column of the HTML tables
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function getShowInTable(): bool
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
|
|
|
return (bool) $this->show_in_table;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Get the type of this attachement.
|
|
|
|
*
|
|
|
|
* @return AttachmentType the type of this attachement
|
|
|
|
*
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-08-26 23:30:35 +02:00
|
|
|
public function getAttachmentType(): ?AttachmentType
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2019-08-26 23:30:35 +02:00
|
|
|
return $this->attachment_type;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ID as an string, defined by the element class.
|
|
|
|
* This should have a form like P000014, for a part with ID 14.
|
2019-03-20 23:16:07 +01:00
|
|
|
*
|
2019-02-23 22:41:13 +01:00
|
|
|
* @return string The ID as a string;
|
|
|
|
*/
|
|
|
|
public function getIDString(): string
|
|
|
|
{
|
2019-03-20 23:16:07 +01:00
|
|
|
return 'A'.sprintf('%09d', $this->getID());
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
2019-03-05 14:52:23 +01:00
|
|
|
|
|
|
|
/*****************************************************************************************************
|
|
|
|
* Setters
|
|
|
|
****************************************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $show_in_table
|
2019-03-20 23:16:07 +01:00
|
|
|
*
|
2019-03-05 14:52:23 +01:00
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setShowInTable(bool $show_in_table): self
|
|
|
|
{
|
|
|
|
$this->show_in_table = $show_in_table;
|
2019-03-20 23:16:07 +01:00
|
|
|
|
2019-03-05 14:52:23 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-08-06 18:47:09 +02:00
|
|
|
|
2019-08-26 23:30:35 +02:00
|
|
|
abstract public function setElement(AttachmentContainingDBElement $element) : Attachment;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return Attachment
|
|
|
|
*/
|
|
|
|
public function setPath(string $path): Attachment
|
|
|
|
{
|
|
|
|
$this->path = $path;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param AttachmentType $attachement_type
|
|
|
|
* @return Attachment
|
|
|
|
*/
|
|
|
|
public function setAttachmentType(AttachmentType $attachement_type): Attachment
|
|
|
|
{
|
|
|
|
$this->attachment_type = $attachement_type;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-08-27 13:15:18 +02:00
|
|
|
/**
|
|
|
|
* Sets the url associated with this attachment.
|
|
|
|
* If the url is empty nothing is changed, to not override the file path.
|
|
|
|
* @param string|null $url
|
|
|
|
* @return Attachment
|
|
|
|
*/
|
2019-08-26 23:30:35 +02:00
|
|
|
public function setURL(?string $url) : Attachment
|
|
|
|
{
|
|
|
|
//Only set if the URL is not empty
|
|
|
|
if (!empty($url)) {
|
2019-08-27 22:24:56 +02:00
|
|
|
if (strpos($url, '%BASE%') !== false || strpos($url, '%MEDIA%') !== false) {
|
2019-09-16 22:04:59 +02:00
|
|
|
throw new \InvalidArgumentException('You can not reference internal files via the url field! But nice try!');
|
2019-08-27 22:24:56 +02:00
|
|
|
}
|
2019-08-26 23:30:35 +02:00
|
|
|
|
2019-08-27 22:24:56 +02:00
|
|
|
$this->path = $url;
|
2019-08-27 13:15:18 +02:00
|
|
|
}
|
|
|
|
|
2019-08-26 23:30:35 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-06 18:47:09 +02:00
|
|
|
/*****************************************************************************************************
|
|
|
|
* Static functions
|
|
|
|
*****************************************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a string is a URL and is valid.
|
|
|
|
* @param $string string The string which should be checked.
|
|
|
|
* @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).
|
|
|
|
* @param $only_http bool Set this to true, if only HTTPS or HTTP schemata should be allowed.
|
|
|
|
* *Caution: When this is set to false, a attacker could use the file:// schema, to get internal server files, like /etc/passwd.*
|
|
|
|
* @return bool True if the string is a valid URL. False, if the string is not an URL or invalid.
|
|
|
|
*/
|
|
|
|
public static function isURL(string $string, bool $path_required = true, bool $only_http = true) : bool
|
|
|
|
{
|
|
|
|
if ($only_http) { //Check if scheme is HTTPS or HTTP
|
|
|
|
$scheme = parse_url($string, PHP_URL_SCHEME);
|
|
|
|
if ($scheme !== 'http' && $scheme !== 'https') {
|
|
|
|
return false; //All other schemes are not valid.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($path_required) {
|
|
|
|
return (bool) filter_var($string, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED);
|
|
|
|
}
|
2019-08-20 18:39:57 +02:00
|
|
|
|
|
|
|
return (bool) filter_var($string, FILTER_VALIDATE_URL);
|
2019-08-06 18:47:09 +02:00
|
|
|
}
|
2019-03-20 23:16:07 +01:00
|
|
|
}
|