]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Text/BBCode.php
Merge pull request #4350 from MrPetovan/task/4116-move-twitteroauth-to-composer
[friendica.git] / src / Content / Text / BBCode.php
index 7568cd716e53d0b8dbcd684afd5be6ed767035db..6feafdf854a51f1b4d42eb75b7d65d6a0fbdb331 100644 (file)
@@ -12,7 +12,7 @@ use Friendica\Core\PConfig;
 use Friendica\Core\System;
 use Friendica\Object\Image;
 use Friendica\Util\ParseUrl;
-use Friendica\Content\OEmbed;
+use Friendica\Util\Network;
 
 require_once "include/bbcode.php";
 require_once "include/html2plain.php";
@@ -511,8 +511,8 @@ class BBCode
                                } else {
                                        $scaled = $mtch[1];
                                }
-                               $i = OEmbed::fetchURL($scaled);
-                               if (! $i) {
+                               $i = Network::fetchUrl($scaled);
+                               if (!$i) {
                                        return $srctext;
                                }
 
@@ -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;
+               }
+       }
 }