3 * @file src/Util/ParseUrl.php
4 * @brief Get informations about a given URL
6 namespace Friendica\Util;
10 use Friendica\Content\OEmbed;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Logger;
13 use Friendica\Database\DBA;
16 * @brief Class with methods for extracting certain content from an url
21 * @brief Search for chached embeddable data of an url otherwise fetch it
23 * @param string $url The url of the page which should be scraped
24 * @param bool $no_guessing If true the parse doens't search for
26 * @param bool $do_oembed The false option is used by the function fetch_oembed()
27 * to avoid endless loops
29 * @return array which contains needed data for embedding
30 * string 'url' => The url of the parsed page
31 * string 'type' => Content type
32 * string 'title' => The title of the content
33 * string 'text' => The description for the content
34 * string 'image' => A preview image of the content (only available
35 * if $no_geuessing = false
36 * array'images' = Array of preview pictures
37 * string 'keywords' => The tags which belong to the content
39 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
40 * @see ParseUrl::getSiteinfo() for more information about scraping
43 public static function getSiteinfoCached($url, $no_guessing = false, $do_oembed = true)
49 $parsed_url = DBA::selectFirst('parsed_url', ['content'],
50 ['url' => Strings::normaliseLink($url), 'guessing' => !$no_guessing, 'oembed' => $do_oembed]
52 if (!empty($parsed_url['content'])) {
53 $data = unserialize($parsed_url['content']);
57 $data = self::getSiteinfo($url, $no_guessing, $do_oembed);
62 'url' => Strings::normaliseLink($url), 'guessing' => !$no_guessing,
63 'oembed' => $do_oembed, 'content' => serialize($data),
64 'created' => DateTimeFormat::utcNow()
73 * @brief Parse a page for embeddable content information
75 * This method parses to url for meta data which can be used to embed
76 * the content. If available it prioritizes Open Graph meta tags.
77 * If this is not available it uses the twitter cards meta tags.
78 * As fallback it uses standard html elements with meta informations
79 * like \<title\>Awesome Title\</title\> or
80 * \<meta name="description" content="An awesome description"\>
82 * @param string $url The url of the page which should be scraped
83 * @param bool $no_guessing If true the parse doens't search for
85 * @param bool $do_oembed The false option is used by the function fetch_oembed()
86 * to avoid endless loops
87 * @param int $count Internal counter to avoid endless loops
89 * @return array which contains needed data for embedding
90 * string 'url' => The url of the parsed page
91 * string 'type' => Content type
92 * string 'title' => The title of the content
93 * string 'text' => The description for the content
94 * string 'image' => A preview image of the content (only available
95 * if $no_geuessing = false
96 * array'images' = Array of preview pictures
97 * string 'keywords' => The tags which belong to the content
99 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
100 * @todo https://developers.google.com/+/plugins/snippet/
102 * <meta itemprop="name" content="Awesome title">
103 * <meta itemprop="description" content="An awesome description">
104 * <meta itemprop="image" content="http://maple.libertreeproject.org/images/tree-icon.png">
106 * <body itemscope itemtype="http://schema.org/Product">
107 * <h1 itemprop="name">Shiny Trinket</h1>
108 * <img itemprop="image" src="{image-url}" />
109 * <p itemprop="description">Shiny trinkets are shiny.</p>
113 public static function getSiteinfo($url, $no_guessing = false, $do_oembed = true, $count = 1)
117 // Check if the URL does contain a scheme
118 $scheme = parse_url($url, PHP_URL_SCHEME);
121 $url = 'http://' . trim($url, '/');
125 Logger::log('Endless loop detected for ' . $url, Logger::DEBUG);
129 $url = trim($url, "'");
130 $url = trim($url, '"');
132 $url = Network::stripTrackingQueryParams($url);
134 $siteinfo['url'] = $url;
135 $siteinfo['type'] = 'link';
137 $curlResult = Network::curl($url);
138 if (!$curlResult->isSuccess()) {
142 // If the file is too large then exit
143 if (($curlResult->getInfo()['download_content_length'] ?? 0) > 1000000) {
147 // If it isn't a HTML file then exit
148 if (($curlResult->getContentType() != '') && !strstr(strtolower($curlResult->getContentType()), 'html')) {
152 $header = $curlResult->getHeader();
153 $body = $curlResult->getBody();
156 $oembed_data = OEmbed::fetchURL($url);
158 if (!empty($oembed_data->type)) {
159 if (!in_array($oembed_data->type, ['error', 'rich', ''])) {
160 $siteinfo['type'] = $oembed_data->type;
163 // See https://github.com/friendica/friendica/pull/5763#discussion_r217913178
164 if ($siteinfo['type'] != 'photo') {
165 if (isset($oembed_data->title)) {
166 $siteinfo['title'] = trim($oembed_data->title);
168 if (isset($oembed_data->description)) {
169 $siteinfo['text'] = trim($oembed_data->description);
171 if (isset($oembed_data->thumbnail_url)) {
172 $siteinfo['image'] = $oembed_data->thumbnail_url;
178 // Fetch the first mentioned charset. Can be in body or header
180 if (preg_match('/charset=(.*?)[\'"\s\n]/', $header, $matches)) {
181 $charset = trim(trim(trim(array_pop($matches)), ';,'));
184 if ($charset && strtoupper($charset) != 'UTF-8') {
185 // See https://github.com/friendica/friendica/issues/5470#issuecomment-418351211
186 $charset = str_ireplace('latin-1', 'latin1', $charset);
188 Logger::log('detected charset ' . $charset, Logger::DEBUG);
189 $body = iconv($charset, 'UTF-8//TRANSLIT', $body);
192 $body = mb_convert_encoding($body, 'HTML-ENTITIES', 'UTF-8');
194 $doc = new DOMDocument();
195 @$doc->loadHTML($body);
197 XML::deleteNode($doc, 'style');
198 XML::deleteNode($doc, 'script');
199 XML::deleteNode($doc, 'option');
200 XML::deleteNode($doc, 'h1');
201 XML::deleteNode($doc, 'h2');
202 XML::deleteNode($doc, 'h3');
203 XML::deleteNode($doc, 'h4');
204 XML::deleteNode($doc, 'h5');
205 XML::deleteNode($doc, 'h6');
206 XML::deleteNode($doc, 'ol');
207 XML::deleteNode($doc, 'ul');
209 $xpath = new DOMXPath($doc);
211 $list = $xpath->query('//meta[@content]');
212 foreach ($list as $node) {
214 if ($node->attributes->length) {
215 foreach ($node->attributes as $attribute) {
216 $meta_tag[$attribute->name] = $attribute->value;
220 if (@$meta_tag['http-equiv'] == 'refresh') {
221 $path = $meta_tag['content'];
222 $pathinfo = explode(';', $path);
224 foreach ($pathinfo as $value) {
225 if (substr(strtolower($value), 0, 4) == 'url=') {
226 $content = substr($value, 4);
229 if ($content != '') {
230 $siteinfo = self::getSiteinfo($content, $no_guessing, $do_oembed, ++$count);
236 $list = $xpath->query('//title');
237 if ($list->length > 0) {
238 $siteinfo['title'] = trim($list->item(0)->nodeValue);
241 $list = $xpath->query('//meta[@name]');
242 foreach ($list as $node) {
244 if ($node->attributes->length) {
245 foreach ($node->attributes as $attribute) {
246 $meta_tag[$attribute->name] = $attribute->value;
250 if (empty($meta_tag['content'])) {
254 $meta_tag['content'] = trim(html_entity_decode($meta_tag['content'], ENT_QUOTES, 'UTF-8'));
256 switch (strtolower($meta_tag['name'])) {
258 $siteinfo['title'] = trim($meta_tag['content']);
261 $siteinfo['text'] = trim($meta_tag['content']);
264 $siteinfo['image'] = $meta_tag['content'];
266 case 'twitter:image':
267 $siteinfo['image'] = $meta_tag['content'];
269 case 'twitter:image:src':
270 $siteinfo['image'] = $meta_tag['content'];
273 // Detect photo pages
274 if ($meta_tag['content'] == 'summary_large_image') {
275 $siteinfo['type'] = 'photo';
278 case 'twitter:description':
279 $siteinfo['text'] = trim($meta_tag['content']);
281 case 'twitter:title':
282 $siteinfo['title'] = trim($meta_tag['content']);
285 $siteinfo['title'] = trim($meta_tag['content']);
287 case 'dc.description':
288 $siteinfo['text'] = trim($meta_tag['content']);
291 $keywords = explode(',', $meta_tag['content']);
293 case 'news_keywords':
294 $keywords = explode(',', $meta_tag['content']);
299 if (isset($keywords)) {
300 $siteinfo['keywords'] = [];
301 foreach ($keywords as $keyword) {
302 if (!in_array(trim($keyword), $siteinfo['keywords'])) {
303 $siteinfo['keywords'][] = trim($keyword);
308 $list = $xpath->query('//meta[@property]');
309 foreach ($list as $node) {
311 if ($node->attributes->length) {
312 foreach ($node->attributes as $attribute) {
313 $meta_tag[$attribute->name] = $attribute->value;
317 if (!empty($meta_tag['content'])) {
318 $meta_tag['content'] = trim(html_entity_decode($meta_tag['content'], ENT_QUOTES, 'UTF-8'));
320 switch (strtolower($meta_tag['property'])) {
322 $siteinfo['image'] = $meta_tag['content'];
325 $siteinfo['title'] = trim($meta_tag['content']);
327 case 'og:description':
328 $siteinfo['text'] = trim($meta_tag['content']);
334 // Prevent to have a photo type without an image
335 if ((empty($siteinfo['image']) || !empty($siteinfo['text'])) && ($siteinfo['type'] == 'photo')) {
336 $siteinfo['type'] = 'link';
339 if (empty($siteinfo['image']) && !$no_guessing) {
340 $list = $xpath->query('//img[@src]');
341 foreach ($list as $node) {
343 if ($node->attributes->length) {
344 foreach ($node->attributes as $attribute) {
345 $img_tag[$attribute->name] = $attribute->value;
349 $src = self::completeUrl($img_tag['src'], $url);
350 $photodata = Images::getInfoFromURLCached($src);
352 if (($photodata) && ($photodata[0] > 150) && ($photodata[1] > 150)) {
353 if ($photodata[0] > 300) {
354 $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
357 if ($photodata[1] > 300) {
358 $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
361 $siteinfo['images'][] = [
363 'width' => $photodata[0],
364 'height' => $photodata[1]
368 } elseif (!empty($siteinfo['image'])) {
369 $src = self::completeUrl($siteinfo['image'], $url);
371 unset($siteinfo['image']);
373 $photodata = Images::getInfoFromURLCached($src);
375 if (($photodata) && ($photodata[0] > 10) && ($photodata[1] > 10)) {
376 $siteinfo['images'][] = ['src' => $src,
377 'width' => $photodata[0],
378 'height' => $photodata[1]];
382 if ((@$siteinfo['text'] == '') && (@$siteinfo['title'] != '') && !$no_guessing) {
385 $list = $xpath->query('//div[@class="article"]');
386 foreach ($list as $node) {
387 if (strlen($node->nodeValue) > 40) {
388 $text .= ' ' . trim($node->nodeValue);
393 $list = $xpath->query('//div[@class="content"]');
394 foreach ($list as $node) {
395 if (strlen($node->nodeValue) > 40) {
396 $text .= ' ' . trim($node->nodeValue);
401 // If none text was found then take the paragraph content
403 $list = $xpath->query('//p');
404 foreach ($list as $node) {
405 if (strlen($node->nodeValue) > 40) {
406 $text .= ' ' . trim($node->nodeValue);
412 $text = trim(str_replace(["\n", "\r"], [' ', ' '], $text));
414 while (strpos($text, ' ')) {
415 $text = trim(str_replace(' ', ' ', $text));
418 $siteinfo['text'] = trim(html_entity_decode(substr($text, 0, 350), ENT_QUOTES, 'UTF-8') . '...');
422 Logger::log('Siteinfo for ' . $url . ' ' . print_r($siteinfo, true), Logger::DEBUG);
424 Hook::callAll('getsiteinfo', $siteinfo);
430 * @brief Convert tags from CSV to an array
432 * @param string $string Tags
433 * @return array with formatted Hashtags
435 public static function convertTagsToArray($string)
437 $arr_tags = str_getcsv($string);
438 if (count($arr_tags)) {
439 // add the # sign to every tag
440 array_walk($arr_tags, ["self", "arrAddHashes"]);
447 * @brief Add a hasht sign to a string
449 * This method is used as callback function
451 * @param string $tag The pure tag name
452 * @param int $k Counter for internal use
455 private static function arrAddHashes(&$tag, $k)
461 * @brief Add a scheme to an url
463 * The src attribute of some html elements (e.g. images)
464 * can miss the scheme so we need to add the correct
467 * @param string $url The url which possibly does have
468 * a missing scheme (a link to an image)
469 * @param string $scheme The url with a correct scheme
470 * (e.g. the url from the webpage which does contain the image)
472 * @return string The url with a scheme
474 private static function completeUrl($url, $scheme)
476 $urlarr = parse_url($url);
478 // If the url does allready have an scheme
479 // we can stop the process here
480 if (isset($urlarr["scheme"])) {
484 $schemearr = parse_url($scheme);
486 $complete = $schemearr["scheme"]."://".$schemearr["host"];
488 if (!empty($schemearr["port"])) {
489 $complete .= ":".$schemearr["port"];
492 if (!empty($urlarr["path"])) {
493 if (strpos($urlarr["path"], "/") !== 0) {
497 $complete .= $urlarr["path"];
500 if (!empty($urlarr["query"])) {
501 $complete .= "?".$urlarr["query"];
504 if (!empty($urlarr["fragment"])) {
505 $complete .= "#".$urlarr["fragment"];