]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Text/BBCode.php
Merge pull request #8142 from nupplaphil/task/di_config
[friendica.git] / src / Content / Text / BBCode.php
index cdb48215c8bad12af225b82d2ceb71d9228d748a..f8b9fb29851d3df13abe2139a87c59d00cd43fc2 100644 (file)
@@ -10,9 +10,7 @@ use DOMXPath;
 use Exception;
 use Friendica\Content\OEmbed;
 use Friendica\Content\Smilies;
-use Friendica\Core\Config;
 use Friendica\Core\Hook;
-use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
@@ -487,7 +485,7 @@ class BBCode
 
        /**
         * Truncates imported message body string length to max_import_size
-        * 
+        *
         * The purpose of this function is to apply system message length limits to
         * imported messages without including any embedded photos in the length
         *
@@ -497,7 +495,7 @@ class BBCode
         */
        public static function limitBodySize($body)
        {
-               $maxlen = Config::get('config', 'max_import_size', 0);
+               $maxlen = DI::config()->get('config', 'max_import_size', 0);
 
                // If the length of the body, including the embedded images, is smaller
                // than the maximum, then don't waste time looking for the images
@@ -599,7 +597,7 @@ class BBCode
                        $data['title'] = null;
                }
 
-               if (((strpos($data['text'], "[img=") !== false) || (strpos($data['text'], "[img]") !== false) || Config::get('system', 'always_show_preview')) && !empty($data['image'])) {
+               if (((strpos($data['text'], "[img=") !== false) || (strpos($data['text'], "[img]") !== false) || DI::config()->get('system', 'always_show_preview')) && !empty($data['image'])) {
                        $data['preview'] = $data['image'];
                        $data['image'] = '';
                }
@@ -712,6 +710,7 @@ class BBCode
 
        /**
         * Converts [url] BBCodes in a format that looks fine on ActivityPub systems.
+        *
         * @param string $url URL that is about to be reformatted
         * @return string reformatted link including HTML codes
         */
@@ -723,6 +722,7 @@ class BBCode
 
        /**
         * Converts an URL in a nicer format (without the scheme and possibly shortened)
+        *
         * @param string $url URL that is about to be reformatted
         * @return string reformatted link
         */
@@ -905,7 +905,7 @@ class BBCode
                        // it loops over the array starting from the first element and going sequentially
                        // to the last element
                        $newbody = str_replace('[$#saved_image' . $cnt . '#$]',
-                               '<img src="' . self::proxyUrl($image) . '" alt="' . L10n::t('Image/photo') . '" />', $newbody);
+                               '<img src="' . self::proxyUrl($image) . '" alt="' . DI::l10n()->t('Image/photo') . '" />', $newbody);
                        $cnt++;
                }
 
@@ -1022,7 +1022,7 @@ class BBCode
                                break;
                        case 4:
                                $headline = '<p><b>' . html_entity_decode('&#x2672; ', ENT_QUOTES, 'UTF-8');
-                               $headline .= L10n::t('<a href="%1$s" target="_blank">%2$s</a> %3$s', $attributes['link'], $mention, $attributes['posted']);
+                               $headline .= DI::l10n()->t('<a href="%1$s" target="_blank">%2$s</a> %3$s', $attributes['link'], $mention, $attributes['posted']);
                                $headline .= ':</b></p>' . "\n";
 
                                $text = ($is_quote_share? '<hr />' : '') . $headline . '<blockquote class="shared_content">' . trim($content) . '</blockquote>' . "\n";
@@ -1344,7 +1344,7 @@ class BBCode
                $text = str_replace($search, $replace, $text);
 
                // removing multiplicated newlines
-               if (Config::get('system', 'remove_multiplicated_lines')) {
+               if (DI::config()->get('system', 'remove_multiplicated_lines')) {
                        $search = ["\n\n\n", "\n ", " \n", "[/quote]\n\n", "\n[/quote]", "[/li]\n", "\n[li]", "\n[ul]", "[/ul]\n", "\n\n[share ", "[/attachment]\n",
                                        "\n[h1]", "[/h1]\n", "\n[h2]", "[/h2]\n", "\n[h3]", "[/h3]\n", "\n[h4]", "[/h4]\n", "\n[h5]", "[/h5]\n", "\n[h6]", "[/h6]\n"];
                        $replace = ["\n\n", "\n", "\n", "[/quote]\n", "[/quote]", "[/li]", "[li]", "[ul]", "[/ul]", "\n[share ", "[/attachment]",
@@ -1519,7 +1519,7 @@ class BBCode
                $text = preg_replace("/\[font=(.*?)\](.*?)\[\/font\]/sm", "<span style=\"font-family: $1;\">$2</span>", $text);
 
                // Declare the format for [spoiler] layout
-               $SpoilerLayout = '<details class="spoiler"><summary>' . L10n::t('Click to open/close') . '</summary>$1</details>';
+               $SpoilerLayout = '<details class="spoiler"><summary>' . DI::l10n()->t('Click to open/close') . '</summary>$1</details>';
 
                // Check for [spoiler] text
                // handle nested quotes
@@ -1550,7 +1550,7 @@ class BBCode
 
                // Check for [quote=Author] text
 
-               $t_wrote = L10n::t('$1 wrote:');
+               $t_wrote = DI::l10n()->t('$1 wrote:');
 
                // handle nested quotes
                $endlessloop = 0;
@@ -1601,12 +1601,12 @@ class BBCode
                        $text
                );
 
-               $text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '<img src="$1" alt="' . L10n::t('Image/photo') . '" />', $text);
-               $text = preg_replace("/\[zmg\](.*?)\[\/zmg\]/ism", '<img src="$1" alt="' . L10n::t('Image/photo') . '" />', $text);
+               $text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '<img src="$1" alt="' . DI::l10n()->t('Image/photo') . '" />', $text);
+               $text = preg_replace("/\[zmg\](.*?)\[\/zmg\]/ism", '<img src="$1" alt="' . DI::l10n()->t('Image/photo') . '" />', $text);
 
