2019-03-20 23:16:07 +01:00
|
|
|
<?php
|
2019-11-01 13:40:30 +01:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-11-01 13:40:30 +01:00
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2019-11-01 13:40:30 +01:00
|
|
|
*
|
2020-02-22 18:14:36 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2019-11-01 13:40:30 +01:00
|
|
|
*
|
|
|
|
* 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
|
2020-02-22 18:14:36 +01:00
|
|
|
* GNU Affero General Public License for more details.
|
2019-11-01 13:40:30 +01:00
|
|
|
*
|
2020-02-22 18:14:36 +01:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-11-01 13:40:30 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
namespace App\Entity\Attachments;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2024-03-03 19:57:31 +01:00
|
|
|
use ApiPlatform\Doctrine\Common\Filter\DateFilterInterface;
|
2023-10-03 16:29:04 +02:00
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
|
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
|
|
|
|
use ApiPlatform\Metadata\ApiFilter;
|
2023-09-18 21:57:17 +02:00
|
|
|
use ApiPlatform\Metadata\ApiProperty;
|
|
|
|
use ApiPlatform\Metadata\ApiResource;
|
|
|
|
use ApiPlatform\Metadata\Delete;
|
|
|
|
use ApiPlatform\Metadata\Get;
|
|
|
|
use ApiPlatform\Metadata\GetCollection;
|
|
|
|
use ApiPlatform\Metadata\Patch;
|
|
|
|
use ApiPlatform\Metadata\Post;
|
2023-10-02 00:22:15 +02:00
|
|
|
use App\ApiPlatform\DocumentedAPIProperty;
|
2023-10-03 23:58:41 +02:00
|
|
|
use App\ApiPlatform\Filter\EntityFilter;
|
2023-10-03 16:29:04 +02:00
|
|
|
use App\ApiPlatform\Filter\LikeFilter;
|
2024-03-03 18:40:35 +01:00
|
|
|
use App\ApiPlatform\HandleAttachmentsUploadsProcessor;
|
2023-06-11 14:55:06 +02:00
|
|
|
use App\Repository\AttachmentRepository;
|
|
|
|
use App\EntityListeners\AttachmentDeleteListener;
|
|
|
|
use Doctrine\DBAL\Types\Types;
|
2020-02-01 19:48:07 +01:00
|
|
|
use App\Entity\Base\AbstractNamedDBElement;
|
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;
|
2023-03-05 23:47:45 +01:00
|
|
|
use Symfony\Component\Serializer\Annotation\Groups;
|
2023-09-18 21:57:17 +02:00
|
|
|
use Symfony\Component\Serializer\Annotation\SerializedName;
|
2024-03-02 21:21:16 +01:00
|
|
|
use Symfony\Component\Serializer\Attribute\DiscriminatorMap;
|
2022-07-22 00:48:51 +02:00
|
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
2020-01-05 22:49:00 +01:00
|
|
|
use function in_array;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use LogicException;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Class Attachment.
|
2023-06-11 15:02:59 +02:00
|
|
|
* @see \App\Tests\Entity\Attachments\AttachmentTest
|
2023-06-13 10:36:34 +02:00
|
|
|
* @template-covariant T of AttachmentContainingDBElement
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\Entity(repositoryClass: AttachmentRepository::class)]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\InheritanceType('SINGLE_TABLE')]
|
|
|
|
#[ORM\DiscriminatorColumn(name: 'class_name', type: 'string')]
|
2024-03-02 21:45:02 +01:00
|
|
|
#[ORM\DiscriminatorMap(self::ORM_DISCRIMINATOR_MAP)]
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\EntityListeners([AttachmentDeleteListener::class])]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\Table(name: '`attachments`')]
|
2024-03-03 19:57:31 +01:00
|
|
|
#[ORM\Index(columns: ['id', 'element_id', 'class_name'], name: 'attachments_idx_id_element_id_class_name')]
|
|
|
|
#[ORM\Index(columns: ['class_name', 'id'], name: 'attachments_idx_class_name_id')]
|
|
|
|
#[ORM\Index(columns: ['name'], name: 'attachment_name_idx')]
|
|
|
|
#[ORM\Index(columns: ['class_name', 'element_id'], name: 'attachment_element_idx')]
|
2023-09-18 21:57:17 +02:00
|
|
|
#[ApiResource(
|
|
|
|
operations: [
|
|
|
|
new Get(security: 'is_granted("read", object)'),
|
|
|
|
new GetCollection(security: 'is_granted("@attachments.list_attachments")'),
|
2024-03-03 18:40:35 +01:00
|
|
|
new Post(securityPostDenormalize: 'is_granted("create", object)', ),
|
2023-09-18 21:57:17 +02:00
|
|
|
new Patch(security: 'is_granted("edit", object)'),
|
|
|
|
new Delete(security: 'is_granted("delete", object)'),
|
|
|
|
],
|
|
|
|
normalizationContext: ['groups' => ['attachment:read', 'attachment:read:standalone', 'api:basic:read'], 'openapi_definition_name' => 'Read'],
|
2024-02-06 22:42:25 +01:00
|
|
|
denormalizationContext: ['groups' => ['attachment:write', 'attachment:write:standalone', 'api:basic:write'], 'openapi_definition_name' => 'Write'],
|
2024-03-03 18:40:35 +01:00
|
|
|
processor: HandleAttachmentsUploadsProcessor::class,
|
2023-09-18 21:57:17 +02:00
|
|
|
)]
|
2023-10-02 00:22:15 +02:00
|
|
|
#[DocumentedAPIProperty(schemaName: 'Attachment-Read', property: 'media_url', type: 'string', nullable: true,
|
|
|
|
description: 'The URL to the file, where the attachment file can be downloaded. This can be an internal or external URL.',
|
|
|
|
example: '/media/part/2/bc547-6508afa5a79c8.pdf')]
|
|
|
|
#[DocumentedAPIProperty(schemaName: 'Attachment-Read', property: 'thumbnail_url', type: 'string', nullable: true,
|
|
|
|
description: 'The URL to a thumbnail version of this file. This only exists for internal picture attachments.')]
|
2023-10-03 16:29:04 +02:00
|
|
|
#[ApiFilter(LikeFilter::class, properties: ["name"])]
|
2023-10-03 23:58:41 +02:00
|
|
|
#[ApiFilter(EntityFilter::class, properties: ["attachment_type"])]
|
2024-03-03 19:57:31 +01:00
|
|
|
#[ApiFilter(DateFilter::class, strategy: DateFilterInterface::EXCLUDE_NULL)]
|
2023-10-03 16:29:04 +02:00
|
|
|
#[ApiFilter(OrderFilter::class, properties: ['name', 'id', 'addedDate', 'lastModified'])]
|
2024-03-02 21:39:30 +01:00
|
|
|
//This discriminator map is required for API platform to know which class to use for deserialization, when creating a new attachment.
|
2024-03-02 21:45:02 +01:00
|
|
|
#[DiscriminatorMap(typeProperty: '_type', mapping: self::API_DISCRIMINATOR_MAP)]
|
2020-02-01 19:48:07 +01:00
|
|
|
abstract class Attachment extends AbstractNamedDBElement
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2024-03-02 21:45:02 +01:00
|
|
|
private const ORM_DISCRIMINATOR_MAP = ['PartDB\Part' => PartAttachment::class, 'Part' => PartAttachment::class,
|
2024-03-02 21:39:30 +01:00
|
|
|
'PartDB\Device' => ProjectAttachment::class, 'Device' => ProjectAttachment::class, 'AttachmentType' => AttachmentTypeAttachment::class,
|
|
|
|
'Category' => CategoryAttachment::class, 'Footprint' => FootprintAttachment::class, 'Manufacturer' => ManufacturerAttachment::class,
|
|
|
|
'Currency' => CurrencyAttachment::class, 'Group' => GroupAttachment::class, 'MeasurementUnit' => MeasurementUnitAttachment::class,
|
|
|
|
'Storelocation' => StorageLocationAttachment::class, 'Supplier' => SupplierAttachment::class,
|
|
|
|
'User' => UserAttachment::class, 'LabelProfile' => LabelAttachment::class];
|
|
|
|
|
2024-03-02 21:45:02 +01:00
|
|
|
/*
|
|
|
|
* The discriminator map used for API platform. The key should be the same as the api platform short type (the @type JSONLD field).
|
|
|
|
*/
|
|
|
|
private const API_DISCRIMINATOR_MAP = ["Part" => PartAttachment::class, "Project" => ProjectAttachment::class, "AttachmentType" => AttachmentTypeAttachment::class,
|
|
|
|
"Category" => CategoryAttachment::class, "Footprint" => FootprintAttachment::class, "Manufacturer" => ManufacturerAttachment::class,
|
|
|
|
"Currency" => CurrencyAttachment::class, "Group" => GroupAttachment::class, "MeasurementUnit" => MeasurementUnitAttachment::class,
|
|
|
|
"StorageLocation" => StorageLocationAttachment::class, "Supplier" => SupplierAttachment::class, "User" => UserAttachment::class, "LabelProfile" => LabelAttachment::class];
|
|
|
|
|
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
|
2023-04-15 23:14:53 +02:00
|
|
|
* It will be used to determine if an attachment is a picture and therefore will be shown to user as preview.
|
2019-09-22 21:25:06 +02:00
|
|
|
*/
|
2023-06-11 14:15:46 +02:00
|
|
|
final public const PICTURE_EXTS = ['apng', 'bmp', 'gif', 'ico', 'cur', 'jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp', 'png',
|
2019-11-09 00:47:20 +01:00
|
|
|
'svg', 'webp', ];
|
2019-09-22 21:25:06 +02:00
|
|
|
|
2019-10-03 14:04:09 +02:00
|
|
|
/**
|
|
|
|
* A list of extensions that will be treated as a 3D Model that can be shown to user directly in Part-DB.
|
|
|
|
*/
|
2023-06-11 14:15:46 +02:00
|
|
|
final public const MODEL_EXTS = ['x3d'];
|
2019-10-03 14:04:09 +02:00
|
|
|
|
2019-09-24 16:36:41 +02:00
|
|
|
/**
|
2023-04-15 23:14:53 +02:00
|
|
|
* When the path begins with one of the placeholders.
|
2019-09-24 16:36:41 +02:00
|
|
|
*/
|
2023-06-11 14:15:46 +02:00
|
|
|
final public const INTERNAL_PLACEHOLDER = ['%BASE%', '%MEDIA%', '%SECURE%'];
|
2019-09-25 16:03:22 +02:00
|
|
|
|
2020-01-05 22:49:00 +01:00
|
|
|
/**
|
2020-08-21 21:36:22 +02:00
|
|
|
* @var array placeholders for attachments which using built in files
|
2020-01-05 22:49:00 +01:00
|
|
|
*/
|
2023-06-11 14:15:46 +02:00
|
|
|
final public const BUILTIN_PLACEHOLDER = ['%FOOTPRINTS%', '%FOOTPRINTS3D%'];
|
2019-09-24 16:36:41 +02:00
|
|
|
|
2020-01-05 15:46:58 +01:00
|
|
|
/**
|
|
|
|
* @var string The class of the element that can be passed to this attachment. Must be overridden in subclasses.
|
2023-06-13 10:36:34 +02:00
|
|
|
* @phpstan-var class-string<T>
|
2020-01-05 15:46:58 +01:00
|
|
|
*/
|
2023-06-13 10:36:34 +02:00
|
|
|
protected const ALLOWED_ELEMENT_CLASS = AttachmentContainingDBElement::class;
|
2020-01-05 15:46:58 +01:00
|
|
|
|
2024-03-03 18:40:35 +01:00
|
|
|
/**
|
|
|
|
* @var AttachmentUpload|null The options used for uploading a file to this attachment or modify it.
|
|
|
|
* This value is not persisted in the database, but is just used to pass options to the upload manager.
|
|
|
|
* If it is null, no upload process is started.
|
|
|
|
*/
|
|
|
|
#[Groups(['attachment:write'])]
|
|
|
|
protected ?AttachmentUpload $upload = null;
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
2020-02-01 19:42:28 +01:00
|
|
|
* @var string|null the original filename the file had, when the user uploaded it
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\Column(type: Types::STRING, nullable: true)]
|
2024-06-23 00:41:25 +02:00
|
|
|
#[Groups(['attachment:read', 'import'])]
|
2024-03-06 19:46:11 +01:00
|
|
|
#[Assert\Length(max: 255)]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?string $original_filename = null;
|
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%
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2024-03-03 19:57:31 +01:00
|
|
|
#[ORM\Column(name: 'path', type: Types::STRING)]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected string $path = '';
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2023-03-05 23:47:45 +01:00
|
|
|
/**
|
|
|
|
* @var string the name of this element
|
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Assert\NotBlank(message: 'validator.attachment.name_not_blank')]
|
2024-06-23 00:41:25 +02:00
|
|
|
#[Groups(['simple', 'extended', 'full', 'attachment:read', 'attachment:write', 'import'])]
|
2023-03-05 23:47:45 +01:00
|
|
|
protected string $name = '';
|
|
|
|
|
2019-09-24 13:39:49 +02:00
|
|
|
/**
|
2023-04-15 23:14:53 +02:00
|
|
|
* ORM mapping is done in subclasses (like PartAttachment).
|
2023-06-13 10:36:34 +02:00
|
|
|
* @phpstan-param T|null $element
|
2019-09-24 13:39:49 +02:00
|
|
|
*/
|
2024-02-06 22:42:25 +01:00
|
|
|
#[Groups(['attachment:read:standalone', 'attachment:write:standalone'])]
|
2024-03-02 21:21:16 +01:00
|
|
|
#[ApiProperty(writableLink: false)]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?AttachmentContainingDBElement $element = null;
|
2019-09-24 13:39:49 +02:00
|
|
|
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\Column(type: Types::BOOLEAN)]
|
2024-06-23 00:41:25 +02:00
|
|
|
#[Groups(['attachment:read', 'attachment_write', 'full', 'import'])]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected bool $show_in_table = false;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Assert\NotNull(message: 'validator.attachment.must_not_be_null')]
|
2023-06-13 20:24:54 +02:00
|
|
|
#[ORM\ManyToOne(targetEntity: AttachmentType::class, inversedBy: 'attachments_with_type')]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\JoinColumn(name: 'type_id', nullable: false)]
|
2024-03-03 19:57:31 +01:00
|
|
|
#[Selectable]
|
2024-06-23 00:41:25 +02:00
|
|
|
#[Groups(['attachment:read', 'attachment:write', 'import', 'full'])]
|
2024-03-03 21:52:26 +01:00
|
|
|
#[ApiProperty(readableLink: false)]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?AttachmentType $attachment_type = null;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2023-10-04 00:08:10 +02:00
|
|
|
#[Groups(['attachment:read'])]
|
2024-06-22 17:36:54 +02:00
|
|
|
protected ?\DateTimeImmutable $addedDate = null;
|
2023-10-04 00:08:10 +02:00
|
|
|
#[Groups(['attachment:read'])]
|
2024-06-22 17:36:54 +02:00
|
|
|
protected ?\DateTimeImmutable $lastModified = null;
|
2023-10-04 00:08:10 +02:00
|
|
|
|
|
|
|
|
2019-11-08 22:05:12 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
//parent::__construct();
|
2023-06-13 10:36:34 +02:00
|
|
|
if (AttachmentContainingDBElement::class === static::ALLOWED_ELEMENT_CLASS) {
|
2020-01-05 22:49:00 +01:00
|
|
|
throw new LogicException('An *Attachment class must override the ALLOWED_ELEMENT_CLASS const!');
|
2019-11-08 22:05:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 23:36:44 +02:00
|
|
|
public function updateTimestamps(): void
|
|
|
|
{
|
|
|
|
parent::updateTimestamps();
|
|
|
|
if ($this->element instanceof AttachmentContainingDBElement) {
|
|
|
|
$this->element->updateTimestamps();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-03 18:40:35 +01:00
|
|
|
/**
|
|
|
|
* Gets the upload currently associated with this attachment.
|
|
|
|
* This is only temporary and not persisted directly in the database.
|
|
|
|
* @internal This function should only be used by the Attachment Submit handler service
|
|
|
|
* @return AttachmentUpload|null
|
|
|
|
*/
|
|
|
|
public function getUpload(): ?AttachmentUpload
|
|
|
|
{
|
|
|
|
return $this->upload;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the current upload for this attachment.
|
|
|
|
* It will be processed as the attachment is persisted/flushed.
|
|
|
|
* @param AttachmentUpload|null $upload
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setUpload(?AttachmentUpload $upload): Attachment
|
|
|
|
{
|
|
|
|
$this->upload = $upload;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/***********************************************************
|
|
|
|
* Various function
|
|
|
|
***********************************************************/
|
|
|
|
|
|
|
|
/**
|
2019-11-10 14:00:56 +01:00
|
|
|
* Check if this attachment is a picture (analyse the file's extension).
|
2019-10-03 14:04:09 +02:00
|
|
|
* If the link is external, it is assumed that this is true.
|
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
|
|
|
*/
|
2023-09-18 21:57:17 +02:00
|
|
|
#[Groups(['attachment:read'])]
|
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
|
|
|
if ($this->isExternal()) {
|
2023-07-29 18:50:18 +02:00
|
|
|
//Check if we can extract a file extension from the URL
|
|
|
|
$extension = pathinfo(parse_url($this->path, PHP_URL_PATH) ?? '', PATHINFO_EXTENSION);
|
|
|
|
|
|
|
|
//If no extension is found or it is known picture extension, we assume that this is a picture extension
|
2024-03-03 19:57:31 +01:00
|
|
|
return $extension === '' || in_array(strtolower($extension), static::PICTURE_EXTS, true);
|
2019-09-02 23:09:58 +02:00
|
|
|
}
|
|
|
|
|
2019-08-06 18:47:09 +02:00
|
|
|
$extension = pathinfo($this->getPath(), PATHINFO_EXTENSION);
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2020-01-05 22:49:00 +01:00
|
|
|
return in_array(strtolower($extension), static::PICTURE_EXTS, true);
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-03 14:04:09 +02:00
|
|
|
/**
|
2019-11-10 14:00:56 +01:00
|
|
|
* Check if this attachment is a 3D model and therefore can be directly shown to user.
|
2019-10-03 14:04:09 +02:00
|
|
|
* If the attachment is external, false is returned (3D Models must be internal).
|
|
|
|
*/
|
2023-09-18 21:57:17 +02:00
|
|
|
#[Groups(['attachment:read'])]
|
|
|
|
#[SerializedName('3d_model')]
|
2019-11-09 00:47:20 +01:00
|
|
|
public function is3DModel(): bool
|
2019-10-03 14:04:09 +02:00
|
|
|
{
|
|
|
|
//We just assume that 3D Models are internally saved, otherwise we get problems loading them.
|
|
|
|
if ($this->isExternal()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$extension = pathinfo($this->getPath(), PATHINFO_EXTENSION);
|
|
|
|
|
2020-01-05 22:49:00 +01:00
|
|
|
return in_array(strtolower($extension), static::MODEL_EXTS, true);
|
2019-10-03 14:04:09 +02:00
|
|
|
}
|
|
|
|
|
2019-08-06 18:47:09 +02:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* Checks if the attachment file is externally saved (the database saves an URL).
|
|
|
|
*
|
2019-08-06 18:47:09 +02:00
|
|
|
* @return bool true, if the file is saved externally
|
|
|
|
*/
|
2023-09-18 21:57:17 +02:00
|
|
|
#[Groups(['attachment:read'])]
|
2019-11-09 00:47:20 +01:00
|
|
|
public function isExternal(): bool
|
2019-08-06 18:47:09 +02:00
|
|
|
{
|
2019-10-19 17:13:13 +02:00
|
|
|
//When path is empty, this attachment can not be external
|
2023-06-11 18:59:07 +02:00
|
|
|
if ($this->path === '') {
|
2019-10-19 17:13:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-10 14:00:56 +01:00
|
|
|
//After the %PLACEHOLDER% comes a slash, so we can check if we have a placeholder via explode
|
2019-11-09 00:47:20 +01:00
|
|
|
$tmp = explode('/', $this->path);
|
2019-09-24 16:36:41 +02:00
|
|
|
|
2022-12-18 19:45:04 +01:00
|
|
|
return !in_array($tmp[0], array_merge(static::INTERNAL_PLACEHOLDER, static::BUILTIN_PLACEHOLDER), true);
|
2019-09-25 16:03:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-05 20:30:27 +02:00
|
|
|
* Check if this attachment is saved in a secure place.
|
|
|
|
* This means that it can not be accessed directly via a web request, but must be viewed via a controller.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @return bool true, if the file is secure
|
2019-10-05 20:30:27 +02:00
|
|
|
*/
|
2023-09-18 21:57:17 +02:00
|
|
|
#[Groups(['attachment:read'])]
|
|
|
|
#[SerializedName('private')]
|
2019-11-09 00:47:20 +01:00
|
|
|
public function isSecure(): bool
|
2019-10-05 20:30:27 +02:00
|
|
|
{
|
2019-11-10 14:00:56 +01:00
|
|
|
//After the %PLACEHOLDER% comes a slash, so we can check if we have a placeholder via explode
|
2019-11-09 00:47:20 +01:00
|
|
|
$tmp = explode('/', $this->path);
|
2019-10-05 20:30:27 +02:00
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
return '%SECURE%' === $tmp[0];
|
2019-10-05 20:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-25 16:03:22 +02:00
|
|
|
* Checks if the attachment file is using a builtin file. (see BUILTIN_PLACEHOLDERS const for possible placeholders)
|
2019-11-09 00:47:20 +01:00
|
|
|
* If a file is built in, the path is shown to user in url field (no sensitive infos are provided).
|
|
|
|
*
|
2023-04-15 23:14:53 +02:00
|
|
|
* @return bool true if the attachment is using a builtin file
|
2019-09-25 16:03:22 +02:00
|
|
|
*/
|
2023-09-18 21:57:17 +02:00
|
|
|
#[Groups(['attachment:read'])]
|
2019-11-09 00:47:20 +01:00
|
|
|
public function isBuiltIn(): bool
|
2019-09-25 16:03:22 +02:00
|
|
|
{
|
|
|
|
return static::checkIfBuiltin($this->path);
|
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.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @return string|null the file extension in lower case
|
2019-08-06 18:47:09 +02:00
|
|
|
*/
|
2019-11-09 00:47:20 +01: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;
|
|
|
|
}
|
2019-09-24 16:36:41 +02:00
|
|
|
|
2023-06-11 18:59:07 +02:00
|
|
|
if ($this->original_filename !== null && $this->original_filename !== '') {
|
2019-09-24 16:36:41 +02:00
|
|
|
return strtolower(pathinfo($this->original_filename, PATHINFO_EXTENSION));
|
|
|
|
}
|
|
|
|
|
2019-09-22 23:47:40 +02:00
|
|
|
return strtolower(pathinfo($this->getPath(), PATHINFO_EXTENSION));
|
2019-08-06 18:47:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
2019-11-10 14:00:56 +01:00
|
|
|
* Get the element, associated with this Attachment (for example a "Part" object).
|
2019-03-20 23:16:07 +01:00
|
|
|
*
|
2023-06-18 00:00:58 +02:00
|
|
|
* @return AttachmentContainingDBElement|null the associated Element
|
|
|
|
* @phpstan-return T|null
|
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
|
|
|
/**
|
2023-04-15 23:14:53 +02:00
|
|
|
* The URL to the external file, or the path to the built-in file.
|
2019-09-25 16:03:22 +02:00
|
|
|
* Returns null, if the file is not external (and not builtin).
|
2019-08-10 19:16:56 +02:00
|
|
|
*/
|
2023-09-18 21:57:17 +02:00
|
|
|
#[Groups(['attachment:read'])]
|
|
|
|
#[SerializedName('url')]
|
2019-08-10 19:16:56 +02:00
|
|
|
public function getURL(): ?string
|
|
|
|
{
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!$this->isExternal() && !$this->isBuiltIn()) {
|
2019-08-10 19:16:56 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the hostname where the external file is stored.
|
|
|
|
* Returns null, if the file is not external.
|
|
|
|
*/
|
|
|
|
public function getHost(): ?string
|
|
|
|
{
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!$this->isExternal()) {
|
2019-08-10 19:16:56 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-06-22 00:31:43 +02:00
|
|
|
return parse_url((string) $this->getURL(), PHP_URL_HOST);
|
2019-08-10 19:16:56 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
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;
|
|
|
|
}
|
|
|
|
|
2019-09-24 16:36:41 +02:00
|
|
|
//If we have a stored original filename, then use it
|
2023-06-11 18:59:07 +02:00
|
|
|
if ($this->original_filename !== null && $this->original_filename !== '') {
|
2019-09-24 16:36:41 +02:00
|
|
|
return $this->original_filename;
|
|
|
|
}
|
|
|
|
|
2019-08-06 18:47:09 +02:00
|
|
|
return pathinfo($this->getPath(), PATHINFO_BASENAME);
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
2019-09-24 16:36:41 +02:00
|
|
|
/**
|
|
|
|
* Sets the filename that is shown for this attachment. Useful when the internal path is some generated value.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-09-24 16:36:41 +02:00
|
|
|
* @param string|null $new_filename The filename that should be shown.
|
2019-11-09 00:47:20 +01:00
|
|
|
* Set to null to generate the filename from path.
|
|
|
|
*
|
2019-09-24 16:36:41 +02:00
|
|
|
* @return Attachment
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setFilename(?string $new_filename): self
|
2019-09-24 16:36:41 +02:00
|
|
|
{
|
2019-11-09 00:47:20 +01:00
|
|
|
if ('' === $new_filename) {
|
2019-10-19 18:42:06 +02:00
|
|
|
$new_filename = null;
|
|
|
|
}
|
2019-09-24 16:36:41 +02:00
|
|
|
$this->original_filename = $new_filename;
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-09-24 18:39:11 +02:00
|
|
|
return $this;
|
2019-09-24 16:36:41 +02:00
|
|
|
}
|
|
|
|
|
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-11-10 14:00:56 +01:00
|
|
|
* @return bool true means, this attachment will be listed in the "Attachments" column of the HTML tables
|
|
|
|
* false means, this attachment won't be listed in the "Attachments" 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
|
|
|
{
|
2022-12-18 19:45:04 +01:00
|
|
|
return $this->show_in_table;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
}
|
|
|
|
|
2019-03-05 14:52:23 +01:00
|
|
|
/*****************************************************************************************************
|
|
|
|
* Setters
|
2020-02-02 14:05:36 +01:00
|
|
|
***************************************************************************************************
|
|
|
|
* @param bool $show_in_table
|
|
|
|
* @return Attachment
|
|
|
|
*/
|
2019-03-05 14:52:23 +01:00
|
|
|
|
|
|
|
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-11-10 14:00:56 +01:00
|
|
|
/**
|
|
|
|
* Sets the element that is associated with this attachment.
|
|
|
|
* @return $this
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setElement(AttachmentContainingDBElement $element): self
|
2019-11-08 22:05:12 +01:00
|
|
|
{
|
2024-06-22 00:44:59 +02:00
|
|
|
//Do not allow Rector to replace this check with a instanceof. It will not work!!
|
|
|
|
if (!is_a($element, static::ALLOWED_ELEMENT_CLASS, true)) {
|
2020-01-05 22:49:00 +01:00
|
|
|
throw new InvalidArgumentException(sprintf('The element associated with a %s must be a %s!', static::class, static::ALLOWED_ELEMENT_CLASS));
|
2019-11-08 22:05:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->element = $element;
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-11-08 22:05:12 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-08-26 23:30:35 +02:00
|
|
|
|
|
|
|
/**
|
2020-01-04 20:24:09 +01:00
|
|
|
* Sets the filepath (with relative placeholder) for this attachment.
|
|
|
|
*
|
|
|
|
* @param string $path the new filepath of the attachment
|
|
|
|
*
|
2019-08-26 23:30:35 +02:00
|
|
|
* @return Attachment
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setPath(string $path): self
|
2019-08-26 23:30:35 +02:00
|
|
|
{
|
|
|
|
$this->path = $path;
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-08-26 23:30:35 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-10 14:00:56 +01:00
|
|
|
* @return $this
|
2019-08-26 23:30:35 +02:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setAttachmentType(AttachmentType $attachement_type): self
|
2019-08-26 23:30:35 +02:00
|
|
|
{
|
|
|
|
$this->attachment_type = $attachement_type;
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-08-26 23:30:35 +02:00
|
|
|
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.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-08-27 13:15:18 +02:00
|
|
|
* @return Attachment
|
|
|
|
*/
|
2023-09-18 21:57:17 +02:00
|
|
|
#[Groups(['attachment:write'])]
|
|
|
|
#[SerializedName('url')]
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setURL(?string $url): self
|
2019-08-26 23:30:35 +02:00
|
|
|
{
|
2024-03-03 19:57:31 +01:00
|
|
|
//Do nothing if the URL is empty
|
|
|
|
if ($url === null || $url === '') {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-10-15 00:50:43 +02:00
|
|
|
$url = trim($url);
|
|
|
|
//Escape spaces in URL
|
|
|
|
$url = str_replace(' ', '%20', $url);
|
|
|
|
|
2019-08-26 23:30:35 +02:00
|
|
|
//Only set if the URL is not empty
|
2023-06-11 18:59:07 +02:00
|
|
|
if ($url !== null && $url !== '') {
|
2023-05-27 23:58:28 +02:00
|
|
|
if (str_contains($url, '%BASE%') || str_contains($url, '%MEDIA%')) {
|
2020-01-05 22:49:00 +01: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-10-03 13:42:06 +02:00
|
|
|
//Reset internal filename
|
|
|
|
$this->original_filename = null;
|
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
|
|
|
|
*****************************************************************************************************/
|
|
|
|
|
2019-09-25 16:03:22 +02:00
|
|
|
/**
|
2019-11-10 14:00:56 +01:00
|
|
|
* Checks if the given path is a path to a builtin resource.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-09-25 16:03:22 +02:00
|
|
|
* @param string $path The path that should be checked
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @return bool true if the path is pointing to a builtin resource
|
2019-09-25 16:03:22 +02:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public static function checkIfBuiltin(string $path): bool
|
2019-09-25 16:03:22 +02:00
|
|
|
{
|
2019-11-10 14:00:56 +01:00
|
|
|
//After the %PLACEHOLDER% comes a slash, so we can check if we have a placeholder via explode
|
2019-09-25 16:03:22 +02:00
|
|
|
$tmp = explode('/', $path);
|
|
|
|
//Builtins must have a %PLACEHOLDER% construction
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2023-06-18 00:00:58 +02:00
|
|
|
return in_array($tmp[0], static::BUILTIN_PLACEHOLDER, true);
|
2019-09-25 16:03:22 +02:00
|
|
|
}
|
|
|
|
|
2019-08-06 18:47:09 +02:00
|
|
|
/**
|
|
|
|
* Check if a string is a URL and is valid.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-05 15:46:58 +01:00
|
|
|
* @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 bool $only_http Set this to true, if only HTTPS or HTTP schemata should be allowed.
|
2023-04-15 23:14:53 +02:00
|
|
|
* *Caution: When this is set to false, an attacker could use the file:// schema, to get internal server files, like /etc/passwd.*
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2023-04-15 23:14:53 +02:00
|
|
|
* @return bool True if the string is a valid URL. False, if the string is not a URL or invalid.
|
2019-08-06 18:47:09 +02:00
|
|
|
*/
|
2020-05-31 15:03:27 +02:00
|
|
|
public static function isValidURL(string $string, bool $path_required = true, bool $only_http = true): bool
|
2019-08-06 18:47:09 +02:00
|
|
|
{
|
|
|
|
if ($only_http) { //Check if scheme is HTTPS or HTTP
|
|
|
|
$scheme = parse_url($string, PHP_URL_SCHEME);
|
2019-11-09 00:47:20 +01:00
|
|
|
if ('http' !== $scheme && 'https' !== $scheme) {
|
2019-08-06 18:47:09 +02:00
|
|
|
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
|
|
|
}
|
2023-06-11 14:01:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the class of the element that is allowed to be associated with this attachment.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getElementClass(): string
|
|
|
|
{
|
|
|
|
return static::ALLOWED_ELEMENT_CLASS;
|
|
|
|
}
|
2019-03-20 23:16:07 +01:00
|
|
|
}
|