3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Minimalistic library, usable even when no other moodle libs are loaded.
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
26 * @copyright 2009 Petr Skoda (skodak)
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 * Minimalistic parameter validation function.
32 * Can not use optional param because moodlelib.php is not loaded yet
34 * @param mixed $default
38 function min_optional_param($name, $default, $type) {
40 if (isset($_GET[$name])) {
41 $value = $_GET[$name];
43 } else if (isset($_GET['amp;'.$name])) {
44 // very, very, very ugly hack, unfortunately $OUTPUT->pix_url() is not used properly in javascript code :-(
45 $value = $_GET['amp;'.$name];
48 return min_clean_param($value, $type);
52 * Minimalistic parameter cleaning function.
53 * Can not use optional param because moodlelib.php is not loaded yet
55 * @param mixed $default
59 function min_clean_param($value, $type) {
61 case 'RAW': $value = iconv('UTF-8', 'UTF-8//IGNORE', $value);
63 case 'INT': $value = (int)$value;
65 case 'SAFEDIR': $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value);
67 case 'SAFEPATH': $value = preg_replace('/[^a-zA-Z0-9\/\._-]/', '', $value);
68 $value = preg_replace('/\.+/', '.', $value);
69 $value = preg_replace('#/+#', '/', $value);
71 default: die("Coding error: incorrect parameter type specified ($type).");
78 * This method tries to enable output compression if possible.
79 * This function must be called before any output or headers.
81 * (IE6 is not supported at all.)
83 * @return boolean, true if compression enabled
85 function min_enable_zlib_compression() {
91 // zlib.output_compression is preferred over ob_gzhandler()
92 if (!empty($_SERVER['HTTP_USER_AGENT'])) {
93 $agent = $_SERVER['HTTP_USER_AGENT'];
94 // try to detect IE6 and prevent gzip because it is extremely buggy browser
95 $parts = explode(';', $agent);
96 if (isset($parts[1])) {
97 $parts = explode(' ', trim($parts[1]));
98 if (count($parts) > 1) {
99 if ($parts[0] === 'MSIE' and (float)$parts[1] < 7) {
100 @ini_set
('zlib.output_compression', '0');
107 @ini_set
('output_handler', '');
110 * docs clearly say 'on' means enable and number means size of buffer,
111 * but unfortunately some PHP version break when we set 'on' here.
112 * 1 probably sets chunk size to 4096. our CSS and JS scripts are much bigger,
113 * so let's try some bigger sizes.
115 @ini_set
('zlib.output_compression', 65536);