Browse Source

Text class redux

biotorrents 4 years ago
parent
commit
3beb50fa32
1 changed files with 46 additions and 54 deletions
  1. 46
    54
      classes/text.class.php

+ 46
- 54
classes/text.class.php View File

@@ -14,8 +14,6 @@ class Text
14 14
       's' => 0,
15 15
       '*' => 0,
16 16
       '#' => 0,
17
-      #'ch' => 0,
18
-      #'uch' => 0,
19 17
       'artist' => 0,
20 18
       'user' => 0,
21 19
       'n' => 0,
@@ -128,7 +126,41 @@ class Text
128 126
      */
129 127
     public static $TOC = false;
130 128
 
129
+
130
+    /**
131
+     * Fix the links
132
+     * 
133
+     * Make it so that internal links are in the form "/section?p=foo"
134
+     * and that external links are secure and look like Wikipedia.
135
+     * Takes an already-parsed input, to hit Markdown and BBcode.
136
+     */
137
+    public function fix_links($Parsed) {
138
+            # Replace links to $ENV->SITE_DOMAIN
139
+            $Parsed = preg_replace(
140
+                "/<a href=\"$ENV->RESOURCE_REGEX$ENV->SITE_DOMAIN\//",
141
+                '<a href="/',
142
+                $Parsed
143
+            );
144
+                
145
+            # Replace external links and add Wikipedia-style CSS class
146
+            $RelTags = 'external nofollow noopener noreferrer';
147
+
148
+            $Parsed = preg_replace(
149
+                '/<a href="https?:\/\//',
150
+                '<a class="external" rel="'.$RelTags.'" target="_blank" href="https://',
151
+                $Parsed
152
+            );
153
+
154
+            $Parsed = preg_replace(
155
+                '/<a href="ftps?:\/\//',
156
+                '<a class="external" rel="'.$RelTags.'" target="_blank" href="ftps://',
157
+                $Parsed
158
+            );
159
+
160
+            return $Parsed;       
161
+    }
131 162
     
163
+
132 164
     /**
133 165
      * Output BBCode as XHTML
134 166
      *
@@ -160,24 +192,7 @@ class Text
160 192
             $Parsed = $Parsedown->text($Str);
161 193
             
162 194
             # Replace links to $ENV->SITE_DOMAIN
163
-            $Parsed = preg_replace(
164
-                "/<a href=\"$ENV->RESOURCE_REGEX$ENV->SITE_DOMAIN\//",
165
-                '<a href="/',
166
-                $Parsed
167
-            );
168
-                
169
-            # Replace external links and add Wikipedia-style class
170
-            $Parsed = preg_replace(
171
-                '/<a href="https?:\/\//',
172
-                '<a class="external" rel="noopener" rel="noreferrer" href="https://',
173
-                $Parsed
174
-            );
175
-
176
-            $Parsed = preg_replace(
177
-                '/<a href="ftps?:\/\//',
178
-                '<a class="external" rel="noopener" rel="noreferrer" href="ftps://',
179
-                $Parsed
180
-            );
195
+            $Parsed = self::fix_links($Parsed);
181 196
 
182 197
             return $Parsed;
183 198
 
@@ -208,37 +223,24 @@ class Text
208 223
                 : null)
209 224
             . $Parsedown->text($Str);
210 225
         */
211
-        } else {
226
+        }
227
+        
228
+        /**
229
+         * BBcode formatting
230
+         */
231
+        else {
212 232
             global $Debug;
213 233
             $Debug->set_flag('BBCode start');
214 234
 
215 235
             self::$Headlines = [];
216 236
             $Str = display_str($Str);
217 237
 
218
-            # Checkboxes: broken and stupid
219
-            /*
220
-            $Str = preg_replace('/\[\\[(ch|uch)]\]/i', '', $Str);
221
-            $Str = preg_replace('/\[ch\]/i', '[ch][/ch]', $Str);
222
-            $Str = preg_replace('/\[uch\]/i', '[uch][/uch]', $Str);
223
-            */
224
-
225 238
             // Inline links
226 239
             $URLPrefix = '(\[url\]|\[url\=|\[img\=|\[img\])';
227 240
             $Str = preg_replace('/'.$URLPrefix.'\s+/i', '$1', $Str);
228 241
             $Str = preg_replace('/(?<!'.$URLPrefix.')http(s)?:\/\//i', '$1[inlineurl]http$2://', $Str);
229 242
             $Str = preg_replace('/\[embed\]\[inlineurl\]/', '[embed]', $Str);
230 243
 
231
-            // For anonym.to and archive.org links, remove any [inlineurl] in the middle of the link
232
-            /*
233
-            $Str = preg_replace_callback(
234
-                '/(?<=\[inlineurl\]|'.$URLPrefix.')(\S*\[inlineurl\]\S*)/m',
235
-                function ($matches) {
236
-                    return str_replace("[inlineurl]", "", $matches[0]);
237
-                },
238
-                $Str
239
-            );
240
-            */
241
-
242 244
             if (self::$TOC) {
243 245
                 $Str = preg_replace('/(\={5})([^=].*)\1/i', '[headline=4]$2[/headline]', $Str);
244 246
                 $Str = preg_replace('/(\={4})([^=].*)\1/i', '[headline=3]$2[/headline]', $Str);
@@ -258,6 +260,9 @@ class Text
258 260
                 $HTML = self::parse_toc($Min) . $HTML;
259 261
             }
260 262
 
263
+            # Rewrite the URLs
264
+            $HTML = self::fix_links($HTML);
265
+
261 266
             $Debug->set_flag('BBCode end');
262 267
             return $HTML;
263 268
         }
@@ -896,20 +901,6 @@ class Text
896 901
                   break;
897 902
 
898 903
 
899
-                /*
900
-                case 'ch':
901
-                  $Str .= '<input type="checkbox" checked="checked" disabled="disabled">';
902
-                  break;
903
-                */
904
-
905
-
906
-                /*
907
-                case 'uch':
908
-                  $Str .= '<input type="checkbox" disabled="disabled">';
909
-                  break;
910
-                */
911
-
912
-
913 904
                 case 'list':
914 905
                   $Str .= "<$Block[ListType] class=\"postlist\">";
915 906
                   foreach ($Block['Val'] as $Line) {
@@ -1076,7 +1067,8 @@ class Text
1076 1067
                       if ($LocalURL) {
1077 1068
                           $Str .= '<a href="'.$LocalURL.'">'.substr($LocalURL, 1).'</a>';
1078 1069
                       } else {
1079
-                          $Str .= '<a rel="noreferrer" target="_blank" href="'.$Block['Attr'].'">'.$Block['Attr'].'</a>';
1070
+                          $Str .= '<a href="'.$Block['Attr'].'">'.$Block['Attr'].'</a>';
1071
+                          #$Str .= '<a rel="noreferrer" target="_blank" href="'.$Block['Attr'].'">'.$Block['Attr'].'</a>';
1080 1072
                       }
1081 1073
                   }
1082 1074
                   break;

Loading…
Cancel
Save