]> 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 9162316d72444498f781fc4d535d7bce98527f45..6feafdf854a51f1b4d42eb75b7d65d6a0fbdb331 100644 (file)
@@ -6,13 +6,16 @@ namespace Friendica\Content\Text;
 
 use Friendica\App;
 use Friendica\Content\Text\Plaintext;
+use Friendica\Core\Config;
+use Friendica\Core\L10n;
 use Friendica\Core\PConfig;
+use Friendica\Core\System;
 use Friendica\Object\Image;
 use Friendica\Util\ParseUrl;
+use Friendica\Util\Network;
 
 require_once "include/bbcode.php";
 require_once "include/html2plain.php";
-require_once "include/network.php";
 
 class BBCode
 {
@@ -476,4 +479,161 @@ class BBCode
 
                return($post);
        }
+
+       public static function scaleExternalImages($srctext, $include_link = true, $scale_replace = false)
+       {
+               // Suppress "view full size"
+               if (intval(Config::get('system', 'no_view_full_size'))) {
+                       $include_link = false;
+               }
+
+               // Picture addresses can contain special characters
+               $s = htmlspecialchars_decode($srctext);
+
+               $matches = null;
+               $c = preg_match_all('/\[img.*?\](.*?)\[\/img\]/ism', $s, $matches, PREG_SET_ORDER);
+               if ($c) {
+                       foreach ($matches as $mtch) {
+                               logger('scale_external_image: ' . $mtch[1]);
+
+                               $hostname = str_replace('www.', '', substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3));
+                               if (stristr($mtch[1], $hostname)) {
+                                       continue;
+                               }
+
+                               // $scale_replace, if passed, is an array of two elements. The
+                               // first is the name of the full-size image. The second is the
+                               // name of a remote, scaled-down version of the full size image.
+                               // This allows Friendica to display the smaller remote image if
+                               // one exists, while still linking to the full-size image
+                               if ($scale_replace) {
+                                       $scaled = str_replace($scale_replace[0], $scale_replace[1], $mtch[1]);
+                               } else {
+                                       $scaled = $mtch[1];
+                               }
+                               $i = Network::fetchUrl($scaled);
+                               if (!$i) {
+                                       return $srctext;
+                               }
+
+                               // guess mimetype from headers or filename
+                               $type = Image::guessType($mtch[1], true);
+
+                               if ($i) {
+                                       $Image = new Image($i, $type);
+                                       if ($Image->isValid()) {
+                                               $orig_width = $Image->getWidth();
+                                               $orig_height = $Image->getHeight();
+
+                                               if ($orig_width > 640 || $orig_height > 640) {
+                                                       $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);
+                                                       $s = str_replace(
+                                                               $mtch[0],
+                                                               '[img=' . $new_width . 'x' . $new_height. ']' . $scaled . '[/img]'
+                                                               . "\n" . (($include_link)
+                                                                       ? '[url=' . $mtch[1] . ']' . L10n::t('view full size') . '[/url]' . "\n"
+                                                                       : ''),
+                                                               $s
+                                                       );
+                                                       logger('scale_external_images: new string: ' . $s, LOGGER_DEBUG);
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               // replace the special char encoding
+               $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;
+               }
+       }
 }