*** empty log 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
11// Define text formatting types ... eventually we can add Wiki, BBcode etc
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
64$ALLOWED_TAGS = "<b><i><u><font><table><tbody><span><div><tr><td><ol><ul><dl><li><dt><dd><h1><h2><h3><hr><img><a>";
65
66
0095d5cd 67/// Functions
68
8553b700 69function s($var) {
f9903ed0 70// returns $var with HTML characters (like "<", ">", etc.) properly quoted,
71// or if $var is empty, will return an empty string.
72
73 return empty($var) ? "" : htmlSpecialChars(stripslashes($var));
74}
75
76function p($var) {
77// prints $var with HTML characters (like "<", ">", etc.) properly quoted,
78// or if $var is empty, will print an empty string.
79
80 echo empty($var) ? "" : htmlSpecialChars(stripslashes($var));
81}
82
8553b700 83function nvl(&$var, $default="") {
84// if $var is undefined, return $default, otherwise return $var
85
86 return isset($var) ? $var : $default;
87}
f9903ed0 88
89function strip_querystring($url) {
90// takes a URL and returns it without the querystring portion
91
92 if ($commapos = strpos($url, '?')) {
93 return substr($url, 0, $commapos);
94 } else {
95 return $url;
96 }
97}
98
99function get_referer() {
100// returns the URL of the HTTP_REFERER, less the querystring portion
101
7fbd6b1c 102 global $HTTP_REFERER;
8553b700 103 return strip_querystring(nvl($HTTP_REFERER));
f9903ed0 104}
105
f9903ed0 106function me() {
107// returns the name of the current script, WITH the querystring portion.
108// this function is necessary because PHP_SELF and REQUEST_URI and PATH_INFO
109// return different things depending on a lot of things like your OS, Web
110// server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.)
111
7fbd6b1c 112 global $REQUEST_URI, $PATH_INFO, $PHP_SELF;
f9903ed0 113
7fbd6b1c 114 if ($REQUEST_URI) {
115 return $REQUEST_URI;
116 } else if ($PATH_INFO) {
117 return $PATH_INFO;
118 } else if ($PHP_SELF) {
119 return $PHP_SELF;
120 } else {
121 return "";
122 }
f9903ed0 123}
124
125
126
127function qualified_me() {
128// like me() but returns a full URL
129
7fbd6b1c 130 global $HTTPS, $SERVER_PROTOCOL, $HTTP_HOST;
f9903ed0 131
132 $protocol = (isset($HTTPS) && $HTTPS == "on") ? "https://" : "http://";
133 $url_prefix = "$protocol$HTTP_HOST";
134 return $url_prefix . me();
135}
136
137
138function match_referer($good_referer = "") {
139// returns true if the referer is the same as the good_referer. If
140// good_refer is not specified, use qualified_me as the good_referer
141
142 if ($good_referer == "") { $good_referer = qualified_me(); }
143 return $good_referer == get_referer();
144}
145
146
3fe3851d 147function stri_replace($find, $replace, $string ) {
148// This does a search and replace, ignoring case
149// This function is only here because one doesn't exist yet in PHP
150// Unlike str_replace(), this only works on single values (not arrays)
151
152 $parts = explode(strtolower($find), strtolower($string));
153
154 $pos = 0;
155
156 foreach ($parts as $key => $part) {
157 $parts[$key] = substr($string, $pos, strlen($part));
158 $pos += strlen($part) + strlen($find);
159 }
160
161 return (join($replace, $parts));
162}
163
f9903ed0 164function read_template($filename, &$var) {
165// return a (big) string containing the contents of a template file with all
166// the variables interpolated. all the variables must be in the $var[] array or
167// object (whatever you decide to use).
168//
169// WARNING: do not use this on big files!!
170
171 $temp = str_replace("\\", "\\\\", implode(file($filename), ""));
172 $temp = str_replace('"', '\"', $temp);
173 eval("\$template = \"$temp\";");
174 return $template;
175}
176
177function checked(&$var, $set_value = 1, $unset_value = 0) {
178// if variable is set, set it to the set_value otherwise set it to the
179// unset_value. used to handle checkboxes when you are expecting them from
180// a form
181
182 if (empty($var)) {
183 $var = $unset_value;
184 } else {
185 $var = $set_value;
186 }
187}
188
189function frmchecked(&$var, $true_value = "checked", $false_value = "") {
190// prints the word "checked" if a variable is true, otherwise prints nothing,
191// used for printing the word "checked" in a checkbox form input
192
193 if ($var) {
194 echo $true_value;
195 } else {
196 echo $false_value;
197 }
198}
199
200
65cf9fc3 201function link_to_popup_window ($url, $name="popup", $linkname="click here", $height=400, $width=500, $title="Popup window") {
f9903ed0 202// This will create a HTML link that will work on both
203// Javascript and non-javascript browsers.
204// Relies on the Javascript function openpopup in javascript.php
205// $url must be relative to home page eg /mod/survey/stuff.php
206
ff80e012 207 global $CFG;
208
f9903ed0 209 echo "\n<SCRIPT language=\"Javascript\">";
210 echo "\n<!--";
65cf9fc3 211 echo "\ndocument.write('<A TITLE=\"$title\" HREF=javascript:openpopup(\"$url\",\"$name\",\"$height\",\"$width\") >$linkname</A>');";
f9903ed0 212 echo "\n//-->";
213 echo "\n</SCRIPT>";
ff80e012 214 echo "\n<NOSCRIPT>\n<A TARGET=\"$name\" TITLE=\"$title\" HREF=\"$CFG->wwwroot/$url\">$linkname</A>\n</NOSCRIPT>\n";
f9903ed0 215
216}
217
218function close_window_button() {
219 echo "<FORM><CENTER>";
220 echo "<INPUT TYPE=button onClick=\"self.close();\" VALUE=\"Close this window\">";
221 echo "</CENTER></FORM>";
222}
223
224
08056730 225function choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", $nothingvalue="0", $return=false) {
f9903ed0 226// $options["value"]["label"]
227
618b22c5 228 if ($nothing == "choose") {
229 $nothing = get_string("choose")."...";
230 }
231
f9903ed0 232 if ($script) {
233 $javascript = "onChange=\"$script\"";
234 }
08056730 235 $output = "<SELECT NAME=$name $javascript>\n";
bda8d43a 236 if ($nothing) {
08056730 237 $output .= " <OPTION VALUE=\"$nothingvalue\"\n";
bda8d43a 238 if ($nothingvalue == $selected) {
08056730 239 $output .= " SELECTED";
bda8d43a 240 }
08056730 241 $output .= ">$nothing</OPTION>\n";
873960de 242 }
f9903ed0 243 foreach ($options as $value => $label) {
08056730 244 $output .= " <OPTION VALUE=\"$value\"";
f9903ed0 245 if ($value == $selected) {
08056730 246 $output .= " SELECTED";
f9903ed0 247 }
248 if ($label) {
08056730 249 $output .= ">$label</OPTION>\n";
f9903ed0 250 } else {
08056730 251 $output .= ">$value</OPTION>\n";
f9903ed0 252 }
253 }
08056730 254 $output .= "</SELECT>\n";
255
256 if ($return) {
257 return $output;
258 } else {
259 echo $output;
260 }
f9903ed0 261}
262
618b22c5 263function popup_form ($common, $options, $formname, $selected="", $nothing="choose") {
f9903ed0 264// Implements a complete little popup form
265// $common = the URL up to the point of the variable that changes
266// $options = A list of value-label pairs for the popup list
267// $formname = name must be unique on the page
268// $selected = the option that is already selected
269// $nothing = The label for the "no choice" option
270
618b22c5 271 if ($nothing == "choose") {
272 $nothing = get_string("choose")."...";
273 }
274
f9903ed0 275 echo "<FORM NAME=$formname>";
276 echo "<SELECT NAME=popup onChange=\"window.location=document.$formname.popup.options[document.$formname.popup.selectedIndex].value\">\n";
277
278 if ($nothing != "") {
279 echo " <OPTION VALUE=\"javascript:void(0)\">$nothing</OPTION>\n";
280 }
281
282 foreach ($options as $value => $label) {
283 echo " <OPTION VALUE=\"$common$value\"";
284 if ($value == $selected) {
285 echo " SELECTED";
286 }
287 if ($label) {
288 echo ">$label</OPTION>\n";
289 } else {
290 echo ">$value</OPTION>\n";
291 }
292 }
293 echo "</SELECT></FORM>\n";
294}
295
296
297
298function formerr($error) {
299 if (!empty($error)) {
300 echo "<font color=#ff0000>$error</font>";
301 }
302}
303
304
305function validate_email ($address) {
306// Validates an email to make it makes sense.
307 return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.
308 '@'.
309 '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
310 '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
311 $address));
312}
313
314
315function get_slash_arguments($i=0) {
316// Extracts arguments from "/foo/bar/something"
317// eg http://mysite.com/script.php/foo/bar/something
318// Might only work on Apache
319
320 global $PATH_INFO;
321
322 if (!isset($PATH_INFO)) {
323 return false;
324 }
325
e2d89725 326 if (strpos($PATH_INFO, "..")) { // check for funny business
327 return false;
328 }
329
f9903ed0 330 $args = explode("/", $PATH_INFO);
331
332 if ($i) { // return just the required argument
333 return $args[$i];
334
335 } else { // return the whole array
336 array_shift($args); // get rid of the empty first one
337 return $args;
338 }
339}
340
0095d5cd 341function format_text_menu() {
342 return array (FORMAT_MOODLE => get_string("formattext"),
343 FORMAT_HTML => get_string("formathtml") );
344}
345
346function format_text($text, $format, $options=NULL) {
347// Given text in a variety of format codings, this function returns
348// the text as safe HTML.
349//
350// $text is raw text (originally from a user)
351// $format is one of the format constants, defined above
352
353 switch ($format) {
354 case FORMAT_MOODLE:
c9dda990 355 if (!isset($options->smiley)) {
356 $options->smiley=true;
357 }
358 if (!isset($options->para)) {
1a072208 359 $options->para=true;
c9dda990 360 }
0095d5cd 361 return text_to_html($text, $options->smiley, $options->para);
362 break;
f9903ed0 363
0095d5cd 364 case FORMAT_HTML:
1a072208 365 $text = replace_smilies($text);
3fe3851d 366 return $text;
0095d5cd 367 break;
368 }
369}
370
371
372function clean_text($text, $format) {
b7a3cf49 373// Given raw text (eg typed in by a user), this function cleans it up
374// and removes any nasty tags that could mess up Moodle pages.
375
3fe3851d 376 global $JAVASCRIPT_TAGS, $ALLOWED_TAGS;
377
378 switch ($format) { // Does the same thing, currently, but it's nice to have the option
0095d5cd 379 case FORMAT_MOODLE:
3fe3851d 380 $text = strip_tags($text, $ALLOWED_TAGS);
381 foreach ($JAVASCRIPT_TAGS as $tag) {
382 $text = stri_replace($tag, "", $text);
383 }
384 return $text;
0095d5cd 385
386 case FORMAT_HTML:
3fe3851d 387 $text = strip_tags($text, $ALLOWED_TAGS);
388 foreach ($JAVASCRIPT_TAGS as $tag) {
389 $text = stri_replace($tag, "", $text);
390 }
391 return $text;
0095d5cd 392 }
b7a3cf49 393}
f9903ed0 394
1a072208 395function replace_smilies($text) {
396 global $CFG, $SMILEY_TEXT, $SMILEY_IMAGE;
b7a3cf49 397
1a072208 398 return str_replace($SMILEY_TEXT, $SMILEY_IMAGE, $text);
399}
0095d5cd 400
909f539d 401function text_to_html($text, $smiley=true, $para=true) {
f9903ed0 402// Given plain text, makes it into HTML as nicely as possible.
3fe3851d 403// May contain most HTML tags
f9903ed0 404
7b3be1b1 405 // Remove any whitespace that may be between HTML tags
406 $text = eregi_replace(">([[:space:]]+)<", "><", $text);
407
408 // Remove any returns that precede or follow HTML tags
0eae8049 409 $text = eregi_replace("([\n\r])<", " <", $text);
410 $text = eregi_replace(">([\n\r])", "> ", $text);
7b3be1b1 411
3fe3851d 412 // Make lone URLs into links. eg http://moodle.com/
0be05df0 413 $text = eregi_replace("([\n\r ([])([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
3fe3851d 414 "\\1<A HREF=\"\\2://\\3\\4\" TARGET=\"newpage\">\\2://\\3\\4</A>", $text);
f9903ed0 415
416 // eg www.moodle.com
417 $text = eregi_replace("([[:space:]])www.([^[:space:]]*)([[:alnum:]#?/&=])",
418 "\\1<A HREF=\"http://www.\\2\\3\" TARGET=\"newpage\">www.\\2\\3</A>", $text);
419
420 // Make returns into HTML newlines.
421 $text = nl2br($text);
422
423 // Turn smileys into images.
d69cb7f4 424 if ($smiley) {
1a072208 425 $text = replace_smilies($text);
d69cb7f4 426 }
f9903ed0 427
909f539d 428 if ($para) {
429 return "<P>".$text."</P>";
430 } else {
431 return $text;
432 }
f9903ed0 433}
434
5af78ed2 435function highlight($needle, $haystack) {
436// This function will highlight instances of $needle in $haystack
437
438 $parts = explode(strtolower($needle), strtolower($haystack));
439
440 $pos = 0;
441
442 foreach ($parts as $key => $part) {
443 $parts[$key] = substr($haystack, $pos, strlen($part));
444 $pos += strlen($part);
445
446 $parts[$key] .= "<SPAN CLASS=highlight>".substr($haystack, $pos, strlen($needle))."</SPAN>";
447 $pos += strlen($needle);
448 }
449
450 return (join('', $parts));
451}
452
f9903ed0 453
454?>