2010-07-18 09:02:55 -04:00
|
|
|
|
<?php
|
2016-11-23 19:11:22 -05:00
|
|
|
|
/**
|
2020-02-09 10:18:46 -05:00
|
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
|
*
|
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
2016-11-23 19:11:22 -05:00
|
|
|
|
*/
|
2018-01-24 21:08:45 -05:00
|
|
|
|
|
2018-12-26 01:06:24 -05:00
|
|
|
|
use Friendica\Core\Hook;
|
2018-10-29 17:20:46 -04:00
|
|
|
|
use Friendica\Core\Logger;
|
2018-08-11 16:40:44 -04:00
|
|
|
|
use Friendica\Core\Protocol;
|
2018-10-31 10:35:50 -04:00
|
|
|
|
use Friendica\Core\Renderer;
|
2019-09-28 05:36:41 -04:00
|
|
|
|
use Friendica\Core\Session;
|
2018-07-20 08:19:26 -04:00
|
|
|
|
use Friendica\Database\DBA;
|
2019-12-15 18:28:31 -05:00
|
|
|
|
use Friendica\DI;
|
2018-01-24 21:08:45 -05:00
|
|
|
|
use Friendica\Model\Item;
|
2017-11-07 22:57:46 -05:00
|
|
|
|
use Friendica\Protocol\DFRN;
|
2017-12-13 02:02:52 -05:00
|
|
|
|
use Friendica\Protocol\Feed;
|
2018-01-24 21:08:45 -05:00
|
|
|
|
use Friendica\Protocol\OStatus;
|
2018-01-26 21:38:34 -05:00
|
|
|
|
use Friendica\Util\Network;
|
2018-01-04 12:03:15 -05:00
|
|
|
|
use Friendica\Util\ParseUrl;
|
2018-11-08 08:45:46 -05:00
|
|
|
|
use Friendica\Util\Strings;
|
2016-11-23 19:11:22 -05:00
|
|
|
|
|
2019-03-09 23:21:19 -05:00
|
|
|
|
require_once __DIR__ . '/../mod/share.php';
|
|
|
|
|
|
2018-07-30 21:02:34 -04:00
|
|
|
|
function add_page_info_data(array $data, $no_photos = false)
|
|
|
|
|
{
|
2018-12-26 01:06:24 -05:00
|
|
|
|
Hook::callAll('page_info_data', $data);
|
2014-01-05 10:10:02 -05:00
|
|
|
|
|
2018-08-24 07:09:58 -04:00
|
|
|
|
if (empty($data['type'])) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-04 04:52:53 -04:00
|
|
|
|
// It maybe is a rich content, but if it does have everything that a link has,
|
|
|
|
|
// then treat it that way
|
2017-06-07 22:00:59 -04:00
|
|
|
|
if (($data["type"] == "rich") && is_string($data["title"]) &&
|
2018-05-17 01:35:58 -04:00
|
|
|
|
is_string($data["text"]) && !empty($data["images"])) {
|
2014-04-04 04:52:53 -04:00
|
|
|
|
$data["type"] = "link";
|
2016-11-13 14:09:47 -05:00
|
|
|
|
}
|
2014-01-05 10:10:02 -05:00
|
|
|
|
|
2019-10-16 08:43:59 -04:00
|
|
|
|
$data["title"] = $data["title"] ?? '';
|
2018-07-10 08:27:56 -04:00
|
|
|
|
|
2017-06-07 22:00:59 -04:00
|
|
|
|
if ((($data["type"] != "link") && ($data["type"] != "video") && ($data["type"] != "photo")) || ($data["title"] == $data["url"])) {
|
2016-11-13 03:50:01 -05:00
|
|
|
|
return "";
|
2016-11-12 15:23:00 -05:00
|
|
|
|
}
|
2014-01-05 10:10:02 -05:00
|
|
|
|
|
2017-06-07 22:00:59 -04:00
|
|
|
|
if ($no_photos && ($data["type"] == "photo")) {
|
2016-11-13 14:09:47 -05:00
|
|
|
|
return "";
|
|
|
|
|
}
|
2014-01-05 10:10:02 -05:00
|
|
|
|
|
2016-04-17 14:43:41 -04:00
|
|
|
|
// Escape some bad characters
|
2018-01-15 08:05:12 -05:00
|
|
|
|
$data["url"] = str_replace(["[", "]"], ["[", "]"], htmlentities($data["url"], ENT_QUOTES, 'UTF-8', false));
|
|
|
|
|
$data["title"] = str_replace(["[", "]"], ["[", "]"], htmlentities($data["title"], ENT_QUOTES, 'UTF-8', false));
|
2016-04-17 14:43:41 -04:00
|
|
|
|
|
|
|
|
|
$text = "[attachment type='".$data["type"]."'";
|
|
|
|
|
|
2018-07-10 08:27:56 -04:00
|
|
|
|
if (empty($data["text"])) {
|
2016-11-12 15:23:00 -05:00
|
|
|
|
$data["text"] = $data["title"];
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 08:27:56 -04:00
|
|
|
|
if (empty($data["text"])) {
|
2016-11-12 15:23:00 -05:00
|
|
|
|
$data["text"] = $data["url"];
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 08:27:56 -04:00
|
|
|
|
if (!empty($data["url"])) {
|
2016-04-17 14:43:41 -04:00
|
|
|
|
$text .= " url='".$data["url"]."'";
|
2016-11-13 14:09:47 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 08:27:56 -04:00
|
|
|
|
if (!empty($data["title"])) {
|
2016-04-17 14:43:41 -04:00
|
|
|
|
$text .= " title='".$data["title"]."'";
|
2016-11-13 14:09:47 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-31 03:48:44 -04:00
|
|
|
|
// Only embedd a picture link when it seems to be a valid picture ("width" is set)
|
|
|
|
|
if (!empty($data["images"]) && !empty($data["images"][0]["width"])) {
|
2018-01-15 08:05:12 -05:00
|
|
|
|
$preview = str_replace(["[", "]"], ["[", "]"], htmlentities($data["images"][0]["src"], ENT_QUOTES, 'UTF-8', false));
|
2016-04-17 14:43:41 -04:00
|
|
|
|
// 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)
|
2020-01-19 15:21:13 -05:00
|
|
|
|
if (!DI::config()->get('system', 'always_show_preview') && ($data["images"][0]["width"] >= 500)
|
2017-11-25 05:07:49 -05:00
|
|
|
|
&& ($data["images"][0]["width"] >= $data["images"][0]["height"])) {
|
2016-04-17 14:43:41 -04:00
|
|
|
|
$text .= " image='".$preview."'";
|
2016-11-13 14:09:47 -05:00
|
|
|
|
} else {
|
2016-04-17 14:43:41 -04:00
|
|
|
|
$text .= " preview='".$preview."'";
|
2016-11-13 14:09:47 -05:00
|
|
|
|
}
|
2014-06-15 17:33:25 -04:00
|
|
|
|
}
|
2016-11-13 14:09:47 -05:00
|
|
|
|
|
2016-04-17 14:43:41 -04:00
|
|
|
|
$text .= "]".$data["text"]."[/attachment]";
|
2014-04-04 04:52:53 -04:00
|
|
|
|
|
2014-10-03 06:18:33 -04:00
|
|
|
|
$hashtags = "";
|
2017-06-07 22:00:59 -04:00
|
|
|
|
if (isset($data["keywords"]) && count($data["keywords"])) {
|
2014-10-03 07:02:15 -04:00
|
|
|
|
$hashtags = "\n";
|
2018-07-30 21:02:34 -04:00
|
|
|
|
foreach ($data["keywords"] as $keyword) {
|
2018-01-20 18:52:54 -05:00
|
|
|
|
/// @TODO make a positive list of allowed characters
|
2020-02-11 17:14:26 -05:00
|
|
|
|
$hashtag = str_replace([' ', '+', '/', '.', '#', '@', "'", '"', '’', '`', '(', ')', '„', '“'], '', $keyword);
|
2019-12-30 17:00:08 -05:00
|
|
|
|
$hashtags .= "#[url=" . DI::baseUrl() . "/search?tag=" . $hashtag . "]" . $hashtag . "[/url] ";
|
2014-10-03 07:02:15 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-03 06:18:33 -04:00
|
|
|
|
|
2016-04-17 14:43:41 -04:00
|
|
|
|
return "\n".$text.$hashtags;
|
2014-04-04 04:52:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 16:44:39 -04:00
|
|
|
|
function query_page_info($url, $photo = "", $keywords = false, $keyword_denylist = "")
|
2018-07-30 21:02:34 -04:00
|
|
|
|
{
|
2016-11-23 19:11:22 -05:00
|
|
|
|
$data = ParseUrl::getSiteinfoCached($url, true);
|
2014-10-20 02:03:47 -04:00
|
|
|
|
|
2017-04-08 13:30:35 -04:00
|
|
|
|
if ($photo != "") {
|
2014-12-17 15:57:38 -05:00
|
|
|
|
$data["images"][0]["src"] = $photo;
|
2017-04-08 13:30:35 -04:00
|
|
|
|
}
|
2014-12-17 15:57:38 -05:00
|
|
|
|
|
2018-10-30 09:58:45 -04:00
|
|
|
|
Logger::log('fetch page info for ' . $url . ' ' . print_r($data, true), Logger::DEBUG);
|
2014-10-20 02:03:47 -04:00
|
|
|
|
|
2017-06-07 22:00:59 -04:00
|
|
|
|
if (!$keywords && isset($data["keywords"])) {
|
2014-10-20 02:03:47 -04:00
|
|
|
|
unset($data["keywords"]);
|
2017-04-08 13:30:35 -04:00
|
|
|
|
}
|
2014-10-20 02:03:47 -04:00
|
|
|
|
|
2020-06-06 16:44:39 -04:00
|
|
|
|
if (($keyword_denylist != "") && isset($data["keywords"])) {
|
|
|
|
|
$list = explode(", ", $keyword_denylist);
|
2018-07-30 21:02:34 -04:00
|
|
|
|
|
|
|
|
|
foreach ($list as $keyword) {
|
2014-10-29 19:24:23 -04:00
|
|
|
|
$keyword = trim($keyword);
|
2018-07-30 21:02:34 -04:00
|
|
|
|
|
2014-10-29 19:24:23 -04:00
|
|
|
|
$index = array_search($keyword, $data["keywords"]);
|
2017-04-08 13:30:35 -04:00
|
|
|
|
if ($index !== false) {
|
2014-10-29 19:24:23 -04:00
|
|
|
|
unset($data["keywords"][$index]);
|
2017-04-08 13:30:35 -04:00
|
|
|
|
}
|
2014-10-29 19:24:23 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-14 10:59:51 -04:00
|
|
|
|
return $data;
|
2015-01-20 16:54:25 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 16:44:39 -04:00
|
|
|
|
function get_page_keywords($url, $photo = "", $keywords = false, $keyword_denylist = "")
|
2020-04-17 02:35:20 -04:00
|
|
|
|
{
|
2020-06-06 16:44:39 -04:00
|
|
|
|
$data = query_page_info($url, $photo, $keywords, $keyword_denylist);
|
2020-04-22 11:22:39 -04:00
|
|
|
|
if (empty($data["keywords"]) || !is_array($data["keywords"])) {
|
2020-04-17 02:35:20 -04:00
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$taglist = [];
|
|
|
|
|
foreach ($data['keywords'] as $keyword) {
|
|
|
|
|
$hashtag = str_replace([" ", "+", "/", ".", "#", "'"],
|
|
|
|
|
["", "", "", "", "", ""], $keyword);
|
|
|
|
|
|
|
|
|
|
$taglist[] = $hashtag;
|
2020-04-15 01:10:40 -04:00
|
|
|
|
}
|
2020-04-17 02:35:20 -04:00
|
|
|
|
|
|
|
|
|
return $taglist;
|
2015-01-20 16:54:25 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 16:44:39 -04:00
|
|
|
|
function add_page_info($url, $no_photos = false, $photo = "", $keywords = false, $keyword_denylist = "")
|
2018-07-30 21:02:34 -04:00
|
|
|
|
{
|
2020-06-06 16:44:39 -04:00
|
|
|
|
$data = query_page_info($url, $photo, $keywords, $keyword_denylist);
|
2015-01-20 16:54:25 -05:00
|
|
|
|
|
2018-07-30 22:24:32 -04:00
|
|
|
|
$text = '';
|
|
|
|
|
|
|
|
|
|
if (is_array($data)) {
|
|
|
|
|
$text = add_page_info_data($data, $no_photos);
|
|
|
|
|
}
|
2014-10-20 02:03:47 -04:00
|
|
|
|
|
2017-04-14 10:59:51 -04:00
|
|
|
|
return $text;
|
2014-10-20 02:03:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-30 21:02:34 -04:00
|
|
|
|
function add_page_info_to_body($body, $texturl = false, $no_photos = false)
|
|
|
|
|
{
|
2018-10-30 09:58:45 -04:00
|
|
|
|
Logger::log('add_page_info_to_body: fetch page info for body ' . $body, Logger::DEBUG);
|
2014-04-04 04:52:53 -04:00
|
|
|
|
|
2014-06-15 17:33:25 -04:00
|
|
|
|
$URLSearchString = "^\[\]";
|
2014-04-04 04:52:53 -04:00
|
|
|
|
|
2016-12-12 01:46:02 -05:00
|
|
|
|
// Fix for Mastodon where the mentions are in a different format
|
|
|
|
|
$body = preg_replace("/\[url\=([$URLSearchString]*)\]([#!@])(.*?)\[\/url\]/ism",
|
2017-04-14 10:59:51 -04:00
|
|
|
|
'$2[url=$1]$3[/url]', $body);
|
2016-12-12 01:46:02 -05:00
|
|
|
|
|
2014-06-15 17:33:25 -04:00
|
|
|
|
// Adding these spaces is a quick hack due to my problems with regular expressions :)
|
2017-04-14 10:59:51 -04:00
|
|
|
|
preg_match("/[^!#@]\[url\]([$URLSearchString]*)\[\/url\]/ism", " " . $body, $matches);
|
2014-04-04 04:52:53 -04:00
|
|
|
|
|
2017-04-14 10:59:51 -04:00
|
|
|
|
if (!$matches) {
|
|
|
|
|
preg_match("/[^!#@]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", " " . $body, $matches);
|
|
|
|
|
}
|
2014-04-04 04:52:53 -04:00
|
|
|
|
|
|
|
|
|
// Convert urls without bbcode elements
|
2017-06-07 22:00:59 -04:00
|
|
|
|
if (!$matches && $texturl) {
|
2014-04-04 04:52:53 -04:00
|
|
|
|
preg_match("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", " ".$body, $matches);
|
2014-01-05 10:10:02 -05:00
|
|
|
|
|
2014-04-04 04:52:53 -04:00
|
|
|
|
// Yeah, a hack. I really hate regular expressions :)
|
2017-04-14 10:59:51 -04:00
|
|
|
|
if ($matches) {
|
2014-06-15 17:33:25 -04:00
|
|
|
|
$matches[1] = $matches[2];
|
2017-04-14 10:59:51 -04:00
|
|
|
|
}
|
2014-01-05 10:10:02 -05:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-14 10:59:51 -04:00
|
|
|
|
if ($matches) {
|
2014-07-27 10:10:09 -04:00
|
|
|
|
$footer = add_page_info($matches[1], $no_photos);
|
2017-04-14 10:59:51 -04:00
|
|
|
|
}
|
2014-07-27 10:10:09 -04:00
|
|
|
|
|
|
|
|
|
// Remove the link from the body if the link is attached at the end of the post
|
2017-06-07 22:00:59 -04:00
|
|
|
|
if (isset($footer) && (trim($footer) != "") && (strpos($footer, $matches[1]))) {
|
2014-07-27 10:10:09 -04:00
|
|
|
|
$removedlink = trim(str_replace($matches[1], "", $body));
|
2017-06-07 22:00:59 -04:00
|
|
|
|
if (($removedlink == "") || strstr($body, $removedlink)) {
|
2014-07-27 10:10:09 -04:00
|
|
|
|
$body = $removedlink;
|
2017-04-14 10:59:51 -04:00
|
|
|
|
}
|
2014-07-27 10:10:09 -04:00
|
|
|
|
|
2018-12-27 19:23:01 -05:00
|
|
|
|
$removedlink = preg_replace("/\[url\=" . preg_quote($matches[1], '/') . "\](.*?)\[\/url\]/ism", '', $body);
|
2017-06-07 22:00:59 -04:00
|
|
|
|
if (($removedlink == "") || strstr($body, $removedlink)) {
|
2014-07-27 10:10:09 -04:00
|
|
|
|
$body = $removedlink;
|
2017-04-14 10:59:51 -04:00
|
|
|
|
}
|
2014-07-27 10:10:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the page information to the bottom
|
2017-06-07 22:00:59 -04:00
|
|
|
|
if (isset($footer) && (trim($footer) != "")) {
|
2014-07-27 10:10:09 -04:00
|
|
|
|
$body .= $footer;
|
2017-04-14 10:59:51 -04:00
|
|
|
|
}
|
2014-01-05 10:10:02 -05:00
|
|
|
|
|
2014-06-15 17:33:25 -04:00
|
|
|
|
return $body;
|
2014-01-05 10:10:02 -05:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-08 00:16:39 -05:00
|
|
|
|
/**
|
2020-06-17 04:54:44 -04:00
|
|
|
|
* @deprecated since 2020.06
|
|
|
|
|
* @see \Friendica\Protocol\Feed::consume
|
2010-10-22 00:48:22 -04:00
|
|
|
|
*/
|
2019-01-21 11:37:43 -05:00
|
|
|
|
function consume_feed($xml, array $importer, array $contact, &$hub)
|
2018-07-30 21:02:34 -04:00
|
|
|
|
{
|
2020-06-17 04:54:44 -04:00
|
|
|
|
\Friendica\Protocol\Feed::consume($xml, $importer, $contact, $hub);
|
2016-02-05 15:25:20 -05:00
|
|
|
|
}
|