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