2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Library functions for managing text filter plugins.
20 * @package core_filter
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
27 /** The states a filter can be in, stored in the filter_active table. */
28 define('TEXTFILTER_ON', 1);
29 /** The states a filter can be in, stored in the filter_active table. */
30 define('TEXTFILTER_INHERIT', 0);
31 /** The states a filter can be in, stored in the filter_active table. */
32 define('TEXTFILTER_OFF', -1);
33 /** The states a filter can be in, stored in the filter_active table. */
34 define('TEXTFILTER_DISABLED', -9999);
37 * Define one exclusive separator that we'll use in the temp saved tags
38 * keys. It must be something rare enough to avoid having matches with
39 * filterobjects. MDL-18165
41 define('TEXTFILTER_EXCL_SEPARATOR', '-%-');
45 * Class to manage the filtering of strings. It is intended that this class is
46 * only used by weblib.php. Client code should probably be using the
47 * format_text and format_string functions.
49 * This class is a singleton.
51 * @package core_filter
52 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
53 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
55 class filter_manager
{
57 * @var array This list of active filters, by context, for filtering content.
58 * An array contextid => array of filter objects.
60 protected $textfilters = array();
63 * @var array This list of active filters, by context, for filtering strings.
64 * An array contextid => array of filter objects.
66 protected $stringfilters = array();
68 /** @var array Exploded version of $CFG->stringfilters. */
69 protected $stringfilternames = array();
71 /** @var object Holds the singleton instance. */
72 protected static $singletoninstance;
74 protected function __construct() {
75 $this->stringfilternames
= filter_get_string_filters();
79 * @return filter_manager the singleton instance.
81 public static function instance() {
83 if (is_null(self
::$singletoninstance)) {
84 if (!empty($CFG->perfdebug
) and $CFG->perfdebug
> 7) {
85 self
::$singletoninstance = new performance_measuring_filter_manager();
87 self
::$singletoninstance = new self();
90 return self
::$singletoninstance;
94 * Resets the caches, usually to be called between unit tests
96 public static function reset_caches() {
97 if (self
::$singletoninstance) {
98 self
::$singletoninstance->unload_all_filters();
100 self
::$singletoninstance = null;
104 * Unloads all filters and other cached information
106 protected function unload_all_filters() {
107 $this->textfilters
= array();
108 $this->stringfilters
= array();
109 $this->stringfilternames
= array();
113 * Load all the filters required by this context.
115 * @param object $context
117 protected function load_filters($context) {
118 $filters = filter_get_active_in_context($context);
119 $this->textfilters
[$context->id
] = array();
120 $this->stringfilters
[$context->id
] = array();
121 foreach ($filters as $filtername => $localconfig) {
122 $filter = $this->make_filter_object($filtername, $context, $localconfig);
123 if (is_null($filter)) {
126 $this->textfilters
[$context->id
][] = $filter;
127 if (in_array($filtername, $this->stringfilternames
)) {
128 $this->stringfilters
[$context->id
][] = $filter;
134 * Factory method for creating a filter.
136 * @param string $filtername The filter name, for example 'tex'.
137 * @param context $context context object.
138 * @param array $localconfig array of local configuration variables for this filter.
139 * @return moodle_text_filter The filter, or null, if this type of filter is
140 * not recognised or could not be created.
142 protected function make_filter_object($filtername, $context, $localconfig) {
144 $path = $CFG->dirroot
.'/filter/'. $filtername .'/filter.php';
145 if (!is_readable($path)) {
150 $filterclassname = 'filter_' . $filtername;
151 if (class_exists($filterclassname)) {
152 return new $filterclassname($context, $localconfig);
159 * @todo Document this function
160 * @param string $text
161 * @param array $filterchain
162 * @param array $options options passed to the filters
163 * @return string $text
165 protected function apply_filter_chain($text, $filterchain, array $options = array()) {
166 foreach ($filterchain as $filter) {
167 $text = $filter->filter($text, $options);
173 * @todo Document this function
174 * @param object $context
175 * @return object A text filter
177 protected function get_text_filters($context) {
178 if (!isset($this->textfilters
[$context->id
])) {
179 $this->load_filters($context);
181 return $this->textfilters
[$context->id
];
185 * @todo Document this function
186 * @param object $context
187 * @return object A string filter
189 protected function get_string_filters($context) {
190 if (!isset($this->stringfilters
[$context->id
])) {
191 $this->load_filters($context);
193 return $this->stringfilters
[$context->id
];
199 * @param string $text The text to filter
200 * @param object $context
201 * @param array $options options passed to the filters
202 * @return string resulting text
204 public function filter_text($text, $context, array $options = array()) {
205 $text = $this->apply_filter_chain($text, $this->get_text_filters($context), $options);
206 // <nolink> tags removed for XHTML compatibility
207 $text = str_replace(array('<nolink>', '</nolink>'), '', $text);
212 * Filter a piece of string
214 * @param string $string The text to filter
215 * @param context $context
216 * @return string resulting string
218 public function filter_string($string, $context) {
219 return $this->apply_filter_chain($string, $this->get_string_filters($context));
223 * @todo Document this function
224 * @param context $context
225 * @return object A string filter
227 public function text_filtering_hash($context) {
228 $filters = $this->get_text_filters($context);
230 foreach ($filters as $filter) {
231 $hashes[] = $filter->hash();
233 return implode('-', $hashes);
237 * Setup page with filters requirements and other prepare stuff.
239 * This method is used by {@see format_text()} and {@see format_string()}
240 * in order to allow filters to setup any page requirement (js, css...)
241 * or perform any action needed to get them prepared before filtering itself
242 * happens by calling to each every active setup() method.
244 * Note it's executed for each piece of text filtered, so filter implementations
245 * are responsible of controlling the cardinality of the executions that may
246 * be different depending of the stuff to prepare.
248 * @param moodle_page $page the page we are going to add requirements to.
249 * @param context $context the context which contents are going to be filtered.
252 public function setup_page_for_filters($page, $context) {
253 $filters = $this->get_text_filters($context);
254 foreach ($filters as $filter) {
255 $filter->setup($page, $context);
261 * Filter manager subclass that does nothing. Having this simplifies the logic
262 * of format_text, etc.
264 * @todo Document this class
266 * @package core_filter
267 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
268 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
270 class null_filter_manager
{
274 public function filter_text($text, $context, $options) {
281 public function filter_string($string, $context) {
288 public function text_filtering_hash() {
294 * Filter manager subclass that tacks how much work it does.
296 * @todo Document this class
298 * @package core_filter
299 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
300 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
302 class performance_measuring_filter_manager
extends filter_manager
{
304 protected $filterscreated = 0;
305 protected $textsfiltered = 0;
306 protected $stringsfiltered = 0;
309 * Unloads all filters and other cached information
311 protected function unload_all_filters() {
312 parent
::unload_all_filters();
313 $this->filterscreated
= 0;
314 $this->textsfiltered
= 0;
315 $this->stringsfiltered
= 0;
319 * @param string $filtername
320 * @param object $context
321 * @param mixed $localconfig
324 protected function make_filter_object($filtername, $context, $localconfig) {
325 $this->filterscreated++
;
326 return parent
::make_filter_object($filtername, $context, $localconfig);
330 * @param string $text
331 * @param object $context
332 * @param array $options options passed to the filters
335 public function filter_text($text, $context, array $options = array()) {
336 $this->textsfiltered++
;
337 return parent
::filter_text($text, $context, $options);
341 * @param string $string
342 * @param object $context
345 public function filter_string($string, $context) {
346 $this->stringsfiltered++
;
347 return parent
::filter_string($string, $context);
353 public function get_performance_summary() {
355 'contextswithfilters' => count($this->textfilters
),
356 'filterscreated' => $this->filterscreated
,
357 'textsfiltered' => $this->textsfiltered
,
358 'stringsfiltered' => $this->stringsfiltered
,
360 'contextswithfilters' => 'Contexts for which filters were loaded',
361 'filterscreated' => 'Filters created',
362 'textsfiltered' => 'Pieces of content filtered',
363 'stringsfiltered' => 'Strings filtered',
369 * Base class for text filters. You just need to override this class and
370 * implement the filter method.
372 * @package core_filter
373 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
374 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
376 abstract class moodle_text_filter
{
377 /** @var object The context we are in. */
379 /** @var array Any local configuration for this filter in this context. */
380 protected $localconfig;
383 * Set any context-specific configuration for this filter.
385 * @param context $context The current context.
386 * @param array $localconfig Any context-specific configuration for this filter.
388 public function __construct($context, array $localconfig) {
389 $this->context
= $context;
390 $this->localconfig
= $localconfig;
394 * @return string The class name of the current class
396 public function hash() {
401 * Setup page with filter requirements and other prepare stuff.
403 * Override this method if the filter needs to setup page
404 * requirements or needs other stuff to be executed.
406 * Note this method is invoked from {@see setup_page_for_filters()}
407 * for each piece of text being filtered, so it is responsible
408 * for controlling its own execution cardinality.
410 * @param moodle_page $page the page we are going to add requirements to.
411 * @param context $context the context which contents are going to be filtered.
414 public function setup($page, $context) {
415 // Override me, if needed.
419 * Override this function to actually implement the filtering.
421 * @param $text some HTML content.
422 * @param array $options options passed to the filters
423 * @return the HTML content after the filtering has been applied.
425 public abstract function filter($text, array $options = array());
429 * This is just a little object to define a phrase and some instructions
430 * for how to process it. Filters can create an array of these to pass
431 * to the filter_phrases function below.
435 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
436 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
447 var $replacementphrase;
449 var $work_hreftagbegin;
450 var $work_hreftagend;
451 var $work_casesensitive;
453 var $work_replacementphrase;
455 var $work_calculated;
458 * A constructor just because I like constructing
460 * @param string $phrase
461 * @param string $hreftagbegin
462 * @param string $hreftagend
463 * @param bool $casesensitive
464 * @param bool $fullmatch
465 * @param mixed $replacementphrase
467 function filterobject($phrase, $hreftagbegin = '<span class="highlight">',
468 $hreftagend = '</span>',
469 $casesensitive = false,
471 $replacementphrase = NULL) {
473 $this->phrase
= $phrase;
474 $this->hreftagbegin
= $hreftagbegin;
475 $this->hreftagend
= $hreftagend;
476 $this->casesensitive
= $casesensitive;
477 $this->fullmatch
= $fullmatch;
478 $this->replacementphrase
= $replacementphrase;
479 $this->work_calculated
= false;
485 * Look up the name of this filter
487 * @param string $filter the filter name
488 * @return string the human-readable name for this filter.
490 function filter_get_name($filter) {
491 if (strpos($filter, 'filter/') === 0) {
492 debugging("Old '$filter'' parameter used in filter_get_name()");
493 $filter = substr($filter, 7);
494 } else if (strpos($filter, '/') !== false) {
495 throw new coding_exception('Unknown filter type ' . $filter);
498 if (get_string_manager()->string_exists('filtername', 'filter_' . $filter)) {
499 return get_string('filtername', 'filter_' . $filter);
506 * Get the names of all the filters installed in this Moodle.
508 * @return array path => filter name from the appropriate lang file. e.g.
509 * array('tex' => 'TeX Notation');
510 * sorted in alphabetical order of name.
512 function filter_get_all_installed() {
515 $filternames = array();
516 foreach (core_component
::get_plugin_list('filter') as $filter => $fulldir) {
517 if (is_readable("$fulldir/filter.php")) {
518 $filternames[$filter] = filter_get_name($filter);
521 core_collator
::asort($filternames);
526 * Set the global activated state for a text filter.
528 * @param string $filtername The filter name, for example 'tex'.
529 * @param int $state One of the values TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_DISABLED.
530 * @param int $move -1 means up, 0 means the same, 1 means down
532 function filter_set_global_state($filtername, $state, $move = 0) {
535 // Check requested state is valid.
536 if (!in_array($state, array(TEXTFILTER_ON
, TEXTFILTER_OFF
, TEXTFILTER_DISABLED
))) {
537 throw new coding_exception("Illegal option '$state' passed to filter_set_global_state. " .
538 "Must be one of TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_DISABLED.");
543 } else if ($move < 0) {
547 if (strpos($filtername, 'filter/') === 0) {
548 //debugging("Old filtername '$filtername' parameter used in filter_set_global_state()", DEBUG_DEVELOPER);
549 $filtername = substr($filtername, 7);
550 } else if (strpos($filtername, '/') !== false) {
551 throw new coding_exception("Invalid filter name '$filtername' used in filter_set_global_state()");
554 $transaction = $DB->start_delegated_transaction();
556 $syscontext = context_system
::instance();
557 $filters = $DB->get_records('filter_active', array('contextid' => $syscontext->id
), 'sortorder ASC');
562 foreach($filters as $f) {
563 if ($f->active
== TEXTFILTER_DISABLED
) {
564 $off[$f->filter
] = $f;
566 $on[$f->filter
] = $f;
570 // Update the state or add new record.
571 if (isset($on[$filtername])) {
572 $filter = $on[$filtername];
573 if ($filter->active
!= $state) {
574 add_to_config_log('filter_active', $filter->active
, $state, $filtername);
576 $filter->active
= $state;
577 $DB->update_record('filter_active', $filter);
578 if ($filter->active
== TEXTFILTER_DISABLED
) {
579 unset($on[$filtername]);
580 $off = array($filter->filter
=> $filter) +
$off;
585 } else if (isset($off[$filtername])) {
586 $filter = $off[$filtername];
587 if ($filter->active
!= $state) {
588 add_to_config_log('filter_active', $filter->active
, $state, $filtername);
590 $filter->active
= $state;
591 $DB->update_record('filter_active', $filter);
592 if ($filter->active
!= TEXTFILTER_DISABLED
) {
593 unset($off[$filtername]);
594 $on[$filter->filter
] = $filter;
599 add_to_config_log('filter_active', '', $state, $filtername);
601 $filter = new stdClass();
602 $filter->filter
= $filtername;
603 $filter->contextid
= $syscontext->id
;
604 $filter->active
= $state;
605 $filter->sortorder
= 99999;
606 $filter->id
= $DB->insert_record('filter_active', $filter);
608 $filters[$filter->id
] = $filter;
609 if ($state == TEXTFILTER_DISABLED
) {
610 $off[$filter->filter
] = $filter;
612 $on[$filter->filter
] = $filter;
617 if ($move != 0 and isset($on[$filter->filter
])) {
619 foreach ($on as $f) {
620 $f->newsortorder
= $i;
624 $filter->newsortorder
= $filter->newsortorder +
$move;
626 foreach ($on as $f) {
627 if ($f->id
== $filter->id
) {
630 if ($f->newsortorder
== $filter->newsortorder
) {
632 $f->newsortorder
= $f->newsortorder
- 1;
634 $f->newsortorder
= $f->newsortorder +
1;
639 core_collator
::asort_objects_by_property($on, 'newsortorder', core_collator
::SORT_NUMERIC
);
642 // Inactive are sorted by filter name.
643 core_collator
::asort_objects_by_property($off, 'filter', core_collator
::SORT_NATURAL
);
645 // Update records if necessary.
647 foreach ($on as $f) {
648 if ($f->sortorder
!= $i) {
649 $DB->set_field('filter_active', 'sortorder', $i, array('id'=>$f->id
));
653 foreach ($off as $f) {
654 if ($f->sortorder
!= $i) {
655 $DB->set_field('filter_active', 'sortorder', $i, array('id'=>$f->id
));
660 $transaction->allow_commit();
664 * @param string $filtername The filter name, for example 'tex'.
665 * @return boolean is this filter allowed to be used on this site. That is, the
666 * admin has set the global 'active' setting to On, or Off, but available.
668 function filter_is_enabled($filtername) {
669 if (strpos($filtername, 'filter/') === 0) {
670 //debugging("Old filtername '$filtername' parameter used in filter_is_enabled()", DEBUG_DEVELOPER);
671 $filtername = substr($filtername, 7);
672 } else if (strpos($filtername, '/') !== false) {
673 throw new coding_exception("Invalid filter name '$filtername' used in filter_is_enabled()");
675 return array_key_exists($filtername, filter_get_globally_enabled());
679 * Return a list of all the filters that may be in use somewhere.
681 * @staticvar array $enabledfilters
682 * @return array where the keys and values are both the filter name, like 'tex'.
684 function filter_get_globally_enabled() {
685 static $enabledfilters = null;
686 if (is_null($enabledfilters)) {
687 $filters = filter_get_global_states();
688 $enabledfilters = array();
689 foreach ($filters as $filter => $filerinfo) {
690 if ($filerinfo->active
!= TEXTFILTER_DISABLED
) {
691 $enabledfilters[$filter] = $filter;
695 return $enabledfilters;
699 * Return the names of the filters that should also be applied to strings
700 * (when they are enabled).
702 * @return array where the keys and values are both the filter name, like 'tex'.
704 function filter_get_string_filters() {
706 $stringfilters = array();
707 if (!empty($CFG->filterall
) && !empty($CFG->stringfilters
)) {
708 $stringfilters = explode(',', $CFG->stringfilters
);
709 $stringfilters = array_combine($stringfilters, $stringfilters);
711 return $stringfilters;
715 * Sets whether a particular active filter should be applied to all strings by
716 * format_string, or just used by format_text.
718 * @param string $filter The filter name, for example 'tex'.
719 * @param boolean $applytostrings if true, this filter will apply to format_string
720 * and format_text, when it is enabled.
722 function filter_set_applies_to_strings($filter, $applytostrings) {
723 $stringfilters = filter_get_string_filters();
724 $prevfilters = $stringfilters;
725 $allfilters = core_component
::get_plugin_list('filter');
727 if ($applytostrings) {
728 $stringfilters[$filter] = $filter;
730 unset($stringfilters[$filter]);
733 // Remove missing filters.
734 foreach ($stringfilters as $filter) {
735 if (!isset($allfilters[$filter])) {
736 unset($stringfilters[$filter]);
740 if ($prevfilters != $stringfilters) {
741 set_config('stringfilters', implode(',', $stringfilters));
742 set_config('filterall', !empty($stringfilters));
747 * Set the local activated state for a text filter.
749 * @param string $filter The filter name, for example 'tex'.
750 * @param integer $contextid The id of the context to get the local config for.
751 * @param integer $state One of the values TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_INHERIT.
754 function filter_set_local_state($filter, $contextid, $state) {
757 // Check requested state is valid.
758 if (!in_array($state, array(TEXTFILTER_ON
, TEXTFILTER_OFF
, TEXTFILTER_INHERIT
))) {
759 throw new coding_exception("Illegal option '$state' passed to filter_set_local_state. " .
760 "Must be one of TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_INHERIT.");
763 if ($contextid == context_system
::instance()->id
) {
764 throw new coding_exception('You cannot use filter_set_local_state ' .
765 'with $contextid equal to the system context id.');
768 if ($state == TEXTFILTER_INHERIT
) {
769 $DB->delete_records('filter_active', array('filter' => $filter, 'contextid' => $contextid));
773 $rec = $DB->get_record('filter_active', array('filter' => $filter, 'contextid' => $contextid));
778 $rec->filter
= $filter;
779 $rec->contextid
= $contextid;
782 $rec->active
= $state;
785 $DB->insert_record('filter_active', $rec);
787 $DB->update_record('filter_active', $rec);
792 * Set a particular local config variable for a filter in a context.
794 * @param string $filter The filter name, for example 'tex'.
795 * @param integer $contextid The id of the context to get the local config for.
796 * @param string $name the setting name.
797 * @param string $value the corresponding value.
799 function filter_set_local_config($filter, $contextid, $name, $value) {
801 $rec = $DB->get_record('filter_config', array('filter' => $filter, 'contextid' => $contextid, 'name' => $name));
806 $rec->filter
= $filter;
807 $rec->contextid
= $contextid;
811 $rec->value
= $value;
814 $DB->insert_record('filter_config', $rec);
816 $DB->update_record('filter_config', $rec);
821 * Remove a particular local config variable for a filter in a context.
823 * @param string $filter The filter name, for example 'tex'.
824 * @param integer $contextid The id of the context to get the local config for.
825 * @param string $name the setting name.
827 function filter_unset_local_config($filter, $contextid, $name) {
829 $DB->delete_records('filter_config', array('filter' => $filter, 'contextid' => $contextid, 'name' => $name));
833 * Get local config variables for a filter in a context. Normally (when your
834 * filter is running) you don't need to call this, becuase the config is fetched
835 * for you automatically. You only need this, for example, when you are getting
836 * the config so you can show the user an editing from.
838 * @param string $filter The filter name, for example 'tex'.
839 * @param integer $contextid The ID of the context to get the local config for.
840 * @return array of name => value pairs.
842 function filter_get_local_config($filter, $contextid) {
844 return $DB->get_records_menu('filter_config', array('filter' => $filter, 'contextid' => $contextid), '', 'name,value');
848 * This function is for use by backup. Gets all the filter information specific
851 * @param int $contextid
852 * @return array Array with two elements. The first element is an array of objects with
853 * fields filter and active. These come from the filter_active table. The
854 * second element is an array of objects with fields filter, name and value
855 * from the filter_config table.
857 function filter_get_all_local_settings($contextid) {
860 $DB->get_records('filter_active', array('contextid' => $contextid), 'filter', 'filter,active'),
861 $DB->get_records('filter_config', array('contextid' => $contextid), 'filter,name', 'filter,name,value'),
866 * Get the list of active filters, in the order that they should be used
867 * for a particular context, along with any local configuration variables.
869 * @param context $context a context
870 * @return array an array where the keys are the filter names, for example
871 * 'tex' and the values are any local
872 * configuration for that filter, as an array of name => value pairs
873 * from the filter_config table. In a lot of cases, this will be an
874 * empty array. So, an example return value for this function might be
875 * array(tex' => array())
877 function filter_get_active_in_context($context) {
878 global $DB, $FILTERLIB_PRIVATE;
880 if (!isset($FILTERLIB_PRIVATE)) {
881 $FILTERLIB_PRIVATE = new stdClass();
884 // Use cache (this is a within-request cache only) if available. See
885 // function filter_preload_activities.
886 if (isset($FILTERLIB_PRIVATE->active
) &&
887 array_key_exists($context->id
, $FILTERLIB_PRIVATE->active
)) {
888 return $FILTERLIB_PRIVATE->active
[$context->id
];
891 $contextids = str_replace('/', ',', trim($context->path
, '/'));
893 // The following SQL is tricky. It is explained on
894 // http://docs.moodle.org/dev/Filter_enable/disable_by_context
895 $sql = "SELECT active.filter, fc.name, fc.value
896 FROM (SELECT f.filter, MAX(f.sortorder) AS sortorder
897 FROM {filter_active} f
898 JOIN {context} ctx ON f.contextid = ctx.id
899 WHERE ctx.id IN ($contextids)
901 HAVING MAX(f.active * ctx.depth) > -MIN(f.active * ctx.depth)
903 LEFT JOIN {filter_config} fc ON fc.filter = active.filter AND fc.contextid = $context->id
904 ORDER BY active.sortorder";
905 $rs = $DB->get_recordset_sql($sql);
907 // Massage the data into the specified format to return.
909 foreach ($rs as $row) {
910 if (!isset($filters[$row->filter
])) {
911 $filters[$row->filter
] = array();
913 if (!is_null($row->name
)) {
914 $filters[$row->filter
][$row->name
] = $row->value
;
924 * Preloads the list of active filters for all activities (modules) on the course
925 * using two database queries.
927 * @param course_modinfo $modinfo Course object from get_fast_modinfo
929 function filter_preload_activities(course_modinfo
$modinfo) {
930 global $DB, $FILTERLIB_PRIVATE;
932 if (!isset($FILTERLIB_PRIVATE)) {
933 $FILTERLIB_PRIVATE = new stdClass();
936 // Don't repeat preload
937 if (!isset($FILTERLIB_PRIVATE->preloaded
)) {
938 $FILTERLIB_PRIVATE->preloaded
= array();
940 if (!empty($FILTERLIB_PRIVATE->preloaded
[$modinfo->get_course_id()])) {
943 $FILTERLIB_PRIVATE->preloaded
[$modinfo->get_course_id()] = true;
945 // Get contexts for all CMs
946 $cmcontexts = array();
947 $cmcontextids = array();
948 foreach ($modinfo->get_cms() as $cm) {
949 $modulecontext = context_module
::instance($cm->id
);
950 $cmcontextids[] = $modulecontext->id
;
951 $cmcontexts[] = $modulecontext;
954 // Get course context and all other parents...
955 $coursecontext = context_course
::instance($modinfo->get_course_id());
956 $parentcontextids = explode('/', substr($coursecontext->path
, 1));
957 $allcontextids = array_merge($cmcontextids, $parentcontextids);
959 // Get all filter_active rows relating to all these contexts
960 list ($sql, $params) = $DB->get_in_or_equal($allcontextids);
961 $filteractives = $DB->get_records_select('filter_active', "contextid $sql", $params);
963 // Get all filter_config only for the cm contexts
964 list ($sql, $params) = $DB->get_in_or_equal($cmcontextids);
965 $filterconfigs = $DB->get_records_select('filter_config', "contextid $sql", $params);
967 // Note: I was a bit surprised that filter_config only works for the
968 // most specific context (i.e. it does not need to be checked for course
969 // context if we only care about CMs) however basede on code in
970 // filter_get_active_in_context, this does seem to be correct.
972 // Build course default active list. Initially this will be an array of
973 // filter name => active score (where an active score >0 means it's active)
974 $courseactive = array();
976 // Also build list of filter_active rows below course level, by contextid
977 $remainingactives = array();
979 // Array lists filters that are banned at top level
982 // Add any active filters in parent contexts to the array
983 foreach ($filteractives as $row) {
984 $depth = array_search($row->contextid
, $parentcontextids);
985 if ($depth !== false) {
987 if (!array_key_exists($row->filter
, $courseactive)) {
988 $courseactive[$row->filter
] = 0;
990 // This maths copes with reading rows in any order. Turning on/off
991 // at site level counts 1, at next level down 4, at next level 9,
992 // then 16, etc. This means the deepest level always wins, except
993 // against the -9999 at top level.
994 $courseactive[$row->filter
] +
=
995 ($depth +
1) * ($depth +
1) * $row->active
;
997 if ($row->active
== TEXTFILTER_DISABLED
) {
998 $banned[$row->filter
] = true;
1001 // Build list of other rows indexed by contextid
1002 if (!array_key_exists($row->contextid
, $remainingactives)) {
1003 $remainingactives[$row->contextid
] = array();
1005 $remainingactives[$row->contextid
][] = $row;
1009 // Chuck away the ones that aren't active.
1010 foreach ($courseactive as $filter=>$score) {
1012 unset($courseactive[$filter]);
1014 $courseactive[$filter] = array();
1018 // Loop through the contexts to reconstruct filter_active lists for each
1019 // cm on the course.
1020 if (!isset($FILTERLIB_PRIVATE->active
)) {
1021 $FILTERLIB_PRIVATE->active
= array();
1023 foreach ($cmcontextids as $contextid) {
1025 $FILTERLIB_PRIVATE->active
[$contextid] = $courseactive;
1027 // Are there any changes to the active list?
1028 if (array_key_exists($contextid, $remainingactives)) {
1029 foreach ($remainingactives[$contextid] as $row) {
1030 if ($row->active
> 0 && empty($banned[$row->filter
])) {
1031 // If it's marked active for specific context, add entry
1032 // (doesn't matter if one exists already).
1033 $FILTERLIB_PRIVATE->active
[$contextid][$row->filter
] = array();
1035 // If it's marked inactive, remove entry (doesn't matter
1036 // if it doesn't exist).
1037 unset($FILTERLIB_PRIVATE->active
[$contextid][$row->filter
]);
1043 // Process all config rows to add config data to these entries.
1044 foreach ($filterconfigs as $row) {
1045 if (isset($FILTERLIB_PRIVATE->active
[$row->contextid
][$row->filter
])) {
1046 $FILTERLIB_PRIVATE->active
[$row->contextid
][$row->filter
][$row->name
] = $row->value
;
1052 * List all of the filters that are available in this context, and what the
1053 * local and inherited states of that filter are.
1055 * @param context $context a context that is not the system context.
1056 * @return array an array with filter names, for example 'tex'
1057 * as keys. and and the values are objects with fields:
1058 * ->filter filter name, same as the key.
1059 * ->localstate TEXTFILTER_ON/OFF/INHERIT
1060 * ->inheritedstate TEXTFILTER_ON/OFF - the state that will be used if localstate is set to TEXTFILTER_INHERIT.
1062 function filter_get_available_in_context($context) {
1065 // The complex logic is working out the active state in the parent context,
1066 // so strip the current context from the list.
1067 $contextids = explode('/', trim($context->path
, '/'));
1068 array_pop($contextids);
1069 $contextids = implode(',', $contextids);
1070 if (empty($contextids)) {
1071 throw new coding_exception('filter_get_available_in_context cannot be called with the system context.');
1074 // The following SQL is tricky, in the same way at the SQL in filter_get_active_in_context.
1075 $sql = "SELECT parent_states.filter,
1076 CASE WHEN fa.active IS NULL THEN " . TEXTFILTER_INHERIT
. "
1077 ELSE fa.active END AS localstate,
1078 parent_states.inheritedstate
1079 FROM (SELECT f.filter, MAX(f.sortorder) AS sortorder,
1080 CASE WHEN MAX(f.active * ctx.depth) > -MIN(f.active * ctx.depth) THEN " . TEXTFILTER_ON
. "
1081 ELSE " . TEXTFILTER_OFF
. " END AS inheritedstate
1082 FROM {filter_active} f
1083 JOIN {context} ctx ON f.contextid = ctx.id
1084 WHERE ctx.id IN ($contextids)
1086 HAVING MIN(f.active) > " . TEXTFILTER_DISABLED
. "
1088 LEFT JOIN {filter_active} fa ON fa.filter = parent_states.filter AND fa.contextid = $context->id
1089 ORDER BY parent_states.sortorder";
1090 return $DB->get_records_sql($sql);
1094 * This function is for use by the filter administration page.
1096 * @return array 'filtername' => object with fields 'filter' (=filtername), 'active' and 'sortorder'
1098 function filter_get_global_states() {
1100 $context = context_system
::instance();
1101 return $DB->get_records('filter_active', array('contextid' => $context->id
), 'sortorder', 'filter,active,sortorder');
1105 * Delete all the data in the database relating to a filter, prior to deleting it.
1107 * @param string $filter The filter name, for example 'tex'.
1109 function filter_delete_all_for_filter($filter) {
1112 unset_all_config_for_plugin('filter_' . $filter);
1113 $DB->delete_records('filter_active', array('filter' => $filter));
1114 $DB->delete_records('filter_config', array('filter' => $filter));
1118 * Delete all the data in the database relating to a context, used when contexts are deleted.
1120 * @param integer $contextid The id of the context being deleted.
1122 function filter_delete_all_for_context($contextid) {
1124 $DB->delete_records('filter_active', array('contextid' => $contextid));
1125 $DB->delete_records('filter_config', array('contextid' => $contextid));
1129 * Does this filter have a global settings page in the admin tree?
1130 * (The settings page for a filter must be called, for example, filtersettingfiltertex.)
1132 * @param string $filter The filter name, for example 'tex'.
1133 * @return boolean Whether there should be a 'Settings' link on the config page.
1135 function filter_has_global_settings($filter) {
1137 $settingspath = $CFG->dirroot
. '/filter/' . $filter . '/settings.php';
1138 if (is_readable($settingspath)) {
1141 $settingspath = $CFG->dirroot
. '/filter/' . $filter . '/filtersettings.php';
1142 return is_readable($settingspath);
1146 * Does this filter have local (per-context) settings?
1148 * @param string $filter The filter name, for example 'tex'.
1149 * @return boolean Whether there should be a 'Settings' link on the manage filters in context page.
1151 function filter_has_local_settings($filter) {
1153 $settingspath = $CFG->dirroot
. '/filter/' . $filter . '/filterlocalsettings.php';
1154 return is_readable($settingspath);
1158 * Certain types of context (block and user) may not have local filter settings.
1159 * the function checks a context to see whether it may have local config.
1161 * @param object $context a context.
1162 * @return boolean whether this context may have local filter settings.
1164 function filter_context_may_have_filter_settings($context) {
1165 return $context->contextlevel
!= CONTEXT_BLOCK
&& $context->contextlevel
!= CONTEXT_USER
;
1169 * Process phrases intelligently found within a HTML text (such as adding links).
1171 * @staticvar array $usedpharses
1172 * @param string $text the text that we are filtering
1173 * @param array $link_array an array of filterobjects
1174 * @param array $ignoretagsopen an array of opening tags that we should ignore while filtering
1175 * @param array $ignoretagsclose an array of corresponding closing tags
1176 * @param bool $overridedefaultignore True to only use tags provided by arguments
1179 function filter_phrases($text, &$link_array, $ignoretagsopen=NULL, $ignoretagsclose=NULL,
1180 $overridedefaultignore=false) {
1184 static $usedphrases;
1186 $ignoretags = array(); // To store all the enclosig tags to be completely ignored.
1187 $tags = array(); // To store all the simple tags to be ignored.
1189 if (!$overridedefaultignore) {
1190 // A list of open/close tags that we should not replace within
1191 // Extended to include <script>, <textarea>, <select> and <a> tags
1192 // Regular expression allows tags with or without attributes
1193 $filterignoretagsopen = array('<head>' , '<nolink>' , '<span class="nolink">',
1194 '<script(\s[^>]*?)?>', '<textarea(\s[^>]*?)?>',
1195 '<select(\s[^>]*?)?>', '<a(\s[^>]*?)?>');
1196 $filterignoretagsclose = array('</head>', '</nolink>', '</span>',
1197 '</script>', '</textarea>', '</select>','</a>');
1199 // Set an empty default list.
1200 $filterignoretagsopen = array();
1201 $filterignoretagsclose = array();
1204 // Add the user defined ignore tags to the default list.
1205 if ( is_array($ignoretagsopen) ) {
1206 foreach ($ignoretagsopen as $open) {
1207 $filterignoretagsopen[] = $open;
1209 foreach ($ignoretagsclose as $close) {
1210 $filterignoretagsclose[] = $close;
1214 // Invalid prefixes and suffixes for the fullmatch searches
1215 // Every "word" character, but the underscore, is a invalid suffix or prefix.
1216 // (nice to use this because it includes national characters (accents...) as word characters.
1217 $filterinvalidprefixes = '([^\W_])';
1218 $filterinvalidsuffixes = '([^\W_])';
1220 // Double up some magic chars to avoid "accidental matches"
1221 $text = preg_replace('/([#*%])/','\1\1',$text);
1224 //Remove everything enclosed by the ignore tags from $text
1225 filter_save_ignore_tags($text,$filterignoretagsopen,$filterignoretagsclose,$ignoretags);
1227 // Remove tags from $text
1228 filter_save_tags($text,$tags);
1230 // Time to cycle through each phrase to be linked
1231 $size = sizeof($link_array);
1232 for ($n=0; $n < $size; $n++
) {
1233 $linkobject =& $link_array[$n];
1235 // Set some defaults if certain properties are missing
1236 // Properties may be missing if the filterobject class has not been used to construct the object
1237 if (empty($linkobject->phrase
)) {
1241 // Avoid integers < 1000 to be linked. See bug 1446.
1242 $intcurrent = intval($linkobject->phrase
);
1243 if (!empty($intcurrent) && strval($intcurrent) == $linkobject->phrase
&& $intcurrent < 1000) {
1247 // All this work has to be done ONLY it it hasn't been done before
1248 if (!$linkobject->work_calculated
) {
1249 if (!isset($linkobject->hreftagbegin
) or !isset($linkobject->hreftagend
)) {
1250 $linkobject->work_hreftagbegin
= '<span class="highlight"';
1251 $linkobject->work_hreftagend
= '</span>';
1253 $linkobject->work_hreftagbegin
= $linkobject->hreftagbegin
;
1254 $linkobject->work_hreftagend
= $linkobject->hreftagend
;
1257 // Double up chars to protect true duplicates
1258 // be cleared up before returning to the user.
1259 $linkobject->work_hreftagbegin
= preg_replace('/([#*%])/','\1\1',$linkobject->work_hreftagbegin
);
1261 if (empty($linkobject->casesensitive
)) {
1262 $linkobject->work_casesensitive
= false;
1264 $linkobject->work_casesensitive
= true;
1266 if (empty($linkobject->fullmatch
)) {
1267 $linkobject->work_fullmatch
= false;
1269 $linkobject->work_fullmatch
= true;
1272 // Strip tags out of the phrase
1273 $linkobject->work_phrase
= strip_tags($linkobject->phrase
);
1275 // Double up chars that might cause a false match -- the duplicates will
1276 // be cleared up before returning to the user.
1277 $linkobject->work_phrase
= preg_replace('/([#*%])/','\1\1',$linkobject->work_phrase
);
1279 // Set the replacement phrase properly
1280 if ($linkobject->replacementphrase
) { //We have specified a replacement phrase
1282 $linkobject->work_replacementphrase
= strip_tags($linkobject->replacementphrase
);
1283 } else { //The replacement is the original phrase as matched below
1284 $linkobject->work_replacementphrase
= '$1';
1287 // Quote any regular expression characters and the delimiter in the work phrase to be searched
1288 $linkobject->work_phrase
= preg_quote($linkobject->work_phrase
, '/');
1291 $linkobject->work_calculated
= true;
1295 // If $CFG->filtermatchoneperpage, avoid previously (request) linked phrases
1296 if (!empty($CFG->filtermatchoneperpage
)) {
1297 if (!empty($usedphrases) && in_array($linkobject->work_phrase
,$usedphrases)) {
1302 // Regular expression modifiers
1303 $modifiers = ($linkobject->work_casesensitive
) ?
's' : 'isu'; // works in unicode mode!
1305 // Do we need to do a fullmatch?
1306 // If yes then go through and remove any non full matching entries
1307 if ($linkobject->work_fullmatch
) {
1308 $notfullmatches = array();
1309 $regexp = '/'.$filterinvalidprefixes.'('.$linkobject->work_phrase
.')|('.$linkobject->work_phrase
.')'.$filterinvalidsuffixes.'/'.$modifiers;
1311 preg_match_all($regexp,$text,$list_of_notfullmatches);
1313 if ($list_of_notfullmatches) {
1314 foreach (array_unique($list_of_notfullmatches[0]) as $key=>$value) {
1315 $notfullmatches['<*'.$key.'*>'] = $value;
1317 if (!empty($notfullmatches)) {
1318 $text = str_replace($notfullmatches,array_keys($notfullmatches),$text);
1323 // Finally we do our highlighting
1324 if (!empty($CFG->filtermatchonepertext
) ||
!empty($CFG->filtermatchoneperpage
)) {
1325 $resulttext = preg_replace('/('.$linkobject->work_phrase
.')/'.$modifiers,
1326 $linkobject->work_hreftagbegin
.
1327 $linkobject->work_replacementphrase
.
1328 $linkobject->work_hreftagend
, $text, 1);
1330 $resulttext = preg_replace('/('.$linkobject->work_phrase
.')/'.$modifiers,
1331 $linkobject->work_hreftagbegin
.
1332 $linkobject->work_replacementphrase
.
1333 $linkobject->work_hreftagend
, $text);
1337 // If the text has changed we have to look for links again
1338 if ($resulttext != $text) {
1339 // Set $text to $resulttext
1340 $text = $resulttext;
1341 // Remove everything enclosed by the ignore tags from $text
1342 filter_save_ignore_tags($text,$filterignoretagsopen,$filterignoretagsclose,$ignoretags);
1343 // Remove tags from $text
1344 filter_save_tags($text,$tags);
1345 // If $CFG->filtermatchoneperpage, save linked phrases to request
1346 if (!empty($CFG->filtermatchoneperpage
)) {
1347 $usedphrases[] = $linkobject->work_phrase
;
1352 // Replace the not full matches before cycling to next link object
1353 if (!empty($notfullmatches)) {
1354 $text = str_replace(array_keys($notfullmatches),$notfullmatches,$text);
1355 unset($notfullmatches);
1359 // Rebuild the text with all the excluded areas
1361 if (!empty($tags)) {
1362 $text = str_replace(array_keys($tags), $tags, $text);
1365 if (!empty($ignoretags)) {
1366 $ignoretags = array_reverse($ignoretags); // Reversed so "progressive" str_replace() will solve some nesting problems.
1367 $text = str_replace(array_keys($ignoretags),$ignoretags,$text);
1370 // Remove the protective doubleups
1371 $text = preg_replace('/([#*%])(\1)/','\1',$text);
1373 // Add missing javascript for popus
1374 $text = filter_add_javascript($text);
1381 * @todo Document this function
1382 * @param array $linkarray
1385 function filter_remove_duplicates($linkarray) {
1387 $concepts = array(); // keep a record of concepts as we cycle through
1388 $lconcepts = array(); // a lower case version for case insensitive
1390 $cleanlinks = array();
1392 foreach ($linkarray as $key=>$filterobject) {
1393 if ($filterobject->casesensitive
) {
1394 $exists = in_array($filterobject->phrase
, $concepts);
1396 $exists = in_array(core_text
::strtolower($filterobject->phrase
), $lconcepts);
1400 $cleanlinks[] = $filterobject;
1401 $concepts[] = $filterobject->phrase
;
1402 $lconcepts[] = core_text
::strtolower($filterobject->phrase
);
1410 * Extract open/lose tags and their contents to avoid being processed by filters.
1411 * Useful to extract pieces of code like <a>...</a> tags. It returns the text
1412 * converted with some <#xTEXTFILTER_EXCL_SEPARATORx#> codes replacing the extracted text. Such extracted
1413 * texts are returned in the ignoretags array (as values), with codes as keys.
1415 * @param string $text the text that we are filtering (in/out)
1416 * @param array $filterignoretagsopen an array of open tags to start searching
1417 * @param array $filterignoretagsclose an array of close tags to end searching
1418 * @param array $ignoretags an array of saved strings useful to rebuild the original text (in/out)
1420 function filter_save_ignore_tags(&$text, $filterignoretagsopen, $filterignoretagsclose, &$ignoretags) {
1422 // Remove everything enclosed by the ignore tags from $text
1423 foreach ($filterignoretagsopen as $ikey=>$opentag) {
1424 $closetag = $filterignoretagsclose[$ikey];
1425 // form regular expression
1426 $opentag = str_replace('/','\/',$opentag); // delimit forward slashes
1427 $closetag = str_replace('/','\/',$closetag); // delimit forward slashes
1428 $pregexp = '/'.$opentag.'(.*?)'.$closetag.'/is';
1430 preg_match_all($pregexp, $text, $list_of_ignores);
1431 foreach (array_unique($list_of_ignores[0]) as $key=>$value) {
1432 $prefix = (string)(count($ignoretags) +
1);
1433 $ignoretags['<#'.$prefix.TEXTFILTER_EXCL_SEPARATOR
.$key.'#>'] = $value;
1435 if (!empty($ignoretags)) {
1436 $text = str_replace($ignoretags,array_keys($ignoretags),$text);
1442 * Extract tags (any text enclosed by < and > to avoid being processed by filters.
1443 * It returns the text converted with some <%xTEXTFILTER_EXCL_SEPARATORx%> codes replacing the extracted text. Such extracted
1444 * texts are returned in the tags array (as values), with codes as keys.
1446 * @param string $text the text that we are filtering (in/out)
1447 * @param array $tags an array of saved strings useful to rebuild the original text (in/out)
1449 function filter_save_tags(&$text, &$tags) {
1451 preg_match_all('/<([^#%*].*?)>/is',$text,$list_of_newtags);
1452 foreach (array_unique($list_of_newtags[0]) as $ntkey=>$value) {
1453 $prefix = (string)(count($tags) +
1);
1454 $tags['<%'.$prefix.TEXTFILTER_EXCL_SEPARATOR
.$ntkey.'%>'] = $value;
1456 if (!empty($tags)) {
1457 $text = str_replace($tags,array_keys($tags),$text);
1462 * Add missing openpopup javascript to HTML files.
1464 * @param string $text
1467 function filter_add_javascript($text) {
1470 if (stripos($text, '</html>') === FALSE) {
1471 return $text; // This is not a html file.
1473 if (strpos($text, 'onclick="return openpopup') === FALSE) {
1474 return $text; // No popup - no need to add javascript.
1477 <script type=\"text/javascript\">
1479 function openpopup(url,name,options,fullscreen) {
1480 fullurl = \"".$CFG->httpswwwroot
."\" + url;
1481 windowobj = window.open(fullurl,name,options);
1483 windowobj.moveTo(0,0);
1484 windowobj.resizeTo(screen.availWidth,screen.availHeight);
1491 if (stripos($text, '</head>') !== FALSE) {
1492 // Try to add it into the head element.
1493 $text = str_ireplace('</head>', $js.'</head>', $text);
1497 // Last chance - try adding head element.
1498 return preg_replace("/<html.*?>/is", "\\0<head>".$js.'</head>', $text);