3 * @copyright Copyright (C) 2020, Friendica
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\Core\Hook;
28 use Friendica\Core\Logger;
29 use Friendica\Database\Database;
30 use Friendica\Database\DBA;
32 use Friendica\Network\HTTPException;
35 * Get information about a given URL
37 * Class with methods for extracting certain content from an url
41 const DEFAULT_EXPIRATION_FAILURE = 'now + 1 day';
42 const DEFAULT_EXPIRATION_SUCCESS = 'now + 3 months';
45 * Maximum number of characters for the description
47 const MAX_DESC_COUNT = 250;
50 * Minimum number of characters for the description
52 const MIN_DESC_COUNT = 100;
55 * Fetch the content type of the given url
56 * @param string $url URL of the page
57 * @return array content type
59 public static function getContentType(string $url)
61 $curlResult = DI::httpRequest()->head($url);
62 if (!$curlResult->isSuccess()) {
66 $contenttype = $curlResult->getHeader('Content-Type');
67 if (empty($contenttype)) {
71 return explode('/', current(explode(';', $contenttype)));
75 * Search for chached embeddable data of an url otherwise fetch it
77 * @param string $url The url of the page which should be scraped
78 * @param bool $do_oembed The false option is used by the function fetch_oembed()
79 * to avoid endless loops
81 * @return array which contains needed data for embedding
82 * string 'url' => The url of the parsed page
83 * string 'type' => Content type
84 * string 'title' => (optional) The title of the content
85 * string 'text' => (optional) The description for the content
86 * string 'image' => (optional) A preview image of the content
87 * array 'images' => (optional) Array of preview pictures
88 * string 'keywords' => (optional) The tags which belong to the content
90 * @throws HTTPException\InternalServerErrorException
91 * @see ParseUrl::getSiteinfo() for more information about scraping
94 public static function getSiteinfoCached($url, $do_oembed = true): array
103 $urlHash = hash('sha256', $url);
105 $parsed_url = DBA::selectFirst('parsed_url', ['content'],
106 ['url_hash' => $urlHash, 'oembed' => $do_oembed]
108 if (!empty($parsed_url['content'])) {
109 $data = unserialize($parsed_url['content']);
113 $data = self::getSiteinfo($url, $do_oembed);
115 $expires = $data['expires'];
117 unset($data['expires']);
122 'url_hash' => $urlHash,
123 'oembed' => $do_oembed,
125 'content' => serialize($data),
126 'created' => DateTimeFormat::utcNow(),
127 'expires' => $expires,
129 Database::INSERT_UPDATE
136 * Parse a page for embeddable content information
138 * This method parses to url for meta data which can be used to embed
139 * the content. If available it prioritizes Open Graph meta tags.
140 * If this is not available it uses the twitter cards meta tags.
141 * As fallback it uses standard html elements with meta informations
142 * like \<title\>Awesome Title\</title\> or
143 * \<meta name="description" content="An awesome description"\>
145 * @param string $url The url of the page which should be scraped
146 * @param bool $do_oembed The false option is used by the function fetch_oembed()
147 * to avoid endless loops
148 * @param int $count Internal counter to avoid endless loops
150 * @return array which contains needed data for embedding
151 * string 'url' => The url of the parsed page
152 * string 'type' => Content type (error, link, photo, image, audio, video)
153 * string 'title' => (optional) The title of the content
154 * string 'text' => (optional) The description for the content
155 * string 'image' => (optional) A preview image of the content
156 * array 'images' => (optional) Array of preview pictures
157 * string 'keywords' => (optional) The tags which belong to the content
159 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
160 * @todo https://developers.google.com/+/plugins/snippet/
162 * <meta itemprop="name" content="Awesome title">
163 * <meta itemprop="description" content="An awesome description">
164 * <meta itemprop="image" content="http://maple.libertreeproject.org/images/tree-icon.png">
166 * <body itemscope itemtype="http://schema.org/Product">
167 * <h1 itemprop="name">Shiny Trinket</h1>
168 * <img itemprop="image" src="{image-url}" />
169 * <p itemprop="description">Shiny trinkets are shiny.</p>
173 public static function getSiteinfo($url, $do_oembed = true, $count = 1)
182 // Check if the URL does contain a scheme
183 $scheme = parse_url($url, PHP_URL_SCHEME);
186 $url = 'http://' . ltrim($url, '/');
189 $url = trim($url, "'\"");
191 $url = Network::stripTrackingQueryParams($url);
196 'expires' => DateTimeFormat::utc(self::DEFAULT_EXPIRATION_FAILURE),
200 Logger::log('Endless loop detected for ' . $url, Logger::DEBUG);
204 $type = self::getContentType($url);
205 Logger::info('Got content-type', ['content-type' => $type, 'url' => $url]);
206 if (!empty($type) && in_array($type[0], ['image', 'video', 'audio'])) {
207 $siteinfo['type'] = $type[0];
211 if ((count($type) >= 2) && (($type[0] != 'text') || ($type[1] != 'html'))) {
212 Logger::info('Unparseable content-type, quitting here, ', ['content-type' => $type, 'url' => $url]);
216 $curlResult = DI::httpRequest()->get($url);
217 if (!$curlResult->isSuccess()) {
221 $siteinfo['expires'] = DateTimeFormat::utc(self::DEFAULT_EXPIRATION_SUCCESS);
223 // If the file is too large then exit
224 if (($curlResult->getInfo()['download_content_length'] ?? 0) > 1000000) {
228 if ($cacheControlHeader = $curlResult->getHeader('Cache-Control')) {
229 if (preg_match('/max-age=([0-9]+)/i', $cacheControlHeader, $matches)) {
230 $maxAge = max(86400, (int)array_pop($matches));
231 $siteinfo['expires'] = DateTimeFormat::utc("now + $maxAge seconds");
235 $header = $curlResult->getHeader();
236 $body = $curlResult->getBody();
239 $oembed_data = OEmbed::fetchURL($url, false, false);
241 if (!empty($oembed_data->type)) {
242 if (!in_array($oembed_data->type, ['error', 'rich', 'image', 'video', 'audio', ''])) {
243 $siteinfo['type'] = $oembed_data->type;
246 // See https://github.com/friendica/friendica/pull/5763#discussion_r217913178
247 if ($siteinfo['type'] != 'photo') {
248 if (!empty($oembed_data->title)) {
249 $siteinfo['title'] = trim($oembed_data->title);
251 if (!empty($oembed_data->description)) {
252 $siteinfo['text'] = trim($oembed_data->description);
254 if (!empty($oembed_data->author_name)) {
255 $siteinfo['author_name'] = trim($oembed_data->author_name);
257 if (!empty($oembed_data->author_url)) {
258 $siteinfo['author_url'] = trim($oembed_data->author_url);
260 if (!empty($oembed_data->provider_name)) {
261 $siteinfo['publisher_name'] = trim($oembed_data->provider_name);
263 if (!empty($oembed_data->provider_url)) {
264 $siteinfo['publisher_url'] = trim($oembed_data->provider_url);
266 if (!empty($oembed_data->thumbnail_url)) {
267 $siteinfo['image'] = $oembed_data->thumbnail_url;
274 // Look for a charset, first in headers
275 // Expected form: Content-Type: text/html; charset=ISO-8859-4
276 if (preg_match('/charset=([a-z0-9-_.\/]+)/i', $header, $matches)) {
277 $charset = trim(trim(trim(array_pop($matches)), ';,'));
280 // Then in body that gets precedence
282 // - <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
283 // - <meta charset="utf-8">
284 // - <meta charset=utf-8>
285 // - <meta charSet="utf-8">
286 // We escape <style> and <script> tags since they can contain irrelevant charset information
287 // (see https://github.com/friendica/friendica/issues/9251#issuecomment-698636806)
288 Strings::performWithEscapedBlocks($body, '#<(?:style|script).*?</(?:style|script)>#ism', function ($body) use (&$charset) {
289 if (preg_match('/charset=["\']?([a-z0-9-_.\/]+)/i', $body, $matches)) {
290 $charset = trim(trim(trim(array_pop($matches)), ';,'));
294 $siteinfo['charset'] = $charset;
296 if ($charset && strtoupper($charset) != 'UTF-8') {
297 // See https://github.com/friendica/friendica/issues/5470#issuecomment-418351211
298 $charset = str_ireplace('latin-1', 'latin1', $charset);
300 Logger::log('detected charset ' . $charset, Logger::DEBUG);
301 $body = iconv($charset, 'UTF-8//TRANSLIT', $body);
304 $body = mb_convert_encoding($body, 'HTML-ENTITIES', 'UTF-8');
306 $doc = new DOMDocument();
307 @$doc->loadHTML($body);
309 XML::deleteNode($doc, 'style');
310 XML::deleteNode($doc, 'option');
311 XML::deleteNode($doc, 'h1');
312 XML::deleteNode($doc, 'h2');
313 XML::deleteNode($doc, 'h3');
314 XML::deleteNode($doc, 'h4');
315 XML::deleteNode($doc, 'h5');
316 XML::deleteNode($doc, 'h6');
317 XML::deleteNode($doc, 'ol');
318 XML::deleteNode($doc, 'ul');
320 $xpath = new DOMXPath($doc);
322 $list = $xpath->query('//meta[@content]');
323 foreach ($list as $node) {
325 if ($node->attributes->length) {
326 foreach ($node->attributes as $attribute) {
327 $meta_tag[$attribute->name] = $attribute->value;
331 if (@$meta_tag['http-equiv'] == 'refresh') {
332 $path = $meta_tag['content'];
333 $pathinfo = explode(';', $path);
335 foreach ($pathinfo as $value) {
336 if (substr(strtolower($value), 0, 4) == 'url=') {
337 $content = substr($value, 4);
340 if ($content != '') {
341 $siteinfo = self::getSiteinfo($content, $do_oembed, ++$count);
347 $list = $xpath->query('//title');
348 if ($list->length > 0) {
349 $siteinfo['title'] = trim($list->item(0)->nodeValue);
352 $list = $xpath->query('//meta[@name]');
353 foreach ($list as $node) {
355 if ($node->attributes->length) {
356 foreach ($node->attributes as $attribute) {
357 $meta_tag[$attribute->name] = $attribute->value;
361 if (empty($meta_tag['content'])) {
365 $meta_tag['content'] = trim(html_entity_decode($meta_tag['content'], ENT_QUOTES, 'UTF-8'));
367 switch (strtolower($meta_tag['name'])) {
369 $siteinfo['title'] = trim($meta_tag['content']);
372 $siteinfo['text'] = trim($meta_tag['content']);
375 $siteinfo['image'] = $meta_tag['content'];
377 case 'twitter:image':
378 $siteinfo['image'] = $meta_tag['content'];
380 case 'twitter:image:src':
381 $siteinfo['image'] = $meta_tag['content'];
384 // Detect photo pages
385 if ($meta_tag['content'] == 'summary_large_image') {
386 $siteinfo['type'] = 'photo';
389 case 'twitter:description':
390 $siteinfo['text'] = trim($meta_tag['content']);
392 case 'twitter:title':
393 $siteinfo['title'] = trim($meta_tag['content']);
396 $siteinfo['title'] = trim($meta_tag['content']);
398 case 'dc.description':
399 $siteinfo['text'] = trim($meta_tag['content']);
402 $siteinfo['publisher_name'] = trim($meta_tag['content']);
405 $keywords = explode(',', $meta_tag['content']);
407 case 'news_keywords':
408 $keywords = explode(',', $meta_tag['content']);
413 if (isset($keywords)) {
414 $siteinfo['keywords'] = [];
415 foreach ($keywords as $keyword) {
416 if (!in_array(trim($keyword), $siteinfo['keywords'])) {
417 $siteinfo['keywords'][] = trim($keyword);
422 $list = $xpath->query('//meta[@property]');
423 foreach ($list as $node) {
425 if ($node->attributes->length) {
426 foreach ($node->attributes as $attribute) {
427 $meta_tag[$attribute->name] = $attribute->value;
431 if (!empty($meta_tag['content'])) {
432 $meta_tag['content'] = trim(html_entity_decode($meta_tag['content'], ENT_QUOTES, 'UTF-8'));
434 switch (strtolower($meta_tag['property'])) {
436 $siteinfo['image'] = $meta_tag['content'];
439 $siteinfo['image'] = $meta_tag['content'];
441 case 'og:image:secure_url':
442 $siteinfo['image'] = $meta_tag['content'];
445 $siteinfo['title'] = trim($meta_tag['content']);
447 case 'og:description':
448 $siteinfo['text'] = trim($meta_tag['content']);
451 $siteinfo['publisher_name'] = trim($meta_tag['content']);
453 case 'twitter:description':
454 $siteinfo['text'] = trim($meta_tag['content']);
456 case 'twitter:title':
457 $siteinfo['title'] = trim($meta_tag['content']);
459 case 'twitter:image':
460 $siteinfo['image'] = $meta_tag['content'];
466 $list = $xpath->query("//script[@type='application/ld+json']");
467 foreach ($list as $node) {
468 if (!empty($node->nodeValue)) {
469 $nodevalue = html_entity_decode($node->nodeValue, ENT_COMPAT, 'UTF-8');
470 if ($jsonld = json_decode($nodevalue, true)) {
471 if (!empty($jsonld['@graph']) && is_array($jsonld['@graph'])) {
472 foreach ($jsonld['@graph'] as $part) {
473 $siteinfo = self::parseJsonLd($siteinfo, $part);
476 $siteinfo = self::parseJsonLd($siteinfo, $jsonld);
482 // Prevent to have a photo type without an image
483 if ((empty($siteinfo['image']) || !empty($siteinfo['text'])) && ($siteinfo['type'] == 'photo')) {
484 $siteinfo['type'] = 'link';
487 if (!empty($siteinfo['image'])) {
488 $src = self::completeUrl($siteinfo['image'], $url);
490 unset($siteinfo['image']);
492 $photodata = Images::getInfoFromURLCached($src);
494 if (($photodata) && ($photodata[0] > 10) && ($photodata[1] > 10)) {
495 $siteinfo['images'][] = ['src' => $src,
496 'width' => $photodata[0],
497 'height' => $photodata[1]];
501 if (!empty($siteinfo['text']) && mb_strlen($siteinfo['text']) > self::MAX_DESC_COUNT) {
502 $siteinfo['text'] = mb_substr($siteinfo['text'], 0, self::MAX_DESC_COUNT) . '…';
503 $pos = mb_strrpos($siteinfo['text'], '.');
504 if ($pos > self::MIN_DESC_COUNT) {
505 $siteinfo['text'] = mb_substr($siteinfo['text'], 0, $pos + 1);
509 Logger::info('Siteinfo fetched', ['url' => $url, 'siteinfo' => $siteinfo]);
511 Hook::callAll('getsiteinfo', $siteinfo);
517 * Improve the siteinfo with information from the provided JSON-LD information
518 * @see https://jsonld.com/
520 * @param array $siteinfo
521 * @param array $jsonld
522 * @return array siteinfo
524 private static function parseJsonLd(array $siteinfo, array $jsonld)
526 $type = JsonLD::fetchElement($jsonld, '@type');
531 return self::parseJsonLdArticle($siteinfo, $jsonld);
533 return self::parseJsonLdWebPage($siteinfo, $jsonld);
535 return self::parseJsonLdWebSite($siteinfo, $jsonld);
537 return self::parseJsonLdWebOrganization($siteinfo, $jsonld);
539 return self::parseJsonLdWebPerson($siteinfo, $jsonld);
540 case 'BreadcrumbList':
541 case 'Audio': /// @todo Can contain direct media links to audio - can be interesting in the future
544 case 'LiveBlogPosting':
545 case 'SocialMediaPosting':
549 Logger::info('Unsupported or unknown type', ['type' => $type, 'url' => $siteinfo['url']]);
555 * Improve the siteinfo with information from the provided JSON-LD information concerning authors and publishers
557 * @param array $siteinfo
558 * @param array $jsonld
559 * @return array siteinfo
561 private static function parseJsonLdAuthor(array $siteinfo, array $jsonld)
565 if (!empty($jsonld['publisher']) && is_array($jsonld['publisher'])) {
566 $content = JsonLD::fetchElement($jsonld, 'publisher', 'name', '@type', 'Person');
567 if (!empty($content) && is_string($content)) {
568 $jsonldinfo['publisher_name'] = trim($content);
571 $content = JsonLD::fetchElement($jsonld, 'publisher', 'url', '@type', 'Person');
572 if (!empty($content) && is_string($content)) {
573 $jsonldinfo['publisher_url'] = trim($content);
576 $content = JsonLD::fetchElement($jsonld, 'publisher', 'name', '@type', 'Organization');
577 if (!empty($content) && is_string($content)) {
578 $jsonldinfo['publisher_name'] = trim($content);
581 $content = JsonLD::fetchElement($jsonld, 'publisher', 'url', '@type', 'Organization');
582 if (!empty($content) && is_string($content)) {
583 $jsonldinfo['publisher_url'] = trim($content);
586 $brand = JsonLD::fetchElement($jsonld, 'publisher', 'brand', '@type', 'Organization');
587 if (!empty($brand) && is_array($brand)) {
588 $content = JsonLD::fetchElement($brand, 'name', '@type', 'brand');
589 if (!empty($content) && is_string($content)) {
590 $jsonldinfo['publisher_name'] = trim($content);
595 if (!empty($jsonld['author']) && is_array($jsonld['author'])) {
596 $content = JsonLD::fetchElement($jsonld, 'author', 'name', '@type', 'Organization');
597 if (!empty($content) && is_string($content)) {
598 $jsonldinfo['author_name'] = trim($content);
601 $content = JsonLD::fetchElement($jsonld, 'author', 'url', '@type', 'Organization');
602 if (!empty($content) && is_string($content)) {
603 $jsonldinfo['author_url'] = trim($content);
606 $content = JsonLD::fetchElement($jsonld, 'author', 'name', '@type', 'Person');
607 if (!empty($content) && is_string($content)) {
608 $jsonldinfo['author_name'] = trim($content);
611 $content = JsonLD::fetchElement($jsonld, 'author', 'url', '@type', 'Person');
612 if (!empty($content) && is_string($content)) {
613 $jsonldinfo['author_url'] = trim($content);
617 Logger::info('Fetched author information', ['fetched' => $jsonldinfo]);
619 return array_merge($siteinfo, $jsonldinfo);
623 * Improve the siteinfo with information from the provided JSON-LD Article information
625 * @param array $siteinfo
626 * @param array $jsonld
627 * @return array siteinfo
629 private static function parseJsonLdArticle(array $siteinfo, array $jsonld)
633 $content = JsonLD::fetchElement($jsonld, 'headline');
634 if (!empty($content) && is_string($content)) {
635 $jsonldinfo['title'] = trim($content);
638 $content = JsonLD::fetchElement($jsonld, 'alternativeHeadline');
639 if (!empty($content) && is_string($content)) {
640 $jsonldinfo['alternative_title'] = trim($content);
643 $content = JsonLD::fetchElement($jsonld, 'description');
644 if (!empty($content) && is_string($content)) {
645 $jsonldinfo['text'] = trim($content);
648 $content = JsonLD::fetchElement($jsonld, 'image', 'url', '@type', 'ImageObject');
649 if (!empty($content) && is_string($content)) {
650 $jsonldinfo['image'] = trim($content);
653 /// @todo Check for the correct separator, also check for dpublicates before adding
654 // $content = JsonLD::fetchElement($jsonld, 'keywords');
655 // if (!empty($content) && is_string($content)) {
656 // $jsonldinfo['keywords'] = trim($content);
659 $jsonldinfo = self::parseJsonLdAuthor($jsonldinfo, $jsonld);
661 Logger::info('Fetched article information', ['url' => $siteinfo['url'], 'fetched' => $jsonldinfo]);
663 return array_merge($siteinfo, $jsonldinfo);
667 * Improve the siteinfo with information from the provided JSON-LD WebPage information
669 * @param array $siteinfo
670 * @param array $jsonld
671 * @return array siteinfo
673 private static function parseJsonLdWebPage(array $siteinfo, array $jsonld)
677 $content = JsonLD::fetchElement($jsonld, 'name');
678 if (!empty($content)) {
679 $jsonldinfo['title'] = trim($content);
682 $content = JsonLD::fetchElement($jsonld, 'description');
683 if (!empty($content)) {
684 $jsonldinfo['text'] = trim($content);
687 $content = JsonLD::fetchElement($jsonld, 'thumbnailUrl');
688 if (!empty($content)) {
689 $jsonldinfo['image'] = trim($content);
692 $jsonldinfo = self::parseJsonLdAuthor($jsonldinfo, $jsonld);
694 Logger::info('Fetched webpage information', ['url' => $siteinfo['url'], 'fetched' => $jsonldinfo]);
696 return array_merge($siteinfo, $jsonldinfo);
700 * Improve the siteinfo with information from the provided JSON-LD WebSite information
702 * @param array $siteinfo
703 * @param array $jsonld
704 * @return array siteinfo
706 private static function parseJsonLdWebSite(array $siteinfo, array $jsonld)
710 $content = JsonLD::fetchElement($jsonld, 'name');
711 if (!empty($content)) {
712 $jsonldinfo['publisher_name'] = trim($content);
715 $content = JsonLD::fetchElement($jsonld, 'description');
716 if (!empty($content)) {
717 $jsonldinfo['publisher_description'] = trim($content);
720 $content = JsonLD::fetchElement($jsonld, 'url');
721 if (!empty($content)) {
722 $jsonldinfo['publisher_url'] = trim($content);
725 $content = JsonLD::fetchElement($jsonld, 'thumbnailUrl');
726 if (!empty($content)) {
727 $jsonldinfo['image'] = trim($content);
730 $jsonldinfo = self::parseJsonLdAuthor($jsonldinfo, $jsonld);
732 Logger::info('Fetched WebSite information', ['url' => $siteinfo['url'], 'fetched' => $jsonldinfo]);
733 return array_merge($siteinfo, $jsonldinfo);
737 * Improve the siteinfo with information from the provided JSON-LD Organization information
739 * @param array $siteinfo
740 * @param array $jsonld
741 * @return array siteinfo
743 private static function parseJsonLdWebOrganization(array $siteinfo, array $jsonld)
747 $content = JsonLD::fetchElement($jsonld, 'name');
748 if (!empty($content)) {
749 $jsonldinfo['publisher_name'] = trim($content);
752 $content = JsonLD::fetchElement($jsonld, 'url');
753 if (!empty($content)) {
754 $jsonldinfo['publisher_url'] = trim($content);
757 $content = JsonLD::fetchElement($jsonld, 'logo', 'url', '@type', 'ImageObject');
758 if (!empty($content)) {
759 $jsonldinfo['publisher_img'] = trim($content);
762 Logger::info('Fetched Organization information', ['url' => $siteinfo['url'], 'fetched' => $jsonldinfo]);
763 return array_merge($siteinfo, $jsonldinfo);
767 * Improve the siteinfo with information from the provided JSON-LD Person information
769 * @param array $siteinfo
770 * @param array $jsonld
771 * @return array siteinfo
773 private static function parseJsonLdWebPerson(array $siteinfo, array $jsonld)
777 $content = JsonLD::fetchElement($jsonld, 'name');
778 if (!empty($content)) {
779 $jsonldinfo['author_name'] = trim($content);
782 $content = JsonLD::fetchElement($jsonld, 'description');
783 if (!empty($content)) {
784 $jsonldinfo['author_description'] = trim($content);
787 $content = JsonLD::fetchElement($jsonld, 'url');
788 if (!empty($content)) {
789 $jsonldinfo['author_url'] = trim($content);
792 $content = JsonLD::fetchElement($jsonld, 'image', 'url', '@type', 'ImageObject');
793 if (!empty($content)) {
794 $jsonldinfo['author_img'] = trim($content);
797 Logger::info('Fetched Person information', ['url' => $siteinfo['url'], 'fetched' => $jsonldinfo]);
798 return array_merge($siteinfo, $jsonldinfo);
802 * Convert tags from CSV to an array
804 * @param string $string Tags
805 * @return array with formatted Hashtags
807 public static function convertTagsToArray($string)
809 $arr_tags = str_getcsv($string);
810 if (count($arr_tags)) {
811 // add the # sign to every tag
812 array_walk($arr_tags, ["self", "arrAddHashes"]);
819 * Add a hasht sign to a string
821 * This method is used as callback function
823 * @param string $tag The pure tag name
824 * @param int $k Counter for internal use
827 private static function arrAddHashes(&$tag, $k)
833 * Add a scheme to an url
835 * The src attribute of some html elements (e.g. images)
836 * can miss the scheme so we need to add the correct
839 * @param string $url The url which possibly does have
840 * a missing scheme (a link to an image)
841 * @param string $scheme The url with a correct scheme
842 * (e.g. the url from the webpage which does contain the image)
844 * @return string The url with a scheme
846 private static function completeUrl($url, $scheme)
848 $urlarr = parse_url($url);
850 // If the url does allready have an scheme
851 // we can stop the process here
852 if (isset($urlarr["scheme"])) {
856 $schemearr = parse_url($scheme);
858 $complete = $schemearr["scheme"]."://".$schemearr["host"];
860 if (!empty($schemearr["port"])) {
861 $complete .= ":".$schemearr["port"];
864 if (!empty($urlarr["path"])) {
865 if (strpos($urlarr["path"], "/") !== 0) {
869 $complete .= $urlarr["path"];
872 if (!empty($urlarr["query"])) {
873 $complete .= "?".$urlarr["query"];
876 if (!empty($urlarr["fragment"])) {
877 $complete .= "#".$urlarr["fragment"];