f9903ed0 |
1 | <?PHP // $Id$ |
2 | |
3 | // weblib.php |
4 | // |
5 | // Library of useful PHP functions related to web pages. |
6 | // |
7 | // |
8 | |
9 | function nvl(&$var, $default="") { |
10 | // if $var is undefined, return $default, otherwise return $var |
11 | |
12 | return isset($var) ? $var : $default; |
13 | } |
14 | |
15 | function 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 | |
23 | function 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 | |
31 | function 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 | |
38 | function 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 | |
46 | function 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 | |
56 | function 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 | |
64 | function 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 | |
85 | function 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 | |
98 | function 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 | |
107 | function 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 | |
120 | function 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 | |
132 | function 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 | |
144 | function 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<!--"; |
f3ecd2c8 |
152 | echo "\ndocument.write('<A TITLE=\"Popup\" HREF=javascript:openpopup(\"$url\",\"$name\",\"$height\",\"$width\") >$linkname</A>');"; |
f9903ed0 |
153 | echo "\n//-->"; |
154 | echo "\n</SCRIPT>"; |
f3ecd2c8 |
155 | echo "\n<NOSCRIPT>\n<A TARGET=\"$name\" TITLE=\"Popup\" HREF=\"$url\">$linkname</A>\n</NOSCRIPT>\n"; |
f9903ed0 |
156 | |
157 | } |
158 | |
159 | function 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 | |
873960de |
166 | function choose_from_menu ($options, $name, $selected="", $nothing="Choose...", $script="", $nothingvalue="0") { |
f9903ed0 |
167 | // $options["value"]["label"] |
168 | |
169 | if ($script) { |
170 | $javascript = "onChange=\"$script\""; |
171 | } |
172 | echo "<SELECT NAME=$name $javascript>\n"; |
bda8d43a |
173 | if ($nothing) { |
174 | echo " <OPTION VALUE=\"$nothingvalue\"\n"; |
175 | if ($nothingvalue == $selected) { |
176 | echo " SELECTED"; |
177 | } |
178 | echo ">$nothing</OPTION>\n"; |
873960de |
179 | } |
f9903ed0 |
180 | foreach ($options as $value => $label) { |
181 | echo " <OPTION VALUE=\"$value\""; |
182 | if ($value == $selected) { |
183 | echo " SELECTED"; |
184 | } |
185 | if ($label) { |
186 | echo ">$label</OPTION>\n"; |
187 | } else { |
188 | echo ">$value</OPTION>\n"; |
189 | } |
190 | } |
191 | echo "</SELECT>\n"; |
192 | } |
193 | |
194 | function popup_form ($common, $options, $formname, $selected="", $nothing="Choose...") { |
195 | // Implements a complete little popup form |
196 | // $common = the URL up to the point of the variable that changes |
197 | // $options = A list of value-label pairs for the popup list |
198 | // $formname = name must be unique on the page |
199 | // $selected = the option that is already selected |
200 | // $nothing = The label for the "no choice" option |
201 | |
202 | echo "<FORM NAME=$formname>"; |
203 | echo "<SELECT NAME=popup onChange=\"window.location=document.$formname.popup.options[document.$formname.popup.selectedIndex].value\">\n"; |
204 | |
205 | if ($nothing != "") { |
206 | echo " <OPTION VALUE=\"javascript:void(0)\">$nothing</OPTION>\n"; |
207 | } |
208 | |
209 | foreach ($options as $value => $label) { |
210 | echo " <OPTION VALUE=\"$common$value\""; |
211 | if ($value == $selected) { |
212 | echo " SELECTED"; |
213 | } |
214 | if ($label) { |
215 | echo ">$label</OPTION>\n"; |
216 | } else { |
217 | echo ">$value</OPTION>\n"; |
218 | } |
219 | } |
220 | echo "</SELECT></FORM>\n"; |
221 | } |
222 | |
223 | |
224 | |
225 | function formerr($error) { |
226 | if (!empty($error)) { |
227 | echo "<font color=#ff0000>$error</font>"; |
228 | } |
229 | } |
230 | |
231 | |
232 | function validate_email ($address) { |
233 | // Validates an email to make it makes sense. |
234 | return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'. |
235 | '@'. |
236 | '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'. |
237 | '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', |
238 | $address)); |
239 | } |
240 | |
241 | |
242 | function get_slash_arguments($i=0) { |
243 | // Extracts arguments from "/foo/bar/something" |
244 | // eg http://mysite.com/script.php/foo/bar/something |
245 | // Might only work on Apache |
246 | |
247 | global $PATH_INFO; |
248 | |
249 | if (!isset($PATH_INFO)) { |
250 | return false; |
251 | } |
252 | |
253 | $args = explode("/", $PATH_INFO); |
254 | |
255 | if ($i) { // return just the required argument |
256 | return $args[$i]; |
257 | |
258 | } else { // return the whole array |
259 | array_shift($args); // get rid of the empty first one |
260 | return $args; |
261 | } |
262 | } |
263 | |
264 | |
b7a3cf49 |
265 | function cleantext($text) { |
266 | // Given raw text (eg typed in by a user), this function cleans it up |
267 | // and removes any nasty tags that could mess up Moodle pages. |
268 | |
8c7dc440 |
269 | return strip_tags($text, '<b><i><u><font><ol><ul><li>'); |
b7a3cf49 |
270 | } |
f9903ed0 |
271 | |
b7a3cf49 |
272 | |
d69cb7f4 |
273 | function text_to_html($text, $smiley=true) { |
f9903ed0 |
274 | // Given plain text, makes it into HTML as nicely as possible. |
275 | |
b7a3cf49 |
276 | global $CFG; |
277 | |
f9903ed0 |
278 | // Make URLs into links. eg http://moodle.com/ |
279 | $text = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", |
280 | "<A HREF=\"\\1://\\2\\3\" TARGET=\"newpage\">\\1://\\2\\3</A>", $text); |
281 | |
282 | // eg www.moodle.com |
283 | $text = eregi_replace("([[:space:]])www.([^[:space:]]*)([[:alnum:]#?/&=])", |
284 | "\\1<A HREF=\"http://www.\\2\\3\" TARGET=\"newpage\">www.\\2\\3</A>", $text); |
285 | |
286 | // Make returns into HTML newlines. |
287 | $text = nl2br($text); |
288 | |
289 | // Turn smileys into images. |
290 | |
d69cb7f4 |
291 | if ($smiley) { |
292 | $text = ereg_replace(":)", "<IMG ALT=\"{smile}\" SRC=\"$CFG->wwwroot/pix/s/smiley.gif\">", $text); |
293 | $text = ereg_replace(":-)", "<IMG ALT=\"{smile}\" SRC=\"$CFG->wwwroot/pix/s/smiley.gif\">", $text); |
294 | $text = ereg_replace(":-D", "<IMG ALT=\"{grin}\" SRC=\"$CFG->wwwroot/pix/s/biggrin.gif\">", $text); |
295 | $text = ereg_replace(";-)", "<IMG ALT=\"{wink}\" SRC=\"$CFG->wwwroot/pix/s/wink.gif\">", $text); |
296 | $text = ereg_replace("8-)", "<IMG ALT=\"{wide-eyed}\" SRC=\"$CFG->wwwroot/pix/s/wideeyes.gif\">", $text); |
297 | $text = ereg_replace(":-\(","<IMG ALT=\"{sad}\" SRC=\"$CFG->wwwroot/pix/s/sad.gif\">", $text); |
298 | $text = ereg_replace(":-P", "<IMG ALT=\"{tongue-out}\" SRC=\"$CFG->wwwroot/pix/s/tongueout.gif\">", $text); |
299 | $text = ereg_replace(":-/", "<IMG ALT=\"{mixed}\" SRC=\"$CFG->wwwroot/pix/s/mixed.gif\">", $text); |
300 | $text = ereg_replace(":-o", "<IMG ALT=\"{surprised}\" SRC=\"$CFG->wwwroot/pix/s/surprise.gif\">", $text); |
301 | $text = ereg_replace("B-)", "<IMG ALT=\"{cool}\" SRC=\"$CFG->wwwroot/pix/s/cool.gif\">", $text); |
302 | } |
f9903ed0 |
303 | |
304 | return "<P>".$text."</P>"; |
305 | } |
306 | |
5af78ed2 |
307 | function highlight($needle, $haystack) { |
308 | // This function will highlight instances of $needle in $haystack |
309 | |
310 | $parts = explode(strtolower($needle), strtolower($haystack)); |
311 | |
312 | $pos = 0; |
313 | |
314 | foreach ($parts as $key => $part) { |
315 | $parts[$key] = substr($haystack, $pos, strlen($part)); |
316 | $pos += strlen($part); |
317 | |
318 | $parts[$key] .= "<SPAN CLASS=highlight>".substr($haystack, $pos, strlen($needle))."</SPAN>"; |
319 | $pos += strlen($needle); |
320 | } |
321 | |
322 | return (join('', $parts)); |
323 | } |
324 | |
f9903ed0 |
325 | |
326 | ?> |