on-demand release 2.4rc1
[moodle.git] / lib / outputlib.php
CommitLineData
571fa828 1<?php
571fa828 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.
13//
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/>.
16
78946b9b
PS
17/**
18 * Functions for generating the HTML that Moodle should output.
19 *
20 * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
21 * for an overview.
22 *
473dd811
SH
23 * @copyright 2009 Tim Hunt
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
f8129210 25 * @package core
76be40cc 26 * @category output
78946b9b
PS
27 */
28
78bfb562
PS
29defined('MOODLE_INTERNAL') || die();
30
d9c8f425 31require_once($CFG->libdir.'/outputcomponents.php');
32require_once($CFG->libdir.'/outputactions.php');
33require_once($CFG->libdir.'/outputfactories.php');
d9c8f425 34require_once($CFG->libdir.'/outputrenderers.php');
0bb38e8c 35require_once($CFG->libdir.'/outputrequirementslib.php');
d9c8f425 36
571fa828 37/**
78946b9b 38 * Invalidate all server and client side caches.
473dd811 39 *
f11db1a6 40 * This method deletes the physical directory that is used to cache the theme
473dd811 41 * files used for serving.
f11db1a6 42 * Because it deletes the main theme cache directory all themes are reset by
473dd811 43 * this function.
78946b9b
PS
44 */
45function theme_reset_all_caches() {
46 global $CFG;
47 require_once("$CFG->libdir/filelib.php");
48
f11db1a6
PS
49 $next = time();
50 if (isset($CFG->themerev) and $next <= $CFG->themerev and $CFG->themerev - $next < 60*60) {
51 // This resolves problems when reset is requested repeatedly within 1s,
52 // the < 1h condition prevents accidental switching to future dates
53 // because we might not recover from it.
54 $next = $CFG->themerev+1;
55 }
56
57 set_config('themerev', $next); // time is unique even when you reset/switch database
365bec4c 58 fulldelete("$CFG->cachedir/theme");
78946b9b
PS
59}
60
61/**
62 * Enable or disable theme designer mode.
473dd811 63 *
78946b9b 64 * @param bool $state
78946b9b
PS
65 */
66function theme_set_designer_mod($state) {
67 theme_reset_all_caches();
68 set_config('themedesignermode', (int)!empty($state));
69}
70
71/**
72 * Returns current theme revision number.
473dd811 73 *
78946b9b 74 * @return int
571fa828 75 */
78946b9b
PS
76function theme_get_revision() {
77 global $CFG;
78
79 if (empty($CFG->themedesignermode)) {
80 if (empty($CFG->themerev)) {
81 return -1;
82 } else {
83 return $CFG->themerev;
84 }
85
86 } else {
87 return -1;
88 }
89}
90
571fa828 91
571fa828 92/**
fdeb7fa1 93 * This class represents the configuration variables of a Moodle theme.
94 *
95 * All the variables with access: public below (with a few exceptions that are marked)
3d3fae72 96 * are the properties you can set in your themes config.php file.
fdeb7fa1 97 *
98 * There are also some methods and protected variables that are part of the inner
3d3fae72 99 * workings of Moodle's themes system. If you are just editing a themes config.php
fa1afe32 100 * file, you can just ignore those, and the following information for developers.
ebebf55c 101 *
102 * Normally, to create an instance of this class, you should use the
103 * {@link theme_config::load()} factory method to load a themes config.php file.
fa1afe32 104 * However, normally you don't need to bother, because moodle_page (that is, $PAGE)
fdeb7fa1 105 * will create one for you, accessible as $PAGE->theme.
571fa828 106 *
107 * @copyright 2009 Tim Hunt
473dd811
SH
108 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
109 * @since Moodle 2.0
f8129210 110 * @package core
76be40cc 111 * @category output
571fa828 112 */
ebebf55c 113class theme_config {
473dd811 114
5f0baa43 115 /**
76be40cc 116 * @var string Default theme, used when requested theme not found.
5f0baa43 117 */
90723839 118 const DEFAULT_THEME = 'standard';
5f0baa43 119
ebebf55c 120 /**
76be40cc 121 * @var array You can base your theme on other themes by linking to the other theme as
78946b9b 122 * parents. This lets you use the CSS and layouts from the other themes
f8129210 123 * (see {@link theme_config::$layouts}).
fdeb7fa1 124 * That makes it easy to create a new theme that is similar to another one
3d3fae72 125 * but with a few changes. In this themes CSS you only need to override
fdeb7fa1 126 * those rules you want to change.
fdeb7fa1 127 */
78946b9b 128 public $parents;
571fa828 129
fdeb7fa1 130 /**
76be40cc 131 * @var array The names of all the stylesheets from this theme that you would
78946b9b 132 * like included, in order. Give the names of the files without .css.
fdeb7fa1 133 */
78946b9b 134 public $sheets = array();
571fa828 135
fdeb7fa1 136 /**
76be40cc 137 * @var array The names of all the stylesheets from parents that should be excluded.
78946b9b
PS
138 * true value may be used to specify all parents or all themes from one parent.
139 * If no value specified value from parent theme used.
fdeb7fa1 140 */
78946b9b 141 public $parents_exclude_sheets = null;
571fa828 142
fdeb7fa1 143 /**
76be40cc 144 * @var array List of plugin sheets to be excluded.
78946b9b 145 * If no value specified value from parent theme used.
fdeb7fa1 146 */
78946b9b 147 public $plugins_exclude_sheets = null;
571fa828 148
fdeb7fa1 149 /**
76be40cc 150 * @var array List of style sheets that are included in the text editor bodies.
78946b9b 151 * Sheets from parent themes are used automatically and can not be excluded.
fdeb7fa1 152 */
78946b9b 153 public $editor_sheets = array();
571fa828 154
38aafea2 155 /**
76be40cc 156 * @var array The names of all the javascript files this theme that you would
04c01408 157 * like included from head, in order. Give the names of the files without .js.
38aafea2
PS
158 */
159 public $javascripts = array();
160
04c01408 161 /**
76be40cc 162 * @var array The names of all the javascript files this theme that you would
04c01408 163 * like included from footer, in order. Give the names of the files without .js.
04c01408
PS
164 */
165 public $javascripts_footer = array();
4399b9d5 166
38aafea2 167 /**
76be40cc
SH
168 * @var array The names of all the javascript files from parents that should
169 * be excluded. true value may be used to specify all parents or all themes
170 * from one parent.
38aafea2 171 * If no value specified value from parent theme used.
38aafea2
PS
172 */
173 public $parents_exclude_javascripts = null;
174
fdeb7fa1 175 /**
76be40cc 176 * @var array Which file to use for each page layout.
fdeb7fa1 177 *
78946b9b
PS
178 * This is an array of arrays. The keys of the outer array are the different layouts.
179 * Pages in Moodle are using several different layouts like 'normal', 'course', 'home',
180 * 'popup', 'form', .... The most reliable way to get a complete list is to look at
181 * {@link http://cvs.moodle.org/moodle/theme/base/config.php?view=markup the base theme config.php file}.
d4a03c00 182 * That file also has a good example of how to set this setting.
fdeb7fa1 183 *
78946b9b 184 * For each layout, the value in the outer array is an array that describes
d4a03c00 185 * how you want that type of page to look. For example
186 * <pre>
187 * $THEME->layouts = array(
39df78bf 188 * // Most pages - if we encounter an unknown or a missing page type, this one is used.
191b267b 189 * 'standard' => array(
78946b9b
PS
190 * 'theme' = 'mytheme',
191 * 'file' => 'normal.php',
d4a03c00 192 * 'regions' => array('side-pre', 'side-post'),
193 * 'defaultregion' => 'side-post'
194 * ),
195 * // The site home page.
196 * 'home' => array(
78946b9b
PS
197 * 'theme' = 'mytheme',
198 * 'file' => 'home.php',
d4a03c00 199 * 'regions' => array('side-pre', 'side-post'),
200 * 'defaultregion' => 'side-post'
201 * ),
202 * // ...
203 * );
204 * </pre>
fdeb7fa1 205 *
78946b9b
PS
206 * 'theme' name of the theme where is the layout located
207 * 'file' is the layout file to use for this type of page.
208 * layout files are stored in layout subfolder
d4a03c00 209 * 'regions' This lists the regions on the page where blocks may appear. For
210 * each region you list here, your layout file must include a call to
211 * <pre>
212 * echo $OUTPUT->blocks_for_region($regionname);
213 * </pre>
214 * or equivalent so that the blocks are actually visible.
fdeb7fa1 215 *
d4a03c00 216 * 'defaultregion' If the list of regions is non-empty, then you must pick
217 * one of the one of them as 'default'. This has two meanings. First, this is
218 * where new blocks are added. Second, if there are any blocks associated with
fa1afe32 219 * the page, but in non-existent regions, they appear here. (Imaging, for example,
d4a03c00 220 * that someone added blocks using a different theme that used different region
221 * names, and then switched to this theme.)
fdeb7fa1 222 */
d4a03c00 223 public $layouts = array();
571fa828 224
fdeb7fa1 225 /**
76be40cc
SH
226 * @var string Name of the renderer factory class to use. Must implement the
227 * {@link renderer_factory} interface.
fdeb7fa1 228 *
229 * This is an advanced feature. Moodle output is generated by 'renderers',
230 * you can customise the HTML that is output by writing custom renderers,
231 * and then you need to specify 'renderer factory' so that Moodle can find
232 * your renderers.
233 *
234 * There are some renderer factories supplied with Moodle. Please follow these
235 * links to see what they do.
236 * <ul>
237 * <li>{@link standard_renderer_factory} - the default.</li>
238 * <li>{@link theme_overridden_renderer_factory} - use this if you want to write
78946b9b 239 * your own custom renderers in a lib.php file in this theme (or the parent theme).</li>
fdeb7fa1 240 * </ul>
fdeb7fa1 241 */
ebebf55c 242 public $rendererfactory = 'standard_renderer_factory';
ebebf55c 243
fdeb7fa1 244 /**
76be40cc 245 * @var string Function to do custom CSS post-processing.
fdeb7fa1 246 *
78946b9b
PS
247 * This is an advanced feature. If you want to do custom post-processing on the
248 * CSS before it is output (for example, to replace certain variable names
249 * with particular values) you can give the name of a function here.
fdeb7fa1 250 */
78946b9b 251 public $csspostprocess = null;
571fa828 252
253 /**
76be40cc 254 * @var string Accessibility: Right arrow-like character is
78946b9b
PS
255 * used in the breadcrumb trail, course navigation menu
256 * (previous/next activity), calendar, and search forum block.
257 * If the theme does not set characters, appropriate defaults
258 * are set automatically. Please DO NOT
259 * use &lt; &gt; &raquo; - these are confusing for blind users.
78946b9b
PS
260 */
261 public $rarrow = null;
262
263 /**
76be40cc 264 * @var string Accessibility: Right arrow-like character is
78946b9b
PS
265 * used in the breadcrumb trail, course navigation menu
266 * (previous/next activity), calendar, and search forum block.
267 * If the theme does not set characters, appropriate defaults
268 * are set automatically. Please DO NOT
269 * use &lt; &gt; &raquo; - these are confusing for blind users.
571fa828 270 */
78946b9b
PS
271 public $larrow = null;
272
03276af6 273 /**
76be40cc 274 * @var bool Some themes may want to disable ajax course editing.
03276af6
DM
275 */
276 public $enablecourseajax = true;
78946b9b 277
13725b37
PS
278 /**
279 * @var string Determines served document types
280 * - 'html5' the only officially supported doctype in Moodle
281 * - 'xhtml5' may be used in development for validation (not intended for production servers!)
282 * - 'xhtml' XHTML 1.0 Strict for legacy themes only
283 */
284 public $doctype = 'html5';
285
78946b9b 286 //==Following properties are not configurable from theme config.php==
571fa828 287
fdeb7fa1 288 /**
76be40cc 289 * @var string The name of this theme. Set automatically when this theme is
78946b9b 290 * loaded. This can not be set in theme config.php
fdeb7fa1 291 */
78946b9b 292 public $name;
fdeb7fa1 293
294 /**
76be40cc 295 * @var string The folder where this themes files are stored. This is set
78946b9b 296 * automatically. This can not be set in theme config.php
fdeb7fa1 297 */
78946b9b 298 public $dir;
fdeb7fa1 299
300 /**
76be40cc 301 * @var stdClass Theme settings stored in config_plugins table.
78946b9b 302 * This can not be set in theme config.php
78946b9b
PS
303 */
304 public $setting = null;
305
d2c394f3 306 /**
76be40cc 307 * @var bool If set to true and the theme enables the dock then blocks will be able
d2c394f3 308 * to be moved to the special dock
d2c394f3
SH
309 */
310 public $enable_dock = false;
311
4d56ee95 312 /**
76be40cc 313 * @var bool If set to true then this theme will not be shown in the theme selector unless
4d56ee95 314 * theme designer mode is turned on.
4d56ee95
SH
315 */
316 public $hidefromselector = false;
317
78946b9b 318 /**
76be40cc 319 * @var renderer_factory Instance of the renderer_factory implementation
fdeb7fa1 320 * we are using. Implementation detail.
321 */
322 protected $rf = null;
323
324 /**
76be40cc 325 * @var array List of parent config objects.
78946b9b
PS
326 **/
327 protected $parent_configs = array();
fdeb7fa1 328
b305d623
SH
329 /**
330 * @var bool If set to true then the theme is safe to run through the optimiser (if it is enabled)
331 * If set to false then we know either the theme has already been optimised and the CSS optimiser is not needed
332 * or the theme is not compatible with the CSS optimiser. In both cases even if enabled the CSS optimiser will not
333 * be used with this theme if set to false.
334 */
335 public $supportscssoptimisation = true;
336
436dbeec
SH
337 /**
338 * Used to determine whether we can serve SVG images or not.
339 * @var bool
340 */
341 private $usesvg = null;
342
571fa828 343 /**
ebebf55c 344 * Load the config.php file for a particular theme, and return an instance
345 * of this class. (That is, this is a factory method.)
346 *
347 * @param string $themename the name of the theme.
348 * @return theme_config an instance of this class.
571fa828 349 */
ebebf55c 350 public static function load($themename) {
351 global $CFG;
571fa828 352
78946b9b
PS
353 // load theme settings from db
354 try {
355 $settings = get_config('theme_'.$themename);
356 } catch (dml_exception $e) {
da0c0e25 357 // most probably moodle tables not created yet
365a5941 358 $settings = new stdClass();
78946b9b 359 }
ebebf55c 360
78946b9b
PS
361 if ($config = theme_config::find_theme_config($themename, $settings)) {
362 return new theme_config($config);
5f0baa43
PS
363
364 } else if ($themename == theme_config::DEFAULT_THEME) {
365 throw new coding_exception('Default theme '.theme_config::DEFAULT_THEME.' not available or broken!');
366
78946b9b
PS
367 } else {
368 // bad luck, the requested theme has some problems - admin see details in theme config
5f0baa43 369 return new theme_config(theme_config::find_theme_config(theme_config::DEFAULT_THEME, $settings));
78946b9b
PS
370 }
371 }
ebebf55c 372
78946b9b
PS
373 /**
374 * Theme diagnostic code. It is very problematic to send debug output
375 * to the actual CSS file, instead this functions is supposed to
ae5b6801 376 * diagnose given theme and highlights all potential problems.
78946b9b
PS
377 * This information should be available from the theme selection page
378 * or some other debug page for theme designers.
379 *
380 * @param string $themename
381 * @return array description of problems
382 */
383 public static function diagnose($themename) {
5ef719e7 384 //TODO: MDL-21108
78946b9b
PS
385 return array();
386 }
387
388 /**
389 * Private constructor, can be called only from the factory method.
390 * @param stdClass $config
391 */
392 private function __construct($config) {
393 global $CFG; //needed for included lib.php files
394
395 $this->settings = $config->settings;
396 $this->name = $config->name;
397 $this->dir = $config->dir;
398
399 if ($this->name != 'base') {
400 $baseconfig = theme_config::find_theme_config('base', $this->settings);
401 } else {
402 $baseconfig = $config;
571fa828 403 }
ebebf55c 404
fef27578 405 $configurable = array('parents', 'sheets', 'parents_exclude_sheets', 'plugins_exclude_sheets', 'javascripts', 'javascripts_footer',
b305d623 406 'parents_exclude_javascripts', 'layouts', 'enable_dock', 'enablecourseajax', 'supportscssoptimisation',
13725b37 407 'rendererfactory', 'csspostprocess', 'editor_sheets', 'rarrow', 'larrow', 'hidefromselector', 'doctype');
ebebf55c 408
78946b9b
PS
409 foreach ($config as $key=>$value) {
410 if (in_array($key, $configurable)) {
411 $this->$key = $value;
412 }
413 }
414
415 // verify all parents and load configs and renderers
416 foreach ($this->parents as $parent) {
417 if ($parent == 'base') {
418 $parent_config = $baseconfig;
419 } else if (!$parent_config = theme_config::find_theme_config($parent, $this->settings)) {
420 // this is not good - better exclude faulty parents
421 continue;
422 }
423 $libfile = $parent_config->dir.'/lib.php';
424 if (is_readable($libfile)) {
425 // theme may store various function here
426 include_once($libfile);
427 }
428 $renderersfile = $parent_config->dir.'/renderers.php';
429 if (is_readable($renderersfile)) {
430 // may contain core and plugin renderers and renderer factory
431 include_once($renderersfile);
432 }
433 $this->parent_configs[$parent] = $parent_config;
434 $rendererfile = $parent_config->dir.'/renderers.php';
435 if (is_readable($rendererfile)) {
436 // may contain core and plugin renderers and renderer factory
437 include_once($rendererfile);
438 }
439 }
440 $libfile = $this->dir.'/lib.php';
441 if (is_readable($libfile)) {
442 // theme may store various function here
443 include_once($libfile);
444 }
445 $rendererfile = $this->dir.'/renderers.php';
446 if (is_readable($rendererfile)) {
447 // may contain core and plugin renderers and renderer factory
448 include_once($rendererfile);
6fc267a0
BJ
449 } else {
450 // check if renderers.php file is missnamed renderer.php
451 if (is_readable($this->dir.'/renderer.php')) {
452 debugging('Developer hint: '.$this->dir.'/renderer.php should be renamed to ' . $this->dir."/renderers.php.
11c0ecce 453 See: http://docs.moodle.org/dev/Output_renderers#Theme_renderers.", DEBUG_DEVELOPER);
6fc267a0 454 }
78946b9b 455 }
4399b9d5 456
78946b9b
PS
457 // cascade all layouts properly
458 foreach ($baseconfig->layouts as $layout=>$value) {
459 if (!isset($this->layouts[$layout])) {
460 foreach ($this->parent_configs as $parent_config) {
461 if (isset($parent_config->layouts[$layout])) {
462 $this->layouts[$layout] = $parent_config->layouts[$layout];
463 continue 2;
464 }
465 }
466 $this->layouts[$layout] = $value;
467 }
468 }
469
470 //fix arrows if needed
471 $this->check_theme_arrows();
571fa828 472 }
473
f8129210 474 /**
78946b9b
PS
475 * Checks if arrows $THEME->rarrow, $THEME->larrow have been set (theme/-/config.php).
476 * If not it applies sensible defaults.
477 *
478 * Accessibility: right and left arrow Unicode characters for breadcrumb, calendar,
479 * search forum block, etc. Important: these are 'silent' in a screen-reader
480 * (unlike &gt; &raquo;), and must be accompanied by text.
34a2777c 481 */
78946b9b
PS
482 private function check_theme_arrows() {
483 if (!isset($this->rarrow) and !isset($this->larrow)) {
484 // Default, looks good in Win XP/IE 6, Win/Firefox 1.5, Win/Netscape 8...
485 // Also OK in Win 9x/2K/IE 5.x
486 $this->rarrow = '&#x25BA;';
487 $this->larrow = '&#x25C4;';
488 if (empty($_SERVER['HTTP_USER_AGENT'])) {
489 $uagent = '';
ebebf55c 490 } else {
78946b9b
PS
491 $uagent = $_SERVER['HTTP_USER_AGENT'];
492 }
493 if (false !== strpos($uagent, 'Opera')
494 || false !== strpos($uagent, 'Mac')) {
495 // Looks good in Win XP/Mac/Opera 8/9, Mac/Firefox 2, Camino, Safari.
496 // Not broken in Mac/IE 5, Mac/Netscape 7 (?).
497 $this->rarrow = '&#x25B6;';
498 $this->larrow = '&#x25C0;';
499 }
500 elseif (false !== strpos($uagent, 'Konqueror')) {
501 $this->rarrow = '&rarr;';
502 $this->larrow = '&larr;';
503 }
504 elseif (isset($_SERVER['HTTP_ACCEPT_CHARSET'])
505 && false === stripos($_SERVER['HTTP_ACCEPT_CHARSET'], 'utf-8')) {
506 // (Win/IE 5 doesn't set ACCEPT_CHARSET, but handles Unicode.)
507 // To be safe, non-Unicode browsers!
508 $this->rarrow = '&gt;';
509 $this->larrow = '&lt;';
510 }
511
f8129210 512 // RTL support - in RTL languages, swap r and l arrows
78946b9b
PS
513 if (right_to_left()) {
514 $t = $this->rarrow;
515 $this->rarrow = $this->larrow;
516 $this->larrow = $t;
ebebf55c 517 }
ebebf55c 518 }
78946b9b 519 }
ebebf55c 520
78946b9b
PS
521 /**
522 * Returns output renderer prefixes, these are used when looking
71c03ac1 523 * for the overridden renderers in themes.
473dd811 524 *
78946b9b
PS
525 * @return array
526 */
527 public function renderer_prefixes() {
528 global $CFG; // just in case the included files need it
529
596af93a 530 $prefixes = array('theme_'.$this->name);
78946b9b
PS
531
532 foreach ($this->parent_configs as $parent) {
533 $prefixes[] = 'theme_'.$parent->name;
534 }
535
536 return $prefixes;
34a2777c 537 }
538
571fa828 539 /**
78946b9b 540 * Returns the stylesheet URL of this editor content
473dd811 541 *
78946b9b
PS
542 * @param bool $encoded false means use & and true use &amp; in URLs
543 * @return string
571fa828 544 */
78946b9b
PS
545 public function editor_css_url($encoded=true) {
546 global $CFG;
547
548 $rev = theme_get_revision();
549
550 if ($rev > -1) {
7070b7f2
PS
551 if (!empty($CFG->slasharguments)) {
552 $url = new moodle_url("$CFG->httpswwwroot/theme/styles.php");
553 $url->set_slashargument('/'.$this->name.'/'.$rev.'/editor', 'noparam', true);
554 return $url;
555 } else {
556 $params = array('theme'=>$this->name,'rev'=>$rev, 'type'=>'editor');
557 return new moodle_url($CFG->httpswwwroot.'/theme/styles.php', $params);
558 }
78946b9b
PS
559 } else {
560 $params = array('theme'=>$this->name, 'type'=>'editor');
561 return new moodle_url($CFG->httpswwwroot.'/theme/styles_debug.php', $params);
571fa828 562 }
571fa828 563 }
564
565 /**
78946b9b 566 * Returns the content of the CSS to be used in editor content
473dd811 567 *
78946b9b 568 * @return string
571fa828 569 */
71be124d 570 public function editor_css_files() {
78946b9b
PS
571 global $CFG;
572
71be124d 573 $files = array();
78946b9b
PS
574
575 // first editor plugins
576 $plugins = get_plugin_list('editor');
577 foreach ($plugins as $plugin=>$fulldir) {
578 $sheetfile = "$fulldir/editor_styles.css";
579 if (is_readable($sheetfile)) {
71be124d 580 $files['plugin_'.$plugin] = $sheetfile;
78946b9b
PS
581 }
582 }
583 // then parent themes
584 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
585 if (empty($parent_config->editor_sheets)) {
586 continue;
587 }
588 foreach ($parent_config->editor_sheets as $sheet) {
ca194849 589 $sheetfile = "$parent_config->dir/style/$sheet.css";
78946b9b 590 if (is_readable($sheetfile)) {
71be124d 591 $files['parent_'.$parent_config->name.'_'.$sheet] = $sheetfile;
78946b9b
PS
592 }
593 }
594 }
595 // finally this theme
596 if (!empty($this->editor_sheets)) {
597 foreach ($this->editor_sheets as $sheet) {
71be124d 598 $sheetfile = "$this->dir/style/$sheet.css";
78946b9b 599 if (is_readable($sheetfile)) {
71be124d 600 $files['theme_'.$sheet] = $sheetfile;
78946b9b
PS
601 }
602 }
603 }
604
71be124d 605 return $files;
571fa828 606 }
607
608 /**
78946b9b 609 * Get the stylesheet URL of this theme
473dd811
SH
610 *
611 * @param moodle_page $page Not used... deprecated?
38aafea2 612 * @return array of moodle_url
571fa828 613 */
efaa4c08 614 public function css_urls(moodle_page $page) {
78946b9b
PS
615 global $CFG;
616
617 $rev = theme_get_revision();
618
efaa4c08 619 $urls = array();
efaa4c08 620
78946b9b 621 if ($rev > -1) {
a6338a13 622 if (check_browser_version('MSIE', 5)) {
537740cb
SH
623 // We need to split the CSS files for IE
624 $urls[] = new moodle_url($CFG->httpswwwroot.'/theme/styles.php', array('theme'=>$this->name,'rev'=>$rev, 'type'=>'plugins'));
625 $urls[] = new moodle_url($CFG->httpswwwroot.'/theme/styles.php', array('theme'=>$this->name,'rev'=>$rev, 'type'=>'parents'));
626 $urls[] = new moodle_url($CFG->httpswwwroot.'/theme/styles.php', array('theme'=>$this->name,'rev'=>$rev, 'type'=>'theme'));
627 } else {
7070b7f2
PS
628 if (!empty($CFG->slasharguments)) {
629 $url = new moodle_url("$CFG->httpswwwroot/theme/styles.php");
630 $url->set_slashargument('/'.$this->name.'/'.$rev.'/all', 'noparam', true);
631 $urls[] = $url;
632 } else {
633 $urls[] = new moodle_url($CFG->httpswwwroot.'/theme/styles.php', array('theme'=>$this->name,'rev'=>$rev, 'type'=>'all'));
634 }
78946b9b 635 }
78946b9b 636 } else {
7829cf79
PS
637 // find out the current CSS and cache it now for 5 seconds
638 // the point is to construct the CSS only once and pass it through the
639 // dataroot to the script that actually serves the sheets
d6b64d15
PS
640 if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) {
641 define('THEME_DESIGNER_CACHE_LIFETIME', 4); // this can be also set in config.php
642 }
bcf7f9f3
643 $candidatedir = "$CFG->cachedir/theme/$this->name";
644 $candidatesheet = "$candidatedir/designer.ser";
645 $rebuild = true;
646 if (file_exists($candidatesheet) and filemtime($candidatesheet) > time() - THEME_DESIGNER_CACHE_LIFETIME) {
7829cf79
PS
647 if ($css = file_get_contents($candidatesheet)) {
648 $css = unserialize($css);
bcf7f9f3
649 if (is_array($css)) {
650 $rebuild = false;
651 }
652 }
653 }
654 if ($rebuild) {
655 // Prepare the CSS optimiser if it is to be used,
656 // please note that it may be very slow and is therefore strongly discouraged in theme designer mode.
657 $optimiser = null;
658 if (!empty($CFG->enablecssoptimiser) && $this->supportscssoptimisation) {
659 require_once($CFG->dirroot.'/lib/csslib.php');
660 $optimiser = new css_optimiser;
7829cf79 661 }
b305d623 662 $css = $this->css_content($optimiser);
bcf7f9f3
663
664 // We do not want any errors here because this may fail easily because of the concurrent access.
665 $prevabort = ignore_user_abort(true);
666 check_dir_exists($candidatedir);
667 $tempfile = tempnam($candidatedir, 'tmpdesigner');
668 file_put_contents($tempfile, serialize($css));
669 $reporting = error_reporting(0);
670 chmod($tempfile, $CFG->filepermissions);
671 unlink($candidatesheet); // Do not rely on rename() deleting original, they may decide to change it at any time as usually.
672 rename($tempfile, $candidatesheet);
673 error_reporting($reporting);
674 ignore_user_abort($prevabort);
7829cf79
PS
675 }
676
c724f835 677 $baseurl = $CFG->httpswwwroot.'/theme/styles_debug.php';
c09e1a2c
PS
678
679 if (check_browser_version('MSIE', 5)) {
680 // lalala, IE does not allow more than 31 linked CSS files from main document
71be124d 681 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name, 'type'=>'ie', 'subtype'=>'plugins'));
a6338a13
SH
682 foreach ($css['parents'] as $parent=>$sheets) {
683 // We need to serve parents individually otherwise we may easily exceed the style limit IE imposes (4096)
71be124d 684 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'ie', 'subtype'=>'parents', 'sheet'=>$parent));
a6338a13 685 }
71be124d 686 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name, 'type'=>'ie', 'subtype'=>'theme'));
c09e1a2c
PS
687
688 } else {
689 foreach ($css['plugins'] as $plugin=>$unused) {
690 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'plugin', 'subtype'=>$plugin));
691 }
692 foreach ($css['parents'] as $parent=>$sheets) {
693 foreach ($sheets as $sheet=>$unused2) {
694 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'parent', 'subtype'=>$parent, 'sheet'=>$sheet));
695 }
696 }
697 foreach ($css['theme'] as $sheet=>$unused) {
698 $urls[] = new moodle_url($baseurl, array('sheet'=>$sheet, 'theme'=>$this->name, 'type'=>'theme')); // sheet first in order to make long urls easier to read
78946b9b 699 }
78946b9b 700 }
78946b9b 701 }
efaa4c08
PS
702
703 return $urls;
571fa828 704 }
34a2777c 705
ebebf55c 706 /**
045f492c 707 * Returns an array of organised CSS files required for this output
473dd811 708 *
045f492c 709 * @return array
ebebf55c 710 */
045f492c
SH
711 public function css_files() {
712 $cssfiles = array('plugins'=>array(), 'parents'=>array(), 'theme'=>array());
78946b9b 713
8c96c3cb 714 // get all plugin sheets
045f492c 715 $excludes = $this->resolve_excludes('plugins_exclude_sheets');
78946b9b
PS
716 if ($excludes !== true) {
717 foreach (get_plugin_types() as $type=>$unused) {
045f492c 718 if ($type === 'theme' || (!empty($excludes[$type]) and $excludes[$type] === true)) {
78946b9b
PS
719 continue;
720 }
721 $plugins = get_plugin_list($type);
722 foreach ($plugins as $plugin=>$fulldir) {
723 if (!empty($excludes[$type]) and is_array($excludes[$type])
724 and in_array($plugin, $excludes[$type])) {
725 continue;
726 }
f8bb9666
SH
727
728 $plugincontent = '';
78946b9b
PS
729 $sheetfile = "$fulldir/styles.css";
730 if (is_readable($sheetfile)) {
045f492c 731 $cssfiles['plugins'][$type.'_'.$plugin] = $sheetfile;
f8bb9666
SH
732 }
733 $sheetthemefile = "$fulldir/styles_{$this->name}.css";
734 if (is_readable($sheetthemefile)) {
045f492c 735 $cssfiles['plugins'][$type.'_'.$plugin.'_'.$this->name] = $sheetthemefile;
f8bb9666 736 }
78946b9b
PS
737 }
738 }
739 }
34a2777c 740
78946b9b 741 // find out wanted parent sheets
045f492c 742 $excludes = $this->resolve_excludes('parents_exclude_sheets');
78946b9b
PS
743 if ($excludes !== true) {
744 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
745 $parent = $parent_config->name;
045f492c 746 if (empty($parent_config->sheets) || (!empty($excludes[$parent]) and $excludes[$parent] === true)) {
78946b9b
PS
747 continue;
748 }
749 foreach ($parent_config->sheets as $sheet) {
750 if (!empty($excludes[$parent]) and is_array($excludes[$parent])
751 and in_array($sheet, $excludes[$parent])) {
752 continue;
753 }
754 $sheetfile = "$parent_config->dir/style/$sheet.css";
755 if (is_readable($sheetfile)) {
045f492c 756 $cssfiles['parents'][$parent][$sheet] = $sheetfile;
78946b9b
PS
757 }
758 }
759 }
760 }
34a2777c 761
78946b9b
PS
762 // current theme sheets
763 if (is_array($this->sheets)) {
764 foreach ($this->sheets as $sheet) {
765 $sheetfile = "$this->dir/style/$sheet.css";
766 if (is_readable($sheetfile)) {
045f492c 767 $cssfiles['theme'][$sheet] = $sheetfile;
78946b9b 768 }
ebebf55c 769 }
78946b9b
PS
770 }
771
045f492c
SH
772 return $cssfiles;
773 }
774
775 /**
776 * Returns the content of the one huge CSS merged from all style sheets.
473dd811 777 *
b305d623 778 * @param css_optimiser|null $optimiser A CSS optimiser to use during on the content. Null = don't optimise
045f492c
SH
779 * @return string
780 */
b305d623 781 public function css_content(css_optimiser $optimiser = null) {
71be124d 782 $files = array_merge($this->css_files(), array('editor'=>$this->editor_css_files()));
b305d623 783 $css = $this->css_files_get_contents($files, array(), $optimiser);
78946b9b
PS
784 return $css;
785 }
34a2777c 786
045f492c
SH
787 /**
788 * Given an array of file paths or a single file path loads the contents of
789 * the CSS file, processes it then returns it in the same structure it was given.
790 *
f8129210 791 * Can be used recursively on the results of {@link css_files}
045f492c
SH
792 *
793 * @param array|string $file An array of file paths or a single file path
71c03ac1 794 * @param array $keys An array of previous array keys [recursive addition]
b305d623 795 * @param css_optimiser|null $optimiser A CSS optimiser to use during on the content. Null = don't optimise
045f492c
SH
796 * @return The converted array or the contents of the single file ($file type)
797 */
b305d623
SH
798 protected function css_files_get_contents($file, array $keys, css_optimiser $optimiser = null) {
799 global $CFG;
045f492c
SH
800 if (is_array($file)) {
801 foreach ($file as $key=>$f) {
b305d623 802 $file[$key] = $this->css_files_get_contents($f, array_merge($keys, array($key)), $optimiser);
045f492c
SH
803 }
804 return $file;
805 } else {
b305d623
SH
806 $contents = file_get_contents($file);
807 $contents = $this->post_process($contents);
045f492c 808 $comment = '/** Path: '.implode(' ', $keys).' **/'."\n";
b305d623
SH
809 $stats = '';
810 if (!is_null($optimiser)) {
811 $contents = $optimiser->process($contents);
812 if (!empty($CFG->cssoptimiserstats)) {
813 $stats = $optimiser->output_stats_css();
814 }
815 }
816 return $comment.$stats.$contents;
045f492c
SH
817 }
818 }
819
e68c5f36
PS
820
821 /**
473dd811
SH
822 * Generate a URL to the file that serves theme JavaScript files.
823 *
71c03ac1 824 * @param bool $inhead true means head url, false means footer
e68c5f36
PS
825 * @return moodle_url
826 */
baeb20bb 827 public function javascript_url($inhead) {
e68c5f36
PS
828 global $CFG;
829
830 $rev = theme_get_revision();
e68c5f36 831 $params = array('theme'=>$this->name,'rev'=>$rev);
baeb20bb
PS
832 $params['type'] = $inhead ? 'head' : 'footer';
833
ecbad2ad
PS
834 if (!empty($CFG->slasharguments) and $rev > 0) {
835 $url = new moodle_url("$CFG->httpswwwroot/theme/javascript.php");
836 $url->set_slashargument('/'.$this->name.'/'.$rev.'/'.$params['type'], 'noparam', true);
837 return $url;
838 } else {
839 return new moodle_url($CFG->httpswwwroot.'/theme/javascript.php', $params);
840 }
e68c5f36
PS
841 }
842
473dd811
SH
843 /**
844 * Get the URL's for the JavaScript files used by this theme.
845 * They won't be served directly, instead they'll be mediated through
846 * theme/javascript.php.
847 *
848 * @param string $type Either javascripts_footer, or javascripts
849 * @return array
850 */
045f492c
SH
851 public function javascript_files($type) {
852 if ($type === 'footer') {
853 $type = 'javascripts_footer';
854 } else {
f856106b 855 $type = 'javascripts';
045f492c 856 }
04c01408 857
358c13cb
PS
858 $js = array();
859 // find out wanted parent javascripts
045f492c 860 $excludes = $this->resolve_excludes('parents_exclude_javascripts');
358c13cb
PS
861 if ($excludes !== true) {
862 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
863 $parent = $parent_config->name;
04c01408 864 if (empty($parent_config->$type)) {
358c13cb
PS
865 continue;
866 }
867 if (!empty($excludes[$parent]) and $excludes[$parent] === true) {
868 continue;
869 }
04c01408 870 foreach ($parent_config->$type as $javascript) {
358c13cb
PS
871 if (!empty($excludes[$parent]) and is_array($excludes[$parent])
872 and in_array($javascript, $excludes[$parent])) {
873 continue;
874 }
875 $javascriptfile = "$parent_config->dir/javascript/$javascript.js";
876 if (is_readable($javascriptfile)) {
045f492c 877 $js[] = $javascriptfile;
358c13cb
PS
878 }
879 }
880 }
881 }
882
883 // current theme javascripts
04c01408
PS
884 if (is_array($this->$type)) {
885 foreach ($this->$type as $javascript) {
358c13cb
PS
886 $javascriptfile = "$this->dir/javascript/$javascript.js";
887 if (is_readable($javascriptfile)) {
045f492c
SH
888 $js[] = $javascriptfile;
889 }
890 }
891 }
892
893 return $js;
894 }
895
896 /**
3d3fae72 897 * Resolves an exclude setting to the themes setting is applicable or the
045f492c
SH
898 * setting of its closest parent.
899 *
900 * @param string $variable The name of the setting the exclude setting to resolve
f8129210 901 * @param string $default
045f492c
SH
902 * @return mixed
903 */
f8129210 904 protected function resolve_excludes($variable, $default = null) {
045f492c
SH
905 $setting = $default;
906 if (is_array($this->{$variable}) or $this->{$variable} === true) {
907 $setting = $this->{$variable};
908 } else {
909 foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last
910 if (!isset($parent_config->{$variable})) {
911 continue;
912 }
913 if (is_array($parent_config->{$variable}) or $parent_config->{$variable} === true) {
914 $setting = $parent_config->{$variable};
915 break;
358c13cb
PS
916 }
917 }
918 }
045f492c
SH
919 return $setting;
920 }
358c13cb 921
045f492c
SH
922 /**
923 * Returns the content of the one huge javascript file merged from all theme javascript files.
473dd811 924 *
f8129210 925 * @param bool $type
045f492c
SH
926 * @return string
927 */
928 public function javascript_content($type) {
929 $jsfiles = $this->javascript_files($type);
930 $js = '';
931 foreach ($jsfiles as $jsfile) {
932 $js .= file_get_contents($jsfile)."\n";
933 }
934 return $js;
e68c5f36
PS
935 }
936
473dd811
SH
937 /**
938 * Post processes CSS.
939 *
940 * This method post processes all of the CSS before it is served for this theme.
941 * This is done so that things such as image URL's can be swapped in and to
942 * run any specific CSS post process method the theme has requested.
3d3fae72 943 * This allows themes to use CSS settings.
473dd811
SH
944 *
945 * @param string $css The CSS to process.
946 * @return string The processed CSS.
947 */
045f492c 948 public function post_process($css) {
78946b9b
PS
949 // now resolve all image locations
950 if (preg_match_all('/\[\[pix:([a-z_]+\|)?([^\]]+)\]\]/', $css, $matches, PREG_SET_ORDER)) {
f7d6a556
SH
951 // We are going to disable the use of SVG images when available in CSS background-image properties
952 // as support for it in browsers is at best quirky.
953 // When we choose to support SVG in background css we will need to remove this code and implement a solution that is
954 // either consistent or varies the URL for serving CSS depending upon SVG being used if available, or not.
0f817d0d 955 $originalsvguse = $this->use_svg_icons();
f7d6a556 956 $this->force_svg_use(false);
78946b9b
PS
957 $replaced = array();
958 foreach ($matches as $match) {
959 if (isset($replaced[$match[0]])) {
960 continue;
961 }
962 $replaced[$match[0]] = true;
963 $imagename = $match[2];
964 $component = rtrim($match[1], '|');
b9bc2019 965 $imageurl = $this->pix_url($imagename, $component)->out(false);
df06c1c4 966 // we do not need full url because the image.php is always in the same dir
9d473266 967 $imageurl = preg_replace('|^http.?://[^/]+|', '', $imageurl);
df06c1c4 968 $css = str_replace($match[0], $imageurl, $css);
ebebf55c 969 }
0f817d0d 970 $this->force_svg_use($originalsvguse);
34a2777c 971 }
17a6649b 972
78946b9b
PS
973 // now resolve all theme settings or do any other postprocessing
974 $csspostprocess = $this->csspostprocess;
975 if (function_exists($csspostprocess)) {
976 $css = $csspostprocess($css, $this);
fdeb7fa1 977 }
978
78946b9b 979 return $css;
ebebf55c 980 }
17a6649b 981
ebebf55c 982 /**
78946b9b
PS
983 * Return the URL for an image
984 *
985 * @param string $imagename the name of the icon.
f8129210 986 * @param string $component specification of one plugin like in get_string()
78946b9b 987 * @return moodle_url
fdeb7fa1 988 */
c39e5ba2 989 public function pix_url($imagename, $component) {
fdeb7fa1 990 global $CFG;
78946b9b 991
9d473266 992 $params = array('theme'=>$this->name);
436dbeec 993 $svg = $this->use_svg_icons();
9d473266
PS
994
995 if (empty($component) or $component === 'moodle' or $component === 'core') {
996 $params['component'] = 'core';
997 } else {
998 $params['component'] = $component;
999 }
78946b9b
PS
1000
1001 $rev = theme_get_revision();
1002 if ($rev != -1) {
1003 $params['rev'] = $rev;
1004 }
9d473266
PS
1005
1006 $params['image'] = $imagename;
1007
436dbeec 1008 $url = new moodle_url("$CFG->httpswwwroot/theme/image.php");
9d473266 1009 if (!empty($CFG->slasharguments) and $rev > 0) {
436dbeec
SH
1010 $path = '/'.$params['theme'].'/'.$params['component'].'/'.$params['rev'].'/'.$params['image'];
1011 if (!$svg) {
1012 // We add a simple /_s to the start of the path.
1013 // The underscore is used to ensure that it isn't a valid theme name.
1014 $path = '/_s'.$path;
1015 }
1016 $url->set_slashargument($path, 'noparam', true);
9d473266 1017 } else {
436dbeec
SH
1018 if (!$svg) {
1019 // We add an SVG param so that we know not to serve SVG images.
1020 // We do this because all modern browsers support SVG and this param will one day be removed.
1021 $params['svg'] = '0';
1022 }
1023 $url->params($params);
fdeb7fa1 1024 }
78946b9b 1025
9d473266 1026 return $url;
fdeb7fa1 1027 }
1028
1029 /**
78946b9b 1030 * Resolves the real image location.
436dbeec
SH
1031 *
1032 * $svg was introduced as an arg in 2.4. It is important because not all supported browsers support the use of SVG
1033 * and we need a way in which to turn it off.
1034 * By default SVG won't be used unless asked for. This is done for two reasons:
1035 * 1. It ensures that we don't serve svg images unless we really want to. The admin has selected to force them, of the users
1036 * browser supports SVG.
1037 * 2. We only serve SVG images from locations we trust. This must NOT include any areas where the image may have been uploaded
1038 * by the user due to security concerns.
1039 *
78946b9b
PS
1040 * @param string $image name of image, may contain relative path
1041 * @param string $component
436dbeec 1042 * @param bool $svg If set to true SVG images will also be looked for.
78946b9b 1043 * @return string full file path
fdeb7fa1 1044 */
436dbeec 1045 public function resolve_image_location($image, $component, $svg = false) {
78946b9b
PS
1046 global $CFG;
1047
436dbeec
SH
1048 if (!is_bool($svg)) {
1049 // If $svg isn't a bool then we need to decide for ourselves.
1050 $svg = $this->use_svg_icons();
1051 }
1052
78946b9b 1053 if ($component === 'moodle' or $component === 'core' or empty($component)) {
436dbeec 1054 if ($imagefile = $this->image_exists("$this->dir/pix_core/$image", $svg)) {
78946b9b
PS
1055 return $imagefile;
1056 }
1057 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
436dbeec 1058 if ($imagefile = $this->image_exists("$parent_config->dir/pix_core/$image", $svg)) {
78946b9b
PS
1059 return $imagefile;
1060 }
1061 }
436dbeec 1062 if ($imagefile = $this->image_exists("$CFG->dataroot/pix/$image", $svg)) {
f930cf6b
EM
1063 return $imagefile;
1064 }
436dbeec 1065 if ($imagefile = $this->image_exists("$CFG->dirroot/pix/$image", $svg)) {
78946b9b
PS
1066 return $imagefile;
1067 }
1068 return null;
1069
1070 } else if ($component === 'theme') { //exception
1071 if ($image === 'favicon') {
1072 return "$this->dir/pix/favicon.ico";
1073 }
436dbeec 1074 if ($imagefile = $this->image_exists("$this->dir/pix/$image", $svg)) {
78946b9b
PS
1075 return $imagefile;
1076 }
1077 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
436dbeec 1078 if ($imagefile = $this->image_exists("$parent_config->dir/pix/$image", $svg)) {
78946b9b
PS
1079 return $imagefile;
1080 }
1081 }
1082 return null;
1083
78946b9b
PS
1084 } else {
1085 if (strpos($component, '_') === false) {
1086 $component = 'mod_'.$component;
1087 }
1088 list($type, $plugin) = explode('_', $component, 2);
1089
436dbeec 1090 if ($imagefile = $this->image_exists("$this->dir/pix_plugins/$type/$plugin/$image", $svg)) {
78946b9b
PS
1091 return $imagefile;
1092 }
1093 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
436dbeec 1094 if ($imagefile = $this->image_exists("$parent_config->dir/pix_plugins/$type/$plugin/$image", $svg)) {
78946b9b
PS
1095 return $imagefile;
1096 }
1097 }
436dbeec 1098 if ($imagefile = $this->image_exists("$CFG->dataroot/pix_plugins/$type/$plugin/$image", $svg)) {
f930cf6b
EM
1099 return $imagefile;
1100 }
78946b9b 1101 $dir = get_plugin_directory($type, $plugin);
436dbeec 1102 if ($imagefile = $this->image_exists("$dir/pix/$image", $svg)) {
78946b9b
PS
1103 return $imagefile;
1104 }
1105 return null;
fdeb7fa1 1106 }
fdeb7fa1 1107 }
1108
436dbeec
SH
1109 /**
1110 * Return true if we should look for SVG images as well.
1111 *
1112 * @staticvar bool $svg
1113 * @return bool
1114 */
1115 public function use_svg_icons() {
1116 global $CFG;
1117 if ($this->usesvg === null) {
1118 if (!isset($CFG->svgicons) || !is_bool($CFG->svgicons)) {
1119 // IE 5 - 8 don't support SVG at all.
1120 if (empty($_SERVER['HTTP_USER_AGENT'])) {
1121 // Can't be sure, just say no.
1122 $this->usesvg = false;
3c1405a1 1123 } else if (check_browser_version('MSIE', 0) and !check_browser_version('MSIE', 9)) {
436dbeec
SH
1124 // IE < 9 doesn't support SVG. Say no.
1125 $this->usesvg = false;
1126 } else if (preg_match('#Android +[0-2]\.#', $_SERVER['HTTP_USER_AGENT'])) {
1127 // Android < 3 doesn't support SVG. Say no.
1128 $this->usesvg = false;
fbbf043c
FM
1129 } else if (check_browser_version('Opera', 0)) {
1130 // Opera 12 still does not support SVG well enough. Say no.
1131 $this->usesvg = false;
436dbeec
SH
1132 } else {
1133 // Presumed fine.
1134 $this->usesvg = true;
1135 }
1136 } else {
1137 // Force them on/off depending upon the setting.
1138 $this->usesvg = $CFG->svgicons;
1139 }
1140 }
1141 return $this->usesvg;
1142 }
1143
f7d6a556
SH
1144 /**
1145 * Forces the usesvg setting to either true or false, avoiding any decision making.
1146 *
1147 * This function should only ever be used when absolutely required, and before any generation of image URL's has occurred.
1148 *
1149 * @param bool $setting True to force the use of svg when available, null otherwise.
1150 */
1151 private function force_svg_use($setting) {
1152 $this->usesvg = (bool)$setting;
1153 }
1154
d4a03c00 1155 /**
78946b9b 1156 * Checks if file with any image extension exists.
473dd811 1157 *
436dbeec
SH
1158 * The order to these images was adjusted prior to the release of 2.4
1159 * At that point the were the following image counts in Moodle core:
1160 *
1161 * - png = 667 in pix dirs (1499 total)
1162 * - gif = 385 in pix dirs (606 total)
1163 * - jpg = 62 in pix dirs (74 total)
1164 * - jpeg = 0 in pix dirs (1 total)
1165 *
1166 * There is work in progress to move towards SVG presently hence that has been prioritiesed.
1167 *
78946b9b 1168 * @param string $filepath
436dbeec 1169 * @param bool $svg If set to true SVG images will also be looked for.
78946b9b 1170 * @return string image name with extension
d4a03c00 1171 */
436dbeec
SH
1172 private static function image_exists($filepath, $svg = false) {
1173 if ($svg && file_exists("$filepath.svg")) {
1174 return "$filepath.svg";
78946b9b
PS
1175 } else if (file_exists("$filepath.png")) {
1176 return "$filepath.png";
436dbeec
SH
1177 } else if (file_exists("$filepath.gif")) {
1178 return "$filepath.gif";
78946b9b
PS
1179 } else if (file_exists("$filepath.jpg")) {
1180 return "$filepath.jpg";
1181 } else if (file_exists("$filepath.jpeg")) {
1182 return "$filepath.jpeg";
d4a03c00 1183 } else {
78946b9b 1184 return false;
d4a03c00 1185 }
1186 }
1187
fdeb7fa1 1188 /**
78946b9b 1189 * Loads the theme config from config.php file.
473dd811 1190 *
78946b9b 1191 * @param string $themename
473dd811
SH
1192 * @param stdClass $settings from config_plugins table
1193 * @return stdClass The theme configuration
fdeb7fa1 1194 */
78946b9b
PS
1195 private static function find_theme_config($themename, $settings) {
1196 // We have to use the variable name $THEME (upper case) because that
1197 // is what is used in theme config.php files.
fdeb7fa1 1198
78946b9b
PS
1199 if (!$dir = theme_config::find_theme_location($themename)) {
1200 return null;
fdeb7fa1 1201 }
2f67a9b3 1202
365a5941 1203 $THEME = new stdClass();
78946b9b
PS
1204 $THEME->name = $themename;
1205 $THEME->dir = $dir;
1206 $THEME->settings = $settings;
1207
1208 global $CFG; // just in case somebody tries to use $CFG in theme config
1209 include("$THEME->dir/config.php");
1210
1211 // verify the theme configuration is OK
1212 if (!is_array($THEME->parents)) {
1213 // parents option is mandatory now
1214 return null;
fdeb7fa1 1215 }
1216
78946b9b 1217 return $THEME;
fdeb7fa1 1218 }
1219
d4a03c00 1220 /**
78946b9b
PS
1221 * Finds the theme location and verifies the theme has all needed files
1222 * and is not obsoleted.
473dd811 1223 *
78946b9b
PS
1224 * @param string $themename
1225 * @return string full dir path or null if not found
d4a03c00 1226 */
78946b9b
PS
1227 private static function find_theme_location($themename) {
1228 global $CFG;
1229
1230 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
1231 $dir = "$CFG->dirroot/theme/$themename";
d4a03c00 1232
3dd1d7e7
DM
1233 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
1234 $dir = "$CFG->themedir/$themename";
1235
7d875874 1236 } else {
78946b9b 1237 return null;
d4a03c00 1238 }
78946b9b
PS
1239
1240 if (file_exists("$dir/styles.php")) {
1241 //legacy theme - needs to be upgraded - upgrade info is displayed on the admin settings page
1242 return null;
1243 }
2f67a9b3 1244
78946b9b 1245 return $dir;
d4a03c00 1246 }
1247
1d13c75c 1248 /**
78946b9b 1249 * Get the renderer for a part of Moodle for this theme.
473dd811 1250 *
78946b9b 1251 * @param moodle_page $page the page we are rendering
f8129210 1252 * @param string $component the name of part of moodle. E.g. 'core', 'quiz', 'qtype_multichoice'.
78946b9b 1253 * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
c927e35c 1254 * @param string $target one of rendering target constants
78946b9b 1255 * @return renderer_base the requested renderer.
1d13c75c 1256 */
c927e35c 1257 public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null) {
78946b9b 1258 if (is_null($this->rf)) {
c927e35c 1259 $classname = $this->rendererfactory;
78946b9b 1260 $this->rf = new $classname($this);
1d13c75c 1261 }
78946b9b 1262
c927e35c 1263 return $this->rf->get_renderer($page, $component, $subtype, $target);
1d13c75c 1264 }
1265
fdeb7fa1 1266 /**
78946b9b 1267 * Get the information from {@link $layouts} for this type of page.
473dd811 1268 *
78946b9b
PS
1269 * @param string $pagelayout the the page layout name.
1270 * @return array the appropriate part of {@link $layouts}.
fdeb7fa1 1271 */
78946b9b
PS
1272 protected function layout_info_for_page($pagelayout) {
1273 if (array_key_exists($pagelayout, $this->layouts)) {
1274 return $this->layouts[$pagelayout];
1275 } else {
191b267b
PS
1276 debugging('Invalid page layout specified: ' . $pagelayout);
1277 return $this->layouts['standard'];
fdeb7fa1 1278 }
1279 }
1280
1281 /**
78946b9b
PS
1282 * Given the settings of this theme, and the page pagelayout, return the
1283 * full path of the page layout file to use.
1284 *
1285 * Used by {@link core_renderer::header()}.
1286 *
1287 * @param string $pagelayout the the page layout name.
1288 * @return string Full path to the lyout file to use
ebebf55c 1289 */
78946b9b 1290 public function layout_file($pagelayout) {
ebebf55c 1291 global $CFG;
fdeb7fa1 1292
78946b9b
PS
1293 $layoutinfo = $this->layout_info_for_page($pagelayout);
1294 $layoutfile = $layoutinfo['file'];
fdeb7fa1 1295
84db3ea2
SH
1296 if (array_key_exists('theme', $layoutinfo)) {
1297 $themes = array($layoutinfo['theme']);
1298 } else {
1299 $themes = array_merge(array($this->name),$this->parents);
1300 }
4399b9d5 1301
84db3ea2
SH
1302 foreach ($themes as $theme) {
1303 if ($dir = $this->find_theme_location($theme)) {
1304 $path = "$dir/layout/$layoutfile";
1305
1306 // Check the template exists, return general base theme template if not.
1307 if (is_readable($path)) {
1308 return $path;
1309 }
78946b9b 1310 }
34a2777c 1311 }
1312
191b267b 1313 debugging('Can not find layout file for: ' . $pagelayout);
78946b9b
PS
1314 // fallback to standard normal layout
1315 return "$CFG->dirroot/theme/base/layout/general.php";
1316 }
ebebf55c 1317
78946b9b
PS
1318 /**
1319 * Returns auxiliary page layout options specified in layout configuration array.
473dd811 1320 *
78946b9b
PS
1321 * @param string $pagelayout
1322 * @return array
1323 */
1324 public function pagelayout_options($pagelayout) {
1325 $info = $this->layout_info_for_page($pagelayout);
1326 if (!empty($info['options'])) {
1327 return $info['options'];
34a2777c 1328 }
78946b9b
PS
1329 return array();
1330 }
34a2777c 1331
78946b9b
PS
1332 /**
1333 * Inform a block_manager about the block regions this theme wants on this
1334 * page layout.
473dd811 1335 *
78946b9b
PS
1336 * @param string $pagelayout the general type of the page.
1337 * @param block_manager $blockmanager the block_manger to set up.
78946b9b
PS
1338 */
1339 public function setup_blocks($pagelayout, $blockmanager) {
1340 $layoutinfo = $this->layout_info_for_page($pagelayout);
1341 if (!empty($layoutinfo['regions'])) {
1342 $blockmanager->add_regions($layoutinfo['regions']);
1343 $blockmanager->set_default_region($layoutinfo['defaultregion']);
ebebf55c 1344 }
34a2777c 1345 }
1346
473dd811
SH
1347 /**
1348 * Gets the visible name for the requested block region.
1349 *
1350 * @param string $region The region name to get
1351 * @param string $theme The theme the region belongs to (may come from the parent theme)
1352 * @return string
1353 */
cadc0d28
TH
1354 protected function get_region_name($region, $theme) {
1355 $regionstring = get_string('region-' . $region, 'theme_' . $theme);
1356 // A name exists in this theme, so use it
1357 if (substr($regionstring, 0, 1) != '[') {
1358 return $regionstring;
1359 }
1360
1361 // Otherwise, try to find one elsewhere
1362 // Check parents, if any
1363 foreach ($this->parents as $parentthemename) {
1364 $regionstring = get_string('region-' . $region, 'theme_' . $parentthemename);
1365 if (substr($regionstring, 0, 1) != '[') {
1366 return $regionstring;
1367 }
1368 }
1369
1370 // Last resort, try the base theme for names
1371 return get_string('region-' . $region, 'theme_base');
1372 }
1373
ebebf55c 1374 /**
78946b9b 1375 * Get the list of all block regions known to this theme in all templates.
473dd811 1376 *
78946b9b 1377 * @return array internal region name => human readable name.
ebebf55c 1378 */
78946b9b
PS
1379 public function get_all_block_regions() {
1380 $regions = array();
78946b9b
PS
1381 foreach ($this->layouts as $layoutinfo) {
1382 foreach ($layoutinfo['regions'] as $region) {
a9535f79 1383 $regions[$region] = $this->get_region_name($region, $this->name);
ebebf55c 1384 }
34a2777c 1385 }
78946b9b 1386 return $regions;
34a2777c 1387 }
d609d962
SH
1388
1389 /**
1390 * Returns the human readable name of the theme
1391 *
1392 * @return string
1393 */
1394 public function get_theme_name() {
1395 return get_string('pluginname', 'theme_'.$this->name);
1396 }
ebebf55c 1397}
34a2777c 1398
ebebf55c 1399/**
d9c8f425 1400 * This class keeps track of which HTML tags are currently open.
1401 *
1402 * This makes it much easier to always generate well formed XHTML output, even
1403 * if execution terminates abruptly. Any time you output some opening HTML
1404 * without the matching closing HTML, you should push the necessary close tags
1405 * onto the stack.
ebebf55c 1406 *
1407 * @copyright 2009 Tim Hunt
473dd811
SH
1408 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1409 * @since Moodle 2.0
f8129210 1410 * @package core
76be40cc 1411 * @category output
ebebf55c 1412 */
d9c8f425 1413class xhtml_container_stack {
473dd811
SH
1414
1415 /**
76be40cc 1416 * @var array Stores the list of open containers.
473dd811 1417 */
d9c8f425 1418 protected $opencontainers = array();
473dd811 1419
fa1afe32 1420 /**
76be40cc 1421 * @var array In developer debug mode, stores a stack trace of all opens and
d9c8f425 1422 * closes, so we can output helpful error messages when there is a mismatch.
fa1afe32 1423 */
d9c8f425 1424 protected $log = array();
473dd811 1425
fa1afe32 1426 /**
76be40cc
SH
1427 * @var boolean Store whether we are developer debug mode. We need this in
1428 * several places including in the destructor where we may not have access to $CFG.
fa1afe32 1429 */
d9c8f425 1430 protected $isdebugging;
34a2777c 1431
473dd811
SH
1432 /**
1433 * Constructor
1434 */
d9c8f425 1435 public function __construct() {
1436 $this->isdebugging = debugging('', DEBUG_DEVELOPER);
ebebf55c 1437 }
34a2777c 1438
fa1afe32 1439 /**
d9c8f425 1440 * Push the close HTML for a recently opened container onto the stack.
473dd811 1441 *
d9c8f425 1442 * @param string $type The type of container. This is checked when {@link pop()}
1443 * is called and must match, otherwise a developer debug warning is output.
1444 * @param string $closehtml The HTML required to close the container.
fa1afe32 1445 */
d9c8f425 1446 public function push($type, $closehtml) {
1447 $container = new stdClass;
1448 $container->type = $type;
1449 $container->closehtml = $closehtml;
1450 if ($this->isdebugging) {
1451 $this->log('Open', $type);
3aaa27f4 1452 }
d9c8f425 1453 array_push($this->opencontainers, $container);
ebebf55c 1454 }
34a2777c 1455
fa1afe32 1456 /**
d9c8f425 1457 * Pop the HTML for the next closing container from the stack. The $type
1458 * must match the type passed when the container was opened, otherwise a
1459 * warning will be output.
473dd811 1460 *
d9c8f425 1461 * @param string $type The type of container.
1462 * @return string the HTML required to close the container.
fa1afe32 1463 */
d9c8f425 1464 public function pop($type) {
1465 if (empty($this->opencontainers)) {
1466 debugging('<p>There are no more open containers. This suggests there is a nesting problem.</p>' .
1467 $this->output_log(), DEBUG_DEVELOPER);
1468 return;
3aaa27f4 1469 }
ebebf55c 1470
d9c8f425 1471 $container = array_pop($this->opencontainers);
1472 if ($container->type != $type) {
1473 debugging('<p>The type of container to be closed (' . $container->type .
1474 ') does not match the type of the next open container (' . $type .
1475 '). This suggests there is a nesting problem.</p>' .
1476 $this->output_log(), DEBUG_DEVELOPER);
ebebf55c 1477 }
d9c8f425 1478 if ($this->isdebugging) {
1479 $this->log('Close', $type);
e8775320 1480 }
d9c8f425 1481 return $container->closehtml;
ebebf55c 1482 }
e8775320 1483
fa1afe32 1484 /**
d9c8f425 1485 * Close all but the last open container. This is useful in places like error
1486 * handling, where you want to close all the open containers (apart from <body>)
1487 * before outputting the error message.
473dd811 1488 *
d9c8f425 1489 * @param bool $shouldbenone assert that the stack should be empty now - causes a
1490 * developer debug warning if it isn't.
1491 * @return string the HTML required to close any open containers inside <body>.
fa1afe32 1492 */
d9c8f425 1493 public function pop_all_but_last($shouldbenone = false) {
1494 if ($shouldbenone && count($this->opencontainers) != 1) {
1495 debugging('<p>Some HTML tags were opened in the body of the page but not closed.</p>' .
1496 $this->output_log(), DEBUG_DEVELOPER);
1497 }
1498 $output = '';
1499 while (count($this->opencontainers) > 1) {
1500 $container = array_pop($this->opencontainers);
1501 $output .= $container->closehtml;
e8775320 1502 }
d9c8f425 1503 return $output;
e8775320 1504 }
34a2777c 1505
ebebf55c 1506 /**
d9c8f425 1507 * You can call this function if you want to throw away an instance of this
1508 * class without properly emptying the stack (for example, in a unit test).
1509 * Calling this method stops the destruct method from outputting a developer
1510 * debug warning. After calling this method, the instance can no longer be used.
ebebf55c 1511 */
d9c8f425 1512 public function discard() {
1513 $this->opencontainers = null;
ebebf55c 1514 }
d9c8f425 1515
fa1afe32 1516 /**
d9c8f425 1517 * Adds an entry to the log.
473dd811 1518 *
d9c8f425 1519 * @param string $action The name of the action
1520 * @param string $type The type of action
fa1afe32 1521 */
d9c8f425 1522 protected function log($action, $type) {
1523 $this->log[] = '<li>' . $action . ' ' . $type . ' at:' .
1524 format_backtrace(debug_backtrace()) . '</li>';
ebebf55c 1525 }
34a2777c 1526
fa1afe32 1527 /**
d9c8f425 1528 * Outputs the log's contents as a HTML list.
473dd811 1529 *
d9c8f425 1530 * @return string HTML list of the log
fa1afe32 1531 */
d9c8f425 1532 protected function output_log() {
1533 return '<ul>' . implode("\n", $this->log) . '</ul>';
34a2777c 1534 }
6fc267a0 1535}