Files
moodle/filter/emailprotect/filter.php
T
tjhunt ccc161f8f0 filters: MDL-7336 change weblib to use the new code
* Rename filter base class from filter_base to moodle_text_filter
* Remove unnecessary explicit constructors in moodle_text_filter
subclasses
* New filter_manager class, rather than static methods in filter_base
* Move some logic out of weblib, and into filter_manager
* Count filtering ops when $CFG->perfdebug on, via
performance_measuring_filter_manager
* Kill unused filter_string function. Petr said it should have been
private to weblib
2009-04-13 06:56:32 +00:00

40 lines
1.3 KiB
PHP

<?PHP // $Id$
// This class looks for email addresses in Moodle text and
// hides them using the Moodle obfuscate_text function.
// Original code by Mike Churchward
class emailprotect_filter extends moodle_text_filter {
function filter($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;
}
}
function alter_email($matches) {
return $matches[1].obfuscate_text($matches[2]).$matches[3];
}
function alter_mailto($matches) {
return obfuscate_mailto($matches[2], $matches[4]);
}
?>