on-demand release 2.2beta
[moodle.git] / theme / javascript.php
CommitLineData
38aafea2
PS
1<?php
2
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.
14//
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/>.
17
18/**
19 * This file is responsible for serving the one huge CSS of each theme.
20 *
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
24 */
25
e68c5f36
PS
26
27// we need just the values from config.php and minlib.php
28define('ABORT_AFTER_CONFIG', true);
29require('../config.php'); // this stops immediately at the beginning of lib/setup.php
30
31$themename = min_optional_param('theme', 'standard', 'SAFEDIR');
32$rev = min_optional_param('rev', 0, 'INT');
baeb20bb 33$type = min_optional_param('type', 'head', 'RAW');
04c01408 34
baeb20bb 35if ($type !== 'head' and $type !== 'footer') {
04c01408
PS
36 header('HTTP/1.0 404 not found');
37 die('Theme was not found, sorry.');
38}
39
73e504bc
PS
40if (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 {
e68c5f36
PS
45 header('HTTP/1.0 404 not found');
46 die('Theme was not found, sorry.');
47}
48
365bec4c 49$candidate = "$CFG->cachedir/theme/$themename/javascript_$type.js";
e68c5f36
PS
50
51if ($rev > -1 and file_exists($candidate)) {
aa603b14 52 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
e68c5f36
PS
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
ccc0fff9 55 $lifetime = 60*60*24*30; // 30 days
aa603b14 56 header('HTTP/1.1 304 Not Modified');
ccc0fff9
TL
57 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
58 header('Cache-Control: max-age='.$lifetime);
59 header('Content-Type: application/javascript; charset=utf-8');
e68c5f36
PS
60 die;
61 }
62 send_cached_js($candidate, $rev);
63}
64
65//=================================================================================
66// ok, now we need to start normal moodle script, we need to load all libs and $DB
67define('ABORT_AFTER_CONFIG_CANCEL', true);
68
69define('NO_MOODLE_COOKIES', true); // Session not used here
70define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
71
72require("$CFG->dirroot/lib/setup.php");
045f492c
SH
73// setup include path
74set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path());
75require_once('Minify.php');
e68c5f36
PS
76
77$theme = theme_config::load($themename);
78
e68c5f36 79if ($rev > -1) {
c426ef3a 80 check_dir_exists(dirname($candidate));
e68c5f36 81 $fp = fopen($candidate, 'w');
045f492c 82 fwrite($fp, minify($theme->javascript_files($type)));
e68c5f36
PS
83 fclose($fp);
84 send_cached_js($candidate);
e68c5f36 85} else {
045f492c 86 send_uncached_js($theme->javascript_content($type));
e68c5f36
PS
87}
88
89//=================================================================================
90//=== utility functions ==
91// we are not using filelib because we need to fine tune all header
92// parameters to get the best performance.
93
94function send_cached_js($jspath) {
ccc0fff9 95 $lifetime = 60*60*24*30; // 30 days
e68c5f36 96
baeb20bb 97 header('Content-Disposition: inline; filename="javascript.php"');
e68c5f36
PS
98 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($jspath)) .' GMT');
99 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
100 header('Pragma: ');
ccc0fff9 101 header('Cache-Control: max-age='.$lifetime);
e68c5f36 102 header('Accept-Ranges: none');
8a7703ce 103 header('Content-Type: application/javascript; charset=utf-8');
7c986f04
PS
104 if (!min_enable_zlib_compression()) {
105 header('Content-Length: '.filesize($jspath));
106 }
e68c5f36 107
e68c5f36
PS
108 readfile($jspath);
109 die;
110}
111
112function send_uncached_js($js) {
baeb20bb 113 header('Content-Disposition: inline; filename="javascript.php"');
e68c5f36
PS
114 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
115 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
116 header('Pragma: ');
117 header('Accept-Ranges: none');
8a7703ce 118 header('Content-Type: application/javascript; charset=utf-8');
e68c5f36
PS
119 header('Content-Length: '.strlen($js));
120
e68c5f36
PS
121 echo $js;
122 die;
123}
045f492c
SH
124
125function minify($files) {
045f492c
SH
126 if (0 === stripos(PHP_OS, 'win')) {
127 Minify::setDocRoot(); // IIS may need help
128 }
e1c2a211
PS
129 // disable all caching, we do it in moodle
130 Minify::setCache(null, false);
045f492c
SH
131
132 $options = array(
133 'bubbleCssImports' => false,
134 // Don't gzip content we just want text for storage
135 'encodeOutput' => false,
136 // Maximum age to cache, not used but required
137 'maxAge' => 1800,
138 // The files to minify
139 'files' => $files,
140 // Turn orr URI rewriting
141 'rewriteCssUris' => false,
142 // This returns the CSS rather than echoing it for display
143 'quiet' => true
144 );
145
146 $result = Minify::serve('Files', $options);
147 return $result['content'];
148}