MDL-27588 Fixed up several bugs with the formal_white theme
[moodle.git] / theme / javascript.php
blob983731b30da25679466a8aae73b3f4d95eb5af93
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 // we need just the values from config.php and minlib.php
28 define('ABORT_AFTER_CONFIG', true);
29 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
31 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
32 $rev = min_optional_param('rev', 0, 'INT');
33 $type = min_optional_param('type', 'head', 'RAW');
35 if ($type !== 'head' and $type !== 'footer') {
36 header('HTTP/1.0 404 not found');
37 die('Theme was not found, sorry.');
40 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
41 // exists
42 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
43 // exists
44 } else {
45 header('HTTP/1.0 404 not found');
46 die('Theme was not found, sorry.');
49 $candidate = "$CFG->dataroot/cache/theme/$themename/javascript_$type.js";
51 if ($rev > -1 and file_exists($candidate)) {
52 if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
53 // we do not actually need to verify the etag value because our files
54 // never change in cache because we increment the rev parameter
55 header('HTTP/1.1 304 Not Modified');
56 die;
58 send_cached_js($candidate, $rev);
61 //=================================================================================
62 // ok, now we need to start normal moodle script, we need to load all libs and $DB
63 define('ABORT_AFTER_CONFIG_CANCEL', true);
65 define('NO_MOODLE_COOKIES', true); // Session not used here
66 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
68 require("$CFG->dirroot/lib/setup.php");
69 // setup include path
70 set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path());
71 require_once('Minify.php');
73 $theme = theme_config::load($themename);
75 if ($rev > -1) {
76 check_dir_exists(dirname($candidate));
77 $fp = fopen($candidate, 'w');
78 fwrite($fp, minify($theme->javascript_files($type)));
79 fclose($fp);
80 send_cached_js($candidate);
81 } else {
82 send_uncached_js($theme->javascript_content($type));
85 //=================================================================================
86 //=== utility functions ==
87 // we are not using filelib because we need to fine tune all header
88 // parameters to get the best performance.
90 function send_cached_js($jspath) {
91 $lifetime = 60*60*24*20;
93 header('Content-Disposition: inline; filename="javascript.php"');
94 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($jspath)) .' GMT');
95 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
96 header('Pragma: ');
97 header('Accept-Ranges: none');
98 header('Content-Type: application/javascript; charset=utf-8');
99 if (!min_enable_zlib_compression()) {
100 header('Content-Length: '.filesize($jspath));
103 readfile($jspath);
104 die;
107 function send_uncached_js($js) {
108 header('Content-Disposition: inline; filename="javascript.php"');
109 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
110 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
111 header('Pragma: ');
112 header('Accept-Ranges: none');
113 header('Content-Type: application/javascript; charset=utf-8');
114 header('Content-Length: '.strlen($js));
116 echo $js;
117 die;
120 function minify($files) {
121 global $CFG;
123 if (0 === stripos(PHP_OS, 'win')) {
124 Minify::setDocRoot(); // IIS may need help
126 Minify::setCache('', true);
128 $options = array(
129 'bubbleCssImports' => false,
130 // Don't gzip content we just want text for storage
131 'encodeOutput' => false,
132 // Maximum age to cache, not used but required
133 'maxAge' => 1800,
134 // The files to minify
135 'files' => $files,
136 // Turn orr URI rewriting
137 'rewriteCssUris' => false,
138 // This returns the CSS rather than echoing it for display
139 'quiet' => true
142 $result = Minify::serve('Files', $options);
143 return $result['content'];