-               $text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism", '<br/><img src="' .DI::baseUrl() . '/images/lock_icon.gif" alt="' . L10n::t('Encrypted content') . '" title="' . L10n::t('Encrypted content') . '" /><br />', $text);
-               $text = preg_replace("/\[crypt(.*?)\](.*?)\[\/crypt\]/ism", '<br/><img src="' .DI::baseUrl() . '/images/lock_icon.gif" alt="' . L10n::t('Encrypted content') . '" title="' . '$1' . ' ' . L10n::t('Encrypted content') . '" /><br />', $text);
-               //$Text = preg_replace("/\[crypt=(.*?)\](.*?)\[\/crypt\]/ism", '<br/><img src="' .DI::baseUrl() . '/images/lock_icon.gif" alt="' . L10n::t('Encrypted content') . '" title="' . '$1' . ' ' . L10n::t('Encrypted content') . '" /><br />', $Text);
+               $text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism", '<br/><img src="' .DI::baseUrl() . '/images/lock_icon.gif" alt="' . DI::l10n()->t('Encrypted content') . '" title="' . DI::l10n()->t('Encrypted content') . '" /><br />', $text);
+               $text = preg_replace("/\[crypt(.*?)\](.*?)\[\/crypt\]/ism", '<br/><img src="' .DI::baseUrl() . '/images/lock_icon.gif" alt="' . DI::l10n()->t('Encrypted content') . '" title="' . '$1' . ' ' . DI::l10n()->t('Encrypted content') . '" /><br />', $text);
+               //$Text = preg_replace("/\[crypt=(.*?)\](.*?)\[\/crypt\]/ism", '<br/><img src="' .DI::baseUrl() . '/images/lock_icon.gif" alt="' . DI::l10n()->t('Encrypted content') . '" title="' . '$1' . ' ' . DI::l10n()->t('Encrypted content') . '" /><br />', $Text);
 
                // Simplify "video" element
                $text = preg_replace('(\[video.*?\ssrc\s?=\s?([^\s\]]+).*?\].*?\[/video\])ism', '[video]$1[/video]', $text);
@@ -1830,11 +1830,11 @@ class BBCode
                array_walk($allowed_src_protocols, function(&$value) { $value = preg_quote($value, '#');});
 
                $text = preg_replace('#<([^>]*?)(src)="(?!' . implode('|', $allowed_src_protocols) . ')(.*?)"(.*?)>#ism',
-                                        '<$1$2=""$4 data-original-src="$3" class="invalid-src" title="' . L10n::t('Invalid source protocol') . '">', $text);
+                                        '<$1$2=""$4 data-original-src="$3" class="invalid-src" title="' . DI::l10n()->t('Invalid source protocol') . '">', $text);
 
                // sanitize href attributes (only whitelisted protocols URLs)
                // default value for backward compatibility
-               $allowed_link_protocols = Config::get('system', 'allowed_link_protocols', []);
+               $allowed_link_protocols = DI::config()->get('system', 'allowed_link_protocols', []);
 
                // Always allowed protocol even if config isn't set or not including it
                $allowed_link_protocols[] = '//';
@@ -1845,7 +1845,7 @@ class BBCode
                array_walk($allowed_link_protocols, function(&$value) { $value = preg_quote($value, '#');});
 
                $regex = '#<([^>]*?)(href)="(?!' . implode('|', $allowed_link_protocols) . ')(.*?)"(.*?)>#ism';
