From 6871481a7858495c204088e17beb5e7b4be2075a Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 28 Jan 2018 17:36:37 +0000 Subject: [PATCH] Moved "limitBodySize" --- src/Content/Text/BBCode.php | 87 +++++++++++++++++++++++++++++++++++++ src/Model/Item.php | 87 ------------------------------------- src/Protocol/DFRN.php | 3 +- src/Worker/OnePoll.php | 3 +- 4 files changed, 91 insertions(+), 89 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index ce6b252218..6feafdf854 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -549,4 +549,91 @@ class BBCode $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8'); return $s; } + + /** + * The purpose of this function is to apply system message length limits to + * imported messages without including any embedded photos in the length + * + * @brief Truncates imported message body string length to max_import_size + * @param string $body + * @return string + */ + public static function limitBodySize($body) + { + $maxlen = get_max_import_size(); + + // If the length of the body, including the embedded images, is smaller + // 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); + + $orig_body = $body; + $new_body = ''; + $textlen = 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; + $img_end += strlen('[/img]'); + + if (!strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) { + // This is an embedded image + + if (($textlen + $img_start) > $maxlen) { + if ($textlen < $maxlen) { + logger('the limit happens before an embedded image', LOGGER_DEBUG); + $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen); + $textlen = $maxlen; + } + } else { + $new_body = $new_body . substr($orig_body, 0, $img_start); + $textlen += $img_start; + } + + $new_body = $new_body . substr($orig_body, $img_start, $img_end - $img_start); + } else { + + if (($textlen + $img_end) > $maxlen) { + if ($textlen < $maxlen) { + logger('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; + } + } else { + $new_body = $new_body . substr($orig_body, 0, $img_end); + $textlen += $img_end; + } + } + $orig_body = substr($orig_body, $img_end); + + 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); + } + + if (($textlen + strlen($orig_body)) > $maxlen) { + if ($textlen < $maxlen) { + logger('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); + $new_body = $new_body . $orig_body; + } + + return $new_body; + } else { + return $body; + } + } } diff --git a/src/Model/Item.php b/src/Model/Item.php index ebdbbd41cf..98ffd89ff1 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -1067,93 +1067,6 @@ class Item } } - /** - * The purpose of this function is to apply system message length limits to - * imported messages without including any embedded photos in the length - * - * @brief Truncates imported message body string length to max_import_size - * @param string $body - * @return string - */ - public static function limitBodySize($body) - { - $maxlen = get_max_import_size(); - - // If the length of the body, including the embedded images, is smaller - // 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); - - $orig_body = $body; - $new_body = ''; - $textlen = 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; - $img_end += strlen('[/img]'); - - if (!strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) { - // This is an embedded image - - if (($textlen + $img_start) > $maxlen) { - if ($textlen < $maxlen) { - logger('the limit happens before an embedded image', LOGGER_DEBUG); - $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen); - $textlen = $maxlen; - } - } else { - $new_body = $new_body . substr($orig_body, 0, $img_start); - $textlen += $img_start; - } - - $new_body = $new_body . substr($orig_body, $img_start, $img_end - $img_start); - } else { - - if (($textlen + $img_end) > $maxlen) { - if ($textlen < $maxlen) { - logger('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; - } - } else { - $new_body = $new_body . substr($orig_body, 0, $img_end); - $textlen += $img_end; - } - } - $orig_body = substr($orig_body, $img_end); - - 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); - } - - if (($textlen + strlen($orig_body)) > $maxlen) { - if ($textlen < $maxlen) { - logger('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); - $new_body = $new_body . $orig_body; - } - - return $new_body; - } else { - return $body; - } - } - private static function setHashtags(&$item) { diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 22a7ecb611..6a83981371 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -28,6 +28,7 @@ use Friendica\Protocol\OStatus; use Friendica\Util\Crypto; use Friendica\Util\Network; use Friendica\Util\XML; +use Friendica\Content\Text\BBCode; use dba; use DOMDocument; @@ -2449,7 +2450,7 @@ class DFRN // make sure nobody is trying to sneak some html tags by us $item["body"] = notags(base64url_decode($item["body"])); - $item["body"] = Item::limitBodySize($item["body"]); + $item["body"] = BBCode::limitBodySize($item["body"]); /// @todo Do we really need this check for HTML elements? (It was copied from the old function) if ((strpos($item['body'], '<') !== false) && (strpos($item['body'], '>') !== false)) { diff --git a/src/Worker/OnePoll.php b/src/Worker/OnePoll.php index 29ab4e758f..c83c69f7ae 100644 --- a/src/Worker/OnePoll.php +++ b/src/Worker/OnePoll.php @@ -9,6 +9,7 @@ use Friendica\Core\PConfig; use Friendica\Database\DBM; use Friendica\Model\Contact; use Friendica\Model\Item; +use Friendica\Content\Text\BBCode; use Friendica\Protocol\Email; use Friendica\Protocol\PortableContact; use Friendica\Util\Network; @@ -488,7 +489,7 @@ class OnePoll continue; } $datarray['body'] = escape_tags($r['body']); - $datarray['body'] = Item::limitBodySize($datarray['body']); + $datarray['body'] = BBCode::limitBodySize($datarray['body']); logger("Mail: Importing ".$msg_uid." for ".$mailconf['user']);