MDL-15727 basic jQuery support
[moodle.git] / theme / jquery.php
CommitLineData
63c88397
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * jQuery serving script.
19 *
20 * Do not include jQuery scripts or CSS directly, always use
21 * $PAGE->requires->jquery() or $PAGE->requires->jquery_plugin('xx', 'yy').
22 *
23 * @package core
24 * @copyright 2013 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28// Disable moodle specific debug messages and any errors in output,
29// comment out when debugging or better look into error log!
30define('NO_DEBUG_DISPLAY', true);
31
32// We need just the values from config.php and minlib.php.
33define('ABORT_AFTER_CONFIG', true);
34require('../config.php'); // This stops immediately at the beginning of lib/setup.php.
35
36if ($slashargument = min_get_slash_argument()) {
37 $path = ltrim($slashargument, '/');
38} else {
39 $path = min_optional_param('file', '', 'SAFEPATH');
40 $path = ltrim($path, '/');
41}
42
43if (strpos($path, '/') === false) {
44 jquery_file_not_found();
45}
46
47list($component, $path) = explode('/', $path, 2);
48
49if (empty($path) or empty($component)) {
50 jquery_file_not_found();
51}
52
53// Find the jQuery dir for this component.
54if ($component === 'core') {
55 $componentdir = "$CFG->dirroot/lib";
56
57} else if (strpos($component, 'theme_')) {
58 if (!empty($CFG->themedir)) {
59 $componentdir = "$CFG->themedir/$component";
60 } else {
61 $componentdir = "$CFG->dirroot/theme/$component";
62 }
63
64} else {
65 define('ABORT_AFTER_CONFIG_CANCEL', true);
66 define('NO_MOODLE_COOKIES', true); // Session not used here.
67 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check.
68 require("$CFG->dirroot/lib/setup.php");
69 $componentdir = get_component_directory($component);
70}
71
72if (!file_exists($componentdir) or !file_exists("$componentdir/jquery/plugins.php")) {
73 jquery_file_not_found();
74}
75
76$file = realpath("$componentdir/jquery/$path");
77
78if (!$file or is_dir($file)) {
79 jquery_file_not_found();
80}
81
82$etag = sha1("$component/$path");
83$lifetime = 60*60*24*120; // 120 days should be enough.
84$pathinfo = pathinfo($path);
85
86if (empty($pathinfo['extension'])) {
87 jquery_file_not_found();
88}
89
90$filename = $pathinfo['filename'].'.'.$pathinfo['extension'];
91
92switch($pathinfo['extension']) {
93 case 'gif' : $mimetype = 'image/gif';
94 break;
95 case 'png' : $mimetype = 'image/png';
96 break;
97 case 'jpg' : $mimetype = 'image/jpeg';
98 break;
99 case 'jpeg' : $mimetype = 'image/jpeg';
100 break;
101 case 'ico' : $mimetype = 'image/vnd.microsoft.icon';
102 break;
103 case 'svg' : $mimetype = 'image/svg+xml';
104 break;
105 case 'js' : $mimetype = 'application/javascript';
106 break;
107 case 'css' : $mimetype = 'text/css';
108 break;
109 case 'php' : jquery_file_not_found();
110 break;
111 default : $mimetype = 'document/unknown';
112}
113
114if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
115 // We do not actually need to verify the etag value because these files
116 // never change, devs need to change file names on update!
117 header('HTTP/1.1 304 Not Modified');
118 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
119 header('Cache-Control: public, max-age='.$lifetime);
120 header('Content-Type: '.$mimetype);
121 header('Etag: '.$etag);
122 die;
123}
124
125require_once("$CFG->dirroot/lib/xsendfilelib.php");
126
127header('Etag: '.$etag);
128header('Content-Disposition: inline; filename="'.$filename.'"');
129header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($file)) .' GMT');
130header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
131header('Pragma: ');
132header('Cache-Control: public, max-age='.$lifetime);
133header('Accept-Ranges: none');
134header('Content-Type: '.$mimetype);
135
136if (xsendfile($file)) {
137 die;
138}
139
140if ($mimetype === 'text/css' or $mimetype === 'application/javascript') {
141 if (!min_enable_zlib_compression()) {
142 header('Content-Length: '.filesize($file));
143 }
144} else {
145 // No need to compress images.
146 header('Content-Length: '.filesize($file));
147}
148
149readfile($file);
150die;
151
152
153
154function jquery_file_not_found() {
155 // Note: we can not disclose the exact file path here, sorry.
156 header('HTTP/1.0 404 not found');
157 die('File was not found, sorry.');
158}