X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FContent%2FText%2FBBCode.php;h=e304f47637eeaf1b0b2d9cb10a0734d35340685c;hb=5a3991d4f7bc929c1087d9275716fc1c8cc299a6;hp=f4872e7b37c1aadf6023474f8144604156b2b6d2;hpb=9239ecc5e0207eb5e62bea36f6ee200e4581932c;p=friendica.git diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index f4872e7b37..e304f47637 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -15,7 +15,9 @@ use Friendica\Core\Addon; use Friendica\Core\Cache; use Friendica\Core\Config; 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; @@ -25,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 { @@ -126,12 +130,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]); } @@ -149,12 +153,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]; } @@ -164,12 +168,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]; } @@ -182,12 +186,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]; } @@ -197,12 +201,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]; } @@ -230,7 +234,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. @@ -379,7 +383,7 @@ class BBCode extends BaseObject $c = preg_match_all('/\[img.*?\](.*?)\[\/img\]/ism', $s, $matches, PREG_SET_ORDER); if ($c) { foreach ($matches as $mtch) { - logger('scale_external_image: ' . $mtch[1]); + Logger::log('scale_external_image: ' . $mtch[1]); $hostname = str_replace('www.', '', substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3)); if (stristr($mtch[1], $hostname)) { @@ -414,7 +418,7 @@ class BBCode extends BaseObject $Image->scaleDown(640); $new_width = $Image->getWidth(); $new_height = $Image->getHeight(); - logger('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]' @@ -423,7 +427,7 @@ class BBCode extends BaseObject : ''), $s ); - logger('scale_external_images: new string: ' . $s, LOGGER_DEBUG); + Logger::log('scale_external_images: new string: ' . $s, Logger::DEBUG); } } } @@ -451,7 +455,7 @@ class BBCode extends BaseObject // than the maximum, then don't waste time looking for the images if ($maxlen && (strlen($body) > $maxlen)) { - logger('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 = ''; @@ -471,7 +475,7 @@ class BBCode extends BaseObject if (($textlen + $img_start) > $maxlen) { if ($textlen < $maxlen) { - logger('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; } @@ -485,7 +489,7 @@ class BBCode extends BaseObject if (($textlen + $img_end) > $maxlen) { if ($textlen < $maxlen) { - logger('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; } @@ -508,11 +512,11 @@ class BBCode extends BaseObject if (($textlen + strlen($orig_body)) > $maxlen) { if ($textlen < $maxlen) { - logger('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('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; } @@ -861,13 +865,14 @@ class BBCode extends BaseObject /** * This function converts a [share] block to text according to a provided callback function whose signature is: * - * function(array $attributes, array $author_contact, string $content): string + * function(array $attributes, array $author_contact, string $content, boolean $is_quote_share): string * * Where: * - $attributes is an array of attributes of the [share] block itself. Missing keys will be completed by the contact * data lookup * - $author_contact is a contact record array * - $content is the inner content of the [share] block + * - $is_quote_share indicates whether there's any content before the [share] block * - Return value is the string that should replace the [share] block in the provided text * * This function is intended to be used by addon connector to format a share block like the target network is expecting it. @@ -879,9 +884,9 @@ class BBCode extends BaseObject public static function convertShare($text, callable $callback) { $return = preg_replace_callback( - "/\[share(.*?)\](.*?)\[\/share\]/ism", + "/(.*?)\[share(.*?)\](.*?)\[\/share\]/ism", function ($match) use ($callback) { - $attribute_string = $match[1]; + $attribute_string = $match[2]; $attributes = []; foreach(['author', 'profile', 'avatar', 'link', 'posted'] as $field) { @@ -905,7 +910,7 @@ class BBCode extends BaseObject $attributes['avatar'] = ProxyUtils::proxifyUrl($attributes['avatar'], false, ProxyUtils::SIZE_THUMB); } - return $callback($attributes, $author_contact, $match[2]); + return $match[1] . $callback($attributes, $author_contact, $match[3], trim($match[1]) != ''); }, $text ); @@ -922,27 +927,28 @@ class BBCode extends BaseObject * @param array $attributes [share] block attribute values * @param array $author_contact Contact row of the shared author * @param string $content Inner content of the [share] block + * @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 */ - private static function convertShareCallback(array $attributes, array $author_contact, $content, $simplehtml) + private static function convertShareCallback(array $attributes, array $author_contact, $content, $is_quote_share, $simplehtml) { $mention = Protocol::formatMention($attributes['profile'], $attributes['author']); switch ($simplehtml) { case 1: - $text = '
' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' ' . $mention . ':
' . "\n" . '«' . $content . '»'; + $text = ($is_quote_share? '' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' ' . $mention . ':
' . "\n" . '«' . $content . '»'; break; case 2: - $text = '' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ':
' . "\n" . $content; + $text = ($is_quote_share? '' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ':
' . "\n" . $content; break; case 3: // Diaspora $headline = '' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . $mention . ':
' . "\n"; - if (stripos(normalise_link($attributes['link']), 'http://twitter.com/') === 0) { - $text = '' . "\n"; + if (stripos(Strings::normaliseLink($attributes['link']), 'http://twitter.com/') === 0) { + $text = ($is_quote_share? '' . trim($content) . '' . "\n"; + $text = ($is_quote_share? '
' . trim($content) . '' . "\n"; if ($attributes['link'] != '') { $text .= '' . "\n"; @@ -955,17 +961,17 @@ class BBCode extends BaseObject $headline .= L10n::t('%2$s %3$s', $attributes['link'], $mention, $attributes['posted']); $headline .= ':' . "\n"; - $text = $headline . '
' . trim($content) . '' . "\n"; + $text = ($is_quote_share? '
' . trim($content) . '' . "\n"; break; case 5: - $text = '
' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ':
' . "\n" . $content; + $text = ($is_quote_share? '' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ':
' . "\n" . $content; break; case 7: // statusnet/GNU Social - $text = '' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' @' . $author_contact['addr'] . ': ' . $content . '
' . "\n"; + $text = ($is_quote_share? '' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' @' . $author_contact['addr'] . ': ' . $content . '
' . "\n"; break; case 9: // Google+ - $text = '' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ':
' . "\n"; + $text = ($is_quote_share? '' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ':
' . "\n"; $text .= '' . $content . '
' . "\n"; if ($attributes['link'] != '') { @@ -974,15 +980,17 @@ 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 = OEmbed::getHTML($attributes['link']); + $text = ($is_quote_share? '