forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.php
150 lines (132 loc) · 5.78 KB
/
filter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Filter converting emoticon texts into images
*
* This filter uses the emoticon settings in Site admin > Appearance > HTML settings
* and replaces emoticon texts with images.
*
* @package filter
* @subpackage emoticon
* @see emoticon_manager
* @copyright 2010 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class filter_emoticon extends moodle_text_filter {
/**
* Internal cache used for replacing. Multidimensional array;
* - dimension 1: language,
* - dimension 2: theme.
* @var array
*/
protected static $emoticontexts = array();
/**
* Internal cache used for replacing. Multidimensional array;
* - dimension 1: language,
* - dimension 2: theme.
* @var array
*/
protected static $emoticonimgs = array();
/**
* Apply the filter to the text
*
* @see filter_manager::apply_filter_chain()
* @param string $text to be processed by the text
* @param array $options filter options
* @return string text after processing
*/
public function filter($text, array $options = array()) {
if (!isset($options['originalformat'])) {
// if the format is not specified, we are probably called by {@see format_string()}
// in that case, it would be dangerous to replace text with the image because it could
// be stripped. therefore, we do nothing
return $text;
}
if (in_array($options['originalformat'], explode(',', get_config('filter_emoticon', 'formats')))) {
return $this->replace_emoticons($text);
}
return $text;
}
////////////////////////////////////////////////////////////////////////////
// internal implementation starts here
////////////////////////////////////////////////////////////////////////////
/**
* Replace emoticons found in the text with their images
*
* @param string $text to modify
* @return string the modified result
*/
protected function replace_emoticons($text) {
global $CFG, $OUTPUT, $PAGE;
$lang = current_language();
$theme = $PAGE->theme->name;
if (!isset(self::$emoticontexts[$lang][$theme]) or !isset(self::$emoticonimgs[$lang][$theme])) {
// prepare internal caches
$manager = get_emoticon_manager();
$emoticons = $manager->get_emoticons();
self::$emoticontexts[$lang][$theme] = array();
self::$emoticonimgs[$lang][$theme] = array();
foreach ($emoticons as $emoticon) {
self::$emoticontexts[$lang][$theme][] = $emoticon->text;
self::$emoticonimgs[$lang][$theme][] = $OUTPUT->render($manager->prepare_renderable_emoticon($emoticon));
}
unset($emoticons);
}
if (empty(self::$emoticontexts[$lang][$theme])) { // No emoticons defined, nothing to process here.
return $text;
}
// Detect all zones that we should not handle (including the nested tags).
$processing = preg_split('/(<\/?(?:span|script)[^>]*>)/is', $text, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
// Initialize the results.
$resulthtml = "";
$exclude = 0;
// Define the patterns that mark the start of the forbidden zones.
$excludepattern = array('/^<script/is', '/^<span[^>]+class="nolink[^"]*"/is');
// Loop through the fragments.
foreach ($processing as $fragment) {
// If we are not ignoring, we MUST test if we should.
if ($exclude == 0) {
foreach ($excludepattern as $exp) {
if (preg_match($exp, $fragment)) {
$exclude = $exclude + 1;
break;
}
}
}
if ($exclude > 0) {
// If we are ignoring the fragment, then we must check if we may have reached the end of the zone.
if (strpos($fragment, '</span') !== false || strpos($fragment, '</script') !== false) {
$exclude -= 1;
// This is needed because of a double increment at the first element.
if ($exclude == 1) {
$exclude -= 1;
}
} else if (strpos($fragment, '<span') !== false || strpos($fragment, '<script') !== false) {
// If we find a nested tag we increase the exclusion level.
$exclude = $exclude + 1;
}
} else if (strpos($fragment, '<span') === false ||
strpos($fragment, '</span') === false) {
// This is the meat of the code - this is run every time.
// This code only runs for fragments that are not ignored (including the tags themselves).
$fragment = str_replace(self::$emoticontexts[$lang][$theme], self::$emoticonimgs[$lang][$theme], $fragment);
}
$resulthtml .= $fragment;
}
return $resulthtml;
}
}