"FILTER/MDL-14582, filters 2.0, compatible with old filters"
authordongsheng <dongsheng>
Fri, 19 Dec 2008 02:16:00 +0000 (02:16 +0000)
committerdongsheng <dongsheng>
Fri, 19 Dec 2008 02:16:00 +0000 (02:16 +0000)
filter/activitynames/filter.php
filter/algebra/filter.php
filter/censor/filter.php
filter/emailprotect/filter.php
filter/mediaplugin/filter.php
filter/multilang/filter.php
filter/tex/filter.php
filter/tidy/filter.php
lib/filterlib.php
lib/weblib.php

index 7027a7d..89347b8 100644 (file)
@@ -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)) {
@@ -41,7 +45,7 @@
 
             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');
@@ -55,9 +59,9 @@
                         /// 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;
+}
 ?>
index d0121e4..1ec66b1 100644 (file)
@@ -84,16 +84,19 @@ function string_file_picture_algebra($imagefile, $tex= "", $height="", $width=""
   return $output;
 }
 
-
-function algebra_filter ($courseid, $text) {
-    global $CFG, $DB;
-
-    /// Do a quick check using stripos to avoid unnecessary wor
-    if (!preg_match('/<algebra/i',$text) && !strstr($text,'@@')) {
-        return $text;
+class algebra_filter extends filter_base {
+    function __construct($courseid, $format, $options){
+        parent::__construct($courseid, $format, $options);
     }
+    function filter($text){
+        global $CFG, $DB;
+
+        /// Do a quick check using stripos to avoid unnecessary wor
+        if (!preg_match('/<algebra/i',$text) && !strstr($text,'@@')) {
+            return $text;
+        }
 
-#    //restrict filtering to forum 130 (Maths Tools on moodle.org)
+//restrict filtering to forum 130 (Maths Tools on moodle.org)
 #    $scriptname = $_SERVER['SCRIPT_NAME'];
 #    if (!strstr($scriptname,'/forum/')) {
 #        return $text;
@@ -102,7 +105,7 @@ function algebra_filter ($courseid, $text) {
 #        $parent = forum_get_post_full($_GET['reply']);
 #        $discussion = $DB->get_record("forum_discussions",array("id"=>$parent->discussion));
 #    } else if (strstr($scriptname,'discuss.php')) {
-#        $discussion = $DB->get_record("forum_discussions", array("id"=>$_GET['d']));
+#        $discussion = $DB->get_record("forum_discussions",array("id"=>$_GET['d']));
 #    } else {
 #        return $text;
 #    }
@@ -111,125 +114,126 @@ function algebra_filter ($courseid, $text) {
 #    }
 
 
-    $text .= ' ';
+        $text .= ' ';
 
-    preg_match_all('/@(@@+)([^@])/',$text,$matches);
-    for ($i=0;$i<count($matches[0]);$i++) {
-        $replacement = str_replace('@','&#x00040;',$matches[1][$i]).$matches[2][$i];
-        $text = str_replace($matches[0][$i],$replacement,$text);
-    }
+        preg_match_all('/@(@@+)([^@])/',$text,$matches);
+        for ($i=0;$i<count($matches[0]);$i++) {
+            $replacement = str_replace('@','&#x00040;',$matches[1][$i]).$matches[2][$i];
+            $text = str_replace($matches[0][$i],$replacement,$text);
+        }
 
-    // <algebra> some algebraic input expression </algebra>
-    // or @@ some algebraic input expression @@
+        // <algebra> some algebraic input expression </algebra>
+        // or @@ some algebraic input expression @@
 
-    preg_match_all('/<algebra>(.+?)<\/algebra>|@@(.+?)@@/is', $text, $matches);
-    for ($i=0; $i<count($matches[0]); $i++) {
-        $algebra = $matches[1][$i] . $matches[2][$i];
-        $algebra = str_replace('<nolink>','',$algebra);
-        $algebra = str_replace('</nolink>','',$algebra);
-        $algebra = str_replace('<span class="nolink">','',$algebra);
-        $algebra = str_replace('</span>','',$algebra);
-        $align = "middle";
-        if (preg_match('/^align=bottom /',$algebra)) {
-          $align = "text-bottom";
-          $algebra = preg_replace('/^align=bottom /','',$algebra);
-        } else if (preg_match('/^align=top /',$algebra)) {
-          $align = "text-top";
-          $algebra = preg_replace('/^align=top /','',$algebra);
-        }
-        $md5 =  md5($algebra);
-        $filename =  $md5  . ".gif";
-        if (! $texcache = $DB->get_record("cache_filters", array("filter"=>"algebra", "md5key"=>$md5))) {
-           $algebra = str_replace('&lt;','<',$algebra);
-           $algebra = str_replace('&gt;','>',$algebra);
-           $algebra = str_replace('<>','#',$algebra);
-           $algebra = str_replace('<=','%',$algebra);
-           $algebra = str_replace('>=','!',$algebra);
-           $algebra = preg_replace('/([=><%!#] *)-/',"\$1 zeroplace -",$algebra);
-           $algebra = str_replace('delta','zdelta',$algebra);
-           $algebra = str_replace('beta','bita',$algebra);
-           $algebra = str_replace('theta','thita',$algebra);
-           $algebra = str_replace('zeta','zita',$algebra);
-           $algebra = str_replace('eta','xeta',$algebra);
-           $algebra = str_replace('epsilon','zepslon',$algebra);
-           $algebra = str_replace('upsilon','zupslon',$algebra);
-           $algebra = preg_replace('!\r\n?!',' ',$algebra);
-           $algebra = escapeshellarg($algebra);
-           if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) {
-              $cmd  = "cd $CFG->dirroot\\filter\\algebra & algebra2tex.pl $algebra";
-           } else {
-              $cmd  = "cd $CFG->dirroot/filter/algebra; ./algebra2tex.pl $algebra";
-           }
-           $texexp = `$cmd`;
-           if (preg_match('/parsehilight/',$texexp)) {
-             $text = str_replace( $matches[0][$i],"<b>Syntax error:</b> " . $texexp,$text);
-           } else if ($texexp) {
-              $texexp = str_replace('zeroplace','',$texexp);
-              $texexp = str_replace('#','\not= ',$texexp);
-              $texexp = str_replace('%','\leq ',$texexp);
-              $texexp = str_replace('!','\geq ',$texexp);
-              $texexp = str_replace('\left{','{',$texexp);
-              $texexp = str_replace('\right}','}',$texexp);
-              $texexp = str_replace('\fun',' ',$texexp);
-              $texexp = str_replace('infty','\infty',$texexp);
-              $texexp = str_replace('alpha','\alpha',$texexp);
-              $texexp = str_replace('gamma','\gamma',$texexp);
-              $texexp = str_replace('iota','\iota',$texexp);
-              $texexp = str_replace('kappa','\kappa',$texexp);
-              $texexp = str_replace('lambda','\lambda',$texexp);
-              $texexp = str_replace('mu','\mu',$texexp);
-              $texexp = str_replace('nu','\nu',$texexp);
-              $texexp = str_replace('xi','\xi',$texexp);
-              $texexp = str_replace('rho','\rho',$texexp);
-              $texexp = str_replace('sigma','\sigma',$texexp);
-              $texexp = str_replace('tau','\tau',$texexp);
-              $texexp = str_replace('phi','\phi',$texexp);
-              $texexp = str_replace('chi','\chi',$texexp);
-              $texexp = str_replace('psi','\psi',$texexp);
-              $texexp = str_replace('omega','\omega',$texexp);
-              $texexp = str_replace('zdelta','\delta',$texexp);
-              $texexp = str_replace('bita','\beta',$texexp);
-              $texexp = str_replace('thita','\theta',$texexp);
-              $texexp = str_replace('zita','\zeta',$texexp);
-              $texexp = str_replace('xeta','\eta',$texexp);
-              $texexp = str_replace('zepslon','\epsilon',$texexp);
-              $texexp = str_replace('zupslon','\upsilon',$texexp);
-              $texexp = str_replace('\mbox{logten}','\mbox{log}_{10}',$texexp);
-              $texexp = str_replace('\mbox{acos}','\mbox{cos}^{-1}',$texexp);
-              $texexp = str_replace('\mbox{asin}','\mbox{sin}^{-1}',$texexp);
-              $texexp = str_replace('\mbox{atan}','\mbox{tan}^{-1}',$texexp);
-              $texexp = str_replace('\mbox{asec}','\mbox{sec}^{-1}',$texexp);
-              $texexp = str_replace('\mbox{acsc}','\mbox{csc}^{-1}',$texexp);
-              $texexp = str_replace('\mbox{acot}','\mbox{cot}^{-1}',$texexp);
-              $texexp = str_replace('\mbox{acosh}','\mbox{cosh}^{-1}',$texexp);
-              $texexp = str_replace('\mbox{asinh}','\mbox{sinh}^{-1}',$texexp);
-              $texexp = str_replace('\mbox{atanh}','\mbox{tanh}^{-1}',$texexp);
-              $texexp = str_replace('\mbox{asech}','\mbox{sech}^{-1}',$texexp);
-              $texexp = str_replace('\mbox{acsch}','\mbox{csch}^{-1}',$texexp);
-              $texexp = str_replace('\mbox{acoth}','\mbox{coth}^{-1}',$texexp);
-              //$texexp = preg_replace('/\\\frac{(.+?)}{\\\left\((.+?)\\\right\)}/s','\frac{'."\$1}{\$2}",$texexp);
-              $texexp = preg_replace('/\\\sqrt{(.+?),(.+?)}/s','\sqrt['. "\$2]{\$1}",$texexp);
-              $texexp = preg_replace('/\\\mbox{abs}\\\left\((.+?)\\\right\)/s',"|\$1|",$texexp);
-              $texexp = preg_replace('/\\\log\\\left\((.+?),(.+?)\\\right\)/s','\log_{'. "\$2}\\left(\$1\\right)",$texexp);
-              $texexp = preg_replace('/(\\\cos|\\\sin|\\\tan|\\\sec|\\\csc|\\\cot)([h]*)\\\left\((.+?),(.+?)\\\right\)/s',"\$1\$2^{". "\$4}\\left(\$3\\right)",$texexp);
-              $texexp = preg_replace('/\\\int\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\int_'. "{\$2}^{\$3}\$1 ",$texexp);
-              $texexp = preg_replace('/\\\int\\\left\((.+?d[a-z])\\\right\)/s','\int '. "\$1 ",$texexp);
-              $texexp = preg_replace('/\\\lim\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\lim_'. "{\$2\\to \$3}\$1 ",$texexp);
-              $texcache->filter = 'algebra';
-              $texcache->version = 1;
-              $texcache->md5key = $md5;
-              $texcache->rawtext = $texexp;
-              $texcache->timemodified = time();
-              $DB->insert_record("cache_filters",$texcache, false);
-              $text = str_replace( $matches[0][$i], string_file_picture_algebra($filename, $texexp, '', '', $align), $text);
-           } else {
-              $text = str_replace( $matches[0][$i],"<b>Undetermined error:</b> ",$text);
-           }
-        } else {
-           $text = str_replace( $matches[0][$i], string_file_picture_algebra($filename, $texcache->rawtext), $text);
+        preg_match_all('/<algebra>(.+?)<\/algebra>|@@(.+?)@@/is', $text, $matches);
+        for ($i=0; $i<count($matches[0]); $i++) {
+            $algebra = $matches[1][$i] . $matches[2][$i];
+            $algebra = str_replace('<nolink>','',$algebra);
+            $algebra = str_replace('</nolink>','',$algebra);
+            $algebra = str_replace('<span class="nolink">','',$algebra);
+            $algebra = str_replace('</span>','',$algebra);
+            $align = "middle";
+            if (preg_match('/^align=bottom /',$algebra)) {
+              $align = "text-bottom";
+              $algebra = preg_replace('/^align=bottom /','',$algebra);
+            } else if (preg_match('/^align=top /',$algebra)) {
+              $align = "text-top";
+              $algebra = preg_replace('/^align=top /','',$algebra);
+            }
+            $md5 =  md5($algebra);
+            $filename =  $md5  . ".gif";
+            if (! $texcache = $DB->get_record("cache_filters",array("filter"=>"algebra", "md5key"=>$md5))) {
+               $algebra = str_replace('&lt;','<',$algebra);
+               $algebra = str_replace('&gt;','>',$algebra);
+               $algebra = str_replace('<>','#',$algebra);
+               $algebra = str_replace('<=','%',$algebra);
+               $algebra = str_replace('>=','!',$algebra);
+               $algebra = preg_replace('/([=><%!#] *)-/',"\$1 zeroplace -",$algebra);
+               $algebra = str_replace('delta','zdelta',$algebra);
+               $algebra = str_replace('beta','bita',$algebra);
+               $algebra = str_replace('theta','thita',$algebra);
+               $algebra = str_replace('zeta','zita',$algebra);
+               $algebra = str_replace('eta','xeta',$algebra);
+               $algebra = str_replace('epsilon','zepslon',$algebra);
+               $algebra = str_replace('upsilon','zupslon',$algebra);
+               $algebra = preg_replace('!\r\n?!',' ',$algebra);
+               $algebra = escapeshellarg($algebra);
+               if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) {
+                  $cmd  = "cd $CFG->dirroot\\filter\\algebra & algebra2tex.pl $algebra";
+               } else {
+                  $cmd  = "cd $CFG->dirroot/filter/algebra; ./algebra2tex.pl $algebra";
+               }
+               $texexp = `$cmd`;
+               if (preg_match('/parsehilight/',$texexp)) {
+                 $text = str_replace( $matches[0][$i],"<b>Syntax error:</b> " . $texexp,$text);
+               } else if ($texexp) {
+                  $texexp = str_replace('zeroplace','',$texexp);
+                  $texexp = str_replace('#','\not= ',$texexp);
+                  $texexp = str_replace('%','\leq ',$texexp);
+                  $texexp = str_replace('!','\geq ',$texexp);
+                  $texexp = str_replace('\left{','{',$texexp);
+                  $texexp = str_replace('\right}','}',$texexp);
+                  $texexp = str_replace('\fun',' ',$texexp);
+                  $texexp = str_replace('infty','\infty',$texexp);
+                  $texexp = str_replace('alpha','\alpha',$texexp);
+                  $texexp = str_replace('gamma','\gamma',$texexp);
+                  $texexp = str_replace('iota','\iota',$texexp);
+                  $texexp = str_replace('kappa','\kappa',$texexp);
+                  $texexp = str_replace('lambda','\lambda',$texexp);
+                  $texexp = str_replace('mu','\mu',$texexp);
+                  $texexp = str_replace('nu','\nu',$texexp);
+                  $texexp = str_replace('xi','\xi',$texexp);
+                  $texexp = str_replace('rho','\rho',$texexp);
+                  $texexp = str_replace('sigma','\sigma',$texexp);
+                  $texexp = str_replace('tau','\tau',$texexp);
+                  $texexp = str_replace('phi','\phi',$texexp);
+                  $texexp = str_replace('chi','\chi',$texexp);
+                  $texexp = str_replace('psi','\psi',$texexp);
+                  $texexp = str_replace('omega','\omega',$texexp);
+                  $texexp = str_replace('zdelta','\delta',$texexp);
+                  $texexp = str_replace('bita','\beta',$texexp);
+                  $texexp = str_replace('thita','\theta',$texexp);
+                  $texexp = str_replace('zita','\zeta',$texexp);
+                  $texexp = str_replace('xeta','\eta',$texexp);
+                  $texexp = str_replace('zepslon','\epsilon',$texexp);
+                  $texexp = str_replace('zupslon','\upsilon',$texexp);
+                  $texexp = str_replace('\mbox{logten}','\mbox{log}_{10}',$texexp);
+                  $texexp = str_replace('\mbox{acos}','\mbox{cos}^{-1}',$texexp);
+                  $texexp = str_replace('\mbox{asin}','\mbox{sin}^{-1}',$texexp);
+                  $texexp = str_replace('\mbox{atan}','\mbox{tan}^{-1}',$texexp);
+                  $texexp = str_replace('\mbox{asec}','\mbox{sec}^{-1}',$texexp);
+                  $texexp = str_replace('\mbox{acsc}','\mbox{csc}^{-1}',$texexp);
+                  $texexp = str_replace('\mbox{acot}','\mbox{cot}^{-1}',$texexp);
+                  $texexp = str_replace('\mbox{acosh}','\mbox{cosh}^{-1}',$texexp);
+                  $texexp = str_replace('\mbox{asinh}','\mbox{sinh}^{-1}',$texexp);
+                  $texexp = str_replace('\mbox{atanh}','\mbox{tanh}^{-1}',$texexp);
+                  $texexp = str_replace('\mbox{asech}','\mbox{sech}^{-1}',$texexp);
+                  $texexp = str_replace('\mbox{acsch}','\mbox{csch}^{-1}',$texexp);
+                  $texexp = str_replace('\mbox{acoth}','\mbox{coth}^{-1}',$texexp);
+                  //$texexp = preg_replace('/\\\frac{(.+?)}{\\\left\((.+?)\\\right\)}/s','\frac{'."\$1}{\$2}",$texexp);
+                  $texexp = preg_replace('/\\\sqrt{(.+?),(.+?)}/s','\sqrt['. "\$2]{\$1}",$texexp);
+                  $texexp = preg_replace('/\\\mbox{abs}\\\left\((.+?)\\\right\)/s',"|\$1|",$texexp);
+                  $texexp = preg_replace('/\\\log\\\left\((.+?),(.+?)\\\right\)/s','\log_{'. "\$2}\\left(\$1\\right)",$texexp);
+                  $texexp = preg_replace('/(\\\cos|\\\sin|\\\tan|\\\sec|\\\csc|\\\cot)([h]*)\\\left\((.+?),(.+?)\\\right\)/s',"\$1\$2^{". "\$4}\\left(\$3\\right)",$texexp);
+                  $texexp = preg_replace('/\\\int\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\int_'. "{\$2}^{\$3}\$1 ",$texexp);
+                  $texexp = preg_replace('/\\\int\\\left\((.+?d[a-z])\\\right\)/s','\int '. "\$1 ",$texexp);
+                  $texexp = preg_replace('/\\\lim\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\lim_'. "{\$2\\to \$3}\$1 ",$texexp);
+                  $texcache->filter = 'algebra';
+                  $texcache->version = 1;
+                  $texcache->md5key = $md5;
+                  $texcache->rawtext = $texexp;
+                  $texcache->timemodified = time();
+                  $DB->insert_record("cache_filters", $texcache, false);
+                  $text = str_replace( $matches[0][$i], string_file_picture_algebra($filename, $texexp, '', '', $align), $text);
+               } else {
+                  $text = str_replace( $matches[0][$i],"<b>Undetermined error:</b> ",$text);
+               }
+            } else {
+               $text = str_replace( $matches[0][$i], string_file_picture_algebra($filename, $texcache->rawtext), $text);
+            }
         }
+        return $text;
     }
-    return $text;
 }
 
 ?>
index 1c24422..db06e15 100644 (file)
@@ -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);
+    }
+}
 
 ?>
index 9e706c5..02b1f4e 100644 (file)
@@ -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]);
 }
