New filter for Email protection ... filters all email addresses in Moodle
[moodle.git] / filter / emailprotect / filter.php
CommitLineData
e3f19cb0 1<?PHP // $Id$
2 // This function looks for email addresses in Moodle text and
3 // hides them using the Moodle obfuscate_text function.
4 // Original code by Mike Churchward
5
6function emailprotect_filter($courseid, $text) {
7
8/// Do a quick check using stripos to avoid unnecessary work
9 if (strpos($text, '@') === false) {
10 return $text;
11 }
12
13/// There might be an email in here somewhere so continue ...
14 $matches = array();
15
16/// regular expression to define a standard email string.
17 $emailregex = '((?:[\w\.\-])+\@(?:(?:[a-zA-Z\d\-])+\.)+(?:[a-zA-Z\d]{2,4}))';
18
19/// pattern to find a mailto link with the linked text.
20 $pattern = '|(<a\s+href\s*=\s*[\'"]?mailto:)'.$emailregex.'([\'"]?\s*>)'.'(.*)'.'(</a>)|iU';
21 $text = preg_replace_callback($pattern, 'alter_mailto', $text);
22
23/// pattern to find any other email address in the text.
24 $pattern = '/(^|\s+|>)'.$emailregex.'($|\s+|\.\s+|\.$|<)/i';
25 $text = preg_replace_callback($pattern, 'alter_email', $text);
26
27 return $text;
28}
29
30
31function alter_email($matches) {
32 return $matches[1].obfuscate_text($matches[2]).$matches[3];
33}
34
35
36function alter_mailto($matches) {
37 return obfuscate_mailto($matches[2], $matches[4]);
38}
39
40?>