- * @file include/items.php
- */
-
-use Friendica\Content\Feature;
-use Friendica\Core\Addon;
-use Friendica\Core\Config;
-use Friendica\Core\L10n;
-use Friendica\Core\PConfig;
-use Friendica\Core\System;
-use Friendica\Database\dba;
-use Friendica\Database\DBM;
-use Friendica\Model\Item;
-use Friendica\Protocol\DFRN;
-use Friendica\Protocol\Feed;
-use Friendica\Protocol\OStatus;
-use Friendica\Util\DateTimeFormat;
-use Friendica\Util\Network;
-use Friendica\Util\ParseUrl;
-use Friendica\Util\Temporal;
-
-require_once 'include/text.php';
-require_once 'mod/share.php';
-require_once 'include/enotify.php';
-
-function add_page_info_data($data, $no_photos = false) {
- Addon::callHooks('page_info_data', $data);
-
- // It maybe is a rich content, but if it does have everything that a link has,
- // then treat it that way
- if (($data["type"] == "rich") && is_string($data["title"]) &&
- is_string($data["text"]) && !empty($data["images"])) {
- $data["type"] = "link";
- }
-
- $data["title"] = defaults($data, "title", "");
-
- if ((($data["type"] != "link") && ($data["type"] != "video") && ($data["type"] != "photo")) || ($data["title"] == $data["url"])) {
- return "";
- }
-
- if ($no_photos && ($data["type"] == "photo")) {
- return "";
- }
-
- // Escape some bad characters
- $data["url"] = str_replace(["[", "]"], ["[", "]"], htmlentities($data["url"], ENT_QUOTES, 'UTF-8', false));
- $data["title"] = str_replace(["[", "]"], ["[", "]"], htmlentities($data["title"], ENT_QUOTES, 'UTF-8', false));
-
- $text = "[attachment type='".$data["type"]."'";
-
- if (empty($data["text"])) {
- $data["text"] = $data["title"];
- }
-
- if (empty($data["text"])) {
- $data["text"] = $data["url"];
- }
-
- if (!empty($data["url"])) {
- $text .= " url='".$data["url"]."'";
- }
-
- if (!empty($data["title"])) {
- $text .= " title='".$data["title"]."'";
- }
-
- if (!empty($data["images"])) {
- $preview = str_replace(["[", "]"], ["[", "]"], htmlentities($data["images"][0]["src"], ENT_QUOTES, 'UTF-8', false));
- // if the preview picture is larger than 500 pixels then show it in a larger mode
- // But only, if the picture isn't higher than large (To prevent huge posts)
- if (!Config::get('system', 'always_show_preview') && ($data["images"][0]["width"] >= 500)
- && ($data["images"][0]["width"] >= $data["images"][0]["height"])) {
- $text .= " image='".$preview."'";
- } else {
- $text .= " preview='".$preview."'";
- }
- }
-
- $text .= "]".$data["text"]."[/attachment]";
-
- $hashtags = "";
- if (isset($data["keywords"]) && count($data["keywords"])) {
- $hashtags = "\n";
- foreach ($data["keywords"] AS $keyword) {
- /// @TODO make a positive list of allowed characters
- $hashtag = str_replace([" ", "+", "/", ".", "#", "'", "’", "`", "(", ")", "„", "“"],
- ["", "", "", "", "", "", "", "", "", "", "", ""], $keyword);
- $hashtags .= "#[url=" . System::baseUrl() . "/search?tag=" . rawurlencode($hashtag) . "]" . $hashtag . "[/url] ";
- }
- }
-
- return "\n".$text.$hashtags;
-}
-
-function query_page_info($url, $photo = "", $keywords = false, $keyword_blacklist = "") {
-
- $data = ParseUrl::getSiteinfoCached($url, true);
-
- if ($photo != "") {
- $data["images"][0]["src"] = $photo;
- }
-
- logger('fetch page info for ' . $url . ' ' . print_r($data, true), LOGGER_DEBUG);
-
- if (!$keywords && isset($data["keywords"])) {
- unset($data["keywords"]);
- }
-
- if (($keyword_blacklist != "") && isset($data["keywords"])) {
- $list = explode(", ", $keyword_blacklist);
- foreach ($list AS $keyword) {
- $keyword = trim($keyword);
- $index = array_search($keyword, $data["keywords"]);
- if ($index !== false) {
- unset($data["keywords"][$index]);
- }
- }
- }
-
- return $data;
-}
-
-function add_page_keywords($url, $photo = "", $keywords = false, $keyword_blacklist = "") {
- $data = query_page_info($url, $photo, $keywords, $keyword_blacklist);
-
- $tags = "";
- if (isset($data["keywords"]) && count($data["keywords"])) {
- foreach ($data["keywords"] AS $keyword) {
- $hashtag = str_replace([" ", "+", "/", ".", "#", "'"],
- ["", "", "", "", "", ""], $keyword);
-
- if ($tags != "") {
- $tags .= ", ";
- }
-
- $tags .= "#[url=" . System::baseUrl() . "/search?tag=" . rawurlencode($hashtag) . "]" . $hashtag . "[/url]";
- }
- }
-
- return $tags;
-}
-
-function add_page_info($url, $no_photos = false, $photo = "", $keywords = false, $keyword_blacklist = "") {
- $data = query_page_info($url, $photo, $keywords, $keyword_blacklist);
-
- $text = add_page_info_data($data, $no_photos);
-
- return $text;
-}
-
-function add_page_info_to_body($body, $texturl = false, $no_photos = false) {
-
- logger('add_page_info_to_body: fetch page info for body ' . $body, LOGGER_DEBUG);
-
- $URLSearchString = "^\[\]";
-
- // Fix for Mastodon where the mentions are in a different format
- $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#!@])(.*?)\[\/url\]/ism",
- '$2[url=$1]$3[/url]', $body);
-
- // Adding these spaces is a quick hack due to my problems with regular expressions :)
- preg_match("/[^!#@]\[url\]([$URLSearchString]*)\[\/url\]/ism", " " . $body, $matches);
-
- if (!$matches) {
- preg_match("/[^!#@]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", " " . $body, $matches);
- }
-
- // Convert urls without bbcode elements
- if (!$matches && $texturl) {
- preg_match("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", " ".$body, $matches);
-
- // Yeah, a hack. I really hate regular expressions :)
- if ($matches) {
- $matches[1] = $matches[2];
- }
- }
-
- if ($matches) {
- $footer = add_page_info($matches[1], $no_photos);
- }
-
- // Remove the link from the body if the link is attached at the end of the post
- if (isset($footer) && (trim($footer) != "") && (strpos($footer, $matches[1]))) {
- $removedlink = trim(str_replace($matches[1], "", $body));
- if (($removedlink == "") || strstr($body, $removedlink)) {
- $body = $removedlink;
- }
-
- $url = str_replace(['/', '.'], ['\/', '\.'], $matches[1]);
- $removedlink = preg_replace("/\[url\=" . $url . "\](.*?)\[\/url\]/ism", '', $body);
- if (($removedlink == "") || strstr($body, $removedlink)) {
- $body = $removedlink;
- }
- }
-
- // Add the page information to the bottom
- if (isset($footer) && (trim($footer) != "")) {
- $body .= $footer;
- }
-
- return $body;
-}
-
-/**