123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- declare(strict_types = 1);
- // {{{ License
- // This file is part of GNU social - https://www.gnu.org/software/social
- //
- // GNU social 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.
- //
- // GNU social 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 Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
- // }}}
- namespace Component\Link\Entity;
- use App\Core\DB\DB;
- use App\Core\Entity;
- use App\Core\Event;
- use App\Core\GSFile;
- use App\Core\HTTPClient;
- use App\Core\Log;
- use App\Util\Common;
- use App\Util\Exception\DuplicateFoundException;
- use App\Util\Exception\NotFoundException;
- use DateTimeInterface;
- use Exception;
- use InvalidArgumentException;
- /**
- * Entity for representing a Link
- *
- * @category DB
- * @package GNUsocial
- *
- * @author Diogo Peralta Cordeiro <mail@diogo.site>
- * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
- * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
- */
- class Link extends Entity
- {
- // {{{ Autocode
- // @codeCoverageIgnoreStart
- private int $id;
- private ?string $url = null;
- private ?string $url_hash = null;
- private ?string $mimetype = null;
- private DateTimeInterface $modified;
- public function setId(int $id): self
- {
- $this->id = $id;
- return $this;
- }
- public function getId(): int
- {
- return $this->id;
- }
- public function setUrl(?string $url): self
- {
- $this->url = $url;
- return $this;
- }
- public function getUrl(): ?string
- {
- return $this->url;
- }
- public function setUrlHash(?string $url_hash): self
- {
- $this->url_hash = \is_null($url_hash) ? null : mb_substr($url_hash, 0, 64);
- return $this;
- }
- public function getUrlHash(): ?string
- {
- return $this->url_hash;
- }
- public function setMimetype(?string $mimetype): self
- {
- $this->mimetype = \is_null($mimetype) ? null : mb_substr($mimetype, 0, 50);
- return $this;
- }
- public function getMimetype(): ?string
- {
- return $this->mimetype;
- }
- public function setModified(DateTimeInterface $modified): self
- {
- $this->modified = $modified;
- return $this;
- }
- public function getModified(): DateTimeInterface
- {
- return $this->modified;
- }
- // @codeCoverageIgnoreEnd
- // }}} Autocode
- public const URLHASH_ALGO = 'sha256';
- /**
- * Create an attachment for the given URL, fetching the mimetype
- *
- * @throws DuplicateFoundException
- *@throws InvalidArgumentException
- *
- * @return Link
- */
- public static function getOrCreate(string $url): self
- {
- if (Common::isValidHttpUrl($url)) {
- // If the URL is a local one, do not create a Link to it
- if (parse_url($url, \PHP_URL_HOST) === $_ENV['SOCIAL_DOMAIN']) {
- Log::warning("It was attempted to create a Link to a local location {$url}.");
- // Forbidden
- throw new InvalidArgumentException(message: "A Link can't point to a local location ({$url}), it must be a remote one", code: 400);
- }
- try {
- $head = HTTPClient::head($url);
- // This must come before getInfo given that Symfony HTTPClient is lazy (thus forcing curl exec)
- $headers = $head->getHeaders();
- // @codeCoverageIgnoreStart
- } catch (Exception $e) {
- throw new InvalidArgumentException(previous: $e);
- // @codeCoverageIgnoreEnd
- }
- $url = $head->getInfo('url'); // The last effective url (after getHeaders, so it follows redirects)
- $url_hash = hash(self::URLHASH_ALGO, $url);
- try {
- return DB::findOneBy('link', ['url_hash' => $url_hash]);
- } catch (NotFoundException) {
- $headers = array_change_key_case($headers, \CASE_LOWER);
- $link = self::create([
- 'url' => $url,
- 'url_hash' => $url_hash,
- 'mimetype' => $headers['content-type'][0] ?? null,
- ]);
- DB::wrapInTransaction(fn () => DB::persist($link));
- Event::handle('LinkStoredNew', [&$link]);
- return $link;
- }
- } else {
- throw new InvalidArgumentException();
- }
- }
- public function getMimetypeMajor(): ?string
- {
- $mime = $this->getMimetype();
- return \is_null($mime) ? $mime : GSFile::mimetypeMajor($mime);
- }
- public function getMimetypeMinor(): ?string
- {
- $mime = $this->getMimetype();
- return \is_null($mime) ? $mime : GSFile::mimetypeMinor($mime);
- }
- public static function schemaDef(): array
- {
- return [
- 'name' => 'link',
- 'fields' => [
- 'id' => ['type' => 'serial', 'not null' => true],
- 'url' => ['type' => 'text', 'description' => 'URL after following possible redirections'],
- 'url_hash' => ['type' => 'varchar', 'length' => 64, 'description' => 'sha256 of destination URL (url field)'],
- 'mimetype' => ['type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'],
- 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
- ],
- 'primary key' => ['id'],
- 'indexes' => [
- 'actor_url_hash_idx' => ['url_hash'],
- ],
- ];
- }
- }
|