forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutputlib.php
1530 lines (1379 loc) · 57.6 KB
/
outputlib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Functions for generating the HTML that Moodle should output.
*
* Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
* for an overview.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package core
* @category output
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/outputcomponents.php');
require_once($CFG->libdir.'/outputactions.php');
require_once($CFG->libdir.'/outputfactories.php');
require_once($CFG->libdir.'/outputrenderers.php');
require_once($CFG->libdir.'/outputrequirementslib.php');
/**
* Invalidate all server and client side caches.
*
* This method deletes the physical directory that is used to cache the theme
* files used for serving.
* Because it deletes the main theme cache directory all themes are reset by
* this function.
*/
function theme_reset_all_caches() {
global $CFG;
require_once("$CFG->libdir/filelib.php");
$next = time();
if (isset($CFG->themerev) and $next <= $CFG->themerev and $CFG->themerev - $next < 60*60) {
// This resolves problems when reset is requested repeatedly within 1s,
// the < 1h condition prevents accidental switching to future dates
// because we might not recover from it.
$next = $CFG->themerev+1;
}
set_config('themerev', $next); // time is unique even when you reset/switch database
fulldelete("$CFG->cachedir/theme");
}
/**
* Enable or disable theme designer mode.
*
* @param bool $state
*/
function theme_set_designer_mod($state) {
theme_reset_all_caches();
set_config('themedesignermode', (int)!empty($state));
}
/**
* Returns current theme revision number.
*
* @return int
*/
function theme_get_revision() {
global $CFG;
if (empty($CFG->themedesignermode)) {
if (empty($CFG->themerev)) {
return -1;
} else {
return $CFG->themerev;
}
} else {
return -1;
}
}
/**
* This class represents the configuration variables of a Moodle theme.
*
* All the variables with access: public below (with a few exceptions that are marked)
* are the properties you can set in your themes config.php file.
*
* There are also some methods and protected variables that are part of the inner
* workings of Moodle's themes system. If you are just editing a themes config.php
* file, you can just ignore those, and the following information for developers.
*
* Normally, to create an instance of this class, you should use the
* {@link theme_config::load()} factory method to load a themes config.php file.
* However, normally you don't need to bother, because moodle_page (that is, $PAGE)
* will create one for you, accessible as $PAGE->theme.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
* @package core
* @category output
*/
class theme_config {
/**
* @var string Default theme, used when requested theme not found.
*/
const DEFAULT_THEME = 'standard';
/**
* @var array You can base your theme on other themes by linking to the other theme as
* parents. This lets you use the CSS and layouts from the other themes
* (see {@link theme_config::$layouts}).
* That makes it easy to create a new theme that is similar to another one
* but with a few changes. In this themes CSS you only need to override
* those rules you want to change.
*/
public $parents;
/**
* @var array The names of all the stylesheets from this theme that you would
* like included, in order. Give the names of the files without .css.
*/
public $sheets = array();
/**
* @var array The names of all the stylesheets from parents that should be excluded.
* true value may be used to specify all parents or all themes from one parent.
* If no value specified value from parent theme used.
*/
public $parents_exclude_sheets = null;
/**
* @var array List of plugin sheets to be excluded.
* If no value specified value from parent theme used.
*/
public $plugins_exclude_sheets = null;
/**
* @var array List of style sheets that are included in the text editor bodies.
* Sheets from parent themes are used automatically and can not be excluded.
*/
public $editor_sheets = array();
/**
* @var array The names of all the javascript files this theme that you would
* like included from head, in order. Give the names of the files without .js.
*/
public $javascripts = array();
/**
* @var array The names of all the javascript files this theme that you would
* like included from footer, in order. Give the names of the files without .js.
*/
public $javascripts_footer = array();
/**
* @var array The names of all the javascript files from parents that should
* be excluded. true value may be used to specify all parents or all themes
* from one parent.
* If no value specified value from parent theme used.
*/
public $parents_exclude_javascripts = null;
/**
* @var array Which file to use for each page layout.
*
* This is an array of arrays. The keys of the outer array are the different layouts.
* Pages in Moodle are using several different layouts like 'normal', 'course', 'home',
* 'popup', 'form', .... The most reliable way to get a complete list is to look at
* {@link http://cvs.moodle.org/moodle/theme/base/config.php?view=markup the base theme config.php file}.
* That file also has a good example of how to set this setting.
*
* For each layout, the value in the outer array is an array that describes
* how you want that type of page to look. For example
* <pre>
* $THEME->layouts = array(
* // Most pages - if we encounter an unknown or a missing page type, this one is used.
* 'standard' => array(
* 'theme' = 'mytheme',
* 'file' => 'normal.php',
* 'regions' => array('side-pre', 'side-post'),
* 'defaultregion' => 'side-post'
* ),
* // The site home page.
* 'home' => array(
* 'theme' = 'mytheme',
* 'file' => 'home.php',
* 'regions' => array('side-pre', 'side-post'),
* 'defaultregion' => 'side-post'
* ),
* // ...
* );
* </pre>
*
* 'theme' name of the theme where is the layout located
* 'file' is the layout file to use for this type of page.
* layout files are stored in layout subfolder
* 'regions' This lists the regions on the page where blocks may appear. For
* each region you list here, your layout file must include a call to
* <pre>
* echo $OUTPUT->blocks_for_region($regionname);
* </pre>
* or equivalent so that the blocks are actually visible.
*
* 'defaultregion' If the list of regions is non-empty, then you must pick
* one of the one of them as 'default'. This has two meanings. First, this is
* where new blocks are added. Second, if there are any blocks associated with
* the page, but in non-existent regions, they appear here. (Imaging, for example,
* that someone added blocks using a different theme that used different region
* names, and then switched to this theme.)
*/
public $layouts = array();
/**
* @var string Name of the renderer factory class to use. Must implement the
* {@link renderer_factory} interface.
*
* This is an advanced feature. Moodle output is generated by 'renderers',
* you can customise the HTML that is output by writing custom renderers,
* and then you need to specify 'renderer factory' so that Moodle can find
* your renderers.
*
* There are some renderer factories supplied with Moodle. Please follow these
* links to see what they do.
* <ul>
* <li>{@link standard_renderer_factory} - the default.</li>
* <li>{@link theme_overridden_renderer_factory} - use this if you want to write
* your own custom renderers in a lib.php file in this theme (or the parent theme).</li>
* </ul>
*/
public $rendererfactory = 'standard_renderer_factory';
/**
* @var string Function to do custom CSS post-processing.
*
* This is an advanced feature. If you want to do custom post-processing on the
* CSS before it is output (for example, to replace certain variable names
* with particular values) you can give the name of a function here.
*/
public $csspostprocess = null;
/**
* @var string Accessibility: Right arrow-like character is
* used in the breadcrumb trail, course navigation menu
* (previous/next activity), calendar, and search forum block.
* If the theme does not set characters, appropriate defaults
* are set automatically. Please DO NOT
* use < > » - these are confusing for blind users.
*/
public $rarrow = null;
/**
* @var string Accessibility: Right arrow-like character is
* used in the breadcrumb trail, course navigation menu
* (previous/next activity), calendar, and search forum block.
* If the theme does not set characters, appropriate defaults
* are set automatically. Please DO NOT
* use < > » - these are confusing for blind users.
*/
public $larrow = null;
/**
* @var bool Some themes may want to disable ajax course editing.
*/
public $enablecourseajax = true;
/**
* @var string Determines served document types
* - 'html5' the only officially supported doctype in Moodle
* - 'xhtml5' may be used in development for validation (not intended for production servers!)
* - 'xhtml' XHTML 1.0 Strict for legacy themes only
*/
public $doctype = 'html5';
//==Following properties are not configurable from theme config.php==
/**
* @var string The name of this theme. Set automatically when this theme is
* loaded. This can not be set in theme config.php
*/
public $name;
/**
* @var string The folder where this themes files are stored. This is set
* automatically. This can not be set in theme config.php
*/
public $dir;
/**
* @var stdClass Theme settings stored in config_plugins table.
* This can not be set in theme config.php
*/
public $setting = null;
/**
* @var bool If set to true and the theme enables the dock then blocks will be able
* to be moved to the special dock
*/
public $enable_dock = false;
/**
* @var bool If set to true then this theme will not be shown in the theme selector unless
* theme designer mode is turned on.
*/
public $hidefromselector = false;
/**
* @var renderer_factory Instance of the renderer_factory implementation
* we are using. Implementation detail.
*/
protected $rf = null;
/**
* @var array List of parent config objects.
**/
protected $parent_configs = array();
/**
* @var bool If set to true then the theme is safe to run through the optimiser (if it is enabled)
* If set to false then we know either the theme has already been optimised and the CSS optimiser is not needed
* or the theme is not compatible with the CSS optimiser. In both cases even if enabled the CSS optimiser will not
* be used with this theme if set to false.
*/
public $supportscssoptimisation = true;
/**
* Used to determine whether we can serve SVG images or not.
* @var bool
*/
private $usesvg = null;
/**
* Load the config.php file for a particular theme, and return an instance
* of this class. (That is, this is a factory method.)
*
* @param string $themename the name of the theme.
* @return theme_config an instance of this class.
*/
public static function load($themename) {
global $CFG;
// load theme settings from db
try {
$settings = get_config('theme_'.$themename);
} catch (dml_exception $e) {
// most probably moodle tables not created yet
$settings = new stdClass();
}
if ($config = theme_config::find_theme_config($themename, $settings)) {
return new theme_config($config);
} else if ($themename == theme_config::DEFAULT_THEME) {
throw new coding_exception('Default theme '.theme_config::DEFAULT_THEME.' not available or broken!');
} else {
// bad luck, the requested theme has some problems - admin see details in theme config
return new theme_config(theme_config::find_theme_config(theme_config::DEFAULT_THEME, $settings));
}
}
/**
* Theme diagnostic code. It is very problematic to send debug output
* to the actual CSS file, instead this functions is supposed to
* diagnose given theme and highlights all potential problems.
* This information should be available from the theme selection page
* or some other debug page for theme designers.
*
* @param string $themename
* @return array description of problems
*/
public static function diagnose($themename) {
//TODO: MDL-21108
return array();
}
/**
* Private constructor, can be called only from the factory method.
* @param stdClass $config
*/
private function __construct($config) {
global $CFG; //needed for included lib.php files
$this->settings = $config->settings;
$this->name = $config->name;
$this->dir = $config->dir;
if ($this->name != 'base') {
$baseconfig = theme_config::find_theme_config('base', $this->settings);
} else {
$baseconfig = $config;
}
$configurable = array('parents', 'sheets', 'parents_exclude_sheets', 'plugins_exclude_sheets', 'javascripts', 'javascripts_footer',
'parents_exclude_javascripts', 'layouts', 'enable_dock', 'enablecourseajax', 'supportscssoptimisation',
'rendererfactory', 'csspostprocess', 'editor_sheets', 'rarrow', 'larrow', 'hidefromselector', 'doctype');
foreach ($config as $key=>$value) {
if (in_array($key, $configurable)) {
$this->$key = $value;
}
}
// verify all parents and load configs and renderers
foreach ($this->parents as $parent) {
if ($parent == 'base') {
$parent_config = $baseconfig;
} else if (!$parent_config = theme_config::find_theme_config($parent, $this->settings)) {
// this is not good - better exclude faulty parents
continue;
}
$libfile = $parent_config->dir.'/lib.php';
if (is_readable($libfile)) {
// theme may store various function here
include_once($libfile);
}
$renderersfile = $parent_config->dir.'/renderers.php';
if (is_readable($renderersfile)) {
// may contain core and plugin renderers and renderer factory
include_once($renderersfile);
}
$this->parent_configs[$parent] = $parent_config;
$rendererfile = $parent_config->dir.'/renderers.php';
if (is_readable($rendererfile)) {
// may contain core and plugin renderers and renderer factory
include_once($rendererfile);
}
}
$libfile = $this->dir.'/lib.php';
if (is_readable($libfile)) {
// theme may store various function here
include_once($libfile);
}
$rendererfile = $this->dir.'/renderers.php';
if (is_readable($rendererfile)) {
// may contain core and plugin renderers and renderer factory
include_once($rendererfile);
} else {
// check if renderers.php file is missnamed renderer.php
if (is_readable($this->dir.'/renderer.php')) {
debugging('Developer hint: '.$this->dir.'/renderer.php should be renamed to ' . $this->dir."/renderers.php.
See: http://docs.moodle.org/dev/Output_renderers#Theme_renderers.", DEBUG_DEVELOPER);
}
}
// cascade all layouts properly
foreach ($baseconfig->layouts as $layout=>$value) {
if (!isset($this->layouts[$layout])) {
foreach ($this->parent_configs as $parent_config) {
if (isset($parent_config->layouts[$layout])) {
$this->layouts[$layout] = $parent_config->layouts[$layout];
continue 2;
}
}
$this->layouts[$layout] = $value;
}
}
//fix arrows if needed
$this->check_theme_arrows();
}
/**
* Checks if arrows $THEME->rarrow, $THEME->larrow have been set (theme/-/config.php).
* If not it applies sensible defaults.
*
* Accessibility: right and left arrow Unicode characters for breadcrumb, calendar,
* search forum block, etc. Important: these are 'silent' in a screen-reader
* (unlike > »), and must be accompanied by text.
*/
private function check_theme_arrows() {
if (!isset($this->rarrow) and !isset($this->larrow)) {
// Default, looks good in Win XP/IE 6, Win/Firefox 1.5, Win/Netscape 8...
// Also OK in Win 9x/2K/IE 5.x
$this->rarrow = '►';
$this->larrow = '◄';
if (empty($_SERVER['HTTP_USER_AGENT'])) {
$uagent = '';
} else {
$uagent = $_SERVER['HTTP_USER_AGENT'];
}
if (false !== strpos($uagent, 'Opera')
|| false !== strpos($uagent, 'Mac')) {
// Looks good in Win XP/Mac/Opera 8/9, Mac/Firefox 2, Camino, Safari.
// Not broken in Mac/IE 5, Mac/Netscape 7 (?).
$this->rarrow = '▶';
$this->larrow = '◀';
}
elseif (false !== strpos($uagent, 'Konqueror')) {
$this->rarrow = '→';
$this->larrow = '←';
}
elseif (isset($_SERVER['HTTP_ACCEPT_CHARSET'])
&& false === stripos($_SERVER['HTTP_ACCEPT_CHARSET'], 'utf-8')) {
// (Win/IE 5 doesn't set ACCEPT_CHARSET, but handles Unicode.)
// To be safe, non-Unicode browsers!
$this->rarrow = '>';
$this->larrow = '<';
}
// RTL support - in RTL languages, swap r and l arrows
if (right_to_left()) {
$t = $this->rarrow;
$this->rarrow = $this->larrow;
$this->larrow = $t;
}
}
}
/**
* Returns output renderer prefixes, these are used when looking
* for the overridden renderers in themes.
*
* @return array
*/
public function renderer_prefixes() {
global $CFG; // just in case the included files need it
$prefixes = array('theme_'.$this->name);
foreach ($this->parent_configs as $parent) {
$prefixes[] = 'theme_'.$parent->name;
}
return $prefixes;
}
/**
* Returns the stylesheet URL of this editor content
*
* @param bool $encoded false means use & and true use & in URLs
* @return string
*/
public function editor_css_url($encoded=true) {
global $CFG;
$rev = theme_get_revision();
if ($rev > -1) {
if (!empty($CFG->slasharguments)) {
$url = new moodle_url("$CFG->httpswwwroot/theme/styles.php");
$url->set_slashargument('/'.$this->name.'/'.$rev.'/editor', 'noparam', true);
return $url;
} else {
$params = array('theme'=>$this->name,'rev'=>$rev, 'type'=>'editor');
return new moodle_url($CFG->httpswwwroot.'/theme/styles.php', $params);
}
} else {
$params = array('theme'=>$this->name, 'type'=>'editor');
return new moodle_url($CFG->httpswwwroot.'/theme/styles_debug.php', $params);
}
}
/**
* Returns the content of the CSS to be used in editor content
*
* @return string
*/
public function editor_css_files() {
global $CFG;
$files = array();
// first editor plugins
$plugins = get_plugin_list('editor');
foreach ($plugins as $plugin=>$fulldir) {
$sheetfile = "$fulldir/editor_styles.css";
if (is_readable($sheetfile)) {
$files['plugin_'.$plugin] = $sheetfile;
}
}
// then parent themes
foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
if (empty($parent_config->editor_sheets)) {
continue;
}
foreach ($parent_config->editor_sheets as $sheet) {
$sheetfile = "$parent_config->dir/style/$sheet.css";
if (is_readable($sheetfile)) {
$files['parent_'.$parent_config->name.'_'.$sheet] = $sheetfile;
}
}
}
// finally this theme
if (!empty($this->editor_sheets)) {
foreach ($this->editor_sheets as $sheet) {
$sheetfile = "$this->dir/style/$sheet.css";
if (is_readable($sheetfile)) {
$files['theme_'.$sheet] = $sheetfile;
}
}
}
return $files;
}
/**
* Get the stylesheet URL of this theme
*
* @param moodle_page $page Not used... deprecated?
* @return array of moodle_url
*/
public function css_urls(moodle_page $page) {
global $CFG;
$rev = theme_get_revision();
$urls = array();
if ($rev > -1) {
if (check_browser_version('MSIE', 5)) {
// We need to split the CSS files for IE
$urls[] = new moodle_url($CFG->httpswwwroot.'/theme/styles.php', array('theme'=>$this->name,'rev'=>$rev, 'type'=>'plugins'));
$urls[] = new moodle_url($CFG->httpswwwroot.'/theme/styles.php', array('theme'=>$this->name,'rev'=>$rev, 'type'=>'parents'));
$urls[] = new moodle_url($CFG->httpswwwroot.'/theme/styles.php', array('theme'=>$this->name,'rev'=>$rev, 'type'=>'theme'));
} else {
if (!empty($CFG->slasharguments)) {
$url = new moodle_url("$CFG->httpswwwroot/theme/styles.php");
$url->set_slashargument('/'.$this->name.'/'.$rev.'/all', 'noparam', true);
$urls[] = $url;
} else {
$urls[] = new moodle_url($CFG->httpswwwroot.'/theme/styles.php', array('theme'=>$this->name,'rev'=>$rev, 'type'=>'all'));
}
}
} else {
// find out the current CSS and cache it now for 5 seconds
// the point is to construct the CSS only once and pass it through the
// dataroot to the script that actually serves the sheets
if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) {
define('THEME_DESIGNER_CACHE_LIFETIME', 4); // this can be also set in config.php
}
$candidatedir = "$CFG->cachedir/theme/$this->name";
$candidatesheet = "$candidatedir/designer.ser";
$rebuild = true;
if (file_exists($candidatesheet) and filemtime($candidatesheet) > time() - THEME_DESIGNER_CACHE_LIFETIME) {
if ($css = file_get_contents($candidatesheet)) {
$css = unserialize($css);
if (is_array($css)) {
$rebuild = false;
}
}
}
if ($rebuild) {
// Prepare the CSS optimiser if it is to be used,
// please note that it may be very slow and is therefore strongly discouraged in theme designer mode.
$optimiser = null;
if (!empty($CFG->enablecssoptimiser) && $this->supportscssoptimisation) {
require_once($CFG->dirroot.'/lib/csslib.php');
$optimiser = new css_optimiser;
}
$css = $this->css_content($optimiser);
// We do not want any errors here because this may fail easily because of the concurrent access.
$prevabort = ignore_user_abort(true);
check_dir_exists($candidatedir);
$tempfile = tempnam($candidatedir, 'tmpdesigner');
file_put_contents($tempfile, serialize($css));
$reporting = error_reporting(0);
chmod($tempfile, $CFG->filepermissions);
unlink($candidatesheet); // Do not rely on rename() deleting original, they may decide to change it at any time as usually.
rename($tempfile, $candidatesheet);
error_reporting($reporting);
ignore_user_abort($prevabort);
}
$baseurl = $CFG->httpswwwroot.'/theme/styles_debug.php';
if (check_browser_version('MSIE', 5)) {
// lalala, IE does not allow more than 31 linked CSS files from main document
$urls[] = new moodle_url($baseurl, array('theme'=>$this->name, 'type'=>'ie', 'subtype'=>'plugins'));
foreach ($css['parents'] as $parent=>$sheets) {
// We need to serve parents individually otherwise we may easily exceed the style limit IE imposes (4096)
$urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'ie', 'subtype'=>'parents', 'sheet'=>$parent));
}
$urls[] = new moodle_url($baseurl, array('theme'=>$this->name, 'type'=>'ie', 'subtype'=>'theme'));
} else {
foreach ($css['plugins'] as $plugin=>$unused) {
$urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'plugin', 'subtype'=>$plugin));
}
foreach ($css['parents'] as $parent=>$sheets) {
foreach ($sheets as $sheet=>$unused2) {
$urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'parent', 'subtype'=>$parent, 'sheet'=>$sheet));
}
}
foreach ($css['theme'] as $sheet=>$unused) {
$urls[] = new moodle_url($baseurl, array('sheet'=>$sheet, 'theme'=>$this->name, 'type'=>'theme')); // sheet first in order to make long urls easier to read
}
}
}
return $urls;
}
/**
* Returns an array of organised CSS files required for this output
*
* @return array
*/
public function css_files() {
$cssfiles = array('plugins'=>array(), 'parents'=>array(), 'theme'=>array());
// get all plugin sheets
$excludes = $this->resolve_excludes('plugins_exclude_sheets');
if ($excludes !== true) {
foreach (get_plugin_types() as $type=>$unused) {
if ($type === 'theme' || (!empty($excludes[$type]) and $excludes[$type] === true)) {
continue;
}
$plugins = get_plugin_list($type);
foreach ($plugins as $plugin=>$fulldir) {
if (!empty($excludes[$type]) and is_array($excludes[$type])
and in_array($plugin, $excludes[$type])) {
continue;
}
$plugincontent = '';
$sheetfile = "$fulldir/styles.css";
if (is_readable($sheetfile)) {
$cssfiles['plugins'][$type.'_'.$plugin] = $sheetfile;
}
$sheetthemefile = "$fulldir/styles_{$this->name}.css";
if (is_readable($sheetthemefile)) {
$cssfiles['plugins'][$type.'_'.$plugin.'_'.$this->name] = $sheetthemefile;
}
}
}
}
// find out wanted parent sheets
$excludes = $this->resolve_excludes('parents_exclude_sheets');
if ($excludes !== true) {
foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
$parent = $parent_config->name;
if (empty($parent_config->sheets) || (!empty($excludes[$parent]) and $excludes[$parent] === true)) {
continue;
}
foreach ($parent_config->sheets as $sheet) {
if (!empty($excludes[$parent]) and is_array($excludes[$parent])
and in_array($sheet, $excludes[$parent])) {
continue;
}
$sheetfile = "$parent_config->dir/style/$sheet.css";
if (is_readable($sheetfile)) {
$cssfiles['parents'][$parent][$sheet] = $sheetfile;
}
}
}
}
// current theme sheets
if (is_array($this->sheets)) {
foreach ($this->sheets as $sheet) {
$sheetfile = "$this->dir/style/$sheet.css";
if (is_readable($sheetfile)) {
$cssfiles['theme'][$sheet] = $sheetfile;
}
}
}
return $cssfiles;
}
/**
* Returns the content of the one huge CSS merged from all style sheets.
*
* @param css_optimiser|null $optimiser A CSS optimiser to use during on the content. Null = don't optimise
* @return string
*/
public function css_content(css_optimiser $optimiser = null) {
$files = array_merge($this->css_files(), array('editor'=>$this->editor_css_files()));
$css = $this->css_files_get_contents($files, array(), $optimiser);
return $css;
}
/**
* Given an array of file paths or a single file path loads the contents of
* the CSS file, processes it then returns it in the same structure it was given.
*
* Can be used recursively on the results of {@link css_files}
*
* @param array|string $file An array of file paths or a single file path
* @param array $keys An array of previous array keys [recursive addition]
* @param css_optimiser|null $optimiser A CSS optimiser to use during on the content. Null = don't optimise
* @return The converted array or the contents of the single file ($file type)
*/
protected function css_files_get_contents($file, array $keys, css_optimiser $optimiser = null) {
global $CFG;
if (is_array($file)) {
foreach ($file as $key=>$f) {
$file[$key] = $this->css_files_get_contents($f, array_merge($keys, array($key)), $optimiser);
}
return $file;
} else {
$contents = file_get_contents($file);
$contents = $this->post_process($contents);
$comment = '/** Path: '.implode(' ', $keys).' **/'."\n";
$stats = '';
if (!is_null($optimiser)) {
$contents = $optimiser->process($contents);
if (!empty($CFG->cssoptimiserstats)) {
$stats = $optimiser->output_stats_css();
}
}
return $comment.$stats.$contents;
}
}
/**
* Generate a URL to the file that serves theme JavaScript files.
*
* @param bool $inhead true means head url, false means footer
* @return moodle_url
*/
public function javascript_url($inhead) {
global $CFG;
$rev = theme_get_revision();
$params = array('theme'=>$this->name,'rev'=>$rev);
$params['type'] = $inhead ? 'head' : 'footer';
if (!empty($CFG->slasharguments) and $rev > 0) {
$url = new moodle_url("$CFG->httpswwwroot/theme/javascript.php");
$url->set_slashargument('/'.$this->name.'/'.$rev.'/'.$params['type'], 'noparam', true);
return $url;
} else {
return new moodle_url($CFG->httpswwwroot.'/theme/javascript.php', $params);
}
}
/**
* Get the URL's for the JavaScript files used by this theme.
* They won't be served directly, instead they'll be mediated through
* theme/javascript.php.
*
* @param string $type Either javascripts_footer, or javascripts
* @return array
*/
public function javascript_files($type) {
if ($type === 'footer') {
$type = 'javascripts_footer';
} else {
$type = 'javascripts';
}
$js = array();
// find out wanted parent javascripts
$excludes = $this->resolve_excludes('parents_exclude_javascripts');
if ($excludes !== true) {
foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
$parent = $parent_config->name;
if (empty($parent_config->$type)) {
continue;
}
if (!empty($excludes[$parent]) and $excludes[$parent] === true) {
continue;
}
foreach ($parent_config->$type as $javascript) {
if (!empty($excludes[$parent]) and is_array($excludes[$parent])
and in_array($javascript, $excludes[$parent])) {
continue;
}
$javascriptfile = "$parent_config->dir/javascript/$javascript.js";
if (is_readable($javascriptfile)) {
$js[] = $javascriptfile;
}
}
}
}
// current theme javascripts
if (is_array($this->$type)) {
foreach ($this->$type as $javascript) {
$javascriptfile = "$this->dir/javascript/$javascript.js";
if (is_readable($javascriptfile)) {
$js[] = $javascriptfile;
}
}
}
return $js;
}
/**
* Resolves an exclude setting to the themes setting is applicable or the
* setting of its closest parent.
*
* @param string $variable The name of the setting the exclude setting to resolve
* @param string $default
* @return mixed
*/
protected function resolve_excludes($variable, $default = null) {
$setting = $default;
if (is_array($this->{$variable}) or $this->{$variable} === true) {
$setting = $this->{$variable};
} else {
foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last
if (!isset($parent_config->{$variable})) {
continue;
}
if (is_array($parent_config->{$variable}) or $parent_config->{$variable} === true) {
$setting = $parent_config->{$variable};
break;
}
}
}
return $setting;
}
/**
* Returns the content of the one huge javascript file merged from all theme javascript files.
*
* @param bool $type
* @return string
*/
public function javascript_content($type) {
$jsfiles = $this->javascript_files($type);
$js = '';
foreach ($jsfiles as $jsfile) {
$js .= file_get_contents($jsfile)."\n";
}
return $js;
}
/**
* Post processes CSS.
*
* This method post processes all of the CSS before it is served for this theme.
* This is done so that things such as image URL's can be swapped in and to
* run any specific CSS post process method the theme has requested.
* This allows themes to use CSS settings.
*
* @param string $css The CSS to process.
* @return string The processed CSS.
*/
public function post_process($css) {
// now resolve all image locations
if (preg_match_all('/\[\[pix:([a-z_]+\|)?([^\]]+)\]\]/', $css, $matches, PREG_SET_ORDER)) {
// We are going to disable the use of SVG images when available in CSS background-image properties
// as support for it in browsers is at best quirky.
// When we choose to support SVG in background css we will need to remove this code and implement a solution that is
// either consistent or varies the URL for serving CSS depending upon SVG being used if available, or not.
$this->force_svg_use(false);
$replaced = array();
foreach ($matches as $match) {
if (isset($replaced[$match[0]])) {
continue;
}
$replaced[$match[0]] = true;
$imagename = $match[2];
$component = rtrim($match[1], '|');
$imageurl = $this->pix_url($imagename, $component)->out(false);
// we do not need full url because the image.php is always in the same dir
$imageurl = preg_replace('|^http.?://[^/]+|', '', $imageurl);
$css = str_replace($match[0], $imageurl, $css);
}
}
// now resolve all theme settings or do any other postprocessing
$csspostprocess = $this->csspostprocess;
if (function_exists($csspostprocess)) {
$css = $csspostprocess($css, $this);
}
return $css;
}
/**
* Return the URL for an image
*
* @param string $imagename the name of the icon.
* @param string $component specification of one plugin like in get_string()
* @return moodle_url
*/
public function pix_url($imagename, $component) {
global $CFG;
$params = array('theme'=>$this->name);
$svg = $this->use_svg_icons();
if (empty($component) or $component === 'moodle' or $component === 'core') {
$params['component'] = 'core';
} else {
$params['component'] = $component;
}
$rev = theme_get_revision();
if ($rev != -1) {