X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FContent%2FText%2FBBCode.php;h=b710dc13ec3640e2c830325f57612ba4c1d9052d;hb=a505dbbe5ca3d28fe290d51b695abdf85fee7883;hp=9426677c29fc5c94356a2ff98e824fccfd10013c;hpb=14fde5dc9b1915392601fb94efc6224c01f2b216;p=friendica.git diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 9426677c29..b710dc13ec 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -11,12 +11,13 @@ use Exception; use Friendica\BaseObject; use Friendica\Content\OEmbed; use Friendica\Content\Smilies; -use Friendica\Core\Addon; use Friendica\Core\Cache; use Friendica\Core\Config; +use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Logger; use Friendica\Core\Protocol; +use Friendica\Core\Renderer; use Friendica\Core\System; use Friendica\Model\Contact; use Friendica\Model\Event; @@ -26,6 +27,8 @@ use Friendica\Util\Map; use Friendica\Util\Network; use Friendica\Util\ParseUrl; use Friendica\Util\Proxy as ProxyUtils; +use Friendica\Util\Strings; +use Friendica\Util\XML; class BBCode extends BaseObject { @@ -34,13 +37,14 @@ class BBCode extends BaseObject * * @param string $body Message body * @return array - * 'type' -> Message type ("link", "video", "photo") - * 'text' -> Text before the shared message - * 'after' -> Text after the shared message - * 'image' -> Preview image of the message - * 'url' -> Url to the attached message - * 'title' -> Title of the attachment - * 'description' -> Description of the attachment + * 'type' -> Message type ("link", "video", "photo") + * 'text' -> Text before the shared message + * 'after' -> Text after the shared message + * 'image' -> Preview image of the message + * 'url' -> Url to the attached message + * 'title' -> Title of the attachment + * 'description' -> Description of the attachment + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ private static function getOldAttachmentData($body) { @@ -105,13 +109,14 @@ class BBCode extends BaseObject * * @param string $body Message body * @return array - * 'type' -> Message type ("link", "video", "photo") - * 'text' -> Text before the shared message - * 'after' -> Text after the shared message - * 'image' -> Preview image of the message - * 'url' -> Url to the attached message - * 'title' -> Title of the attachment - * 'description' -> Description of the attachment + * 'type' -> Message type ("link", "video", "photo") + * 'text' -> Text before the shared message + * 'after' -> Text after the shared message + * 'image' -> Preview image of the message + * 'url' -> Url to the attached message + * 'title' -> Title of the attachment + * 'description' -> Description of the attachment + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ public static function getAttachmentData($body) { @@ -127,12 +132,12 @@ class BBCode extends BaseObject $type = ""; preg_match("/type='(.*?)'/ism", $attributes, $matches); - if (x($matches, 1)) { + if (!empty($matches[1])) { $type = strtolower($matches[1]); } preg_match('/type="(.*?)"/ism', $attributes, $matches); - if (x($matches, 1)) { + if (!empty($matches[1])) { $type = strtolower($matches[1]); } @@ -150,12 +155,12 @@ class BBCode extends BaseObject $url = ""; preg_match("/url='(.*?)'/ism", $attributes, $matches); - if (x($matches, 1)) { + if (!empty($matches[1])) { $url = $matches[1]; } preg_match('/url="(.*?)"/ism', $attributes, $matches); - if (x($matches, 1)) { + if (!empty($matches[1])) { $url = $matches[1]; } @@ -165,12 +170,12 @@ class BBCode extends BaseObject $title = ""; preg_match("/title='(.*?)'/ism", $attributes, $matches); - if (x($matches, 1)) { + if (!empty($matches[1])) { $title = $matches[1]; } preg_match('/title="(.*?)"/ism', $attributes, $matches); - if (x($matches, 1)) { + if (!empty($matches[1])) { $title = $matches[1]; } @@ -183,12 +188,12 @@ class BBCode extends BaseObject $image = ""; preg_match("/image='(.*?)'/ism", $attributes, $matches); - if (x($matches, 1)) { + if (!empty($matches[1])) { $image = $matches[1]; } preg_match('/image="(.*?)"/ism', $attributes, $matches); - if (x($matches, 1)) { + if (!empty($matches[1])) { $image = $matches[1]; } @@ -198,12 +203,12 @@ class BBCode extends BaseObject $preview = ""; preg_match("/preview='(.*?)'/ism", $attributes, $matches); - if (x($matches, 1)) { + if (!empty($matches[1])) { $preview = $matches[1]; } preg_match('/preview="(.*?)"/ism', $attributes, $matches); - if (x($matches, 1)) { + if (!empty($matches[1])) { $preview = $matches[1]; } @@ -231,7 +236,7 @@ class BBCode extends BaseObject */ $has_title = !empty($item['title']); - $plink = (!empty($item['plink']) ? $item['plink'] : ''); + $plink = defaults($item, 'plink', ''); $post = self::getAttachmentData($body); // if nothing is found, it maybe having an image. @@ -342,6 +347,7 @@ class BBCode extends BaseObject /** * @brief Converts a BBCode text into plaintext * + * @param $text * @param bool $keep_urls Whether to keep URLs in the resulting plaintext * * @return string @@ -415,7 +421,7 @@ class BBCode extends BaseObject $Image->scaleDown(640); $new_width = $Image->getWidth(); $new_height = $Image->getHeight(); - Logger::log('scale_external_images: ' . $orig_width . '->' . $new_width . 'w ' . $orig_height . '->' . $new_height . 'h' . ' match: ' . $mtch[0], LOGGER_DEBUG); + Logger::log('scale_external_images: ' . $orig_width . '->' . $new_width . 'w ' . $orig_height . '->' . $new_height . 'h' . ' match: ' . $mtch[0], Logger::DEBUG); $s = str_replace( $mtch[0], '[img=' . $new_width . 'x' . $new_height. ']' . $scaled . '[/img]' @@ -424,7 +430,7 @@ class BBCode extends BaseObject : ''), $s ); - Logger::log('scale_external_images: new string: ' . $s, LOGGER_DEBUG); + Logger::log('scale_external_images: new string: ' . $s, Logger::DEBUG); } } } @@ -443,6 +449,7 @@ class BBCode extends BaseObject * @brief Truncates imported message body string length to max_import_size * @param string $body * @return string + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ public static function limitBodySize($body) { @@ -452,7 +459,7 @@ class BBCode extends BaseObject // than the maximum, then don't waste time looking for the images if ($maxlen && (strlen($body) > $maxlen)) { - Logger::log('the total body length exceeds the limit', LOGGER_DEBUG); + Logger::log('the total body length exceeds the limit', Logger::DEBUG); $orig_body = $body; $new_body = ''; @@ -472,7 +479,7 @@ class BBCode extends BaseObject if (($textlen + $img_start) > $maxlen) { if ($textlen < $maxlen) { - Logger::log('the limit happens before an embedded image', LOGGER_DEBUG); + Logger::log('the limit happens before an embedded image', Logger::DEBUG); $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen); $textlen = $maxlen; } @@ -486,7 +493,7 @@ class BBCode extends BaseObject if (($textlen + $img_end) > $maxlen) { if ($textlen < $maxlen) { - Logger::log('the limit happens before the end of a non-embedded image', LOGGER_DEBUG); + Logger::log('the limit happens before the end of a non-embedded image', Logger::DEBUG); $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen); $textlen = $maxlen; } @@ -509,11 +516,11 @@ class BBCode extends BaseObject if (($textlen + strlen($orig_body)) > $maxlen) { if ($textlen < $maxlen) { - Logger::log('the limit happens after the end of the last image', LOGGER_DEBUG); + Logger::log('the limit happens after the end of the last image', Logger::DEBUG); $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen); } } else { - Logger::log('the text size with embedded images extracted did not violate the limit', LOGGER_DEBUG); + Logger::log('the text size with embedded images extracted did not violate the limit', Logger::DEBUG); $new_body = $new_body . $orig_body; } @@ -529,10 +536,11 @@ class BBCode extends BaseObject * Note: Can produce a [bookmark] tag in the returned string * * @brief Processes [attachment] tags - * @param string $return + * @param string $return * @param bool|int $simplehtml - * @param bool $tryoembed + * @param bool $tryoembed * @return string + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ private static function convertAttachment($return, $simplehtml = false, $tryoembed = true) { @@ -623,7 +631,7 @@ class BBCode extends BaseObject $data["title"] = $data["url"]; } - if (($data["text"] == "") && ($data["title"] != "") && ($data["url"] == "")) { + if (empty($data["text"]) && !empty($data["title"]) && empty($data["url"])) { return $data["title"] . $data["after"]; } @@ -774,10 +782,10 @@ class BBCode extends BaseObject /** * Performs a preg_replace within the boundaries of all named BBCode tags in a text * - * @param type $pattern Preg pattern string - * @param type $replace Preg replace string - * @param type $name BBCode tag name - * @param type $text Text to search + * @param string $pattern Preg pattern string + * @param string $replace Preg replace string + * @param string $name BBCode tag name + * @param string $text Text to search * @return string */ public static function pregReplaceInTag($pattern, $replace, $name, $text) @@ -927,6 +935,7 @@ class BBCode extends BaseObject * @param boolean $is_quote_share Whether there is content before the [share] block * @param integer $simplehtml Mysterious integer value depending on the target network/formatting style * @return string + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ private static function convertShareCallback(array $attributes, array $author_contact, $content, $is_quote_share, $simplehtml) { @@ -942,7 +951,7 @@ class BBCode extends BaseObject case 3: // Diaspora $headline = '

' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . $mention . ':

' . "\n"; - if (stripos(normalise_link($attributes['link']), 'http://twitter.com/') === 0) { + if (stripos(Strings::normaliseLink($attributes['link']), 'http://twitter.com/') === 0) { $text = ($is_quote_share? '
' : '') . '

' . $attributes['link'] . '

' . "\n"; } else { $text = ($is_quote_share? '
' : '') . $headline . '
' . trim($content) . '
' . "\n"; @@ -977,7 +986,7 @@ class BBCode extends BaseObject break; default: // Transforms quoted tweets in rich attachments to avoid nested tweets - if (stripos(normalise_link($attributes['link']), 'http://twitter.com/') === 0 && OEmbed::isAllowedURL($attributes['link'])) { + if (stripos(Strings::normaliseLink($attributes['link']), 'http://twitter.com/') === 0 && OEmbed::isAllowedURL($attributes['link'])) { try { $text = ($is_quote_share? '
' : '') . OEmbed::getHTML($attributes['link']); } catch (Exception $e) { @@ -986,8 +995,8 @@ class BBCode extends BaseObject } else { $text = ($is_quote_share? "\n" : ''); - $tpl = get_markup_template('shared_content.tpl'); - $text .= replace_macros($tpl, [ + $tpl = Renderer::getMarkupTemplate('shared_content.tpl'); + $text .= Renderer::replaceMacros($tpl, [ '$profile' => $attributes['profile'], '$avatar' => $attributes['avatar'], '$author' => $attributes['author'], @@ -1139,8 +1148,9 @@ class BBCode extends BaseObject * @param int $simple_html * @param bool $for_plaintext * @return string + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function convert($text, $try_oembed = true, $simple_html = false, $for_plaintext = false) + public static function convert($text, $try_oembed = true, $simple_html = 0, $for_plaintext = false) { $a = self::getApp(); @@ -1338,15 +1348,30 @@ class BBCode extends BaseObject $expression = "=diaspora://.*?/post/([0-9A-Za-z\-_@.:]{15,254}[0-9A-Za-z])=ism"; $text = preg_replace($expression, System::baseUrl()."/display/$1", $text); - $text = preg_replace("/([#])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", - '$1$3', $text); - - $text = preg_replace("/\[url\=([$URLSearchString]*)\]#(.*?)\[\/url\]/ism", - '#$2', $text); + /* Tag conversion + * Supports: + * - #[url=][/url] + * - [url=]#[/url] + */ + $text = preg_replace_callback("/(?:#\[url\=[$URLSearchString]*\]|\[url\=[$URLSearchString]*\]#)(.*?)\[\/url\]/ism", function($matches) { + return '#' + . XML::escape($matches[1]) + . ''; + }, $text); + + // We need no target="_blank" for local links + // convert links start with System::baseUrl() as local link + $escapedBaseUrl = str_replace('://', '\:\/\/', System::baseUrl()); + $text = preg_replace("/\[url\]($escapedBaseUrl)([$URLSearchString]*)\[\/url\]/ism", '$1$2', $text); + $text = preg_replace("/\[url\=($escapedBaseUrl)([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$3', $text); + // convert links that start with / as local link + $text = preg_replace("/\[url\](\/[$URLSearchString]*)\[\/url\]/ism", '$1', $text); + $text = preg_replace("/\[url\=(\/[$URLSearchString]*)\](.*?)\[\/url\]/ism", '$2', $text); $text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '$1', $text); $text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$2', $text); - //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '$2', $Text); // Red compatibility, though the link can't be authenticated on Friendica $text = preg_replace("/\[zrl\=([$URLSearchString]*)\](.*?)\[\/zrl\]/ism", '$2', $text); @@ -1362,7 +1387,7 @@ class BBCode extends BaseObject $text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.*?)\[\/mail\]/", '$2', $text); // leave open the posibility of [map=something] - // this is replaced in prepare_body() which has knowledge of the item location + // this is replaced in Item::prepareBody() which has knowledge of the item location if (strpos($text, '[/map]') !== false) { $text = preg_replace_callback( @@ -1473,7 +1498,7 @@ class BBCode extends BaseObject $text = str_replace('[hr]', '
', $text); - // This is actually executed in prepare_body() + // This is actually executed in Item::prepareBody() $text = str_replace('[nosmile]', '', $text); @@ -1653,7 +1678,7 @@ class BBCode extends BaseObject // Summary (e.g. title) is required, earlier revisions only required description (in addition to // start which is always required). Allow desc with a missing summary for compatibility. - if ((x($ev, 'desc') || x($ev, 'summary')) && x($ev, 'start')) { + if ((!empty($ev['desc']) || !empty($ev['summary'])) && !empty($ev['start'])) { $sub = Event::getHTML($ev, $simple_html); $text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/ism", '', $text); @@ -1667,7 +1692,7 @@ class BBCode extends BaseObject // Replace non graphical smilies for external posts if ($simple_html) { - $text = Smilies::replace($text, false, true); + $text = Smilies::replace($text); } // Unhide all [noparse] contained bbtags unspacefying them @@ -1747,7 +1772,7 @@ class BBCode extends BaseObject //$Text = str_replace('
  • ', '
  • ', $Text); //$Text = str_replace('