7cf1c7bd |
1 | <?php |
f9903ed0 |
2 | |
9fa49e22 |
3 | /////////////////////////////////////////////////////////////////////////// |
4 | // // |
5 | // moodlelib.php // |
6 | // // |
7 | // Main library file of miscellaneous general-purpose Moodle functions // |
8 | // // |
9 | // Other main libraries: // |
10 | // // |
11 | // weblib.php - functions that produce web output // |
12 | // datalib.php - functions that access the database // |
13 | // // |
14 | /////////////////////////////////////////////////////////////////////////// |
15 | // // |
16 | // NOTICE OF COPYRIGHT // |
17 | // // |
18 | // Moodle - Modular Object-Oriented Dynamic Learning Environment // |
abc3b857 |
19 | // http://moodle.org // |
9fa49e22 |
20 | // // |
abc3b857 |
21 | // Copyright (C) 1999-2004 Martin Dougiamas http://dougiamas.com // |
9fa49e22 |
22 | // // |
23 | // This program is free software; you can redistribute it and/or modify // |
24 | // it under the terms of the GNU General Public License as published by // |
25 | // the Free Software Foundation; either version 2 of the License, or // |
26 | // (at your option) any later version. // |
27 | // // |
28 | // This program is distributed in the hope that it will be useful, // |
29 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
30 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
31 | // GNU General Public License for more details: // |
32 | // // |
33 | // http://www.gnu.org/copyleft/gpl.html // |
34 | // // |
35 | /////////////////////////////////////////////////////////////////////////// |
65ccdd8c |
36 | |
7cf1c7bd |
37 | /** |
38 | * Moodle main library |
39 | * |
40 | * Main library file of miscellaneous general-purpose Moodle functions. |
41 | * Other main libraries: |
42 | * <ul><li> weblib.php - functions that produce web output |
43 | * <li> datalib.php - functions that access the database</ul> |
44 | * @author Martin Dougiamas |
45 | * @version $Id$ |
46 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
47 | * @package moodlecore |
48 | */ |
f374fb10 |
49 | /// CONSTANTS ///////////////////////////////////////////////////////////// |
50 | |
7cf1c7bd |
51 | /** |
52 | * No groups used? |
53 | */ |
d8ba183c |
54 | define('NOGROUPS', 0); |
7cf1c7bd |
55 | |
56 | /** |
57 | * Groups used? |
58 | */ |
f374fb10 |
59 | define('SEPARATEGROUPS', 1); |
7cf1c7bd |
60 | |
61 | /** |
62 | * Groups visible? |
63 | */ |
f374fb10 |
64 | define('VISIBLEGROUPS', 2); |
65 | |
f9903ed0 |
66 | |
9fa49e22 |
67 | /// PARAMETER HANDLING //////////////////////////////////////////////////// |
6b174680 |
68 | |
7cf1c7bd |
69 | /** |
70 | * Ensure that a variable is set or display error |
71 | * |
72 | * If $var is undefined display an error message using the {@link error()} function. |
73 | * |
74 | * @param mixed $var the variable which may not be set |
75 | */ |
9fa49e22 |
76 | function require_variable($var) { |
77 | /// Variable must be present |
78 | if (! isset($var)) { |
b0ccd3fb |
79 | error('A required parameter was missing'); |
6b174680 |
80 | } |
81 | } |
82 | |
7cf1c7bd |
83 | |
84 | /** |
85 | * Ensure that a variable is set |
86 | * |
87 | * If $var is undefined set it (by reference), otherwise return $var. |
88 | * This function is very similar to {@link nvl()} |
89 | * |
90 | * @param mixed $var the variable which may be unset |
91 | * @param mixed $default the value to return if $var is unset |
92 | */ |
9fa49e22 |
93 | function optional_variable(&$var, $default=0) { |
94 | /// Variable may be present, if not then set a default |
95 | if (! isset($var)) { |
96 | $var = $default; |
6b174680 |
97 | } |
98 | } |
99 | |
7cf1c7bd |
100 | /** |
101 | * Set a key in global configuration |
102 | * |
103 | * Set a key/value pair in both this session's $CFG global variable |
104 | * and in the 'config' database table for future sessions. |
105 | * |
106 | * @param string $name the key to set |
107 | * @param string $value the value to set |
108 | * @uses $CFG |
109 | * @return bool |
110 | */ |
9fa49e22 |
111 | function set_config($name, $value) { |
112 | /// No need for get_config because they are usually always available in $CFG |
70812e39 |
113 | |
42282810 |
114 | global $CFG; |
115 | |
7cf1c7bd |
116 | |
42282810 |
117 | $CFG->$name = $value; // So it's defined for this invocation at least |
dfc9ba9b |
118 | |
b0ccd3fb |
119 | if (get_field('config', 'name', 'name', $name)) { |
120 | return set_field('config', 'value', $value, 'name', $name); |
d897cae4 |
121 | } else { |
9fa49e22 |
122 | $config->name = $name; |
123 | $config->value = $value; |
b0ccd3fb |
124 | return insert_record('config', $config); |
39917a09 |
125 | } |
39917a09 |
126 | } |
127 | |
7cf1c7bd |
128 | /** |
129 | * Refresh current $USER session global variable with all their current preferences. |
130 | * @uses $USER |
131 | */ |
70812e39 |
132 | function reload_user_preferences() { |
133 | /// Refresh current USER with all their current preferences |
134 | |
135 | global $USER; |
136 | |
d8ba183c |
137 | unset($USER->preference); |
70812e39 |
138 | |
139 | if ($preferences = get_records('user_preferences', 'userid', $USER->id)) { |
140 | foreach ($preferences as $preference) { |
141 | $USER->preference[$preference->name] = $preference->value; |
142 | } |
4586d60c |
143 | } else { |
144 | //return empty preference array to hold new values |
145 | $USER->preference = array(); |
146 | } |
70812e39 |
147 | } |
148 | |
7cf1c7bd |
149 | /** |
150 | * Sets a preference for the current user |
151 | * Optionally, can set a preference for a different user object |
152 | * @uses $USER |
153 | * @todo Add a better description and include usage examples. |
154 | * @param string $name The key to set as preference for the specified user |
155 | * @param string $value The value to set forthe $name key in the specified user's record |
156 | * @param object $user A moodle user object |
157 | * @todo Add inline links to $USER and user functions in above line. |
158 | * @return boolean |
159 | */ |
d35757eb |
160 | function set_user_preference($name, $value, $user=NULL) { |
70812e39 |
161 | /// Sets a preference for the current user |
d35757eb |
162 | /// Optionally, can set a preference for a different user object |
70812e39 |
163 | |
164 | global $USER; |
165 | |
d35757eb |
166 | if (empty($user)){ |
167 | $user = $USER; |
168 | } |
169 | |
70812e39 |
170 | if (empty($name)) { |
171 | return false; |
172 | } |
173 | |
d35757eb |
174 | if ($preference = get_record('user_preferences', 'userid', $user->id, 'name', $name)) { |
b0ccd3fb |
175 | if (set_field('user_preferences', 'value', $value, 'id', $preference->id)) { |
d35757eb |
176 | $user->preference[$name] = $value; |
066af654 |
177 | return true; |
178 | } else { |
179 | return false; |
180 | } |
70812e39 |
181 | |
182 | } else { |
d35757eb |
183 | $preference->userid = $user->id; |
70812e39 |
184 | $preference->name = $name; |
185 | $preference->value = (string)$value; |
066af654 |
186 | if (insert_record('user_preferences', $preference)) { |
d35757eb |
187 | $user->preference[$name] = $value; |
70812e39 |
188 | return true; |
189 | } else { |
190 | return false; |
191 | } |
192 | } |
193 | } |
194 | |
7cf1c7bd |
195 | /** |
196 | * Sets a whole array of preferences for the current user |
197 | * @param array $prefarray An array of key/value pairs to be set |
198 | * @return boolean |
199 | */ |
70812e39 |
200 | function set_user_preferences($prefarray) { |
201 | /// Sets a whole array of preferences for the current user |
202 | |
203 | if (!is_array($prefarray) or empty($prefarray)) { |
204 | return false; |
205 | } |
206 | |
207 | $return = true; |
208 | foreach ($prefarray as $name => $value) { |
209 | // The order is important; if the test for return is done first, |
210 | // then if one function call fails all the remaining ones will |
211 | // be "optimized away" |
212 | $return = set_user_preference($name, $value) and $return; |
213 | } |
214 | return $return; |
215 | } |
216 | |
7cf1c7bd |
217 | /** |
218 | * If no arguments are supplied this function will return |
219 | * all of the current user preferences as an array. |
220 | * If a name is specified then this function |
221 | * attempts to return that particular preference value. If |
222 | * none is found, then the optional value $default is returned, |
223 | * otherwise NULL. |
224 | * @param string $name Name of the key to use in finding a preference value |
225 | * @param string $default Value to be returned if the $name key is not set in the user preferences |
226 | * @uses $USER |
227 | * @return string |
228 | */ |
70812e39 |
229 | function get_user_preferences($name=NULL, $default=NULL) { |
230 | /// Without arguments, returns all the current user preferences |
d8ba183c |
231 | /// as an array. If a name is specified, then this function |
232 | /// attempts to return that particular preference value. If |
70812e39 |
233 | /// none is found, then the optional value $default is returned, |
234 | /// otherwise NULL. |
235 | |
236 | global $USER; |
237 | |
238 | if (empty($USER->preference)) { |
239 | return $default; // Default value (or NULL) |
240 | } |
241 | if (empty($name)) { |
242 | return $USER->preference; // Whole array |
243 | } |
244 | if (!isset($USER->preference[$name])) { |
245 | return $default; // Default value (or NULL) |
246 | } |
247 | return $USER->preference[$name]; // The single value |
248 | } |
249 | |
250 | |
9fa49e22 |
251 | /// FUNCTIONS FOR HANDLING TIME //////////////////////////////////////////// |
39917a09 |
252 | |
7cf1c7bd |
253 | /** |
254 | * Given date parts in user time, produce a GMT timestamp. |
255 | * |
256 | * @param type description |
257 | * @todo Finish documenting this function |
258 | */ |
3db75c62 |
259 | function make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0, $timezone=99) { |
9fa49e22 |
260 | /// Given date parts in user time, produce a GMT timestamp |
39917a09 |
261 | |
f30fe8d0 |
262 | $timezone = get_user_timezone($timezone); |
94e34118 |
263 | |
264 | if (abs($timezone) > 13) { |
03c17ddf |
265 | return mktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year); |
266 | } else { |
267 | $time = gmmktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year); |
268 | return usertime($time, $timezone); // This is GMT |
269 | } |
39917a09 |
270 | } |
271 | |
7cf1c7bd |
272 | /** |
273 | * Given an amount of time in seconds, returns string |
274 | * formatted nicely as months, days, hours etc as needed |
275 | * |
276 | * @param type description |
277 | * @todo Finish documenting this function |
278 | */ |
279 | function format_time($totalsecs, $str=NULL) { |
9fa49e22 |
280 | /// Given an amount of time in seconds, returns string |
281 | /// formatted nicely as months, days, hours etc as needed |
c7e3ac2a |
282 | |
6b174680 |
283 | $totalsecs = abs($totalsecs); |
c7e3ac2a |
284 | |
8dbed6be |
285 | if (!$str) { // Create the str structure the slow way |
b0ccd3fb |
286 | $str->day = get_string('day'); |
287 | $str->days = get_string('days'); |
288 | $str->hour = get_string('hour'); |
289 | $str->hours = get_string('hours'); |
290 | $str->min = get_string('min'); |
291 | $str->mins = get_string('mins'); |
292 | $str->sec = get_string('sec'); |
293 | $str->secs = get_string('secs'); |
8dbed6be |
294 | } |
295 | |
296 | $days = floor($totalsecs/86400); |
6b174680 |
297 | $remainder = $totalsecs - ($days*86400); |
8dbed6be |
298 | $hours = floor($remainder/3600); |
6b174680 |
299 | $remainder = $remainder - ($hours*3600); |
8dbed6be |
300 | $mins = floor($remainder/60); |
301 | $secs = $remainder - ($mins*60); |
302 | |
303 | $ss = ($secs == 1) ? $str->sec : $str->secs; |
304 | $sm = ($mins == 1) ? $str->min : $str->mins; |
305 | $sh = ($hours == 1) ? $str->hour : $str->hours; |
306 | $sd = ($days == 1) ? $str->day : $str->days; |
307 | |
b0ccd3fb |
308 | $odays = ''; |
309 | $ohours = ''; |
310 | $omins = ''; |
311 | $osecs = ''; |
9c9f7d77 |
312 | |
b0ccd3fb |
313 | if ($days) $odays = $days .' '. $sd; |
314 | if ($hours) $ohours = $hours .' '. $sh; |
315 | if ($mins) $omins = $mins .' '. $sm; |
316 | if ($secs) $osecs = $secs .' '. $ss; |
6b174680 |
317 | |
b0ccd3fb |
318 | if ($days) return $odays .' '. $ohours; |
319 | if ($hours) return $ohours .' '. $omins; |
320 | if ($mins) return $omins .' '. $osecs; |
321 | if ($secs) return $osecs; |
322 | return get_string('now'); |
6b174680 |
323 | } |
f9903ed0 |
324 | |
7cf1c7bd |
325 | /** |
326 | * Returns a formatted string that represents a date in user time |
327 | * <b>WARNING: note that the format is for strftime(), not date().</b> |
328 | * Because of a bug in most Windows time libraries, we can't use |
329 | * the nicer %e, so we have to use %d which has leading zeroes. |
330 | * A lot of the fuss in the function is just getting rid of these leading |
331 | * zeroes as efficiently as possible. |
332 | * |
333 | * If parammeter fixday = true (default), then take off leading |
334 | * zero from %d, else mantain it. |
335 | * |
336 | * @param type description |
337 | * @todo Finish documenting this function |
338 | */ |
b0ccd3fb |
339 | function userdate($date, $format='', $timezone=99, $fixday = true) { |
9fa49e22 |
340 | /// Returns a formatted string that represents a date in user time |
341 | /// WARNING: note that the format is for strftime(), not date(). |
d8ba183c |
342 | /// Because of a bug in most Windows time libraries, we can't use |
9fa49e22 |
343 | /// the nicer %e, so we have to use %d which has leading zeroes. |
d8ba183c |
344 | /// A lot of the fuss below is just getting rid of these leading |
9fa49e22 |
345 | /// zeroes as efficiently as possible. |
61ae5d36 |
346 | /// |
d8ba183c |
347 | /// If parammeter fixday = true (default), then take off leading |
61ae5d36 |
348 | /// zero from %d, else mantain it. |
7a302afc |
349 | |
b0ccd3fb |
350 | if ($format == '') { |
351 | $format = get_string('strftimedaydatetime'); |
5fa51a39 |
352 | } |
035cdbff |
353 | |
b0ccd3fb |
354 | $formatnoday = str_replace('%d', 'DD', $format); |
61ae5d36 |
355 | if ($fixday) { |
356 | $fixday = ($formatnoday != $format); |
357 | } |
dcde9f02 |
358 | |
f30fe8d0 |
359 | $timezone = get_user_timezone($timezone); |
90207a06 |
360 | |
0431bd7c |
361 | if (abs($timezone) > 13) { |
035cdbff |
362 | if ($fixday) { |
363 | $datestring = strftime($formatnoday, $date); |
b0ccd3fb |
364 | $daystring = str_replace(' 0', '', strftime(" %d", $date)); |
365 | $datestring = str_replace('DD', $daystring, $datestring); |
035cdbff |
366 | } else { |
367 | $datestring = strftime($format, $date); |
368 | } |
bea7a51e |
369 | } else { |
70d4cf82 |
370 | $date = $date + (int)($timezone * 3600); |
035cdbff |
371 | if ($fixday) { |
70d4cf82 |
372 | $datestring = gmstrftime($formatnoday, $date); |
b0ccd3fb |
373 | $daystring = str_replace(' 0', '', gmstrftime(" %d", $date)); |
374 | $datestring = str_replace('DD', $daystring, $datestring); |
035cdbff |
375 | } else { |
70d4cf82 |
376 | $datestring = gmstrftime($format, $date); |
035cdbff |
377 | } |
873960de |
378 | } |
bea7a51e |
379 | |
035cdbff |
380 | return $datestring; |
873960de |
381 | } |
382 | |
7cf1c7bd |
383 | /** |
384 | * Given a $date timestamp in GMT, returns an array |
385 | * that represents the date in user time |
386 | * |
387 | * @param type description |
388 | * @todo Finish documenting this function |
389 | */ |
5fa51a39 |
390 | function usergetdate($date, $timezone=99) { |
d8ba183c |
391 | /// Given a $date timestamp in GMT, returns an array |
9fa49e22 |
392 | /// that represents the date in user time |
6b174680 |
393 | |
f30fe8d0 |
394 | $timezone = get_user_timezone($timezone); |
a36166d3 |
395 | |
0431bd7c |
396 | if (abs($timezone) > 13) { |
873960de |
397 | return getdate($date); |
398 | } |
d2d6171f |
399 | //There is no gmgetdate so I have to fake it... |
400 | $date = $date + (int)($timezone * 3600); |
b0ccd3fb |
401 | $getdate['seconds'] = gmstrftime("%S", $date); |
402 | $getdate['minutes'] = gmstrftime("%M", $date); |
403 | $getdate['hours'] = gmstrftime("%H", $date); |
404 | $getdate['mday'] = gmstrftime("%d", $date); |
405 | $getdate['wday'] = gmstrftime("%u", $date); |
406 | $getdate['mon'] = gmstrftime("%m", $date); |
407 | $getdate['year'] = gmstrftime("%Y", $date); |
408 | $getdate['yday'] = gmstrftime("%j", $date); |
409 | $getdate['weekday'] = gmstrftime("%A", $date); |
410 | $getdate['month'] = gmstrftime("%B", $date); |
d2d6171f |
411 | return $getdate; |
d552ead0 |
412 | } |
413 | |
7cf1c7bd |
414 | /** |
415 | * Given a GMT timestamp (seconds since epoch), offsets it by |
416 | * the timezone. eg 3pm in India is 3pm GMT - 7 * 3600 seconds |
417 | * |
418 | * @param type description |
419 | * @todo Finish documenting this function |
420 | */ |
d552ead0 |
421 | function usertime($date, $timezone=99) { |
d8ba183c |
422 | /// Given a GMT timestamp (seconds since epoch), offsets it by |
9fa49e22 |
423 | /// the timezone. eg 3pm in India is 3pm GMT - 7 * 3600 seconds |
a36166d3 |
424 | |
f30fe8d0 |
425 | $timezone = get_user_timezone($timezone); |
0431bd7c |
426 | if (abs($timezone) > 13) { |
d552ead0 |
427 | return $date; |
428 | } |
429 | return $date - (int)($timezone * 3600); |
430 | } |
431 | |
edf7fe8c |
432 | function usergetmidnight($date, $timezone=99) { |
9fa49e22 |
433 | /// Given a time, return the GMT timestamp of the most recent midnight |
434 | /// for the current user. |
edf7fe8c |
435 | |
f30fe8d0 |
436 | $timezone = get_user_timezone($timezone); |
edf7fe8c |
437 | $userdate = usergetdate($date, $timezone); |
4606d9bb |
438 | |
0431bd7c |
439 | if (abs($timezone) > 13) { |
b0ccd3fb |
440 | return mktime(0, 0, 0, $userdate['mon'], $userdate['mday'], $userdate['year']); |
4606d9bb |
441 | } |
442 | |
b0ccd3fb |
443 | $timemidnight = gmmktime (0, 0, 0, $userdate['mon'], $userdate['mday'], $userdate['year']); |
edf7fe8c |
444 | return usertime($timemidnight, $timezone); // Time of midnight of this user's day, in GMT |
445 | |
446 | } |
447 | |
7cf1c7bd |
448 | /** |
449 | * Returns a string that prints the user's timezone |
450 | * |
451 | * @param float $timezone The user's timezone |
452 | * @return string |
453 | */ |
d552ead0 |
454 | function usertimezone($timezone=99) { |
9fa49e22 |
455 | /// Returns a string that prints the user's timezone |
d552ead0 |
456 | |
f30fe8d0 |
457 | $timezone = get_user_timezone($timezone); |
458 | |
0431bd7c |
459 | if (abs($timezone) > 13) { |
b0ccd3fb |
460 | return 'server time'; |
d552ead0 |
461 | } |
462 | if (abs($timezone) < 0.5) { |
b0ccd3fb |
463 | return 'GMT'; |
d552ead0 |
464 | } |
465 | if ($timezone > 0) { |
b0ccd3fb |
466 | return 'GMT+'. $timezone; |
d552ead0 |
467 | } else { |
b0ccd3fb |
468 | return 'GMT'. $timezone; |
d552ead0 |
469 | } |
f9903ed0 |
470 | } |
471 | |
7cf1c7bd |
472 | /** |
473 | * Returns a float which represents the user's timezone difference from GMT in hours |
474 | * Checks various settings and picks the most dominant of those which have a value |
475 | * |
476 | * @param float $tz The user's timezone |
477 | * @return float |
478 | * @uses $CFG |
479 | * @uses $USER |
480 | */ |
f30fe8d0 |
481 | function get_user_timezone($tz = 99) { |
482 | // Returns a float which represents the user's timezone difference from GMT in hours |
483 | // Checks various settings and picks the most dominant of those which have a value |
484 | |
485 | // Variables declared explicitly global here so that if we add |
486 | // something later we won't forget to global it... |
487 | $timezones = array( |
488 | isset($GLOBALS['USER']->timezone) ? $GLOBALS['USER']->timezone : 99, |
489 | isset($GLOBALS['CFG']->timezone) ? $GLOBALS['CFG']->timezone : 99, |
490 | ); |
491 | while($tz == 99 && $next = each($timezones)) { |
492 | $tz = (float)$next['value']; |
493 | } |
494 | |
495 | return $tz; |
496 | } |
f9903ed0 |
497 | |
9fa49e22 |
498 | /// USER AUTHENTICATION AND LOGIN //////////////////////////////////////// |
f9903ed0 |
499 | |
7cf1c7bd |
500 | /** |
501 | * This function checks that the current user is logged in, and optionally |
502 | * whether they are "logged in" or allowed to be in a particular course. |
503 | * If not, then it redirects them to the site login or course enrolment. |
504 | * $autologinguest determines whether visitors should automatically be |
505 | * logged in as guests provide $CFG->autologinguests is set to 1 |
506 | * |
507 | * @param type description |
508 | * @uses $CFG |
509 | * @uses $SESSION |
510 | * @uses $USER |
511 | * @uses $FULLME |
512 | * @uses $MoodleSession |
513 | * @todo Finish documenting this function |
514 | */ |
8e8d0524 |
515 | function require_login($courseid=0, $autologinguest=true) { |
9fa49e22 |
516 | /// This function checks that the current user is logged in, and optionally |
517 | /// whether they are "logged in" or allowed to be in a particular course. |
518 | /// If not, then it redirects them to the site login or course enrolment. |
8e8d0524 |
519 | /// $autologinguest determines whether visitors should automatically be |
520 | /// logged in as guests provide $CFG->autologinguests is set to 1 |
f9903ed0 |
521 | |
73047f2f |
522 | global $CFG, $SESSION, $USER, $FULLME, $MoodleSession; |
d8ba183c |
523 | |
da5c172a |
524 | // First check that the user is logged in to the site. |
c21c671d |
525 | if (! (isset($USER->loggedin) and $USER->confirmed and ($USER->site == $CFG->wwwroot)) ) { // They're not |
f9903ed0 |
526 | $SESSION->wantsurl = $FULLME; |
b0ccd3fb |
527 | if (!empty($_SERVER['HTTP_REFERER'])) { |
528 | $SESSION->fromurl = $_SERVER['HTTP_REFERER']; |
9f44d972 |
529 | } |
c21c671d |
530 | $USER = NULL; |
8e8d0524 |
531 | if ($autologinguest and $CFG->autologinguests and $courseid and get_field('course','guest','id',$courseid)) { |
532 | $loginguest = '?loginguest=true'; |
533 | } else { |
534 | $loginguest = ''; |
a2ebe6a5 |
535 | } |
8a33e371 |
536 | if (empty($CFG->loginhttps)) { |
b0ccd3fb |
537 | redirect($CFG->wwwroot .'/login/index.php'. $loginguest); |
8a33e371 |
538 | } else { |
b0ccd3fb |
539 | $wwwroot = str_replace('http','https', $CFG->wwwroot); |
540 | redirect($wwwroot .'/login/index.php'. $loginguest); |
8a33e371 |
541 | } |
f9903ed0 |
542 | die; |
f9903ed0 |
543 | } |
808a3baa |
544 | |
d35757eb |
545 | // check whether the user should be changing password |
546 | reload_user_preferences(); |
4586d60c |
547 | if (isset($USER->preference['auth_forcepasswordchange'])){ |
d35757eb |
548 | if (is_internal_auth() || $CFG->{'auth_'.$USER->auth.'_stdchangepassword'}){ |
b0ccd3fb |
549 | redirect($CFG->wwwroot .'/login/change_password.php'); |
d35757eb |
550 | } elseif($CFG->changepassword) { |
551 | redirect($CFG->changepassword); |
552 | } else { |
b0ccd3fb |
553 | error('You cannot proceed without changing your password. |
d35757eb |
554 | However there is no available page for changing it. |
b0ccd3fb |
555 | Please contact your Moodle Administrator.'); |
d35757eb |
556 | } |
557 | } |
558 | |
808a3baa |
559 | // Check that the user account is properly set up |
560 | if (user_not_fully_set_up($USER)) { |
b0ccd3fb |
561 | redirect($CFG->wwwroot .'/user/edit.php?id='. $USER->id .'&course='. SITEID); |
808a3baa |
562 | die; |
563 | } |
d8ba183c |
564 | |
da5c172a |
565 | // Next, check if the user can be in a particular course |
566 | if ($courseid) { |
9c9f7d77 |
567 | if (!empty($USER->student[$courseid]) or !empty($USER->teacher[$courseid]) or !empty($USER->admin)) { |
cb909d74 |
568 | if (isset($USER->realuser)) { // Make sure the REAL person can also access this course |
569 | if (!isteacher($courseid, $USER->realuser)) { |
570 | print_header(); |
b0ccd3fb |
571 | notice(get_string('studentnotallowed', '', fullname($USER, true)), $CFG->wwwroot .'/'); |
cb909d74 |
572 | } |
3ce2f1e0 |
573 | } |
da5c172a |
574 | return; // user is a member of this course. |
575 | } |
b0ccd3fb |
576 | if (! $course = get_record('course', 'id', $courseid)) { |
577 | error('That course doesn\'t exist'); |
da5c172a |
578 | } |
1efa27fd |
579 | if (!$course->visible) { |
580 | print_header(); |
b0ccd3fb |
581 | notice(get_string('studentnotallowed', '', fullname($USER, true)), $CFG->wwwroot .'/'); |
1efa27fd |
582 | } |
b0ccd3fb |
583 | if ($USER->username == 'guest') { |
7363ff91 |
584 | switch ($course->guest) { |
585 | case 0: // Guests not allowed |
586 | print_header(); |
b0ccd3fb |
587 | notice(get_string('guestsnotallowed', '', $course->fullname)); |
7363ff91 |
588 | break; |
589 | case 1: // Guests allowed |
7363ff91 |
590 | return; |
591 | case 2: // Guests allowed with key (drop through) |
592 | break; |
593 | } |
da5c172a |
594 | } |
f9903ed0 |
595 | |
7363ff91 |
596 | // Currently not enrolled in the course, so see if they want to enrol |
da5c172a |
597 | $SESSION->wantsurl = $FULLME; |
b0ccd3fb |
598 | redirect($CFG->wwwroot .'/course/enrol.php?id='. $courseid); |
da5c172a |
599 | die; |
600 | } |
f9903ed0 |
601 | } |
602 | |
7cf1c7bd |
603 | /** |
604 | * This is a weaker version of {@link require_login()} which only requires login |
605 | * when called from within a course rather than the site page, unless |
606 | * the forcelogin option is turned on. |
607 | * |
608 | * @uses $CFG |
609 | * @param type description |
610 | * @todo Finish documenting this function |
611 | */ |
f950af3c |
612 | function require_course_login($course, $autologinguest=true) { |
613 | // This is a weaker version of require_login which only requires login |
614 | // when called from within a course rather than the site page, unless |
615 | // the forcelogin option is turned on. |
616 | global $CFG; |
617 | if ($CFG->forcelogin) { |
618 | require_login(); |
619 | } |
620 | if ($course->category) { |
621 | require_login($course->id, $autologinguest); |
622 | } |
623 | } |
624 | |
7cf1c7bd |
625 | /** |
626 | * Modify the user table by setting the currently logged in user's |
627 | * last login to now. |
628 | * |
629 | * @uses $USER |
630 | * @return boolean |
631 | */ |
1d881d92 |
632 | function update_user_login_times() { |
633 | global $USER; |
634 | |
635 | $USER->lastlogin = $user->lastlogin = $USER->currentlogin; |
2a2f5f11 |
636 | $USER->currentlogin = $user->lastaccess = $user->currentlogin = time(); |
1d881d92 |
637 | |
638 | $user->id = $USER->id; |
639 | |
b0ccd3fb |
640 | return update_record('user', $user); |
1d881d92 |
641 | } |
642 | |
7cf1c7bd |
643 | /** |
644 | * Determines if a user has completed setting up their account. |
645 | * |
646 | * @param object $user A user object to test for the existance of a valid name and email |
647 | * @return boolean |
648 | */ |
808a3baa |
649 | function user_not_fully_set_up($user) { |
b0ccd3fb |
650 | return ($user->username != 'guest' and (empty($user->firstname) or empty($user->lastname) or empty($user->email))); |
808a3baa |
651 | } |
f9903ed0 |
652 | |
7cf1c7bd |
653 | /** |
654 | * Keeps track of login attempts |
655 | * |
656 | * @uses $SESSION |
657 | */ |
f9903ed0 |
658 | function update_login_count() { |
9fa49e22 |
659 | /// Keeps track of login attempts |
660 | |
f9903ed0 |
661 | global $SESSION; |
662 | |
663 | $max_logins = 10; |
664 | |
665 | if (empty($SESSION->logincount)) { |
666 | $SESSION->logincount = 1; |
667 | } else { |
668 | $SESSION->logincount++; |
669 | } |
670 | |
671 | if ($SESSION->logincount > $max_logins) { |
9fa49e22 |
672 | unset($SESSION->wantsurl); |
b0ccd3fb |
673 | error(get_string('errortoomanylogins')); |
d578afc8 |
674 | } |
675 | } |
676 | |
7cf1c7bd |
677 | /** |
678 | * Resets login attempts |
679 | * |
680 | * @uses $SESSION |
681 | */ |
9fa49e22 |
682 | function reset_login_count() { |
683 | /// Resets login attempts |
684 | global $SESSION; |
d578afc8 |
685 | |
9fa49e22 |
686 | $SESSION->logincount = 0; |
d578afc8 |
687 | } |
688 | |
7cf1c7bd |
689 | /** |
690 | * check_for_restricted_user |
691 | * |
692 | * @param type description |
693 | * @todo Finish documenting this function |
694 | */ |
b0ccd3fb |
695 | function check_for_restricted_user($username=NULL, $redirect='') { |
cb98d312 |
696 | global $CFG, $USER; |
697 | |
698 | if (!$username) { |
699 | if (!empty($USER->username)) { |
700 | $username = $USER->username; |
701 | } else { |
702 | return false; |
703 | } |
704 | } |
705 | |
706 | if (!empty($CFG->restrictusers)) { |
707 | $names = explode(',', $CFG->restrictusers); |
708 | if (in_array($username, $names)) { |
b0ccd3fb |
709 | error(get_string('restricteduser', 'error', fullname($USER)), $redirect); |
cb98d312 |
710 | } |
711 | } |
712 | } |
713 | |
7cf1c7bd |
714 | /** |
715 | * Determines if a user an admin |
716 | * |
717 | * @uses $USER |
718 | * @param integer $userid The id of the user as is found in the 'user' table |
719 | * @return boolean |
720 | */ |
581d7b49 |
721 | function isadmin($userid=0) { |
9fa49e22 |
722 | /// Is the user an admin? |
f9903ed0 |
723 | global $USER; |
aa095969 |
724 | static $admins = array(); |
725 | static $nonadmins = array(); |
f9903ed0 |
726 | |
581d7b49 |
727 | if (!$userid){ |
728 | if (empty($USER->id)) { |
729 | return false; |
730 | } |
731 | $userid = $USER->id; |
9bd2c874 |
732 | } |
733 | |
581d7b49 |
734 | if (in_array($userid, $admins)) { |
aa095969 |
735 | return true; |
581d7b49 |
736 | } else if (in_array($userid, $nonadmins)) { |
aa095969 |
737 | return false; |
b0ccd3fb |
738 | } else if (record_exists('user_admins', 'userid', $userid)){ |
581d7b49 |
739 | $admins[] = $userid; |
aa095969 |
740 | return true; |
741 | } else { |
581d7b49 |
742 | $nonadmins[] = $userid; |
aa095969 |
743 | return false; |
f9903ed0 |
744 | } |
f9903ed0 |
745 | } |
746 | |
7cf1c7bd |
747 | /** |
748 | * Determines if a user is a teacher or an admin |
749 | * |
750 | * @uses $USER |
751 | * @param integer $courseid The id of the course that is being viewed, if any |
752 | * @param integer $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user. |
753 | * @param boolean $includeadmin If true this function will return true when it encounters an admin user. |
754 | * @return boolean |
755 | * @todo Finish documenting this function |
756 | */ |
9d3c795c |
757 | function isteacher($courseid=0, $userid=0, $includeadmin=true) { |
9fa49e22 |
758 | /// Is the user a teacher or admin? |
f9903ed0 |
759 | global $USER; |
760 | |
9788367b |
761 | if ($includeadmin and isadmin($userid)) { // admins can do anything the teacher can |
d115a57f |
762 | return true; |
763 | } |
764 | |
f9903ed0 |
765 | if (!$userid) { |
71f9abf9 |
766 | if ($courseid) { |
767 | return !empty($USER->teacher[$courseid]); |
768 | } |
769 | if (!isset($USER->id)) { |
770 | return false; |
771 | } |
772 | $userid = $USER->id; |
f9903ed0 |
773 | } |
774 | |
9d3c795c |
775 | if (!$courseid) { |
b0ccd3fb |
776 | return record_exists('user_teachers', 'userid', $userid); |
9d3c795c |
777 | } |
778 | |
b0ccd3fb |
779 | return record_exists('user_teachers', 'userid', $userid, 'course', $courseid); |
f9903ed0 |
780 | } |
781 | |
7cf1c7bd |
782 | /** |
783 | * Determines if a user is allowed to edit a given course |
784 | * |
785 | * @uses $USER |
786 | * @param integer $courseid The id of the course that is being edited |
787 | * @param integer $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user. |
788 | * @return boolean |
789 | */ |
73047f2f |
790 | function isteacheredit($courseid, $userid=0) { |
791 | /// Is the user allowed to edit this course? |
792 | global $USER; |
793 | |
d8ba183c |
794 | if (isadmin($userid)) { // admins can do anything |
73047f2f |
795 | return true; |
796 | } |
797 | |
798 | if (!$userid) { |
799 | return !empty($USER->teacheredit[$courseid]); |
800 | } |
801 | |
b0ccd3fb |
802 | return get_field('user_teachers', 'editall', 'userid', $userid, 'course', $courseid); |
73047f2f |
803 | } |
804 | |
7cf1c7bd |
805 | /** |
806 | * Determines if a user can create new courses |
807 | * |
808 | * @uses $USER |
809 | * @param integer $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user. |
810 | * @return boolean |
811 | */ |
1924074c |
812 | function iscreator ($userid=0) { |
813 | /// Can user create new courses? |
814 | global $USER; |
8a205861 |
815 | if (empty($USER->id)) { |
816 | return false; |
817 | } |
1924074c |
818 | if (isadmin($userid)) { // admins can do anything |
819 | return true; |
820 | } |
8a205861 |
821 | if (empty($userid)) { |
b0ccd3fb |
822 | return record_exists('user_coursecreators', 'userid', $USER->id); |
1924074c |
823 | } |
824 | |
b0ccd3fb |
825 | return record_exists('user_coursecreators', 'userid', $userid); |
1924074c |
826 | } |
827 | |
7cf1c7bd |
828 | /** |
829 | * Determines if a user is a student in the specified course |
830 | * |
831 | * If the course id specifies the site then the function determines |
832 | * if the user is a confirmed and valid user of this site. |
833 | * |
834 | * @uses $USER |
835 | * @uses $CFG |
836 | * @param integer $courseid The id of the course being tested |
837 | * @param integer $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user. |
838 | * @return boolean |
839 | */ |
8a9e3fd7 |
840 | function isstudent($courseid, $userid=0) { |
9fa49e22 |
841 | /// Is the user a student in this course? |
2cc72e84 |
842 | /// If course is site, is the user a confirmed user on the site? |
71f9abf9 |
843 | global $USER, $CFG; |
f9903ed0 |
844 | |
2700d113 |
845 | if (empty($USER->id) and !$userid) { |
7064e18f |
846 | return false; |
847 | } |
848 | |
222ac91b |
849 | if ($courseid == SITEID) { |
2cc72e84 |
850 | if (!$userid) { |
851 | $userid = $USER->id; |
852 | } |
853 | if (isguest($userid)) { |
854 | return false; |
855 | } |
71f9abf9 |
856 | // a site teacher can never be a site student |
857 | if (isteacher($courseid, $userid)) { |
858 | return false; |
859 | } |
2700d113 |
860 | if ($CFG->allusersaresitestudents) { |
861 | return record_exists('user', 'id', $userid); |
862 | } else { |
863 | return (record_exists('user_students', 'userid', $userid) |
71f9abf9 |
864 | or record_exists('user_teachers', 'userid', $userid)); |
2700d113 |
865 | } |
8f0cd6ef |
866 | } |
2cc72e84 |
867 | |
f9903ed0 |
868 | if (!$userid) { |
346b1a24 |
869 | return !empty($USER->student[$courseid]); |
f9903ed0 |
870 | } |
871 | |
ebc3bd2b |
872 | // $timenow = time(); // todo: add time check below |
f9903ed0 |
873 | |
b0ccd3fb |
874 | return record_exists('user_students', 'userid', $userid, 'course', $courseid); |
f9903ed0 |
875 | } |
876 | |
7cf1c7bd |
877 | /** |
878 | * Determines if the specified user is logged in as guest. |
879 | * |
880 | * @uses $USER |
881 | * @param integer $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user. |
882 | * @return boolean |
883 | */ |
da5c172a |
884 | function isguest($userid=0) { |
9fa49e22 |
885 | /// Is the user a guest? |
da5c172a |
886 | global $USER; |
887 | |
888 | if (!$userid) { |
b35e8568 |
889 | if (empty($USER->username)) { |
890 | return false; |
891 | } |
b0ccd3fb |
892 | return ($USER->username == 'guest'); |
da5c172a |
893 | } |
894 | |
b0ccd3fb |
895 | return record_exists('user', 'id', $userid, 'username', 'guest'); |
da5c172a |
896 | } |
897 | |
7cf1c7bd |
898 | /** |
899 | * Determines if the currently logged in user is in editing mode |
900 | * |
901 | * @uses $USER |
902 | * @param integer $courseid The id of the course being tested |
903 | * @param object $user A user object. If null then the currently logged in user is used. |
904 | * @return boolean |
905 | */ |
2c309dc2 |
906 | function isediting($courseid, $user=NULL) { |
9fa49e22 |
907 | /// Is the current user in editing mode? |
2c309dc2 |
908 | global $USER; |
909 | if (!$user){ |
910 | $user = $USER; |
911 | } |
9c9f7d77 |
912 | if (empty($user->editing)) { |
913 | return false; |
914 | } |
2c309dc2 |
915 | return ($user->editing and isteacher($courseid, $user->id)); |
916 | } |
917 | |
7cf1c7bd |
918 | /** |
919 | * Determines if the logged in user is currently moving an activity |
920 | * |
921 | * @uses $USER |
922 | * @param integer $courseid The id of the course being tested |
923 | * @return boolean |
924 | */ |
7977cffd |
925 | function ismoving($courseid) { |
926 | /// Is the current user currently moving an activity? |
927 | global $USER; |
928 | |
929 | if (!empty($USER->activitycopy)) { |
930 | return ($USER->activitycopycourse == $courseid); |
931 | } |
932 | return false; |
933 | } |
934 | |
7cf1c7bd |
935 | /** |
936 | * Given an object containing firstname and lastname |
937 | * values, this function returns a string with the |
938 | * full name of the person. |
939 | * The result may depend on system settings |
940 | * or language. 'override' will force both names |
941 | * to be used even if system settings specify one. |
942 | * @uses $CFG |
943 | * @uses $SESSION |
944 | * @param type description |
945 | * @todo Finish documenting this function |
946 | */ |
e2cd5065 |
947 | function fullname($user, $override=false) { |
b5cbb64d |
948 | /// Given an object containing firstname and lastname |
d8ba183c |
949 | /// values, this function returns a string with the |
b5cbb64d |
950 | /// full name of the person. |
e2cd5065 |
951 | /// The result may depend on system settings |
b5cbb64d |
952 | /// or language. 'override' will force both names |
e2cd5065 |
953 | /// to be used even if system settings specify one. |
b5cbb64d |
954 | |
f374fb10 |
955 | global $CFG, $SESSION; |
956 | |
6527c077 |
957 | if (!isset($user->firstname) and !isset($user->lastname)) { |
958 | return ''; |
959 | } |
960 | |
f374fb10 |
961 | if (!empty($SESSION->fullnamedisplay)) { |
962 | $CFG->fullnamedisplay = $SESSION->fullnamedisplay; |
963 | } |
e2cd5065 |
964 | |
b5cbb64d |
965 | if ($CFG->fullnamedisplay == 'firstname lastname') { |
b0ccd3fb |
966 | return $user->firstname .' '. $user->lastname; |
b5cbb64d |
967 | |
968 | } else if ($CFG->fullnamedisplay == 'lastname firstname') { |
b0ccd3fb |
969 | return $user->lastname .' '. $user->firstname; |
e2cd5065 |
970 | |
b5cbb64d |
971 | } else if ($CFG->fullnamedisplay == 'firstname') { |
972 | if ($override) { |
973 | return get_string('fullnamedisplay', '', $user); |
974 | } else { |
975 | return $user->firstname; |
976 | } |
977 | } |
e2cd5065 |
978 | |
b5cbb64d |
979 | return get_string('fullnamedisplay', '', $user); |
e2cd5065 |
980 | } |
981 | |
7cf1c7bd |
982 | /** |
983 | * Sets a moodle cookie with an encrypted string |
984 | * |
985 | * @uses $CFG |
986 | * @param string $thing The string to encrypt and place in a cookie |
987 | */ |
f9903ed0 |
988 | function set_moodle_cookie($thing) { |
9fa49e22 |
989 | /// Sets a moodle cookie with an encrypted string |
7185e073 |
990 | global $CFG; |
482b6e6e |
991 | |
992 | $cookiename = 'MOODLEID_'.$CFG->sessioncookie; |
f9903ed0 |
993 | |
994 | $days = 60; |
995 | $seconds = 60*60*24*$days; |
996 | |
b0ccd3fb |
997 | setCookie($cookiename, '', time() - 3600, '/'); |
998 | setCookie($cookiename, rc4encrypt($thing), time()+$seconds, '/'); |
f9903ed0 |
999 | } |
1000 | |
7cf1c7bd |
1001 | /** |
1002 | * Gets a moodle cookie with an encrypted string |
1003 | * |
1004 | * @uses $CFG |
1005 | * @return string |
1006 | */ |
f9903ed0 |
1007 | function get_moodle_cookie() { |
9fa49e22 |
1008 | /// Gets a moodle cookie with an encrypted string |
7185e073 |
1009 | global $CFG; |
1010 | |
482b6e6e |
1011 | $cookiename = 'MOODLEID_'.$CFG->sessioncookie; |
7185e073 |
1012 | |
1079c8a8 |
1013 | if (empty($_COOKIE[$cookiename])) { |
b0ccd3fb |
1014 | return ''; |
1079c8a8 |
1015 | } else { |
1016 | return rc4decrypt($_COOKIE[$cookiename]); |
1017 | } |
f9903ed0 |
1018 | } |
1019 | |
7cf1c7bd |
1020 | /** |
1021 | * Returns true if an internal authentication method is being used. |
1022 | * if method not specified then, global default is assumed |
1023 | * |
1024 | * @uses $CFG |
1025 | * @param string $auth Form of authentication required |
1026 | * @return boolean |
1027 | * @todo Outline auth types and provide code example |
1028 | */ |
39a5a35d |
1029 | function is_internal_auth($auth='') { |
ba7166c3 |
1030 | /// Returns true if an internal authentication method is being used. |
39a5a35d |
1031 | /// if method not specified then, global default is assumed |
ba7166c3 |
1032 | |
1033 | global $CFG; |
1034 | |
39a5a35d |
1035 | $method = $CFG->auth; |
1036 | |
1037 | if (!empty($auth)) { |
1038 | $method = $auth; |
1039 | } |
1040 | |
b0ccd3fb |
1041 | return ($method == 'email' || $method == 'none' || $method == 'manual'); |
ba7166c3 |
1042 | } |
f9903ed0 |
1043 | |
7cf1c7bd |
1044 | /** |
1045 | * Creates a bare-bones user record |
1046 | * |
1047 | * @uses $CFG |
1048 | * @uses $REMOTE_ADDR |
1049 | * @param string $username New user's username to add to record |
1050 | * @param string $password New user's password to add to record |
1051 | * @param string $auth Form of authentication required |
1052 | * @return object |
1053 | * @todo Outline auth types and provide code example |
1054 | */ |
71f9abf9 |
1055 | function create_user_record($username, $password, $auth='') { |
d8ba183c |
1056 | /// Creates a bare-bones user record |
e858f9da |
1057 | global $REMOTE_ADDR, $CFG; |
71f9abf9 |
1058 | |
1e22bc9c |
1059 | //just in case check text case |
1060 | $username = trim(moodle_strtolower($username)); |
71f9abf9 |
1061 | |
3271b70f |
1062 | if (function_exists('auth_get_userinfo')) { |
e858f9da |
1063 | if ($newinfo = auth_get_userinfo($username)) { |
34daec9b |
1064 | foreach ($newinfo as $key => $value){ |
9f44d972 |
1065 | $newuser->$key = addslashes(stripslashes($value)); // Just in case |
e858f9da |
1066 | } |
1067 | } |
1068 | } |
f9903ed0 |
1069 | |
85a1d4c9 |
1070 | if (!empty($newuser->email)) { |
1071 | if (email_is_not_allowed($newuser->email)) { |
1072 | unset($newuser->email); |
1073 | } |
1074 | } |
1075 | |
71f9abf9 |
1076 | $newuser->auth = (empty($auth)) ? $CFG->auth : $auth; |
faebaf0f |
1077 | $newuser->username = $username; |
1078 | $newuser->password = md5($password); |
a0bac19d |
1079 | $newuser->lang = $CFG->lang; |
faebaf0f |
1080 | $newuser->confirmed = 1; |
59619427 |
1081 | $newuser->lastIP = getremoteaddr(); |
faebaf0f |
1082 | $newuser->timemodified = time(); |
f9903ed0 |
1083 | |
b0ccd3fb |
1084 | if (insert_record('user', $newuser)) { |
1085 | $user = get_user_info_from_db('username', $newuser->username); |
d35757eb |
1086 | if($CFG->{'auth_'.$newuser->auth.'_forcechangepassword'}){ |
1087 | set_user_preference('auth_forcepasswordchange', 1, $user); |
1088 | } |
1089 | return $user; |
faebaf0f |
1090 | } |
1091 | return false; |
1092 | } |
1093 | |
7cf1c7bd |
1094 | /** |
1095 | * Will update a local user record from an external source |
1096 | * |
1097 | * @uses $CFG |
1098 | * @param string $username New user's username to add to record |
1099 | * @return object |
1100 | */ |
d35757eb |
1101 | function update_user_record($username) { |
1102 | /// will update a local user record from an external source. |
1103 | global $CFG; |
1104 | |
1105 | if (function_exists('auth_get_userinfo')) { |
1106 | $username = trim(moodle_strtolower($username)); /// just in case check text case |
1107 | |
1108 | if ($newinfo = auth_get_userinfo($username)) { |
1109 | foreach ($newinfo as $key => $value){ |
1110 | if (!empty($CFG->{'auth_user_' . $key. '_updatelocal'})) { |
1111 | $value = addslashes(stripslashes($value)); // Just in case |
1112 | set_field('user', $key, $value, 'username', $username); |
1113 | } |
1114 | } |
1115 | } |
1116 | } |
b0ccd3fb |
1117 | return get_user_info_from_db('username', $username); |
d35757eb |
1118 | } |
0609562b |
1119 | |
7cf1c7bd |
1120 | /** |
1121 | * Retrieve the guest user object |
1122 | * |
1123 | * @uses $CFG |
1124 | * @return object |
1125 | */ |
0609562b |
1126 | function guest_user() { |
1127 | global $CFG; |
1128 | |
b0ccd3fb |
1129 | if ($newuser = get_record('user', 'username', 'guest')) { |
0609562b |
1130 | $newuser->loggedin = true; |
1131 | $newuser->confirmed = 1; |
1132 | $newuser->site = $CFG->wwwroot; |
1133 | $newuser->lang = $CFG->lang; |
1134 | } |
1135 | |
1136 | return $newuser; |
1137 | } |
1138 | |
7cf1c7bd |
1139 | /** |
1140 | * Given a username and password, this function looks them |
1141 | * up using the currently selected authentication mechanism, |
1142 | * and if the authentication is successful, it returns a |
1143 | * valid $user object from the 'user' table. |
1144 | * |
1145 | * Uses auth_ functions from the currently active auth module |
1146 | * |
1147 | * @uses $CFG |
1148 | * @param string $username user's username |
1149 | * @param string $password user's password |
1150 | * @return object |
1151 | */ |
faebaf0f |
1152 | function authenticate_user_login($username, $password) { |
d8ba183c |
1153 | /// Given a username and password, this function looks them |
9fa49e22 |
1154 | /// up using the currently selected authentication mechanism, |
d8ba183c |
1155 | /// and if the authentication is successful, it returns a |
9fa49e22 |
1156 | /// valid $user object from the 'user' table. |
1157 | /// |
1158 | /// Uses auth_ functions from the currently active auth module |
faebaf0f |
1159 | |
1160 | global $CFG; |
1161 | |
466558e3 |
1162 | $md5password = md5($password); |
1163 | |
27286aeb |
1164 | // First try to find the user in the database |
466558e3 |
1165 | |
b0ccd3fb |
1166 | $user = get_user_info_from_db('username', $username); |
39a5a35d |
1167 | |
27286aeb |
1168 | // Sort out the authentication method we are using. |
39a5a35d |
1169 | |
27286aeb |
1170 | if (empty($CFG->auth)) { |
b0ccd3fb |
1171 | $CFG->auth = 'manual'; // Default authentication module |
27286aeb |
1172 | } |
39a5a35d |
1173 | |
27286aeb |
1174 | if (empty($user->auth)) { // For some reason it isn't set yet |
1175 | if (isadmin($user->id) or isguest($user->id)) { |
71f9abf9 |
1176 | $auth = 'manual'; // Always assume these guys are internal |
27286aeb |
1177 | } else { |
71f9abf9 |
1178 | $auth = $CFG->auth; // Normal users default to site method |
27286aeb |
1179 | } |
d35757eb |
1180 | // update user record from external DB |
1181 | if ($user->auth != 'manual' && $user->auth != 'email') { |
1182 | $user = update_user_record($username); |
1183 | } |
71f9abf9 |
1184 | } else { |
1185 | $auth = $user->auth; |
27286aeb |
1186 | } |
8f0cd6ef |
1187 | |
b0ccd3fb |
1188 | if (!file_exists($CFG->dirroot .'/auth/'. $auth .'/lib.php')) { |
1189 | $auth = 'manual'; // Can't find auth module, default to internal |
466558e3 |
1190 | } |
1191 | |
b0ccd3fb |
1192 | require_once($CFG->dirroot .'/auth/'. $auth .'/lib.php'); |
faebaf0f |
1193 | |
1194 | if (auth_user_login($username, $password)) { // Successful authentication |
71f9abf9 |
1195 | if ($user) { // User already exists in database |
1196 | if (empty($user->auth)) { // For some reason auth isn't set yet |
1197 | set_field('user', 'auth', $auth, 'username', $username); |
1198 | } |
92710226 |
1199 | if ($md5password <> $user->password) { // Update local copy of password for reference |
71f9abf9 |
1200 | set_field('user', 'password', $md5password, 'username', $username); |
faebaf0f |
1201 | } |
d35757eb |
1202 | // update user record from external DB |
1203 | if ($user->auth != 'manual' && $user->auth != 'email'){ |
1204 | $user = update_user_record($username); |
1205 | } |
faebaf0f |
1206 | } else { |
71f9abf9 |
1207 | $user = create_user_record($username, $password, $auth); |
faebaf0f |
1208 | } |
89b54325 |
1209 | |
e582b65e |
1210 | if (function_exists('auth_iscreator')) { // Check if the user is a creator |
1211 | if (auth_iscreator($username)) { |
b0ccd3fb |
1212 | if (! record_exists('user_coursecreators', 'userid', $user->id)) { |
39a5a35d |
1213 | $cdata->userid = $user->id; |
b0ccd3fb |
1214 | if (! insert_record('user_coursecreators', $cdata)) { |
1215 | error('Cannot add user to course creators.'); |
39a5a35d |
1216 | } |
1217 | } |
e582b65e |
1218 | } else { |
b0ccd3fb |
1219 | if ( record_exists('user_coursecreators', 'userid', $user->id)) { |
1220 | if (! delete_records('user_coursecreators', 'userid', $user->id)) { |
1221 | error('Cannot remove user from course creators.'); |
39a5a35d |
1222 | } |
1223 | } |
e582b65e |
1224 | } |
39a5a35d |
1225 | } |
e582b65e |
1226 | return $user; |
9d3c795c |
1227 | |
e582b65e |
1228 | } else { |
b0ccd3fb |
1229 | add_to_log(0, 'login', 'error', $_SERVER['HTTP_REFERER'], $username); |
9d3c795c |
1230 | $date = date('Y-m-d H:i:s'); |
b0ccd3fb |
1231 | error_log($date ."\tfailed login\t". getremoteaddr() ."\t". $_SERVER['HTTP_USER_AGENT'] ."\t". $username); |
e582b65e |
1232 | return false; |
1233 | } |
f9903ed0 |
1234 | } |
1235 | |
7cf1c7bd |
1236 | /** |
1237 | * Enrols (or re-enrols) a student in a given course |
1238 | * |
1239 | * @param integer $courseid The id of the course that is being viewed |
1240 | * @param integer $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user. |
1241 | * @param integer $timestart ? |
1242 | * @param integer $timeend ? |
1243 | * @return boolean |
1244 | * @todo Finish documenting this function |
1245 | */ |
b40bc478 |
1246 | function enrol_student($userid, $courseid, $timestart=0, $timeend=0) { |
631cf796 |
1247 | /// Enrols (or re-enrols) a student in a given course |
b40bc478 |
1248 | |
b0ccd3fb |
1249 | if (!$course = get_record('course', 'id', $courseid)) { // Check course |
3041b0f8 |
1250 | return false; |
4d312bbe |
1251 | } |
b0ccd3fb |
1252 | if (!$user = get_record('user', 'id', $userid)) { // Check user |
631cf796 |
1253 | return false; |
1254 | } |
b0ccd3fb |
1255 | if ($student = get_record('user_students', 'userid', $userid, 'course', $courseid)) { |
631cf796 |
1256 | $student->timestart = $timestart; |
1257 | $student->timeend = $timeend; |
1258 | $student->time = time(); |
b0ccd3fb |
1259 | return update_record('user_students', $student); |
8f0cd6ef |
1260 | |
631cf796 |
1261 | } else { |
1262 | $student->userid = $userid; |
1263 | $student->course = $courseid; |
1264 | $student->timestart = $timestart; |
1265 | $student->timeend = $timeend; |
1266 | $student->time = time(); |
b0ccd3fb |
1267 | return insert_record('user_students', $student); |
631cf796 |
1268 | } |
d7facad8 |
1269 | } |
1270 | |
7cf1c7bd |
1271 | /** |
1272 | * Unenrols a student from a given course |
1273 | * |
1274 | * @param integer $courseid The id of the course that is being viewed, if any |
1275 | * @param integer $userid The id of the user that is being tested against. |
1276 | * @return boolean |
1277 | */ |
9fa62805 |
1278 | function unenrol_student($userid, $courseid=0) { |
9fa49e22 |
1279 | /// Unenrols a student from a given course |
d7facad8 |
1280 | |
9fa62805 |
1281 | if ($courseid) { |
9fa49e22 |
1282 | /// First delete any crucial stuff that might still send mail |
b0ccd3fb |
1283 | if ($forums = get_records('forum', 'course', $courseid)) { |
9fa49e22 |
1284 | foreach ($forums as $forum) { |
b0ccd3fb |
1285 | delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid); |
9fa62805 |
1286 | } |
1287 | } |
1288 | if ($groups = get_groups($courseid, $userid)) { |
1289 | foreach ($groups as $group) { |
b0ccd3fb |
1290 | delete_records('groups_members', 'groupid', $group->id, 'userid', $userid); |
bb09fb11 |
1291 | } |
f9903ed0 |
1292 | } |
b0ccd3fb |
1293 | return delete_records('user_students', 'userid', $userid, 'course', $courseid); |
9fa49e22 |
1294 | |
f9903ed0 |
1295 | } else { |
b0ccd3fb |
1296 | delete_records('forum_subscriptions', 'userid', $userid); |
1297 | delete_records('groups_members', 'userid', $userid); |
1298 | return delete_records('user_students', 'userid', $userid); |
f9903ed0 |
1299 | } |
1300 | } |
1301 | |
7cf1c7bd |
1302 | /** |
1303 | * Add a teacher to a given course |
1304 | * |
1305 | * @uses $USER |
1306 | * @param integer $courseid The id of the course that is being viewed, if any |
1307 | * @param integer $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user. |
1308 | * @param integer $editall ? |
1309 | * @param string $role ? |
1310 | * @param integer $timestart ? |
1311 | * @param integer $timeend ? |
1312 | * @return boolean |
1313 | * @todo Finish documenting this function |
1314 | */ |
b0ccd3fb |
1315 | function add_teacher($userid, $courseid, $editall=1, $role='', $timestart=0, $timeend=0) { |
3041b0f8 |
1316 | /// Add a teacher to a given course |
7b5944cd |
1317 | global $CFG; |
3041b0f8 |
1318 | |
61451a36 |
1319 | if ($teacher = get_record('user_teachers', 'userid', $userid, 'course', $courseid)) { |
b40bc478 |
1320 | $newteacher = NULL; |
1321 | $newteacher->id = $teacher->id; |
1322 | $newteacher->editall = $editall; |
1323 | if ($role) { |
1324 | $newteacher->role = $role; |
1325 | } |
1326 | if ($timestart) { |
1327 | $newteacher->timestart = $timestart; |
3041b0f8 |
1328 | } |
b40bc478 |
1329 | if ($timeend) { |
1330 | $newteacher->timeend = $timeend; |
1331 | } |
1332 | return update_record('user_teachers', $newteacher); |
3041b0f8 |
1333 | } |
61451a36 |
1334 | |
b0ccd3fb |
1335 | if (!record_exists('user', 'id', $userid)) { |
61451a36 |
1336 | return false; // no such user |
1337 | } |
1338 | |
b0ccd3fb |
1339 | if (!record_exists('course', 'id', $courseid)) { |
61451a36 |
1340 | return false; // no such course |
1341 | } |
1342 | |
1343 | $teacher = NULL; |
1344 | $teacher->userid = $userid; |
1345 | $teacher->course = $courseid; |
1346 | $teacher->editall = $editall; |
1347 | $teacher->role = $role; |
5a2dea02 |
1348 | $teacher->timemodified = time(); |
1349 | $newteacher->timestart = $timestart; |
1350 | $newteacher->timeend = $timeend; |
b0ccd3fb |
1351 | if ($student = get_record('user_students', 'userid', $userid, 'course', $courseid)) { |
5a2dea02 |
1352 | $teacher->timestart = $student->timestart; |
1353 | $teacher->timeend = $student->timeend; |
1354 | $teacher->timeaccess = $student->timeaccess; |
1355 | } |
61451a36 |
1356 | |
b0ccd3fb |
1357 | if (record_exists('user_teachers', 'course', $courseid)) { |
61451a36 |
1358 | $teacher->authority = 2; |
1359 | } else { |
1360 | $teacher->authority = 1; |
1361 | } |
b0ccd3fb |
1362 | delete_records('user_students', 'userid', $userid, 'course', $courseid); // Unenrol as student |
8f0cd6ef |
1363 | |
709f0ec8 |
1364 | /// Add forum subscriptions for new users |
7b5944cd |
1365 | require_once('../mod/forum/lib.php'); |
1366 | forum_add_user($userid, $courseid); |
61451a36 |
1367 | |
b0ccd3fb |
1368 | return insert_record('user_teachers', $teacher); |
61451a36 |
1369 | |
3041b0f8 |
1370 | } |
1371 | |
7cf1c7bd |
1372 | /** |
1373 | * Removes a teacher from a given course (or ALL courses) |
1374 | * Does not delete the user account |
1375 | * |
1376 | * @param integer $courseid The id of the course that is being viewed, if any |
1377 | * @param integer $userid The id of the user that is being tested against. |
1378 | * @return boolean |
1379 | */ |
3041b0f8 |
1380 | function remove_teacher($userid, $courseid=0) { |
9fa49e22 |
1381 | /// Removes a teacher from a given course (or ALL courses) |
1382 | /// Does not delete the user account |
3041b0f8 |
1383 | if ($courseid) { |
9fa49e22 |
1384 | /// First delete any crucial stuff that might still send mail |
b0ccd3fb |
1385 | if ($forums = get_records('forum', 'course', $courseid)) { |
9fa49e22 |
1386 | foreach ($forums as $forum) { |
b0ccd3fb |
1387 | delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid); |
9fa49e22 |
1388 | } |
1389 | } |
b02193e6 |
1390 | |
1391 | /// Next if the teacher is not registered as a student, but is |
1392 | /// a member of a group, remove them from the group. |
1393 | if (!isstudent($courseid, $userid)) { |
1394 | if ($groups = get_groups($courseid, $userid)) { |
1395 | foreach ($groups as $group) { |
b0ccd3fb |
1396 | delete_records('groups_members', 'groupid', $group->id, 'userid', $userid); |
b02193e6 |
1397 | } |
1398 | } |
1399 | } |
1400 | |
b0ccd3fb |
1401 | return delete_records('user_teachers', 'userid', $userid, 'course', $courseid); |
57507290 |
1402 | } else { |
b0ccd3fb |
1403 | delete_records('forum_subscriptions', 'userid', $userid); |
1404 | return delete_records('user_teachers', 'userid', $userid); |
57507290 |
1405 | } |
f9903ed0 |
1406 | } |
1407 | |
7cf1c7bd |
1408 | /** |
1409 | * Add a creator to the site |
1410 | * |
1411 | * @param integer $userid The id of the user that is being tested against. |
1412 | * @return boolean |
1413 | */ |
3041b0f8 |
1414 | function add_creator($userid) { |
1415 | /// Add a creator to the site |
1416 | |
b0ccd3fb |
1417 | if (!record_exists('user_admins', 'userid', $userid)) { |
1418 | if (record_exists('user', 'id', $userid)) { |
3041b0f8 |
1419 | $creator->userid = $userid; |
b0ccd3fb |
1420 | return insert_record('user_coursecreators', $creator); |
3041b0f8 |
1421 | } |
1422 | return false; |
1423 | } |
1424 | return true; |
1425 | } |
1426 | |
7cf1c7bd |
1427 | /** |
1428 | * Remove a creator from a site |
1429 | * |
1430 | * @uses $db |
1431 | * @param integer $userid The id of the user that is being tested against. |
1432 | * @return boolean |
1433 | */ |
3041b0f8 |
1434 | function remove_creator($userid) { |
1435 | /// Removes a creator from a site |
1436 | global $db; |
1437 | |
b0ccd3fb |
1438 | return delete_records('user_coursecreators', 'userid', $userid); |
3041b0f8 |
1439 | } |
1440 | |
7cf1c7bd |
1441 | /** |
1442 | * Add an admin to a site |
1443 | * |
1444 | * @uses SITEID |
1445 | * @param integer $userid The id of the user that is being tested against. |
1446 | * @return boolean |
1447 | */ |
3041b0f8 |
1448 | function add_admin($userid) { |
1449 | /// Add an admin to the site |
1450 | |
b0ccd3fb |
1451 | if (!record_exists('user_admins', 'userid', $userid)) { |
1452 | if (record_exists('user', 'id', $userid)) { |
3041b0f8 |
1453 | $admin->userid = $userid; |
8f0cd6ef |
1454 | |
f950af3c |
1455 | // any admin is also a teacher on the site course |
222ac91b |
1456 | if (!record_exists('user_teachers', 'course', SITEID, 'userid', $userid)) { |
1457 | if (!add_teacher($userid, SITEID)) { |
f950af3c |
1458 | return false; |
1459 | } |
1460 | } |
8f0cd6ef |
1461 | |
b0ccd3fb |
1462 | return insert_record('user_admins', $admin); |
3041b0f8 |
1463 | } |
1464 | return false; |
1465 | } |
1466 | return true; |
1467 | } |
1468 | |
7cf1c7bd |
1469 | /** |
1470 | * Removes an admin from a site |
1471 | * |
1472 | * @uses $db |
1473 | * @uses SITEID |
1474 | * @param integer $userid The id of the user that is being tested against. |
1475 | * @return boolean |
1476 | */ |
3041b0f8 |
1477 | function remove_admin($userid) { |
9fa49e22 |
1478 | /// Removes an admin from a site |
1479 | global $db; |
f9903ed0 |
1480 | |
f950af3c |
1481 | // remove also from the list of site teachers |
222ac91b |
1482 | remove_teacher($userid, SITEID); |
f950af3c |
1483 | |
b0ccd3fb |
1484 | return delete_records('user_admins', 'userid', $userid); |
f9903ed0 |
1485 | } |
1486 | |
7cf1c7bd |
1487 | /** |
1488 | * Clear a course out completely, deleting all content |
1489 | * but don't delete the course itself |
1490 | * |
1491 | * @uses $USER |
1492 | * @uses $SESSION |
1493 | * @uses $CFG |
1494 | * @param integer $courseid The id of the course that is being viewed |
1495 | * @param boolean $showfeedback Set this to false to suppress notifications from being printed as the functions performs its steps. |
1496 | * @return boolean |
1497 | */ |
07aeb7b0 |
1498 | function remove_course_contents($courseid, $showfeedback=true) { |
1499 | /// Clear a course out completely, deleting all content |
1500 | /// but don't delete the course itself |
1501 | |
ee23f384 |
1502 | global $CFG, $THEME, $USER, $SESSION; |
07aeb7b0 |
1503 | |
1504 | $result = true; |
1505 | |
b0ccd3fb |
1506 | if (! $course = get_record('course', 'id', $courseid)) { |
1507 | error('Course ID was incorrect (can\'t find it)'); |
07aeb7b0 |
1508 | } |
1509 | |
b0ccd3fb |
1510 | $strdeleted = get_string('deleted'); |
07aeb7b0 |
1511 | |
1512 | // First delete every instance of every module |
d8ba183c |
1513 | |
b0ccd3fb |
1514 | if ($allmods = get_records('modules') ) { |
07aeb7b0 |
1515 | foreach ($allmods as $mod) { |
1516 | $modname = $mod->name; |
b0ccd3fb |
1517 | $modfile = $CFG->dirroot .'/mod/'. $modname .'/lib.php'; |
1518 | $moddelete = $modname .'_delete_instance'; // Delete everything connected to an instance |
1519 | $moddeletecourse = $modname .'_delete_course'; // Delete other stray stuff (uncommon) |
07aeb7b0 |
1520 | $count=0; |
1521 | if (file_exists($modfile)) { |
1522 | include_once($modfile); |
1523 | if (function_exists($moddelete)) { |
b0ccd3fb |
1524 | if ($instances = get_records($modname, 'course', $course->id)) { |
07aeb7b0 |
1525 | foreach ($instances as $instance) { |
1526 | if ($moddelete($instance->id)) { |
1527 | $count++; |
1528 | } else { |
b0ccd3fb |
1529 | notify('Could not delete '. $modname .' instance '. $instance->id .' ('. $instance->name .')'); |
07aeb7b0 |
1530 | $result = false; |
1531 | } |
1532 | } |
1533 | } |
1534 | } else { |
b0ccd3fb |
1535 | notify('Function '. $moddelete() .'doesn\'t exist!'); |
07aeb7b0 |
1536 | $result = false; |
1537 | } |
1538 | |
ca952b03 |
1539 | if (function_exists($moddeletecourse)) { |
1540 | $moddeletecourse($course); |
1541 | } |
07aeb7b0 |
1542 | } |
1543 | if ($showfeedback) { |
b0ccd3fb |
1544 | notify($strdeleted .' '. $count .' x '. $modname); |
07aeb7b0 |
1545 | } |
1546 | } |
1547 | } else { |
b0ccd3fb |
1548 | error('No modules are installed!'); |
07aeb7b0 |
1549 | } |
1550 | |
1551 | // Delete any user stuff |
1552 | |
b0ccd3fb |
1553 | if (delete_records('user_students', 'course', $course->id)) { |
07aeb7b0 |
1554 | if ($showfeedback) { |
b0ccd3fb |
1555 | notify($strdeleted .' user_students'); |
07aeb7b0 |
1556 | } |
1557 | } else { |
1558 | $result = false; |
1559 | } |
1560 | |
b0ccd3fb |
1561 | if (delete_records('user_teachers', 'course', $course->id)) { |
07aeb7b0 |
1562 | if ($showfeedback) { |
b0ccd3fb |
1563 | notify($strdeleted .' user_teachers'); |
07aeb7b0 |
1564 | } |
1565 | } else { |
1566 | $result = false; |
1567 | } |
1568 | |
082e3ebc |
1569 | // Delete any groups |
1570 | |
b0ccd3fb |
1571 | if ($groups = get_records('groups', 'courseid', $course->id)) { |
082e3ebc |
1572 | foreach ($groups as $group) { |
b0ccd3fb |
1573 | if (delete_records('groups_members', 'groupid', $group->id)) { |
082e3ebc |
1574 | if ($showfeedback) { |
b0ccd3fb |
1575 | notify($strdeleted .' groups_members'); |
082e3ebc |
1576 | } |
1577 | } else { |
1578 | $result = false; |
1579 | } |
b0ccd3fb |
1580 | if (delete_records('groups', 'id', $group->id)) { |
082e3ebc |
1581 | if ($showfeedback) { |
b0ccd3fb |
1582 | notify($strdeleted .' groups'); |
082e3ebc |
1583 | } |
1584 | } else { |
1585 | $result = false; |
1586 | } |
1587 | } |
1588 | } |
1589 | |
1590 | // Delete events |
1591 | |
b0ccd3fb |
1592 | if (delete_records('event', 'courseid', $course->id)) { |
082e3ebc |
1593 | if ($showfeedback) { |
b0ccd3fb |
1594 | notify($strdeleted .' event'); |
082e3ebc |
1595 | } |
1596 | } else { |
1597 | $result = false; |
1598 | } |
1599 | |
07aeb7b0 |
1600 | // Delete logs |
1601 | |
b0ccd3fb |
1602 | if (delete_records('log', 'course', $course->id)) { |
07aeb7b0 |
1603 | if ($showfeedback) { |
b0ccd3fb |
1604 | notify($strdeleted .' log'); |
07aeb7b0 |
1605 | } |
1606 | } else { |
1607 | $result = false; |
1608 | } |
1609 | |
1610 | // Delete any course stuff |
1611 | |
b0ccd3fb |
1612 | if (delete_records('course_sections', 'course', $course->id)) { |
07aeb7b0 |
1613 | if ($showfeedback) { |
b0ccd3fb |
1614 | notify($strdeleted .' course_sections'); |
07aeb7b0 |
1615 | } |
1616 | } else { |
1617 | $result = false; |
1618 | } |
1619 | |
b0ccd3fb |
1620 | if (delete_records('course_modules', 'course', $course->id)) { |
07aeb7b0 |
1621 | if ($showfeedback) { |
b0ccd3fb |
1622 | notify($strdeleted .' course_modules'); |
07aeb7b0 |
1623 | } |
1624 | } else { |
1625 | $result = false; |
1626 | } |
1627 | |
1628 | return $result; |
1629 | |
1630 | } |
1631 | |
7cf1c7bd |
1632 | /** |
1633 | * This function will empty a course of USER data as much as |
1634 | /// possible. It will retain the activities and the structure |
1635 | /// of the course. |
1636 | * |
1637 | * @uses $USER |
1638 | * @uses $THEME |
1639 | * @uses $SESSION |
1640 | * @uses $CFG |
1641 | * @param integer $courseid The id of the course that is being viewed |
1642 | * @param boolean $showfeedback Set this to false to suppress notifications from being printed as the functions performs its steps. |
1643 | * @param boolean $removestudents ? |
1644 | * @param boolean $removeteachers ? |
1645 | * @param boolean $removegroups ? |
1646 | * @param boolean $removeevents ? |
1647 | * @param boolean $removelogs ? |
1648 | * @return boolean |
1649 | * @todo Finish documenting this function |
1650 | */ |
3831de52 |
1651 | function remove_course_userdata($courseid, $showfeedback=true, |
1652 | $removestudents=true, $removeteachers=false, $removegroups=true, |
1653 | $removeevents=true, $removelogs=false) { |
8f0cd6ef |
1654 | /// This function will empty a course of USER data as much as |
1655 | /// possible. It will retain the activities and the structure |
3831de52 |
1656 | /// of the course. |
1657 | |
1658 | global $CFG, $THEME, $USER, $SESSION; |
1659 | |
1660 | $result = true; |
1661 | |
b0ccd3fb |
1662 | if (! $course = get_record('course', 'id', $courseid)) { |
1663 | error('Course ID was incorrect (can\'t find it)'); |
3831de52 |
1664 | } |
1665 | |
b0ccd3fb |
1666 | $strdeleted = get_string('deleted'); |
3831de52 |
1667 | |
1668 | // Look in every instance of every module for data to delete |
1669 | |
b0ccd3fb |
1670 | if ($allmods = get_records('modules') ) { |
3831de52 |
1671 | foreach ($allmods as $mod) { |
1672 | $modname = $mod->name; |
b0ccd3fb |
1673 | $modfile = $CFG->dirroot .'/mod/'. $modname .'/lib.php'; |
1674 | $moddeleteuserdata = $modname .'_delete_userdata'; // Function to delete user data |
3831de52 |
1675 | $count=0; |
1676 | if (file_exists($modfile)) { |
1677 | @include_once($modfile); |
1678 | if (function_exists($moddeleteuserdata)) { |
1679 | $moddeleteuserdata($course, $showfeedback); |
1680 | } |
1681 | } |
1682 | } |
1683 | } else { |
b0ccd3fb |
1684 | error('No modules are installed!'); |
3831de52 |
1685 | } |
1686 | |
1687 | // Delete other stuff |
1688 | |
1689 | if ($removestudents) { |
1690 | /// Delete student enrolments |
b0ccd3fb |
1691 | if (delete_records('user_students', 'course', $course->id)) { |
3831de52 |
1692 | if ($showfeedback) { |
b0ccd3fb |
1693 | notify($strdeleted .' user_students'); |
3831de52 |
1694 | } |
1695 | } else { |
1696 | $result = false; |
1697 | } |
1698 | /// Delete group members (but keep the groups) |
b0ccd3fb |
1699 | if ($groups = get_records('groups', 'courseid', $course->id)) { |
3831de52 |
1700 | foreach ($groups as $group) { |
b0ccd3fb |
1701 | if (delete_records('groups_members', 'groupid', $group->id)) { |
3831de52 |
1702 | if ($showfeedback) { |
b0ccd3fb |
1703 | notify($strdeleted .' groups_members'); |
3831de52 |
1704 | } |
1705 | } else { |
1706 | $result = false; |
1707 | } |
1708 | } |
1709 | } |
1710 | } |
1711 | |
1712 | if ($removeteachers) { |
b0ccd3fb |
1713 | if (delete_records('user_teachers', 'course', $course->id)) { |
3831de52 |
1714 | if ($showfeedback) { |
b0ccd3fb |
1715 | notify($strdeleted .' user_teachers'); |
3831de52 |
1716 | } |
1717 | } else { |
1718 | $result = false; |
1719 | } |
1720 | } |
1721 | |
1722 | if ($removegroups) { |
b0ccd3fb |
1723 | if ($groups = get_records('groups', 'courseid', $course->id)) { |
3831de52 |
1724 | foreach ($groups as $group) { |
b0ccd3fb |
1725 | if (delete_records('groups', 'id', $group->id)) { |
3831de52 |
1726 | if ($showfeedback) { |
b0ccd3fb |
1727 | notify($strdeleted .' groups'); |
3831de52 |
1728 | } |
1729 | } else { |
1730 | $result = false; |
1731 | } |
1732 | } |
1733 | } |
1734 | } |
1735 | |
1736 | if ($removeevents) { |
b0ccd3fb |
1737 | if (delete_records('event', 'courseid', $course->id)) { |
3831de52 |
1738 | if ($showfeedback) { |
b0ccd3fb |
1739 | notify($strdeleted .' event'); |
3831de52 |
1740 | } |
1741 | } else { |
1742 | $result = false; |
1743 | } |
1744 | } |
1745 | |
1746 | if ($removelogs) { |
b0ccd3fb |
1747 | if (delete_records('log', 'course', $course->id)) { |
3831de52 |
1748 | if ($showfeedback) { |
b0ccd3fb |
1749 | notify($strdeleted .' log'); |
3831de52 |
1750 | } |
1751 | } else { |
1752 | $result = false; |
1753 | } |
1754 | } |
1755 | |
1756 | return $result; |
1757 | |
1758 | } |
1759 | |
1760 | |
f9903ed0 |
1761 | |
f374fb10 |
1762 | /// GROUPS ///////////////////////////////////////////////////////// |
d8ba183c |
1763 | |
f374fb10 |
1764 | |
1765 | /** |
1766 | * Returns a boolean: is the user a member of the given group? |
d8ba183c |
1767 | * |
dcd338ff |
1768 | * @param type description |
7cf1c7bd |
1769 | * @todo Finish documenting this function |
f374fb10 |
1770 | */ |
1771 | function ismember($groupid, $userid=0) { |
1772 | global $USER; |
1773 | |
8a2c9076 |
1774 | if (!$groupid) { // No point doing further checks |
1775 | return false; |
1776 | } |
1777 | |
f374fb10 |
1778 | if (!$userid) { |
0d67c514 |
1779 | if (empty($USER->groupmember)) { |
1780 | return false; |
1781 | } |
1782 | foreach ($USER->groupmember as $courseid => $mgroupid) { |
1783 | if ($mgroupid == $groupid) { |
1784 | return true; |
1785 | } |
1786 | } |
1787 | return false; |
f374fb10 |
1788 | } |
1789 | |
b0ccd3fb |
1790 | return record_exists('groups_members', 'groupid', $groupid, 'userid', $userid); |
f374fb10 |
1791 | } |
1792 | |
0d67c514 |
1793 | /** |
1794 | * Returns the group ID of the current user in the given course |
d8ba183c |
1795 | * |
dcd338ff |
1796 | * @param type description |
7cf1c7bd |
1797 | * @todo Finish documenting this function |
0d67c514 |
1798 | */ |
1799 | function mygroupid($courseid) { |
1800 | global $USER; |
1801 | |
1802 | if (empty($USER->groupmember[$courseid])) { |
1803 | return 0; |
1804 | } else { |
1805 | return $USER->groupmember[$courseid]; |
1806 | } |
1807 | } |
1808 | |
f374fb10 |
1809 | /** |
d8ba183c |
1810 | * For a given course, and possibly course module, determine |
f374fb10 |
1811 | * what the current default groupmode is: |
1812 | * NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS |
d8ba183c |
1813 | * |
dcd338ff |
1814 | * @param type description |
7cf1c7bd |
1815 | * @todo Finish documenting this function |
f374fb10 |
1816 | */ |
1817 | function groupmode($course, $cm=null) { |
1818 | |
1819 | if ($cm and !$course->groupmodeforce) { |
1820 | return $cm->groupmode; |
1821 | } |
1822 | return $course->groupmode; |
1823 | } |
1824 | |
1825 | |
1826 | /** |
1827 | * Sets the current group in the session variable |
d8ba183c |
1828 | * |
dcd338ff |
1829 | * @param type description |
7cf1c7bd |
1830 | * @todo Finish documenting this function |
f374fb10 |
1831 | */ |
1832 | function set_current_group($courseid, $groupid) { |
1833 | global $SESSION; |
1834 | |
1835 | return $SESSION->currentgroup[$courseid] = $groupid; |
1836 | } |
1837 | |
1838 | |
1839 | /** |
1840 | * Gets the current group for the current user as an id or an object |
d8ba183c |
1841 | * |
dcd338ff |
1842 | * @param type description |
7cf1c7bd |
1843 | * @todo Finish documenting this function |
f374fb10 |
1844 | */ |
1845 | function get_current_group($courseid, $full=false) { |
1846 | global $SESSION, $USER; |
1847 | |
ce04df6b |
1848 | if (!isset($SESSION->currentgroup[$courseid])) { |
f374fb10 |
1849 | if (empty($USER->groupmember[$courseid])) { |
8a2c9076 |
1850 | return 0; |
f374fb10 |
1851 | } else { |
1852 | $SESSION->currentgroup[$courseid] = $USER->groupmember[$courseid]; |
1853 | } |
1854 | } |
1855 | |
1856 | if ($full) { |
0da33e07 |
1857 | return get_record('groups', 'id', $SESSION->currentgroup[$courseid]); |
f374fb10 |
1858 | } else { |
1859 | return $SESSION->currentgroup[$courseid]; |
1860 | } |
1861 | } |
1862 | |
0d67c514 |
1863 | /** |
1864 | * A combination function to make it easier for modules |
1865 | * to set up groups. |
1866 | * |
1867 | * It will use a given "groupid" parameter and try to use |
1868 | * that to reset the current group for the user. |
1869 | * |
dcd338ff |
1870 | * @param type description |
7cf1c7bd |
1871 | * @todo Finish documenting this function |
0d67c514 |
1872 | */ |
eb6147a8 |
1873 | function get_and_set_current_group($course, $groupmode, $groupid=-1) { |
0d67c514 |
1874 | |
1875 | if (!$groupmode) { // Groups don't even apply |
d8ba183c |
1876 | return false; |
0d67c514 |
1877 | } |
1878 | |
1879 | $currentgroupid = get_current_group($course->id); |
1880 | |
eb6147a8 |
1881 | if ($groupid < 0) { // No change was specified |
1882 | return $currentgroupid; |
1883 | } |
1884 | |
1885 | if ($groupid) { // Try to change the current group to this groupid |
0d67c514 |
1886 | if ($group = get_record('groups', 'id', $groupid, 'courseid', $course->id)) { // Exists |
1887 | if (isteacheredit($course->id)) { // Sets current default group |
1888 | $currentgroupid = set_current_group($course->id, $group->id); |
1889 | |
1890 | } else if ($groupmode == VISIBLEGROUPS) { // All groups are visible |
1891 | $currentgroupid = $group->id; |
1892 | } |
1893 | } |
eb6147a8 |
1894 | } else { // When groupid = 0 it means show ALL groups |
1895 | if (isteacheredit($course->id)) { // Sets current default group |
1896 | $currentgroupid = set_current_group($course->id, 0); |
1897 | |
1898 | } else if ($groupmode == VISIBLEGROUPS) { // All groups are visible |
1899 | $currentgroupid = 0; |
1900 | } |
0d67c514 |
1901 | } |
1902 | |
1903 | return $currentgroupid; |
1904 | } |
1905 | |
1906 | |
c3cbfe7f |
1907 | /** |
1908 | * A big combination function to make it easier for modules |
1909 | * to set up groups. |
1910 | * |
1911 | * Terminates if the current user shouldn't be looking at this group |
1912 | * Otherwise returns the current group if there is one |
1913 | * Otherwise returns false if groups aren't relevant |
1914 | * |
dcd338ff |
1915 | * @param type description |
7cf1c7bd |
1916 | * @todo Finish documenting this function |
c3cbfe7f |
1917 | */ |
1918 | function setup_and_print_groups($course, $groupmode, $urlroot) { |
1919 | |
eb6147a8 |
1920 | if (isset($_GET['group'])) { |
1921 | $changegroup = $_GET['group']; /// 0 or higher |
1922 | } else { |
1923 | $changegroup = -1; /// This means no group change was specified |
1924 | } |
1925 | |
1926 | $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup); |
c3cbfe7f |
1927 | |
eb6147a8 |
1928 | if ($currentgroup === false) { |
c3cbfe7f |
1929 | return false; |
1930 | } |
1931 | |
4b6d8dd5 |
1932 | if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id) and !$currentgroup) { |
1933 | print_heading(get_string('notingroup')); |
c3cbfe7f |
1934 | print_footer($course); |
1935 | exit; |
1936 | } |
1937 | |
1938 | if ($groupmode == VISIBLEGROUPS or ($groupmode and isteacheredit($course->id))) { |
b0ccd3fb |
1939 | if ($groups = get_records_menu('groups', 'courseid', $course->id, 'name ASC', 'id,name')) { |
eb6147a8 |
1940 | echo '<div align="center">'; |
c3cbfe7f |
1941 | print_group_menu($groups, $groupmode, $currentgroup, $urlroot); |
eb6147a8 |
1942 | echo '</div>'; |
c3cbfe7f |
1943 | } |
1944 | } |
1945 | |
1946 | return $currentgroup; |
1947 | } |
0d67c514 |
1948 | |
f374fb10 |
1949 | |
1950 | |
f9903ed0 |
1951 | /// CORRESPONDENCE //////////////////////////////////////////////// |
1952 | |
7cf1c7bd |
1953 | /** |
1954 | * Send an email to a specified user |
1955 | * |
1956 | * Returns "true" if mail was sent OK, "emailstop" if email was blocked by user |
1957 | * and "false" if there was another sort of error. |
1958 | * |
1959 | * @uses $CFG |
1960 | * @uses $_SERVER |
1961 | * @param object $user a user record as an object |
1962 | * @param object $from a user record as an object |
1963 | * @param string $subject plain text subject line of the email |
1964 | * @param string $messagetext plain text version of the message |
1965 | * @param string $messagehtml complete html version of the message (optional) |
1966 | * @param string $attachment a file on the filesystem, relative to $CFG->dataroot |
1967 | * @param string $attachname the name of the file (extension indicates MIME) |
1968 | * @param boolean $usetrueaddress determines whether $from email address should be sent out. Will be overruled by user profile setting for maildisplay |
1969 | * @return boolean |
1970 | */ |
b0ccd3fb |
1971 | function email_to_user($user, $from, $subject, $messagetext, $messagehtml='', $attachment='', $attachname='', $usetrueaddress=true) { |
9fa49e22 |
1972 | /// user - a user record as an object |
1973 | /// from - a user record as an object |
1974 | /// subject - plain text subject line of the email |
1975 | /// messagetext - plain text version of the message |
1976 | /// messagehtml - complete html version of the message (optional) |
1977 | /// attachment - a file on the filesystem, relative to $CFG->dataroot |
1978 | /// attachname - the name of the file (extension indicates MIME) |
a402bdcb |
1979 | /// usetrueaddress - determines whether $from email address should be sent out. |
6e506bf9 |
1980 | /// Will be overruled by user profile setting for maildisplay |
579dcca4 |
1981 | /// |
1982 | /// Returns "true" if mail was sent OK, "emailstop" if email was blocked by user |
1983 | /// and "false" if there was another sort of error. |
f9903ed0 |
1984 | |
4216daa6 |
1985 | global $CFG, $_SERVER; |
f9903ed0 |
1986 | |
0cc6fa6a |
1987 | global $course; // This is a bit of an ugly hack to be gotten rid of later |
1988 | if (!empty($course->lang)) { // Course language is defined |
1989 | $CFG->courselang = $course->lang; |
1990 | } |
1991 | |
b0ccd3fb |
1992 | include_once($CFG->libdir .'/phpmailer/class.phpmailer.php'); |
f9903ed0 |
1993 | |
cadb96f2 |
1994 | if (empty($user)) { |
1995 | return false; |
1996 | } |
1997 | |
1998 | if (!empty($user->emailstop)) { |
579dcca4 |
1999 | return 'emailstop'; |
f9903ed0 |
2000 | } |
d8ba183c |
2001 | |
f9903ed0 |
2002 | $mail = new phpmailer; |
2003 | |
b0ccd3fb |
2004 | $mail->Version = 'Moodle '. $CFG->version; // mailer version |
2005 | $mail->PluginDir = $CFG->libdir .'/phpmailer/'; // plugin directory (eg smtp plugin) |
562bbe90 |
2006 | |
98c4eae3 |
2007 | |
b0ccd3fb |
2008 | if (current_language() != 'en') { |
2009 | $mail->CharSet = get_string('thischarset'); |
98c4eae3 |
2010 | } |
2011 | |
b0ccd3fb |
2012 | if ($CFG->smtphosts == 'qmail') { |
62740736 |
2013 | $mail->IsQmail(); // use Qmail system |
2014 | |
2015 | } else if (empty($CFG->smtphosts)) { |
2016 | $mail->IsMail(); // use PHP mail() = sendmail |
2017 | |
2018 | } else { |
1e411ffc |
2019 | $mail->IsSMTP(); // use SMTP directly |
57ef3480 |
2020 | if ($CFG->debug > 7) { |
b0ccd3fb |
2021 | echo '<pre>' . "\n"; |
57ef3480 |
2022 | $mail->SMTPDebug = true; |
2023 | } |
b0ccd3fb |
2024 | $mail->Host = $CFG->smtphosts; // specify main and backup servers |
9f58537a |
2025 | |
2026 | if ($CFG->smtpuser) { // Use SMTP authentication |
2027 | $mail->SMTPAuth = true; |
2028 | $mail->Username = $CFG->smtpuser; |
2029 | $mail->Password = $CFG->smtppass; |
2030 | } |
7f86ce17 |
2031 | } |
f9903ed0 |
2032 | |
2b97bd71 |
2033 | $adminuser = get_admin(); |
2034 | |
b0ccd3fb |
2035 | $mail->Sender = $adminuser->email; |
2b97bd71 |
2036 | |
a402bdcb |
2037 | if (is_string($from)) { // So we can pass whatever we want if there is need |
2038 | $mail->From = $CFG->noreplyaddress; |
0d8a590a |
2039 | $mail->FromName = $from; |
a402bdcb |
2040 | } else if ($usetrueaddress and $from->maildisplay) { |
b0ccd3fb |
2041 | $mail->From = $from->email; |
6e506bf9 |
2042 | $mail->FromName = fullname($from); |
2043 | } else { |
b0ccd3fb |
2044 | $mail->From = $CFG->noreplyaddress; |
0d8a590a |
2045 | $mail->FromName = fullname($from); |
6e506bf9 |
2046 | } |
136dabd8 |
2047 | $mail->Subject = stripslashes($subject); |
f9903ed0 |
2048 | |
b0ccd3fb |
2049 | $mail->AddAddress($user->email, fullname($user) ); |
f9903ed0 |
2050 | |
58d24720 |
2051 | $mail->WordWrap = 79; // set word wrap |
f9903ed0 |
2052 | |
857b798b |
2053 | if (!empty($from->customheaders)) { // Add custom headers |
2054 | if (is_array($from->customheaders)) { |
2055 | foreach ($from->customheaders as $customheader) { |
2056 | $mail->AddCustomHeader($customheader); |
2057 | } |
2058 | } else { |
2059 | $mail->AddCustomHeader($from->customheaders); |
2060 | } |
b68dca19 |
2061 | } |
8f0cd6ef |
2062 | |
136dabd8 |
2063 | if ($messagehtml) { |
2064 | $mail->IsHTML(true); |
b0ccd3fb |
2065 | $mail->Encoding = 'quoted-printable'; // Encoding to use |
136dabd8 |
2066 | $mail->Body = $messagehtml; |
78681899 |
2067 | $mail->AltBody = "\n$messagetext\n"; |
136dabd8 |
2068 | } else { |
2069 | $mail->IsHTML(false); |
78681899 |
2070 | $mail->Body = "\n$messagetext\n"; |
f9903ed0 |
2071 | } |
2072 | |
136dabd8 |
2073 | if ($attachment && $attachname) { |
2074 | if (ereg( "\\.\\." ,$attachment )) { // Security check for ".." in dir path |
b0ccd3fb |
2075 | $mail->AddAddress($adminuser->email, fullname($adminuser) ); |
2076 | $mail->AddStringAttachment('Error in attachment. User attempted to attach a filename with a unsafe name.', 'error.txt', '8bit', 'text/plain'); |
136dabd8 |
2077 | } else { |
b0ccd3fb |
2078 | include_once($CFG->dirroot .'/files/mimetypes.php'); |
2079 | $mimetype = mimeinfo('type', $attachname); |
2080 | $mail->AddAttachment($CFG->dataroot .'/'. $attachment, $attachname, 'base64', $mimetype); |
136dabd8 |
2081 | } |
f9903ed0 |
2082 | } |
2083 | |
136dabd8 |
2084 | if ($mail->Send()) { |
2085 | return true; |
2086 | } else { |
b0ccd3fb |
2087 | mtrace('ERROR: '. $mail->ErrorInfo); |
2088 | add_to_log(SITEID, 'library', 'mailer', $_SERVER['REQUEST_URI'], 'ERROR: '. $mail->ErrorInfo); |
f9903ed0 |
2089 | return false; |
2090 | } |
f9903ed0 |
2091 | } |
2092 | |
7cf1c7bd |
2093 | /** |
2094 | * Resets specified user's password and send the new password to the user via email. |
2095 | * |
2096 | * @uses $CFG |
2097 | * @param object $user |
2098 | * @return boolean |
2099 | */ |
1d881d92 |
2100 | function reset_password_and_mail($user) { |
2101 | |
2102 | global $CFG; |
2103 | |
2104 | $site = get_site(); |
2105 | $from = get_admin(); |
2106 | |
2107 | $newpassword = generate_password(); |
2108 | |
b0ccd3fb |
2109 | if (! set_field('user', 'password', md5($newpassword), 'id', $user->id) ) { |
2110 | error('Could not set user password!'); |
1d881d92 |
2111 | } |
2112 | |
2113 | $a->firstname = $user->firstname; |
2114 | $a->sitename = $site->fullname; |
2115 | $a->username = $user->username; |
2116 | $a->newpassword = $newpassword; |
b0ccd3fb |
2117 | $a->link = $CFG->wwwroot .'/login/change_password.php'; |
2118 | $a->signoff = fullname($from, true).' ('. $from->email .')'; |
1d881d92 |
2119 | |
b0ccd3fb |
2120 | $message = get_string('newpasswordtext', '', $a); |
1d881d92 |
2121 | |
b0ccd3fb |
2122 | $subject = $site->fullname .': '. get_string('changedpassword'); |
1d881d92 |
2123 | |
2124 | return email_to_user($user, $from, $subject, $message); |
2125 | |
2126 | } |
2127 | |
7cf1c7bd |
2128 | /** |
2129 | * Send email to specified user with confirmation text and activation link. |
2130 | * |
2131 | * @uses $CFG |
2132 | * @param object $user |
2133 | * @return boolean |
2134 | */ |
2135 | function send_confirmation_email($user) { |
1d881d92 |
2136 | |
2137 | global $CFG; |
2138 | |
2139 | $site = get_site(); |
2140 | $from = get_admin(); |
2141 | |
2142 | $data->firstname = $user->firstname; |
2143 | $data->sitename = $site->fullname; |
b0ccd3fb |
2144 | $data->link = $CFG->wwwroot .'/login/confirm.php?p='. $user->secret .'&s='. $user->username; |
2145 | $data->admin = fullname($from) .' ('. $from->email .')'; |
1d881d92 |
2146 | |
b0ccd3fb |
2147 | $message = get_string('emailconfirmation', '', $data); |
2148 | $subject = get_string('emailconfirmationsubject', '', $site->fullname); |
1d881d92 |
2149 | |
58d24720 |
2150 | $messagehtml = text_to_html($message, false, false, true); |
2151 | |
2152 | return email_to_user($user, $from, $subject, $message, $messagehtml); |
1d881d92 |
2153 | |
2154 | } |
2155 | |
7cf1c7bd |
2156 | /** |
2157 | * send_password_change_confirmation_email. |
2158 | * |
2159 | * @param type description |
2160 | * @todo Finish documenting this function |
2161 | */ |
eb347b6b |
2162 | function send_password_change_confirmation_email($user) { |
2163 | |
2164 | global $CFG; |
2165 | |
2166 | $site = get_site(); |
2167 | $from = get_admin(); |
2168 | |
2169 | $data->firstname = $user->firstname; |
2170 | $data->sitename = $site->fullname; |
b0ccd3fb |
2171 | $data->link = $CFG->wwwroot .'/login/forgot_password.php?p='. $user->secret .'&s='. $user->username; |
2172 | $data->admin = fullname($from).' ('. $from->email .')'; |
eb347b6b |
2173 | |
b0ccd3fb |
2174 | $message = get_string('emailpasswordconfirmation', '', $data); |
2175 | $subject = get_string('emailpasswordconfirmationsubject', '', $site->fullname); |
eb347b6b |
2176 | |
2177 | return email_to_user($user, $from, $subject, $message); |
2178 | |
2179 | } |
2180 | |
7cf1c7bd |
2181 | /** |
2182 | * Check that an email is allowed. It returns an error message if there |
2183 | * was a problem. |
2184 | * |
2185 | * @param type description |
2186 | * @todo Finish documenting this function |
2187 | */ |
85a1d4c9 |
2188 | function email_is_not_allowed($email) { |
8f0cd6ef |
2189 | /// Check that an email is allowed. It returns an error message if there |
85a1d4c9 |
2190 | /// was a problem. |
2191 | |
2192 | global $CFG; |
2193 | |
2194 | if (!empty($CFG->allowemailaddresses)) { |
2195 | $allowed = explode(' ', $CFG->allowemailaddresses); |
2196 | foreach ($allowed as $allowedpattern) { |
2197 | $allowedpattern = trim($allowedpattern); |
2198 | if (!$allowedpattern) { |
2199 | continue; |
2200 | } |
2201 | if (strpos($email, $allowedpattern) !== false) { // Match! |
2202 | return false; |
2203 | } |
2204 | } |
b0ccd3fb |
2205 | return get_string('emailonlyallowed', '', $CFG->allowemailaddresses); |
85a1d4c9 |
2206 | |
2207 | } else if (!empty($CFG->denyemailaddresses)) { |
2208 | $denied = explode(' ', $CFG->denyemailaddresses); |
2209 | foreach ($denied as $deniedpattern) { |
2210 | $deniedpattern = trim($deniedpattern); |
2211 | if (!$deniedpattern) { |
2212 | continue; |
2213 | } |
2214 | if (strpos($email, $deniedpattern) !== false) { // Match! |
b0ccd3fb |
2215 | return get_string('emailnotallowed', '', $CFG->denyemailaddresses); |
85a1d4c9 |
2216 | } |
2217 | } |
2218 | } |
2219 | |
2220 | return false; |
2221 | } |
1d881d92 |
2222 | |
136dabd8 |
2223 | |
f9903ed0 |
2224 | /// FILE HANDLING ///////////////////////////////////////////// |
2225 | |
7cf1c7bd |
2226 | /** |
2227 | * Create a directory. |
2228 | * |
2229 | * @uses $CFG |
2230 | * @param string $directory a string of directory names under $CFG->dataroot |
2231 | * @return object |
2232 | * @todo Finish documenting this function |
2233 | */ |
66f9a82c |
2234 | function make_upload_directory($directory, $shownotices=true) { |
9fa49e22 |
2235 | /// $directory = a string of directory names under $CFG->dataroot |
2236 | /// eg stuff/assignment/1 |
2237 | /// Returns full directory if successful, false if not |
6b174680 |
2238 | |
2239 | global $CFG; |
2240 | |
2241 | $currdir = $CFG->dataroot; |
fe287429 |
2242 | |
2e6d4273 |
2243 | umask(0000); |
2244 | |
6b174680 |
2245 | if (!file_exists($currdir)) { |
2e6d4273 |
2246 | if (! mkdir($currdir, $CFG->directorypermissions)) { |
66f9a82c |
2247 | if ($shownotices) { |
b0ccd3fb |
2248 | notify('ERROR: You need to create the directory '. $currdir .' with web server write access'); |
66f9a82c |
2249 | } |
6b174680 |
2250 | return false; |
2251 | } |
2252 | } |
2253 | |
b0ccd3fb |
2254 | $dirarray = explode('/', $directory); |
6b174680 |
2255 | |
2256 | foreach ($dirarray as $dir) { |
b0ccd3fb |
2257 | $currdir = $currdir .'/'. $dir; |
6b174680 |
2258 | if (! file_exists($currdir)) { |
2e6d4273 |
2259 | if (! mkdir($currdir, $CFG->directorypermissions)) { |
66f9a82c |
2260 | if ($shownotices) { |
b0ccd3fb |
2261 | notify('ERROR: Could not find or create a directory ('. $currdir .')'); |
66f9a82c |
2262 | } |
6b174680 |
2263 | return false; |
2264 | } |
feffa4e6 |
2265 | @chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it |
6b174680 |
2266 | } |
2267 | } |
2268 | |
2269 | return $currdir; |
2270 | } |
1e03c552 |
2271 | |
7cf1c7bd |
2272 | /** |
2273 | * Makes an upload directory for a particular module. |
2274 | * |
2275 | * @uses $CFG |
2276 | * @param integer $courseid ? |
2277 | * @todo Finish documenting this function |
2278 | */ |
ca4f8eb8 |
2279 | function make_mod_upload_directory($courseid) { |
9fa49e22 |
2280 | /// Makes an upload directory for a particular module |
ca4f8eb8 |
2281 | global $CFG; |
2282 | |
b0ccd3fb |
2283 | if (! $moddata = make_upload_directory($courseid .'/'. $CFG->moddata)) { |
ca4f8eb8 |
2284 | return false; |
2285 | } |
2286 | |
b0ccd3fb |
2287 | $strreadme = get_string('readme'); |
ca4f8eb8 |
2288 | |
b0ccd3fb |
2289 | if (file_exists($CFG->dirroot .'/lang/'. $CFG->lang .'/docs/module_files.txt')) { |
2290 | copy($CFG->dirroot .'/lang/'. $CFG->lang .'/docs/module_files.txt', $moddata .'/'. $strreadme .'.txt'); |
ca4f8eb8 |
2291 | } else { |
b0ccd3fb |
2292 | copy($CFG->dirroot .'/lang/en/docs/module_files.txt', $moddata .'/'. $strreadme .'.txt'); |
ca4f8eb8 |
2293 | } |
2294 | return $moddata; |
2295 | } |
2296 | |
7cf1c7bd |
2297 | /** |
2298 | * Returns current name of file on disk if true. |
2299 | * |
2300 | * @param string $newfile ? |
2301 | * @todo Finish documenting this function |
2302 | */ |
44e2d2bb |
2303 | function valid_uploaded_file($newfile) { |
9fa49e22 |
2304 | /// Returns current name of file on disk if true |
9c9f7d77 |
2305 | if (empty($newfile)) { |
b0ccd3fb |
2306 | return ''; |
9c9f7d77 |
2307 | } |
44e2d2bb |
2308 | if (is_uploaded_file($newfile['tmp_name']) and $newfile['size'] > 0) { |
2309 | return $newfile['tmp_name']; |
2310 | } else { |
b0ccd3fb |
2311 | return ''; |
44e2d2bb |
2312 | } |
2313 | } |
2314 | |
7cf1c7bd |
2315 | /** |
2316 | * Returns the maximum size for uploading files. |
2317 | * |
2318 | * There are seven possible upload limits: |
2319 | * 1. in Apache using LimitRequestBody (no way of checking or changing this) |
2320 | * 2. in php.ini for 'upload_max_filesize' (can not be changed inside PHP) |
2321 | * 3. in .htaccess for 'upload_max_filesize' (can not be changed inside PHP) |
2322 | * 4. in php.ini for 'post_max_size' (can not be changed inside PHP) |
2323 | * 5. by the Moodle admin in $CFG->maxbytes |
2324 | * 6. by the teacher in the current course $course->maxbytes |
2325 | * 7. by the teacher for the current module, eg $assignment->maxbytes |
2326 | * |
2327 | * These last two are passed to this function as arguments (in bytes). |
2328 | * Anything defined as 0 is ignored. |
2329 | * The smallest of all the non-zero numbers is returned. |
2330 | * |
2331 | * @param type description |
2332 | * @todo Finish documenting this function |
2333 | */ |
4909e176 |
2334 | function get_max_upload_file_size($sitebytes=0, $coursebytes=0, $modulebytes=0) { |
9fa49e22 |
2335 | /// Returns the maximum size for uploading files |
316ebf78 |
2336 | /// There are seven possible upload limits: |
4909e176 |
2337 | /// |
2338 | /// 1) in Apache using LimitRequestBody (no way of checking or changing this) |
2339 | /// 2) in php.ini for 'upload_max_filesize' (can not be changed inside PHP) |
2340 | /// 3) in .htaccess for 'upload_max_filesize' (can not be changed inside PHP) |
316ebf78 |
2341 | /// 4) in php.ini for 'post_max_size' (can not be changed inside PHP) |
2342 | /// 5) by the Moodle admin in $CFG->maxbytes |
2343 | /// 6) by the teacher in the current course $course->maxbytes |
2344 | /// 7) by the teacher for the current module, eg $assignment->maxbytes |
4909e176 |
2345 | /// |
2346 | /// These last two are passed to this function as arguments (in bytes). |
2347 | /// Anything defined as 0 is ignored. |
2348 | /// The smallest of all the non-zero numbers is returned. |
2349 | |
b0ccd3fb |
2350 | if (! $filesize = ini_get('upload_max_filesize')) { |
2351 | $filesize = '5M'; |
44e2d2bb |
2352 | } |
4909e176 |
2353 | $minimumsize = get_real_size($filesize); |
2354 | |
b0ccd3fb |
2355 | if ($postsize = ini_get('post_max_size')) { |
316ebf78 |
2356 | $postsize = get_real_size($postsize); |
2357 | if ($postsize < $minimumsize) { |
2358 | $minimumsize = $postsize; |
2359 | } |
2360 | } |
2361 | |
4909e176 |
2362 | if ($sitebytes and $sitebytes < $minimumsize) { |
2363 | $minimumsize = $sitebytes; |
2364 | } |
2365 | |
2366 | if ($coursebytes and $coursebytes < $minimumsize) { |
2367 | $minimumsize = $coursebytes; |
2368 | } |
2369 | |
2370 | if ($modulebytes and $modulebytes < $minimumsize) { |
2371 | $minimumsize = $modulebytes; |
2372 | } |
2373 | |
2374 | return $minimumsize; |
2375 | } |
2376 | |
7cf1c7bd |
2377 | /** |
2378 | * Related to the above function - this function returns an |
2379 | * array of possible sizes in an array, translated to the |
2380 | * local language. |
2381 | * |
2382 | * @param integer $sizebytes ? |
2383 | * @param integer $coursebytes ? |
2384 | * @param integer $modulebytes |
2385 | * @return integer |
2386 | * @todo Finish documenting this function |
2387 | */ |
4909e176 |
2388 | function get_max_upload_sizes($sitebytes=0, $coursebytes=0, $modulebytes=0) { |
d8ba183c |
2389 | /// Related to the above function - this function returns an |
2390 | /// array of possible sizes in an array, translated to the |
4909e176 |
2391 | /// local language. |
2392 | |
2393 | if (!$maxsize = get_max_upload_file_size($sitebytes, $coursebytes, $modulebytes)) { |
2394 | return array(); |
2395 | } |
2396 | |
2397 | $filesize[$maxsize] = display_size($maxsize); |
2398 | |
d8ba183c |
2399 | $sizelist = array(10240, 51200, 102400, 512000, 1048576, 2097152, |
4909e176 |
2400 | 5242880, 10485760, 20971520, 52428800, 104857600); |
2401 | |
2402 | foreach ($sizelist as $sizebytes) { |
2403 | if ($sizebytes < $maxsize) { |
2404 | $filesize[$sizebytes] = display_size($sizebytes); |
2405 | } |
2406 | } |
2407 | |
2408 | krsort($filesize, SORT_NUMERIC); |
2409 | |
2410 | return $filesize; |
44e2d2bb |
2411 | } |
2412 | |
7cf1c7bd |
2413 | /** |
2414 | * If there has been an error uploading a file, print the appropriate error message |
2415 | * Numerical constants used as constant definitions not added until PHP version 4.2.0 |
2416 | * |
2417 | * filearray is a 1-dimensional sub-array of the $_FILES array |
2418 | * eg $filearray = $_FILES['userfile1'] |
2419 | * If left empty then the first element of the $_FILES array will be used * |
2420 | * @param array $filearray ? |
2421 | * @param boolean $returnerror |
2422 | * @return boolean |
2423 | * @todo Finish documenting this function |
2424 | */ |
ebd52396 |
2425 | function print_file_upload_error($filearray = '', $returnerror = false) { |
2426 | /// If there has been an error uploading a file, print the appropriate error message |
2427 | /// Numerical constants used as constant definitions not added until PHP version 4.2.0 |
2428 | /// |
2429 | /// filearray is a 1-dimensional sub-array of the $_FILES array |
2430 | /// eg $filearray = $_FILES['userfile1'] |
2431 | /// If left empty then the first element of the $_FILES array will be used |
2432 | |
2433 | if ($filearray == '' or !isset($filearray['error'])) { |
2434 | |
2435 | if (empty($_FILES)) return false; |
2436 | |
2437 | $files = $_FILES; /// so we don't mess up the _FILES array for subsequent code |
2438 | $filearray = array_shift($files); /// use first element of array |
2439 | } |
2440 | |
2441 | switch ($filearray['error']) { |
2442 | |
2443 | case 0: // UPLOAD_ERR_OK |
2444 | if ($filearray['size'] > 0) { |
2445 | $errmessage = get_string('uploadproblem', $filearray['name']); |
2446 | } else { |
2447 | $errmessage = get_string('uploadnofilefound'); /// probably a dud file name |
2448 | } |
2449 | break; |
2450 | |
2451 | case 1: // UPLOAD_ERR_INI_SIZE |
2452 | $errmessage = get_string('uploadserverlimit'); |
2453 | break; |
2454 | |
2455 | case 2: // UPLOAD_ERR_FORM_SIZE |
2456 | $errmessage = get_string('uploadformlimit'); |
2457 | break; |
2458 | |
2459 | case 3: // UPLOAD_ERR_PARTIAL |
2460 | $errmessage = get_string('uploadpartialfile'); |
2461 | break; |
2462 | |
2463 | case 4: // UPLOAD_ERR_NO_FILE |
2464 | $errmessage = get_string('uploadnofilefound'); |
2465 | break; |
2466 | |
2467 | default: |
2468 | $errmessage = get_string('uploadproblem', $filearray['name']); |
2469 | } |
2470 | |
2471 | if ($returnerror) { |
2472 | return $errmessage; |
2473 | } else { |
2474 | notify($errmessage); |
2475 | return true; |
2476 | } |
2477 | |
2478 | } |
2479 | |
7cf1c7bd |
2480 | /** |
2481 | * Returns an array with all the filenames in |
2482 | * all subdirectories, relative to the given rootdir. |
2483 | * If excludefile is defined, then that file/directory is ignored |
2484 | * If getdirs is true, then (sub)directories are included in the output |
2485 | * If getfiles is true, then files are included in the output |
2486 | * (at least one of these must be true!) |
2487 | * |
2488 | * @param type name definition |
2489 | * @return array |
2490 | * @todo Finish documenting this function |
2491 | */ |
b0ccd3fb |
2492 | function get_directory_list($rootdir, $excludefile='', $descend=true, $getdirs=false, $getfiles=true) { |
d8ba183c |
2493 | /// Returns an array with all the filenames in |
9fa49e22 |
2494 | /// all subdirectories, relative to the given rootdir. |
2495 | /// If excludefile is defined, then that file/directory is ignored |
16a5602c |
2496 | /// If getdirs is true, then (sub)directories are included in the output |
2497 | /// If getfiles is true, then files are included in the output |
2498 | /// (at least one of these must be true!) |
f9903ed0 |
2499 | |
2500 | $dirs = array(); |
f9903ed0 |
2501 | |
16a5602c |
2502 | if (!$getdirs and !$getfiles) { // Nothing to show |
12407705 |
2503 | return $dirs; |
2504 | } |
2505 | |
16a5602c |
2506 | if (!is_dir($rootdir)) { // Must be a directory |
2507 | return $dirs; |
2508 | } |
2509 | |
2510 | if (!$dir = opendir($rootdir)) { // Can't open it for some reason |
d897cae4 |
2511 | return $dirs; |
2512 | } |
2513 | |
81fcd0f0 |
2514 | while (false !== ($file = readdir($dir))) { |
b35e8568 |
2515 | $firstchar = substr($file, 0, 1); |
b0ccd3fb |
2516 | if ($firstchar == '.' or $file == 'CVS' or $file == $excludefile) { |
b35e8568 |
2517 | continue; |
2518 | } |
b0ccd3fb |
2519 | $fullfile = $rootdir .'/'. $file; |
2520 | if (filetype($fullfile) == 'dir') { |
16a5602c |
2521 | if ($getdirs) { |
55fd8177 |
2522 | $dirs[] = $file; |
2523 | } |
bf5c2e84 |
2524 | if ($descend) { |
16a5602c |
2525 | $subdirs = get_directory_list($fullfile, $excludefile, $descend, $getdirs, $getfiles); |
bf5c2e84 |
2526 | foreach ($subdirs as $subdir) { |
b0ccd3fb |
2527 | $dirs[] = $file .'/'. $subdir; |
bf5c2e84 |
2528 | } |
f9903ed0 |
2529 | } |
16a5602c |
2530 | } else if ($getfiles) { |
b35e8568 |
2531 | $dirs[] = $file; |
f9903ed0 |
2532 | } |
2533 | } |
44e2d2bb |
2534 | closedir($dir); |
f9903ed0 |
2535 | |
774ab660 |
2536 | asort($dirs); |
2537 | |
f9903ed0 |
2538 | return $dirs; |
2539 | } |
2540 | |
7cf1c7bd |
2541 | /** |
2542 | * Adds up all the files in a directory and works out the size. |
2543 | * |
2544 | * @param string $rootdir ? |
2545 | * @param string $excludefile ? |
2546 | * @return object |
2547 | * @todo Finish documenting this function |
2548 | */ |
b0ccd3fb |
2549 | function get_directory_size($rootdir, $excludefile='') { |
16a5602c |
2550 | /// Adds up all the files in a directory and works out the size |
2551 | |
2552 | $size = 0; |
2553 | |
2554 | if (!is_dir($rootdir)) { // Must be a directory |
2555 | return $dirs; |
2556 | } |
2557 | |
b5b90f26 |
2558 | if (!$dir = @opendir($rootdir)) { // Can't open it for some reason |
16a5602c |
2559 | return $dirs; |
2560 | } |
2561 | |
2562 | while (false !== ($file = readdir($dir))) { |
2563 | $firstchar = substr($file, 0, 1); |
b0ccd3fb |
2564 | if ($firstchar == '.' or $file == 'CVS' or $file == $excludefile) { |
16a5602c |
2565 | continue; |
2566 | } |
b0ccd3fb |
2567 | $fullfile = $rootdir .'/'. $file; |
2568 | if (filetype($fullfile) == 'dir') { |
16a5602c |
2569 | $size += get_directory_size($fullfile, $excludefile); |
2570 | } else { |
2571 | $size += filesize($fullfile); |
2572 | } |
2573 | } |
2574 | closedir($dir); |
2575 | |
2576 | return $size; |
2577 | } |
2578 | |
7cf1c7bd |
2579 | /** |
2580 | * Converts numbers like 10M into bytes. |
2581 | * |
2582 | * @uses $CFG |
2583 | * @param mixed $size The size to be converted |
2584 | * @return mixed |
2585 | */ |
989bfa9d |
2586 | function get_real_size($size=0) { |
9fa49e22 |
2587 | /// Converts numbers like 10M into bytes |
989bfa9d |
2588 | if (!$size) { |
d8ba183c |
2589 | return 0; |
989bfa9d |
2590 | } |
2591 | $scan['MB'] = 1048576; |
64efda84 |
2592 | $scan['Mb'] = 1048576; |
989bfa9d |
2593 | $scan['M'] = 1048576; |
266a416e |
2594 | $scan['m'] = 1048576; |
989bfa9d |
2595 | $scan['KB'] = 1024; |
64efda84 |
2596 | $scan['Kb'] = 1024; |
989bfa9d |
2597 | $scan['K'] = 1024; |
266a416e |
2598 | $scan['k'] = 1024; |
989bfa9d |
2599 | |
2600 | while (list($key) = each($scan)) { |
2601 | if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) { |
2602 | $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key]; |
2603 | break; |
2604 | } |
2605 | } |
2606 | return $size; |
2607 | } |
2608 | |
7cf1c7bd |
2609 | /** |
2610 | * Converts bytes into display form |
2611 | * |
2612 | * @uses $CFG |
2613 | * @param string $directory a string of directory names under $CFG->dataroot |
2614 | * @return object |
2615 | * @todo Finish documenting this function |
2616 | */ |
44e2d2bb |
2617 | function display_size($size) { |
9fa49e22 |
2618 | /// Converts bytes into display form |
4909e176 |
2619 | |
7cf1c7bd |
2620 | static $gb, $mb, $kb, $b; |
4909e176 |
2621 | |
2622 | if (empty($gb)) { |
2623 | $gb = get_string('sizegb'); |
2624 | $mb = get_string('sizemb'); |
2625 | $kb = get_string('sizekb'); |
2626 | $b = get_string('sizeb'); |
2627 | } |
2628 | |
44e2d2bb |
2629 | if ($size >= 1073741824) { |
4909e176 |
2630 | $size = round($size / 1073741824 * 10) / 10 . $gb; |
44e2d2bb |
2631 | } else if ($size >= 1048576) { |
4909e176 |
2632 | $size = round($size / 1048576 * 10) / 10 . $mb; |
44e2d2bb |
2633 | } else if ($size >= 1024) { |
4909e176 |
2634 | $size = round($size / 1024 * 10) / 10 . $kb; |
d8ba183c |
2635 | } else { |
b0ccd3fb |
2636 | $size = $size .' '. $b; |
44e2d2bb |
2637 | } |
2638 | return $size; |
2639 | } |
2640 | |
7cf1c7bd |
2641 | /** |
2642 | * Create a directory. |
2643 | * |
2644 | * @uses $CFG |
2645 | * @param string $directory a string of directory names under $CFG->dataroot |
2646 | * @return object |
2647 | * @todo Finish documenting this function |
2648 | */ |
6b174680 |
2649 | function clean_filename($string) { |
9fa49e22 |
2650 | /// Cleans a given filename by removing suspicious or troublesome characters |
bbf4d8e6 |
2651 | /// Only these are allowed: |
a402bdcb |
2652 | /// alphanumeric _ - . |
bbf4d8e6 |
2653 | |
b0ccd3fb |
2654 | $string = eregi_replace("\.\.+", '', $string); |
8644437d |
2655 | $string = preg_replace('/[^\.a-zA-Z\d\_-]/','_', $string ); // only allowed chars |
b0ccd3fb |
2656 | $string = eregi_replace("_+", '_', $string); |
bbf4d8e6 |
2657 | return $string; |
6b174680 |
2658 | } |
2659 | |
2660 | |
1180c6dc |
2661 | /// STRING TRANSLATION //////////////////////////////////////// |
2662 | |
4bfa92e7 |
2663 | function current_language() { |
9fa49e22 |
2664 | /// Returns the code for the current language |
3db3acfb |
2665 | global $CFG, $USER, $SESSION; |
4bfa92e7 |
2666 | |
e5415d58 |
2667 | if (!empty($CFG->courselang)) { // Course language can override all other settings for this page |
b3153e4b |
2668 | return $CFG->courselang; |
2669 | |
e5415d58 |
2670 | } else if (!empty($SESSION->lang)) { // Session language can override other settings |
3db3acfb |
2671 | return $SESSION->lang; |
2672 | |
e5415d58 |
2673 | } else if (!empty($USER->lang)) { // User language can override site language |
4bfa92e7 |
2674 | return $USER->lang; |
3db3acfb |
2675 | |
4bfa92e7 |
2676 | } else { |
2677 | return $CFG->lang; |
2678 | } |
2679 | } |
bcc83c41 |
2680 | |
b0ccd3fb |
2681 | function print_string($identifier, $module='', $a=NULL) { |
9fa49e22 |
2682 | /// Given a string to translate - prints it out. |
2683 | echo get_string($identifier, $module, $a); |
2684 | } |
2685 | |
b0ccd3fb |
2686 | function get_string($identifier, $module='', $a=NULL) { |
d8ba183c |
2687 | /// Return the translated string specified by $identifier as |
9fa49e22 |
2688 | /// for $module. Uses the same format files as STphp. |
2689 | /// $a is an object, string or number that can be used |
2690 | /// within translation strings |
2691 | /// |
2692 | /// eg "hello \$a->firstname \$a->lastname" |
2693 | /// or "hello \$a" |
1180c6dc |
2694 | |
4bfa92e7 |
2695 | global $CFG; |
1180c6dc |
2696 | |
e11dc9b6 |
2697 | global $course; /// Not a nice hack, but quick |
ac8abb5f |
2698 | if (empty($CFG->courselang)) { |
2699 | if (!empty($course->lang)) { |
2700 | $CFG->courselang = $course->lang; |
2701 | } |
e11dc9b6 |
2702 | } |
2703 | |
4bfa92e7 |
2704 | $lang = current_language(); |
1180c6dc |
2705 | |
b0ccd3fb |
2706 | if ($module == '') { |
2707 | $module = 'moodle'; |
1180c6dc |
2708 | } |
2709 | |
b0ccd3fb |
2710 | $langpath = $CFG->dirroot .'/lang'; |
7cf1c7bd |
2711 | $langfile = $langpath .'/'. $lang .'/'. $module .'.php'; |
1180c6dc |
2712 | |
b947c69a |
2713 | // Look for the string - if found then return it |
2714 | |
2715 | if (file_exists($langfile)) { |
2716 | if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { |
2717 | eval($result); |
2718 | return $resultstring; |
1180c6dc |
2719 | } |
2720 | } |
2721 | |
cdac797c |
2722 | // If it's a module, then look within the module pack itself mod/xxxx/lang/en/module.php |
2723 | |
b0ccd3fb |
2724 | if ($module != 'moodle') { |
2725 | $modlangpath = $CFG->dirroot .'/mod/'. $module .'/lang'; |
2726 | $langfile = $modlangpath .'/'. $lang .'/'. $module .'.php'; |
cdac797c |
2727 | if (file_exists($langfile)) { |
2728 | if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { |
2729 | eval($result); |
2730 | return $resultstring; |
2731 | } |
2732 | } |
2733 | } |
1180c6dc |
2734 | |
cdac797c |
2735 | // If the preferred language was English we can abort now |
b0ccd3fb |
2736 | if ($lang == 'en') { |
2737 | return '[['. $identifier .']]'; |
b947c69a |
2738 | } |
1180c6dc |
2739 | |
b947c69a |
2740 | // Is a parent language defined? If so, try it. |
d8ba183c |
2741 | |
b0ccd3fb |
2742 | if ($result = get_string_from_file('parentlanguage', $langpath .'/'. $lang .'/moodle.php', "\$parentlang")) { |
b947c69a |
2743 | eval($result); |
2744 | if (!empty($parentlang)) { |
b0ccd3fb |
2745 | $langfile = $langpath .'/'. $parentlang .'/'. $module .'.php'; |
b947c69a |
2746 | if (file_exists($langfile)) { |
2747 | if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { |
2748 | eval($result); |
2749 | return $resultstring; |
2750 | } |
1180c6dc |
2751 | } |
2752 | } |
2753 | } |
b947c69a |
2754 | |
2755 | // Our only remaining option is to try English |
2756 | |
b0ccd3fb |
2757 | $langfile = $langpath .'/en/'. $module .'.php'; |
b947c69a |
2758 | if (!file_exists($langfile)) { |
b0ccd3fb |
2759 | return 'ERROR: No lang file ('. $langpath .'/en/'. $module .'.php)!'; |
b947c69a |
2760 | } |
2761 | if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { |
2762 | eval($result); |
2763 | return $resultstring; |
2764 | } |
2765 | |
cdac797c |
2766 | // If it's a module, then look within the module pack itself mod/xxxx/lang/en/module.php |
2767 | |
b0ccd3fb |
2768 | if ($module != 'moodle') { |
2769 | $langfile = $modlangpath .'/en/'. $module .'.php'; |
cdac797c |
2770 | if (file_exists($langfile)) { |
2771 | if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { |
2772 | eval($result); |
2773 | return $resultstring; |
2774 | } |
2775 | } |
2776 | } |
2777 | |
b0ccd3fb |
2778 | return '[['. $identifier .']]'; // Last resort |
1180c6dc |
2779 | } |
2780 | |
2781 | |
1180c6dc |
2782 | function get_string_from_file($identifier, $langfile, $destination) { |
9fa49e22 |
2783 | /// This function is only used from get_string(). |
2b32bddd |
2784 | |
2785 | static $strings; // Keep the strings cached in memory. |
2786 | |
2787 | if (empty($strings[$langfile])) { |
a32c99e2 |
2788 | $string = array(); |
2b32bddd |
2789 | include ($langfile); |
2790 | $strings[$langfile] = $string; |
2791 | } else { |
2792 | $string = &$strings[$langfile]; |
2793 | } |
1180c6dc |
2794 | |
2795 | if (!isset ($string[$identifier])) { |
2796 | return false; |
2797 | } |
2798 | |
b0ccd3fb |
2799 | return $destination .'= sprintf("'. $string[$identifier] .'");'; |
1180c6dc |
2800 | } |
f9903ed0 |
2801 | |
9d3c795c |
2802 | function get_strings($array, $module='') { |
2803 | /// Converts an array of strings |
2804 | |
2805 | $string = NULL; |
2806 | foreach ($array as $item) { |
2807 | $string->$item = get_string($item, $module); |
2808 | } |
2809 | return $string; |
2810 | } |
f9903ed0 |
2811 | |
1a72314d |
2812 | function get_list_of_languages() { |
2813 | /// Returns a list of language codes and their full names |
2814 | global $CFG; |
2815 | |
984a8bf3 |
2816 | $languages = array(); |
2817 | |
2818 | if (!empty($CFG->langlist)) { // use admin's list of languages |
2819 | $langlist = explode(',', $CFG->langlist); |
2820 | foreach ($langlist as $lang) { |
b0ccd3fb |
2821 | if (file_exists($CFG->dirroot .'/lang/'. $lang .'/moodle.php')) { |
2822 | include($CFG->dirroot .'/lang/'. $lang .'/moodle.php'); |
2823 | $languages[$lang] = $string['thislanguage'].' ('. $lang .')'; |
984a8bf3 |
2824 | unset($string); |
2825 | } |
2826 | } |
2827 | } else { |
b0ccd3fb |
2828 | if (!$langdirs = get_list_of_plugins('lang')) { |
984a8bf3 |
2829 | return false; |
2830 | } |
2831 | foreach ($langdirs as $lang) { |
b0ccd3fb |
2832 | include($CFG->dirroot .'/lang/'. $lang .'/moodle.php'); |
2833 | $languages[$lang] = $string['thislanguage'] .' ('. $lang .')'; |
984a8bf3 |
2834 | unset($string); |
2835 | } |
1a72314d |
2836 | } |
2837 | |
1a72314d |
2838 | return $languages; |
2839 | } |
2840 | |
5833a6c8 |
2841 | function get_list_of_countries() { |
2842 | /// Returns a list of country names in the current language |
2843 | global $CFG, $USER; |
2844 | |
2845 | $lang = current_language(); |
2846 | |
b0ccd3fb |
2847 | if (!file_exists($CFG->dirroot .'/lang/'. $lang .'/countries.php')) { |
2848 | if ($parentlang = get_string('parentlanguage')) { |
2849 | if (file_exists($CFG->dirroot .'/lang/'. $parentlang .'/countries.php')) { |
aa3eb050 |
2850 | $lang = $parentlang; |
2851 | } else { |
b0ccd3fb |
2852 | $lang = 'en'; // countries.php must exist in this pack |
aa3eb050 |
2853 | } |
2854 | } else { |
b0ccd3fb |
2855 | $lang = 'en'; // countries.php must exist in this pack |
aa3eb050 |
2856 | } |
5833a6c8 |
2857 | } |
2858 | |
b0ccd3fb |
2859 | include($CFG->dirroot .'/lang/'. $lang .'/countries.php'); |
5833a6c8 |
2860 | |
f8dbffb1 |
2861 | if (!empty($string)) { |
2862 | asort($string); |
2863 | } |
5833a6c8 |
2864 | |
2865 | return $string; |
2866 | } |
2867 | |
82196932 |
2868 | function get_list_of_pixnames() { |
2869 | /// Returns a list of picture names in the current language |
2870 | global $CFG; |
2871 | |
2872 | $lang = current_language(); |
2873 | |
b0ccd3fb |
2874 | if (!file_exists($CFG->dirroot .'/lang/'. $lang .'/pix.php')) { |
2875 | if ($parentlang = get_string('parentlanguage')) { |
2876 | if (file_exists($CFG->dirroot .'/lang/'. $parentlang .'/pix.php')) { |
82196932 |
2877 | $lang = $parentlang; |
2878 | } else { |
b0ccd3fb |
2879 | $lang = 'en'; // countries.php must exist in this pack |
82196932 |
2880 | } |
2881 | } else { |
b0ccd3fb |
2882 | $lang = 'en'; // countries.php must exist in this pack |
82196932 |
2883 | } |
2884 | } |
2885 | |
b0ccd3fb |
2886 | include_once($CFG->dirroot .'/lang/'. $lang .'/pix.php'); |
82196932 |
2887 | |
2888 | return $string; |
2889 | } |
2890 | |
9bd2c874 |
2891 | function document_file($file, $include=true) { |
2892 | /// Can include a given document file (depends on second |
2893 | /// parameter) or just return info about it |
2894 | |
c9d4e6da |
2895 | global $CFG; |
9bd2c874 |
2896 | |
db356340 |
2897 | $file = clean_filename($file); |
2898 | |
9bd2c874 |
2899 | if (empty($file)) { |
9bd2c874 |
2900 | return false; |
2901 | } |
2902 | |
b0ccd3fb |
2903 | $langs = array(current_language(), get_string('parentlanguage'), 'en'); |
9bd2c874 |
2904 | |
db356340 |
2905 | foreach ($langs as $lang) { |
b0ccd3fb |
2906 | $info->filepath = $CFG->dirroot .'/lang/'. $lang .'/docs/'. $file; |
2907 | $info->urlpath = $CFG->wwwroot .'/lang/'. $lang .'/docs/'. $file; |
9bd2c874 |
2908 | |
db356340 |
2909 | if (file_exists($info->filepath)) { |
2910 | if ($include) { |
2911 | include($info->filepath); |
2912 | } |
2913 | return $info; |
0c106cd3 |
2914 | } |
9bd2c874 |
2915 | } |
2916 | |
db356340 |
2917 | return false; |
9bd2c874 |
2918 | } |
2919 | |
1a72314d |
2920 | |
f9903ed0 |
2921 | /// ENCRYPTION //////////////////////////////////////////////// |
2922 | |
2923 | function rc4encrypt($data) { |
b0ccd3fb |
2924 | $password = 'nfgjeingjk'; |
2925 | return endecrypt($password, $data, ''); |
f9903ed0 |
2926 | } |
2927 | |
2928 | function rc4decrypt($data) { |
b0ccd3fb |
2929 | $password = 'nfgjeingjk'; |
2930 | return endecrypt($password, $data, 'de'); |
f9903ed0 |
2931 | } |
2932 | |
2933 | function endecrypt ($pwd, $data, $case) { |
9fa49e22 |
2934 | /// Based on a class by Mukul Sabharwal [mukulsabharwal@yahoo.com] |
f9903ed0 |
2935 | |
2936 | if ($case == 'de') { |
2937 | $data = urldecode($data); |
2938 | } |
2939 | |
b0ccd3fb |
2940 | $key[] = ''; |
2941 | $box[] = ''; |
2942 | $temp_swap = ''; |
f9903ed0 |
2943 | $pwd_length = 0; |
2944 | |
2945 | $pwd_length = strlen($pwd); |
2946 | |
2947 | for ($i = 0; $i <= 255; $i++) { |
2948 | $key[$i] = ord(substr($pwd, ($i % $pwd_length), 1)); |
2949 | $box[$i] = $i; |
2950 | } |
2951 | |
2952 | $x = 0; |
2953 | |
2954 | for ($i = 0; $i <= 255; $i++) { |
2955 | $x = ($x + $box[$i] + $key[$i]) % 256; |
2956 | $temp_swap = $box[$i]; |
2957 | $box[$i] = $box[$x]; |
2958 | $box[$x] = $temp_swap; |
2959 | } |
2960 | |
b0ccd3fb |
2961 | $temp = ''; |
2962 | $k = ''; |
f9903ed0 |
2963 | |
b0ccd3fb |
2964 | $cipherby = ''; |
2965 | $cipher = ''; |
f9903ed0 |
2966 | |
2967 | $a = 0; |
2968 | $j = 0; |
2969 | |
2970 | for ($i = 0; $i < strlen($data); $i++) { |
2971 | $a = ($a + 1) % 256; |
2972 | $j = ($j + $box[$a]) % 256; |
2973 | $temp = $box[$a]; |
2974 | $box[$a] = $box[$j]; |
2975 | $box[$j] = $temp; |
2976 | $k = $box[(($box[$a] + $box[$j]) % 256)]; |
2977 | $cipherby = ord(substr($data, $i, 1)) ^ $k; |
2978 | $cipher .= chr($cipherby); |
2979 | } |
2980 | |
2981 | if ($case == 'de') { |
2982 | $cipher = urldecode(urlencode($cipher)); |
2983 | } else { |
2984 | $cipher = urlencode($cipher); |
2985 | } |
2986 | |
2987 | return $cipher; |
2988 | } |
2989 | |
2990 | |
5fba04fb |
2991 | /// CALENDAR MANAGEMENT //////////////////////////////////////////////////////////////// |
2992 | |
2993 | |
2994 | function add_event($event) { |
2995 | /// call this function to add an event to the calendar table |
2996 | /// and to call any calendar plugins |
2997 | /// The function returns the id number of the resulting record |
2998 | /// The object event should include the following: |
2999 | /// $event->name Name for the event |
3000 | /// $event->description Description of the event (defaults to '') |
3001 | /// $event->courseid The id of the course this event belongs to (0 = all courses) |
3002 | /// $event->groupid The id of the group this event belongs to (0 = no group) |
3003 | /// $event->userid The id of the user this event belongs to (0 = no user) |
3004 | /// $event->modulename Name of the module that creates this event |
3005 | /// $event->instance Instance of the module that owns this event |
3006 | /// $event->eventtype The type info together with the module info could |
3007 | /// be used by calendar plugins to decide how to display event |
3008 | /// $event->timestart Timestamp for start of event |
3009 | /// $event->timeduration Duration (defaults to zero) |
3010 | |
3011 | global $CFG; |
3012 | |
3013 | $event->timemodified = time(); |
d8ba183c |
3014 | |
b0ccd3fb |
3015 | if (!$event->id = insert_record('event', $event)) { |
5fba04fb |
3016 | return false; |
3017 | } |
d8ba183c |
3018 | |
5fba04fb |
3019 | if (!empty($CFG->calendar)) { // call the add_event function of the selected calendar |
b0ccd3fb |
3020 | if (file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) { |
3021 | include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php'); |
5fba04fb |
3022 | $calendar_add_event = $CFG->calendar.'_add_event'; |
3023 | if (function_exists($calendar_add_event)) { |
3024 | $calendar_add_event($event); |
3025 | } |
3026 | } |
3027 | } |
d8ba183c |
3028 | |
5fba04fb |
3029 | return $event->id; |
3030 | } |
3031 | |
3032 | |
3033 | function update_event($event) { |
3034 | /// call this function to update an event in the calendar table |
3035 | /// the event will be identified by the id field of the $event object |
3036 | |
3037 | global $CFG; |
3038 | |
3039 | $event->timemodified = time(); |
d8ba183c |
3040 | |
5fba04fb |
3041 | if (!empty($CFG->calendar)) { // call the update_event function of the selected calendar |
b0ccd3fb |
3042 | if (file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) { |
3043 | include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php'); |
5fba04fb |
3044 | $calendar_update_event = $CFG->calendar.'_update_event'; |
3045 | if (function_exists($calendar_update_event)) { |
3046 | $calendar_update_event($event); |
3047 | } |
3048 | } |
3049 | } |
b0ccd3fb |
3050 | return update_record('event', $event); |
5fba04fb |
3051 | } |
3052 | |
3053 | |
3054 | function delete_event($id) { |
3055 | /// call this function to delete the event with id $id from calendar table |
3056 | |
3057 | global $CFG; |
3058 | |
3059 | if (!empty($CFG->calendar)) { // call the delete_event function of the selected calendar |
b0ccd3fb |
3060 | if (file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) { |
3061 | include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php'); |
5fba04fb |
3062 | $calendar_delete_event = $CFG->calendar.'_delete_event'; |
3063 | if (function_exists($calendar_delete_event)) { |
3064 | $calendar_delete_event($id); |
3065 | } |
3066 | } |
3067 | } |
b0ccd3fb |
3068 | return delete_records('event', 'id', $id); |
5fba04fb |
3069 | } |
3070 | |
3071 | |
dcd338ff |
3072 | function hide_event($event) { |
3073 | /// call this function to hide an event in the calendar table |
3074 | /// the event will be identified by the id field of the $event object |
3075 | |
3076 | global $CFG; |
3077 | |
3078 | if (!empty($CFG->calendar)) { // call the update_event function of the selected calendar |
b0ccd3fb |
3079 | if (file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) { |
3080 | include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php'); |
dcd338ff |
3081 | $calendar_hide_event = $CFG->calendar.'_hide_event'; |
3082 | if (function_exists($calendar_hide_event)) { |
3083 | $calendar_hide_event($event); |
3084 | } |
3085 | } |
3086 | } |
3087 | return set_field('event', 'visible', 0, 'id', $event->id); |
3088 | } |
3089 | |
3090 | |
3091 | function show_event($event) { |
3092 | /// call this function to unhide an event in the calendar table |
3093 | /// the event will be identified by the id field of the $event object |
3094 | |
3095 | global $CFG; |
3096 | |
3097 | if (!empty($CFG->calendar)) { // call the update_event function of the selected calendar |
b0ccd3fb |
3098 | if (file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) { |
3099 | include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php'); |
dcd338ff |
3100 | $calendar_show_event = $CFG->calendar.'_show_event'; |
3101 | if (function_exists($calendar_show_event)) { |
3102 | $calendar_show_event($event); |
3103 | } |
3104 | } |
3105 | } |
3106 | return set_field('event', 'visible', 1, 'id', $event->id); |
3107 | } |
5fba04fb |
3108 | |
3109 | |
9fa49e22 |
3110 | /// ENVIRONMENT CHECKING //////////////////////////////////////////////////////////// |
1e3e716f |
3111 | |
b0ccd3fb |
3112 | function get_list_of_plugins($plugin='mod', $exclude='') { |
1d881d92 |
3113 | /// Lists plugin directories within some directory |
3114 | |
3115 | global $CFG; |
3116 | |
b0ccd3fb |
3117 | $basedir = opendir($CFG->dirroot .'/'. $plugin); |
1d881d92 |
3118 | while ($dir = readdir($basedir)) { |
b35e8568 |
3119 | $firstchar = substr($dir, 0, 1); |
b0ccd3fb |
3120 | if ($firstchar == '.' or $dir == 'CVS' or $dir == '_vti_cnf' or $dir == $exclude) { |
1d881d92 |
3121 | continue; |
3122 | } |
b0ccd3fb |
3123 | if (filetype($CFG->dirroot .'/'. $plugin .'/'. $dir) != 'dir') { |
1d881d92 |
3124 | continue; |
3125 | } |
3126 | $plugins[] = $dir; |
3127 | } |
3128 | if ($plugins) { |
3129 | asort($plugins); |
3130 | } |
3131 | return $plugins; |
3132 | } |
3133 | |
b0ccd3fb |
3134 | function check_php_version($version='4.1.0') { |
9fa49e22 |
3135 | /// Returns true is the current version of PHP is greater that the specified one |
b0ccd3fb |
3136 | $minversion = intval(str_replace('.', '', $version)); |
3137 | $curversion = intval(str_replace('.', '', phpversion())); |
b0cb5e22 |
3138 | return ($curversion >= $minversion); |
3139 | } |
3140 | |
b0ccd3fb |
3141 | function check_browser_version($brand='MSIE', $version=5.5) { |
9fa49e22 |
3142 | /// Checks to see if is a browser matches the specified |
3143 | /// brand and is equal or better version. |
0095d5cd |
3144 | |
b0ccd3fb |
3145 | $agent = $_SERVER['HTTP_USER_AGENT']; |
4c46c425 |
3146 | |
3147 | if (empty($agent)) { |
0095d5cd |
3148 | return false; |
3149 | } |
4c46c425 |
3150 | |
3151 | switch ($brand) { |
3152 | |
b0ccd3fb |
3153 | case 'Gecko': /// Gecko based browsers |
4c46c425 |
3154 | |
b0ccd3fb |
3155 | if (substr_count($agent, 'Camino')) { // MacOS X Camino not supported. |
4c46c425 |
3156 | return false; |
3157 | } |
3158 | |
3159 | // the proper string - Gecko/CCYYMMDD Vendor/Version |
3160 | if (ereg("^([a-zA-Z]+)/([0-9]+\.[0-9]+) \((.*)\) (.*)$", $agent, $match)) { |
3161 | if (ereg("^([Gecko]+)/([0-9]+)",$match[4], $reldate)) { |
3162 | if ($reldate[2] > $version) { |
3163 | return true; |
3164 | } |
3165 | } |
3166 | } |
3167 | break; |
3168 | |
3169 | |
b0ccd3fb |
3170 | case 'MSIE': /// Internet Explorer |
4c46c425 |
3171 | |
0e2585ac |
3172 | if (strpos($agent, 'Opera')) { // Reject Opera |
3173 | return false; |
3174 | } |
b0ccd3fb |
3175 | $string = explode(';', $agent); |
4c46c425 |
3176 | if (!isset($string[1])) { |
3177 | return false; |
3178 | } |
b0ccd3fb |
3179 | $string = explode(' ', trim($string[1])); |
4c46c425 |
3180 | if (!isset($string[0]) and !isset($string[1])) { |
3181 | return false; |
3182 | } |
3183 | if ($string[0] == $brand and (float)$string[1] >= $version ) { |
3184 | return true; |
3185 | } |
3186 | break; |
3187 | |
0095d5cd |
3188 | } |
4c46c425 |
3189 | |
0095d5cd |
3190 | return false; |
3191 | } |
3192 | |
c39c66a5 |
3193 | function ini_get_bool($ini_get_arg) { |
3194 | /// This function makes the return value of ini_get consistent if you are |
3195 | /// setting server directives through the .htaccess file in apache. |
3196 | /// Current behavior for value set from php.ini On = 1, Off = [blank] |
3197 | /// Current behavior for value set from .htaccess On = On, Off = Off |
3198 | /// Contributed by jdell@unr.edu |
3199 | |
3200 | $temp = ini_get($ini_get_arg); |
3201 | |
b0ccd3fb |
3202 | if ($temp == '1' or strtolower($temp) == 'on') { |
c39c66a5 |
3203 | return true; |
3204 | } |
3205 | return false; |
3206 | } |
3207 | |
0095d5cd |
3208 | function can_use_richtext_editor() { |
47037513 |
3209 | /// Compatibility stub to provide backward compatibility |
3210 | return can_use_html_editor(); |
3211 | } |
3212 | |
3213 | function can_use_html_editor() { |
4c46c425 |
3214 | /// Is the HTML editor enabled? This depends on site and user |
3215 | /// settings, as well as the current browser being used. |
47037513 |
3216 | /// Returns false is editor is not being used, otherwise |
3217 | /// returns "MSIE" or "Gecko" |
4c46c425 |
3218 | |
0095d5cd |
3219 | global $USER, $CFG; |
4c46c425 |
3220 | |
ce78926d |
3221 | if (!empty($USER->htmleditor) and !empty($CFG->htmleditor)) { |
b0ccd3fb |
3222 | if (check_browser_version('MSIE', 5.5)) { |
3223 | return 'MSIE'; |
3224 | } else if (check_browser_version('Gecko', 20030516)) { |
3225 | return 'Gecko'; |
4c46c425 |
3226 | } |
7ce20f09 |
3227 | } |
3228 | return false; |
0095d5cd |
3229 | } |
3230 | |
47037513 |
3231 | |
74944b73 |
3232 | function check_gd_version() { |
9fa49e22 |
3233 | /// Hack to find out the GD version by parsing phpinfo output |
aa095969 |
3234 | $gdversion = 0; |
74944b73 |
3235 | |
aa095969 |
3236 | if (function_exists('gd_info')){ |
3237 | $gd_info = gd_info(); |
b0ccd3fb |
3238 | if (substr_count($gd_info['GD Version'], '2.')) { |
aa095969 |
3239 | $gdversion = 2; |
b0ccd3fb |
3240 | } else if (substr_count($gd_info['GD Version'], '1.')) { |
3ee23682 |
3241 | $gdversion = 1; |
aa095969 |
3242 | } |
3ee23682 |
3243 | |
aa095969 |
3244 | } else { |
3245 | ob_start(); |
3246 | phpinfo(8); |
3247 | $phpinfo = ob_get_contents(); |
3248 | ob_end_clean(); |
74944b73 |
3249 | |
aa095969 |
3250 | $phpinfo = explode("\n",$phpinfo); |
74944b73 |
3251 | |
92a4b0f1 |
3252 | |
aa095969 |
3253 | foreach ($phpinfo as $text) { |
3254 | $parts = explode('</td>',$text); |
3255 | foreach ($parts as $key => $val) { |
3256 | $parts[$key] = trim(strip_tags($val)); |
3257 | } |
b0ccd3fb |
3258 | if ($parts[0] == 'GD Version') { |
3259 | if (substr_count($parts[1], '2.0')) { |
3260 | $parts[1] = '2.0'; |
aa095969 |
3261 | } |
3262 | $gdversion = intval($parts[1]); |
92a4b0f1 |
3263 | } |
74944b73 |
3264 | } |
3265 | } |
3266 | |
3267 | return $gdversion; // 1, 2 or 0 |
3268 | } |
f9903ed0 |
3269 | |
0095d5cd |
3270 | |
9fa49e22 |
3271 | function moodle_needs_upgrading() { |
3272 | /// Checks version numbers of Main code and all modules to see |
3273 | /// if there are any mismatches ... returns true or false |
3274 | global $CFG; |
3275 | |
b0ccd3fb |
3276 | include_once($CFG->dirroot .'/version.php'); # defines $version and upgrades |
d8ba183c |
3277 | if ($CFG->version) { |
9fa49e22 |
3278 | if ($version > $CFG->version) { |
3279 | return true; |
3280 | } |
b0ccd3fb |
3281 | if ($mods = get_list_of_plugins('mod')) { |
9fa49e22 |
3282 | foreach ($mods as $mod) { |
b0ccd3fb |
3283 | $fullmod = $CFG->dirroot .'/mod/'. $mod; |
9fa49e22 |
3284 | unset($module); |
b0ccd3fb |
3285 | if (!is_readable($fullmod .'/version.php')) { |
3286 | notify('Module "'. $mod .'" is not readable - check permissions'); |
1079c8a8 |
3287 | continue; |
3288 | } |
b0ccd3fb |
3289 | include_once($fullmod .'/version.php'); # defines $module with version etc |
3290 | if ($currmodule = get_record('modules', 'name', $mod)) { |
9fa49e22 |
3291 | if ($module->version > $currmodule->version) { |
3292 | return true; |
3293 | } |
3294 | } |
3295 | } |
3296 | } |
3297 | } else { |
3298 | return true; |
3299 | } |
3300 | return false; |
3301 | } |
3302 | |
3303 | |
3304 | /// MISCELLANEOUS //////////////////////////////////////////////////////////////////// |
3305 | |
9d3c795c |
3306 | function notify_login_failures() { |
b40bc478 |
3307 | global $CFG, $db; |
9d3c795c |
3308 | |
3309 | // notify admin users or admin user of any failed logins (since last notification). |
3310 | switch ($CFG->notifyloginfailures) { |
3311 | case 'mainadmin' : |
3312 | $recip = array(get_admin()); |
3313 | break; |
3314 | case 'alladmins': |
3315 | $recip = get_admins(); |
3316 | break; |
3317 | } |
8f0cd6ef |
3318 | |
9d3c795c |
3319 | if (empty($CFG->lastnotifyfailure)) { |
3320 | $CFG->lastnotifyfailure=0; |
3321 | } |
8f0cd6ef |
3322 | |
3323 | // we need to deal with the threshold stuff first. |
27106bac |
3324 | if (empty($CFG->notifyloginthreshold)) { |
3325 | $CFG->notifyloginthreshold = 10; // default to something sensible. |
9d3c795c |
3326 | } |
3327 | |
b0ccd3fb |
3328 | $notifyipsrs = $db->Execute('SELECT ip FROM '. $CFG->prefix .'log WHERE time > '. $CFG->lastnotifyfailure .' |
3329 | AND module=\'login\' AND action=\'error\' GROUP BY ip HAVING count(*) > '. $CFG->notifyloginthreshold); |
9d3c795c |
3330 | |
b0ccd3fb |
3331 | $notifyusersrs = $db->Execute('SELECT info FROM '. $CFG->prefix .'log WHERE time > '. $CFG->lastnotifyfailure .' |
3332 | AND module=\'login\' AND action=\'error\' GROUP BY info HAVING count(*) > '. $CFG->notifyloginthreshold); |
8f0cd6ef |
3333 | |
9d3c795c |
3334 | if ($notifyipsrs) { |
b40bc478 |
3335 | $ipstr = ''; |
9d3c795c |
3336 | while ($row = $notifyipsrs->FetchRow()) { |
|