2001-11-22 06:23:56 +00:00
|
|
|
<?PHP // $Id$
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// //
|
|
|
|
// moodlelib.php //
|
|
|
|
// //
|
|
|
|
// Main library file of miscellaneous general-purpose Moodle functions //
|
|
|
|
// //
|
|
|
|
// Other main libraries: //
|
|
|
|
// //
|
|
|
|
// weblib.php - functions that produce web output //
|
|
|
|
// datalib.php - functions that access the database //
|
|
|
|
// //
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// //
|
|
|
|
// NOTICE OF COPYRIGHT //
|
|
|
|
// //
|
|
|
|
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
|
2004-03-10 13:23:00 +00:00
|
|
|
// http://moodle.org //
|
2002-12-20 14:44:14 +00:00
|
|
|
// //
|
2004-03-10 13:23:00 +00:00
|
|
|
// Copyright (C) 1999-2004 Martin Dougiamas http://dougiamas.com //
|
2002-12-20 14:44:14 +00:00
|
|
|
// //
|
|
|
|
// This program is free software; you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or //
|
|
|
|
// (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// This program is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License for more details: //
|
|
|
|
// //
|
|
|
|
// http://www.gnu.org/copyleft/gpl.html //
|
|
|
|
// //
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2002-11-06 07:54:44 +00:00
|
|
|
|
2003-12-30 17:18:06 +00:00
|
|
|
/// CONSTANTS /////////////////////////////////////////////////////////////
|
|
|
|
|
2004-04-01 10:11:20 +00:00
|
|
|
define('NOGROUPS', 0);
|
2003-12-30 17:18:06 +00:00
|
|
|
define('SEPARATEGROUPS', 1);
|
|
|
|
define('VISIBLEGROUPS', 2);
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
/// PARAMETER HANDLING ////////////////////////////////////////////////////
|
2002-08-04 16:20:30 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function require_variable($var) {
|
|
|
|
/// Variable must be present
|
|
|
|
if (! isset($var)) {
|
|
|
|
error("A required parameter was missing");
|
2002-08-04 16:20:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function optional_variable(&$var, $default=0) {
|
|
|
|
/// Variable may be present, if not then set a default
|
|
|
|
if (! isset($var)) {
|
|
|
|
$var = $default;
|
2002-08-04 16:20:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function set_config($name, $value) {
|
|
|
|
/// No need for get_config because they are usually always available in $CFG
|
2004-01-28 14:14:19 +00:00
|
|
|
|
2003-08-18 17:41:06 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$CFG->$name = $value; // So it's defined for this invocation at least
|
2002-11-10 08:43:44 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
if (get_field("config", "name", "name", $name)) {
|
|
|
|
return set_field("config", "value", $value, "name", $name);
|
2002-11-10 07:37:15 +00:00
|
|
|
} else {
|
2002-12-20 14:44:14 +00:00
|
|
|
$config->name = $name;
|
|
|
|
$config->value = $value;
|
|
|
|
return insert_record("config", $config);
|
2002-07-29 06:21:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-28 14:14:19 +00:00
|
|
|
function reload_user_preferences() {
|
|
|
|
/// Refresh current USER with all their current preferences
|
|
|
|
|
|
|
|
global $USER;
|
|
|
|
|
2004-04-01 10:11:20 +00:00
|
|
|
unset($USER->preference);
|
2004-01-28 14:14:19 +00:00
|
|
|
|
|
|
|
if ($preferences = get_records('user_preferences', 'userid', $USER->id)) {
|
|
|
|
foreach ($preferences as $preference) {
|
|
|
|
$USER->preference[$preference->name] = $preference->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_user_preference($name, $value) {
|
|
|
|
/// Sets a preference for the current user
|
|
|
|
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if (empty($name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($preference = get_record('user_preferences', 'userid', $USER->id, 'name', $name)) {
|
2004-01-28 14:47:39 +00:00
|
|
|
if (set_field("user_preferences", "value", $value, "id", $preference->id)) {
|
|
|
|
$USER->preference[$name] = $value;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2004-01-28 14:14:19 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
$preference->userid = $USER->id;
|
|
|
|
$preference->name = $name;
|
|
|
|
$preference->value = (string)$value;
|
2004-01-28 14:47:39 +00:00
|
|
|
if (insert_record('user_preferences', $preference)) {
|
2004-01-28 14:14:19 +00:00
|
|
|
$USER->preference[$name] = $value;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_user_preferences($prefarray) {
|
|
|
|
/// Sets a whole array of preferences for the current user
|
|
|
|
|
|
|
|
if (!is_array($prefarray) or empty($prefarray)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$return = true;
|
|
|
|
foreach ($prefarray as $name => $value) {
|
|
|
|
// The order is important; if the test for return is done first,
|
|
|
|
// then if one function call fails all the remaining ones will
|
|
|
|
// be "optimized away"
|
|
|
|
$return = set_user_preference($name, $value) and $return;
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_user_preferences($name=NULL, $default=NULL) {
|
|
|
|
/// Without arguments, returns all the current user preferences
|
2004-04-01 10:11:20 +00:00
|
|
|
/// as an array. If a name is specified, then this function
|
|
|
|
/// attempts to return that particular preference value. If
|
2004-01-28 14:14:19 +00:00
|
|
|
/// none is found, then the optional value $default is returned,
|
|
|
|
/// otherwise NULL.
|
|
|
|
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if (empty($USER->preference)) {
|
|
|
|
return $default; // Default value (or NULL)
|
|
|
|
}
|
|
|
|
if (empty($name)) {
|
|
|
|
return $USER->preference; // Whole array
|
|
|
|
}
|
|
|
|
if (!isset($USER->preference[$name])) {
|
|
|
|
return $default; // Default value (or NULL)
|
|
|
|
}
|
|
|
|
return $USER->preference[$name]; // The single value
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
/// FUNCTIONS FOR HANDLING TIME ////////////////////////////////////////////
|
2002-07-29 06:21:37 +00:00
|
|
|
|
2003-01-10 05:41:20 +00:00
|
|
|
function make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0, $timezone=99) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Given date parts in user time, produce a GMT timestamp
|
2002-07-29 06:21:37 +00:00
|
|
|
|
2004-04-01 12:27:37 +00:00
|
|
|
$timezone = get_user_timezone($timezone);
|
2003-01-13 02:42:57 +00:00
|
|
|
|
|
|
|
if (abs($timezone) > 13) {
|
2003-01-13 02:37:47 +00:00
|
|
|
return mktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year);
|
|
|
|
} else {
|
|
|
|
$time = gmmktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year);
|
|
|
|
return usertime($time, $timezone); // This is GMT
|
|
|
|
}
|
2002-07-29 06:21:37 +00:00
|
|
|
}
|
|
|
|
|
2002-10-03 04:04:59 +00:00
|
|
|
function format_time($totalsecs, $str=NULL) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Given an amount of time in seconds, returns string
|
|
|
|
/// formatted nicely as months, days, hours etc as needed
|
2002-06-08 06:47:33 +00:00
|
|
|
|
2002-08-04 16:20:30 +00:00
|
|
|
$totalsecs = abs($totalsecs);
|
2002-06-08 06:47:33 +00:00
|
|
|
|
2002-10-03 04:04:59 +00:00
|
|
|
if (!$str) { // Create the str structure the slow way
|
|
|
|
$str->day = get_string("day");
|
|
|
|
$str->days = get_string("days");
|
|
|
|
$str->hour = get_string("hour");
|
|
|
|
$str->hours = get_string("hours");
|
|
|
|
$str->min = get_string("min");
|
|
|
|
$str->mins = get_string("mins");
|
|
|
|
$str->sec = get_string("sec");
|
|
|
|
$str->secs = get_string("secs");
|
|
|
|
}
|
|
|
|
|
|
|
|
$days = floor($totalsecs/86400);
|
2002-08-04 16:20:30 +00:00
|
|
|
$remainder = $totalsecs - ($days*86400);
|
2002-10-03 04:04:59 +00:00
|
|
|
$hours = floor($remainder/3600);
|
2002-08-04 16:20:30 +00:00
|
|
|
$remainder = $remainder - ($hours*3600);
|
2002-10-03 04:04:59 +00:00
|
|
|
$mins = floor($remainder/60);
|
|
|
|
$secs = $remainder - ($mins*60);
|
|
|
|
|
|
|
|
$ss = ($secs == 1) ? $str->sec : $str->secs;
|
|
|
|
$sm = ($mins == 1) ? $str->min : $str->mins;
|
|
|
|
$sh = ($hours == 1) ? $str->hour : $str->hours;
|
|
|
|
$sd = ($days == 1) ? $str->day : $str->days;
|
|
|
|
|
2002-12-29 17:32:32 +00:00
|
|
|
$odays = "";
|
|
|
|
$ohours = "";
|
|
|
|
$omins = "";
|
|
|
|
$osecs = "";
|
|
|
|
|
2002-10-03 04:04:59 +00:00
|
|
|
if ($days) $odays = "$days $sd";
|
|
|
|
if ($hours) $ohours = "$hours $sh";
|
|
|
|
if ($mins) $omins = "$mins $sm";
|
|
|
|
if ($secs) $osecs = "$secs $ss";
|
2002-08-04 16:20:30 +00:00
|
|
|
|
|
|
|
if ($days) return "$odays $ohours";
|
|
|
|
if ($hours) return "$ohours $omins";
|
|
|
|
if ($mins) return "$omins $osecs";
|
|
|
|
if ($secs) return "$osecs";
|
|
|
|
return get_string("now");
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2003-05-02 09:37:41 +00:00
|
|
|
function userdate($date, $format="", $timezone=99, $fixday = true) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Returns a formatted string that represents a date in user time
|
|
|
|
/// WARNING: note that the format is for strftime(), not date().
|
2004-04-01 10:11:20 +00:00
|
|
|
/// Because of a bug in most Windows time libraries, we can't use
|
2002-12-20 14:44:14 +00:00
|
|
|
/// the nicer %e, so we have to use %d which has leading zeroes.
|
2004-04-01 10:11:20 +00:00
|
|
|
/// A lot of the fuss below is just getting rid of these leading
|
2002-12-20 14:44:14 +00:00
|
|
|
/// zeroes as efficiently as possible.
|
2003-05-02 09:37:41 +00:00
|
|
|
///
|
2004-04-01 10:11:20 +00:00
|
|
|
/// If parammeter fixday = true (default), then take off leading
|
2003-05-02 09:37:41 +00:00
|
|
|
/// zero from %d, else mantain it.
|
2002-07-02 07:02:28 +00:00
|
|
|
|
2002-06-05 14:05:59 +00:00
|
|
|
if ($format == "") {
|
2003-01-15 10:55:54 +00:00
|
|
|
$format = get_string("strftimedaydatetime");
|
2002-06-05 14:05:59 +00:00
|
|
|
}
|
2002-12-02 01:35:18 +00:00
|
|
|
|
2003-01-15 10:55:54 +00:00
|
|
|
$formatnoday = str_replace("%d", "DD", $format);
|
2003-05-02 09:37:41 +00:00
|
|
|
if ($fixday) {
|
|
|
|
$fixday = ($formatnoday != $format);
|
|
|
|
}
|
2003-01-15 10:55:54 +00:00
|
|
|
|
2004-04-01 12:27:37 +00:00
|
|
|
$timezone = get_user_timezone($timezone);
|
2004-02-12 05:55:04 +00:00
|
|
|
|
2002-11-15 02:46:48 +00:00
|
|
|
if (abs($timezone) > 13) {
|
2002-12-02 01:35:18 +00:00
|
|
|
if ($fixday) {
|
|
|
|
$datestring = strftime($formatnoday, $date);
|
|
|
|
$daystring = str_replace(" 0", "", strftime(" %d", $date));
|
|
|
|
$datestring = str_replace("DD", $daystring, $datestring);
|
|
|
|
} else {
|
|
|
|
$datestring = strftime($format, $date);
|
|
|
|
}
|
2002-12-02 01:13:14 +00:00
|
|
|
} else {
|
2003-01-03 16:58:37 +00:00
|
|
|
$date = $date + (int)($timezone * 3600);
|
2002-12-02 01:35:18 +00:00
|
|
|
if ($fixday) {
|
2003-01-03 16:58:37 +00:00
|
|
|
$datestring = gmstrftime($formatnoday, $date);
|
2002-12-20 14:44:14 +00:00
|
|
|
$daystring = str_replace(" 0", "", gmstrftime(" %d", $date));
|
2002-12-02 01:35:18 +00:00
|
|
|
$datestring = str_replace("DD", $daystring, $datestring);
|
|
|
|
} else {
|
2003-01-03 16:58:37 +00:00
|
|
|
$datestring = gmstrftime($format, $date);
|
2002-12-02 01:35:18 +00:00
|
|
|
}
|
2002-06-05 03:15:30 +00:00
|
|
|
}
|
2002-12-02 01:13:14 +00:00
|
|
|
|
2002-12-02 01:35:18 +00:00
|
|
|
return $datestring;
|
2002-06-05 03:15:30 +00:00
|
|
|
}
|
|
|
|
|
2002-06-05 14:05:59 +00:00
|
|
|
function usergetdate($date, $timezone=99) {
|
2004-04-01 10:11:20 +00:00
|
|
|
/// Given a $date timestamp in GMT, returns an array
|
2002-12-20 14:44:14 +00:00
|
|
|
/// that represents the date in user time
|
2002-08-04 16:20:30 +00:00
|
|
|
|
2004-04-01 12:27:37 +00:00
|
|
|
$timezone = get_user_timezone($timezone);
|
2004-04-01 10:39:06 +00:00
|
|
|
|
2002-11-15 02:46:48 +00:00
|
|
|
if (abs($timezone) > 13) {
|
2002-06-05 03:15:30 +00:00
|
|
|
return getdate($date);
|
|
|
|
}
|
2002-06-11 04:04:45 +00:00
|
|
|
//There is no gmgetdate so I have to fake it...
|
|
|
|
$date = $date + (int)($timezone * 3600);
|
|
|
|
$getdate["seconds"] = gmstrftime("%S", $date);
|
|
|
|
$getdate["minutes"] = gmstrftime("%M", $date);
|
|
|
|
$getdate["hours"] = gmstrftime("%H", $date);
|
|
|
|
$getdate["mday"] = gmstrftime("%d", $date);
|
|
|
|
$getdate["wday"] = gmstrftime("%u", $date);
|
|
|
|
$getdate["mon"] = gmstrftime("%m", $date);
|
|
|
|
$getdate["year"] = gmstrftime("%Y", $date);
|
|
|
|
$getdate["yday"] = gmstrftime("%j", $date);
|
|
|
|
$getdate["weekday"] = gmstrftime("%A", $date);
|
|
|
|
$getdate["month"] = gmstrftime("%B", $date);
|
|
|
|
return $getdate;
|
2002-06-10 09:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function usertime($date, $timezone=99) {
|
2004-04-01 10:11:20 +00:00
|
|
|
/// Given a GMT timestamp (seconds since epoch), offsets it by
|
2002-12-20 14:44:14 +00:00
|
|
|
/// the timezone. eg 3pm in India is 3pm GMT - 7 * 3600 seconds
|
2004-04-01 10:39:06 +00:00
|
|
|
|
2004-04-01 12:27:37 +00:00
|
|
|
$timezone = get_user_timezone($timezone);
|
2002-11-15 02:46:48 +00:00
|
|
|
if (abs($timezone) > 13) {
|
2002-06-10 09:43:40 +00:00
|
|
|
return $date;
|
|
|
|
}
|
|
|
|
return $date - (int)($timezone * 3600);
|
|
|
|
}
|
|
|
|
|
2002-06-10 14:01:30 +00:00
|
|
|
function usergetmidnight($date, $timezone=99) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Given a time, return the GMT timestamp of the most recent midnight
|
|
|
|
/// for the current user.
|
2002-06-10 14:01:30 +00:00
|
|
|
|
2004-04-01 12:27:37 +00:00
|
|
|
$timezone = get_user_timezone($timezone);
|
2002-06-10 14:01:30 +00:00
|
|
|
$userdate = usergetdate($date, $timezone);
|
2002-08-08 17:28:30 +00:00
|
|
|
|
2002-11-15 02:46:48 +00:00
|
|
|
if (abs($timezone) > 13) {
|
2002-08-08 17:28:30 +00:00
|
|
|
return mktime(0, 0, 0, $userdate["mon"], $userdate["mday"], $userdate["year"]);
|
|
|
|
}
|
|
|
|
|
2002-06-10 14:01:30 +00:00
|
|
|
$timemidnight = gmmktime (0, 0, 0, $userdate["mon"], $userdate["mday"], $userdate["year"]);
|
|
|
|
return usertime($timemidnight, $timezone); // Time of midnight of this user's day, in GMT
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-06-10 09:43:40 +00:00
|
|
|
function usertimezone($timezone=99) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Returns a string that prints the user's timezone
|
2002-06-10 09:43:40 +00:00
|
|
|
|
2004-04-01 12:27:37 +00:00
|
|
|
$timezone = get_user_timezone($timezone);
|
|
|
|
|
2002-11-15 02:46:48 +00:00
|
|
|
if (abs($timezone) > 13) {
|
2002-06-10 09:43:40 +00:00
|
|
|
return "server time";
|
|
|
|
}
|
|
|
|
if (abs($timezone) < 0.5) {
|
|
|
|
return "GMT";
|
|
|
|
}
|
|
|
|
if ($timezone > 0) {
|
|
|
|
return "GMT+$timezone";
|
|
|
|
} else {
|
|
|
|
return "GMT$timezone";
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2004-04-01 12:27:37 +00:00
|
|
|
function get_user_timezone($tz = 99) {
|
|
|
|
// Returns a float which represents the user's timezone difference from GMT in hours
|
|
|
|
// Checks various settings and picks the most dominant of those which have a value
|
|
|
|
|
|
|
|
// Variables declared explicitly global here so that if we add
|
|
|
|
// something later we won't forget to global it...
|
|
|
|
$timezones = array(
|
|
|
|
isset($GLOBALS['USER']->timezone) ? $GLOBALS['USER']->timezone : 99,
|
|
|
|
isset($GLOBALS['CFG']->timezone) ? $GLOBALS['CFG']->timezone : 99,
|
|
|
|
);
|
|
|
|
while($tz == 99 && $next = each($timezones)) {
|
|
|
|
$tz = (float)$next['value'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tz;
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
/// USER AUTHENTICATION AND LOGIN ////////////////////////////////////////
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-07-01 22:29:04 +00:00
|
|
|
function require_login($courseid=0, $autologinguest=true) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// This function checks that the current user is logged in, and optionally
|
|
|
|
/// whether they are "logged in" or allowed to be in a particular course.
|
|
|
|
/// If not, then it redirects them to the site login or course enrolment.
|
2004-07-01 22:29:04 +00:00
|
|
|
/// $autologinguest determines whether visitors should automatically be
|
|
|
|
/// logged in as guests provide $CFG->autologinguests is set to 1
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2003-08-16 05:19:24 +00:00
|
|
|
global $CFG, $SESSION, $USER, $FULLME, $MoodleSession;
|
2004-04-01 10:11:20 +00:00
|
|
|
|
2002-06-09 14:14:07 +00:00
|
|
|
// First check that the user is logged in to the site.
|
2002-08-28 15:07:46 +00:00
|
|
|
if (! (isset($USER->loggedin) and $USER->confirmed and ($USER->site == $CFG->wwwroot)) ) { // They're not
|
2001-11-22 06:23:56 +00:00
|
|
|
$SESSION->wantsurl = $FULLME;
|
2003-01-22 01:55:54 +00:00
|
|
|
if (!empty($_SERVER["HTTP_REFERER"])) {
|
|
|
|
$SESSION->fromurl = $_SERVER["HTTP_REFERER"];
|
|
|
|
}
|
2002-08-28 15:07:46 +00:00
|
|
|
$USER = NULL;
|
2004-07-01 22:29:04 +00:00
|
|
|
if ($autologinguest and $CFG->autologinguests and $courseid and get_field('course','guest','id',$courseid)) {
|
|
|
|
$loginguest = '?loginguest=true';
|
|
|
|
} else {
|
|
|
|
$loginguest = '';
|
2004-06-30 12:43:05 +00:00
|
|
|
}
|
2004-06-19 16:13:28 +00:00
|
|
|
if (empty($CFG->loginhttps)) {
|
2004-06-30 12:09:29 +00:00
|
|
|
redirect("$CFG->wwwroot/login/index.php$loginguest");
|
2004-06-19 16:13:28 +00:00
|
|
|
} else {
|
|
|
|
$wwwroot = str_replace('http','https',$CFG->wwwroot);
|
2004-06-30 12:09:29 +00:00
|
|
|
redirect("$wwwroot/login/index.php$loginguest");
|
2004-06-19 16:13:28 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
die;
|
|
|
|
}
|
2002-12-30 15:30:05 +00:00
|
|
|
|
|
|
|
// Check that the user account is properly set up
|
|
|
|
if (user_not_fully_set_up($USER)) {
|
2004-08-29 14:15:40 +00:00
|
|
|
redirect("$CFG->wwwroot/user/edit.php?id=$USER->id&course=SITEID");
|
2002-12-30 15:30:05 +00:00
|
|
|
die;
|
|
|
|
}
|
2004-04-01 10:11:20 +00:00
|
|
|
|
2002-06-09 14:14:07 +00:00
|
|
|
// Next, check if the user can be in a particular course
|
|
|
|
if ($courseid) {
|
2002-12-29 17:32:32 +00:00
|
|
|
if (!empty($USER->student[$courseid]) or !empty($USER->teacher[$courseid]) or !empty($USER->admin)) {
|
2002-11-05 16:38:02 +00:00
|
|
|
if (isset($USER->realuser)) { // Make sure the REAL person can also access this course
|
|
|
|
if (!isteacher($courseid, $USER->realuser)) {
|
|
|
|
print_header();
|
2004-05-06 03:11:15 +00:00
|
|
|
notice(get_string("studentnotallowed", "", fullname($USER, true)), "$CFG->wwwroot/");
|
2002-11-05 16:38:02 +00:00
|
|
|
}
|
2002-08-11 05:07:15 +00:00
|
|
|
}
|
2002-06-09 14:14:07 +00:00
|
|
|
return; // user is a member of this course.
|
|
|
|
}
|
|
|
|
if (! $course = get_record("course", "id", $courseid)) {
|
|
|
|
error("That course doesn't exist");
|
|
|
|
}
|
2003-07-21 07:50:59 +00:00
|
|
|
if (!$course->visible) {
|
|
|
|
print_header();
|
2004-05-06 03:11:15 +00:00
|
|
|
notice(get_string("studentnotallowed", "", fullname($USER, true)), "$CFG->wwwroot/");
|
2003-07-21 07:50:59 +00:00
|
|
|
}
|
2002-08-21 12:54:27 +00:00
|
|
|
if ($USER->username == "guest") {
|
|
|
|
switch ($course->guest) {
|
|
|
|
case 0: // Guests not allowed
|
|
|
|
print_header();
|
|
|
|
notice(get_string("guestsnotallowed", "", $course->fullname));
|
|
|
|
break;
|
|
|
|
case 1: // Guests allowed
|
|
|
|
return;
|
|
|
|
case 2: // Guests allowed with key (drop through)
|
|
|
|
break;
|
|
|
|
}
|
2002-06-09 14:14:07 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-08-21 12:54:27 +00:00
|
|
|
// Currently not enrolled in the course, so see if they want to enrol
|
2002-06-09 14:14:07 +00:00
|
|
|
$SESSION->wantsurl = $FULLME;
|
|
|
|
redirect("$CFG->wwwroot/course/enrol.php?id=$courseid");
|
|
|
|
die;
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2004-08-22 14:38:47 +00:00
|
|
|
function require_course_login($course, $autologinguest=true) {
|
|
|
|
// This is a weaker version of require_login which only requires login
|
|
|
|
// when called from within a course rather than the site page, unless
|
|
|
|
// the forcelogin option is turned on.
|
|
|
|
global $CFG;
|
|
|
|
if ($CFG->forcelogin) {
|
|
|
|
require_login();
|
|
|
|
}
|
|
|
|
if ($course->category) {
|
|
|
|
require_login($course->id, $autologinguest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-23 03:07:01 +00:00
|
|
|
function update_user_login_times() {
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
$USER->lastlogin = $user->lastlogin = $USER->currentlogin;
|
2004-08-29 15:00:08 +00:00
|
|
|
$USER->currentlogin = $user->lastaccess = $user->currentlogin = time();
|
2002-12-23 03:07:01 +00:00
|
|
|
|
|
|
|
$user->id = $USER->id;
|
|
|
|
|
|
|
|
return update_record("user", $user);
|
|
|
|
}
|
|
|
|
|
2002-12-30 15:30:05 +00:00
|
|
|
function user_not_fully_set_up($user) {
|
2003-01-02 10:32:51 +00:00
|
|
|
return ($user->username != "guest" and (empty($user->firstname) or empty($user->lastname) or empty($user->email)));
|
2002-12-30 15:30:05 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
function update_login_count() {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Keeps track of login attempts
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
global $SESSION;
|
|
|
|
|
|
|
|
$max_logins = 10;
|
|
|
|
|
|
|
|
if (empty($SESSION->logincount)) {
|
|
|
|
$SESSION->logincount = 1;
|
|
|
|
} else {
|
|
|
|
$SESSION->logincount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($SESSION->logincount > $max_logins) {
|
2002-12-20 14:44:14 +00:00
|
|
|
unset($SESSION->wantsurl);
|
2002-12-23 03:07:01 +00:00
|
|
|
error(get_string("errortoomanylogins"));
|
2002-09-22 14:06:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function reset_login_count() {
|
|
|
|
/// Resets login attempts
|
|
|
|
global $SESSION;
|
2002-09-22 14:06:38 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
$SESSION->logincount = 0;
|
2002-09-22 14:06:38 +00:00
|
|
|
}
|
|
|
|
|
2004-01-30 08:25:49 +00:00
|
|
|
function check_for_restricted_user($username=NULL, $redirect="") {
|
|
|
|
global $CFG, $USER;
|
|
|
|
|
|
|
|
if (!$username) {
|
|
|
|
if (!empty($USER->username)) {
|
|
|
|
$username = $USER->username;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($CFG->restrictusers)) {
|
|
|
|
$names = explode(',', $CFG->restrictusers);
|
|
|
|
if (in_array($username, $names)) {
|
2004-02-04 12:59:05 +00:00
|
|
|
error(get_string("restricteduser", "error", fullname($USER)), $redirect);
|
2004-01-30 08:25:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-05 11:38:54 +00:00
|
|
|
function isadmin($userid=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Is the user an admin?
|
2001-11-22 06:23:56 +00:00
|
|
|
global $USER;
|
2003-05-01 23:12:05 +00:00
|
|
|
static $admins = array();
|
|
|
|
static $nonadmins = array();
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2003-05-05 11:38:54 +00:00
|
|
|
if (!$userid){
|
|
|
|
if (empty($USER->id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$userid = $USER->id;
|
2002-12-29 14:41:03 +00:00
|
|
|
}
|
|
|
|
|
2003-05-05 11:38:54 +00:00
|
|
|
if (in_array($userid, $admins)) {
|
2003-05-01 23:12:05 +00:00
|
|
|
return true;
|
2003-05-05 11:38:54 +00:00
|
|
|
} else if (in_array($userid, $nonadmins)) {
|
2003-05-01 23:12:05 +00:00
|
|
|
return false;
|
2003-05-05 11:38:54 +00:00
|
|
|
} else if (record_exists("user_admins", "userid", $userid)){
|
|
|
|
$admins[] = $userid;
|
2003-05-01 23:12:05 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2003-05-05 11:38:54 +00:00
|
|
|
$nonadmins[] = $userid;
|
2003-05-01 23:12:05 +00:00
|
|
|
return false;
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-25 13:48:55 +00:00
|
|
|
function isteacher($courseid=0, $userid=0, $includeadmin=true) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Is the user a teacher or admin?
|
2001-11-22 06:23:56 +00:00
|
|
|
global $USER;
|
|
|
|
|
2003-10-28 14:44:39 +00:00
|
|
|
if ($includeadmin and isadmin($userid)) { // admins can do anything the teacher can
|
2002-04-03 06:30:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
if (!$userid) {
|
2004-08-31 12:33:20 +00:00
|
|
|
if ($courseid) {
|
|
|
|
return !empty($USER->teacher[$courseid]);
|
|
|
|
}
|
|
|
|
if (!isset($USER->id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$userid = $USER->id;
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2004-07-25 13:48:55 +00:00
|
|
|
if (!$courseid) {
|
|
|
|
return record_exists("user_teachers","userid",$userid);
|
|
|
|
}
|
|
|
|
|
2002-12-23 09:39:26 +00:00
|
|
|
return record_exists("user_teachers", "userid", $userid, "course", $courseid);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2003-08-16 05:19:24 +00:00
|
|
|
function isteacheredit($courseid, $userid=0) {
|
|
|
|
/// Is the user allowed to edit this course?
|
|
|
|
global $USER;
|
|
|
|
|
2004-04-01 10:11:20 +00:00
|
|
|
if (isadmin($userid)) { // admins can do anything
|
2003-08-16 05:19:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$userid) {
|
|
|
|
return !empty($USER->teacheredit[$courseid]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_field("user_teachers", "editall", "userid", $userid, "course", $courseid);
|
|
|
|
}
|
|
|
|
|
2003-02-24 18:48:55 +00:00
|
|
|
function iscreator ($userid=0) {
|
|
|
|
/// Can user create new courses?
|
|
|
|
global $USER;
|
2003-04-01 02:38:31 +00:00
|
|
|
if (empty($USER->id)) {
|
|
|
|
return false;
|
|
|
|
}
|
2003-02-24 18:48:55 +00:00
|
|
|
if (isadmin($userid)) { // admins can do anything
|
|
|
|
return true;
|
|
|
|
}
|
2003-04-01 02:38:31 +00:00
|
|
|
if (empty($userid)) {
|
2003-02-24 18:48:55 +00:00
|
|
|
return record_exists("user_coursecreators", "userid", $USER->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return record_exists("user_coursecreators", "userid", $userid);
|
|
|
|
}
|
|
|
|
|
2002-07-29 06:52:48 +00:00
|
|
|
function isstudent($courseid, $userid=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Is the user a student in this course?
|
2004-07-01 21:00:07 +00:00
|
|
|
/// If course is site, is the user a confirmed user on the site?
|
2004-08-31 12:33:20 +00:00
|
|
|
global $USER, $CFG;
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-08-30 17:27:00 +00:00
|
|
|
if (empty($USER->id) and !$userid) {
|
2004-08-01 06:21:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-08-29 14:15:40 +00:00
|
|
|
if ($courseid == SITEID) {
|
2004-07-01 21:00:07 +00:00
|
|
|
if (!$userid) {
|
|
|
|
$userid = $USER->id;
|
|
|
|
}
|
|
|
|
if (isguest($userid)) {
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-31 12:33:20 +00:00
|
|
|
// a site teacher can never be a site student
|
|
|
|
if (isteacher($courseid, $userid)) {
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-30 17:27:00 +00:00
|
|
|
if ($CFG->allusersaresitestudents) {
|
|
|
|
return record_exists('user', 'id', $userid);
|
|
|
|
} else {
|
|
|
|
return (record_exists('user_students', 'userid', $userid)
|
2004-08-31 12:33:20 +00:00
|
|
|
or record_exists('user_teachers', 'userid', $userid));
|
2004-08-30 17:27:00 +00:00
|
|
|
}
|
2004-08-22 14:38:47 +00:00
|
|
|
}
|
2004-07-01 21:00:07 +00:00
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
if (!$userid) {
|
2003-01-01 06:40:31 +00:00
|
|
|
return !empty($USER->student[$courseid]);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2002-12-23 09:39:26 +00:00
|
|
|
// $timenow = time(); // todo: add time check below
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-12-23 09:39:26 +00:00
|
|
|
return record_exists("user_students", "userid", $userid, "course", $courseid);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2002-06-09 14:14:07 +00:00
|
|
|
function isguest($userid=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Is the user a guest?
|
2002-06-09 14:14:07 +00:00
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if (!$userid) {
|
2003-01-28 03:34:26 +00:00
|
|
|
if (empty($USER->username)) {
|
|
|
|
return false;
|
|
|
|
}
|
2002-06-09 14:14:07 +00:00
|
|
|
return ($USER->username == "guest");
|
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
return record_exists("user", "id", $userid, "username", "guest");
|
2002-06-09 14:14:07 +00:00
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2002-07-29 15:45:46 +00:00
|
|
|
function isediting($courseid, $user=NULL) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Is the current user in editing mode?
|
2002-07-29 15:45:46 +00:00
|
|
|
global $USER;
|
|
|
|
if (!$user){
|
|
|
|
$user = $USER;
|
|
|
|
}
|
2002-12-29 17:32:32 +00:00
|
|
|
if (empty($user->editing)) {
|
|
|
|
return false;
|
|
|
|
}
|
2002-07-29 15:45:46 +00:00
|
|
|
return ($user->editing and isteacher($courseid, $user->id));
|
|
|
|
}
|
|
|
|
|
2003-07-14 13:08:38 +00:00
|
|
|
function ismoving($courseid) {
|
|
|
|
/// Is the current user currently moving an activity?
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if (!empty($USER->activitycopy)) {
|
|
|
|
return ($USER->activitycopycourse == $courseid);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-11-19 15:59:45 +00:00
|
|
|
function fullname($user, $override=false) {
|
2003-11-30 14:02:01 +00:00
|
|
|
/// Given an object containing firstname and lastname
|
2004-04-01 10:11:20 +00:00
|
|
|
/// values, this function returns a string with the
|
2003-11-30 14:02:01 +00:00
|
|
|
/// full name of the person.
|
2003-11-19 15:59:45 +00:00
|
|
|
/// The result may depend on system settings
|
2003-11-30 14:02:01 +00:00
|
|
|
/// or language. 'override' will force both names
|
2003-11-19 15:59:45 +00:00
|
|
|
/// to be used even if system settings specify one.
|
2003-11-30 14:02:01 +00:00
|
|
|
|
2003-12-30 17:18:06 +00:00
|
|
|
global $CFG, $SESSION;
|
|
|
|
|
2004-08-07 13:39:36 +00:00
|
|
|
if (!isset($user->firstname) and !isset($user->lastname)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2003-12-30 17:18:06 +00:00
|
|
|
if (!empty($SESSION->fullnamedisplay)) {
|
|
|
|
$CFG->fullnamedisplay = $SESSION->fullnamedisplay;
|
|
|
|
}
|
2003-11-19 15:59:45 +00:00
|
|
|
|
2003-11-30 14:02:01 +00:00
|
|
|
if ($CFG->fullnamedisplay == 'firstname lastname') {
|
|
|
|
return "$user->firstname $user->lastname";
|
|
|
|
|
|
|
|
} else if ($CFG->fullnamedisplay == 'lastname firstname') {
|
|
|
|
return "$user->lastname $user->firstname";
|
2003-11-19 15:59:45 +00:00
|
|
|
|
2003-11-30 14:02:01 +00:00
|
|
|
} else if ($CFG->fullnamedisplay == 'firstname') {
|
|
|
|
if ($override) {
|
|
|
|
return get_string('fullnamedisplay', '', $user);
|
|
|
|
} else {
|
|
|
|
return $user->firstname;
|
|
|
|
}
|
|
|
|
}
|
2003-11-19 15:59:45 +00:00
|
|
|
|
2003-11-30 14:02:01 +00:00
|
|
|
return get_string('fullnamedisplay', '', $user);
|
2003-11-19 15:59:45 +00:00
|
|
|
}
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
function set_moodle_cookie($thing) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Sets a moodle cookie with an encrypted string
|
2002-12-24 10:07:45 +00:00
|
|
|
global $CFG;
|
2003-10-17 12:30:17 +00:00
|
|
|
|
|
|
|
$cookiename = 'MOODLEID_'.$CFG->sessioncookie;
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
$days = 60;
|
|
|
|
$seconds = 60*60*24*$days;
|
|
|
|
|
2002-12-24 10:07:45 +00:00
|
|
|
setCookie($cookiename, "", time() - 3600, "/");
|
|
|
|
setCookie($cookiename, rc4encrypt($thing), time()+$seconds, "/");
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function get_moodle_cookie() {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Gets a moodle cookie with an encrypted string
|
2002-12-24 10:07:45 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2003-10-17 12:30:17 +00:00
|
|
|
$cookiename = 'MOODLEID_'.$CFG->sessioncookie;
|
2002-12-24 10:07:45 +00:00
|
|
|
|
2003-01-08 09:07:07 +00:00
|
|
|
if (empty($_COOKIE[$cookiename])) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return rc4decrypt($_COOKIE[$cookiename]);
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2004-08-16 15:41:57 +00:00
|
|
|
function is_internal_auth($auth='') {
|
2003-09-22 13:58:20 +00:00
|
|
|
/// Returns true if an internal authentication method is being used.
|
2004-08-16 15:41:57 +00:00
|
|
|
/// if method not specified then, global default is assumed
|
2003-09-22 13:58:20 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2004-08-16 15:41:57 +00:00
|
|
|
$method = $CFG->auth;
|
|
|
|
|
|
|
|
if (!empty($auth)) {
|
|
|
|
$method = $auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($method == "email" || $method == "none" || $method == "manual");
|
2003-09-22 13:58:20 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-08-31 12:33:20 +00:00
|
|
|
function create_user_record($username, $password, $auth='') {
|
2004-04-01 10:11:20 +00:00
|
|
|
/// Creates a bare-bones user record
|
2002-10-05 16:49:42 +00:00
|
|
|
global $REMOTE_ADDR, $CFG;
|
2004-08-31 12:33:20 +00:00
|
|
|
|
2003-04-24 20:06:40 +00:00
|
|
|
//just in case check text case
|
|
|
|
$username = trim(moodle_strtolower($username));
|
2004-08-31 12:33:20 +00:00
|
|
|
|
2004-08-30 17:47:21 +00:00
|
|
|
if (function_exists('auth_get_userinfo')) {
|
2002-10-05 16:49:42 +00:00
|
|
|
if ($newinfo = auth_get_userinfo($username)) {
|
2002-11-21 07:37:21 +00:00
|
|
|
foreach ($newinfo as $key => $value){
|
2003-01-22 01:55:54 +00:00
|
|
|
$newuser->$key = addslashes(stripslashes($value)); // Just in case
|
2002-10-05 16:49:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-08-31 12:33:20 +00:00
|
|
|
$newuser->auth = (empty($auth)) ? $CFG->auth : $auth;
|
2002-09-26 07:03:22 +00:00
|
|
|
$newuser->username = $username;
|
|
|
|
$newuser->password = md5($password);
|
2002-10-18 09:00:09 +00:00
|
|
|
$newuser->lang = $CFG->lang;
|
2002-09-26 07:03:22 +00:00
|
|
|
$newuser->confirmed = 1;
|
2004-09-04 11:02:45 +00:00
|
|
|
$newuser->lastIP = getremoteaddr();
|
2002-09-26 07:03:22 +00:00
|
|
|
$newuser->timemodified = time();
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-09-26 07:03:22 +00:00
|
|
|
if (insert_record("user", $newuser)) {
|
|
|
|
return get_user_info_from_db("username", $username);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-01-25 09:35:45 +00:00
|
|
|
|
|
|
|
function guest_user() {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if ($newuser = get_record("user", "username", "guest")) {
|
|
|
|
$newuser->loggedin = true;
|
|
|
|
$newuser->confirmed = 1;
|
|
|
|
$newuser->site = $CFG->wwwroot;
|
|
|
|
$newuser->lang = $CFG->lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $newuser;
|
|
|
|
}
|
|
|
|
|
2002-09-26 07:03:22 +00:00
|
|
|
function authenticate_user_login($username, $password) {
|
2004-04-01 10:11:20 +00:00
|
|
|
/// Given a username and password, this function looks them
|
2002-12-20 14:44:14 +00:00
|
|
|
/// up using the currently selected authentication mechanism,
|
2004-04-01 10:11:20 +00:00
|
|
|
/// and if the authentication is successful, it returns a
|
2002-12-20 14:44:14 +00:00
|
|
|
/// valid $user object from the 'user' table.
|
|
|
|
///
|
|
|
|
/// Uses auth_ functions from the currently active auth module
|
2002-09-26 07:03:22 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2002-11-22 08:43:35 +00:00
|
|
|
$md5password = md5($password);
|
|
|
|
|
2004-08-29 05:48:15 +00:00
|
|
|
// First try to find the user in the database
|
2002-11-22 08:43:35 +00:00
|
|
|
|
2004-08-29 05:48:15 +00:00
|
|
|
$user = get_user_info_from_db("username", $username);
|
2004-08-16 15:41:57 +00:00
|
|
|
|
2004-08-29 05:48:15 +00:00
|
|
|
// Sort out the authentication method we are using.
|
2004-08-16 15:41:57 +00:00
|
|
|
|
2004-08-29 05:48:15 +00:00
|
|
|
if (empty($CFG->auth)) {
|
|
|
|
$CFG->auth = "manual"; // Default authentication module
|
|
|
|
}
|
2004-08-16 15:41:57 +00:00
|
|
|
|
2004-08-29 05:48:15 +00:00
|
|
|
if (empty($user->auth)) { // For some reason it isn't set yet
|
|
|
|
if (isadmin($user->id) or isguest($user->id)) {
|
2004-08-31 12:33:20 +00:00
|
|
|
$auth = 'manual'; // Always assume these guys are internal
|
2004-08-29 05:48:15 +00:00
|
|
|
} else {
|
2004-08-31 12:33:20 +00:00
|
|
|
$auth = $CFG->auth; // Normal users default to site method
|
2004-08-29 05:48:15 +00:00
|
|
|
}
|
2004-08-31 12:33:20 +00:00
|
|
|
} else {
|
|
|
|
$auth = $user->auth;
|
2004-08-29 05:48:15 +00:00
|
|
|
}
|
|
|
|
|
2004-08-31 12:33:20 +00:00
|
|
|
if (!file_exists("$CFG->dirroot/auth/$auth/lib.php")) {
|
|
|
|
$auth = "manual"; // Can't find auth module, default to internal
|
2002-11-22 08:43:35 +00:00
|
|
|
}
|
|
|
|
|
2004-08-31 12:33:20 +00:00
|
|
|
require_once("$CFG->dirroot/auth/$auth/lib.php");
|
2002-09-26 07:03:22 +00:00
|
|
|
|
|
|
|
if (auth_user_login($username, $password)) { // Successful authentication
|
2004-08-31 12:33:20 +00:00
|
|
|
if ($user) { // User already exists in database
|
|
|
|
if (empty($user->auth)) { // For some reason auth isn't set yet
|
|
|
|
set_field('user', 'auth', $auth, 'username', $username);
|
|
|
|
}
|
2002-11-22 09:38:15 +00:00
|
|
|
if ($md5password <> $user->password) { // Update local copy of password for reference
|
2004-08-31 12:33:20 +00:00
|
|
|
set_field('user', 'password', $md5password, 'username', $username);
|
2002-09-26 07:03:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
2004-08-31 12:33:20 +00:00
|
|
|
$user = create_user_record($username, $password, $auth);
|
2002-09-26 07:03:22 +00:00
|
|
|
}
|
2002-11-21 12:39:33 +00:00
|
|
|
|
2003-03-14 21:34:39 +00:00
|
|
|
if (function_exists('auth_iscreator')) { // Check if the user is a creator
|
|
|
|
if (auth_iscreator($username)) {
|
2004-08-16 15:41:57 +00:00
|
|
|
if (! record_exists("user_coursecreators", "userid", $user->id)) {
|
|
|
|
$cdata->userid = $user->id;
|
|
|
|
if (! insert_record("user_coursecreators", $cdata)) {
|
|
|
|
error("Cannot add user to course creators.");
|
|
|
|
}
|
|
|
|
}
|
2003-03-14 21:34:39 +00:00
|
|
|
} else {
|
2004-08-16 15:41:57 +00:00
|
|
|
if ( record_exists("user_coursecreators", "userid", $user->id)) {
|
|
|
|
if (! delete_records("user_coursecreators", "userid", $user->id)) {
|
|
|
|
error("Cannot remove user from course creators.");
|
|
|
|
}
|
|
|
|
}
|
2003-03-14 21:34:39 +00:00
|
|
|
}
|
2004-08-16 15:41:57 +00:00
|
|
|
}
|
2003-03-14 21:34:39 +00:00
|
|
|
return $user;
|
2004-07-25 13:48:55 +00:00
|
|
|
|
2003-03-14 21:34:39 +00:00
|
|
|
} else {
|
2004-07-25 13:48:55 +00:00
|
|
|
add_to_log(0, "login", "error", $_SERVER['HTTP_REFERER'], $username);
|
|
|
|
$date = date('Y-m-d H:i:s');
|
2004-09-04 11:02:45 +00:00
|
|
|
error_log("$date\tfailed login\t".getremoteaddr()."\t".$_SERVER['HTTP_USER_AGENT']."\t$username");
|
2003-03-14 21:34:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2004-07-29 14:54:25 +00:00
|
|
|
function enrol_student($userid, $courseid, $timestart=0, $timeend=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Enrols a student in a given course
|
2004-08-29 18:20:13 +00:00
|
|
|
global $CFG;
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-07-29 14:54:25 +00:00
|
|
|
$course = get_record("course", "id", $courseid);
|
|
|
|
|
2003-08-17 05:48:05 +00:00
|
|
|
if (!record_exists("user_students", "userid", $userid, "course", $courseid)) {
|
2003-08-17 08:51:55 +00:00
|
|
|
if (record_exists("user", "id", $userid)) {
|
2004-08-29 18:20:13 +00:00
|
|
|
|
|
|
|
require_once('../mod/forum/lib.php');
|
|
|
|
forum_add_user($userid, $courseid);
|
|
|
|
|
2003-08-17 08:51:55 +00:00
|
|
|
$student->userid = $userid;
|
|
|
|
$student->course = $courseid;
|
2004-07-29 14:54:25 +00:00
|
|
|
$student->timestart = $timestart;
|
|
|
|
$student->timeend = $timeend;
|
2003-08-17 08:51:55 +00:00
|
|
|
$student->time = time();
|
|
|
|
return insert_record("user_students", $student);
|
|
|
|
}
|
|
|
|
return false;
|
2003-08-17 05:48:05 +00:00
|
|
|
}
|
|
|
|
return true;
|
2002-12-11 14:59:37 +00:00
|
|
|
}
|
|
|
|
|
2004-01-30 06:44:02 +00:00
|
|
|
function unenrol_student($userid, $courseid=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Unenrols a student from a given course
|
2002-12-11 14:59:37 +00:00
|
|
|
|
2004-01-30 06:44:02 +00:00
|
|
|
if ($courseid) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// First delete any crucial stuff that might still send mail
|
2004-01-30 06:44:02 +00:00
|
|
|
if ($forums = get_records("forum", "course", $courseid)) {
|
2002-12-20 14:44:14 +00:00
|
|
|
foreach ($forums as $forum) {
|
2004-01-30 06:44:02 +00:00
|
|
|
delete_records("forum_subscriptions", "forum", $forum->id, "userid", $userid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($groups = get_groups($courseid, $userid)) {
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
delete_records("groups_members", "groupid", $group->id, "userid", $userid);
|
2002-09-22 03:01:17 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
2004-01-30 06:44:02 +00:00
|
|
|
return delete_records("user_students", "userid", $userid, "course", $courseid);
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
} else {
|
2004-01-30 06:44:02 +00:00
|
|
|
delete_records("forum_subscriptions", "userid", $userid);
|
2004-03-10 13:45:23 +00:00
|
|
|
delete_records("groups_members", "userid", $userid);
|
2004-01-30 06:44:02 +00:00
|
|
|
return delete_records("user_students", "userid", $userid);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-29 14:54:25 +00:00
|
|
|
function add_teacher($userid, $courseid, $editall=1, $role="", $timestart=0, $timeend=0) {
|
2003-08-17 08:51:55 +00:00
|
|
|
/// Add a teacher to a given course
|
2004-08-29 19:17:27 +00:00
|
|
|
global $CFG;
|
2003-08-17 08:51:55 +00:00
|
|
|
|
2004-06-24 12:51:25 +00:00
|
|
|
if ($teacher = get_record('user_teachers', 'userid', $userid, 'course', $courseid)) {
|
2004-07-29 14:54:25 +00:00
|
|
|
$newteacher = NULL;
|
|
|
|
$newteacher->id = $teacher->id;
|
|
|
|
$newteacher->editall = $editall;
|
|
|
|
if ($role) {
|
|
|
|
$newteacher->role = $role;
|
|
|
|
}
|
|
|
|
if ($timestart) {
|
|
|
|
$newteacher->timestart = $timestart;
|
2003-08-17 08:51:55 +00:00
|
|
|
}
|
2004-07-29 14:54:25 +00:00
|
|
|
if ($timeend) {
|
|
|
|
$newteacher->timeend = $timeend;
|
|
|
|
}
|
|
|
|
return update_record('user_teachers', $newteacher);
|
2003-08-17 08:51:55 +00:00
|
|
|
}
|
2004-06-24 12:51:25 +00:00
|
|
|
|
|
|
|
if (!record_exists("user", "id", $userid)) {
|
|
|
|
return false; // no such user
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!record_exists("course", "id", $courseid)) {
|
|
|
|
return false; // no such course
|
|
|
|
}
|
|
|
|
|
|
|
|
$teacher = NULL;
|
|
|
|
$teacher->userid = $userid;
|
|
|
|
$teacher->course = $courseid;
|
|
|
|
$teacher->editall = $editall;
|
|
|
|
$teacher->role = $role;
|
|
|
|
|
|
|
|
if (record_exists("user_teachers", "course", $courseid)) {
|
|
|
|
$teacher->authority = 2;
|
|
|
|
} else {
|
|
|
|
$teacher->authority = 1;
|
|
|
|
}
|
|
|
|
delete_records("user_students", "userid", $userid, "course", $courseid); // Unenrol as student
|
2004-08-29 18:20:13 +00:00
|
|
|
|
|
|
|
/// Add forum subscriptions for new users
|
2004-08-29 19:17:27 +00:00
|
|
|
require_once('../mod/forum/lib.php');
|
|
|
|
forum_add_user($userid, $courseid);
|
2004-06-24 12:51:25 +00:00
|
|
|
|
|
|
|
return insert_record("user_teachers", $teacher);
|
|
|
|
|
2003-08-17 08:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function remove_teacher($userid, $courseid=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Removes a teacher from a given course (or ALL courses)
|
|
|
|
/// Does not delete the user account
|
2003-08-17 08:51:55 +00:00
|
|
|
if ($courseid) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// First delete any crucial stuff that might still send mail
|
2003-08-17 08:51:55 +00:00
|
|
|
if ($forums = get_records("forum", "course", $courseid)) {
|
2002-12-20 14:44:14 +00:00
|
|
|
foreach ($forums as $forum) {
|
2003-08-17 08:51:55 +00:00
|
|
|
delete_records("forum_subscriptions", "forum", $forum->id, "userid", $userid);
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
}
|
2003-08-17 08:51:55 +00:00
|
|
|
return delete_records("user_teachers", "userid", $userid, "course", $courseid);
|
2002-08-02 17:41:14 +00:00
|
|
|
} else {
|
2003-08-17 08:51:55 +00:00
|
|
|
delete_records("forum_subscriptions", "userid", $userid);
|
|
|
|
return delete_records("user_teachers", "userid", $userid);
|
2002-08-02 17:41:14 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2003-08-17 08:51:55 +00:00
|
|
|
|
|
|
|
function add_creator($userid) {
|
|
|
|
/// Add a creator to the site
|
|
|
|
|
|
|
|
if (!record_exists("user_admins", "userid", $userid)) {
|
|
|
|
if (record_exists("user", "id", $userid)) {
|
|
|
|
$creator->userid = $userid;
|
|
|
|
return insert_record("user_coursecreators", $creator);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove_creator($userid) {
|
|
|
|
/// Removes a creator from a site
|
|
|
|
global $db;
|
|
|
|
|
|
|
|
return delete_records("user_coursecreators", "userid", $userid);
|
|
|
|
}
|
|
|
|
|
|
|
|
function add_admin($userid) {
|
|
|
|
/// Add an admin to the site
|
|
|
|
|
|
|
|
if (!record_exists("user_admins", "userid", $userid)) {
|
|
|
|
if (record_exists("user", "id", $userid)) {
|
|
|
|
$admin->userid = $userid;
|
2004-08-22 14:38:47 +00:00
|
|
|
|
|
|
|
// any admin is also a teacher on the site course
|
2004-08-29 14:15:40 +00:00
|
|
|
if (!record_exists('user_teachers', 'course', SITEID, 'userid', $userid)) {
|
|
|
|
if (!add_teacher($userid, SITEID)) {
|
2004-08-22 14:38:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-17 08:51:55 +00:00
|
|
|
return insert_record("user_admins", $admin);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove_admin($userid) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Removes an admin from a site
|
|
|
|
global $db;
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-08-22 14:38:47 +00:00
|
|
|
// remove also from the list of site teachers
|
2004-08-29 14:15:40 +00:00
|
|
|
remove_teacher($userid, SITEID);
|
2004-08-22 14:38:47 +00:00
|
|
|
|
2003-08-17 08:51:55 +00:00
|
|
|
return delete_records("user_admins", "userid", $userid);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-02 11:32:59 +00:00
|
|
|
function remove_course_contents($courseid, $showfeedback=true) {
|
|
|
|
/// Clear a course out completely, deleting all content
|
|
|
|
/// but don't delete the course itself
|
|
|
|
|
2003-08-08 11:29:33 +00:00
|
|
|
global $CFG, $THEME, $USER, $SESSION;
|
2003-08-02 11:32:59 +00:00
|
|
|
|
|
|
|
$result = true;
|
|
|
|
|
|
|
|
if (! $course = get_record("course", "id", $courseid)) {
|
|
|
|
error("Course ID was incorrect (can't find it)");
|
|
|
|
}
|
|
|
|
|
|
|
|
$strdeleted = get_string("deleted");
|
|
|
|
|
|
|
|
// First delete every instance of every module
|
2004-04-01 10:11:20 +00:00
|
|
|
|
2003-08-02 11:32:59 +00:00
|
|
|
if ($allmods = get_records("modules") ) {
|
|
|
|
foreach ($allmods as $mod) {
|
|
|
|
$modname = $mod->name;
|
|
|
|
$modfile = "$CFG->dirroot/mod/$modname/lib.php";
|
2004-01-21 16:15:03 +00:00
|
|
|
$moddelete = $modname."_delete_instance"; // Delete everything connected to an instance
|
|
|
|
$moddeletecourse = $modname."_delete_course"; // Delete other stray stuff (uncommon)
|
2003-08-02 11:32:59 +00:00
|
|
|
$count=0;
|
|
|
|
if (file_exists($modfile)) {
|
|
|
|
include_once($modfile);
|
|
|
|
if (function_exists($moddelete)) {
|
|
|
|
if ($instances = get_records($modname, "course", $course->id)) {
|
|
|
|
foreach ($instances as $instance) {
|
|
|
|
if ($moddelete($instance->id)) {
|
|
|
|
$count++;
|
|
|
|
} else {
|
|
|
|
notify("Could not delete $modname instance $instance->id ($instance->name)");
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
notify("Function $moddelete() doesn't exist!");
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
2004-01-21 16:15:03 +00:00
|
|
|
if (function_exists($moddeletecourse)) {
|
|
|
|
$moddeletecourse($course);
|
|
|
|
}
|
2003-08-02 11:32:59 +00:00
|
|
|
}
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted $count x $modname");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error("No modules are installed!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete any user stuff
|
|
|
|
|
|
|
|
if (delete_records("user_students", "course", $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted user_students");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delete_records("user_teachers", "course", $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted user_teachers");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
2004-02-15 05:08:00 +00:00
|
|
|
// Delete any groups
|
|
|
|
|
|
|
|
if ($groups = get_records("groups", "courseid", $course->id)) {
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
if (delete_records("groups_members", "groupid", $group->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted groups_members");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
if (delete_records("groups", "id", $group->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted groups");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete events
|
|
|
|
|
|
|
|
if (delete_records("event", "courseid", $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted event");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
2003-08-02 11:32:59 +00:00
|
|
|
// Delete logs
|
|
|
|
|
|
|
|
if (delete_records("log", "course", $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted log");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete any course stuff
|
|
|
|
|
|
|
|
if (delete_records("course_sections", "course", $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted course_sections");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delete_records("course_modules", "course", $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted course_modules");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-08-01 07:34:39 +00:00
|
|
|
function remove_course_userdata($courseid, $showfeedback=true,
|
|
|
|
$removestudents=true, $removeteachers=false, $removegroups=true,
|
|
|
|
$removeevents=true, $removelogs=false) {
|
|
|
|
/// This function will empty a course of USER data as much as
|
|
|
|
/// possible. It will retain the activities and the structure
|
|
|
|
/// of the course.
|
|
|
|
|
|
|
|
global $CFG, $THEME, $USER, $SESSION;
|
|
|
|
|
|
|
|
$result = true;
|
|
|
|
|
|
|
|
if (! $course = get_record("course", "id", $courseid)) {
|
|
|
|
error("Course ID was incorrect (can't find it)");
|
|
|
|
}
|
|
|
|
|
|
|
|
$strdeleted = get_string("deleted");
|
|
|
|
|
|
|
|
// Look in every instance of every module for data to delete
|
|
|
|
|
|
|
|
if ($allmods = get_records("modules") ) {
|
|
|
|
foreach ($allmods as $mod) {
|
|
|
|
$modname = $mod->name;
|
|
|
|
$modfile = "$CFG->dirroot/mod/$modname/lib.php";
|
|
|
|
$moddeleteuserdata = $modname."_delete_userdata"; // Function to delete user data
|
|
|
|
$count=0;
|
|
|
|
if (file_exists($modfile)) {
|
|
|
|
@include_once($modfile);
|
|
|
|
if (function_exists($moddeleteuserdata)) {
|
|
|
|
$moddeleteuserdata($course, $showfeedback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error("No modules are installed!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete other stuff
|
|
|
|
|
|
|
|
if ($removestudents) {
|
|
|
|
/// Delete student enrolments
|
|
|
|
if (delete_records("user_students", "course", $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted user_students");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
/// Delete group members (but keep the groups)
|
|
|
|
if ($groups = get_records("groups", "courseid", $course->id)) {
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
if (delete_records("groups_members", "groupid", $group->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted groups_members");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($removeteachers) {
|
|
|
|
if (delete_records("user_teachers", "course", $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted user_teachers");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($removegroups) {
|
|
|
|
if ($groups = get_records("groups", "courseid", $course->id)) {
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
if (delete_records("groups", "id", $group->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted groups");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($removeevents) {
|
|
|
|
if (delete_records("event", "courseid", $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted event");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($removelogs) {
|
|
|
|
if (delete_records("log", "course", $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted log");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2003-12-30 17:18:06 +00:00
|
|
|
/// GROUPS /////////////////////////////////////////////////////////
|
2004-04-01 10:11:20 +00:00
|
|
|
|
2003-12-30 17:18:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a boolean: is the user a member of the given group?
|
2004-04-01 10:11:20 +00:00
|
|
|
*
|
2004-05-05 07:07:56 +00:00
|
|
|
* @param type description
|
2003-12-30 17:18:06 +00:00
|
|
|
*/
|
|
|
|
function ismember($groupid, $userid=0) {
|
|
|
|
global $USER;
|
|
|
|
|
2004-01-26 09:12:52 +00:00
|
|
|
if (!$groupid) { // No point doing further checks
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-12-30 17:18:06 +00:00
|
|
|
if (!$userid) {
|
2004-01-11 17:44:46 +00:00
|
|
|
if (empty($USER->groupmember)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach ($USER->groupmember as $courseid => $mgroupid) {
|
|
|
|
if ($mgroupid == $groupid) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2003-12-30 17:18:06 +00:00
|
|
|
}
|
|
|
|
|
2003-12-30 18:07:09 +00:00
|
|
|
return record_exists("groups_members", "groupid", $groupid, "userid", $userid);
|
2003-12-30 17:18:06 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 17:44:46 +00:00
|
|
|
/**
|
|
|
|
* Returns the group ID of the current user in the given course
|
2004-04-01 10:11:20 +00:00
|
|
|
*
|
2004-05-05 07:07:56 +00:00
|
|
|
* @param type description
|
2004-01-11 17:44:46 +00:00
|
|
|
*/
|
|
|
|
function mygroupid($courseid) {
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if (empty($USER->groupmember[$courseid])) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return $USER->groupmember[$courseid];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-30 17:18:06 +00:00
|
|
|
/**
|
2004-04-01 10:11:20 +00:00
|
|
|
* For a given course, and possibly course module, determine
|
2003-12-30 17:18:06 +00:00
|
|
|
* what the current default groupmode is:
|
|
|
|
* NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
|
2004-04-01 10:11:20 +00:00
|
|
|
*
|
2004-05-05 07:07:56 +00:00
|
|
|
* @param type description
|
2003-12-30 17:18:06 +00:00
|
|
|
*/
|
|
|
|
function groupmode($course, $cm=null) {
|
|
|
|
|
|
|
|
if ($cm and !$course->groupmodeforce) {
|
|
|
|
return $cm->groupmode;
|
|
|
|
}
|
|
|
|
return $course->groupmode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the current group in the session variable
|
2004-04-01 10:11:20 +00:00
|
|
|
*
|
2004-05-05 07:07:56 +00:00
|
|
|
* @param type description
|
2003-12-30 17:18:06 +00:00
|
|
|
*/
|
|
|
|
function set_current_group($courseid, $groupid) {
|
|
|
|
global $SESSION;
|
|
|
|
|
|
|
|
return $SESSION->currentgroup[$courseid] = $groupid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current group for the current user as an id or an object
|
2004-04-01 10:11:20 +00:00
|
|
|
*
|
2004-05-05 07:07:56 +00:00
|
|
|
* @param type description
|
2003-12-30 17:18:06 +00:00
|
|
|
*/
|
|
|
|
function get_current_group($courseid, $full=false) {
|
|
|
|
global $SESSION, $USER;
|
|
|
|
|
2004-08-04 12:32:43 +00:00
|
|
|
if (!isset($SESSION->currentgroup[$courseid])) {
|
2003-12-30 17:18:06 +00:00
|
|
|
if (empty($USER->groupmember[$courseid])) {
|
2004-01-26 09:12:52 +00:00
|
|
|
return 0;
|
2003-12-30 17:18:06 +00:00
|
|
|
} else {
|
|
|
|
$SESSION->currentgroup[$courseid] = $USER->groupmember[$courseid];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($full) {
|
2003-12-30 18:07:09 +00:00
|
|
|
return get_record('groups', 'id', $SESSION->currentgroup[$courseid]);
|
2003-12-30 17:18:06 +00:00
|
|
|
} else {
|
|
|
|
return $SESSION->currentgroup[$courseid];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-11 17:44:46 +00:00
|
|
|
/**
|
|
|
|
* A combination function to make it easier for modules
|
|
|
|
* to set up groups.
|
|
|
|
*
|
|
|
|
* It will use a given "groupid" parameter and try to use
|
|
|
|
* that to reset the current group for the user.
|
|
|
|
*
|
2004-05-05 07:07:56 +00:00
|
|
|
* @param type description
|
2004-01-11 17:44:46 +00:00
|
|
|
*/
|
2004-02-19 17:56:52 +00:00
|
|
|
function get_and_set_current_group($course, $groupmode, $groupid=-1) {
|
2004-01-11 17:44:46 +00:00
|
|
|
|
|
|
|
if (!$groupmode) { // Groups don't even apply
|
2004-04-01 10:11:20 +00:00
|
|
|
return false;
|
2004-01-11 17:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$currentgroupid = get_current_group($course->id);
|
|
|
|
|
2004-02-19 17:56:52 +00:00
|
|
|
if ($groupid < 0) { // No change was specified
|
|
|
|
return $currentgroupid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($groupid) { // Try to change the current group to this groupid
|
2004-01-11 17:44:46 +00:00
|
|
|
if ($group = get_record('groups', 'id', $groupid, 'courseid', $course->id)) { // Exists
|
|
|
|
if (isteacheredit($course->id)) { // Sets current default group
|
|
|
|
$currentgroupid = set_current_group($course->id, $group->id);
|
|
|
|
|
|
|
|
} else if ($groupmode == VISIBLEGROUPS) { // All groups are visible
|
|
|
|
$currentgroupid = $group->id;
|
|
|
|
}
|
|
|
|
}
|
2004-02-19 17:56:52 +00:00
|
|
|
} else { // When groupid = 0 it means show ALL groups
|
|
|
|
if (isteacheredit($course->id)) { // Sets current default group
|
|
|
|
$currentgroupid = set_current_group($course->id, 0);
|
|
|
|
|
|
|
|
} else if ($groupmode == VISIBLEGROUPS) { // All groups are visible
|
|
|
|
$currentgroupid = 0;
|
|
|
|
}
|
2004-01-11 17:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $currentgroupid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-15 07:13:08 +00:00
|
|
|
/**
|
|
|
|
* A big combination function to make it easier for modules
|
|
|
|
* to set up groups.
|
|
|
|
*
|
|
|
|
* Terminates if the current user shouldn't be looking at this group
|
|
|
|
* Otherwise returns the current group if there is one
|
|
|
|
* Otherwise returns false if groups aren't relevant
|
|
|
|
*
|
2004-05-05 07:07:56 +00:00
|
|
|
* @param type description
|
2004-02-15 07:13:08 +00:00
|
|
|
*/
|
|
|
|
function setup_and_print_groups($course, $groupmode, $urlroot) {
|
|
|
|
|
2004-02-19 17:56:52 +00:00
|
|
|
if (isset($_GET['group'])) {
|
|
|
|
$changegroup = $_GET['group']; /// 0 or higher
|
|
|
|
} else {
|
|
|
|
$changegroup = -1; /// This means no group change was specified
|
|
|
|
}
|
|
|
|
|
|
|
|
$currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
|
2004-02-15 07:13:08 +00:00
|
|
|
|
2004-02-19 17:56:52 +00:00
|
|
|
if ($currentgroup === false) {
|
2004-02-15 07:13:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-02-24 07:12:56 +00:00
|
|
|
if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id) and !$currentgroup) {
|
|
|
|
print_heading(get_string('notingroup'));
|
2004-02-15 07:13:08 +00:00
|
|
|
print_footer($course);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($groupmode == VISIBLEGROUPS or ($groupmode and isteacheredit($course->id))) {
|
|
|
|
if ($groups = get_records_menu("groups", "courseid", $course->id, "name ASC", "id,name")) {
|
2004-02-19 17:56:52 +00:00
|
|
|
echo '<div align="center">';
|
2004-02-15 07:13:08 +00:00
|
|
|
print_group_menu($groups, $groupmode, $currentgroup, $urlroot);
|
2004-02-19 17:56:52 +00:00
|
|
|
echo '</div>';
|
2004-02-15 07:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $currentgroup;
|
|
|
|
}
|
2004-01-11 17:44:46 +00:00
|
|
|
|
2003-12-30 17:18:06 +00:00
|
|
|
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
/// CORRESPONDENCE ////////////////////////////////////////////////
|
|
|
|
|
2004-06-06 12:49:55 +00:00
|
|
|
function email_to_user($user, $from, $subject, $messagetext, $messagehtml="", $attachment="", $attachname="", $usetrueaddress=true) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// user - a user record as an object
|
|
|
|
/// from - a user record as an object
|
|
|
|
/// subject - plain text subject line of the email
|
|
|
|
/// messagetext - plain text version of the message
|
|
|
|
/// messagehtml - complete html version of the message (optional)
|
|
|
|
/// attachment - a file on the filesystem, relative to $CFG->dataroot
|
|
|
|
/// attachname - the name of the file (extension indicates MIME)
|
2004-06-25 07:38:44 +00:00
|
|
|
/// usetrueaddress - determines whether $from email address should be sent out.
|
2004-06-06 12:49:55 +00:00
|
|
|
/// Will be overruled by user profile setting for maildisplay
|
2004-08-03 04:03:01 +00:00
|
|
|
///
|
|
|
|
/// Returns "true" if mail was sent OK, "emailstop" if email was blocked by user
|
|
|
|
/// and "false" if there was another sort of error.
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-05-31 09:20:39 +00:00
|
|
|
global $CFG, $_SERVER;
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-02-09 08:49:54 +00:00
|
|
|
global $course; // This is a bit of an ugly hack to be gotten rid of later
|
|
|
|
if (!empty($course->lang)) { // Course language is defined
|
|
|
|
$CFG->courselang = $course->lang;
|
|
|
|
}
|
|
|
|
|
2002-05-24 06:38:37 +00:00
|
|
|
include_once("$CFG->libdir/phpmailer/class.phpmailer.php");
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-02-20 10:27:24 +00:00
|
|
|
if (empty($user)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($user->emailstop)) {
|
2004-08-03 04:03:01 +00:00
|
|
|
return 'emailstop';
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
2004-04-01 10:11:20 +00:00
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
$mail = new phpmailer;
|
|
|
|
|
2004-04-01 10:11:20 +00:00
|
|
|
$mail->Version = "Moodle $CFG->version"; // mailer version
|
2002-05-24 06:38:37 +00:00
|
|
|
$mail->PluginDir = "$CFG->libdir/phpmailer/"; // plugin directory (eg smtp plugin)
|
2002-09-21 08:39:54 +00:00
|
|
|
|
2002-11-14 11:45:14 +00:00
|
|
|
|
2002-11-14 11:48:01 +00:00
|
|
|
if (current_language() != "en") {
|
|
|
|
$mail->CharSet = get_string("thischarset");
|
2002-11-14 11:45:14 +00:00
|
|
|
}
|
|
|
|
|
2003-11-28 10:57:58 +00:00
|
|
|
if ($CFG->smtphosts == "qmail") {
|
|
|
|
$mail->IsQmail(); // use Qmail system
|
|
|
|
|
|
|
|
} else if (empty($CFG->smtphosts)) {
|
|
|
|
$mail->IsMail(); // use PHP mail() = sendmail
|
|
|
|
|
|
|
|
} else {
|
2002-06-07 03:57:38 +00:00
|
|
|
$mail->IsSMTP(); // use SMTP directly
|
2003-10-28 14:35:00 +00:00
|
|
|
if ($CFG->debug > 7) {
|
|
|
|
echo "<pre>\n";
|
|
|
|
$mail->SMTPDebug = true;
|
|
|
|
}
|
2002-06-07 03:57:38 +00:00
|
|
|
$mail->Host = "$CFG->smtphosts"; // specify main and backup servers
|
2002-11-05 06:06:18 +00:00
|
|
|
|
|
|
|
if ($CFG->smtpuser) { // Use SMTP authentication
|
|
|
|
$mail->SMTPAuth = true;
|
|
|
|
$mail->Username = $CFG->smtpuser;
|
|
|
|
$mail->Password = $CFG->smtppass;
|
|
|
|
}
|
2002-06-06 15:21:30 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2003-07-27 13:14:00 +00:00
|
|
|
$adminuser = get_admin();
|
|
|
|
|
|
|
|
$mail->Sender = "$adminuser->email";
|
|
|
|
|
2004-06-25 07:38:44 +00:00
|
|
|
if (is_string($from)) { // So we can pass whatever we want if there is need
|
|
|
|
$mail->From = $CFG->noreplyaddress;
|
2004-06-29 07:01:34 +00:00
|
|
|
$mail->FromName = $from;
|
2004-06-25 07:38:44 +00:00
|
|
|
} else if ($usetrueaddress and $from->maildisplay) {
|
2004-06-06 12:49:55 +00:00
|
|
|
$mail->From = "$from->email";
|
|
|
|
$mail->FromName = fullname($from);
|
|
|
|
} else {
|
|
|
|
$mail->From = "$CFG->noreplyaddress";
|
2004-06-29 07:01:34 +00:00
|
|
|
$mail->FromName = fullname($from);
|
2004-06-06 12:49:55 +00:00
|
|
|
}
|
2002-05-24 06:38:37 +00:00
|
|
|
$mail->Subject = stripslashes($subject);
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-04-01 10:11:20 +00:00
|
|
|
$mail->AddAddress("$user->email", fullname($user) );
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-05-13 03:09:47 +00:00
|
|
|
$mail->WordWrap = 79; // set word wrap
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-05-07 03:29:11 +00:00
|
|
|
if (!empty($from->precedence)) {
|
|
|
|
$mail->Precedence = $from->precedence; // set precedence level eg "bulk" "list" or "junk"
|
|
|
|
}
|
|
|
|
|
2002-05-24 06:38:37 +00:00
|
|
|
if ($messagehtml) {
|
|
|
|
$mail->IsHTML(true);
|
2003-08-08 03:39:25 +00:00
|
|
|
$mail->Encoding = "quoted-printable"; // Encoding to use
|
2002-05-24 06:38:37 +00:00
|
|
|
$mail->Body = $messagehtml;
|
2002-06-07 03:54:39 +00:00
|
|
|
$mail->AltBody = "\n$messagetext\n";
|
2002-05-24 06:38:37 +00:00
|
|
|
} else {
|
|
|
|
$mail->IsHTML(false);
|
2002-06-07 03:54:39 +00:00
|
|
|
$mail->Body = "\n$messagetext\n";
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2002-05-24 06:38:37 +00:00
|
|
|
if ($attachment && $attachname) {
|
|
|
|
if (ereg( "\\.\\." ,$attachment )) { // Security check for ".." in dir path
|
2003-11-28 10:53:07 +00:00
|
|
|
$mail->AddAddress("$adminuser->email", fullname($adminuser) );
|
2002-05-31 09:20:39 +00:00
|
|
|
$mail->AddStringAttachment("Error in attachment. User attempted to attach a filename with a unsafe name.", "error.txt", "8bit", "text/plain");
|
2002-05-24 06:38:37 +00:00
|
|
|
} else {
|
|
|
|
include_once("$CFG->dirroot/files/mimetypes.php");
|
|
|
|
$mimetype = mimeinfo("type", $attachname);
|
|
|
|
$mail->AddAttachment("$CFG->dataroot/$attachment", "$attachname", "base64", "$mimetype");
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2002-05-24 06:38:37 +00:00
|
|
|
if ($mail->Send()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2004-08-24 14:26:35 +00:00
|
|
|
mtrace("ERROR: $mail->ErrorInfo");
|
2004-08-29 14:15:40 +00:00
|
|
|
add_to_log(SITEID, "library", "mailer", $_SERVER["REQUEST_URI"], "ERROR: $mail->ErrorInfo");
|
2001-11-22 06:23:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-23 03:07:01 +00:00
|
|
|
function reset_password_and_mail($user) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$site = get_site();
|
|
|
|
$from = get_admin();
|
|
|
|
|
|
|
|
$newpassword = generate_password();
|
|
|
|
|
|
|
|
if (! set_field("user", "password", md5($newpassword), "id", $user->id) ) {
|
|
|
|
error("Could not set user password!");
|
|
|
|
}
|
|
|
|
|
|
|
|
$a->firstname = $user->firstname;
|
|
|
|
$a->sitename = $site->fullname;
|
|
|
|
$a->username = $user->username;
|
|
|
|
$a->newpassword = $newpassword;
|
|
|
|
$a->link = "$CFG->wwwroot/login/change_password.php";
|
2003-11-28 10:53:07 +00:00
|
|
|
$a->signoff = fullname($from, true)." ($from->email)";
|
2002-12-23 03:07:01 +00:00
|
|
|
|
|
|
|
$message = get_string("newpasswordtext", "", $a);
|
|
|
|
|
|
|
|
$subject = "$site->fullname: ".get_string("changedpassword");
|
|
|
|
|
|
|
|
return email_to_user($user, $from, $subject, $message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function send_confirmation_email($user) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$site = get_site();
|
|
|
|
$from = get_admin();
|
|
|
|
|
|
|
|
$data->firstname = $user->firstname;
|
|
|
|
$data->sitename = $site->fullname;
|
|
|
|
$data->link = "$CFG->wwwroot/login/confirm.php?p=$user->secret&s=$user->username";
|
2003-11-28 10:53:07 +00:00
|
|
|
$data->admin = fullname($from)." ($from->email)";
|
2002-12-23 03:07:01 +00:00
|
|
|
|
|
|
|
$message = get_string("emailconfirmation", "", $data);
|
2003-05-04 03:00:52 +00:00
|
|
|
$subject = get_string("emailconfirmationsubject", "", $site->fullname);
|
2002-12-23 03:07:01 +00:00
|
|
|
|
2004-05-13 03:09:47 +00:00
|
|
|
$messagehtml = text_to_html($message, false, false, true);
|
|
|
|
|
|
|
|
return email_to_user($user, $from, $subject, $message, $messagehtml);
|
2002-12-23 03:07:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-05-04 03:00:52 +00:00
|
|
|
function send_password_change_confirmation_email($user) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$site = get_site();
|
|
|
|
$from = get_admin();
|
|
|
|
|
|
|
|
$data->firstname = $user->firstname;
|
|
|
|
$data->sitename = $site->fullname;
|
|
|
|
$data->link = "$CFG->wwwroot/login/forgot_password.php?p=$user->secret&s=$user->username";
|
2003-11-28 10:53:07 +00:00
|
|
|
$data->admin = fullname($from)." ($from->email)";
|
2003-05-04 03:00:52 +00:00
|
|
|
|
|
|
|
$message = get_string("emailpasswordconfirmation", "", $data);
|
|
|
|
$subject = get_string("emailpasswordconfirmationsubject", "", $site->fullname);
|
|
|
|
|
|
|
|
return email_to_user($user, $from, $subject, $message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-23 03:07:01 +00:00
|
|
|
|
2002-05-24 06:38:37 +00:00
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
/// FILE HANDLING /////////////////////////////////////////////
|
|
|
|
|
2004-02-20 14:12:49 +00:00
|
|
|
|
2004-08-12 15:54:43 +00:00
|
|
|
function make_upload_directory($directory, $shownotices=true) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// $directory = a string of directory names under $CFG->dataroot
|
|
|
|
/// eg stuff/assignment/1
|
|
|
|
/// Returns full directory if successful, false if not
|
2002-08-04 16:20:30 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$currdir = $CFG->dataroot;
|
2003-01-19 05:54:08 +00:00
|
|
|
|
2003-01-20 14:03:11 +00:00
|
|
|
umask(0000);
|
|
|
|
|
2002-08-04 16:20:30 +00:00
|
|
|
if (!file_exists($currdir)) {
|
2003-01-20 14:03:11 +00:00
|
|
|
if (! mkdir($currdir, $CFG->directorypermissions)) {
|
2004-08-12 15:54:43 +00:00
|
|
|
if ($shownotices) {
|
|
|
|
notify("ERROR: You need to create the directory $currdir with web server write access");
|
|
|
|
}
|
2002-08-04 16:20:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-20 14:12:49 +00:00
|
|
|
$dirarray = explode("/", $directory);
|
2002-08-04 16:20:30 +00:00
|
|
|
|
|
|
|
foreach ($dirarray as $dir) {
|
|
|
|
$currdir = "$currdir/$dir";
|
|
|
|
if (! file_exists($currdir)) {
|
2003-01-20 14:03:11 +00:00
|
|
|
if (! mkdir($currdir, $CFG->directorypermissions)) {
|
2004-08-12 15:54:43 +00:00
|
|
|
if ($shownotices) {
|
|
|
|
notify("ERROR: Could not find or create a directory ($currdir)");
|
|
|
|
}
|
2002-08-04 16:20:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2003-04-23 16:51:26 +00:00
|
|
|
@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it
|
2002-08-04 16:20:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $currdir;
|
|
|
|
}
|
2004-02-20 14:12:49 +00:00
|
|
|
|
|
|
|
|
2002-08-28 13:07:10 +00:00
|
|
|
function make_mod_upload_directory($courseid) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Makes an upload directory for a particular module
|
2002-08-28 13:07:10 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (! $moddata = make_upload_directory("$courseid/$CFG->moddata")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$strreadme = get_string("readme");
|
|
|
|
|
|
|
|
if (file_exists("$CFG->dirroot/lang/$CFG->lang/docs/module_files.txt")) {
|
|
|
|
copy("$CFG->dirroot/lang/$CFG->lang/docs/module_files.txt", "$moddata/$strreadme.txt");
|
|
|
|
} else {
|
|
|
|
copy("$CFG->dirroot/lang/en/docs/module_files.txt", "$moddata/$strreadme.txt");
|
|
|
|
}
|
|
|
|
return $moddata;
|
|
|
|
}
|
|
|
|
|
2002-08-04 16:20:30 +00:00
|
|
|
|
2002-09-01 14:29:17 +00:00
|
|
|
function valid_uploaded_file($newfile) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Returns current name of file on disk if true
|
2002-12-29 17:32:32 +00:00
|
|
|
if (empty($newfile)) {
|
|
|
|
return "";
|
|
|
|
}
|
2002-09-01 14:29:17 +00:00
|
|
|
if (is_uploaded_file($newfile['tmp_name']) and $newfile['size'] > 0) {
|
|
|
|
return $newfile['tmp_name'];
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-06 18:02:35 +00:00
|
|
|
function get_max_upload_file_size($sitebytes=0, $coursebytes=0, $modulebytes=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Returns the maximum size for uploading files
|
2003-12-02 14:34:24 +00:00
|
|
|
/// There are seven possible upload limits:
|
2003-10-06 18:02:35 +00:00
|
|
|
///
|
|
|
|
/// 1) in Apache using LimitRequestBody (no way of checking or changing this)
|
|
|
|
/// 2) in php.ini for 'upload_max_filesize' (can not be changed inside PHP)
|
|
|
|
/// 3) in .htaccess for 'upload_max_filesize' (can not be changed inside PHP)
|
2003-12-02 14:34:24 +00:00
|
|
|
/// 4) in php.ini for 'post_max_size' (can not be changed inside PHP)
|
|
|
|
/// 5) by the Moodle admin in $CFG->maxbytes
|
|
|
|
/// 6) by the teacher in the current course $course->maxbytes
|
|
|
|
/// 7) by the teacher for the current module, eg $assignment->maxbytes
|
2003-10-06 18:02:35 +00:00
|
|
|
///
|
|
|
|
/// These last two are passed to this function as arguments (in bytes).
|
|
|
|
/// Anything defined as 0 is ignored.
|
|
|
|
/// The smallest of all the non-zero numbers is returned.
|
|
|
|
|
2002-09-01 14:29:17 +00:00
|
|
|
if (! $filesize = ini_get("upload_max_filesize")) {
|
|
|
|
$filesize = "5M";
|
|
|
|
}
|
2003-10-06 18:02:35 +00:00
|
|
|
$minimumsize = get_real_size($filesize);
|
|
|
|
|
2003-12-02 14:34:24 +00:00
|
|
|
if ($postsize = ini_get("post_max_size")) {
|
|
|
|
$postsize = get_real_size($postsize);
|
|
|
|
if ($postsize < $minimumsize) {
|
|
|
|
$minimumsize = $postsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-06 18:02:35 +00:00
|
|
|
if ($sitebytes and $sitebytes < $minimumsize) {
|
|
|
|
$minimumsize = $sitebytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($coursebytes and $coursebytes < $minimumsize) {
|
|
|
|
$minimumsize = $coursebytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($modulebytes and $modulebytes < $minimumsize) {
|
|
|
|
$minimumsize = $modulebytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $minimumsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_max_upload_sizes($sitebytes=0, $coursebytes=0, $modulebytes=0) {
|
2004-04-01 10:11:20 +00:00
|
|
|
/// Related to the above function - this function returns an
|
|
|
|
/// array of possible sizes in an array, translated to the
|
2003-10-06 18:02:35 +00:00
|
|
|
/// local language.
|
|
|
|
|
|
|
|
if (!$maxsize = get_max_upload_file_size($sitebytes, $coursebytes, $modulebytes)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$filesize[$maxsize] = display_size($maxsize);
|
|
|
|
|
2004-04-01 10:11:20 +00:00
|
|
|
$sizelist = array(10240, 51200, 102400, 512000, 1048576, 2097152,
|
2003-10-06 18:02:35 +00:00
|
|
|
5242880, 10485760, 20971520, 52428800, 104857600);
|
|
|
|
|
|
|
|
foreach ($sizelist as $sizebytes) {
|
|
|
|
if ($sizebytes < $maxsize) {
|
|
|
|
$filesize[$sizebytes] = display_size($sizebytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
krsort($filesize, SORT_NUMERIC);
|
|
|
|
|
|
|
|
return $filesize;
|
2002-09-01 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2004-05-16 08:52:32 +00:00
|
|
|
function get_directory_list($rootdir, $excludefile="", $descend=true, $getdirs=false, $getfiles=true) {
|
2004-04-01 10:11:20 +00:00
|
|
|
/// Returns an array with all the filenames in
|
2002-12-20 14:44:14 +00:00
|
|
|
/// all subdirectories, relative to the given rootdir.
|
|
|
|
/// If excludefile is defined, then that file/directory is ignored
|
2004-05-16 08:52:32 +00:00
|
|
|
/// If getdirs is true, then (sub)directories are included in the output
|
|
|
|
/// If getfiles is true, then files are included in the output
|
|
|
|
/// (at least one of these must be true!)
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
$dirs = array();
|
|
|
|
|
2004-05-16 08:52:32 +00:00
|
|
|
if (!$getdirs and !$getfiles) { // Nothing to show
|
2003-08-17 14:16:08 +00:00
|
|
|
return $dirs;
|
|
|
|
}
|
|
|
|
|
2004-05-16 08:52:32 +00:00
|
|
|
if (!is_dir($rootdir)) { // Must be a directory
|
|
|
|
return $dirs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$dir = opendir($rootdir)) { // Can't open it for some reason
|
2002-11-10 07:37:15 +00:00
|
|
|
return $dirs;
|
|
|
|
}
|
|
|
|
|
2004-04-17 14:01:53 +00:00
|
|
|
while (false !== ($file = readdir($dir))) {
|
2003-01-28 03:34:26 +00:00
|
|
|
$firstchar = substr($file, 0, 1);
|
|
|
|
if ($firstchar == "." or $file == "CVS" or $file == $excludefile) {
|
|
|
|
continue;
|
|
|
|
}
|
2004-05-02 15:31:23 +00:00
|
|
|
$fullfile = "$rootdir/$file";
|
2004-05-04 01:21:17 +00:00
|
|
|
if (filetype($fullfile) == "dir") {
|
2004-05-16 08:52:32 +00:00
|
|
|
if ($getdirs) {
|
2004-05-02 15:31:23 +00:00
|
|
|
$dirs[] = $file;
|
|
|
|
}
|
2004-05-04 01:21:17 +00:00
|
|
|
if ($descend) {
|
2004-05-16 08:52:32 +00:00
|
|
|
$subdirs = get_directory_list($fullfile, $excludefile, $descend, $getdirs, $getfiles);
|
2004-05-04 01:21:17 +00:00
|
|
|
foreach ($subdirs as $subdir) {
|
|
|
|
$dirs[] = "$file/$subdir";
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
2004-05-16 08:52:32 +00:00
|
|
|
} else if ($getfiles) {
|
2003-01-28 03:34:26 +00:00
|
|
|
$dirs[] = $file;
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
}
|
2002-09-01 14:29:17 +00:00
|
|
|
closedir($dir);
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-09-07 14:57:33 +00:00
|
|
|
asort($dirs);
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
return $dirs;
|
|
|
|
}
|
|
|
|
|
2004-05-16 08:52:32 +00:00
|
|
|
function get_directory_size($rootdir, $excludefile="") {
|
|
|
|
/// Adds up all the files in a directory and works out the size
|
|
|
|
|
|
|
|
$size = 0;
|
|
|
|
|
|
|
|
if (!is_dir($rootdir)) { // Must be a directory
|
|
|
|
return $dirs;
|
|
|
|
}
|
|
|
|
|
2004-05-22 04:02:02 +00:00
|
|
|
if (!$dir = @opendir($rootdir)) { // Can't open it for some reason
|
2004-05-16 08:52:32 +00:00
|
|
|
return $dirs;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (false !== ($file = readdir($dir))) {
|
|
|
|
$firstchar = substr($file, 0, 1);
|
|
|
|
if ($firstchar == "." or $file == "CVS" or $file == $excludefile) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$fullfile = "$rootdir/$file";
|
|
|
|
if (filetype($fullfile) == "dir") {
|
|
|
|
$size += get_directory_size($fullfile, $excludefile);
|
|
|
|
} else {
|
|
|
|
$size += filesize($fullfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dir);
|
|
|
|
|
|
|
|
return $size;
|
|
|
|
}
|
|
|
|
|
2002-08-07 08:26:48 +00:00
|
|
|
function get_real_size($size=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Converts numbers like 10M into bytes
|
2002-08-07 08:26:48 +00:00
|
|
|
if (!$size) {
|
2004-04-01 10:11:20 +00:00
|
|
|
return 0;
|
2002-08-07 08:26:48 +00:00
|
|
|
}
|
|
|
|
$scan['MB'] = 1048576;
|
2002-11-28 02:41:56 +00:00
|
|
|
$scan['Mb'] = 1048576;
|
2002-08-07 08:26:48 +00:00
|
|
|
$scan['M'] = 1048576;
|
2003-10-13 03:16:25 +00:00
|
|
|
$scan['m'] = 1048576;
|
2002-08-07 08:26:48 +00:00
|
|
|
$scan['KB'] = 1024;
|
2002-11-28 02:41:56 +00:00
|
|
|
$scan['Kb'] = 1024;
|
2002-08-07 08:26:48 +00:00
|
|
|
$scan['K'] = 1024;
|
2003-10-13 03:16:25 +00:00
|
|
|
$scan['k'] = 1024;
|
2002-08-07 08:26:48 +00:00
|
|
|
|
|
|
|
while (list($key) = each($scan)) {
|
|
|
|
if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) {
|
|
|
|
$size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $size;
|
|
|
|
}
|
|
|
|
|
2002-09-01 14:29:17 +00:00
|
|
|
function display_size($size) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Converts bytes into display form
|
2003-10-06 18:02:35 +00:00
|
|
|
|
|
|
|
static $gb,$mb,$kb,$b;
|
|
|
|
|
|
|
|
if (empty($gb)) {
|
|
|
|
$gb = get_string('sizegb');
|
|
|
|
$mb = get_string('sizemb');
|
|
|
|
$kb = get_string('sizekb');
|
|
|
|
$b = get_string('sizeb');
|
|
|
|
}
|
|
|
|
|
2002-09-01 14:29:17 +00:00
|
|
|
if ($size >= 1073741824) {
|
2003-10-06 18:02:35 +00:00
|
|
|
$size = round($size / 1073741824 * 10) / 10 . $gb;
|
2002-09-01 14:29:17 +00:00
|
|
|
} else if ($size >= 1048576) {
|
2003-10-06 18:02:35 +00:00
|
|
|
$size = round($size / 1048576 * 10) / 10 . $mb;
|
2002-09-01 14:29:17 +00:00
|
|
|
} else if ($size >= 1024) {
|
2003-10-06 18:02:35 +00:00
|
|
|
$size = round($size / 1024 * 10) / 10 . $kb;
|
2004-04-01 10:11:20 +00:00
|
|
|
} else {
|
2003-10-06 18:02:35 +00:00
|
|
|
$size = $size ." $b";
|
2002-09-01 14:29:17 +00:00
|
|
|
}
|
|
|
|
return $size;
|
|
|
|
}
|
|
|
|
|
2002-08-04 16:20:30 +00:00
|
|
|
function clean_filename($string) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Cleans a given filename by removing suspicious or troublesome characters
|
2004-04-16 06:30:01 +00:00
|
|
|
/// Only these are allowed:
|
2004-06-25 07:38:44 +00:00
|
|
|
/// alphanumeric _ - .
|
2004-04-16 06:30:01 +00:00
|
|
|
|
|
|
|
$string = eregi_replace("\.\.+", "", $string);
|
2004-05-07 17:07:31 +00:00
|
|
|
$string = preg_replace('/[^\.a-zA-Z\d\_-]/','_', $string ); // only allowed chars
|
2004-04-16 06:30:01 +00:00
|
|
|
$string = eregi_replace("_+", "_", $string);
|
|
|
|
return $string;
|
2002-08-04 16:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-27 08:47:27 +00:00
|
|
|
/// STRING TRANSLATION ////////////////////////////////////////
|
|
|
|
|
2002-10-02 10:33:05 +00:00
|
|
|
function current_language() {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Returns the code for the current language
|
2003-01-19 12:32:55 +00:00
|
|
|
global $CFG, $USER, $SESSION;
|
2002-10-02 10:33:05 +00:00
|
|
|
|
2004-02-15 14:33:13 +00:00
|
|
|
if (!empty($CFG->courselang)) { // Course language can override all other settings for this page
|
2004-02-09 07:31:04 +00:00
|
|
|
return $CFG->courselang;
|
|
|
|
|
2004-02-15 14:33:13 +00:00
|
|
|
} else if (!empty($SESSION->lang)) { // Session language can override other settings
|
2003-01-19 12:32:55 +00:00
|
|
|
return $SESSION->lang;
|
|
|
|
|
2004-02-15 14:33:13 +00:00
|
|
|
} else if (!empty($USER->lang)) { // User language can override site language
|
2002-10-02 10:33:05 +00:00
|
|
|
return $USER->lang;
|
2003-01-19 12:32:55 +00:00
|
|
|
|
2002-10-02 10:33:05 +00:00
|
|
|
} else {
|
|
|
|
return $CFG->lang;
|
|
|
|
}
|
|
|
|
}
|
2002-07-04 07:49:38 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function print_string($identifier, $module="", $a=NULL) {
|
|
|
|
/// Given a string to translate - prints it out.
|
|
|
|
echo get_string($identifier, $module, $a);
|
|
|
|
}
|
|
|
|
|
2002-07-11 05:30:10 +00:00
|
|
|
function get_string($identifier, $module="", $a=NULL) {
|
2004-04-01 10:11:20 +00:00
|
|
|
/// Return the translated string specified by $identifier as
|
2002-12-20 14:44:14 +00:00
|
|
|
/// for $module. Uses the same format files as STphp.
|
|
|
|
/// $a is an object, string or number that can be used
|
|
|
|
/// within translation strings
|
|
|
|
///
|
|
|
|
/// eg "hello \$a->firstname \$a->lastname"
|
|
|
|
/// or "hello \$a"
|
2002-06-27 08:47:27 +00:00
|
|
|
|
2002-10-02 10:33:05 +00:00
|
|
|
global $CFG;
|
2002-06-27 08:47:27 +00:00
|
|
|
|
2004-02-10 08:10:04 +00:00
|
|
|
global $course; /// Not a nice hack, but quick
|
2004-02-15 14:34:13 +00:00
|
|
|
if (empty($CFG->courselang)) {
|
|
|
|
if (!empty($course->lang)) {
|
|
|
|
$CFG->courselang = $course->lang;
|
|
|
|
}
|
2004-02-10 08:10:04 +00:00
|
|
|
}
|
|
|
|
|
2002-10-02 10:33:05 +00:00
|
|
|
$lang = current_language();
|
2002-06-27 08:47:27 +00:00
|
|
|
|
2002-08-02 09:24:50 +00:00
|
|
|
if ($module == "") {
|
|
|
|
$module = "moodle";
|
2002-06-27 08:47:27 +00:00
|
|
|
}
|
|
|
|
|
2002-08-02 09:24:50 +00:00
|
|
|
$langpath = "$CFG->dirroot/lang";
|
|
|
|
$langfile = "$langpath/$lang/$module.php";
|
2002-06-27 08:47:27 +00:00
|
|
|
|
2003-06-12 03:27:24 +00:00
|
|
|
// Look for the string - if found then return it
|
|
|
|
|
|
|
|
if (file_exists($langfile)) {
|
|
|
|
if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) {
|
|
|
|
eval($result);
|
|
|
|
return $resultstring;
|
2002-06-27 08:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-24 09:19:59 +00:00
|
|
|
// If it's a module, then look within the module pack itself mod/xxxx/lang/en/module.php
|
|
|
|
|
|
|
|
if ($module != "moodle") {
|
|
|
|
$modlangpath = "$CFG->dirroot/mod/$module/lang";
|
|
|
|
$langfile = "$modlangpath/$lang/$module.php";
|
|
|
|
if (file_exists($langfile)) {
|
|
|
|
if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) {
|
|
|
|
eval($result);
|
|
|
|
return $resultstring;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-06-27 08:47:27 +00:00
|
|
|
|
2004-05-24 09:19:59 +00:00
|
|
|
// If the preferred language was English we can abort now
|
2004-04-01 10:11:20 +00:00
|
|
|
if ($lang == "en") {
|
2003-06-12 03:27:24 +00:00
|
|
|
return "[[$identifier]]";
|
|
|
|
}
|
2002-06-27 08:47:27 +00:00
|
|
|
|
2003-06-12 03:27:24 +00:00
|
|
|
// Is a parent language defined? If so, try it.
|
2004-04-01 10:11:20 +00:00
|
|
|
|
2003-06-12 03:27:24 +00:00
|
|
|
if ($result = get_string_from_file("parentlanguage", "$langpath/$lang/moodle.php", "\$parentlang")) {
|
|
|
|
eval($result);
|
|
|
|
if (!empty($parentlang)) {
|
|
|
|
$langfile = "$langpath/$parentlang/$module.php";
|
|
|
|
if (file_exists($langfile)) {
|
|
|
|
if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) {
|
|
|
|
eval($result);
|
|
|
|
return $resultstring;
|
|
|
|
}
|
2002-06-27 08:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-06-12 03:27:24 +00:00
|
|
|
|
|
|
|
// Our only remaining option is to try English
|
|
|
|
|
|
|
|
$langfile = "$langpath/en/$module.php";
|
|
|
|
if (!file_exists($langfile)) {
|
|
|
|
return "ERROR: No lang file ($langpath/en/$module.php)!";
|
|
|
|
}
|
|
|
|
if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) {
|
|
|
|
eval($result);
|
|
|
|
return $resultstring;
|
|
|
|
}
|
|
|
|
|
2004-05-24 09:19:59 +00:00
|
|
|
// If it's a module, then look within the module pack itself mod/xxxx/lang/en/module.php
|
|
|
|
|
|
|
|
if ($module != "moodle") {
|
|
|
|
$langfile = "$modlangpath/en/$module.php";
|
|
|
|
if (file_exists($langfile)) {
|
|
|
|
if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) {
|
|
|
|
eval($result);
|
|
|
|
return $resultstring;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-12 03:27:24 +00:00
|
|
|
return "[[$identifier]]"; // Last resort
|
2002-06-27 08:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function get_string_from_file($identifier, $langfile, $destination) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// This function is only used from get_string().
|
2003-07-03 12:19:54 +00:00
|
|
|
|
|
|
|
static $strings; // Keep the strings cached in memory.
|
|
|
|
|
|
|
|
if (empty($strings[$langfile])) {
|
2004-05-16 09:28:41 +00:00
|
|
|
$string = array();
|
2003-07-03 12:19:54 +00:00
|
|
|
include ($langfile);
|
|
|
|
$strings[$langfile] = $string;
|
|
|
|
} else {
|
|
|
|
$string = &$strings[$langfile];
|
|
|
|
}
|
2002-06-27 08:47:27 +00:00
|
|
|
|
|
|
|
if (!isset ($string[$identifier])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-07-11 05:30:10 +00:00
|
|
|
return "$destination = sprintf(\"".$string[$identifier]."\");";
|
2002-06-27 08:47:27 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-07-25 13:48:55 +00:00
|
|
|
function get_strings($array, $module='') {
|
|
|
|
/// Converts an array of strings
|
|
|
|
|
|
|
|
$string = NULL;
|
|
|
|
foreach ($array as $item) {
|
|
|
|
$string->$item = get_string($item, $module);
|
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-09-14 03:43:24 +00:00
|
|
|
function get_list_of_languages() {
|
|
|
|
/// Returns a list of language codes and their full names
|
|
|
|
global $CFG;
|
|
|
|
|
2003-05-03 06:50:29 +00:00
|
|
|
$languages = array();
|
|
|
|
|
|
|
|
if (!empty($CFG->langlist)) { // use admin's list of languages
|
|
|
|
$langlist = explode(',', $CFG->langlist);
|
|
|
|
foreach ($langlist as $lang) {
|
|
|
|
if (file_exists("$CFG->dirroot/lang/$lang/moodle.php")) {
|
|
|
|
include("$CFG->dirroot/lang/$lang/moodle.php");
|
|
|
|
$languages[$lang] = $string["thislanguage"]." ($lang)";
|
|
|
|
unset($string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!$langdirs = get_list_of_plugins("lang")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach ($langdirs as $lang) {
|
|
|
|
include("$CFG->dirroot/lang/$lang/moodle.php");
|
|
|
|
$languages[$lang] = $string["thislanguage"]." ($lang)";
|
|
|
|
unset($string);
|
|
|
|
}
|
2002-09-14 03:43:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $languages;
|
|
|
|
}
|
|
|
|
|
2003-09-26 08:04:48 +00:00
|
|
|
function get_list_of_countries() {
|
|
|
|
/// Returns a list of country names in the current language
|
|
|
|
global $CFG, $USER;
|
|
|
|
|
|
|
|
$lang = current_language();
|
|
|
|
|
|
|
|
if (!file_exists("$CFG->dirroot/lang/$lang/countries.php")) {
|
2003-09-26 08:21:11 +00:00
|
|
|
if ($parentlang = get_string("parentlanguage")) {
|
|
|
|
if (file_exists("$CFG->dirroot/lang/$parentlang/countries.php")) {
|
|
|
|
$lang = $parentlang;
|
|
|
|
} else {
|
|
|
|
$lang = "en"; // countries.php must exist in this pack
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$lang = "en"; // countries.php must exist in this pack
|
|
|
|
}
|
2003-09-26 08:04:48 +00:00
|
|
|
}
|
|
|
|
|
2004-04-01 10:11:20 +00:00
|
|
|
include("$CFG->dirroot/lang/$lang/countries.php");
|
2003-09-26 08:04:48 +00:00
|
|
|
|
2003-11-17 02:19:13 +00:00
|
|
|
if (!empty($string)) {
|
|
|
|
asort($string);
|
|
|
|
}
|
2003-09-26 08:04:48 +00:00
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2003-11-07 15:11:34 +00:00
|
|
|
function get_list_of_pixnames() {
|
|
|
|
/// Returns a list of picture names in the current language
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$lang = current_language();
|
|
|
|
|
|
|
|
if (!file_exists("$CFG->dirroot/lang/$lang/pix.php")) {
|
|
|
|
if ($parentlang = get_string("parentlanguage")) {
|
|
|
|
if (file_exists("$CFG->dirroot/lang/$parentlang/pix.php")) {
|
|
|
|
$lang = $parentlang;
|
|
|
|
} else {
|
|
|
|
$lang = "en"; // countries.php must exist in this pack
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$lang = "en"; // countries.php must exist in this pack
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-01 10:11:20 +00:00
|
|
|
include_once("$CFG->dirroot/lang/$lang/pix.php");
|
2003-11-07 15:11:34 +00:00
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2002-12-29 14:41:03 +00:00
|
|
|
function document_file($file, $include=true) {
|
|
|
|
/// Can include a given document file (depends on second
|
|
|
|
/// parameter) or just return info about it
|
|
|
|
|
2003-01-19 12:37:56 +00:00
|
|
|
global $CFG;
|
2002-12-29 14:41:03 +00:00
|
|
|
|
2003-06-12 11:55:29 +00:00
|
|
|
$file = clean_filename($file);
|
|
|
|
|
2002-12-29 14:41:03 +00:00
|
|
|
if (empty($file)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-06-12 11:55:29 +00:00
|
|
|
$langs = array(current_language(), get_string("parentlanguage"), "en");
|
2002-12-29 14:41:03 +00:00
|
|
|
|
2003-06-12 11:55:29 +00:00
|
|
|
foreach ($langs as $lang) {
|
|
|
|
$info->filepath = "$CFG->dirroot/lang/$lang/docs/$file";
|
|
|
|
$info->urlpath = "$CFG->wwwroot/lang/$lang/docs/$file";
|
2002-12-29 14:41:03 +00:00
|
|
|
|
2003-06-12 11:55:29 +00:00
|
|
|
if (file_exists($info->filepath)) {
|
|
|
|
if ($include) {
|
|
|
|
include($info->filepath);
|
|
|
|
}
|
|
|
|
return $info;
|
2003-01-01 12:03:15 +00:00
|
|
|
}
|
2002-12-29 14:41:03 +00:00
|
|
|
}
|
|
|
|
|
2003-06-12 11:55:29 +00:00
|
|
|
return false;
|
2002-12-29 14:41:03 +00:00
|
|
|
}
|
|
|
|
|
2002-09-14 03:43:24 +00:00
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
/// ENCRYPTION ////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function rc4encrypt($data) {
|
|
|
|
$password = "nfgjeingjk";
|
|
|
|
return endecrypt($password, $data, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
function rc4decrypt($data) {
|
|
|
|
$password = "nfgjeingjk";
|
|
|
|
return endecrypt($password, $data, "de");
|
|
|
|
}
|
|
|
|
|
|
|
|
function endecrypt ($pwd, $data, $case) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Based on a class by Mukul Sabharwal [mukulsabharwal@yahoo.com]
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
if ($case == 'de') {
|
|
|
|
$data = urldecode($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
$key[] = "";
|
|
|
|
$box[] = "";
|
|
|
|
$temp_swap = "";
|
|
|
|
$pwd_length = 0;
|
|
|
|
|
|
|
|
$pwd_length = strlen($pwd);
|
|
|
|
|
|
|
|
for ($i = 0; $i <= 255; $i++) {
|
|
|
|
$key[$i] = ord(substr($pwd, ($i % $pwd_length), 1));
|
|
|
|
$box[$i] = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
$x = 0;
|
|
|
|
|
|
|
|
for ($i = 0; $i <= 255; $i++) {
|
|
|
|
$x = ($x + $box[$i] + $key[$i]) % 256;
|
|
|
|
$temp_swap = $box[$i];
|
|
|
|
$box[$i] = $box[$x];
|
|
|
|
$box[$x] = $temp_swap;
|
|
|
|
}
|
|
|
|
|
|
|
|
$temp = "";
|
|
|
|
$k = "";
|
|
|
|
|
|
|
|
$cipherby = "";
|
|
|
|
$cipher = "";
|
|
|
|
|
|
|
|
$a = 0;
|
|
|
|
$j = 0;
|
|
|
|
|
|
|
|
for ($i = 0; $i < strlen($data); $i++) {
|
|
|
|
$a = ($a + 1) % 256;
|
|
|
|
$j = ($j + $box[$a]) % 256;
|
|
|
|
$temp = $box[$a];
|
|
|
|
$box[$a] = $box[$j];
|
|
|
|
$box[$j] = $temp;
|
|
|
|
$k = $box[(($box[$a] + $box[$j]) % 256)];
|
|
|
|
$cipherby = ord(substr($data, $i, 1)) ^ $k;
|
|
|
|
$cipher .= chr($cipherby);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($case == 'de') {
|
|
|
|
$cipher = urldecode(urlencode($cipher));
|
|
|
|
} else {
|
|
|
|
$cipher = urlencode($cipher);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cipher;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-17 09:47:45 +00:00
|
|
|
/// CALENDAR MANAGEMENT ////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
function add_event($event) {
|
|
|
|
/// call this function to add an event to the calendar table
|
|
|
|
/// and to call any calendar plugins
|
|
|
|
/// The function returns the id number of the resulting record
|
|
|
|
/// The object event should include the following:
|
|
|
|
/// $event->name Name for the event
|
|
|
|
/// $event->description Description of the event (defaults to '')
|
|
|
|
/// $event->courseid The id of the course this event belongs to (0 = all courses)
|
|
|
|
/// $event->groupid The id of the group this event belongs to (0 = no group)
|
|
|
|
/// $event->userid The id of the user this event belongs to (0 = no user)
|
|
|
|
/// $event->modulename Name of the module that creates this event
|
|
|
|
/// $event->instance Instance of the module that owns this event
|
|
|
|
/// $event->eventtype The type info together with the module info could
|
|
|
|
/// be used by calendar plugins to decide how to display event
|
|
|
|
/// $event->timestart Timestamp for start of event
|
|
|
|
/// $event->timeduration Duration (defaults to zero)
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$event->timemodified = time();
|
2004-04-01 10:11:20 +00:00
|
|
|
|
2004-01-17 09:47:45 +00:00
|
|
|
if (!$event->id = insert_record("event", $event)) {
|
|
|
|
return false;
|
|
|
|
}
|
2004-04-01 10:11:20 +00:00
|
|
|
|
2004-01-17 09:47:45 +00:00
|
|
|
if (!empty($CFG->calendar)) { // call the add_event function of the selected calendar
|
|
|
|
if (file_exists("$CFG->dirroot/calendar/$CFG->calendar/lib.php")) {
|
|
|
|
include_once("$CFG->dirroot/calendar/$CFG->calendar/lib.php");
|
|
|
|
$calendar_add_event = $CFG->calendar.'_add_event';
|
|
|
|
if (function_exists($calendar_add_event)) {
|
|
|
|
$calendar_add_event($event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-04-01 10:11:20 +00:00
|
|
|
|
2004-01-17 09:47:45 +00:00
|
|
|
return $event->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function update_event($event) {
|
|
|
|
/// call this function to update an event in the calendar table
|
|
|
|
/// the event will be identified by the id field of the $event object
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$event->timemodified = time();
|
2004-04-01 10:11:20 +00:00
|
|
|
|
2004-01-17 09:47:45 +00:00
|
|
|
if (!empty($CFG->calendar)) { // call the update_event function of the selected calendar
|
|
|
|
if (file_exists("$CFG->dirroot/calendar/$CFG->calendar/lib.php")) {
|
|
|
|
include_once("$CFG->dirroot/calendar/$CFG->calendar/lib.php");
|
|
|
|
$calendar_update_event = $CFG->calendar.'_update_event';
|
|
|
|
if (function_exists($calendar_update_event)) {
|
|
|
|
$calendar_update_event($event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return update_record("event", $event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function delete_event($id) {
|
|
|
|
/// call this function to delete the event with id $id from calendar table
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!empty($CFG->calendar)) { // call the delete_event function of the selected calendar
|
|
|
|
if (file_exists("$CFG->dirroot/calendar/$CFG->calendar/lib.php")) {
|
|
|
|
include_once("$CFG->dirroot/calendar/$CFG->calendar/lib.php");
|
|
|
|
$calendar_delete_event = $CFG->calendar.'_delete_event';
|
|
|
|
if (function_exists($calendar_delete_event)) {
|
|
|
|
$calendar_delete_event($id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return delete_records("event", 'id', $id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-05 07:07:56 +00:00
|
|
|
function hide_event($event) {
|
|
|
|
/// call this function to hide an event in the calendar table
|
|
|
|
/// the event will be identified by the id field of the $event object
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!empty($CFG->calendar)) { // call the update_event function of the selected calendar
|
|
|
|
if (file_exists("$CFG->dirroot/calendar/$CFG->calendar/lib.php")) {
|
|
|
|
include_once("$CFG->dirroot/calendar/$CFG->calendar/lib.php");
|
|
|
|
$calendar_hide_event = $CFG->calendar.'_hide_event';
|
|
|
|
if (function_exists($calendar_hide_event)) {
|
|
|
|
$calendar_hide_event($event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return set_field('event', 'visible', 0, 'id', $event->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function show_event($event) {
|
|
|
|
/// call this function to unhide an event in the calendar table
|
|
|
|
/// the event will be identified by the id field of the $event object
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!empty($CFG->calendar)) { // call the update_event function of the selected calendar
|
|
|
|
if (file_exists("$CFG->dirroot/calendar/$CFG->calendar/lib.php")) {
|
|
|
|
include_once("$CFG->dirroot/calendar/$CFG->calendar/lib.php");
|
|
|
|
$calendar_show_event = $CFG->calendar.'_show_event';
|
|
|
|
if (function_exists($calendar_show_event)) {
|
|
|
|
$calendar_show_event($event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return set_field('event', 'visible', 1, 'id', $event->id);
|
|
|
|
}
|
2004-01-17 09:47:45 +00:00
|
|
|
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
/// ENVIRONMENT CHECKING ////////////////////////////////////////////////////////////
|
2002-08-17 13:01:06 +00:00
|
|
|
|
2004-04-20 15:14:15 +00:00
|
|
|
function get_list_of_plugins($plugin="mod", $exclude="") {
|
2002-12-23 03:07:01 +00:00
|
|
|
/// Lists plugin directories within some directory
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$basedir = opendir("$CFG->dirroot/$plugin");
|
|
|
|
while ($dir = readdir($basedir)) {
|
2003-01-28 03:34:26 +00:00
|
|
|
$firstchar = substr($dir, 0, 1);
|
2004-04-20 15:14:15 +00:00
|
|
|
if ($firstchar == "." or $dir == "CVS" or $dir == "_vti_cnf" or $dir == $exclude) {
|
2002-12-23 03:07:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (filetype("$CFG->dirroot/$plugin/$dir") != "dir") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$plugins[] = $dir;
|
|
|
|
}
|
|
|
|
if ($plugins) {
|
|
|
|
asort($plugins);
|
|
|
|
}
|
|
|
|
return $plugins;
|
|
|
|
}
|
|
|
|
|
2002-08-23 02:14:19 +00:00
|
|
|
function check_php_version($version="4.1.0") {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Returns true is the current version of PHP is greater that the specified one
|
2002-08-23 02:14:19 +00:00
|
|
|
$minversion = intval(str_replace(".", "", $version));
|
|
|
|
$curversion = intval(str_replace(".", "", phpversion()));
|
|
|
|
return ($curversion >= $minversion);
|
|
|
|
}
|
|
|
|
|
2002-10-10 07:26:10 +00:00
|
|
|
function check_browser_version($brand="MSIE", $version=5.5) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Checks to see if is a browser matches the specified
|
|
|
|
/// brand and is equal or better version.
|
2002-10-10 07:26:10 +00:00
|
|
|
|
2003-10-29 08:06:11 +00:00
|
|
|
$agent = $_SERVER["HTTP_USER_AGENT"];
|
|
|
|
|
|
|
|
if (empty($agent)) {
|
2002-10-10 07:26:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2003-10-29 08:06:11 +00:00
|
|
|
|
|
|
|
switch ($brand) {
|
|
|
|
|
|
|
|
case "Gecko": /// Gecko based browsers
|
|
|
|
|
|
|
|
if (substr_count($agent, "Camino")) { // MacOS X Camino not supported.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the proper string - Gecko/CCYYMMDD Vendor/Version
|
|
|
|
if (ereg("^([a-zA-Z]+)/([0-9]+\.[0-9]+) \((.*)\) (.*)$", $agent, $match)) {
|
|
|
|
if (ereg("^([Gecko]+)/([0-9]+)",$match[4], $reldate)) {
|
|
|
|
if ($reldate[2] > $version) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case "MSIE": /// Internet Explorer
|
|
|
|
|
2004-02-10 14:20:44 +00:00
|
|
|
if (strpos($agent, 'Opera')) { // Reject Opera
|
|
|
|
return false;
|
|
|
|
}
|
2003-10-29 08:06:11 +00:00
|
|
|
$string = explode(";", $agent);
|
|
|
|
if (!isset($string[1])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$string = explode(" ", trim($string[1]));
|
|
|
|
if (!isset($string[0]) and !isset($string[1])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($string[0] == $brand and (float)$string[1] >= $version ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2002-10-10 07:26:10 +00:00
|
|
|
}
|
2003-10-29 08:06:11 +00:00
|
|
|
|
2002-10-10 07:26:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-05-17 02:05:10 +00:00
|
|
|
function ini_get_bool($ini_get_arg) {
|
|
|
|
/// This function makes the return value of ini_get consistent if you are
|
|
|
|
/// setting server directives through the .htaccess file in apache.
|
|
|
|
/// Current behavior for value set from php.ini On = 1, Off = [blank]
|
|
|
|
/// Current behavior for value set from .htaccess On = On, Off = Off
|
|
|
|
/// Contributed by jdell@unr.edu
|
|
|
|
|
|
|
|
$temp = ini_get($ini_get_arg);
|
|
|
|
|
|
|
|
if ($temp == "1" or strtolower($temp) == "on") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-10-10 07:26:10 +00:00
|
|
|
function can_use_richtext_editor() {
|
2003-11-03 15:17:21 +00:00
|
|
|
/// Compatibility stub to provide backward compatibility
|
|
|
|
return can_use_html_editor();
|
|
|
|
}
|
|
|
|
|
|
|
|
function can_use_html_editor() {
|
2003-10-29 08:06:11 +00:00
|
|
|
/// Is the HTML editor enabled? This depends on site and user
|
|
|
|
/// settings, as well as the current browser being used.
|
2003-11-03 15:17:21 +00:00
|
|
|
/// Returns false is editor is not being used, otherwise
|
|
|
|
/// returns "MSIE" or "Gecko"
|
2003-10-29 08:06:11 +00:00
|
|
|
|
2002-10-10 07:26:10 +00:00
|
|
|
global $USER, $CFG;
|
2003-10-29 08:06:11 +00:00
|
|
|
|
2003-01-06 13:55:37 +00:00
|
|
|
if (!empty($USER->htmleditor) and !empty($CFG->htmleditor)) {
|
2003-10-29 08:06:11 +00:00
|
|
|
if (check_browser_version("MSIE", 5.5)) {
|
2003-11-03 15:17:21 +00:00
|
|
|
return "MSIE";
|
|
|
|
} else if (check_browser_version("Gecko", 20030516)) {
|
|
|
|
return "Gecko";
|
2003-10-29 08:06:11 +00:00
|
|
|
}
|
2002-10-16 04:53:44 +00:00
|
|
|
}
|
|
|
|
return false;
|
2002-10-10 07:26:10 +00:00
|
|
|
}
|
|
|
|
|
2003-11-03 15:17:21 +00:00
|
|
|
|
2002-09-19 12:01:55 +00:00
|
|
|
function check_gd_version() {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Hack to find out the GD version by parsing phpinfo output
|
2003-05-01 23:12:05 +00:00
|
|
|
$gdversion = 0;
|
2002-09-19 12:01:55 +00:00
|
|
|
|
2003-05-01 23:12:05 +00:00
|
|
|
if (function_exists('gd_info')){
|
|
|
|
$gd_info = gd_info();
|
2003-05-19 03:27:13 +00:00
|
|
|
if (substr_count($gd_info['GD Version'], "2.")) {
|
2003-05-01 23:12:05 +00:00
|
|
|
$gdversion = 2;
|
2003-05-19 03:27:13 +00:00
|
|
|
} else if (substr_count($gd_info['GD Version'], "1.")) {
|
|
|
|
$gdversion = 1;
|
2003-05-01 23:12:05 +00:00
|
|
|
}
|
2003-05-19 03:27:13 +00:00
|
|
|
|
2003-05-01 23:12:05 +00:00
|
|
|
} else {
|
|
|
|
ob_start();
|
|
|
|
phpinfo(8);
|
|
|
|
$phpinfo = ob_get_contents();
|
|
|
|
ob_end_clean();
|
2002-09-19 12:01:55 +00:00
|
|
|
|
2003-05-01 23:12:05 +00:00
|
|
|
$phpinfo = explode("\n",$phpinfo);
|
2002-09-19 12:01:55 +00:00
|
|
|
|
2002-12-28 14:45:54 +00:00
|
|
|
|
2003-05-01 23:12:05 +00:00
|
|
|
foreach ($phpinfo as $text) {
|
|
|
|
$parts = explode('</td>',$text);
|
|
|
|
foreach ($parts as $key => $val) {
|
|
|
|
$parts[$key] = trim(strip_tags($val));
|
|
|
|
}
|
|
|
|
if ($parts[0] == "GD Version") {
|
|
|
|
if (substr_count($parts[1], "2.0")) {
|
|
|
|
$parts[1] = "2.0";
|
|
|
|
}
|
|
|
|
$gdversion = intval($parts[1]);
|
2002-12-28 14:45:54 +00:00
|
|
|
}
|
2002-09-19 12:01:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $gdversion; // 1, 2 or 0
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-10-10 07:26:10 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function moodle_needs_upgrading() {
|
|
|
|
/// Checks version numbers of Main code and all modules to see
|
|
|
|
/// if there are any mismatches ... returns true or false
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
include_once("$CFG->dirroot/version.php"); # defines $version and upgrades
|
2004-04-01 10:11:20 +00:00
|
|
|
if ($CFG->version) {
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($version > $CFG->version) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ($mods = get_list_of_plugins("mod")) {
|
|
|
|
foreach ($mods as $mod) {
|
|
|
|
$fullmod = "$CFG->dirroot/mod/$mod";
|
|
|
|
unset($module);
|
2003-01-08 09:07:07 +00:00
|
|
|
if (!is_readable("$fullmod/version.php")) {
|
|
|
|
notify("Module '$mod' is not readable - check permissions");
|
|
|
|
continue;
|
|
|
|
}
|
2002-12-20 14:44:14 +00:00
|
|
|
include_once("$fullmod/version.php"); # defines $module with version etc
|
|
|
|
if ($currmodule = get_record("modules", "name", $mod)) {
|
|
|
|
if ($module->version > $currmodule->version) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// MISCELLANEOUS ////////////////////////////////////////////////////////////////////
|
|
|
|
|
2004-07-25 13:48:55 +00:00
|
|
|
function notify_login_failures() {
|
2004-07-29 14:54:25 +00:00
|
|
|
global $CFG, $db;
|
2004-07-25 13:48:55 +00:00
|
|
|
|
|
|
|
// notify admin users or admin user of any failed logins (since last notification).
|
|
|
|
switch ($CFG->notifyloginfailures) {
|
|
|
|
case 'mainadmin' :
|
|
|
|
$recip = array(get_admin());
|
|
|
|
break;
|
|
|
|
case 'alladmins':
|
|
|
|
$recip = get_admins();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($CFG->lastnotifyfailure)) {
|
|
|
|
$CFG->lastnotifyfailure=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need to deal with the threshold stuff first.
|
2004-07-25 14:39:19 +00:00
|
|
|
if (empty($CFG->notifyloginthreshold)) {
|
|
|
|
$CFG->notifyloginthreshold = 10; // default to something sensible.
|
2004-07-25 13:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$notifyipsrs = $db->Execute("SELECT ip FROM {$CFG->prefix}log WHERE time > {$CFG->lastnotifyfailure}
|
2004-07-25 14:39:19 +00:00
|
|
|
AND module='login' AND action='error' GROUP BY ip HAVING count(*) > $CFG->notifyloginthreshold");
|
2004-07-25 13:48:55 +00:00
|
|
|
|
|
|
|
$notifyusersrs = $db->Execute("SELECT info FROM {$CFG->prefix}log WHERE time > {$CFG->lastnotifyfailure}
|
2004-07-25 14:39:19 +00:00
|
|
|
AND module='login' AND action='error' GROUP BY info HAVING count(*) > $CFG->notifyloginthreshold");
|
2004-07-25 13:48:55 +00:00
|
|
|
|
|
|
|
if ($notifyipsrs) {
|
2004-07-29 14:54:25 +00:00
|
|
|
$ipstr = '';
|
2004-07-25 13:48:55 +00:00
|
|
|
while ($row = $notifyipsrs->FetchRow()) {
|
|
|
|
$ipstr .= "'".$row['ip']."',";
|
|
|
|
}
|
|
|
|
$ipstr = substr($ipstr,0,strlen($ipstr)-1);
|
|
|
|
}
|
|
|
|
if ($notifyusersrs) {
|
2004-07-29 14:54:25 +00:00
|
|
|
$userstr = '';
|
2004-07-25 13:48:55 +00:00
|
|
|
while ($row = $notifyusersrs->FetchRow()) {
|
|
|
|
$userstr .= "'".$row['info']."',";
|
|
|
|
}
|
|
|
|
$userstr = substr($userstr,0,strlen($userstr)-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen($userstr) > 0 || strlen($ipstr) > 0) {
|
|
|
|
$count = 0;
|
|
|
|
$logs = get_logs("time > {$CFG->lastnotifyfailure} AND module='login' AND action='error' "
|
|
|
|
.((strlen($ipstr) > 0 && strlen($userstr) > 0) ? " AND ( ip IN ($ipstr) OR info IN ($userstr) ) "
|
|
|
|
: ((strlen($ipstr) != 0) ? " AND ip IN ($ipstr) " : " AND info IN ($userstr) ")),"l.time DESC","","",$count);
|
|
|
|
|
|
|
|
// if we haven't run in the last hour and we have something useful to report and we are actually supposed to be reporting to somebody
|
2004-07-29 14:54:25 +00:00
|
|
|
if (is_array($recip) and count($recip) > 0 and ((time() - (60 * 60)) > $CFG->lastnotifyfailure)
|
2004-07-25 13:48:55 +00:00
|
|
|
and is_array($logs) and count($logs) > 0) {
|
2004-07-29 14:54:25 +00:00
|
|
|
|
|
|
|
$message = '';
|
2004-07-25 13:48:55 +00:00
|
|
|
$site = get_site();
|
|
|
|
$subject = get_string('notifyloginfailuressubject','',$site->fullname);
|
|
|
|
$message .= get_string('notifyloginfailuresmessagestart','',$CFG->wwwroot)
|
|
|
|
.(($CFG->lastnotifyfailure != 0) ? '('.userdate($CFG->lastnotifyfailure).')' : '')."\n\n";
|
|
|
|
foreach ($logs as $log) {
|
2004-08-03 06:53:04 +00:00
|
|
|
$log->time = userdate($log->time);
|
|
|
|
$message .= get_string('notifyloginfailuresmessage','',$log)."\n";
|
2004-07-25 13:48:55 +00:00
|
|
|
}
|
|
|
|
$message .= "\n\n".get_string('notifyloginfailuresmessageend','',$CFG->wwwroot)."\n\n";
|
|
|
|
foreach ($recip as $admin) {
|
2004-08-07 06:05:37 +00:00
|
|
|
mtrace("Emailing $admin->username about ".count($logs)." failed login attempts");
|
2004-07-25 13:48:55 +00:00
|
|
|
email_to_user($admin,get_admin(),$subject,$message);
|
|
|
|
}
|
|
|
|
$conf->name = "lastnotifyfailure";
|
|
|
|
$conf->value = time();
|
|
|
|
if ($current = get_record("config", "name", "lastnotifyfailure")) {
|
|
|
|
$conf->id = $current->id;
|
|
|
|
if (! update_record("config", $conf)) {
|
2004-08-07 06:05:37 +00:00
|
|
|
mtrace("Could not update last notify time");
|
2004-07-25 13:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (! insert_record("config", $conf)) {
|
2004-08-07 06:05:37 +00:00
|
|
|
mtrace("Could not set last notify time");
|
2004-07-25 13:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-21 13:07:44 +00:00
|
|
|
function moodle_setlocale($locale='') {
|
|
|
|
|
|
|
|
global $SESSION, $USER, $CFG;
|
|
|
|
|
|
|
|
if ($locale) {
|
|
|
|
$CFG->locale = $locale;
|
|
|
|
} else if (!empty($CFG->courselang) and ($CFG->courselang != $CFG->lang) ) {
|
|
|
|
$CFG->locale = get_string('locale');
|
|
|
|
} else if (!empty($SESSION->lang) and ($SESSION->lang != $CFG->lang) ) {
|
|
|
|
$CFG->locale = get_string('locale');
|
|
|
|
} else if (!empty($USER->lang) and ($USER->lang != $CFG->lang) ) {
|
|
|
|
$CFG->locale = get_string('locale');
|
|
|
|
} else if (empty($CFG->locale)) {
|
|
|
|
$CFG->locale = get_string('locale');
|
|
|
|
set_config('locale', $CFG->locale); // cache it to save lookups in future
|
|
|
|
}
|
|
|
|
setlocale (LC_TIME, $CFG->locale);
|
|
|
|
setlocale (LC_COLLATE, $CFG->locale);
|
|
|
|
|
|
|
|
if ($CFG->locale != 'tr_TR') { // To workaround a well-known PHP bug with Turkish
|
|
|
|
setlocale (LC_CTYPE, $CFG->locale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-21 16:39:43 +00:00
|
|
|
function moodle_strtolower ($string, $encoding='') {
|
|
|
|
/// Converts string to lowercase using most compatible function available
|
|
|
|
if (function_exists('mb_strtolower')) {
|
|
|
|
if($encoding===''){
|
|
|
|
return mb_strtolower($string); //use multibyte support with default encoding
|
|
|
|
} else {
|
2003-04-24 17:21:54 +00:00
|
|
|
return mb_strtolower($string,$encoding); //use given encoding
|
2004-04-01 10:11:20 +00:00
|
|
|
}
|
2003-04-21 16:39:43 +00:00
|
|
|
} else {
|
|
|
|
return strtolower($string); // use common function what rely on current locale setting
|
2004-04-01 10:11:20 +00:00
|
|
|
}
|
2003-04-21 16:39:43 +00:00
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function count_words($string) {
|
|
|
|
/// Words are defined as things between whitespace
|
|
|
|
$string = strip_tags($string);
|
|
|
|
return count(preg_split("/\w\b/", $string)) - 1;
|
|
|
|
}
|
|
|
|
|
2002-12-23 03:07:01 +00:00
|
|
|
function random_string ($length=15) {
|
|
|
|
$pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
$pool .= "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
$pool .= "0123456789";
|
|
|
|
$poollen = strlen($pool);
|
|
|
|
mt_srand ((double) microtime() * 1000000);
|
|
|
|
$string = "";
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
|
$string .= substr($pool, (mt_rand()%($poollen)), 1);
|
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function getweek ($startdate, $thedate) {
|
|
|
|
/// Given dates in seconds, how many weeks is the date from startdate
|
2004-04-01 10:11:20 +00:00
|
|
|
/// The first week is 1, the second 2 etc ...
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($thedate < $startdate) { // error
|
2004-04-01 10:11:20 +00:00
|
|
|
return 0;
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return floor(($thedate - $startdate) / 604800.0) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generate_password($maxlen=10) {
|
|
|
|
/// returns a randomly generated password of length $maxlen. inspired by
|
2004-04-01 10:11:20 +00:00
|
|
|
/// http://www.phpbuilder.com/columns/jesus19990502.php3
|
2002-12-20 14:44:14 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$fillers = "1234567890!$-+";
|
|
|
|
$wordlist = file($CFG->wordlist);
|
|
|
|
|
|
|
|
srand((double) microtime() * 1000000);
|
|
|
|
$word1 = trim($wordlist[rand(0, count($wordlist) - 1)]);
|
|
|
|
$word2 = trim($wordlist[rand(0, count($wordlist) - 1)]);
|
|
|
|
$filler1 = $fillers[rand(0, strlen($fillers) - 1)];
|
|
|
|
|
|
|
|
return substr($word1 . $filler1 . $word2, 0, $maxlen);
|
|
|
|
}
|
|
|
|
|
2004-03-25 15:29:40 +00:00
|
|
|
function format_float($num, $places=1) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Given a float, prints it nicely
|
|
|
|
return sprintf("%.$places"."f", $num);
|
|
|
|
}
|
|
|
|
|
2003-02-24 09:34:10 +00:00
|
|
|
function swapshuffle($array) {
|
|
|
|
/// Given a simple array, this shuffles it up just like shuffle()
|
|
|
|
/// Unlike PHP's shuffle() ihis function works on any machine.
|
|
|
|
|
|
|
|
srand ((double) microtime() * 10000000);
|
|
|
|
$last = count($array) - 1;
|
|
|
|
for ($i=0;$i<=$last;$i++) {
|
|
|
|
$from = rand(0,$last);
|
|
|
|
$curr = $array[$i];
|
|
|
|
$array[$i] = $array[$from];
|
|
|
|
$array[$from] = $curr;
|
2004-04-01 10:11:20 +00:00
|
|
|
}
|
2003-02-24 09:34:10 +00:00
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2003-04-10 13:45:07 +00:00
|
|
|
function swapshuffle_assoc($array) {
|
|
|
|
/// Like swapshuffle, but works on associative arrays
|
|
|
|
|
|
|
|
$newkeys = swapshuffle(array_keys($array));
|
|
|
|
foreach ($newkeys as $newkey) {
|
|
|
|
$newarray[$newkey] = $array[$newkey];
|
|
|
|
}
|
|
|
|
return $newarray;
|
|
|
|
}
|
|
|
|
|
2003-02-24 09:34:10 +00:00
|
|
|
function draw_rand_array($array, $draws) {
|
2004-04-01 10:11:20 +00:00
|
|
|
/// Given an arbitrary array, and a number of draws,
|
|
|
|
/// this function returns an array with that amount
|
2003-02-24 09:34:10 +00:00
|
|
|
/// of items. The indexes are retained.
|
|
|
|
|
|
|
|
srand ((double) microtime() * 10000000);
|
|
|
|
|
|
|
|
$return = array();
|
|
|
|
|
|
|
|
$last = count($array);
|
|
|
|
|
|
|
|
if ($draws > $last) {
|
|
|
|
$draws = $last;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ($draws > 0) {
|
|
|
|
$last--;
|
|
|
|
|
|
|
|
$keys = array_keys($array);
|
|
|
|
$rand = rand(0, $last);
|
|
|
|
|
|
|
|
$return[$keys[$rand]] = $array[$keys[$rand]];
|
|
|
|
unset($array[$keys[$rand]]);
|
2004-04-01 10:11:20 +00:00
|
|
|
|
2003-02-24 09:34:10 +00:00
|
|
|
$draws--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
2004-04-01 10:11:20 +00:00
|
|
|
}
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2003-04-27 08:13:45 +00:00
|
|
|
function microtime_diff($a, $b) {
|
|
|
|
list($a_dec, $a_sec) = explode(" ", $a);
|
|
|
|
list($b_dec, $b_sec) = explode(" ", $b);
|
|
|
|
return $b_sec - $a_sec + $b_dec - $a_dec;
|
|
|
|
}
|
|
|
|
|
2003-08-15 13:59:24 +00:00
|
|
|
function make_menu_from_list($list, $separator=",") {
|
2004-04-01 10:11:20 +00:00
|
|
|
/// Given a list (eg a,b,c,d,e) this function returns
|
2003-08-15 13:59:24 +00:00
|
|
|
/// an array of 1->a, 2->b, 3->c etc
|
|
|
|
|
|
|
|
$array = array_reverse(explode($separator, $list), true);
|
|
|
|
foreach ($array as $key => $item) {
|
|
|
|
$outarray[$key+1] = trim($item);
|
|
|
|
}
|
|
|
|
return $outarray;
|
|
|
|
}
|
|
|
|
|
2003-08-18 05:10:35 +00:00
|
|
|
function make_grades_menu($gradingtype) {
|
|
|
|
/// Creates an array that represents all the current grades that
|
|
|
|
/// can be chosen using the given grading type. Negative numbers
|
|
|
|
/// are scales, zero is no grade, and positive numbers are maximum
|
|
|
|
/// grades.
|
|
|
|
|
|
|
|
$grades = array();
|
|
|
|
if ($gradingtype < 0) {
|
|
|
|
if ($scale = get_record("scale", "id", - $gradingtype)) {
|
|
|
|
return make_menu_from_list($scale->scale);
|
|
|
|
}
|
|
|
|
} else if ($gradingtype > 0) {
|
|
|
|
for ($i=$gradingtype; $i>=0; $i--) {
|
2003-08-18 05:28:30 +00:00
|
|
|
$grades[$i] = "$i / $gradingtype";
|
2003-08-18 05:10:35 +00:00
|
|
|
}
|
|
|
|
return $grades;
|
|
|
|
}
|
|
|
|
return $grades;
|
|
|
|
}
|
|
|
|
|
2004-05-16 00:36:00 +00:00
|
|
|
function course_scale_used($courseid,$scaleid) {
|
|
|
|
////This function returns the nummber of activities
|
|
|
|
////using scaleid in a courseid
|
|
|
|
|
|
|
|
global $CFG;
|
2004-06-25 07:38:44 +00:00
|
|
|
|
2004-05-16 00:36:00 +00:00
|
|
|
$return = 0;
|
|
|
|
|
|
|
|
if (!empty($scaleid)) {
|
|
|
|
if ($cms = get_course_mods($courseid)) {
|
|
|
|
foreach ($cms as $cm) {
|
|
|
|
//Check cm->name/lib.php exists
|
|
|
|
if (file_exists($CFG->dirroot.'/mod/'.$cm->modname.'/lib.php')) {
|
|
|
|
include_once($CFG->dirroot.'/mod/'.$cm->modname.'/lib.php');
|
|
|
|
$function_name = $cm->modname.'_scale_used';
|
|
|
|
if (function_exists($function_name)) {
|
|
|
|
if ($function_name($cm->instance,$scaleid)) {
|
|
|
|
$return++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function site_scale_used($scaleid) {
|
2004-06-25 07:38:44 +00:00
|
|
|
////This function returns the nummber of activities
|
2004-05-16 00:36:00 +00:00
|
|
|
////using scaleid in the entire site
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$return = 0;
|
|
|
|
|
|
|
|
if (!empty($scaleid)) {
|
|
|
|
if ($courses = get_courses()) {
|
|
|
|
foreach ($courses as $course) {
|
|
|
|
$return += course_scale_used($course->id,$scaleid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2003-08-23 14:18:44 +00:00
|
|
|
function make_unique_id_code($extra="") {
|
2003-08-23 12:22:27 +00:00
|
|
|
|
|
|
|
$hostname = "unknownhost";
|
|
|
|
if (!empty($_SERVER["HTTP_HOST"])) {
|
|
|
|
$hostname = $_SERVER["HTTP_HOST"];
|
|
|
|
} else if (!empty($_ENV["HTTP_HOST"])) {
|
|
|
|
$hostname = $_ENV["HTTP_HOST"];
|
|
|
|
} else if (!empty($_SERVER["SERVER_NAME"])) {
|
|
|
|
$hostname = $_SERVER["SERVER_NAME"];
|
|
|
|
} else if (!empty($_ENV["SERVER_NAME"])) {
|
|
|
|
$hostname = $_ENV["SERVER_NAME"];
|
|
|
|
}
|
|
|
|
|
2003-08-23 12:25:50 +00:00
|
|
|
$date = gmdate("ymdHis");
|
2003-08-23 12:22:27 +00:00
|
|
|
|
|
|
|
$random = random_string(6);
|
|
|
|
|
2003-08-23 14:18:44 +00:00
|
|
|
if ($extra) {
|
|
|
|
return "$hostname+$date+$random+$extra";
|
|
|
|
} else {
|
|
|
|
return "$hostname+$date+$random";
|
|
|
|
}
|
2003-08-23 12:22:27 +00:00
|
|
|
}
|
|
|
|
|
2002-10-10 07:26:10 +00:00
|
|
|
|
2004-07-08 07:52:01 +00:00
|
|
|
/**
|
|
|
|
* Function to check the passed address is within the passed subnet
|
|
|
|
*
|
|
|
|
* The parameter is a comma separated string of subnet definitions.
|
|
|
|
* Subnet strings can be in one of two formats:
|
|
|
|
* 1: xxx.xxx.xxx.xxx/xx
|
|
|
|
* 2: xxx.xxx
|
|
|
|
* Return boolean
|
|
|
|
* Code for type 1 modified from user posted comments by mediator at
|
|
|
|
* http://au.php.net/manual/en/function.ip2long.php
|
|
|
|
*
|
|
|
|
* @param addr the address you are checking
|
|
|
|
* @param subnetstr the string of subnet addresses
|
|
|
|
*/
|
|
|
|
|
|
|
|
function address_in_subnet($addr, $subnetstr) {
|
|
|
|
|
|
|
|
$subnets = explode(",", $subnetstr);
|
|
|
|
$found = false;
|
|
|
|
$addr = trim($addr);
|
|
|
|
|
|
|
|
foreach ($subnets as $subnet) {
|
|
|
|
$subnet = trim($subnet);
|
|
|
|
if (strpos($subnet, "/") !== false) { /// type 1
|
|
|
|
|
|
|
|
list($ip, $mask) = explode('/', $subnet);
|
|
|
|
$mask = 0xffffffff << (32 - $mask);
|
|
|
|
$found = ((ip2long($addr) & $mask) == (ip2long($ip) & $mask));
|
|
|
|
|
|
|
|
} else { /// type 2
|
|
|
|
$found = (strpos($addr, $subnet) === 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($found) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
2004-08-05 17:08:46 +00:00
|
|
|
function mtrace($string, $eol="\n") {
|
|
|
|
// For outputting debugging info
|
2004-08-07 03:59:53 +00:00
|
|
|
|
|
|
|
if (defined('STDOUT')) {
|
|
|
|
fwrite(STDOUT, $string.$eol);
|
|
|
|
} else {
|
|
|
|
echo "$string$eol";
|
|
|
|
}
|
|
|
|
|
2004-08-05 17:08:46 +00:00
|
|
|
flush();
|
|
|
|
}
|
|
|
|
|
2004-09-04 10:54:54 +00:00
|
|
|
function getremoteaddr() {
|
|
|
|
//Returns most reliable client address
|
|
|
|
if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");
|
|
|
|
else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR");
|
|
|
|
else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");
|
|
|
|
else $ip = false; //just in case
|
|
|
|
return $ip;
|
|
|
|
}
|
2004-08-05 17:08:46 +00:00
|
|
|
|
2003-03-14 07:55:22 +00:00
|
|
|
// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
|
2001-11-22 06:23:56 +00:00
|
|
|
?>
|