MDL-24355 tag: minor clean up of tags changes
[moodle.git] / lib / configonlylib.php
blob8d7dbf45d16ccf8641716ed942252f6503e0403e
1 <?php
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.
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/>.
18 /**
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
25 * @package core
26 * @copyright 2009 Petr Skoda (skodak)
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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
38 function min_optional_param($name, $default, $type) {
39 $value = $default;
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);
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
59 function min_clean_param($value, $type) {
60 switch($type) {
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;
70 default: die("Coding error: incorrect parameter type specified ($type).");
73 return $value;
76 /**
77 * This method tries to enable output compression if possible.
78 * This function must be called before any output or headers.
80 * (IE6 is not supported at all.)
82 * @return boolean, true if compression enabled
84 function min_enable_zlib_compression() {
86 if (headers_sent()) {
87 return false;
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)$parts[1] < 7) {
99 @ini_set('zlib.output_compression', '0');
100 return false;
106 @ini_set('output_handler', '');
109 * docs clearly say 'on' means enable and number means size of buffer,
110 * but unfortunately some PHP version break when we set 'on' here.
111 * 1 probably sets chunk size to 4096. our CSS and JS scripts are much bigger,
112 * so let's try some bigger sizes.
114 @ini_set('zlib.output_compression', 65536);
116 return true;