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 emoticon texts into images
21 * This filter uses the emoticon settings in Site admin > Appearance > HTML settings
22 * and replaces emoticon texts with images.
25 * @subpackage emoticon
26 * @see emoticon_manager
27 * @copyright 2010 David Mudrak <david@moodle.com>
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 defined('MOODLE_INTERNAL') ||
die();
33 class filter_emoticon
extends moodle_text_filter
{
36 * Internal cache used for replacing. Multidimensional array;
37 * - dimension 1: language,
38 * - dimension 2: theme.
41 protected static $emoticontexts = array();
44 * Internal cache used for replacing. Multidimensional array;
45 * - dimension 1: language,
46 * - dimension 2: theme.
49 protected static $emoticonimgs = array();
52 * Apply the filter to the text
54 * @see filter_manager::apply_filter_chain()
55 * @param string $text to be processed by the text
56 * @param array $options filter options
57 * @return string text after processing
59 public function filter($text, array $options = array()) {
61 if (!isset($options['originalformat'])) {
62 // if the format is not specified, we are probably called by {@see format_string()}
63 // in that case, it would be dangerous to replace text with the image because it could
64 // be stripped. therefore, we do nothing
67 if (in_array($options['originalformat'], explode(',', get_config('filter_emoticon', 'formats')))) {
68 return $this->replace_emoticons($text);
73 ////////////////////////////////////////////////////////////////////////////
74 // internal implementation starts here
75 ////////////////////////////////////////////////////////////////////////////
78 * Replace emoticons found in the text with their images
80 * @param string $text to modify
81 * @return string the modified result
83 protected function replace_emoticons($text) {
84 global $CFG, $OUTPUT, $PAGE;
86 $lang = current_language();
87 $theme = $PAGE->theme
->name
;
89 if (!isset(self
::$emoticontexts[$lang][$theme]) or !isset(self
::$emoticonimgs[$lang][$theme])) {
90 // prepare internal caches
91 $manager = get_emoticon_manager();
92 $emoticons = $manager->get_emoticons();
93 self
::$emoticontexts[$lang][$theme] = array();
94 self
::$emoticonimgs[$lang][$theme] = array();
95 foreach ($emoticons as $emoticon) {
96 self
::$emoticontexts[$lang][$theme][] = $emoticon->text
;
97 self
::$emoticonimgs[$lang][$theme][] = $OUTPUT->render($manager->prepare_renderable_emoticon($emoticon));
102 if (empty(self
::$emoticontexts[$lang][$theme])) { // No emoticons defined, nothing to process here.
106 // Detect all zones that we should not handle (including the nested tags).
107 $processing = preg_split('/(<\/?(?:span|script|pre)[^>]*>)/is', $text, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
109 // Initialize the results.
113 // Define the patterns that mark the start of the forbidden zones.
114 $excludepattern = array('/^<script/is', '/^<span[^>]+class="nolink[^"]*"/is', '/^<pre/is');
116 // Loop through the fragments.
117 foreach ($processing as $fragment) {
118 // If we are not ignoring, we MUST test if we should.
120 foreach ($excludepattern as $exp) {
121 if (preg_match($exp, $fragment)) {
122 $exclude = $exclude +
1;
128 // If we are ignoring the fragment, then we must check if we may have reached the end of the zone.
129 if (strpos($fragment, '</span') !== false ||
strpos($fragment, '</script') !== false
130 ||
strpos($fragment, '</pre') !== false) {
132 // This is needed because of a double increment at the first element.
136 } else if (strpos($fragment, '<span') !== false ||
strpos($fragment, '<script') !== false
137 ||
strpos($fragment, '<pre') !== false) {
138 // If we find a nested tag we increase the exclusion level.
139 $exclude = $exclude +
1;
141 } else if (strpos($fragment, '<span') === false ||
142 strpos($fragment, '</span') === false) {
143 // This is the meat of the code - this is run every time.
144 // This code only runs for fragments that are not ignored (including the tags themselves).
145 $fragment = str_replace(self
::$emoticontexts[$lang][$theme], self
::$emoticonimgs[$lang][$theme], $fragment);
147 $resulthtml .= $fragment;