Modified this help file to describe the new smilies
[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
15
16/// Functions
17
8553b700 18function s($var) {
f9903ed0 19// returns $var with HTML characters (like "<", ">", etc.) properly quoted,
20// or if $var is empty, will return an empty string.
21
22 return empty($var) ? "" : htmlSpecialChars(stripslashes($var));
23}
24
25function p($var) {
26// prints $var with HTML characters (like "<", ">", etc.) properly quoted,
27// or if $var is empty, will print an empty string.
28
29 echo empty($var) ? "" : htmlSpecialChars(stripslashes($var));
30}
31
8553b700 32function nvl(&$var, $default="") {
33// if $var is undefined, return $default, otherwise return $var
34
35 return isset($var) ? $var : $default;
36}
f9903ed0 37
38function strip_querystring($url) {
39// takes a URL and returns it without the querystring portion
40
41 if ($commapos = strpos($url, '?')) {
42 return substr($url, 0, $commapos);
43 } else {
44 return $url;
45 }
46}
47
48function get_referer() {
49// returns the URL of the HTTP_REFERER, less the querystring portion
50
7fbd6b1c 51 global $HTTP_REFERER;
8553b700 52 return strip_querystring(nvl($HTTP_REFERER));
f9903ed0 53}
54
f9903ed0 55function me() {
56// returns the name of the current script, WITH the querystring portion.
57// this function is necessary because PHP_SELF and REQUEST_URI and PATH_INFO
58// return different things depending on a lot of things like your OS, Web
59// server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.)
60
7fbd6b1c 61 global $REQUEST_URI, $PATH_INFO, $PHP_SELF;
f9903ed0 62
7fbd6b1c 63 if ($REQUEST_URI) {
64 return $REQUEST_URI;
65 } else if ($PATH_INFO) {
66 return $PATH_INFO;
67 } else if ($PHP_SELF) {
68 return $PHP_SELF;
69 } else {
70 return "";
71 }
f9903ed0 72}
73
74
75
76function qualified_me() {
77// like me() but returns a full URL
78
7fbd6b1c 79 global $HTTPS, $SERVER_PROTOCOL, $HTTP_HOST;
f9903ed0 80
81 $protocol = (isset($HTTPS) && $HTTPS == "on") ? "https://" : "http://";
82 $url_prefix = "$protocol$HTTP_HOST";
83 return $url_prefix . me();
84}
85
86
87function match_referer($good_referer = "") {
88// returns true if the referer is the same as the good_referer. If
89// good_refer is not specified, use qualified_me as the good_referer
90
91 if ($good_referer == "") { $good_referer = qualified_me(); }
92 return $good_referer == get_referer();
93}
94
95
96function read_template($filename, &$var) {
97// return a (big) string containing the contents of a template file with all
98// the variables interpolated. all the variables must be in the $var[] array or
99// object (whatever you decide to use).
100//
101// WARNING: do not use this on big files!!
102
103 $temp = str_replace("\\", "\\\\", implode(file($filename), ""));
104 $temp = str_replace('"', '\"', $temp);
105 eval("\$template = \"$temp\";");
106 return $template;
107}
108
109function checked(&$var, $set_value = 1, $unset_value = 0) {
110// if variable is set, set it to the set_value otherwise set it to the
111// unset_value. used to handle checkboxes when you are expecting them from
112// a form
113
114 if (empty($var)) {
115 $var = $unset_value;
116 } else {
117 $var = $set_value;
118 }
119}
120
121function frmchecked(&$var, $true_value = "checked", $false_value = "") {
122// prints the word "checked" if a variable is true, otherwise prints nothing,
123// used for printing the word "checked" in a checkbox form input
124
125 if ($var) {
126 echo $true_value;
127 } else {
128 echo $false_value;
129 }
130}
131
132
65cf9fc3 133function link_to_popup_window ($url, $name="popup", $linkname="click here", $height=400, $width=500, $title="Popup window") {
f9903ed0 134// This will create a HTML link that will work on both
135// Javascript and non-javascript browsers.
136// Relies on the Javascript function openpopup in javascript.php
137// $url must be relative to home page eg /mod/survey/stuff.php
138
139 echo "\n<SCRIPT language=\"Javascript\">";
140 echo "\n<!--";
65cf9fc3 141 echo "\ndocument.write('<A TITLE=\"$title\" HREF=javascript:openpopup(\"$url\",\"$name\",\"$height\",\"$width\") >$linkname</A>');";
f9903ed0 142 echo "\n//-->";
143 echo "\n</SCRIPT>";
65cf9fc3 144 echo "\n<NOSCRIPT>\n<A TARGET=\"$name\" TITLE=\"$title\" HREF=\"$url\">$linkname</A>\n</NOSCRIPT>\n";
f9903ed0 145
146}
147
148function close_window_button() {
149 echo "<FORM><CENTER>";
150 echo "<INPUT TYPE=button onClick=\"self.close();\" VALUE=\"Close this window\">";
151 echo "</CENTER></FORM>";
152}
153
154
08056730 155function choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", $nothingvalue="0", $return=false) {
f9903ed0 156// $options["value"]["label"]
157
618b22c5 158 if ($nothing == "choose") {
159 $nothing = get_string("choose")."...";
160 }
161
f9903ed0 162 if ($script) {
163 $javascript = "onChange=\"$script\"";
164 }
08056730 165 $output = "<SELECT NAME=$name $javascript>\n";
bda8d43a 166 if ($nothing) {
08056730 167 $output .= " <OPTION VALUE=\"$nothingvalue\"\n";
bda8d43a 168 if ($nothingvalue == $selected) {
08056730 169 $output .= " SELECTED";
bda8d43a 170 }
08056730 171 $output .= ">$nothing</OPTION>\n";
873960de 172 }
f9903ed0 173 foreach ($options as $value => $label) {
08056730 174 $output .= " <OPTION VALUE=\"$value\"";
f9903ed0 175 if ($value == $selected) {
08056730 176 $output .= " SELECTED";
f9903ed0 177 }
178 if ($label) {
08056730 179 $output .= ">$label</OPTION>\n";
f9903ed0 180 } else {
08056730 181 $output .= ">$value</OPTION>\n";
f9903ed0 182 }
183 }
08056730 184 $output .= "</SELECT>\n";
185
186 if ($return) {
187 return $output;
188 } else {
189 echo $output;
190 }
f9903ed0 191}
192
618b22c5 193function popup_form ($common, $options, $formname, $selected="", $nothing="choose") {
f9903ed0 194// Implements a complete little popup form
195// $common = the URL up to the point of the variable that changes
196// $options = A list of value-label pairs for the popup list
197// $formname = name must be unique on the page
198// $selected = the option that is already selected
199// $nothing = The label for the "no choice" option
200
618b22c5 201 if ($nothing == "choose") {
202 $nothing = get_string("choose")."...";
203 }
204
f9903ed0 205 echo "<FORM NAME=$formname>";
206 echo "<SELECT NAME=popup onChange=\"window.location=document.$formname.popup.options[document.$formname.popup.selectedIndex].value\">\n";
207
208 if ($nothing != "") {
209 echo " <OPTION VALUE=\"javascript:void(0)\">$nothing</OPTION>\n";
210 }
211
212 foreach ($options as $value => $label) {
213 echo " <OPTION VALUE=\"$common$value\"";
214 if ($value == $selected) {
215 echo " SELECTED";
216 }
217 if ($label) {
218 echo ">$label</OPTION>\n";
219 } else {
220 echo ">$value</OPTION>\n";
221 }
222 }
223 echo "</SELECT></FORM>\n";
224}
225
226
227
228function formerr($error) {
229 if (!empty($error)) {
230 echo "<font color=#ff0000>$error</font>";
231 }
232}
233
234
235function validate_email ($address) {
236// Validates an email to make it makes sense.
237 return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.
238 '@'.
239 '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
240 '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
241 $address));
242}
243
244
245function get_slash_arguments($i=0) {
246// Extracts arguments from "/foo/bar/something"
247// eg http://mysite.com/script.php/foo/bar/something
248// Might only work on Apache
249
250 global $PATH_INFO;
251
252 if (!isset($PATH_INFO)) {
253 return false;
254 }
255
e2d89725 256 if (strpos($PATH_INFO, "..")) { // check for funny business
257 return false;
258 }
259
f9903ed0 260 $args = explode("/", $PATH_INFO);
261
262 if ($i) { // return just the required argument
263 return $args[$i];
264
265 } else { // return the whole array
266 array_shift($args); // get rid of the empty first one
267 return $args;
268 }
269}
270
0095d5cd 271function format_text_menu() {
272 return array (FORMAT_MOODLE => get_string("formattext"),
273 FORMAT_HTML => get_string("formathtml") );
274}
275
276function format_text($text, $format, $options=NULL) {
277// Given text in a variety of format codings, this function returns
278// the text as safe HTML.
279//
280// $text is raw text (originally from a user)
281// $format is one of the format constants, defined above
282
283 switch ($format) {
284 case FORMAT_MOODLE:
285 return text_to_html($text, $options->smiley, $options->para);
286 break;
f9903ed0 287
0095d5cd 288 case FORMAT_HTML:
289 return $text; // Is re-cleaning needed?
290 break;
291 }
292}
293
294
295function clean_text($text, $format) {
b7a3cf49 296// Given raw text (eg typed in by a user), this function cleans it up
297// and removes any nasty tags that could mess up Moodle pages.
298
0095d5cd 299 switch ($format) {
300 case FORMAT_MOODLE:
301 return strip_tags($text, '<b><i><u><font><ol><ul><dl><li><dt><dd><h1><h2><h3><hr>');
302 break;
303
304 case FORMAT_HTML:
305 return $text; // XX May want to add some cleaning on this.
306 break;
307 }
b7a3cf49 308}
f9903ed0 309
b7a3cf49 310
0095d5cd 311
909f539d 312function text_to_html($text, $smiley=true, $para=true) {
f9903ed0 313// Given plain text, makes it into HTML as nicely as possible.
314
b7a3cf49 315 global $CFG;
316
7b3be1b1 317 // Remove any whitespace that may be between HTML tags
318 $text = eregi_replace(">([[:space:]]+)<", "><", $text);
319
320 // Remove any returns that precede or follow HTML tags
0eae8049 321 $text = eregi_replace("([\n\r])<", " <", $text);
322 $text = eregi_replace(">([\n\r])", "> ", $text);
7b3be1b1 323
f9903ed0 324 // Make URLs into links. eg http://moodle.com/
325 $text = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
326 "<A HREF=\"\\1://\\2\\3\" TARGET=\"newpage\">\\1://\\2\\3</A>", $text);
327
328 // eg www.moodle.com
329 $text = eregi_replace("([[:space:]])www.([^[:space:]]*)([[:alnum:]#?/&=])",
330 "\\1<A HREF=\"http://www.\\2\\3\" TARGET=\"newpage\">www.\\2\\3</A>", $text);
331
332 // Make returns into HTML newlines.
333 $text = nl2br($text);
334
335 // Turn smileys into images.
336
d69cb7f4 337 if ($smiley) {
503eced3 338 $text = ereg_replace(":)", "<IMG ALT=\"{smile}\" WIDTH=16 HEIGHT=16 SRC=\"$CFG->wwwroot/pix/s/smiley.gif\">", $text);
339 $text = ereg_replace(":-)", "<IMG ALT=\"{smile}\" WIDTH=16 HEIGHT=16 SRC=\"$CFG->wwwroot/pix/s/smiley.gif\">", $text);
340 $text = ereg_replace(":-D", "<IMG ALT=\"{grin}\" WIDTH=16 HEIGHT=16 SRC=\"$CFG->wwwroot/pix/s/biggrin.gif\">", $text);
341 $text = ereg_replace(";-)", "<IMG ALT=\"{wink}\" WIDTH=16 HEIGHT=16 SRC=\"$CFG->wwwroot/pix/s/wink.gif\">", $text);
342 $text = ereg_replace("8-)", "<IMG ALT=\"{wide-eyed}\" WIDTH=16 HEIGHT=16 SRC=\"$CFG->wwwroot/pix/s/wideeyes.gif\">", $text);
343 $text = ereg_replace(":-\(","<IMG ALT=\"{sad}\" WIDTH=16 HEIGHT=16 SRC=\"$CFG->wwwroot/pix/s/sad.gif\">", $text);
344 $text = ereg_replace(":-P", "<IMG ALT=\"{tongue-out}\" WIDTH=16 HEIGHT=16 SRC=\"$CFG->wwwroot/pix/s/tongueout.gif\">", $text);
345 $text = ereg_replace(":-/", "<IMG ALT=\"{mixed}\" WIDTH=16 HEIGHT=16 SRC=\"$CFG->wwwroot/pix/s/mixed.gif\">", $text);
ddd9a896 346 $text = ereg_replace(":-o", "<IMG ALT=\"{surprised}\" WIDTH=29 HEIGHT=16 SRC=\"$CFG->wwwroot/pix/s/surprise.gif\">", $text);
503eced3 347 $text = ereg_replace("B-)", "<IMG ALT=\"{cool}\" WIDTH=16 HEIGHT=16 SRC=\"$CFG->wwwroot/pix/s/cool.gif\">", $text);
d69cb7f4 348 }
f9903ed0 349
909f539d 350 if ($para) {
351 return "<P>".$text."</P>";
352 } else {
353 return $text;
354 }
f9903ed0 355}
356
5af78ed2 357function highlight($needle, $haystack) {
358// This function will highlight instances of $needle in $haystack
359
360 $parts = explode(strtolower($needle), strtolower($haystack));
361
362 $pos = 0;
363
364 foreach ($parts as $key => $part) {
365 $parts[$key] = substr($haystack, $pos, strlen($part));
366 $pos += strlen($part);
367
368 $parts[$key] .= "<SPAN CLASS=highlight>".substr($haystack, $pos, strlen($needle))."</SPAN>";
369 $pos += strlen($needle);
370 }
371
372 return (join('', $parts));
373}
374
f9903ed0 375
376?>