2004-09-23 02:48:41 +00:00
|
|
|
<?php
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// //
|
|
|
|
// 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
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
2004-09-25 05:29:21 +00:00
|
|
|
* moodlelib.php - Moodle main library
|
2004-09-23 02:48:41 +00:00
|
|
|
*
|
|
|
|
* Main library file of miscellaneous general-purpose Moodle functions.
|
|
|
|
* Other main libraries:
|
2004-09-23 05:10:21 +00:00
|
|
|
* - weblib.php - functions that produce web output
|
|
|
|
* - datalib.php - functions that access the database
|
2004-09-23 02:48:41 +00:00
|
|
|
* @author Martin Dougiamas
|
|
|
|
* @version $Id$
|
2004-09-25 05:29:21 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
2004-09-23 02:48:41 +00:00
|
|
|
* @package moodlecore
|
|
|
|
*/
|
2003-12-30 17:18:06 +00:00
|
|
|
/// CONSTANTS /////////////////////////////////////////////////////////////
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* No groups used?
|
|
|
|
*/
|
2004-04-01 10:11:20 +00:00
|
|
|
define('NOGROUPS', 0);
|
2004-09-23 02:48:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Groups used?
|
|
|
|
*/
|
2003-12-30 17:18:06 +00:00
|
|
|
define('SEPARATEGROUPS', 1);
|
2004-09-23 02:48:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Groups visible?
|
|
|
|
*/
|
2003-12-30 17:18:06 +00:00
|
|
|
define('VISIBLEGROUPS', 2);
|
|
|
|
|
2004-09-28 05:53:08 +00:00
|
|
|
/**
|
|
|
|
* Time constants
|
|
|
|
*/
|
|
|
|
|
|
|
|
define('WEEKSECS', 604800);
|
|
|
|
define('DAYSECS', 86400);
|
|
|
|
define('HOURSECS', 3600);
|
|
|
|
define('MINSECS', 60);
|
|
|
|
define('DAYMINS', 1440);
|
|
|
|
define('HOURMINS', 60);
|
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
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Ensure that a variable is set or display error
|
|
|
|
*
|
|
|
|
* If $var is undefined display an error message using the {@link error()} function.
|
2004-09-28 15:41:36 +00:00
|
|
|
* This function will soon be made obsolete by {@link parameter()}
|
2004-09-23 02:48:41 +00:00
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param mixed $var the variable which may not be set
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2002-12-20 14:44:14 +00:00
|
|
|
function require_variable($var) {
|
|
|
|
/// Variable must be present
|
|
|
|
if (! isset($var)) {
|
2004-09-22 14:39:15 +00:00
|
|
|
error('A required parameter was missing');
|
2002-08-04 16:20:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that a variable is set
|
|
|
|
*
|
|
|
|
* If $var is undefined set it (by reference), otherwise return $var.
|
|
|
|
* This function is very similar to {@link nvl()}
|
2004-09-28 15:41:36 +00:00
|
|
|
* This function will soon be made obsolete by {@link parameter()}
|
2004-09-23 02:48:41 +00:00
|
|
|
*
|
|
|
|
* @param mixed $var the variable which may be unset
|
|
|
|
* @param mixed $default the value to return if $var is unset
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-28 15:41:36 +00:00
|
|
|
/**
|
|
|
|
* Returns a particular value for the named variable, taken from
|
|
|
|
* POST or GET, otherwise returning a given default.
|
|
|
|
*
|
|
|
|
* This function should be used to initialise all values in a script
|
|
|
|
* that are based on parameters. Usually it will be used like this:
|
|
|
|
*
|
|
|
|
* $id = (int)parameter('id');
|
|
|
|
*
|
|
|
|
* @param string $varname the name of the parameter variable we want
|
|
|
|
* @param mixed $default the default value to return if nothing is found
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function parameter($varname, $default=NULL) {
|
|
|
|
|
|
|
|
if (isset($_POST[$varname])) { // POST has precedence
|
|
|
|
return $_POST[$varname];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_GET[$varname])) {
|
|
|
|
return $_GET[$varname];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Set a key in global configuration
|
|
|
|
*
|
2004-09-25 05:29:21 +00:00
|
|
|
* Set a key/value pair in both this session's {@link $CFG} global variable
|
2004-09-23 02:48:41 +00:00
|
|
|
* and in the 'config' database table for future sessions.
|
|
|
|
*
|
|
|
|
* @param string $name the key to set
|
|
|
|
* @param string $value the value to set
|
|
|
|
* @uses $CFG
|
|
|
|
* @return bool
|
|
|
|
*/
|
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;
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
|
2003-08-18 17:41:06 +00:00
|
|
|
$CFG->$name = $value; // So it's defined for this invocation at least
|
2002-11-10 08:43:44 +00:00
|
|
|
|
2004-09-22 14:39:15 +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;
|
2004-09-22 14:39:15 +00:00
|
|
|
return insert_record('config', $config);
|
2002-07-29 06:21:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Refresh current $USER session global variable with all their current preferences.
|
|
|
|
* @uses $USER
|
|
|
|
*/
|
2004-01-28 14:14:19 +00:00
|
|
|
function reload_user_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;
|
|
|
|
}
|
2004-09-21 06:32:36 +00:00
|
|
|
} else {
|
|
|
|
//return empty preference array to hold new values
|
|
|
|
$USER->preference = array();
|
2004-09-25 01:29:37 +00:00
|
|
|
}
|
2004-01-28 14:14:19 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Sets a preference for the current user
|
|
|
|
* Optionally, can set a preference for a different user object
|
|
|
|
* @uses $USER
|
|
|
|
* @todo Add a better description and include usage examples.
|
|
|
|
* @param string $name The key to set as preference for the specified user
|
|
|
|
* @param string $value The value to set forthe $name key in the specified user's record
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $userid A moodle user ID
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Add inline links to $USER and user functions in above line.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2004-09-23 03:56:53 +00:00
|
|
|
function set_user_preference($name, $value, $userid=NULL) {
|
2004-01-28 14:14:19 +00:00
|
|
|
|
|
|
|
global $USER;
|
|
|
|
|
2004-09-23 03:56:53 +00:00
|
|
|
if (empty($userid)){
|
|
|
|
$userid = $USER->id;
|
2004-09-20 09:08:57 +00:00
|
|
|
}
|
|
|
|
|
2004-01-28 14:14:19 +00:00
|
|
|
if (empty($name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-09-23 03:56:53 +00:00
|
|
|
if ($preference = get_record('user_preferences', 'userid', $userid, 'name', $name)) {
|
2004-09-22 14:39:15 +00:00
|
|
|
if (set_field('user_preferences', 'value', $value, 'id', $preference->id)) {
|
2004-09-20 09:08:57 +00:00
|
|
|
$user->preference[$name] = $value;
|
2004-01-28 14:47:39 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2004-01-28 14:14:19 +00:00
|
|
|
|
|
|
|
} else {
|
2004-09-23 03:56:53 +00:00
|
|
|
$preference->userid = $userid;
|
2004-01-28 14:14:19 +00:00
|
|
|
$preference->name = $name;
|
|
|
|
$preference->value = (string)$value;
|
2004-01-28 14:47:39 +00:00
|
|
|
if (insert_record('user_preferences', $preference)) {
|
2004-09-20 09:08:57 +00:00
|
|
|
$user->preference[$name] = $value;
|
2004-01-28 14:14:19 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-23 04:15:19 +00:00
|
|
|
/**
|
|
|
|
* Unsets a preference completely by deleting it from the database
|
|
|
|
* Optionally, can set a preference for a different user id
|
|
|
|
* @uses $USER
|
|
|
|
* @param string $name The key to unset as preference for the specified user
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $userid A moodle user ID
|
2004-09-23 04:15:19 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function unset_user_preference($name, $userid=NULL) {
|
|
|
|
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if (empty($userid)){
|
|
|
|
$userid = $USER->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return delete_records('user_preferences', 'userid', $userid, 'name', $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Sets a whole array of preferences for the current user
|
|
|
|
* @param array $prefarray An array of key/value pairs to be set
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $userid A moodle user ID
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2004-09-23 03:56:53 +00:00
|
|
|
function set_user_preferences($prefarray, $userid=NULL) {
|
|
|
|
|
|
|
|
global $USER;
|
2004-01-28 14:14:19 +00:00
|
|
|
|
|
|
|
if (!is_array($prefarray) or empty($prefarray)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-09-23 03:56:53 +00:00
|
|
|
if (empty($userid)){
|
|
|
|
$userid = $USER->id;
|
|
|
|
}
|
|
|
|
|
2004-01-28 14:14:19 +00:00
|
|
|
$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"
|
2004-09-23 03:56:53 +00:00
|
|
|
$return = set_user_preference($name, $value, $userid) and $return;
|
2004-01-28 14:14:19 +00:00
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* If no arguments are supplied this function will return
|
|
|
|
* all of the current user preferences as an array.
|
|
|
|
* If a name is specified then this function
|
|
|
|
* attempts to return that particular preference value. If
|
|
|
|
* none is found, then the optional value $default is returned,
|
|
|
|
* otherwise NULL.
|
|
|
|
* @param string $name Name of the key to use in finding a preference value
|
|
|
|
* @param string $default Value to be returned if the $name key is not set in the user preferences
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $userid A moodle user ID
|
2004-09-23 02:48:41 +00:00
|
|
|
* @uses $USER
|
|
|
|
* @return string
|
|
|
|
*/
|
2004-09-23 03:56:53 +00:00
|
|
|
function get_user_preferences($name=NULL, $default=NULL, $userid=NULL) {
|
2004-01-28 14:14:19 +00:00
|
|
|
|
|
|
|
global $USER;
|
|
|
|
|
2004-09-23 03:56:53 +00:00
|
|
|
if (empty($userid)) { // assume current 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
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$preference = get_records_menu('user_preferences', 'userid', $userid, 'name', 'name,value');
|
|
|
|
|
|
|
|
if (empty($name)) {
|
|
|
|
return $preference;
|
|
|
|
}
|
|
|
|
if (!isset($preference[$name])) {
|
|
|
|
return $default; // Default value (or NULL)
|
|
|
|
}
|
|
|
|
return $preference[$name]; // The single value
|
2004-01-28 14:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
/// FUNCTIONS FOR HANDLING TIME ////////////////////////////////////////////
|
2002-07-29 06:21:37 +00:00
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* Given date parts in user time produce a GMT timestamp.
|
2004-09-23 02:48:41 +00:00
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $year The year part to create timestamp of.
|
|
|
|
* @param int $month The month part to create timestamp of.
|
|
|
|
* @param int $day The day part to create timestamp of.
|
|
|
|
* @param int $hour The hour part to create timestamp of.
|
|
|
|
* @param int $minute The minute part to create timestamp of.
|
|
|
|
* @param int $second The second part to create timestamp of.
|
|
|
|
* @param int $timezone ?
|
|
|
|
* @return ?
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
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-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
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Given an amount of time in seconds, returns string
|
|
|
|
* formatted nicely as months, days, hours etc as needed
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $totalsecs ?
|
|
|
|
* @param array $str ?
|
2004-09-25 05:29:21 +00:00
|
|
|
* @return string
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
|
|
|
function format_time($totalsecs, $str=NULL) {
|
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
|
2004-09-22 14:39:15 +00:00
|
|
|
$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');
|
2002-10-03 04:04:59 +00:00
|
|
|
}
|
|
|
|
|
2004-09-28 05:53:08 +00:00
|
|
|
$days = floor($totalsecs/DAYSECS);
|
|
|
|
$remainder = $totalsecs - ($days*DAYSECS);
|
|
|
|
$hours = floor($remainder/HOURSECS);
|
|
|
|
$remainder = $remainder - ($hours*HOURSECS);
|
|
|
|
$mins = floor($remainder/MINSECS);
|
|
|
|
$secs = $remainder - ($mins*MINSECS);
|
2002-10-03 04:04:59 +00:00
|
|
|
|
|
|
|
$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;
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$odays = '';
|
|
|
|
$ohours = '';
|
|
|
|
$omins = '';
|
|
|
|
$osecs = '';
|
2002-12-29 17:32:32 +00:00
|
|
|
|
2004-09-22 14:39:15 +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
|
|
|
|
2004-09-22 14:39:15 +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');
|
2002-08-04 16:20:30 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Returns a formatted string that represents a date in user time
|
|
|
|
* <b>WARNING: note that the format is for strftime(), not date().</b>
|
|
|
|
* Because of a bug in most Windows time libraries, we can't use
|
|
|
|
* the nicer %e, so we have to use %d which has leading zeroes.
|
|
|
|
* A lot of the fuss in the function is just getting rid of these leading
|
|
|
|
* zeroes as efficiently as possible.
|
|
|
|
*
|
2004-09-23 05:10:21 +00:00
|
|
|
* If parameter fixday = true (default), then take off leading
|
2004-09-23 02:48:41 +00:00
|
|
|
* zero from %d, else mantain it.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $date ?
|
|
|
|
* @param string $format ?
|
|
|
|
* @param int $timezone ?
|
|
|
|
* @param boolean $fixday If true (default) then the leading
|
|
|
|
* zero from %d is removed. If false then the leading zero is mantained.
|
|
|
|
* @return string
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-09-22 14:39:15 +00:00
|
|
|
function userdate($date, $format='', $timezone=99, $fixday = true) {
|
2002-07-02 07:02:28 +00:00
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($format == '') {
|
|
|
|
$format = get_string('strftimedaydatetime');
|
2002-06-05 14:05:59 +00:00
|
|
|
}
|
2002-12-02 01:35:18 +00:00
|
|
|
|
2004-09-22 14:39:15 +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);
|
2004-09-22 14:39:15 +00:00
|
|
|
$daystring = str_replace(' 0', '', strftime(" %d", $date));
|
|
|
|
$datestring = str_replace('DD', $daystring, $datestring);
|
2002-12-02 01:35:18 +00:00
|
|
|
} else {
|
|
|
|
$datestring = strftime($format, $date);
|
|
|
|
}
|
2002-12-02 01:13:14 +00:00
|
|
|
} else {
|
2004-09-28 05:53:08 +00:00
|
|
|
$date = $date + (int)($timezone * HOURSECS);
|
2002-12-02 01:35:18 +00:00
|
|
|
if ($fixday) {
|
2003-01-03 16:58:37 +00:00
|
|
|
$datestring = gmstrftime($formatnoday, $date);
|
2004-09-22 14:39:15 +00:00
|
|
|
$daystring = str_replace(' 0', '', gmstrftime(" %d", $date));
|
|
|
|
$datestring = str_replace('DD', $daystring, $datestring);
|
2002-12-02 01:35:18 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* Given a $date timestamp in GMT (seconds since epoch),
|
|
|
|
* returns an array that represents the date in user time
|
2004-09-23 02:48:41 +00:00
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $date Timestamp in GMT
|
|
|
|
* @param int $timezone ?
|
|
|
|
* @return array An array that represents the date in user time
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2002-06-05 14:05:59 +00:00
|
|
|
function usergetdate($date, $timezone=99) {
|
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...
|
2004-09-28 05:53:08 +00:00
|
|
|
$date = $date + (int)($timezone * HOURSECS);
|
2004-09-22 14:39:15 +00:00
|
|
|
$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);
|
2002-06-11 04:04:45 +00:00
|
|
|
return $getdate;
|
2002-06-10 09:43:40 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Given a GMT timestamp (seconds since epoch), offsets it by
|
|
|
|
* the timezone. eg 3pm in India is 3pm GMT - 7 * 3600 seconds
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $date Timestamp in GMT
|
|
|
|
* @param int $timezone ?
|
|
|
|
* @return int
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2002-06-10 09:43:40 +00:00
|
|
|
function usertime($date, $timezone=99) {
|
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;
|
|
|
|
}
|
2004-09-28 05:53:08 +00:00
|
|
|
return $date - (int)($timezone * HOURSECS);
|
2002-06-10 09:43:40 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Given a time, return the GMT timestamp of the most recent midnight
|
|
|
|
* for the current user.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $date Timestamp in GMT
|
|
|
|
* @param int $timezone ?
|
|
|
|
* @return ?
|
|
|
|
* @todo Finish documenting this function. Is timezone an int or float?
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2002-06-10 14:01:30 +00:00
|
|
|
function usergetmidnight($date, $timezone=99) {
|
|
|
|
|
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) {
|
2004-09-22 14:39:15 +00:00
|
|
|
return mktime(0, 0, 0, $userdate['mon'], $userdate['mday'], $userdate['year']);
|
2002-08-08 17:28:30 +00:00
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$timemidnight = gmmktime (0, 0, 0, $userdate['mon'], $userdate['mday'], $userdate['year']);
|
2002-06-10 14:01:30 +00:00
|
|
|
return usertime($timemidnight, $timezone); // Time of midnight of this user's day, in GMT
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Returns a string that prints the user's timezone
|
|
|
|
*
|
|
|
|
* @param float $timezone The user's timezone
|
|
|
|
* @return string
|
2004-09-25 01:29:37 +00:00
|
|
|
* @todo is $timezone an int or a float?
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2002-06-10 09:43:40 +00:00
|
|
|
function usertimezone($timezone=99) {
|
|
|
|
|
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) {
|
2004-09-22 14:39:15 +00:00
|
|
|
return 'server time';
|
2002-06-10 09:43:40 +00:00
|
|
|
}
|
|
|
|
if (abs($timezone) < 0.5) {
|
2004-09-22 14:39:15 +00:00
|
|
|
return 'GMT';
|
2002-06-10 09:43:40 +00:00
|
|
|
}
|
|
|
|
if ($timezone > 0) {
|
2004-09-22 14:39:15 +00:00
|
|
|
return 'GMT+'. $timezone;
|
2002-06-10 09:43:40 +00:00
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
return 'GMT'. $timezone;
|
2002-06-10 09:43:40 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @uses $USER
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $tz The user's timezone
|
|
|
|
* @return int
|
|
|
|
* @todo is $tz an int or a float?
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2004-04-01 12:27:37 +00:00
|
|
|
function get_user_timezone($tz = 99) {
|
|
|
|
|
|
|
|
// 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-09-23 02:48:41 +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.
|
|
|
|
* $autologinguest determines whether visitors should automatically be
|
2004-09-25 05:29:21 +00:00
|
|
|
* logged in as guests provide {@link $CFG}->autologinguests is set to 1
|
2004-09-23 02:48:41 +00:00
|
|
|
*
|
|
|
|
* @uses $CFG
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $SESSION
|
2004-09-23 02:48:41 +00:00
|
|
|
* @uses $USER
|
|
|
|
* @uses $FULLME
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses SITEID
|
2004-09-23 02:48:41 +00:00
|
|
|
* @uses $MoodleSession
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $courseid The course in question
|
|
|
|
* @param boolean $autologinguest ?
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-07-01 22:29:04 +00:00
|
|
|
function require_login($courseid=0, $autologinguest=true) {
|
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;
|
2004-09-22 14:39:15 +00:00
|
|
|
if (!empty($_SERVER['HTTP_REFERER'])) {
|
|
|
|
$SESSION->fromurl = $_SERVER['HTTP_REFERER'];
|
2003-01-22 01:55:54 +00:00
|
|
|
}
|
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-09-22 14:39:15 +00:00
|
|
|
redirect($CFG->wwwroot .'/login/index.php'. $loginguest);
|
2004-06-19 16:13:28 +00:00
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
$wwwroot = str_replace('http','https', $CFG->wwwroot);
|
|
|
|
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
|
|
|
|
2004-09-20 09:08:57 +00:00
|
|
|
// check whether the user should be changing password
|
|
|
|
reload_user_preferences();
|
2004-09-23 03:56:53 +00:00
|
|
|
if (!empty($USER->preference['auth_forcepasswordchange'])){
|
2004-09-20 09:08:57 +00:00
|
|
|
if (is_internal_auth() || $CFG->{'auth_'.$USER->auth.'_stdchangepassword'}){
|
2004-09-22 14:39:15 +00:00
|
|
|
redirect($CFG->wwwroot .'/login/change_password.php');
|
2004-09-20 09:08:57 +00:00
|
|
|
} elseif($CFG->changepassword) {
|
|
|
|
redirect($CFG->changepassword);
|
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
error('You cannot proceed without changing your password.
|
2004-09-20 09:08:57 +00:00
|
|
|
However there is no available page for changing it.
|
2004-09-22 14:39:15 +00:00
|
|
|
Please contact your Moodle Administrator.');
|
2004-09-20 09:08:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-22 14:39:15 +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
|
|
|
|
2004-09-27 14:35:37 +00:00
|
|
|
// Make sure current IP matches the one for this session (if required)
|
|
|
|
if (!empty($CFG->tracksessionip)) {
|
|
|
|
if ($USER->sessionIP != md5(getremoteaddr())) {
|
|
|
|
error(get_string('sessionipnomatch', 'error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-09 14:14:07 +00:00
|
|
|
// Next, check if the user can be in a particular course
|
|
|
|
if ($courseid) {
|
2004-09-25 15:23:10 +00:00
|
|
|
if ($courseid == SITEID) {
|
|
|
|
return; // Anyone can be in the site course
|
|
|
|
}
|
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-09-22 14:39: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.
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
if (! $course = get_record('course', 'id', $courseid)) {
|
|
|
|
error('That course doesn\'t exist');
|
2002-06-09 14:14:07 +00:00
|
|
|
}
|
2003-07-21 07:50:59 +00:00
|
|
|
if (!$course->visible) {
|
|
|
|
print_header();
|
2004-09-22 14:39:15 +00:00
|
|
|
notice(get_string('studentnotallowed', '', fullname($USER, true)), $CFG->wwwroot .'/');
|
2003-07-21 07:50:59 +00:00
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($USER->username == 'guest') {
|
2002-08-21 12:54:27 +00:00
|
|
|
switch ($course->guest) {
|
|
|
|
case 0: // Guests not allowed
|
|
|
|
print_header();
|
2004-09-22 14:39:15 +00:00
|
|
|
notice(get_string('guestsnotallowed', '', $course->fullname));
|
2002-08-21 12:54:27 +00:00
|
|
|
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;
|
2004-09-22 14:39:15 +00:00
|
|
|
redirect($CFG->wwwroot .'/course/enrol.php?id='. $courseid);
|
2002-06-09 14:14:07 +00:00
|
|
|
die;
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* This is a weaker version of {@link require_login()} which only requires login
|
|
|
|
* when called from within a course rather than the site page, unless
|
|
|
|
* the forcelogin option is turned on.
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $courseid The course in question
|
|
|
|
* @param boolean $autologinguest ?
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-08-22 14:38:47 +00:00
|
|
|
function require_course_login($course, $autologinguest=true) {
|
|
|
|
global $CFG;
|
|
|
|
if ($CFG->forcelogin) {
|
|
|
|
require_login();
|
|
|
|
}
|
|
|
|
if ($course->category) {
|
|
|
|
require_login($course->id, $autologinguest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Modify the user table by setting the currently logged in user's
|
|
|
|
* last login to now.
|
|
|
|
*
|
|
|
|
* @uses $USER
|
|
|
|
* @return boolean
|
|
|
|
*/
|
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;
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
return update_record('user', $user);
|
2002-12-23 03:07:01 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Determines if a user has completed setting up their account.
|
|
|
|
*
|
2004-09-25 05:29:21 +00:00
|
|
|
* @param user $user A {@link $USER} object to test for the existance of a valid name and email
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2002-12-30 15:30:05 +00:00
|
|
|
function user_not_fully_set_up($user) {
|
2004-09-22 14:39:15 +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
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Keeps track of login attempts
|
|
|
|
*
|
|
|
|
* @uses $SESSION
|
|
|
|
*/
|
2001-11-22 06:23:56 +00:00
|
|
|
function update_login_count() {
|
2002-12-20 14:44:14 +00:00
|
|
|
|
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);
|
2004-09-22 14:39:15 +00:00
|
|
|
error(get_string('errortoomanylogins'));
|
2002-09-22 14:06:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Resets login attempts
|
|
|
|
*
|
|
|
|
* @uses $SESSION
|
|
|
|
*/
|
2002-12-20 14:44:14 +00:00
|
|
|
function reset_login_count() {
|
|
|
|
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-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* check_for_restricted_user
|
|
|
|
*
|
2004-09-25 05:29:21 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @uses $USER
|
|
|
|
* @param string $username ?
|
|
|
|
* @param string $redirect ?
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-09-22 14:39:15 +00:00
|
|
|
function check_for_restricted_user($username=NULL, $redirect='') {
|
2004-01-30 08:25:49 +00:00
|
|
|
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-09-22 14:39:15 +00:00
|
|
|
error(get_string('restricteduser', 'error', fullname($USER)), $redirect);
|
2004-01-30 08:25:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Determines if a user an admin
|
|
|
|
*
|
|
|
|
* @uses $USER
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $userid The id of the user as is found in the 'user' table
|
2004-09-25 05:29:21 +00:00
|
|
|
* @staticvar array $admin ?
|
|
|
|
* @staticvar array $nonadmins ?
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
2004-09-25 05:29:21 +00:00
|
|
|
* @todo Complete documentation for this function
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2003-05-05 11:38:54 +00:00
|
|
|
function isadmin($userid=0) {
|
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;
|
2004-09-22 14:39:15 +00:00
|
|
|
} else if (record_exists('user_admins', 'userid', $userid)){
|
2003-05-05 11:38:54 +00:00
|
|
|
$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-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Determines if a user is a teacher or an admin
|
|
|
|
*
|
|
|
|
* @uses $USER
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $courseid The id of the course that is being viewed, if any
|
|
|
|
* @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @param boolean $includeadmin If true this function will return true when it encounters an admin user.
|
|
|
|
* @return boolean
|
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-07-25 13:48:55 +00:00
|
|
|
function isteacher($courseid=0, $userid=0, $includeadmin=true) {
|
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) {
|
2004-09-22 14:39:15 +00:00
|
|
|
return record_exists('user_teachers', 'userid', $userid);
|
2004-07-25 13:48:55 +00:00
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
return record_exists('user_teachers', 'userid', $userid, 'course', $courseid);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Determines if a user is allowed to edit a given course
|
|
|
|
*
|
|
|
|
* @uses $USER
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $courseid The id of the course that is being edited
|
|
|
|
* @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2003-08-16 05:19:24 +00:00
|
|
|
function isteacheredit($courseid, $userid=0) {
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
return get_field('user_teachers', 'editall', 'userid', $userid, 'course', $courseid);
|
2003-08-16 05:19:24 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Determines if a user can create new courses
|
|
|
|
*
|
|
|
|
* @uses $USER
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2003-02-24 18:48:55 +00:00
|
|
|
function iscreator ($userid=0) {
|
|
|
|
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)) {
|
2004-09-22 14:39:15 +00:00
|
|
|
return record_exists('user_coursecreators', 'userid', $USER->id);
|
2003-02-24 18:48:55 +00:00
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
return record_exists('user_coursecreators', 'userid', $userid);
|
2003-02-24 18:48:55 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Determines if a user is a student in the specified course
|
|
|
|
*
|
|
|
|
* If the course id specifies the site then the function determines
|
|
|
|
* if the user is a confirmed and valid user of this site.
|
|
|
|
*
|
|
|
|
* @uses $USER
|
|
|
|
* @uses $CFG
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses SITEID
|
|
|
|
* @param int $courseid The id of the course being tested
|
|
|
|
* @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2002-07-29 06:52:48 +00:00
|
|
|
function isstudent($courseid, $userid=0) {
|
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-09-21 11:41:58 +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
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
return record_exists('user_students', 'userid', $userid, 'course', $courseid);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Determines if the specified user is logged in as guest.
|
|
|
|
*
|
|
|
|
* @uses $USER
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2002-06-09 14:14:07 +00:00
|
|
|
function isguest($userid=0) {
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if (!$userid) {
|
2003-01-28 03:34:26 +00:00
|
|
|
if (empty($USER->username)) {
|
|
|
|
return false;
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
return ($USER->username == 'guest');
|
2002-06-09 14:14:07 +00:00
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
return record_exists('user', 'id', $userid, 'username', 'guest');
|
2002-06-09 14:14:07 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Determines if the currently logged in user is in editing mode
|
|
|
|
*
|
|
|
|
* @uses $USER
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $courseid The id of the course being tested
|
2004-09-25 05:29:21 +00:00
|
|
|
* @param user $user A {@link $USER} object. If null then the currently logged in user is used.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2002-07-29 15:45:46 +00:00
|
|
|
function isediting($courseid, $user=NULL) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Determines if the logged in user is currently moving an activity
|
|
|
|
*
|
|
|
|
* @uses $USER
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $courseid The id of the course being tested
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2003-07-14 13:08:38 +00:00
|
|
|
function ismoving($courseid) {
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if (!empty($USER->activitycopy)) {
|
|
|
|
return ($USER->activitycopycourse == $courseid);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Given an object containing firstname and lastname
|
|
|
|
* values, this function returns a string with the
|
|
|
|
* full name of the person.
|
|
|
|
* The result may depend on system settings
|
|
|
|
* or language. 'override' will force both names
|
|
|
|
* to be used even if system settings specify one.
|
|
|
|
* @uses $CFG
|
|
|
|
* @uses $SESSION
|
|
|
|
* @param type description
|
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2003-11-19 15:59:45 +00:00
|
|
|
function fullname($user, $override=false) {
|
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') {
|
2004-09-22 14:39:15 +00:00
|
|
|
return $user->firstname .' '. $user->lastname;
|
2003-11-30 14:02:01 +00:00
|
|
|
|
|
|
|
} else if ($CFG->fullnamedisplay == 'lastname firstname') {
|
2004-09-22 14:39:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Sets a moodle cookie with an encrypted string
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @param string $thing The string to encrypt and place in a cookie
|
|
|
|
*/
|
2001-11-22 06:23:56 +00:00
|
|
|
function set_moodle_cookie($thing) {
|
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;
|
2004-09-28 05:53:08 +00:00
|
|
|
$seconds = DAYSECS*$days;
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-09-28 05:53:08 +00:00
|
|
|
setCookie($cookiename, '', time() - HOURSECS, '/');
|
2004-09-22 14:39:15 +00:00
|
|
|
setCookie($cookiename, rc4encrypt($thing), time()+$seconds, '/');
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Gets a moodle cookie with an encrypted string
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @return string
|
|
|
|
*/
|
2001-11-22 06:23:56 +00:00
|
|
|
function get_moodle_cookie() {
|
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])) {
|
2004-09-22 14:39:15 +00:00
|
|
|
return '';
|
2003-01-08 09:07:07 +00:00
|
|
|
} else {
|
|
|
|
return rc4decrypt($_COOKIE[$cookiename]);
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Returns true if an internal authentication method is being used.
|
|
|
|
* if method not specified then, global default is assumed
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @param string $auth Form of authentication required
|
|
|
|
* @return boolean
|
|
|
|
* @todo Outline auth types and provide code example
|
|
|
|
*/
|
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-09-23 03:56:53 +00:00
|
|
|
/// If auth not specified then global default is assumed
|
2003-09-22 13:58:20 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2004-09-23 03:56:53 +00:00
|
|
|
if (empty($auth)) {
|
|
|
|
$auth = $CFG->auth;
|
2004-08-16 15:41:57 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 03:56:53 +00:00
|
|
|
return ($auth == "email" || $auth == "none" || $auth == "manual");
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of user fields
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @uses $db
|
|
|
|
* @return array User field/column names
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-09-23 03:56:53 +00:00
|
|
|
function get_user_fieldnames() {
|
|
|
|
|
|
|
|
global $CFG, $db;
|
|
|
|
|
|
|
|
$fieldarray = $db->MetaColumnNames($CFG->prefix.'user');
|
|
|
|
unset($fieldarray['ID']);
|
|
|
|
|
|
|
|
return $fieldarray;
|
2003-09-22 13:58:20 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Creates a bare-bones user record
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @param string $username New user's username to add to record
|
|
|
|
* @param string $password New user's password to add to record
|
|
|
|
* @param string $auth Form of authentication required
|
2004-09-25 05:29:21 +00:00
|
|
|
* @return user A {@link $USER} object
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Outline auth types and provide code example
|
|
|
|
*/
|
2004-08-31 12:33:20 +00:00
|
|
|
function create_user_record($username, $password, $auth='') {
|
2004-09-27 14:35:37 +00:00
|
|
|
global $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-09-06 15:21:22 +00:00
|
|
|
if (!empty($newuser->email)) {
|
|
|
|
if (email_is_not_allowed($newuser->email)) {
|
|
|
|
unset($newuser->email);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (insert_record('user', $newuser)) {
|
|
|
|
$user = get_user_info_from_db('username', $newuser->username);
|
2004-09-20 09:08:57 +00:00
|
|
|
if($CFG->{'auth_'.$newuser->auth.'_forcechangepassword'}){
|
|
|
|
set_user_preference('auth_forcepasswordchange', 1, $user);
|
|
|
|
}
|
|
|
|
return $user;
|
2002-09-26 07:03:22 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Will update a local user record from an external source
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @param string $username New user's username to add to record
|
2004-09-25 05:29:21 +00:00
|
|
|
* @return user A {@link $USER} object
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2004-09-20 09:08:57 +00:00
|
|
|
function update_user_record($username) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (function_exists('auth_get_userinfo')) {
|
|
|
|
$username = trim(moodle_strtolower($username)); /// just in case check text case
|
|
|
|
|
|
|
|
if ($newinfo = auth_get_userinfo($username)) {
|
|
|
|
foreach ($newinfo as $key => $value){
|
|
|
|
if (!empty($CFG->{'auth_user_' . $key. '_updatelocal'})) {
|
|
|
|
$value = addslashes(stripslashes($value)); // Just in case
|
|
|
|
set_field('user', $key, $value, 'username', $username);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
return get_user_info_from_db('username', $username);
|
2004-09-20 09:08:57 +00:00
|
|
|
}
|
2004-01-25 09:35:45 +00:00
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the guest user object
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
2004-09-25 05:29:21 +00:00
|
|
|
* @return user A {@link $USER} object
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2004-01-25 09:35:45 +00:00
|
|
|
function guest_user() {
|
|
|
|
global $CFG;
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($newuser = get_record('user', 'username', 'guest')) {
|
2004-01-25 09:35:45 +00:00
|
|
|
$newuser->loggedin = true;
|
|
|
|
$newuser->confirmed = 1;
|
|
|
|
$newuser->site = $CFG->wwwroot;
|
|
|
|
$newuser->lang = $CFG->lang;
|
2004-09-27 14:35:37 +00:00
|
|
|
$newuser->lastIP = getremoteaddr();
|
2004-01-25 09:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $newuser;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Given a username and password, this function looks them
|
|
|
|
* up using the currently selected authentication mechanism,
|
|
|
|
* and if the authentication is successful, it returns a
|
|
|
|
* valid $user object from the 'user' table.
|
|
|
|
*
|
|
|
|
* Uses auth_ functions from the currently active auth module
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param string $username User's username
|
|
|
|
* @param string $password User's password
|
2004-09-25 05:29:21 +00:00
|
|
|
* @return user|flase A {@link $USER} object or false if error
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2002-09-26 07:03:22 +00:00
|
|
|
function authenticate_user_login($username, $password) {
|
|
|
|
|
|
|
|
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-09-22 14:39: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)) {
|
2004-09-22 14:39:15 +00:00
|
|
|
$CFG->auth = 'manual'; // Default authentication module
|
2004-08-29 05:48:15 +00:00
|
|
|
}
|
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-09-20 09:08:57 +00:00
|
|
|
// update user record from external DB
|
|
|
|
if ($user->auth != 'manual' && $user->auth != 'email') {
|
|
|
|
$user = update_user_record($username);
|
|
|
|
}
|
2004-08-31 12:33:20 +00:00
|
|
|
} else {
|
|
|
|
$auth = $user->auth;
|
2004-08-29 05:48:15 +00:00
|
|
|
}
|
2004-09-21 11:41:58 +00:00
|
|
|
|
2004-09-27 12:52:25 +00:00
|
|
|
if (detect_munged_arguments($auth, 0)) { // For safety on the next require
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +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-09-22 14:39:15 +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
|
|
|
}
|
2004-09-27 14:35:37 +00:00
|
|
|
if (!is_internal_auth()) { // update user record from external DB
|
2004-09-20 09:08:57 +00:00
|
|
|
$user = update_user_record($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-09-22 14:39:15 +00:00
|
|
|
if (! record_exists('user_coursecreators', 'userid', $user->id)) {
|
2004-08-16 15:41:57 +00:00
|
|
|
$cdata->userid = $user->id;
|
2004-09-22 14:39:15 +00:00
|
|
|
if (! insert_record('user_coursecreators', $cdata)) {
|
|
|
|
error('Cannot add user to course creators.');
|
2004-08-16 15:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
2003-03-14 21:34:39 +00:00
|
|
|
} else {
|
2004-09-27 14:35:37 +00:00
|
|
|
if (record_exists('user_coursecreators', 'userid', $user->id)) {
|
2004-09-22 14:39:15 +00:00
|
|
|
if (! delete_records('user_coursecreators', 'userid', $user->id)) {
|
|
|
|
error('Cannot remove user from course creators.');
|
2004-08-16 15:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
2003-03-14 21:34:39 +00:00
|
|
|
}
|
2004-08-16 15:41:57 +00:00
|
|
|
}
|
2004-09-27 14:35:37 +00:00
|
|
|
$user->sessionIP = md5(getremoteaddr()); // Store the current IP in the session
|
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-09-22 14:39:15 +00:00
|
|
|
add_to_log(0, 'login', 'error', $_SERVER['HTTP_REFERER'], $username);
|
2004-07-25 13:48:55 +00:00
|
|
|
$date = date('Y-m-d H:i:s');
|
2004-09-22 14:39:15 +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-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Enrols (or re-enrols) a student in a given course
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $courseid The id of the course that is being viewed
|
|
|
|
* @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
|
|
|
|
* @param int $timestart ?
|
|
|
|
* @param int $timeend ?
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-07-29 14:54:25 +00:00
|
|
|
function enrol_student($userid, $courseid, $timestart=0, $timeend=0) {
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (!$course = get_record('course', 'id', $courseid)) { // Check course
|
2003-08-17 08:51:55 +00:00
|
|
|
return false;
|
2003-08-17 05:48:05 +00:00
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
if (!$user = get_record('user', 'id', $userid)) { // Check user
|
2004-09-09 09:43:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($student = get_record('user_students', 'userid', $userid, 'course', $courseid)) {
|
2004-09-09 09:43:51 +00:00
|
|
|
$student->timestart = $timestart;
|
|
|
|
$student->timeend = $timeend;
|
|
|
|
$student->time = time();
|
2004-09-22 14:39:15 +00:00
|
|
|
return update_record('user_students', $student);
|
2004-09-21 11:41:58 +00:00
|
|
|
|
2004-09-09 09:43:51 +00:00
|
|
|
} else {
|
|
|
|
$student->userid = $userid;
|
|
|
|
$student->course = $courseid;
|
|
|
|
$student->timestart = $timestart;
|
|
|
|
$student->timeend = $timeend;
|
|
|
|
$student->time = time();
|
2004-09-22 14:39:15 +00:00
|
|
|
return insert_record('user_students', $student);
|
2004-09-09 09:43:51 +00:00
|
|
|
}
|
2002-12-11 14:59:37 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Unenrols a student from a given course
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $courseid The id of the course that is being viewed, if any
|
|
|
|
* @param int $userid The id of the user that is being tested against.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2004-01-30 06:44:02 +00:00
|
|
|
function unenrol_student($userid, $courseid=0) {
|
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-09-22 14:39:15 +00:00
|
|
|
if ($forums = get_records('forum', 'course', $courseid)) {
|
2002-12-20 14:44:14 +00:00
|
|
|
foreach ($forums as $forum) {
|
2004-09-22 14:39:15 +00:00
|
|
|
delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid);
|
2004-01-30 06:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($groups = get_groups($courseid, $userid)) {
|
|
|
|
foreach ($groups as $group) {
|
2004-09-22 14:39:15 +00:00
|
|
|
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-09-22 14:39:15 +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-09-22 14:39:15 +00:00
|
|
|
delete_records('forum_subscriptions', 'userid', $userid);
|
|
|
|
delete_records('groups_members', 'userid', $userid);
|
|
|
|
return delete_records('user_students', 'userid', $userid);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Add a teacher to a given course
|
|
|
|
*
|
|
|
|
* @uses $USER
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $courseid The id of the course that is being viewed, if any
|
|
|
|
* @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
|
|
|
|
* @param int $editall ?
|
2004-09-23 02:48:41 +00:00
|
|
|
* @param string $role ?
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $timestart ?
|
|
|
|
* @param int $timeend ?
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-09-22 14:39:15 +00:00
|
|
|
function add_teacher($userid, $courseid, $editall=1, $role='', $timestart=0, $timeend=0) {
|
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
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (!record_exists('user', 'id', $userid)) {
|
2004-06-24 12:51:25 +00:00
|
|
|
return false; // no such user
|
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (!record_exists('course', 'id', $courseid)) {
|
2004-06-24 12:51:25 +00:00
|
|
|
return false; // no such course
|
|
|
|
}
|
|
|
|
|
|
|
|
$teacher = NULL;
|
|
|
|
$teacher->userid = $userid;
|
|
|
|
$teacher->course = $courseid;
|
|
|
|
$teacher->editall = $editall;
|
|
|
|
$teacher->role = $role;
|
2004-09-04 13:23:08 +00:00
|
|
|
$teacher->timemodified = time();
|
|
|
|
$newteacher->timestart = $timestart;
|
|
|
|
$newteacher->timeend = $timeend;
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($student = get_record('user_students', 'userid', $userid, 'course', $courseid)) {
|
2004-09-04 13:23:08 +00:00
|
|
|
$teacher->timestart = $student->timestart;
|
|
|
|
$teacher->timeend = $student->timeend;
|
|
|
|
$teacher->timeaccess = $student->timeaccess;
|
|
|
|
}
|
2004-06-24 12:51:25 +00:00
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (record_exists('user_teachers', 'course', $courseid)) {
|
2004-06-24 12:51:25 +00:00
|
|
|
$teacher->authority = 2;
|
|
|
|
} else {
|
|
|
|
$teacher->authority = 1;
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
delete_records('user_students', 'userid', $userid, 'course', $courseid); // Unenrol as student
|
2004-09-21 11:41:58 +00:00
|
|
|
|
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
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
return insert_record('user_teachers', $teacher);
|
2004-06-24 12:51:25 +00:00
|
|
|
|
2003-08-17 08:51:55 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Removes a teacher from a given course (or ALL courses)
|
|
|
|
* Does not delete the user account
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $courseid The id of the course that is being viewed, if any
|
|
|
|
* @param int $userid The id of the user that is being tested against.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2003-08-17 08:51:55 +00:00
|
|
|
function remove_teacher($userid, $courseid=0) {
|
|
|
|
if ($courseid) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// First delete any crucial stuff that might still send mail
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($forums = get_records('forum', 'course', $courseid)) {
|
2002-12-20 14:44:14 +00:00
|
|
|
foreach ($forums as $forum) {
|
2004-09-22 14:39:15 +00:00
|
|
|
delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid);
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-14 23:54:23 +00:00
|
|
|
|
|
|
|
/// Next if the teacher is not registered as a student, but is
|
|
|
|
/// a member of a group, remove them from the group.
|
|
|
|
if (!isstudent($courseid, $userid)) {
|
|
|
|
if ($groups = get_groups($courseid, $userid)) {
|
|
|
|
foreach ($groups as $group) {
|
2004-09-22 14:39:15 +00:00
|
|
|
delete_records('groups_members', 'groupid', $group->id, 'userid', $userid);
|
2004-09-14 23:54:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
return delete_records('user_teachers', 'userid', $userid, 'course', $courseid);
|
2002-08-02 17:41:14 +00:00
|
|
|
} else {
|
2004-09-22 14:39:15 +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
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Add a creator to the site
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $userid The id of the user that is being tested against.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2003-08-17 08:51:55 +00:00
|
|
|
function add_creator($userid) {
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (!record_exists('user_admins', 'userid', $userid)) {
|
|
|
|
if (record_exists('user', 'id', $userid)) {
|
2003-08-17 08:51:55 +00:00
|
|
|
$creator->userid = $userid;
|
2004-09-22 14:39:15 +00:00
|
|
|
return insert_record('user_coursecreators', $creator);
|
2003-08-17 08:51:55 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Remove a creator from a site
|
|
|
|
*
|
|
|
|
* @uses $db
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $userid The id of the user that is being tested against.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2003-08-17 08:51:55 +00:00
|
|
|
function remove_creator($userid) {
|
|
|
|
global $db;
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
return delete_records('user_coursecreators', 'userid', $userid);
|
2003-08-17 08:51:55 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Add an admin to a site
|
|
|
|
*
|
|
|
|
* @uses SITEID
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $userid The id of the user that is being tested against.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2003-08-17 08:51:55 +00:00
|
|
|
function add_admin($userid) {
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (!record_exists('user_admins', 'userid', $userid)) {
|
|
|
|
if (record_exists('user', 'id', $userid)) {
|
2003-08-17 08:51:55 +00:00
|
|
|
$admin->userid = $userid;
|
2004-09-21 11:41:58 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2004-09-21 11:41:58 +00:00
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
return insert_record('user_admins', $admin);
|
2003-08-17 08:51:55 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Removes an admin from a site
|
|
|
|
*
|
|
|
|
* @uses $db
|
|
|
|
* @uses SITEID
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $userid The id of the user that is being tested against.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2003-08-17 08:51:55 +00:00
|
|
|
function remove_admin($userid) {
|
2002-12-20 14:44:14 +00:00
|
|
|
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
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
return delete_records('user_admins', 'userid', $userid);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Clear a course out completely, deleting all content
|
|
|
|
* but don't delete the course itself
|
|
|
|
*
|
|
|
|
* @uses $USER
|
|
|
|
* @uses $SESSION
|
|
|
|
* @uses $CFG
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $courseid The id of the course that is being viewed
|
2004-09-23 02:48:41 +00:00
|
|
|
* @param boolean $showfeedback Set this to false to suppress notifications from being printed as the functions performs its steps.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2003-08-02 11:32:59 +00:00
|
|
|
function remove_course_contents($courseid, $showfeedback=true) {
|
|
|
|
|
2003-08-08 11:29:33 +00:00
|
|
|
global $CFG, $THEME, $USER, $SESSION;
|
2003-08-02 11:32:59 +00:00
|
|
|
|
|
|
|
$result = true;
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (! $course = get_record('course', 'id', $courseid)) {
|
|
|
|
error('Course ID was incorrect (can\'t find it)');
|
2003-08-02 11:32:59 +00:00
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$strdeleted = get_string('deleted');
|
2003-08-02 11:32:59 +00:00
|
|
|
|
|
|
|
// First delete every instance of every module
|
2004-04-01 10:11:20 +00:00
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($allmods = get_records('modules') ) {
|
2003-08-02 11:32:59 +00:00
|
|
|
foreach ($allmods as $mod) {
|
|
|
|
$modname = $mod->name;
|
2004-09-22 14:39:15 +00:00
|
|
|
$modfile = $CFG->dirroot .'/mod/'. $modname .'/lib.php';
|
|
|
|
$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)) {
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($instances = get_records($modname, 'course', $course->id)) {
|
2003-08-02 11:32:59 +00:00
|
|
|
foreach ($instances as $instance) {
|
|
|
|
if ($moddelete($instance->id)) {
|
|
|
|
$count++;
|
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify('Could not delete '. $modname .' instance '. $instance->id .' ('. $instance->name .')');
|
2003-08-02 11:32:59 +00:00
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify('Function '. $moddelete() .'doesn\'t exist!');
|
2003-08-02 11:32:59 +00:00
|
|
|
$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) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify($strdeleted .' '. $count .' x '. $modname);
|
2003-08-02 11:32:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
error('No modules are installed!');
|
2003-08-02 11:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete any user stuff
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (delete_records('user_students', 'course', $course->id)) {
|
2003-08-02 11:32:59 +00:00
|
|
|
if ($showfeedback) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify($strdeleted .' user_students');
|
2003-08-02 11:32:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (delete_records('user_teachers', 'course', $course->id)) {
|
2003-08-02 11:32:59 +00:00
|
|
|
if ($showfeedback) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify($strdeleted .' user_teachers');
|
2003-08-02 11:32:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
2004-02-15 05:08:00 +00:00
|
|
|
// Delete any groups
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($groups = get_records('groups', 'courseid', $course->id)) {
|
2004-02-15 05:08:00 +00:00
|
|
|
foreach ($groups as $group) {
|
2004-09-22 14:39:15 +00:00
|
|
|
if (delete_records('groups_members', 'groupid', $group->id)) {
|
2004-02-15 05:08:00 +00:00
|
|
|
if ($showfeedback) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify($strdeleted .' groups_members');
|
2004-02-15 05:08:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
if (delete_records('groups', 'id', $group->id)) {
|
2004-02-15 05:08:00 +00:00
|
|
|
if ($showfeedback) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify($strdeleted .' groups');
|
2004-02-15 05:08:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete events
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (delete_records('event', 'courseid', $course->id)) {
|
2004-02-15 05:08:00 +00:00
|
|
|
if ($showfeedback) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify($strdeleted .' event');
|
2004-02-15 05:08:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
2003-08-02 11:32:59 +00:00
|
|
|
// Delete logs
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (delete_records('log', 'course', $course->id)) {
|
2003-08-02 11:32:59 +00:00
|
|
|
if ($showfeedback) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify($strdeleted .' log');
|
2003-08-02 11:32:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete any course stuff
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (delete_records('course_sections', 'course', $course->id)) {
|
2003-08-02 11:32:59 +00:00
|
|
|
if ($showfeedback) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify($strdeleted .' course_sections');
|
2003-08-02 11:32:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (delete_records('course_modules', 'course', $course->id)) {
|
2003-08-02 11:32:59 +00:00
|
|
|
if ($showfeedback) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify($strdeleted .' course_modules');
|
2003-08-02 11:32:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* This function will empty a course of USER data as much as
|
|
|
|
/// possible. It will retain the activities and the structure
|
|
|
|
/// of the course.
|
|
|
|
*
|
|
|
|
* @uses $USER
|
|
|
|
* @uses $THEME
|
|
|
|
* @uses $SESSION
|
|
|
|
* @uses $CFG
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $courseid The id of the course that is being viewed
|
2004-09-23 02:48:41 +00:00
|
|
|
* @param boolean $showfeedback Set this to false to suppress notifications from being printed as the functions performs its steps.
|
|
|
|
* @param boolean $removestudents ?
|
|
|
|
* @param boolean $removeteachers ?
|
|
|
|
* @param boolean $removegroups ?
|
|
|
|
* @param boolean $removeevents ?
|
|
|
|
* @param boolean $removelogs ?
|
|
|
|
* @return boolean
|
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
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) {
|
|
|
|
|
|
|
|
global $CFG, $THEME, $USER, $SESSION;
|
|
|
|
|
|
|
|
$result = true;
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (! $course = get_record('course', 'id', $courseid)) {
|
|
|
|
error('Course ID was incorrect (can\'t find it)');
|
2004-08-01 07:34:39 +00:00
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$strdeleted = get_string('deleted');
|
2004-08-01 07:34:39 +00:00
|
|
|
|
|
|
|
// Look in every instance of every module for data to delete
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($allmods = get_records('modules') ) {
|
2004-08-01 07:34:39 +00:00
|
|
|
foreach ($allmods as $mod) {
|
|
|
|
$modname = $mod->name;
|
2004-09-22 14:39:15 +00:00
|
|
|
$modfile = $CFG->dirroot .'/mod/'. $modname .'/lib.php';
|
|
|
|
$moddeleteuserdata = $modname .'_delete_userdata'; // Function to delete user data
|
2004-08-01 07:34:39 +00:00
|
|
|
$count=0;
|
|
|
|
if (file_exists($modfile)) {
|
|
|
|
@include_once($modfile);
|
|
|
|
if (function_exists($moddeleteuserdata)) {
|
|
|
|
$moddeleteuserdata($course, $showfeedback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
error('No modules are installed!');
|
2004-08-01 07:34:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete other stuff
|
|
|
|
|
|
|
|
if ($removestudents) {
|
|
|
|
/// Delete student enrolments
|
2004-09-22 14:39:15 +00:00
|
|
|
if (delete_records('user_students', 'course', $course->id)) {
|
2004-08-01 07:34:39 +00:00
|
|
|
if ($showfeedback) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify($strdeleted .' user_students');
|
2004-08-01 07:34:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
/// Delete group members (but keep the groups)
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($groups = get_records('groups', 'courseid', $course->id)) {
|
2004-08-01 07:34:39 +00:00
|
|
|
foreach ($groups as $group) {
|
2004-09-22 14:39:15 +00:00
|
|
|
if (delete_records('groups_members', 'groupid', $group->id)) {
|
2004-08-01 07:34:39 +00:00
|
|
|
if ($showfeedback) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify($strdeleted .' groups_members');
|
2004-08-01 07:34:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($removeteachers) {
|
2004-09-22 14:39:15 +00:00
|
|
|
if (delete_records('user_teachers', 'course', $course->id)) {
|
2004-08-01 07:34:39 +00:00
|
|
|
if ($showfeedback) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify($strdeleted .' user_teachers');
|
2004-08-01 07:34:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($removegroups) {
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($groups = get_records('groups', 'courseid', $course->id)) {
|
2004-08-01 07:34:39 +00:00
|
|
|
foreach ($groups as $group) {
|
2004-09-22 14:39:15 +00:00
|
|
|
if (delete_records('groups', 'id', $group->id)) {
|
2004-08-01 07:34:39 +00:00
|
|
|
if ($showfeedback) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify($strdeleted .' groups');
|
2004-08-01 07:34:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($removeevents) {
|
2004-09-22 14:39:15 +00:00
|
|
|
if (delete_records('event', 'courseid', $course->id)) {
|
2004-08-01 07:34:39 +00:00
|
|
|
if ($showfeedback) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify($strdeleted .' event');
|
2004-08-01 07:34:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($removelogs) {
|
2004-09-22 14:39:15 +00:00
|
|
|
if (delete_records('log', 'course', $course->id)) {
|
2004-08-01 07:34:39 +00:00
|
|
|
if ($showfeedback) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify($strdeleted .' log');
|
2004-08-01 07:34:39 +00:00
|
|
|
}
|
|
|
|
} 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
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
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
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +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
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* Returns the group ID of the current user in the given course
|
|
|
|
*
|
|
|
|
* @uses $USER
|
|
|
|
* @param int $courseid The course being examined - relates to id field in 'course' table.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
2004-09-25 01:29:37 +00:00
|
|
|
*/
|
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-09-25 01:29:37 +00:00
|
|
|
* For a given course, and possibly course module, determine
|
|
|
|
* what the current default groupmode is:
|
|
|
|
* NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
|
|
|
|
*
|
2004-09-25 05:29:21 +00:00
|
|
|
* @param course $course A {@link $COURSE} object
|
|
|
|
* @param array? $cm A course module object
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return int A group mode (NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS)
|
|
|
|
*/
|
2003-12-30 17:18:06 +00:00
|
|
|
function groupmode($course, $cm=null) {
|
|
|
|
|
|
|
|
if ($cm and !$course->groupmodeforce) {
|
|
|
|
return $cm->groupmode;
|
|
|
|
}
|
|
|
|
return $course->groupmode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* Sets the current group in the session variable
|
|
|
|
*
|
|
|
|
* @uses $SESSION
|
|
|
|
* @param int $courseid The course being examined - relates to id field in 'course' table.
|
|
|
|
* @param int $groupid The group being examined.
|
|
|
|
* @return int Current group id which was set by this function
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
2004-09-25 01:29:37 +00:00
|
|
|
*/
|
2003-12-30 17:18:06 +00:00
|
|
|
function set_current_group($courseid, $groupid) {
|
|
|
|
global $SESSION;
|
|
|
|
|
|
|
|
return $SESSION->currentgroup[$courseid] = $groupid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* Gets the current group for the current user as an id or an object
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @uses $SESSION
|
|
|
|
* @param int $courseid The course being examined - relates to id field in 'course' table.
|
|
|
|
* @param boolean $full ?
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
2004-09-25 01:29:37 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
2004-09-25 01:29:37 +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.
|
|
|
|
*
|
|
|
|
* @uses VISIBLEGROUPS
|
2004-09-25 05:29:21 +00:00
|
|
|
* @param course $course A {@link $COURSE} object
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
|
|
|
|
* @param int $groupid Will try to use this optional parameter to
|
|
|
|
* reset the current group for the user
|
2004-09-25 05:29:21 +00:00
|
|
|
* @return int|false Returns the current group id or false if error.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
2004-09-25 01:29:37 +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
|
|
|
/**
|
2004-09-25 01:29:37 +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
|
|
|
|
*
|
|
|
|
* @uses SEPARATEGROUPS
|
|
|
|
* @uses VISIBLEGROUPS
|
2004-09-25 05:29:21 +00:00
|
|
|
* @param course $course A {@link $COURSE} object
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
|
|
|
|
* @param string $urlroot ?
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
2004-09-25 01:29:37 +00:00
|
|
|
*/
|
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))) {
|
2004-09-22 14:39:15 +00:00
|
|
|
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-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Send an email to a specified user
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @uses $_SERVER
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses SITEID
|
2004-09-25 05:29:21 +00:00
|
|
|
* @param user $user A {@link $USER} object
|
|
|
|
* @param user $from A {@link $USER} object
|
2004-09-23 02:48:41 +00:00
|
|
|
* @param string $subject plain text subject line of the email
|
|
|
|
* @param string $messagetext plain text version of the message
|
|
|
|
* @param string $messagehtml complete html version of the message (optional)
|
|
|
|
* @param string $attachment a file on the filesystem, relative to $CFG->dataroot
|
|
|
|
* @param string $attachname the name of the file (extension indicates MIME)
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param boolean $usetrueaddress determines whether $from email address should
|
|
|
|
* be sent out. Will be overruled by user profile setting for maildisplay
|
|
|
|
* @return boolean|string Returns "true" if mail was sent OK, "emailstop" if email
|
|
|
|
* was blocked by user and "false" if there was another sort of error.
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2004-09-22 14:39:15 +00:00
|
|
|
function email_to_user($user, $from, $subject, $messagetext, $messagehtml='', $attachment='', $attachname='', $usetrueaddress=true) {
|
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;
|
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +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-09-22 14:39:15 +00:00
|
|
|
$mail->Version = 'Moodle '. $CFG->version; // mailer version
|
|
|
|
$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
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (current_language() != 'en') {
|
|
|
|
$mail->CharSet = get_string('thischarset');
|
2002-11-14 11:45:14 +00:00
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($CFG->smtphosts == 'qmail') {
|
2003-11-28 10:57:58 +00:00
|
|
|
$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) {
|
2004-09-22 14:39:15 +00:00
|
|
|
echo '<pre>' . "\n";
|
2003-10-28 14:35:00 +00:00
|
|
|
$mail->SMTPDebug = true;
|
|
|
|
}
|
2004-09-22 14:39:15 +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();
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$mail->Sender = $adminuser->email;
|
2003-07-27 13:14:00 +00:00
|
|
|
|
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-09-22 14:39:15 +00:00
|
|
|
$mail->From = $from->email;
|
2004-06-06 12:49:55 +00:00
|
|
|
$mail->FromName = fullname($from);
|
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
$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-09-22 14:39:15 +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-09-07 14:04:33 +00:00
|
|
|
if (!empty($from->customheaders)) { // Add custom headers
|
|
|
|
if (is_array($from->customheaders)) {
|
|
|
|
foreach ($from->customheaders as $customheader) {
|
|
|
|
$mail->AddCustomHeader($customheader);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$mail->AddCustomHeader($from->customheaders);
|
|
|
|
}
|
2004-05-07 03:29:11 +00:00
|
|
|
}
|
2004-09-21 11:41:58 +00:00
|
|
|
|
2002-05-24 06:38:37 +00:00
|
|
|
if ($messagehtml) {
|
|
|
|
$mail->IsHTML(true);
|
2004-09-22 14:39:15 +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
|
2004-09-22 14:39:15 +00:00
|
|
|
$mail->AddAddress($adminuser->email, fullname($adminuser) );
|
|
|
|
$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 {
|
2004-09-22 14:39:15 +00:00
|
|
|
include_once($CFG->dirroot .'/files/mimetypes.php');
|
|
|
|
$mimetype = mimeinfo('type', $attachname);
|
|
|
|
$mail->AddAttachment($CFG->dataroot .'/'. $attachment, $attachname, 'base64', $mimetype);
|
2002-05-24 06:38:37 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2002-05-24 06:38:37 +00:00
|
|
|
if ($mail->Send()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
mtrace('ERROR: '. $mail->ErrorInfo);
|
|
|
|
add_to_log(SITEID, 'library', 'mailer', $_SERVER['REQUEST_URI'], 'ERROR: '. $mail->ErrorInfo);
|
2001-11-22 06:23:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Resets specified user's password and send the new password to the user via email.
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
2004-09-25 05:29:21 +00:00
|
|
|
* @param user $user A {@link $USER} object
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return boolean|string Returns "true" if mail was sent OK, "emailstop" if email
|
|
|
|
* was blocked by user and "false" if there was another sort of error.
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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();
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (! set_field('user', 'password', md5($newpassword), 'id', $user->id) ) {
|
|
|
|
error('Could not set user password!');
|
2002-12-23 03:07:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$a->firstname = $user->firstname;
|
|
|
|
$a->sitename = $site->fullname;
|
|
|
|
$a->username = $user->username;
|
|
|
|
$a->newpassword = $newpassword;
|
2004-09-22 14:39:15 +00:00
|
|
|
$a->link = $CFG->wwwroot .'/login/change_password.php';
|
|
|
|
$a->signoff = fullname($from, true).' ('. $from->email .')';
|
2002-12-23 03:07:01 +00:00
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$message = get_string('newpasswordtext', '', $a);
|
2002-12-23 03:07:01 +00:00
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$subject = $site->fullname .': '. get_string('changedpassword');
|
2002-12-23 03:07:01 +00:00
|
|
|
|
|
|
|
return email_to_user($user, $from, $subject, $message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Send email to specified user with confirmation text and activation link.
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
2004-09-25 05:29:21 +00:00
|
|
|
* @param user $user A {@link $USER} object
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return boolean|string Returns "true" if mail was sent OK, "emailstop" if email
|
|
|
|
* was blocked by user and "false" if there was another sort of error.
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
|
|
|
function send_confirmation_email($user) {
|
2002-12-23 03:07:01 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$site = get_site();
|
|
|
|
$from = get_admin();
|
|
|
|
|
|
|
|
$data->firstname = $user->firstname;
|
|
|
|
$data->sitename = $site->fullname;
|
2004-09-22 14:39:15 +00:00
|
|
|
$data->link = $CFG->wwwroot .'/login/confirm.php?p='. $user->secret .'&s='. $user->username;
|
|
|
|
$data->admin = fullname($from) .' ('. $from->email .')';
|
2002-12-23 03:07:01 +00:00
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$message = get_string('emailconfirmation', '', $data);
|
|
|
|
$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
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* send_password_change_confirmation_email.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $CFG
|
2004-09-25 05:29:21 +00:00
|
|
|
* @param user $user A {@link $USER} object
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return boolean|string Returns "true" if mail was sent OK, "emailstop" if email
|
|
|
|
* was blocked by user and "false" if there was another sort of error.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
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;
|
2004-09-22 14:39:15 +00:00
|
|
|
$data->link = $CFG->wwwroot .'/login/forgot_password.php?p='. $user->secret .'&s='. $user->username;
|
|
|
|
$data->admin = fullname($from).' ('. $from->email .')';
|
2003-05-04 03:00:52 +00:00
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$message = get_string('emailpasswordconfirmation', '', $data);
|
|
|
|
$subject = get_string('emailpasswordconfirmationsubject', '', $site->fullname);
|
2003-05-04 03:00:52 +00:00
|
|
|
|
|
|
|
return email_to_user($user, $from, $subject, $message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Check that an email is allowed. It returns an error message if there
|
|
|
|
* was a problem.
|
|
|
|
*
|
|
|
|
* @param type description
|
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-09-06 15:21:22 +00:00
|
|
|
function email_is_not_allowed($email) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!empty($CFG->allowemailaddresses)) {
|
|
|
|
$allowed = explode(' ', $CFG->allowemailaddresses);
|
|
|
|
foreach ($allowed as $allowedpattern) {
|
|
|
|
$allowedpattern = trim($allowedpattern);
|
|
|
|
if (!$allowedpattern) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strpos($email, $allowedpattern) !== false) { // Match!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
return get_string('emailonlyallowed', '', $CFG->allowemailaddresses);
|
2004-09-06 15:21:22 +00:00
|
|
|
|
|
|
|
} else if (!empty($CFG->denyemailaddresses)) {
|
|
|
|
$denied = explode(' ', $CFG->denyemailaddresses);
|
|
|
|
foreach ($denied as $deniedpattern) {
|
|
|
|
$deniedpattern = trim($deniedpattern);
|
|
|
|
if (!$deniedpattern) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strpos($email, $deniedpattern) !== false) { // Match!
|
2004-09-22 14:39:15 +00:00
|
|
|
return get_string('emailnotallowed', '', $CFG->denyemailaddresses);
|
2004-09-06 15:21:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
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-09-23 02:48:41 +00:00
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* Create a directory.
|
2004-09-23 02:48:41 +00:00
|
|
|
*
|
|
|
|
* @uses $CFG
|
2004-09-23 05:10:21 +00:00
|
|
|
* @param string $directory a string of directory names under $CFG->dataroot eg stuff/assignment/1
|
2004-09-25 01:29:37 +00:00
|
|
|
* param boolean $shownotices If true then notification messages will be printed out on error.
|
|
|
|
* @return string|false Returns full path to directory if successful, false if not
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2004-08-12 15:54:43 +00:00
|
|
|
function make_upload_directory($directory, $shownotices=true) {
|
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) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify('ERROR: You need to create the directory '. $currdir .' with web server write access');
|
2004-08-12 15:54:43 +00:00
|
|
|
}
|
2002-08-04 16:20:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$dirarray = explode('/', $directory);
|
2002-08-04 16:20:30 +00:00
|
|
|
|
|
|
|
foreach ($dirarray as $dir) {
|
2004-09-22 14:39:15 +00:00
|
|
|
$currdir = $currdir .'/'. $dir;
|
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) {
|
2004-09-22 14:39:15 +00:00
|
|
|
notify('ERROR: Could not find or create a directory ('. $currdir .')');
|
2004-08-12 15:54:43 +00:00
|
|
|
}
|
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
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Makes an upload directory for a particular module.
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $courseid The id of the course in question - maps to id field of 'course' table.
|
|
|
|
* @return string|false Returns full path to directory if successful, false if not
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2002-08-28 13:07:10 +00:00
|
|
|
function make_mod_upload_directory($courseid) {
|
|
|
|
global $CFG;
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (! $moddata = make_upload_directory($courseid .'/'. $CFG->moddata)) {
|
2002-08-28 13:07:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$strreadme = get_string('readme');
|
2002-08-28 13:07:10 +00:00
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
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');
|
2002-08-28 13:07:10 +00:00
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
copy($CFG->dirroot .'/lang/en/docs/module_files.txt', $moddata .'/'. $strreadme .'.txt');
|
2002-08-28 13:07:10 +00:00
|
|
|
}
|
|
|
|
return $moddata;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* Returns current name of file on disk if it exists.
|
2004-09-23 02:48:41 +00:00
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param string $newfile File to be verified
|
|
|
|
* @return string Current name of file on disk if true
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2002-09-01 14:29:17 +00:00
|
|
|
function valid_uploaded_file($newfile) {
|
2002-12-29 17:32:32 +00:00
|
|
|
if (empty($newfile)) {
|
2004-09-22 14:39:15 +00:00
|
|
|
return '';
|
2002-12-29 17:32:32 +00:00
|
|
|
}
|
2002-09-01 14:29:17 +00:00
|
|
|
if (is_uploaded_file($newfile['tmp_name']) and $newfile['size'] > 0) {
|
|
|
|
return $newfile['tmp_name'];
|
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
return '';
|
2002-09-01 14:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Returns the maximum size for uploading files.
|
|
|
|
*
|
|
|
|
* There are seven possible upload limits:
|
|
|
|
* 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)
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $sizebytes ?
|
|
|
|
* @param int $coursebytes Current course $course->maxbytes (in bytes)
|
|
|
|
* @param int $modulebytes Current module ->maxbytes (in bytes)
|
|
|
|
* @return int The maximum size for uploading files.
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2003-10-06 18:02:35 +00:00
|
|
|
function get_max_upload_file_size($sitebytes=0, $coursebytes=0, $modulebytes=0) {
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (! $filesize = ini_get('upload_max_filesize')) {
|
|
|
|
$filesize = '5M';
|
2002-09-01 14:29:17 +00:00
|
|
|
}
|
2003-10-06 18:02:35 +00:00
|
|
|
$minimumsize = get_real_size($filesize);
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($postsize = ini_get('post_max_size')) {
|
2003-12-02 14:34:24 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Related to the above function - this function returns an
|
|
|
|
* array of possible sizes in an array, translated to the
|
|
|
|
* local language.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses SORT_NUMERIC
|
|
|
|
* @param int $sizebytes ?
|
|
|
|
* @param int $coursebytes Current course $course->maxbytes (in bytes)
|
|
|
|
* @param int $modulebytes Current module ->maxbytes (in bytes)
|
|
|
|
* @return int
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2003-10-06 18:02:35 +00:00
|
|
|
function get_max_upload_sizes($sitebytes=0, $coursebytes=0, $modulebytes=0) {
|
|
|
|
|
|
|
|
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-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* If there has been an error uploading a file, print the appropriate error message
|
|
|
|
* Numerical constants used as constant definitions not added until PHP version 4.2.0
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* $filearray is a 1-dimensional sub-array of the $_FILES array
|
2004-09-23 02:48:41 +00:00
|
|
|
* eg $filearray = $_FILES['userfile1']
|
2004-09-23 05:10:21 +00:00
|
|
|
* If left empty then the first element of the $_FILES array will be used
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $_FILES
|
|
|
|
* @param array $filearray A 1-dimensional sub-array of the $_FILES array
|
|
|
|
* @param boolean $returnerror ?
|
2004-09-23 02:48:41 +00:00
|
|
|
* @return boolean
|
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-09-06 04:28:07 +00:00
|
|
|
function print_file_upload_error($filearray = '', $returnerror = false) {
|
|
|
|
|
|
|
|
if ($filearray == '' or !isset($filearray['error'])) {
|
|
|
|
|
|
|
|
if (empty($_FILES)) return false;
|
|
|
|
|
|
|
|
$files = $_FILES; /// so we don't mess up the _FILES array for subsequent code
|
|
|
|
$filearray = array_shift($files); /// use first element of array
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($filearray['error']) {
|
|
|
|
|
|
|
|
case 0: // UPLOAD_ERR_OK
|
|
|
|
if ($filearray['size'] > 0) {
|
|
|
|
$errmessage = get_string('uploadproblem', $filearray['name']);
|
|
|
|
} else {
|
|
|
|
$errmessage = get_string('uploadnofilefound'); /// probably a dud file name
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // UPLOAD_ERR_INI_SIZE
|
|
|
|
$errmessage = get_string('uploadserverlimit');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: // UPLOAD_ERR_FORM_SIZE
|
|
|
|
$errmessage = get_string('uploadformlimit');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: // UPLOAD_ERR_PARTIAL
|
|
|
|
$errmessage = get_string('uploadpartialfile');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4: // UPLOAD_ERR_NO_FILE
|
|
|
|
$errmessage = get_string('uploadnofilefound');
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$errmessage = get_string('uploadproblem', $filearray['name']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($returnerror) {
|
|
|
|
return $errmessage;
|
|
|
|
} else {
|
|
|
|
notify($errmessage);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Returns an array with all the filenames in
|
|
|
|
* all subdirectories, relative to the given rootdir.
|
|
|
|
* If excludefile is defined, then that file/directory is ignored
|
|
|
|
* 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!)
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param string $rootdir ?
|
|
|
|
* @param string $excludefile If defined then the specified file/directory is ignored
|
|
|
|
* @param boolean $descend ?
|
|
|
|
* @param boolean $getdirs If true then (sub)directories are included in the output
|
|
|
|
* @param boolean $getfiles If true then files are included in the output
|
|
|
|
* @return array An array with all the filenames in
|
|
|
|
* all subdirectories, relative to the given rootdir
|
|
|
|
* @todo Finish documenting this function. Add examples of $excludefile usage.
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2004-09-22 14:39:15 +00:00
|
|
|
function get_directory_list($rootdir, $excludefile='', $descend=true, $getdirs=false, $getfiles=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);
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($firstchar == '.' or $file == 'CVS' or $file == $excludefile) {
|
2003-01-28 03:34:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
$fullfile = $rootdir .'/'. $file;
|
|
|
|
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) {
|
2004-09-22 14:39:15 +00:00
|
|
|
$dirs[] = $file .'/'. $subdir;
|
2004-05-04 01:21:17 +00:00
|
|
|
}
|
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-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Adds up all the files in a directory and works out the size.
|
|
|
|
*
|
|
|
|
* @param string $rootdir ?
|
|
|
|
* @param string $excludefile ?
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return array
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-09-22 14:39:15 +00:00
|
|
|
function get_directory_size($rootdir, $excludefile='') {
|
2004-05-16 08:52:32 +00:00
|
|
|
|
|
|
|
$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);
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($firstchar == '.' or $file == 'CVS' or $file == $excludefile) {
|
2004-05-16 08:52:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
$fullfile = $rootdir .'/'. $file;
|
|
|
|
if (filetype($fullfile) == 'dir') {
|
2004-05-16 08:52:32 +00:00
|
|
|
$size += get_directory_size($fullfile, $excludefile);
|
|
|
|
} else {
|
|
|
|
$size += filesize($fullfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dir);
|
|
|
|
|
|
|
|
return $size;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Converts numbers like 10M into bytes.
|
|
|
|
*
|
|
|
|
* @param mixed $size The size to be converted
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2002-08-07 08:26:48 +00:00
|
|
|
function get_real_size($size=0) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Converts bytes into display form
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param string $size ?
|
|
|
|
* @return string
|
2004-09-25 05:29:21 +00:00
|
|
|
* @staticvar string $gb Localized string for size in gigabytes
|
|
|
|
* @staticvar string $mb Localized string for size in megabytes
|
|
|
|
* @staticvar string $kb Localized string for size in kilobytes
|
|
|
|
* @staticvar string $b Localized string for size in bytes
|
2004-09-25 01:29:37 +00:00
|
|
|
* @todo Finish documenting this function. Verify return type.
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2002-09-01 14:29:17 +00:00
|
|
|
function display_size($size) {
|
2003-10-06 18:02:35 +00:00
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
static $gb, $mb, $kb, $b;
|
2003-10-06 18:02:35 +00:00
|
|
|
|
|
|
|
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 {
|
2004-09-22 14:39:15 +00:00
|
|
|
$size = $size .' '. $b;
|
2002-09-01 14:29:17 +00:00
|
|
|
}
|
|
|
|
return $size;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
2004-09-23 05:10:21 +00:00
|
|
|
* Cleans a given filename by removing suspicious or troublesome characters
|
|
|
|
* Only these are allowed:
|
|
|
|
* alphanumeric _ - .
|
2004-09-23 02:48:41 +00:00
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param string $string ?
|
|
|
|
* @return string
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2002-08-04 16:20:30 +00:00
|
|
|
function clean_filename($string) {
|
2004-09-22 14:39:15 +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-09-22 14:39:15 +00:00
|
|
|
$string = eregi_replace("_+", '_', $string);
|
2004-09-25 01:29:37 +00:00
|
|
|
return $string;
|
2002-08-04 16:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-27 08:47:27 +00:00
|
|
|
/// STRING TRANSLATION ////////////////////////////////////////
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Returns the code for the current language
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @param $USER
|
|
|
|
* @param $SESSION
|
|
|
|
* @return string
|
|
|
|
*/
|
2002-10-02 10:33:05 +00:00
|
|
|
function 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
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Given a string to translate - prints it out.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param string $identifier ?
|
|
|
|
* @param string $module ?
|
|
|
|
* @param mixed $a ?
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2004-09-22 14:39:15 +00:00
|
|
|
function print_string($identifier, $module='', $a=NULL) {
|
2002-12-20 14:44:14 +00:00
|
|
|
echo get_string($identifier, $module, $a);
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Return the translated string specified by $identifier as
|
|
|
|
* 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"
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param string $identifier ?
|
|
|
|
* @param string $module ?
|
|
|
|
* @param mixed $a ?
|
|
|
|
* @return string
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
|
|
|
function get_string($identifier, $module='', $a=NULL) {
|
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
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($module == '') {
|
|
|
|
$module = 'moodle';
|
2002-06-27 08:47:27 +00:00
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$langpath = $CFG->dirroot .'/lang';
|
2004-09-23 02:48:41 +00:00
|
|
|
$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
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($module != 'moodle') {
|
|
|
|
$modlangpath = $CFG->dirroot .'/mod/'. $module .'/lang';
|
|
|
|
$langfile = $modlangpath .'/'. $lang .'/'. $module .'.php';
|
2004-05-24 09:19:59 +00:00
|
|
|
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-09-22 14:39:15 +00:00
|
|
|
if ($lang == 'en') {
|
|
|
|
return '[['. $identifier .']]';
|
2003-06-12 03:27:24 +00:00
|
|
|
}
|
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
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($result = get_string_from_file('parentlanguage', $langpath .'/'. $lang .'/moodle.php', "\$parentlang")) {
|
2003-06-12 03:27:24 +00:00
|
|
|
eval($result);
|
|
|
|
if (!empty($parentlang)) {
|
2004-09-22 14:39:15 +00:00
|
|
|
$langfile = $langpath .'/'. $parentlang .'/'. $module .'.php';
|
2003-06-12 03:27:24 +00:00
|
|
|
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
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$langfile = $langpath .'/en/'. $module .'.php';
|
2003-06-12 03:27:24 +00:00
|
|
|
if (!file_exists($langfile)) {
|
2004-09-22 14:39:15 +00:00
|
|
|
return 'ERROR: No lang file ('. $langpath .'/en/'. $module .'.php)!';
|
2003-06-12 03:27:24 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($module != 'moodle') {
|
|
|
|
$langfile = $modlangpath .'/en/'. $module .'.php';
|
2004-05-24 09:19:59 +00:00
|
|
|
if (file_exists($langfile)) {
|
|
|
|
if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) {
|
|
|
|
eval($result);
|
|
|
|
return $resultstring;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
return '[['. $identifier .']]'; // Last resort
|
2002-06-27 08:47:27 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* This function is only used from {@link get_string()}.
|
2004-09-23 05:10:21 +00:00
|
|
|
*
|
2004-09-28 16:40:26 +00:00
|
|
|
* @internal Only used from get_string, not meant to be public API
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param string $identifier ?
|
|
|
|
* @param string $langfile ?
|
|
|
|
* @param string $destination ?
|
|
|
|
* @return string|false ?
|
2004-09-25 05:29:21 +00:00
|
|
|
* @staticvar array $strings Localized strings
|
2004-09-28 16:40:26 +00:00
|
|
|
* @access private
|
2004-09-25 01:29:37 +00:00
|
|
|
* @todo Finish documenting this function.
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2002-06-27 08:47:27 +00:00
|
|
|
function get_string_from_file($identifier, $langfile, $destination) {
|
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;
|
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
return $destination .'= sprintf("'. $string[$identifier] .'");';
|
2002-06-27 08:47:27 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Converts an array of strings to their localized value.
|
|
|
|
*
|
|
|
|
* @param array $array An array of strings
|
|
|
|
* @param string $module The language module that these strings can be found in.
|
|
|
|
* @return string
|
|
|
|
*/
|
2004-07-25 13:48:55 +00:00
|
|
|
function get_strings($array, $module='') {
|
|
|
|
|
|
|
|
$string = NULL;
|
|
|
|
foreach ($array as $item) {
|
|
|
|
$string->$item = get_string($item, $module);
|
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of language codes and their full names
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @return array An associative array with contents in the form of LanguageCode => LanguageName
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2002-09-14 03:43:24 +00:00
|
|
|
function get_list_of_languages() {
|
|
|
|
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) {
|
2004-09-22 14:39:15 +00:00
|
|
|
if (file_exists($CFG->dirroot .'/lang/'. $lang .'/moodle.php')) {
|
|
|
|
include($CFG->dirroot .'/lang/'. $lang .'/moodle.php');
|
|
|
|
$languages[$lang] = $string['thislanguage'].' ('. $lang .')';
|
2003-05-03 06:50:29 +00:00
|
|
|
unset($string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
if (!$langdirs = get_list_of_plugins('lang')) {
|
2003-05-03 06:50:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach ($langdirs as $lang) {
|
2004-09-22 14:39:15 +00:00
|
|
|
include($CFG->dirroot .'/lang/'. $lang .'/moodle.php');
|
|
|
|
$languages[$lang] = $string['thislanguage'] .' ('. $lang .')';
|
2003-05-03 06:50:29 +00:00
|
|
|
unset($string);
|
|
|
|
}
|
2002-09-14 03:43:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $languages;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of country names in the current language
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @uses $USER
|
|
|
|
* @return string?
|
|
|
|
* @todo Finish documenting this function.
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2003-09-26 08:04:48 +00:00
|
|
|
function get_list_of_countries() {
|
|
|
|
global $CFG, $USER;
|
|
|
|
|
|
|
|
$lang = current_language();
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (!file_exists($CFG->dirroot .'/lang/'. $lang .'/countries.php')) {
|
|
|
|
if ($parentlang = get_string('parentlanguage')) {
|
|
|
|
if (file_exists($CFG->dirroot .'/lang/'. $parentlang .'/countries.php')) {
|
2003-09-26 08:21:11 +00:00
|
|
|
$lang = $parentlang;
|
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
$lang = 'en'; // countries.php must exist in this pack
|
2003-09-26 08:21:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
$lang = 'en'; // countries.php must exist in this pack
|
2003-09-26 08:21:11 +00:00
|
|
|
}
|
2003-09-26 08:04:48 +00:00
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +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;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of picture names in the current language
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @return string?
|
|
|
|
* @todo Finish documenting this function.
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
|
|
|
function get_list_of_pixnames() {
|
2003-11-07 15:11:34 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$lang = current_language();
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (!file_exists($CFG->dirroot .'/lang/'. $lang .'/pix.php')) {
|
|
|
|
if ($parentlang = get_string('parentlanguage')) {
|
|
|
|
if (file_exists($CFG->dirroot .'/lang/'. $parentlang .'/pix.php')) {
|
2003-11-07 15:11:34 +00:00
|
|
|
$lang = $parentlang;
|
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
$lang = 'en'; // countries.php must exist in this pack
|
2003-11-07 15:11:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
$lang = 'en'; // countries.php must exist in this pack
|
2003-11-07 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
include_once($CFG->dirroot .'/lang/'. $lang .'/pix.php');
|
2003-11-07 15:11:34 +00:00
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Can include a given document file (depends on second
|
|
|
|
* parameter) or just return info about it.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @param string $file ?
|
|
|
|
* @param boolean $include ?
|
|
|
|
* @return ?
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
|
|
|
function document_file($file, $include=true) {
|
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;
|
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +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) {
|
2004-09-22 14:39:15 +00:00
|
|
|
$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 ////////////////////////////////////////////////
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* rc4encrypt
|
|
|
|
*
|
|
|
|
* @param string $data ?
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return string
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2001-11-22 06:23:56 +00:00
|
|
|
function rc4encrypt($data) {
|
2004-09-22 14:39:15 +00:00
|
|
|
$password = 'nfgjeingjk';
|
|
|
|
return endecrypt($password, $data, '');
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* rc4decrypt
|
|
|
|
*
|
|
|
|
* @param string $data ?
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return string
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2001-11-22 06:23:56 +00:00
|
|
|
function rc4decrypt($data) {
|
2004-09-22 14:39:15 +00:00
|
|
|
$password = 'nfgjeingjk';
|
|
|
|
return endecrypt($password, $data, 'de');
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Based on a class by Mukul Sabharwal [mukulsabharwal @ yahoo.com]
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param string $pwd ?
|
|
|
|
* @param string $data ?
|
|
|
|
* @param string $case ?
|
|
|
|
* @return string
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
|
|
|
function endecrypt ($pwd, $data, $case) {
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
if ($case == 'de') {
|
|
|
|
$data = urldecode($data);
|
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$key[] = '';
|
|
|
|
$box[] = '';
|
|
|
|
$temp_swap = '';
|
2001-11-22 06:23:56 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$temp = '';
|
|
|
|
$k = '';
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$cipherby = '';
|
|
|
|
$cipher = '';
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
$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 ////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* Call this function to add an event to the calendar table
|
2004-09-23 05:10:21 +00:00
|
|
|
* and to call any calendar plugins
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @param array $event An associative array representing an event from the calendar table. The event will be identified by the id field. The object event should include the following:
|
|
|
|
* <ul>
|
|
|
|
* <li><b>$event->name</b> - Name for the event
|
|
|
|
* <li><b>$event->description</b> - Description of the event (defaults to '')
|
2004-09-25 20:27:15 +00:00
|
|
|
* <li><b>$event->format</b> - Format for the description (using formatting types defined at the top of weblib.php)
|
2004-09-25 01:29:37 +00:00
|
|
|
* <li><b>$event->courseid</b> - The id of the course this event belongs to (0 = all courses)
|
|
|
|
* <li><b>$event->groupid</b> - The id of the group this event belongs to (0 = no group)
|
|
|
|
* <li><b>$event->userid</b> - The id of the user this event belongs to (0 = no user)
|
|
|
|
* <li><b>$event->modulename</b> - Name of the module that creates this event
|
|
|
|
* <li><b>$event->instance</b> - Instance of the module that owns this event
|
|
|
|
* <li><b>$event->eventtype</b> - The type info together with the module info could
|
|
|
|
* be used by calendar plugins to decide how to display event
|
|
|
|
* <li><b>$event->timestart</b>- Timestamp for start of event
|
|
|
|
* <li><b>$event->timeduration</b> - Duration (defaults to zero)
|
2004-09-25 20:27:15 +00:00
|
|
|
* <li><b>$event->>visible</b> - 0 if the event should be hidden (e.g. because the activity that created it is hidden)
|
2004-09-25 01:29:37 +00:00
|
|
|
* </ul>
|
|
|
|
* @return int The id number of the resulting record
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
|
|
|
function add_event($event) {
|
2004-01-17 09:47:45 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$event->timemodified = time();
|
2004-04-01 10:11:20 +00:00
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (!$event->id = insert_record('event', $event)) {
|
2004-01-17 09:47:45 +00:00
|
|
|
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
|
2004-09-22 14:39:15 +00:00
|
|
|
if (file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) {
|
|
|
|
include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php');
|
2004-01-17 09:47:45 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* Call this function to update an event in the calendar table
|
2004-09-23 05:10:21 +00:00
|
|
|
* the event will be identified by the id field of the $event object.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @param array $event An associative array representing an event from the calendar table. The event will be identified by the id field.
|
|
|
|
* @return boolean
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-01-17 09:47:45 +00:00
|
|
|
function update_event($event) {
|
|
|
|
|
|
|
|
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
|
2004-09-22 14:39:15 +00:00
|
|
|
if (file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) {
|
|
|
|
include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php');
|
2004-01-17 09:47:45 +00:00
|
|
|
$calendar_update_event = $CFG->calendar.'_update_event';
|
|
|
|
if (function_exists($calendar_update_event)) {
|
|
|
|
$calendar_update_event($event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
return update_record('event', $event);
|
2004-01-17 09:47:45 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Call this function to delete the event with id $id from calendar table.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @param int $id The id of an event from the 'calendar' table.
|
|
|
|
* @return array An associative array with the results from the SQL call.
|
|
|
|
* @todo Verify return type
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2004-01-17 09:47:45 +00:00
|
|
|
function delete_event($id) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!empty($CFG->calendar)) { // call the delete_event function of the selected calendar
|
2004-09-22 14:39:15 +00:00
|
|
|
if (file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) {
|
|
|
|
include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php');
|
2004-01-17 09:47:45 +00:00
|
|
|
$calendar_delete_event = $CFG->calendar.'_delete_event';
|
|
|
|
if (function_exists($calendar_delete_event)) {
|
|
|
|
$calendar_delete_event($id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
return delete_records('event', 'id', $id);
|
2004-01-17 09:47:45 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* Call this function to hide an event in the calendar table
|
2004-09-23 05:10:21 +00:00
|
|
|
* the event will be identified by the id field of the $event object.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @param array $event An associative array representing an event from the calendar table. The event will be identified by the id field.
|
|
|
|
* @return array An associative array with the results from the SQL call.
|
|
|
|
* @todo Verify return type
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2004-05-05 07:07:56 +00:00
|
|
|
function hide_event($event) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!empty($CFG->calendar)) { // call the update_event function of the selected calendar
|
2004-09-22 14:39:15 +00:00
|
|
|
if (file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) {
|
|
|
|
include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php');
|
2004-05-05 07:07:56 +00:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Call this function to unhide an event in the calendar table
|
|
|
|
* the event will be identified by the id field of the $event object.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @param array $event An associative array representing an event from the calendar table. The event will be identified by the id field.
|
|
|
|
* @return array An associative array with the results from the SQL call.
|
|
|
|
* @todo Verify return type
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2004-05-05 07:07:56 +00:00
|
|
|
function show_event($event) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!empty($CFG->calendar)) { // call the update_event function of the selected calendar
|
2004-09-22 14:39:15 +00:00
|
|
|
if (file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) {
|
|
|
|
include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php');
|
2004-05-05 07:07:56 +00:00
|
|
|
$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-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Lists plugin directories within some directory
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @param string $plugin ?
|
|
|
|
* @param string $exclude ?
|
|
|
|
* @return array
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-09-22 14:39:15 +00:00
|
|
|
function get_list_of_plugins($plugin='mod', $exclude='') {
|
2002-12-23 03:07:01 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$basedir = opendir($CFG->dirroot .'/'. $plugin);
|
2002-12-23 03:07:01 +00:00
|
|
|
while ($dir = readdir($basedir)) {
|
2003-01-28 03:34:26 +00:00
|
|
|
$firstchar = substr($dir, 0, 1);
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($firstchar == '.' or $dir == 'CVS' or $dir == '_vti_cnf' or $dir == $exclude) {
|
2002-12-23 03:07:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
if (filetype($CFG->dirroot .'/'. $plugin .'/'. $dir) != 'dir') {
|
2002-12-23 03:07:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$plugins[] = $dir;
|
|
|
|
}
|
|
|
|
if ($plugins) {
|
|
|
|
asort($plugins);
|
|
|
|
}
|
|
|
|
return $plugins;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* Returns true if the current version of PHP is greater that the specified one.
|
2004-09-23 05:10:21 +00:00
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param string $version The version of php being tested.
|
|
|
|
* @return boolean
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-09-22 14:39:15 +00:00
|
|
|
function check_php_version($version='4.1.0') {
|
2004-09-25 01:29:37 +00:00
|
|
|
$minversion = intval(str_replace('.', '', $version));
|
2004-09-22 14:39:15 +00:00
|
|
|
$curversion = intval(str_replace('.', '', phpversion()));
|
2002-08-23 02:14:19 +00:00
|
|
|
return ($curversion >= $minversion);
|
|
|
|
}
|
|
|
|
|
2002-10-10 07:26:10 +00:00
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Checks to see if is a browser matches the specified
|
|
|
|
* brand and is equal or better version.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $_SERVER
|
|
|
|
* @param string $brand The browser identifier being tested
|
|
|
|
* @param int $version The version of the browser
|
|
|
|
* @return boolean
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
|
|
|
function check_browser_version($brand='MSIE', $version=5.5) {
|
2004-09-22 14:39:15 +00:00
|
|
|
$agent = $_SERVER['HTTP_USER_AGENT'];
|
2003-10-29 08:06:11 +00:00
|
|
|
|
|
|
|
if (empty($agent)) {
|
2002-10-10 07:26:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2003-10-29 08:06:11 +00:00
|
|
|
|
|
|
|
switch ($brand) {
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
case 'Gecko': /// Gecko based browsers
|
2003-10-29 08:06:11 +00:00
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if (substr_count($agent, 'Camino')) { // MacOS X Camino not supported.
|
2003-10-29 08:06:11 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
case 'MSIE': /// Internet Explorer
|
2003-10-29 08:06:11 +00:00
|
|
|
|
2004-02-10 14:20:44 +00:00
|
|
|
if (strpos($agent, 'Opera')) { // Reject Opera
|
|
|
|
return false;
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
$string = explode(';', $agent);
|
2003-10-29 08:06:11 +00:00
|
|
|
if (!isset($string[1])) {
|
|
|
|
return false;
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
$string = explode(' ', trim($string[1]));
|
2003-10-29 08:06:11 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param string $ini_get_arg ?
|
|
|
|
* @return boolean
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
|
|
|
function ini_get_bool($ini_get_arg) {
|
2003-05-17 02:05:10 +00:00
|
|
|
$temp = ini_get($ini_get_arg);
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($temp == '1' or strtolower($temp) == 'on') {
|
2003-05-17 02:05:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Compatibility stub to provide backward compatibility
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* Determines if the HTML editor is enabled.
|
|
|
|
* @deprecated Use {@link can_use_html_editor()} instead.
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
|
|
|
function can_use_richtext_editor() {
|
2003-11-03 15:17:21 +00:00
|
|
|
return can_use_html_editor();
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* Determines if the HTML editor is enabled.
|
|
|
|
*
|
|
|
|
* This depends on site and user
|
2004-09-23 05:10:21 +00:00
|
|
|
* settings, as well as the current browser being used.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return string|false Returns false if editor is not being used, otherwise
|
|
|
|
* returns 'MSIE' or 'Gecko'.
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
|
|
|
function can_use_html_editor() {
|
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)) {
|
2004-09-22 14:39:15 +00:00
|
|
|
if (check_browser_version('MSIE', 5.5)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Hack to find out the GD version by parsing phpinfo output
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return int GD version (1, 2, or 0)
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2002-09-19 12:01:55 +00:00
|
|
|
function check_gd_version() {
|
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();
|
2004-09-22 14:39:15 +00:00
|
|
|
if (substr_count($gd_info['GD Version'], '2.')) {
|
2003-05-01 23:12:05 +00:00
|
|
|
$gdversion = 2;
|
2004-09-22 14:39:15 +00:00
|
|
|
} else if (substr_count($gd_info['GD Version'], '1.')) {
|
2003-05-19 03:27:13 +00:00
|
|
|
$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
|
|
|
|
2004-09-25 01:29:37 +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) {
|
2004-09-25 01:29:37 +00:00
|
|
|
$parts = explode('</td>', $text);
|
2003-05-01 23:12:05 +00:00
|
|
|
foreach ($parts as $key => $val) {
|
|
|
|
$parts[$key] = trim(strip_tags($val));
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($parts[0] == 'GD Version') {
|
|
|
|
if (substr_count($parts[1], '2.0')) {
|
|
|
|
$parts[1] = '2.0';
|
2003-05-01 23:12:05 +00:00
|
|
|
}
|
|
|
|
$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
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* Determine if moodle installation requires update
|
2004-09-23 05:10:21 +00:00
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* Checks version numbers of main code and all modules to see
|
|
|
|
* if there are any mismatches
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @return boolean
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
|
|
|
function moodle_needs_upgrading() {
|
2002-12-20 14:44:14 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
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;
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($mods = get_list_of_plugins('mod')) {
|
2002-12-20 14:44:14 +00:00
|
|
|
foreach ($mods as $mod) {
|
2004-09-22 14:39:15 +00:00
|
|
|
$fullmod = $CFG->dirroot .'/mod/'. $mod;
|
2002-12-20 14:44:14 +00:00
|
|
|
unset($module);
|
2004-09-22 14:39:15 +00:00
|
|
|
if (!is_readable($fullmod .'/version.php')) {
|
|
|
|
notify('Module "'. $mod .'" is not readable - check permissions');
|
2003-01-08 09:07:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
include_once($fullmod .'/version.php'); # defines $module with version etc
|
|
|
|
if ($currmodule = get_record('modules', 'name', $mod)) {
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($module->version > $currmodule->version) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// MISCELLANEOUS ////////////////////////////////////////////////////////////////////
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* Notify admin users or admin user of any failed logins (since last notification).
|
2004-09-23 05:10:21 +00:00
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @uses $db
|
|
|
|
* @todo Finish documenting this function. Add long description with more detail on what it does.
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
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
|
|
|
|
|
|
|
switch ($CFG->notifyloginfailures) {
|
|
|
|
case 'mainadmin' :
|
|
|
|
$recip = array(get_admin());
|
|
|
|
break;
|
|
|
|
case 'alladmins':
|
|
|
|
$recip = get_admins();
|
|
|
|
break;
|
|
|
|
}
|
2004-09-21 11:41:58 +00:00
|
|
|
|
2004-07-25 13:48:55 +00:00
|
|
|
if (empty($CFG->lastnotifyfailure)) {
|
|
|
|
$CFG->lastnotifyfailure=0;
|
|
|
|
}
|
2004-09-21 11:41:58 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$notifyipsrs = $db->Execute('SELECT ip FROM '. $CFG->prefix .'log WHERE time > '. $CFG->lastnotifyfailure .'
|
|
|
|
AND module=\'login\' AND action=\'error\' GROUP BY ip HAVING count(*) > '. $CFG->notifyloginthreshold);
|
2004-07-25 13:48:55 +00:00
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$notifyusersrs = $db->Execute('SELECT info FROM '. $CFG->prefix .'log WHERE time > '. $CFG->lastnotifyfailure .'
|
|
|
|
AND module=\'login\' AND action=\'error\' GROUP BY info HAVING count(*) > '. $CFG->notifyloginthreshold);
|
2004-09-21 11:41:58 +00:00
|
|
|
|
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()) {
|
2004-09-22 14:39:15 +00:00
|
|
|
$ipstr .= "'". $row['ip'] ."',";
|
2004-07-25 13:48:55 +00:00
|
|
|
}
|
|
|
|
$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()) {
|
2004-09-22 14:39:15 +00:00
|
|
|
$userstr .= "'". $row['info'] ."',";
|
2004-07-25 13:48:55 +00:00
|
|
|
}
|
|
|
|
$userstr = substr($userstr,0,strlen($userstr)-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen($userstr) > 0 || strlen($ipstr) > 0) {
|
|
|
|
$count = 0;
|
2004-09-22 14:39:15 +00:00
|
|
|
$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);
|
2004-09-21 11:41:58 +00:00
|
|
|
|
2004-07-25 13:48:55 +00:00
|
|
|
// 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-09-28 05:53:08 +00:00
|
|
|
if (is_array($recip) and count($recip) > 0 and ((time() - HOURSECS) > $CFG->lastnotifyfailure)
|
2004-07-25 13:48:55 +00:00
|
|
|
and is_array($logs) and count($logs) > 0) {
|
2004-09-21 11:41:58 +00:00
|
|
|
|
2004-07-29 14:54:25 +00:00
|
|
|
$message = '';
|
2004-07-25 13:48:55 +00:00
|
|
|
$site = get_site();
|
2004-09-22 14:39:15 +00:00
|
|
|
$subject = get_string('notifyloginfailuressubject', '', $site->fullname);
|
|
|
|
$message .= get_string('notifyloginfailuresmessagestart', '', $CFG->wwwroot)
|
2004-07-25 13:48:55 +00:00
|
|
|
.(($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-09-22 14:39:15 +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);
|
|
|
|
}
|
2004-09-22 14:39:15 +00:00
|
|
|
$conf->name = 'lastnotifyfailure';
|
2004-07-25 13:48:55 +00:00
|
|
|
$conf->value = time();
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($current = get_record('config', 'name', 'lastnotifyfailure')) {
|
2004-07-25 13:48:55 +00:00
|
|
|
$conf->id = $current->id;
|
2004-09-22 14:39:15 +00:00
|
|
|
if (! update_record('config', $conf)) {
|
|
|
|
mtrace('Could not update last notify time');
|
2004-07-25 13:48:55 +00:00
|
|
|
}
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
} else if (! insert_record('config', $conf)) {
|
|
|
|
mtrace('Could not set last notify time');
|
2004-07-25 13:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* moodle_setlocale
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @uses $USER
|
|
|
|
* @uses $SESSION
|
|
|
|
* @param string $locale ?
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Converts string to lowercase using most compatible function available.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param string $string The string to convert to all lowercase characters.
|
|
|
|
* @param string $encoding The encoding on the string.
|
|
|
|
* @return string
|
|
|
|
* @todo Add examples of calling this function with/without encoding types
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2003-04-21 16:39:43 +00:00
|
|
|
function moodle_strtolower ($string, $encoding='') {
|
|
|
|
if (function_exists('mb_strtolower')) {
|
|
|
|
if($encoding===''){
|
|
|
|
return mb_strtolower($string); //use multibyte support with default encoding
|
|
|
|
} else {
|
2004-09-25 01:29:37 +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
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
2004-09-25 01:29:37 +00:00
|
|
|
* Count words in a string.
|
2004-09-23 05:10:21 +00:00
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* Words are defined as things between whitespace.
|
|
|
|
*
|
|
|
|
* @param string $string The text to be searched for words.
|
|
|
|
* @return int The count of words in the specified string
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
|
|
|
function count_words($string) {
|
2002-12-20 14:44:14 +00:00
|
|
|
$string = strip_tags($string);
|
|
|
|
return count(preg_split("/\w\b/", $string)) - 1;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Generate and return a random string of the specified length.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $length The length of the string to be created.
|
2004-09-23 05:10:21 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2002-12-23 03:07:01 +00:00
|
|
|
function random_string ($length=15) {
|
2004-09-22 14:39:15 +00:00
|
|
|
$pool = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
$pool .= 'abcdefghijklmnopqrstuvwxyz';
|
|
|
|
$pool .= '0123456789';
|
2002-12-23 03:07:01 +00:00
|
|
|
$poollen = strlen($pool);
|
|
|
|
mt_srand ((double) microtime() * 1000000);
|
2004-09-22 14:39:15 +00:00
|
|
|
$string = '';
|
2002-12-23 03:07:01 +00:00
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
|
$string .= substr($pool, (mt_rand()%($poollen)), 1);
|
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Given dates in seconds, how many weeks is the date from startdate
|
|
|
|
* The first week is 1, the second 2 etc ...
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param ? $startdate ?
|
|
|
|
* @param ? $thedate ?
|
|
|
|
* @return string
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
|
|
|
function getweek ($startdate, $thedate) {
|
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
|
|
|
}
|
|
|
|
|
2004-09-28 05:53:08 +00:00
|
|
|
return floor(($thedate - $startdate) / WEEKSECS) + 1;
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* returns a randomly generated password of length $maxlen. inspired by
|
2004-09-25 01:29:37 +00:00
|
|
|
* {@link http://www.phpbuilder.com/columns/jesus19990502.php3}
|
2004-09-23 05:10:21 +00:00
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $maxlength The maximum size of the password being generated.
|
|
|
|
* @return string
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2002-12-20 14:44:14 +00:00
|
|
|
function generate_password($maxlen=10) {
|
|
|
|
global $CFG;
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$fillers = '1234567890!$-+';
|
2002-12-20 14:44:14 +00:00
|
|
|
$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-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Given a float, prints it nicely
|
|
|
|
*
|
|
|
|
* @param float $num The float to print
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $places The number of decimal places to print.
|
2004-09-23 05:10:21 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2004-03-25 15:29:40 +00:00
|
|
|
function format_float($num, $places=1) {
|
2002-12-20 14:44:14 +00:00
|
|
|
return sprintf("%.$places"."f", $num);
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Given a simple array, this shuffles it up just like shuffle()
|
|
|
|
* Unlike PHP's shuffle() ihis function works on any machine.
|
|
|
|
*
|
|
|
|
* @param array $array The array to be rearranged
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function swapshuffle($array) {
|
2003-02-24 09:34:10 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Like {@link swapshuffle()}, but works on associative arrays
|
|
|
|
*
|
|
|
|
* @param array $array The associative array to be rearranged
|
|
|
|
* @return array
|
|
|
|
*/
|
2003-04-10 13:45:07 +00:00
|
|
|
function swapshuffle_assoc($array) {
|
2004-09-23 05:10:21 +00:00
|
|
|
///
|
2003-04-10 13:45:07 +00:00
|
|
|
|
|
|
|
$newkeys = swapshuffle(array_keys($array));
|
|
|
|
foreach ($newkeys as $newkey) {
|
|
|
|
$newarray[$newkey] = $array[$newkey];
|
|
|
|
}
|
|
|
|
return $newarray;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Given an arbitrary array, and a number of draws,
|
|
|
|
* this function returns an array with that amount
|
|
|
|
* of items. The indexes are retained.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param array $array ?
|
|
|
|
* @param ? $draws ?
|
|
|
|
* @return ?
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2003-02-24 09:34:10 +00:00
|
|
|
function draw_rand_array($array, $draws) {
|
|
|
|
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
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* microtime_diff
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param string $a ?
|
|
|
|
* @param string $b ?
|
|
|
|
* @return string
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2003-04-27 08:13:45 +00:00
|
|
|
function microtime_diff($a, $b) {
|
2004-09-22 14:39:15 +00:00
|
|
|
list($a_dec, $a_sec) = explode(' ', $a);
|
|
|
|
list($b_dec, $b_sec) = explode(' ', $b);
|
2003-04-27 08:13:45 +00:00
|
|
|
return $b_sec - $a_sec + $b_dec - $a_dec;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* Given a list (eg a,b,c,d,e) this function returns
|
|
|
|
* an array of 1->a, 2->b, 3->c etc
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param array $list ?
|
|
|
|
* @param string $separator ?
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
|
|
|
function make_menu_from_list($list, $separator=',') {
|
2003-08-15 13:59:24 +00:00
|
|
|
|
|
|
|
$array = array_reverse(explode($separator, $list), true);
|
|
|
|
foreach ($array as $key => $item) {
|
|
|
|
$outarray[$key+1] = trim($item);
|
|
|
|
}
|
|
|
|
return $outarray;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $gradingtype ?
|
|
|
|
* return int
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2003-08-18 05:10:35 +00:00
|
|
|
function make_grades_menu($gradingtype) {
|
|
|
|
$grades = array();
|
|
|
|
if ($gradingtype < 0) {
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($scale = get_record('scale', 'id', - $gradingtype)) {
|
2003-08-18 05:10:35 +00:00
|
|
|
return make_menu_from_list($scale->scale);
|
|
|
|
}
|
|
|
|
} else if ($gradingtype > 0) {
|
|
|
|
for ($i=$gradingtype; $i>=0; $i--) {
|
2004-09-22 14:39:15 +00:00
|
|
|
$grades[$i] = $i .' / '. $gradingtype;
|
2003-08-18 05:10:35 +00:00
|
|
|
}
|
|
|
|
return $grades;
|
|
|
|
}
|
|
|
|
return $grades;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* This function returns the nummber of activities
|
|
|
|
* using scaleid in a courseid
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $courseid ?
|
|
|
|
* @param int $scaleid ?
|
|
|
|
* @return int
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-09-25 01:29:37 +00:00
|
|
|
function course_scale_used($courseid, $scaleid) {
|
2004-05-16 00:36:00 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* This function returns the nummber of activities
|
|
|
|
* using scaleid in the entire site
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $scaleid ?
|
|
|
|
* @return int
|
|
|
|
* @todo Finish documenting this function. Is return type correct?
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2004-05-16 00:36:00 +00:00
|
|
|
function site_scale_used($scaleid) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$return = 0;
|
|
|
|
|
|
|
|
if (!empty($scaleid)) {
|
|
|
|
if ($courses = get_courses()) {
|
|
|
|
foreach ($courses as $course) {
|
|
|
|
$return += course_scale_used($course->id,$scaleid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* make_unique_id_code
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param string $extra ?
|
|
|
|
* @return string
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-09-22 14:39:15 +00:00
|
|
|
function make_unique_id_code($extra='') {
|
2003-08-23 12:22:27 +00:00
|
|
|
|
2004-09-22 14:39:15 +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:22:27 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2004-09-22 14:39:15 +00:00
|
|
|
return $hostname .'+'. $date .'+'. $random .'+'. $extra;
|
2003-08-23 14:18:44 +00:00
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
return $hostname .'+'. $date .'+'. $random;
|
2003-08-23 14:18:44 +00:00
|
|
|
}
|
2003-08-23 12:22:27 +00:00
|
|
|
}
|
|
|
|
|
2002-10-10 07:26:10 +00:00
|
|
|
|
2004-07-08 07:52:01 +00:00
|
|
|
/**
|
2004-09-25 01:29:37 +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
|
|
|
|
* Code for type 1 modified from user posted comments by mediator at
|
|
|
|
* {@link http://au.php.net/manual/en/function.ip2long.php}
|
|
|
|
*
|
|
|
|
* @param string $addr The address you are checking
|
|
|
|
* @param string $subnetstr The string of subnet addresses
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2004-07-08 07:52:01 +00:00
|
|
|
function address_in_subnet($addr, $subnetstr) {
|
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$subnets = explode(',', $subnetstr);
|
2004-07-08 07:52:01 +00:00
|
|
|
$found = false;
|
|
|
|
$addr = trim($addr);
|
|
|
|
|
|
|
|
foreach ($subnets as $subnet) {
|
|
|
|
$subnet = trim($subnet);
|
2004-09-22 14:39:15 +00:00
|
|
|
if (strpos($subnet, '/') !== false) { /// type 1
|
2004-07-08 07:52:01 +00:00
|
|
|
|
|
|
|
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-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* For outputting debugging info
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses STDOUT
|
|
|
|
* @param string $string ?
|
|
|
|
* @param string $eol ?
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-08-05 17:08:46 +00:00
|
|
|
function mtrace($string, $eol="\n") {
|
2004-08-07 03:59:53 +00:00
|
|
|
|
|
|
|
if (defined('STDOUT')) {
|
|
|
|
fwrite(STDOUT, $string.$eol);
|
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
echo $string . $eol;
|
2004-08-07 03:59:53 +00:00
|
|
|
}
|
|
|
|
|
2004-08-05 17:08:46 +00:00
|
|
|
flush();
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns most reliable client address
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return string The remote IP address
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
|
|
|
function getremoteaddr() {
|
2004-09-22 14:39:15 +00:00
|
|
|
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');
|
2004-09-04 10:54:54 +00:00
|
|
|
else $ip = false; //just in case
|
|
|
|
return $ip;
|
|
|
|
}
|
2004-08-05 17:08:46 +00:00
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
|
|
|
* html_entity_decode is only supported by php 4.3.0 and higher
|
|
|
|
* so if it is not predefined, define it here
|
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param string $string ?
|
|
|
|
* @return string
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-09-21 11:41:58 +00:00
|
|
|
if(!function_exists('html_entity_decode')) {
|
2004-09-23 05:10:21 +00:00
|
|
|
function html_entity_decode($string) {
|
2004-09-21 11:41:58 +00:00
|
|
|
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
|
|
|
|
$trans_tbl = array_flip($trans_tbl);
|
|
|
|
return strtr($string, $trans_tbl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-14 07:55:22 +00:00
|
|
|
// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
|
2004-09-25 20:27:15 +00:00
|
|
|
?>
|