Skip to content

Commit

Permalink
"FILTER/MDL-14582, filters 2.0, compatible with old filters"
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsheng committed Dec 19, 2008
1 parent 595a6f2 commit 9e3f34d
Show file tree
Hide file tree
Showing 10 changed files with 510 additions and 389 deletions.
55 changes: 30 additions & 25 deletions filter/activitynames/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,37 @@
//activities when its name (title) is found inside every Moodle text
//It's based in the glosssary filter by Williams Castillo
//Modifications by stronk7.
class activitynames_filter extends filter_base {
// Trivial-cache - keyed on $cachedcourseid
static $activitylist = null;
static $cachedcourseid;

function activitynames_filter($courseid, $text) {
global $CFG, $COURSE, $DB;
function __construct($courseid, $format, $options){
parent::__construct($courseid, $format, $options);
}

// Trivial-cache - keyed on $cachedcourseid
static $activitylist = null;
static $cachedcourseid;
function filter($text) {
global $CFG, $COURSE, $DB;

if (empty($courseid)) {
$courseid = SITEID;
if (empty($this->courseid)) {
$this->courseid = SITEID;
}

// Initialise/invalidate our trivial cache if dealing with a different course
if (!isset($cachedcourseid) || $cachedcourseid !== (int)$courseid) {
$activitylist = null;
if (!isset($this->cachedcourseid) || $this->cachedcourseid !== (int)$this->courseid) {
$this->activitylist = null;
}
$cachedcourseid = (int)$courseid;
$this->cachedcourseid = (int)$this->courseid;

/// It may be cached

if (is_null($activitylist)) {
$activitylist = array();
if (is_null($this->activitylist)) {
$this->activitylist = array();

if ($COURSE->id == $courseid) {
if ($COURSE->id == $this->courseid) {
$course = $COURSE;
} else {
$course = $DB->get_record("course", array("id"=>$courseid));
$course = $DB->get_record("course", array("id"=>$this->courseid));
}

if (!isset($course->modinfo)) {
Expand All @@ -41,7 +45,7 @@ function activitynames_filter($courseid, $text) {

if (!empty($modinfo)) {

$activitylist = array(); /// We will store all the activities here
$this->activitylist = array(); /// We will store all the activities here

//Sort modinfo by name length
usort($modinfo, 'comparemodulenamesbylength');
Expand All @@ -55,30 +59,31 @@ function activitynames_filter($courseid, $text) {
/// Avoid empty or unlinkable activity names
if (!empty($title)) {
$href_tag_begin = "<a class=\"autolink\" title=\"$title\" href=\"$CFG->wwwroot/mod/$activity->mod/view.php?id=$activity->cm\" $CFG->frametarget>";
$activitylist[] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
$this->activitylist[] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
if ($currentname != $entitisedname) { /// If name has some entity (&amp; &quot; &lt; &gt;) add that filter too. MDL-17545
$activitylist[] = new filterobject($entitisedname, $href_tag_begin, '</a>', false, true);
$this->activitylist[] = new filterobject($entitisedname, $href_tag_begin, '</a>', false, true);
}
}
}
}
}
}

if ($activitylist) {
return $text = filter_phrases ($text, $activitylist);
if ($this->activitylist) {
return $text = filter_phrases ($text, $this->activitylist);
} else {
return $text;
}
}
}



//This function is used to order module names from longer to shorter
function comparemodulenamesbylength($a, $b) {
if (strlen($a->name) == strlen($b->name)) {
return 0;
}
return (strlen($a->name) < strlen($b->name)) ? 1 : -1;
//This function is used to order module names from longer to shorter
function comparemodulenamesbylength($a, $b) {
if (strlen($a->name) == strlen($b->name)) {
return 0;
}
return (strlen($a->name) < strlen($b->name)) ? 1 : -1;
}
?>
250 changes: 127 additions & 123 deletions filter/algebra/filter.php

Large diffs are not rendered by default.

72 changes: 46 additions & 26 deletions filter/censor/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,57 @@
//
//////////////////////////////////////////////////////////////

/// This is the filtering function itself. It accepts the
/// courseid and the text to be filtered (in HTML form).

function censor_filter($courseid, $text) {

static $words;
global $CFG;

if (!isset($CFG->filter_censor_badwords)) {
set_config( 'filter_censor_badwords','' );
/// This is the filtering class. It accepts the courseid and
/// options to be filtered (In HTML form).
class censor_filter extends filter_base {
function __construct($courseid, $format, $options) {
parent::__construct($courseid, $format, $options);
}

if (empty($words)) {
$words = array();
// if no user-specified words, use default list from language pack
if (empty($CFG->filter_censor_badwords)) {
$badwords = explode(',',get_string('badwords','censor') );
}
else {
$badwords = explode(',', $CFG->filter_censor_badwords );
private function _canseecensor() {
$cansee = false;
$context = get_context_instance(CONTEXT_SYSTEM, SITEID);
if (has_capability('moodle/site:doanything', $context)) {
$cansee = true;
}
foreach ($badwords as $badword) {
$badword = trim($badword);
// See MDL-3964 for explanation of leaving the badword in the span title
$words[] = new filterobject($badword, '<span class="censoredtext" title="'.$badword.'">', '</span>',
false, false, str_pad('',strlen($badword),'*'));
return $cansee;
}
function hash(){
$cap = "mod/filter:censor";
$context = get_context_instance(CONTEXT_SYSTEM, SITEID);
if (has_capability('moodle/site:doanything', $context)) {
$cap = "mod/filter:seecensor";
}
return $cap;
}
function filter($text){
static $words;
global $CFG;

return filter_phrases($text, $words); // Look for all these words in the text
}
if (!isset($CFG->filter_censor_badwords)) {
set_config( 'filter_censor_badwords','' );
}

if (empty($words)) {
$words = array();
if (empty($CFG->filter_censor_badwords)) {
$badwords = explode(',',get_string('badwords','censor'));
}
else {
$badwords = explode(',', $CFG->filter_censor_badwords);
}
foreach ($badwords as $badword) {
$badword = trim($badword);
if($this->_canseecensor()){
$words[] = new filterobject($badword, '<span title"'.$badword.'">', '</span>',
false, false, $badword);
} else {
$words[] = new filterobject($badword, '<span class="censoredtext" title="'.$badword.'">',
'</span>', false, false, str_pad('',strlen($badword),'*'));
}
}
}
return filter_phrases($text, $words);
}
}

?>
55 changes: 28 additions & 27 deletions filter/emailprotect/filter.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
<?PHP // $Id$
// This function looks for email addresses in Moodle text and
// This class looks for email addresses in Moodle text and
// hides them using the Moodle obfuscate_text function.
// Original code by Mike Churchward

function emailprotect_filter($courseid, $text) {


if (!empty($CFG->formatstring)) {
return $text;
class emailprotect_filter extends filter_base {
function __construct($courseid, $format, $options) {
parent::__construct($courseid, $format, $options);
}

/// Do a quick check using stripos to avoid unnecessary work
if (strpos($text, '@') === false) {
function filter($text) {
if (!empty($CFG->formatstring)) {
return $text;
}

/// Do a quick check using stripos to avoid unnecessary work
if (strpos($text, '@') === false) {
return $text;
}

/// There might be an email in here somewhere so continue ...
$matches = array();

/// regular expression to define a standard email string.
$emailregex = '((?:[\w\.\-])+\@(?:(?:[a-zA-Z\d\-])+\.)+(?:[a-zA-Z\d]{2,4}))';

/// pattern to find a mailto link with the linked text.
$pattern = '|(<a\s+href\s*=\s*[\'"]?mailto:)'.$emailregex.'([\'"]?\s*>)'.'(.*)'.'(</a>)|iU';
$text = preg_replace_callback($pattern, 'alter_mailto', $text);

/// pattern to find any other email address in the text.
$pattern = '/(^|\s+|>)'.$emailregex.'($|\s+|\.\s+|\.$|<)/i';
$text = preg_replace_callback($pattern, 'alter_email', $text);

return $text;
}

/// There might be an email in here somewhere so continue ...
$matches = array();

/// regular expression to define a standard email string.
$emailregex = '((?:[\w\.\-])+\@(?:(?:[a-zA-Z\d\-])+\.)+(?:[a-zA-Z\d]{2,4}))';

/// pattern to find a mailto link with the linked text.
$pattern = '|(<a\s+href\s*=\s*[\'"]?mailto:)'.$emailregex.'([\'"]?\s*>)'.'(.*)'.'(</a>)|iU';
$text = preg_replace_callback($pattern, 'alter_mailto', $text);

/// pattern to find any other email address in the text.
$pattern = '/(^|\s+|>)'.$emailregex.'($|\s+|\.\s+|\.$|<)/i';
$text = preg_replace_callback($pattern, 'alter_email', $text);

return $text;
}


function alter_email($matches) {
return $matches[1].obfuscate_text($matches[2]).$matches[3];
}


function alter_mailto($matches) {
return obfuscate_mailto($matches[2], $matches[4]);
}
Expand Down
Loading

0 comments on commit 9e3f34d

Please sign in to comment.