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) {
39 if (isset($_GET[$name])) {
40 $value = $_GET[$name];
42 } else if (isset($_GET['amp;'.$name])) {
43 // very, very, very ugly hack, unfortunately $OUTPUT->pix_url() is not used properly in javascript code :-(
44 $value = $_GET['amp;'.$name];
46 } else if (isset($_POST[$name])) {
47 $value = $_POST[$name];
53 return min_clean_param($value, $type);
57 * Minimalistic parameter cleaning function.
59 * Note: Can not use optional param because moodlelib.php is not loaded yet.
61 * @param string $value
65 function min_clean_param($value, $type) {
67 case 'RAW': $value = min_fix_utf8((string)$value);
69 case 'INT': $value = (int)$value;
71 case 'SAFEDIR': $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value);
73 case 'SAFEPATH': $value = preg_replace('/[^a-zA-Z0-9\/\._-]/', '', $value);
74 $value = preg_replace('/\.+/', '.', $value);
75 $value = preg_replace('#/+#', '/', $value);
77 default: die("Coding error: incorrect parameter type specified ($type).");
84 * Minimalistic UTF-8 sanitisation.
86 * Note: This duplicates fix_utf8() intentionally for now.
88 * @param string $value
91 function min_fix_utf8($value) {
92 // Lower error reporting because glibc throws bogus notices.
93 $olderror = error_reporting();
94 if ($olderror & E_NOTICE
) {
95 error_reporting($olderror ^ E_NOTICE
);
98 static $buggyiconv = null;
99 if ($buggyiconv === null) {
100 $buggyiconv = (!function_exists('iconv') or iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
104 if (function_exists('mb_convert_encoding')) {
105 $subst = mb_substitute_character();
106 mb_substitute_character('');
107 $result = mb_convert_encoding($value, 'utf-8', 'utf-8');
108 mb_substitute_character($subst);
111 // Warn admins on admin/index.php page.
116 $result = iconv('UTF-8', 'UTF-8//IGNORE', $value);
119 if ($olderror & E_NOTICE
) {
120 error_reporting($olderror);
127 * This method tries to enable output compression if possible.
128 * This function must be called before any output or headers.
130 * (IE6 is not supported at all.)
132 * @return boolean, true if compression enabled
134 function min_enable_zlib_compression() {
136 if (headers_sent()) {
140 // zlib.output_compression is preferred over ob_gzhandler()
141 if (!empty($_SERVER['HTTP_USER_AGENT']) and strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false) {
142 @ini_set
('zlib.output_compression', 'Off');
143 if (function_exists('apache_setenv')) {
144 @apache_setenv
('no-gzip', 1);
149 @ini_set
('output_handler', '');
152 * docs clearly say 'on' means enable and number means size of buffer,
153 * but unfortunately some PHP version break when we set 'on' here.
154 * 1 probably sets chunk size to 4096. our CSS and JS scripts are much bigger,
155 * so let's try some bigger sizes.
157 @ini_set
('zlib.output_compression', 65536);
163 * Returns the slashargument part of the URL.
165 * Note: ".php" is NOT allowed in slasharguments,
166 * it is intended for ASCII characters only.
170 function min_get_slash_argument() {
171 // Note: This code has to work in the same cases as normal get_slash_argument(),
172 // but at the same time it may be simpler because we do not have to deal
173 // with encodings and other tricky stuff.
177 if (!empty($_GET['file']) and strpos($_GET['file'], '/') === 0) {
178 // server is using url rewriting, most probably IIS
179 return $_GET['file'];
181 } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'iis') !== false) {
182 if (isset($_SERVER['PATH_INFO']) and $_SERVER['PATH_INFO'] !== '') {
183 $relativepath = urldecode($_SERVER['PATH_INFO']);
187 if (isset($_SERVER['PATH_INFO'])) {
188 $relativepath = $_SERVER['PATH_INFO'];
193 if (preg_match('|^.+\.php(.*)$|i', $relativepath, $matches)) {
194 $relativepath = $matches[1];
197 return $relativepath;