2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This file contains various javascript related functions,
19 * all functions here are self contained and can be used in ABORT_AFTER_CONFIG scripts.
22 * @copyright 2012 Petr Skoda (skodak) {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 //NOTE: do not verify MOODLE_INTERNAL here, this is used from themes too
29 * Send javascript file content with as much caching as possible
30 * @param string $jspath
32 * @param string $filename
34 function js_send_cached($jspath, $etag, $filename = 'javascript.php') {
35 require(__DIR__
. '/xsendfilelib.php');
37 $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
39 header('Etag: '.$etag);
40 header('Content-Disposition: inline; filename="'.$filename.'"');
41 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($jspath)) .' GMT');
42 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
$lifetime) .' GMT');
44 header('Cache-Control: public, max-age='.$lifetime);
45 header('Accept-Ranges: none');
46 header('Content-Type: application/javascript; charset=utf-8');
48 if (xsendfile($jspath)) {
52 if (!min_enable_zlib_compression()) {
53 header('Content-Length: '.filesize($jspath));
61 * Send javascript without any caching
63 * @param string $filename
65 function js_send_uncached($js, $filename = 'javascript.php') {
66 header('Content-Disposition: inline; filename="'.$filename.'"');
67 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
68 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
2) .' GMT');
70 header('Accept-Ranges: none');
71 header('Content-Type: application/javascript; charset=utf-8');
72 header('Content-Length: '.strlen($js));
79 * Send file not modified headers
80 * @param int $lastmodified
83 function js_send_unmodified($lastmodified, $etag) {
84 $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
85 header('HTTP/1.1 304 Not Modified');
86 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
$lifetime) .' GMT');
87 header('Cache-Control: public, max-age='.$lifetime);
88 header('Content-Type: application/javascript; charset=utf-8');
89 header('Etag: '.$etag);
91 header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
97 * Minify javascript files
101 function js_minify($files) {
102 // setup include path
103 set_include_path(__DIR__
. '/minify/lib' . PATH_SEPARATOR
. get_include_path());
104 require_once('Minify.php');
110 if (0 === stripos(PHP_OS
, 'win')) {
111 Minify
::setDocRoot(); // IIS may need help
113 // disable all caching, we do it in moodle
114 Minify
::setCache(null, false);
117 'bubbleCssImports' => false,
118 // Don't gzip content we just want text for storage
119 'encodeOutput' => false,
120 // Maximum age to cache, not used but required
122 // The files to minify
124 // Turn orr URI rewriting
125 'rewriteCssUris' => false,
126 // This returns the CSS rather than echoing it for display
132 $result = Minify
::serve('Files', $options);
133 if ($result['success']) {
134 return $result['content'];
136 } catch (Exception
$e) {
137 $error = $e->getMessage();
138 $error = str_replace("\r", ' ', $error);
139 $error = str_replace("\n", ' ', $error);
142 // minification failed - try to inform the theme developer and include the non-minified version
144 try {console.log('Error: Minimisation of javascript failed!');} catch (e) {}
147 // Problem detected during javascript minimisation, please review the following code
148 // =================================================================================
152 foreach ($files as $jsfile) {
153 $js .= file_get_contents($jsfile)."\n";