MDL-21241 theme CSS and JS gzip compression + some more fixes
[moodle.git] / lib / configonlylib.php
CommitLineData
78946b9b
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 * Minimalistic library, usable even when no other moodle libs are loaded.
20 *
21 * The only library that gets loaded if you define ABORT_AFTER_CONFIG
22 * before including main config.php. You can resume normal script operation
23 * if you define ABORT_AFTER_CONFIG_CANCEL and require the setup.php
24 *
25 * @package moodlecore
26 * @copyright 2009 petr Skoda (skodak)
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 */
29
30/**
31 * Minimalistic parameter validation function.
32 * Can not use optional param because moodlelib.php is not loaded yet
33 * @param string $name
34 * @param mixed $default
35 * @param string $type
36 * @return mixed
37 */
38function min_optional_param($name, $default, $type) {
39 $value = $default;
40 if (isset($_GET[$name])) {
41 $value = $_GET[$name];
42
43 } else if (isset($_GET['amp;'.$name])) {
b5d0cafc 44 // very, very, very ugly hack, unforunately $OUTPUT->pix_url() is not used properly in javascript code :-(
78946b9b
PS
45 $value = $_GET['amp;'.$name];
46 }
60f2c866 47
aa42314d
PS
48 return min_clean_param($value, $type);
49}
50
51/**
52 * Minimalistic parameter cleaning function.
53 * Can not use optional param because moodlelib.php is not loaded yet
54 * @param string $name
55 * @param mixed $default
56 * @param string $type
57 * @return mixed
58 */
59function min_clean_param($value, $type) {
78946b9b 60 switch($type) {
60f2c866
PS
61 case 'RAW': break;
62 case 'INT': $value = (int)$value;
63 break;
64 case 'SAFEDIR': $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value);
65 break;
66 case 'SAFEPATH': $value = preg_replace('/[^a-zA-Z0-9\/\._-]/', '', $value);
67 $value = preg_replace('/\.+/', '.', $value);
68 $value = preg_replace('#/+#', '/', $value);
69 break;
78946b9b
PS
70 default: die("Coding error: incorrent parameter type specified ($type).");
71 }
60f2c866 72
78946b9b 73 return $value;
7c986f04
PS
74}
75
76/**
77 * This method tries to enable output compression if possible.
78 * This function must be called before any output or headers.
79 *
80 * (IE6 is not supported at all.)
81 *
82 * @return boolean, true if compression enabled
83 */
84function min_enable_zlib_compression() {
85
86 if (headers_sent()) {
87 return false;
88 }
89
90 // zlib.output_compression is preferred over ob_gzhandler()
91 if (!empty($_SERVER['HTTP_USER_AGENT'])) {
92 $agent = $_SERVER['HTTP_USER_AGENT'];
93 // try to detect IE6 and prevent gzip because it is extremely buggy browser
94 $parts = explode(';', $agent);
95 if (isset($parts[1])) {
96 $parts = explode(' ', trim($parts[1]));
97 if (count($parts) > 1) {
98 if ($parts[0] === 'MSIE' and (float)$string[1] < 7) {
99 @ini_set('zlib.output_compression', '0');
100 return false;
101 }
102 }
103 }
104 }
105
106 @ini_set('output_handler', '');
107 @ini_set('zlib.output_compression', 'on');
108
109 return true;
aa42314d 110}