-               $text = preg_replace($regex, '<$1$2="javascript:void(0)"$4 data-original-href="$3" class="invalid-href" title="' . L10n::t('Invalid link protocol') . '">', $text);
+               $text = preg_replace($regex, '<$1$2="javascript:void(0)"$4 data-original-href="$3" class="invalid-href" title="' . DI::l10n()->t('Invalid link protocol') . '">', $text);
 
                // Shared content
                $text = self::convertShare(
@@ -2063,76 +2063,79 @@ class BBCode
        }
 
        /**
-     * Pull out all #hashtags and @person tags from $string.
-     *
-     * We also get @person@domain.com - which would make
-     * the regex quite complicated as tags can also
-     * end a sentence. So we'll run through our results
-     * and strip the period from any tags which end with one.
-     * Returns array of tags found, or empty array.
-     *
-     * @param string $string Post content
-     * 
-     * @return array List of tag and person names
-     */
-    public static function getTags($string)
-    {
-        $ret = [];
-
-        // Convert hashtag links to hashtags
-        $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2', $string);
-
-        // ignore anything in a code block
-        $string = preg_replace('/\[code.*?\].*?\[\/code\]/sm', '', $string);
-
-        // Force line feeds at bbtags
-        $string = str_replace(['[', ']'], ["\n[", "]\n"], $string);
-
-        // ignore anything in a bbtag
-        $string = preg_replace('/\[(.*?)\]/sm', '', $string);
-
-        // Match full names against @tags including the space between first and last
-        // We will look these up afterward to see if they are full names or not recognisable.
-
-        if (preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \x0D\x0A@,:?]+)([ \x0D\x0A@,:?]|$)/', $string, $matches)) {
-            foreach ($matches[1] as $match) {
-                if (strstr($match, ']')) {
-                    // we might be inside a bbcode color tag - leave it alone
-                    continue;
-                }
-
-                if (substr($match, -1, 1) === '.') {
-                    $ret[] = substr($match, 0, -1);
-                } else {
-                    $ret[] = $match;
-                }
-            }
-        }
-
-        // Otherwise pull out single word tags. These can be @nickname, @first_last
-        // and #hash tags.
-
-        if (preg_match_all('/([!#@][^\^ \x0D\x0A,;:?]+)([ \x0D\x0A,;:?]|$)/', $string, $matches)) {
-            foreach ($matches[1] as $match) {
-                if (strstr($match, ']')) {
-                    // we might be inside a bbcode color tag - leave it alone
-                    continue;
-                }
-                if (substr($match, -1, 1) === '.') {
-                    $match = substr($match,0,-1);
-                }
-                // ignore strictly numeric tags like #1
-                if ((strpos($match, '#') === 0) && ctype_digit(substr($match, 1))) {
-                    continue;
-                }
-                // try not to catch url fragments
-                if (strpos($string, $match) && preg_match('/[a-zA-z0-9\/]/', substr($string, strpos($string, $match) - 1, 1))) {
-                    continue;
-                }
-                $ret[] = $match;
-            }
-        }
-
-        return $ret;
-    }
+        * Pull out all #hashtags and @person tags from $string.
+        *
+        * We also get @person@domain.com - which would make
+        * the regex quite complicated as tags can also
+        * end a sentence. So we'll run through our results
+        * and strip the period from any tags which end with one.
+        * Returns array of tags found, or empty array.
+        *
+        * @param string $string Post content
+        *
+        * @return array List of tag and person names
+        */
+       public static function getTags($string)
+       {
+               $ret = [];
+
+               // Convert hashtag links to hashtags
+               $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2', $string);
+
+               // ignore anything in a code block
+               $string = preg_replace('/\[code.*?\].*?\[\/code\]/sm', '', $string);
+
+               // Force line feeds at bbtags
+               $string = str_replace(['[', ']'], ["\n[", "]\n"], $string);
+
+               // ignore anything in a bbtag
+               $string = preg_replace('/\[(.*?)\]/sm', '', $string);
+
+               // Match full names against @tags including the space between first and last
+               // We will look these up afterward to see if they are full names or not recognisable.
+
+               if (preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \x0D\x0A@,:?]+)([ \x0D\x0A@,:?]|$)/', $string, $matches)) {
+                       foreach ($matches[1] as $match) {
+                               if (strstr($match, ']')) {
+                                       // we might be inside a bbcode color tag - leave it alone
+                                       continue;
+                               }
+
+                               if (substr($match, -1, 1) === '.') {
+                                       $ret[] = substr($match, 0, -1);
+                               } else {
+                                       $ret[] = $match;
+                               }
+                       }
+               }
+
+               // Otherwise pull out single word tags. These can be @nickname, @first_last
+               // and #hash tags.
+
+               if (preg_match_all('/([!#@][^\^ \x0D\x0A,;:?]+)([ \x0D\x0A,;:?]|$)/', $string, $matches)) {
+                       foreach ($matches[1] as $match) {
+                               if (strstr($match, ']')) {
+                                       // we might be inside a bbcode color tag - leave it alone
+                                       continue;
+                               }
+
+                               if (substr($match, -1, 1) === '.') {
+                                       $match = substr($match,0,-1);
+                               }
+
+                               // ignore strictly numeric tags like #1
+                               if ((strpos($match, '#') === 0) && ctype_digit(substr($match, 1))) {
+                                       continue;
+                               }
+
+                               // try not to catch url fragments
+                               if (strpos($string, $match) && preg_match('/[a-zA-z0-9\/]/', substr($string, strpos($string, $match) - 1, 1))) {
+                                       continue;
+                               }
+                               $ret[] = $match;
+                       }
+               }
+
+               return $ret;
+       }
 }