3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Filter converting URLs in the text to HTML links
22 * @subpackage urltolink
23 * @copyright 2010 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 class filter_urltolink extends moodle_text_filter {
32 * @var array global configuration for this filter
34 * This might be eventually moved into parent class if we found it
35 * useful for other filters, too.
37 protected static $globalconfig;
40 * Apply the filter to the text
42 * @see filter_manager::apply_filter_chain()
43 * @param string $text to be processed by the text
44 * @param array $options filter options
45 * @return string text after processing
47 public function filter($text, array $options = array()) {
48 if (!isset($options['originalformat'])) {
49 debugging('filter_urltolink requires originalformat option to be provided', DEBUG_DEVELOPER);
52 if (in_array($options['originalformat'], explode(',', $this->get_global_config('formats')))) {
53 $this->convert_urls_into_links($text);
58 ////////////////////////////////////////////////////////////////////////////
59 // internal implementation starts here
60 ////////////////////////////////////////////////////////////////////////////
63 * Returns the global filter setting
65 * If the $name is provided, returns single value. Otherwise returns all
66 * global settings in object. Returns null if the named setting is not
69 * @param mixed $name optional config variable name, defaults to null for all
70 * @return string|object|null
72 protected function get_global_config($name=null) {
73 $this->load_global_config();
75 return self::$globalconfig;
77 } elseif (array_key_exists($name, self::$globalconfig)) {
78 return self::$globalconfig->{$name};
86 * Makes sure that the global config is loaded in $this->globalconfig
90 protected function load_global_config() {
91 if (is_null(self::$globalconfig)) {
92 self::$globalconfig = get_config('filter_urltolink');
97 * Given some text this function converts any URLs it finds into HTML links
99 * @param string $text Passed in by reference. The string to be searched for urls.
101 protected function convert_urls_into_links(&$text) {
102 //I've added img tags to this list of tags to ignore.
103 //See MDL-21168 for more info. A better way to ignore tags whether or not
104 //they are escaped partially or completely would be desirable. For example:
106 //<a href="blah">
108 $filterignoretagsopen = array('<a\s[^>]+?>');
109 $filterignoretagsclose = array('</a>');
110 filter_save_ignore_tags($text,$filterignoretagsopen,$filterignoretagsclose,$ignoretags);
112 // Check if we support unicode modifiers in regular expressions. Cache it.
113 // TODO: this check should be a environment requirement in Moodle 2.0, as far as unicode
114 // chars are going to arrive to URLs officially really soon (2010?)
115 // Original RFC regex from: http://www.bytemycode.com/snippets/snippet/796/
116 // Various ideas from: http://alanstorm.com/url_regex_explained
117 // Unicode check, negative assertion and other bits from Moodle.
118 static $unicoderegexp;
119 if (!isset($unicoderegexp)) {
120 $unicoderegexp = @preg_match('/\pL/u', 'a'); // This will fail silently, returning false,
123 //todo: MDL-21296 - use of unicode modifiers may cause a timeout
124 if ($unicoderegexp) { //We can use unicode modifiers
125 $text = preg_replace('#(?<!=["\'])(((http(s?))://)(((([\pLl0-9]([\pLl0-9]|-)*[\pLl0-9]|[\pLl0-9])\.)+([\pLl]([\pLl0-9]|-)*[\pLl0-9]|[\pLl]))|(([0-9]{1,3}\.){3}[0-9]{1,3}))(:[\pL0-9]*)?(/([\pLl0-9\.!$&\'\(\)*+,;=_~:@-]|%[a-fA-F0-9]{2})*)*(\?([\pLl0-9\.!$&\'\(\)*+,;=_~:@/?-]|%[a-fA-F0-9]{2})*)?(\#[\pLl0-9\.!$&\'\(\)*+,;=_~:@/?-]*)?)(?<![,.;])#iu',
126 '<a href="\\1" class="_blanktarget">\\1</a>', $text);
127 $text = preg_replace('#(?<!=["\']|//)((www\.([\pLl0-9]([\pLl0-9]|-)*[\pLl0-9]|[\pLl0-9])\.)+([\pLl]([\pLl0-9]|-)*[\pLl0-9]|[\pLl])(:[\pL0-9]*)?(/([\pLl0-9\.!$&\'\(\)*+,;=_~:@-]|%[a-fA-F0-9]{2})*)*(\?([\pLl0-9\.!$&\'\(\)*+,;=_~:@/?-]|%[a-fA-F0-9]{2})*)?(\#[\pLl0-9\.!$&\'\(\)*+,;=_~:@/?-]*)?)(?<![,.;])#iu',
128 '<a href="http://\\1" class="_blanktarget">\\1</a>', $text);
129 } else { //We cannot use unicode modifiers
130 $text = preg_replace('#(?<!=["\'])(((http(s?))://)(((([a-z0-9]([a-z0-9]|-)*[a-z0-9]|[a-z0-9])\.)+([a-z]([a-z0-9]|-)*[a-z0-9]|[a-z]))|(([0-9]{1,3}\.){3}[0-9]{1,3}))(:[a-zA-Z0-9]*)?(/([a-z0-9\.!$&\'\(\)*+,;=_~:@-]|%[a-f0-9]{2})*)*(\?([a-z0-9\.!$&\'\(\)*+,;=_~:@/?-]|%[a-fA-F0-9]{2})*)?(\#[a-z0-9\.!$&\'\(\)*+,;=_~:@/?-]*)?)(?<![,.;])#i',
131 '<a href="\\1" class="_blanktarget">\\1</a>', $text);
132 $text = preg_replace('#(?<!=["\']|//)((www\.([a-z0-9]([a-z0-9]|-)*[a-z0-9]|[a-z0-9])\.)+([a-z]([a-z0-9]|-)*[a-z0-9]|[a-z])(:[a-zA-Z0-9]*)?(/([a-z0-9\.!$&\'\(\)*+,;=_~:@-]|%[a-f0-9]{2})*)*(\?([a-z0-9\.!$&\'\(\)*+,;=_~:@/?-]|%[a-fA-F0-9]{2})*)?(\#[a-z0-9\.!$&\'\(\)*+,;=_~:@/?-]*)?)(?<![,.;])#i',
133 '<a href="http://\\1" class="_blanktarget">\\1</a>', $text);
136 if (!empty($ignoretags)) {
137 $ignoretags = array_reverse($ignoretags); /// Reversed so "progressive" str_replace() will solve some nesting problems.
138 $text = str_replace(array_keys($ignoretags),$ignoretags,$text);