. * */ namespace Friendica\Util; use DOMDocument; use DOMXPath; use Friendica\Content\OEmbed; use Friendica\Core\Hook; use Friendica\Core\Logger; use Friendica\Database\DBA; use Friendica\DI; /** * Get information about a given URL * * Class with methods for extracting certain content from an url */ class ParseUrl { /** * Maximum number of characters for the description */ const MAX_DESC_COUNT = 250; /** * Minimum number of characters for the description */ const MIN_DESC_COUNT = 100; /** * Search for chached embeddable data of an url otherwise fetch it * * @param string $url The url of the page which should be scraped * @param bool $no_guessing If true the parse doens't search for * preview pictures * @param bool $do_oembed The false option is used by the function fetch_oembed() * to avoid endless loops * * @return array which contains needed data for embedding * string 'url' => The url of the parsed page * string 'type' => Content type * string 'title' => (optional) The title of the content * string 'text' => (optional) The description for the content * string 'image' => (optional) A preview image of the content (only available if $no_geuessing = false) * array 'images' => (optional) Array of preview pictures * string 'keywords' => (optional) The tags which belong to the content * * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @see ParseUrl::getSiteinfo() for more information about scraping * embeddable content */ public static function getSiteinfoCached($url, $no_guessing = false, $do_oembed = true) { if ($url == "") { return false; } $parsed_url = DBA::selectFirst('parsed_url', ['content'], ['url' => Strings::normaliseLink($url), 'guessing' => !$no_guessing, 'oembed' => $do_oembed] ); if (!empty($parsed_url['content'])) { $data = unserialize($parsed_url['content']); return $data; } $data = self::getSiteinfo($url, $no_guessing, $do_oembed); DBA::insert( 'parsed_url', [ 'url' => substr(Strings::normaliseLink($url), 0, 255), 'guessing' => !$no_guessing, 'oembed' => $do_oembed, 'content' => serialize($data), 'created' => DateTimeFormat::utcNow() ], true ); return $data; } /** * Parse a page for embeddable content information * * This method parses to url for meta data which can be used to embed * the content. If available it prioritizes Open Graph meta tags. * If this is not available it uses the twitter cards meta tags. * As fallback it uses standard html elements with meta informations * like \
Shiny trinkets are shiny.
* * @endverbatim */ public static function getSiteinfo($url, $no_guessing = false, $do_oembed = true, $count = 1) { // Check if the URL does contain a scheme $scheme = parse_url($url, PHP_URL_SCHEME); if ($scheme == '') { $url = 'http://' . ltrim($url, '/'); } $url = trim($url, "'\""); $url = Network::stripTrackingQueryParams($url); $siteinfo = [ 'url' => $url, 'type' => 'link', ]; if ($count > 10) { Logger::log('Endless loop detected for ' . $url, Logger::DEBUG); return $siteinfo; } $curlResult = DI::httpRequest()->get($url, false, ['content_length' => 1000000]); if (!$curlResult->isSuccess()) { return $siteinfo; } // If it isn't a HTML file then exit if (!in_array('html', $curlResult->getContentType())) { return $siteinfo; } $body = $curlResult->getBody(); if ($do_oembed) { $oembed_data = OEmbed::fetchURL($url); if (!empty($oembed_data->type)) { if (!in_array($oembed_data->type, ['error', 'rich', ''])) { $siteinfo['type'] = $oembed_data->type; } // See https://github.com/friendica/friendica/pull/5763#discussion_r217913178 if ($siteinfo['type'] != 'photo') { if (isset($oembed_data->title)) { $siteinfo['title'] = trim($oembed_data->title); } if (isset($oembed_data->description)) { $siteinfo['text'] = trim($oembed_data->description); } if (isset($oembed_data->thumbnail_url)) { $siteinfo['image'] = $oembed_data->thumbnail_url; } } } } $charset = ''; // Look for a charset, first in headers // Expected form: Content-Type: text/html; charset=ISO-8859-4 foreach ($curlResult->getContentType() as $type) { if (preg_match('/charset=([a-z0-9-_.\/]+)/i', $type, $matches)) { $charset = trim(trim(trim(array_pop($matches)), ';,')); } } // Then in body that gets precedence // Expected forms: // - // - // - // - // We escape