|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00031 defined('MOODLE_INTERNAL') || die(); 00032 00033 class filter_emoticon extends moodle_text_filter { 00034 00041 protected static $globalconfig; 00042 00051 public function filter($text, array $options = array()) { 00052 00053 if (!isset($options['originalformat'])) { 00054 // if the format is not specified, we are probably called by {@see format_string()} 00055 // in that case, it would be dangerous to replace text with the image because it could 00056 // be stripped. therefore, we do nothing 00057 return $text; 00058 } 00059 if (in_array($options['originalformat'], explode(',', $this->get_global_config('formats')))) { 00060 $this->replace_emoticons($text); 00061 } 00062 return $text; 00063 } 00064 00066 // internal implementation starts here 00068 00079 protected function get_global_config($name=null) { 00080 $this->load_global_config(); 00081 if (is_null($name)) { 00082 return self::$globalconfig; 00083 00084 } elseif (array_key_exists($name, self::$globalconfig)) { 00085 return self::$globalconfig->{$name}; 00086 00087 } else { 00088 return null; 00089 } 00090 } 00091 00097 protected function load_global_config() { 00098 if (is_null(self::$globalconfig)) { 00099 self::$globalconfig = get_config('filter_emoticon'); 00100 } 00101 } 00102 00109 protected function replace_emoticons(&$text) { 00110 global $CFG, $OUTPUT, $PAGE; 00111 static $emoticontexts = array(); // internal cache used for replacing 00112 static $emoticonimgs = array(); // internal cache used for replacing 00113 00114 $lang = current_language(); 00115 $theme = $PAGE->theme->name; 00116 00117 if (!isset($emoticontexts[$lang][$theme]) or !isset($emoticonimgs[$lang][$theme])) { 00118 // prepare internal caches 00119 $manager = get_emoticon_manager(); 00120 $emoticons = $manager->get_emoticons(); 00121 $emoticontexts[$lang][$theme] = array(); 00122 $emoticonimgs[$lang][$theme] = array(); 00123 foreach ($emoticons as $emoticon) { 00124 $emoticontexts[$lang][$theme][] = $emoticon->text; 00125 $emoticonimgs[$lang][$theme][] = $OUTPUT->render($manager->prepare_renderable_emoticon($emoticon)); 00126 } 00127 unset($emoticons); 00128 } 00129 00130 if (empty($emoticontexts[$lang][$theme])) { // No emoticons defined, nothing to process here 00131 return; 00132 } 00133 00134 // detect all the <script> zones to take out 00135 $excludes = array(); 00136 preg_match_all('/<script language(.+?)<\/script>/is', $text, $listofexcludes); 00137 00138 // take out all the <script> zones from text 00139 foreach (array_unique($listofexcludes[0]) as $key => $value) { 00140 $excludes['<+'.$key.'+>'] = $value; 00141 } 00142 if ($excludes) { 00143 $text = str_replace($excludes, array_keys($excludes), $text); 00144 } 00145 00146 // this is the meat of the code - this is run every time 00147 $text = str_replace($emoticontexts[$lang][$theme], $emoticonimgs[$lang][$theme], $text); 00148 00149 // Recover all the <script> zones to text 00150 if ($excludes) { 00151 $text = str_replace(array_keys($excludes), $excludes, $text); 00152 } 00153 } 00154 }