X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FParseUrl.php;h=1f8fbdeabe4e8898d2a4442196a46a2093c2afd3;hb=f2c31ef1c0e92208b58d22791fc72d0ad3e3d6ae;hp=dd39a052a0c529bfccfa4248174848264bec1c16;hpb=2592c3891ce3134d591c890c7bd7f789f554f208;p=friendica.git diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index dd39a052a0..1f8fbdeabe 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -8,11 +8,9 @@ namespace Friendica\Util; use DOMDocument; use DOMXPath; use Friendica\Content\OEmbed; -use Friendica\Core\Addon; +use Friendica\Core\Hook; +use Friendica\Core\Logger; use Friendica\Database\DBA; -use Friendica\Object\Image; - -require_once 'include/dba.php'; /** * @brief Class with methods for extracting certain content from an url @@ -23,10 +21,10 @@ class ParseUrl * @brief 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 + * @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 @@ -38,7 +36,8 @@ class ParseUrl * array'images' = Array of preview pictures * string 'keywords' => The tags which belong to the content * - * @see ParseUrl::getSiteinfo() for more information about scraping + * @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) @@ -48,7 +47,7 @@ class ParseUrl } $parsed_url = DBA::selectFirst('parsed_url', ['content'], - ['url' => normalise_link($url), 'guessing' => !$no_guessing, 'oembed' => $do_oembed] + ['url' => Strings::normaliseLink($url), 'guessing' => !$no_guessing, 'oembed' => $do_oembed] ); if (!empty($parsed_url['content'])) { $data = unserialize($parsed_url['content']); @@ -60,7 +59,7 @@ class ParseUrl DBA::insert( 'parsed_url', [ - 'url' => normalise_link($url), 'guessing' => !$no_guessing, + 'url' => Strings::normaliseLink($url), 'guessing' => !$no_guessing, 'oembed' => $do_oembed, 'content' => serialize($data), 'created' => DateTimeFormat::utcNow() ], @@ -69,6 +68,7 @@ class ParseUrl return $data; } + /** * @brief Parse a page for embeddable content information * @@ -80,11 +80,11 @@ class ParseUrl * \ * * @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 - * @param int $count Internal counter to avoid endless loops + * @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 + * @param int $count Internal counter to avoid endless loops * * @return array which contains needed data for embedding * string 'url' => The url of the parsed page @@ -96,7 +96,8 @@ class ParseUrl * array'images' = Array of preview pictures * string 'keywords' => The tags which belong to the content * - * @todo https://developers.google.com/+/plugins/snippet/ + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @todo https://developers.google.com/+/plugins/snippet/ * @verbatim * * @@ -111,8 +112,6 @@ class ParseUrl */ public static function getSiteinfo($url, $no_guessing = false, $do_oembed = true, $count = 1) { - $a = get_app(); - $siteinfo = []; // Check if the URL does contain a scheme @@ -123,7 +122,7 @@ class ParseUrl } if ($count > 10) { - logger('Endless loop detected for ' . $url, LOGGER_DEBUG); + Logger::log('Endless loop detected for ' . $url, Logger::DEBUG); return $siteinfo; } @@ -135,23 +134,23 @@ class ParseUrl $siteinfo['url'] = $url; $siteinfo['type'] = 'link'; - $data = Network::curl($url); - if (!$data['success']) { + $curlResult = Network::curl($url); + if (!$curlResult->isSuccess()) { return $siteinfo; } // If the file is too large then exit - if ($data['info']['download_content_length'] > 1000000) { + if (($curlResult->getInfo()['download_content_length'] ?? 0) > 1000000) { return $siteinfo; } // If it isn't a HTML file then exit - if (($data['info']['content_type'] != '') && !strstr(strtolower($data['info']['content_type']), 'html')) { + if (($curlResult->getContentType() != '') && !strstr(strtolower($curlResult->getContentType()), 'html')) { return $siteinfo; } - $header = $data['header']; - $body = $data['body']; + $header = $curlResult->getHeader(); + $body = $curlResult->getBody(); if ($do_oembed) { $oembed_data = OEmbed::fetchURL($url); @@ -161,7 +160,8 @@ class ParseUrl $siteinfo['type'] = $oembed_data->type; } - if (($oembed_data->type == 'link') && ($siteinfo['type'] != 'photo')) { + // 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); } @@ -181,12 +181,11 @@ class ParseUrl $charset = trim(trim(trim(array_pop($matches)), ';,')); } - if ($charset == '') { - $charset = 'utf-8'; - } + if ($charset && strtoupper($charset) != 'UTF-8') { + // See https://github.com/friendica/friendica/issues/5470#issuecomment-418351211 + $charset = str_ireplace('latin-1', 'latin1', $charset); - if (($charset != '') && (strtoupper($charset) != 'UTF-8')) { - logger('detected charset ' . $charset, LOGGER_DEBUG); + Logger::log('detected charset ' . $charset, Logger::DEBUG); $body = iconv($charset, 'UTF-8//TRANSLIT', $body); } @@ -333,11 +332,11 @@ class ParseUrl } // Prevent to have a photo type without an image - if (empty($siteinfo['image']) && ($siteinfo['type'] == 'photo')) { + if ((empty($siteinfo['image']) || !empty($siteinfo['text'])) && ($siteinfo['type'] == 'photo')) { $siteinfo['type'] = 'link'; } - if ((@$siteinfo['image'] == '') && !$no_guessing) { + if (empty($siteinfo['image']) && !$no_guessing) { $list = $xpath->query('//img[@src]'); foreach ($list as $node) { $img_tag = []; @@ -348,7 +347,7 @@ class ParseUrl } $src = self::completeUrl($img_tag['src'], $url); - $photodata = Image::getInfoFromURL($src); + $photodata = Images::getInfoFromURLCached($src); if (($photodata) && ($photodata[0] > 150) && ($photodata[1] > 150)) { if ($photodata[0] > 300) { @@ -371,7 +370,7 @@ class ParseUrl unset($siteinfo['image']); - $photodata = Image::getInfoFromURL($src); + $photodata = Images::getInfoFromURLCached($src); if (($photodata) && ($photodata[0] > 10) && ($photodata[1] > 10)) { $siteinfo['images'][] = ['src' => $src, @@ -420,9 +419,9 @@ class ParseUrl } } - logger('Siteinfo for ' . $url . ' ' . print_r($siteinfo, true), LOGGER_DEBUG); + Logger::log('Siteinfo for ' . $url . ' ' . print_r($siteinfo, true), Logger::DEBUG); - Addon::callHooks('getsiteinfo', $siteinfo); + Hook::callAll('getsiteinfo', $siteinfo); return $siteinfo; } @@ -486,21 +485,23 @@ class ParseUrl $complete = $schemearr["scheme"]."://".$schemearr["host"]; - if (@$schemearr["port"] != "") { + if (!empty($schemearr["port"])) { $complete .= ":".$schemearr["port"]; } - if (strpos($urlarr["path"], "/") !== 0) { - $complete .= "/"; - } + if (!empty($urlarr["path"])) { + if (strpos($urlarr["path"], "/") !== 0) { + $complete .= "/"; + } - $complete .= $urlarr["path"]; + $complete .= $urlarr["path"]; + } - if (@$urlarr["query"] != "") { + if (!empty($urlarr["query"])) { $complete .= "?".$urlarr["query"]; } - if (@$urlarr["fragment"] != "") { + if (!empty($urlarr["fragment"])) { $complete .= "#".$urlarr["fragment"]; }