f9903ed0 |
1 | <?PHP // $Id$ |
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 // |
19 | // http://moodle.com // |
20 | // // |
21 | // Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com // |
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 | |
f9903ed0 |
37 | |
9fa49e22 |
38 | /// PARAMETER HANDLING //////////////////////////////////////////////////// |
6b174680 |
39 | |
9fa49e22 |
40 | function require_variable($var) { |
41 | /// Variable must be present |
42 | if (! isset($var)) { |
43 | error("A required parameter was missing"); |
6b174680 |
44 | } |
45 | } |
46 | |
9fa49e22 |
47 | function optional_variable(&$var, $default=0) { |
48 | /// Variable may be present, if not then set a default |
49 | if (! isset($var)) { |
50 | $var = $default; |
6b174680 |
51 | } |
52 | } |
53 | |
54 | |
9fa49e22 |
55 | function set_config($name, $value) { |
56 | /// No need for get_config because they are usually always available in $CFG |
dfc9ba9b |
57 | |
9fa49e22 |
58 | if (get_field("config", "name", "name", $name)) { |
59 | return set_field("config", "value", $value, "name", $name); |
d897cae4 |
60 | } else { |
9fa49e22 |
61 | $config->name = $name; |
62 | $config->value = $value; |
63 | return insert_record("config", $config); |
39917a09 |
64 | } |
39917a09 |
65 | } |
66 | |
39917a09 |
67 | |
9fa49e22 |
68 | /// FUNCTIONS FOR HANDLING TIME //////////////////////////////////////////// |
39917a09 |
69 | |
3db75c62 |
70 | function make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0, $timezone=99) { |
9fa49e22 |
71 | /// Given date parts in user time, produce a GMT timestamp |
39917a09 |
72 | |
94e34118 |
73 | global $USER; |
74 | |
03c17ddf |
75 | if ($timezone == 99) { |
94e34118 |
76 | $timezone = (float)$USER->timezone; |
77 | } |
78 | |
79 | if (abs($timezone) > 13) { |
03c17ddf |
80 | return mktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year); |
81 | } else { |
82 | $time = gmmktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year); |
83 | return usertime($time, $timezone); // This is GMT |
84 | } |
39917a09 |
85 | } |
86 | |
8dbed6be |
87 | function format_time($totalsecs, $str=NULL) { |
9fa49e22 |
88 | /// Given an amount of time in seconds, returns string |
89 | /// formatted nicely as months, days, hours etc as needed |
c7e3ac2a |
90 | |
6b174680 |
91 | $totalsecs = abs($totalsecs); |
c7e3ac2a |
92 | |
8dbed6be |
93 | if (!$str) { // Create the str structure the slow way |
94 | $str->day = get_string("day"); |
95 | $str->days = get_string("days"); |
96 | $str->hour = get_string("hour"); |
97 | $str->hours = get_string("hours"); |
98 | $str->min = get_string("min"); |
99 | $str->mins = get_string("mins"); |
100 | $str->sec = get_string("sec"); |
101 | $str->secs = get_string("secs"); |
102 | } |
103 | |
104 | $days = floor($totalsecs/86400); |
6b174680 |
105 | $remainder = $totalsecs - ($days*86400); |
8dbed6be |
106 | $hours = floor($remainder/3600); |
6b174680 |
107 | $remainder = $remainder - ($hours*3600); |
8dbed6be |
108 | $mins = floor($remainder/60); |
109 | $secs = $remainder - ($mins*60); |
110 | |
111 | $ss = ($secs == 1) ? $str->sec : $str->secs; |
112 | $sm = ($mins == 1) ? $str->min : $str->mins; |
113 | $sh = ($hours == 1) ? $str->hour : $str->hours; |
114 | $sd = ($days == 1) ? $str->day : $str->days; |
115 | |
9c9f7d77 |
116 | $odays = ""; |
117 | $ohours = ""; |
118 | $omins = ""; |
119 | $osecs = ""; |
120 | |
8dbed6be |
121 | if ($days) $odays = "$days $sd"; |
122 | if ($hours) $ohours = "$hours $sh"; |
123 | if ($mins) $omins = "$mins $sm"; |
124 | if ($secs) $osecs = "$secs $ss"; |
6b174680 |
125 | |
126 | if ($days) return "$odays $ohours"; |
127 | if ($hours) return "$ohours $omins"; |
128 | if ($mins) return "$omins $osecs"; |
129 | if ($secs) return "$osecs"; |
130 | return get_string("now"); |
131 | } |
f9903ed0 |
132 | |
5fa51a39 |
133 | function userdate($date, $format="", $timezone=99) { |
9fa49e22 |
134 | /// Returns a formatted string that represents a date in user time |
135 | /// WARNING: note that the format is for strftime(), not date(). |
136 | /// Because of a bug in most Windows time libraries, we can't use |
137 | /// the nicer %e, so we have to use %d which has leading zeroes. |
138 | /// A lot of the fuss below is just getting rid of these leading |
139 | /// zeroes as efficiently as possible. |
7a302afc |
140 | |
873960de |
141 | global $USER; |
142 | |
5fa51a39 |
143 | if ($format == "") { |
dcde9f02 |
144 | $format = get_string("strftimedaydatetime"); |
5fa51a39 |
145 | } |
035cdbff |
146 | |
dcde9f02 |
147 | $formatnoday = str_replace("%d", "DD", $format); |
148 | $fixday = ($formatnoday != $format); |
149 | |
5fa51a39 |
150 | if ($timezone == 99) { |
ab247495 |
151 | if (isset($USER->timezone)) { |
152 | $timezone = (float)$USER->timezone; |
153 | } |
5fa51a39 |
154 | } |
0431bd7c |
155 | if (abs($timezone) > 13) { |
035cdbff |
156 | if ($fixday) { |
157 | $datestring = strftime($formatnoday, $date); |
158 | $daystring = str_replace(" 0", "", strftime(" %d", $date)); |
159 | $datestring = str_replace("DD", $daystring, $datestring); |
160 | } else { |
161 | $datestring = strftime($format, $date); |
162 | } |
bea7a51e |
163 | } else { |
70d4cf82 |
164 | $date = $date + (int)($timezone * 3600); |
035cdbff |
165 | if ($fixday) { |
70d4cf82 |
166 | $datestring = gmstrftime($formatnoday, $date); |
9fa49e22 |
167 | $daystring = str_replace(" 0", "", gmstrftime(" %d", $date)); |
035cdbff |
168 | $datestring = str_replace("DD", $daystring, $datestring); |
169 | } else { |
70d4cf82 |
170 | $datestring = gmstrftime($format, $date); |
035cdbff |
171 | } |
873960de |
172 | } |
bea7a51e |
173 | |
035cdbff |
174 | return $datestring; |
873960de |
175 | } |
176 | |
5fa51a39 |
177 | function usergetdate($date, $timezone=99) { |
9fa49e22 |
178 | /// Given a $date timestamp in GMT, returns an array |
179 | /// that represents the date in user time |
6b174680 |
180 | |
873960de |
181 | global $USER; |
182 | |
5fa51a39 |
183 | if ($timezone == 99) { |
184 | $timezone = (float)$USER->timezone; |
185 | } |
0431bd7c |
186 | if (abs($timezone) > 13) { |
873960de |
187 | return getdate($date); |
188 | } |
d2d6171f |
189 | //There is no gmgetdate so I have to fake it... |
190 | $date = $date + (int)($timezone * 3600); |
191 | $getdate["seconds"] = gmstrftime("%S", $date); |
192 | $getdate["minutes"] = gmstrftime("%M", $date); |
193 | $getdate["hours"] = gmstrftime("%H", $date); |
194 | $getdate["mday"] = gmstrftime("%d", $date); |
195 | $getdate["wday"] = gmstrftime("%u", $date); |
196 | $getdate["mon"] = gmstrftime("%m", $date); |
197 | $getdate["year"] = gmstrftime("%Y", $date); |
198 | $getdate["yday"] = gmstrftime("%j", $date); |
199 | $getdate["weekday"] = gmstrftime("%A", $date); |
200 | $getdate["month"] = gmstrftime("%B", $date); |
201 | return $getdate; |
d552ead0 |
202 | } |
203 | |
204 | function usertime($date, $timezone=99) { |
9fa49e22 |
205 | /// Given a GMT timestamp (seconds since epoch), offsets it by |
206 | /// the timezone. eg 3pm in India is 3pm GMT - 7 * 3600 seconds |
d552ead0 |
207 | global $USER; |
208 | |
209 | if ($timezone == 99) { |
210 | $timezone = (float)$USER->timezone; |
211 | } |
0431bd7c |
212 | if (abs($timezone) > 13) { |
d552ead0 |
213 | return $date; |
214 | } |
215 | return $date - (int)($timezone * 3600); |
216 | } |
217 | |
edf7fe8c |
218 | function usergetmidnight($date, $timezone=99) { |
9fa49e22 |
219 | /// Given a time, return the GMT timestamp of the most recent midnight |
220 | /// for the current user. |
edf7fe8c |
221 | global $USER; |
222 | |
4606d9bb |
223 | if ($timezone == 99) { |
224 | $timezone = (float)$USER->timezone; |
225 | } |
226 | |
edf7fe8c |
227 | $userdate = usergetdate($date, $timezone); |
4606d9bb |
228 | |
0431bd7c |
229 | if (abs($timezone) > 13) { |
4606d9bb |
230 | return mktime(0, 0, 0, $userdate["mon"], $userdate["mday"], $userdate["year"]); |
231 | } |
232 | |
edf7fe8c |
233 | $timemidnight = gmmktime (0, 0, 0, $userdate["mon"], $userdate["mday"], $userdate["year"]); |
234 | return usertime($timemidnight, $timezone); // Time of midnight of this user's day, in GMT |
235 | |
236 | } |
237 | |
d552ead0 |
238 | function usertimezone($timezone=99) { |
9fa49e22 |
239 | /// Returns a string that prints the user's timezone |
d552ead0 |
240 | global $USER; |
241 | |
242 | if ($timezone == 99) { |
243 | $timezone = (float)$USER->timezone; |
244 | } |
0431bd7c |
245 | if (abs($timezone) > 13) { |
d552ead0 |
246 | return "server time"; |
247 | } |
248 | if (abs($timezone) < 0.5) { |
249 | return "GMT"; |
250 | } |
251 | if ($timezone > 0) { |
252 | return "GMT+$timezone"; |
253 | } else { |
254 | return "GMT$timezone"; |
255 | } |
f9903ed0 |
256 | } |
257 | |
258 | |
9fa49e22 |
259 | /// USER AUTHENTICATION AND LOGIN //////////////////////////////////////// |
f9903ed0 |
260 | |
da5c172a |
261 | function require_login($courseid=0) { |
9fa49e22 |
262 | /// This function checks that the current user is logged in, and optionally |
263 | /// whether they are "logged in" or allowed to be in a particular course. |
264 | /// If not, then it redirects them to the site login or course enrolment. |
f9903ed0 |
265 | |
607809b3 |
266 | global $CFG, $SESSION, $USER, $FULLME, $PHPSESSID; |
f9903ed0 |
267 | |
da5c172a |
268 | // First check that the user is logged in to the site. |
c21c671d |
269 | if (! (isset($USER->loggedin) and $USER->confirmed and ($USER->site == $CFG->wwwroot)) ) { // They're not |
f9903ed0 |
270 | $SESSION->wantsurl = $FULLME; |
9f44d972 |
271 | if (!empty($_SERVER["HTTP_REFERER"])) { |
272 | $SESSION->fromurl = $_SERVER["HTTP_REFERER"]; |
273 | } |
8223d271 |
274 | save_session("SESSION"); |
c21c671d |
275 | $USER = NULL; |
276 | save_session("USER"); |
f9903ed0 |
277 | if ($PHPSESSID) { // Cookies not enabled. |
a7b9e8bc |
278 | redirect("$CFG->wwwroot/login/index.php?PHPSESSID=$PHPSESSID"); |
f9903ed0 |
279 | } else { |
a7b9e8bc |
280 | redirect("$CFG->wwwroot/login/index.php"); |
f9903ed0 |
281 | } |
282 | die; |
f9903ed0 |
283 | } |
808a3baa |
284 | |
285 | // Check that the user account is properly set up |
286 | if (user_not_fully_set_up($USER)) { |
287 | $site = get_site(); |
288 | redirect("$CFG->wwwroot/user/edit.php?id=$USER->id&course=$site->id"); |
289 | die; |
290 | } |
da5c172a |
291 | |
292 | // Next, check if the user can be in a particular course |
293 | if ($courseid) { |
9c9f7d77 |
294 | if (!empty($USER->student[$courseid]) or !empty($USER->teacher[$courseid]) or !empty($USER->admin)) { |
cb909d74 |
295 | if (isset($USER->realuser)) { // Make sure the REAL person can also access this course |
296 | if (!isteacher($courseid, $USER->realuser)) { |
297 | print_header(); |
5ebe27e2 |
298 | notice(get_string("studentnotallowed", "", "$USER->firstname $USER->lastname"), $CFG->wwwroot); |
cb909d74 |
299 | } |
300 | |
301 | } else { // just update their last login time |
3ce2f1e0 |
302 | update_user_in_db(); |
303 | } |
da5c172a |
304 | return; // user is a member of this course. |
305 | } |
306 | if (! $course = get_record("course", "id", $courseid)) { |
307 | error("That course doesn't exist"); |
308 | } |
7363ff91 |
309 | if ($USER->username == "guest") { |
310 | switch ($course->guest) { |
311 | case 0: // Guests not allowed |
312 | print_header(); |
313 | notice(get_string("guestsnotallowed", "", $course->fullname)); |
314 | break; |
315 | case 1: // Guests allowed |
316 | update_user_in_db(); |
317 | return; |
318 | case 2: // Guests allowed with key (drop through) |
319 | break; |
320 | } |
da5c172a |
321 | } |
f9903ed0 |
322 | |
7363ff91 |
323 | // Currently not enrolled in the course, so see if they want to enrol |
da5c172a |
324 | $SESSION->wantsurl = $FULLME; |
8223d271 |
325 | save_session("SESSION"); |
da5c172a |
326 | redirect("$CFG->wwwroot/course/enrol.php?id=$courseid"); |
327 | die; |
328 | } |
f9903ed0 |
329 | } |
330 | |
1d881d92 |
331 | function update_user_login_times() { |
332 | global $USER; |
333 | |
334 | $USER->lastlogin = $user->lastlogin = $USER->currentlogin; |
335 | $USER->currentlogin = $user->currentlogin = time(); |
336 | save_session("USER"); |
337 | |
338 | $user->id = $USER->id; |
339 | |
340 | return update_record("user", $user); |
341 | } |
342 | |
808a3baa |
343 | function user_not_fully_set_up($user) { |
ac5d88eb |
344 | return ($user->username != "guest" and (empty($user->firstname) or empty($user->lastname) or empty($user->email))); |
808a3baa |
345 | } |
f9903ed0 |
346 | |
f9903ed0 |
347 | function update_login_count() { |
9fa49e22 |
348 | /// Keeps track of login attempts |
349 | |
f9903ed0 |
350 | global $SESSION; |
351 | |
352 | $max_logins = 10; |
353 | |
354 | if (empty($SESSION->logincount)) { |
355 | $SESSION->logincount = 1; |
356 | } else { |
357 | $SESSION->logincount++; |
358 | } |
8223d271 |
359 | save_session("SESSION"); |
f9903ed0 |
360 | |
361 | if ($SESSION->logincount > $max_logins) { |
9fa49e22 |
362 | unset($SESSION->wantsurl); |
363 | save_session("SESSION"); |
1d881d92 |
364 | error(get_string("errortoomanylogins")); |
d578afc8 |
365 | } |
366 | } |
367 | |
9fa49e22 |
368 | function reset_login_count() { |
369 | /// Resets login attempts |
370 | global $SESSION; |
d578afc8 |
371 | |
9fa49e22 |
372 | $SESSION->logincount = 0; |
373 | save_session("SESSION"); |
d578afc8 |
374 | } |
375 | |
d578afc8 |
376 | |
f9903ed0 |
377 | function isadmin($userid=0) { |
9fa49e22 |
378 | /// Is the user an admin? |
f9903ed0 |
379 | global $USER; |
380 | |
ac5d88eb |
381 | if (empty($USER->id)) { |
9bd2c874 |
382 | return false; |
383 | } |
384 | |
385 | if (empty($userid)) { |
ebc3bd2b |
386 | return record_exists("user_admins", "userid", $USER->id); |
f9903ed0 |
387 | } |
388 | |
ebc3bd2b |
389 | return record_exists("user_admins", "userid", $userid); |
f9903ed0 |
390 | } |
391 | |
9fa49e22 |
392 | |
8a9e3fd7 |
393 | function isteacher($courseid, $userid=0) { |
9fa49e22 |
394 | /// Is the user a teacher or admin? |
f9903ed0 |
395 | global $USER; |
396 | |
d115a57f |
397 | if (isadmin($userid)) { // admins can do anything the teacher can |
398 | return true; |
399 | } |
400 | |
f9903ed0 |
401 | if (!$userid) { |
9bd2c874 |
402 | return !empty($USER->teacher[$courseid]); |
f9903ed0 |
403 | } |
404 | |
ebc3bd2b |
405 | return record_exists("user_teachers", "userid", $userid, "course", $courseid); |
f9903ed0 |
406 | } |
407 | |
408 | |
8a9e3fd7 |
409 | function isstudent($courseid, $userid=0) { |
9fa49e22 |
410 | /// Is the user a student in this course? |
f9903ed0 |
411 | global $USER; |
412 | |
413 | if (!$userid) { |
346b1a24 |
414 | return !empty($USER->student[$courseid]); |
f9903ed0 |
415 | } |
416 | |
ebc3bd2b |
417 | // $timenow = time(); // todo: add time check below |
f9903ed0 |
418 | |
ebc3bd2b |
419 | return record_exists("user_students", "userid", $userid, "course", $courseid); |
f9903ed0 |
420 | } |
421 | |
da5c172a |
422 | function isguest($userid=0) { |
9fa49e22 |
423 | /// Is the user a guest? |
da5c172a |
424 | global $USER; |
425 | |
426 | if (!$userid) { |
427 | return ($USER->username == "guest"); |
428 | } |
429 | |
9fa49e22 |
430 | return record_exists("user", "id", $userid, "username", "guest"); |
da5c172a |
431 | } |
432 | |
9fa49e22 |
433 | |
2c309dc2 |
434 | function isediting($courseid, $user=NULL) { |
9fa49e22 |
435 | /// Is the current user in editing mode? |
2c309dc2 |
436 | global $USER; |
437 | if (!$user){ |
438 | $user = $USER; |
439 | } |
9c9f7d77 |
440 | if (empty($user->editing)) { |
441 | return false; |
442 | } |
2c309dc2 |
443 | return ($user->editing and isteacher($courseid, $user->id)); |
444 | } |
445 | |
f9903ed0 |
446 | |
447 | function set_moodle_cookie($thing) { |
9fa49e22 |
448 | /// Sets a moodle cookie with an encrypted string |
7185e073 |
449 | global $CFG; |
450 | |
451 | $cookiename = "MOODLEID{$CFG->prefix}"; |
f9903ed0 |
452 | |
453 | $days = 60; |
454 | $seconds = 60*60*24*$days; |
455 | |
7185e073 |
456 | setCookie($cookiename, "", time() - 3600, "/"); |
457 | setCookie($cookiename, rc4encrypt($thing), time()+$seconds, "/"); |
f9903ed0 |
458 | } |
459 | |
460 | |
461 | function get_moodle_cookie() { |
9fa49e22 |
462 | /// Gets a moodle cookie with an encrypted string |
7185e073 |
463 | global $CFG; |
464 | |
4fe04be0 |
465 | $cookiename = "MOODLEID{$CFG->prefix}"; |
7185e073 |
466 | |
1079c8a8 |
467 | if (empty($_COOKIE[$cookiename])) { |
468 | return ""; |
469 | } else { |
470 | return rc4decrypt($_COOKIE[$cookiename]); |
471 | } |
f9903ed0 |
472 | } |
473 | |
474 | |
8223d271 |
475 | function save_session($VAR) { |
9fa49e22 |
476 | /// Copies temporary session variable to permanent session variable |
477 | /// eg $_SESSION["USER"] = $USER; |
8223d271 |
478 | global $$VAR; |
479 | $_SESSION[$VAR] = $$VAR; |
480 | } |
481 | |
f9903ed0 |
482 | |
faebaf0f |
483 | function create_user_record($username, $password) { |
9fa49e22 |
484 | /// Creates a bare-bones user record |
e858f9da |
485 | global $REMOTE_ADDR, $CFG; |
486 | |
6ae24de0 |
487 | if (function_exists(auth_get_userinfo)) { |
e858f9da |
488 | if ($newinfo = auth_get_userinfo($username)) { |
34daec9b |
489 | foreach ($newinfo as $key => $value){ |
9f44d972 |
490 | $newuser->$key = addslashes(stripslashes($value)); // Just in case |
e858f9da |
491 | } |
492 | } |
493 | } |
f9903ed0 |
494 | |
faebaf0f |
495 | $newuser->username = $username; |
496 | $newuser->password = md5($password); |
a0bac19d |
497 | $newuser->lang = $CFG->lang; |
faebaf0f |
498 | $newuser->confirmed = 1; |
499 | $newuser->lastIP = $REMOTE_ADDR; |
500 | $newuser->timemodified = time(); |
f9903ed0 |
501 | |
faebaf0f |
502 | if (insert_record("user", $newuser)) { |
503 | return get_user_info_from_db("username", $username); |
504 | } |
505 | return false; |
506 | } |
507 | |
508 | function authenticate_user_login($username, $password) { |
9fa49e22 |
509 | /// Given a username and password, this function looks them |
510 | /// up using the currently selected authentication mechanism, |
511 | /// and if the authentication is successful, it returns a |
512 | /// valid $user object from the 'user' table. |
513 | /// |
514 | /// Uses auth_ functions from the currently active auth module |
faebaf0f |
515 | |
516 | global $CFG; |
517 | |
466558e3 |
518 | $md5password = md5($password); |
519 | |
14217044 |
520 | if (empty($CFG->auth)) { |
faebaf0f |
521 | $CFG->auth = "email"; // Default authentication module |
522 | } |
523 | |
466558e3 |
524 | if ($username == "guest") { |
525 | $CFG->auth = "none"; // Guest account always internal |
526 | } |
527 | |
528 | // If this is the admin, then just use internal methods |
92710226 |
529 | // Doing this first (even though it's less efficient) because |
530 | // the chosen authentication method might hang and lock the |
531 | // admin out. |
9fa49e22 |
532 | if (adminlogin($username, $md5password)) { |
466558e3 |
533 | return get_user_info_from_db("username", $username); |
534 | } |
535 | |
92710226 |
536 | // OK, the user is a normal user, so try and authenticate them |
e858f9da |
537 | require_once("$CFG->dirroot/auth/$CFG->auth/lib.php"); |
faebaf0f |
538 | |
539 | if (auth_user_login($username, $password)) { // Successful authentication |
540 | |
541 | if ($user = get_user_info_from_db("username", $username)) { |
92710226 |
542 | if ($md5password <> $user->password) { // Update local copy of password for reference |
466558e3 |
543 | set_field("user", "password", $md5password, "username", $username); |
faebaf0f |
544 | } |
545 | return $user; |
546 | |
547 | } else { |
548 | return create_user_record($username, $password); |
549 | } |
f9903ed0 |
550 | } |
89b54325 |
551 | |
faebaf0f |
552 | return false; |
f9903ed0 |
553 | } |
554 | |
faebaf0f |
555 | |
9fa49e22 |
556 | function enrol_student($user, $course) { |
557 | /// Enrols a student in a given course |
558 | global $db; |
f9903ed0 |
559 | |
ebc3bd2b |
560 | $record->userid = $user; |
9fa49e22 |
561 | $record->course = $course; |
562 | $record->start = 0; |
563 | $record->end = 0; |
564 | $record->time = time(); |
f9903ed0 |
565 | |
ebc3bd2b |
566 | return insert_record("user_students", $record); |
d7facad8 |
567 | } |
568 | |
9fa49e22 |
569 | function unenrol_student($user, $course=0) { |
570 | /// Unenrols a student from a given course |
571 | global $db; |
d7facad8 |
572 | |
9fa49e22 |
573 | if ($course) { |
574 | /// First delete any crucial stuff that might still send mail |
575 | if ($forums = get_records("forum", "course", $course)) { |
576 | foreach ($forums as $forum) { |
ebc3bd2b |
577 | delete_records("forum_subscriptions", "forum", $forum->id, "userid", $user); |
bb09fb11 |
578 | } |
f9903ed0 |
579 | } |
ebc3bd2b |
580 | return delete_records("user_students", "userid", $user, "course", $course); |
9fa49e22 |
581 | |
f9903ed0 |
582 | } else { |
ebc3bd2b |
583 | delete_records("forum_subscriptions", "userid", $user); |
584 | return delete_records("user_students", "userid", $user); |
f9903ed0 |
585 | } |
586 | } |
587 | |
9fa49e22 |
588 | function remove_teacher($user, $course=0) { |
589 | /// Removes a teacher from a given course (or ALL courses) |
590 | /// Does not delete the user account |
591 | global $db; |
57507290 |
592 | |
9fa49e22 |
593 | if ($course) { |
594 | /// First delete any crucial stuff that might still send mail |
595 | if ($forums = get_records("forum", "course", $course)) { |
596 | foreach ($forums as $forum) { |
ebc3bd2b |
597 | delete_records("forum_subscriptions", "forum", $forum->id, "userid", $user); |
9fa49e22 |
598 | } |
599 | } |
ebc3bd2b |
600 | return delete_records("user_teachers", "userid", $user, "course", $course); |
57507290 |
601 | } else { |
ebc3bd2b |
602 | delete_records("forum_subscriptions", "userid", $user); |
603 | return delete_records("user_teachers", "userid", $user); |
57507290 |
604 | } |
f9903ed0 |
605 | } |
606 | |
9fa49e22 |
607 | function remove_admin($user) { |
608 | /// Removes an admin from a site |
609 | global $db; |
f9903ed0 |
610 | |
ebc3bd2b |
611 | return delete_records("user_admins", "userid", $user); |
f9903ed0 |
612 | } |
613 | |
f9903ed0 |
614 | |
615 | |
f9903ed0 |
616 | /// CORRESPONDENCE //////////////////////////////////////////////// |
617 | |
5fa51a39 |
618 | function email_to_user($user, $from, $subject, $messagetext, $messagehtml="", $attachment="", $attachname="") { |
9fa49e22 |
619 | /// user - a user record as an object |
620 | /// from - a user record as an object |
621 | /// subject - plain text subject line of the email |
622 | /// messagetext - plain text version of the message |
623 | /// messagehtml - complete html version of the message (optional) |
624 | /// attachment - a file on the filesystem, relative to $CFG->dataroot |
625 | /// attachname - the name of the file (extension indicates MIME) |
f9903ed0 |
626 | |
4216daa6 |
627 | global $CFG, $_SERVER; |
f9903ed0 |
628 | |
136dabd8 |
629 | include_once("$CFG->libdir/phpmailer/class.phpmailer.php"); |
f9903ed0 |
630 | |
5fa51a39 |
631 | if (!$user) { |
f9903ed0 |
632 | return false; |
633 | } |
634 | |
f9903ed0 |
635 | $mail = new phpmailer; |
636 | |
72c578ca |
637 | $mail->Version = "Moodle $CFG->version"; // mailer version |
136dabd8 |
638 | $mail->PluginDir = "$CFG->libdir/phpmailer/"; // plugin directory (eg smtp plugin) |
562bbe90 |
639 | |
98c4eae3 |
640 | |
d483bcd3 |
641 | if (current_language() != "en") { |
642 | $mail->CharSet = get_string("thischarset"); |
98c4eae3 |
643 | } |
644 | |
7f86ce17 |
645 | if ($CFG->smtphosts) { |
1e411ffc |
646 | $mail->IsSMTP(); // use SMTP directly |
647 | $mail->Host = "$CFG->smtphosts"; // specify main and backup servers |
9f58537a |
648 | |
649 | if ($CFG->smtpuser) { // Use SMTP authentication |
650 | $mail->SMTPAuth = true; |
651 | $mail->Username = $CFG->smtpuser; |
652 | $mail->Password = $CFG->smtppass; |
653 | } |
7f86ce17 |
654 | } else { |
1e411ffc |
655 | $mail->IsMail(); // use PHP mail() = sendmail |
7f86ce17 |
656 | } |
f9903ed0 |
657 | |
136dabd8 |
658 | $mail->From = "$from->email"; |
659 | $mail->FromName = "$from->firstname $from->lastname"; |
660 | $mail->Subject = stripslashes($subject); |
f9903ed0 |
661 | |
6b174680 |
662 | $mail->AddAddress("$user->email", "$user->firstname $user->lastname"); |
f9903ed0 |
663 | |
f9903ed0 |
664 | $mail->WordWrap = 70; // set word wrap |
f9903ed0 |
665 | |
136dabd8 |
666 | if ($messagehtml) { |
667 | $mail->IsHTML(true); |
668 | $mail->Body = $messagehtml; |
78681899 |
669 | $mail->AltBody = "\n$messagetext\n"; |
136dabd8 |
670 | } else { |
671 | $mail->IsHTML(false); |
78681899 |
672 | $mail->Body = "\n$messagetext\n"; |
f9903ed0 |
673 | } |
674 | |
136dabd8 |
675 | if ($attachment && $attachname) { |
676 | if (ereg( "\\.\\." ,$attachment )) { // Security check for ".." in dir path |
4216daa6 |
677 | $adminuser = get_admin(); |
678 | $mail->AddAddress("$adminuser->email", "$adminuser->firstname $adminuser->lastname"); |
679 | $mail->AddStringAttachment("Error in attachment. User attempted to attach a filename with a unsafe name.", "error.txt", "8bit", "text/plain"); |
136dabd8 |
680 | } else { |
681 | include_once("$CFG->dirroot/files/mimetypes.php"); |
682 | $mimetype = mimeinfo("type", $attachname); |
683 | $mail->AddAttachment("$CFG->dataroot/$attachment", "$attachname", "base64", "$mimetype"); |
684 | } |
f9903ed0 |
685 | } |
686 | |
136dabd8 |
687 | if ($mail->Send()) { |
688 | return true; |
689 | } else { |
4216daa6 |
690 | echo "ERROR: $mail->ErrorInfo\n"; |
691 | $site = get_site(); |
692 | add_to_log($site->id, "library", "mailer", $_SERVER["REQUEST_URI"], "ERROR: $mail->ErrorInfo"); |
f9903ed0 |
693 | return false; |
694 | } |
f9903ed0 |
695 | } |
696 | |
1d881d92 |
697 | function reset_password_and_mail($user) { |
698 | |
699 | global $CFG; |
700 | |
701 | $site = get_site(); |
702 | $from = get_admin(); |
703 | |
704 | $newpassword = generate_password(); |
705 | |
706 | if (! set_field("user", "password", md5($newpassword), "id", $user->id) ) { |
707 | error("Could not set user password!"); |
708 | } |
709 | |
710 | $a->firstname = $user->firstname; |
711 | $a->sitename = $site->fullname; |
712 | $a->username = $user->username; |
713 | $a->newpassword = $newpassword; |
714 | $a->link = "$CFG->wwwroot/login/change_password.php"; |
715 | $a->signoff = "$from->firstname $from->lastname ($from->email)"; |
716 | |
717 | $message = get_string("newpasswordtext", "", $a); |
718 | |
719 | $subject = "$site->fullname: ".get_string("changedpassword"); |
720 | |
721 | return email_to_user($user, $from, $subject, $message); |
722 | |
723 | } |
724 | |
725 | function send_confirmation_email($user) { |
726 | |
727 | global $CFG; |
728 | |
729 | $site = get_site(); |
730 | $from = get_admin(); |
731 | |
732 | $data->firstname = $user->firstname; |
733 | $data->sitename = $site->fullname; |
734 | $data->link = "$CFG->wwwroot/login/confirm.php?p=$user->secret&s=$user->username"; |
735 | $data->admin = "$from->firstname $from->lastname ($from->email)"; |
736 | |
737 | $message = get_string("emailconfirmation", "", $data); |
738 | $subject = "$site->fullname account confirmation"; |
739 | |
740 | return email_to_user($user, $from, $subject, $message); |
741 | |
742 | } |
743 | |
744 | |
136dabd8 |
745 | |
f9903ed0 |
746 | /// FILE HANDLING ///////////////////////////////////////////// |
747 | |
6b174680 |
748 | function make_upload_directory($directory) { |
9fa49e22 |
749 | /// $directory = a string of directory names under $CFG->dataroot |
750 | /// eg stuff/assignment/1 |
751 | /// Returns full directory if successful, false if not |
6b174680 |
752 | |
753 | global $CFG; |
754 | |
755 | $currdir = $CFG->dataroot; |
fe287429 |
756 | |
2e6d4273 |
757 | umask(0000); |
758 | |
6b174680 |
759 | if (!file_exists($currdir)) { |
2e6d4273 |
760 | if (! mkdir($currdir, $CFG->directorypermissions)) { |
6b174680 |
761 | notify("ERROR: You need to create the directory $currdir with web server write access"); |
762 | return false; |
763 | } |
764 | } |
765 | |
766 | $dirarray = explode("/", $directory); |
767 | |
768 | foreach ($dirarray as $dir) { |
769 | $currdir = "$currdir/$dir"; |
770 | if (! file_exists($currdir)) { |
2e6d4273 |
771 | if (! mkdir($currdir, $CFG->directorypermissions)) { |
6b174680 |
772 | notify("ERROR: Could not find or create a directory ($currdir)"); |
773 | return false; |
774 | } |
775 | } |
776 | } |
777 | |
778 | return $currdir; |
779 | } |
780 | |
ca4f8eb8 |
781 | function make_mod_upload_directory($courseid) { |
9fa49e22 |
782 | /// Makes an upload directory for a particular module |
ca4f8eb8 |
783 | global $CFG; |
784 | |
785 | if (! $moddata = make_upload_directory("$courseid/$CFG->moddata")) { |
786 | return false; |
787 | } |
788 | |
789 | $strreadme = get_string("readme"); |
790 | |
791 | if (file_exists("$CFG->dirroot/lang/$CFG->lang/docs/module_files.txt")) { |
792 | copy("$CFG->dirroot/lang/$CFG->lang/docs/module_files.txt", "$moddata/$strreadme.txt"); |
793 | } else { |
794 | copy("$CFG->dirroot/lang/en/docs/module_files.txt", "$moddata/$strreadme.txt"); |
795 | } |
796 | return $moddata; |
797 | } |
798 | |
6b174680 |
799 | |
44e2d2bb |
800 | function valid_uploaded_file($newfile) { |
9fa49e22 |
801 | /// Returns current name of file on disk if true |
9c9f7d77 |
802 | if (empty($newfile)) { |
803 | return ""; |
804 | } |
44e2d2bb |
805 | if (is_uploaded_file($newfile['tmp_name']) and $newfile['size'] > 0) { |
806 | return $newfile['tmp_name']; |
807 | } else { |
808 | return ""; |
809 | } |
810 | } |
811 | |
812 | function get_max_upload_file_size() { |
9fa49e22 |
813 | /// Returns the maximum size for uploading files |
44e2d2bb |
814 | if (! $filesize = ini_get("upload_max_filesize")) { |
815 | $filesize = "5M"; |
816 | } |
817 | return get_real_size($filesize); |
818 | } |
819 | |
774ab660 |
820 | function get_directory_list($rootdir, $excludefile="", $descend=true) { |
9fa49e22 |
821 | /// Returns an array with all the filenames in |
822 | /// all subdirectories, relative to the given rootdir. |
823 | /// If excludefile is defined, then that file/directory is ignored |
f9903ed0 |
824 | |
825 | $dirs = array(); |
826 | |
44e2d2bb |
827 | $dir = opendir($rootdir); |
f9903ed0 |
828 | |
d897cae4 |
829 | if (!$dir) { |
830 | notify("Error: unable to read this directory! : $rootdir"); |
831 | return $dirs; |
832 | } |
833 | |
ca4f8eb8 |
834 | while ($file = readdir($dir)) { |
774ab660 |
835 | if ($file != "." and $file != ".." and $file != "CVS" and $file != $excludefile) { |
ca4f8eb8 |
836 | $fullfile = $rootdir."/".$file; |
774ab660 |
837 | if ($descend and filetype($fullfile) == "dir") { |
838 | $subdirs = get_directory_list($fullfile, $excludefile, $descend); |
f9903ed0 |
839 | foreach ($subdirs as $subdir) { |
840 | $dirs[] = $file."/".$subdir; |
841 | } |
842 | } else { |
843 | $dirs[] = $file; |
844 | } |
845 | } |
846 | } |
44e2d2bb |
847 | closedir($dir); |
f9903ed0 |
848 | |
774ab660 |
849 | asort($dirs); |
850 | |
f9903ed0 |
851 | return $dirs; |
852 | } |
853 | |
989bfa9d |
854 | function get_real_size($size=0) { |
9fa49e22 |
855 | /// Converts numbers like 10M into bytes |
989bfa9d |
856 | if (!$size) { |
857 | return 0; |
858 | } |
859 | $scan['MB'] = 1048576; |
64efda84 |
860 | $scan['Mb'] = 1048576; |
989bfa9d |
861 | $scan['M'] = 1048576; |
862 | $scan['KB'] = 1024; |
64efda84 |
863 | $scan['Kb'] = 1024; |
989bfa9d |
864 | $scan['K'] = 1024; |
865 | |
866 | while (list($key) = each($scan)) { |
867 | if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) { |
868 | $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key]; |
869 | break; |
870 | } |
871 | } |
872 | return $size; |
873 | } |
874 | |
44e2d2bb |
875 | function display_size($size) { |
9fa49e22 |
876 | /// Converts bytes into display form |
44e2d2bb |
877 | if ($size >= 1073741824) { |
878 | $size = round($size / 1073741824 * 10) / 10 . "Gb"; |
879 | } else if ($size >= 1048576) { |
880 | $size = round($size / 1048576 * 10) / 10 . "Mb"; |
881 | } else if ($size >= 1024) { |
882 | $size = round($size / 1024 * 10) / 10 . "Kb"; |
883 | } else { |
884 | $size = $size . "b"; |
885 | } |
886 | return $size; |
887 | } |
888 | |
6b174680 |
889 | function clean_filename($string) { |
9fa49e22 |
890 | /// Cleans a given filename by removing suspicious or troublesome characters |
fc05fccb |
891 | $string = stripslashes($string); |
6b174680 |
892 | $string = eregi_replace("\.\.", "", $string); |
893 | $string = eregi_replace("[^([:alnum:]|\.)]", "_", $string); |
894 | return eregi_replace("_+", "_", $string); |
895 | } |
896 | |
897 | |
1180c6dc |
898 | /// STRING TRANSLATION //////////////////////////////////////// |
899 | |
4bfa92e7 |
900 | function current_language() { |
9fa49e22 |
901 | /// Returns the code for the current language |
3db3acfb |
902 | global $CFG, $USER, $SESSION; |
4bfa92e7 |
903 | |
3db3acfb |
904 | if (isset($SESSION->lang)) { // Session language can override other settings |
905 | return $SESSION->lang; |
906 | |
907 | } else if (isset($USER->lang)) { // User language can override site language |
4bfa92e7 |
908 | return $USER->lang; |
3db3acfb |
909 | |
4bfa92e7 |
910 | } else { |
911 | return $CFG->lang; |
912 | } |
913 | } |
bcc83c41 |
914 | |
9fa49e22 |
915 | function print_string($identifier, $module="", $a=NULL) { |
916 | /// Given a string to translate - prints it out. |
917 | echo get_string($identifier, $module, $a); |
918 | } |
919 | |
a83fded1 |
920 | function get_string($identifier, $module="", $a=NULL) { |
9fa49e22 |
921 | /// Return the translated string specified by $identifier as |
922 | /// for $module. Uses the same format files as STphp. |
923 | /// $a is an object, string or number that can be used |
924 | /// within translation strings |
925 | /// |
926 | /// eg "hello \$a->firstname \$a->lastname" |
927 | /// or "hello \$a" |
1180c6dc |
928 | |
4bfa92e7 |
929 | global $CFG; |
1180c6dc |
930 | |
4bfa92e7 |
931 | $lang = current_language(); |
1180c6dc |
932 | |
058eec18 |
933 | if ($module == "") { |
934 | $module = "moodle"; |
1180c6dc |
935 | } |
936 | |
058eec18 |
937 | $langpath = "$CFG->dirroot/lang"; |
938 | $langfile = "$langpath/$lang/$module.php"; |
1180c6dc |
939 | |
940 | if (!file_exists($langfile)) { // try English instead |
058eec18 |
941 | $langfile = "$langpath/en/$module.php"; |
1180c6dc |
942 | if (!file_exists($langfile)) { |
058eec18 |
943 | return "ERROR: No lang file ($langpath/en/$module.php)!"; |
1180c6dc |
944 | } |
945 | } |
946 | |
947 | if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { |
948 | |
949 | eval($result); |
950 | return $resultstring; |
951 | |
952 | } else { |
953 | if ($lang == "en") { |
44e2d2bb |
954 | return "[['$identifier']]"; |
1180c6dc |
955 | |
956 | } else { // Try looking in the english file. |
058eec18 |
957 | $langfile = "$langpath/en/$module.php"; |
1180c6dc |
958 | if (!file_exists($langfile)) { |
058eec18 |
959 | return "ERROR: No lang file ($langpath/en/$module.php)!"; |
1180c6dc |
960 | } |
961 | if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { |
962 | eval($result); |
963 | return $resultstring; |
964 | } else { |
44e2d2bb |
965 | return "[['$identifier']]"; |
1180c6dc |
966 | } |
967 | } |
968 | } |
969 | } |
970 | |
971 | |
1180c6dc |
972 | function get_string_from_file($identifier, $langfile, $destination) { |
9fa49e22 |
973 | /// This function is only used from get_string(). |
1180c6dc |
974 | include ($langfile); |
975 | |
976 | if (!isset ($string[$identifier])) { |
977 | return false; |
978 | } |
979 | |
a83fded1 |
980 | return "$destination = sprintf(\"".$string[$identifier]."\");"; |
1180c6dc |
981 | } |
f9903ed0 |
982 | |
983 | |
1a72314d |
984 | function get_list_of_languages() { |
985 | /// Returns a list of language codes and their full names |
986 | global $CFG; |
987 | |
988 | if (!$langdirs = get_list_of_plugins("lang")) { |
989 | return false; |
990 | } |
991 | |
992 | foreach ($langdirs as $lang) { |
993 | include("$CFG->dirroot/lang/$lang/moodle.php"); |
994 | $languages[$lang] = $string["thislanguage"]." ($lang)"; |
995 | unset($string); |
996 | } |
997 | return $languages; |
998 | } |
999 | |
9bd2c874 |
1000 | function document_file($file, $include=true) { |
1001 | /// Can include a given document file (depends on second |
1002 | /// parameter) or just return info about it |
1003 | |
c9d4e6da |
1004 | global $CFG; |
9bd2c874 |
1005 | |
1006 | if (empty($file)) { |
1007 | echo "Error 404"; |
1008 | return false; |
1009 | } |
1010 | |
c9d4e6da |
1011 | $lang = current_language(); |
cae74366 |
1012 | |
9bd2c874 |
1013 | $file = clean_filename($file); |
1014 | |
1015 | $info->filepath = "$CFG->dirroot/lang/$lang/docs/$file"; |
1016 | $info->urlpath = "$CFG->wwwroot/lang/$lang/docs/$file"; |
1017 | |
1018 | if (!file_exists($info->filepath)) { |
1019 | $info->filepath = "$CFG->dirroot/lang/en/docs/$file"; |
1020 | $info->urlpath = "$CFG->wwwroot/lang/en/docs/$file"; |
0c106cd3 |
1021 | if (!file_exists($info->filepath)) { |
1022 | error("Error 404 - $file does not exist"); |
1023 | return NULL; |
1024 | } |
9bd2c874 |
1025 | } |
1026 | |
1027 | if ($include) { |
1028 | include($info->filepath); |
1029 | } |
1030 | |
1031 | return $info; |
1032 | } |
1033 | |
1a72314d |
1034 | |
f9903ed0 |
1035 | /// ENCRYPTION //////////////////////////////////////////////// |
1036 | |
1037 | function rc4encrypt($data) { |
1038 | $password = "nfgjeingjk"; |
1039 | return endecrypt($password, $data, ""); |
1040 | } |
1041 | |
1042 | function rc4decrypt($data) { |
1043 | $password = "nfgjeingjk"; |
1044 | return endecrypt($password, $data, "de"); |
1045 | } |
1046 | |
1047 | function endecrypt ($pwd, $data, $case) { |
9fa49e22 |
1048 | /// Based on a class by Mukul Sabharwal [mukulsabharwal@yahoo.com] |
f9903ed0 |
1049 | |
1050 | if ($case == 'de') { |
1051 | $data = urldecode($data); |
1052 | } |
1053 | |
1054 | $key[] = ""; |
1055 | $box[] = ""; |
1056 | $temp_swap = ""; |
1057 | $pwd_length = 0; |
1058 | |
1059 | $pwd_length = strlen($pwd); |
1060 | |
1061 | for ($i = 0; $i <= 255; $i++) { |
1062 | $key[$i] = ord(substr($pwd, ($i % $pwd_length), 1)); |
1063 | $box[$i] = $i; |
1064 | } |
1065 | |
1066 | $x = 0; |
1067 | |
1068 | for ($i = 0; $i <= 255; $i++) { |
1069 | $x = ($x + $box[$i] + $key[$i]) % 256; |
1070 | $temp_swap = $box[$i]; |
1071 | $box[$i] = $box[$x]; |
1072 | $box[$x] = $temp_swap; |
1073 | } |
1074 | |
1075 | $temp = ""; |
1076 | $k = ""; |
1077 | |
1078 | $cipherby = ""; |
1079 | $cipher = ""; |
1080 | |
1081 | $a = 0; |
1082 | $j = 0; |
1083 | |
1084 | for ($i = 0; $i < strlen($data); $i++) { |
1085 | $a = ($a + 1) % 256; |
1086 | $j = ($j + $box[$a]) % 256; |
1087 | $temp = $box[$a]; |
1088 | $box[$a] = $box[$j]; |
1089 | $box[$j] = $temp; |
1090 | $k = $box[(($box[$a] + $box[$j]) % 256)]; |
1091 | $cipherby = ord(substr($data, $i, 1)) ^ $k; |
1092 | $cipher .= chr($cipherby); |
1093 | } |
1094 | |
1095 | if ($case == 'de') { |
1096 | $cipher = urldecode(urlencode($cipher)); |
1097 | } else { |
1098 | $cipher = urlencode($cipher); |
1099 | } |
1100 | |
1101 | return $cipher; |
1102 | } |
1103 | |
1104 | |
9fa49e22 |
1105 | /// ENVIRONMENT CHECKING //////////////////////////////////////////////////////////// |
1e3e716f |
1106 | |
1d881d92 |
1107 | function get_list_of_plugins($plugin="mod") { |
1108 | /// Lists plugin directories within some directory |
1109 | |
1110 | global $CFG; |
1111 | |
1112 | $basedir = opendir("$CFG->dirroot/$plugin"); |
1113 | while ($dir = readdir($basedir)) { |
1114 | if ($dir == "." || $dir == ".." || $dir == "CVS") { |
1115 | continue; |
1116 | } |
1117 | if (filetype("$CFG->dirroot/$plugin/$dir") != "dir") { |
1118 | continue; |
1119 | } |
1120 | $plugins[] = $dir; |
1121 | } |
1122 | if ($plugins) { |
1123 | asort($plugins); |
1124 | } |
1125 | return $plugins; |
1126 | } |
1127 | |
b0cb5e22 |
1128 | function check_php_version($version="4.1.0") { |
9fa49e22 |
1129 | /// Returns true is the current version of PHP is greater that the specified one |
b0cb5e22 |
1130 | $minversion = intval(str_replace(".", "", $version)); |
1131 | $curversion = intval(str_replace(".", "", phpversion())); |
1132 | return ($curversion >= $minversion); |
1133 | } |
1134 | |
0095d5cd |
1135 | function check_browser_version($brand="MSIE", $version=5.5) { |
9fa49e22 |
1136 | /// Checks to see if is a browser matches the specified |
1137 | /// brand and is equal or better version. |
0095d5cd |
1138 | |
607809b3 |
1139 | if (empty($_SERVER["HTTP_USER_AGENT"])) { |
0095d5cd |
1140 | return false; |
1141 | } |
607809b3 |
1142 | $string = explode(";", $_SERVER["HTTP_USER_AGENT"]); |
0095d5cd |
1143 | if (!isset($string[1])) { |
1144 | return false; |
1145 | } |
1146 | $string = explode(" ", trim($string[1])); |
1147 | if (!isset($string[0]) and !isset($string[1])) { |
1148 | return false; |
1149 | } |
1150 | if ($string[0] == $brand and (float)$string[1] >= $version ) { |
1151 | return true; |
1152 | } |
1153 | return false; |
1154 | } |
1155 | |
1156 | function can_use_richtext_editor() { |
9fa49e22 |
1157 | /// Is the richedit editor enabled? |
0095d5cd |
1158 | global $USER, $CFG; |
ce78926d |
1159 | if (!empty($USER->htmleditor) and !empty($CFG->htmleditor)) { |
7ce20f09 |
1160 | return check_browser_version("MSIE", 5.5); |
1161 | } |
1162 | return false; |
0095d5cd |
1163 | } |
1164 | |
74944b73 |
1165 | function check_gd_version() { |
9fa49e22 |
1166 | /// Hack to find out the GD version by parsing phpinfo output |
74944b73 |
1167 | ob_start(); |
a3e175ee |
1168 | phpinfo(8); |
74944b73 |
1169 | $phpinfo = ob_get_contents(); |
1170 | ob_end_clean(); |
1171 | |
1172 | $phpinfo = explode("\n",$phpinfo); |
1173 | |
1174 | $gdversion = 0; |
1175 | |
92a4b0f1 |
1176 | |
74944b73 |
1177 | foreach ($phpinfo as $text) { |
92a4b0f1 |
1178 | $parts = explode('</td>',$text); |
74944b73 |
1179 | foreach ($parts as $key => $val) { |
92a4b0f1 |
1180 | $parts[$key] = trim(strip_tags($val)); |
74944b73 |
1181 | } |
92a4b0f1 |
1182 | if ($parts[0] == "GD Version") { |
1183 | if (substr_count($parts[1], "2.0")) { |
1184 | $parts[1] = "2.0"; |
1185 | } |
74944b73 |
1186 | $gdversion = intval($parts[1]); |
1187 | } |
1188 | } |
1189 | |
1190 | return $gdversion; // 1, 2 or 0 |
1191 | } |
f9903ed0 |
1192 | |
0095d5cd |
1193 | |
9fa49e22 |
1194 | function moodle_needs_upgrading() { |
1195 | /// Checks version numbers of Main code and all modules to see |
1196 | /// if there are any mismatches ... returns true or false |
1197 | global $CFG; |
1198 | |
1199 | include_once("$CFG->dirroot/version.php"); # defines $version and upgrades |
1200 | if ($CFG->version) { |
1201 | if ($version > $CFG->version) { |
1202 | return true; |
1203 | } |
1204 | if ($mods = get_list_of_plugins("mod")) { |
1205 | foreach ($mods as $mod) { |
1206 | $fullmod = "$CFG->dirroot/mod/$mod"; |
1207 | unset($module); |
1079c8a8 |
1208 | if (!is_readable("$fullmod/version.php")) { |
1209 | notify("Module '$mod' is not readable - check permissions"); |
1210 | continue; |
1211 | } |
9fa49e22 |
1212 | include_once("$fullmod/version.php"); # defines $module with version etc |
1213 | if ($currmodule = get_record("modules", "name", $mod)) { |
1214 | if ($module->version > $currmodule->version) { |
1215 | return true; |
1216 | } |
1217 | } |
1218 | } |
1219 | } |
1220 | } else { |
1221 | return true; |
1222 | } |
1223 | return false; |
1224 | } |
1225 | |
1226 | |
1227 | /// MISCELLANEOUS //////////////////////////////////////////////////////////////////// |
1228 | |
1229 | function count_words($string) { |
1230 | /// Words are defined as things between whitespace |
1231 | $string = strip_tags($string); |
1232 | return count(preg_split("/\w\b/", $string)) - 1; |
1233 | } |
1234 | |
1d881d92 |
1235 | function random_string ($length=15) { |
1236 | $pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
1237 | $pool .= "abcdefghijklmnopqrstuvwxyz"; |
1238 | $pool .= "0123456789"; |
1239 | $poollen = strlen($pool); |
1240 | mt_srand ((double) microtime() * 1000000); |
1241 | $string = ""; |
1242 | for ($i = 0; $i < $length; $i++) { |
1243 | $string .= substr($pool, (mt_rand()%($poollen)), 1); |
1244 | } |
1245 | return $string; |
1246 | } |
1247 | |
1248 | |
9fa49e22 |
1249 | function getweek ($startdate, $thedate) { |
1250 | /// Given dates in seconds, how many weeks is the date from startdate |
1251 | /// The first week is 1, the second 2 etc ... |
1252 | |
1253 | if ($thedate < $startdate) { // error |
1254 | return 0; |
1255 | } |
1256 | |
1257 | return floor(($thedate - $startdate) / 604800.0) + 1; |
1258 | } |
1259 | |
1260 | function generate_password($maxlen=10) { |
1261 | /// returns a randomly generated password of length $maxlen. inspired by |
1262 | /// http://www.phpbuilder.com/columns/jesus19990502.php3 |
1263 | |
1264 | global $CFG; |
1265 | |
1266 | $fillers = "1234567890!$-+"; |
1267 | $wordlist = file($CFG->wordlist); |
1268 | |
1269 | srand((double) microtime() * 1000000); |
1270 | $word1 = trim($wordlist[rand(0, count($wordlist) - 1)]); |
1271 | $word2 = trim($wordlist[rand(0, count($wordlist) - 1)]); |
1272 | $filler1 = $fillers[rand(0, strlen($fillers) - 1)]; |
1273 | |
1274 | return substr($word1 . $filler1 . $word2, 0, $maxlen); |
1275 | } |
1276 | |
1277 | function format_float($num, $places=0) { |
1278 | /// Given a float, prints it nicely |
1279 | return sprintf("%.$places"."f", $num); |
1280 | } |
1281 | |
1282 | |
0095d5cd |
1283 | |
f9903ed0 |
1284 | ?> |