abf45bed |
1 | <?php // $Id$ |
f9903ed0 |
2 | |
9fa49e22 |
3 | /////////////////////////////////////////////////////////////////////////// |
4 | // // |
5 | // NOTICE OF COPYRIGHT // |
6 | // // |
7 | // Moodle - Modular Object-Oriented Dynamic Learning Environment // |
8 | // http://moodle.com // |
9 | // // |
10 | // Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com // |
11 | // // |
12 | // This program is free software; you can redistribute it and/or modify // |
13 | // it under the terms of the GNU General Public License as published by // |
14 | // the Free Software Foundation; either version 2 of the License, or // |
15 | // (at your option) any later version. // |
16 | // // |
17 | // This program is distributed in the hope that it will be useful, // |
18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
20 | // GNU General Public License for more details: // |
21 | // // |
22 | // http://www.gnu.org/copyleft/gpl.html // |
23 | // // |
24 | /////////////////////////////////////////////////////////////////////////// |
f9903ed0 |
25 | |
7cf1c7bd |
26 | /** |
27 | * Library of functions for web output |
28 | * |
29 | * Library of all general-purpose Moodle PHP functions and constants |
30 | * that produce HTML output |
31 | * |
32 | * Other main libraries: |
33 | * - datalib.php - functions that access the database. |
34 | * - moodlelib.php - general-purpose Moodle functions. |
35 | * @author Martin Dougiamas |
36 | * @version $Id$ |
89dcb99d |
37 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
7cf1c7bd |
38 | * @package moodlecore |
39 | */ |
772e78be |
40 | |
6aaa17c7 |
41 | /// We are going to uses filterlib functions here |
42 | require_once("$CFG->libdir/filterlib.php"); |
43 | |
a194510a |
44 | require_once("$CFG->libdir/ajax/ajaxlib.php"); |
45 | |
0095d5cd |
46 | /// Constants |
47 | |
c1d57101 |
48 | /// Define text formatting types ... eventually we can add Wiki, BBcode etc |
7cf1c7bd |
49 | |
50 | /** |
51 | * Does all sorts of transformations and filtering |
52 | */ |
b0ccd3fb |
53 | define('FORMAT_MOODLE', '0'); // Does all sorts of transformations and filtering |
7cf1c7bd |
54 | |
55 | /** |
56 | * Plain HTML (with some tags stripped) |
57 | */ |
b0ccd3fb |
58 | define('FORMAT_HTML', '1'); // Plain HTML (with some tags stripped) |
7cf1c7bd |
59 | |
60 | /** |
61 | * Plain text (even tags are printed in full) |
62 | */ |
b0ccd3fb |
63 | define('FORMAT_PLAIN', '2'); // Plain text (even tags are printed in full) |
7cf1c7bd |
64 | |
65 | /** |
66 | * Wiki-formatted text |
6a6495ff |
67 | * Deprecated: left here just to note that '3' is not used (at the moment) |
68 | * and to catch any latent wiki-like text (which generates an error) |
7cf1c7bd |
69 | */ |
b0ccd3fb |
70 | define('FORMAT_WIKI', '3'); // Wiki-formatted text |
7cf1c7bd |
71 | |
72 | /** |
73 | * Markdown-formatted text http://daringfireball.net/projects/markdown/ |
74 | */ |
b0ccd3fb |
75 | define('FORMAT_MARKDOWN', '4'); // Markdown-formatted text http://daringfireball.net/projects/markdown/ |
0095d5cd |
76 | |
7d8a3cb0 |
77 | /** |
78 | * TRUSTTEXT marker - if present in text, text cleaning should be bypassed |
79 | */ |
80 | define('TRUSTTEXT', '#####TRUSTTEXT#####'); |
81 | |
7cf1c7bd |
82 | |
83 | /** |
84 | * Allowed tags - string of html tags that can be tested against for safe html tags |
85 | * @global string $ALLOWED_TAGS |
86 | */ |
5ea4af22 |
87 | global $ALLOWED_TAGS; |
39dda0fc |
88 | $ALLOWED_TAGS = |
d046ae55 |
89 | '<p><br><b><i><u><font><table><tbody><span><div><tr><td><th><ol><ul><dl><li><dt><dd><h1><h2><h3><h4><h5><h6><hr><img><a><strong><emphasis><em><sup><sub><address><cite><blockquote><pre><strike><param><acronym><nolink><lang><tex><algebra><math><mi><mn><mo><mtext><mspace><ms><mrow><mfrac><msqrt><mroot><mstyle><merror><mpadded><mphantom><mfenced><msub><msup><msubsup><munder><mover><munderover><mmultiscripts><mtable><mtr><mtd><maligngroup><malignmark><maction><cn><ci><apply><reln><fn><interval><inverse><sep><condition><declare><lambda><compose><ident><quotient><exp><factorial><divide><max><min><minus><plus><power><rem><times><root><gcd><and><or><xor><not><implies><forall><exists><abs><conjugate><eq><neq><gt><lt><geq><leq><ln><log><int><diff><partialdiff><lowlimit><uplimit><bvar><degree><set><list><union><intersect><in><notin><subset><prsubset><notsubset><notprsubset><setdiff><sum><product><limit><tendsto><mean><sdev><variance><median><mode><moment><vector><matrix><matrixrow><determinant><transpose><selector><annotation><semantics><annotation-xml><tt><code>'; |
90 | |
037dcbb6 |
91 | /** |
92 | * Allowed protocols - array of protocols that are safe to use in links and so on |
93 | * @global string $ALLOWED_PROTOCOLS |
94 | */ |
f941df22 |
95 | $ALLOWED_PROTOCOLS = array('http', 'https', 'ftp', 'news', 'mailto', 'rtsp', 'teamspeak', 'gopher', 'mms', |
c06c8492 |
96 | 'color', 'callto', 'cursor', 'text-align', 'font-size', 'font-weight', 'font-style', |
016ffbd7 |
97 | 'border', 'margin', 'padding', 'background'); // CSS as well to get through kses |
037dcbb6 |
98 | |
99 | |
0095d5cd |
100 | /// Functions |
101 | |
7cf1c7bd |
102 | /** |
103 | * Add quotes to HTML characters |
104 | * |
105 | * Returns $var with HTML characters (like "<", ">", etc.) properly quoted. |
106 | * This function is very similar to {@link p()} |
107 | * |
108 | * @param string $var the string potentially containing HTML characters |
d4a42ff4 |
109 | * @param boolean $strip to decide if we want to strip slashes or no. Default to false. |
110 | * true should be used to print data from forms and false for data from DB. |
7cf1c7bd |
111 | * @return string |
112 | */ |
473d29eb |
113 | function s($var, $strip=false) { |
d4a42ff4 |
114 | |
63e554d0 |
115 | if ($var == '0') { // for integer 0, boolean false, string '0' |
116 | return '0'; |
3662bce5 |
117 | } |
d4a42ff4 |
118 | |
119 | if ($strip) { |
473d29eb |
120 | return preg_replace("/&(#\d+);/i", "&$1;", htmlspecialchars(stripslashes_safe($var))); |
121 | } else { |
122 | return preg_replace("/&(#\d+);/i", "&$1;", htmlspecialchars($var)); |
d4a42ff4 |
123 | } |
f9903ed0 |
124 | } |
125 | |
7cf1c7bd |
126 | /** |
127 | * Add quotes to HTML characters |
128 | * |
d48b00b4 |
129 | * Prints $var with HTML characters (like "<", ">", etc.) properly quoted. |
7cf1c7bd |
130 | * This function is very similar to {@link s()} |
131 | * |
132 | * @param string $var the string potentially containing HTML characters |
d4a42ff4 |
133 | * @param boolean $strip to decide if we want to strip slashes or no. Default to false. |
134 | * true should be used to print data from forms and false for data from DB. |
7cf1c7bd |
135 | * @return string |
136 | */ |
d4a42ff4 |
137 | function p($var, $strip=false) { |
138 | echo s($var, $strip); |
f9903ed0 |
139 | } |
140 | |
0d1cd0ea |
141 | /** |
142 | * Does proper javascript quoting. |
5ce73257 |
143 | * Do not use addslashes anymore, because it does not work when magic_quotes_sybase is enabled. |
144 | * |
e2cd3ed0 |
145 | * @since 1.8 - 22/02/2007 |
0d1cd0ea |
146 | * @param mixed value |
147 | * @return mixed quoted result |
148 | */ |
149 | function addslashes_js($var) { |
150 | if (is_string($var)) { |
151 | $var = str_replace('\\', '\\\\', $var); |
152 | $var = str_replace(array('\'', '"', "\n", "\r", "\0"), array('\\\'', '\\"', '\\n', '\\r', '\\0'), $var); |
4702be4e |
153 | $var = str_replace('</', '<\/', $var); // XHTML compliance |
0d1cd0ea |
154 | } else if (is_array($var)) { |
155 | $var = array_map('addslashes_js', $var); |
156 | } else if (is_object($var)) { |
157 | $a = get_object_vars($var); |
158 | foreach ($a as $key=>$value) { |
159 | $a[$key] = addslashes_js($value); |
160 | } |
161 | $var = (object)$a; |
162 | } |
163 | return $var; |
164 | } |
7cf1c7bd |
165 | |
7cf1c7bd |
166 | /** |
167 | * Remove query string from url |
168 | * |
169 | * Takes in a URL and returns it without the querystring portion |
170 | * |
171 | * @param string $url the url which may have a query string attached |
172 | * @return string |
173 | */ |
174 | function strip_querystring($url) { |
f9903ed0 |
175 | |
b9b8ab69 |
176 | if ($commapos = strpos($url, '?')) { |
177 | return substr($url, 0, $commapos); |
178 | } else { |
179 | return $url; |
180 | } |
f9903ed0 |
181 | } |
182 | |
7cf1c7bd |
183 | /** |
c8135a35 |
184 | * Returns the URL of the HTTP_REFERER, less the querystring portion if required |
7cf1c7bd |
185 | * @return string |
186 | */ |
c8135a35 |
187 | function get_referer($stripquery=true) { |
d90ffc1f |
188 | if (isset($_SERVER['HTTP_REFERER'])) { |
c8135a35 |
189 | if ($stripquery) { |
190 | return strip_querystring($_SERVER['HTTP_REFERER']); |
191 | } else { |
192 | return $_SERVER['HTTP_REFERER']; |
193 | } |
d90ffc1f |
194 | } else { |
5ce73257 |
195 | return ''; |
d90ffc1f |
196 | } |
f9903ed0 |
197 | } |
198 | |
c1d57101 |
199 | |
7cf1c7bd |
200 | /** |
201 | * Returns the name of the current script, WITH the querystring portion. |
202 | * this function is necessary because PHP_SELF and REQUEST_URI and SCRIPT_NAME |
203 | * return different things depending on a lot of things like your OS, Web |
204 | * server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.) |
d48b00b4 |
205 | * <b>NOTE:</b> This function returns false if the global variables needed are not set. |
206 | * |
7cf1c7bd |
207 | * @return string |
208 | */ |
209 | function me() { |
f9903ed0 |
210 | |
b0ccd3fb |
211 | if (!empty($_SERVER['REQUEST_URI'])) { |
212 | return $_SERVER['REQUEST_URI']; |
c1d57101 |
213 | |
b0ccd3fb |
214 | } else if (!empty($_SERVER['PHP_SELF'])) { |
215 | if (!empty($_SERVER['QUERY_STRING'])) { |
216 | return $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING']; |
fced815c |
217 | } |
b0ccd3fb |
218 | return $_SERVER['PHP_SELF']; |
c1d57101 |
219 | |
b0ccd3fb |
220 | } else if (!empty($_SERVER['SCRIPT_NAME'])) { |
221 | if (!empty($_SERVER['QUERY_STRING'])) { |
222 | return $_SERVER['SCRIPT_NAME'] .'?'. $_SERVER['QUERY_STRING']; |
fced815c |
223 | } |
b0ccd3fb |
224 | return $_SERVER['SCRIPT_NAME']; |
fced815c |
225 | |
b0ccd3fb |
226 | } else if (!empty($_SERVER['URL'])) { // May help IIS (not well tested) |
227 | if (!empty($_SERVER['QUERY_STRING'])) { |
228 | return $_SERVER['URL'] .'?'. $_SERVER['QUERY_STRING']; |
16c4d82a |
229 | } |
b0ccd3fb |
230 | return $_SERVER['URL']; |
16c4d82a |
231 | |
b9b8ab69 |
232 | } else { |
b0ccd3fb |
233 | notify('Warning: Could not find any of these web server variables: $REQUEST_URI, $PHP_SELF, $SCRIPT_NAME or $URL'); |
bcdfe14e |
234 | return false; |
7fbd6b1c |
235 | } |
f9903ed0 |
236 | } |
237 | |
7cf1c7bd |
238 | /** |
d48b00b4 |
239 | * Like {@link me()} but returns a full URL |
7cf1c7bd |
240 | * @see me() |
7cf1c7bd |
241 | * @return string |
242 | */ |
f9903ed0 |
243 | function qualified_me() { |
f9903ed0 |
244 | |
b6e22603 |
245 | global $CFG; |
246 | |
68796e6b |
247 | if (!empty($CFG->wwwroot)) { |
248 | $url = parse_url($CFG->wwwroot); |
249 | } |
b6e22603 |
250 | |
251 | if (!empty($url['host'])) { |
252 | $hostname = $url['host']; |
253 | } else if (!empty($_SERVER['SERVER_NAME'])) { |
b0ccd3fb |
254 | $hostname = $_SERVER['SERVER_NAME']; |
255 | } else if (!empty($_ENV['SERVER_NAME'])) { |
256 | $hostname = $_ENV['SERVER_NAME']; |
257 | } else if (!empty($_SERVER['HTTP_HOST'])) { |
258 | $hostname = $_SERVER['HTTP_HOST']; |
259 | } else if (!empty($_ENV['HTTP_HOST'])) { |
260 | $hostname = $_ENV['HTTP_HOST']; |
39e018b3 |
261 | } else { |
b0ccd3fb |
262 | notify('Warning: could not find the name of this server!'); |
bcdfe14e |
263 | return false; |
c1d57101 |
264 | } |
dc28eede |
265 | |
266 | if (!empty($url['port'])) { |
267 | $hostname .= ':'.$url['port']; |
268 | } else if (!empty($_SERVER['SERVER_PORT'])) { |
269 | if ($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) { |
270 | $hostname .= ':'.$_SERVER['SERVER_PORT']; |
271 | } |
272 | } |
273 | |
f77c261e |
274 | if (isset($_SERVER['HTTPS'])) { |
275 | $protocol = ($_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://'; |
276 | } else if (isset($_SERVER['SERVER_PORT'])) { # Apache2 does not export $_SERVER['HTTPS'] |
277 | $protocol = ($_SERVER['SERVER_PORT'] == '443') ? 'https://' : 'http://'; |
278 | } else { |
279 | $protocol = 'http://'; |
280 | } |
f9903ed0 |
281 | |
39e018b3 |
282 | $url_prefix = $protocol.$hostname; |
b9b8ab69 |
283 | return $url_prefix . me(); |
f9903ed0 |
284 | } |
285 | |
7cf1c7bd |
286 | /** |
287 | * Determine if there is data waiting to be processed from a form |
288 | * |
289 | * Used on most forms in Moodle to check for data |
290 | * Returns the data as an object, if it's found. |
291 | * This object can be used in foreach loops without |
292 | * casting because it's cast to (array) automatically |
772e78be |
293 | * |
9c0f063b |
294 | * Checks that submitted POST data exists and returns it as object. |
d48b00b4 |
295 | * |
9c0f063b |
296 | * @param string $url not used anymore |
297 | * @return mixed false or object |
7cf1c7bd |
298 | */ |
b0ccd3fb |
299 | function data_submitted($url='') { |
d48b00b4 |
300 | |
607809b3 |
301 | if (empty($_POST)) { |
36b4f985 |
302 | return false; |
303 | } else { |
9c0f063b |
304 | return (object)$_POST; |
36b4f985 |
305 | } |
306 | } |
307 | |
7cf1c7bd |
308 | /** |
e8d1c9ed |
309 | * Moodle replacement for php stripslashes() function, |
310 | * works also for objects and arrays. |
7cf1c7bd |
311 | * |
772e78be |
312 | * The standard php stripslashes() removes ALL backslashes |
7cf1c7bd |
313 | * even from strings - so C:\temp becomes C:temp - this isn't good. |
314 | * This function should work as a fairly safe replacement |
315 | * to be called on quoted AND unquoted strings (to be sure) |
d48b00b4 |
316 | * |
e8d1c9ed |
317 | * @param mixed something to remove unsafe slashes from |
318 | * @return mixed |
7cf1c7bd |
319 | */ |
e8d1c9ed |
320 | function stripslashes_safe($mixed) { |
321 | // there is no need to remove slashes from int, float and bool types |
322 | if (empty($mixed)) { |
323 | //nothing to do... |
324 | } else if (is_string($mixed)) { |
de64b6c6 |
325 | if (ini_get_bool('magic_quotes_sybase')) { //only unescape single quotes |
326 | $mixed = str_replace("''", "'", $mixed); |
327 | } else { //the rest, simple and double quotes and backslashes |
328 | $mixed = str_replace("\\'", "'", $mixed); |
329 | $mixed = str_replace('\\"', '"', $mixed); |
330 | $mixed = str_replace('\\\\', '\\', $mixed); |
331 | } |
e8d1c9ed |
332 | } else if (is_array($mixed)) { |
333 | foreach ($mixed as $key => $value) { |
334 | $mixed[$key] = stripslashes_safe($value); |
335 | } |
336 | } else if (is_object($mixed)) { |
337 | $vars = get_object_vars($mixed); |
338 | foreach ($vars as $key => $value) { |
339 | $mixed->$key = stripslashes_safe($value); |
340 | } |
341 | } |
7d8f674d |
342 | |
e8d1c9ed |
343 | return $mixed; |
7d8f674d |
344 | } |
f9903ed0 |
345 | |
9b4b78fd |
346 | /** |
347 | * Recursive implementation of stripslashes() |
348 | * |
349 | * This function will allow you to strip the slashes from a variable. |
350 | * If the variable is an array or object, slashes will be stripped |
351 | * from the items (or properties) it contains, even if they are arrays |
352 | * or objects themselves. |
353 | * |
354 | * @param mixed the variable to remove slashes from |
355 | * @return mixed |
356 | */ |
357 | function stripslashes_recursive($var) { |
358 | if(is_object($var)) { |
359 | $properties = get_object_vars($var); |
360 | foreach($properties as $property => $value) { |
361 | $var->$property = stripslashes_recursive($value); |
362 | } |
363 | } |
364 | else if(is_array($var)) { |
365 | foreach($var as $property => $value) { |
366 | $var[$property] = stripslashes_recursive($value); |
367 | } |
368 | } |
369 | else if(is_string($var)) { |
370 | $var = stripslashes($var); |
371 | } |
04480de3 |
372 | return $var; |
373 | } |
374 | |
375 | /** |
376 | * Recursive implementation of addslashes() |
377 | * |
378 | * This function will allow you to add the slashes from a variable. |
379 | * If the variable is an array or object, slashes will be added |
380 | * to the items (or properties) it contains, even if they are arrays |
381 | * or objects themselves. |
382 | * |
383 | * @param mixed the variable to add slashes from |
384 | * @return mixed |
385 | */ |
386 | function addslashes_recursive($var) { |
387 | if(is_object($var)) { |
388 | $properties = get_object_vars($var); |
389 | foreach($properties as $property => $value) { |
390 | $var->$property = addslashes_recursive($value); |
391 | } |
392 | } |
393 | else if(is_array($var)) { |
394 | foreach($var as $property => $value) { |
395 | $var[$property] = addslashes_recursive($value); |
396 | } |
397 | } |
398 | else if(is_string($var)) { |
399 | $var = addslashes($var); |
400 | } |
9b4b78fd |
401 | return $var; |
402 | } |
403 | |
7cf1c7bd |
404 | /** |
d48b00b4 |
405 | * Given some normal text this function will break up any |
406 | * long words to a given size by inserting the given character |
407 | * |
6aaa17c7 |
408 | * It's multibyte savvy and doesn't change anything inside html tags. |
409 | * |
7cf1c7bd |
410 | * @param string $string the string to be modified |
89dcb99d |
411 | * @param int $maxsize maximum length of the string to be returned |
7cf1c7bd |
412 | * @param string $cutchar the string used to represent word breaks |
413 | * @return string |
414 | */ |
4a5644e5 |
415 | function break_up_long_words($string, $maxsize=20, $cutchar=' ') { |
a2b3f884 |
416 | |
6aaa17c7 |
417 | /// Loading the textlib singleton instance. We are going to need it. |
418 | $textlib = textlib_get_instance(); |
8f7dc7f1 |
419 | |
6aaa17c7 |
420 | /// First of all, save all the tags inside the text to skip them |
421 | $tags = array(); |
422 | filter_save_tags($string,$tags); |
5b07d990 |
423 | |
6aaa17c7 |
424 | /// Process the string adding the cut when necessary |
4a5644e5 |
425 | $output = ''; |
810944af |
426 | $length = $textlib->strlen($string); |
4a5644e5 |
427 | $wordlength = 0; |
428 | |
429 | for ($i=0; $i<$length; $i++) { |
810944af |
430 | $char = $textlib->substr($string, $i, 1); |
6aaa17c7 |
431 | if ($char == ' ' or $char == "\t" or $char == "\n" or $char == "\r" or $char == "<" or $char == ">") { |
4a5644e5 |
432 | $wordlength = 0; |
433 | } else { |
434 | $wordlength++; |
435 | if ($wordlength > $maxsize) { |
436 | $output .= $cutchar; |
437 | $wordlength = 0; |
438 | } |
439 | } |
440 | $output .= $char; |
441 | } |
6aaa17c7 |
442 | |
443 | /// Finally load the tags back again |
444 | if (!empty($tags)) { |
445 | $output = str_replace(array_keys($tags), $tags, $output); |
446 | } |
447 | |
4a5644e5 |
448 | return $output; |
449 | } |
450 | |
7cf1c7bd |
451 | /** |
452 | * This does a search and replace, ignoring case |
453 | * This function is only used for versions of PHP older than version 5 |
454 | * which do not have a native version of this function. |
455 | * Taken from the PHP manual, by bradhuizenga @ softhome.net |
d48b00b4 |
456 | * |
7cf1c7bd |
457 | * @param string $find the string to search for |
458 | * @param string $replace the string to replace $find with |
459 | * @param string $string the string to search through |
460 | * return string |
461 | */ |
ce57cc79 |
462 | if (!function_exists('str_ireplace')) { /// Only exists in PHP 5 |
7ec2fc00 |
463 | function str_ireplace($find, $replace, $string) { |
7ec2fc00 |
464 | |
465 | if (!is_array($find)) { |
466 | $find = array($find); |
467 | } |
468 | |
469 | if(!is_array($replace)) { |
470 | if (!is_array($find)) { |
471 | $replace = array($replace); |
472 | } else { |
473 | // this will duplicate the string into an array the size of $find |
474 | $c = count($find); |
475 | $rString = $replace; |
476 | unset($replace); |
477 | for ($i = 0; $i < $c; $i++) { |
478 | $replace[$i] = $rString; |
479 | } |
480 | } |
481 | } |
482 | |
483 | foreach ($find as $fKey => $fItem) { |
484 | $between = explode(strtolower($fItem),strtolower($string)); |
485 | $pos = 0; |
486 | foreach($between as $bKey => $bItem) { |
487 | $between[$bKey] = substr($string,$pos,strlen($bItem)); |
488 | $pos += strlen($bItem) + strlen($fItem); |
489 | } |
490 | $string = implode($replace[$fKey],$between); |
72e4eac6 |
491 | } |
7ec2fc00 |
492 | return ($string); |
3fe3851d |
493 | } |
3fe3851d |
494 | } |
495 | |
7cf1c7bd |
496 | /** |
497 | * Locate the position of a string in another string |
498 | * |
499 | * This function is only used for versions of PHP older than version 5 |
500 | * which do not have a native version of this function. |
501 | * Taken from the PHP manual, by dmarsh @ spscc.ctc.edu |
d48b00b4 |
502 | * |
7cf1c7bd |
503 | * @param string $haystack The string to be searched |
504 | * @param string $needle The string to search for |
89dcb99d |
505 | * @param int $offset The position in $haystack where the search should begin. |
7cf1c7bd |
506 | */ |
ce57cc79 |
507 | if (!function_exists('stripos')) { /// Only exists in PHP 5 |
508 | function stripos($haystack, $needle, $offset=0) { |
d48b00b4 |
509 | |
ce57cc79 |
510 | return strpos(strtoupper($haystack), strtoupper($needle), $offset); |
511 | } |
512 | } |
513 | |
7cf1c7bd |
514 | /** |
515 | * This function will create a HTML link that will work on both |
516 | * Javascript and non-javascript browsers. |
517 | * Relies on the Javascript function openpopup in javascript.php |
d48b00b4 |
518 | * |
7cf1c7bd |
519 | * $url must be relative to home page eg /mod/survey/stuff.php |
520 | * @param string $url Web link relative to home page |
521 | * @param string $name Name to be assigned to the popup window |
522 | * @param string $linkname Text to be displayed as web link |
89dcb99d |
523 | * @param int $height Height to assign to popup window |
524 | * @param int $width Height to assign to popup window |
7cf1c7bd |
525 | * @param string $title Text to be displayed as popup page title |
526 | * @param string $options List of additional options for popup window |
527 | * @todo Add code examples and list of some options that might be used. |
528 | * @param boolean $return Should the link to the popup window be returned as a string (true) or printed immediately (false)? |
529 | * @return string |
530 | * @uses $CFG |
531 | */ |
b0ccd3fb |
532 | function link_to_popup_window ($url, $name='popup', $linkname='click here', |
da4124be |
533 | $height=400, $width=500, $title='Popup window', |
534 | $options='none', $return=false) { |
f9903ed0 |
535 | |
ff80e012 |
536 | global $CFG; |
537 | |
b0ccd3fb |
538 | if ($options == 'none') { |
539 | $options = 'menubar=0,location=0,scrollbars,resizable,width='. $width .',height='. $height; |
b48f834c |
540 | } |
86aa7ccf |
541 | $fullscreen = 0; |
f9903ed0 |
542 | |
c80b7585 |
543 | if (!(strpos($url,$CFG->wwwroot) === false)) { // some log url entries contain _SERVER[HTTP_REFERRER] in which case wwwroot is already there. |
d2fa35a8 |
544 | $url = substr($url, strlen($CFG->wwwroot)); |
c80b7585 |
545 | } |
546 | |
886c109d |
547 | $link = '<a title="'. s(strip_tags($title)) .'" href="'. $CFG->wwwroot . $url .'" '. |
593bc22b |
548 | "onclick=\"this.target='$name'; return openpopup('$url', '$name', '$options', $fullscreen);\">$linkname</a>"; |
1f2eec7b |
549 | if ($return) { |
550 | return $link; |
551 | } else { |
552 | echo $link; |
553 | } |
f9903ed0 |
554 | } |
555 | |
7cf1c7bd |
556 | /** |
557 | * This function will print a button submit form element |
558 | * that will work on both Javascript and non-javascript browsers. |
559 | * Relies on the Javascript function openpopup in javascript.php |
d48b00b4 |
560 | * |
7cf1c7bd |
561 | * $url must be relative to home page eg /mod/survey/stuff.php |
562 | * @param string $url Web link relative to home page |
563 | * @param string $name Name to be assigned to the popup window |
564 | * @param string $linkname Text to be displayed as web link |
89dcb99d |
565 | * @param int $height Height to assign to popup window |
566 | * @param int $width Height to assign to popup window |
7cf1c7bd |
567 | * @param string $title Text to be displayed as popup page title |
568 | * @param string $options List of additional options for popup window |
dc28eede |
569 | * @param string $return If true, return as a string, otherwise print |
7cf1c7bd |
570 | * @return string |
571 | * @uses $CFG |
572 | */ |
b0ccd3fb |
573 | function button_to_popup_window ($url, $name='popup', $linkname='click here', |
572fe9ab |
574 | $height=400, $width=500, $title='Popup window', $options='none', $return=false, |
0f7d4e5e |
575 | $id='', $class='') { |
52f1b496 |
576 | |
577 | global $CFG; |
578 | |
b0ccd3fb |
579 | if ($options == 'none') { |
580 | $options = 'menubar=0,location=0,scrollbars,resizable,width='. $width .',height='. $height; |
52f1b496 |
581 | } |
0f7d4e5e |
582 | |
583 | if ($id) { |
584 | $id = ' id="'.$id.'" '; |
585 | } |
586 | if ($class) { |
587 | $class = ' class="'.$class.'" '; |
588 | } |
52f1b496 |
589 | $fullscreen = 0; |
590 | |
0f7d4e5e |
591 | $button = '<input type="button" name="'.$name.'" title="'. $title .'" value="'. $linkname .' ..." '.$id.$class. |
dc28eede |
592 | "onclick=\"return openpopup('$url', '$name', '$options', $fullscreen);\" />\n"; |
593 | if ($return) { |
594 | return $button; |
595 | } else { |
596 | echo $button; |
597 | } |
52f1b496 |
598 | } |
599 | |
600 | |
7cf1c7bd |
601 | /** |
602 | * Prints a simple button to close a window |
603 | */ |
eaeaf964 |
604 | function close_window_button($name='closewindow', $return=false) { |
38d5ab86 |
605 | global $CFG; |
606 | |
eaeaf964 |
607 | $output = ''; |
608 | |
4079b3d7 |
609 | $output .= '<div class="closewindow">' . "\n"; |
38d5ab86 |
610 | $output .= '<form action="'.$CFG->wwwroot.'"><div>'; // We don't use this |
611 | $output .= '<input type="button" onclick="self.close();" value="'.get_string($name).'" />'; |
612 | $output .= '</div></form>'; |
4079b3d7 |
613 | $output .= '</div>' . "\n"; |
eaeaf964 |
614 | |
615 | if ($return) { |
616 | return $output; |
617 | } else { |
618 | echo $output; |
619 | } |
f9903ed0 |
620 | } |
621 | |
08396bb2 |
622 | /* |
623 | * Try and close the current window immediately using Javascript |
624 | */ |
625 | function close_window($delay=0) { |
dfa924cc |
626 | ?> |
627 | <script type="text/javascript"> |
4079b3d7 |
628 | //<![CDATA[ |
dfa924cc |
629 | function close_this_window() { |
630 | self.close(); |
631 | } |
632 | setTimeout("close_this_window()", <?php echo $delay * 1000 ?>); |
4079b3d7 |
633 | //]]> |
dfa924cc |
634 | </script> |
635 | <noscript><center> |
636 | <?php print_string('pleaseclose') ?> |
637 | </center></noscript> |
638 | <?php |
639 | die; |
08396bb2 |
640 | } |
641 | |
642 | |
d48b00b4 |
643 | /** |
644 | * Given an array of value, creates a popup menu to be part of a form |
645 | * $options["value"]["label"] |
646 | * |
647 | * @param type description |
648 | * @todo Finish documenting this function |
649 | */ |
c06c8492 |
650 | function choose_from_menu ($options, $name, $selected='', $nothing='choose', $script='', |
7850588a |
651 | $nothingvalue='0', $return=false, $disabled=false, $tabindex=0, $id='') { |
ab9f24ad |
652 | |
b0ccd3fb |
653 | if ($nothing == 'choose') { |
654 | $nothing = get_string('choose') .'...'; |
618b22c5 |
655 | } |
656 | |
48e535bc |
657 | $attributes = ($script) ? 'onchange="'. $script .'"' : ''; |
658 | if ($disabled) { |
659 | $attributes .= ' disabled="disabled"'; |
f9903ed0 |
660 | } |
9c9f7d77 |
661 | |
25e5dbf9 |
662 | if ($tabindex) { |
663 | $attributes .= ' tabindex="'.$tabindex.'"'; |
664 | } |
665 | |
7850588a |
666 | if ($id ==='') { |
a1c91f9a |
667 | $id = 'menu'.$name; |
668 | // name may contaion [], which would make an invalid id. e.g. numeric question type editing form, assignment quickgrading |
669 | $id = str_replace('[', '', $id); |
670 | $id = str_replace(']', '', $id); |
7850588a |
671 | } |
672 | |
673 | $output = '<select id="'.$id.'" name="'. $name .'" '. $attributes .'>' . "\n"; |
bda8d43a |
674 | if ($nothing) { |
7850588a |
675 | $output .= ' <option value="'. s($nothingvalue) .'"'. "\n"; |
cec0a0fc |
676 | if ($nothingvalue === $selected) { |
b0ccd3fb |
677 | $output .= ' selected="selected"'; |
bda8d43a |
678 | } |
b0ccd3fb |
679 | $output .= '>'. $nothing .'</option>' . "\n"; |
873960de |
680 | } |
607809b3 |
681 | if (!empty($options)) { |
682 | foreach ($options as $value => $label) { |
7850588a |
683 | $output .= ' <option value="'. s($value) .'"'; |
bd905b45 |
684 | if ((string)$value == (string)$selected) { |
b0ccd3fb |
685 | $output .= ' selected="selected"'; |
607809b3 |
686 | } |
b0ccd3fb |
687 | if ($label === '') { |
688 | $output .= '>'. $value .'</option>' . "\n"; |
a20c1090 |
689 | } else { |
b0ccd3fb |
690 | $output .= '>'. $label .'</option>' . "\n"; |
607809b3 |
691 | } |
f9903ed0 |
692 | } |
693 | } |
b0ccd3fb |
694 | $output .= '</select>' . "\n"; |
08056730 |
695 | |
696 | if ($return) { |
697 | return $output; |
698 | } else { |
699 | echo $output; |
700 | } |
ab9f24ad |
701 | } |
f9903ed0 |
702 | |
73d4db84 |
703 | /** |
704 | * Choose value 0 or 1 from a menu with options 'No' and 'Yes'. |
705 | * Other options like choose_from_menu. |
706 | */ |
707 | function choose_from_menu_yesno($name, $selected, $script = '', |
708 | $return = false, $disabled = false, $tabindex = 0) { |
709 | return choose_from_menu(array(get_string('no'), get_string('yes')), $name, |
710 | $selected, '', $script, '0', $return, $disabled, $tabindex); |
711 | } |
712 | |
14040797 |
713 | /** |
714 | * Just like choose_from_menu, but takes a nested array (2 levels) and makes a dropdown menu |
715 | * including option headings with the first level. |
716 | */ |
717 | function choose_from_menu_nested($options,$name,$selected='',$nothing='choose',$script = '', |
718 | $nothingvalue=0,$return=false,$disabled=false,$tabindex=0) { |
719 | |
720 | if ($nothing == 'choose') { |
721 | $nothing = get_string('choose') .'...'; |
722 | } |
723 | |
724 | $attributes = ($script) ? 'onchange="'. $script .'"' : ''; |
725 | if ($disabled) { |
726 | $attributes .= ' disabled="disabled"'; |
727 | } |
728 | |
729 | if ($tabindex) { |
730 | $attributes .= ' tabindex="'.$tabindex.'"'; |
731 | } |
732 | |
733 | $output = '<select id="menu'.$name.'" name="'. $name .'" '. $attributes .'>' . "\n"; |
734 | if ($nothing) { |
735 | $output .= ' <option value="'. $nothingvalue .'"'. "\n"; |
736 | if ($nothingvalue === $selected) { |
737 | $output .= ' selected="selected"'; |
738 | } |
739 | $output .= '>'. $nothing .'</option>' . "\n"; |
740 | } |
741 | if (!empty($options)) { |
742 | foreach ($options as $section => $values) { |
dacb47c0 |
743 | |
744 | $output .= ' <optgroup label="'. s(format_string($section)) .'">'."\n"; |
14040797 |
745 | foreach ($values as $value => $label) { |
dacb47c0 |
746 | $output .= ' <option value="'. format_string($value) .'"'; |
f332bd02 |
747 | if ((string)$value == (string)$selected) { |
14040797 |
748 | $output .= ' selected="selected"'; |
749 | } |
750 | if ($label === '') { |
751 | $output .= '>'. $value .'</option>' . "\n"; |
752 | } else { |
753 | $output .= '>'. $label .'</option>' . "\n"; |
754 | } |
755 | } |
756 | $output .= ' </optgroup>'."\n"; |
757 | } |
758 | } |
759 | $output .= '</select>' . "\n"; |
760 | |
761 | if ($return) { |
762 | return $output; |
763 | } else { |
764 | echo $output; |
765 | } |
766 | } |
767 | |
768 | |
531a3cb2 |
769 | /** |
770 | * Given an array of values, creates a group of radio buttons to be part of a form |
c06c8492 |
771 | * |
531a3cb2 |
772 | * @param array $options An array of value-label pairs for the radio group (values as keys) |
773 | * @param string $name Name of the radiogroup (unique in the form) |
774 | * @param string $checked The value that is already checked |
775 | */ |
eaeaf964 |
776 | function choose_from_radio ($options, $name, $checked='', $return=false) { |
531a3cb2 |
777 | |
20cbef63 |
778 | static $idcounter = 0; |
779 | |
531a3cb2 |
780 | if (!$name) { |
781 | $name = 'unnamed'; |
782 | } |
783 | |
784 | $output = '<span class="radiogroup '.$name."\">\n"; |
785 | |
786 | if (!empty($options)) { |
787 | $currentradio = 0; |
788 | foreach ($options as $value => $label) { |
20cbef63 |
789 | $htmlid = 'auto-rb'.sprintf('%04d', ++$idcounter); |
790 | $output .= ' <span class="radioelement '.$name.' rb'.$currentradio."\">"; |
791 | $output .= '<input name="'.$name.'" id="'.$htmlid.'" type="radio" value="'.$value.'"'; |
531a3cb2 |
792 | if ($value == $checked) { |
793 | $output .= ' checked="checked"'; |
794 | } |
795 | if ($label === '') { |
20cbef63 |
796 | $output .= ' /> <label for="'.$htmlid.'">'. $value .'</label></span>' . "\n"; |
531a3cb2 |
797 | } else { |
20cbef63 |
798 | $output .= ' /> <label for="'.$htmlid.'">'. $label .'</label></span>' . "\n"; |
531a3cb2 |
799 | } |
800 | $currentradio = ($currentradio + 1) % 2; |
801 | } |
802 | } |
803 | |
804 | $output .= '</span>' . "\n"; |
805 | |
eaeaf964 |
806 | if ($return) { |
807 | return $output; |
808 | } else { |
809 | echo $output; |
810 | } |
531a3cb2 |
811 | } |
812 | |
2481037f |
813 | /** Display an standard html checkbox with an optional label |
814 | * |
815 | * @param string $name The name of the checkbox |
816 | * @param string $value The valus that the checkbox will pass when checked |
817 | * @param boolean $checked The flag to tell the checkbox initial state |
818 | * @param string $label The label to be showed near the checkbox |
819 | * @param string $alt The info to be inserted in the alt tag |
820 | */ |
d0d272e7 |
821 | function print_checkbox ($name, $value, $checked = true, $label = '', $alt = '', $script='',$return=false) { |
2481037f |
822 | |
20cbef63 |
823 | static $idcounter = 0; |
824 | |
2481037f |
825 | if (!$name) { |
826 | $name = 'unnamed'; |
827 | } |
828 | |
27f79fda |
829 | if ($alt) { |
830 | $alt = strip_tags($alt); |
831 | } else { |
2481037f |
832 | $alt = 'checkbox'; |
833 | } |
834 | |
835 | if ($checked) { |
836 | $strchecked = ' checked="checked"'; |
f89f924e |
837 | } else { |
838 | $strchecked = ''; |
2481037f |
839 | } |
840 | |
20cbef63 |
841 | $htmlid = 'auto-cb'.sprintf('%04d', ++$idcounter); |
2481037f |
842 | $output = '<span class="checkbox '.$name."\">"; |
fbe31d22 |
843 | $output .= '<input name="'.$name.'" id="'.$htmlid.'" type="checkbox" value="'.$value.'" alt="'.$alt.'"'.$strchecked.' '.((!empty($script)) ? ' onclick="'.$script.'" ' : '').' />'; |
20cbef63 |
844 | if(!empty($label)) { |
845 | $output .= ' <label for="'.$htmlid.'">'.$label.'</label>'; |
846 | } |
2481037f |
847 | $output .= '</span>'."\n"; |
848 | |
d0d272e7 |
849 | if (empty($return)) { |
850 | echo $output; |
851 | } else { |
852 | return $output; |
853 | } |
2481037f |
854 | |
855 | } |
856 | |
d0d272e7 |
857 | /** Display an standard html text field with an optional label |
858 | * |
859 | * @param string $name The name of the text field |
860 | * @param string $value The value of the text field |
861 | * @param string $label The label to be showed near the text field |
862 | * @param string $alt The info to be inserted in the alt tag |
863 | */ |
eaeaf964 |
864 | function print_textfield ($name, $value, $alt = '',$size=50,$maxlength=0, $return=false) { |
d0d272e7 |
865 | |
866 | static $idcounter = 0; |
867 | |
868 | if (empty($name)) { |
869 | $name = 'unnamed'; |
870 | } |
871 | |
872 | if (empty($alt)) { |
873 | $alt = 'textfield'; |
874 | } |
875 | |
876 | if (!empty($maxlength)) { |
877 | $maxlength = ' maxlength="'.$maxlength.'" '; |
878 | } |
879 | |
ce432524 |
880 | $htmlid = 'auto-tf'.sprintf('%04d', ++$idcounter); |
d0d272e7 |
881 | $output = '<span class="textfield '.$name."\">"; |
882 | $output .= '<input name="'.$name.'" id="'.$htmlid.'" type="text" value="'.$value.'" size="'.$size.'" '.$maxlength.' alt="'.$alt.'" />'; |
c06c8492 |
883 | |
d0d272e7 |
884 | $output .= '</span>'."\n"; |
885 | |
886 | if (empty($return)) { |
887 | echo $output; |
888 | } else { |
889 | return $output; |
890 | } |
891 | |
892 | } |
893 | |
894 | |
d48b00b4 |
895 | /** |
896 | * Implements a complete little popup form |
897 | * |
89dcb99d |
898 | * @uses $CFG |
899 | * @param string $common The URL up to the point of the variable that changes |
900 | * @param array $options Alist of value-label pairs for the popup list |
ee5567d1 |
901 | * @param string $formid Id must be unique on the page (originaly $formname) |
89dcb99d |
902 | * @param string $selected The option that is already selected |
903 | * @param string $nothing The label for the "no choice" option |
904 | * @param string $help The name of a help page if help is required |
905 | * @param string $helptext The name of the label for the help button |
906 | * @param boolean $return Indicates whether the function should return the text |
907 | * as a string or echo it directly to the page being rendered |
772e78be |
908 | * @param string $targetwindow The name of the target page to open the linked page in. |
89dcb99d |
909 | * @return string If $return is true then the entire form is returned as a string. |
910 | * @todo Finish documenting this function<br> |
89dcb99d |
911 | */ |
4609c6f0 |
912 | function popup_form($common, $options, $formid, $selected='', $nothing='choose', $help='', $helptext='', $return=false, $targetwindow='self', $selectlabel='') { |
89dcb99d |
913 | |
772e78be |
914 | global $CFG; |
b0542a1e |
915 | static $go, $choose; /// Locally cached, in case there's lots on a page |
87180677 |
916 | |
6f9f3b69 |
917 | if (empty($options)) { |
918 | return ''; |
919 | } |
0d0baabf |
920 | |
b0542a1e |
921 | if (!isset($go)) { |
922 | $go = get_string('go'); |
037dcbb6 |
923 | } |
924 | |
b0ccd3fb |
925 | if ($nothing == 'choose') { |
037dcbb6 |
926 | if (!isset($choose)) { |
927 | $choose = get_string('choose'); |
928 | } |
929 | $nothing = $choose.'...'; |
618b22c5 |
930 | } |
931 | |
9d3c6ee5 |
932 | // changed reference to document.getElementById('id_abc') instead of document.abc |
933 | // MDL-7861 |
4f24b3e3 |
934 | $output = '<form action="'.$CFG->wwwroot.'/course/jumpto.php"'. |
fa738731 |
935 | ' method="get" '. |
936 | $CFG->frametarget. |
ee5567d1 |
937 | ' id="'.$formid.'"'. |
c4d951e1 |
938 | ' class="popupform">'; |
4f24b3e3 |
939 | if ($help) { |
940 | $button = helpbutton($help, $helptext, 'moodle', true, false, '', true); |
941 | } else { |
942 | $button = ''; |
943 | } |
037dcbb6 |
944 | |
fcf9577a |
945 | if ($selectlabel) { |
946 | $selectlabel = '<label for="'.$formid.'_jump">'.$selectlabel.'</label>'; |
947 | } |
948 | |
60ec23af |
949 | $output .= '<div>'.$selectlabel.$button.'<select id="'.$formid.'_jump" name="jump" onchange="'.$targetwindow.'.location=document.getElementById(\''.$formid.'\').jump.options[document.getElementById(\''.$formid.'\').jump.selectedIndex].value;">'."\n"; |
f9903ed0 |
950 | |
b0ccd3fb |
951 | if ($nothing != '') { |
dfec7b01 |
952 | $output .= " <option value=\"javascript:void(0)\">$nothing</option>\n"; |
f9903ed0 |
953 | } |
954 | |
72b4e283 |
955 | $inoptgroup = false; |
89bfeee0 |
956 | |
f9903ed0 |
957 | foreach ($options as $value => $label) { |
772e78be |
958 | |
89bfeee0 |
959 | if ($label == '--') { /// we are ending previous optgroup |
960 | /// Check to see if we already have a valid open optgroup |
961 | /// XHTML demands that there be at least 1 option within an optgroup |
962 | if ($inoptgroup and (count($optgr) > 1) ) { |
963 | $output .= implode('', $optgr); |
964 | $output .= ' </optgroup>'; |
965 | } |
966 | $optgr = array(); |
967 | $inoptgroup = false; |
968 | continue; |
969 | } else if (substr($label,0,2) == '--') { /// we are starting a new optgroup |
772e78be |
970 | |
2a003a90 |
971 | /// Check to see if we already have a valid open optgroup |
972 | /// XHTML demands that there be at least 1 option within an optgroup |
973 | if ($inoptgroup and (count($optgr) > 1) ) { |
974 | $output .= implode('', $optgr); |
72b4e283 |
975 | $output .= ' </optgroup>'; |
72b4e283 |
976 | } |
2a003a90 |
977 | |
978 | unset($optgr); |
979 | $optgr = array(); |
980 | |
dacb47c0 |
981 | $optgr[] = ' <optgroup label="'. s(format_string(substr($label,2))) .'">'; // Plain labels |
772e78be |
982 | |
2a003a90 |
983 | $inoptgroup = true; /// everything following will be in an optgroup |
3326450b |
984 | continue; |
772e78be |
985 | |
d897cae4 |
986 | } else { |
fd78420b |
987 | if (!empty($CFG->usesid) && !isset($_COOKIE[session_name()])) |
988 | { |
989 | $url=sid_process_url( $common . $value ); |
990 | } else |
991 | { |
992 | $url=$common . $value; |
993 | } |
994 | $optstr = ' <option value="' . $url . '"'; |
772e78be |
995 | |
d897cae4 |
996 | if ($value == $selected) { |
2a003a90 |
997 | $optstr .= ' selected="selected"'; |
998 | } |
772e78be |
999 | |
2a003a90 |
1000 | if ($label) { |
1001 | $optstr .= '>'. $label .'</option>' . "\n"; |
1002 | } else { |
1003 | $optstr .= '>'. $value .'</option>' . "\n"; |
1004 | } |
772e78be |
1005 | |
2a003a90 |
1006 | if ($inoptgroup) { |
1007 | $optgr[] = $optstr; |
1008 | } else { |
1009 | $output .= $optstr; |
d897cae4 |
1010 | } |
f9903ed0 |
1011 | } |
772e78be |
1012 | |
f9903ed0 |
1013 | } |
2a003a90 |
1014 | |
1015 | /// catch the final group if not closed |
1016 | if ($inoptgroup and count($optgr) > 1) { |
1017 | $output .= implode('', $optgr); |
72b4e283 |
1018 | $output .= ' </optgroup>'; |
1019 | } |
2a003a90 |
1020 | |
b0ccd3fb |
1021 | $output .= '</select>'; |
3435f39b |
1022 | $output .= '<input type="hidden" name="sesskey" value="'.sesskey().'" />'; |
ee5567d1 |
1023 | $output .= '<div id="noscript'.$formid.'" style="display: inline;">'; |
929089e7 |
1024 | $output .= '<input type="submit" value="'.$go.'" /></div>'; |
037dcbb6 |
1025 | $output .= '<script type="text/javascript">'. |
ce926cb8 |
1026 | "\n//<![CDATA[\n". |
ee5567d1 |
1027 | 'document.getElementById("noscript'.$formid.'").style.display = "none";'. |
ce926cb8 |
1028 | "\n//]]>\n".'</script>'; |
60ec23af |
1029 | $output .= '</div>'; |
3d621607 |
1030 | $output .= '</form>'; |
d897cae4 |
1031 | |
1032 | if ($return) { |
4f24b3e3 |
1033 | return $output; |
d897cae4 |
1034 | } else { |
4f24b3e3 |
1035 | echo $output; |
d897cae4 |
1036 | } |
f9903ed0 |
1037 | } |
1038 | |
1039 | |
d48b00b4 |
1040 | /** |
1041 | * Prints some red text |
1042 | * |
1043 | * @param string $error The text to be displayed in red |
1044 | */ |
f9903ed0 |
1045 | function formerr($error) { |
d48b00b4 |
1046 | |
f9903ed0 |
1047 | if (!empty($error)) { |
7bc1c17f |
1048 | echo '<span class="error">'. $error .'</span>'; |
f9903ed0 |
1049 | } |
1050 | } |
1051 | |
d48b00b4 |
1052 | /** |
1053 | * Validates an email to make sure it makes sense. |
1054 | * |
1055 | * @param string $address The email address to validate. |
1056 | * @return boolean |
1057 | */ |
89dcb99d |
1058 | function validate_email($address) { |
d48b00b4 |
1059 | |
78fbaeae |
1060 | return (ereg('^[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+'. |
1061 | '(\.[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+)*'. |
f9903ed0 |
1062 | '@'. |
1063 | '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'. |
1064 | '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', |
1065 | $address)); |
1066 | } |
1067 | |
690f358b |
1068 | /** |
1069 | * Extracts file argument either from file parameter or PATH_INFO |
1070 | * |
1071 | * @param string $scriptname name of the calling script |
1072 | * @return string file path (only safe characters) |
1073 | */ |
1074 | function get_file_argument($scriptname) { |
1075 | global $_SERVER; |
1076 | |
1077 | $relativepath = FALSE; |
1078 | |
1079 | // first try normal parameter (compatible method == no relative links!) |
1080 | $relativepath = optional_param('file', FALSE, PARAM_PATH); |
48283ff6 |
1081 | if ($relativepath === '/testslasharguments') { |
9bbb40d6 |
1082 | echo 'test -1 : Incorrect use - try "file.php/testslasharguments" instead'; //indicate fopen/fread works for health center |
48283ff6 |
1083 | die; |
1084 | } |
690f358b |
1085 | |
1086 | // then try extract file from PATH_INFO (slasharguments method) |
1087 | if (!$relativepath and !empty($_SERVER['PATH_INFO'])) { |
1088 | $path_info = $_SERVER['PATH_INFO']; |
1089 | // check that PATH_INFO works == must not contain the script name |
1090 | if (!strpos($path_info, $scriptname)) { |
1091 | $relativepath = clean_param(rawurldecode($path_info), PARAM_PATH); |
48283ff6 |
1092 | if ($relativepath === '/testslasharguments') { |
9bbb40d6 |
1093 | echo 'test 1 : Slasharguments test passed. Server confguration is compatible with file.php/1/pic.jpg slashargument setting.'; //indicate ok for health center |
690f358b |
1094 | die; |
1095 | } |
1096 | } |
1097 | } |
1098 | |
1099 | // now if both fail try the old way |
1100 | // (for compatibility with misconfigured or older buggy php implementations) |
1101 | if (!$relativepath) { |
1102 | $arr = explode($scriptname, me()); |
1103 | if (!empty($arr[1])) { |
1104 | $path_info = strip_querystring($arr[1]); |
1105 | $relativepath = clean_param(rawurldecode($path_info), PARAM_PATH); |
48283ff6 |
1106 | if ($relativepath === '/testslasharguments') { |
9bbb40d6 |
1107 | echo 'test 2 : Slasharguments test passed (compatibility hack). Server confguration may be compatible with file.php/1/pic.jpg slashargument setting'; //indicate ok for health center |
690f358b |
1108 | die; |
1109 | } |
1110 | } |
1111 | } |
1112 | |
1113 | return $relativepath; |
1114 | } |
1115 | |
d48b00b4 |
1116 | /** |
1117 | * Searches the current environment variables for some slash arguments |
1118 | * |
1119 | * @param string $file ? |
1120 | * @todo Finish documenting this function |
1121 | */ |
b0ccd3fb |
1122 | function get_slash_arguments($file='file.php') { |
f9903ed0 |
1123 | |
eaa50dbc |
1124 | if (!$string = me()) { |
f9903ed0 |
1125 | return false; |
1126 | } |
eaa50dbc |
1127 | |
6ed3da1d |
1128 | $pathinfo = explode($file, $string); |
ab9f24ad |
1129 | |
bcdfe14e |
1130 | if (!empty($pathinfo[1])) { |
9b74055f |
1131 | return addslashes($pathinfo[1]); |
6ed3da1d |
1132 | } else { |
1133 | return false; |
1134 | } |
1135 | } |
1136 | |
d48b00b4 |
1137 | /** |
1138 | * Extracts arguments from "/foo/bar/something" |
1139 | * eg http://mysite.com/script.php/foo/bar/something |
1140 | * |
89dcb99d |
1141 | * @param string $string ? |
1142 | * @param int $i ? |
1143 | * @return array|string |
d48b00b4 |
1144 | * @todo Finish documenting this function |
1145 | */ |
6ed3da1d |
1146 | function parse_slash_arguments($string, $i=0) { |
f9903ed0 |
1147 | |
6c8e8b5e |
1148 | if (detect_munged_arguments($string)) { |
780db230 |
1149 | return false; |
1150 | } |
b0ccd3fb |
1151 | $args = explode('/', $string); |
f9903ed0 |
1152 | |
1153 | if ($i) { // return just the required argument |
1154 | return $args[$i]; |
1155 | |
1156 | } else { // return the whole array |
1157 | array_shift($args); // get rid of the empty first one |
1158 | return $args; |
1159 | } |
1160 | } |
1161 | |
d48b00b4 |
1162 | /** |
89dcb99d |
1163 | * Just returns an array of text formats suitable for a popup menu |
d48b00b4 |
1164 | * |
89dcb99d |
1165 | * @uses FORMAT_MOODLE |
1166 | * @uses FORMAT_HTML |
1167 | * @uses FORMAT_PLAIN |
89dcb99d |
1168 | * @uses FORMAT_MARKDOWN |
1169 | * @return array |
d48b00b4 |
1170 | */ |
0095d5cd |
1171 | function format_text_menu() { |
d48b00b4 |
1172 | |
b0ccd3fb |
1173 | return array (FORMAT_MOODLE => get_string('formattext'), |
1174 | FORMAT_HTML => get_string('formathtml'), |
1175 | FORMAT_PLAIN => get_string('formatplain'), |
b0ccd3fb |
1176 | FORMAT_MARKDOWN => get_string('formatmarkdown')); |
0095d5cd |
1177 | } |
1178 | |
d48b00b4 |
1179 | /** |
1180 | * Given text in a variety of format codings, this function returns |
772e78be |
1181 | * the text as safe HTML. |
d48b00b4 |
1182 | * |
1183 | * @uses $CFG |
89dcb99d |
1184 | * @uses FORMAT_MOODLE |
1185 | * @uses FORMAT_HTML |
1186 | * @uses FORMAT_PLAIN |
1187 | * @uses FORMAT_WIKI |
1188 | * @uses FORMAT_MARKDOWN |
1189 | * @param string $text The text to be formatted. This is raw text originally from user input. |
772e78be |
1190 | * @param int $format Identifier of the text format to be used |
89dcb99d |
1191 | * (FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN, FORMAT_WIKI, FORMAT_MARKDOWN) |
1192 | * @param array $options ? |
1193 | * @param int $courseid ? |
1194 | * @return string |
d48b00b4 |
1195 | * @todo Finish documenting this function |
1196 | */ |
7d8a3cb0 |
1197 | function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL) { |
0095d5cd |
1198 | |
dcf6d93c |
1199 | global $CFG, $COURSE; |
c4ae4fa1 |
1200 | |
d53ca6ad |
1201 | if ($text === '') { |
1202 | return ''; // no need to do any filters and cleaning |
1203 | } |
1204 | |
7d8a3cb0 |
1205 | if (!isset($options->trusttext)) { |
1206 | $options->trusttext = false; |
1207 | } |
1208 | |
e7a47153 |
1209 | if (!isset($options->noclean)) { |
1210 | $options->noclean=false; |
1211 | } |
a17c57b5 |
1212 | if (!isset($options->nocache)) { |
1213 | $options->nocache=false; |
1214 | } |
e7a47153 |
1215 | if (!isset($options->smiley)) { |
1216 | $options->smiley=true; |
1217 | } |
1218 | if (!isset($options->filter)) { |
1219 | $options->filter=true; |
1220 | } |
1221 | if (!isset($options->para)) { |
1222 | $options->para=true; |
1223 | } |
1224 | if (!isset($options->newlines)) { |
1225 | $options->newlines=true; |
f0aa2fed |
1226 | } |
1227 | |
c4ae4fa1 |
1228 | if (empty($courseid)) { |
dcf6d93c |
1229 | $courseid = $COURSE->id; |
c4ae4fa1 |
1230 | } |
a751a4e5 |
1231 | |
a17c57b5 |
1232 | if (!empty($CFG->cachetext) and empty($options->nocache)) { |
e7a47153 |
1233 | $time = time() - $CFG->cachetext; |
90bbe689 |
1234 | $md5key = md5($text.'-'.(int)$courseid.'-'.current_language().'-'.(int)$format.(int)$options->trusttext.(int)$options->noclean.(int)$options->smiley.(int)$options->filter.(int)$options->para.(int)$options->newlines); |
a9743837 |
1235 | if ($oldcacheitem = get_record_sql('SELECT * FROM '.$CFG->prefix.'cache_text WHERE md5key = \''.$md5key.'\'', true)) { |
1236 | if ($oldcacheitem->timemodified >= $time) { |
1237 | return $oldcacheitem->formattedtext; |
1238 | } |
e7a47153 |
1239 | } |
1240 | } |
1241 | |
7d8a3cb0 |
1242 | // trusttext overrides the noclean option! |
1243 | if ($options->trusttext) { |
1244 | if (trusttext_present($text)) { |
1245 | $text = trusttext_strip($text); |
1246 | if (!empty($CFG->enabletrusttext)) { |
1247 | $options->noclean = true; |
1248 | } else { |
1249 | $options->noclean = false; |
1250 | } |
1251 | } else { |
1252 | $options->noclean = false; |
1253 | } |
d53ca6ad |
1254 | } else if (!debugging('', DEBUG_DEVELOPER)) { |
1255 | // strip any forgotten trusttext in non-developer mode |
1256 | // do not forget to disable text cache when debugging trusttext!! |
1257 | $text = trusttext_strip($text); |
7d8a3cb0 |
1258 | } |
1259 | |
8eaa4c61 |
1260 | $CFG->currenttextiscacheable = true; // Default status - can be changed by any filter |
c06c8492 |
1261 | |
0095d5cd |
1262 | switch ($format) { |
73f8658c |
1263 | case FORMAT_HTML: |
7d8a3cb0 |
1264 | if ($options->smiley) { |
e7a47153 |
1265 | replace_smilies($text); |
1266 | } |
7d8a3cb0 |
1267 | if (!$options->noclean) { |
5c6347ce |
1268 | $text = clean_text($text, FORMAT_HTML); |
9d40806d |
1269 | } |
7d8a3cb0 |
1270 | if ($options->filter) { |
e7a47153 |
1271 | $text = filter_text($text, $courseid); |
1272 | } |
73f8658c |
1273 | break; |
1274 | |
6901fa79 |
1275 | case FORMAT_PLAIN: |
5c6347ce |
1276 | $text = s($text); // cleans dangerous JS |
ab892a4f |
1277 | $text = rebuildnolinktag($text); |
b0ccd3fb |
1278 | $text = str_replace(' ', ' ', $text); |
6901fa79 |
1279 | $text = nl2br($text); |
6901fa79 |
1280 | break; |
1281 | |
d342c763 |
1282 | case FORMAT_WIKI: |
6a6495ff |
1283 | // this format is deprecated |
572fe9ab |
1284 | $text = '<p>NOTICE: Wiki-like formatting has been removed from Moodle. You should not be seeing |
1285 | this message as all texts should have been converted to Markdown format instead. |
ce50cc70 |
1286 | Please post a bug report to http://moodle.org/bugs with information about where you |
e7a47153 |
1287 | saw this message.</p>'.s($text); |
d342c763 |
1288 | break; |
1289 | |
e7cdcd18 |
1290 | case FORMAT_MARKDOWN: |
1291 | $text = markdown_to_html($text); |
7d8a3cb0 |
1292 | if ($options->smiley) { |
e7a47153 |
1293 | replace_smilies($text); |
1294 | } |
7d8a3cb0 |
1295 | if (!$options->noclean) { |
5c6347ce |
1296 | $text = clean_text($text, FORMAT_HTML); |
9d40806d |
1297 | } |
c06c8492 |
1298 | |
7d8a3cb0 |
1299 | if ($options->filter) { |
e7a47153 |
1300 | $text = filter_text($text, $courseid); |
1301 | } |
e7cdcd18 |
1302 | break; |
1303 | |
73f8658c |
1304 | default: // FORMAT_MOODLE or anything else |
b7a3d3b2 |
1305 | $text = text_to_html($text, $options->smiley, $options->para, $options->newlines); |
7d8a3cb0 |
1306 | if (!$options->noclean) { |
5c6347ce |
1307 | $text = clean_text($text, FORMAT_HTML); |
9d40806d |
1308 | } |
c06c8492 |
1309 | |
7d8a3cb0 |
1310 | if ($options->filter) { |
e7a47153 |
1311 | $text = filter_text($text, $courseid); |
1312 | } |
0095d5cd |
1313 | break; |
0095d5cd |
1314 | } |
f0aa2fed |
1315 | |
f47f4659 |
1316 | if (empty($options->nocache) and !empty($CFG->cachetext) and $CFG->currenttextiscacheable) { |
1317 | $newcacheitem = new object(); |
a9743837 |
1318 | $newcacheitem->md5key = $md5key; |
1319 | $newcacheitem->formattedtext = addslashes($text); |
1320 | $newcacheitem->timemodified = time(); |
1321 | if ($oldcacheitem) { // See bug 4677 for discussion |
1322 | $newcacheitem->id = $oldcacheitem->id; |
1323 | @update_record('cache_text', $newcacheitem); // Update existing record in the cache table |
5ce73257 |
1324 | // It's unlikely that the cron cache cleaner could have |
a9743837 |
1325 | // deleted this entry in the meantime, as it allows |
1326 | // some extra time to cover these cases. |
1327 | } else { |
1328 | @insert_record('cache_text', $newcacheitem); // Insert a new record in the cache table |
1329 | // Again, it's possible that another user has caused this |
5ce73257 |
1330 | // record to be created already in the time that it took |
1331 | // to traverse this function. That's OK too, as the |
a9743837 |
1332 | // call above handles duplicate entries, and eventually |
1333 | // the cron cleaner will delete them. |
1334 | } |
f0aa2fed |
1335 | } |
1336 | |
1337 | return $text; |
0095d5cd |
1338 | } |
1339 | |
4a28b5ca |
1340 | /** Converts the text format from the value to the 'internal' |
1341 | * name or vice versa. $key can either be the value or the name |
1342 | * and you get the other back. |
c06c8492 |
1343 | * |
4a28b5ca |
1344 | * @param mixed int 0-4 or string one of 'moodle','html','plain','markdown' |
1345 | * @return mixed as above but the other way around! |
1346 | */ |
1347 | function text_format_name( $key ) { |
1348 | $lookup = array(); |
1349 | $lookup[FORMAT_MOODLE] = 'moodle'; |
1350 | $lookup[FORMAT_HTML] = 'html'; |
1351 | $lookup[FORMAT_PLAIN] = 'plain'; |
1352 | $lookup[FORMAT_MARKDOWN] = 'markdown'; |
1353 | $value = "error"; |
1354 | if (!is_numeric($key)) { |
1355 | $key = strtolower( $key ); |
1356 | $value = array_search( $key, $lookup ); |
1357 | } |
1358 | else { |
1359 | if (isset( $lookup[$key] )) { |
1360 | $value = $lookup[ $key ]; |
1361 | } |
1362 | } |
1363 | return $value; |
1364 | } |
1365 | |
473d29eb |
1366 | |
7b2c5e72 |
1367 | /** Given a simple string, this function returns the string |
1368 | * processed by enabled filters if $CFG->filterall is enabled |
1369 | * |
3e6691ee |
1370 | * @param string $string The string to be filtered. |
fae83c8d |
1371 | * @param boolean $striplinks To strip any link in the result text (Moodle 1.8 default changed from false to true! MDL-8713) |
3e6691ee |
1372 | * @param int $courseid Current course as filters can, potentially, use it |
7b2c5e72 |
1373 | * @return string |
1374 | */ |
fae83c8d |
1375 | function format_string ($string, $striplinks=true, $courseid=NULL ) { |
7b2c5e72 |
1376 | |
65b0c132 |
1377 | global $CFG, $COURSE; |
38701b69 |
1378 | |
2a3affe9 |
1379 | //We'll use a in-memory cache here to speed up repeated strings |
473d29eb |
1380 | static $strcache = false; |
1381 | |
38701b69 |
1382 | if ($strcache === false or count($strcache) > 2000 ) { // this number might need some tuning to limit memory usage in cron |
473d29eb |
1383 | $strcache = array(); |
1384 | } |
268ddd50 |
1385 | |
38701b69 |
1386 | //init course id |
a97e0ccb |
1387 | if (empty($courseid)) { |
38701b69 |
1388 | $courseid = $COURSE->id; |
1389 | } |
1390 | |
2a3affe9 |
1391 | //Calculate md5 |
38701b69 |
1392 | $md5 = md5($string.'<+>'.$striplinks.'<+>'.$courseid.'<+>'.current_language()); |
2a3affe9 |
1393 | |
1394 | //Fetch from cache if possible |
38701b69 |
1395 | if (isset($strcache[$md5])) { |
2a3affe9 |
1396 | return $strcache[$md5]; |
1397 | } |
1398 | |
dacb47c0 |
1399 | // First replace all ampersands not followed by html entity code |
cb6a4f09 |
1400 | $string = preg_replace("/\&(?![a-zA-Z0-9#]{1,8};)/", "&", $string); |
268ddd50 |
1401 | |
45daee10 |
1402 | if (!empty($CFG->filterall)) { |
037da74d |
1403 | $string = filter_string($string, $courseid); |
7b2c5e72 |
1404 | } |
268ddd50 |
1405 | |
9fbed9c9 |
1406 | // If the site requires it, strip ALL tags from this string |
1407 | if (!empty($CFG->formatstringstriptags)) { |
1408 | $string = strip_tags($string); |
1409 | |
1410 | // Otherwise strip just links if that is required (default) |
1411 | } else if ($striplinks) { //strip links in string |
3e6691ee |
1412 | $string = preg_replace('/(<a[^>]+?>)(.+?)(<\/a>)/is','$2',$string); |
1413 | } |
1414 | |
2a3affe9 |
1415 | //Store to cache |
1416 | $strcache[$md5] = $string; |
c3d327b4 |
1417 | |
7b2c5e72 |
1418 | return $string; |
1419 | } |
1420 | |
d48b00b4 |
1421 | /** |
1422 | * Given text in a variety of format codings, this function returns |
1423 | * the text as plain text suitable for plain email. |
d48b00b4 |
1424 | * |
89dcb99d |
1425 | * @uses FORMAT_MOODLE |
1426 | * @uses FORMAT_HTML |
1427 | * @uses FORMAT_PLAIN |
1428 | * @uses FORMAT_WIKI |
1429 | * @uses FORMAT_MARKDOWN |
1430 | * @param string $text The text to be formatted. This is raw text originally from user input. |
772e78be |
1431 | * @param int $format Identifier of the text format to be used |
89dcb99d |
1432 | * (FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN, FORMAT_WIKI, FORMAT_MARKDOWN) |
1433 | * @return string |
d48b00b4 |
1434 | */ |
d342c763 |
1435 | function format_text_email($text, $format) { |
d342c763 |
1436 | |
1437 | switch ($format) { |
1438 | |
1439 | case FORMAT_PLAIN: |
1440 | return $text; |
1441 | break; |
1442 | |
1443 | case FORMAT_WIKI: |
1444 | $text = wiki_to_html($text); |
5b472756 |
1445 | /// This expression turns links into something nice in a text format. (Russell Jungwirth) |
1446 | /// From: http://php.net/manual/en/function.eregi-replace.php and simplified |
76add072 |
1447 | $text = eregi_replace('(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)','\\3 [ \\2 ]', $text); |
7c55a29b |
1448 | return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES))); |
d342c763 |
1449 | break; |
1450 | |
6ff45b59 |
1451 | case FORMAT_HTML: |
1452 | return html_to_text($text); |
1453 | break; |
1454 | |
e7cdcd18 |
1455 | case FORMAT_MOODLE: |
1456 | case FORMAT_MARKDOWN: |
67ccec43 |
1457 | default: |
76add072 |
1458 | $text = eregi_replace('(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)','\\3 [ \\2 ]', $text); |
7c55a29b |
1459 | return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES))); |
d342c763 |
1460 | break; |
1461 | } |
1462 | } |
0095d5cd |
1463 | |
d48b00b4 |
1464 | /** |
1465 | * Given some text in HTML format, this function will pass it |
1466 | * through any filters that have been defined in $CFG->textfilterx |
1467 | * The variable defines a filepath to a file containing the |
1468 | * filter function. The file must contain a variable called |
1469 | * $textfilter_function which contains the name of the function |
1470 | * with $courseid and $text parameters |
1471 | * |
89dcb99d |
1472 | * @param string $text The text to be passed through format filters |
1473 | * @param int $courseid ? |
d48b00b4 |
1474 | * @return string |
1475 | * @todo Finish documenting this function |
1476 | */ |
c4ae4fa1 |
1477 | function filter_text($text, $courseid=NULL) { |
dcf6d93c |
1478 | global $CFG, $COURSE; |
1479 | |
1480 | if (empty($courseid)) { |
1481 | $courseid = $COURSE->id; // (copied from format_text) |
1482 | } |
e67b9e31 |
1483 | |
d523d2ea |
1484 | if (!empty($CFG->textfilters)) { |
cbdfb929 |
1485 | require_once($CFG->libdir.'/filterlib.php'); |
d523d2ea |
1486 | $textfilters = explode(',', $CFG->textfilters); |
1487 | foreach ($textfilters as $textfilter) { |
b0ccd3fb |
1488 | if (is_readable($CFG->dirroot .'/'. $textfilter .'/filter.php')) { |
1489 | include_once($CFG->dirroot .'/'. $textfilter .'/filter.php'); |
df1c4611 |
1490 | $functionname = basename($textfilter).'_filter'; |
1491 | if (function_exists($functionname)) { |
1492 | $text = $functionname($courseid, $text); |
1493 | } |
d523d2ea |
1494 | } |
e67b9e31 |
1495 | } |
1496 | } |
d523d2ea |
1497 | |
c7444a36 |
1498 | /// <nolink> tags removed for XHTML compatibility |
1499 | $text = str_replace('<nolink>', '', $text); |
1500 | $text = str_replace('</nolink>', '', $text); |
1501 | |
e67b9e31 |
1502 | return $text; |
1503 | } |
1504 | |
cbdfb929 |
1505 | |
1506 | /** |
1507 | * Given a string (short text) in HTML format, this function will pass it |
1508 | * through any filters that have been defined in $CFG->stringfilters |
1509 | * The variable defines a filepath to a file containing the |
1510 | * filter function. The file must contain a variable called |
1511 | * $textfilter_function which contains the name of the function |
1512 | * with $courseid and $text parameters |
1513 | * |
1514 | * @param string $string The text to be passed through format filters |
1515 | * @param int $courseid The id of a course |
1516 | * @return string |
1517 | */ |
1518 | function filter_string($string, $courseid=NULL) { |
1519 | global $CFG, $COURSE; |
1520 | |
1521 | if (empty($CFG->textfilters)) { // All filters are disabled anyway so quit |
1522 | return $string; |
1523 | } |
1524 | |
1525 | if (empty($courseid)) { |
1526 | $courseid = $COURSE->id; |
1527 | } |
1528 | |
1529 | require_once($CFG->libdir.'/filterlib.php'); |
1530 | |
1531 | if (isset($CFG->stringfilters)) { // We have a predefined list to use, great! |
1532 | if (empty($CFG->stringfilters)) { // but it's blank, so finish now |
1533 | return $string; |
1534 | } |
1535 | $stringfilters = explode(',', $CFG->stringfilters); // ..use the list we have |
1536 | |
1537 | } else { // Otherwise try to derive a list from textfilters |
9fbed9c9 |
1538 | if (strpos($CFG->textfilters, 'filter/multilang') !== false) { // Multilang is here |
cbdfb929 |
1539 | $stringfilters = array('filter/multilang'); // Let's use just that |
1540 | $CFG->stringfilters = 'filter/multilang'; // Save it for next time through |
9fbed9c9 |
1541 | } else { |
1542 | $CFG->stringfilters = ''; // Save the result and return |
1543 | return $string; |
cbdfb929 |
1544 | } |
1545 | } |
1546 | |
9fbed9c9 |
1547 | |
cbdfb929 |
1548 | foreach ($stringfilters as $stringfilter) { |
1549 | if (is_readable($CFG->dirroot .'/'. $stringfilter .'/filter.php')) { |
1550 | include_once($CFG->dirroot .'/'. $stringfilter .'/filter.php'); |
1551 | $functionname = basename($stringfilter).'_filter'; |
1552 | if (function_exists($functionname)) { |
1553 | $string = $functionname($courseid, $string); |
1554 | } |
1555 | } |
1556 | } |
1557 | |
1558 | /// <nolink> tags removed for XHTML compatibility |
1559 | $string = str_replace('<nolink>', '', $string); |
1560 | $string = str_replace('</nolink>', '', $string); |
1561 | |
1562 | return $string; |
1563 | } |
1564 | |
7d8a3cb0 |
1565 | /** |
1566 | * Is the text marked as trusted? |
1567 | * |
1568 | * @param string $text text to be searched for TRUSTTEXT marker |
1569 | * @return boolean |
1570 | */ |
1571 | function trusttext_present($text) { |
1572 | if (strpos($text, TRUSTTEXT) !== FALSE) { |
1573 | return true; |
1574 | } else { |
1575 | return false; |
1576 | } |
1577 | } |
1578 | |
1579 | /** |
1580 | * This funtion MUST be called before the cleaning or any other |
1581 | * function that modifies the data! We do not know the origin of trusttext |
1582 | * in database, if it gets there in tweaked form we must not convert it |
1583 | * to supported form!!! |
1584 | * |
1585 | * Please be carefull not to use stripslashes on data from database |
1586 | * or twice stripslashes when processing data recieved from user. |
1587 | * |
5ce73257 |
1588 | * @param string $text text that may contain TRUSTTEXT marker |
7d8a3cb0 |
1589 | * @return text without any TRUSTTEXT marker |
1590 | */ |
1591 | function trusttext_strip($text) { |
1592 | global $CFG; |
1593 | |
1594 | while (true) { //removing nested TRUSTTEXT |
5ce73257 |
1595 | $orig = $text; |
7d8a3cb0 |
1596 | $text = str_replace(TRUSTTEXT, '', $text); |
1597 | if (strcmp($orig, $text) === 0) { |
1598 | return $text; |
1599 | } |
1600 | } |
1601 | } |
1602 | |
1603 | /** |
1604 | * Mark text as trusted, such text may contain any HTML tags because the |
1605 | * normal text cleaning will be bypassed. |
1606 | * Please make sure that the text comes from trusted user before storing |
1607 | * it into database! |
1608 | */ |
1609 | function trusttext_mark($text) { |
1610 | global $CFG; |
1611 | if (!empty($CFG->enabletrusttext) and (strpos($text, TRUSTTEXT) === FALSE)) { |
1612 | return TRUSTTEXT.$text; |
1613 | } else { |
1614 | return $text; |
1615 | } |
1616 | } |
1617 | function trusttext_after_edit(&$text, $context) { |
1618 | if (has_capability('moodle/site:trustcontent', $context)) { |
daed1a2f |
1619 | $text = trusttext_strip($text); |
7d8a3cb0 |
1620 | $text = trusttext_mark($text); |
1621 | } else { |
1622 | $text = trusttext_strip($text); |
1623 | } |
1624 | } |
1625 | |
1626 | function trusttext_prepare_edit(&$text, &$format, $usehtmleditor, $context) { |
1627 | global $CFG; |
1628 | |
1629 | $options = new object(); |
1630 | $options->smiley = false; |
1631 | $options->filter = false; |
1632 | if (!empty($CFG->enabletrusttext) |
1633 | and has_capability('moodle/site:trustcontent', $context) |
1634 | and trusttext_present($text)) { |
1635 | $options->noclean = true; |
1636 | } else { |
1637 | $options->noclean = false; |
1638 | } |
1639 | $text = trusttext_strip($text); |
1640 | if ($usehtmleditor) { |
1641 | $text = format_text($text, $format, $options); |
1642 | $format = FORMAT_HTML; |
1643 | } else if (!$options->noclean){ |
1644 | $text = clean_text($text, $format); |
1645 | } |
1646 | } |
1647 | |
d48b00b4 |
1648 | /** |
1649 | * Given raw text (eg typed in by a user), this function cleans it up |
1650 | * and removes any nasty tags that could mess up Moodle pages. |
1651 | * |
89dcb99d |
1652 | * @uses FORMAT_MOODLE |
1653 | * @uses FORMAT_PLAIN |
1654 | * @uses ALLOWED_TAGS |
1655 | * @param string $text The text to be cleaned |
772e78be |
1656 | * @param int $format Identifier of the text format to be used |
89dcb99d |
1657 | * (FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN, FORMAT_WIKI, FORMAT_MARKDOWN) |
1658 | * @return string The cleaned up text |
d48b00b4 |
1659 | */ |
3da47524 |
1660 | function clean_text($text, $format=FORMAT_MOODLE) { |
b7a3cf49 |
1661 | |
85fbf884 |
1662 | global $ALLOWED_TAGS, $CFG; |
1663 | |
e0ac8448 |
1664 | if (empty($text) or is_numeric($text)) { |
1665 | return (string)$text; |
1666 | } |
3fe3851d |
1667 | |
ab9f24ad |
1668 | switch ($format) { |
e7cdcd18 |
1669 | case FORMAT_PLAIN: |
8f660562 |
1670 | case FORMAT_MARKDOWN: |
e7cdcd18 |
1671 | return $text; |
1672 | |
1673 | default: |
1674 | |
e0ac8448 |
1675 | if (!empty($CFG->enablehtmlpurifier)) { |
1676 | $text = purify_html($text); |
1677 | } else { |
1678 | /// Fix non standard entity notations |
1679 | $text = preg_replace('/(&#[0-9]+)(;?)/', "\\1;", $text); |
1680 | $text = preg_replace('/(&#x[0-9a-fA-F]+)(;?)/', "\\1;", $text); |
1681 | |
1682 | /// Remove tags that are not allowed |
1683 | $text = strip_tags($text, $ALLOWED_TAGS); |
1684 | |
1685 | /// Clean up embedded scripts and , using kses |
1686 | $text = cleanAttributes($text); |
1687 | } |
7789ffbf |
1688 | |
e0ac8448 |
1689 | /// Remove potential script events - some extra protection for undiscovered bugs in our code |
ab9f24ad |
1690 | $text = eregi_replace("([^a-z])language([[:space:]]*)=", "\\1Xlanguage=", $text); |
1691 | $text = eregi_replace("([^a-z])on([a-z]+)([[:space:]]*)=", "\\1Xon\\2=", $text); |
6901fa79 |
1692 | |
6901fa79 |
1693 | return $text; |
0095d5cd |
1694 | } |
b7a3cf49 |
1695 | } |
f9903ed0 |
1696 | |
e0ac8448 |
1697 | /** |
1698 | * KSES replacement cleaning function - uses HTML Purifier. |
1699 | */ |
1700 | function purify_html($text) { |
1701 | global $CFG; |
1702 | |
1703 | static $purifier = false; |
1704 | if (!$purifier) { |
1705 | require_once $CFG->libdir.'/htmlpurifier/HTMLPurifier.auto.php'; |
1706 | $config = HTMLPurifier_Config::createDefault(); |
1707 | $config->set('Core', 'AcceptFullDocuments', false); |
1708 | //$config->set('HTML', 'Strict', true); |
1709 | $config->set('URI', 'AllowedSchemes', array('http'=>1, 'https'=>1, 'ftp'=>1, 'irc'=>1, 'nntp'=>1, 'news'=>1, 'rtsp'=>1, 'teamspeak'=>1, 'gopher'=>1, 'mms'=>1)); |
1710 | $purifier = new HTMLPurifier($config); |
1711 | } |
1712 | return $purifier->purify($text); |
1713 | } |
1714 | |
d48b00b4 |
1715 | /** |
89dcb99d |
1716 | * This function takes a string and examines it for HTML tags. |
d48b00b4 |
1717 | * If tags are detected it passes the string to a helper function {@link cleanAttributes2()} |
1718 | * which checks for attributes and filters them for malicious content |
1719 | * 17/08/2004 :: Eamon DOT Costello AT dcu DOT ie |
1720 | * |
1721 | * @param string $str The string to be examined for html tags |
1722 | * @return string |
1723 | */ |
3bd7ffec |
1724 | function cleanAttributes($str){ |
4e8f2e6b |
1725 | $result = preg_replace_callback( |
1726 | '%(<[^>]*(>|$)|>)%m', #search for html tags |
1727 | "cleanAttributes2", |
3bd7ffec |
1728 | $str |
67ccec43 |
1729 | ); |
3bd7ffec |
1730 | return $result; |
67ccec43 |
1731 | } |
1732 | |
d48b00b4 |
1733 | /** |
1734 | * This function takes a string with an html tag and strips out any unallowed |
1735 | * protocols e.g. javascript: |
1736 | * It calls ancillary functions in kses which are prefixed by kses |
1737 | * 17/08/2004 :: Eamon DOT Costello AT dcu DOT ie |
1738 | * |
4e8f2e6b |
1739 | * @param array $htmlArray An array from {@link cleanAttributes()}, containing in its 1st |
1740 | * element the html to be cleared |
d48b00b4 |
1741 | * @return string |
1742 | */ |
4e8f2e6b |
1743 | function cleanAttributes2($htmlArray){ |
3bd7ffec |
1744 | |
037dcbb6 |
1745 | global $CFG, $ALLOWED_PROTOCOLS; |
b0ccd3fb |
1746 | require_once($CFG->libdir .'/kses.php'); |
3bd7ffec |
1747 | |
4e8f2e6b |
1748 | $htmlTag = $htmlArray[1]; |
037dcbb6 |
1749 | if (substr($htmlTag, 0, 1) != '<') { |
3bd7ffec |
1750 | return '>'; //a single character ">" detected |
1751 | } |
037dcbb6 |
1752 | if (!preg_match('%^<\s*(/\s*)?([a-zA-Z0-9]+)([^>]*)>?$%', $htmlTag, $matches)) { |
67ccec43 |
1753 | return ''; // It's seriously malformed |
1754 | } |
3bd7ffec |
1755 | $slash = trim($matches[1]); //trailing xhtml slash |
67ccec43 |
1756 | $elem = $matches[2]; //the element name |
3bd7ffec |
1757 | $attrlist = $matches[3]; // the list of attributes as a string |
1758 | |
037dcbb6 |
1759 | $attrArray = kses_hair($attrlist, $ALLOWED_PROTOCOLS); |
3bd7ffec |
1760 | |
67ccec43 |
1761 | $attStr = ''; |
037dcbb6 |
1762 | foreach ($attrArray as $arreach) { |
29939bea |
1763 | $arreach['name'] = strtolower($arreach['name']); |
1764 | if ($arreach['name'] == 'style') { |
1765 | $value = $arreach['value']; |
1766 | while (true) { |
1767 | $prevvalue = $value; |
1768 | $value = kses_no_null($value); |
1769 | $value = preg_replace("/\/\*.*\*\//Us", '', $value); |
1770 | $value = kses_decode_entities($value); |
1771 | $value = preg_replace('/(&#[0-9]+)(;?)/', "\\1;", $value); |
1772 | $value = preg_replace('/(&#x[0-9a-fA-F]+)(;?)/', "\\1;", $value); |
1773 | if ($value === $prevvalue) { |
1774 | $arreach['value'] = $value; |
1775 | break; |
1776 | } |
1777 | } |
1778 | $arreach['value'] = preg_replace("/j\s*a\s*v\s*a\s*s\s*c\s*r\s*i\s*p\s*t/i", "Xjavascript", $arreach['value']); |
1779 | $arreach['value'] = preg_replace("/e\s*x\s*p\s*r\s*e\s*s\s*s\s*i\s*o\s*n/i", "Xexpression", $arreach['value']); |
b8806ccc |
1780 | } else if ($arreach['name'] == 'href') { |
ee7f231d |
1781 | //Adobe Acrobat Reader XSS protection |
1782 | $arreach['value'] = preg_replace('/(\.(pdf|fdf|xfdf|xdp|xfd))[^a-z0-9_\.\-].*$/i', '$1', $arreach['value']); |
29939bea |
1783 | } |
049bd7db |
1784 | $attStr .= ' '.$arreach['name'].'="'.$arreach['value'].'"'; |
3bd7ffec |
1785 | } |
713126cd |
1786 | |
3bd7ffec |
1787 | $xhtml_slash = ''; |
037dcbb6 |
1788 | if (preg_match('%/\s*$%', $attrlist)) { |
67ccec43 |
1789 | $xhtml_slash = ' /'; |
3bd7ffec |
1790 | } |
b0ccd3fb |
1791 | return '<'. $slash . $elem . $attStr . $xhtml_slash .'>'; |
3bd7ffec |
1792 | } |
1793 | |
d48b00b4 |
1794 | /** |
1795 | * Replaces all known smileys in the text with image equivalents |
1796 | * |
1797 | * @uses $CFG |
1798 | * @param string $text Passed by reference. The string to search for smily strings. |
1799 | * @return string |
1800 | */ |
5f350e8f |
1801 | function replace_smilies(&$text) { |
772e78be |
1802 | /// |
2ea9027b |
1803 | global $CFG; |
c1d57101 |
1804 | |
5a144b3a |
1805 | $lang = current_language(); |
1806 | |
5b472756 |
1807 | /// this builds the mapping array only once |
69081931 |
1808 | static $e = array(); |
1809 | static $img = array(); |
617778f2 |
1810 | static $emoticons = array( |
fbfc2675 |
1811 | ':-)' => 'smiley', |
1812 | ':)' => 'smiley', |
1813 | ':-D' => 'biggrin', |
1814 | ';-)' => 'wink', |
1815 | ':-/' => 'mixed', |
1816 | 'V-.' => 'thoughtful', |
1817 | ':-P' => 'tongueout', |
1818 | 'B-)' => 'cool', |
1819 | '^-)' => 'approve', |
1820 | '8-)' => 'wideeyes', |
1821 | ':o)' => 'clown', |
1822 | ':-(' => 'sad', |
1823 | ':(' => 'sad', |
1824 | '8-.' => 'shy', |
1825 | ':-I' => 'blush', |
1826 | ':-X' => 'kiss', |
1827 | '8-o' => 'surprise', |
1828 | 'P-|' => 'blackeye', |
1829 | '8-[' => 'angry', |
1830 | 'xx-P' => 'dead', |
1831 | '|-.' => 'sleepy', |
1832 | '}-]' => 'evil', |
35cf8b79 |
1833 | '(h)' => 'heart', |
1834 | '(heart)' => 'heart', |
1835 | '(y)' => 'yes', |
1836 | '(n)' => 'no', |
2eb662d2 |
1837 | '(martin)' => 'martin', |
e84ea106 |
1838 | '( )' => 'egg' |
2ea9027b |
1839 | ); |
1840 | |
5a144b3a |
1841 | if (empty($img[$lang])) { /// After the first time this is not run again |
1842 | $e[$lang] = array(); |
1843 | $img[$lang] = array(); |
617778f2 |
1844 | foreach ($emoticons as $emoticon => $image){ |
fbfc2675 |
1845 | $alttext = get_string($image, 'pix'); |
1846 | |
5a144b3a |
1847 | $e[$lang][] = $emoticon; |
1848 | $img[$lang][] = '<img alt="'. $alttext .'" width="15" height="15" src="'. $CFG->pixpath .'/s/'. $image .'.gif" />'; |
617778f2 |
1849 | } |
c0f728ba |
1850 | } |
b7a3cf49 |
1851 | |
8dcd43f3 |
1852 | // Exclude from transformations all the code inside <script> tags |
1853 | // Needed to solve Bug 1185. Thanks to jouse 2001 detecting it. :-) |
1854 | // Based on code from glossary fiter by Williams Castillo. |
1855 | // - Eloy |
1856 | |
1857 | // Detect all the <script> zones to take out |
1858 | $excludes = array(); |
1859 | preg_match_all('/<script language(.+?)<\/script>/is',$text,$list_of_excludes); |
1860 | |
1861 | // Take out all the <script> zones from text |
1862 | foreach (array_unique($list_of_excludes[0]) as $key=>$value) { |
1863 | $excludes['<+'.$key.'+>'] = $value; |
1864 | } |
1865 | if ($excludes) { |
1866 | $text = str_replace($excludes,array_keys($excludes),$text); |
1867 | } |
1868 | |
fbfc2675 |
1869 | /// this is the meat of the code - this is run every time |
5a144b3a |
1870 | $text = str_replace($e[$lang], $img[$lang], $text); |
8dcd43f3 |
1871 | |
1872 | // Recover all the <script> zones to text |
1873 | if ($excludes) { |
1874 | $text = str_replace(array_keys($excludes),$excludes,$text); |
1875 | } |
1a072208 |
1876 | } |
0095d5cd |
1877 | |
89dcb99d |
1878 | /** |
1879 | * Given plain text, makes it into HTML as nicely as possible. |
1880 | * May contain HTML tags already |
1881 | * |
1882 | * @uses $CFG |
1883 | * @param string $text The string to convert. |
1884 | * @param boolean $smiley Convert any smiley characters to smiley images? |
1885 | * @param boolean $para If true then the returned string will be wrapped in paragraph tags |
1886 | * @param boolean $newlines If true then lines newline breaks will be converted to HTML newline breaks. |
1887 | * @return string |
1888 | */ |
1889 | |
b7a3d3b2 |
1890 | function text_to_html($text, $smiley=true, $para=true, $newlines=true) { |
772e78be |
1891 | /// |
f9903ed0 |
1892 | |
27326a3e |
1893 | global $CFG; |
1894 | |
c1d57101 |
1895 | /// Remove any whitespace that may be between HTML tags |
7b3be1b1 |
1896 | $text = eregi_replace(">([[:space:]]+)<", "><", $text); |
1897 | |
c1d57101 |
1898 | /// Remove any returns that precede or follow HTML tags |
0eae8049 |
1899 | $text = eregi_replace("([\n\r])<", " <", $text); |
1900 | $text = eregi_replace(">([\n\r])", "> ", $text); |
7b3be1b1 |
1901 | |
5f350e8f |
1902 | convert_urls_into_links($text); |
f9903ed0 |
1903 | |
c1d57101 |
1904 | /// Make returns into HTML newlines. |
b7a3d3b2 |
1905 | if ($newlines) { |
1906 | $text = nl2br($text); |
1907 | } |
f9903ed0 |
1908 | |
c1d57101 |
1909 | /// Turn smileys into images. |
d69cb7f4 |
1910 | if ($smiley) { |
5f350e8f |
1911 | replace_smilies($text); |
d69cb7f4 |
1912 | } |
f9903ed0 |
1913 | |
c1d57101 |
1914 | /// Wrap the whole thing in a paragraph tag if required |
909f539d |
1915 | if ($para) { |
b0ccd3fb |
1916 | return '<p>'.$text.'</p>'; |
909f539d |
1917 | } else { |
1918 | return $text; |
1919 | } |
f9903ed0 |
1920 | } |
1921 | |
d48b00b4 |
1922 | /** |
1923 | * Given Markdown formatted text, make it into XHTML using external function |
1924 | * |
89dcb99d |
1925 | * @uses $CFG |
1926 | * @param string $text The markdown formatted text to be converted. |
1927 | * @return string Converted text |
d48b00b4 |
1928 | */ |
e7cdcd18 |
1929 | function markdown_to_html($text) { |
e7cdcd18 |
1930 | global $CFG; |
1931 | |
b0ccd3fb |
1932 | require_once($CFG->libdir .'/markdown.php'); |
e7cdcd18 |
1933 | |
1934 | return Markdown($text); |
1935 | } |
1936 | |
d48b00b4 |
1937 | /** |
89dcb99d |
1938 | * Given HTML text, make it into plain text using external function |
d48b00b4 |
1939 | * |
1940 | * @uses $CFG |
1941 | * @param string $html The text to be converted. |
1942 | * @return string |
1943 | */ |
6ff45b59 |
1944 | function html_to_text($html) { |
89dcb99d |
1945 | |
428aaa29 |
1946 | global $CFG; |
6ff45b59 |
1947 | |
b0ccd3fb |
1948 | require_once($CFG->libdir .'/html2text.php'); |
6ff45b59 |
1949 | |
1950 | return html2text($html); |
1951 | } |
1952 | |
d48b00b4 |
1953 | /** |
1954 | * Given some text this function converts any URLs it finds into HTML links |
1955 | * |
1956 | * @param string $text Passed in by reference. The string to be searched for urls. |
1957 | */ |
5f350e8f |
1958 | function convert_urls_into_links(&$text) { |
5f350e8f |
1959 | /// Make lone URLs into links. eg http://moodle.com/ |
3405b212 |
1960 | $text = eregi_replace("([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", |
bc01a2b8 |
1961 | "\\1<a href=\"\\2://\\3\\4\" target=\"_blank\">\\2://\\3\\4</a>", $text); |
5f350e8f |
1962 | |
1963 | /// eg www.moodle.com |
ab9f24ad |
1964 | $text = eregi_replace("([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])", |
bc01a2b8 |
1965 | "\\1<a href=\"http://www.\\2\\3\" target=\"_blank\">www.\\2\\3</a>", $text); |
5f350e8f |
1966 | } |
1967 | |
d48b00b4 |
1968 | /** |
1969 | * This function will highlight search words in a given string |
1970 | * It cares about HTML and will not ruin links. It's best to use |
1971 | * this function after performing any conversions to HTML. |
1972 | * Function found here: http://forums.devshed.com/t67822/scdaa2d1c3d4bacb4671d075ad41f0854.html |
1973 | * |
89dcb99d |
1974 | * @param string $needle The string to search for |
1975 | * @param string $haystack The string to search for $needle in |
1976 | * @param int $case ? |
1977 | * @return string |
d48b00b4 |
1978 | * @todo Finish documenting this function |
1979 | */ |
ab9f24ad |
1980 | function highlight($needle, $haystack, $case=0, |
b0ccd3fb |
1981 | $left_string='<span class="highlight">', $right_string='</span>') { |
69d51d3a |
1982 | if (empty($needle)) { |
1983 | return $haystack; |
1984 | } |
1985 | |
5eecb8cb |
1986 | //$list_of_words = eregi_replace("[^-a-zA-Z0-9&.']", " ", $needle); // bug 3101 |
1987 | $list_of_words = $needle; |
b0ccd3fb |
1988 | $list_array = explode(' ', $list_of_words); |
88438a58 |
1989 | for ($i=0; $i<sizeof($list_array); $i++) { |
1990 | if (strlen($list_array[$i]) == 1) { |
b0ccd3fb |
1991 | $list_array[$i] = ''; |
88438a58 |
1992 | } |
1993 | } |
b0ccd3fb |
1994 | $list_of_words = implode(' ', $list_array); |
88438a58 |
1995 | $list_of_words_cp = $list_of_words; |
1996 | $final = array(); |
1997 | preg_match_all('/<(.+?)>/is',$haystack,$list_of_words); |
1998 | |
1999 | foreach (array_unique($list_of_words[0]) as $key=>$value) { |
2000 | $final['<|'.$key.'|>'] = $value; |
2001 | } |
2002 | |
2003 | $haystack = str_replace($final,array_keys($final),$haystack); |
b0ccd3fb |
2004 | $list_of_words_cp = eregi_replace(' +', '|', $list_of_words_cp); |
88438a58 |
2005 | |
b0ccd3fb |
2006 | if ($list_of_words_cp{0}=='|') { |
2007 | $list_of_words_cp{0} = ''; |
88438a58 |
2008 | } |
b0ccd3fb |
2009 | if ($list_of_words_cp{strlen($list_of_words_cp)-1}=='|') { |
2010 | $list_of_words_cp{strlen($list_of_words_cp)-1}=''; |
88438a58 |
2011 | } |
88438a58 |
2012 | |
9ccdcd97 |
2013 | $list_of_words_cp = trim($list_of_words_cp); |
2014 | |
2015 | if ($list_of_words_cp) { |
2016 | |
2017 | $list_of_words_cp = "(". $list_of_words_cp .")"; |
2018 | |
2019 | if (!$case){ |
2020 | $haystack = eregi_replace("$list_of_words_cp", "$left_string"."\\1"."$right_string", $haystack); |
2021 | } else { |
2022 | $haystack = ereg_replace("$list_of_words_cp", "$left_string"."\\1"."$right_string", $haystack); |
2023 | } |
88438a58 |
2024 | } |
2025 | $haystack = str_replace(array_keys($final),$final,$haystack); |
2026 | |
f60e7cfe |
2027 | return $haystack; |
88438a58 |
2028 | } |
2029 | |
d48b00b4 |
2030 | /** |
2031 | * This function will highlight instances of $needle in $haystack |
2032 | * It's faster that the above function and doesn't care about |
2033 | * HTML or anything. |
2034 | * |
2035 | * @param string $needle The string to search for |
2036 | * @param string $haystack The string to search for $needle in |
2037 | * @return string |
2038 | */ |
88438a58 |
2039 | function highlightfast($needle, $haystack) { |
5af78ed2 |
2040 | |
57f1b914 |
2041 | $parts = explode(moodle_strtolower($needle), moodle_strtolower($haystack)); |
5af78ed2 |
2042 | |
2043 | $pos = 0; |
2044 | |
2045 | foreach ($parts as $key => $part) { |
2046 | $parts[$key] = substr($haystack, $pos, strlen($part)); |
2047 | $pos += strlen($part); |
2048 | |
b0ccd3fb |
2049 | $parts[$key] .= '<span class="highlight">'.substr($haystack, $pos, strlen($needle)).'</span>'; |
5af78ed2 |
2050 | $pos += strlen($needle); |
ab9f24ad |
2051 | } |
5af78ed2 |
2052 | |
2053 | return (join('', $parts)); |
2054 | } |
2055 | |
2ab4e4b8 |
2056 | /** |
2057 | * Return a string containing 'lang', xml:lang and optionally 'dir' HTML attributes. |
2058 | * Internationalisation, for print_header and backup/restorelib. |
2059 | * @param $dir Default false. |
2060 | * @return string Attributes. |
2061 | */ |
2062 | function get_html_lang($dir = false) { |
2063 | $direction = ''; |
2064 | if ($dir) { |
2065 | if (get_string('thisdirection') == 'rtl') { |
2066 | $direction = ' dir="rtl"'; |
2067 | } else { |
2068 | $direction = ' dir="ltr"'; |
2069 | } |
2070 | } |
2071 | //Accessibility: added the 'lang' attribute to $direction, used in theme <html> tag. |
2072 | $language = str_replace('_', '-', str_replace('_utf8', '', current_language())); |
0946fff4 |
2073 | @header('Content-Language: '.$language); |
2ab4e4b8 |
2074 | return ($direction.' lang="'.$language.'" xml:lang="'.$language.'"'); |
2075 | } |
2076 | |
f9903ed0 |
2077 | |
9fa49e22 |
2078 | /// STANDARD WEB PAGE PARTS /////////////////////////////////////////////////// |
2079 | |
d48b00b4 |
2080 | /** |
2081 | * Print a standard header |
2082 | * |
89dcb99d |
2083 | * @uses $USER |
2084 | * @uses $CFG |
89dcb99d |
2085 | * @uses $SESSION |
2086 | * @param string $title Appears at the top of the window |
2087 | * @param string $heading Appears at the top of the page |
2088 | * @param string $navigation Premade navigation string (for use as breadcrumbs links) |
2089 | * @param string $focus Indicates form element to get cursor focus on load eg inputform.password |
2090 | * @param string $meta Meta tags to be added to the header |
2091 | * @param boolean $cache Should this page be cacheable? |
2092 | * @param string $button HTML code for a button (usually for module editing) |
2093 | * @param string $menu HTML code for a popup menu |
2094 | * @param boolean $usexml use XML for this page |
2095 | * @param string $bodytags This text will be included verbatim in the <body> tag (useful for onload() etc) |
36b6bcec |
2096 | * @param bool $return If true, return the visible elements of the header instead of echoing them. |
d48b00b4 |
2097 | */ |
36b6bcec |
2098 | function print_header ($title='', $heading='', $navigation='', $focus='', |
2099 | $meta='', $cache=true, $button=' ', $menu='', |
2100 | $usexml=false, $bodytags='', $return=false) { |
63f3cbbd |
2101 | |
dcf6d93c |
2102 | global $USER, $CFG, $THEME, $SESSION, $ME, $SITE, $COURSE; |
6ba65fa0 |
2103 | |
2104 | $heading = format_string($heading); // Fix for MDL-8582 |
2105 | |
32613b50 |
2106 | /// This makes sure that the header is never repeated twice on a page |
f231e867 |
2107 | if (defined('HEADER_PRINTED')) { |
ea82d6b6 |
2108 | debugging('print_header() was called more than once - this should not happen. Please check the code for this page closely. Note: error() and redirect() are now safe to call after print_header().'); |
32613b50 |
2109 | return; |
32613b50 |
2110 | } |
f231e867 |
2111 | define('HEADER_PRINTED', 'true'); |
2112 | |
be933850 |
2113 | |
c3f55692 |
2114 | /// Add the required stylesheets |
d74d4f20 |
2115 | $stylesheetshtml = ''; |
2116 | foreach ($CFG->stylesheets as $stylesheet) { |
2117 | $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n"; |
c3f55692 |
2118 | } |
d74d4f20 |
2119 | $meta = $stylesheetshtml.$meta; |
e89fb61e |
2120 | |
79f533c3 |
2121 | |
2122 | /// Add the meta page from the themes if any were requested |
2123 | |
2124 | $metapage = ''; |
2125 | |
2126 | if (!isset($THEME->standardmetainclude) || $THEME->standardmetainclude) { |
2127 | ob_start(); |
2128 | include_once($CFG->dirroot.'/theme/standard/meta.php'); |
2129 | $metapage .= ob_get_contents(); |
2130 | ob_end_clean(); |
2131 | } |
2132 | |
2133 | if ($THEME->parent && (!isset($THEME->parentmetainclude) || $THEME->parentmetainclude)) { |
36e8c122 |
2134 | if (file_exists($CFG->dirroot.'/theme/'.$THEME->parent.'/meta.php')) { |
2135 | ob_start(); |
2136 | include_once($CFG->dirroot.'/theme/'.$THEME->parent.'/meta.php'); |
2137 | $metapage .= ob_get_contents(); |
2138 | ob_end_clean(); |
2139 | } |
79f533c3 |
2140 | } |
2141 | |
2142 | if (!isset($THEME->metainclude) || $THEME->metainclude) { |
36e8c122 |
2143 | if (file_exists($CFG->dirroot.'/theme/'.current_theme().'/meta.php')) { |
2144 | ob_start(); |
2145 | include_once($CFG->dirroot.'/theme/'.current_theme().'/meta.php'); |
2146 | $metapage .= ob_get_contents(); |
2147 | ob_end_clean(); |
2148 | } |
79f533c3 |
2149 | } |
2150 | |
2151 | $meta = $meta."\n".$metapage; |
2152 | |
2153 | |
604c6341 |
2154 | /// Add the required JavaScript Libraries |
2155 | $meta .= "\n".require_js(); |
2156 | |
ac4feb59 |
2157 | if(is_newnav($navigation)){ |
9d378732 |
2158 | $home = false; |
ac4feb59 |
2159 | } else { |
2160 | if ($navigation == 'home') { |
2161 | $home = true; |
2162 | $navigation = ''; |
2163 | } else { |
2164 | $home = false; |
2165 | } |
9fa49e22 |
2166 | } |
2167 | |
0d741155 |
2168 | /// This is another ugly hack to make navigation elements available to print_footer later |
2169 | $THEME->title = $title; |
2170 | $THEME->heading = $heading; |
2171 | $THEME->navigation = $navigation; |
2172 | $THEME->button = $button; |
2173 | $THEME->menu = $menu; |
2507b2f5 |
2174 | $navmenulist = isset($THEME->navmenulist) ? $THEME->navmenulist : ''; |
0d741155 |
2175 | |
b0ccd3fb |
2176 | if ($button == '') { |
2177 | $button = ' '; |
9fa49e22 |
2178 | } |
2179 | |
2180 | if (!$menu and $navigation) { |
8a33e371 |
2181 | if (empty($CFG->loginhttps)) { |
2182 | $wwwroot = $CFG->wwwroot; |
2183 | } else { |
2c3432e6 |
2184 | $wwwroot = str_replace('http:','https:',$CFG->wwwroot); |
8a33e371 |
2185 | } |
60f9e36e |
2186 | $menu = user_login_string($COURSE); |
9fa49e22 |
2187 | } |
67ccec43 |
2188 | |
b4bac9b6 |
2189 | if (isset($SESSION->justloggedin)) { |
2190 | unset($SESSION->justloggedin); |
2191 | if (!empty($CFG->displayloginfailures)) { |
8f8ed475 |
2192 | if (!empty($USER->username) and $USER->username != 'guest') { |
b4bac9b6 |
2193 | if ($count = count_login_failures($CFG->displayloginfailures, $USER->username, $USER->lastlogin)) { |
2194 | $menu .= ' <font size="1">'; |
2195 | if (empty($count->accounts)) { |
2196 | $menu .= get_string('failedloginattempts', '', $count); |
2197 | } else { |
2198 | $menu .= get_string('failedloginattemptsall', '', $count); |
2199 | } |
51792df0 |
2200 | if (has_capability('moodle/site:viewreports', get_context_instance(CONTEXT_SYSTEM, SITEID))) { |
52af9a35 |
2201 | $menu .= ' (<a href="'.$CFG->wwwroot.'/course/report/log/index.php'. |
839f2456 |
2202 | '?chooselog=1&id=1&modid=site_errors">'.get_string('logs').'</a>)'; |
b4bac9b6 |
2203 | } |
2204 | $menu .= '</font>'; |
2205 | } |
2206 | } |
2207 | } |
2208 | } |
9fa49e22 |
2209 | |
47037513 |
2210 | |
810944af |
2211 | $meta = '<meta http-equiv="content-type" content="text/html; charset=utf-8" />'. "\n". $meta ."\n"; |
03fe48e7 |
2212 | if (!$usexml) { |
810944af |
2213 | @header('Content-type: text/html; charset=utf-8'); |
03fe48e7 |
2214 | } |
9fa49e22 |
2215 | |
6575bfd4 |
2216 | //Accessibility: added the 'lang' attribute to $direction, used in theme <html> tag. |
2ab4e4b8 |
2217 | $direction = get_html_lang($dir=true); |
ab9f24ad |
2218 | |
5debee2d |
2219 | if ($cache) { // Allow caching on "back" (but not on normal clicks) |
2220 | @header('Cache-Control: private, pre-check=0, post-check=0, max-age=0'); |
2221 | @header('Pragma: no-cache'); |
772e78be |
2222 | @header('Expires: '); |
5debee2d |
2223 | } else { // Do everything we can to always prevent clients and proxies caching |
03fe48e7 |
2224 | @header('Cache-Control: no-store, no-cache, must-revalidate'); |
2225 | @header('Cache-Control: post-check=0, pre-check=0', false); |
2226 | @header('Pragma: no-cache'); |
5debee2d |
2227 | @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT'); |
2228 | @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); |
03fe48e7 |
2229 | |
5debee2d |
2230 | $meta .= "\n<meta http-equiv=\"pragma\" content=\"no-cache\" />"; |
2231 | $meta .= "\n<meta http-equiv=\"expires\" content=\"0\" />"; |
66a51452 |
2232 | } |
5debee2d |
2233 | @header('Accept-Ranges: none'); |
66a51452 |
2234 | |
5ce73257 |
2235 | if (empty($usexml)) { |
27f79fda |
2236 | $direction = ' xmlns="http://www.w3.org/1999/xhtml"'. $direction; // See debug_header |
2237 | } else { |
66a51452 |
2238 | $currentlanguage = current_language(); |
8f0cd6ef |
2239 | $mathplayer = preg_match("/MathPlayer/i", $_SERVER['HTTP_USER_AGENT']); |
2240 | if(!$mathplayer) { |
2241 | header('Content-Type: application/xhtml+xml'); |
2242 | } |
b0ccd3fb |
2243 | echo '<?xml version="1.0" ?>'."\n"; |
66a51452 |
2244 | if (!empty($CFG->xml_stylesheets)) { |
b0ccd3fb |
2245 | $stylesheets = explode(';', $CFG->xml_stylesheets); |
66a51452 |
2246 | foreach ($stylesheets as $stylesheet) { |
b0ccd3fb |
2247 | echo '<?xml-stylesheet type="text/xsl" href="'. $CFG->wwwroot .'/'. $stylesheet .'" ?>' . "\n"; |
66a51452 |
2248 | } |
2249 | } |
b0ccd3fb |
2250 | echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1'; |
e4576482 |
2251 | if (!empty($CFG->xml_doctype_extra)) { |
b0ccd3fb |
2252 | echo ' plus '. $CFG->xml_doctype_extra; |
e4576482 |
2253 | } |
b0ccd3fb |
2254 | echo '//' . strtoupper($currentlanguage) . '" "'. $CFG->xml_dtd .'">'."\n"; |
8f0cd6ef |
2255 | $direction = " xmlns=\"http://www.w3.org/1999/xhtml\" |
2256 | xmlns:math=\"http://www.w3.org/1998/Math/MathML\" |
8f0cd6ef |
2257 | xmlns:xlink=\"http://www.w3.org/1999/xlink\" |
2258 | $direction"; |
2259 | if($mathplayer) { |
2260 | $meta .= '<object id="mathplayer" classid="clsid:32F66A20-7614-11D4-BD11-00104BD3F987">' . "\n"; |
b0ccd3fb |
2261 | $meta .= '<!--comment required to prevent this becoming an empty tag-->'."\n"; |
2262 | $meta .= '</object>'."\n"; |
8f0cd6ef |
2263 | $meta .= '<?import namespace="math" implementation="#mathplayer" ?>' . "\n"; |
2264 | } |
9fa49e22 |
2265 | } |
2266 | |
7bb6d80f |
2267 | // Clean up the title |
2268 | |
268ddd50 |
2269 | $title = format_string($title); // fix for MDL-8582 |
2eea2cce |
2270 | $title = str_replace('"', '"', $title); |
2271 | |
7bb6d80f |
2272 | // Create class and id for this page |
2273 | |
68d5f00a |
2274 | page_id_and_class($pageid, $pageclass); |
7bb6d80f |
2275 | |
60f9e36e |
2276 | $pageclass .= ' course-'.$COURSE->id; |
d6c66e12 |
2277 | |
758027d0 |
2278 | if (($pageid != 'site-index') && ($pageid != 'course-view') && |
1d41dcd0 |
2279 | (strstr($pageid, 'admin') === FALSE)) { |
758027d0 |
2280 | $pageclass .= ' nocoursepage'; |
2281 | } |
2282 | |
367e23c2 |
2283 | if (!isloggedin()) { |
2284 | $pageclass .= ' notloggedin'; |
2285 | } |
2286 | |
e299f862 |
2287 | if (!empty($USER->editing)) { |
2288 | $pageclass .= ' editing'; |
2289 | } |
2290 | |
75c3849a |
2291 | if (!empty($CFG->blocksdrag)) { |
2292 | $pageclass .= ' drag'; |
2293 | } |
2294 | |
7bb6d80f |
2295 | $bodytags .= ' class="'.$pageclass.'" id="'.$pageid.'"'; |
772e78be |
2296 | |
36b6bcec |
2297 | ob_start(); |
ea35ab87 |
2298 | include($CFG->header); |
36b6bcec |
2299 | $output = ob_get_contents(); |
2300 | ob_end_clean(); |
e608dddd |
2301 | |
8af1b1ba |
2302 | $output = force_strict_header($output); |
27f79fda |
2303 | |
cdf39255 |
2304 | if (!empty($CFG->messaging)) { |
36b6bcec |
2305 | $output .= message_popup_window(); |
2306 | } |
2307 | |
2308 | if ($return) { |
2309 | return $output; |
2310 | } else { |
2311 | echo $output; |
cdf39255 |
2312 | } |
9fa49e22 |
2313 | } |
2314 | |
27f79fda |
2315 | /** |
2316 | * Debugging aid: serve page as 'application/xhtml+xml' where possible, |
2317 | * and substitute the XHTML strict document type. |
2318 | * Note, requires the 'xmlns' fix in function print_header above. |
2319 | * See: http://tracker.moodle.org/browse/MDL-7883 |
2320 | * TODO: |
2321 | */ |
8af1b1ba |
2322 | function force_strict_header($output) { |
27f79fda |
2323 | global $CFG; |
2324 | $strict = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; |
2325 | $xsl = '/lib/xhtml.xsl'; |
2326 | |
8af1b1ba |
2327 | if (!headers_sent() && debugging(NULL, DEBUG_DEVELOPER)) { // In developer debugging, the browser will barf |
27f79fda |
2328 | $ctype = 'Content-Type: '; |
2329 | $prolog= "<?xml version='1.0' encoding='utf-8'?>\n"; |
2330 | |
5ce73257 |
2331 | if (isset($_SERVER['HTTP_ACCEPT']) |
27f79fda |
2332 | && false !== strpos($_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml')) { |
2333 | //|| false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') //Safari "Entity 'copy' not defined". |
2334 | // Firefox et al. |
2335 | $ctype .= 'application/xhtml+xml'; |
2336 | $prolog .= "<!--\n DEBUG: $ctype \n-->\n"; |
2337 | |
2338 | } else if (file_exists($CFG->dirroot.$xsl) |
2339 | && preg_match('/MSIE.*Windows NT/', $_SERVER['HTTP_USER_AGENT'])) { |
2340 | // XSL hack for IE 5+ on Windows. |
2341 | //$www_xsl = preg_replace('/(http:\/\/.+?\/).*/', '', $CFG->wwwroot) .$xsl; |
2342 | $www_xsl = $CFG->wwwroot .$xsl; |
2343 | $ctype .= 'application/xml'; |
2344 | $prolog .= "<?xml-stylesheet type='text/xsl' href='$www_xsl'?>\n"; |
2345 | $prolog .= "<!--\n DEBUG: $ctype \n-->\n"; |
2346 | |
2347 | } else { |
2348 | //ELSE: Mac/IE, old/non-XML browsers. |
2349 | $ctype .= 'text/html'; |
2350 | $prolog = ''; |
2351 | } |
2352 | @header($ctype.'; charset=utf-8'); |
2353 | $output = $prolog . $output; |
2354 | |
2355 | // Test parser error-handling. |
2356 | if (isset($_GET['error'])) { |
2357 | $output .= "__ TEST: XML well-formed error < __\n"; |
2358 | } |
2359 | } |
8af1b1ba |
2360 | |
84dec541 |
2361 | $output = preg_replace('/(<!DOCTYPE.+?>)/s', $strict, $output); // Always change the DOCTYPE to Strict 1.0 |
8af1b1ba |
2362 | |
27f79fda |
2363 | return $output; |
2364 | } |
2365 | |
2366 | |
2367 | |
d48b00b4 |
2368 | /** |
2369 | * This version of print_header is simpler because the course name does not have to be |
2370 | * provided explicitly in the strings. It can be used on the site page as in courses |
2371 | * Eventually all print_header could be replaced by print_header_simple |
2372 | * |
89dcb99d |
2373 | * @param string $title Appears at the top of the window |
2374 | * @param string $heading Appears at the top of the page |
2375 | * @param string $navigation Premade navigation string (for use as breadcrumbs links) |
2376 | * @param string $focus Indicates form element to get cursor focus on load eg inputform.password |
2377 | * @param string $meta Meta tags to be added to the header |
2378 | * @param boolean $cache Should this page be cacheable? |
2379 | * @param string $button HTML code for a button (usually for module editing) |
2380 | * @param string $menu HTML code for a popup menu |
2381 | * @param boolean $usexml use XML for this page |
2382 | * @param string $bodytags This text will be included verbatim in the <body> tag (useful for onload() etc) |
36b6bcec |
2383 | * @param bool $return If true, return the visible elements of the header instead of echoing them. |
d48b00b4 |
2384 | */ |
b0ccd3fb |
2385 | function print_header_simple($title='', $heading='', $navigation='', $focus='', $meta='', |
36b6bcec |
2386 | $cache=true, $button=' ', $menu='', $usexml=false, $bodytags='', $return=false) { |
90fcc576 |
2387 | |
5ce73257 |
2388 | global $COURSE, $CFG; |
90fcc576 |
2389 | |
2390 | $shortname =''; |
c66c2c02 |
2391 | if ($COURSE->id != SITEID) { |
6ba65fa0 |
2392 | $shortname = '<a href="'.$CFG->wwwroot.'/course/view.php?id='. $COURSE->id .'">'. $COURSE->shortname .'</a> ->'; |
90fcc576 |
2393 | } |
2394 | |
ac4feb59 |
2395 | // If old style nav prepend course short name otherwise leave $navigation object alone |
2396 | if (!is_newnav($navigation)) { |
2397 | $navigation = $shortname.' '.$navigation; |
2398 | } |
2399 | |
2400 | $output = print_header($COURSE->shortname .': '. $title, $COURSE->fullname .' '. $heading, $navigation, $focus, $meta, |
da4124be |
2401 | $cache, $button, $menu, $usexml, $bodytags, true); |
36b6bcec |
2402 | |
2403 | if ($return) { |
2404 | return $output; |
2405 | } else { |
2406 | echo $output; |
2407 | } |
90fcc576 |
2408 | } |
629b5885 |
2409 | |
2410 | |
d48b00b4 |
2411 | /** |
2412 | * Can provide a course object to make the footer contain a link to |
2413 | * to the course home page, otherwise the link will go to the site home |
2414 | * |
2415 | * @uses $CFG |
2416 | * @uses $USER |
89dcb99d |
2417 | * @param course $course {@link $COURSE} object containing course information |
2418 | * @param ? $usercourse ? |
d48b00b4 |
2419 | * @todo Finish documenting this function |
2420 | */ |
469b6501 |
2421 | function print_footer($course=NULL, $usercourse=NULL, $return=false) { |
dcf6d93c |
2422 | global $USER, $CFG, $THEME, $COURSE; |
9fa49e22 |
2423 | |
1ae083e4 |
2424 | if (defined('ADMIN_EXT_HEADER_PRINTED') and !defined('ADMIN_EXT_FOOTER_PRINTED')) { |
635773c6 |
2425 | admin_externalpage_print_footer(); |
2426 | return; |
1ae083e4 |
2427 | } |
2428 | |
9fa49e22 |
2429 | /// Course links |
2430 | if ($course) { |
a789577b |
2431 | if (is_string($course) && $course == 'none') { // Don't print any links etc |
b3a2b9dd |
2432 | $homelink = ''; |
2433 | $loggedinas = ''; |
0d741155 |
2434 | $home = false; |
a789577b |
2435 | } else if (is_string($course) && $course == 'home') { // special case for site home page - please do not remove |
b3a2b9dd |
2436 | $course = get_site(); |
a77ac3dc |
2437 | $homelink = '<div class="sitelink">'. |
244f6f32 |
2438 | '<a title="moodle '. $CFG->release .' ('. $CFG->version .')" href="http://moodle.org/">'. |
2439 | '<br /><img style="width:100px;height:30px" src="pix/moodlelogo.gif" alt="moodlelogo" /></a></div>'; |
0d741155 |
2440 | $home = true; |
9fa49e22 |
2441 | } else { |
fa738731 |
2442 | $homelink = '<div class="homelink"><a '.$CFG->frametarget.' href="'.$CFG->wwwroot. |
6ba65fa0 |
2443 | '/course/view.php?id='.$course->id.'">'.format_string($course->shortname).'</a></div>'; |
0d741155 |
2444 | $home = false; |
9fa49e22 |
2445 | } |
2446 | } else { |
b3a2b9dd |
2447 | $course = get_site(); // Set course as site course by default |
fa738731 |
2448 | $homelink = '<div class="homelink"><a '.$CFG->frametarget.' href="'.$CFG->wwwroot.'/">'.get_string('home').'</a></div>'; |
0d741155 |
2449 | $home = false; |
9fa49e22 |
2450 | } |
2451 | |
0d741155 |
2452 | /// Set up some other navigation links (passed from print_header by ugly hack) |
2507b2f5 |
2453 | $menu = isset($THEME->menu) ? str_replace('navmenu', 'navmenufooter', $THEME->menu) : ''; |
2454 | $title = isset($THEME->title) ? $THEME->title : ''; |
2455 | $button = isset($THEME->button) ? $THEME->button : ''; |
2456 | $heading = isset($THEME->heading) ? $THEME->heading : ''; |
2457 | $navigation = isset($THEME->navigation) ? $THEME->navigation : ''; |
2458 | $navmenulist = isset($THEME->navmenulist) ? $THEME->navmenulist : ''; |
0d741155 |
2459 | |
f940ee41 |
2460 | |
b3a2b9dd |
2461 | /// Set the user link if necessary |
2462 | if (!$usercourse and is_object($course)) { |
1f2eec7b |
2463 | $usercourse = $course; |
2464 | } |
2465 | |
b3a2b9dd |
2466 | if (!isset($loggedinas)) { |
2467 | $loggedinas = user_login_string($usercourse, $USER); |
2468 | } |
2469 | |
0d741155 |
2470 | if ($loggedinas == $menu) { |
9e0d1983 |
2471 | $menu = ''; |
0d741155 |
2472 | } |
2473 | |
b8cea9b2 |
2474 | /// Provide some performance info if required |
c2fd9e95 |
2475 | $performanceinfo = ''; |
afd2b299 |
2476 | if (defined('MDL_PERF') || (!empty($CFG->perfdebug) and $CFG->perfdebug > 7)) { |
c2fd9e95 |
2477 | $perf = get_performance_info(); |
853df85e |
2478 | if (defined('MDL_PERFTOLOG')) { |
2479 | error_log("PERF: " . $perf['txt']); |
2480 | } |
ea82d6b6 |
2481 | if (defined('MDL_PERFTOFOOT') || debugging() || $CFG->perfdebug > 7) { |
c2fd9e95 |
2482 | $performanceinfo = $perf['html']; |
2483 | } |
572fe9ab |
2484 | } |
b8cea9b2 |
2485 | |
0d741155 |
2486 | |
b3a2b9dd |
2487 | /// Include the actual footer file |
a282d0ff |
2488 | |
469b6501 |
2489 | ob_start(); |
ea35ab87 |
2490 | include($CFG->footer); |
469b6501 |
2491 | $output = ob_get_contents(); |
2492 | ob_end_clean(); |
a9a9bdba |
2493 | |
469b6501 |
2494 | if ($return) { |
2495 | return $output; |
2496 | } else { |
2497 | echo $output; |
2498 | } |
a282d0ff |
2499 | } |
2500 | |
c3f55692 |
2501 | /** |
2502 | * Returns the name of the current theme |
2503 | * |
2504 | * @uses $CFG |
2505 | * @param $USER |
2506 | * @param $SESSION |
2507 | * @return string |
2508 | */ |
2509 | function current_theme() { |
9152fc99 |
2510 | global $CFG, $USER, $SESSION, $COURSE; |
c3f55692 |
2511 | |
417375b7 |
2512 | if (!empty($CFG->pagetheme)) { // Page theme is for special page-only themes set by code |
2513 | return $CFG->pagetheme; |
c3f55692 |
2514 | |
9152fc99 |
2515 | } else if (!empty($CFG->allowcoursethemes) and !empty($COURSE->theme)) { // Course themes override others |
2516 | return $COURSE->theme; |
c3f55692 |
2517 | |
2518 | } else if (!empty($SESSION->theme)) { // Session theme can override other settings |
2519 | return $SESSION->theme; |
2520 | |
9152fc99 |
2521 | } else if (!empty($CFG->allowuserthemes) and !empty($USER->theme)) { // User theme can override site theme |
c3f55692 |
2522 | return $USER->theme; |
2523 | |
2524 | } else { |
2525 | return $CFG->theme; |
2526 | } |
2527 | } |
2528 | |
2529 | |
d48b00b4 |
2530 | /** |
2531 | * This function is called by stylesheets to set up the header |
2532 | * approriately as well as the current path |
2533 | * |
2534 | * @uses $CFG |
89dcb99d |
2535 | * @param int $lastmodified ? |
2536 | * @param int $lifetime ? |
2537 | * @param string $thename ? |
d48b00b4 |
2538 | */ |
55b734fb |
2539 | function style_sheet_setup($lastmodified=0, $lifetime=300, $themename='', $forceconfig='', $lang='') { |
6535be85 |
2540 | |
9c4e6e21 |
2541 | global $CFG, $THEME; |
ab9f24ad |
2542 | |
a2e2bf64 |
2543 | // Fix for IE6 caching - we don't want the filemtime('styles.php'), instead use now. |
2544 | $lastmodified = time(); |
2545 | |
b0ccd3fb |
2546 | header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $lastmodified) . ' GMT'); |
2547 | header('Expires: ' . gmdate("D, d M Y H:i:s", time() + $lifetime) . ' GMT'); |
a2e2bf64 |
2548 | header('Cache-Control: max-age='. $lifetime); |
b0ccd3fb |
2549 | header('Pragma: '); |
2550 | header('Content-type: text/css'); // Correct MIME type |
6535be85 |
2551 | |
909ec807 |
2552 | $DEFAULT_SHEET_LIST = array('styles_layout', 'styles_fonts', 'styles_color'); |
9c4e6e21 |
2553 | |
2554 | if (empty($themename)) { |
2555 | $themename = current_theme(); // So we have something. Normally not needed. |
ab25ce31 |
2556 | } else { |
2557 | $themename = clean_param($themename, PARAM_SAFEDIR); |
9c4e6e21 |
2558 | } |
2559 | |
2560 | if (!empty($forceconfig)) { // Page wants to use the config from this theme instead |
2561 | unset($THEME); |
a44091bf |
2562 | include($CFG->themedir.'/'.$forceconfig.'/'.'config.php'); |
9c4e6e21 |
2563 | } |
2564 | |
2565 | /// If this is the standard theme calling us, then find out what sheets we need |
2566 | |
2567 | if ($themename == 'standard') { |
2568 | if (!isset($THEME->standardsheets) or $THEME->standardsheets === true) { // Use all the sheets we have |
2569 | $THEME->sheets = $DEFAULT_SHEET_LIST; |
2570 | } else if (empty($THEME->standardsheets)) { // We can stop right now! |
2571 | echo "/***** Nothing required from this stylesheet by main theme *****/\n\n"; |
2572 | exit; |
2573 | } else { // Use the provided subset only |
2574 | $THEME->sheets = $THEME->standardsheets; |
2575 | } |
2576 | |
2577 | /// If we are a parent theme, then check for parent definitions |
2578 | |
2579 | } else if (!empty($THEME->parent) && $themename == $THEME->parent) { |
2580 | if (!isset($THEME->parentsheets) or $THEME->parentsheets === true) { // Use all the sheets we have |
2581 | $THEME->sheets = $DEFAULT_SHEET_LIST; |
2582 | } else if (empty($THEME->parentsheets)) { // We can stop right now! |
2583 | echo "/***** Nothing required from this stylesheet by main theme *****/\n\n"; |
2584 | exit; |
2585 | } else { // Use the provided subset only |
2586 | $THEME->sheets = $THEME->parentsheets; |
2587 | } |
2588 | } |
2589 | |
2590 | /// Work out the last modified date for this theme |
2591 | |
2592 | foreach ($THEME->sheets as $sheet) { |
a44091bf |
2593 | if (file_exists($CFG->themedir.'/'.$themename.'/'.$sheet.'.css')) { |
2594 | $sheetmodified = filemtime($CFG->themedir.'/'.$themename.'/'.$sheet.'.css'); |
9c4e6e21 |
2595 | if ($sheetmodified > $lastmodified) { |
2596 | $lastmodified = $sheetmodified; |
2597 | } |
2598 | } |
6535be85 |
2599 | } |
2600 | |
6535be85 |
2601 | |
6ba172fb |
2602 | /// Get a list of all the files we want to include |
2603 | $files = array(); |
2604 | |
2605 | foreach ($THEME->sheets as $sheet) { |
2606 | $files[] = array($CFG->themedir, $themename.'/'.$sheet.'.css'); |
2607 | } |
2608 | |
2609 | if ($themename == 'standard') { // Add any standard styles included in any modules |
2610 | if (!empty($THEME->modsheets)) { // Search for styles.php within activity modules |
2611 | if ($mods = get_list_of_plugins('mod')) { |
2612 | foreach ($mods as $mod) { |
2613 | if (file_exists($CFG->dirroot.'/mod/'.$mod.'/styles.php')) { |
2614 | $files[] = array($CFG->dirroot, '/mod/'.$mod.'/styles.php'); |
2615 | } |
08396bb2 |
2616 | } |
2617 | } |
2618 | } |
a2b3f884 |
2619 | |
6ba172fb |
2620 | if (!empty($THEME->blocksheets)) { // Search for styles.php within block modules |
2621 | if ($mods = get_list_of_plugins('blocks')) { |
2622 | foreach ($mods as $mod) { |
2623 | if (file_exists($CFG->dirroot.'/blocks/'.$mod.'/styles.php')) { |
2624 | $files[] = array($CFG->dirroot, '/blocks/'.$mod.'/styles.php'); |
2625 | } |
08396bb2 |
2626 | } |
2627 | } |
2628 | } |
a2b3f884 |
2629 | |
ae628043 |
2630 | if (!isset($THEME->courseformatsheets) || $THEME->courseformatsheets) { // Search for styles.php in course formats |
2631 | if ($mods = get_list_of_plugins('format','',$CFG->dirroot.'/course')) { |
2632 | foreach ($mods as $mod) { |
2633 | if (file_exists($CFG->dirroot.'/course/format/'.$mod.'/styles.php')) { |
2634 | $files[] = array($CFG->dirroot, '/course/format/'.$mod.'/styles.php'); |
2635 | } |
2636 | } |
2637 | } |
2638 | } |
2639 | |
6ba172fb |
2640 | if (!empty($THEME->langsheets)) { // Search for styles.php within the current language |
6ba172fb |
2641 | if (file_exists($CFG->dirroot.'/lang/'.$lang.'/styles.php')) { |
2642 | $files[] = array($CFG->dirroot, '/lang/'.$lang.'/styles.php'); |
2643 | } |
2644 | } |
08396bb2 |
2645 | } |
2646 | |
6ba172fb |
2647 | |
2648 | if ($files) { |
2649 | /// Produce a list of all the files first |
2650 | echo '/**************************************'."\n"; |
2651 | echo ' * THEME NAME: '.$themename."\n *\n"; |
2652 | echo ' * Files included in this sheet:'."\n *\n"; |
2653 | foreach ($files as $file) { |
2654 | echo ' * '.$file[1]."\n"; |
08396bb2 |
2655 | } |
6ba172fb |
2656 | echo ' **************************************/'."\n\n"; |
08396bb2 |
2657 | |
6ba172fb |
2658 | |
16f0af59 |
2659 | /// check if csscobstants is set |
2660 | if (!empty($THEME->cssconstants)) { |
2661 | require_once("$CFG->libdir/cssconstants.php"); |
2662 | /// Actually collect all the files in order. |
2663 | $css = ''; |
2664 | foreach ($files as $file) { |
2665 | $css .= '/***** '.$file[1].' start *****/'."\n\n"; |
2666 | $css .= file_get_contents($file[0].'/'.$file[1]); |
2667 | $ccs .= '/***** '.$file[1].' end *****/'."\n\n"; |
2668 | } |
2669 | /// replace css_constants with their values |
2670 | echo replace_cssconstants($css); |
2671 | } else { |
2672 | /// Actually output all the files in order. |
2673 | foreach ($files as $file) { |
2674 | echo '/***** '.$file[1].' start *****/'."\n\n"; |
2675 | @include_once($file[0].'/'.$file[1]); |
2676 | echo '/***** '.$file[1].' end *****/'."\n\n"; |
2677 | } |
6ba172fb |
2678 | } |
9c4e6e21 |
2679 | } |
2680 | |
a44091bf |
2681 | return $CFG->themewww.'/'.$themename; // Only to help old themes (1.4 and earlier) |
6535be85 |
2682 | } |
2683 | |
9c4e6e21 |
2684 | |
2685 | function theme_setup($theme = '', $params=NULL) { |
d74d4f20 |
2686 | /// Sets up global variables related to themes |
2687 | |
9152fc99 |
2688 | global $CFG, $THEME, $SESSION, $USER, $HTTPSPAGEREQUIRED; |
d74d4f20 |
2689 | |
2690 | if (empty($theme)) { |
2691 | $theme = current_theme(); |
2692 | } |
9c4e6e21 |
2693 | |
09ad59dc |
2694 | /// If the theme doesn't exist for some reason then revert to standardwhite |
a44091bf |
2695 | if (!file_exists($CFG->themedir .'/'. $theme .'/config.php')) { |
09ad59dc |
2696 | $CFG->theme = $theme = 'standardwhite'; |
2697 | } |
2698 | |
55b734fb |
2699 | /// Load up the theme config |
2700 | $THEME = NULL; // Just to be sure |
a44091bf |
2701 | include($CFG->themedir .'/'. $theme .'/config.php'); // Main config for current theme |
55b734fb |
2702 | |
9c4e6e21 |
2703 | /// Put together the parameters |
2704 | if (!$params) { |
2705 | $params = array(); |
2706 | } |
56766f85 |
2707 | if ($theme != $CFG->theme) { |
2708 | $params[] = 'forceconfig='.$theme; |
9c4e6e21 |
2709 | } |
55b734fb |
2710 | |
2711 | /// Force language too if required |
2712 | if (!empty($THEME->langsheets)) { |
2713 | $params[] = 'lang='.current_language(); |
2714 | } |
2715 | |
2716 | /// Convert params to string |
9c4e6e21 |
2717 | if ($params) { |
55b734fb |
2718 | $paramstring = '?'.implode('&', $params); |
d74d4f20 |
2719 | } else { |
9c4e6e21 |
2720 | $paramstring = ''; |
d74d4f20 |
2721 | } |
2722 | |
9c4e6e21 |
2723 | /// Set up image paths |
24c66531 |
2724 | if(isset($CFG->smartpix) && $CFG->smartpix==1) { |
2725 | if($CFG->slasharguments) { // Use this method if possible for better caching |
2726 | $extra=''; |
2727 | } else { |
2728 | $extra='?file='; |
2729 | } |
2730 | |
2731 | $CFG->pixpath = $CFG->wwwroot. '/pix/smartpix.php'.$extra.'/'.$theme; |
2732 | $CFG->modpixpath = $CFG->wwwroot .'/pix/smartpix.php'.$extra.'/'.$theme.'/mod'; |
2733 | } else if (empty($THEME->custompix)) { // Could be set in the above file |
d74d4f20 |
2734 | $CFG->pixpath = $CFG->wwwroot .'/pix'; |
2735 | $CFG->modpixpath = $CFG->wwwroot .'/mod'; |
2736 | } else { |
a44091bf |
2737 | $CFG->pixpath = $CFG->themewww .'/'. $theme .'/pix'; |
2738 | $CFG->modpixpath = $CFG->themewww .'/'. $theme .'/pix/mod'; |
d74d4f20 |
2739 | } |
2740 | |
9c4e6e21 |
2741 | /// Header and footer paths |
a44091bf |
2742 | $CFG->header = $CFG->themedir .'/'. $theme .'/header.html'; |
2743 | $CFG->footer = $CFG->themedir .'/'. $theme .'/footer.html'; |
d74d4f20 |
2744 | |
9c4e6e21 |
2745 | /// Define stylesheet loading order |
d74d4f20 |
2746 | $CFG->stylesheets = array(); |
2747 | if ($theme != 'standard') { /// The standard sheet is always loaded first |
a44091bf |
2748 | $CFG->stylesheets[] = $CFG->themewww.'/standard/styles.php'.$paramstring; |
d74d4f20 |
2749 | } |
2750 | if (!empty($THEME->parent)) { /// Parent stylesheets are loaded next |
a44091bf |
2751 | $CFG->stylesheets[] = $CFG->themewww.'/'.$THEME->parent.'/styles.php'.$paramstring; |
d74d4f20 |
2752 | } |
a44091bf |
2753 | $CFG->stylesheets[] = $CFG->themewww.'/'.$theme.'/styles.php'.$paramstring; |
d74d4f20 |
2754 | |
9152fc99 |
2755 | /// We have to change some URLs in styles if we are in a $HTTPSPAGEREQUIRED page |
2756 | if (!empty($HTTPSPAGEREQUIRED)) { |
2757 | $CFG->themewww = str_replace('http:', 'https:', $CFG->themewww); |
2758 | $CFG->pixpath = str_replace('http:', 'https:', $CFG->pixpath); |
2759 | $CFG->modpixpath = str_replace('http:', 'https:', $CFG->modpixpath); |
2760 | foreach ($CFG->stylesheets as $key => $stylesheet) { |
2761 | $CFG->stylesheets[$key] = str_replace('http:', 'https:', $stylesheet); |
2762 | } |
2763 | } |
2764 | |
d74d4f20 |
2765 | } |
2766 | |
c3f55692 |
2767 | |
d48b00b4 |
2768 | /** |
2769 | * Returns text to be displayed to the user which reflects their login status |
2770 | * |
2771 | * @uses $CFG |
2772 | * @uses $USER |
89dcb99d |
2773 | * @param course $course {@link $COURSE} object containing course information |
2774 | * @param user $user {@link $USER} object containing user information |
2775 | * @return string |
d48b00b4 |
2776 | */ |
c44d5d42 |
2777 | function user_login_string($course=NULL, $user=NULL) { |
2778 | global $USER, $CFG, $SITE; |
a282d0ff |
2779 | |
86a1ba04 |
2780 | if (empty($user) and !empty($USER->id)) { |
a282d0ff |
2781 | $user = $USER; |
2782 | } |
2783 | |
c44d5d42 |
2784 | if (empty($course)) { |
2785 | $course = $SITE; |
2786 | } |
2787 | |
f6f66b03 |
2788 | if (!empty($user->realuser)) { |
b0ccd3fb |
2789 | if ($realuser = get_record('user', 'id', $user->realuser)) { |
2d71e8ee |
2790 | $fullname = fullname($realuser, true); |
fa738731 |
2791 | $realuserinfo = " [<a $CFG->frametarget |
f6f66b03 |
2792 | href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&return=1&sesskey=".sesskey()."\">$fullname</a>] "; |
9fa49e22 |
2793 | } |
9d378732 |
2794 | } else { |
b0ccd3fb |
2795 | $realuserinfo = ''; |
9fa49e22 |
2796 | } |
2797 | |
87180677 |
2798 | if (empty($CFG->loginhttps)) { |
2799 | $wwwroot = $CFG->wwwroot; |
2800 | } else { |
2c3432e6 |
2801 | $wwwroot = str_replace('http:','https:',$CFG->wwwroot); |
87180677 |
2802 | } |
2803 | |
8743f4f3 |
2804 | if (empty($course->id)) { |
2805 | // $course->id is not defined during installation |
2806 | return ''; |
f6f66b03 |
2807 | } else if (!empty($user->id)) { |
2d07587b |
2808 | $context = get_context_instance(CONTEXT_COURSE, $course->id); |
2809 | |
2d71e8ee |
2810 | $fullname = fullname($user, true); |
fa738731 |
2811 | $username = "<a $CFG->frametarget href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$fullname</a>"; |
03d820c7 |
2812 | if (is_mnet_remote_user($user) and $idprovider = get_record('mnet_host', 'id', $user->mnethostid)) { |
fa738731 |
2813 | $username .= " from <a $CFG->frametarget href=\"{$idprovider->wwwroot}\">{$idprovider->name}</a>"; |
03d820c7 |
2814 | } |
8f8ed475 |
2815 | if (isset($user->username) && $user->username == 'guest') { |
158de846 |
2816 | $loggedinas = $realuserinfo.get_string('loggedinasguest'). |
fa738731 |
2817 | " (<a $CFG->frametarget href=\"$wwwroot/login/index.php\">".get_string('login').'</a>)'; |
2d07587b |
2818 | } else if (!empty($user->switchrole[$context->id])) { |
91ea292e |
2819 | $rolename = ''; |
2d07587b |
2820 | if ($role = get_record('role', 'id', $user->switchrole[$context->id])) { |
91ea292e |
2821 | $rolename = ': '.format_string($role->name); |
2822 | } |
2823 | $loggedinas = get_string('loggedinas', 'moodle', $username).$rolename. |
fa738731 |
2824 | " (<a $CFG->frametarget |
91ea292e |
2825 | href=\"$CFG->wwwroot/course/view.php?id=$course->id&switchrole=0&sesskey=".sesskey()."\">".get_string('switchrolereturn').'</a>)'; |
0ae7e6f4 |
2826 | } else { |
d5a01638 |
2827 | $loggedinas = $realuserinfo.get_string('loggedinas', 'moodle', $username).' '. |
18b07efa |
2828 | " (<a $CFG->frametarget href=\"$CFG->wwwroot/login/logout.php?sesskey=".sesskey()."\">".get_string('logout').'</a>)'; |
0ae7e6f4 |
2829 | } |
9fa49e22 |
2830 | } else { |
b0ccd3fb |
2831 | $loggedinas = get_string('loggedinnot', 'moodle'). |
fa738731 |
2832 | " (<a $CFG->frametarget href=\"$wwwroot/login/index.php\">".get_string('login').'</a>)'; |
9fa49e22 |
2833 | } |
d3593a67 |
2834 | return '<div class="logininfo">'.$loggedinas.'</div>'; |
9fa49e22 |
2835 | } |
2836 | |
d48b00b4 |
2837 | /** |
57100a90 |
2838 | * Tests whether $THEME->rarrow, $THEME->larrow have been set (theme/-/config.php). |
2839 | * If not it applies sensible defaults. |
d48b00b4 |
2840 | * |
57100a90 |
2841 | * Accessibility: right and left arrow Unicode characters for breadcrumb, calendar, |
5ce73257 |
2842 | * search forum block, etc. Important: these are 'silent' in a screen-reader |
57100a90 |
2843 | * (unlike > »), and must be accompanied by text. |
2844 | * @uses $THEME |
d48b00b4 |
2845 | */ |
57100a90 |
2846 | function check_theme_arrows() { |
2847 | global $THEME; |
5ce73257 |
2848 | |
57100a90 |
2849 | if (!isset($THEME->rarrow) and !isset($THEME->larrow)) { |
ce27e6f4 |
2850 | // Default, looks good in Win XP/IE 6, Win/Firefox 1.5, Win/Netscape 8... |
2851 | // Also OK in Win 9x/2K/IE 5.x |
57100a90 |
2852 | $THEME->rarrow = '►'; |
2853 | $THEME->larrow = '◄'; |
ce27e6f4 |
2854 | $uagent = $_SERVER['HTTP_USER_AGENT']; |
2855 | if (false !== strpos($uagent, 'Opera') |
2856 | || false !== strpos($uagent, 'Mac')) { |
2857 | // Looks good in Win XP/Mac/Opera 8/9, Mac/Firefox 2, Camino, Safari. |
2858 | // Not broken in Mac/IE 5, Mac/Netscape 7 (?). |
57100a90 |
2859 | $THEME->rarrow = '▶'; |
2860 | $THEME->larrow = '◀'; |
ce27e6f4 |
2861 | } |
2862 | elseif (false !== strpos($uagent, 'Konqueror')) { |
2863 | $THEME->rarrow = '→'; |
2864 | $THEME->larrow = '←'; |
2865 | } |
2866 | elseif (isset($_SERVER['HTTP_ACCEPT_CHARSET']) |
2867 | && false === stripos($_SERVER['HTTP_ACCEPT_CHARSET'], 'utf-8')) { |
2868 | // (Win/IE 5 doesn't set ACCEPT_CHARSET, but handles Unicode.) |
2869 | // To be safe, non-Unicode browsers! |
2870 | $THEME->rarrow = '>'; |
2871 | $THEME->larrow = '<'; |
2872 | } |
57100a90 |
2873 | } |
2874 | } |
da4124be |
2875 | |
57100a90 |
2876 | /** |
2877 | * Prints breadcrumb trail of links, called in theme/-/header.html |
2878 | * |
2879 | * @uses $CFG |
e683263f |
2880 | * @param mixed $navigation The breadcrumb navigation string to be printed |
57100a90 |
2881 | * @param string $separator The breadcrumb trail separator. The default 0 leads to the use |
2882 | * of $THEME->rarrow, themes could use '→', '/', or '' for a style-sheet solution. |
2883 | * @param boolean $return False to echo the breadcrumb string (default), true to return it. |
2884 | */ |
2885 | function print_navigation ($navigation, $separator=0, $return=false) { |
2886 | global $CFG, $THEME; |
da4124be |
2887 | $output = ''; |
5ce73257 |
2888 | |
57100a90 |
2889 | check_theme_arrows(); |
2890 | if (0 === $separator) { |
2891 | $separator = $THEME->rarrow; |
2892 | } |
2893 | if (!empty($separator)) { |
2894 | $separator = '<span class="sep">'. $separator .'</span>'; |
2895 | } |
da4124be |
2896 | |
2897 | if ($navigation) { |
ac4feb59 |
2898 | |
2899 | if (is_newnav($navigation)) { |
2900 | if ($return) { |
2901 | return($navigation['breadcrumbs']); |
2902 | } else { |
2903 | echo $navigation['breadcrumbs']; |
2904 | return; |
2905 | } |
2906 | } else { |
2907 | debugging('Navigation needs to be updated to use build_navigation()', DEBUG_DEVELOPER); |
2908 | } |
6ba65fa0 |
2909 | |
2910 | if (!is_array($navigation)) { |
2911 | $ar = explode('->', $navigation); |
2912 | $navigation = array(); |
2913 | |
2914 | foreach ($ar as $a) { |
2915 | if (strpos($a, '</a>') === false) { |
e683263f |
2916 | $navigation[] = array('title' => $a, 'url' => ''); |
6ba65fa0 |
2917 | } else { |
2918 | if (preg_match('/<a.*href="([^"]*)">(.*)<\/a>/', $a, $matches)) { |
e683263f |
2919 | $navigation[] = array('title' => $matches[2], 'url' => $matches[1]); |
6ba65fa0 |
2920 | } |
2921 | } |
2922 | } |
2923 | } |
2924 | |
da4124be |
2925 | if (! $site = get_site()) { |
6ba65fa0 |
2926 | $site = new object(); |
da4124be |
2927 | $site->shortname = get_string('home'); |
2928 | } |
6ba65fa0 |
2929 | |
2930 | //Accessibility: breadcrumb links now in a list, » replaced with a 'silent' character. |
2931 | $nav_text = get_string('youarehere','access'); |
2932 | $output .= '<h2 class="accesshide">'.$nav_text."</h2><ul>\n"; |
2933 | |
4d0ccfa7 |
2934 | $output .= '<li class="first">'."\n".'<a '.$CFG->frametarget.' onclick="this.target=\''.$CFG->framename.'\'" href="' |
e683263f |
2935 | .$CFG->wwwroot.((!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)) |
6ba65fa0 |
2936 | && !empty($USER->id) && !empty($CFG->mymoodleredirect) && !isguest()) |
4d0ccfa7 |
2937 | ? '/my' : '') .'/">'. format_string($site->shortname) ."</a>\n</li>\n"; |
6ba65fa0 |
2938 | |
2939 | |
c3d327b4 |
2940 | foreach ($navigation as $navitem) { |
e683263f |
2941 | $title = trim(strip_tags(format_string($navitem['title'], false))); |
2942 | $url = $navitem['url']; |
2943 | |
6ba65fa0 |
2944 | if (empty($url)) { |
2945 | $output .= '<li class="first">'."$separator $title</li>\n"; |
2946 | } else { |
4d0ccfa7 |
2947 | $output .= '<li class="first">'."$separator\n<a ".$CFG->frametarget.' onclick="this.target=\''.$CFG->framename.'\'" href="' |
2948 | .$url.'">'."$title</a>\n</li>\n"; |
6ba65fa0 |
2949 | } |
2950 | } |
2951 | |
5ce73257 |
2952 | $output .= "</ul>\n"; |
da4124be |
2953 | } |
2954 | |
2955 | if ($return) { |
2956 | return $output; |
2957 | } else { |
2958 | echo $output; |
2959 | } |
9fa49e22 |
2960 | } |
2961 | |
d48b00b4 |
2962 | /** |
bfca0094 |
2963 | * Prints a string in a specified size (retained for backward compatibility) |
d48b00b4 |
2964 | * |
89dcb99d |
2965 | * @param string $text The text to be displayed |
2966 | * @param int $size The size to set the font for text display. |
d48b00b4 |
2967 | */ |
da4124be |
2968 | function print_headline($text, $size=2, $return=false) { |
593bc22b |
2969 | $output = print_heading($text, '', $size, true); |
da4124be |
2970 | if ($return) { |
2971 | return $output; |
2972 | } else { |
2973 | echo $output; |
2974 | } |
d4df9200 |
2975 | } |
2976 | |
d48b00b4 |
2977 | /** |
2978 | * Prints text in a format for use in headings. |
2979 | * |
89dcb99d |
2980 | * @param string $text The text to be displayed |
2981 | * @param string $align The alignment of the printed paragraph of text |
2982 | * @param int $size The size to set the font for text display. |
d48b00b4 |
2983 | */ |
da4124be |
2984 | function print_heading($text, $align='', $size=2, $class='main', $return=false) { |
bfca0094 |
2985 | if ($align) { |
fc9cb24a |
2986 | $align = ' style="text-align:'.$align.';"'; |
bfca0094 |
2987 | } |
4419aa80 |
2988 | if ($class) { |
2989 | $class = ' class="'.$class.'"'; |
2990 | } |
da4124be |
2991 | $output = "<h$size $align $class>".stripslashes_safe($text)."</h$size>"; |
2992 | |
2993 | if ($return) { |
2994 | return $output; |
2995 | } else { |
2996 | echo $output; |
2997 | } |
9fa49e22 |
2998 | } |
2999 | |
d48b00b4 |
3000 | /** |
3001 | * Centered heading with attached help button (same title text) |
3002 | * and optional icon attached |
3003 | * |
89dcb99d |
3004 | * @param string $text The text to be displayed |
3005 | * @param string $helppage The help page to link to |
3006 | * @param string $module The module whose help should be linked to |
3007 | * @param string $icon Image to display if needed |
d48b00b4 |
3008 | */ |
da4124be |
3009 | function print_heading_with_help($text, $helppage, $module='moodle', $icon='', $return=false) { |
3010 | $output = ''; |
3011 | $output .= '<h2 class="main help">'.$icon.stripslashes_safe($text); |
3012 | $output .= helpbutton($helppage, $text, $module, true, false, '', true); |
3013 | $output .= '</h2>'; |
3014 | |
3015 | if ($return) { |
3016 | return $output; |
3017 | } else { |
3018 | echo $output; |
3019 | } |
9fa49e22 |
3020 | } |
ab9f24ad |
3021 | |
6ee8277f |
3022 | |
da4124be |
3023 | function print_heading_block($heading, $class='', $return=false) { |
b6f30b18 |
3024 | //Accessibility: 'headingblock' is now H1, see theme/standard/styles_*.css: ?? |
da4124be |
3025 | $output = '<h2 class="headingblock header '.$class.'">'.stripslashes($heading).'</h2>'; |
3026 | |
3027 | if ($return) { |
3028 | return $output; |
3029 | } else { |
3030 | echo $output; |
3031 | } |
6ee8277f |
3032 | } |
3033 | |
3034 | |
d48b00b4 |
3035 | /** |
3036 | * Print a link to continue on to another page. |
3037 | * |
3038 | * @uses $CFG |
3039 | * @param string $link The url to create a link to. |
3040 | */ |
da4124be |
3041 | function print_continue($link, $return=false) { |
9fa49e22 |
3042 | |
51a96819 |
3043 | global $CFG; |
3044 | |
dedb2304 |
3045 | // in case we are logging upgrade in admin/index.php stop it |
3046 | if (function_exists('upgrade_log_finish')) { |
3047 | upgrade_log_finish(); |
3048 | } |
3049 | |
da4124be |
3050 | $output = ''; |
3051 | |
e1968a8e |
3052 | if ($link == '') { |
3053 | if (!empty($_SERVER['HTTP_REFERER'])) { |
3054 | $link = $_SERVER['HTTP_REFERER']; |
3055 | $link = str_replace('&', '&', $link); // make it valid XHTML |
3056 | } else { |
3057 | $link = $CFG->wwwroot .'/'; |
3058 | } |
9fa49e22 |
3059 | } |
3060 | |
da4124be |
3061 | $output .= '<div class="continuebutton">'; |
c35e8b1c |
3062 | |
da4124be |
3063 | $output .= print_single_button($link, NULL, get_string('continue'), 'post', $CFG->framename, true); |
3064 | $output .= '</div>'."\n"; |
3065 | |
3066 | if ($return) { |
3067 | return $output; |
3068 | } else { |
3069 | echo $output; |
3070 | } |
9fa49e22 |
3071 | } |
3072 | |
2123b644 |
3073 | |
d48b00b4 |
3074 | /** |
3075 | * Print a message in a standard themed box. |
2123b644 |
3076 | * Replaces print_simple_box (see deprecatedlib.php) |
d48b00b4 |
3077 | * |
2123b644 |
3078 | * @param string $message, the content of the box |
f79aa892 |
3079 | * @param string $classes, space-separated class names. |
3080 | * @param string $ids, space-separated id names. |
2123b644 |
3081 | * @param boolean $return, return as string or just print it |
d48b00b4 |
3082 | */ |
f79aa892 |
3083 | function print_box($message, $classes='generalbox', $ids='', $return=false) { |
2123b644 |
3084 | |
f79aa892 |
3085 | $output = print_box_start($classes, $ids, true); |
da4124be |
3086 | $output .= stripslashes_safe($message); |
2123b644 |
3087 | $output .= print_box_end(true); |
da4124be |
3088 | |
3089 | if ($return) { |
3090 | return $output; |
3091 | } else { |
3092 | echo $output; |
3093 | } |
9fa49e22 |
3094 | } |
3095 | |
e1968a8e |
3096 | /** |
2123b644 |
3097 | * Starts a box using divs |
3098 | * Replaces print_simple_box_start (see deprecatedlib.php) |
408b65dd |
3099 | * |
f79aa892 |
3100 | * @param string $classes, space-separated class names. |
3101 | * @param string $ids, space-separated id names. |
2123b644 |
3102 | * @param boolean $return, return as string or just print it |
d48b00b4 |
3103 | */ |
f79aa892 |
3104 | function print_box_start($classes='generalbox', $ids='', $return=false) { |
da4124be |
3105 | $output = ''; |
9fa49e22 |
3106 | |
f79aa892 |
3107 | if ($ids) { |
3108 | $ids = ' id="'.$ids.'"'; |
30c9e694 |
3109 | } |
593bc22b |
3110 | |
8f36e33e |
3111 | $output .= '<div'.$ids.' class="box '.$classes.'">'; |
da4124be |
3112 | |
3113 | if ($return) { |
3114 | return $output; |
3115 | } else { |
3116 | echo $output; |
3117 | } |
9fa49e22 |
3118 | } |
3119 | |
2123b644 |
3120 | |
e1968a8e |
3121 | /** |
2123b644 |
3122 | * Simple function to end a box (see above) |
3123 | * Replaces print_simple_box_end (see deprecatedlib.php) |
3124 | * |
3125 | * @param boolean $return, return as string or just print it |
d48b00b4 |
3126 | */ |
2123b644 |
3127 | function print_box_end($return=false) { |
df26dd34 |
3128 | $output = '</div>'; |
da4124be |
3129 | if ($return) { |
3130 | return $output; |
3131 | } else { |
3132 | echo $output; |
3133 | } |
9fa49e22 |
3134 | } |
3135 | |
30c9e694 |
3136 | |
d48b00b4 |
3137 | /** |
3138 | * Print a self contained form with a single submit button. |
3139 | * |
89dcb99d |
3140 | * @param string $link ? |
3141 | * @param array $options ? |
3142 | * @param string $label ? |
3143 | * @param string $method ? |
d48b00b4 |
3144 | * @todo Finish documenting this function |
3145 | */ |
da4124be |
3146 | function print_single_button($link, $options, $label='OK', $method='get', $target='_self', $return=false) { |
3147 | $output = ''; |
2463ef81 |
3148 | $link = str_replace('"', '"', $link); //basic XSS protection |
da4124be |
3149 | $output .= '<div class="singlebutton">'; |
64888fc1 |
3150 | // taking target out, will need to add later target="'.$target.'" |
2463ef81 |
3151 | $output .= '<form action="'. $link .'" method="'. $method .'">'; |
6ebb7659 |
3152 | $output .= '<div>'; |
9fa49e22 |
3153 | if ($options) { |
3154 | foreach ($options as $name => $value) { |
e1968a8e |
3155 | $output .= '<input type="hidden" name="'. $name .'" value="'. s($value) .'" />'; |
9fa49e22 |
3156 | } |
3157 | } |
c03ebabe |
3158 | $output .= '<input type="submit" value="'. s($label) .'" /></div></form></div>'; |
da4124be |
3159 | |
3160 | if ($return) { |
3161 | return $output; |
3162 | } else { |
3163 | echo $output; |
3164 | } |
9fa49e22 |
3165 | } |
3166 | |
2123b644 |
3167 | |
d48b00b4 |
3168 | /** |
3169 | * Print a spacer image with the option of including a line break. |
3170 | * |
89dcb99d |
3171 | * @param int $height ? |
3172 | * @param int $width ? |
3173 | * @param boolean $br ? |
d48b00b4 |
3174 | * @todo Finish documenting this function |
3175 | */ |
da4124be |
3176 | function print_spacer($height=1, $width=1, $br=true, $return=false) { |
9fa49e22 |
3177 | global $CFG; |
da4124be |
3178 | $output = ''; |
3179 | |
3180 | $output .= '<img class="spacer" height="'. $height .'" width="'. $width .'" src="'. $CFG->wwwroot .'/pix/spacer.gif" alt="" />'; |
9fa49e22 |
3181 | if ($br) { |
da4124be |
3182 | $output .= '<br />'."\n"; |
3183 | } |
3184 | |
3185 | if ($return) { |
3186 | return $output; |
3187 | } else { |
3188 | echo $output; |
9fa49e22 |
3189 | } |
3190 | } |
3191 | |
d48b00b4 |
3192 | /** |
3193 | * Given the path to a picture file in a course, or a URL, |
3194 | * this function includes the picture in the page. |
3195 | * |
89dcb99d |
3196 | * @param string $path ? |
3197 | * @param int $courseid ? |
3198 | * @param int $height ? |
3199 | * @param int $width ? |
3200 | * @param string $link ? |
d48b00b4 |
3201 | * @todo Finish documenting this function |
3202 | */ |
da4124be |
3203 | function print_file_picture($path, $courseid=0, $height='', $width='', $link='', $return=false) { |
9fa49e22 |
3204 | global $CFG; |
da4124be |
3205 | $output = ''; |
9fa49e22 |
3206 | |
3207 | if ($height) { |
b0ccd3fb |
3208 | $height = 'height="'. $height .'"'; |
9fa49e22 |
3209 | } |
3210 | if ($width) { |
b0ccd3fb |
3211 | $width = 'width="'. $width .'"'; |
9fa49e22 |
3212 | } |
3213 | if ($link) { |
da4124be |
3214 | $output .= '<a href="'. $link .'">'; |
9fa49e22 |
3215 | } |
b0ccd3fb |
3216 | if (substr(strtolower($path), 0, 7) == 'http://') { |
7cd2f598 |
3217 | $output .= '<img style="height:'.$height.'px;width:'.$width.'px;" src="'. $path .'" />'; |
9fa49e22 |
3218 | |
3219 | } else if ($courseid) { |
7cd2f598 |
3220 | $output .= '<img style="height:'.$height.'px;width:'.$width.'px;" src="'; |
9fa49e22 |
3221 | if ($CFG->slasharguments) { // Use this method if possible for better caching |
da4124be |
3222 | $output .= $CFG->wwwroot .'/file.php/'. $courseid .'/'. $path; |
9fa49e22 |
3223 | } else { |
da4124be |
3224 | $output .= $CFG->wwwroot .'/file.php?file=/'. $courseid .'/'. $path; |
9fa49e22 |
3225 | } |
da4124be |
3226 | $output .= '" />'; |
9fa49e22 |
3227 | } else { |
da4124be |
3228 | $output .= 'Error: must pass URL or course'; |
9fa49e22 |
3229 | } |
3230 | if ($link) { |
da4124be |
3231 | $output .= '</a>'; |
3232 | } |
3233 | |
3234 | if ($return) { |
3235 | return $output; |
3236 | } else { |
3237 | echo $output; |
9fa49e22 |
3238 | } |
3239 | } |
3240 | |
d48b00b4 |
3241 | /** |
3242 | * Print the specified user's avatar. |
3243 | * |
89dcb99d |
3244 | * @param int $userid ? |
3245 | * @param int $courseid ? |
3246 | * @param boolean $picture Print the user picture? |
4c6c0e01 |
3247 | * @param int $size Size in pixels. Special values are (true/1 = 100px) and (false/0 = 35px) for backward compatability |
da4124be |
3248 | * @param boolean $return If false print picture to current page, otherwise return the output as string |
89dcb99d |
3249 | * @param boolean $link Enclose printed image in a link to view specified course? |
953eb6f3 |
3250 | * @param string $target link target attribute |
3251 | * @param boolean $alttext use username or userspecified text in image alt attribute |
89dcb99d |
3252 | * return string |
d48b00b4 |
3253 | * @todo Finish documenting this function |
3254 | */ |
953eb6f3 |
3255 | function print_user_picture($userid, $courseid, $picture, $size=0, $return=false, $link=true, $target='', $alttext=true) { |
f374fb10 |
3256 | global $CFG; |
9fa49e22 |
3257 | |
3258 | if ($link) { |
f66e1977 |
3259 | if ($target) { |
3260 | $target=' target="_blank"'; |
3261 | } |
3262 | $output = '<a '.$target.' href="'. $CFG->wwwroot .'/user/view.php?id='. $userid .'&course='. $courseid .'">'; |
9fa49e22 |
3263 | } else { |
b0ccd3fb |
3264 | $output = ''; |
9fa49e22 |
3265 | } |
4c6c0e01 |
3266 | if (empty($size)) { |
3267 | $file = 'f2'; |
da7a785b |
3268 | $size = 35; |
4c6c0e01 |
3269 | } else if ($size === true or $size == 1) { |
b0ccd3fb |
3270 | $file = 'f1'; |
9fa49e22 |
3271 | $size = 100; |
4c6c0e01 |
3272 | } else if ($size >= 50) { |
3273 | $file = 'f1'; |
9fa49e22 |
3274 | } else { |
b0ccd3fb |
3275 | $file = 'f2'; |
9fa49e22 |
3276 | } |
113a79e2 |
3277 | $class = "userpicture"; |
67a63a30 |
3278 | if ($picture) { // Print custom user picture |
9fa49e22 |
3279 | if ($CFG->slasharguments) { // Use this method if possible for better caching |
c1138797 |
3280 | $src = $CFG->wwwroot .'/user/pix.php/'. $userid .'/'. $file .'.jpg'; |
9fa49e22 |
3281 | } else { |
c1138797 |
3282 | $src = $CFG->wwwroot .'/user/pix.php?file=/'. $userid .'/'. $file .'.jpg'; |
9fa49e22 |
|