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