Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/filter/urltolink/filter.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00029 class filter_urltolink extends moodle_text_filter {
00030 
00037     protected static $globalconfig;
00038 
00047     public function filter($text, array $options = array()) {
00048         if (!isset($options['originalformat'])) {
00049             // if the format is not specified, we are probably called by {@see format_string()}
00050             // in that case, it would be dangerous to replace URL with the link because it could
00051             // be stripped. therefore, we do nothing
00052             return $text;
00053         }
00054         if (in_array($options['originalformat'], explode(',', $this->get_global_config('formats')))) {
00055             $this->convert_urls_into_links($text);
00056         }
00057         return $text;
00058     }
00059 
00061     // internal implementation starts here
00063 
00074     protected function get_global_config($name=null) {
00075         $this->load_global_config();
00076         if (is_null($name)) {
00077             return self::$globalconfig;
00078 
00079         } elseif (array_key_exists($name, self::$globalconfig)) {
00080             return self::$globalconfig->{$name};
00081 
00082         } else {
00083             return null;
00084         }
00085     }
00086 
00092     protected function load_global_config() {
00093         if (is_null(self::$globalconfig)) {
00094             self::$globalconfig = get_config('filter_urltolink');
00095         }
00096     }
00097 
00103     protected function convert_urls_into_links(&$text) {
00104         //I've added img tags to this list of tags to ignore.
00105         //See MDL-21168 for more info. A better way to ignore tags whether or not
00106         //they are escaped partially or completely would be desirable. For example:
00107         //<a href="blah">
00108         //&lt;a href="blah"&gt;
00109         //&lt;a href="blah">
00110         $filterignoretagsopen  = array('<a\s[^>]+?>');
00111         $filterignoretagsclose = array('</a>');
00112         filter_save_ignore_tags($text,$filterignoretagsopen,$filterignoretagsclose,$ignoretags);
00113 
00114         // Check if we support unicode modifiers in regular expressions. Cache it.
00115         // TODO: this check should be a environment requirement in Moodle 2.0, as far as unicode
00116         // chars are going to arrive to URLs officially really soon (2010?)
00117         // Original RFC regex from: http://www.bytemycode.com/snippets/snippet/796/
00118         // Various ideas from: http://alanstorm.com/url_regex_explained
00119         // Unicode check, negative assertion and other bits from Moodle.
00120         static $unicoderegexp;
00121         if (!isset($unicoderegexp)) {
00122             $unicoderegexp = @preg_match('/\pL/u', 'a'); // This will fail silently, returning false,
00123         }
00124 
00125         //todo: MDL-21296 - use of unicode modifiers may cause a timeout
00126         if ($unicoderegexp) { //We can use unicode modifiers
00127             $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',
00128                                  '<a href="\\1" class="_blanktarget">\\1</a>', $text);
00129             $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',
00130                                  '<a href="http://\\1" class="_blanktarget">\\1</a>', $text);
00131         } else { //We cannot use unicode modifiers
00132             $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',
00133                                  '<a href="\\1" class="_blanktarget">\\1</a>', $text);
00134             $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',
00135                                  '<a href="http://\\1" class="_blanktarget">\\1</a>', $text);
00136         }
00137 
00138         if (!empty($ignoretags)) {
00139             $ignoretags = array_reverse($ignoretags); 
00140             $text = str_replace(array_keys($ignoretags),$ignoretags,$text);
00141         }
00142 
00143         if ($this->get_global_config('embedimages')) {
00144             // now try to inject the images, this code was originally in the mediapluing filter
00145             // this may be useful only if somebody relies on the fact the links in FORMAT_MOODLE get converted
00146             // to URLs which in turn change to real images
00147             $search = '/<a href="([^"]+\.(jpg|png|gif))" class="_blanktarget">([^>]*)<\/a>/is';
00148             $text = preg_replace_callback($search, 'filter_urltolink_img_callback', $text);
00149         }
00150     }
00151 }
00152 
00153 
00162 function filter_urltolink_img_callback($link) {
00163     if ($link[1] !== $link[3]) {
00164         // this is not a link created by this filter, because the url does not match the text
00165         return $link[0];
00166     }
00167     return '<img class="filter_urltolink_image" alt="" src="'.$link[1].'" />';
00168 }
00169 
 All Data Structures Namespaces Files Functions Variables Enumerations