Merge branch 'MDL-62370-master' of git://github.com/andrewnicols/moodle
[moodle.git] / filter / censor / filter.php
blob67d695ed8a75cf53b4ef1632f0a2904a9213f52d
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 * Censorship filtering
21 * This very simple example of a Text Filter will parse
22 * printed text, blacking out words perceived to be bad
24 * @package filter
25 * @subpackage censor
26 * @copyright 2004 onwards Martin Dougiamas {@link http://moodle.com}
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 defined('MOODLE_INTERNAL') || die();
32 //////////////////////////////////////////////////////////////
33 // Censorship filtering
35 // This very simple example of a Text Filter will parse
36 // printed text, blacking out words perceived to be bad
38 // The list of words is in the lang/xx/moodle.php
40 //////////////////////////////////////////////////////////////
42 class filter_censor extends moodle_text_filter {
43 private function _canseecensor() {
44 return is_siteadmin(); //TODO: add proper access control
47 function hash(){
48 $cap = "mod/filter:censor";
49 if (is_siteadmin()) { //TODO: add proper access control
50 $cap = "mod/filter:seecensor";
52 return $cap;
55 function filter($text, array $options = array()){
56 static $words;
57 global $CFG;
59 if (!isset($CFG->filter_censor_badwords)) {
60 set_config( 'filter_censor_badwords','' );
63 if (empty($words)) {
64 $words = array();
65 if (empty($CFG->filter_censor_badwords)) {
66 $badwords = explode(',',get_string('badwords', 'filter_censor'));
68 else {
69 $badwords = explode(',', $CFG->filter_censor_badwords);
71 foreach ($badwords as $badword) {
72 $badword = trim($badword);
73 if($this->_canseecensor()){
74 $words[] = new filterobject($badword, '<span class="censoredtexthighlight" title="'.$badword.'">', '</span>',
75 false, false, $badword);
76 } else {
77 $words[] = new filterobject($badword, '<span class="censoredtext" title="'.$badword.'">',
78 '</span>', false, false, str_pad('',strlen($badword),'*'));
82 return filter_phrases($text, $words);