abf45bed |
1 | <?php // $Id$ |
f9903ed0 |
2 | |
9fa49e22 |
3 | /////////////////////////////////////////////////////////////////////////// |
4 | // // |
5 | // NOTICE OF COPYRIGHT // |
6 | // // |
7 | // Moodle - Modular Object-Oriented Dynamic Learning Environment // |
8 | // http://moodle.com // |
9 | // // |
77344659 |
10 | // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // |
9fa49e22 |
11 | // // |
12 | // This program is free software; you can redistribute it and/or modify // |
13 | // it under the terms of the GNU General Public License as published by // |
14 | // the Free Software Foundation; either version 2 of the License, or // |
15 | // (at your option) any later version. // |
16 | // // |
17 | // This program is distributed in the hope that it will be useful, // |
18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
20 | // GNU General Public License for more details: // |
21 | // // |
22 | // http://www.gnu.org/copyleft/gpl.html // |
23 | // // |
24 | /////////////////////////////////////////////////////////////////////////// |
f9903ed0 |
25 | |
7cf1c7bd |
26 | /** |
27 | * Library of functions for web output |
28 | * |
29 | * Library of all general-purpose Moodle PHP functions and constants |
30 | * that produce HTML output |
31 | * |
32 | * Other main libraries: |
33 | * - datalib.php - functions that access the database. |
34 | * - moodlelib.php - general-purpose Moodle functions. |
35 | * @author Martin Dougiamas |
549c2dd2 |
36 | * @version $Id$ |
89dcb99d |
37 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
7cf1c7bd |
38 | * @package moodlecore |
39 | */ |
772e78be |
40 | |
0095d5cd |
41 | /// Constants |
42 | |
c1d57101 |
43 | /// Define text formatting types ... eventually we can add Wiki, BBcode etc |
7cf1c7bd |
44 | |
45 | /** |
46 | * Does all sorts of transformations and filtering |
47 | */ |
b0ccd3fb |
48 | define('FORMAT_MOODLE', '0'); // Does all sorts of transformations and filtering |
7cf1c7bd |
49 | |
50 | /** |
51 | * Plain HTML (with some tags stripped) |
52 | */ |
b0ccd3fb |
53 | define('FORMAT_HTML', '1'); // Plain HTML (with some tags stripped) |
7cf1c7bd |
54 | |
55 | /** |
56 | * Plain text (even tags are printed in full) |
57 | */ |
b0ccd3fb |
58 | define('FORMAT_PLAIN', '2'); // Plain text (even tags are printed in full) |
7cf1c7bd |
59 | |
60 | /** |
61 | * Wiki-formatted text |
6a6495ff |
62 | * Deprecated: left here just to note that '3' is not used (at the moment) |
63 | * and to catch any latent wiki-like text (which generates an error) |
7cf1c7bd |
64 | */ |
b0ccd3fb |
65 | define('FORMAT_WIKI', '3'); // Wiki-formatted text |
7cf1c7bd |
66 | |
67 | /** |
68 | * Markdown-formatted text http://daringfireball.net/projects/markdown/ |
69 | */ |
b0ccd3fb |
70 | define('FORMAT_MARKDOWN', '4'); // Markdown-formatted text http://daringfireball.net/projects/markdown/ |
0095d5cd |
71 | |
7d8a3cb0 |
72 | /** |
73 | * TRUSTTEXT marker - if present in text, text cleaning should be bypassed |
74 | */ |
75 | define('TRUSTTEXT', '#####TRUSTTEXT#####'); |
76 | |
7cf1c7bd |
77 | |
d795bfdb |
78 | /** |
79 | * Javascript related defines |
80 | */ |
81 | define('REQUIREJS_BEFOREHEADER', 0); |
82 | define('REQUIREJS_INHEADER', 1); |
83 | define('REQUIREJS_AFTERHEADER', 2); |
84 | |
7cf1c7bd |
85 | /** |
86 | * Allowed tags - string of html tags that can be tested against for safe html tags |
87 | * @global string $ALLOWED_TAGS |
88 | */ |
5ea4af22 |
89 | global $ALLOWED_TAGS; |
39dda0fc |
90 | $ALLOWED_TAGS = |
cf34d0ea |
91 | '<p><br><b><i><u><font><table><tbody><thead><tfoot><span><div><tr><td><th><ol><ul><dl><li><dt><dd><h1><h2><h3><h4><h5><h6><hr><img><a><strong><emphasis><em><sup><sub><address><cite><blockquote><pre><strike><param><acronym><nolink><lang><tex><algebra><math><mi><mn><mo><mtext><mspace><ms><mrow><mfrac><msqrt><mroot><mstyle><merror><mpadded><mphantom><mfenced><msub><msup><msubsup><munder><mover><munderover><mmultiscripts><mtable><mtr><mtd><maligngroup><malignmark><maction><cn><ci><apply><reln><fn><interval><inverse><sep><condition><declare><lambda><compose><ident><quotient><exp><factorial><divide><max><min><minus><plus><power><rem><times><root><gcd><and><or><xor><not><implies><forall><exists><abs><conjugate><eq><neq><gt><lt><geq><leq><ln><log><int><diff><partialdiff><lowlimit><uplimit><bvar><degree><set><list><union><intersect><in><notin><subset><prsubset><notsubset><notprsubset><setdiff><sum><product><limit><tendsto><mean><sdev><variance><median><mode><moment><vector><matrix><matrixrow><determinant><transpose><selector><annotation><semantics><annotation-xml><tt><code>'; |
d046ae55 |
92 | |
037dcbb6 |
93 | /** |
94 | * Allowed protocols - array of protocols that are safe to use in links and so on |
95 | * @global string $ALLOWED_PROTOCOLS |
96 | */ |
f941df22 |
97 | $ALLOWED_PROTOCOLS = array('http', 'https', 'ftp', 'news', 'mailto', 'rtsp', 'teamspeak', 'gopher', 'mms', |
f697a421 |
98 | 'color', 'callto', 'cursor', 'text-align', 'font-size', 'font-weight', 'font-style', 'font-family', |
62097c91 |
99 | 'border', 'margin', 'padding', 'background', 'background-color', 'text-decoration'); // CSS as well to get through kses |
037dcbb6 |
100 | |
101 | |
0095d5cd |
102 | /// Functions |
103 | |
7cf1c7bd |
104 | /** |
105 | * Add quotes to HTML characters |
106 | * |
107 | * Returns $var with HTML characters (like "<", ">", etc.) properly quoted. |
108 | * This function is very similar to {@link p()} |
109 | * |
110 | * @param string $var the string potentially containing HTML characters |
b4cf9371 |
111 | * @param boolean $obsolete no longer used. |
7cf1c7bd |
112 | * @return string |
113 | */ |
b4cf9371 |
114 | function s($var, $obsolete = false) { |
d4a42ff4 |
115 | |
63e554d0 |
116 | if ($var == '0') { // for integer 0, boolean false, string '0' |
117 | return '0'; |
3662bce5 |
118 | } |
d4a42ff4 |
119 | |
0c34c7eb |
120 | return preg_replace("/&(#\d+);/i", "&$1;", htmlspecialchars($var)); |
f9903ed0 |
121 | } |
122 | |
7cf1c7bd |
123 | /** |
124 | * Add quotes to HTML characters |
125 | * |
d48b00b4 |
126 | * Prints $var with HTML characters (like "<", ">", etc.) properly quoted. |
7cf1c7bd |
127 | * This function is very similar to {@link s()} |
128 | * |
129 | * @param string $var the string potentially containing HTML characters |
b4cf9371 |
130 | * @param boolean $obsolete no longer used. |
7cf1c7bd |
131 | * @return string |
132 | */ |
b4cf9371 |
133 | function p($var, $obsolete = false) { |
134 | echo s($var, $obsolete); |
f9903ed0 |
135 | } |
136 | |
0d1cd0ea |
137 | /** |
138 | * Does proper javascript quoting. |
5ce73257 |
139 | * Do not use addslashes anymore, because it does not work when magic_quotes_sybase is enabled. |
140 | * |
e2cd3ed0 |
141 | * @since 1.8 - 22/02/2007 |
0d1cd0ea |
142 | * @param mixed value |
143 | * @return mixed quoted result |
144 | */ |
145 | function addslashes_js($var) { |
146 | if (is_string($var)) { |
147 | $var = str_replace('\\', '\\\\', $var); |
148 | $var = str_replace(array('\'', '"', "\n", "\r", "\0"), array('\\\'', '\\"', '\\n', '\\r', '\\0'), $var); |
4702be4e |
149 | $var = str_replace('</', '<\/', $var); // XHTML compliance |
0d1cd0ea |
150 | } else if (is_array($var)) { |
151 | $var = array_map('addslashes_js', $var); |
152 | } else if (is_object($var)) { |
153 | $a = get_object_vars($var); |
154 | foreach ($a as $key=>$value) { |
155 | $a[$key] = addslashes_js($value); |
156 | } |
157 | $var = (object)$a; |
158 | } |
159 | return $var; |
160 | } |
7cf1c7bd |
161 | |
7cf1c7bd |
162 | /** |
163 | * Remove query string from url |
164 | * |
165 | * Takes in a URL and returns it without the querystring portion |
166 | * |
167 | * @param string $url the url which may have a query string attached |
168 | * @return string |
169 | */ |
170 | function strip_querystring($url) { |
f9903ed0 |
171 | |
b9b8ab69 |
172 | if ($commapos = strpos($url, '?')) { |
173 | return substr($url, 0, $commapos); |
174 | } else { |
175 | return $url; |
176 | } |
f9903ed0 |
177 | } |
178 | |
7cf1c7bd |
179 | /** |
c8135a35 |
180 | * Returns the URL of the HTTP_REFERER, less the querystring portion if required |
9ea04325 |
181 | * @param boolean $stripquery if true, also removes the query part of the url. |
7cf1c7bd |
182 | * @return string |
183 | */ |
c8135a35 |
184 | function get_referer($stripquery=true) { |
d90ffc1f |
185 | if (isset($_SERVER['HTTP_REFERER'])) { |
c8135a35 |
186 | if ($stripquery) { |
187 | return strip_querystring($_SERVER['HTTP_REFERER']); |
188 | } else { |
189 | return $_SERVER['HTTP_REFERER']; |
190 | } |
d90ffc1f |
191 | } else { |
5ce73257 |
192 | return ''; |
d90ffc1f |
193 | } |
f9903ed0 |
194 | } |
195 | |
c1d57101 |
196 | |
7cf1c7bd |
197 | /** |
198 | * Returns the name of the current script, WITH the querystring portion. |
199 | * this function is necessary because PHP_SELF and REQUEST_URI and SCRIPT_NAME |
200 | * return different things depending on a lot of things like your OS, Web |
201 | * server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.) |
d48b00b4 |
202 | * <b>NOTE:</b> This function returns false if the global variables needed are not set. |
203 | * |
7cf1c7bd |
204 | * @return string |
205 | */ |
206 | function me() { |
11e7b506 |
207 | global $ME; |
208 | return $ME; |
f9903ed0 |
209 | } |
210 | |
7cf1c7bd |
211 | /** |
d48b00b4 |
212 | * Like {@link me()} but returns a full URL |
7cf1c7bd |
213 | * @see me() |
7cf1c7bd |
214 | * @return string |
215 | */ |
f9903ed0 |
216 | function qualified_me() { |
11e7b506 |
217 | global $FULLME; |
218 | return $FULLME; |
f9903ed0 |
219 | } |
220 | |
360e503e |
221 | /** |
222 | * Class for creating and manipulating urls. |
84e3d2cc |
223 | * |
fae60ee3 |
224 | * See short write up here http://docs.moodle.org/en/Development:lib/weblib.php_moodle_url |
360e503e |
225 | */ |
226 | class moodle_url { |
7ceb61d8 |
227 | protected $scheme = ''; // e.g. http |
228 | protected $host = ''; |
229 | protected $port = ''; |
230 | protected $user = ''; |
231 | protected $pass = ''; |
232 | protected $path = ''; |
233 | protected $fragment = ''; |
234 | protected $params = array(); // Associative array of query string params |
84e3d2cc |
235 | |
360e503e |
236 | /** |
237 | * Pass no arguments to create a url that refers to this page. Use empty string to create empty url. |
84e3d2cc |
238 | * |
75781f87 |
239 | * @param mixed $url a number of different forms are accespted: |
240 | * null - create a URL that is the same as the URL used to load this page, but with no query string |
241 | * '' - and empty URL |
242 | * string - a URL, will be parsed into it's bits, including query string |
243 | * array - as returned from the PHP function parse_url |
244 | * moodle_url - make a copy of another moodle_url |
7ceb61d8 |
245 | * @param array $params these params override anything in the query string |
246 | * where params have the same name. |
360e503e |
247 | */ |
7ceb61d8 |
248 | public function __construct($url = null, $params = array()) { |
75781f87 |
249 | if ($url === '') { |
250 | // Leave URL blank. |
251 | } else if (is_a($url, 'moodle_url')) { |
252 | $this->scheme = $url->scheme; |
253 | $this->host = $url->host; |
254 | $this->port = $url->port; |
255 | $this->user = $url->user; |
256 | $this->pass = $url->pass; |
ad52c04f |
257 | $this->path = $url->path; |
75781f87 |
258 | $this->fragment = $url->fragment; |
259 | $this->params = $url->params; |
260 | } else { |
7ceb61d8 |
261 | if ($url === null) { |
75781f87 |
262 | global $ME; |
11e7b506 |
263 | $url = $ME; |
360e503e |
264 | } |
75781f87 |
265 | if (is_string($url)) { |
266 | $url = parse_url($url); |
267 | } |
268 | $parts = $url; |
7ceb61d8 |
269 | if ($parts === FALSE) { |
75781f87 |
270 | throw new moodle_exception('invalidurl'); |
360e503e |
271 | } |
7ceb61d8 |
272 | if (isset($parts['query'])) { |
24a905f9 |
273 | parse_str(str_replace('&', '&', $parts['query']), $this->params); |
360e503e |
274 | } |
275 | unset($parts['query']); |
7ceb61d8 |
276 | foreach ($parts as $key => $value) { |
360e503e |
277 | $this->$key = $value; |
278 | } |
279 | } |
75781f87 |
280 | $this->params($params); |
84e3d2cc |
281 | } |
7ceb61d8 |
282 | |
360e503e |
283 | /** |
84e3d2cc |
284 | * Add an array of params to the params for this page. The added params override existing ones if they |
360e503e |
285 | * have the same name. |
286 | * |
7ceb61d8 |
287 | * @param array $params Defaults to null. If null then return value of param 'name'. |
c1f41c59 |
288 | * @return array params for url. |
360e503e |
289 | */ |
7ceb61d8 |
290 | public function params($params = null) { |
291 | if (!is_null($params)) { |
c1f41c59 |
292 | return $this->params = $params + $this->params; |
293 | } else { |
294 | return $this->params; |
295 | } |
360e503e |
296 | } |
84e3d2cc |
297 | |
360e503e |
298 | /** |
75781f87 |
299 | * Remove all params if no arguments passed. Remove selected params if |
300 | * arguments are passed. Can be called as either remove_params('param1', 'param2') |
301 | * or remove_params(array('param1', 'param2')). |
360e503e |
302 | * |
75781f87 |
303 | * @param mixed $params either an array of param names, or a string param name, |
304 | * @param string $params,... any number of additional param names. |
360e503e |
305 | */ |
49127522 |
306 | public function remove_params($params = NULL) { |
75781f87 |
307 | if (empty($params)) { |
360e503e |
308 | $this->params = array(); |
75781f87 |
309 | return; |
310 | } |
311 | if (!is_array($params)) { |
312 | $params = func_get_args(); |
313 | } |
314 | foreach ($params as $param) { |
315 | if (isset($this->params[$param])) { |
316 | unset($this->params[$param]); |
317 | } |
360e503e |
318 | } |
319 | } |
320 | |
321 | /** |
84e3d2cc |
322 | * Add a param to the params for this page. The added param overrides existing one if they |
360e503e |
323 | * have the same name. |
324 | * |
325 | * @param string $paramname name |
7ceb61d8 |
326 | * @param string $param value. Defaults to null. If null then return value of param 'name' |
360e503e |
327 | */ |
7ceb61d8 |
328 | public function param($paramname, $param = null) { |
329 | if (!is_null($param)) { |
c1f41c59 |
330 | $this->params = array($paramname => $param) + $this->params; |
331 | } else { |
5762b36e |
332 | return $this->params[$paramname]; |
c1f41c59 |
333 | } |
360e503e |
334 | } |
335 | |
7ceb61d8 |
336 | /** |
337 | * Get the params as as a query string. |
338 | * @param array $overrideparams params to add to the output params, these |
339 | * override existing ones with the same name. |
340 | * @return string query string that can be added to a url. |
341 | */ |
342 | public function get_query_string($overrideparams = array()) { |
360e503e |
343 | $arr = array(); |
344 | $params = $overrideparams + $this->params; |
7ceb61d8 |
345 | foreach ($params as $key => $val) { |
360e503e |
346 | $arr[] = urlencode($key)."=".urlencode($val); |
347 | } |
dc1f7683 |
348 | return implode($arr, "&"); |
360e503e |
349 | } |
7ceb61d8 |
350 | |
360e503e |
351 | /** |
352 | * Outputs params as hidden form elements. |
fcdb06c4 |
353 | * |
fae60ee3 |
354 | * @param array $exclude params to ignore |
355 | * @param integer $indent indentation |
f44e4d14 |
356 | * @param array $overrideparams params to add to the output params, these |
7ceb61d8 |
357 | * override existing ones with the same name. |
360e503e |
358 | * @return string html for form elements. |
359 | */ |
7ceb61d8 |
360 | public function hidden_params_out($exclude = array(), $indent = 0, $overrideparams=array()) { |
360e503e |
361 | $tabindent = str_repeat("\t", $indent); |
362 | $str = ''; |
f44e4d14 |
363 | $params = $overrideparams + $this->params; |
7ceb61d8 |
364 | foreach ($params as $key => $val) { |
fcdb06c4 |
365 | if (FALSE === array_search($key, $exclude)) { |
83bc64db |
366 | $val = s($val); |
fcdb06c4 |
367 | $str.= "$tabindent<input type=\"hidden\" name=\"$key\" value=\"$val\" />\n"; |
368 | } |
360e503e |
369 | } |
370 | return $str; |
371 | } |
7ceb61d8 |
372 | |
360e503e |
373 | /** |
374 | * Output url |
84e3d2cc |
375 | * |
7130fb21 |
376 | * @param boolean $omitquerystring whether to output page params as a query string in the url. |
360e503e |
377 | * @param array $overrideparams params to add to the output url, these override existing ones with the same name. |
378 | * @return string url |
379 | */ |
7130fb21 |
380 | public function out($omitquerystring = false, $overrideparams = array()) { |
360e503e |
381 | $uri = $this->scheme ? $this->scheme.':'.((strtolower($this->scheme) == 'mailto') ? '':'//'): ''; |
382 | $uri .= $this->user ? $this->user.($this->pass? ':'.$this->pass:'').'@':''; |
383 | $uri .= $this->host ? $this->host : ''; |
384 | $uri .= $this->port ? ':'.$this->port : ''; |
385 | $uri .= $this->path ? $this->path : ''; |
7130fb21 |
386 | if (!$omitquerystring) { |
387 | $querystring = $this->get_query_string($overrideparams); |
388 | if ($querystring) { |
389 | $uri .= '?' . $querystring; |
390 | } |
360e503e |
391 | } |
392 | $uri .= $this->fragment ? '#'.$this->fragment : ''; |
84e3d2cc |
393 | return $uri; |
360e503e |
394 | } |
7ceb61d8 |
395 | |
360e503e |
396 | /** |
397 | * Output action url with sesskey |
84e3d2cc |
398 | * |
360e503e |
399 | * @param boolean $noquerystring whether to output page params as a query string in the url. |
400 | * @return string url |
401 | */ |
7ceb61d8 |
402 | public function out_action($overrideparams = array()) { |
360e503e |
403 | $overrideparams = array('sesskey'=> sesskey()) + $overrideparams; |
dc1f7683 |
404 | return $this->out(false, $overrideparams); |
360e503e |
405 | } |
406 | } |
407 | |
7cf1c7bd |
408 | /** |
409 | * Determine if there is data waiting to be processed from a form |
410 | * |
411 | * Used on most forms in Moodle to check for data |
412 | * Returns the data as an object, if it's found. |
413 | * This object can be used in foreach loops without |
414 | * casting because it's cast to (array) automatically |
772e78be |
415 | * |
9c0f063b |
416 | * Checks that submitted POST data exists and returns it as object. |
d48b00b4 |
417 | * |
9c0f063b |
418 | * @return mixed false or object |
7cf1c7bd |
419 | */ |
294ce987 |
420 | function data_submitted() { |
d48b00b4 |
421 | |
607809b3 |
422 | if (empty($_POST)) { |
36b4f985 |
423 | return false; |
424 | } else { |
294ce987 |
425 | return (object)$_POST; |
36b4f985 |
426 | } |
427 | } |
428 | |
7cf1c7bd |
429 | /** |
d48b00b4 |
430 | * Given some normal text this function will break up any |
431 | * long words to a given size by inserting the given character |
432 | * |
6aaa17c7 |
433 | * It's multibyte savvy and doesn't change anything inside html tags. |
434 | * |
7cf1c7bd |
435 | * @param string $string the string to be modified |
89dcb99d |
436 | * @param int $maxsize maximum length of the string to be returned |
7cf1c7bd |
437 | * @param string $cutchar the string used to represent word breaks |
438 | * @return string |
439 | */ |
4a5644e5 |
440 | function break_up_long_words($string, $maxsize=20, $cutchar=' ') { |
a2b3f884 |
441 | |
6aaa17c7 |
442 | /// Loading the textlib singleton instance. We are going to need it. |
443 | $textlib = textlib_get_instance(); |
8f7dc7f1 |
444 | |
6aaa17c7 |
445 | /// First of all, save all the tags inside the text to skip them |
446 | $tags = array(); |
447 | filter_save_tags($string,$tags); |
5b07d990 |
448 | |
6aaa17c7 |
449 | /// Process the string adding the cut when necessary |
4a5644e5 |
450 | $output = ''; |
810944af |
451 | $length = $textlib->strlen($string); |
4a5644e5 |
452 | $wordlength = 0; |
453 | |
454 | for ($i=0; $i<$length; $i++) { |
810944af |
455 | $char = $textlib->substr($string, $i, 1); |
6aaa17c7 |
456 | if ($char == ' ' or $char == "\t" or $char == "\n" or $char == "\r" or $char == "<" or $char == ">") { |
4a5644e5 |
457 | $wordlength = 0; |
458 | } else { |
459 | $wordlength++; |
460 | if ($wordlength > $maxsize) { |
461 | $output .= $cutchar; |
462 | $wordlength = 0; |
463 | } |
464 | } |
465 | $output .= $char; |
466 | } |
6aaa17c7 |
467 | |
468 | /// Finally load the tags back again |
469 | if (!empty($tags)) { |
470 | $output = str_replace(array_keys($tags), $tags, $output); |
471 | } |
472 | |
4a5644e5 |
473 | return $output; |
474 | } |
475 | |
7cf1c7bd |
476 | /** |
fc5e9c40 |
477 | * This function will print a button/link/etc. form element |
478 | * that will work on both Javascript and non-javascript browsers. |
7cf1c7bd |
479 | * Relies on the Javascript function openpopup in javascript.php |
d48b00b4 |
480 | * |
fc5e9c40 |
481 | * All parameters default to null, only $type and $url are mandatory. |
482 | * |
7cf1c7bd |
483 | * $url must be relative to home page eg /mod/survey/stuff.php |
bed9cec8 |
484 | * @param string $url Web link. Either relative to $CFG->wwwroot, or a full URL. |
73f7ad71 |
485 | * @param string $name Name to be assigned to the popup window (this is used by |
a5fe6177 |
486 | * client-side scripts to "talk" to the popup window) |
7cf1c7bd |
487 | * @param string $linkname Text to be displayed as web link |
89dcb99d |
488 | * @param int $height Height to assign to popup window |
489 | * @param int $width Height to assign to popup window |
7cf1c7bd |
490 | * @param string $title Text to be displayed as popup page title |
491 | * @param string $options List of additional options for popup window |
fc5e9c40 |
492 | * @param string $return If true, return as a string, otherwise print |
493 | * @param string $id id added to the element |
494 | * @param string $class class added to the element |
7cf1c7bd |
495 | * @return string |
496 | * @uses $CFG |
497 | */ |
fc5e9c40 |
498 | function element_to_popup_window ($type=null, $url=null, $name=null, $linkname=null, |
c5659019 |
499 | $height=400, $width=500, $title=null, |
fc5e9c40 |
500 | $options=null, $return=false, $id=null, $class=null) { |
f9903ed0 |
501 | |
c5659019 |
502 | if (is_null($url)) { |
b867e602 |
503 | debugging('You must give the url to display in the popup. URL is missing - can\'t create popup window.', DEBUG_DEVELOPER); |
2416a6f7 |
504 | } |
ff80e012 |
505 | |
fc5e9c40 |
506 | global $CFG; |
f9903ed0 |
507 | |
2416a6f7 |
508 | if ($options == 'none') { // 'none' is legacy, should be removed in v2.0 |
c5659019 |
509 | $options = null; |
fc5e9c40 |
510 | } |
511 | |
512 | // add some sane default options for popup windows |
c5659019 |
513 | if (!$options) { |
514 | $options = 'menubar=0,location=0,scrollbars,resizable'; |
2416a6f7 |
515 | } |
c5659019 |
516 | if ($width) { |
517 | $options .= ',width='. $width; |
2416a6f7 |
518 | } |
c5659019 |
519 | if ($height) { |
520 | $options .= ',height='. $height; |
2416a6f7 |
521 | } |
c5659019 |
522 | if ($id) { |
523 | $id = ' id="'.$id.'" '; |
2416a6f7 |
524 | } |
c5659019 |
525 | if ($class) { |
526 | $class = ' class="'.$class.'" '; |
2416a6f7 |
527 | } |
b867e602 |
528 | if ($name) { |
4ba6b3e3 |
529 | $_name = $name; |
530 | if (($name = preg_replace("/\s/", '_', $name)) != $_name) { |
531 | debugging('The $name of a popup window shouldn\'t contain spaces - string modified. '. $_name .' changed to '. $name, DEBUG_DEVELOPER); |
532 | } |
b867e602 |
533 | } else { |
a5fe6177 |
534 | $name = 'popup'; |
2416a6f7 |
535 | } |
73f7ad71 |
536 | |
a5fe6177 |
537 | // get some default string, using the localized version of legacy defaults |
32ba2f0d |
538 | if (is_null($linkname) || $linkname === '') { |
5df984eb |
539 | $linkname = get_string('clickhere'); |
2416a6f7 |
540 | } |
c5659019 |
541 | if (!$title) { |
5df984eb |
542 | $title = get_string('popupwindowname'); |
2416a6f7 |
543 | } |
fc5e9c40 |
544 | |
c5659019 |
545 | $fullscreen = 0; // must be passed to openpopup |
fc5e9c40 |
546 | $element = ''; |
547 | |
548 | switch ($type) { |
bed9cec8 |
549 | case 'button': |
fc5e9c40 |
550 | $element = '<input type="button" name="'. $name .'" title="'. $title .'" value="'. $linkname .'" '. $id . $class . |
551 | "onclick=\"return openpopup('$url', '$name', '$options', $fullscreen);\" />\n"; |
552 | break; |
bed9cec8 |
553 | case 'link': |
554 | // Add wwwroot only if the URL does not already start with http:// or https:// |
555 | if (!preg_match('|https?://|', $url)) { |
556 | $url = $CFG->wwwroot . $url; |
fc5e9c40 |
557 | } |
bed9cec8 |
558 | $element = '<a title="'. s(strip_tags($title)) .'" href="'. $url .'" '. |
fc5e9c40 |
559 | "onclick=\"this.target='$name'; return openpopup('$url', '$name', '$options', $fullscreen);\">$linkname</a>"; |
560 | break; |
561 | default : |
e49ef64a |
562 | print_error('cannotcreatepopupwin'); |
fc5e9c40 |
563 | break; |
c80b7585 |
564 | } |
565 | |
1f2eec7b |
566 | if ($return) { |
fc5e9c40 |
567 | return $element; |
1f2eec7b |
568 | } else { |
fc5e9c40 |
569 | echo $element; |
1f2eec7b |
570 | } |
f9903ed0 |
571 | } |
572 | |
7cf1c7bd |
573 | /** |
fc5e9c40 |
574 | * Creates and displays (or returns) a link to a popup window, using element_to_popup_window function. |
d48b00b4 |
575 | * |
fc5e9c40 |
576 | * @return string html code to display a link to a popup window. |
577 | * @see element_to_popup_window() |
7cf1c7bd |
578 | */ |
fc5e9c40 |
579 | function link_to_popup_window ($url, $name=null, $linkname=null, |
580 | $height=400, $width=500, $title=null, |
581 | $options=null, $return=false) { |
52f1b496 |
582 | |
fc5e9c40 |
583 | return element_to_popup_window('link', $url, $name, $linkname, $height, $width, $title, $options, $return, null, null); |
584 | } |
0f7d4e5e |
585 | |
fc5e9c40 |
586 | /** |
587 | * Creates and displays (or returns) a buttons to a popup window, using element_to_popup_window function. |
588 | * |
589 | * @return string html code to display a button to a popup window. |
590 | * @see element_to_popup_window() |
591 | */ |
592 | function button_to_popup_window ($url, $name=null, $linkname=null, |
593 | $height=400, $width=500, $title=null, $options=null, $return=false, |
594 | $id=null, $class=null) { |
52f1b496 |
595 | |
fc5e9c40 |
596 | return element_to_popup_window('button', $url, $name, $linkname, $height, $width, $title, $options, $return, $id, $class); |
52f1b496 |
597 | } |
598 | |
599 | |
7cf1c7bd |
600 | /** |
601 | * Prints a simple button to close a window |
9ea04325 |
602 | * @param string $name name of the window to close |
829eabfe |
603 | * @param boolean $return whether this function should return a string or output it. |
604 | * @param boolean $reloadopener if true, clicking the button will also reload |
605 | * the page that opend this popup window. |
9ea04325 |
606 | * @return string if $return is true, nothing otherwise |
7cf1c7bd |
607 | */ |
829eabfe |
608 | function close_window_button($name='closewindow', $return=false, $reloadopener = false) { |
38d5ab86 |
609 | global $CFG; |
610 | |
829eabfe |
611 | $js = 'self.close();'; |
612 | if ($reloadopener) { |
613 | $js = 'window.opener.location.reload(1);' . $js; |
614 | } |
615 | |
eaeaf964 |
616 | $output = ''; |
617 | |
4079b3d7 |
618 | $output .= '<div class="closewindow">' . "\n"; |
450522e3 |
619 | $output .= '<form action="#"><div>'; |
829eabfe |
620 | $output .= '<input type="button" onclick="' . $js . '" value="'.get_string($name).'" />'; |
38d5ab86 |
621 | $output .= '</div></form>'; |
4079b3d7 |
622 | $output .= '</div>' . "\n"; |
eaeaf964 |
623 | |
624 | if ($return) { |
625 | return $output; |
626 | } else { |
627 | echo $output; |
628 | } |
f9903ed0 |
629 | } |
630 | |
08396bb2 |
631 | /* |
b166403f |
632 | * Try and close the current window using JavaScript, either immediately, or after a delay. |
633 | * @param integer $delay a delay in seconds before closing the window. Default 0. |
634 | * @param boolean $reloadopener if true, we will see if this window was a pop-up, and try |
635 | * to reload the parent window before this one closes. |
08396bb2 |
636 | */ |
b166403f |
637 | function close_window($delay = 0, $reloadopener = false) { |
c13a5e71 |
638 | global $THEME, $PAGE; |
08396bb2 |
639 | |
c13a5e71 |
640 | if (!$PAGE->headerprinted) { |
b166403f |
641 | print_header(get_string('closewindow')); |
642 | } else { |
643 | print_container_end_all(false, $THEME->open_header_containers); |
644 | } |
645 | |
646 | if ($reloadopener) { |
647 | $function = 'close_window_reloading_opener'; |
648 | } else { |
649 | $function = 'close_window'; |
650 | } |
651 | echo '<p class="centerpara">' . get_string('windowclosing') . '</p>'; |
652 | print_delayed_js_call($delay, $function); |
653 | |
654 | print_footer('empty'); |
655 | exit; |
656 | } |
08396bb2 |
657 | |
d48b00b4 |
658 | /** |
75afe0e7 |
659 | * Given an array of values, output the HTML for a select element with those options. |
660 | * Normally, you only need to use the first few parameters. |
d48b00b4 |
661 | * |
75afe0e7 |
662 | * @param array $options The options to offer. An array of the form |
663 | * $options[{value}] = {text displayed for that option}; |
664 | * @param string $name the name of this form control, as in <select name="..." ... |
665 | * @param string $selected the option to select initially, default none. |
666 | * @param string $nothing The label for the 'nothing is selected' option. Defaults to get_string('choose'). |
667 | * Set this to '' if you don't want a 'nothing is selected' option. |
668 | * @param string $script in not '', then this is added to the <select> element as an onchange handler. |
669 | * @param string $nothingvalue The value corresponding to the $nothing option. Defaults to 0. |
670 | * @param boolean $return if false (the default) the the output is printed directly, If true, the |
671 | * generated HTML is returned as a string. |
672 | * @param boolean $disabled if true, the select is generated in a disabled state. Default, false. |
673 | * @param int $tabindex if give, sets the tabindex attribute on the <select> element. Default none. |
674 | * @param string $id value to use for the id attribute of the <select> element. If none is given, |
675 | * then a suitable one is constructed. |
1ea118e4 |
676 | * @param mixed $listbox if false, display as a dropdown menu. If true, display as a list box. |
677 | * By default, the list box will have a number of rows equal to min(10, count($options)), but if |
678 | * $listbox is an integer, that number is used for size instead. |
d7074a5d |
679 | * @param boolean $multiple if true, enable multiple selections, else only 1 item can be selected. Used |
680 | * when $listbox display is enabled |
681 | * @param string $class value to use for the class attribute of the <select> element. If none is given, |
682 | * then a suitable one is constructed. |
d48b00b4 |
683 | */ |
c06c8492 |
684 | function choose_from_menu ($options, $name, $selected='', $nothing='choose', $script='', |
1ea118e4 |
685 | $nothingvalue='0', $return=false, $disabled=false, $tabindex=0, |
d7074a5d |
686 | $id='', $listbox=false, $multiple=false, $class='') { |
ab9f24ad |
687 | |
b0ccd3fb |
688 | if ($nothing == 'choose') { |
689 | $nothing = get_string('choose') .'...'; |
618b22c5 |
690 | } |
691 | |
48e535bc |
692 | $attributes = ($script) ? 'onchange="'. $script .'"' : ''; |
693 | if ($disabled) { |
694 | $attributes .= ' disabled="disabled"'; |
f9903ed0 |
695 | } |
9c9f7d77 |
696 | |
25e5dbf9 |
697 | if ($tabindex) { |
698 | $attributes .= ' tabindex="'.$tabindex.'"'; |
699 | } |
700 | |
7850588a |
701 | if ($id ==='') { |
a1c91f9a |
702 | $id = 'menu'.$name; |
703 | // name may contaion [], which would make an invalid id. e.g. numeric question type editing form, assignment quickgrading |
704 | $id = str_replace('[', '', $id); |
705 | $id = str_replace(']', '', $id); |
7850588a |
706 | } |
707 | |
d7074a5d |
708 | if ($class ==='') { |
caec4b6f |
709 | $class = 'menu'.$name; |
d7074a5d |
710 | // name may contaion [], which would make an invalid class. e.g. numeric question type editing form, assignment quickgrading |
711 | $class = str_replace('[', '', $class); |
712 | $class = str_replace(']', '', $class); |
713 | } |
caec4b6f |
714 | $class = 'select ' . $class; /// Add 'select' selector always |
d7074a5d |
715 | |
1ea118e4 |
716 | if ($listbox) { |
717 | if (is_integer($listbox)) { |
718 | $size = $listbox; |
719 | } else { |
720 | $numchoices = count($options); |
721 | if ($nothing) { |
722 | $numchoices += 1; |
723 | } |
724 | $size = min(10, $numchoices); |
725 | } |
726 | $attributes .= ' size="' . $size . '"'; |
727 | if ($multiple) { |
728 | $attributes .= ' multiple="multiple"'; |
729 | } |
730 | } |
731 | |
d7074a5d |
732 | $output = '<select id="'. $id .'" class="'. $class .'" name="'. $name .'" '. $attributes .'>' . "\n"; |
bda8d43a |
733 | if ($nothing) { |
7850588a |
734 | $output .= ' <option value="'. s($nothingvalue) .'"'. "\n"; |
cec0a0fc |
735 | if ($nothingvalue === $selected) { |
b0ccd3fb |
736 | $output .= ' selected="selected"'; |
bda8d43a |
737 | } |
b0ccd3fb |
738 | $output .= '>'. $nothing .'</option>' . "\n"; |
873960de |
739 | } |
1ea118e4 |
740 | |
607809b3 |
741 | if (!empty($options)) { |
742 | foreach ($options as $value => $label) { |
7850588a |
743 | $output .= ' <option value="'. s($value) .'"'; |
1ea118e4 |
744 | if ((string)$value == (string)$selected || |
745 | (is_array($selected) && in_array($value, $selected))) { |
b0ccd3fb |
746 | $output .= ' selected="selected"'; |
607809b3 |
747 | } |
b0ccd3fb |
748 | if ($label === '') { |
749 | $output .= '>'. $value .'</option>' . "\n"; |
a20c1090 |
750 | } else { |
b0ccd3fb |
751 | $output .= '>'. $label .'</option>' . "\n"; |
607809b3 |
752 | } |
f9903ed0 |
753 | } |
754 | } |
b0ccd3fb |
755 | $output .= '</select>' . "\n"; |
08056730 |
756 | |
757 | if ($return) { |
758 | return $output; |
759 | } else { |
760 | echo $output; |
761 | } |
ab9f24ad |
762 | } |
f9903ed0 |
763 | |
73d4db84 |
764 | /** |
765 | * Choose value 0 or 1 from a menu with options 'No' and 'Yes'. |
766 | * Other options like choose_from_menu. |
9ea04325 |
767 | * @param string $name |
73f7ad71 |
768 | * @param string $selected |
9ea04325 |
769 | * @param string $string (defaults to '') |
770 | * @param boolean $return whether this function should return a string or output it (defaults to false) |
771 | * @param boolean $disabled (defaults to false) |
772 | * @param int $tabindex |
73d4db84 |
773 | */ |
774 | function choose_from_menu_yesno($name, $selected, $script = '', |
775 | $return = false, $disabled = false, $tabindex = 0) { |
776 | return choose_from_menu(array(get_string('no'), get_string('yes')), $name, |
777 | $selected, '', $script, '0', $return, $disabled, $tabindex); |
778 | } |
779 | |
14040797 |
780 | /** |
781 | * Just like choose_from_menu, but takes a nested array (2 levels) and makes a dropdown menu |
782 | * including option headings with the first level. |
783 | */ |
784 | function choose_from_menu_nested($options,$name,$selected='',$nothing='choose',$script = '', |
785 | $nothingvalue=0,$return=false,$disabled=false,$tabindex=0) { |
786 | |
787 | if ($nothing == 'choose') { |
788 | $nothing = get_string('choose') .'...'; |
789 | } |
790 | |
791 | $attributes = ($script) ? 'onchange="'. $script .'"' : ''; |
792 | if ($disabled) { |
793 | $attributes .= ' disabled="disabled"'; |
794 | } |
795 | |
796 | if ($tabindex) { |
797 | $attributes .= ' tabindex="'.$tabindex.'"'; |
798 | } |
799 | |
800 | $output = '<select id="menu'.$name.'" name="'. $name .'" '. $attributes .'>' . "\n"; |
801 | if ($nothing) { |
802 | $output .= ' <option value="'. $nothingvalue .'"'. "\n"; |
803 | if ($nothingvalue === $selected) { |
804 | $output .= ' selected="selected"'; |
805 | } |
806 | $output .= '>'. $nothing .'</option>' . "\n"; |
807 | } |
808 | if (!empty($options)) { |
809 | foreach ($options as $section => $values) { |
84e3d2cc |
810 | |
dacb47c0 |
811 | $output .= ' <optgroup label="'. s(format_string($section)) .'">'."\n"; |
14040797 |
812 | foreach ($values as $value => $label) { |
dacb47c0 |
813 | $output .= ' <option value="'. format_string($value) .'"'; |
f332bd02 |
814 | if ((string)$value == (string)$selected) { |
14040797 |
815 | $output .= ' selected="selected"'; |
816 | } |
817 | if ($label === '') { |
818 | $output .= '>'. $value .'</option>' . "\n"; |
819 | } else { |
820 | $output .= '>'. $label .'</option>' . "\n"; |
821 | } |
822 | } |
823 | $output .= ' </optgroup>'."\n"; |
824 | } |
825 | } |
826 | $output .= '</select>' . "\n"; |
827 | |
828 | if ($return) { |
829 | return $output; |
830 | } else { |
831 | echo $output; |
832 | } |
833 | } |
834 | |
835 | |
531a3cb2 |
836 | /** |
837 | * Given an array of values, creates a group of radio buttons to be part of a form |
c06c8492 |
838 | * |
531a3cb2 |
839 | * @param array $options An array of value-label pairs for the radio group (values as keys) |
840 | * @param string $name Name of the radiogroup (unique in the form) |
841 | * @param string $checked The value that is already checked |
842 | */ |
eaeaf964 |
843 | function choose_from_radio ($options, $name, $checked='', $return=false) { |
531a3cb2 |
844 | |
20cbef63 |
845 | static $idcounter = 0; |
846 | |
531a3cb2 |
847 | if (!$name) { |
848 | $name = 'unnamed'; |
849 | } |
850 | |
851 | $output = '<span class="radiogroup '.$name."\">\n"; |
852 | |
853 | if (!empty($options)) { |
854 | $currentradio = 0; |
855 | foreach ($options as $value => $label) { |
20cbef63 |
856 | $htmlid = 'auto-rb'.sprintf('%04d', ++$idcounter); |
857 | $output .= ' <span class="radioelement '.$name.' rb'.$currentradio."\">"; |
858 | $output .= '<input name="'.$name.'" id="'.$htmlid.'" type="radio" value="'.$value.'"'; |
531a3cb2 |
859 | if ($value == $checked) { |
860 | $output .= ' checked="checked"'; |
861 | } |
862 | if ($label === '') { |
20cbef63 |
863 | $output .= ' /> <label for="'.$htmlid.'">'. $value .'</label></span>' . "\n"; |
531a3cb2 |
864 | } else { |
20cbef63 |
865 | $output .= ' /> <label for="'.$htmlid.'">'. $label .'</label></span>' . "\n"; |
531a3cb2 |
866 | } |
867 | $currentradio = ($currentradio + 1) % 2; |
868 | } |
869 | } |
870 | |
871 | $output .= '</span>' . "\n"; |
872 | |
eaeaf964 |
873 | if ($return) { |
874 | return $output; |
875 | } else { |
876 | echo $output; |
877 | } |
531a3cb2 |
878 | } |
879 | |
2481037f |
880 | /** Display an standard html checkbox with an optional label |
881 | * |
882 | * @param string $name The name of the checkbox |
883 | * @param string $value The valus that the checkbox will pass when checked |
884 | * @param boolean $checked The flag to tell the checkbox initial state |
885 | * @param string $label The label to be showed near the checkbox |
886 | * @param string $alt The info to be inserted in the alt tag |
887 | */ |
d0d272e7 |
888 | function print_checkbox ($name, $value, $checked = true, $label = '', $alt = '', $script='',$return=false) { |
2481037f |
889 | |
20cbef63 |
890 | static $idcounter = 0; |
891 | |
2481037f |
892 | if (!$name) { |
893 | $name = 'unnamed'; |
894 | } |
895 | |
27f79fda |
896 | if ($alt) { |
897 | $alt = strip_tags($alt); |
898 | } else { |
2481037f |
899 | $alt = 'checkbox'; |
900 | } |
901 | |
902 | if ($checked) { |
903 | $strchecked = ' checked="checked"'; |
f89f924e |
904 | } else { |
905 | $strchecked = ''; |
2481037f |
906 | } |
907 | |
20cbef63 |
908 | $htmlid = 'auto-cb'.sprintf('%04d', ++$idcounter); |
2481037f |
909 | $output = '<span class="checkbox '.$name."\">"; |
fbe31d22 |
910 | $output .= '<input name="'.$name.'" id="'.$htmlid.'" type="checkbox" value="'.$value.'" alt="'.$alt.'"'.$strchecked.' '.((!empty($script)) ? ' onclick="'.$script.'" ' : '').' />'; |
20cbef63 |
911 | if(!empty($label)) { |
912 | $output .= ' <label for="'.$htmlid.'">'.$label.'</label>'; |
913 | } |
2481037f |
914 | $output .= '</span>'."\n"; |
915 | |
d0d272e7 |
916 | if (empty($return)) { |
917 | echo $output; |
918 | } else { |
919 | return $output; |
920 | } |
2481037f |
921 | |
922 | } |
923 | |
d0d272e7 |
924 | /** Display an standard html text field with an optional label |
925 | * |
926 | * @param string $name The name of the text field |
927 | * @param string $value The value of the text field |
928 | * @param string $label The label to be showed near the text field |
929 | * @param string $alt The info to be inserted in the alt tag |
930 | */ |
eaeaf964 |
931 | function print_textfield ($name, $value, $alt = '',$size=50,$maxlength=0, $return=false) { |
d0d272e7 |
932 | |
933 | static $idcounter = 0; |
934 | |
935 | if (empty($name)) { |
936 | $name = 'unnamed'; |
937 | } |
938 | |
939 | if (empty($alt)) { |
940 | $alt = 'textfield'; |
941 | } |
942 | |
943 | if (!empty($maxlength)) { |
944 | $maxlength = ' maxlength="'.$maxlength.'" '; |
945 | } |
946 | |
ce432524 |
947 | $htmlid = 'auto-tf'.sprintf('%04d', ++$idcounter); |
d0d272e7 |
948 | $output = '<span class="textfield '.$name."\">"; |
949 | $output .= '<input name="'.$name.'" id="'.$htmlid.'" type="text" value="'.$value.'" size="'.$size.'" '.$maxlength.' alt="'.$alt.'" />'; |
c06c8492 |
950 | |
d0d272e7 |
951 | $output .= '</span>'."\n"; |
952 | |
953 | if (empty($return)) { |
954 | echo $output; |
955 | } else { |
956 | return $output; |
957 | } |
958 | |
959 | } |
960 | |
961 | |
d48b00b4 |
962 | /** |
fa583f5f |
963 | * Implements a complete little form with a dropdown menu. When JavaScript is on |
964 | * selecting an option from the dropdown automatically submits the form (while |
965 | * avoiding the usual acessibility problems with this appoach). With JavaScript |
966 | * off, a 'Go' button is printed. |
d48b00b4 |
967 | * |
fa583f5f |
968 | * @param string $baseurl The target URL up to the point of the variable that changes |
969 | * @param array $options A list of value-label pairs for the popup list |
970 | * @param string $formid id for the control. Must be unique on the page. Used in the HTML. |
971 | * @param string $selected The option that is initially selected |
89dcb99d |
972 | * @param string $nothing The label for the "no choice" option |
973 | * @param string $help The name of a help page if help is required |
974 | * @param string $helptext The name of the label for the help button |
fa583f5f |
975 | * @param boolean $return Indicates whether the function should return the HTML |
89dcb99d |
976 | * as a string or echo it directly to the page being rendered |
772e78be |
977 | * @param string $targetwindow The name of the target page to open the linked page in. |
1d75edd0 |
978 | * @param string $selectlabel Text to place in a [label] element - preferred for accessibility. |
fa583f5f |
979 | * @param array $optionsextra an array with the same keys as $options. The values are added within the corresponding <option ...> tag. |
980 | * @param string $submitvalue Optional label for the 'Go' button. Defaults to get_string('go'). |
981 | * @param boolean $disabled If true, the menu will be displayed disabled. |
ad70c470 |
982 | * @param boolean $showbutton If true, the button will always be shown even if JavaScript is available |
89dcb99d |
983 | * @return string If $return is true then the entire form is returned as a string. |
984 | * @todo Finish documenting this function<br> |
89dcb99d |
985 | */ |
fa583f5f |
986 | function popup_form($baseurl, $options, $formid, $selected='', $nothing='choose', $help='', $helptext='', $return=false, |
ad70c470 |
987 | $targetwindow='self', $selectlabel='', $optionsextra=NULL, $submitvalue='', $disabled=false, $showbutton=false) { |
eee5d9bb |
988 | global $CFG, $SESSION; |
b0542a1e |
989 | static $go, $choose; /// Locally cached, in case there's lots on a page |
87180677 |
990 | |
6f9f3b69 |
991 | if (empty($options)) { |
992 | return ''; |
993 | } |
0d0baabf |
994 | |
fa583f5f |
995 | if (empty($submitvalue)){ |
996 | if (!isset($go)) { |
997 | $go = get_string('go'); |
998 | $submitvalue=$go; |
999 | } |
037dcbb6 |
1000 | } |
b0ccd3fb |
1001 | if ($nothing == 'choose') { |
037dcbb6 |
1002 | if (!isset($choose)) { |
1003 | $choose = get_string('choose'); |
1004 | } |
1005 | $nothing = $choose.'...'; |
618b22c5 |
1006 | } |
fa583f5f |
1007 | if ($disabled) { |
3fb00221 |
1008 | $disabled = ' disabled="disabled"'; |
fa583f5f |
1009 | } else { |
1010 | $disabled = ''; |
1011 | } |
618b22c5 |
1012 | |
9d3c6ee5 |
1013 | // changed reference to document.getElementById('id_abc') instead of document.abc |
1014 | // MDL-7861 |
4f24b3e3 |
1015 | $output = '<form action="'.$CFG->wwwroot.'/course/jumpto.php"'. |
fa738731 |
1016 | ' method="get" '. |
1017 | $CFG->frametarget. |
ee5567d1 |
1018 | ' id="'.$formid.'"'. |
c4d951e1 |
1019 | ' class="popupform">'; |
4f24b3e3 |
1020 | if ($help) { |
1021 | $button = helpbutton($help, $helptext, 'moodle', true, false, '', true); |
1022 | } else { |
1023 | $button = ''; |
1024 | } |
037dcbb6 |
1025 | |
fcf9577a |
1026 | if ($selectlabel) { |
1027 | $selectlabel = '<label for="'.$formid.'_jump">'.$selectlabel.'</label>'; |
1028 | } |
1029 | |
ad70c470 |
1030 | if ($showbutton) { |
1031 | // Using the no-JavaScript version |
1032 | $javascript = ''; |
1033 | } else if (check_browser_version('MSIE') || (check_browser_version('Opera') && !check_browser_operating_system("Linux"))) { |
1034 | //IE and Opera fire the onchange when ever you move into a dropdown list with the keyboard. |
1035 | //onfocus will call a function inside dropdown.js. It fixes this IE/Opera behavior. |
1036 | //Note: There is a bug on Opera+Linux with the javascript code (first mouse selection is inactive), |
1037 | //so we do not fix the Opera behavior on Linux |
1038 | $javascript = ' onfocus="initSelect(\''.$formid.'\','.$targetwindow.')"'; |
1039 | } else { |
1040 | //Other browser |
1041 | $javascript = ' onchange="'.$targetwindow. |
1042 | '.location=document.getElementById(\''.$formid. |
1043 | '\').jump.options[document.getElementById(\''. |
1044 | $formid.'\').jump.selectedIndex].value;"'; |
795a08ad |
1045 | } |
ad70c470 |
1046 | |
3fb00221 |
1047 | $output .= '<div style="white-space:nowrap">'.$selectlabel.$button.'<select id="'.$formid.'_jump" name="jump"'.$javascript.$disabled.'>'."\n"; |
73f7ad71 |
1048 | |
b0ccd3fb |
1049 | if ($nothing != '') { |
ffadf71b |
1050 | $selectlabeloption = ''; |
1051 | if ($selected=='') { |
1052 | $selectlabeloption = ' selected="selected"'; |
1053 | } |
1054 | foreach ($options as $value => $label) { //if one of the options is the empty value, don't make this the default |
1055 | if ($value == '') { |
1056 | $selected = ''; |
1057 | } |
1058 | } |
1059 | $output .= " <option value=\"javascript:void(0)\"$selectlabeloption>$nothing</option>\n"; |
f9903ed0 |
1060 | } |
1061 | |
72b4e283 |
1062 | $inoptgroup = false; |
89bfeee0 |
1063 | |
f9903ed0 |
1064 | foreach ($options as $value => $label) { |
772e78be |
1065 | |
89bfeee0 |
1066 | if ($label == '--') { /// we are ending previous optgroup |
1067 | /// Check to see if we already have a valid open optgroup |
1068 | /// XHTML demands that there be at least 1 option within an optgroup |
1069 | if ($inoptgroup and (count($optgr) > 1) ) { |
1070 | $output .= implode('', $optgr); |
1071 | $output .= ' </optgroup>'; |
1072 | } |
1073 | $optgr = array(); |
1074 | $inoptgroup = false; |
1075 | continue; |
1076 | } else if (substr($label,0,2) == '--') { /// we are starting a new optgroup |
772e78be |
1077 | |
2a003a90 |
1078 | /// Check to see if we already have a valid open optgroup |
1079 | /// XHTML demands that there be at least 1 option within an optgroup |
1080 | if ($inoptgroup and (count($optgr) > 1) ) { |
1081 | $output .= implode('', $optgr); |
72b4e283 |
1082 | $output .= ' </optgroup>'; |
72b4e283 |
1083 | } |
2a003a90 |
1084 | |
1085 | unset($optgr); |
1086 | $optgr = array(); |
1087 | |
dacb47c0 |
1088 | $optgr[] = ' <optgroup label="'. s(format_string(substr($label,2))) .'">'; // Plain labels |
772e78be |
1089 | |
2a003a90 |
1090 | $inoptgroup = true; /// everything following will be in an optgroup |
3326450b |
1091 | continue; |
772e78be |
1092 | |
d897cae4 |
1093 | } else { |
fd78420b |
1094 | if (!empty($CFG->usesid) && !isset($_COOKIE[session_name()])) |
1095 | { |
fa583f5f |
1096 | $url = $SESSION->sid_process_url( $baseurl . $value ); |
fd78420b |
1097 | } else |
1098 | { |
fa583f5f |
1099 | $url=$baseurl . $value; |
fd78420b |
1100 | } |
1101 | $optstr = ' <option value="' . $url . '"'; |
772e78be |
1102 | |
d897cae4 |
1103 | if ($value == $selected) { |
2a003a90 |
1104 | $optstr .= ' selected="selected"'; |
1105 | } |
772e78be |
1106 | |
b6b354e3 |
1107 | if (!empty($optionsextra[$value])) { |
1108 | $optstr .= ' '.$optionsextra[$value]; |
1109 | } |
1110 | |
2a003a90 |
1111 | if ($label) { |
1112 | $optstr .= '>'. $label .'</option>' . "\n"; |
1113 | } else { |
1114 | $optstr .= '>'. $value .'</option>' . "\n"; |
1115 | } |
772e78be |
1116 | |
2a003a90 |
1117 | if ($inoptgroup) { |
1118 | $optgr[] = $optstr; |
1119 | } else { |
1120 | $output .= $optstr; |
d897cae4 |
1121 | } |
f9903ed0 |
1122 | } |
772e78be |
1123 | |
f9903ed0 |
1124 | } |
2a003a90 |
1125 | |
1126 | /// catch the final group if not closed |
1127 | if ($inoptgroup and count($optgr) > 1) { |
1128 | $output .= implode('', $optgr); |
72b4e283 |
1129 | $output .= ' </optgroup>'; |
1130 | } |
2a003a90 |
1131 | |
b0ccd3fb |
1132 | $output .= '</select>'; |
3435f39b |
1133 | $output .= '<input type="hidden" name="sesskey" value="'.sesskey().'" />'; |
ad70c470 |
1134 | if (!$showbutton) { |
1135 | $output .= '<div id="noscript'.$formid.'" style="display: inline;">'; |
1136 | } |
fa583f5f |
1137 | $output .= '<input type="submit" value="'.$submitvalue.'" '.$disabled.' /></div>'; |
ad70c470 |
1138 | if (!$showbutton) { |
1139 | $output .= '<script type="text/javascript">'. |
1140 | "\n//<![CDATA[\n". |
1141 | 'document.getElementById("noscript'.$formid.'").style.display = "none";'. |
1142 | "\n//]]>\n".'</script>'; |
ad70c470 |
1143 | } |
9cef0c27 |
1144 | $output .= '</div></form>'; |
d897cae4 |
1145 | |
1146 | if ($return) { |
4f24b3e3 |
1147 | return $output; |
d897cae4 |
1148 | } else { |
4f24b3e3 |
1149 | echo $output; |
d897cae4 |
1150 | } |
f9903ed0 |
1151 | } |
1152 | |
1153 | |
d48b00b4 |
1154 | /** |
1155 | * Prints some red text |
1156 | * |
1157 | * @param string $error The text to be displayed in red |
1158 | */ |
f9903ed0 |
1159 | function formerr($error) { |
d48b00b4 |
1160 | |
f9903ed0 |
1161 | if (!empty($error)) { |
7bc1c17f |
1162 | echo '<span class="error">'. $error .'</span>'; |
f9903ed0 |
1163 | } |
1164 | } |
1165 | |
d48b00b4 |
1166 | /** |
1167 | * Validates an email to make sure it makes sense. |
1168 | * |
1169 | * @param string $address The email address to validate. |
1170 | * @return boolean |
1171 | */ |
89dcb99d |
1172 | function validate_email($address) { |
d48b00b4 |
1173 | |
78fbaeae |
1174 | return (ereg('^[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+'. |
1175 | '(\.[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+)*'. |
f9903ed0 |
1176 | '@'. |
1177 | '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'. |
1178 | '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', |
1179 | $address)); |
1180 | } |
1181 | |
690f358b |
1182 | /** |
1183 | * Extracts file argument either from file parameter or PATH_INFO |
11e7b506 |
1184 | * Note: $scriptname parameter is not needed anymore |
690f358b |
1185 | * |
690f358b |
1186 | * @return string file path (only safe characters) |
1187 | */ |
11e7b506 |
1188 | function get_file_argument() { |
1189 | global $SCRIPT; |
690f358b |
1190 | |
690f358b |
1191 | $relativepath = optional_param('file', FALSE, PARAM_PATH); |
1192 | |
1193 | // then try extract file from PATH_INFO (slasharguments method) |
11e7b506 |
1194 | if ($relativepath === false and isset($_SERVER['PATH_INFO']) and $_SERVER['PATH_INFO'] !== '') { |
690f358b |
1195 | // check that PATH_INFO works == must not contain the script name |
11e7b506 |
1196 | if (strpos($_SERVER['PATH_INFO'], $SCRIPT) === false) { |
1197 | $relativepath = clean_param(urldecode($_SERVER['PATH_INFO']), PARAM_PATH); |
690f358b |
1198 | } |
1199 | } |
1200 | |
11e7b506 |
1201 | // note: we are not using any other way because they are not compatible with unicode file names ;-) |
690f358b |
1202 | |
1203 | return $relativepath; |
1204 | } |
1205 | |
d48b00b4 |
1206 | /** |
89dcb99d |
1207 | * Just returns an array of text formats suitable for a popup menu |
d48b00b4 |
1208 | * |
89dcb99d |
1209 | * @uses FORMAT_MOODLE |
1210 | * @uses FORMAT_HTML |
1211 | * @uses FORMAT_PLAIN |
89dcb99d |
1212 | * @uses FORMAT_MARKDOWN |
1213 | * @return array |
d48b00b4 |
1214 | */ |
0095d5cd |
1215 | function format_text_menu() { |
d48b00b4 |
1216 | |
b0ccd3fb |
1217 | return array (FORMAT_MOODLE => get_string('formattext'), |
1218 | FORMAT_HTML => get_string('formathtml'), |
1219 | FORMAT_PLAIN => get_string('formatplain'), |
b0ccd3fb |
1220 | FORMAT_MARKDOWN => get_string('formatmarkdown')); |
0095d5cd |
1221 | } |
1222 | |
d48b00b4 |
1223 | /** |
1224 | * Given text in a variety of format codings, this function returns |
772e78be |
1225 | * the text as safe HTML. |
d48b00b4 |
1226 | * |
c5659019 |
1227 | * This function should mainly be used for long strings like posts, |
e8276c10 |
1228 | * answers, glossary items etc. For short strings @see format_string(). |
1229 | * |
d48b00b4 |
1230 | * @uses $CFG |
89dcb99d |
1231 | * @uses FORMAT_MOODLE |
1232 | * @uses FORMAT_HTML |
1233 | * @uses FORMAT_PLAIN |
1234 | * @uses FORMAT_WIKI |
1235 | * @uses FORMAT_MARKDOWN |
1236 | * @param string $text The text to be formatted. This is raw text originally from user input. |
772e78be |
1237 | * @param int $format Identifier of the text format to be used |
89dcb99d |
1238 | * (FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN, FORMAT_WIKI, FORMAT_MARKDOWN) |
1239 | * @param array $options ? |
1240 | * @param int $courseid ? |
1241 | * @return string |
d48b00b4 |
1242 | * @todo Finish documenting this function |
1243 | */ |
7d8a3cb0 |
1244 | function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL) { |
e3e40b43 |
1245 | global $CFG, $COURSE, $DB, $PAGE; |
c4ae4fa1 |
1246 | |
1cc54a45 |
1247 | static $croncache = array(); |
795a08ad |
1248 | |
9e3f34d1 |
1249 | $hashstr = ''; |
1cc54a45 |
1250 | |
d53ca6ad |
1251 | if ($text === '') { |
1252 | return ''; // no need to do any filters and cleaning |
1253 | } |
1254 | |
cbc2b5df |
1255 | if (!isset($options->trusted)) { |
1256 | $options->trusted = false; |
7d8a3cb0 |
1257 | } |
e7a47153 |
1258 | if (!isset($options->noclean)) { |
cbc2b5df |
1259 | if ($options->trusted and trusttext_active()) { |
1260 | // no cleaning if text trusted and noclean not specified |
1261 | $options->noclean=true; |
1262 | } else { |
1263 | $options->noclean=false; |
1264 | } |
e7a47153 |
1265 | } |
a17c57b5 |
1266 | if (!isset($options->nocache)) { |
1267 | $options->nocache=false; |
1268 | } |
e7a47153 |
1269 | if (!isset($options->smiley)) { |
1270 | $options->smiley=true; |
1271 | } |
1272 | if (!isset($options->filter)) { |
1273 | $options->filter=true; |
1274 | } |
1275 | if (!isset($options->para)) { |
1276 | $options->para=true; |
1277 | } |
1278 | if (!isset($options->newlines)) { |
1279 | $options->newlines=true; |
f0aa2fed |
1280 | } |
c4ae4fa1 |
1281 | if (empty($courseid)) { |
dcf6d93c |
1282 | $courseid = $COURSE->id; |
c4ae4fa1 |
1283 | } |
a751a4e5 |
1284 | |
ccc161f8 |
1285 | if ($options->filter) { |
1286 | $filtermanager = filter_manager::instance(); |
1287 | } else { |
1288 | $filtermanager = new null_filter_manager(); |
9e3f34d1 |
1289 | } |
e3e40b43 |
1290 | $context = $PAGE->context; |
ccc161f8 |
1291 | |
a17c57b5 |
1292 | if (!empty($CFG->cachetext) and empty($options->nocache)) { |
ccc161f8 |
1293 | $hashstr .= $text.'-'.$filtermanager->text_filtering_hash($context, $courseid).'-'.(int)$courseid.'-'.current_language().'-'. |
cbc2b5df |
1294 | (int)$format.(int)$options->trusted.(int)$options->noclean.(int)$options->smiley. |
ccc161f8 |
1295 | (int)$options->filter.(int)$options->para.(int)$options->newlines; |
1cc54a45 |
1296 | |
9e3f34d1 |
1297 | $time = time() - $CFG->cachetext; |
795a08ad |
1298 | $md5key = md5($hashstr); |
a91b910e |
1299 | if (CLI_SCRIPT) { |
1cc54a45 |
1300 | if (isset($croncache[$md5key])) { |
1301 | return $croncache[$md5key]; |
1302 | } |
1303 | } |
1304 | |
f33e1ed4 |
1305 | if ($oldcacheitem = $DB->get_record('cache_text', array('md5key'=>$md5key), '*', true)) { |
a9743837 |
1306 | if ($oldcacheitem->timemodified >= $time) { |
a91b910e |
1307 | if (CLI_SCRIPT) { |
1cc54a45 |
1308 | if (count($croncache) > 150) { |
5087c945 |
1309 | reset($croncache); |
1310 | $key = key($croncache); |
1311 | unset($croncache[$key]); |
1cc54a45 |
1312 | } |
1313 | $croncache[$md5key] = $oldcacheitem->formattedtext; |
1314 | } |
a9743837 |
1315 | return $oldcacheitem->formattedtext; |
1316 | } |
e7a47153 |
1317 | } |
1318 | } |
1319 | |
0095d5cd |
1320 | switch ($format) { |
73f8658c |
1321 | case FORMAT_HTML: |
7d8a3cb0 |
1322 | if ($options->smiley) { |
e7a47153 |
1323 | replace_smilies($text); |
1324 | } |
7d8a3cb0 |
1325 | if (!$options->noclean) { |
5c6347ce |
1326 | $text = clean_text($text, FORMAT_HTML); |
9d40806d |
1327 | } |
ccc161f8 |
1328 | $text = $filtermanager->filter_text($text, $context, $courseid); |
73f8658c |
1329 | break; |
1330 | |
6901fa79 |
1331 | case FORMAT_PLAIN: |
5c6347ce |
1332 | $text = s($text); // cleans dangerous JS |
ab892a4f |
1333 | $text = rebuildnolinktag($text); |
b0ccd3fb |
1334 | $text = str_replace(' ', ' ', $text); |
6901fa79 |
1335 | $text = nl2br($text); |
6901fa79 |
1336 | break; |
1337 | |
d342c763 |
1338 | case FORMAT_WIKI: |
6a6495ff |
1339 | // this format is deprecated |
572fe9ab |
1340 | $text = '<p>NOTICE: Wiki-like formatting has been removed from Moodle. You should not be seeing |
1341 | this message as all texts should have been converted to Markdown format instead. |
ce50cc70 |
1342 | Please post a bug report to http://moodle.org/bugs with information about where you |
e7a47153 |
1343 | saw this message.</p>'.s($text); |
d342c763 |
1344 | break; |
1345 | |
e7cdcd18 |
1346 | case FORMAT_MARKDOWN: |
1347 | $text = markdown_to_html($text); |
7d8a3cb0 |
1348 | if ($options->smiley) { |
e7a47153 |
1349 | replace_smilies($text); |
1350 | } |
7d8a3cb0 |
1351 | if (!$options->noclean) { |
5c6347ce |
1352 | $text = clean_text($text, FORMAT_HTML); |
9d40806d |
1353 | } |
ccc161f8 |
1354 | $text = $filtermanager->filter_text($text, $context, $courseid); |
e7cdcd18 |
1355 | break; |
1356 | |
73f8658c |
1357 | default: // FORMAT_MOODLE or anything else |
b7a3d3b2 |
1358 | $text = text_to_html($text, $options->smiley, $options->para, $options->newlines); |
7d8a3cb0 |
1359 | if (!$options->noclean) { |
5c6347ce |
1360 | $text = clean_text($text, FORMAT_HTML); |
9d40806d |
1361 | } |
ccc161f8 |
1362 | $text = $filtermanager->filter_text($text, $context, $courseid); |
0095d5cd |
1363 | break; |
0095d5cd |
1364 | } |
f0aa2fed |
1365 | |
ccc161f8 |
1366 | // Warn people that we have removed this old mechanism, just in case they |
1367 | // were stupid enough to rely on it. |
1368 | if (isset($CFG->currenttextiscacheable)) { |
1369 | debugging('Once upon a time, Moodle had a truly evil use of global variables ' . |
1370 | 'called $CFG->currenttextiscacheable. The good news is that this no ' . |
1371 | 'longer exists. The bad news is that you seem to be using a filter that '. |
1372 | 'relies on it. Please seek out and destroy that filter code.', DEBUG_DEVELOPER); |
1373 | } |
1374 | |
1375 | if (empty($options->nocache) and !empty($CFG->cachetext)) { |
a91b910e |
1376 | if (CLI_SCRIPT) { |
1cc54a45 |
1377 | // special static cron cache - no need to store it in db if its not already there |
1378 | if (count($croncache) > 150) { |
5087c945 |
1379 | reset($croncache); |
1380 | $key = key($croncache); |
1381 | unset($croncache[$key]); |
1cc54a45 |
1382 | } |
1383 | $croncache[$md5key] = $text; |
1384 | return $text; |
1385 | } |
1386 | |
f47f4659 |
1387 | $newcacheitem = new object(); |
a9743837 |
1388 | $newcacheitem->md5key = $md5key; |
f33e1ed4 |
1389 | $newcacheitem->formattedtext = $text; |
a9743837 |
1390 | $newcacheitem->timemodified = time(); |
1391 | if ($oldcacheitem) { // See bug 4677 for discussion |
1392 | $newcacheitem->id = $oldcacheitem->id; |
f6949ddb |
1393 | try { |
1394 | $DB->update_record('cache_text', $newcacheitem); // Update existing record in the cache table |
1395 | } catch (dml_exception $e) { |
1396 | // It's unlikely that the cron cache cleaner could have |
1397 | // deleted this entry in the meantime, as it allows |
1398 | // some extra time to cover these cases. |
1399 | } |
a9743837 |
1400 | } else { |
f6949ddb |
1401 | try { |
1402 | $DB->insert_record('cache_text', $newcacheitem); // Insert a new record in the cache table |
1403 | } catch (dml_exception $e) { |
1404 | // Again, it's possible that another user has caused this |
1405 | // record to be created already in the time that it took |
1406 | // to traverse this function. That's OK too, as the |
1407 | // call above handles duplicate entries, and eventually |
1408 | // the cron cleaner will delete them. |
1409 | } |
a9743837 |
1410 | } |
f0aa2fed |
1411 | } |
1412 | |
1413 | return $text; |
0095d5cd |
1414 | } |
1415 | |
4a28b5ca |
1416 | /** Converts the text format from the value to the 'internal' |
1417 | * name or vice versa. $key can either be the value or the name |
1418 | * and you get the other back. |
c06c8492 |
1419 | * |
4a28b5ca |
1420 | * @param mixed int 0-4 or string one of 'moodle','html','plain','markdown' |
1421 | * @return mixed as above but the other way around! |
1422 | */ |
1423 | function text_format_name( $key ) { |
1424 | $lookup = array(); |
1425 | $lookup[FORMAT_MOODLE] = 'moodle'; |
1426 | $lookup[FORMAT_HTML] = 'html'; |
1427 | $lookup[FORMAT_PLAIN] = 'plain'; |
1428 | $lookup[FORMAT_MARKDOWN] = 'markdown'; |
1429 | $value = "error"; |
1430 | if (!is_numeric($key)) { |
1431 | $key = strtolower( $key ); |
1432 | $value = array_search( $key, $lookup ); |
1433 | } |
1434 | else { |
1435 | if (isset( $lookup[$key] )) { |
1436 | $value = $lookup[ $key ]; |
1437 | } |
1438 | } |
1439 | return $value; |
1440 | } |
1441 | |
109e3cb2 |
1442 | /** |
1443 | * Resets all data related to filters, called during upgrade or when filter settings change. |
1444 | * @return void |
1445 | */ |
1446 | function reset_text_filters_cache() { |
8618fd2a |
1447 | global $CFG, $DB; |
109e3cb2 |
1448 | |
8618fd2a |
1449 | $DB->delete_records('cache_text'); |
109e3cb2 |
1450 | $purifdir = $CFG->dataroot.'/cache/htmlpurifier'; |
1451 | remove_dir($purifdir, true); |
1452 | } |
473d29eb |
1453 | |
7b2c5e72 |
1454 | /** Given a simple string, this function returns the string |
e8276c10 |
1455 | * processed by enabled string filters if $CFG->filterall is enabled |
1456 | * |
c5659019 |
1457 | * This function should be used to print short strings (non html) that |
1458 | * need filter processing e.g. activity titles, post subjects, |
e8276c10 |
1459 | * glossary concepts. |
7b2c5e72 |
1460 | * |
3e6691ee |
1461 | * @param string $string The string to be filtered. |
fae83c8d |
1462 | * @param boolean $striplinks To strip any link in the result text (Moodle 1.8 default changed from false to true! MDL-8713) |
3e6691ee |
1463 | * @param int $courseid Current course as filters can, potentially, use it |
7b2c5e72 |
1464 | * @return string |
1465 | */ |
ccc161f8 |
1466 | function format_string($string, $striplinks=true, $courseid=NULL ) { |
e3e40b43 |
1467 | global $CFG, $COURSE, $PAGE; |
38701b69 |
1468 | |
2a3affe9 |
1469 | //We'll use a in-memory cache here to speed up repeated strings |
473d29eb |
1470 | static $strcache = false; |
1471 | |
38701b69 |
1472 | if ($strcache === false or count($strcache) > 2000 ) { // this number might need some tuning to limit memory usage in cron |
473d29eb |
1473 | $strcache = array(); |
1474 | } |
84e3d2cc |
1475 | |
38701b69 |
1476 | //init course id |
a97e0ccb |
1477 | if (empty($courseid)) { |
38701b69 |
1478 | $courseid = $COURSE->id; |
1479 | } |
1480 | |
2a3affe9 |
1481 | //Calculate md5 |
38701b69 |
1482 | $md5 = md5($string.'<+>'.$striplinks.'<+>'.$courseid.'<+>'.current_language()); |
2a3affe9 |
1483 | |
1484 | //Fetch from cache if possible |
38701b69 |
1485 | if (isset($strcache[$md5])) { |
2a3affe9 |
1486 | return $strcache[$md5]; |
1487 | } |
1488 | |
dacb47c0 |
1489 | // First replace all ampersands not followed by html entity code |
cb6a4f09 |
1490 | $string = preg_replace("/\&(?![a-zA-Z0-9#]{1,8};)/", "&", $string); |
268ddd50 |
1491 | |
14d20de1 |
1492 | if (!empty($CFG->filterall) && $CFG->version >= 2009040600) { // Avoid errors during the upgrade to the new system. |
e3e40b43 |
1493 | $context = $PAGE->context; |
ccc161f8 |
1494 | $string = filter_manager::instance()->filter_string($string, $context, $courseid); |
7b2c5e72 |
1495 | } |
84e3d2cc |
1496 | |
9fbed9c9 |
1497 | // If the site requires it, strip ALL tags from this string |
1498 | if (!empty($CFG->formatstringstriptags)) { |
1499 | $string = strip_tags($string); |
1500 | |
408d5327 |
1501 | } else { |
1502 | // Otherwise strip just links if that is required (default) |
1503 | if ($striplinks) { //strip links in string |
1504 | $string = preg_replace('/(<a\s[^>]+?>)(.+?)(<\/a>)/is','$2',$string); |
1505 | } |
1506 | $string = clean_text($string); |
3e6691ee |
1507 | } |
1508 | |
2a3affe9 |
1509 | //Store to cache |
1510 | $strcache[$md5] = $string; |
84e3d2cc |
1511 | |
7b2c5e72 |
1512 | return $string; |
1513 | } |
1514 | |
d48b00b4 |
1515 | /** |
1516 | * Given text in a variety of format codings, this function returns |
1517 | * the text as plain text suitable for plain email. |
d48b00b4 |
1518 | * |
89dcb99d |
1519 | * @uses FORMAT_MOODLE |
1520 | * @uses FORMAT_HTML |
1521 | * @uses FORMAT_PLAIN |
1522 | * @uses FORMAT_WIKI |
1523 | * @uses FORMAT_MARKDOWN |
1524 | * @param string $text The text to be formatted. This is raw text originally from user input. |
772e78be |
1525 | * @param int $format Identifier of the text format to be used |
89dcb99d |
1526 | * (FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN, FORMAT_WIKI, FORMAT_MARKDOWN) |
1527 | * @return string |
d48b00b4 |
1528 | */ |
d342c763 |
1529 | function format_text_email($text, $format) { |
d342c763 |
1530 | |
1531 | switch ($format) { |
1532 | |
1533 | case FORMAT_PLAIN: |
1534 | return $text; |
1535 | break; |
1536 | |
1537 | case FORMAT_WIKI: |
1538 | $text = wiki_to_html($text); |
5b472756 |
1539 | /// This expression turns links into something nice in a text format. (Russell Jungwirth) |
1540 | /// From: http://php.net/manual/en/function.eregi-replace.php and simplified |
76add072 |
1541 | $text = eregi_replace('(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)','\\3 [ \\2 ]', $text); |
7c55a29b |
1542 | return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES))); |
d342c763 |
1543 | break; |
1544 | |
6ff45b59 |
1545 | case FORMAT_HTML: |
1546 | return html_to_text($text); |
1547 | break; |
1548 | |
e7cdcd18 |
1549 | case FORMAT_MOODLE: |
1550 | case FORMAT_MARKDOWN: |
67ccec43 |
1551 | default: |
76add072 |
1552 | $text = eregi_replace('(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)','\\3 [ \\2 ]', $text); |
7c55a29b |
1553 | return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES))); |
d342c763 |
1554 | break; |
1555 | } |
1556 | } |
0095d5cd |
1557 | |
d48b00b4 |
1558 | /** |
1559 | * Given some text in HTML format, this function will pass it |
ccc161f8 |
1560 | * through any filters that have been configured for this context. |
d48b00b4 |
1561 | * |
89dcb99d |
1562 | * @param string $text The text to be passed through format filters |
ccc161f8 |
1563 | * @param int $courseid The current course. |
1564 | * @return string the filtered string. |
d48b00b4 |
1565 | */ |
c4ae4fa1 |
1566 | function filter_text($text, $courseid=NULL) { |
e3e40b43 |
1567 | global $CFG, $COURSE, $PAGE; |
dcf6d93c |
1568 | |
1569 | if (empty($courseid)) { |
1570 | $courseid = $COURSE->id; // (copied from format_text) |
1571 | } |
e67b9e31 |
1572 | |
e3e40b43 |
1573 | $context = $PAGE->context; |
9fbed9c9 |
1574 | |
ccc161f8 |
1575 | return filter_manager::instance()->filter_text($text, $context, $courseid); |
cbdfb929 |
1576 | } |
dc5c2bd9 |
1577 | /** |
1578 | * Formats activity intro text |
1579 | * @param string $module name of module |
1580 | * @param object $activity instance of activity |
1581 | * @param int $cmid course module id |
43b44d5e |
1582 | * @param bool $filter filter resulting html text |
dc5c2bd9 |
1583 | * @return text |
1584 | */ |
43b44d5e |
1585 | function format_module_intro($module, $activity, $cmid, $filter=true) { |
ac3668bf |
1586 | global $CFG; |
1587 | require_once("$CFG->libdir/filelib.php"); |
43b44d5e |
1588 | $options = (object)array('noclean'=>true, 'para'=>false, 'filter'=>false); |
dc5c2bd9 |
1589 | $context = get_context_instance(CONTEXT_MODULE, $cmid); |
7d2948bd |
1590 | $intro = file_rewrite_pluginfile_urls($activity->intro, 'pluginfile.php', $context->id, $module.'_intro', null); |
ac3668bf |
1591 | return trim(format_text($intro, $activity->introformat, $options)); |
dc5c2bd9 |
1592 | } |
cbdfb929 |
1593 | |
7d8a3cb0 |
1594 | /** |
cbc2b5df |
1595 | * Legacy function, used for cleaning of old forum and glossary text only. |
5ce73257 |
1596 | * @param string $text text that may contain TRUSTTEXT marker |
7d8a3cb0 |
1597 | * @return text without any TRUSTTEXT marker |
1598 | */ |
1599 | function trusttext_strip($text) { |
1600 | global $CFG; |
1601 | |
1602 | while (true) { //removing nested TRUSTTEXT |
5ce73257 |
1603 | $orig = $text; |
cbc2b5df |
1604 | $text = str_replace('#####TRUSTTEXT#####', '', $text); |
7d8a3cb0 |
1605 | if (strcmp($orig, $text) === 0) { |
1606 | return $text; |
1607 | } |
1608 | } |
1609 | } |
1610 | |
cbc2b5df |
1611 | /** |
1612 | * Must be called before editing of all texts |
1613 | * with trust flag. Removes all XSS nasties |
1614 | * from texts stored in database if needed. |
1615 | * @param object $object data object with xxx, xxxformat and xxxtrust fields |
1616 | * @param string $field name of text field |
1617 | * @param object $context active context |
1618 | * @return object updated $object |
1619 | */ |
1620 | function trusttext_pre_edit($object, $field, $context) { |
1621 | $trustfield = $field.'trust'; |
1622 | $formatfield = $field.'format'; |
1623 | |
1624 | if (!$object->$trustfield or !trusttext_trusted($context)) { |
1625 | $object->$field = clean_text($object->$field, $object->$formatfield); |
1626 | } |
1627 | |
1628 | return $object; |
1629 | } |
1630 | |
1631 | /** |
7467e721 |
1632 | * Is urrent user trusted to enter no dangerous XSS in this context? |
cbc2b5df |
1633 | * Please note the user must be in fact trusted everywhere on this server!! |
1634 | * @param $context |
1635 | * @return bool true if user trusted |
1636 | */ |
1637 | function trusttext_trusted($context) { |
1638 | return (trusttext_active() and has_capability('moodle/site:trustcontent', $context)); |
1639 | } |
1640 | |
1641 | /** |
1642 | * Is trusttext feature active? |
1643 | * @param $context |
1644 | * @return bool |
1645 | */ |
1646 | function trusttext_active() { |
1647 | global $CFG; |
1648 | |
1649 | return !empty($CFG->enabletrusttext); |
1650 | } |
1651 | |
d48b00b4 |
1652 | /** |
1653 | * Given raw text (eg typed in by a user), this function cleans it up |
1654 | * and removes any nasty tags that could mess up Moodle pages. |
1655 | * |
89dcb99d |
1656 | * @uses FORMAT_MOODLE |
1657 | * @uses FORMAT_PLAIN |
1658 | * @uses ALLOWED_TAGS |
1659 | * @param string $text The text to be cleaned |
772e78be |
1660 | * @param int $format Identifier of the text format to be used |
89dcb99d |
1661 | * (FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN, FORMAT_WIKI, FORMAT_MARKDOWN) |
1662 | * @return string The cleaned up text |
d48b00b4 |
1663 | */ |
3da47524 |
1664 | function clean_text($text, $format=FORMAT_MOODLE) { |
b7a3cf49 |
1665 | |
85fbf884 |
1666 | global $ALLOWED_TAGS, $CFG; |
1667 | |
e0ac8448 |
1668 | if (empty($text) or is_numeric($text)) { |
84e3d2cc |
1669 | return (string)$text; |
e0ac8448 |
1670 | } |
3fe3851d |
1671 | |
ab9f24ad |
1672 | switch ($format) { |
e7cdcd18 |
1673 | case FORMAT_PLAIN: |
8f660562 |
1674 | case FORMAT_MARKDOWN: |
e7cdcd18 |
1675 | return $text; |
1676 | |
1677 | default: |
1678 | |
e0ac8448 |
1679 | if (!empty($CFG->enablehtmlpurifier)) { |
1680 | $text = purify_html($text); |
1681 | } else { |
1682 | /// Fix non standard entity notations |
1683 | $text = preg_replace('/(&#[0-9]+)(;?)/', "\\1;", $text); |
1684 | $text = preg_replace('/(&#x[0-9a-fA-F]+)(;?)/', "\\1;", $text); |
84e3d2cc |
1685 | |
e0ac8448 |
1686 | /// Remove tags that are not allowed |
1687 | $text = strip_tags($text, $ALLOWED_TAGS); |
84e3d2cc |
1688 | |
e0ac8448 |
1689 | /// Clean up embedded scripts and , using kses |
1690 | $text = cleanAttributes($text); |
a33c44c4 |
1691 | |
1692 | /// Again remove tags that are not allowed |
1693 | $text = strip_tags($text, $ALLOWED_TAGS); |
1694 | |
e0ac8448 |
1695 | } |
7789ffbf |
1696 | |
e0ac8448 |
1697 | /// Remove potential script events - some extra protection for undiscovered bugs in our code |
ab9f24ad |
1698 | $text = eregi_replace("([^a-z])language([[:space:]]*)=", "\\1Xlanguage=", $text); |
1699 | $text = eregi_replace("([^a-z])on([a-z]+)([[:space:]]*)=", "\\1Xon\\2=", $text); |
6901fa79 |
1700 | |
6901fa79 |
1701 | return $text; |
0095d5cd |
1702 | } |
b7a3cf49 |
1703 | } |
f9903ed0 |
1704 | |
e0ac8448 |
1705 | /** |
1706 | * KSES replacement cleaning function - uses HTML Purifier. |
1707 | */ |
1708 | function purify_html($text) { |
1709 | global $CFG; |
1710 | |
109e3cb2 |
1711 | // this can not be done only once because we sometimes need to reset the cache |
eb203ee4 |
1712 | $cachedir = $CFG->dataroot.'/cache/htmlpurifier'; |
109e3cb2 |
1713 | $status = check_dir_exists($cachedir, true, true); |
1714 | |
e0ac8448 |
1715 | static $purifier = false; |
eb203ee4 |
1716 | static $config; |
109e3cb2 |
1717 | if ($purifier === false) { |
eb203ee4 |
1718 | require_once $CFG->libdir.'/htmlpurifier/HTMLPurifier.safe-includes.php'; |
e0ac8448 |
1719 | $config = HTMLPurifier_Config::createDefault(); |
eb203ee4 |
1720 | $config->set('Core', 'ConvertDocumentToFragment', true); |
5adad310 |
1721 | $config->set('Core', 'Encoding', 'UTF-8'); |
1722 | $config->set('HTML', 'Doctype', 'XHTML 1.0 Transitional'); |
109e3cb2 |
1723 | $config->set('Cache', 'SerializerPath', $cachedir); |
e0ac8448 |
1724 | $config->set('URI', 'AllowedSchemes', array('http'=>1, 'https'=>1, 'ftp'=>1, 'irc'=>1, 'nntp'=>1, 'news'=>1, 'rtsp'=>1, 'teamspeak'=>1, 'gopher'=>1, 'mms'=>1)); |
3c24a391 |
1725 | $config->set('Attr', 'AllowedFrameTargets', array('_blank')); |
e0ac8448 |
1726 | $purifier = new HTMLPurifier($config); |
1727 | } |
1728 | return $purifier->purify($text); |
1729 | } |
1730 | |
d48b00b4 |
1731 | /** |
89dcb99d |
1732 | * This function takes a string and examines it for HTML tags. |
d48b00b4 |
1733 | * If tags are detected it passes the string to a helper function {@link cleanAttributes2()} |
1734 | * which checks for attributes and filters them for malicious content |
1735 | * 17/08/2004 :: Eamon DOT Costello AT dcu DOT ie |
1736 | * |
1737 | * @param string $str The string to be examined for html tags |
1738 | * @return string |
1739 | */ |
3bd7ffec |
1740 | function cleanAttributes($str){ |
4e8f2e6b |
1741 | $result = preg_replace_callback( |
1742 | '%(<[^>]*(>|$)|>)%m', #search for html tags |
1743 | "cleanAttributes2", |
3bd7ffec |
1744 | $str |
67ccec43 |
1745 | ); |
3bd7ffec |
1746 | return $result; |
67ccec43 |
1747 | } |
1748 | |
d48b00b4 |
1749 | /** |
1750 | * This function takes a string with an html tag and strips out any unallowed |
1751 | * protocols e.g. javascript: |
1752 | * It calls ancillary functions in kses which are prefixed by kses |
1753 | * 17/08/2004 :: Eamon DOT Costello AT dcu DOT ie |
1754 | * |
4e8f2e6b |
1755 | * @param array $htmlArray An array from {@link cleanAttributes()}, containing in its 1st |
1756 | * element the html to be cleared |
d48b00b4 |
1757 | * @return string |
1758 | */ |
4e8f2e6b |
1759 | function cleanAttributes2($htmlArray){ |
3bd7ffec |
1760 | |
037dcbb6 |
1761 | global $CFG, $ALLOWED_PROTOCOLS; |
b0ccd3fb |
1762 | require_once($CFG->libdir .'/kses.php'); |
3bd7ffec |
1763 | |
4e8f2e6b |
1764 | $htmlTag = $htmlArray[1]; |
037dcbb6 |
1765 | if (substr($htmlTag, 0, 1) != '<') { |
3bd7ffec |
1766 | return '>'; //a single character ">" detected |
1767 | } |
037dcbb6 |
1768 | if (!preg_match('%^<\s*(/\s*)?([a-zA-Z0-9]+)([^>]*)>?$%', $htmlTag, $matches)) { |
67ccec43 |
1769 | return ''; // It's seriously malformed |
1770 | } |
3bd7ffec |
1771 | $slash = trim($matches[1]); //trailing xhtml slash |
67ccec43 |
1772 | $elem = $matches[2]; //the element name |
3bd7ffec |
1773 | $attrlist = $matches[3]; // the list of attributes as a string |
1774 | |
037dcbb6 |
1775 | $attrArray = kses_hair($attrlist, $ALLOWED_PROTOCOLS); |
3bd7ffec |
1776 | |
67ccec43 |
1777 | $attStr = ''; |
037dcbb6 |
1778 | foreach ($attrArray as $arreach) { |
29939bea |
1779 | $arreach['name'] = strtolower($arreach['name']); |
1780 | if ($arreach['name'] == 'style') { |
1781 | $value = $arreach['value']; |
1782 | while (true) { |
1783 | $prevvalue = $value; |
1784 | $value = kses_no_null($value); |
1785 | $value = preg_replace("/\/\*.*\*\//Us", '', $value); |
1786 | $value = kses_decode_entities($value); |
1787 | $value = preg_replace('/(&#[0-9]+)(;?)/', "\\1;", $value); |
1788 | $value = preg_replace('/(&#x[0-9a-fA-F]+)(;?)/', "\\1;", $value); |
1789 | if ($value === $prevvalue) { |
1790 | $arreach['value'] = $value; |
1791 | break; |
1792 | } |
1793 | } |
1794 | $arreach['value'] = preg_replace("/j\s*a\s*v\s*a\s*s\s*c\s*r\s*i\s*p\s*t/i", "Xjavascript", $arreach['value']); |
1795 | $arreach['value'] = preg_replace("/e\s*x\s*p\s*r\s*e\s*s\s*s\s*i\s*o\s*n/i", "Xexpression", $arreach['value']); |
8cd2314b |
1796 | $arreach['value'] = preg_replace("/b\s*i\s*n\s*d\s*i\s*n\s*g/i", "Xbinding", $arreach['value']); |
b8806ccc |
1797 | } else if ($arreach['name'] == 'href') { |
ee7f231d |
1798 | //Adobe Acrobat Reader XSS protection |
920337d1 |
1799 | $arreach['value'] = preg_replace('/(\.(pdf|fdf|xfdf|xdp|xfd)[^#]*)#.*$/i', '$1', $arreach['value']); |
29939bea |
1800 | } |
049bd7db |
1801 | $attStr .= ' '.$arreach['name'].'="'.$arreach['value'].'"'; |
3bd7ffec |
1802 | } |
713126cd |
1803 | |
3bd7ffec |
1804 | $xhtml_slash = ''; |
037dcbb6 |
1805 | if (preg_match('%/\s*$%', $attrlist)) { |
67ccec43 |
1806 | $xhtml_slash = ' /'; |
3bd7ffec |
1807 | } |
b0ccd3fb |
1808 | return '<'. $slash . $elem . $attStr . $xhtml_slash .'>'; |
3bd7ffec |
1809 | } |
1810 | |
d48b00b4 |
1811 | /** |
1812 | * Replaces all known smileys in the text with image equivalents |
1813 | * |
1814 | * @uses $CFG |
1815 | * @param string $text Passed by reference. The string to search for smily strings. |
1816 | * @return string |
1817 | */ |
5f350e8f |
1818 | function replace_smilies(&$text) { |
183a13c2 |
1819 | |
2ea9027b |
1820 | global $CFG; |
183a13c2 |
1821 | |
1822 | if (empty($CFG->emoticons)) { /// No emoticons defined, nothing to process here |
1823 | return; |
1824 | } |
1825 | |
5a144b3a |
1826 | $lang = current_language(); |
93c61c18 |
1827 | $emoticonstring = $CFG->emoticons; |
69081931 |
1828 | static $e = array(); |
1829 | static $img = array(); |
93c61c18 |
1830 | static $emoticons = null; |
1831 | |
1832 | if (is_null($emoticons)) { |
1833 | $emoticons = array(); |
1834 | if ($emoticonstring) { |
1835 | $items = explode('{;}', $CFG->emoticons); |
1836 | foreach ($items as $item) { |
1837 | $item = explode('{:}', $item); |
c5659019 |
1838 | $emoticons[$item[0]] = $item[1]; |
93c61c18 |
1839 | } |
1840 | } |
1841 | } |
1842 | |
5a144b3a |
1843 | if (empty($img[$lang])) { /// After the first time this is not run again |
1844 | $e[$lang] = array(); |
1845 | $img[$lang] = array(); |
617778f2 |
1846 | foreach ($emoticons as $emoticon => $image){ |
fbfc2675 |
1847 | $alttext = get_string($image, 'pix'); |
4bfa414f |
1848 | $alttext = preg_replace('/^\[\[(.*)\]\]$/', '$1', $alttext); /// Clean alttext in case there isn't lang string for it. |
5a144b3a |
1849 | $e[$lang][] = $emoticon; |
1850 | $img[$lang][] = '<img alt="'. $alttext .'" width="15" height="15" src="'. $CFG->pixpath .'/s/'. $image .'.gif" />'; |
617778f2 |
1851 | } |
c0f728ba |
1852 | } |
b7a3cf49 |
1853 | |
8dcd43f3 |
1854 | // Exclude from transformations all the code inside <script> tags |
1855 | // Needed to solve Bug 1185. Thanks to jouse 2001 detecting it. :-) |
1856 | // Based on code from glossary fiter by Williams Castillo. |
1857 | // - Eloy |
1858 | |
1859 | // Detect all the <script> zones to take out |
1860 | $excludes = array(); |
1861 | preg_match_all('/<script language(.+?)<\/script>/is',$text,$list_of_excludes); |
1862 | |
1863 | // Take out all the <script> zones from text |
1864 | foreach (array_unique($list_of_excludes[0]) as $key=>$value) { |
1865 | $excludes['<+'.$key.'+>'] = $value; |
1866 | } |
1867 | if ($excludes) { |
1868 | $text = str_replace($excludes,array_keys($excludes),$text); |
1869 | } |
1870 | |
fbfc2675 |
1871 | /// this is the meat of the code - this is run every time |
5a144b3a |
1872 | $text = str_replace($e[$lang], $img[$lang], $text); |
8dcd43f3 |
1873 | |
1874 | // Recover all the <script> zones to text |
1875 | if ($excludes) { |
1876 | $text = str_replace(array_keys($excludes),$excludes,$text); |
1877 | } |
1a072208 |
1878 | } |
0095d5cd |
1879 | |
740939ec |
1880 | /** |
1881 | * This code is called from help.php to inject a list of smilies into the |
1882 | * emoticons help file. |
1883 | * |
1884 | * @return string HTML for a list of smilies. |
1885 | */ |
1886 | function get_emoticons_list_for_help_file(){ |
1887 | global $CFG, $SESSION; |
1888 | if (empty($CFG->emoticons)) { |
1889 | return ''; |
1890 | } |
1891 | |
1892 | require_js(array('yui_yahoo', 'yui_event')); |
1893 | $items = explode('{;}', $CFG->emoticons); |
1894 | $output = '<ul id="emoticonlist">'; |
1895 | foreach ($items as $item) { |
1896 | $item = explode('{:}', $item); |
1897 | $output .= '<li><img src="' . $CFG->pixpath.'/s/' . $item[1] . '.gif" alt="' . |
1898 | $item[0] . '" /><code>' . $item[0] . '</code></li>'; |
1899 | } |
1900 | $output .= '</ul>'; |
1901 | if (!empty($SESSION->inserttextform)) { |
1902 | $formname = $SESSION->inserttextform; |
1903 | $fieldname = $SESSION->inserttextfield; |
1904 | } else { |
1905 | $formname = 'theform'; |
1906 | $fieldname = 'message'; |
1907 | } |
fa583f5f |
1908 | |
740939ec |
1909 | $output .= print_js_call('emoticons_help.init', array($formname, $fieldname, 'emoticonlist'), true); |
1910 | return $output; |
1911 | |
1912 | } |
1913 | |
89dcb99d |
1914 | /** |
1915 | * Given plain text, makes it into HTML as nicely as possible. |
1916 | * May contain HTML tags already |
1917 | * |
1918 | * @uses $CFG |
1919 | * @param string $text The string to convert. |
1920 | * @param boolean $smiley Convert any smiley characters to smiley images? |
1921 | * @param boolean $para If true then the returned string will be wrapped in paragraph tags |
1922 | * @param boolean $newlines If true then lines newline breaks will be converted to HTML newline breaks. |
1923 | * @return string |
1924 | */ |
1925 | |
b7a3d3b2 |
1926 | function text_to_html($text, $smiley=true, $para=true, $newlines=true) { |
772e78be |
1927 | /// |
f9903ed0 |
1928 | |
27326a3e |
1929 | global $CFG; |
1930 | |
c1d57101 |
1931 | /// Remove any whitespace that may be between HTML tags |
7b3be1b1 |
1932 | $text = eregi_replace(">([[:space:]]+)<", "><", $text); |
1933 | |
c1d57101 |
1934 | /// Remove any returns that precede or follow HTML tags |
0eae8049 |
1935 | $text = eregi_replace("([\n\r])<", " <", $text); |
1936 | $text = eregi_replace(">([\n\r])", "> ", $text); |
7b3be1b1 |
1937 | |
5f350e8f |
1938 | convert_urls_into_links($text); |
f9903ed0 |
1939 | |
c1d57101 |
1940 | /// Make returns into HTML newlines. |
b7a3d3b2 |
1941 | if ($newlines) { |
1942 | $text = nl2br($text); |
1943 | } |
f9903ed0 |
1944 | |
c1d57101 |
1945 | /// Turn smileys into images. |
d69cb7f4 |
1946 | if ($smiley) { |
5f350e8f |
1947 | replace_smilies($text); |
d69cb7f4 |
1948 | } |
f9903ed0 |
1949 | |
c1d57101 |
1950 | /// Wrap the whole thing in a paragraph tag if required |
909f539d |
1951 | if ($para) { |
b0ccd3fb |
1952 | return '<p>'.$text.'</p>'; |
909f539d |
1953 | } else { |
1954 | return $text; |
1955 | } |
f9903ed0 |
1956 | } |
1957 | |
d48b00b4 |
1958 | /** |
1959 | * Given Markdown formatted text, make it into XHTML using external function |
1960 | * |
89dcb99d |
1961 | * @uses $CFG |
1962 | * @param string $text The markdown formatted text to be converted. |
1963 | * @return string Converted text |
d48b00b4 |
1964 | */ |
e7cdcd18 |
1965 | function markdown_to_html($text) { |
e7cdcd18 |
1966 | global $CFG; |
1967 | |
b0ccd3fb |
1968 | require_once($CFG->libdir .'/markdown.php'); |
e7cdcd18 |
1969 | |
1970 | return Markdown($text); |
1971 | } |
1972 | |
d48b00b4 |
1973 | /** |
89dcb99d |
1974 | * Given HTML text, make it into plain text using external function |
d48b00b4 |
1975 | * |
1976 | * @uses $CFG |
1977 | * @param string $html The text to be converted. |
1978 | * @return string |
1979 | */ |
6ff45b59 |
1980 | function html_to_text($html) { |
89dcb99d |
1981 | |
428aaa29 |
1982 | global $CFG; |
6ff45b59 |
1983 | |
b0ccd3fb |
1984 | require_once($CFG->libdir .'/html2text.php'); |
6ff45b59 |
1985 | |
588acd06 |
1986 | $h2t = new html2text($html); |
1987 | $result = $h2t->get_text(); |
07e9a300 |
1988 | |
977b3d31 |
1989 | return $result; |
6ff45b59 |
1990 | } |
1991 | |
d48b00b4 |
1992 | /** |
1993 | * Given some text this function converts any URLs it finds into HTML links |
1994 | * |
1995 | * @param string $text Passed in by reference. The string to be searched for urls. |
1996 | */ |
5f350e8f |
1997 | function convert_urls_into_links(&$text) { |
5f350e8f |
1998 | /// Make lone URLs into links. eg http://moodle.com/ |
3405b212 |
1999 | $text = eregi_replace("([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", |
bc01a2b8 |
2000 | "\\1<a href=\"\\2://\\3\\4\" target=\"_blank\">\\2://\\3\\4</a>", $text); |
5f350e8f |
2001 | |
2002 | /// eg www.moodle.com |
ab9f24ad |
2003 | $text = eregi_replace("([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])", |
bc01a2b8 |
2004 | "\\1<a href=\"http://www.\\2\\3\" target=\"_blank\">www.\\2\\3</a>", $text); |
5f350e8f |
2005 | } |
2006 | |
d48b00b4 |
2007 | /** |
2008 | * This function will highlight search words in a given string |
2009 | * It cares about HTML and will not ruin links. It's best to use |
2010 | * this function after performing any conversions to HTML. |
d48b00b4 |
2011 | * |
9289e4c9 |
2012 | * @param string $needle The search string. Syntax like "word1 +word2 -word3" is dealt with correctly. |
2013 | * @param string $haystack The string (HTML) within which to highlight the search terms. |
2014 | * @param boolean $matchcase whether to do case-sensitive. Default case-insensitive. |
2015 | * @param string $prefix the string to put before each search term found. |
2016 | * @param string $suffix the string to put after each search term found. |
2017 | * @return string The highlighted HTML. |
d48b00b4 |
2018 | */ |
9289e4c9 |
2019 | function highlight($needle, $haystack, $matchcase = false, |
2020 | $prefix = '<span class="highlight">', $suffix = '</span>') { |
587c7040 |
2021 | |
9289e4c9 |
2022 | /// Quick bail-out in trivial cases. |
587c7040 |
2023 | if (empty($needle) or empty($haystack)) { |
69d51d3a |
2024 | return $haystack; |
2025 | } |
2026 | |
9289e4c9 |
2027 | /// Break up the search term into words, discard any -words and build a regexp. |
2028 | $words = preg_split('/ +/', trim($needle)); |
2029 | foreach ($words as $index => $word) { |
2030 | if (strpos($word, '-') === 0) { |
2031 | unset($words[$index]); |
2032 | } else if (strpos($word, '+') === 0) { |
2033 | $words[$index] = '\b' . preg_quote(ltrim($word, '+'), '/') . '\b'; // Match only as a complete word. |
2034 | } else { |
2035 | $words[$index] = preg_quote($word, '/'); |
88438a58 |
2036 | } |
2037 | } |
9289e4c9 |
2038 | $regexp = '/(' . implode('|', $words) . ')/u'; // u is do UTF-8 matching. |
2039 | if (!$matchcase) { |
2040 | $regexp .= 'i'; |
88438a58 |
2041 | } |
2042 | |
9289e4c9 |
2043 | /// Another chance to bail-out if $search was only -words |
2044 | if (empty($words)) { |
2045 | return $haystack; |
88438a58 |
2046 | } |
88438a58 |
2047 | |
9289e4c9 |
2048 | /// Find all the HTML tags in the input, and store them in a placeholders array. |
2049 | $placeholders = array(); |
2050 | $matches = array(); |
2051 | preg_match_all('/<[^>]*>/', $haystack, $matches); |
2052 | foreach (array_unique($matches[0]) as $key => $htmltag) { |
2053 | $placeholders['<|' . $key . '|>'] = $htmltag; |
2054 | } |
9ccdcd97 |
2055 | |
9289e4c9 |
2056 | /// In $hastack, replace each HTML tag with the corresponding placeholder. |
2057 | $haystack = str_replace($placeholders, array_keys($placeholders), $haystack); |
9ccdcd97 |
2058 | |
9289e4c9 |
2059 | /// In the resulting string, Do the highlighting. |
2060 | $haystack = preg_replace($regexp, $prefix . '$1' . $suffix, $haystack); |
9ccdcd97 |
2061 | |
9289e4c9 |
2062 | /// Turn the placeholders back into HTML tags. |
2063 | $haystack = str_replace(array_keys($placeholders), $placeholders, $haystack); |
88438a58 |
2064 | |
f60e7cfe |
2065 | return $haystack; |
88438a58 |
2066 | } |
2067 | |
d48b00b4 |
2068 | /** |
2069 | * This function will highlight instances of $needle in $haystack |
2070 | * It's faster that the above function and doesn't care about |
2071 | * HTML or anything. |
2072 | * |
2073 | * @param string $needle The string to search for |
2074 | * @param string $haystack The string to search for $needle in |
2075 | * @return string |
2076 | */ |
88438a58 |
2077 | function highlightfast($needle, $haystack) { |
5af78ed2 |
2078 | |
587c7040 |
2079 | if (empty($needle) or empty($haystack)) { |
2080 | return $haystack; |
2081 | } |
2082 | |
57f1b914 |
2083 | $parts = explode(moodle_strtolower($needle), moodle_strtolower($haystack)); |
5af78ed2 |
2084 | |
587c7040 |
2085 | if (count($parts) === 1) { |
2086 | return $haystack; |
2087 | } |
2088 | |
5af78ed2 |
2089 | $pos = 0; |
2090 | |
2091 | foreach ($parts as $key => $part) { |
2092 | $parts[$key] = substr($haystack, $pos, strlen($part)); |
2093 | $pos += strlen($part); |
2094 | |
b0ccd3fb |
2095 | $parts[$key] .= '<span class="highlight">'.substr($haystack, $pos, strlen($needle)).'</span>'; |
5af78ed2 |
2096 | $pos += strlen($needle); |
ab9f24ad |
2097 | } |
5af78ed2 |
2098 | |
587c7040 |
2099 | return str_replace('<span class="highlight"></span>', '', join('', $parts)); |
5af78ed2 |
2100 | } |
2101 | |
2ab4e4b8 |
2102 | /** |
2103 | * Return a string containing 'lang', xml:lang and optionally 'dir' HTML attributes. |
2104 | * Internationalisation, for print_header and backup/restorelib. |
2105 | * @param $dir Default false. |
2106 | * @return string Attributes. |
2107 | */ |
2108 | function get_html_lang($dir = false) { |
2109 | $direction = ''; |
2110 | if ($dir) { |
2111 | if (get_string('thisdirection') == 'rtl') { |
2112 | $direction = ' dir="rtl"'; |
2113 | } else { |
2114 | $direction = ' dir="ltr"'; |
2115 | } |
2116 | } |
2117 | //Accessibility: added the 'lang' attribute to $direction, used in theme <html> tag. |
2118 | $language = str_replace('_', '-', str_replace('_utf8', '', current_language())); |
0946fff4 |
2119 | @header('Content-Language: '.$language); |
84e3d2cc |
2120 | return ($direction.' lang="'.$language.'" xml:lang="'.$language.'"'); |
2ab4e4b8 |
2121 | } |
2122 | |
5c355019 |
2123 | /** |
2124 | * Return the markup for the destination of the 'Skip to main content' links. |
317d5ddc |
2125 | * Accessibility improvement for keyboard-only users. |
2126 | * Used in course formats, /index.php and /course/index.php |
2127 | * @return string HTML element. |
5c355019 |
2128 | */ |
2129 | function skip_main_destination() { |
2130 | return '<span id="maincontent"></span>'; |
2131 | } |
2132 | |
f9903ed0 |
2133 | |
9fa49e22 |
2134 | /// STANDARD WEB PAGE PARTS /////////////////////////////////////////////////// |
2135 | |
d48b00b4 |
2136 | /** |
2137 | * Print a standard header |
2138 | * |
89dcb99d |
2139 | * @uses $USER |
2140 | * @uses $CFG |
89dcb99d |
2141 | * @uses $SESSION |
f1af7aaa |
2142 | * @param string $title Appears at the top of the window |
2143 | * @param string $heading Appears at the top of the page |
2144 | * @param array $navigation Array of $navlinks arrays (keys: name, link, type) for use as breadcrumbs links |
2145 | * @param string $focus Indicates form element to get cursor focus on load eg inputform.password |
2146 | * @param string $meta Meta tags to be added to the header |
89dcb99d |
2147 | * @param boolean $cache Should this page be cacheable? |
f1af7aaa |
2148 | * @param string $button HTML code for a button (usually for module editing) |
2149 | * @param string $menu HTML code for a popup menu |
89dcb99d |
2150 | * @param boolean $usexml use XML for this page |
f1af7aaa |
2151 | * @param string $bodytags This text will be included verbatim in the <body> tag (useful for onload() etc) |
2152 | * @param bool $return If true, return the visible elements of the header instead of echoing them. |
d48b00b4 |
2153 | */ |
36b6bcec |
2154 | function print_header ($title='', $heading='', $navigation='', $focus='', |
2155 | $meta='', $cache=true, $button=' ', $menu='', |
2156 | $usexml=false, $bodytags='', $return=false) { |
63f3cbbd |
2157 | |
c13a5e71 |
2158 | global $USER, $CFG, $THEME, $SESSION, $ME, $SITE, $COURSE, $PAGE; |
84e3d2cc |
2159 | |
432038e0 |
2160 | if (gettype($navigation) == 'string' && strlen($navigation) != 0 && $navigation != 'home') { |
364fffda |
2161 | debugging("print_header() was sent a string as 3rd ($navigation) parameter. " |
8ebb324b |
2162 | . "This is deprecated in favour of an array built by build_navigation(). Please upgrade your code.", DEBUG_DEVELOPER); |
364fffda |
2163 | } |
2164 | |
c13a5e71 |
2165 | $PAGE->set_state(moodle_page::STATE_PRINTING_HEADER); |
2166 | |
6ba65fa0 |
2167 | $heading = format_string($heading); // Fix for MDL-8582 |
84e3d2cc |
2168 | |
fbd0c5e3 |
2169 | if (CLI_SCRIPT) { |
2f13f94c |
2170 | $output = $heading; |
2171 | if ($return) { |
2172 | return $output; |
2173 | } else { |
27bd819b |
2174 | console_write($output . "\n",'',false); |
2f13f94c |
2175 | return; |
2176 | } |
2177 | } |
2178 | |
c3f55692 |
2179 | /// Add the required stylesheets |
d74d4f20 |
2180 | $stylesheetshtml = ''; |
2181 | foreach ($CFG->stylesheets as $stylesheet) { |
2182 | $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n"; |
c3f55692 |
2183 | } |
d74d4f20 |
2184 | $meta = $stylesheetshtml.$meta; |
e89fb61e |
2185 | |
84e3d2cc |
2186 | |
79f533c3 |
2187 | /// Add the meta page from the themes if any were requested |
2188 | |
2189 | $metapage = ''; |
2190 | |
2191 | if (!isset($THEME->standardmetainclude) || $THEME->standardmetainclude) { |
2192 | ob_start(); |
2193 | include_once($CFG->dirroot.'/theme/standard/meta.php'); |
2194 | $metapage .= ob_get_contents(); |
2195 | ob_end_clean(); |
2196 | } |
2197 | |
2198 | if ($THEME->parent && (!isset($THEME->parentmetainclude) || $THEME->parentmetainclude)) { |
36e8c122 |
2199 | if (file_exists($CFG->dirroot.'/theme/'.$THEME->parent.'/meta.php')) { |
2200 | ob_start(); |
2201 | include_once($CFG->dirroot.'/theme/'.$THEME->parent.'/meta.php'); |
2202 | $metapage .= ob_get_contents(); |
2203 | ob_end_clean(); |
2204 | } |
79f533c3 |
2205 | } |
2206 | |
2207 | if (!isset($THEME->metainclude) || $THEME->metainclude) { |
36e8c122 |
2208 | if (file_exists($CFG->dirroot.'/theme/'.current_theme().'/meta.php')) { |
2209 | ob_start(); |
2210 | include_once($CFG->dirroot.'/theme/'.current_theme().'/meta.php'); |
2211 | $metapage .= ob_get_contents(); |
2212 | ob_end_clean(); |
2213 | } |
79f533c3 |
2214 | } |
2215 | |
2216 | $meta = $meta."\n".$metapage; |
2217 | |
ca70075a |
2218 | $meta .= "\n".require_js('',1); |
c77f9f07 |
2219 | |
830c58b7 |
2220 | $meta .= standard_js_config(); |
2221 | |
c77f9f07 |
2222 | /// Set up some navigation variables |
604c6341 |
2223 | |
c77f9f07 |
2224 | if (is_newnav($navigation)){ |
9d378732 |
2225 | $home = false; |
ac4feb59 |
2226 | } else { |
2227 | if ($navigation == 'home') { |
2228 | $home = true; |
2229 | $navigation = ''; |
2230 | } else { |
2231 | $home = false; |
2232 | } |
9fa49e22 |
2233 | } |
2234 | |
0d741155 |
2235 | /// This is another ugly hack to make navigation elements available to print_footer later |
2236 | $THEME->title = $title; |
2237 | $THEME->heading = $heading; |
2238 | $THEME->navigation = $navigation; |
2239 | $THEME->button = $button; |
2240 | $THEME->menu = $menu; |
2507b2f5 |
2241 | $navmenulist = isset($THEME->navmenulist) ? $THEME->navmenulist : ''; |
0d741155 |
2242 | |
b0ccd3fb |
2243 | if ($button == '') { |
2244 | $button = ' '; |
9fa49e22 |
2245 | } |
2246 | |
a2353e9b |
2247 | if (file_exists($CFG->dataroot.'/'.SITEID.'/maintenance.html')) { |
d1aa1e48 |
2248 | $button = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/maintenance.php">'.get_string('maintenancemode', 'admin').'</a> '.$button; |
a2353e9b |
2249 | if(!empty($title)) { |
2250 | $title .= ' - '; |
2251 | } |
2252 | $title .= get_string('maintenancemode', 'admin'); |
2253 | } |
2254 | |
9fa49e22 |
2255 | if (!$menu and $navigation) { |
8a33e371 |
2256 | if (empty($CFG->loginhttps)) { |
2257 | $wwwroot = $CFG->wwwroot; |
2258 | } else { |
2c3432e6 |
2259 | $wwwroot = str_replace('http:','https:',$CFG->wwwroot); |
8a33e371 |
2260 | } |
60f9e36e |
2261 | $menu = user_login_string($COURSE); |
9fa49e22 |
2262 | } |
67ccec43 |
2263 | |
b4bac9b6 |
2264 | if (isset($SESSION->justloggedin)) { |
2265 | unset($SESSION->justloggedin); |
2266 | if (!empty($CFG->displayloginfailures)) { |
8f8ed475 |
2267 | if (!empty($USER->username) and $USER->username != 'guest') { |
b4bac9b6 |
2268 | if ($count = count_login_failures($CFG->displayloginfailures, $USER->username, $USER->lastlogin)) { |
2269 | $menu .= ' <font size="1">'; |
2270 | if (empty($count->accounts)) { |
2271 | $menu .= get_string('failedloginattempts', '', $count); |
2272 | } else { |
2273 | $menu .= get_string('failedloginattemptsall', '', $count); |
2274 | } |
a2e4bf7f |
2275 | if (has_capability('coursereport/log:view', get_context_instance(CONTEXT_SYSTEM))) { |
52af9a35 |
2276 | $menu .= ' (<a href="'.$CFG->wwwroot.'/course/report/log/index.php'. |
839f2456 |
2277 | '?chooselog=1&id=1&modid=site_errors">'.get_string('logs').'</a>)'; |
b4bac9b6 |
2278 | } |
2279 | $menu .= '</font>'; |
2280 | } |
2281 | } |
2282 | } |
2283 | } |
9fa49e22 |
2284 | |
47037513 |
2285 | |
bc1bbaf4 |
2286 | $meta = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . |
c6f9234c |
2287 | "\n" . $meta . "\n"; |
03fe48e7 |
2288 | if (!$usexml) { |
bc1bbaf4 |
2289 | @header('Content-Type: text/html; charset=utf-8'); |
03fe48e7 |
2290 | } |
bc1bbaf4 |
2291 | @header('Content-Script-Type: text/javascript'); |
2292 | @header('Content-Style-Type: text/css'); |
9fa49e22 |
2293 | |
6575bfd4 |
2294 | //Accessibility: added the 'lang' attribute to $direction, used in theme <html> tag. |
2ab4e4b8 |
2295 | $direction = get_html_lang($dir=true); |
ab9f24ad |
2296 | |
5debee2d |
2297 | if ($cache) { // Allow caching on "back" (but not on normal clicks) |
2298 | @header('Cache-Control: private, pre-check=0, post-check=0, max-age=0'); |
2299 | @header('Pragma: no-cache'); |
772e78be |
2300 | @header('Expires: '); |
5debee2d |
2301 | } else { // Do everything we can to always prevent clients and proxies caching |
03fe48e7 |
2302 | @header('Cache-Control: no-store, no-cache, must-revalidate'); |
2303 | @header('Cache-Control: post-check=0, pre-check=0', false); |
2304 | @header('Pragma: no-cache'); |
5debee2d |
2305 | @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT'); |
2306 | @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); |
03fe48e7 |
2307 | |
5debee2d |
2308 | $meta .= "\n<meta http-equiv=\"pragma\" content=\"no-cache\" />"; |
2309 | $meta .= "\n<meta http-equiv=\"expires\" content=\"0\" />"; |
66a51452 |
2310 | } |
5debee2d |
2311 | @header('Accept-Ranges: none'); |
66a51452 |
2312 | |
b0b4ffe1 |
2313 | $currentlanguage = current_language(); |
2314 | |
5ce73257 |
2315 | if (empty($usexml)) { |
27f79fda |
2316 | $direction = ' xmlns="http://www.w3.org/1999/xhtml"'. $direction; // See debug_header |
2317 | } else { |
8f0cd6ef |
2318 | $mathplayer = preg_match("/MathPlayer/i", $_SERVER['HTTP_USER_AGENT']); |
2319 | if(!$mathplayer) { |
2320 | header('Content-Type: application/xhtml+xml'); |
2321 | } |
b0ccd3fb |
2322 | echo '<?xml version="1.0" ?>'."\n"; |
66a51452 |
2323 | if (!empty($CFG->xml_stylesheets)) { |
b0ccd3fb |
2324 | $stylesheets = explode(';', $CFG->xml_stylesheets); |
66a51452 |
2325 | foreach ($stylesheets as $stylesheet) { |
b0ccd3fb |
2326 | echo '<?xml-stylesheet type="text/xsl" href="'. $CFG->wwwroot .'/'. $stylesheet .'" ?>' . "\n"; |
66a51452 |
2327 | } |
2328 | } |
b0ccd3fb |
2329 | echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1'; |
e4576482 |
2330 | if (!empty($CFG->xml_doctype_extra)) { |
b0ccd3fb |
2331 | echo ' plus '. $CFG->xml_doctype_extra; |
e4576482 |
2332 | } |
b0ccd3fb |
2333 | echo '//' . strtoupper($currentlanguage) . '" "'. $CFG->xml_dtd .'">'."\n"; |
8f0cd6ef |
2334 | $direction = " xmlns=\"http://www.w3.org/1999/xhtml\" |
2335 | xmlns:math=\"http://www.w3.org/1998/Math/MathML\" |
8f0cd6ef |
2336 | xmlns:xlink=\"http://www.w3.org/1999/xlink\" |
2337 | $direction"; |
2338 | if($mathplayer) { |
2339 | $meta .= '<object id="mathplayer" classid="clsid:32F66A20-7614-11D4-BD11-00104BD3F987">' . "\n"; |
b0ccd3fb |
2340 | $meta .= '<!--comment required to prevent this becoming an empty tag-->'."\n"; |
2341 | $meta .= '</object>'."\n"; |
8f0cd6ef |
2342 | $meta .= '<?import namespace="math" implementation="#mathplayer" ?>' . "\n"; |
2343 | } |
9fa49e22 |
2344 | } |
2345 | |
7bb6d80f |
2346 | // Clean up the title |
2347 | |
268ddd50 |
2348 | $title = format_string($title); // fix for MDL-8582 |
2eea2cce |
2349 | $title = str_replace('"', '"', $title); |
2350 | |
7bb6d80f |
2351 | // Create class and id for this page |
d529807a |
2352 | $pageid = $PAGE->pagetype; |
753debd2 |
2353 | $pageclass = $PAGE->bodyclasses; |
7bb6d80f |
2354 | $bodytags .= ' class="'.$pageclass.'" id="'.$pageid.'"'; |
772e78be |
2355 | |
6f8a7d39 |
2356 | ob_start(); |
ea35ab87 |
2357 | include($CFG->header); |
6f8a7d39 |
2358 | $output = ob_get_contents(); |
2359 | ob_end_clean(); |
b26be9a9 |
2360 | |
d795bfdb |
2361 | // container debugging info |
2362 | $THEME->open_header_containers = open_containers(); |
2363 | |
317d5ddc |
2364 | // Skip to main content, see skip_main_destination(). |
2365 | if ($pageid=='course-view' or $pageid=='site-index' or $pageid=='course-index') { |
5c355019 |
2366 | $skiplink = '<a class="skip" href="#maincontent">'.get_string('tocontent', 'access').'</a>'; |
d0b8e40d |
2367 | if (! preg_match('/(.*<div[^>]+id="page"[^>]*>)(.*)/s', $output, $matches)) { |
5c355019 |
2368 | preg_match('/(.*<body.*?>)(.*)/s', $output, $matches); |
2369 | } |
2370 | $output = $matches[1]."\n". $skiplink .$matches[2]; |
317d5ddc |
2371 | } |
5c355019 |
2372 | |
8af1b1ba |
2373 | $output = force_strict_header($output); |
27f79fda |
2374 | |
cdf39255 |
2375 | if (!empty($CFG->messaging)) { |
36b6bcec |
2376 | $output .= message_popup_window(); |
2377 | } |
2378 | |
ca70075a |
2379 | // Add in any extra JavaScript libraries that occurred during the header |
2380 | $output .= require_js('', 2); |
3b0bf2e4 |
2381 | $output .= print_js_call('moodle_initialise_body', array(), true); |
ca70075a |
2382 | |
c13a5e71 |
2383 | $PAGE->set_state(moodle_page::STATE_IN_BODY); |
2384 | |
36b6bcec |
2385 | if ($return) { |
2386 | return $output; |
2387 | } else { |
2388 | echo $output; |
cdf39255 |
2389 | } |
9fa49e22 |
2390 | } |
2391 | |
ca70075a |
2392 | /** |
2393 | * Used to include JavaScript libraries. |
2394 | * |
2395 | * When the $lib parameter is given, the function will ensure that the |
e874af28 |
2396 | * named library or libraries is loaded onto the page - either in the |
2397 | * HTML <head>, just after the header, or at an arbitrary later point in |
2398 | * the page, depending on where this function is called. |
ca70075a |
2399 | * |
2400 | * Libraries will not be included more than once, so this works like |
2401 | * require_once in PHP. |
2402 | * |
e874af28 |
2403 | * There are two special-case calls to this function from print_header which are |
fa583f5f |
2404 | * internal to weblib and use the second $extracthtml parameter: |
ca70075a |
2405 | * $extracthtml = 1: this is used before printing the header. |
e874af28 |
2406 | * It returns the script tag code that should go inside the <head>. |
ca70075a |
2407 | * $extracthtml = 2: this is used after printing the header and handles any |
e874af28 |
2408 | * require_js calls that occurred within the header itself. |
ca70075a |
2409 | * |
e874af28 |
2410 | * @param mixed $lib The library or libraries to load (a string or array of strings) |
2411 | * There are three way to specify the library: |
2412 | * 1. a shorname like 'yui_yahoo'. The list of recognised values is in lib/ajax/ajaxlib.php |
2413 | * 2. the path to the library relative to wwwroot, for example 'lib/javascript-static.js' |
2414 | * 3. (legacy) a full URL like $CFG->wwwroot . '/lib/javascript-static.js'. |
2415 | * @param int $extracthtml Private. For internal weblib use only. |
2416 | * @return mixed No return value (except when doing the internal $extracthtml |
2417 | * calls, when it returns html code). |
ca70075a |
2418 | */ |
e874af28 |
2419 | function require_js($lib, $extracthtml = 0) { |
ca70075a |
2420 | global $CFG; |
2421 | static $loadlibs = array(); |
2422 | |
2423 | static $state = REQUIREJS_BEFOREHEADER; |
2424 | static $latecode = ''; |
2425 | |
2426 | if (!empty($lib)) { |
2427 | // Add the lib to the list of libs to be loaded, if it isn't already |
2428 | // in the list. |
2429 | if (is_array($lib)) { |
2430 | foreach($lib as $singlelib) { |
2431 | require_js($singlelib); |
2432 | } |
2433 | } else { |
2434 | $libpath = ajax_get_lib($lib); |
2435 | if (array_search($libpath, $loadlibs) === false) { |
2436 | $loadlibs[] = $libpath; |
2437 | |
2438 | // For state other than 0 we need to take action as well as just |
2439 | // adding it to loadlibs |
2440 | if($state != REQUIREJS_BEFOREHEADER) { |
2441 | // Get the script statement for this library |
2442 | $scriptstatement=get_require_js_code(array($libpath)); |
2443 | |
2444 | if($state == REQUIREJS_AFTERHEADER) { |
2445 | // After the header, print it immediately |
2446 | print $scriptstatement; |
2447 | } else { |
2448 | // Haven't finished the header yet. Add it after the |
2449 | // header |
2450 | $latecode .= $scriptstatement; |
2451 | } |
2452 | } |
2453 | } |
2454 | } |
2455 | } else if($extracthtml==1) { |
2456 | if($state !== REQUIREJS_BEFOREHEADER) { |
2457 | debugging('Incorrect state in require_js (expected BEFOREHEADER): be careful not to call with empty $lib (except in print_header)'); |
2458 | } else { |
2459 | $state = REQUIREJS_INHEADER; |
2460 | } |
2461 | |
2462 | return get_require_js_code($loadlibs); |
2463 | } else if($extracthtml==2) { |
2464 | if($state !== REQUIREJS_INHEADER) { |
2465 | debugging('Incorrect state in require_js (expected INHEADER): be careful not to call with empty $lib (except in print_header)'); |
2466 | return ''; |
2467 | } else { |
2468 | $state = REQUIREJS_AFTERHEADER; |
2469 | return $latecode; |
2470 | } |
2471 | } else { |
2472 | debugging('Unexpected value for $extracthtml'); |
2473 | } |
2474 | } |
2475 | |
2476 | /** |
2477 | * Should not be called directly - use require_js. This function obtains the code |
2478 | * (script tags) needed to include JavaScript libraries. |
2479 | * @param array $loadlibs Array of library files to include |
2480 | * @return string HTML code to include them |
2481 | */ |
2482 | function get_require_js_code($loadlibs) { |
2483 | global $CFG; |
2484 | // Return the html needed to load the JavaScript files defined in |
2485 | // our list of libs to be loaded. |
2486 | $output = ''; |
2487 | foreach ($loadlibs as $loadlib) { |
2488 | $output .= '<script type="text/javascript" '; |
2489 | $output .= " src=\"$loadlib\"></script>\n"; |
2490 | if ($loadlib == $CFG->wwwroot.'/lib/yui/logger/logger-min.js') { |
2491 | // Special case, we need the CSS too. |
2492 | $output .= '<link type="text/css" rel="stylesheet" '; |
2493 | $output .= " href=\"{$CFG->wwwroot}/lib/yui/logger/assets/logger.css\" />\n"; |
2494 | } |
2495 | } |
2496 | return $output; |
2497 | } |
2498 | |
78b5eb25 |
2499 | /** |
2500 | * Generate the HTML for calling a javascript funtion. You often need to do this |
2501 | * if you have your javascript in an external file, and need to call one function |
2502 | * to initialise it. |
2503 | * |
949cf26c |
2504 | * You can pass in an optional list of arguments, which are properly escaped for |
2505 | * you using the json_encode function. |
78b5eb25 |
2506 | * |
2507 | * @param string $function the name of the JavaScript function to call. |
2508 | * @param array $args an optional list of arguments to the function call. |
2509 | * @param boolean $return if true, return the HTML code, otherwise output it. |
aa41944f |
2510 | * @return mixed string if $return is true, otherwise nothing. |
78b5eb25 |
2511 | */ |
2512 | function print_js_call($function, $args = array(), $return = false) { |
2513 | $quotedargs = array(); |
2514 | foreach ($args as $arg) { |
bef45cd3 |
2515 | $quotedargs[] = json_encode($arg); |
78b5eb25 |
2516 | } |
2517 | $html = ''; |
2518 | $html .= '<script type="text/javascript">//<![CDATA[' . "\n"; |
2519 | $html .= $function . '(' . implode(', ', $quotedargs) . ");\n"; |
2520 | $html .= "//]]></script>\n"; |
2521 | if ($return) { |
2522 | return $html; |
2523 | } else { |
2524 | echo $html; |
2525 | } |
2526 | } |
ca70075a |
2527 | |
29305001 |
2528 | /** |
2529 | * Generate the HTML for calling a javascript funtion after a time delay. |
2530 | * In other respects, this function is the same as print_js_call. |
2531 | * |
2532 | * @param integer $delay the desired delay in seconds. |
2533 | * @param string $function the name of the JavaScript function to call. |
2534 | * @param array $args an optional list of arguments to the function call. |
2535 | * @param boolean $return if true, return the HTML code, otherwise output it. |
2536 | * @return mixed string if $return is true, otherwise nothing. |
2537 | */ |
2538 | function print_delayed_js_call($delay, $function, $args = array(), $return = false) { |
2539 | $quotedargs = array(); |
2540 | foreach ($args as $arg) { |
2541 | $quotedargs[] = json_encode($arg); |
2542 | } |
2543 | $html = ''; |
2544 | $html .= '<script type="text/javascript">//<![CDATA[' . "\n"; |
2545 | $html .= 'setTimeout(function() {' . $function . '(' . |
2546 | implode(', ', $quotedargs) . ');}, ' . ($delay * 1000) . ");\n"; |
2547 | $html .= "//]]></script>\n"; |
2548 | if ($return) { |
2549 | return $html; |
2550 | } else { |
2551 | echo $html; |
2552 | } |
2553 | } |
2554 | |
aa41944f |
2555 | /** |
2556 | * Sometimes you need access to some values in your JavaScript that you can only |
2557 | * get from PHP code. You can handle this by generating your JS in PHP, but a |
2558 | * better idea is to write static javascrip code that reads some configuration |
2559 | * variable, and then just output the configuration variables from PHP using |
2560 | * this function. |
2561 | * |
fa583f5f |
2562 | * For example, look at the code in question_init_qenginejs_script() in |
aa41944f |
2563 | * lib/questionlib.php. It writes out a bunch of $settings like |
2564 | * 'pixpath' => $CFG->pixpath, with $prefix = 'qengine_config'. This gets output |
2565 | * in print_header, then the code in question/qengine.js can access these variables |
2566 | * as qengine_config.pixpath, and so on. |
2567 | * |
2568 | * This method will also work without a prefix, but it is better to avoid that |
2569 | * we don't want to add more things than necessary to the global JavaScript scope. |
2570 | * |
2571 | * This method automatically wrapps the values in quotes, and addslashes_js them. |
2572 | * |
2573 | * @param array $settings the values you want to write out, as variablename => value. |
2574 | * @param string $prefix a namespace prefix to use in the JavaScript. |
2575 | * @param boolean $return if true, return the HTML code, otherwise output it. |
2576 | * @return mixed string if $return is true, otherwise nothing. |
2577 | */ |
2578 | function print_js_config($settings = array(), $prefix='', $return = false) { |
2579 | $html = ''; |
2580 | $html .= '<script type="text/javascript">//<![CDATA[' . "\n"; |
2581 | |
2582 | // Have to treat the prefix and no prefix cases separately. |
2583 | if ($prefix) { |
2584 | // Recommended way, only one thing in global scope. |
740939ec |
2585 | $html .= "var $prefix = " . json_encode($settings) . "\n"; |
aa41944f |
2586 | |
2587 | } else { |
2588 | // Old fashioned way. |
2589 | foreach ($settings as $name => $value) { |
740939ec |
2590 | $html .= "var $name = '" . addslashes_js($value) . "'\n"; |
aa41944f |
2591 | } |
2592 | } |
2593 | |
2594 | // Finish off and return/output. |
2595 | $html .= "//]]></script>\n"; |
2596 | if ($return) { |
2597 | return $html; |
2598 | } else { |
2599 | echo $html; |
2600 | } |
2601 | } |
2602 | |
830c58b7 |
2603 | /** |
2604 | * This function generates the code that defines the standard moodle_cfg object. |
2605 | * This object has a number of fields that are values that various pieces of |
2606 | * JavaScript code need access too. For example $CFG->wwwroot and $CFG->pixpath. |
2607 | * |
2608 | * @return string a <script> tag that defines the moodle_cfg object. |
2609 | */ |
2610 | function standard_js_config() { |
2611 | global $CFG; |
e4749fcc |
2612 | $config = array( |
830c58b7 |
2613 | 'wwwroot' => $CFG->httpswwwroot, // Yes, really. |
2614 | 'pixpath' => $CFG->pixpath, |
2615 | 'modpixpath' => $CFG->modpixpath, |
bd1884fe |
2616 | 'sesskey' => sesskey(), |
e4749fcc |
2617 | ); |
2618 | if (debugging('', DEBUG_DEVELOPER)) { |
2619 | $config['developerdebug'] = true; |
2620 | } |
2621 | return print_js_config($config, 'moodle_cfg', true); |
830c58b7 |
2622 | } |
2623 | |
27f79fda |
2624 | /** |
2625 | * Debugging aid: serve page as 'application/xhtml+xml' where possible, |
2626 | * and substitute the XHTML strict document type. |
2627 | * Note, requires the 'xmlns' fix in function print_header above. |
2628 | * See: http://tracker.moodle.org/browse/MDL-7883 |
2629 | * TODO: |
2630 | */ |
8af1b1ba |
2631 | function force_strict_header($output) { |
27f79fda |
2632 | global $CFG; |
2633 | $strict = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; |
2634 | $xsl = '/lib/xhtml.xsl'; |
2635 | |
8cf6cb90 |
2636 | if (!headers_sent() && !empty($CFG->xmlstrictheaders)) { // With xml strict headers, the browser will barf |
27f79fda |
2637 | $ctype = 'Content-Type: '; |
2638 | $prolog= "<?xml version='1.0' encoding='utf-8'?>\n"; |
2639 | |
5ce73257 |
2640 | if (isset($_SERVER['HTTP_ACCEPT']) |
27f79fda |
2641 | && false !== strpos($_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml')) { |
2642 | //|| false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') //Safari "Entity 'copy' not defined". |
2643 | // Firefox et al. |
2644 | $ctype .= 'application/xhtml+xml'; |
2645 | $prolog .= "<!--\n DEBUG: $ctype \n-->\n"; |
2646 | |
2647 | } else if (file_exists($CFG->dirroot.$xsl) |
2648 | && preg_match('/MSIE.*Windows NT/', $_SERVER['HTTP_USER_AGENT'])) { |
2649 | // XSL hack for IE 5+ on Windows. |
2650 | //$www_xsl = preg_replace('/(http:\/\/.+?\/).*/', '', $CFG->wwwroot) .$xsl; |
2651 | $www_xsl = $CFG->wwwroot .$xsl; |
2652 | $ctype .= 'application/xml'; |
2653 | $prolog .= "<?xml-stylesheet type='text/xsl' href='$www_xsl'?>\n"; |
2654 | $prolog .= "<!--\n DEBUG: $ctype \n-->\n"; |
2655 | |
2656 | } else { |
2657 | //ELSE: Mac/IE, old/non-XML browsers. |
2658 | $ctype .= 'text/html'; |
2659 | $prolog = ''; |
2660 | } |
2661 | @header($ctype.'; charset=utf-8'); |
2662 | $output = $prolog . $output; |
2663 | |
2664 | // Test parser error-handling. |
2665 | if (isset($_GET['error'])) { |
2666 | $output .= "__ TEST: XML well-formed error < __\n"; |
2667 | } |
2668 | } |
8af1b1ba |
2669 | |
84dec541 |
2670 | $output = preg_replace('/(<!DOCTYPE.+?>)/s', $strict, $output); // Always change the DOCTYPE to Strict 1.0 |
8af1b1ba |
2671 | |
27f79fda |
2672 | return $output; |
2673 | } |
2674 | |
2675 | |
2676 | |
d48b00b4 |
2677 | /** |
2678 | * This version of print_header is simpler because the course name does not have to be |
2679 | * provided explicitly in the strings. It can be used on the site page as in courses |
2680 | * Eventually all print_header could be replaced by print_header_simple |
2681 | * |
89dcb99d |
2682 | * @param string $title Appears at the top of the window |
2683 | * @param string $heading Appears at the top of the page |
2684 | * @param string $navigation Premade navigation string (for use as breadcrumbs links) |
2685 | * @param string $focus Indicates form element to get cursor focus on load eg inputform.password |
2686 | * @param string $meta Meta tags to be added to the header |
2687 | * @param boolean $cache Should this page be cacheable? |
2688 | * @param string $button HTML code for a button (usually for module editing) |
2689 | * @param string $menu HTML code for a popup menu |
2690 | * @param boolean $usexml use XML for this page |
2691 | * @param string $bodytags This text will be included verbatim in the <body> tag (useful for onload() etc) |
36b6bcec |
2692 | * @param bool $return If true, return the visible elements of the header instead of echoing them. |
d48b00b4 |
2693 | */ |
b0ccd3fb |
2694 | function print_header_simple($title='', $heading='', $navigation='', $focus='', $meta='', |
36b6bcec |
2695 | $cache=true, $button=' ', $menu='', $usexml=false, $bodytags='', $return=false) { |
90fcc576 |
2696 | |
5ce73257 |
2697 | global $COURSE, $CFG; |
90fcc576 |
2698 | |
5dc1e0be |
2699 | // if we have no navigation specified, build it |
2700 | if( empty($navigation) ){ |
2701 | $navigation = build_navigation(''); |
2702 | } |
2703 | |
84e3d2cc |
2704 | // If old style nav prepend course short name otherwise leave $navigation object alone |
ac4feb59 |
2705 | if (!is_newnav($navigation)) { |
7243c7c8 |
2706 | if ($COURSE->id != SITEID) { |
2707 | $shortname = '<a href="'.$CFG->wwwroot.'/course/view.php?id='. $COURSE->id .'">'. $COURSE->shortname .'</a> ->'; |
2708 | $navigation = $shortname.' '.$navigation; |
2709 | } |
ac4feb59 |
2710 | } |
2711 | |
2712 | $output = print_header($COURSE->shortname .': '. $title, $COURSE->fullname .' '. $heading, $navigation, $focus, $meta, |
da4124be |
2713 | $cache, $button, $menu, $usexml, $bodytags, true); |
36b6bcec |
2714 | |
2715 | if ($return) { |
2716 | return $output; |
2717 | } else { |
2718 | echo $output; |
2719 | } |
90fcc576 |
2720 | } |
629b5885 |
2721 | |
2722 | |
d48b00b4 |
2723 | /** |
2724 | * Can provide a course object to make the footer contain a link to |
2725 | * to the course home page, otherwise the link will go to the site home |
d48b00b4 |
2726 | * @uses $USER |
d795bfdb |
2727 | * @param mixed $course course object, used for course link button or |
2728 | * 'none' means no user link, only docs link |
2729 | * 'empty' means nothing printed in footer |
2730 | * 'home' special frontpage footer |
2731 | * @param object $usercourse course used in user link |
2732 | * @param boolean $return output as string |
2733 | * @return mixed string or void |
d48b00b4 |
2734 | */ |
469b6501 |
2735 | function print_footer($course=NULL, $usercourse=NULL, $return=false) { |
c13a5e71 |
2736 | global $USER, $CFG, $THEME, $COURSE, $SITE, $PAGE; |
9fa49e22 |
2737 | |
1ae083e4 |
2738 | if (defined('ADMIN_EXT_HEADER_PRINTED') and !defined('ADMIN_EXT_FOOTER_PRINTED')) { |
635773c6 |
2739 | admin_externalpage_print_footer(); |
2740 | return; |
1ae083e4 |
2741 | } |
2742 | |
c13a5e71 |
2743 | $PAGE->set_state(moodle_page::STATE_PRINTING_FOOTER); |
2744 | |
d795bfdb |
2745 | /// Course links or special footer |
9fa49e22 |
2746 | if ($course) { |
d795bfdb |
2747 | if ($course === 'empty') { |
2748 | // special hack - sometimes we do not want even the docs link in footer |
2749 | $output = ''; |
2750 | if (!empty($THEME->open_header_containers)) { |
2751 | for ($i=0; $i<$THEME->open_header_containers; $i++) { |
2752 | $output .= print_container_end_all(); // containers opened from header |
2753 | } |
2754 | } else { |
2755 | //1.8 theme compatibility |
2756 | $output .= "\n</div>"; // content div |
2757 | } |
2758 | $output .= "\n</div>\n</body>\n</html>"; // close page div started in header |
2759 | if ($return) { |
2760 | return $output; |
2761 | } else { |
2762 | echo $output; |
2763 | return; |
2764 | } |
2765 | |
2766 | } else if ($course === 'none') { // Don't print any links etc |
b3a2b9dd |
2767 | $homelink = ''; |
2768 | $loggedinas = ''; |
0d741155 |
2769 | $home = false; |
d795bfdb |
2770 | |
2771 | } else if ($course === 'home') { // special case for site home page - please do not remove |
c23b0ea1 |
2772 | $course = $SITE; |
a77ac3dc |
2773 | $homelink = '<div class="sitelink">'. |
8cd94820 |
2774 | '<a title="Moodle '. $CFG->release .'" href="http://moodle.org/">'. |
9ace5094 |
2775 | '<img style="width:100px;height:30px" src="'.$CFG->wwwroot.'/pix/moodlelogo.gif" alt="moodlelogo" /></a></div>'; |
0d741155 |
2776 | $home = true; |
d795bfdb |
2777 | |
9ace5094 |
2778 | } else if ($course === 'upgrade') { |
2779 | $home = false; |
2780 | $loggedinas = ''; |
2781 | $homelink = '<div class="sitelink">'. |
2782 | '<a title="Moodle '. $CFG->target_release .'" href="http://docs.moodle.org/en/Administrator_documentation" onclick="this.target=\'_blank\'">'. |
2783 | '<img style="width:100px;height:30px" src="'.$CFG->wwwroot.'/pix/moodlelogo.gif" alt="moodlelogo" /></a></div>'; |
2784 | |
9fa49e22 |
2785 | } else { |
fa738731 |
2786 | $homelink = '<div class="homelink"><a '.$CFG->frametarget.' href="'.$CFG->wwwroot. |
6ba65fa0 |
2787 | '/course/view.php?id='.$course->id.'">'.format_string($course->shortname).'</a></div>'; |
0d741155 |
2788 | $home = false; |
9fa49e22 |
2789 | } |
d795bfdb |
2790 | |
9fa49e22 |
2791 | } else { |
c23b0ea1 |
2792 | $course = $SITE; // Set course as site course by default |
fa738731 |
2793 | $homelink = '<div class="homelink"><a '.$CFG->frametarget.' href="'.$CFG->wwwroot.'/">'.get_string('home').'</a></div>'; |
0d741155 |
2794 | $home = false; |
9fa49e22 |
2795 | } |
2796 | |
0d741155 |
2797 | /// Set up some other navigation links (passed from print_header by ugly hack) |
2507b2f5 |
2798 | $menu = isset($THEME->menu) ? str_replace('navmenu', 'navmenufooter', $THEME->menu) : ''; |
2799 | $title = isset($THEME->title) ? $THEME->title : ''; |
2800 | $button = isset($THEME->button) ? $THEME->button : ''; |
2801 | $heading = isset($THEME->heading) ? $THEME->heading : ''; |
2802 | $navigation = isset($THEME->navigation) ? $THEME->navigation : ''; |
2803 | $navmenulist = isset($THEME->navmenulist) ? $THEME->navmenulist : ''; |
0d741155 |
2804 | |
f940ee41 |
2805 | |
b3a2b9dd |
2806 | /// Set the user link if necessary |
2807 | if (!$usercourse and is_object($course)) { |
1f2eec7b |
2808 | $usercourse = $course; |
2809 | } |
2810 | |
b3a2b9dd |
2811 | if (!isset($loggedinas)) { |
2812 | $loggedinas = user_login_string($usercourse, $USER); |
2813 | } |
2814 | |
0d741155 |
2815 | if ($loggedinas == $menu) { |
9e0d1983 |
2816 | $menu = ''; |
0d741155 |
2817 | } |
2818 | |
d795bfdb |
2819 | /// there should be exactly the same number of open containers as after the header |
2820 | if ($THEME->open_header_containers != open_containers()) { |
2821 | debugging('Unexpected number of open containers: '.open_containers().', expecting '.$THEME->open_header_containers, DEBUG_DEVELOPER); |
0ad439bb |
2822 | } |
2823 | |
b8cea9b2 |
2824 | /// Provide some performance info if required |
c2fd9e95 |
2825 | $performanceinfo = ''; |
afd2b299 |
2826 | if (defined('MDL_PERF') || (!empty($CFG->perfdebug) and $CFG->perfdebug > 7)) { |
c2fd9e95 |
2827 | $perf = get_performance_info(); |
cf1348ca |
2828 | if (defined('MDL_PERFTOLOG') && !function_exists('register_shutdown_function')) { |
853df85e |
2829 | error_log("PERF: " . $perf['txt']); |
2830 | } |
ea82d6b6 |
2831 | if (defined('MDL_PERFTOFOOT') || debugging() || $CFG->perfdebug > 7) { |
c2fd9e95 |
2832 | $performanceinfo = $perf['html']; |
2833 | } |
572fe9ab |
2834 | } |
b8cea9b2 |
2835 | |
b3a2b9dd |
2836 | /// Include the actual footer file |
a282d0ff |
2837 | |
469b6501 |
2838 | ob_start(); |
ea35ab87 |
2839 | include($CFG->footer); |
469b6501 |
2840 | $output = ob_get_contents(); |
2841 | ob_end_clean(); |
a9a9bdba |
2842 | |
c13a5e71 |
2843 | $PAGE->set_state(moodle_page::STATE_DONE); |
2844 | |
469b6501 |
2845 | if ($return) { |
2846 | return $output; |
2847 | } else { |
2848 | echo $output; |
2849 | } |
a282d0ff |
2850 | } |
2851 | |
c3f55692 |
2852 | /** |
2853 | * Returns the name of the current theme |
2854 | * |
2855 | * @uses $CFG |
8f8210b3 |
2856 | * @uses $USER |
2857 | * @uses $SESSION |
2858 | * @uses $COURSE |
11e7b506 |
2859 | * @uses $SCRIPT |
c3f55692 |
2860 | * @return string |
2861 | */ |
2862 | function current_theme() { |
11e7b506 |
2863 | global $CFG, $USER, $SESSION, $COURSE, $SCRIPT; |
8f8210b3 |
2864 | |
2865 | if (empty($CFG->themeorder)) { |
2866 | $themeorder = array('page', 'course', 'category', 'session', 'user', 'site'); |
2867 | } else { |
2868 | $themeorder = $CFG->themeorder; |
2869 | } |
2870 | |
7507f325 |
2871 | if (isloggedin() and isset($CFG->mnet_localhost_id) and $USER->mnethostid != $CFG->mnet_localhost_id) { |
1673e134 |
2872 | require_once($CFG->dirroot.'/mnet/peer.php'); |
2873 | $mnet_peer = new mnet_peer(); |
2874 | $mnet_peer->set_id($USER->mnethostid); |
2875 | } |
2876 | |
8f8210b3 |
2877 | $theme = ''; |
2878 | foreach ($themeorder as $themetype) { |
2879 | |
2880 | if (!empty($theme)) continue; |
84e3d2cc |
2881 | |
8f8210b3 |
2882 | switch ($themetype) { |
2883 | case 'page': // Page theme is for special page-only themes set by code |
2884 | if (!empty($CFG->pagetheme)) { |
2885 | $theme = $CFG->pagetheme; |
2886 | } |
2887 | break; |
2888 | case 'course': |
2889 | if (!empty($CFG->allowcoursethemes) and !empty($COURSE->theme)) { |
2890 | $theme = $COURSE->theme; |
2891 | } |
2892 | break; |
2893 | case 'category': |
2894 | if (!empty($CFG->allowcategorythemes)) { |
2895 | /// Nasty hack to check if we're in a category page |
11e7b506 |
2896 | if ($SCRIPT == '/course/category.php') { |
8f8210b3 |
2897 | global $id; |
2898 | if (!empty($id)) { |
2899 | $theme = current_category_theme($id); |
2900 | } |
2901 | /// Otherwise check if we're in a course that has a category theme set |
2902 | } else if (!empty($COURSE->category)) { |
2903 | $theme = current_category_theme($COURSE->category); |
2904 | } |
2905 | } |
2906 | break; |
2907 | case 'session': |
2908 | if (!empty($SESSION->theme)) { |
2909 | $theme = $SESSION->theme; |
2910 | } |
2911 | break; |
2912 | case 'user': |
2913 | if (!empty($CFG->allowuserthemes) and !empty($USER->theme)) { |
178bcb75 |
2914 | if (isloggedin() and $USER->mnethostid != $CFG->mnet_localhost_id && $mnet_peer->force_theme == 1 && $mnet_peer->theme != '') { |
1673e134 |
2915 | $theme = $mnet_peer->theme; |
2916 | } else { |
2917 | $theme = $USER->theme; |
2918 | } |
8f8210b3 |
2919 | } |
2920 | break; |
2921 | case 'site': |
7507f325 |
2922 | if (isloggedin() and isset($CFG->mnet_localhost_id) and $USER->mnethostid != $CFG->mnet_localhost_id && $mnet_peer->force_theme == 1 && $mnet_peer->theme != '') { |
1673e134 |
2923 | $theme = $mnet_peer->theme; |
2924 | } else { |
2925 | $theme = $CFG->theme; |
2926 | } |
8f8210b3 |
2927 | break; |
2928 | default: |
2929 | /// do nothing |
2930 | } |
2931 | } |
c3f55692 |
2932 | |
8f8210b3 |
2933 | /// A final check in case 'site' was not included in $CFG->themeorder |
2934 | if (empty($theme)) { |
2935 | $theme = $CFG->theme; |
2936 | } |
c3f55692 |
2937 | |
8f8210b3 |
2938 | return $theme; |
2939 | } |
c3f55692 |
2940 | |
8f8210b3 |
2941 | /** |
2942 | * Retrieves the category theme if one exists, otherwise checks the parent categories. |
2943 | * Recursive function. |
2944 | * |
2945 | * @uses $COURSE |
2946 | * @param integer $categoryid id of the category to check |
2947 | * @return string theme name |
2948 | */ |
2949 | function current_category_theme($categoryid=0) { |
f33e1ed4 |
2950 | global $COURSE, $DB; |
84e3d2cc |
2951 | |
8f8210b3 |
2952 | /// Use the COURSE global if the categoryid not set |
2953 | if (empty($categoryid)) { |
2954 | if (!empty($COURSE->category)) { |
2955 | $categoryid = $COURSE->category; |
2956 | } else { |
2957 | return false; |
2958 | } |
2959 | } |
84e3d2cc |
2960 | |
8f8210b3 |
2961 | /// Retrieve the current category |
f33e1ed4 |
2962 | if ($category = $DB->get_record('course_categories', array('id'=>$categoryid))) { |
84e3d2cc |
2963 | |
8f8210b3 |
2964 | /// Return the category theme if it exists |
2965 | if (!empty($category->theme)) { |
2966 | return $category->theme; |
c3f55692 |
2967 | |
8f8210b3 |
2968 | /// Otherwise try the parent category if one exists |
2969 | } else if (!empty($category->parent)) { |
2970 | return current_category_theme($category->parent); |
2971 | } |
c3f55692 |
2972 | |
8f8210b3 |
2973 | /// Return false if we can't find the category record |
c3f55692 |
2974 | } else { |
8f8210b3 |
2975 | return false; |
c3f55692 |
2976 | } |
2977 | } |
2978 | |
d48b00b4 |
2979 | /** |
2980 | * This function is called by stylesheets to set up the header |
2981 | * approriately as well as the current path |
2982 | * |
2983 | * @uses $CFG |
89dcb99d |
2984 | * @param int $lastmodified ? |
2985 | * @param int $lifetime ? |
2986 | * @param string $thename ? |
d48b00b4 |
2987 | */ |
f31701c4 |
2988 | function style_sheet_setup($lastmodified=0, $lifetime=300, $themename='', $forceconfig='', $lang='') { |
6535be85 |
2989 | |
9c4e6e21 |
2990 | global $CFG, $THEME; |
ab9f24ad |
2991 | |
a2e2bf64 |
2992 | // Fix for IE6 caching - we don't want the filemtime('styles.php'), instead use now. |
2993 | $lastmodified = time(); |
2994 | |
b0ccd3fb |
2995 | header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $lastmodified) . ' GMT'); |
2996 | header('Expires: ' . gmdate("D, d M Y H:i:s", time() + $lifetime) . ' GMT'); |
a2e2bf64 |
2997 | header('Cache-Control: max-age='. $lifetime); |
b0ccd3fb |
2998 | header('Pragma: '); |
c13a5e71 |
2999 | header('Content-type: text/css'); // Correct MIME type |
6535be85 |
3000 | |
909ec807 |
3001 | $DEFAULT_SHEET_LIST = array('styles_layout', 'styles_fonts', 'styles_color'); |
9c4e6e21 |
3002 | |
3003 | if (empty($themename)) { |
c13a5e71 |
3004 | $themename = current_theme(); // So we have something. Normally not needed. |
ab25ce31 |
3005 | } else { |
3006 | $themename = clean_param($themename, PARAM_SAFEDIR); |
9c4e6e21 |
3007 | } |
3008 | |
c13a5e71 |
3009 | theme_setup($themename); |
3010 | |
3011 | if (!empty($forceconfig)) { // Page wants to use the config from this theme instead |
9c4e6e21 |
3012 | unset($THEME); |
a44091bf |
3013 | include($CFG->themedir.'/'.$forceconfig.'/'.'config.php'); |
9c4e6e21 |
3014 | } |
3015 | |
3016 | /// If this is the standard theme calling us, then find out what sheets we need |
9c4e6e21 |
3017 | if ($themename == 'standard') { |
3018 | if (!isset($THEME->standardsheets) or $THEME->standardsheets === true) { // Use all the sheets we have |
3019 | $THEME->sheets = $DEFAULT_SHEET_LIST; |
c13a5e71 |
3020 | } else if (empty($THEME->standardsheets)) { // We can stop right now! |
9c4e6e21 |
3021 | echo "/***** Nothing required from this stylesheet by main theme *****/\n\n"; |
3022 | exit; |
c13a5e71 |
3023 | } else { // Use the provided subset only |
9c4e6e21 |
3024 | $THEME->sheets = $THEME->standardsheets; |
3025 | } |
3026 | |
3027 | /// If we are a parent theme, then check for parent definitions |
9c4e6e21 |
3028 | } else if (!empty($THEME->parent) && $themename == $THEME->parent) { |
3029 | if (!isset($THEME->parentsheets) or $THEME->parentsheets === true) { // Use all the sheets we have |
3030 | $THEME->sheets = $DEFAULT_SHEET_LIST; |
3031 | } else if (empty($THEME->parentsheets)) { // We can stop right now! |
3032 | echo "/***** Nothing required from this stylesheet by main theme *****/\n\n"; |
3033 | exit; |
3034 | } else { // Use the provided subset only |
3035 | $THEME->sheets = $THEME->parentsheets; |
3036 | } |
3037 | } |
3038 | |
3039 | /// Work out the last modified date for this theme |
9c4e6e21 |
3040 | foreach ($THEME->sheets as $sheet) { |
a44091bf |
3041 | if (file_exists($CFG->themedir.'/'.$themename.'/'.$sheet.'.css')) { |
3042 | $sheetmodified = filemtime($CFG->themedir.'/'.$themename.'/'.$sheet.'.css'); |
9c4e6e21 |
3043 | if ($sheetmodified > $lastmodified) { |
3044 | $lastmodified = $sheetmodified; |
3045 | } |
3046 | } |
6535be85 |
3047 | } |
3048 | |
6ba172fb |
3049 | /// Get a list of all the files we want to include |
3050 | $files = array(); |
3051 | |
3052 | foreach ($THEME->sheets as $sheet) { |
3053 | $files[] = array($CFG->themedir, $themename.'/'.$sheet.'.css'); |
3054 | } |
3055 | |
3056 | if ($themename == 'standard') { // Add any standard styles included in any modules |
3057 | if (!empty($THEME->modsheets)) { // Search for styles.php within activity modules |
3058 | if ($mods = get_list_of_plugins('mod')) { |
3059 | foreach ($mods as $mod) { |
3060 | if (file_exists($CFG->dirroot.'/mod/'.$mod.'/styles.php')) { |
3061 | $files[] = array($CFG->dirroot, '/mod/'.$mod.'/styles.php'); |
3062 | } |
08396bb2 |
3063 | } |
3064 | } |
3065 | } |
a2b3f884 |
3066 | |
6ba172fb |
3067 | if (!empty($THEME->blocksheets)) { // Search for styles.php within block modules |
3068 | if ($mods = get_list_of_plugins('blocks')) { |
3069 | foreach ($mods as $mod) { |
3070 | if (file_exists($CFG->dirroot.'/blocks/'.$mod.'/styles.php')) { |
3071 | $files[] = array($CFG->dirroot, '/blocks/'.$mod.'/styles.php'); |
3072 | } |
08396bb2 |
3073 | } |
3074 | } |
3075 | } |
a2b3f884 |
3076 | |
ae628043 |
3077 | if (!isset($THEME->courseformatsheets) || $THEME->courseformatsheets) { // Search for styles.php in course formats |
3078 | if ($mods = get_list_of_plugins('format','',$CFG->dirroot.'/course')) { |
3079 | foreach ($mods as $mod) { |
3080 | if (file_exists($CFG->dirroot.'/course/format/'.$mod.'/styles.php')) { |
3081 | $files[] = array($CFG->dirroot, '/course/format/'.$mod.'/styles.php'); |
3082 | } |
3083 | } |
3084 | } |
3085 | } |
3086 | |
1273d7dd |
3087 | if (!isset($THEME->gradereportsheets) || $THEME->gradereportsheets) { // Search for styles.php in grade reports |
3088 | if ($reports = get_list_of_plugins('grade/report')) { |
3089 | foreach ($reports as $report) { |
3090 | if (file_exists($CFG->dirroot.'/grade/report/'.$report.'/styles.php')) { |
3091 | $files[] = array($CFG->dirroot, '/grade/report/'.$report.'/styles.php'); |
3092 | } |
3093 | } |
3094 | } |
3095 | } |
3096 | |
6ba172fb |
3097 | if (!empty($THEME->langsheets)) { // Search for styles.php within the current language |
6ba172fb |
3098 | if (file_exists($CFG->dirroot.'/lang/'.$lang.'/styles.php')) { |
3099 | $files[] = array($CFG->dirroot, '/lang/'.$lang.'/styles.php'); |
3100 | } |
3101 | } |
08396bb2 |
3102 | } |
3103 | |
6ba172fb |
3104 | if ($files) { |
3105 | /// Produce a list of all the files first |
3106 | echo '/**************************************'."\n"; |
3107 | echo ' * THEME NAME: '.$themename."\n *\n"; |
3108 | echo ' * Files included in this sheet:'."\n *\n"; |
3109 | foreach ($files as $file) { |
3110 | echo ' * '.$file[1]."\n"; |
08396bb2 |
3111 | } |
6ba172fb |
3112 | echo ' **************************************/'."\n\n"; |
08396bb2 |
3113 | |
6ba172fb |
3114 | |
16f0af59 |
3115 | /// check if csscobstants is set |
3116 | if (!empty($THEME->cssconstants)) { |
3117 | require_once("$CFG->libdir/cssconstants.php"); |
3118 | /// Actually collect all the files in order. |
3119 | $css = ''; |
3120 | foreach ($files as $file) { |
3121 | $css .= '/***** '.$file[1].' start *****/'."\n\n"; |
3122 | $css .= file_get_contents($file[0].'/'.$file[1]); |
3123 | $ccs .= '/***** '.$file[1].' end *****/'."\n\n"; |
3124 | } |
3125 | /// replace css_constants with their values |
3126 | echo replace_cssconstants($css); |
3127 | } else { |
3128 | /// Actually output all the files in order. |
d0c92410 |
3129 | if (empty($CFG->CSSEdit) && empty($THEME->CSSEdit)) { |
3130 | foreach ($files as $file) { |
3131 | echo '/***** '.$file[1].' start *****/'."\n\n"; |
3132 | @include_once($file[0].'/'.$file[1]); |
3133 | echo '/***** '.$file[1].' end *****/'."\n\n"; |
3134 | } |
3135 | } else { |
3136 | foreach ($files as $file) { |
3137 | echo '/* @group '.$file[1].' */'."\n\n"; |
3138 | if (strstr($file[1], '.css') !== FALSE) { |
3139 | echo '@import url("'.$CFG->themewww.'/'.$file[1].'");'."\n\n"; |
3140 | } else { |
3141 | @include_once($file[0].'/'.$file[1]); |
3142 | } |
3143 | echo '/* @end */'."\n\n"; |
3144 | } |
16f0af59 |
3145 | } |
6ba172fb |
3146 | } |
9c4e6e21 |
3147 | } |
3148 | |
a44091bf |
3149 | return $CFG->themewww.'/'.$themename; // Only to help old themes (1.4 and earlier) |
6535be85 |
3150 | } |
3151 | |
9c4e6e21 |
3152 | |
3153 | function theme_setup($theme = '', $params=NULL) { |
d74d4f20 |
3154 | /// Sets up global variables related to themes |
3155 | |
c13a5e71 |
3156 | global $CFG, $THEME, $SESSION, $USER, $HTTPSPAGEREQUIRED, $PAGE; |
d74d4f20 |
3157 | |
32462b2c |
3158 | /// Do not mess with THEME if header already printed - this would break all the extra stuff in global $THEME from print_header()!! |
c13a5e71 |
3159 | if ($PAGE->headerprinted) { |
32462b2c |
3160 | return; |
3161 | } |
3162 | |
d74d4f20 |
3163 | if (empty($theme)) { |
3164 | $theme = current_theme(); |
3165 | } |
9c4e6e21 |
3166 | |
09ad59dc |
3167 | /// If the theme doesn't exist for some reason then revert to standardwhite |
a44091bf |
3168 | if (!file_exists($CFG->themedir .'/'. $theme .'/config.php')) { |
09ad59dc |
3169 | $CFG->theme = $theme = 'standardwhite'; |
3170 | } |
3171 | |
55b734fb |
3172 | /// Load up the theme config |
3173 | $THEME = NULL; // Just to be sure |
a44091bf |
3174 | include($CFG->themedir .'/'. $theme .'/config.php'); // Main config for current theme |
55b734fb |
3175 | |
9c4e6e21 |
3176 | /// Put together the parameters |
3177 | if (!$params) { |
3178 | $params = array(); |
3179 | } |
3572b8b1 |
3180 | |
56766f85 |
3181 | if ($theme != $CFG->theme) { |
3182 | $params[] = 'forceconfig='.$theme; |
9c4e6e21 |
3183 | } |
55b734fb |
3184 | |
3185 | /// Force language too if required |
3186 | if (!empty($THEME->langsheets)) { |
3187 | $params[] = 'lang='.current_language(); |
3188 | } |
3189 | |
3190 | /// Convert params to string |
9c4e6e21 |
3191 | if ($params) { |
55b734fb |
3192 | $paramstring = '?'.implode('&', $params); |
d74d4f20 |
3193 | } else { |
9c4e6e21 |
3194 | $paramstring = ''; |
d74d4f20 |
3195 | } |
3196 | |
9c4e6e21 |
3197 | /// Set up image paths |
24c66531 |
3198 | if(isset($CFG->smartpix) && $CFG->smartpix==1) { |
3199 | if($CFG->slasharguments) { // Use this method if possible for better caching |
3200 | $extra=''; |
3201 | } else { |
3202 | $extra='?file='; |
3203 | } |
84e3d2cc |
3204 | |
24c66531 |
3205 | $CFG->pixpath = $CFG->wwwroot. '/pix/smartpix.php'.$extra.'/'.$theme; |
3206 | $CFG->modpixpath = $CFG->wwwroot .'/pix/smartpix.php'.$extra.'/'.$theme.'/mod'; |
3207 | } else if (empty($THEME->custompix)) { // Could be set in the above file |
d74d4f20 |
3208 | $CFG->pixpath = $CFG->wwwroot .'/pix'; |
3209 | $CFG->modpixpath = $CFG->wwwroot .'/mod'; |
3210 | } else { |
a44091bf |
3211 | $CFG->pixpath = $CFG->themewww .'/'. $theme .'/pix'; |
3212 | $CFG->modpixpath = $CFG->themewww .'/'. $theme .'/pix/mod'; |
d74d4f20 |
3213 | } |
3214 | |
9c4e6e21 |
3215 | /// Header and footer paths |
a44091bf |
3216 | $CFG->header = $CFG->themedir .'/'. $theme .'/header.html'; |
3217 | $CFG->footer = $CFG->themedir .'/'. $theme .'/footer.html'; |
d74d4f20 |
3218 | |
9c4e6e21 |
3219 | /// Define stylesheet loading order |
d74d4f20 |
3220 | $CFG->stylesheets = array(); |
3221 | if ($theme != 'standard') { /// The standard sheet is always loaded first |
a44091bf |
3222 | $CFG->stylesheets[] = $CFG->themewww.'/standard/styles.php'.$paramstring; |
d74d4f20 |
3223 | } |
3224 | if (!empty($THEME->parent)) { /// Parent stylesheets are loaded next |
a44091bf |
3225 | $CFG->stylesheets[] = $CFG->themewww.'/'.$THEME->parent.'/styles.php'.$paramstring; |
d74d4f20 |
3226 | } |
a44091bf |
3227 | $CFG->stylesheets[] = $CFG->themewww.'/'.$theme.'/styles.php'.$paramstring; |
d74d4f20 |
3228 | |
9152fc99 |
3229 | /// We have to change some URLs in styles if we are in a $HTTPSPAGEREQUIRED page |
|