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
if (isset($CFG->textfilters)) {
$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
* for how to process it. Filters can create an array of these to pass
* to the filter_phrases function below.
**/
class filterobject {
var $phrase;
var $hreftagbegin;
var $hreftagend;
var $casesensitive;
var $fullmatch;
var $replacementphrase;
var $work_phrase;
var $work_hreftagbegin;
var $work_hreftagend;
var $work_casesensitive;
var $work_fullmatch;
var $work_replacementphrase;
var $work_calculated;
/// a constructor just because I like constructing
function filterobject($phrase, $hreftagbegin='',
$hreftagend='',
$casesensitive=false,
$fullmatch=false,
$replacementphrase=NULL) {
$this->phrase = $phrase;
$this->hreftagbegin = $hreftagbegin;
$this->hreftagend = $hreftagend;
$this->casesensitive = $casesensitive;
$this->fullmatch = $fullmatch;
$this->replacementphrase= $replacementphrase;
$this->work_calculated = false;
}
}
/**
* Process phrases intelligently found within a HTML text (such as adding links)
*
* param text the text that we are filtering
* param link_array an array of filterobjects
* param ignoretagsopen an array of opening tags that we should ignore while filtering
* param ignoretagsclose an array of corresponding closing tags
**/
function filter_phrases ($text, &$link_array, $ignoretagsopen=NULL, $ignoretagsclose=NULL) {
global $CFG;
static $usedphrases;
$ignoretags = array(); //To store all the enclosig tags to be completely ignored
$tags = array(); //To store all the simple tags to be ignored
/// A list of open/close tags that we should not replace within
/// No reason why you can't put full preg expressions in here too
/// eg '";
if (stripos($text, '') !== FALSE) {
//try to add it into the head element
$text = str_ireplace('', $js.'', $text);
return $text;
}
//last chance - try adding head element
return preg_replace("//is", "\\0".$js.'', $text);
}
?>