Initial revision
[moodle.git] / lib / weblib.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
3// weblib.php
4//
5// Library of useful PHP functions related to web pages.
6//
7//
8
9function nvl(&$var, $default="") {
10// if $var is undefined, return $default, otherwise return $var
11
12 return isset($var) ? $var : $default;
13}
14
15function ov(&$var) {
16// returns $var with the HTML characters (like "<", ">", etc.) properly quoted,
17// or if $var is undefined, will return an empty string. note this function
18// must be called with a variable, for normal strings or functions use o()
19
20 return isset($var) ? htmlSpecialChars(stripslashes($var)) : "";
21}
22
23function pv(&$var) {
24// prints $var with the HTML characters (like "<", ">", etc.) properly quoted,
25// or if $var is undefined, will print an empty string. note this function
26// must be called with a variable, for normal strings or functions use p()
27
28 echo isset($var) ? htmlSpecialChars(stripslashes($var)) : "";
29}
30
31function o($var) {
32// returns $var with HTML characters (like "<", ">", etc.) properly quoted,
33// or if $var is empty, will return an empty string.
34
35 return empty($var) ? "" : htmlSpecialChars(stripslashes($var));
36}
37
38function p($var) {
39// prints $var with HTML characters (like "<", ">", etc.) properly quoted,
40// or if $var is empty, will print an empty string.
41
42 echo empty($var) ? "" : htmlSpecialChars(stripslashes($var));
43}
44
45
46function strip_querystring($url) {
47// takes a URL and returns it without the querystring portion
48
49 if ($commapos = strpos($url, '?')) {
50 return substr($url, 0, $commapos);
51 } else {
52 return $url;
53 }
54}
55
56function get_referer() {
57// returns the URL of the HTTP_REFERER, less the querystring portion
58
59 $HTTP_REFERER = getenv("HTTP_REFERER");
60 return strip_querystring(nvl($HTTP_REFERER));
61}
62
63
64function me() {
65// returns the name of the current script, WITH the querystring portion.
66// this function is necessary because PHP_SELF and REQUEST_URI and PATH_INFO
67// return different things depending on a lot of things like your OS, Web
68// server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.)
69
70 if (getenv("REQUEST_URI")) {
71 $me = getenv("REQUEST_URI");
72
73 } elseif (getenv("PATH_INFO")) {
74 $me = getenv("PATH_INFO");
75
76 } elseif ($GLOBALS["PHP_SELF"]) {
77 $me = $GLOBALS["PHP_SELF"];
78 }
79
80 return $me;
81}
82
83
84
85function qualified_me() {
86// like me() but returns a full URL
87
88 $HTTPS = getenv("HTTPS");
89 $SERVER_PROTOCOL = getenv("SERVER_PROTOCOL");
90 $HTTP_HOST = getenv("HTTP_HOST");
91
92 $protocol = (isset($HTTPS) && $HTTPS == "on") ? "https://" : "http://";
93 $url_prefix = "$protocol$HTTP_HOST";
94 return $url_prefix . me();
95}
96
97
98function match_referer($good_referer = "") {
99// returns true if the referer is the same as the good_referer. If
100// good_refer is not specified, use qualified_me as the good_referer
101
102 if ($good_referer == "") { $good_referer = qualified_me(); }
103 return $good_referer == get_referer();
104}
105
106
107function read_template($filename, &$var) {
108// return a (big) string containing the contents of a template file with all
109// the variables interpolated. all the variables must be in the $var[] array or
110// object (whatever you decide to use).
111//
112// WARNING: do not use this on big files!!
113
114 $temp = str_replace("\\", "\\\\", implode(file($filename), ""));
115 $temp = str_replace('"', '\"', $temp);
116 eval("\$template = \"$temp\";");
117 return $template;
118}
119
120function checked(&$var, $set_value = 1, $unset_value = 0) {
121// if variable is set, set it to the set_value otherwise set it to the
122// unset_value. used to handle checkboxes when you are expecting them from
123// a form
124
125 if (empty($var)) {
126 $var = $unset_value;
127 } else {
128 $var = $set_value;
129 }
130}
131
132function frmchecked(&$var, $true_value = "checked", $false_value = "") {
133// prints the word "checked" if a variable is true, otherwise prints nothing,
134// used for printing the word "checked" in a checkbox form input
135
136 if ($var) {
137 echo $true_value;
138 } else {
139 echo $false_value;
140 }
141}
142
143
144function link_to_popup_window ($url, $name="popup", $linkname="click here", $height=400, $width=500) {
145// This will create a HTML link that will work on both
146// Javascript and non-javascript browsers.
147// Relies on the Javascript function openpopup in javascript.php
148// $url must be relative to home page eg /mod/survey/stuff.php
149
150 echo "\n<SCRIPT language=\"Javascript\">";
151 echo "\n<!--";
152 echo "\ndocument.write('<A HREF=javascript:openpopup(\"$url\",\"$name\",\"$height\",\"$width\") >$linkname</A>');";
153 echo "\n//-->";
154 echo "\n</SCRIPT>";
155 echo "\n<NOSCRIPT>\n<A TARGET=\"$name\" HREF=\"$url\">$linkname</A>\n</NOSCRIPT>\n";
156
157}
158
159function close_window_button() {
160 echo "<FORM><CENTER>";
161 echo "<INPUT TYPE=button onClick=\"self.close();\" VALUE=\"Close this window\">";
162 echo "</CENTER></FORM>";
163}
164
165
166function choose_from_menu ($options, $name, $selected="", $nothing="Choose...", $script="") {
167// $options["value"]["label"]
168
169 if ($script) {
170 $javascript = "onChange=\"$script\"";
171 }
172 echo "<SELECT NAME=$name $javascript>\n";
173 echo " <OPTION VALUE=0>$nothing</OPTION>\n";
174 foreach ($options as $value => $label) {
175 echo " <OPTION VALUE=\"$value\"";
176 if ($value == $selected) {
177 echo " SELECTED";
178 }
179 if ($label) {
180 echo ">$label</OPTION>\n";
181 } else {
182 echo ">$value</OPTION>\n";
183 }
184 }
185 echo "</SELECT>\n";
186}
187
188function popup_form ($common, $options, $formname, $selected="", $nothing="Choose...") {
189// Implements a complete little popup form
190// $common = the URL up to the point of the variable that changes
191// $options = A list of value-label pairs for the popup list
192// $formname = name must be unique on the page
193// $selected = the option that is already selected
194// $nothing = The label for the "no choice" option
195
196 echo "<FORM NAME=$formname>";
197 echo "<SELECT NAME=popup onChange=\"window.location=document.$formname.popup.options[document.$formname.popup.selectedIndex].value\">\n";
198
199 if ($nothing != "") {
200 echo " <OPTION VALUE=\"javascript:void(0)\">$nothing</OPTION>\n";
201 }
202
203 foreach ($options as $value => $label) {
204 echo " <OPTION VALUE=\"$common$value\"";
205 if ($value == $selected) {
206 echo " SELECTED";
207 }
208 if ($label) {
209 echo ">$label</OPTION>\n";
210 } else {
211 echo ">$value</OPTION>\n";
212 }
213 }
214 echo "</SELECT></FORM>\n";
215}
216
217
218
219function formerr($error) {
220 if (!empty($error)) {
221 echo "<font color=#ff0000>$error</font>";
222 }
223}
224
225
226function validate_email ($address) {
227// Validates an email to make it makes sense.
228 return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.
229 '@'.
230 '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
231 '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
232 $address));
233}
234
235
236function get_slash_arguments($i=0) {
237// Extracts arguments from "/foo/bar/something"
238// eg http://mysite.com/script.php/foo/bar/something
239// Might only work on Apache
240
241 global $PATH_INFO;
242
243 if (!isset($PATH_INFO)) {
244 return false;
245 }
246
247 $args = explode("/", $PATH_INFO);
248
249 if ($i) { // return just the required argument
250 return $args[$i];
251
252 } else { // return the whole array
253 array_shift($args); // get rid of the empty first one
254 return $args;
255 }
256}
257
258
259function text_to_html($text) {
260 global $CFG;
261
262// Given plain text, makes it into HTML as nicely as possible.
263
264 // Make URLs into links. eg http://moodle.com/
265 $text = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
266 "<A HREF=\"\\1://\\2\\3\" TARGET=\"newpage\">\\1://\\2\\3</A>", $text);
267
268 // eg www.moodle.com
269 $text = eregi_replace("([[:space:]])www.([^[:space:]]*)([[:alnum:]#?/&=])",
270 "\\1<A HREF=\"http://www.\\2\\3\" TARGET=\"newpage\">www.\\2\\3</A>", $text);
271
272 // Make returns into HTML newlines.
273 $text = nl2br($text);
274
275 // Turn smileys into images.
276
277 $text = ereg_replace(":-)", "<IMG ALT=smile SRC=$CFG->wwwroot/pix/s/smiley.gif>", $text);
278 $text = ereg_replace(":-D", "<IMG ALT=grin SRC=$CFG->wwwroot/pix/s/biggrin.gif>", $text);
279 $text = ereg_replace(";-)", "<IMG ALT=wink SRC=$CFG->wwwroot/pix/s/wink.gif>", $text);
280 $text = ereg_replace("8-)", "<IMG ALT=wide-eyed SRC=$CFG->wwwroot/pix/s/wideeyes.gif>", $text);
281 $text = ereg_replace(":-\(", "<IMG ALT=sad SRC=$CFG->wwwroot/pix/s/sad.gif>", $text);
282 $text = ereg_replace(":-P", "<IMG ALT=tongue-out SRC=$CFG->wwwroot/pix/s/tongueout.gif>", $text);
283 $text = ereg_replace(":-/", "<IMG ALT=mixed SRC=$CFG->wwwroot/pix/s/mixed.gif>", $text);
284 $text = ereg_replace(":-o", "<IMG ALT=surprised SRC=$CFG->wwwroot/pix/s/surprise.gif>", $text);
285 $text = ereg_replace("B-)", "<IMG ALT=cool SRC=$CFG->wwwroot/pix/s/cool.gif>", $text);
286
287 return "<P>".$text."</P>";
288}
289
290
291?>