MDL-21249 improved php docs and adding direct access prevention in dtl
[moodle.git] / theme / yui_combo.php
CommitLineData
60f2c866 1<?php
aa42314d
PS
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 of yui images
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
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// get special url parameters
32if (!$parts = combo_params()) {
33 combo_not_found();
34}
35
945f19f7
PS
36$parts = trim($parts, '&');
37
aa42314d
PS
38// find out what we are serving - only one type per request
39$content = '';
40if (substr($parts, -3) === '.js') {
a6338a13 41 $mimetype = 'application/javascript';
aa42314d
PS
42} else if (substr($parts, -4) === '.css') {
43 $mimetype = 'text/css';
44} else {
45 combo_not_found();
46}
47
48$parts = explode('&', $parts);
49
50foreach ($parts as $part) {
945f19f7
PS
51 if (empty($part)) {
52 continue;
53 }
aa42314d
PS
54 $part = min_clean_param($part, 'SAFEPATH');
55 $bits = explode('/', $part);
56 if (count($bits) < 2) {
a4738eb3
PS
57 $content .= "\n// Wrong combo resource $part!\n";
58 continue;
aa42314d 59 }
2b722f87
SH
60 //debug($bits);
61 $version = array_shift($bits);
62 if ($version == 'moodle') {
3b17690c 63 require_once($CFG->libdir.'/moodlelib.php');
2b722f87 64 $frankenstyle = array_shift($bits);
3b17690c 65 $filename = array_pop($bits);
2b722f87 66 $dir = get_component_directory($frankenstyle);
3b17690c
SH
67 if ($mimetype == 'text/css') {
68 $bits[] = 'assets';
69 $bits[] = 'skins';
70 $bits[] = 'sam';
71 }
72 $contentfile = $dir.'/yui/'.join('/', $bits).'/'.$filename;
2b722f87
SH
73 } else {
74 if ($version != $CFG->yui3version and $version != $CFG->yui2version and $version != 'gallery') {
75 $content .= "\n// Wrong yui version $part!\n";
76 continue;
77 }
78 $contentfile = "$CFG->libdir/yui/$part";
aa42314d 79 }
aa42314d 80 if (!file_exists($contentfile) or !is_file($contentfile)) {
a4738eb3
PS
81 $content .= "\n// Combo resource $part not found!\n";
82 continue;
aa42314d
PS
83 }
84 $filecontent = file_get_contents($contentfile);
85
86 if ($mimetype === 'text/css') {
3b17690c
SH
87 if ($version == 'moodle') {
88 $filecontent = preg_replace('/([a-z_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$frankenstyle.'/'.array_shift($bits).'/$1.$2', $filecontent);
89 } else if ($version == 'gallery') {
2a102b90 90 // search for all images in gallery module CSS and serve them through the yui_image.php script
2b722f87 91 $filecontent = preg_replace('/([a-z_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
2a102b90
SH
92 } else {
93 // search for all images in yui2 CSS and serve them through the yui_image.php script
94 $filecontent = preg_replace('/([a-z_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/$1.$2', $filecontent);
95 }
aa42314d
PS
96 }
97
98 $content .= $filecontent;
99}
100
101
102combo_send_cached($content, $mimetype);
103
104
105
106function combo_send_cached($content, $mimetype) {
107 $lifetime = 60*60*24*300; // 300 days === forever
108
109 header('Content-Disposition: inline; filename="combo"');
110 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
111 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
112 header('Pragma: ');
945f19f7 113 header('Cache-Control: max-age=315360000');
aa42314d
PS
114 header('Accept-Ranges: none');
115 header('Content-Type: '.$mimetype);
7c986f04
PS
116 if (!min_enable_zlib_compression()) {
117 header('Content-Length: '.strlen($content));
118 }
aa42314d 119
aa42314d
PS
120 echo $content;
121 die;
122}
123
124function combo_not_found() {
125 header('HTTP/1.0 404 not found');
126 die('Combo resource not found, sorry.');
127}
128
129function combo_params() {
130 if (!empty($_SERVER['REQUEST_URI'])) {
131 $parts = explode('?', $_SERVER['REQUEST_URI']);
132 if (count($parts) != 2) {
133 return '';
134 }
135 return $parts[1];
136
137 } else if (!empty($_SERVER['QUERY_STRING'])) {
138 return $_SERVER['QUERY_STRING'];
139
140 } else {
141 return '';
142 }
143}