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);
}
/// Define one exclusive separator that we'll use in the temp saved tags
/// keys. It must be something rare enough to avoid having matches with
/// filterobjects. MDL-18165
define('EXCL_SEPARATOR', '-%-');
/**
* 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;
}
}
/**
* Look up the name of this filter in the most appropriate location.
* If $filterlocation = 'mod' then does get_string('filtername', $filter);
* else if $filterlocation = 'filter' then does get_string('filtername', 'filter_' . $filter);
* with a fallback to get_string('filtername', $filter) for backwards compatibility.
* These are the only two options supported at the moment.
* @param string $filterlocation 'filter' or 'mod'.
* @param string $filter the folder name where the filter lives.
* @return string the human-readable name for this filter.
*/
function filter_get_name($filterlocation, $filter) {
switch ($filterlocation) {
case 'filter':
$strfiltername = get_string('filtername', 'filter_' . $filter);
if (substr($strfiltername, 0, 2) != '[[') {
// found a valid string.
return $strfiltername;
}
// Fall through to try the legacy location.
case 'mod':
$strfiltername = get_string('filtername', $filter);
if (substr($strfiltername, 0, 2) == '[[') {
$strfiltername .= ' (' . $filterlocation . '/' . $filter . ')';
}
return $strfiltername;
default:
throw new coding_exception('Unknown filter location ' . $filterlocation);
}
}
/**
* Get the names of all the filters installed in this Moodle.
* @return array path => filter name from the appropriate lang file. e.g.
* array('mod/glossary' => 'Glossary Auto-linking', 'filter/tex' => 'TeX Notation');
* sorted in alphabetical order of name.
*/
function filter_get_all_installed() {
global $CFG;
$filternames = array();
$filterlocations = array('mod', 'filter');
foreach ($filterlocations as $filterlocation) {
$filters = get_list_of_plugins($filterlocation);
foreach ($filters as $filter) {
$path = $filterlocation . '/' . $filter;
if (is_readable($CFG->dirroot . '/' . $path . '/filter.php')) {
$strfiltername = filter_get_name($filterlocation, $filter);
$filternames[$path] = $strfiltername;
}
}
}
asort($filternames, SORT_LOCALE_STRING);
return $filternames;
}
/**
* Set the global activated state for a text filter.
* @param string$filter The filter name, for example 'filter/tex' or 'mod/glossary'.
* @param integer $state One of the values TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_DISABLED.
* @param integer $sortorder (optional) a position in the sortorder to place this filter.
* If not given defaults to:
* No change in order if we are updating an exsiting record, and not changing to or from TEXTFILTER_DISABLED.
* Just after the last currently active filter for a change to TEXTFILTER_ON or TEXTFILTER_OFF
* Just after the very last filter for a change to TEXTFILTER_DISABLED
*/
function filter_set_global_state($filter, $state, $sortorder = false) {
global $DB;
// Check requested state is valid.
if (!in_array($state, array(TEXTFILTER_ON, TEXTFILTER_OFF, TEXTFILTER_DISABLED))) {
throw new coding_exception("Illegal option '$state' passed to filter_set_global_state. ' .
'Must be one of TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_DISABLED.");
}
// Check sortorder is valid.
if ($sortorder !== false) {
if ($sortorder < 1 || $sortorder > $DB->get_field('filter_active', 'MAX(sortorder)', array()) + 1) {
throw new coding_exception("Invalid sort order passed to filter_set_global_state.");
}
}
// See if there is an existing record.
$syscontext = get_context_instance(CONTEXT_SYSTEM);
$rec = $DB->get_record('filter_active', array('filter' => $filter, 'contextid' => $syscontext->id));
if (empty($rec)) {
$insert = true;
$rec = new stdClass;
$rec->filter = $filter;
$rec->contextid = $syscontext->id;
} else {
$insert = false;
if ($sortorder === false && !($rec->active == TEXTFILTER_DISABLED xor $state == TEXTFILTER_DISABLED)) {
$sortorder = $rec->sortorder;
}
}
// Automatic sort order.
if ($sortorder === false) {
if ($state == TEXTFILTER_DISABLED) {
$prevmaxsortorder = $DB->get_field('filter_active', 'MAX(sortorder)', array());
} else {
$prevmaxsortorder = $DB->get_field_select('filter_active', 'MAX(sortorder)', 'active <> ?', array(TEXTFILTER_DISABLED));
}
if (empty($prevmaxsortorder)) {
$sortorder = 1;
} else {
$sortorder = $prevmaxsortorder + 1;
if (!$insert && $state == TEXTFILTER_DISABLED) {
$sortorder = $prevmaxsortorder;
}
}
}
// Move any existing records out of the way of the sortorder.
if ($insert) {
$DB->execute('UPDATE {filter_active} SET sortorder = sortorder + 1 WHERE sortorder >= ?', array($sortorder));
} else if ($sortorder != $rec->sortorder) {
$sparesortorder = $DB->get_field('filter_active', 'MIN(sortorder)', array()) - 1;
$DB->set_field('filter_active', 'sortorder', $sparesortorder, array('filter' => $filter, 'contextid' => $syscontext->id));
if ($sortorder < $rec->sortorder) {
$DB->execute('UPDATE {filter_active} SET sortorder = sortorder + 1 WHERE sortorder >= ? AND sortorder < ?',
array($sortorder, $rec->sortorder));
} else if ($sortorder > $rec->sortorder) {
$DB->execute('UPDATE {filter_active} SET sortorder = sortorder - 1 WHERE sortorder <= ? AND sortorder > ?',
array($sortorder, $rec->sortorder));
}
}
// Insert/update the new record.
$rec->active = $state;
$rec->sortorder = $sortorder;
if ($insert) {
$DB->insert_record('filter_active', $rec);
} else {
$DB->update_record('filter_active', $rec);
}
}
/**
* Set a particular local config variable for a filter in a context.
* @param string $filter The filter name, for example 'filter/tex' or 'mod/glossary'.
* @param integer $contextid The ID of the context to get the local config for.
* @param string $name the setting name.
* @param string $value the corresponding value.
*/
function filter_set_local_config($filter, $contextid, $name, $value) {
global $DB;
$rec = $DB->get_record('filter_config', array('filter' => $filter, 'contextid' => $contextid, 'name' => $name));
$insert = false;
if (empty($rec)) {
$insert = true;
$rec = new stdClass;
$rec->filter = $filter;
$rec->contextid = $contextid;
$rec->name = $name;
}
$rec->value = $value;
if ($insert) {
$DB->insert_record('filter_config', $rec);
} else {
$DB->update_record('filter_config', $rec);
}
}
/**
* Get local config variables for a filter in a context. Normally (when your
* filter is running) you don't need to call this, becuase the config is fetched
* for you automatically. You only need this, for example, when you are getting
* the config so you can show the user an editing from.
* @param string $filter The filter name, for example 'filter/tex' or 'mod/glossary'.
* @param integer $contextid The ID of the context to get the local config for.
* @return array of name => value pairs.
*/
function filter_get_local_config($filter, $contextid) {
global $DB;
return $DB->get_records_menu('filter_config', array('filter' => $filter, 'contextid' => $contextid), '', 'name,value');
}
/**
* 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);
}
?>