Ignore:
Timestamp:
May 2, 2016, 12:09:23 PM (3 years ago)
Author:
jrpelegrina
Message:

Updated to moodle 3.0.3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • moodle/trunk/fuentes/filter/urltolink/filter.php

    r136 r1331  
    5252            return $text;
    5353        }
    54         if (in_array($options['originalformat'], explode(',', $this->get_global_config('formats')))) {
     54        if (in_array($options['originalformat'], explode(',', get_config('filter_urltolink', 'formats')))) {
    5555            $this->convert_urls_into_links($text);
    5656        }
     
    6161    // internal implementation starts here
    6262    ////////////////////////////////////////////////////////////////////////////
    63 
    64     /**
    65      * Returns the global filter setting
    66      *
    67      * If the $name is provided, returns single value. Otherwise returns all
    68      * global settings in object. Returns null if the named setting is not
    69      * found.
    70      *
    71      * @param mixed $name optional config variable name, defaults to null for all
    72      * @return string|object|null
    73      */
    74     protected function get_global_config($name=null) {
    75         $this->load_global_config();
    76         if (is_null($name)) {
    77             return self::$globalconfig;
    78 
    79         } elseif (array_key_exists($name, self::$globalconfig)) {
    80             return self::$globalconfig->{$name};
    81 
    82         } else {
    83             return null;
    84         }
    85     }
    86 
    87     /**
    88      * Makes sure that the global config is loaded in $this->globalconfig
    89      *
    90      * @return void
    91      */
    92     protected function load_global_config() {
    93         if (is_null(self::$globalconfig)) {
    94             self::$globalconfig = get_config('filter_urltolink');
    95         }
    96     }
    9763
    9864    /**
     
    135101        // Lookbehind assertions.
    136102        // Is not HTML attribute or CSS URL property. Unfortunately legit text like "url(http://...)" will not be a link.
    137         $lookbehindstart = "(?<!=[\"']|\burl\([\"' ]|\burl\()";
    138103        $lookbehindend = "(?<![]),.;])";
    139104
    140         $regex = "$lookbehindstart$urlstart((?:$domainsegment\.)+$domainsegment|$numericip)" .
     105        $regex = "$urlstart((?:$domainsegment\.)+$domainsegment|$numericip)" .
    141106                "($port?$path$querystring?$fragment?)$lookbehindend";
    142107        if ($unicoderegexp) {
     
    146111        }
    147112
    148         $text = preg_replace($regex, '<a href="http$1://$2$3$4" class="_blanktarget">$0</a>', $text);
     113        // Locate any HTML tags.
     114        $matches = preg_split('/(<[^<|>]*>)/i', $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
     115
     116        // Iterate through the tokenized text to handle chunks (html and content).
     117        foreach ($matches as $idx => $chunk) {
     118            // Nothing to do. We skip completely any html chunk.
     119            if (strpos(trim($chunk), '<') === 0) {
     120                continue;
     121            }
     122
     123            // Nothing to do. We skip any content chunk having any of these attributes.
     124            if (preg_match('#(background=")|(action=")|(style="background)|(href=")|(src=")|(url [(])#', $chunk)) {
     125                continue;
     126            }
     127
     128            // Arrived here, we want to process every word in this chunk.
     129            $text = $chunk;
     130            $words = explode(' ', $text);
     131
     132            foreach ($words as $idx2 => $word) {
     133                // ReDoS protection. Stop processing if a word is too large.
     134                if (strlen($word) < 4096) {
     135                    $words[$idx2] = preg_replace($regex, '<a href="http$1://$2$3$4" class="_blanktarget">$0</a>', $word);
     136                }
     137            }
     138            $text = implode(' ', $words);
     139
     140            // Copy the result back to the array.
     141            $matches[$idx] = $text;
     142        }
     143
     144        $text = implode('', $matches);
    149145
    150146        if (!empty($ignoretags)) {
     
    153149        }
    154150
    155         if ($this->get_global_config('embedimages')) {
     151        if (get_config('filter_urltolink', 'embedimages')) {
    156152            // now try to inject the images, this code was originally in the mediapluing filter
    157153            // this may be useful only if somebody relies on the fact the links in FORMAT_MOODLE get converted
Note: See TracChangeset for help on using the changeset viewer.