Merge branch 'MDL-44952-30' of git://github.com/marinaglancy/moodle into MOODLE_30_STABLE
[moodle.git] / lib / filterlib.php
blob0c2343105a9c4aa0545a13d31127d9cea16659ce
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Library functions for managing text filter plugins.
20 * @package core
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);
36 /**
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', '-%-');
44 /**
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 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
52 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
54 class filter_manager {
55 /**
56 * @var moodle_text_filter[][] This list of active filters, by context, for filtering content.
57 * An array contextid => ordered array of filter name => filter objects.
59 protected $textfilters = array();
61 /**
62 * @var moodle_text_filter[][] This list of active filters, by context, for filtering strings.
63 * An array contextid => ordered array of filter name => filter objects.
65 protected $stringfilters = array();
67 /** @var array Exploded version of $CFG->stringfilters. */
68 protected $stringfilternames = array();
70 /** @var filter_manager Holds the singleton instance. */
71 protected static $singletoninstance;
73 /**
74 * Constructor. Protected. Use {@link instance()} instead.
76 protected function __construct() {
77 $this->stringfilternames = filter_get_string_filters();
80 /**
81 * Factory method. Use this to get the filter manager.
83 * @return filter_manager the singleton instance.
85 public static function instance() {
86 global $CFG;
87 if (is_null(self::$singletoninstance)) {
88 if (!empty($CFG->perfdebug) and $CFG->perfdebug > 7) {
89 self::$singletoninstance = new performance_measuring_filter_manager();
90 } else {
91 self::$singletoninstance = new self();
94 return self::$singletoninstance;
97 /**
98 * Resets the caches, usually to be called between unit tests
100 public static function reset_caches() {
101 if (self::$singletoninstance) {
102 self::$singletoninstance->unload_all_filters();
104 self::$singletoninstance = null;
108 * Unloads all filters and other cached information
110 protected function unload_all_filters() {
111 $this->textfilters = array();
112 $this->stringfilters = array();
113 $this->stringfilternames = array();
117 * Load all the filters required by this context.
119 * @param context $context the context.
121 protected function load_filters($context) {
122 $filters = filter_get_active_in_context($context);
123 $this->textfilters[$context->id] = array();
124 $this->stringfilters[$context->id] = array();
125 foreach ($filters as $filtername => $localconfig) {
126 $filter = $this->make_filter_object($filtername, $context, $localconfig);
127 if (is_null($filter)) {
128 continue;
130 $this->textfilters[$context->id][$filtername] = $filter;
131 if (in_array($filtername, $this->stringfilternames)) {
132 $this->stringfilters[$context->id][$filtername] = $filter;
138 * Factory method for creating a filter.
140 * @param string $filtername The filter name, for example 'tex'.
141 * @param context $context context object.
142 * @param array $localconfig array of local configuration variables for this filter.
143 * @return moodle_text_filter The filter, or null, if this type of filter is
144 * not recognised or could not be created.
146 protected function make_filter_object($filtername, $context, $localconfig) {
147 global $CFG;
148 $path = $CFG->dirroot .'/filter/'. $filtername .'/filter.php';
149 if (!is_readable($path)) {
150 return null;
152 include_once($path);
154 $filterclassname = 'filter_' . $filtername;
155 if (class_exists($filterclassname)) {
156 return new $filterclassname($context, $localconfig);
159 return null;
163 * Apply a list of filters to some content.
164 * @param string $text
165 * @param moodle_text_filter[] $filterchain array filter name => filter object.
166 * @param array $options options passed to the filters.
167 * @param array $skipfilters of filter names. Any filters that should not be applied to this text.
168 * @return string $text
170 protected function apply_filter_chain($text, $filterchain, array $options = array(),
171 array $skipfilters = null) {
172 foreach ($filterchain as $filtername => $filter) {
173 if ($skipfilters !== null && in_array($filtername, $skipfilters)) {
174 continue;
176 $text = $filter->filter($text, $options);
178 return $text;
182 * Get all the filters that apply to a given context for calls to format_text.
184 * @param context $context
185 * @return moodle_text_filter[] A text filter
187 protected function get_text_filters($context) {
188 if (!isset($this->textfilters[$context->id])) {
189 $this->load_filters($context);
191 return $this->textfilters[$context->id];
195 * Get all the filters that apply to a given context for calls to format_string.
197 * @param context $context the context.
198 * @return moodle_text_filter[] A text filter
200 protected function get_string_filters($context) {
201 if (!isset($this->stringfilters[$context->id])) {
202 $this->load_filters($context);
204 return $this->stringfilters[$context->id];
208 * Filter some text
210 * @param string $text The text to filter
211 * @param context $context the context.
212 * @param array $options options passed to the filters
213 * @param array $skipfilters of filter names. Any filters that should not be applied to this text.
214 * @return string resulting text
216 public function filter_text($text, $context, array $options = array(),
217 array $skipfilters = null) {
218 $text = $this->apply_filter_chain($text, $this->get_text_filters($context), $options, $skipfilters);
219 // <nolink> tags removed for XHTML compatibility
220 $text = str_replace(array('<nolink>', '</nolink>'), '', $text);
221 return $text;
225 * Filter a piece of string
227 * @param string $string The text to filter
228 * @param context $context the context.
229 * @return string resulting string
231 public function filter_string($string, $context) {
232 return $this->apply_filter_chain($string, $this->get_string_filters($context));
236 * @deprecated Since Moodle 3.0 MDL-50491. This was used by the old text filtering system, but no more.
237 * @todo MDL-50632 This will be deleted in Moodle 3.2.
238 * @param context $context the context.
239 * @return string the hash.
241 public function text_filtering_hash($context) {
242 debugging('filter_manager::text_filtering_hash() is deprecated. ' .
243 'It was an internal part of the old format_text caching, ' .
244 'and should not have been called from other code.', DEBUG_DEVELOPER);
245 $filters = $this->get_text_filters($context);
246 $hashes = array();
247 foreach ($filters as $filter) {
248 $hashes[] = $filter->hash();
250 return implode('-', $hashes);
254 * Setup page with filters requirements and other prepare stuff.
256 * This method is used by {@see format_text()} and {@see format_string()}
257 * in order to allow filters to setup any page requirement (js, css...)
258 * or perform any action needed to get them prepared before filtering itself
259 * happens by calling to each every active setup() method.
261 * Note it's executed for each piece of text filtered, so filter implementations
262 * are responsible of controlling the cardinality of the executions that may
263 * be different depending of the stuff to prepare.
265 * @param moodle_page $page the page we are going to add requirements to.
266 * @param context $context the context which contents are going to be filtered.
267 * @since Moodle 2.3
269 public function setup_page_for_filters($page, $context) {
270 $filters = $this->get_text_filters($context);
271 foreach ($filters as $filter) {
272 $filter->setup($page, $context);
279 * Filter manager subclass that does nothing. Having this simplifies the logic
280 * of format_text, etc.
282 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
283 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
285 class null_filter_manager {
286 public function filter_text($text, $context, array $options = array(),
287 array $skipfilters = null) {
288 return $text;
291 public function filter_string($string, $context) {
292 return $string;
295 public function text_filtering_hash() {
296 debugging('filter_manager::text_filtering_hash() is deprecated. ' .
297 'It was an internal part of the old format_text caching, ' .
298 'and should not have been called from other code.', DEBUG_DEVELOPER);
299 return '';
305 * Filter manager subclass that tracks how much work it does.
307 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
308 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
310 class performance_measuring_filter_manager extends filter_manager {
311 /** @var int number of filter objects created. */
312 protected $filterscreated = 0;
314 /** @var int number of calls to filter_text. */
315 protected $textsfiltered = 0;
317 /** @var int number of calls to filter_string. */
318 protected $stringsfiltered = 0;
320 protected function unload_all_filters() {
321 parent::unload_all_filters();
322 $this->filterscreated = 0;
323 $this->textsfiltered = 0;
324 $this->stringsfiltered = 0;
327 protected function make_filter_object($filtername, $context, $localconfig) {
328 $this->filterscreated++;
329 return parent::make_filter_object($filtername, $context, $localconfig);
332 public function filter_text($text, $context, array $options = array(),
333 array $skipfilters = null) {
334 $this->textsfiltered++;
335 return parent::filter_text($text, $context, $options, $skipfilters);
338 public function filter_string($string, $context) {
339 $this->stringsfiltered++;
340 return parent::filter_string($string, $context);
344 * Return performance information, in the form required by {@link get_performance_info()}.
345 * @return array the performance info.
347 public function get_performance_summary() {
348 return array(array(
349 'contextswithfilters' => count($this->textfilters),
350 'filterscreated' => $this->filterscreated,
351 'textsfiltered' => $this->textsfiltered,
352 'stringsfiltered' => $this->stringsfiltered,
353 ), array(
354 'contextswithfilters' => 'Contexts for which filters were loaded',
355 'filterscreated' => 'Filters created',
356 'textsfiltered' => 'Pieces of content filtered',
357 'stringsfiltered' => 'Strings filtered',
364 * Base class for text filters. You just need to override this class and
365 * implement the filter method.
367 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
368 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
370 abstract class moodle_text_filter {
371 /** @var context The context we are in. */
372 protected $context;
374 /** @var array Any local configuration for this filter in this context. */
375 protected $localconfig;
378 * Set any context-specific configuration for this filter.
380 * @param context $context The current context.
381 * @param array $localconfig Any context-specific configuration for this filter.
383 public function __construct($context, array $localconfig) {
384 $this->context = $context;
385 $this->localconfig = $localconfig;
389 * @deprecated Since Moodle 3.0 MDL-50491. This was used by the old text filtering system, but no more.
390 * @todo MDL-50632 This will be deleted in Moodle 3.2.
391 * @return string The class name of the current class
393 public function hash() {
394 debugging('moodle_text_filter::hash() is deprecated. ' .
395 'It was an internal part of the old format_text caching, ' .
396 'and should not have been called from other code.', DEBUG_DEVELOPER);
397 return __CLASS__;
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.
412 * @since Moodle 2.3
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());
430 * This is just a little object to define a phrase and some instructions
431 * for how to process it. Filters can create an array of these to pass
432 * to the filter_phrases function below.
434 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
435 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
437 class filterobject {
438 /** @var string */
439 var $phrase;
440 var $hreftagbegin;
441 var $hreftagend;
442 /** @var bool */
443 var $casesensitive;
444 var $fullmatch;
445 /** @var mixed */
446 var $replacementphrase;
447 var $work_phrase;
448 var $work_hreftagbegin;
449 var $work_hreftagend;
450 var $work_casesensitive;
451 var $work_fullmatch;
452 var $work_replacementphrase;
453 /** @var bool */
454 var $work_calculated;
457 * A constructor just because I like constructing
459 * @param string $phrase
460 * @param string $hreftagbegin
461 * @param string $hreftagend
462 * @param bool $casesensitive
463 * @param bool $fullmatch
464 * @param mixed $replacementphrase
466 public function __construct($phrase, $hreftagbegin = '<span class="highlight">',
467 $hreftagend = '</span>',
468 $casesensitive = false,
469 $fullmatch = false,
470 $replacementphrase = NULL) {
472 $this->phrase = $phrase;
473 $this->hreftagbegin = $hreftagbegin;
474 $this->hreftagend = $hreftagend;
475 $this->casesensitive = $casesensitive;
476 $this->fullmatch = $fullmatch;
477 $this->replacementphrase= $replacementphrase;
478 $this->work_calculated = false;
484 * Look up the name of this filter
486 * @param string $filter the filter name
487 * @return string the human-readable name for this filter.
489 function filter_get_name($filter) {
490 if (strpos($filter, 'filter/') === 0) {
491 debugging("Old '$filter'' parameter used in filter_get_name()");
492 $filter = substr($filter, 7);
493 } else if (strpos($filter, '/') !== false) {
494 throw new coding_exception('Unknown filter type ' . $filter);
497 if (get_string_manager()->string_exists('filtername', 'filter_' . $filter)) {
498 return get_string('filtername', 'filter_' . $filter);
499 } else {
500 return $filter;
505 * Get the names of all the filters installed in this Moodle.
507 * @return array path => filter name from the appropriate lang file. e.g.
508 * array('tex' => 'TeX Notation');
509 * sorted in alphabetical order of name.
511 function filter_get_all_installed() {
512 global $CFG;
514 $filternames = array();
515 foreach (core_component::get_plugin_list('filter') as $filter => $fulldir) {
516 if (is_readable("$fulldir/filter.php")) {
517 $filternames[$filter] = filter_get_name($filter);
520 core_collator::asort($filternames);
521 return $filternames;
525 * Set the global activated state for a text filter.
527 * @param string $filtername The filter name, for example 'tex'.
528 * @param int $state One of the values TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_DISABLED.
529 * @param int $move -1 means up, 0 means the same, 1 means down
531 function filter_set_global_state($filtername, $state, $move = 0) {
532 global $DB;
534 // Check requested state is valid.
535 if (!in_array($state, array(TEXTFILTER_ON, TEXTFILTER_OFF, TEXTFILTER_DISABLED))) {
536 throw new coding_exception("Illegal option '$state' passed to filter_set_global_state. " .
537 "Must be one of TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_DISABLED.");
540 if ($move > 0) {
541 $move = 1;
542 } else if ($move < 0) {
543 $move = -1;
546 if (strpos($filtername, 'filter/') === 0) {
547 //debugging("Old filtername '$filtername' parameter used in filter_set_global_state()", DEBUG_DEVELOPER);
548 $filtername = substr($filtername, 7);
549 } else if (strpos($filtername, '/') !== false) {
550 throw new coding_exception("Invalid filter name '$filtername' used in filter_set_global_state()");
553 $transaction = $DB->start_delegated_transaction();
555 $syscontext = context_system::instance();
556 $filters = $DB->get_records('filter_active', array('contextid' => $syscontext->id), 'sortorder ASC');
558 $on = array();
559 $off = array();
561 foreach($filters as $f) {
562 if ($f->active == TEXTFILTER_DISABLED) {
563 $off[$f->filter] = $f;
564 } else {
565 $on[$f->filter] = $f;
569 // Update the state or add new record.
570 if (isset($on[$filtername])) {
571 $filter = $on[$filtername];
572 if ($filter->active != $state) {
573 add_to_config_log('filter_active', $filter->active, $state, $filtername);
575 $filter->active = $state;
576 $DB->update_record('filter_active', $filter);
577 if ($filter->active == TEXTFILTER_DISABLED) {
578 unset($on[$filtername]);
579 $off = array($filter->filter => $filter) + $off;
584 } else if (isset($off[$filtername])) {
585 $filter = $off[$filtername];
586 if ($filter->active != $state) {
587 add_to_config_log('filter_active', $filter->active, $state, $filtername);
589 $filter->active = $state;
590 $DB->update_record('filter_active', $filter);
591 if ($filter->active != TEXTFILTER_DISABLED) {
592 unset($off[$filtername]);
593 $on[$filter->filter] = $filter;
597 } else {
598 add_to_config_log('filter_active', '', $state, $filtername);
600 $filter = new stdClass();
601 $filter->filter = $filtername;
602 $filter->contextid = $syscontext->id;
603 $filter->active = $state;
604 $filter->sortorder = 99999;
605 $filter->id = $DB->insert_record('filter_active', $filter);
607 $filters[$filter->id] = $filter;
608 if ($state == TEXTFILTER_DISABLED) {
609 $off[$filter->filter] = $filter;
610 } else {
611 $on[$filter->filter] = $filter;
615 // Move only active.
616 if ($move != 0 and isset($on[$filter->filter])) {
617 $i = 1;
618 foreach ($on as $f) {
619 $f->newsortorder = $i;
620 $i++;
623 $filter->newsortorder = $filter->newsortorder + $move;
625 foreach ($on as $f) {
626 if ($f->id == $filter->id) {
627 continue;
629 if ($f->newsortorder == $filter->newsortorder) {
630 if ($move == 1) {
631 $f->newsortorder = $f->newsortorder - 1;
632 } else {
633 $f->newsortorder = $f->newsortorder + 1;
638 core_collator::asort_objects_by_property($on, 'newsortorder', core_collator::SORT_NUMERIC);
641 // Inactive are sorted by filter name.
642 core_collator::asort_objects_by_property($off, 'filter', core_collator::SORT_NATURAL);
644 // Update records if necessary.
645 $i = 1;
646 foreach ($on as $f) {
647 if ($f->sortorder != $i) {
648 $DB->set_field('filter_active', 'sortorder', $i, array('id'=>$f->id));
650 $i++;
652 foreach ($off as $f) {
653 if ($f->sortorder != $i) {
654 $DB->set_field('filter_active', 'sortorder', $i, array('id'=>$f->id));
656 $i++;
659 $transaction->allow_commit();
663 * @param string $filtername The filter name, for example 'tex'.
664 * @return boolean is this filter allowed to be used on this site. That is, the
665 * admin has set the global 'active' setting to On, or Off, but available.
667 function filter_is_enabled($filtername) {
668 if (strpos($filtername, 'filter/') === 0) {
669 //debugging("Old filtername '$filtername' parameter used in filter_is_enabled()", DEBUG_DEVELOPER);
670 $filtername = substr($filtername, 7);
671 } else if (strpos($filtername, '/') !== false) {
672 throw new coding_exception("Invalid filter name '$filtername' used in filter_is_enabled()");
674 return array_key_exists($filtername, filter_get_globally_enabled());
678 * Return a list of all the filters that may be in use somewhere.
680 * @staticvar array $enabledfilters
681 * @return array where the keys and values are both the filter name, like 'tex'.
683 function filter_get_globally_enabled() {
684 static $enabledfilters = null;
685 if (is_null($enabledfilters)) {
686 $filters = filter_get_global_states();
687 $enabledfilters = array();
688 foreach ($filters as $filter => $filerinfo) {
689 if ($filerinfo->active != TEXTFILTER_DISABLED) {
690 $enabledfilters[$filter] = $filter;
694 return $enabledfilters;
698 * Return the names of the filters that should also be applied to strings
699 * (when they are enabled).
701 * @return array where the keys and values are both the filter name, like 'tex'.
703 function filter_get_string_filters() {
704 global $CFG;
705 $stringfilters = array();
706 if (!empty($CFG->filterall) && !empty($CFG->stringfilters)) {
707 $stringfilters = explode(',', $CFG->stringfilters);
708 $stringfilters = array_combine($stringfilters, $stringfilters);
710 return $stringfilters;
714 * Sets whether a particular active filter should be applied to all strings by
715 * format_string, or just used by format_text.
717 * @param string $filter The filter name, for example 'tex'.
718 * @param boolean $applytostrings if true, this filter will apply to format_string
719 * and format_text, when it is enabled.
721 function filter_set_applies_to_strings($filter, $applytostrings) {
722 $stringfilters = filter_get_string_filters();
723 $prevfilters = $stringfilters;
724 $allfilters = core_component::get_plugin_list('filter');
726 if ($applytostrings) {
727 $stringfilters[$filter] = $filter;
728 } else {
729 unset($stringfilters[$filter]);
732 // Remove missing filters.
733 foreach ($stringfilters as $filter) {
734 if (!isset($allfilters[$filter])) {
735 unset($stringfilters[$filter]);
739 if ($prevfilters != $stringfilters) {
740 set_config('stringfilters', implode(',', $stringfilters));
741 set_config('filterall', !empty($stringfilters));
746 * Set the local activated state for a text filter.
748 * @param string $filter The filter name, for example 'tex'.
749 * @param integer $contextid The id of the context to get the local config for.
750 * @param integer $state One of the values TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_INHERIT.
751 * @return void
753 function filter_set_local_state($filter, $contextid, $state) {
754 global $DB;
756 // Check requested state is valid.
757 if (!in_array($state, array(TEXTFILTER_ON, TEXTFILTER_OFF, TEXTFILTER_INHERIT))) {
758 throw new coding_exception("Illegal option '$state' passed to filter_set_local_state. " .
759 "Must be one of TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_INHERIT.");
762 if ($contextid == context_system::instance()->id) {
763 throw new coding_exception('You cannot use filter_set_local_state ' .
764 'with $contextid equal to the system context id.');
767 if ($state == TEXTFILTER_INHERIT) {
768 $DB->delete_records('filter_active', array('filter' => $filter, 'contextid' => $contextid));
769 return;
772 $rec = $DB->get_record('filter_active', array('filter' => $filter, 'contextid' => $contextid));
773 $insert = false;
774 if (empty($rec)) {
775 $insert = true;
776 $rec = new stdClass;
777 $rec->filter = $filter;
778 $rec->contextid = $contextid;
781 $rec->active = $state;
783 if ($insert) {
784 $DB->insert_record('filter_active', $rec);
785 } else {
786 $DB->update_record('filter_active', $rec);
791 * Set a particular local config variable for a filter in a context.
793 * @param string $filter The filter name, for example 'tex'.
794 * @param integer $contextid The id of the context to get the local config for.
795 * @param string $name the setting name.
796 * @param string $value the corresponding value.
798 function filter_set_local_config($filter, $contextid, $name, $value) {
799 global $DB;
800 $rec = $DB->get_record('filter_config', array('filter' => $filter, 'contextid' => $contextid, 'name' => $name));
801 $insert = false;
802 if (empty($rec)) {
803 $insert = true;
804 $rec = new stdClass;
805 $rec->filter = $filter;
806 $rec->contextid = $contextid;
807 $rec->name = $name;
810 $rec->value = $value;
812 if ($insert) {
813 $DB->insert_record('filter_config', $rec);
814 } else {
815 $DB->update_record('filter_config', $rec);
820 * Remove a particular local config variable for a filter in a context.
822 * @param string $filter The filter name, for example 'tex'.
823 * @param integer $contextid The id of the context to get the local config for.
824 * @param string $name the setting name.
826 function filter_unset_local_config($filter, $contextid, $name) {
827 global $DB;
828 $DB->delete_records('filter_config', array('filter' => $filter, 'contextid' => $contextid, 'name' => $name));
832 * Get local config variables for a filter in a context. Normally (when your
833 * filter is running) you don't need to call this, becuase the config is fetched
834 * for you automatically. You only need this, for example, when you are getting
835 * the config so you can show the user an editing from.
837 * @param string $filter The filter name, for example 'tex'.
838 * @param integer $contextid The ID of the context to get the local config for.
839 * @return array of name => value pairs.
841 function filter_get_local_config($filter, $contextid) {
842 global $DB;
843 return $DB->get_records_menu('filter_config', array('filter' => $filter, 'contextid' => $contextid), '', 'name,value');
847 * This function is for use by backup. Gets all the filter information specific
848 * to one context.
850 * @param int $contextid
851 * @return array Array with two elements. The first element is an array of objects with
852 * fields filter and active. These come from the filter_active table. The
853 * second element is an array of objects with fields filter, name and value
854 * from the filter_config table.
856 function filter_get_all_local_settings($contextid) {
857 global $DB;
858 return array(
859 $DB->get_records('filter_active', array('contextid' => $contextid), 'filter', 'filter,active'),
860 $DB->get_records('filter_config', array('contextid' => $contextid), 'filter,name', 'filter,name,value'),
865 * Get the list of active filters, in the order that they should be used
866 * for a particular context, along with any local configuration variables.
868 * @param context $context a context
869 * @return array an array where the keys are the filter names, for example
870 * 'tex' and the values are any local
871 * configuration for that filter, as an array of name => value pairs
872 * from the filter_config table. In a lot of cases, this will be an
873 * empty array. So, an example return value for this function might be
874 * array(tex' => array())
876 function filter_get_active_in_context($context) {
877 global $DB, $FILTERLIB_PRIVATE;
879 if (!isset($FILTERLIB_PRIVATE)) {
880 $FILTERLIB_PRIVATE = new stdClass();
883 // Use cache (this is a within-request cache only) if available. See
884 // function filter_preload_activities.
885 if (isset($FILTERLIB_PRIVATE->active) &&
886 array_key_exists($context->id, $FILTERLIB_PRIVATE->active)) {
887 return $FILTERLIB_PRIVATE->active[$context->id];
890 $contextids = str_replace('/', ',', trim($context->path, '/'));
892 // The following SQL is tricky. It is explained on
893 // http://docs.moodle.org/dev/Filter_enable/disable_by_context
894 $sql = "SELECT active.filter, fc.name, fc.value
895 FROM (SELECT f.filter, MAX(f.sortorder) AS sortorder
896 FROM {filter_active} f
897 JOIN {context} ctx ON f.contextid = ctx.id
898 WHERE ctx.id IN ($contextids)
899 GROUP BY filter
900 HAVING MAX(f.active * ctx.depth) > -MIN(f.active * ctx.depth)
901 ) active
902 LEFT JOIN {filter_config} fc ON fc.filter = active.filter AND fc.contextid = $context->id
903 ORDER BY active.sortorder";
904 $rs = $DB->get_recordset_sql($sql);
906 // Massage the data into the specified format to return.
907 $filters = array();
908 foreach ($rs as $row) {
909 if (!isset($filters[$row->filter])) {
910 $filters[$row->filter] = array();
912 if (!is_null($row->name)) {
913 $filters[$row->filter][$row->name] = $row->value;
917 $rs->close();
919 return $filters;
923 * Preloads the list of active filters for all activities (modules) on the course
924 * using two database queries.
926 * @param course_modinfo $modinfo Course object from get_fast_modinfo
928 function filter_preload_activities(course_modinfo $modinfo) {
929 global $DB, $FILTERLIB_PRIVATE;
931 if (!isset($FILTERLIB_PRIVATE)) {
932 $FILTERLIB_PRIVATE = new stdClass();
935 // Don't repeat preload
936 if (!isset($FILTERLIB_PRIVATE->preloaded)) {
937 $FILTERLIB_PRIVATE->preloaded = array();
939 if (!empty($FILTERLIB_PRIVATE->preloaded[$modinfo->get_course_id()])) {
940 return;
942 $FILTERLIB_PRIVATE->preloaded[$modinfo->get_course_id()] = true;
944 // Get contexts for all CMs
945 $cmcontexts = array();
946 $cmcontextids = array();
947 foreach ($modinfo->get_cms() as $cm) {
948 $modulecontext = context_module::instance($cm->id);
949 $cmcontextids[] = $modulecontext->id;
950 $cmcontexts[] = $modulecontext;
953 // Get course context and all other parents...
954 $coursecontext = context_course::instance($modinfo->get_course_id());
955 $parentcontextids = explode('/', substr($coursecontext->path, 1));
956 $allcontextids = array_merge($cmcontextids, $parentcontextids);
958 // Get all filter_active rows relating to all these contexts
959 list ($sql, $params) = $DB->get_in_or_equal($allcontextids);
960 $filteractives = $DB->get_records_select('filter_active', "contextid $sql", $params);
962 // Get all filter_config only for the cm contexts
963 list ($sql, $params) = $DB->get_in_or_equal($cmcontextids);
964 $filterconfigs = $DB->get_records_select('filter_config', "contextid $sql", $params);
966 // Note: I was a bit surprised that filter_config only works for the
967 // most specific context (i.e. it does not need to be checked for course
968 // context if we only care about CMs) however basede on code in
969 // filter_get_active_in_context, this does seem to be correct.
971 // Build course default active list. Initially this will be an array of
972 // filter name => active score (where an active score >0 means it's active)
973 $courseactive = array();
975 // Also build list of filter_active rows below course level, by contextid
976 $remainingactives = array();
978 // Array lists filters that are banned at top level
979 $banned = array();
981 // Add any active filters in parent contexts to the array
982 foreach ($filteractives as $row) {
983 $depth = array_search($row->contextid, $parentcontextids);
984 if ($depth !== false) {
985 // Find entry
986 if (!array_key_exists($row->filter, $courseactive)) {
987 $courseactive[$row->filter] = 0;
989 // This maths copes with reading rows in any order. Turning on/off
990 // at site level counts 1, at next level down 4, at next level 9,
991 // then 16, etc. This means the deepest level always wins, except
992 // against the -9999 at top level.
993 $courseactive[$row->filter] +=
994 ($depth + 1) * ($depth + 1) * $row->active;
996 if ($row->active == TEXTFILTER_DISABLED) {
997 $banned[$row->filter] = true;
999 } else {
1000 // Build list of other rows indexed by contextid
1001 if (!array_key_exists($row->contextid, $remainingactives)) {
1002 $remainingactives[$row->contextid] = array();
1004 $remainingactives[$row->contextid][] = $row;
1008 // Chuck away the ones that aren't active.
1009 foreach ($courseactive as $filter=>$score) {
1010 if ($score <= 0) {
1011 unset($courseactive[$filter]);
1012 } else {
1013 $courseactive[$filter] = array();
1017 // Loop through the contexts to reconstruct filter_active lists for each
1018 // cm on the course.
1019 if (!isset($FILTERLIB_PRIVATE->active)) {
1020 $FILTERLIB_PRIVATE->active = array();
1022 foreach ($cmcontextids as $contextid) {
1023 // Copy course list
1024 $FILTERLIB_PRIVATE->active[$contextid] = $courseactive;
1026 // Are there any changes to the active list?
1027 if (array_key_exists($contextid, $remainingactives)) {
1028 foreach ($remainingactives[$contextid] as $row) {
1029 if ($row->active > 0 && empty($banned[$row->filter])) {
1030 // If it's marked active for specific context, add entry
1031 // (doesn't matter if one exists already).
1032 $FILTERLIB_PRIVATE->active[$contextid][$row->filter] = array();
1033 } else {
1034 // If it's marked inactive, remove entry (doesn't matter
1035 // if it doesn't exist).
1036 unset($FILTERLIB_PRIVATE->active[$contextid][$row->filter]);
1042 // Process all config rows to add config data to these entries.
1043 foreach ($filterconfigs as $row) {
1044 if (isset($FILTERLIB_PRIVATE->active[$row->contextid][$row->filter])) {
1045 $FILTERLIB_PRIVATE->active[$row->contextid][$row->filter][$row->name] = $row->value;
1051 * List all of the filters that are available in this context, and what the
1052 * local and inherited states of that filter are.
1054 * @param context $context a context that is not the system context.
1055 * @return array an array with filter names, for example 'tex'
1056 * as keys. and and the values are objects with fields:
1057 * ->filter filter name, same as the key.
1058 * ->localstate TEXTFILTER_ON/OFF/INHERIT
1059 * ->inheritedstate TEXTFILTER_ON/OFF - the state that will be used if localstate is set to TEXTFILTER_INHERIT.
1061 function filter_get_available_in_context($context) {
1062 global $DB;
1064 // The complex logic is working out the active state in the parent context,
1065 // so strip the current context from the list.
1066 $contextids = explode('/', trim($context->path, '/'));
1067 array_pop($contextids);
1068 $contextids = implode(',', $contextids);
1069 if (empty($contextids)) {
1070 throw new coding_exception('filter_get_available_in_context cannot be called with the system context.');
1073 // The following SQL is tricky, in the same way at the SQL in filter_get_active_in_context.
1074 $sql = "SELECT parent_states.filter,
1075 CASE WHEN fa.active IS NULL THEN " . TEXTFILTER_INHERIT . "
1076 ELSE fa.active END AS localstate,
1077 parent_states.inheritedstate
1078 FROM (SELECT f.filter, MAX(f.sortorder) AS sortorder,
1079 CASE WHEN MAX(f.active * ctx.depth) > -MIN(f.active * ctx.depth) THEN " . TEXTFILTER_ON . "
1080 ELSE " . TEXTFILTER_OFF . " END AS inheritedstate
1081 FROM {filter_active} f
1082 JOIN {context} ctx ON f.contextid = ctx.id
1083 WHERE ctx.id IN ($contextids)
1084 GROUP BY f.filter
1085 HAVING MIN(f.active) > " . TEXTFILTER_DISABLED . "
1086 ) parent_states
1087 LEFT JOIN {filter_active} fa ON fa.filter = parent_states.filter AND fa.contextid = $context->id
1088 ORDER BY parent_states.sortorder";
1089 return $DB->get_records_sql($sql);
1093 * This function is for use by the filter administration page.
1095 * @return array 'filtername' => object with fields 'filter' (=filtername), 'active' and 'sortorder'
1097 function filter_get_global_states() {
1098 global $DB;
1099 $context = context_system::instance();
1100 return $DB->get_records('filter_active', array('contextid' => $context->id), 'sortorder', 'filter,active,sortorder');
1104 * Delete all the data in the database relating to a filter, prior to deleting it.
1106 * @param string $filter The filter name, for example 'tex'.
1108 function filter_delete_all_for_filter($filter) {
1109 global $DB;
1111 unset_all_config_for_plugin('filter_' . $filter);
1112 $DB->delete_records('filter_active', array('filter' => $filter));
1113 $DB->delete_records('filter_config', array('filter' => $filter));
1117 * Delete all the data in the database relating to a context, used when contexts are deleted.
1119 * @param integer $contextid The id of the context being deleted.
1121 function filter_delete_all_for_context($contextid) {
1122 global $DB;
1123 $DB->delete_records('filter_active', array('contextid' => $contextid));
1124 $DB->delete_records('filter_config', array('contextid' => $contextid));
1128 * Does this filter have a global settings page in the admin tree?
1129 * (The settings page for a filter must be called, for example, filtersettingfiltertex.)
1131 * @param string $filter The filter name, for example 'tex'.
1132 * @return boolean Whether there should be a 'Settings' link on the config page.
1134 function filter_has_global_settings($filter) {
1135 global $CFG;
1136 $settingspath = $CFG->dirroot . '/filter/' . $filter . '/settings.php';
1137 if (is_readable($settingspath)) {
1138 return true;
1140 $settingspath = $CFG->dirroot . '/filter/' . $filter . '/filtersettings.php';
1141 return is_readable($settingspath);
1145 * Does this filter have local (per-context) settings?
1147 * @param string $filter The filter name, for example 'tex'.
1148 * @return boolean Whether there should be a 'Settings' link on the manage filters in context page.
1150 function filter_has_local_settings($filter) {
1151 global $CFG;
1152 $settingspath = $CFG->dirroot . '/filter/' . $filter . '/filterlocalsettings.php';
1153 return is_readable($settingspath);
1157 * Certain types of context (block and user) may not have local filter settings.
1158 * the function checks a context to see whether it may have local config.
1160 * @param object $context a context.
1161 * @return boolean whether this context may have local filter settings.
1163 function filter_context_may_have_filter_settings($context) {
1164 return $context->contextlevel != CONTEXT_BLOCK && $context->contextlevel != CONTEXT_USER;
1168 * Process phrases intelligently found within a HTML text (such as adding links).
1170 * @staticvar array $usedpharses
1171 * @param string $text the text that we are filtering
1172 * @param array $link_array an array of filterobjects
1173 * @param array $ignoretagsopen an array of opening tags that we should ignore while filtering
1174 * @param array $ignoretagsclose an array of corresponding closing tags
1175 * @param bool $overridedefaultignore True to only use tags provided by arguments
1176 * @return string
1178 function filter_phrases($text, &$link_array, $ignoretagsopen=NULL, $ignoretagsclose=NULL,
1179 $overridedefaultignore=false) {
1181 global $CFG;
1183 static $usedphrases;
1185 $ignoretags = array(); // To store all the enclosig tags to be completely ignored.
1186 $tags = array(); // To store all the simple tags to be ignored.
1188 if (!$overridedefaultignore) {
1189 // A list of open/close tags that we should not replace within
1190 // Extended to include <script>, <textarea>, <select> and <a> tags
1191 // Regular expression allows tags with or without attributes
1192 $filterignoretagsopen = array('<head>' , '<nolink>' , '<span class="nolink">',
1193 '<script(\s[^>]*?)?>', '<textarea(\s[^>]*?)?>',
1194 '<select(\s[^>]*?)?>', '<a(\s[^>]*?)?>');
1195 $filterignoretagsclose = array('</head>', '</nolink>', '</span>',
1196 '</script>', '</textarea>', '</select>','</a>');
1197 } else {
1198 // Set an empty default list.
1199 $filterignoretagsopen = array();
1200 $filterignoretagsclose = array();
1203 // Add the user defined ignore tags to the default list.
1204 if ( is_array($ignoretagsopen) ) {
1205 foreach ($ignoretagsopen as $open) {
1206 $filterignoretagsopen[] = $open;
1208 foreach ($ignoretagsclose as $close) {
1209 $filterignoretagsclose[] = $close;
1213 // Invalid prefixes and suffixes for the fullmatch searches
1214 // Every "word" character, but the underscore, is a invalid suffix or prefix.
1215 // (nice to use this because it includes national characters (accents...) as word characters.
1216 $filterinvalidprefixes = '([^\W_])';
1217 $filterinvalidsuffixes = '([^\W_])';
1219 // Double up some magic chars to avoid "accidental matches"
1220 $text = preg_replace('/([#*%])/','\1\1',$text);
1223 //Remove everything enclosed by the ignore tags from $text
1224 filter_save_ignore_tags($text,$filterignoretagsopen,$filterignoretagsclose,$ignoretags);
1226 // Remove tags from $text
1227 filter_save_tags($text,$tags);
1229 // Time to cycle through each phrase to be linked
1230 $size = sizeof($link_array);
1231 for ($n=0; $n < $size; $n++) {
1232 $linkobject =& $link_array[$n];
1234 // Set some defaults if certain properties are missing
1235 // Properties may be missing if the filterobject class has not been used to construct the object
1236 if (empty($linkobject->phrase)) {
1237 continue;
1240 // Avoid integers < 1000 to be linked. See bug 1446.
1241 $intcurrent = intval($linkobject->phrase);
1242 if (!empty($intcurrent) && strval($intcurrent) == $linkobject->phrase && $intcurrent < 1000) {
1243 continue;
1246 // All this work has to be done ONLY it it hasn't been done before
1247 if (!$linkobject->work_calculated) {
1248 if (!isset($linkobject->hreftagbegin) or !isset($linkobject->hreftagend)) {
1249 $linkobject->work_hreftagbegin = '<span class="highlight"';
1250 $linkobject->work_hreftagend = '</span>';
1251 } else {
1252 $linkobject->work_hreftagbegin = $linkobject->hreftagbegin;
1253 $linkobject->work_hreftagend = $linkobject->hreftagend;
1256 // Double up chars to protect true duplicates
1257 // be cleared up before returning to the user.
1258 $linkobject->work_hreftagbegin = preg_replace('/([#*%])/','\1\1',$linkobject->work_hreftagbegin);
1260 if (empty($linkobject->casesensitive)) {
1261 $linkobject->work_casesensitive = false;
1262 } else {
1263 $linkobject->work_casesensitive = true;
1265 if (empty($linkobject->fullmatch)) {
1266 $linkobject->work_fullmatch = false;
1267 } else {
1268 $linkobject->work_fullmatch = true;
1271 // Strip tags out of the phrase
1272 $linkobject->work_phrase = strip_tags($linkobject->phrase);
1274 // Double up chars that might cause a false match -- the duplicates will
1275 // be cleared up before returning to the user.
1276 $linkobject->work_phrase = preg_replace('/([#*%])/','\1\1',$linkobject->work_phrase);
1278 // Set the replacement phrase properly
1279 if ($linkobject->replacementphrase) { //We have specified a replacement phrase
1280 // Strip tags
1281 $linkobject->work_replacementphrase = strip_tags($linkobject->replacementphrase);
1282 } else { //The replacement is the original phrase as matched below
1283 $linkobject->work_replacementphrase = '$1';
1286 // Quote any regular expression characters and the delimiter in the work phrase to be searched
1287 $linkobject->work_phrase = preg_quote($linkobject->work_phrase, '/');
1289 // Work calculated
1290 $linkobject->work_calculated = true;
1294 // If $CFG->filtermatchoneperpage, avoid previously (request) linked phrases
1295 if (!empty($CFG->filtermatchoneperpage)) {
1296 if (!empty($usedphrases) && in_array($linkobject->work_phrase,$usedphrases)) {
1297 continue;
1301 // Regular expression modifiers
1302 $modifiers = ($linkobject->work_casesensitive) ? 's' : 'isu'; // works in unicode mode!
1304 // Do we need to do a fullmatch?
1305 // If yes then go through and remove any non full matching entries
1306 if ($linkobject->work_fullmatch) {
1307 $notfullmatches = array();
1308 $regexp = '/'.$filterinvalidprefixes.'('.$linkobject->work_phrase.')|('.$linkobject->work_phrase.')'.$filterinvalidsuffixes.'/'.$modifiers;
1310 preg_match_all($regexp,$text,$list_of_notfullmatches);
1312 if ($list_of_notfullmatches) {
1313 foreach (array_unique($list_of_notfullmatches[0]) as $key=>$value) {
1314 $notfullmatches['<*'.$key.'*>'] = $value;
1316 if (!empty($notfullmatches)) {
1317 $text = str_replace($notfullmatches,array_keys($notfullmatches),$text);
1322 // Finally we do our highlighting
1323 if (!empty($CFG->filtermatchonepertext) || !empty($CFG->filtermatchoneperpage)) {
1324 $resulttext = preg_replace('/('.$linkobject->work_phrase.')/'.$modifiers,
1325 $linkobject->work_hreftagbegin.
1326 $linkobject->work_replacementphrase.
1327 $linkobject->work_hreftagend, $text, 1);
1328 } else {
1329 $resulttext = preg_replace('/('.$linkobject->work_phrase.')/'.$modifiers,
1330 $linkobject->work_hreftagbegin.
1331 $linkobject->work_replacementphrase.
1332 $linkobject->work_hreftagend, $text);
1336 // If the text has changed we have to look for links again
1337 if ($resulttext != $text) {
1338 // Set $text to $resulttext
1339 $text = $resulttext;
1340 // Remove everything enclosed by the ignore tags from $text
1341 filter_save_ignore_tags($text,$filterignoretagsopen,$filterignoretagsclose,$ignoretags);
1342 // Remove tags from $text
1343 filter_save_tags($text,$tags);
1344 // If $CFG->filtermatchoneperpage, save linked phrases to request
1345 if (!empty($CFG->filtermatchoneperpage)) {
1346 $usedphrases[] = $linkobject->work_phrase;
1351 // Replace the not full matches before cycling to next link object
1352 if (!empty($notfullmatches)) {
1353 $text = str_replace(array_keys($notfullmatches),$notfullmatches,$text);
1354 unset($notfullmatches);
1358 // Rebuild the text with all the excluded areas
1360 if (!empty($tags)) {
1361 $text = str_replace(array_keys($tags), $tags, $text);
1364 if (!empty($ignoretags)) {
1365 $ignoretags = array_reverse($ignoretags); // Reversed so "progressive" str_replace() will solve some nesting problems.
1366 $text = str_replace(array_keys($ignoretags),$ignoretags,$text);
1369 // Remove the protective doubleups
1370 $text = preg_replace('/([#*%])(\1)/','\1',$text);
1372 // Add missing javascript for popus
1373 $text = filter_add_javascript($text);
1376 return $text;
1380 * @todo Document this function
1381 * @param array $linkarray
1382 * @return array
1384 function filter_remove_duplicates($linkarray) {
1386 $concepts = array(); // keep a record of concepts as we cycle through
1387 $lconcepts = array(); // a lower case version for case insensitive
1389 $cleanlinks = array();
1391 foreach ($linkarray as $key=>$filterobject) {
1392 if ($filterobject->casesensitive) {
1393 $exists = in_array($filterobject->phrase, $concepts);
1394 } else {
1395 $exists = in_array(core_text::strtolower($filterobject->phrase), $lconcepts);
1398 if (!$exists) {
1399 $cleanlinks[] = $filterobject;
1400 $concepts[] = $filterobject->phrase;
1401 $lconcepts[] = core_text::strtolower($filterobject->phrase);
1405 return $cleanlinks;
1409 * Extract open/lose tags and their contents to avoid being processed by filters.
1410 * Useful to extract pieces of code like <a>...</a> tags. It returns the text
1411 * converted with some <#xTEXTFILTER_EXCL_SEPARATORx#> codes replacing the extracted text. Such extracted
1412 * texts are returned in the ignoretags array (as values), with codes as keys.
1414 * @param string $text the text that we are filtering (in/out)
1415 * @param array $filterignoretagsopen an array of open tags to start searching
1416 * @param array $filterignoretagsclose an array of close tags to end searching
1417 * @param array $ignoretags an array of saved strings useful to rebuild the original text (in/out)
1419 function filter_save_ignore_tags(&$text, $filterignoretagsopen, $filterignoretagsclose, &$ignoretags) {
1421 // Remove everything enclosed by the ignore tags from $text
1422 foreach ($filterignoretagsopen as $ikey=>$opentag) {
1423 $closetag = $filterignoretagsclose[$ikey];
1424 // form regular expression
1425 $opentag = str_replace('/','\/',$opentag); // delimit forward slashes
1426 $closetag = str_replace('/','\/',$closetag); // delimit forward slashes
1427 $pregexp = '/'.$opentag.'(.*?)'.$closetag.'/is';
1429 preg_match_all($pregexp, $text, $list_of_ignores);
1430 foreach (array_unique($list_of_ignores[0]) as $key=>$value) {
1431 $prefix = (string)(count($ignoretags) + 1);
1432 $ignoretags['<#'.$prefix.TEXTFILTER_EXCL_SEPARATOR.$key.'#>'] = $value;
1434 if (!empty($ignoretags)) {
1435 $text = str_replace($ignoretags,array_keys($ignoretags),$text);
1441 * Extract tags (any text enclosed by < and > to avoid being processed by filters.
1442 * It returns the text converted with some <%xTEXTFILTER_EXCL_SEPARATORx%> codes replacing the extracted text. Such extracted
1443 * texts are returned in the tags array (as values), with codes as keys.
1445 * @param string $text the text that we are filtering (in/out)
1446 * @param array $tags an array of saved strings useful to rebuild the original text (in/out)
1448 function filter_save_tags(&$text, &$tags) {
1450 preg_match_all('/<([^#%*].*?)>/is',$text,$list_of_newtags);
1451 foreach (array_unique($list_of_newtags[0]) as $ntkey=>$value) {
1452 $prefix = (string)(count($tags) + 1);
1453 $tags['<%'.$prefix.TEXTFILTER_EXCL_SEPARATOR.$ntkey.'%>'] = $value;
1455 if (!empty($tags)) {
1456 $text = str_replace($tags,array_keys($tags),$text);
1461 * Add missing openpopup javascript to HTML files.
1463 * @param string $text
1464 * @return string
1466 function filter_add_javascript($text) {
1467 global $CFG;
1469 if (stripos($text, '</html>') === FALSE) {
1470 return $text; // This is not a html file.
1472 if (strpos($text, 'onclick="return openpopup') === FALSE) {
1473 return $text; // No popup - no need to add javascript.
1475 $js ="
1476 <script type=\"text/javascript\">
1477 <!--
1478 function openpopup(url,name,options,fullscreen) {
1479 fullurl = \"".$CFG->httpswwwroot."\" + url;
1480 windowobj = window.open(fullurl,name,options);
1481 if (fullscreen) {
1482 windowobj.moveTo(0,0);
1483 windowobj.resizeTo(screen.availWidth,screen.availHeight);
1485 windowobj.focus();
1486 return false;
1488 // -->
1489 </script>";
1490 if (stripos($text, '</head>') !== FALSE) {
1491 // Try to add it into the head element.
1492 $text = str_ireplace('</head>', $js.'</head>', $text);
1493 return $text;
1496 // Last chance - try adding head element.
1497 return preg_replace("/<html.*?>/is", "\\0<head>".$js.'</head>', $text);