From 07d2dfcd6015dfb0ba65b71cd8c304f83cb39dbb Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 10 Jul 2021 12:58:48 +0000 Subject: [PATCH] Some more "convertForUriId" replacements --- database.sql | 3 +- include/api.php | 16 ++-- include/conversation.php | 97 ----------------------- include/enotify.php | 2 +- mod/message.php | 7 +- src/Model/Profile.php | 8 +- src/Module/Api/Friendica/Events/Index.php | 2 +- src/Module/Profile/Profile.php | 4 +- src/Protocol/ActivityPub/Receiver.php | 3 +- src/Protocol/ActivityPub/Transmitter.php | 2 +- src/Protocol/DFRN.php | 11 +-- src/Protocol/Feed.php | 4 +- src/Protocol/OStatus.php | 4 +- static/dbstructure.config.php | 2 +- static/dbview.config.php | 1 + 15 files changed, 33 insertions(+), 133 deletions(-) diff --git a/database.sql b/database.sql index 5ee4ef053d..8f9fc8f0d5 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2021.09-dev (Siberian Iris) --- DB_UPDATE_VERSION 1426 +-- DB_UPDATE_VERSION 1427 -- ------------------------------------------ @@ -2291,6 +2291,7 @@ CREATE VIEW `owner-view` AS SELECT `contact`.`dfrn-id` AS `dfrn-id`, `contact`.`url` AS `url`, `contact`.`nurl` AS `nurl`, + `contact`.`uri-id` AS `uri-id`, `contact`.`addr` AS `addr`, `contact`.`alias` AS `alias`, `contact`.`pubkey` AS `pubkey`, diff --git a/include/api.php b/include/api.php index 25efa8f1b7..951c2b3e58 100644 --- a/include/api.php +++ b/include/api.php @@ -2525,12 +2525,12 @@ function api_format_messages($item, $recipient, $sender) if (!empty($_GET['getText'])) { $ret['title'] = $item['title']; if ($_GET['getText'] == 'html') { - $ret['text'] = BBCode::convert($item['body'], false); + $ret['text'] = BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::API); } elseif ($_GET['getText'] == 'plain') { - $ret['text'] = trim(HTML::toPlaintext(BBCode::convert(api_clean_plain_items($item['body']), false, BBCode::API, true), 0)); + $ret['text'] = trim(HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], api_clean_plain_items($item['body']), BBCode::API), 0)); } } else { - $ret['text'] = $item['title'] . "\n" . HTML::toPlaintext(BBCode::convert(api_clean_plain_items($item['body']), false, BBCode::API, true), 0); + $ret['text'] = $item['title'] . "\n" . HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], api_clean_plain_items($item['body']), BBCode::API), 0); } if (!empty($_GET['getUserObjects']) && $_GET['getUserObjects'] == 'false') { unset($ret['sender']); @@ -2557,7 +2557,7 @@ function api_convert_item($item) $attachments = api_get_attachments($body, $item['uri-id']); // Workaround for ostatus messages where the title is identically to the body - $html = BBCode::convert(api_clean_plain_items($body), false, BBCode::API, true); + $html = BBCode::convertForUriId($item['uri-id'], api_clean_plain_items($body), BBCode::API); $statusbody = trim(HTML::toPlaintext($html, 0)); // handle data: images @@ -2575,7 +2575,7 @@ function api_convert_item($item) $statustext = mb_substr($statustext, 0, 1000) . "... \n" . ($item['plink'] ?? ''); } - $statushtml = BBCode::convert(BBCode::removeAttachment($body), false, BBCode::API, true); + $statushtml = BBCode::convertForUriId($item['uri-id'], BBCode::removeAttachment($body), BBCode::API); // Workaround for clients with limited HTML parser functionality $search = ["
", "
", "
", @@ -2589,7 +2589,7 @@ function api_convert_item($item) $statushtml = str_replace($search, $replace, $statushtml); if ($item['title'] != "") { - $statushtml = "

" . BBCode::convert($item['title']) . "


" . $statushtml; + $statushtml = "

" . BBCode::convertForUriId($item['uri-id'], $item['title']) . "


" . $statushtml; } do { @@ -2607,7 +2607,7 @@ function api_convert_item($item) // feeds without body should contain the link if ((($item['network'] ?? Protocol::PHANTOM) == Protocol::FEED) && (strlen($item['body']) == 0)) { - $statushtml .= BBCode::convert($item['plink']); + $statushtml .= BBCode::convertForUriId($item['uri-id'], $item['plink']); } return [ @@ -3054,7 +3054,7 @@ function api_format_item($item, $type = "json", $status_user = null, $author_use 'external_url' => DI::baseUrl() . "/display/" . $item['guid'], 'friendica_activities' => api_format_items_activities($item, $type), 'friendica_title' => $item['title'], - 'friendica_html' => BBCode::convert($item['body'], false) + 'friendica_html' => BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::EXTERNAL) ]; if (count($converted["attachments"]) > 0) { diff --git a/include/conversation.php b/include/conversation.php index 4bd6ce6007..cd9e098990 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -47,91 +47,6 @@ use Friendica\Util\Strings; use Friendica\Util\Temporal; use Friendica\Util\XML; -function item_extract_images($body) { - - $saved_image = []; - $orig_body = $body; - $new_body = ''; - - $cnt = 0; - $img_start = strpos($orig_body, '[img'); - $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false); - $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false); - while (($img_st_close !== false) && ($img_end !== false)) { - - $img_st_close++; // make it point to AFTER the closing bracket - $img_end += $img_start; - - if (!strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) { - // This is an embedded image - - $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close)); - $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]'; - - $cnt++; - } else { - $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]')); - } - - $orig_body = substr($orig_body, $img_end + strlen('[/img]')); - - if ($orig_body === false) { - // in case the body ends on a closing image tag - $orig_body = ''; - } - - $img_start = strpos($orig_body, '[img'); - $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false); - $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false); - } - - $new_body = $new_body . $orig_body; - - return ['body' => $new_body, 'images' => $saved_image]; -} - -function item_redir_and_replace_images($body, $images, $cid) { - - $origbody = $body; - $newbody = ''; - - $cnt = 1; - $pos = BBCode::getTagPosition($origbody, 'url', 0); - while ($pos !== false && $cnt < 1000) { - - $search = '/\[url\=(.*?)\]\[!#saved_image([0-9]*)#!\]\[\/url\]' . '/is'; - $replace = '[url=' . DI::baseUrl() . '/redir/' . $cid - . '?url=' . '$1' . '][!#saved_image' . '$2' .'#!][/url]'; - - $newbody .= substr($origbody, 0, $pos['start']['open']); - $subject = substr($origbody, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']); - $origbody = substr($origbody, $pos['end']['close']); - if ($origbody === false) { - $origbody = ''; - } - - $subject = preg_replace($search, $replace, $subject); - $newbody .= $subject; - - $cnt++; - // Isn't this supposed to use $cnt value for $occurrences? - @MrPetovan - $pos = BBCode::getTagPosition($origbody, 'url', 0); - } - $newbody .= $origbody; - - $cnt = 0; - foreach ($images as $image) { - /* - * We're depending on the property of 'foreach' (specified on the PHP website) that - * it loops over the array starting from the first element and going sequentially - * to the last element. - */ - $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody); - $cnt++; - } - return $newbody; -} - /** * Render actions localized * @@ -141,11 +56,6 @@ function item_redir_and_replace_images($body, $images, $cid) { */ function localize_item(&$item) { - $extracted = item_extract_images($item['body']); - if ($extracted['images']) { - $item['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $item['contact-id']); - } - /// @todo The following functionality needs to be cleaned up. if (!empty($item['verb'])) { $activity = DI::activity(); @@ -260,13 +170,6 @@ function localize_item(&$item) } } - // add zrl's to public images - $photo_pattern = "/\[url=(.*?)\/photos\/(.*?)\/image\/(.*?)\]\[img(.*?)\]h(.*?)\[\/img\]\[\/url\]/is"; - if (preg_match($photo_pattern, $item['body'])) { - $photo_replace = '[url=' . Profile::zrl('$1' . '/photos/' . '$2' . '/image/' . '$3' , true) . '][img' . '$4' . ']h' . '$5' . '[/img][/url]'; - $item['body'] = BBCode::pregReplaceInTag($photo_pattern, $photo_replace, 'url', $item['body']); - } - // add sparkle links to appropriate permalinks // Only create a redirection to a magic link when logged in if (!empty($item['plink']) && Session::isAuthenticated()) { diff --git a/include/enotify.php b/include/enotify.php index 409fc66fac..cfc03626f5 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -474,7 +474,7 @@ function notification($params) if ($show_in_notification_page) { $fields = [ 'name' => $params['source_name'] ?? '', - 'name_cache' => substr(strip_tags(BBCode::convert($params['source_name'])), 0, 255), + 'name_cache' => substr(strip_tags(BBCode::convertForUriId($uri_id, $params['source_name'])), 0, 255), 'url' => $params['source_link'] ?? '', 'photo' => $params['source_photo'] ?? '', 'link' => $itemlink ?? '', diff --git a/mod/message.php b/mod/message.php index 2705708ea6..ff4998992d 100644 --- a/mod/message.php +++ b/mod/message.php @@ -314,14 +314,9 @@ function message_content(App $a) $sparkle = ' sparkle'; } - $extracted = item_extract_images($message['body']); - if ($extracted['images']) { - $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']); - } - $from_name_e = $message['from-name']; $subject_e = $message['title']; - $body_e = BBCode::convert($message['body']); + $body_e = BBCode::convertForUriId($message['uri-id'], $message['body']); $to_name_e = $message['name']; $contact = Contact::getByURL($message['from-url'], false, ['thumb', 'addr', 'id', 'avatar']); diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 4a0db9ecd8..b3345eb63f 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -486,11 +486,11 @@ class Profile } if (isset($p['about'])) { - $p['about'] = BBCode::convert($p['about']); + $p['about'] = BBCode::convertForUriId($profile['uri-id'] ?? 0, $p['about']); } if (isset($p['address'])) { - $p['address'] = BBCode::convert($p['address']); + $p['address'] = BBCode::convertForUriId($profile['uri-id'] ?? 0, $p['address']); } $p['photo'] = Contact::getAvatarUrlForId($cid, ProxyUtils::SIZE_SMALL); @@ -670,13 +670,13 @@ class Profile $istoday = true; } - $title = strip_tags(html_entity_decode(BBCode::convert($rr['summary']), ENT_QUOTES, 'UTF-8')); + $title = strip_tags(html_entity_decode(BBCode::convertForUriId($rr['uri-id'], $rr['summary']), ENT_QUOTES, 'UTF-8')); if (strlen($title) > 35) { $title = substr($title, 0, 32) . '... '; } - $description = substr(strip_tags(BBCode::convert($rr['desc'])), 0, 32) . '... '; + $description = substr(strip_tags(BBCode::convertForUriId($rr['uri-id'], $rr['desc'])), 0, 32) . '... '; if (!$description) { $description = DI::l10n()->t('[No description]'); } diff --git a/src/Module/Api/Friendica/Events/Index.php b/src/Module/Api/Friendica/Events/Index.php index d53273ef66..febbdea48e 100644 --- a/src/Module/Api/Friendica/Events/Index.php +++ b/src/Module/Api/Friendica/Events/Index.php @@ -55,7 +55,7 @@ class Index extends BaseApi 'cid' => $event['cid'], 'uri' => $event['uri'], 'name' => $event['summary'], - 'desc' => BBCode::convert($event['desc']), + 'desc' => BBCode::convertForUriId($event['uri-id'], $event['desc']), 'startTime' => $event['start'], 'endTime' => $event['finish'], 'type' => $event['type'], diff --git a/src/Module/Profile/Profile.php b/src/Module/Profile/Profile.php index dfe537c8c0..96545deaf9 100644 --- a/src/Module/Profile/Profile.php +++ b/src/Module/Profile/Profile.php @@ -169,7 +169,7 @@ class Profile extends BaseProfile } if ($a->profile['about']) { - $basic_fields += self::buildField('about', DI::l10n()->t('Description:'), BBCode::convert($a->profile['about'])); + $basic_fields += self::buildField('about', DI::l10n()->t('Description:'), BBCode::convertForUriId($a->profile['uri-id'], $a->profile['about'])); } if ($a->profile['xmpp']) { @@ -218,7 +218,7 @@ class Profile extends BaseProfile $custom_fields += self::buildField( 'custom_' . $profile_field->order, $profile_field->label, - BBCode::convert($profile_field->value), + BBCode::convertForUriId($a->profile['uri-id'], $profile_field->value), 'aprofile custom' ); }; diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index d59d9c4af6..7669934a65 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -1345,8 +1345,7 @@ class Receiver // Some AP software allow formatted text in post location, so we run all the text converters we have to boil // down to HTML and then finally format to plaintext. $location = Markdown::convert($location); - $location = BBCode::convert($location); - $location = HTML::toPlaintext($location); + $location = BBCode::toPlaintext($location); } $object_data['sc:identifier'] = JsonLD::fetchElement($object, 'sc:identifier', '@value'); diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index cdf1538308..29fb1a11a7 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -345,7 +345,7 @@ class Transmitter } if (!empty($owner['about'])) { - $data['summary'] = BBCode::convert($owner['about'], false); + $data['summary'] = BBCode::convertForUriId($owner['uri-id'], $owner['about'], BBCode::EXTERNAL); } $data['url'] = $owner['url']; diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 4d27444987..faae900b33 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -765,12 +765,13 @@ class DFRN * @param DOMDocument $doc XML document * @param string $element Element name for the activity * @param string $activity activity value + * @param int $uriid Uri-Id of the post * * @return \DOMElement XML activity object * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @todo Find proper type-hints */ - private static function createActivity(DOMDocument $doc, $element, $activity) + private static function createActivity(DOMDocument $doc, $element, $activity, $uriid) { if ($activity) { $entry = $doc->createElement($element); @@ -817,7 +818,7 @@ class DFRN } } if ($r->content) { - XML::addElement($doc, $entry, "content", BBCode::convert($r->content), ["type" => "html"]); + XML::addElement($doc, $entry, "content", BBCode::convertForUriId($uriid, $r->content, BBCode::EXTERNAL), ["type" => "html"]); } return $entry; @@ -918,7 +919,7 @@ class DFRN $htmlbody = "[b]" . $item['title'] . "[/b]\n\n" . $htmlbody; } - $htmlbody = BBCode::convertForUriId($item['uri-id'], $htmlbody, BBCode::OSTATUS); + $htmlbody = BBCode::convertForUriId($item['uri-id'], $htmlbody, BBCode::ACTIVITYPUB); } $author = self::addEntryAuthor($doc, "author", $item["author-link"], $item); @@ -1033,12 +1034,12 @@ class DFRN XML::addElement($doc, $entry, "activity:object-type", Activity\ObjectType::COMMENT); } - $actobj = self::createActivity($doc, "activity:object", $item['object']); + $actobj = self::createActivity($doc, "activity:object", $item['object'], $item['uri-id']); if ($actobj) { $entry->appendChild($actobj); } - $actarg = self::createActivity($doc, "activity:target", $item['target']); + $actarg = self::createActivity($doc, "activity:target", $item['target'], $item['uri-id']); if ($actarg) { $entry->appendChild($actarg); } diff --git a/src/Protocol/Feed.php b/src/Protocol/Feed.php index c088876d93..c155e9e3d2 100644 --- a/src/Protocol/Feed.php +++ b/src/Protocol/Feed.php @@ -1109,7 +1109,7 @@ class Feed $body = OStatus::formatPicturePost($item['body'], $item['uri-id']); - $body = BBCode::convertForUriId($item['uri-id'], $body, BBCode::OSTATUS, false); + $body = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB); XML::addElement($doc, $entry, "content", $body, ["type" => "html"]); @@ -1186,7 +1186,7 @@ class Feed private static function getTitle(array $item) { if ($item['title'] != '') { - return BBCode::convertForUriId($item['uri-id'], $item['title'], BBCode::OSTATUS); + return BBCode::convertForUriId($item['uri-id'], $item['title'], BBCode::ACTIVITYPUB); } // Fetch information about the post diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php index 03ff68d963..e813cc2507 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -1418,7 +1418,7 @@ class OStatus XML::addElement($doc, $author, "name", $owner["nick"]); XML::addElement($doc, $author, "email", $owner["addr"]); if ($show_profile) { - XML::addElement($doc, $author, "summary", BBCode::convert($owner["about"], false, BBCode::OSTATUS)); + XML::addElement($doc, $author, "summary", BBCode::convertForUriId($owner['uri-id'], $owner["about"], BBCode::OSTATUS)); } $attributes = ["rel" => "alternate", "type" => "text/html", "href" => $owner["url"]]; @@ -1445,7 +1445,7 @@ class OStatus XML::addElement($doc, $author, "poco:preferredUsername", $owner["nick"]); XML::addElement($doc, $author, "poco:displayName", $owner["name"]); if ($show_profile) { - XML::addElement($doc, $author, "poco:note", BBCode::convert($owner["about"], false, BBCode::OSTATUS)); + XML::addElement($doc, $author, "poco:note", BBCode::convertForUriId($owner['uri-id'], $owner["about"], BBCode::OSTATUS)); if (trim($owner["location"]) != "") { $element = $doc->createElement("poco:address"); diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index e47f0d5dc1..7eec39a984 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -55,7 +55,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1426); + define('DB_UPDATE_VERSION', 1427); } return [ diff --git a/static/dbview.config.php b/static/dbview.config.php index 01cda59a62..f4ff02a268 100644 --- a/static/dbview.config.php +++ b/static/dbview.config.php @@ -729,6 +729,7 @@ "dfrn-id" => ["contact", "dfrn-id"], "url" => ["contact", "url"], "nurl" => ["contact", "nurl"], + "uri-id" => ["contact", "uri-id"], "addr" => ["contact", "addr"], "alias" => ["contact", "alias"], "pubkey" => ["contact", "pubkey"],