index 29e4b15..161068c 100644 (file)
 
 require_once($CFG->libdir.'/filelib.php');
 
-
-function mediaplugin_filter($courseid, $text) {
-    global $CFG;
-    static $eolas_fix_applied = false;
-
-    // You should never modify parameters passed to a method or function, it's BAD practice. Create a copy instead.
-    // The reason is that you must always be able to refer to the original parameter that was passed.
-    // For this reason, I changed $text = preg_replace(..,..,$text) into $newtext = preg.... (NICOLAS CONNAULT)
-    // Thanks to Pablo Etcheverry for pointing this out! MDL-10177
-
-    // We're using the UFO technique for flash to attain XHTML Strict 1.0
-    // See: http://www.bobbyvandersluis.com/ufo/
-    if (!is_string($text)) {
-        // non string data can not be filtered anyway
-        return $text;
-    }
-    $newtext = $text; // fullclone is slow and not needed here
-
-    if (!empty($CFG->filter_mediaplugin_enable_mp3)) {
-        $search = '/<a.*?href="([^<]+\.mp3)"[^>]*>.*?<\/a>/is';
-        $newtext = preg_replace_callback($search, 'mediaplugin_filter_mp3_callback', $newtext);
-    }
-
-    if (!empty($CFG->filter_mediaplugin_enable_swf)) {
-        $search = '/<a.*?href="([^<]+\.swf)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is';
-        $newtext = preg_replace_callback($search, 'mediaplugin_filter_swf_callback', $newtext);
-    }
-
-    if (!empty($CFG->filter_mediaplugin_enable_flv)) {
-        $search = '/<a.*?href="([^<]+\.flv)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is';
-        $newtext = preg_replace_callback($search, 'mediaplugin_filter_flv_callback', $newtext);
-    }
-
-    if (!empty($CFG->filter_mediaplugin_enable_mov)) {
-        $search = '/<a.*?href="([^<]+\.mov)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is';
-        $newtext = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $newtext);
-    }
-
-    if (!empty($CFG->filter_mediaplugin_enable_wmv)) {
-        $search = '/<a.*?href="([^<]+\.wmv)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is';
-        $newtext = preg_replace_callback($search, 'mediaplugin_filter_wmp_callback', $newtext);
-    }
-
-    if (!empty($CFG->filter_mediaplugin_enable_mpg)) {
-        $search = '/<a.*?href="([^<]+\.mpe?g)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is';
-        $newtext = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $newtext);
+class mediaplugin_filter extends filter_base {
+    private $eolas_fix_applied;
+    function __construct($courseid, $format, $options) {
+        parent::__construct($courseid, $format, $options);
+        $this->eolas_fix_applied = false;
     }
-
-    if (!empty($CFG->filter_mediaplugin_enable_avi)) {
-        $search = '/<a.*?href="([^<]+\.avi)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is';
-        $newtext = preg_replace_callback($search, 'mediaplugin_filter_wmp_callback', $newtext);
-    }
-
-    if (!empty($CFG->filter_mediaplugin_enable_ram)) {
-        $search = '/<a.*?href="([^<]+\.ram)"[^>]*>.*?<\/a>/is';
-        $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
-    }
-
-    if (!empty($CFG->filter_mediaplugin_enable_rpm)) {
-        $search = '/<a.*?href="([^<]+\.rpm)"[^>]*>.*?<\/a>/is';
-        $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
-    }
-
-    if (!empty($CFG->filter_mediaplugin_enable_rm)) {
-        $search = '/<a.*?href="([^<]+\.rm)"[^>]*>.*?<\/a>/is';
-        $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
+    function filter($text) {
+        global $CFG;
+        // You should never modify parameters passed to a method or function, it's BAD practice. Create a copy instead.
+        // The reason is that you must always be able to refer to the original parameter that was passed.
+        // For this reason, I changed $text = preg_replace(..,..,$text) into $newtext = preg.... (NICOLAS CONNAULT)
+        // Thanks to Pablo Etcheverry for pointing this out! MDL-10177
+
+        // We're using the UFO technique for flash to attain XHTML Strict 1.0
+        // See: http://www.bobbyvandersluis.com/ufo/
+        if (!is_string($text)) {
+            // non string data can not be filtered anyway
+            return $text;
+        }
+        $newtext = $text; // fullclone is slow and not needed here
+
+        if ($CFG->filter_mediaplugin_enable_mp3) {
+            $search = '/<a.*?href="([^<]+\.mp3)"[^>]*>.*?<\/a>/is';
+            $newtext = preg_replace_callback($search, 'mediaplugin_filter_mp3_callback', $newtext);
+        }
+
+        if ($CFG->filter_mediaplugin_enable_swf) {
+            $search = '/<a.*?href="([^<]+\.swf)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
+            $newtext = preg_replace_callback($search, 'mediaplugin_filter_swf_callback', $newtext);
+        }
+
+        if ($CFG->filter_mediaplugin_enable_flv) {
+            $search = '/<a.*?href="([^<]+\.flv)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
+            $newtext = preg_replace_callback($search, 'mediaplugin_filter_flv_callback', $newtext);
+        }
+
+        if ($CFG->filter_mediaplugin_enable_mov) {
+            $search = '/<a.*?href="([^<]+\.mov)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
+            $newtext = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $newtext);
+        }
+
+        if ($CFG->filter_mediaplugin_enable_wmv) {
+            $search = '/<a.*?href="([^<]+\.wmv)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
+            $newtext = preg_replace_callback($search, 'mediaplugin_filter_wmp_callback', $newtext);
+        }
+
+        if ($CFG->filter_mediaplugin_enable_mpg) {
+            $search = '/<a.*?href="([^<]+\.mpe?g)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
+            $newtext = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $newtext);
+        }
+
+        if ($CFG->filter_mediaplugin_enable_avi) {
+            $search = '/<a.*?href="([^<]+\.avi)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
+            $newtext = preg_replace_callback($search, 'mediaplugin_filter_wmp_callback', $newtext);
+        }
+
+        if ($CFG->filter_mediaplugin_enable_ram) {
+            $search = '/<a.*?href="([^<]+\.ram)"[^>]*>.*?<\/a>/is';
+            $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
+        }
+
+        if ($CFG->filter_mediaplugin_enable_rpm) {
+            $search = '/<a.*?href="([^<]+\.rpm)"[^>]*>.*?<\/a>/is';
+            $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
+        }
+
+        if ($CFG->filter_mediaplugin_enable_rm) {
+            $search = '/<a.*?href="([^<]+\.rm)"[^>]*>.*?<\/a>/is';
+            $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
+        }
+
+        if (!empty($CFG->filter_mediaplugin_enable_youtube)) {
+            $search = '/<a.*?href="([^<]*)youtube.com\/watch\?v=([^"]*)"[^>]*>(.*?)<\/a>/is';
+            $newtext = preg_replace_callback($search, 'mediaplugin_filter_youtube_callback', $newtext);
+
+            $search = '/<a.*?href="([^<]*)youtube.com\/v\/([^"]*)"[^>]*>(.*?)<\/a>/is';
+            $newtext = preg_replace_callback($search, 'mediaplugin_filter_youtube_callback', $newtext);
+        }
+
+        if (empty($newtext) or $newtext === $text) {
+            // error or not filtered
+            unset($newtext);
+            return $text;
+        }
+
+        if (!$this->eolas_fix_applied) {
+            $newtext .= '<script defer="defer" src="' . $CFG->wwwroot . '/filter/mediaplugin/eolas_fix.js" type="text/javascript">// <![CDATA[ ]]></script>';
+            $this->eolas_fix_applied = true;
+        }
+
+        return $newtext;
     }
