MDL-29248 backup: delete user_files settings, lang strings and uses
[moodle.git] / theme / javascript.php
blob9b359a1f97a2ad6ad268be791376d0735b752761
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']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
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 $lifetime = 60*60*24*30; // 30 days
56 header('HTTP/1.1 304 Not Modified');
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');
60 die;
62 send_cached_js($candidate, $rev);
65 //=================================================================================
66 // ok, now we need to start normal moodle script, we need to load all libs and $DB
67 define('ABORT_AFTER_CONFIG_CANCEL', true);
69 define('NO_MOODLE_COOKIES', true); // Session not used here
70 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
72 require("$CFG->dirroot/lib/setup.php");
73 // setup include path
74 set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path());
75 require_once('Minify.php');
77 $theme = theme_config::load($themename);
79 if ($rev > -1) {
80 check_dir_exists(dirname($candidate));
81 $fp = fopen($candidate, 'w');
82 fwrite($fp, minify($theme->javascript_files($type)));
83 fclose($fp);
84 send_cached_js($candidate);
85 } else {
86 send_uncached_js($theme->javascript_content($type));
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.
94 function send_cached_js($jspath) {
95 $lifetime = 60*60*24*30; // 30 days
97 header('Content-Disposition: inline; filename="javascript.php"');
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: ');
101 header('Cache-Control: max-age='.$lifetime);
102 header('Accept-Ranges: none');
103 header('Content-Type: application/javascript; charset=utf-8');
104 if (!min_enable_zlib_compression()) {
105 header('Content-Length: '.filesize($jspath));
108 readfile($jspath);
109 die;
112 function send_uncached_js($js) {
113 header('Content-Disposition: inline; filename="javascript.php"');
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');
118 header('Content-Type: application/javascript; charset=utf-8');
119 header('Content-Length: '.strlen($js));
121 echo $js;
122 die;
125 function minify($files) {
126 if (0 === stripos(PHP_OS, 'win')) {
127 Minify::setDocRoot(); // IIS may need help
129 // disable all caching, we do it in moodle
130 Minify::setCache(null, false);
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
146 $result = Minify::serve('Files', $options);
147 return $result['content'];