Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / filter / emailprotect / filter.php
blob9e706c52e5ab24cbe0af1d41dd8957a78f781bd8
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
6 function emailprotect_filter($courseid, $text) {
9 if (!empty($CFG->formatstring)) {
10 return $text;
13 /// Do a quick check using stripos to avoid unnecessary work
14 if (strpos($text, '@') === false) {
15 return $text;
18 /// There might be an email in here somewhere so continue ...
19 $matches = array();
21 /// regular expression to define a standard email string.
22 $emailregex = '((?:[\w\.\-])+\@(?:(?:[a-zA-Z\d\-])+\.)+(?:[a-zA-Z\d]{2,4}))';
24 /// pattern to find a mailto link with the linked text.
25 $pattern = '|(<a\s+href\s*=\s*[\'"]?mailto:)'.$emailregex.'([\'"]?\s*>)'.'(.*)'.'(</a>)|iU';
26 $text = preg_replace_callback($pattern, 'alter_mailto', $text);
28 /// pattern to find any other email address in the text.
29 $pattern = '/(^|\s+|>)'.$emailregex.'($|\s+|\.\s+|\.$|<)/i';
30 $text = preg_replace_callback($pattern, 'alter_email', $text);
32 return $text;
36 function alter_email($matches) {
37 return $matches[1].obfuscate_text($matches[2]).$matches[3];
41 function alter_mailto($matches) {
42 return obfuscate_mailto($matches[2], $matches[4]);