Merge branch 'MDL-50805_master' of https://github.com/crazyserver/moodle
[moodle.git] / filter / emoticon / filter.php
blobd579bf65b9f04cbd2665cafb62f1d103831f9350
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
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.
24 * @package filter
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 {
35 /**
36 * Apply the filter to the text
38 * @see filter_manager::apply_filter_chain()
39 * @param string $text to be processed by the text
40 * @param array $options filter options
41 * @return string text after processing
43 public function filter($text, array $options = array()) {
45 if (!isset($options['originalformat'])) {
46 // if the format is not specified, we are probably called by {@see format_string()}
47 // in that case, it would be dangerous to replace text with the image because it could
48 // be stripped. therefore, we do nothing
49 return $text;
51 if (in_array($options['originalformat'], explode(',', get_config('filter_emoticon', 'formats')))) {
52 $this->replace_emoticons($text);
54 return $text;
57 ////////////////////////////////////////////////////////////////////////////
58 // internal implementation starts here
59 ////////////////////////////////////////////////////////////////////////////
61 /**
62 * Replace emoticons found in the text with their images
64 * @param string $text to modify
65 * @return void
67 protected function replace_emoticons(&$text) {
68 global $CFG, $OUTPUT, $PAGE;
69 static $emoticontexts = array(); // internal cache used for replacing
70 static $emoticonimgs = array(); // internal cache used for replacing
72 $lang = current_language();
73 $theme = $PAGE->theme->name;
75 if (!isset($emoticontexts[$lang][$theme]) or !isset($emoticonimgs[$lang][$theme])) {
76 // prepare internal caches
77 $manager = get_emoticon_manager();
78 $emoticons = $manager->get_emoticons();
79 $emoticontexts[$lang][$theme] = array();
80 $emoticonimgs[$lang][$theme] = array();
81 foreach ($emoticons as $emoticon) {
82 $emoticontexts[$lang][$theme][] = $emoticon->text;
83 $emoticonimgs[$lang][$theme][] = $OUTPUT->render($manager->prepare_renderable_emoticon($emoticon));
85 unset($emoticons);
88 if (empty($emoticontexts[$lang][$theme])) { // No emoticons defined, nothing to process here
89 return;
92 // detect all the <script> zones to take out
93 $excludes = array();
94 preg_match_all('/<script language(.+?)<\/script>/is', $text, $listofexcludes);
96 // take out all the <script> zones from text
97 foreach (array_unique($listofexcludes[0]) as $key => $value) {
98 $excludes['<+'.$key.'+>'] = $value;
100 if ($excludes) {
101 $text = str_replace($excludes, array_keys($excludes), $text);
104 // this is the meat of the code - this is run every time
105 $text = str_replace($emoticontexts[$lang][$theme], $emoticonimgs[$lang][$theme], $text);
107 // Recover all the <script> zones to text
108 if ($excludes) {
109 $text = str_replace(array_keys($excludes), $excludes, $text);