-
-    if (!empty($CFG->filter_mediaplugin_enable_youtube)) {
-        $search = '/<a.*?href="([^<]*)youtube.com\/watch\?v=([^"]*)"[^>]*>(.*?)<\/a>/is';
-        $newtext = preg_replace_callback($search, 'mediaplugin_filter_youtube_callback', $newtext);
-
-        $search = '/<a.*?href="([^<]*)youtube.com\/v\/([^"]*)"[^>]*>(.*?)<\/a>/is';
-        $newtext = preg_replace_callback($search, 'mediaplugin_filter_youtube_callback', $newtext);
-    }
-
-    if (is_null($newtext) or $newtext === $text) {
-        // error or not filtered
-        return $text;
-    }
-    
-    if (!$eolas_fix_applied) {
-        $newtext .= '<script defer="defer" src="' . $CFG->wwwroot . '/filter/mediaplugin/eolas_fix.js" type="text/javascript">// <![CDATA[ ]]></script>';
-        $eolas_fix_applied = true;
-    }
-
-    return $newtext;
 }
 
 ///===========================
index 7da88d6..2f5eb0c 100644 (file)
 // Following new syntax is not compatible with old one:
 //   <span lang="XX" class="multilang">one lang</span><span lang="YY" class="multilang">another language</span>
 
