Changes to improve robustness of uploads, and make things compatible
[moodle.git] / lib / weblib.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
9fa49e22 3///////////////////////////////////////////////////////////////////////////
4// weblib.php - functions for web output
f9903ed0 5//
9fa49e22 6// Library of all general-purpose Moodle PHP functions and constants
7// that produce HTML output
f9903ed0 8//
9fa49e22 9///////////////////////////////////////////////////////////////////////////
10// //
11// NOTICE OF COPYRIGHT //
12// //
13// Moodle - Modular Object-Oriented Dynamic Learning Environment //
14// http://moodle.com //
15// //
16// Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com //
17// //
18// This program is free software; you can redistribute it and/or modify //
19// it under the terms of the GNU General Public License as published by //
20// the Free Software Foundation; either version 2 of the License, or //
21// (at your option) any later version. //
22// //
23// This program is distributed in the hope that it will be useful, //
24// but WITHOUT ANY WARRANTY; without even the implied warranty of //
25// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
26// GNU General Public License for more details: //
27// //
28// http://www.gnu.org/copyleft/gpl.html //
29// //
30///////////////////////////////////////////////////////////////////////////
f9903ed0 31
0095d5cd 32/// Constants
33
c1d57101 34/// Define text formatting types ... eventually we can add Wiki, BBcode etc
0095d5cd 35define("FORMAT_MOODLE", "0");
36define("FORMAT_HTML", "1");
37
c9dda990 38$SMILEY_TEXT[] = ":-)";
39$SMILEY_IMAGE[] = "<IMG ALT=\":-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/smiley.gif\">";
40$SMILEY_TEXT[] = ":)";
41$SMILEY_IMAGE[] = "<IMG ALT=\":-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/smiley.gif\">";
42$SMILEY_TEXT[] = ":-D";
43$SMILEY_IMAGE[] = "<IMG ALT=\":-D\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/biggrin.gif\">";
44$SMILEY_TEXT[] = ";-)";
45$SMILEY_IMAGE[] = "<IMG ALT=\";-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/wink.gif\">";
46$SMILEY_TEXT[] = ":-/";
47$SMILEY_IMAGE[] = "<IMG ALT=\":-/\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/mixed.gif\">";
48$SMILEY_TEXT[] = "V-.";
49$SMILEY_IMAGE[] = "<IMG ALT=\"V-.\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/thoughtful.gif\">";
50$SMILEY_TEXT[] = ":-P";
51$SMILEY_IMAGE[] = "<IMG ALT=\":-P\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/tongueout.gif\">";
52$SMILEY_TEXT[] = "B-)";
53$SMILEY_IMAGE[] = "<IMG ALT=\"B-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/cool.gif\">";
54$SMILEY_TEXT[] = "^-)";
55$SMILEY_IMAGE[] = "<IMG ALT=\"^-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/approve.gif\">";
56$SMILEY_TEXT[] = "8-)";
57$SMILEY_IMAGE[] = "<IMG ALT=\"8-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/wideeyes.gif\">";
58$SMILEY_TEXT[] = ":o)";
59$SMILEY_IMAGE[] = "<IMG ALT=\":o)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/clown.gif\">";
60$SMILEY_TEXT[] = ":-(";
61$SMILEY_IMAGE[] = "<IMG ALT=\":-(\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/sad.gif\">";
62$SMILEY_TEXT[] = ":(";
63$SMILEY_IMAGE[] = "<IMG ALT=\":-(\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/sad.gif\">";
64$SMILEY_TEXT[] = "8-.";
65$SMILEY_IMAGE[] = "<IMG ALT=\"8-.\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/shy.gif\">";
66$SMILEY_TEXT[] = ":-I";
67$SMILEY_IMAGE[] = "<IMG ALT=\":-I\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/blush.gif\">";
68$SMILEY_TEXT[] = ":-X";
69$SMILEY_IMAGE[] = "<IMG ALT=\":-X\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/kiss.gif\">";
70$SMILEY_TEXT[] = "8-o";
71$SMILEY_IMAGE[] = "<IMG ALT=\"8-o\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/surprise.gif\">";
72$SMILEY_TEXT[] = "P-|";
73$SMILEY_IMAGE[] = "<IMG ALT=\"P-|\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/blackeye.gif\">";
74$SMILEY_TEXT[] = "8-[";
75$SMILEY_IMAGE[] = "<IMG ALT=\"8-[\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/angry.gif\">";
76$SMILEY_TEXT[] = "xx-P";
77$SMILEY_IMAGE[] = "<IMG ALT=\"xx-P\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/dead.gif\">";
78$SMILEY_TEXT[] = "|-.";
79$SMILEY_IMAGE[] = "<IMG ALT=\"|-.\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/sleepy.gif\">";
80$SMILEY_TEXT[] = "}-]";
81$SMILEY_IMAGE[] = "<IMG ALT=\"}-]\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/evil.gif\">";
0095d5cd 82
3fe3851d 83$JAVASCRIPT_TAGS = array("javascript:", "onclick=", "ondblclick=", "onkeydown=", "onkeypress=", "onkeyup=",
84 "onmouseover=", "onmouseout=", "onmousedown=", "onmouseup=",
85 "onblur=", "onfocus=", "onload=", "onselect=");
86
6dd38d46 87$ALLOWED_TAGS = "<p><br><b><i><u><font><table><tbody><span><div><tr><td><ol><ul><dl><li><dt><dd><h1><h2><h3><h4><h5><h6><hr><img><a><strong><emphasis><sup><sub><address><cite><blockquote><pre><strike>";
3fe3851d 88
89
0095d5cd 90/// Functions
91
8553b700 92function s($var) {
c1d57101 93/// returns $var with HTML characters (like "<", ">", etc.) properly quoted,
f9903ed0 94
b9b8ab69 95 return htmlSpecialChars(stripslashes($var));
f9903ed0 96}
97
98function p($var) {
c1d57101 99/// prints $var with HTML characters (like "<", ">", etc.) properly quoted,
f9903ed0 100
b9b8ab69 101 echo htmlSpecialChars(stripslashes($var));
f9903ed0 102}
103
8553b700 104function nvl(&$var, $default="") {
c1d57101 105/// if $var is undefined, return $default, otherwise return $var
8553b700 106
107 return isset($var) ? $var : $default;
108}
f9903ed0 109
110function strip_querystring($url) {
c1d57101 111/// takes a URL and returns it without the querystring portion
f9903ed0 112
b9b8ab69 113 if ($commapos = strpos($url, '?')) {
114 return substr($url, 0, $commapos);
115 } else {
116 return $url;
117 }
f9903ed0 118}
119
120function get_referer() {
c1d57101 121/// returns the URL of the HTTP_REFERER, less the querystring portion
f9903ed0 122
7fbd6b1c 123 global $HTTP_REFERER;
c1d57101 124
8553b700 125 return strip_querystring(nvl($HTTP_REFERER));
f9903ed0 126}
127
c1d57101 128
f9903ed0 129function me() {
c1d57101 130/// returns the name of the current script, WITH the querystring portion.
131/// this function is necessary because PHP_SELF and REQUEST_URI and PATH_INFO
132/// return different things depending on a lot of things like your OS, Web
133/// server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.)
f9903ed0 134
7fbd6b1c 135 global $REQUEST_URI, $PATH_INFO, $PHP_SELF;
f9903ed0 136
b9b8ab69 137 if ($REQUEST_URI) {
138 return $REQUEST_URI;
c1d57101 139
b9b8ab69 140 } else if ($PATH_INFO) {
141 return $PATH_INFO;
c1d57101 142
b9b8ab69 143 } else if ($PHP_SELF) {
144 return $PHP_SELF;
c1d57101 145
b9b8ab69 146 } else {
c1d57101 147 notify("Error: Could not find any of these web server variables: \$REQUEST_URI, \$PATH_INFO or \$PHP_SELF");
7fbd6b1c 148 }
f9903ed0 149}
150
151
f9903ed0 152function qualified_me() {
c1d57101 153/// like me() but returns a full URL
f9903ed0 154
c1d57101 155 global $HTTPS, $HTTP_HOST;
156
157 if (!$HTTP_HOST) {
158 notify("Error: could not find web server variable: \$HTTP_HOST");
159 }
f9903ed0 160
b9b8ab69 161 $protocol = (isset($HTTPS) && $HTTPS == "on") ? "https://" : "http://";
162 $url_prefix = "$protocol$HTTP_HOST";
163 return $url_prefix . me();
f9903ed0 164}
165
166
167function match_referer($good_referer = "") {
c1d57101 168/// returns true if the referer is the same as the good_referer. If
87a2fa03 169/// good_referer is not specified, use qualified_me as the good_referer
60f18531 170 global $CFG;
171
172 if ($CFG->buggy_referer) {
173 return true;
174 }
f9903ed0 175
b9b8ab69 176 if ($good_referer == "") {
c1d57101 177 $good_referer = qualified_me();
178 }
b9b8ab69 179 return $good_referer == get_referer();
f9903ed0 180}
181
36b4f985 182function data_submitted($url="") {
183/// Used on most forms in Moodle to check for data
184/// Returns the data as an object, if it's found.
185///
186/// Checks that submitted POST data exists, and also
187/// checks the referer against the given url (it uses
188/// the current page if none was specified.
189
190 global $HTTP_POST_VARS, $CFG;
191
192 if (empty($HTTP_POST_VARS)) {
193 return false;
194 } else {
195 if (match_referer($url)) {
196 return (object)$HTTP_POST_VARS;
197 } else {
198 if ($CFG->debug > 10) {
199 notice("The form did not come from this page! (referer = ".get_referer().")");
200 }
201 return false;
202 }
203 }
204}
205
f9903ed0 206
3fe3851d 207function stri_replace($find, $replace, $string ) {
c1d57101 208/// This does a search and replace, ignoring case
209/// This function is only here because one doesn't exist yet in PHP
210/// Unlike str_replace(), this only works on single values (not arrays)
3fe3851d 211
212 $parts = explode(strtolower($find), strtolower($string));
213
214 $pos = 0;
215
216 foreach ($parts as $key => $part) {
217 $parts[$key] = substr($string, $pos, strlen($part));
218 $pos += strlen($part) + strlen($find);
219 }
220
221 return (join($replace, $parts));
222}
223
f9903ed0 224function read_template($filename, &$var) {
c1d57101 225/// return a (big) string containing the contents of a template file with all
226/// the variables interpolated. all the variables must be in the $var[] array or
227/// object (whatever you decide to use).
228///
229/// WARNING: do not use this on big files!!
f9903ed0 230
b9b8ab69 231 $temp = str_replace("\\", "\\\\", implode(file($filename), ""));
232 $temp = str_replace('"', '\"', $temp);
233 eval("\$template = \"$temp\";");
234 return $template;
f9903ed0 235}
236
237function checked(&$var, $set_value = 1, $unset_value = 0) {
c1d57101 238/// if variable is set, set it to the set_value otherwise set it to the
239/// unset_value. used to handle checkboxes when you are expecting them from
240/// a form
f9903ed0 241
b9b8ab69 242 if (empty($var)) {
243 $var = $unset_value;
244 } else {
245 $var = $set_value;
246 }
f9903ed0 247}
248
249function frmchecked(&$var, $true_value = "checked", $false_value = "") {
c1d57101 250/// prints the word "checked" if a variable is true, otherwise prints nothing,
251/// used for printing the word "checked" in a checkbox form input
f9903ed0 252
b9b8ab69 253 if ($var) {
254 echo $true_value;
255 } else {
256 echo $false_value;
257 }
f9903ed0 258}
259
260
65cf9fc3 261function link_to_popup_window ($url, $name="popup", $linkname="click here", $height=400, $width=500, $title="Popup window") {
c1d57101 262/// This will create a HTML link that will work on both
263/// Javascript and non-javascript browsers.
264/// Relies on the Javascript function openpopup in javascript.php
265/// $url must be relative to home page eg /mod/survey/stuff.php
f9903ed0 266
ff80e012 267 global $CFG;
268
b9b8ab69 269 echo "\n<SCRIPT language=\"Javascript\">";
f9903ed0 270 echo "\n<!--";
b9b8ab69 271 echo "\ndocument.write('<A TITLE=\"$title\" HREF=javascript:openpopup(\"$url\",\"$name\",\"$height\",\"$width\") >$linkname</A>');";
f9903ed0 272 echo "\n//-->";
b9b8ab69 273 echo "\n</SCRIPT>";
274 echo "\n<NOSCRIPT>\n<A TARGET=\"$name\" TITLE=\"$title\" HREF=\"$CFG->wwwroot/$url\">$linkname</A>\n</NOSCRIPT>\n";
f9903ed0 275
276}
277
278function close_window_button() {
c1d57101 279/// Prints a simple button to close a window
280
f9903ed0 281 echo "<FORM><CENTER>";
e5dfd0f3 282 echo "<INPUT TYPE=button onClick=\"self.close();\" VALUE=\"".get_string("closewindow")."\">";
f9903ed0 283 echo "</CENTER></FORM>";
284}
285
286
08056730 287function choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", $nothingvalue="0", $return=false) {
c1d57101 288/// Given an array of value, creates a popup menu to be part of a form
289/// $options["value"]["label"]
f9903ed0 290
618b22c5 291 if ($nothing == "choose") {
292 $nothing = get_string("choose")."...";
293 }
294
f9903ed0 295 if ($script) {
296 $javascript = "onChange=\"$script\"";
9c9f7d77 297 } else {
298 $javascript = "";
f9903ed0 299 }
9c9f7d77 300
08056730 301 $output = "<SELECT NAME=$name $javascript>\n";
bda8d43a 302 if ($nothing) {
08056730 303 $output .= " <OPTION VALUE=\"$nothingvalue\"\n";
bda8d43a 304 if ($nothingvalue == $selected) {
08056730 305 $output .= " SELECTED";
bda8d43a 306 }
08056730 307 $output .= ">$nothing</OPTION>\n";
873960de 308 }
f9903ed0 309 foreach ($options as $value => $label) {
08056730 310 $output .= " <OPTION VALUE=\"$value\"";
f9903ed0 311 if ($value == $selected) {
08056730 312 $output .= " SELECTED";
f9903ed0 313 }
314 if ($label) {
08056730 315 $output .= ">$label</OPTION>\n";
f9903ed0 316 } else {
08056730 317 $output .= ">$value</OPTION>\n";
f9903ed0 318 }
319 }
08056730 320 $output .= "</SELECT>\n";
321
322 if ($return) {
323 return $output;
324 } else {
325 echo $output;
326 }
f9903ed0 327}
328
d897cae4 329function popup_form ($common, $options, $formname, $selected="", $nothing="choose", $help="", $helptext="", $return=false) {
c1d57101 330/// Implements a complete little popup form
331/// $common = the URL up to the point of the variable that changes
332/// $options = A list of value-label pairs for the popup list
333/// $formname = name must be unique on the page
334/// $selected = the option that is already selected
335/// $nothing = The label for the "no choice" option
e5dfd0f3 336/// $help = The name of a help page if help is required
337/// $helptext = The name of the label for the help button
f9903ed0 338
618b22c5 339 if ($nothing == "choose") {
340 $nothing = get_string("choose")."...";
341 }
342
3e50a139 343 $output = "<FORM TARGET=_top NAME=$formname>";
344 $output .= "<SELECT NAME=popup onChange=\"top.location=document.$formname.popup.options[document.$formname.popup.selectedIndex].value\">\n";
f9903ed0 345
346 if ($nothing != "") {
d897cae4 347 $output .= " <OPTION VALUE=\"javascript:void(0)\">$nothing</OPTION>\n";
f9903ed0 348 }
349
350 foreach ($options as $value => $label) {
d897cae4 351 if (substr($label,0,1) == "-") {
352 $output .= " <OPTION VALUE=\"\"";
353 } else {
354 $output .= " <OPTION VALUE=\"$common$value\"";
355 if ($value == $selected) {
356 $output .= " SELECTED";
357 }
f9903ed0 358 }
359 if ($label) {
d897cae4 360 $output .= ">$label</OPTION>\n";
f9903ed0 361 } else {
d897cae4 362 $output .= ">$value</OPTION>\n";
f9903ed0 363 }
364 }
d897cae4 365 $output .= "</SELECT>";
d897cae4 366 $output .= "</FORM>\n";
367
368 if ($return) {
369 return $output;
370 } else {
9c9f7d77 371 if ($help) {
372 helpbutton($help, $helptext);
373 }
d897cae4 374 echo $output;
375 }
f9903ed0 376}
377
378
379
380function formerr($error) {
c1d57101 381/// Prints some red text
f9903ed0 382 if (!empty($error)) {
383 echo "<font color=#ff0000>$error</font>";
384 }
385}
386
387
388function validate_email ($address) {
c1d57101 389/// Validates an email to make it makes sense.
f9903ed0 390 return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.
391 '@'.
392 '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
393 '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
394 $address));
395}
396
397
398function get_slash_arguments($i=0) {
c1d57101 399/// Extracts arguments from "/foo/bar/something"
400/// eg http://mysite.com/script.php/foo/bar/something
401/// Might only work on Apache
f9903ed0 402
403 global $PATH_INFO;
404
405 if (!isset($PATH_INFO)) {
406 return false;
407 }
408
780db230 409 if (strpos($PATH_INFO, "..")) { // check for parent URLs
410 return false;
411 }
412 if (strpos($PATH_INFO, "|")) { // check for pipes
413 return false;
414 }
415 if (strpos($PATH_INFO, "`")) { // check for backquotes
e2d89725 416 return false;
417 }
418
f9903ed0 419 $args = explode("/", $PATH_INFO);
420
421 if ($i) { // return just the required argument
422 return $args[$i];
423
424 } else { // return the whole array
425 array_shift($args); // get rid of the empty first one
426 return $args;
427 }
428}
429
0095d5cd 430function format_text_menu() {
c1d57101 431/// Just returns an array of formats suitable for a popup menu
0095d5cd 432 return array (FORMAT_MOODLE => get_string("formattext"),
433 FORMAT_HTML => get_string("formathtml") );
434}
435
60f18531 436function format_text($text, $format=FORMAT_MOODLE, $options=NULL) {
c1d57101 437/// Given text in a variety of format codings, this function returns
438/// the text as safe HTML.
439///
440/// $text is raw text (originally from a user)
441/// $format is one of the format constants, defined above
0095d5cd 442
443 switch ($format) {
444 case FORMAT_MOODLE:
c9dda990 445 if (!isset($options->smiley)) {
446 $options->smiley=true;
447 }
448 if (!isset($options->para)) {
1a072208 449 $options->para=true;
c9dda990 450 }
0095d5cd 451 return text_to_html($text, $options->smiley, $options->para);
452 break;
f9903ed0 453
0095d5cd 454 case FORMAT_HTML:
1a072208 455 $text = replace_smilies($text);
3fe3851d 456 return $text;
0095d5cd 457 break;
458 }
459}
460
461
462function clean_text($text, $format) {
c1d57101 463/// Given raw text (eg typed in by a user), this function cleans it up
464/// and removes any nasty tags that could mess up Moodle pages.
b7a3cf49 465
3fe3851d 466 global $JAVASCRIPT_TAGS, $ALLOWED_TAGS;
467
468 switch ($format) { // Does the same thing, currently, but it's nice to have the option
0095d5cd 469 case FORMAT_MOODLE:
3fe3851d 470 $text = strip_tags($text, $ALLOWED_TAGS);
471 foreach ($JAVASCRIPT_TAGS as $tag) {
472 $text = stri_replace($tag, "", $text);
473 }
474 return $text;
0095d5cd 475
476 case FORMAT_HTML:
3fe3851d 477 $text = strip_tags($text, $ALLOWED_TAGS);
478 foreach ($JAVASCRIPT_TAGS as $tag) {
479 $text = stri_replace($tag, "", $text);
480 }
481 return $text;
0095d5cd 482 }
b7a3cf49 483}
f9903ed0 484
1a072208 485function replace_smilies($text) {
c1d57101 486/// Replaces all known smileys in the text with image equivalents
487
1a072208 488 global $CFG, $SMILEY_TEXT, $SMILEY_IMAGE;
b7a3cf49 489
1a072208 490 return str_replace($SMILEY_TEXT, $SMILEY_IMAGE, $text);
491}
0095d5cd 492
909f539d 493function text_to_html($text, $smiley=true, $para=true) {
c1d57101 494/// Given plain text, makes it into HTML as nicely as possible.
495/// May contain HTML tags already
f9903ed0 496
c1d57101 497/// Remove any whitespace that may be between HTML tags
7b3be1b1 498 $text = eregi_replace(">([[:space:]]+)<", "><", $text);
499
c1d57101 500/// Remove any returns that precede or follow HTML tags
0eae8049 501 $text = eregi_replace("([\n\r])<", " <", $text);
502 $text = eregi_replace(">([\n\r])", "> ", $text);
7b3be1b1 503
c1d57101 504/// Make lone URLs into links. eg http://moodle.com/
0be05df0 505 $text = eregi_replace("([\n\r ([])([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
3fe3851d 506 "\\1<A HREF=\"\\2://\\3\\4\" TARGET=\"newpage\">\\2://\\3\\4</A>", $text);
f9903ed0 507
c1d57101 508/// eg www.moodle.com
e1bf736f 509 $text = eregi_replace("([[:space:]])www\.([^[:space:]]*)([[:alnum:]#?/&=])",
f9903ed0 510 "\\1<A HREF=\"http://www.\\2\\3\" TARGET=\"newpage\">www.\\2\\3</A>", $text);
511
c1d57101 512/// Make returns into HTML newlines.
f9903ed0 513 $text = nl2br($text);
514
c1d57101 515/// Turn smileys into images.
d69cb7f4 516 if ($smiley) {
1a072208 517 $text = replace_smilies($text);
d69cb7f4 518 }
f9903ed0 519
c1d57101 520/// Wrap the whole thing in a paragraph tag if required
909f539d 521 if ($para) {
522 return "<P>".$text."</P>";
523 } else {
524 return $text;
525 }
f9903ed0 526}
527
5af78ed2 528function highlight($needle, $haystack) {
c1d57101 529/// This function will highlight instances of $needle in $haystack
5af78ed2 530
531 $parts = explode(strtolower($needle), strtolower($haystack));
532
533 $pos = 0;
534
535 foreach ($parts as $key => $part) {
536 $parts[$key] = substr($haystack, $pos, strlen($part));
537 $pos += strlen($part);
538
539 $parts[$key] .= "<SPAN CLASS=highlight>".substr($haystack, $pos, strlen($needle))."</SPAN>";
540 $pos += strlen($needle);
541 }
542
543 return (join('', $parts));
544}
545
f9903ed0 546
9fa49e22 547
548/// STANDARD WEB PAGE PARTS ///////////////////////////////////////////////////
549
550function print_header ($title="", $heading="", $navigation="", $focus="", $meta="", $cache=true, $button="&nbsp;", $menu="") {
551// $title - appears top of window
552// $heading - appears top of page
553// $navigation - premade navigation string
554// $focus - indicates form element eg inputform.password
555// $meta - meta tags in the header
556// $cache - should this page be cacheable?
557// $button - HTML code for a button (usually for module editing)
558// $menu - HTML code for a popup menu
559 global $USER, $CFG, $THEME;
560
561 if (file_exists("$CFG->dirroot/theme/$CFG->theme/styles.php")) {
562 $styles = $CFG->stylesheet;
563 } else {
564 $styles = "$CFG->wwwroot/theme/standard/styles.php";
565 }
566
567 if ($navigation == "home") {
568 $home = true;
569 $navigation = "";
9d378732 570 } else {
571 $home = false;
9fa49e22 572 }
573
574 if ($button == "") {
575 $button = "&nbsp;";
576 }
577
578 if (!$menu and $navigation) {
579 if (isset($USER->id)) {
580 $menu = "<FONT SIZE=2><A TARGET=_parent HREF=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</A></FONT>";
581 } else {
582 $menu = "<FONT SIZE=2><A TARGET=_parent HREF=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</A></FONT>";
583 }
584 }
585
586 // Specify character set ... default is iso-8859-1 but some languages might need something else
587 // Could be optimised by carrying the charset variable around in $USER
588 if (current_language() == "en") {
589 $meta = "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=iso-8859-1\">\n$meta\n";
590 } else {
591 $meta = "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=".get_string("thischarset")."\">\n$meta\n";
592 }
593
594 if ($CFG->langdir == "RTL") {
595 $direction = " DIR=\"RTL\"";
596 } else {
597 $direction = " DIR=\"LTR\"";
598 }
599
600 if (!$cache) { // Do everything we can to prevent clients and proxies caching
601 @header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
602 @header("Pragma: no-cache");
603 $meta .= "\n<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">";
604 $meta .= "\n<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">";
605 }
606
607 include ("$CFG->dirroot/theme/$CFG->theme/header.html");
608}
609
610function print_footer ($course=NULL) {
611// Can provide a course object to make the footer contain a link to
612// to the course home page, otherwise the link will go to the site home
613 global $USER, $CFG, $THEME;
614
615
616/// Course links
617 if ($course) {
618 if ($course == "home") { // special case for site home page - please do not remove
619 $homelink = "<P ALIGN=center><A TITLE=\"Moodle $CFG->release ($CFG->version)\" HREF=\"http://moodle.com/\">";
620 $homelink .= "<BR><IMG WIDTH=130 HEIGHT=19 SRC=\"pix/madewithmoodle2.gif\" BORDER=0></A></P>";
621 $course = get_site();
622 $homepage = true;
623 } else {
624 $homelink = "<A TARGET=_top HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>";
625 }
626 } else {
627 $homelink = "<A TARGET=_top HREF=\"$CFG->wwwroot\">".get_string("home")."</A>";
628 $course = get_site();
629 }
630
631/// User links
9d378732 632 if (isset($USER->realuser)) {
9fa49e22 633 if ($realuser = get_record("user", "id", $USER->realuser)) {
634 $realuserinfo = " [<A HREF=\"$CFG->wwwroot/course/loginas.php?id=$course->id&return=$realuser->id\">$realuser->firstname $realuser->lastname</A>] ";
635 }
9d378732 636 } else {
637 $realuserinfo = "";
9fa49e22 638 }
639
9c9f7d77 640 if (isset($USER->id) and $USER->id) {
9fa49e22 641 $username = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id\">$USER->firstname $USER->lastname</A>";
642 $loggedinas = $realuserinfo.get_string("loggedinas", "moodle", "$username").
643 " (<A HREF=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</A>)";
644 } else {
645 $loggedinas = get_string("loggedinnot", "moodle").
646 " (<A HREF=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</A>)";
647 }
648
649 include ("$CFG->dirroot/theme/$CFG->theme/footer.html");
650}
651
652
653
654function print_navigation ($navigation) {
655 global $CFG;
656
657 if ($navigation) {
658 if (! $site = get_site()) {
659 $site->shortname = get_string("home");;
660 }
661 echo "<A TARGET=_top HREF=\"$CFG->wwwroot/\">$site->shortname</A> -> $navigation";
662 }
663}
664
665function print_heading($text, $align="CENTER", $size=3) {
666 echo "<P ALIGN=\"$align\"><FONT SIZE=\"$size\"><B>".stripslashes($text)."</B></FONT></P>";
667}
668
669function print_heading_with_help($text, $helppage, $module="moodle") {
670// Centered heading with attached help button (same title text)
671 echo "<P ALIGN=\"CENTER\"><FONT SIZE=\"3\"><B>".stripslashes($text);
672 helpbutton($helppage, $text, $module);
673 echo "</B></FONT></P>";
674}
675
676function print_continue($link) {
677 global $HTTP_REFERER;
678
679 if (!$link) {
680 $link = $HTTP_REFERER;
681 }
682
683 print_heading("<A HREF=\"$link\">".get_string("continue")."</A>");
684}
685
686
687function print_simple_box($message, $align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
688 print_simple_box_start($align, $width, $color, $padding, $class);
689 echo stripslashes($message);
690 print_simple_box_end();
691}
692
693function print_simple_box_start($align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
694 global $THEME;
695
696 if ($align) {
9d378732 697 $align = "ALIGN=\"$align\"";
9fa49e22 698 }
699 if ($width) {
9d378732 700 $width = "WIDTH=\"$width\"";
9fa49e22 701 }
9d378732 702 echo "<table $align $width class=\"$class\" border=\"0\" cellpadding=\"$padding\" cellspacing=\"0\"><tr><td bgcolor=\"$color\" class=\"$class"."content\">";
9fa49e22 703}
704
705function print_simple_box_end() {
706 echo "</td></tr></table>";
707}
708
709function print_single_button($link, $options, $label="OK") {
710 echo "<FORM ACTION=\"$link\" METHOD=GET>";
711 if ($options) {
712 foreach ($options as $name => $value) {
713 echo "<INPUT TYPE=hidden NAME=\"$name\" VALUE=\"$value\">";
714 }
715 }
716 echo "<INPUT TYPE=submit VALUE=\"$label\"></FORM>";
717}
718
719function print_spacer($height=1, $width=1, $br=true) {
720 global $CFG;
721 echo "<IMG HEIGHT=\"$height\" WIDTH=\"$width\" SRC=\"$CFG->wwwroot/pix/spacer.gif\" ALT=\"\">";
722 if ($br) {
723 echo "<BR \>\n";
724 }
725}
726
727function print_file_picture($path, $courseid=0, $height="", $width="", $link="") {
728// Given the path to a picture file in a course, or a URL,
729// this function includes the picture in the page.
730 global $CFG;
731
732 if ($height) {
733 $height = "HEIGHT=\"$height\"";
734 }
735 if ($width) {
736 $width = "WIDTH=\"$width\"";
737 }
738 if ($link) {
739 echo "<A HREF=\"$link\">";
740 }
741 if (substr(strtolower($path), 0, 7) == "http://") {
742 echo "<IMG BORDER=0 $height $width SRC=\"$path\">";
743
744 } else if ($courseid) {
745 echo "<IMG BORDER=0 $height $width SRC=\"";
746 if ($CFG->slasharguments) { // Use this method if possible for better caching
747 echo "$CFG->wwwroot/file.php/$courseid/$path";
748 } else {
749 echo "$CFG->wwwroot/file.php?file=$courseid/$path";
750 }
751 echo "\">";
752 } else {
753 echo "Error: must pass URL or course";
754 }
755 if ($link) {
756 echo "</A>";
757 }
758}
759
760function print_user_picture($userid, $courseid, $picture, $large=false, $returnstring=false, $link=true) {
761 global $CFG;
762
763 if ($link) {
764 $output = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$userid&course=$courseid\">";
765 } else {
766 $output = "";
767 }
768 if ($large) {
769 $file = "f1.jpg";
770 $size = 100;
771 } else {
772 $file = "f2.jpg";
773 $size = 35;
774 }
775 if ($picture) {
776 if ($CFG->slasharguments) { // Use this method if possible for better caching
777 $output .= "<IMG SRC=\"$CFG->wwwroot/user/pix.php/$userid/$file\" BORDER=0 WIDTH=$size HEIGHT=$size ALT=\"\">";
778 } else {
779 $output .= "<IMG SRC=\"$CFG->wwwroot/user/pix.php?file=/$userid/$file\" BORDER=0 WIDTH=$size HEIGHT=$size ALT=\"\">";
780 }
781 } else {
782 $output .= "<IMG SRC=\"$CFG->wwwroot/user/default/$file\" BORDER=0 WIDTH=$size HEIGHT=$size ALT=\"\">";
783 }
784 if ($link) {
785 $output .= "</A>";
786 }
787
788 if ($returnstring) {
789 return $output;
790 } else {
791 echo $output;
792 }
793}
794
795function print_table($table) {
796// Prints a nicely formatted table.
797// $table is an object with several properties.
798// $table->head is an array of heading names.
799// $table->align is an array of column alignments
800// $table->size is an array of column sizes
801// $table->data[] is an array of arrays containing the data.
802// $table->width is an percentage of the page
803// $table->cellpadding padding on each cell
804// $table->cellspacing spacing between cells
805
806 if (isset($table->align)) {
807 foreach ($table->align as $key => $aa) {
808 if ($aa) {
809 $align[$key] = " ALIGN=\"$aa\"";
810 } else {
811 $align[$key] = "";
812 }
813 }
814 }
815 if (isset($table->size)) {
816 foreach ($table->size as $key => $ss) {
817 if ($ss) {
818 $size[$key] = " WIDTH=\"$ss\"";
819 } else {
820 $size[$key] = "";
821 }
822 }
823 }
824
9d378732 825 if (empty($table->width)) {
9fa49e22 826 $table->width = "80%";
827 }
828
9d378732 829 if (empty($table->cellpadding)) {
9fa49e22 830 $table->cellpadding = "5";
831 }
832
9d378732 833 if (empty($table->cellspacing)) {
9fa49e22 834 $table->cellspacing = "1";
835 }
836
837 print_simple_box_start("CENTER", "$table->width", "#FFFFFF", 0);
838 echo "<TABLE WIDTH=100% BORDER=0 valign=top align=center ";
839 echo " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"generaltable\">\n";
840
b79f41cd 841 if (!empty($table->head)) {
9fa49e22 842 echo "<TR>";
843 foreach ($table->head as $key => $heading) {
9d378732 844 if (!isset($size[$key])) {
845 $size[$key] = "";
846 }
847 if (!isset($align[$key])) {
848 $align[$key] = "";
849 }
9fa49e22 850 echo "<TH VALIGN=top ".$align[$key].$size[$key]." NOWRAP class=\"generaltableheader\">$heading</TH>";
851 }
852 echo "</TR>\n";
853 }
854
855 foreach ($table->data as $row) {
856 echo "<TR VALIGN=TOP>";
857 foreach ($row as $key => $item) {
9d378732 858 if (!isset($size[$key])) {
859 $size[$key] = "";
860 }
861 if (!isset($align[$key])) {
862 $align[$key] = "";
863 }
9fa49e22 864 echo "<TD ".$align[$key].$size[$key]." class=\"generaltablecell\">$item</TD>";
865 }
866 echo "</TR>\n";
867 }
868 echo "</TABLE>\n";
869 print_simple_box_end();
870
871 return true;
872}
873
874function print_editing_switch($courseid) {
875 global $CFG, $USER;
876
877 if (isteacher($courseid)) {
878 if ($USER->editing) {
879 echo "<A HREF=\"$CFG->wwwroot/course/view.php?id=$courseid&edit=off\">Turn editing off</A>";
880 } else {
881 echo "<A HREF=\"$CFG->wwwroot/course/view.php?id=$courseid&edit=on\">Turn editing on</A>";
882 }
883 }
884}
885
886function print_textarea($richedit, $rows, $cols, $width, $height, $name, $value="") {
887 global $CFG, $THEME;
888
889 if ($richedit) {
890 echo "<object id=richedit style=\"BACKGROUND-COLOR: buttonface\"";
891 echo " data=\"$CFG->wwwroot/lib/rte/richedit.html\"";
892 echo " width=\"$width\" height=\"$height\" ";
893 echo " type=\"text/x-scriptlet\" VIEWASTEXT></object>\n";
894 echo "<TEXTAREA style=\"display:none\" NAME=\"$name\" ROWS=1 COLS=1>";
895 p($value);
896 echo "</TEXTAREA>\n";
897 } else {
898 echo "<TEXTAREA name=\"$name\" rows=\"$rows\" cols=\"$cols\" wrap=virtual>";
899 p($value);
900 echo "</TEXTAREA>\n";
901 }
902}
903
904function print_richedit_javascript($form, $name, $source="no") {
905 echo "<SCRIPT language=\"JavaScript\" event=\"onload\" for=\"window\">\n";
906 echo " document.richedit.options = \"history=no;source=$source\";";
907 echo " document.richedit.docHtml = $form.$name.innerText;";
908 echo "</SCRIPT>";
909}
910
911
912function update_course_icon($courseid) {
913// Used to be an icon, but it's now a simple form button
914 global $CFG, $USER;
915
916 if (isteacher($courseid)) {
9c9f7d77 917 if (!empty($USER->editing)) {
9fa49e22 918 $string = get_string("turneditingoff");
919 $edit = "off";
920 } else {
921 $string = get_string("turneditingon");
922 $edit = "on";
923 }
924 return "<FORM TARGET=_parent METHOD=GET ACTION=\"$CFG->wwwroot/course/view.php\">".
925 "<INPUT TYPE=hidden NAME=id VALUE=\"$courseid\">".
926 "<INPUT TYPE=hidden NAME=edit VALUE=\"$edit\">".
927 "<INPUT TYPE=submit VALUE=\"$string\"></FORM>";
928 }
929}
930
931function update_module_button($moduleid, $courseid, $string) {
932// Prints the editing button on a module "view" page
933 global $CFG;
934
935 if (isteacher($courseid)) {
936 $string = get_string("updatethis", "", $string);
937 return "<FORM TARGET=_parent METHOD=GET ACTION=\"$CFG->wwwroot/course/mod.php\">".
938 "<INPUT TYPE=hidden NAME=update VALUE=\"$moduleid\">".
939 "<INPUT TYPE=hidden NAME=return VALUE=\"true\">".
940 "<INPUT TYPE=submit VALUE=\"$string\"></FORM>";
941 }
942}
943
944
945function navmenu($course, $cm=NULL) {
946// Given a course and a (current) coursemodule
947// This function returns a small popup menu with all the
948// course activity modules in it, as a navigation menu
949// The data is taken from the serialised array stored in
950// the course record
951
952 global $CFG;
953
954 if ($cm) {
955 $cm = $cm->id;
956 }
957
958 if ($course->format == 'weeks') {
959 $strsection = get_string("week");
960 } else {
961 $strsection = get_string("topic");
962 }
963
964 if (!$modinfo = unserialize($course->modinfo)) {
965 return "";
966 }
967 $section = -1;
968 $selected = "";
969 foreach ($modinfo as $mod) {
970 if ($mod->section > 0 and $section <> $mod->section) {
971 $menu[] = "-------------- $strsection $mod->section --------------";
972 }
973 $section = $mod->section;
974 $url = "$mod->mod/view.php?id=$mod->cm";
975 if ($cm == $mod->cm) {
976 $selected = $url;
977 }
978 $mod->name = urldecode($mod->name);
979 if (strlen($mod->name) > 55) {
980 $mod->name = substr($mod->name, 0, 50)."...";
981 }
982 $menu[$url] = $mod->name;
983 }
984
985 return popup_form("$CFG->wwwroot/mod/", $menu, "navmenu", $selected, get_string("jumpto"), "", "", true);
986}
987
988
989
990function print_date_selector($day, $month, $year, $currenttime=0) {
991// Currenttime is a default timestamp in GMT
992// Prints form items with the names $day, $month and $year
993
994 if (!$currenttime) {
995 $currenttime = time();
996 }
997 $currentdate = usergetdate($currenttime);
998
999 for ($i=1; $i<=31; $i++) {
1000 $days[$i] = "$i";
1001 }
1002 for ($i=1; $i<=12; $i++) {
8663bfdb 1003 $months[$i] = userdate(mktime(0,0,0,$i,1,2000), "%B");
9fa49e22 1004 }
1005 for ($i=2000; $i<=2010; $i++) {
1006 $years[$i] = $i;
1007 }
47f1da80 1008 choose_from_menu($days, $day, $currentdate['mday'], "");
1009 choose_from_menu($months, $month, $currentdate['mon'], "");
1010 choose_from_menu($years, $year, $currentdate['year'], "");
9fa49e22 1011}
1012
1013function print_time_selector($hour, $minute, $currenttime=0) {
1014// Currenttime is a default timestamp in GMT
1015// Prints form items with the names $hour and $minute
1016
1017 if (!$currenttime) {
1018 $currenttime = time();
1019 }
1020 $currentdate = usergetdate($currenttime);
1021 for ($i=0; $i<=23; $i++) {
1022 $hours[$i] = sprintf("%02d",$i);
1023 }
1024 for ($i=0; $i<=59; $i++) {
1025 $minutes[$i] = sprintf("%02d",$i);
1026 }
47f1da80 1027 choose_from_menu($hours, $hour, $currentdate['hours'], "");
1028 choose_from_menu($minutes, $minute, $currentdate['minutes'], "");
9fa49e22 1029}
1030
1031function error ($message, $link="") {
1032 global $CFG, $SESSION;
1033
1034 print_header(get_string("error"));
1035 echo "<BR>";
1036 print_simple_box($message, "center", "", "#FFBBBB");
1037
1038 if (!$link) {
1039 if ( !empty($SESSION->fromurl) ) {
1040 $link = "$SESSION->fromurl";
1041 unset($SESSION->fromurl);
1042 save_session("SESSION");
1043 } else {
1044 $link = "$CFG->wwwroot";
1045 }
1046 }
1047 print_continue($link);
1048 print_footer();
1049 die;
1050}
1051
1052function helpbutton ($page, $title="", $module="moodle", $image=true, $linktext=false, $text="") {
1053 // $page = the keyword that defines a help page
1054 // $title = the title of links, rollover tips, alt tags etc
1055 // $module = which module is the page defined in
1056 // $image = use a help image for the link? (true/false/"both")
1057 // $text = if defined then this text is used in the page, and
1058 // the $page variable is ignored.
1059 global $CFG;
1060
1061 if ($module == "") {
1062 $module = "moodle";
1063 }
1064
1065 if ($image) {
1066 if ($linktext) {
1067 $linkobject = "$title<IMG align=\"absmiddle\" BORDER=0 HEIGHT=17 WIDTH=22 ALT=\"\" SRC=\"$CFG->wwwroot/pix/help.gif\">";
1068 } else {
1069 $linkobject = "<IMG align=\"absmiddle\" BORDER=0 HEIGHT=17 WIDTH=22 ALT=\"$title\" SRC=\"$CFG->wwwroot/pix/help.gif\">";
1070 }
1071 } else {
1072 $linkobject = $title;
1073 }
1074 if ($text) {
1075 $url = "/help.php?module=$module&text=".htmlentities(urlencode($text));
1076 } else {
1077 $url = "/help.php?module=$module&file=$page.html";
1078 }
1079 link_to_popup_window ($url, "popup", $linkobject, 400, 500, $title);
1080}
1081
1082function notice ($message, $link="") {
1083 global $THEME, $HTTP_REFERER;
1084
1085 if (!$link) {
1086 $link = $HTTP_REFERER;
1087 }
1088
1089 echo "<BR>";
1090 print_simple_box($message, "center", "", "$THEME->cellheading");
1091 print_heading("<A HREF=\"$link\">".get_string("continue")."</A>");
1092 print_footer(get_site());
1093 die;
1094}
1095
1096function notice_yesno ($message, $linkyes, $linkno) {
1097 global $THEME;
1098
1099 print_simple_box_start("center", "", "$THEME->cellheading");
1100 echo "<P ALIGN=CENTER><FONT SIZE=3>$message</FONT></P>";
1101 echo "<P ALIGN=CENTER><FONT SIZE=3><B>";
1102 echo "<A HREF=\"$linkyes\">".get_string("yes")."</A>";
1103 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
1104 echo "<A HREF=\"$linkno\">".get_string("no")."</A>";
1105 echo "</B></FONT></P>";
1106 print_simple_box_end();
1107}
1108
1109function redirect($url, $message="", $delay=0) {
1110// Uses META tags to redirect the user, after printing a notice
1111
1112 echo "<META HTTP-EQUIV='Refresh' CONTENT='$delay; URL=$url'>";
1113
1114 if (!empty($message)) {
1115 print_header();
1116 echo "<CENTER>";
1117 echo "<P>$message</P>";
1118 echo "<P>( <A HREF=\"$url\">".get_string("continue")."</A> )</P>";
1119 echo "</CENTER>";
1120 }
1121 die;
1122}
1123
99988d1a 1124function notify ($message, $color="red", $align="center") {
1125 echo "<p align=\"$align\"><b><font color=\"$color\">$message</font></b></p>\n";
9fa49e22 1126}
1127
1128
f9903ed0 1129?>