no message
[moodle.git] / lib / weblib.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
3// weblib.php
4//
0095d5cd 5// Library of useful PHP functions and constants related to web pages.
f9903ed0 6//
7//
8
0095d5cd 9/// Constants
10
c1d57101 11/// Define text formatting types ... eventually we can add Wiki, BBcode etc
0095d5cd 12define("FORMAT_MOODLE", "0");
13define("FORMAT_HTML", "1");
14
c9dda990 15$SMILEY_TEXT[] = ":-)";
16$SMILEY_IMAGE[] = "<IMG ALT=\":-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/smiley.gif\">";
17$SMILEY_TEXT[] = ":)";
18$SMILEY_IMAGE[] = "<IMG ALT=\":-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/smiley.gif\">";
19$SMILEY_TEXT[] = ":-D";
20$SMILEY_IMAGE[] = "<IMG ALT=\":-D\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/biggrin.gif\">";
21$SMILEY_TEXT[] = ";-)";
22$SMILEY_IMAGE[] = "<IMG ALT=\";-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/wink.gif\">";
23$SMILEY_TEXT[] = ":-/";
24$SMILEY_IMAGE[] = "<IMG ALT=\":-/\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/mixed.gif\">";
25$SMILEY_TEXT[] = "V-.";
26$SMILEY_IMAGE[] = "<IMG ALT=\"V-.\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/thoughtful.gif\">";
27$SMILEY_TEXT[] = ":-P";
28$SMILEY_IMAGE[] = "<IMG ALT=\":-P\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/tongueout.gif\">";
29$SMILEY_TEXT[] = "B-)";
30$SMILEY_IMAGE[] = "<IMG ALT=\"B-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/cool.gif\">";
31$SMILEY_TEXT[] = "^-)";
32$SMILEY_IMAGE[] = "<IMG ALT=\"^-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/approve.gif\">";
33$SMILEY_TEXT[] = "8-)";
34$SMILEY_IMAGE[] = "<IMG ALT=\"8-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/wideeyes.gif\">";
35$SMILEY_TEXT[] = ":o)";
36$SMILEY_IMAGE[] = "<IMG ALT=\":o)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/clown.gif\">";
37$SMILEY_TEXT[] = ":-(";
38$SMILEY_IMAGE[] = "<IMG ALT=\":-(\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/sad.gif\">";
39$SMILEY_TEXT[] = ":(";
40$SMILEY_IMAGE[] = "<IMG ALT=\":-(\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/sad.gif\">";
41$SMILEY_TEXT[] = "8-.";
42$SMILEY_IMAGE[] = "<IMG ALT=\"8-.\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/shy.gif\">";
43$SMILEY_TEXT[] = ":-I";
44$SMILEY_IMAGE[] = "<IMG ALT=\":-I\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/blush.gif\">";
45$SMILEY_TEXT[] = ":-X";
46$SMILEY_IMAGE[] = "<IMG ALT=\":-X\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/kiss.gif\">";
47$SMILEY_TEXT[] = "8-o";
48$SMILEY_IMAGE[] = "<IMG ALT=\"8-o\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/surprise.gif\">";
49$SMILEY_TEXT[] = "P-|";
50$SMILEY_IMAGE[] = "<IMG ALT=\"P-|\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/blackeye.gif\">";
51$SMILEY_TEXT[] = "8-[";
52$SMILEY_IMAGE[] = "<IMG ALT=\"8-[\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/angry.gif\">";
53$SMILEY_TEXT[] = "xx-P";
54$SMILEY_IMAGE[] = "<IMG ALT=\"xx-P\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/dead.gif\">";
55$SMILEY_TEXT[] = "|-.";
56$SMILEY_IMAGE[] = "<IMG ALT=\"|-.\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/sleepy.gif\">";
57$SMILEY_TEXT[] = "}-]";
58$SMILEY_IMAGE[] = "<IMG ALT=\"}-]\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/evil.gif\">";
0095d5cd 59
3fe3851d 60$JAVASCRIPT_TAGS = array("javascript:", "onclick=", "ondblclick=", "onkeydown=", "onkeypress=", "onkeyup=",
61 "onmouseover=", "onmouseout=", "onmousedown=", "onmouseup=",
62 "onblur=", "onfocus=", "onload=", "onselect=");
63
840aaba1 64$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>";
3fe3851d 65
66
0095d5cd 67/// Functions
68
8553b700 69function s($var) {
c1d57101 70/// returns $var with HTML characters (like "<", ">", etc.) properly quoted,
71/// or if $var is empty, will return an empty string.
f9903ed0 72
73 return empty($var) ? "" : htmlSpecialChars(stripslashes($var));
74}
75
76function p($var) {
c1d57101 77/// prints $var with HTML characters (like "<", ">", etc.) properly quoted,
78/// or if $var is empty, will print an empty string.
f9903ed0 79
80 echo empty($var) ? "" : htmlSpecialChars(stripslashes($var));
81}
82
8553b700 83function nvl(&$var, $default="") {
c1d57101 84/// if $var is undefined, return $default, otherwise return $var
8553b700 85
86 return isset($var) ? $var : $default;
87}
f9903ed0 88
89function strip_querystring($url) {
c1d57101 90/// takes a URL and returns it without the querystring portion
f9903ed0 91
92 if ($commapos = strpos($url, '?')) {
93 return substr($url, 0, $commapos);
94 } else {
95 return $url;
96 }
97}
98
99function get_referer() {
c1d57101 100/// returns the URL of the HTTP_REFERER, less the querystring portion
f9903ed0 101
7fbd6b1c 102 global $HTTP_REFERER;
c1d57101 103
8553b700 104 return strip_querystring(nvl($HTTP_REFERER));
f9903ed0 105}
106
c1d57101 107
f9903ed0 108function me() {
c1d57101 109/// returns the name of the current script, WITH the querystring portion.
110/// this function is necessary because PHP_SELF and REQUEST_URI and PATH_INFO
111/// return different things depending on a lot of things like your OS, Web
112/// server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.)
f9903ed0 113
7fbd6b1c 114 global $REQUEST_URI, $PATH_INFO, $PHP_SELF;
f9903ed0 115
7fbd6b1c 116 if ($REQUEST_URI) {
117 return $REQUEST_URI;
c1d57101 118
7fbd6b1c 119 } else if ($PATH_INFO) {
120 return $PATH_INFO;
c1d57101 121
7fbd6b1c 122 } else if ($PHP_SELF) {
123 return $PHP_SELF;
c1d57101 124
7fbd6b1c 125 } else {
c1d57101 126 notify("Error: Could not find any of these web server variables: \$REQUEST_URI, \$PATH_INFO or \$PHP_SELF");
7fbd6b1c 127 }
f9903ed0 128}
129
130
f9903ed0 131function qualified_me() {
c1d57101 132/// like me() but returns a full URL
f9903ed0 133
c1d57101 134 global $HTTPS, $HTTP_HOST;
135
136 if (!$HTTP_HOST) {
137 notify("Error: could not find web server variable: \$HTTP_HOST");
138 }
f9903ed0 139
140 $protocol = (isset($HTTPS) && $HTTPS == "on") ? "https://" : "http://";
141 $url_prefix = "$protocol$HTTP_HOST";
142 return $url_prefix . me();
143}
144
145
146function match_referer($good_referer = "") {
c1d57101 147/// returns true if the referer is the same as the good_referer. If
87a2fa03 148/// good_referer is not specified, use qualified_me as the good_referer
60f18531 149 global $CFG;
150
151 if ($CFG->buggy_referer) {
152 return true;
153 }
f9903ed0 154
c1d57101 155 if ($good_referer == "") {
156 $good_referer = qualified_me();
157 }
f9903ed0 158 return $good_referer == get_referer();
159}
160
161
3fe3851d 162function stri_replace($find, $replace, $string ) {
c1d57101 163/// This does a search and replace, ignoring case
164/// This function is only here because one doesn't exist yet in PHP
165/// Unlike str_replace(), this only works on single values (not arrays)
3fe3851d 166
167 $parts = explode(strtolower($find), strtolower($string));
168
169 $pos = 0;
170
171 foreach ($parts as $key => $part) {
172 $parts[$key] = substr($string, $pos, strlen($part));
173 $pos += strlen($part) + strlen($find);
174 }
175
176 return (join($replace, $parts));
177}
178
f9903ed0 179function read_template($filename, &$var) {
c1d57101 180/// return a (big) string containing the contents of a template file with all
181/// the variables interpolated. all the variables must be in the $var[] array or
182/// object (whatever you decide to use).
183///
184/// WARNING: do not use this on big files!!
f9903ed0 185
186 $temp = str_replace("\\", "\\\\", implode(file($filename), ""));
187 $temp = str_replace('"', '\"', $temp);
188 eval("\$template = \"$temp\";");
189 return $template;
190}
191
192function checked(&$var, $set_value = 1, $unset_value = 0) {
c1d57101 193/// if variable is set, set it to the set_value otherwise set it to the
194/// unset_value. used to handle checkboxes when you are expecting them from
195/// a form
f9903ed0 196
197 if (empty($var)) {
198 $var = $unset_value;
199 } else {
200 $var = $set_value;
201 }
202}
203
204function frmchecked(&$var, $true_value = "checked", $false_value = "") {
c1d57101 205/// prints the word "checked" if a variable is true, otherwise prints nothing,
206/// used for printing the word "checked" in a checkbox form input
f9903ed0 207
208 if ($var) {
209 echo $true_value;
210 } else {
211 echo $false_value;
212 }
213}
214
215
65cf9fc3 216function link_to_popup_window ($url, $name="popup", $linkname="click here", $height=400, $width=500, $title="Popup window") {
c1d57101 217/// This will create a HTML link that will work on both
218/// Javascript and non-javascript browsers.
219/// Relies on the Javascript function openpopup in javascript.php
220/// $url must be relative to home page eg /mod/survey/stuff.php
f9903ed0 221
ff80e012 222 global $CFG;
223
f9903ed0 224 echo "\n<SCRIPT language=\"Javascript\">";
225 echo "\n<!--";
65cf9fc3 226 echo "\ndocument.write('<A TITLE=\"$title\" HREF=javascript:openpopup(\"$url\",\"$name\",\"$height\",\"$width\") >$linkname</A>');";
f9903ed0 227 echo "\n//-->";
228 echo "\n</SCRIPT>";
ff80e012 229 echo "\n<NOSCRIPT>\n<A TARGET=\"$name\" TITLE=\"$title\" HREF=\"$CFG->wwwroot/$url\">$linkname</A>\n</NOSCRIPT>\n";
f9903ed0 230
231}
232
233function close_window_button() {
c1d57101 234/// Prints a simple button to close a window
235
f9903ed0 236 echo "<FORM><CENTER>";
e5dfd0f3 237 echo "<INPUT TYPE=button onClick=\"self.close();\" VALUE=\"".get_string("closewindow")."\">";
f9903ed0 238 echo "</CENTER></FORM>";
239}
240
241
08056730 242function choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", $nothingvalue="0", $return=false) {
c1d57101 243/// Given an array of value, creates a popup menu to be part of a form
244/// $options["value"]["label"]
f9903ed0 245
618b22c5 246 if ($nothing == "choose") {
247 $nothing = get_string("choose")."...";
248 }
249
f9903ed0 250 if ($script) {
251 $javascript = "onChange=\"$script\"";
252 }
08056730 253 $output = "<SELECT NAME=$name $javascript>\n";
bda8d43a 254 if ($nothing) {
08056730 255 $output .= " <OPTION VALUE=\"$nothingvalue\"\n";
bda8d43a 256 if ($nothingvalue == $selected) {
08056730 257 $output .= " SELECTED";
bda8d43a 258 }
08056730 259 $output .= ">$nothing</OPTION>\n";
873960de 260 }
f9903ed0 261 foreach ($options as $value => $label) {
08056730 262 $output .= " <OPTION VALUE=\"$value\"";
f9903ed0 263 if ($value == $selected) {
08056730 264 $output .= " SELECTED";
f9903ed0 265 }
266 if ($label) {
08056730 267 $output .= ">$label</OPTION>\n";
f9903ed0 268 } else {
08056730 269 $output .= ">$value</OPTION>\n";
f9903ed0 270 }
271 }
08056730 272 $output .= "</SELECT>\n";
273
274 if ($return) {
275 return $output;
276 } else {
277 echo $output;
278 }
f9903ed0 279}
280
d897cae4 281function popup_form ($common, $options, $formname, $selected="", $nothing="choose", $help="", $helptext="", $return=false) {
c1d57101 282/// Implements a complete little popup form
283/// $common = the URL up to the point of the variable that changes
284/// $options = A list of value-label pairs for the popup list
285/// $formname = name must be unique on the page
286/// $selected = the option that is already selected
287/// $nothing = The label for the "no choice" option
e5dfd0f3 288/// $help = The name of a help page if help is required
289/// $helptext = The name of the label for the help button
f9903ed0 290
618b22c5 291 if ($nothing == "choose") {
292 $nothing = get_string("choose")."...";
293 }
294
3e50a139 295 $output = "<FORM TARGET=_top NAME=$formname>";
296 $output .= "<SELECT NAME=popup onChange=\"top.location=document.$formname.popup.options[document.$formname.popup.selectedIndex].value\">\n";
f9903ed0 297
298 if ($nothing != "") {
d897cae4 299 $output .= " <OPTION VALUE=\"javascript:void(0)\">$nothing</OPTION>\n";
f9903ed0 300 }
301
302 foreach ($options as $value => $label) {
d897cae4 303 if (substr($label,0,1) == "-") {
304 $output .= " <OPTION VALUE=\"\"";
305 } else {
306 $output .= " <OPTION VALUE=\"$common$value\"";
307 if ($value == $selected) {
308 $output .= " SELECTED";
309 }
f9903ed0 310 }
311 if ($label) {
d897cae4 312 $output .= ">$label</OPTION>\n";
f9903ed0 313 } else {
d897cae4 314 $output .= ">$value</OPTION>\n";
f9903ed0 315 }
316 }
d897cae4 317 $output .= "</SELECT>";
318 if (!$return and $help) {
e5dfd0f3 319 helpbutton($help, $helptext);
320 }
d897cae4 321 $output .= "</FORM>\n";
322
323 if ($return) {
324 return $output;
325 } else {
326 echo $output;
327 }
f9903ed0 328}
329
330
331
332function formerr($error) {
c1d57101 333/// Prints some red text
f9903ed0 334 if (!empty($error)) {
335 echo "<font color=#ff0000>$error</font>";
336 }
337}
338
339
340function validate_email ($address) {
c1d57101 341/// Validates an email to make it makes sense.
f9903ed0 342 return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.
343 '@'.
344 '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
345 '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
346 $address));
347}
348
349
350function get_slash_arguments($i=0) {
c1d57101 351/// Extracts arguments from "/foo/bar/something"
352/// eg http://mysite.com/script.php/foo/bar/something
353/// Might only work on Apache
f9903ed0 354
355 global $PATH_INFO;
356
357 if (!isset($PATH_INFO)) {
358 return false;
359 }
360
780db230 361 if (strpos($PATH_INFO, "..")) { // check for parent URLs
362 return false;
363 }
364 if (strpos($PATH_INFO, "|")) { // check for pipes
365 return false;
366 }
367 if (strpos($PATH_INFO, "`")) { // check for backquotes
e2d89725 368 return false;
369 }
370
f9903ed0 371 $args = explode("/", $PATH_INFO);
372
373 if ($i) { // return just the required argument
374 return $args[$i];
375
376 } else { // return the whole array
377 array_shift($args); // get rid of the empty first one
378 return $args;
379 }
380}
381
0095d5cd 382function format_text_menu() {
c1d57101 383/// Just returns an array of formats suitable for a popup menu
0095d5cd 384 return array (FORMAT_MOODLE => get_string("formattext"),
385 FORMAT_HTML => get_string("formathtml") );
386}
387
60f18531 388function format_text($text, $format=FORMAT_MOODLE, $options=NULL) {
c1d57101 389/// Given text in a variety of format codings, this function returns
390/// the text as safe HTML.
391///
392/// $text is raw text (originally from a user)
393/// $format is one of the format constants, defined above
0095d5cd 394
395 switch ($format) {
396 case FORMAT_MOODLE:
c9dda990 397 if (!isset($options->smiley)) {
398 $options->smiley=true;
399 }
400 if (!isset($options->para)) {
1a072208 401 $options->para=true;
c9dda990 402 }
0095d5cd 403 return text_to_html($text, $options->smiley, $options->para);
404 break;
f9903ed0 405
0095d5cd 406 case FORMAT_HTML:
1a072208 407 $text = replace_smilies($text);
3fe3851d 408 return $text;
0095d5cd 409 break;
410 }
411}
412
413
414function clean_text($text, $format) {
c1d57101 415/// Given raw text (eg typed in by a user), this function cleans it up
416/// and removes any nasty tags that could mess up Moodle pages.
b7a3cf49 417
3fe3851d 418 global $JAVASCRIPT_TAGS, $ALLOWED_TAGS;
419
420 switch ($format) { // Does the same thing, currently, but it's nice to have the option
0095d5cd 421 case FORMAT_MOODLE:
3fe3851d 422 $text = strip_tags($text, $ALLOWED_TAGS);
423 foreach ($JAVASCRIPT_TAGS as $tag) {
424 $text = stri_replace($tag, "", $text);
425 }
426 return $text;
0095d5cd 427
428 case FORMAT_HTML:
3fe3851d 429 $text = strip_tags($text, $ALLOWED_TAGS);
430 foreach ($JAVASCRIPT_TAGS as $tag) {
431 $text = stri_replace($tag, "", $text);
432 }
433 return $text;
0095d5cd 434 }
b7a3cf49 435}
f9903ed0 436
1a072208 437function replace_smilies($text) {
c1d57101 438/// Replaces all known smileys in the text with image equivalents
439
1a072208 440 global $CFG, $SMILEY_TEXT, $SMILEY_IMAGE;
b7a3cf49 441
1a072208 442 return str_replace($SMILEY_TEXT, $SMILEY_IMAGE, $text);
443}
0095d5cd 444
909f539d 445function text_to_html($text, $smiley=true, $para=true) {
c1d57101 446/// Given plain text, makes it into HTML as nicely as possible.
447/// May contain HTML tags already
f9903ed0 448
c1d57101 449/// Remove any whitespace that may be between HTML tags
7b3be1b1 450 $text = eregi_replace(">([[:space:]]+)<", "><", $text);
451
c1d57101 452/// Remove any returns that precede or follow HTML tags
0eae8049 453 $text = eregi_replace("([\n\r])<", " <", $text);
454 $text = eregi_replace(">([\n\r])", "> ", $text);
7b3be1b1 455
c1d57101 456/// Make lone URLs into links. eg http://moodle.com/
0be05df0 457 $text = eregi_replace("([\n\r ([])([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
3fe3851d 458 "\\1<A HREF=\"\\2://\\3\\4\" TARGET=\"newpage\">\\2://\\3\\4</A>", $text);
f9903ed0 459
c1d57101 460/// eg www.moodle.com
f9903ed0 461 $text = eregi_replace("([[:space:]])www.([^[:space:]]*)([[:alnum:]#?/&=])",
462 "\\1<A HREF=\"http://www.\\2\\3\" TARGET=\"newpage\">www.\\2\\3</A>", $text);
463
c1d57101 464/// Make returns into HTML newlines.
f9903ed0 465 $text = nl2br($text);
466
c1d57101 467/// Turn smileys into images.
d69cb7f4 468 if ($smiley) {
1a072208 469 $text = replace_smilies($text);
d69cb7f4 470 }
f9903ed0 471
c1d57101 472/// Wrap the whole thing in a paragraph tag if required
909f539d 473 if ($para) {
474 return "<P>".$text."</P>";
475 } else {
476 return $text;
477 }
f9903ed0 478}
479
5af78ed2 480function highlight($needle, $haystack) {
c1d57101 481/// This function will highlight instances of $needle in $haystack
5af78ed2 482
483 $parts = explode(strtolower($needle), strtolower($haystack));
484
485 $pos = 0;
486
487 foreach ($parts as $key => $part) {
488 $parts[$key] = substr($haystack, $pos, strlen($part));
489 $pos += strlen($part);
490
491 $parts[$key] .= "<SPAN CLASS=highlight>".substr($haystack, $pos, strlen($needle))."</SPAN>";
492 $pos += strlen($needle);
493 }
494
495 return (join('', $parts));
496}
497
f9903ed0 498
499?>