-function multilang_filter($courseid, $text) {
-    global $CFG;
+class multilang_filter extends filter_base {
+    function __construct($courseid, $format, $options) {
+        parent::__construct($courseid, $format, $options);
+    }
 
-    // [pj] I don't know about you but I find this new implementation funny :P
-    // [skodak] I was laughing while rewriting it ;-)
-    // [nicolasconnault] Should support inverted attributes: <span class="multilang" lang="en"> (Doesn't work curently)
-    // [skodak] it supports it now, though it is slower - any better idea? 
+    function filter($text) {
+        global $CFG;
 
-    if (empty($text) or is_numeric($text)) {
-        return $text;
-    }
+        // [pj] I don't know about you but I find this new implementation funny :P
+        // [skodak] I was laughing while rewriting it ;-)
+        // [nicolasconnault] Should support inverted attributes: <span class="multilang" lang="en"> (Doesn't work curently)
+        // [skodak] it supports it now, though it is slower - any better idea? 
 
-    if (empty($CFG->filter_multilang_force_old) and !empty($CFG->filter_multilang_converted)) {
-        // new syntax
-        $search = '/(<span(\s+lang="[a-zA-Z0-9_-]+"|\s+class="multilang"){2}\s*>.*?<\/span>)(\s*<span(\s+lang="[a-zA-Z0-9_-]+"|\s+class="multilang"){2}\s*>.*?<\/span>)+/is';
-    } else {
-        // old syntax
-        $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)+/is';
-    }
+        if (empty($text) or is_numeric($text)) {
+            return $text;
+        }
 
-    $result = preg_replace_callback($search, 'multilang_filter_impl', $text);
+        if (empty($CFG->filter_multilang_force_old) and !empty($CFG->filter_multilang_converted)) {
+            // new syntax
+            $search = '/(<span(\s+lang="[a-zA-Z0-9_-]+"|\s+class="multilang"){2}\s*>.*?<\/span>)(\s*<span(\s+lang="[a-zA-Z0-9_-]+"|\s+class="multilang"){2}\s*>.*?<\/span>)+/is';
+        } else {
+            // old syntax
+            $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)+/is';
+        }
 
-    if (is_null($result)) {
-        return $text; //error during regex processing (too many nested spans?)
-    } else {
-        return $result;
+        $result = preg_replace_callback($search, 'multilang_filter_impl', $text);
+
+        if (is_null($result)) {
+            return $text; //error during regex processing (too many nested spans?)
+        } else {
+            return $result;
+        }
     }
 }
 
