2021-08-14 16:47:45 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// {{{ 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 App\Entity;
|
|
|
|
|
|
|
|
use App\Core\DB\DB;
|
|
|
|
use App\Core\Entity;
|
|
|
|
use App\Core\Event;
|
2021-08-12 00:39:36 +01:00
|
|
|
use App\Core\GSFile;
|
2021-08-14 16:47:45 +01:00
|
|
|
use App\Core\HTTPClient;
|
2021-08-18 14:34:15 +01:00
|
|
|
use App\Core\Log;
|
2021-08-14 16:47:45 +01:00
|
|
|
use App\Util\Common;
|
|
|
|
use App\Util\Exception\DuplicateFoundException;
|
|
|
|
use App\Util\Exception\NotFoundException;
|
|
|
|
use DateTimeInterface;
|
|
|
|
use InvalidArgumentException;
|
2021-09-14 13:40:50 +01:00
|
|
|
use Symfony\Component\HttpClient\Exception\ClientException as HTTPClientException;
|
|
|
|
use Symfony\Component\HttpClient\Exception\TransportException;
|
2021-08-14 16:47:45 +01:00
|
|
|
|
|
|
|
/**
|
2021-08-13 20:09:20 +01:00
|
|
|
* Entity for representing a Link
|
2021-08-14 16:47:45 +01:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
2021-08-13 20:09:20 +01:00
|
|
|
class Link extends Entity
|
2021-08-14 16:47:45 +01:00
|
|
|
{
|
|
|
|
// {{{ Autocode
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
private int $id;
|
2021-08-13 20:09:20 +01:00
|
|
|
private ?string $url;
|
|
|
|
private ?string $url_hash;
|
2021-08-14 16:47:45 +01:00
|
|
|
private ?string $mimetype;
|
|
|
|
private DateTimeInterface $modified;
|
|
|
|
|
|
|
|
public function setId(int $id): self
|
|
|
|
{
|
|
|
|
$this->id = $id;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getId(): int
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
2021-08-13 20:09:20 +01:00
|
|
|
public function getUrl(): ?string
|
2021-08-14 16:47:45 +01:00
|
|
|
{
|
2021-08-13 20:09:20 +01:00
|
|
|
return $this->url;
|
2021-08-14 16:47:45 +01:00
|
|
|
}
|
|
|
|
|
2021-08-13 20:09:20 +01:00
|
|
|
public function setUrl(?string $url): self
|
2021-08-14 16:47:45 +01:00
|
|
|
{
|
2021-08-13 20:09:20 +01:00
|
|
|
$this->url = $url;
|
2021-08-14 16:47:45 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-08-13 20:09:20 +01:00
|
|
|
public function setUrlHash(?string $url_hash): self
|
2021-08-14 16:47:45 +01:00
|
|
|
{
|
2021-08-13 20:09:20 +01:00
|
|
|
$this->url_hash = $url_hash;
|
2021-08-14 16:47:45 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-08-13 20:09:20 +01:00
|
|
|
public function getUrlHash(): ?string
|
2021-08-14 16:47:45 +01:00
|
|
|
{
|
2021-08-13 20:09:20 +01:00
|
|
|
return $this->url_hash;
|
2021-08-14 16:47:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setMimetype(?string $mimetype): self
|
|
|
|
{
|
|
|
|
$this->mimetype = $mimetype;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMimetype(): ?string
|
|
|
|
{
|
|
|
|
return $this->mimetype;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 function setModified(DateTimeInterface $modified): self
|
|
|
|
{
|
|
|
|
$this->modified = $modified;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getModified(): DateTimeInterface
|
|
|
|
{
|
|
|
|
return $this->modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
// }}} Autocode
|
|
|
|
|
|
|
|
const URLHASH_ALGO = 'sha256';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an attachment for the given URL, fetching the mimetype
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
*
|
2021-08-13 20:09:20 +01:00
|
|
|
*@throws InvalidArgumentException
|
2021-08-14 16:47:45 +01:00
|
|
|
* @throws DuplicateFoundException
|
|
|
|
*
|
2021-08-13 20:09:20 +01:00
|
|
|
* @return Link
|
|
|
|
*
|
2021-08-14 16:47:45 +01:00
|
|
|
*/
|
|
|
|
public static function getOrCreate(string $url): self
|
|
|
|
{
|
|
|
|
if (Common::isValidHttpUrl($url)) {
|
2021-08-18 14:34:15 +01:00
|
|
|
// 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);
|
|
|
|
}
|
2021-08-14 15:39:55 +01:00
|
|
|
try {
|
2021-09-14 13:40:50 +01:00
|
|
|
$head = HTTPClient::head($url);
|
|
|
|
// This must come before getInfo given that Symfony HTTPClient is lazy (thus forcing curl exec)
|
2021-08-14 15:39:55 +01:00
|
|
|
$headers = $head->getHeaders();
|
2021-08-18 17:30:02 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-09-14 13:40:50 +01:00
|
|
|
} catch (HTTPClientException | TransportException $e) {
|
2021-08-14 15:39:55 +01:00
|
|
|
throw new InvalidArgumentException(previous: $e);
|
2021-08-18 17:30:02 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2021-08-14 15:39:55 +01:00
|
|
|
}
|
2021-08-12 00:39:36 +01:00
|
|
|
$url = $head->getInfo('url'); // The last effective url (after getHeaders, so it follows redirects)
|
2021-08-14 16:47:45 +01:00
|
|
|
$url_hash = hash(self::URLHASH_ALGO, $url);
|
|
|
|
try {
|
2021-08-13 20:09:20 +01:00
|
|
|
return DB::findOneBy('link', ['url_hash' => $url_hash]);
|
2021-08-14 16:47:45 +01:00
|
|
|
} catch (NotFoundException) {
|
2021-08-13 20:09:20 +01:00
|
|
|
$headers = array_change_key_case($headers, CASE_LOWER);
|
|
|
|
$link = self::create([
|
|
|
|
'url' => $url,
|
|
|
|
'url_hash' => $url_hash,
|
|
|
|
'mimetype' => $headers['content-type'][0],
|
2021-08-14 16:47:45 +01:00
|
|
|
]);
|
2021-08-13 20:09:20 +01:00
|
|
|
DB::persist($link);
|
|
|
|
Event::handle('LinkStoredNew', [&$link]);
|
|
|
|
return $link;
|
2021-08-14 16:47:45 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function schemaDef(): array
|
|
|
|
{
|
|
|
|
return [
|
2021-08-13 20:09:20 +01:00
|
|
|
'name' => 'link',
|
2021-08-14 16:47:45 +01:00
|
|
|
'fields' => [
|
2021-08-13 20:09:20 +01:00
|
|
|
'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'],
|
2021-08-14 16:47:45 +01:00
|
|
|
],
|
|
|
|
'primary key' => ['id'],
|
|
|
|
'indexes' => [
|
2021-08-13 20:09:20 +01:00
|
|
|
'gsactor_url_hash_idx' => ['url_hash'],
|
2021-08-14 16:47:45 +01:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|