isExternal()) { return true; } $extension = pathinfo($this->getPath(), PATHINFO_EXTENSION); return in_array(strtolower($extension), static::PICTURE_EXTS, true); } /** * 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 { //After the %PLACEHOLDER% comes a slash, so we can check if we have a placholder via explode $tmp = explode("/", $this->path); if (empty($tmp)) { return true; } return !in_array($tmp[0], static::INTERNAL_PLACEHOLDER, false); } /******************************************************************************** * * Getters * *********************************************************************************/ /** * Returns the extension of the file referenced via the attachment. * For a path like %BASE/path/foo.bar, bar will be returned. * If this attachment is external null is returned. * @return string|null The file extension in lower case. */ public function getExtension() : ?string { if ($this->isExternal()) { return null; } if (!empty($this->original_filename)) { return strtolower(pathinfo($this->original_filename, PATHINFO_EXTENSION)); } return strtolower(pathinfo($this->getPath(), PATHINFO_EXTENSION)); } /** * Get the element, associated with this Attachement (for example a "Part" object). * * @return AttachmentContainingDBElement The associated Element. */ public function getElement(): ?AttachmentContainingDBElement { return $this->element; } /** * 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); } /** * Get the filepath, relative to %BASE%. * * @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. * * @return string|null */ public function getFilename(): ?string { if ($this->isExternal()) { return null; } //If we have a stored original filename, then use it if (!empty($this->original_filename)) { return $this->original_filename; } return pathinfo($this->getPath(), PATHINFO_BASENAME); } /** * Sets the filename that is shown for this attachment. Useful when the internal path is some generated value. * @param string|null $new_filename The filename that should be shown. * Set to null to generate the filename from path. * @return Attachment */ public function setFilename(?string $new_filename): Attachment { $this->original_filename = $new_filename; return $this; } /** * Get the show_in_table attribute. * * @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 */ public function getShowInTable(): bool { return (bool) $this->show_in_table; } /** * Get the type of this attachement. * * @return AttachmentType the type of this attachement * */ public function getAttachmentType(): ?AttachmentType { return $this->attachment_type; } /** * Returns the ID as an string, defined by the element class. * This should have a form like P000014, for a part with ID 14. * * @return string The ID as a string; */ public function getIDString(): string { return 'A'.sprintf('%09d', $this->getID()); } /***************************************************************************************************** * Setters ****************************************************************************************************/ /** * @param bool $show_in_table * * @return self */ public function setShowInTable(bool $show_in_table): self { $this->show_in_table = $show_in_table; return $this; } 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; } /** * 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 */ public function setURL(?string $url) : Attachment { //Only set if the URL is not empty if (!empty($url)) { if (strpos($url, '%BASE%') !== false || strpos($url, '%MEDIA%') !== false) { throw new \InvalidArgumentException('You can not reference internal files via the url field! But nice try!'); } $this->path = $url; } return $this; } /***************************************************************************************************** * 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); } return (bool) filter_var($string, FILTER_VALIDATE_URL); } }