MDL-21204 fixed incorrect url params, credit goes to Gabriel Dias
[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
36// find out what we are serving - only one type per request
37$content = '';
38if (substr($parts, -3) === '.js') {
39 $mimetype = 'application/x-javascript';
40} else if (substr($parts, -4) === '.css') {
41 $mimetype = 'text/css';
42} else {
43 combo_not_found();
44}
45
46$parts = explode('&', $parts);
47
48foreach ($parts as $part) {
49 $part = min_clean_param($part, 'SAFEPATH');
50 $bits = explode('/', $part);
51 if (count($bits) < 2) {
52 combo_not_found();
53 }
54 $version = $bits[0];
55 if ($version != $CFG->yui3version and $version != $CFG->yui2version) {
56 combo_not_found();
57 }
58 $contentfile = "$CFG->libdir/yui/$part";
59 if (!file_exists($contentfile) or !is_file($contentfile)) {
60 combo_not_found();
61 }
62 $filecontent = file_get_contents($contentfile);
63
64 if ($mimetype === 'text/css') {
65 // search for all images in yui2 CSS and serve them through the yui_image.php script
66 $filecontent = preg_replace('/([a-z-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/$1.$2', $filecontent);
67 }
68
69 $content .= $filecontent;
70}
71
72
73combo_send_cached($content, $mimetype);
74
75
76
77function combo_send_cached($content, $mimetype) {
78 $lifetime = 60*60*24*300; // 300 days === forever
79
80 header('Content-Disposition: inline; filename="combo"');
81 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
82 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
83 header('Pragma: ');
84 header('Accept-Ranges: none');
85 header('Content-Type: '.$mimetype);
86 header('Content-Length: '.strlen($content));
87
88 while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
89 echo $content;
90 die;
91}
92
93function combo_not_found() {
94 header('HTTP/1.0 404 not found');
95 die('Combo resource not found, sorry.');
96}
97
98function combo_params() {
99 if (!empty($_SERVER['REQUEST_URI'])) {
100 $parts = explode('?', $_SERVER['REQUEST_URI']);
101 if (count($parts) != 2) {
102 return '';
103 }
104 return $parts[1];
105
106 } else if (!empty($_SERVER['QUERY_STRING'])) {
107 return $_SERVER['QUERY_STRING'];
108
109 } else {
110 return '';
111 }
112}