index ba74ca3..e02e906 100644 (file)
@@ -91,13 +91,18 @@ function string_file_picture_tex($imagefile, $tex= "", $height="", $width="", $a
     return $output;
 }
 
-function tex_filter ($courseid, $text) {
-    global $CFG, $DB;
-
-    /// Do a quick check using stripos to avoid unnecessary work
-    if (!preg_match('/<tex/i',$text) and !strstr($text,'$$') and !strstr($text,'\\[') and !preg_match('/\[tex/i',$text)) { //added one more tag (dlnsk)
-        return $text;
+class tex_filter extends filter_base {
+    function __construct($courseid, $format, $options) {
+        parent::__construct($courseid, $format, $options);
     }
+    function filter ($text) {
+
+        global $CFG, $DB;
+
+        /// Do a quick check using stripos to avoid unnecessary work
+        if (!preg_match('/<tex/i',$text) and !strstr($text,'$$') and !strstr($text,'\\[') and !preg_match('/\[tex/i',$text)) { //added one more tag (dlnsk)
+            return $text;
+        }
 
 #    //restrict filtering to forum 130 (Maths Tools on moodle.org)
 #    $scriptname = $_SERVER['SCRIPT_NAME'];
@@ -115,48 +120,49 @@ function tex_filter ($courseid, $text) {
 #    if ($discussion->forum != 130) {
 #        return $text;
 #    }
-    $text .= ' ';
-    preg_match_all('/\$(\$\$+?)([^\$])/s',$text,$matches);
-    for ($i=0;$i<count($matches[0]);$i++) {
-        $replacement = str_replace('$','&#x00024;',$matches[1][$i]).$matches[2][$i];
-        $text = str_replace($matches[0][$i],$replacement,$text);
-    }
-
-    // <tex> TeX expression </tex>
-    // or <tex alt="My alternative text to be used instead of the TeX form"> TeX expression </tex>
-    // or $$ TeX expression $$
-    // or \[ TeX expression \]          // original tag of MathType and TeXaide (dlnsk)
-    // or [tex] TeX expression [/tex]   // somtime it's more comfortable than <tex> (dlnsk)
-    preg_match_all('/<tex(?:\s+alt=["\'](.*?)["\'])?>(.+?)<\/tex>|\$\$(.+?)\$\$|\\\\\[(.+?)\\\\\]|\\[tex\\](.+?)\\[\/tex\\]/is', $text, $matches);
-    for ($i=0; $i<count($matches[0]); $i++) {
-        $texexp = $matches[2][$i] . $matches[3][$i] . $matches[4][$i] . $matches[5][$i];
-        $alt = $matches[1][$i];
-        $texexp = str_replace('<nolink>','',$texexp);
-        $texexp = str_replace('</nolink>','',$texexp);
-        $texexp = str_replace('<span class="nolink">','',$texexp);
-        $texexp = str_replace('</span>','',$texexp);
-        $texexp = eregi_replace("<br[[:space:]]*\/?>", '', $texexp);  //dlnsk
-        $align = "middle";
-        if (preg_match('/^align=bottom /',$texexp)) {
-          $align = "text-bottom";
-          $texexp = preg_replace('/^align=bottom /','',$texexp);
-        } else if (preg_match('/^align=top /',$texexp)) {
-          $align = "text-top";
-          $texexp = preg_replace('/^align=top /','',$texexp);
+        $text .= ' ';
+        preg_match_all('/\$(\$\$+?)([^\$])/s',$text,$matches);
+        for ($i=0;$i<count($matches[0]);$i++) {
+            $replacement = str_replace('$','&#x00024;',$matches[1][$i]).$matches[2][$i];
+            $text = str_replace($matches[0][$i],$replacement,$text);
         }
-        $md5 = md5($texexp);
-        if (! $texcache = $DB->get_record("cache_filters", array("filter"=>"tex", "md5key"=>$md5))) {
-            $texcache->filter = 'tex';
-            $texcache->version = 1;
-            $texcache->md5key = $md5;
-            $texcache->rawtext = $texexp;
-            $texcache->timemodified = time();
-            $DB->insert_record("cache_filters",$texcache, false);
+
+        // <tex> TeX expression </tex>
+        // or <tex alt="My alternative text to be used instead of the TeX form"> TeX expression </tex>
+        // or $$ TeX expression $$
+        // or \[ TeX expression \]          // original tag of MathType and TeXaide (dlnsk)
+        // or [tex] TeX expression [/tex]   // somtime it's more comfortable than <tex> (dlnsk)
+        preg_match_all('/<tex(?:\s+alt=["\'](.*?)["\'])?>(.+?)<\/tex>|\$\$(.+?)\$\$|\\\\\[(.+?)\\\\\]|\\[tex\\](.+?)\\[\/tex\\]/is', $text, $matches);
+        for ($i=0; $i<count($matches[0]); $i++) {
+            $texexp = $matches[2][$i] . $matches[3][$i] . $matches[4][$i] . $matches[5][$i];
+            $alt = $matches[1][$i];
+            $texexp = str_replace('<nolink>','',$texexp);
+            $texexp = str_replace('</nolink>','',$texexp);
+            $texexp = str_replace('<span class="nolink">','',$texexp);
+            $texexp = str_replace('</span>','',$texexp);
+            $texexp = eregi_replace("<br[[:space:]]*\/?>", '', $texexp);  //dlnsk
+            $align = "middle";
+            if (preg_match('/^align=bottom /',$texexp)) {
+              $align = "text-bottom";
+              $texexp = preg_replace('/^align=bottom /','',$texexp);
+            } else if (preg_match('/^align=top /',$texexp)) {
+              $align = "text-top";
+              $texexp = preg_replace('/^align=top /','',$texexp);
+            }
+            $md5 = md5($texexp);
+            if (! $texcache = $DB->get_record("cache_filters", array("filter"=>"tex", "md5key"=>$md5))) {
+                $texcache->filter = 'tex';
+                $texcache->version = 1;
+                $texcache->md5key = $md5;
+                $texcache->rawtext = $texexp;
+                $texcache->timemodified = time();
+                $DB->insert_record("cache_filters", $texcache, false);
+            }
+            $filename = $md5 . ".gif";
+            $text = str_replace( $matches[0][$i], string_file_picture_tex($filename, $texexp, '', '', $align, $alt), $text);
         }
-        $filename = $md5 . ".gif";
-        $text = str_replace( $matches[0][$i], string_file_picture_tex($filename, $texexp, '', '', $align, $alt), $text);
+        return $text;
     }
-    return $text;
 }
 
 ?>
index c12778f..bb6893d 100644 (file)
@@ -1,6 +1,6 @@
 <?php
       
-// This function looks for text including markup and
+// This class looks for text including markup and
 // applies tidy's repair function to it.
 // Tidy is a HTML clean and
 // repair utility, which is currently available for PHP 4.3.x and PHP 5 as a
 // If you want to know what you can set in $tidyoptions and what their default
 // values are, see http://php.net/manual/en/function.tidy-get-config.php.
       
-/**
-* @author Hannes Gassert <hannes at mediagonal dot ch>
-* @param        int            course id
-* @param        string         text to be filtered
-*/
-function tidy_filter($courseid, $text) {
-       
-/// Configuration for tidy. Feel free to tune for your needs, e.g. to allow
-/// proprietary markup.
-    $tidyoptions = array(             
-             'output-xhtml' => true,
-             'show-body-only' => true,
-             'tidy-mark' => false,
-             'drop-proprietary-attributes' => true,
-             'drop-font-tags' => true,
-             'drop-empty-paras' => true,
-             'indent' => true,
-             'quiet' => true,
-    );
-    
-/// Do a quick check using strpos to avoid unnecessary work
-    if (strpos($text, '<') === false) {
-        return $text;
+class tidy_filter extends filter_base {
+    function __construct($courseid, $format, $options) {
+        parent::__construct($courseid, $format, $options);
     }
 
-    
-/// If enabled: run tidy over the entire string
-    if (function_exists('tidy_repair_string')){
-        $text = tidy_repair_string($text, $tidyoptions, 'utf8');
-    }
+    /**
+    * @author Hannes Gassert <hannes at mediagonal dot ch>
+    * @param        string         text to be filtered
+    */
+    function filter($text) {
+           
+    /// Configuration for tidy. Feel free to tune for your needs, e.g. to allow
+    /// proprietary markup.
+        $tidyoptions = array(             
+                 'output-xhtml' => true,
+                 'show-body-only' => true,
+                 'tidy-mark' => false,
+                 'drop-proprietary-attributes' => true,
+                 'drop-font-tags' => true,
+                 'drop-empty-paras' => true,
+                 'indent' => true,
+                 'quiet' => true,
+        );
+        
+    /// Do a quick check using strpos to avoid unnecessary work
+        if (strpos($text, '<') === false) {
+            return $text;
+        }
+
+        
+    /// If enabled: run tidy over the entire string
+        if (function_exists('tidy_repair_string')){
+            $text = tidy_repair_string($text, $tidyoptions, 'utf8');
+        }
 
-    return $text;
+        return $text;
+    }
 }
 ?>
index 94c410c..7227180 100644 (file)
@@ -1,6 +1,55 @@
 <?php // $Id$
       // Contains special functions that are particularly useful to filters
 
+abstract class filter_base {
+    public static $filters = array();
+    protected $courseid;
+    protected $format;
+    protected $options;
+
+    public function __construct($courseid, $format, $options) {
+        $this->courseid = $courseid;
+        $this->format   = $format;
+        $this->options  = $options;
+    }
+
+    public static function addfilter($classname, $obj) {
+        if (empty(self::$filters[$classname])) {
+            self::$filters[$classname] = $obj;
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    public static function do_filter($text, $courseid = null) {
+        global $CFG;
+
+        foreach (self::$filters as $n=>$obj) {
+            $text = $obj->filter($text); 
+        }
+
+        // back compatable with old filter plugins
+        $textfilters = explode(',', $CFG->textfilters);
+        foreach ($textfilters as $v) {
+            $text_filter = basename($v).'_filter';
+            if (empty(self::$filters[$text_filter]) && is_readable($CFG->dirroot .'/'. $v .'/filter.php')) {
+                include_once($CFG->dirroot .'/'. $v .'/filter.php');
+                if (function_exists($text_filter)) {
+                    $text = $text_filter($courseid, $text);
+                }
+            }
+        }
+        return $text;
+    }
+
+    public function hash() {
+        return __CLASS__;
+    }
+
+    // filter plugin must overwrite this function to filter
+    abstract function filter($text);
+}
 
 /**
  * This is just a little object to define a phrase and some instructions 
index 5e45d08..7b5c7c0 100644 (file)
@@ -1,6 +1,5 @@
 <?php // $Id$
 
-
 ///////////////////////////////////////////////////////////////////////////
 //                                                                       //
 // NOTICE OF COPYRIGHT                                                   //
@@ -1352,6 +1351,8 @@ function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL
     global $CFG, $COURSE, $DB;
 
     static $croncache = array();
+    
+    $hashstr = '';
 
     if ($text === '') {
         return ''; // no need to do any filters and cleaning
@@ -1379,15 +1380,34 @@ function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL
     if (!isset($options->newlines)) {
         $options->newlines=true;
     }
-
     if (empty($courseid)) {
         $courseid = $COURSE->id;
     }
 
+    // if filter plugin is OOP format, add it to filter list and append hash
+    // value to $hashstr
+    if (!empty($CFG->textfilters)) {
+        require_once($CFG->libdir.'/filterlib.php');
+        $textfilters = explode(',', $CFG->textfilters);
+        foreach ($textfilters as $textfilter) {
+            if (is_readable($CFG->dirroot .'/'. $textfilter .'/filter.php')) {
+                include_once($CFG->dirroot .'/'. $textfilter .'/filter.php');
+                $text_filter = basename($textfilter).'_filter';
+                if (class_exists($text_filter)) {
+                    $obj = new $text_filter($courseid, $format, $options);
+                    filter_base::addfilter($text_filter, $obj);
+                    $hashstr .= $obj->hash();
+                }
+            }
+        }
+    }
     if (!empty($CFG->cachetext) and empty($options->nocache)) {
-        $time = time() - $CFG->cachetext;
-        $md5key = md5($text.'-'.(int)$courseid.'-'.current_language().'-'.(int)$format.(int)$options->trusttext.(int)$options->noclean.(int)$options->smiley.(int)$options->filter.(int)$options->para.(int)$options->newlines);
+        $hashstr .= $text.'-'.(int)$courseid.'-'.current_language().'-'.(int)$format.(int)$options->trusttext.(int)$options->noclean.(int)$options->smiley.(int)$options->filter.(int)$options->para.(int)$options->newlines;
+        // for debug filtering system
+        // $hashstr .= time();
 
+        $time = time() - $CFG->cachetext;
+        $md5key = md5($hashstr); 
         if (defined('FULLME') and FULLME == 'cron') {
             if (isset($croncache[$md5key])) {
                 return $croncache[$md5key];
@@ -1438,7 +1458,7 @@ function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL
                 $text = clean_text($text, FORMAT_HTML);
             }
             if ($options->filter) {
-                $text = filter_text($text, $courseid);
+                $text = filter_base::do_filter($text, $courseid);
             }
             break;
 
@@ -1467,7 +1487,7 @@ function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL
             }
 
             if ($options->filter) {
-                $text = filter_text($text, $courseid);
+                $text = filter_base::do_filter($text, $courseid);
             }
             break;
 
@@ -1478,7 +1498,7 @@ function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL
             }
 
             if ($options->filter) {
-                $text = filter_text($text, $courseid);
+                $text = filter_base::do_filter($text, $courseid);
             }
             break;
     }