3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Util;
26 use Friendica\Content\OEmbed;
27 use Friendica\Content\Text\HTML;
28 use Friendica\Protocol\HTTP\MediaType;
29 use Friendica\Core\Hook;
30 use Friendica\Core\Logger;
31 use Friendica\Database\Database;
32 use Friendica\Database\DBA;
34 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
35 use Friendica\Network\HTTPException;
36 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
39 * Get information about a given URL
41 * Class with methods for extracting certain content from an url
45 const DEFAULT_EXPIRATION_FAILURE = 'now + 1 day';
46 const DEFAULT_EXPIRATION_SUCCESS = 'now + 3 months';
49 * Maximum number of characters for the description
51 const MAX_DESC_COUNT = 250;
54 * Minimum number of characters for the description
56 const MIN_DESC_COUNT = 100;
59 * Fetch the content type of the given url
60 * @param string $url URL of the page
61 * @param string $accept content-type to accept
63 * @return array content type
65 public static function getContentType(string $url, string $accept = HttpClientAccept::DEFAULT, int $timeout = 0): array
67 if (!empty($timeout)) {
68 $options = [HttpClientOptions::TIMEOUT => $timeout];
73 $curlResult = DI::httpClient()->head($url, array_merge([HttpClientOptions::ACCEPT_CONTENT => $accept], $options));
75 // Workaround for systems that can't handle a HEAD request. Don't retry on timeouts.
76 if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() >= 400) && !in_array($curlResult->getReturnCode(), [408, 504])) {
77 $curlResult = DI::httpClient()->get($url, $accept, array_merge([HttpClientOptions::CONTENT_LENGTH => 1000000], $options));
80 if (!$curlResult->isSuccess()) {
81 Logger::debug('Got HTTP Error', ['http error' => $curlResult->getReturnCode(), 'url' => $url]);
85 $contenttype = $curlResult->getHeader('Content-Type')[0] ?? '';
86 if (empty($contenttype)) {
87 return ['application', 'octet-stream'];
90 return explode('/', current(explode(';', $contenttype)));
94 * Search for chached embeddable data of an url otherwise fetch it
96 * @param string $url The url of the page which should be scraped
97 * @param bool $do_oembed The false option is used by the function fetch_oembed()
98 * to avoid endless loops
100 * @return array which contains needed data for embedding
101 * string 'url' => The url of the parsed page
102 * string 'type' => Content type
103 * string 'title' => (optional) The title of the content
104 * string 'text' => (optional) The description for the content
105 * string 'image' => (optional) A preview image of the content
106 * array 'images' => (optional) Array of preview pictures
107 * string 'keywords' => (optional) The tags which belong to the content
109 * @throws HTTPException\InternalServerErrorException
110 * @see ParseUrl::getSiteinfo() for more information about scraping
113 public static function getSiteinfoCached(string $url, bool $do_oembed = true): array
122 $urlHash = hash('sha256', $url);
124 $parsed_url = DBA::selectFirst('parsed_url', ['content'],
125 ['url_hash' => $urlHash, 'oembed' => $do_oembed]
127 if (!empty($parsed_url['content'])) {
128 $data = unserialize($parsed_url['content']);
132 $data = self::getSiteinfo($url, $do_oembed);
134 $expires = $data['expires'];
136 unset($data['expires']);
141 'url_hash' => $urlHash,
142 'oembed' => $do_oembed,
144 'content' => serialize($data),
145 'created' => DateTimeFormat::utcNow(),
146 'expires' => $expires,
148 Database::INSERT_UPDATE
155 * Parse a page for embeddable content information
157 * This method parses to url for meta data which can be used to embed
158 * the content. If available it prioritizes Open Graph meta tags.
159 * If this is not available it uses the twitter cards meta tags.
160 * As fallback it uses standard html elements with meta informations
161 * like \<title\>Awesome Title\</title\> or
162 * \<meta name="description" content="An awesome description"\>
164 * @param string $url The url of the page which should be scraped
165 * @param bool $do_oembed The false option is used by the function fetch_oembed()
166 * to avoid endless loops
167 * @param int $count Internal counter to avoid endless loops
169 * @return array which contains needed data for embedding
170 * string 'url' => The url of the parsed page
171 * string 'type' => Content type (error, link, photo, image, audio, video)
172 * string 'title' => (optional) The title of the content
173 * string 'text' => (optional) The description for the content
174 * string 'image' => (optional) A preview image of the content
175 * array 'images' => (optional) Array of preview pictures
176 * string 'keywords' => (optional) The tags which belong to the content
178 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
179 * @todo https://developers.google.com/+/plugins/snippet/
181 * <meta itemprop="name" content="Awesome title">
182 * <meta itemprop="description" content="An awesome description">
183 * <meta itemprop="image" content="http://maple.libertreeproject.org/images/tree-icon.png">
185 * <body itemscope itemtype="http://schema.org/Product">
186 * <h1 itemprop="name">Shiny Trinket</h1>
187 * <img itemprop="image" src="{image-url}" />
188 * <p itemprop="description">Shiny trinkets are shiny.</p>
192 public static function getSiteinfo(string $url, bool $do_oembed = true, int $count = 1): array
201 // Check if the URL does contain a scheme
202 $scheme = parse_url($url, PHP_URL_SCHEME);
205 $url = 'http://' . ltrim($url, '/');
208 $url = trim($url, "'\"");
210 $url = Network::stripTrackingQueryParams($url);
215 'expires' => DateTimeFormat::utc(self::DEFAULT_EXPIRATION_FAILURE),
219 Logger::warning('Endless loop detected', ['url' => $url]);
223 $type = self::getContentType($url);
224 Logger::info('Got content-type', ['content-type' => $type, 'url' => $url]);
225 if (!empty($type) && in_array($type[0], ['image', 'video', 'audio'])) {
226 $siteinfo['type'] = $type[0];
230 if ((count($type) >= 2) && (($type[0] != 'text') || ($type[1] != 'html'))) {
231 Logger::info('Unparseable content-type, quitting here, ', ['content-type' => $type, 'url' => $url]);
235 $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML, [HttpClientOptions::CONTENT_LENGTH => 1000000]);
236 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
237 Logger::info('Empty body or error when fetching', ['url' => $url, 'success' => $curlResult->isSuccess(), 'code' => $curlResult->getReturnCode()]);
241 $siteinfo['expires'] = DateTimeFormat::utc(self::DEFAULT_EXPIRATION_SUCCESS);
243 if ($cacheControlHeader = $curlResult->getHeader('Cache-Control')[0] ?? '') {
244 if (preg_match('/max-age=([0-9]+)/i', $cacheControlHeader, $matches)) {
245 $maxAge = max(86400, (int)array_pop($matches));
246 $siteinfo['expires'] = DateTimeFormat::utc("now + $maxAge seconds");
250 $body = $curlResult->getBody();
253 $oembed_data = OEmbed::fetchURL($url, false, false);
255 if (!empty($oembed_data->type)) {
256 if (!in_array($oembed_data->type, ['error', 'rich', 'image', 'video', 'audio', ''])) {
257 $siteinfo['type'] = $oembed_data->type;
260 // See https://github.com/friendica/friendica/pull/5763#discussion_r217913178
261 if ($siteinfo['type'] != 'photo') {
262 if (!empty($oembed_data->title)) {
263 $siteinfo['title'] = trim($oembed_data->title);
265 if (!empty($oembed_data->description)) {
266 $siteinfo['text'] = trim($oembed_data->description);
268 if (!empty($oembed_data->author_name)) {
269 $siteinfo['author_name'] = trim($oembed_data->author_name);
271 if (!empty($oembed_data->author_url)) {
272 $siteinfo['author_url'] = trim($oembed_data->author_url);
274 if (!empty($oembed_data->provider_name)) {
275 $siteinfo['publisher_name'] = trim($oembed_data->provider_name);
277 if (!empty($oembed_data->provider_url)) {
278 $siteinfo['publisher_url'] = trim($oembed_data->provider_url);
280 if (!empty($oembed_data->thumbnail_url)) {
281 $siteinfo['image'] = $oembed_data->thumbnail_url;
289 // Look for a charset, first in headers
290 $mediaType = MediaType::fromContentType($curlResult->getContentType());
291 if (isset($mediaType->parameters['charset'])) {
292 $charset = $mediaType->parameters['charset'];
294 } catch(\InvalidArgumentException $e) {}
296 $siteinfo['charset'] = $charset;
298 if ($charset && strtoupper($charset) != 'UTF-8') {
299 // See https://github.com/friendica/friendica/issues/5470#issuecomment-418351211
300 $charset = str_ireplace('latin-1', 'latin1', $charset);
302 Logger::info('detected charset', ['charset' => $charset]);
303 $body = iconv($charset, 'UTF-8//TRANSLIT', $body);
306 $body = mb_convert_encoding($body, 'HTML-ENTITIES', 'UTF-8');
312 $doc = new DOMDocument();
313 @$doc->loadHTML($body);
315 $siteinfo['charset'] = HTML::extractCharset($doc) ?? $siteinfo['charset'];
317 XML::deleteNode($doc, 'style');
318 XML::deleteNode($doc, 'option');
319 XML::deleteNode($doc, 'h1');
320 XML::deleteNode($doc, 'h2');
321 XML::deleteNode($doc, 'h3');
322 XML::deleteNode($doc, 'h4');
323 XML::deleteNode($doc, 'h5');
324 XML::deleteNode($doc, 'h6');
325 XML::deleteNode($doc, 'ol');
326 XML::deleteNode($doc, 'ul');
328 $xpath = new DOMXPath($doc);
330 $list = $xpath->query('//meta[@content]');
331 foreach ($list as $node) {
333 if ($node->attributes->length) {
334 foreach ($node->attributes as $attribute) {
335 $meta_tag[$attribute->name] = $attribute->value;
339 if (@$meta_tag['http-equiv'] == 'refresh') {
340 $path = $meta_tag['content'];
341 $pathinfo = explode(';', $path);
343 foreach ($pathinfo as $value) {
344 if (substr(strtolower($value), 0, 4) == 'url=') {
345 $content = substr($value, 4);
348 if ($content != '') {
349 $siteinfo = self::getSiteinfo($content, $do_oembed, ++$count);
355 $list = $xpath->query('//title');
356 if ($list->length > 0) {
357 $siteinfo['title'] = trim($list->item(0)->nodeValue);
360 $list = $xpath->query('//meta[@name]');
361 foreach ($list as $node) {
363 if ($node->attributes->length) {
364 foreach ($node->attributes as $attribute) {
365 $meta_tag[$attribute->name] = $attribute->value;
369 if (empty($meta_tag['content'])) {
373 $meta_tag['content'] = trim(html_entity_decode($meta_tag['content'], ENT_QUOTES, 'UTF-8'));
375 switch (strtolower($meta_tag['name'])) {
377 $siteinfo['title'] = trim($meta_tag['content']);
380 $siteinfo['text'] = trim($meta_tag['content']);
383 $siteinfo['image'] = $meta_tag['content'];
385 case 'twitter:image':
386 $siteinfo['image'] = $meta_tag['content'];
388 case 'twitter:image:src':
389 $siteinfo['image'] = $meta_tag['content'];
391 case 'twitter:description':
392 $siteinfo['text'] = trim($meta_tag['content']);
394 case 'twitter:title':
395 $siteinfo['title'] = trim($meta_tag['content']);
397 case 'twitter:player':
398 $siteinfo['player']['embed'] = trim($meta_tag['content']);
400 case 'twitter:player:stream':
401 $siteinfo['player']['stream'] = trim($meta_tag['content']);
403 case 'twitter:player:width':
404 $siteinfo['player']['width'] = intval($meta_tag['content']);
406 case 'twitter:player:height':
407 $siteinfo['player']['height'] = intval($meta_tag['content']);
410 $siteinfo['title'] = trim($meta_tag['content']);
412 case 'dc.description':
413 $siteinfo['text'] = trim($meta_tag['content']);
416 $siteinfo['publisher_name'] = trim($meta_tag['content']);
419 $keywords = explode(',', $meta_tag['content']);
421 case 'news_keywords':
422 $keywords = explode(',', $meta_tag['content']);
427 if (isset($keywords)) {
428 $siteinfo['keywords'] = [];
429 foreach ($keywords as $keyword) {
430 if (!in_array(trim($keyword), $siteinfo['keywords'])) {
431 $siteinfo['keywords'][] = trim($keyword);
436 $list = $xpath->query('//meta[@property]');
437 foreach ($list as $node) {
439 if ($node->attributes->length) {
440 foreach ($node->attributes as $attribute) {
441 $meta_tag[$attribute->name] = $attribute->value;
445 if (!empty($meta_tag['content'])) {
446 $meta_tag['content'] = trim(html_entity_decode($meta_tag['content'], ENT_QUOTES, 'UTF-8'));
448 switch (strtolower($meta_tag['property'])) {
450 $siteinfo['image'] = $meta_tag['content'];
453 $siteinfo['image'] = $meta_tag['content'];
455 case 'og:image:secure_url':
456 $siteinfo['image'] = $meta_tag['content'];
459 $siteinfo['title'] = trim($meta_tag['content']);
461 case 'og:description':
462 $siteinfo['text'] = trim($meta_tag['content']);
465 $siteinfo['publisher_name'] = trim($meta_tag['content']);
468 $siteinfo['language'] = trim($meta_tag['content']);
471 $siteinfo['pagetype'] = trim($meta_tag['content']);
473 case 'twitter:description':
474 $siteinfo['text'] = trim($meta_tag['content']);
476 case 'twitter:title':
477 $siteinfo['title'] = trim($meta_tag['content']);
479 case 'twitter:image':
480 $siteinfo['image'] = $meta_tag['content'];
486 $list = $xpath->query("//script[@type='application/ld+json']");
487 foreach ($list as $node) {
488 if (!empty($node->nodeValue)) {
489 if ($jsonld = json_decode($node->nodeValue, true)) {
490 $siteinfo = self::parseParts($siteinfo, $jsonld);
495 if (!empty($siteinfo['player']['stream'])) {
496 // Only add player data to media arrays if there is no duplicate
497 $content_urls = array_merge(array_column($siteinfo['audio'] ?? [], 'content'), array_column($siteinfo['video'] ?? [], 'content'));
498 if (!in_array($siteinfo['player']['stream'], $content_urls)) {
499 $contenttype = self::getContentType($siteinfo['player']['stream']);
500 if (!empty($contenttype[0]) && in_array($contenttype[0], ['audio', 'video'])) {
501 $media = ['content' => $siteinfo['player']['stream']];
503 if (!empty($siteinfo['player']['embed'])) {
504 $media['embed'] = $siteinfo['player']['embed'];
507 $siteinfo[$contenttype[0]][] = $media;
512 if (!empty($siteinfo['image'])) {
513 $siteinfo['images'] = $siteinfo['images'] ?? [];
514 array_unshift($siteinfo['images'], ['url' => $siteinfo['image']]);
515 unset($siteinfo['image']);
518 $siteinfo = self::checkMedia($url, $siteinfo);
520 if (!empty($siteinfo['text']) && mb_strlen($siteinfo['text']) > self::MAX_DESC_COUNT) {
521 $siteinfo['text'] = mb_substr($siteinfo['text'], 0, self::MAX_DESC_COUNT) . '…';
522 $pos = mb_strrpos($siteinfo['text'], '.');
523 if ($pos > self::MIN_DESC_COUNT) {
524 $siteinfo['text'] = mb_substr($siteinfo['text'], 0, $pos + 1);
528 Logger::info('Siteinfo fetched', ['url' => $url, 'siteinfo' => $siteinfo]);
530 Hook::callAll('getsiteinfo', $siteinfo);
538 * Check the attached media elements.
539 * Fix existing data and add missing data.
541 * @param string $page_url
542 * @param array $siteinfo
545 private static function checkMedia(string $page_url, array $siteinfo) : array
547 if (!empty($siteinfo['images'])) {
548 array_walk($siteinfo['images'], function (&$image) use ($page_url) {
550 * According to the specifications someone could place a picture
551 * URL into the content field as well. But this doesn't seem to
552 * happen in the wild, so we don't cover it here.
554 if (!empty($image['url'])) {
555 $image['url'] = self::completeUrl($image['url'], $page_url);
556 $photodata = Images::getInfoFromURLCached($image['url']);
557 if (($photodata) && ($photodata[0] > 50) && ($photodata[1] > 50)) {
558 $image['src'] = $image['url'];
559 $image['width'] = $photodata[0];
560 $image['height'] = $photodata[1];
561 $image['contenttype'] = $photodata['mime'];
562 $image['blurhash'] = $photodata['blurhash'] ?? null;
563 unset($image['url']);
573 $siteinfo['images'] = array_values(array_filter($siteinfo['images']));
576 foreach (['audio', 'video'] as $element) {
577 if (!empty($siteinfo[$element])) {
578 array_walk($siteinfo[$element], function (&$media) use ($page_url, &$siteinfo) {
583 foreach (['embed', 'content', 'url'] as $field) {
584 if (!empty($media[$field])) {
585 $media[$field] = self::completeUrl($media[$field], $page_url);
586 $type = self::getContentType($media[$field]);
587 if (($type[0] ?? '') == 'text') {
588 if ($field == 'embed') {
589 $embed = $media[$field];
591 $url = $media[$field];
593 } elseif (!empty($type[0])) {
594 $content = $media[$field];
595 $contenttype = implode('/', $type);
598 unset($media[$field]);
601 foreach (['image', 'preview'] as $field) {
602 if (!empty($media[$field])) {
603 $media[$field] = self::completeUrl($media[$field], $page_url);
608 $media['url'] = $url;
610 if (!empty($embed)) {
611 $media['embed'] = $embed;
612 if (empty($siteinfo['player']['embed'])) {
613 $siteinfo['player']['embed'] = $embed;
616 if (!empty($content)) {
617 $media['src'] = $content;
619 if (!empty($contenttype)) {
620 $media['contenttype'] = $contenttype;
622 if (empty($url) && empty($content) && empty($embed)) {
628 $siteinfo[$element] = array_values(array_filter($siteinfo[$element]));
630 if (empty($siteinfo[$element])) {
631 unset($siteinfo[$element]);
638 * Convert tags from CSV to an array
640 * @param string $string Tags
642 * @return array with formatted Hashtags
644 public static function convertTagsToArray(string $string): array
646 $arr_tags = str_getcsv($string);
647 if (count($arr_tags)) {
648 // add the # sign to every tag
649 array_walk($arr_tags, [self::class, 'arrAddHashes']);
657 * Add a hasht sign to a string
659 * This method is used as callback function
661 * @param string $tag The pure tag name
662 * @param int $k Counter for internal use
666 private static function arrAddHashes(string &$tag, int $k)
672 * Add a scheme to an url
674 * The src attribute of some html elements (e.g. images)
675 * can miss the scheme so we need to add the correct
678 * @param string $url The url which possibly does have
679 * a missing scheme (a link to an image)
680 * @param string $scheme The url with a correct scheme
681 * (e.g. the url from the webpage which does contain the image)
683 * @return string The url with a scheme
685 private static function completeUrl(string $url, string $scheme): string
687 $urlarr = parse_url($url);
689 // If the url does allready have an scheme
690 // we can stop the process here
691 if (isset($urlarr['scheme'])) {
695 $schemearr = parse_url($scheme);
697 $complete = $schemearr['scheme'] . '://' . $schemearr['host'];
699 if (!empty($schemearr['port'])) {
700 $complete .= ':' . $schemearr['port'];
703 if (!empty($urlarr['path'])) {
704 if (strpos($urlarr['path'], '/') !== 0) {
708 $complete .= $urlarr['path'];
711 if (!empty($urlarr['query'])) {
712 $complete .= '?' . $urlarr['query'];
715 if (!empty($urlarr['fragment'])) {
716 $complete .= '#' . $urlarr['fragment'];
723 * Parse the Json-Ld parts of a web page
725 * @param array $siteinfo
726 * @param array $jsonld
728 * @return array siteinfo
730 private static function parseParts(array $siteinfo, array $jsonld): array
732 if (!empty($jsonld['@graph']) && is_array($jsonld['@graph'])) {
733 foreach ($jsonld['@graph'] as $part) {
734 if (!empty($part) && is_array($part)) {
735 $siteinfo = self::parseParts($siteinfo, $part);
738 } elseif (!empty($jsonld['@type'])) {
739 $siteinfo = self::parseJsonLd($siteinfo, $jsonld);
740 } elseif (!empty($jsonld)) {
741 $keys = array_keys($jsonld);
742 $numeric_keys = true;
743 foreach ($keys as $key) {
745 $numeric_keys = false;
749 foreach ($jsonld as $part) {
750 if (!empty($part) && is_array($part)) {
751 $siteinfo = self::parseParts($siteinfo, $part);
757 array_walk_recursive($siteinfo, function (&$element) {
758 if (is_string($element)) {
759 $element = trim(strip_tags(html_entity_decode($element, ENT_COMPAT, 'UTF-8')));
767 * Improve the siteinfo with information from the provided JSON-LD information
768 * @see https://jsonld.com/
769 * @see https://schema.org/
771 * @param array $siteinfo
772 * @param array $jsonld
774 * @return array siteinfo
776 private static function parseJsonLd(array $siteinfo, array $jsonld): array
778 $type = JsonLD::fetchElement($jsonld, '@type');
780 Logger::info('Empty type', ['url' => $siteinfo['url']]);
784 // Silently ignore some types that aren't processed
785 if (in_array($type, ['SiteNavigationElement', 'JobPosting', 'CreativeWork', 'MusicAlbum',
786 'WPHeader', 'WPSideBar', 'WPFooter', 'LegalService', 'MusicRecording',
787 'ItemList', 'BreadcrumbList', 'Blog', 'Dataset', 'Product'])) {
793 case 'AdvertiserContentArticle':
796 case 'SatiricalArticle':
797 case 'ScholarlyArticle':
798 case 'SocialMediaPosting':
800 case 'ReportageNewsArticle':
801 case 'SocialMediaPosting':
803 case 'LiveBlogPosting':
804 case 'DiscussionForumPosting':
805 return self::parseJsonLdArticle($siteinfo, $jsonld);
809 case 'CollectionPage':
813 case 'MedicalWebPage':
816 case 'RealEstateListing':
817 case 'SearchResultsPage':
823 return self::parseJsonLdWebPage($siteinfo, $jsonld);
825 return self::parseJsonLdWebSite($siteinfo, $jsonld);
830 case 'EducationalOrganization':
831 case 'FundingScheme':
832 case 'GovernmentOrganization':
833 case 'LibrarySystem':
834 case 'LocalBusiness':
835 case 'MedicalOrganization':
837 case 'NewsMediaOrganization':
839 case 'SportsOrganization':
841 return self::parseJsonLdWebOrganization($siteinfo, $jsonld);
844 case 'PerformingGroup':
848 return self::parseJsonLdWebPerson($siteinfo, $jsonld);
851 return self::parseJsonLdMediaObject($siteinfo, $jsonld, 'audio');
853 return self::parseJsonLdMediaObject($siteinfo, $jsonld, 'video');
855 return self::parseJsonLdMediaObject($siteinfo, $jsonld, 'images');
857 Logger::info('Unknown type', ['type' => $type, 'url' => $siteinfo['url']]);
863 * Fetch author and publisher data
865 * @param array $siteinfo
866 * @param array $jsonld
868 * @return array siteinfo
870 private static function parseJsonLdAuthor(array $siteinfo, array $jsonld): array
874 if (!empty($jsonld['publisher']) && is_array($jsonld['publisher'])) {
875 $content = JsonLD::fetchElement($jsonld, 'publisher', 'name');
876 if (!empty($content) && is_string($content)) {
877 $jsonldinfo['publisher_name'] = trim($content);
880 $content = JsonLD::fetchElement($jsonld, 'publisher', 'url');
881 if (!empty($content) && is_string($content)) {
882 $jsonldinfo['publisher_url'] = trim($content);
885 $brand = JsonLD::fetchElement($jsonld, 'publisher', 'brand', '@type', 'Organization');
886 if (!empty($brand) && is_array($brand)) {
887 $content = JsonLD::fetchElement($brand, 'name');
888 if (!empty($content) && is_string($content)) {
889 $jsonldinfo['publisher_name'] = trim($content);
892 $content = JsonLD::fetchElement($brand, 'url');
893 if (!empty($content) && is_string($content)) {
894 $jsonldinfo['publisher_url'] = trim($content);
897 $content = JsonLD::fetchElement($brand, 'logo', 'url');
898 if (!empty($content) && is_string($content)) {
899 $jsonldinfo['publisher_img'] = trim($content);
903 $logo = JsonLD::fetchElement($jsonld, 'publisher', 'logo');
904 if (!empty($logo) && is_array($logo)) {
905 $content = JsonLD::fetchElement($logo, 'url');
906 if (!empty($content) && is_string($content)) {
907 $jsonldinfo['publisher_img'] = trim($content);
910 } elseif (!empty($jsonld['publisher']) && is_string($jsonld['publisher'])) {
911 $jsonldinfo['publisher_name'] = trim($jsonld['publisher']);
914 if (!empty($jsonld['author']) && is_array($jsonld['author'])) {
915 $content = JsonLD::fetchElement($jsonld, 'author', 'name');
916 if (!empty($content) && is_string($content)) {
917 $jsonldinfo['author_name'] = trim($content);
920 $content = JsonLD::fetchElement($jsonld, 'author', 'sameAs');
921 if (!empty($content) && is_string($content)) {
922 $jsonldinfo['author_url'] = trim($content);
925 $content = JsonLD::fetchElement($jsonld, 'author', 'url');
926 if (!empty($content) && is_string($content)) {
927 $jsonldinfo['author_url'] = trim($content);
930 $logo = JsonLD::fetchElement($jsonld, 'author', 'logo');
931 if (!empty($logo) && is_array($logo)) {
932 $content = JsonLD::fetchElement($logo, 'url');
933 if (!empty($content) && is_string($content)) {
934 $jsonldinfo['author_img'] = trim($content);
937 } elseif (!empty($jsonld['author']) && is_string($jsonld['author'])) {
938 $jsonldinfo['author_name'] = trim($jsonld['author']);
941 Logger::info('Fetched Author information', ['fetched' => $jsonldinfo]);
943 return array_merge($siteinfo, $jsonldinfo);
947 * Fetch data from the provided JSON-LD Article type
948 * @see https://schema.org/Article
950 * @param array $siteinfo
951 * @param array $jsonld
953 * @return array siteinfo
955 private static function parseJsonLdArticle(array $siteinfo, array $jsonld): array
959 $content = JsonLD::fetchElement($jsonld, 'headline');
960 if (!empty($content) && is_string($content)) {
961 $jsonldinfo['title'] = trim($content);
964 $content = JsonLD::fetchElement($jsonld, 'alternativeHeadline');
965 if (!empty($content) && is_string($content) && (($jsonldinfo['title'] ?? '') != trim($content))) {
966 $jsonldinfo['alternative_title'] = trim($content);
969 $content = JsonLD::fetchElement($jsonld, 'description');
970 if (!empty($content) && is_string($content)) {
971 $jsonldinfo['text'] = trim($content);
974 $content = JsonLD::fetchElement($jsonld, 'thumbnailUrl');
975 if (!empty($content)) {
976 $jsonldinfo['image'] = trim($content);
979 $content = JsonLD::fetchElement($jsonld, 'image', 'url', '@type', 'ImageObject');
980 if (!empty($content) && is_string($content)) {
981 $jsonldinfo['image'] = trim($content);
984 if (!empty($jsonld['keywords']) && !is_array($jsonld['keywords'])) {
985 $content = JsonLD::fetchElement($jsonld, 'keywords');
986 if (!empty($content)) {
987 $siteinfo['keywords'] = [];
988 $keywords = explode(',', $content);
989 foreach ($keywords as $keyword) {
990 $siteinfo['keywords'][] = trim($keyword);
993 } elseif (!empty($jsonld['keywords'])) {
994 $content = JsonLD::fetchElementArray($jsonld, 'keywords');
995 if (!empty($content) && is_array($content)) {
996 $jsonldinfo['keywords'] = $content;
1000 $content = JsonLD::fetchElement($jsonld, 'datePublished');
1001 if (!empty($content) && is_string($content)) {
1002 $jsonldinfo['published'] = DateTimeFormat::utc($content);
1005 $content = JsonLD::fetchElement($jsonld, 'dateModified');
1006 if (!empty($content) && is_string($content)) {
1007 $jsonldinfo['modified'] = DateTimeFormat::utc($content);
1010 $jsonldinfo = self::parseJsonLdAuthor($jsonldinfo, $jsonld);
1012 Logger::info('Fetched article information', ['url' => $siteinfo['url'], 'fetched' => $jsonldinfo]);
1014 return array_merge($siteinfo, $jsonldinfo);
1018 * Fetch data from the provided JSON-LD WebPage type
1019 * @see https://schema.org/WebPage
1021 * @param array $siteinfo
1022 * @param array $jsonld
1024 * @return array siteinfo
1026 private static function parseJsonLdWebPage(array $siteinfo, array $jsonld): array
1030 $content = JsonLD::fetchElement($jsonld, 'name');
1031 if (!empty($content)) {
1032 $jsonldinfo['title'] = trim($content);
1035 $content = JsonLD::fetchElement($jsonld, 'description');
1036 if (!empty($content) && is_string($content)) {
1037 $jsonldinfo['text'] = trim($content);
1040 $content = JsonLD::fetchElement($jsonld, 'image');
1041 if (!empty($content) && is_string($content)) {
1042 $jsonldinfo['image'] = trim($content);
1045 $content = JsonLD::fetchElement($jsonld, 'thumbnailUrl');
1046 if (!empty($content) && is_string($content)) {
1047 $jsonldinfo['image'] = trim($content);
1050 $jsonldinfo = self::parseJsonLdAuthor($jsonldinfo, $jsonld);
1052 Logger::info('Fetched WebPage information', ['url' => $siteinfo['url'], 'fetched' => $jsonldinfo]);
1054 return array_merge($siteinfo, $jsonldinfo);
1058 * Fetch data from the provided JSON-LD WebSite type
1059 * @see https://schema.org/WebSite
1061 * @param array $siteinfo
1062 * @param array $jsonld
1064 * @return array siteinfo
1066 private static function parseJsonLdWebSite(array $siteinfo, array $jsonld): array
1070 $content = JsonLD::fetchElement($jsonld, 'name');
1071 if (!empty($content) && is_string($content)) {
1072 $jsonldinfo['publisher_name'] = trim($content);
1075 $content = JsonLD::fetchElement($jsonld, 'description');
1076 if (!empty($content) && is_string($content)) {
1077 $jsonldinfo['publisher_description'] = trim($content);
1080 $content = JsonLD::fetchElement($jsonld, 'url');
1081 if (!empty($content) && is_string($content)) {
1082 $jsonldinfo['publisher_url'] = trim($content);
1085 $content = JsonLD::fetchElement($jsonld, 'thumbnailUrl');
1086 if (!empty($content) && is_string($content)) {
1087 $jsonldinfo['image'] = trim($content);
1090 $jsonldinfo = self::parseJsonLdAuthor($jsonldinfo, $jsonld);
1092 Logger::info('Fetched WebSite information', ['url' => $siteinfo['url'], 'fetched' => $jsonldinfo]);
1093 return array_merge($siteinfo, $jsonldinfo);
1097 * Fetch data from the provided JSON-LD Organization type
1098 * @see https://schema.org/Organization
1100 * @param array $siteinfo
1101 * @param array $jsonld
1103 * @return array siteinfo
1105 private static function parseJsonLdWebOrganization(array $siteinfo, array $jsonld): array
1109 $content = JsonLD::fetchElement($jsonld, 'name');
1110 if (!empty($content) && is_string($content)) {
1111 $jsonldinfo['publisher_name'] = trim($content);
1114 $content = JsonLD::fetchElement($jsonld, 'description');
1115 if (!empty($content) && is_string($content)) {
1116 $jsonldinfo['publisher_description'] = trim($content);
1119 $content = JsonLD::fetchElement($jsonld, 'url');
1120 if (!empty($content) && is_string($content)) {
1121 $jsonldinfo['publisher_url'] = trim($content);
1124 $content = JsonLD::fetchElement($jsonld, 'logo', 'url', '@type', 'ImageObject');
1125 if (!empty($content) && is_string($content)) {
1126 $jsonldinfo['publisher_img'] = trim($content);
1127 } elseif (!empty($content) && is_array($content)) {
1128 $jsonldinfo['publisher_img'] = trim($content[0]);
1131 $content = JsonLD::fetchElement($jsonld, 'brand', 'name', '@type', 'Organization');
1132 if (!empty($content) && is_string($content)) {
1133 $jsonldinfo['publisher_name'] = trim($content);
1136 $content = JsonLD::fetchElement($jsonld, 'brand', 'url', '@type', 'Organization');
1137 if (!empty($content) && is_string($content)) {
1138 $jsonldinfo['publisher_url'] = trim($content);
1141 Logger::info('Fetched Organization information', ['url' => $siteinfo['url'], 'fetched' => $jsonldinfo]);
1142 return array_merge($siteinfo, $jsonldinfo);
1146 * Fetch data from the provided JSON-LD Person type
1147 * @see https://schema.org/Person
1149 * @param array $siteinfo
1150 * @param array $jsonld
1152 * @return array siteinfo
1154 private static function parseJsonLdWebPerson(array $siteinfo, array $jsonld): array
1158 $content = JsonLD::fetchElement($jsonld, 'name');
1159 if (!empty($content) && is_string($content)) {
1160 $jsonldinfo['author_name'] = trim($content);
1163 $content = JsonLD::fetchElement($jsonld, 'description');
1164 if (!empty($content) && is_string($content)) {
1165 $jsonldinfo['author_description'] = trim($content);
1168 $content = JsonLD::fetchElement($jsonld, 'sameAs');
1169 if (!empty($content) && is_string($content)) {
1170 $jsonldinfo['author_url'] = trim($content);
1173 $content = JsonLD::fetchElement($jsonld, 'url');
1174 if (!empty($content) && is_string($content)) {
1175 $jsonldinfo['author_url'] = trim($content);
1178 $content = JsonLD::fetchElement($jsonld, 'image', 'url', '@type', 'ImageObject');
1179 if (!empty($content) && !is_string($content)) {
1180 Logger::notice('Unexpected return value for the author image', ['content' => $content]);
1183 if (!empty($content) && is_string($content)) {
1184 $jsonldinfo['author_img'] = trim($content);
1187 Logger::info('Fetched Person information', ['url' => $siteinfo['url'], 'fetched' => $jsonldinfo]);
1188 return array_merge($siteinfo, $jsonldinfo);
1192 * Fetch data from the provided JSON-LD MediaObject type
1193 * @see https://schema.org/MediaObject
1195 * @param array $siteinfo
1196 * @param array $jsonld
1198 * @return array siteinfo
1200 private static function parseJsonLdMediaObject(array $siteinfo, array $jsonld, string $name): array
1204 $content = JsonLD::fetchElement($jsonld, 'caption');
1205 if (!empty($content) && is_string($content)) {
1206 $media['caption'] = trim($content);
1209 $content = JsonLD::fetchElement($jsonld, 'url');
1210 if (!empty($content) && is_string($content)) {
1211 $media['url'] = trim($content);
1214 $content = JsonLD::fetchElement($jsonld, 'mainEntityOfPage');
1215 if (!empty($content) && is_string($content)) {
1216 $media['main'] = Strings::compareLink($content, $siteinfo['url']);
1219 $content = JsonLD::fetchElement($jsonld, 'description');
1220 if (!empty($content) && is_string($content)) {
1221 $media['description'] = trim($content);
1224 $content = JsonLD::fetchElement($jsonld, 'name');
1225 if (!empty($content) && (($media['description'] ?? '') != trim($content))) {
1226 $media['name'] = trim($content);
1229 $content = JsonLD::fetchElement($jsonld, 'contentUrl');
1230 if (!empty($content) && is_string($content)) {
1231 $media['content'] = trim($content);
1234 $content = JsonLD::fetchElement($jsonld, 'embedUrl');
1235 if (!empty($content) && is_string($content)) {
1236 $media['embed'] = trim($content);
1239 $content = JsonLD::fetchElement($jsonld, 'height');
1240 if (!empty($content) && is_string($content)) {
1241 $media['height'] = trim($content);
1244 $content = JsonLD::fetchElement($jsonld, 'width');
1245 if (!empty($content) && is_string($content)) {
1246 $media['width'] = trim($content);
1249 $content = JsonLD::fetchElement($jsonld, 'image');
1250 if (!empty($content) && is_string($content)) {
1251 $media['image'] = trim($content);
1254 $content = JsonLD::fetchElement($jsonld, 'thumbnailUrl');
1255 if (!empty($content) && (($media['image'] ?? '') != trim($content))) {
1256 if (!empty($media['image'])) {
1257 $media['preview'] = trim($content);
1259 $media['image'] = trim($content);
1263 Logger::info('Fetched Media information', ['url' => $siteinfo['url'], 'fetched' => $media]);
1264 $siteinfo[$name][] = $media;