MDL-20787 removing obsolete smartpix redirect code
[moodle.git] / theme / javascripts.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');
33
34if (!file_exists("$CFG->dirroot/theme/$themename/config.php") and !file_exists("$CFG->dataroot/theme/$themename/config.php")) {
35 header('HTTP/1.0 404 not found');
36 die('Theme was not found, sorry.');
37}
38
39$candidate = "$CFG->dataroot/cache/theme/$themename/javascript.js";
40
41if ($rev > -1 and file_exists($candidate)) {
42 if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
43 // we do not actually need to verify the etag value because our files
44 // never change in cache because we increment the rev parameter
45 header('HTTP/1.1 304 Not Modified');
46 die;
47 }
48 send_cached_js($candidate, $rev);
49}
50
51//=================================================================================
52// ok, now we need to start normal moodle script, we need to load all libs and $DB
53define('ABORT_AFTER_CONFIG_CANCEL', true);
54
55define('NO_MOODLE_COOKIES', true); // Session not used here
56define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
57
58require("$CFG->dirroot/lib/setup.php");
59
60$theme = theme_config::load($themename);
61
62$js = $theme->javascript_content();
63if ($rev > -1) {
64 check_dir_exists(dirname($candidate), true, true);
65 $fp = fopen($candidate, 'w');
66 fwrite($fp, $js);
67 fclose($fp);
68 send_cached_js($candidate);
69
70} else {
71 send_uncached_js($js);
72}
73
74//=================================================================================
75//=== utility functions ==
76// we are not using filelib because we need to fine tune all header
77// parameters to get the best performance.
78
79function send_cached_js($jspath) {
80 $lifetime = 60*60*24*20;
81
82 header('Content-Disposition: inline; filename="javascripts.php"');
83 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($jspath)) .' GMT');
84 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
85 header('Pragma: ');
86 header('Accept-Ranges: none');
87 header('Content-Type: application/x-javascript');
88 header('Content-Length: '.filesize($jspath));
89
90 while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
91 readfile($jspath);
92 die;
93}
94
95function send_uncached_js($js) {
96 header('Content-Disposition: inline; filename="javascripts.php"');
97 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
98 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
99 header('Pragma: ');
100 header('Accept-Ranges: none');
101 header('Content-Type: application/x-javascript');
102 header('Content-Length: '.strlen($js));
103
104 while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
105 echo $js;
106 die;
107}