MDL-31794 theme_formalwhite: Fixed up tabs
[moodle.git] / theme / javascript.php
blob3ebba8bb191edfc4bc4157121c19e95986068e06
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * This file is responsible for serving the one huge CSS of each theme.
21 * @package moodlecore
22 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 // disable moodle specific debug messages and any errors in output,
28 // comment out when debugging or better look into error log!
29 define('NO_DEBUG_DISPLAY', true);
31 // we need just the values from config.php and minlib.php
32 define('ABORT_AFTER_CONFIG', true);
33 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
35 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
36 $rev = min_optional_param('rev', 0, 'INT');
37 $type = min_optional_param('type', 'head', 'RAW');
39 if ($type !== 'head' and $type !== 'footer') {
40 header('HTTP/1.0 404 not found');
41 die('Theme was not found, sorry.');
44 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
45 // exists
46 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
47 // exists
48 } else {
49 header('HTTP/1.0 404 not found');
50 die('Theme was not found, sorry.');
53 $candidate = "$CFG->cachedir/theme/$themename/javascript_$type.js";
55 if ($rev > -1 and file_exists($candidate)) {
56 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
57 // we do not actually need to verify the etag value because our files
58 // never change in cache because we increment the rev parameter
59 $lifetime = 60*60*24*30; // 30 days
60 header('HTTP/1.1 304 Not Modified');
61 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
62 header('Cache-Control: max-age='.$lifetime);
63 header('Content-Type: application/javascript; charset=utf-8');
64 die;
66 send_cached_js($candidate, $rev);
69 //=================================================================================
70 // ok, now we need to start normal moodle script, we need to load all libs and $DB
71 define('ABORT_AFTER_CONFIG_CANCEL', true);
73 define('NO_MOODLE_COOKIES', true); // Session not used here
74 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
76 require("$CFG->dirroot/lib/setup.php");
77 // setup include path
78 set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path());
79 require_once('Minify.php');
81 $theme = theme_config::load($themename);
83 if ($rev > -1) {
84 // note: cache reset might have purged our cache dir structure,
85 // make sure we do not use stale file stat cache in the next check_dir_exists()
86 clearstatcache();
87 check_dir_exists(dirname($candidate));
88 $fp = fopen($candidate, 'w');
89 fwrite($fp, minify($theme->javascript_files($type)));
90 fclose($fp);
91 send_cached_js($candidate);
92 } else {
93 send_uncached_js($theme->javascript_content($type));
96 //=================================================================================
97 //=== utility functions ==
98 // we are not using filelib because we need to fine tune all header
99 // parameters to get the best performance.
101 function send_cached_js($jspath) {
102 $lifetime = 60*60*24*30; // 30 days
104 header('Content-Disposition: inline; filename="javascript.php"');
105 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($jspath)) .' GMT');
106 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
107 header('Pragma: ');
108 header('Cache-Control: max-age='.$lifetime);
109 header('Accept-Ranges: none');
110 header('Content-Type: application/javascript; charset=utf-8');
111 if (!min_enable_zlib_compression()) {
112 header('Content-Length: '.filesize($jspath));
115 readfile($jspath);
116 die;
119 function send_uncached_js($js) {
120 header('Content-Disposition: inline; filename="javascript.php"');
121 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
122 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
123 header('Pragma: ');
124 header('Accept-Ranges: none');
125 header('Content-Type: application/javascript; charset=utf-8');
126 header('Content-Length: '.strlen($js));
128 echo $js;
129 die;
132 function minify($files) {
133 if (0 === stripos(PHP_OS, 'win')) {
134 Minify::setDocRoot(); // IIS may need help
136 // disable all caching, we do it in moodle
137 Minify::setCache(null, false);
139 $options = array(
140 'bubbleCssImports' => false,
141 // Don't gzip content we just want text for storage
142 'encodeOutput' => false,
143 // Maximum age to cache, not used but required
144 'maxAge' => 1800,
145 // The files to minify
146 'files' => $files,
147 // Turn orr URI rewriting
148 'rewriteCssUris' => false,
149 // This returns the CSS rather than echoing it for display
150 'quiet' => true
153 $result = Minify::serve('Files', $options);
154 return $result['content'];