MDL-25973 add missing charset
[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
04c01408 49$candidate = "$CFG->dataroot/cache/theme/$themename/javascript_$type.js";
e68c5f36
PS
50
51if ($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;
57 }
58 send_cached_js($candidate, $rev);
59}
60
61//=================================================================================
62// ok, now we need to start normal moodle script, we need to load all libs and $DB
63define('ABORT_AFTER_CONFIG_CANCEL', true);
64
65define('NO_MOODLE_COOKIES', true); // Session not used here
66define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
67
68require("$CFG->dirroot/lib/setup.php");
045f492c
SH
69// setup include path
70set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path());
71require_once('Minify.php');
e68c5f36
PS
72
73$theme = theme_config::load($themename);
74
e68c5f36 75if ($rev > -1) {
c426ef3a 76 check_dir_exists(dirname($candidate));
e68c5f36 77 $fp = fopen($candidate, 'w');
045f492c 78 fwrite($fp, minify($theme->javascript_files($type)));
e68c5f36
PS
79 fclose($fp);
80 send_cached_js($candidate);
e68c5f36 81} else {
045f492c 82 send_uncached_js($theme->javascript_content($type));
e68c5f36
PS
83}
84
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.
89
90function send_cached_js($jspath) {
91 $lifetime = 60*60*24*20;
92
baeb20bb 93 header('Content-Disposition: inline; filename="javascript.php"');
e68c5f36
PS
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');
8a7703ce 98 header('Content-Type: application/javascript; charset=utf-8');
7c986f04
PS
99 if (!min_enable_zlib_compression()) {
100 header('Content-Length: '.filesize($jspath));
101 }
e68c5f36 102
e68c5f36
PS
103 readfile($jspath);
104 die;
105}
106
107function send_uncached_js($js) {
baeb20bb 108 header('Content-Disposition: inline; filename="javascript.php"');
e68c5f36
PS
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');
8a7703ce 113 header('Content-Type: application/javascript; charset=utf-8');
e68c5f36
PS
114 header('Content-Length: '.strlen($js));
115
e68c5f36
PS
116 echo $js;
117 die;
118}
045f492c
SH
119
120function minify($files) {
121 global $CFG;
122
123 if (0 === stripos(PHP_OS, 'win')) {
124 Minify::setDocRoot(); // IIS may need help
125 }
126 Minify::setCache('', true);
127
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
140 );
141
142 $result = Minify::serve('Files', $options);
143 return $result['content'];
144}