Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
20 / 20 |
| Link | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
20 | |
100.00% |
20 / 20 |
| setId | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| getId | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| getUrl | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| setUrl | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| setUrlHash | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| getUrlHash | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| setMimetype | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| getMimetype | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| getMimetypeMajor | n/a |
0 / 0 |
2 | n/a |
0 / 0 |
|||||
| getMimetypeMinor | n/a |
0 / 0 |
2 | n/a |
0 / 0 |
|||||
| setModified | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| getModified | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| getOrCreate | |
100.00% |
1 / 1 |
5 | |
100.00% |
19 / 19 |
|||
| schemaDef | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| 1 | <?php |
| 2 | |
| 3 | // {{{ License |
| 4 | // This file is part of GNU social - https://www.gnu.org/software/social |
| 5 | // |
| 6 | // GNU social is free software: you can redistribute it and/or modify |
| 7 | // it under the terms of the GNU Affero General Public License as published by |
| 8 | // the Free Software Foundation, either version 3 of the License, or |
| 9 | // (at your option) any later version. |
| 10 | // |
| 11 | // GNU social is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU Affero General Public License for more details. |
| 15 | // |
| 16 | // You should have received a copy of the GNU Affero General Public License |
| 17 | // along with GNU social. If not, see <http://www.gnu.org/licenses/>. |
| 18 | // }}} |
| 19 | |
| 20 | namespace App\Entity; |
| 21 | |
| 22 | use App\Core\DB\DB; |
| 23 | use App\Core\Entity; |
| 24 | use App\Core\Event; |
| 25 | use App\Core\GSFile; |
| 26 | use App\Core\HTTPClient; |
| 27 | use App\Core\Log; |
| 28 | use App\Util\Common; |
| 29 | use App\Util\Exception\DuplicateFoundException; |
| 30 | use App\Util\Exception\NotFoundException; |
| 31 | use DateTimeInterface; |
| 32 | use InvalidArgumentException; |
| 33 | use Symfony\Component\HttpClient\Exception\ClientException; |
| 34 | |
| 35 | /** |
| 36 | * Entity for representing a Link |
| 37 | * |
| 38 | * @category DB |
| 39 | * @package GNUsocial |
| 40 | * |
| 41 | * @author Diogo Peralta Cordeiro <mail@diogo.site> |
| 42 | * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org |
| 43 | * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later |
| 44 | */ |
| 45 | class Link extends Entity |
| 46 | { |
| 47 | // {{{ Autocode |
| 48 | // @codeCoverageIgnoreStart |
| 49 | private int $id; |
| 50 | private ?string $url; |
| 51 | private ?string $url_hash; |
| 52 | private ?string $mimetype; |
| 53 | private DateTimeInterface $modified; |
| 54 | |
| 55 | public function setId(int $id): self |
| 56 | { |
| 57 | $this->id = $id; |
| 58 | return $this; |
| 59 | } |
| 60 | |
| 61 | public function getId(): int |
| 62 | { |
| 63 | return $this->id; |
| 64 | } |
| 65 | |
| 66 | public function getUrl(): ?string |
| 67 | { |
| 68 | return $this->url; |
| 69 | } |
| 70 | |
| 71 | public function setUrl(?string $url): self |
| 72 | { |
| 73 | $this->url = $url; |
| 74 | return $this; |
| 75 | } |
| 76 | |
| 77 | public function setUrlHash(?string $url_hash): self |
| 78 | { |
| 79 | $this->url_hash = $url_hash; |
| 80 | return $this; |
| 81 | } |
| 82 | |
| 83 | public function getUrlHash(): ?string |
| 84 | { |
| 85 | return $this->url_hash; |
| 86 | } |
| 87 | |
| 88 | public function setMimetype(?string $mimetype): self |
| 89 | { |
| 90 | $this->mimetype = $mimetype; |
| 91 | return $this; |
| 92 | } |
| 93 | |
| 94 | public function getMimetype(): ?string |
| 95 | { |
| 96 | return $this->mimetype; |
| 97 | } |
| 98 | |
| 99 | public function getMimetypeMajor(): ?string |
| 100 | { |
| 101 | $mime = $this->getMimetype(); |
| 102 | return is_null($mime) ? $mime : GSFile::mimetypeMajor($mime); |
| 103 | } |
| 104 | |
| 105 | public function getMimetypeMinor(): ?string |
| 106 | { |
| 107 | $mime = $this->getMimetype(); |
| 108 | return is_null($mime) ? $mime : GSFile::mimetypeMinor($mime); |
| 109 | } |
| 110 | |
| 111 | public function setModified(DateTimeInterface $modified): self |
| 112 | { |
| 113 | $this->modified = $modified; |
| 114 | return $this; |
| 115 | } |
| 116 | |
| 117 | public function getModified(): DateTimeInterface |
| 118 | { |
| 119 | return $this->modified; |
| 120 | } |
| 121 | |
| 122 | // @codeCoverageIgnoreEnd |
| 123 | // }}} Autocode |
| 124 | |
| 125 | const URLHASH_ALGO = 'sha256'; |
| 126 | |
| 127 | /** |
| 128 | * Create an attachment for the given URL, fetching the mimetype |
| 129 | * |
| 130 | * @param string $url |
| 131 | * |
| 132 | *@throws InvalidArgumentException |
| 133 | * @throws DuplicateFoundException |
| 134 | * |
| 135 | * @return Link |
| 136 | * |
| 137 | */ |
| 138 | public static function getOrCreate(string $url): self |
| 139 | { |
| 140 | if (Common::isValidHttpUrl($url)) { |
| 141 | // If the URL is a local one, do not create a Link to it |
| 142 | if (parse_url($url, PHP_URL_HOST) === $_ENV['SOCIAL_DOMAIN']) { |
| 143 | Log::warning("It was attempted to create a Link to a local location {$url}."); |
| 144 | // Forbidden |
| 145 | throw new InvalidArgumentException(message: "A Link can't point to a local location ({$url}), it must be a remote one", code: 400); |
| 146 | } |
| 147 | $head = HTTPClient::head($url); |
| 148 | // This must come before getInfo given that Symfony HTTPClient is lazy (thus forcing curl exec) |
| 149 | try { |
| 150 | $headers = $head->getHeaders(); |
| 151 | // @codeCoverageIgnoreStart |
| 152 | } catch (ClientException $e) { |
| 153 | throw new InvalidArgumentException(previous: $e); |
| 154 | // @codeCoverageIgnoreEnd |
| 155 | } |
| 156 | $url = $head->getInfo('url'); // The last effective url (after getHeaders, so it follows redirects) |
| 157 | $url_hash = hash(self::URLHASH_ALGO, $url); |
| 158 | try { |
| 159 | return DB::findOneBy('link', ['url_hash' => $url_hash]); |
| 160 | } catch (NotFoundException) { |
| 161 | $headers = array_change_key_case($headers, CASE_LOWER); |
| 162 | $link = self::create([ |
| 163 | 'url' => $url, |
| 164 | 'url_hash' => $url_hash, |
| 165 | 'mimetype' => $headers['content-type'][0], |
| 166 | ]); |
| 167 | DB::persist($link); |
| 168 | Event::handle('LinkStoredNew', [&$link]); |
| 169 | return $link; |
| 170 | } |
| 171 | } else { |
| 172 | throw new InvalidArgumentException(); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | public static function schemaDef(): array |
| 177 | { |
| 178 | return [ |
| 179 | 'name' => 'link', |
| 180 | 'fields' => [ |
| 181 | 'id' => ['type' => 'serial', 'not null' => true], |
| 182 | 'url' => ['type' => 'text', 'description' => 'URL after following possible redirections'], |
| 183 | 'url_hash' => ['type' => 'varchar', 'length' => 64, 'description' => 'sha256 of destination URL (url field)'], |
| 184 | 'mimetype' => ['type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'], |
| 185 | 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'], |
| 186 | ], |
| 187 | 'primary key' => ['id'], |
| 188 | 'indexes' => [ |
| 189 | 'gsactor_url_hash_idx' => ['url_hash'], |
| 190 | ], |
| 191 | ]; |
| 192 | } |
| 193 | } |