2004-10-10 01:07:47 +00:00
|
|
|
<?php // $Id$
|
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
|
|
|
|
*/
|
2005-07-12 02:06:33 +00:00
|
|
|
|
|
|
|
/// CONSTANTS (Encased in phpdoc proper comments)/////////////////////////
|
2003-12-30 17:18:06 +00:00
|
|
|
|
2004-12-12 04:33:13 +00:00
|
|
|
/**
|
|
|
|
* Used by some scripts to check they are being called by Moodle
|
|
|
|
*/
|
|
|
|
define('MOODLE_INTERNAL', true);
|
|
|
|
|
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);
|
|
|
|
|
2005-07-12 02:06:33 +00:00
|
|
|
/// Date and time constants ///
|
2004-09-28 05:53:08 +00:00
|
|
|
/**
|
2004-09-29 18:56:50 +00:00
|
|
|
* Time constant - the number of seconds in a week
|
2004-09-28 05:53:08 +00:00
|
|
|
*/
|
2004-11-22 18:38:33 +00:00
|
|
|
define('WEEKSECS', 604800);
|
2004-09-29 18:56:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Time constant - the number of seconds in a day
|
|
|
|
*/
|
2004-09-28 05:53:08 +00:00
|
|
|
define('DAYSECS', 86400);
|
2004-09-29 18:56:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Time constant - the number of seconds in an hour
|
|
|
|
*/
|
2004-09-28 05:53:08 +00:00
|
|
|
define('HOURSECS', 3600);
|
2004-09-29 18:56:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Time constant - the number of seconds in a minute
|
|
|
|
*/
|
2004-09-28 05:53:08 +00:00
|
|
|
define('MINSECS', 60);
|
2004-09-29 18:56:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Time constant - the number of minutes in a day
|
|
|
|
*/
|
2004-09-28 05:53:08 +00:00
|
|
|
define('DAYMINS', 1440);
|
2004-09-29 18:56:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Time constant - the number of minutes in an hour
|
|
|
|
*/
|
2004-09-28 05:53:08 +00:00
|
|
|
define('HOURMINS', 60);
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2005-07-12 02:06:33 +00:00
|
|
|
/// Parameter constants - if set then the parameter is cleaned of scripts etc. ///
|
2004-10-01 12:09:52 +00:00
|
|
|
/**
|
2005-07-12 02:06:33 +00:00
|
|
|
* PARAM_RAW specifies a parameter that should contain:
|
2004-10-01 12:09:52 +00:00
|
|
|
*/
|
2005-02-25 13:10:24 +00:00
|
|
|
define('PARAM_RAW', 0x0000);
|
2005-07-12 02:06:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_CLEAN specifies a parameter that should contain:
|
|
|
|
*/
|
2005-02-25 13:10:24 +00:00
|
|
|
define('PARAM_CLEAN', 0x0001);
|
2005-07-12 02:06:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_INT specifies a parameter that should contain an integer value only.
|
|
|
|
*/
|
2005-02-25 13:10:24 +00:00
|
|
|
define('PARAM_INT', 0x0002);
|
2005-07-12 02:06:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_INTEGER - an alias for PARAM_INT
|
|
|
|
*/
|
|
|
|
define('PARAM_INTEGER', 0x0002);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_ALPHA specifies a parameter that should contain a string type (?).
|
|
|
|
*/
|
2005-02-25 13:10:24 +00:00
|
|
|
define('PARAM_ALPHA', 0x0004);
|
2005-07-12 02:06:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_INTEGER - an alias for PARAM_ALPHA
|
|
|
|
*/
|
|
|
|
define('PARAM_ACTION', 0x0004);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_INTEGER - an alias for PARAM_ALPHA
|
|
|
|
*/
|
|
|
|
define('PARAM_FORMAT', 0x0004);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_NOTAGS specifies a parameter that should contain:
|
|
|
|
*/
|
2005-02-25 13:10:24 +00:00
|
|
|
define('PARAM_NOTAGS', 0x0008);
|
2005-07-12 02:06:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_FILE specifies a parameter that should contain:
|
|
|
|
*/
|
2005-02-25 13:10:24 +00:00
|
|
|
define('PARAM_FILE', 0x0010);
|
2005-07-12 02:06:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_PATH specifies a parameter that should contain:
|
|
|
|
*/
|
2005-02-25 13:10:24 +00:00
|
|
|
define('PARAM_PATH', 0x0020);
|
2005-07-12 02:06:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_HOST specifies a parameter that should contain a fully qualified domain name (FQDN) or an IPv4 dotted quad (IP address)
|
|
|
|
*/
|
|
|
|
define('PARAM_HOST', 0x0040);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_URL specifies a parameter that should contain a string in the form of a properly formatted URL.
|
|
|
|
*/
|
2005-02-25 13:10:24 +00:00
|
|
|
define('PARAM_URL', 0x0080);
|
2005-07-12 02:06:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_LOCALURL specifies a parameter that should contain a string in the form of a properly formatted URL as well as one that refers to the local server itself. (NOT orthogonal to the others! Implies PARAM_URL!)
|
|
|
|
*/
|
|
|
|
define('PARAM_LOCALURL', 0x0180);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_CLEANFILE specifies a parameter that should contain:
|
|
|
|
*/
|
2005-01-29 15:53:24 +00:00
|
|
|
define('PARAM_CLEANFILE',0x0200);
|
2004-10-01 12:09:52 +00:00
|
|
|
|
2005-02-01 07:39:21 +00:00
|
|
|
/**
|
2005-07-12 02:06:33 +00:00
|
|
|
* PARAM_ALPHANUM specifies a parameter that should contain either numbers or letters only.
|
|
|
|
*/
|
|
|
|
define('PARAM_ALPHANUM', 0x0400);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_BOOL specifies a parameter that should contain a 0 or 1 boolean value only. It will convert to value 1 or 0 using empty()
|
|
|
|
*/
|
|
|
|
define('PARAM_BOOL', 0x0800);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_CLEANHTML specifies a parameter that should contain actual HTML code that you want cleaned and slashes removed
|
|
|
|
*/
|
|
|
|
define('PARAM_CLEANHTML',0x1000);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_ALPHAEXT specifies a parameter that should contain the same contents as PARAM_ALPHA plus the chars in quotes: "/-_" allowed
|
|
|
|
*/
|
|
|
|
define('PARAM_ALPHAEXT', 0x2000);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PARAM_SAFEDIR specifies a parameter that should contain a safe directory name, suitable for include() and require()
|
|
|
|
*/
|
|
|
|
define('PARAM_SAFEDIR', 0x4000);
|
|
|
|
|
|
|
|
/// Page types ///
|
|
|
|
/**
|
|
|
|
* PAGE_COURSE_VIEW is a definition of a page type. For more information on the page class see moodle/lib/pagelib.php.
|
2005-02-01 07:39:21 +00:00
|
|
|
*/
|
|
|
|
define('PAGE_COURSE_VIEW', 'course-view');
|
|
|
|
|
2005-07-12 02:06:33 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
/// PARAMETER HANDLING ////////////////////////////////////////////////////
|
2002-08-04 16:20:30 +00:00
|
|
|
|
2004-10-01 12:09:52 +00:00
|
|
|
/**
|
2004-11-22 18:38:33 +00:00
|
|
|
* Returns a particular value for the named variable, taken from
|
|
|
|
* POST or GET. If the parameter doesn't exist then an error is
|
2004-10-01 12:09:52 +00:00
|
|
|
* thrown because we require this variable.
|
|
|
|
*
|
2004-11-22 18:38:33 +00:00
|
|
|
* This function should be used to initialise all required values
|
|
|
|
* in a script that are based on parameters. Usually it will be
|
2004-10-01 12:09:52 +00:00
|
|
|
* used like this:
|
|
|
|
* $id = required_param('id');
|
|
|
|
*
|
|
|
|
* @param string $varname the name of the parameter variable we want
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param int $options a bit field that specifies any cleaning needed
|
2004-10-01 12:09:52 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function required_param($varname, $options=PARAM_CLEAN) {
|
|
|
|
|
|
|
|
if (isset($_POST[$varname])) { // POST has precedence
|
|
|
|
$param = $_POST[$varname];
|
|
|
|
} else if (isset($_GET[$varname])) {
|
|
|
|
$param = $_GET[$varname];
|
|
|
|
} else {
|
2004-10-08 06:11:17 +00:00
|
|
|
error('A required parameter ('.$varname.') was missing');
|
2004-10-01 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return clean_param($param, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-11-22 18:38:33 +00:00
|
|
|
* Returns a particular value for the named variable, taken from
|
2004-10-01 12:09:52 +00:00
|
|
|
* POST or GET, otherwise returning a given default.
|
|
|
|
*
|
2004-11-22 18:38:33 +00:00
|
|
|
* This function should be used to initialise all optional values
|
|
|
|
* in a script that are based on parameters. Usually it will be
|
2004-10-01 12:09:52 +00:00
|
|
|
* used like this:
|
|
|
|
* $name = optional_param('name', 'Fred');
|
|
|
|
*
|
|
|
|
* @param string $varname the name of the parameter variable we want
|
|
|
|
* @param mixed $default the default value to return if nothing is found
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param int $options a bit field that specifies any cleaning needed
|
2004-10-01 12:09:52 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function optional_param($varname, $default=NULL, $options=PARAM_CLEAN) {
|
|
|
|
|
|
|
|
if (isset($_POST[$varname])) { // POST has precedence
|
|
|
|
$param = $_POST[$varname];
|
|
|
|
} else if (isset($_GET[$varname])) {
|
|
|
|
$param = $_GET[$varname];
|
|
|
|
} else {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
return clean_param($param, $options);
|
|
|
|
}
|
|
|
|
|
2005-06-10 09:51:25 +00:00
|
|
|
/**
|
|
|
|
* Convenience function to test if a parameter is set
|
2005-07-12 02:06:33 +00:00
|
|
|
*
|
|
|
|
* @param string $varname The name of the parameter being checked
|
|
|
|
* @return bool Returns true if the parameter set, otherwise false
|
2005-06-10 09:51:25 +00:00
|
|
|
*/
|
|
|
|
function isset_param($varname) {
|
|
|
|
if (isset($_GET[$varname])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (isset($_POST[$varname])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-10-01 12:09:52 +00:00
|
|
|
/**
|
2004-11-22 18:38:33 +00:00
|
|
|
* Used by {@link optional_param()} and {@link required_param()} to
|
|
|
|
* clean the variables and/or cast to specific types, based on
|
2004-10-01 12:09:52 +00:00
|
|
|
* an options field.
|
2005-07-12 02:06:33 +00:00
|
|
|
* <code>
|
|
|
|
* $course->format = clean_param($course->format, PARAM_ALPHA);
|
|
|
|
* $selectedgrade_item = clean_param($selectedgrade_item, PARAM_CLEAN);
|
|
|
|
* </code>
|
2004-10-01 12:09:52 +00:00
|
|
|
*
|
2005-07-12 02:06:33 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @uses PARAM_CLEAN
|
|
|
|
* @uses PARAM_INT
|
|
|
|
* @uses PARAM_INTEGER
|
|
|
|
* @uses PARAM_ALPHA
|
|
|
|
* @uses PARAM_ALPHANUM
|
|
|
|
* @uses PARAM_NOTAGS
|
|
|
|
* @uses PARAM_ALPHATEXT
|
|
|
|
* @uses PARAM_BOOL
|
|
|
|
* @uses PARAM_SAFEDIR
|
|
|
|
* @uses PARAM_CLEANFILE
|
|
|
|
* @uses PARAM_FILE
|
|
|
|
* @uses PARAM_PATH
|
|
|
|
* @uses PARAM_HOST
|
|
|
|
* @uses PARAM_URL
|
|
|
|
* @uses PARAM_LOCALURL
|
|
|
|
* @uses PARAM_CLEANHTML
|
2004-10-01 12:09:52 +00:00
|
|
|
* @param mixed $param the variable we are cleaning
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param int $options a bit field that specifies the cleaning needed. This field is specified by combining PARAM_* definitions together with a logical or.
|
2004-10-01 12:09:52 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function clean_param($param, $options) {
|
|
|
|
|
2005-01-25 06:09:39 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2004-10-08 06:11:17 +00:00
|
|
|
if (!$options) {
|
|
|
|
return $param; // Return raw value
|
|
|
|
}
|
|
|
|
|
2004-10-01 15:06:39 +00:00
|
|
|
if ((string)$param == (string)(int)$param) { // It's just an integer
|
2004-10-01 12:09:52 +00:00
|
|
|
return (int)$param;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($options & PARAM_CLEAN) {
|
2005-05-04 23:14:09 +00:00
|
|
|
$param = stripslashes($param); // Needed by kses to work fine
|
2004-10-01 12:09:52 +00:00
|
|
|
$param = clean_text($param); // Sweep for scripts, etc
|
2005-05-04 23:14:09 +00:00
|
|
|
$param = addslashes($param); // Restore original request parameter slashes
|
2004-10-01 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($options & PARAM_INT) {
|
|
|
|
$param = (int)$param; // Convert to integer
|
|
|
|
}
|
|
|
|
|
2004-10-08 06:11:17 +00:00
|
|
|
if ($options & PARAM_ALPHA) { // Remove everything not a-z
|
2005-02-03 22:53:49 +00:00
|
|
|
$param = eregi_replace('[^a-zA-Z]', '', $param);
|
2004-10-08 06:11:17 +00:00
|
|
|
}
|
|
|
|
|
2005-02-03 18:14:45 +00:00
|
|
|
if ($options & PARAM_ALPHANUM) { // Remove everything not a-zA-Z0-9
|
|
|
|
$param = eregi_replace('[^A-Za-z0-9]', '', $param);
|
|
|
|
}
|
|
|
|
|
2005-03-23 01:54:45 +00:00
|
|
|
if ($options & PARAM_ALPHAEXT) { // Remove everything not a-zA-Z/_-
|
|
|
|
$param = eregi_replace('[^a-zA-Z/_-]', '', $param);
|
|
|
|
}
|
|
|
|
|
2005-02-03 18:14:45 +00:00
|
|
|
if ($options & PARAM_BOOL) { // Convert to 1 or 0
|
|
|
|
$param = empty($param) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
2004-10-08 06:11:17 +00:00
|
|
|
if ($options & PARAM_NOTAGS) { // Strip all tags completely
|
|
|
|
$param = strip_tags($param);
|
|
|
|
}
|
|
|
|
|
2005-07-04 20:23:53 +00:00
|
|
|
if ($options & PARAM_SAFEDIR) { // Remove everything not a-zA-Z0-9_-
|
|
|
|
$param = eregi_replace('[^a-zA-Z0-9_-]', '', $param);
|
|
|
|
}
|
|
|
|
|
2005-01-29 15:53:24 +00:00
|
|
|
if ($options & PARAM_CLEANFILE) { // allow only safe characters
|
|
|
|
$param = clean_filename($param);
|
|
|
|
}
|
|
|
|
|
2004-10-08 06:11:17 +00:00
|
|
|
if ($options & PARAM_FILE) { // Strip all suspicious characters from filename
|
2005-01-29 15:53:24 +00:00
|
|
|
$param = ereg_replace('[[:cntrl:]]|[<>"`\|\':\\/]', '', $param);
|
|
|
|
$param = ereg_replace('\.\.+', '', $param);
|
2005-02-22 23:36:20 +00:00
|
|
|
if($param == '.') {
|
|
|
|
$param = '';
|
|
|
|
}
|
2004-10-08 06:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($options & PARAM_PATH) { // Strip all suspicious characters from file path
|
2004-11-19 21:28:29 +00:00
|
|
|
$param = str_replace('\\\'', '\'', $param);
|
|
|
|
$param = str_replace('\\"', '"', $param);
|
2004-10-16 11:39:52 +00:00
|
|
|
$param = str_replace('\\', '/', $param);
|
2005-01-29 15:53:24 +00:00
|
|
|
$param = ereg_replace('[[:cntrl:]]|[<>"`\|\':]', '', $param);
|
2004-10-16 11:39:52 +00:00
|
|
|
$param = ereg_replace('\.\.+', '', $param);
|
2004-11-19 21:28:29 +00:00
|
|
|
$param = ereg_replace('//+', '/', $param);
|
2005-02-22 23:36:20 +00:00
|
|
|
$param = ereg_replace('/(\./)+', '/', $param);
|
2004-10-08 06:11:17 +00:00
|
|
|
}
|
|
|
|
|
2005-01-25 06:08:06 +00:00
|
|
|
if ($options & PARAM_HOST) { // allow FQDN or IPv4 dotted quad
|
2005-02-25 06:08:40 +00:00
|
|
|
preg_replace('/[^\.\d\w-]/','', $param ); // only allowed chars
|
2005-02-13 11:43:05 +00:00
|
|
|
// match ipv4 dotted quad
|
2005-01-25 06:08:06 +00:00
|
|
|
if (preg_match('/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/',$param, $match)){
|
|
|
|
// confirm values are ok
|
|
|
|
if ( $match[0] > 255
|
|
|
|
|| $match[1] > 255
|
2005-02-25 06:08:40 +00:00
|
|
|
|| $match[3] > 255
|
2005-01-25 06:08:06 +00:00
|
|
|
|| $match[4] > 255 ) {
|
|
|
|
// hmmm, what kind of dotted quad is this?
|
|
|
|
$param = '';
|
|
|
|
}
|
|
|
|
} elseif ( preg_match('/^[\w\d\.-]+$/', $param) // dots, hyphens, numbers
|
|
|
|
&& !preg_match('/^[\.-]/', $param) // no leading dots/hyphens
|
|
|
|
&& !preg_match('/[\.-]$/', $param) // no trailing dots/hyphens
|
|
|
|
) {
|
|
|
|
// all is ok - $param is respected
|
|
|
|
} else {
|
|
|
|
// all is not ok...
|
2005-02-25 06:08:40 +00:00
|
|
|
$param='';
|
|
|
|
}
|
2005-01-25 06:08:06 +00:00
|
|
|
}
|
|
|
|
|
2005-01-25 06:09:39 +00:00
|
|
|
if ($options & PARAM_URL) { // allow safe ftp, http, mailto urls
|
|
|
|
|
|
|
|
include_once($CFG->dirroot . '/lib/validateurlsyntax.php');
|
|
|
|
|
|
|
|
//
|
|
|
|
// Parameters to validateurlsyntax()
|
|
|
|
//
|
|
|
|
// s? scheme is optional
|
|
|
|
// H? http optional
|
|
|
|
// S? https optional
|
|
|
|
// F? ftp optional
|
|
|
|
// E? mailto optional
|
|
|
|
// u- user section not allowed
|
|
|
|
// P- password not allowed
|
|
|
|
// a? address optional
|
|
|
|
// I? Numeric IP address optional (can use IP or domain)
|
|
|
|
// p- port not allowed -- restrict to default port
|
|
|
|
// f? "file" path section optional
|
|
|
|
// q? query section optional
|
|
|
|
// r? fragment (anchor) optional
|
|
|
|
//
|
|
|
|
if (!empty($param) && validateUrlSyntax($param, 's?H?S?F?E?u-P-a?I?p-f?q?r?')) {
|
|
|
|
// all is ok, param is respected
|
|
|
|
} else {
|
|
|
|
$param =''; // not really ok
|
|
|
|
}
|
2005-01-30 04:39:55 +00:00
|
|
|
$options ^= PARAM_URL; // Turn off the URL bit so that simple PARAM_URLs don't test true for PARAM_LOCALURL
|
2005-01-25 06:09:39 +00:00
|
|
|
}
|
|
|
|
|
2005-02-25 06:08:40 +00:00
|
|
|
if ($options & PARAM_LOCALURL) {
|
2005-01-25 06:09:39 +00:00
|
|
|
// assume we passed the PARAM_URL test...
|
|
|
|
// allow http absolute, root relative and relative URLs within wwwroot
|
|
|
|
if (!empty($param)) {
|
2005-02-25 06:08:40 +00:00
|
|
|
if (preg_match(':^/:', $param)) {
|
2005-01-25 06:09:39 +00:00
|
|
|
// root-relative, ok!
|
2005-02-03 19:37:11 +00:00
|
|
|
} elseif (preg_match('/^'.preg_quote($CFG->wwwroot, '/').'/i',$param)) {
|
2005-01-25 06:09:39 +00:00
|
|
|
// absolute, and matches our wwwroot
|
2005-02-25 06:08:40 +00:00
|
|
|
} else {
|
2005-01-25 06:09:39 +00:00
|
|
|
// relative - let's make sure there are no tricks
|
|
|
|
if (validateUrlSyntax($param, 's-u-P-a-p-f+q?r?')) {
|
|
|
|
// looks ok.
|
|
|
|
} else {
|
|
|
|
$param = '';
|
2005-02-25 06:08:40 +00:00
|
|
|
}
|
2005-01-25 06:09:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-25 13:10:24 +00:00
|
|
|
if ($options & PARAM_CLEANHTML) {
|
|
|
|
$param = stripslashes($param); // Remove any slashes
|
|
|
|
$param = clean_text($param); // Sweep for scripts, etc
|
|
|
|
$param = trim($param); // Sweep for scripts, etc
|
|
|
|
}
|
|
|
|
|
2004-10-01 12:09:52 +00:00
|
|
|
return $param;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
2004-10-01 15:06:39 +00:00
|
|
|
* For security purposes, this function will check that the currently
|
|
|
|
* given sesskey (passed as a parameter to the script or this function)
|
|
|
|
* matches that of the current user.
|
2004-09-23 02:48:41 +00:00
|
|
|
*
|
2004-10-01 15:06:39 +00:00
|
|
|
* @param string $sesskey optionally provided sesskey
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-10-01 15:06:39 +00:00
|
|
|
*/
|
|
|
|
function confirm_sesskey($sesskey=NULL) {
|
|
|
|
global $USER;
|
|
|
|
|
2005-05-28 14:03:32 +00:00
|
|
|
if (!empty($USER->ignoresesskey) || !empty($CFG->ignoresesskey)) {
|
2004-12-22 02:54:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-10-01 15:06:39 +00:00
|
|
|
if (empty($sesskey)) {
|
|
|
|
$sesskey = required_param('sesskey'); // Check script parameters
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($USER->sesskey)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($USER->sesskey === $sesskey);
|
|
|
|
}
|
|
|
|
|
2005-06-10 08:55:26 +00:00
|
|
|
/**
|
|
|
|
* Unregister globals - emulates register_globals=off
|
|
|
|
* for additional security
|
2005-06-13 16:23:10 +00:00
|
|
|
* NOTE - needs some tweaking/thinking before use!!!
|
2005-06-10 08:55:26 +00:00
|
|
|
* Borrowed from Mambo
|
|
|
|
*/
|
|
|
|
function unregister_globals () {
|
|
|
|
$REQUEST = $_REQUEST;
|
|
|
|
$GET = $_GET;
|
|
|
|
$POST = $_POST;
|
|
|
|
$COOKIE = $_COOKIE;
|
|
|
|
if (isset ( $_SESSION )) {
|
|
|
|
$SESSION = $_SESSION;
|
|
|
|
}
|
|
|
|
$FILES = $_FILES;
|
|
|
|
$ENV = $_ENV;
|
|
|
|
$SERVER = $_SERVER;
|
|
|
|
foreach ($GLOBALS as $key => $value) {
|
|
|
|
if ( $key != 'GLOBALS' ) {
|
|
|
|
unset ( $GLOBALS [ $key ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$_REQUEST = $REQUEST;
|
|
|
|
$_GET = $GET;
|
|
|
|
$_POST = $POST;
|
|
|
|
$_COOKIE = $COOKIE;
|
|
|
|
if (isset ( $SESSION )) {
|
|
|
|
$_SESSION = $SESSION;
|
|
|
|
}
|
|
|
|
$_FILES = $FILES;
|
|
|
|
$_ENV = $ENV;
|
|
|
|
$_SERVER = $SERVER;
|
|
|
|
}
|
2004-10-01 15:06:39 +00:00
|
|
|
|
2005-06-07 12:37:44 +00:00
|
|
|
|
2004-10-01 15:06:39 +00:00
|
|
|
/**
|
|
|
|
* Ensure that a variable is set
|
|
|
|
*
|
|
|
|
* If $var is undefined throw an error, otherwise return $var.
|
|
|
|
* This function will soon be made obsolete by {@link required_param()}
|
2004-09-23 02:48:41 +00:00
|
|
|
*
|
2004-10-01 15:06:39 +00:00
|
|
|
* @param mixed $var the variable which may be unset
|
|
|
|
* @param mixed $default the value to return if $var is unset
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2002-12-20 14:44:14 +00:00
|
|
|
function require_variable($var) {
|
2005-06-10 08:55:26 +00:00
|
|
|
global $CFG;
|
|
|
|
if (!empty($CFG->disableglobalshack)) {
|
|
|
|
error( 'The require_variable() function is deprecated.' );
|
|
|
|
}
|
2002-12-20 14:44:14 +00:00
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @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) {
|
2005-06-10 08:55:26 +00:00
|
|
|
global $CFG;
|
|
|
|
if (!empty($CFG->disableglobalshack)) {
|
2005-06-10 09:51:25 +00:00
|
|
|
error( "The optional_variable() function is deprecated ($var, $default)." );
|
2005-06-10 08:55:26 +00:00
|
|
|
}
|
2002-12-20 14:44:14 +00:00
|
|
|
if (! isset($var)) {
|
|
|
|
$var = $default;
|
2002-08-04 16:20:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-10 09:51:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If a variable is empty set it to the default
|
|
|
|
* otherwise leave it alone
|
|
|
|
* @param mixed $var the variable to test
|
|
|
|
* @param mixed $default the default value
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool true if variable has changed
|
2005-06-10 09:51:25 +00:00
|
|
|
*/
|
|
|
|
function set_default( &$var, $default ) {
|
|
|
|
if (empty($var)) {
|
|
|
|
$var = $default;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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.
|
2005-06-02 05:39:41 +00:00
|
|
|
*
|
|
|
|
* Can also be used to update keys for plugin-scoped configs in config_plugin table.
|
|
|
|
* In that case it doesn't affect $CFG.
|
2004-09-23 02:48:41 +00:00
|
|
|
*
|
|
|
|
* @param string $name the key to set
|
|
|
|
* @param string $value the value to set
|
2005-06-02 05:39:41 +00:00
|
|
|
* @param string $plugin (optional) the plugin scope
|
2004-09-23 02:48:41 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @return bool
|
|
|
|
*/
|
2005-06-02 05:39:41 +00:00
|
|
|
function set_config($name, $value, $plugin=NULL) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// 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;
|
|
|
|
|
2005-06-02 05:39:41 +00:00
|
|
|
if (empty($plugin)) {
|
|
|
|
$CFG->$name = $value; // So it's defined for this invocation at least
|
|
|
|
|
|
|
|
if (get_field('config', 'name', 'name', $name)) {
|
|
|
|
return set_field('config', 'value', $value, 'name', $name);
|
|
|
|
} else {
|
|
|
|
$config->name = $name;
|
|
|
|
$config->value = $value;
|
|
|
|
return insert_record('config', $config);
|
|
|
|
}
|
|
|
|
} else { // plugin scope
|
|
|
|
if ($id = get_field('config_plugins', 'id', 'name', $name, 'plugin', $plugin)) {
|
|
|
|
return set_field('config_plugins', 'value', $value, 'id', $id);
|
|
|
|
} else {
|
|
|
|
$config->plugin = $plugin;
|
|
|
|
$config->name = $name;
|
|
|
|
$config->value = $value;
|
|
|
|
return insert_record('config_plugins', $config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get configuration values from the global config table
|
|
|
|
* or the config_plugins table.
|
|
|
|
*
|
|
|
|
* If called with no parameters it will do the right thing
|
|
|
|
* generating $CFG safely from the database without overwriting
|
|
|
|
* existing values.
|
|
|
|
*
|
|
|
|
* @param string $plugin
|
|
|
|
* @param string $name
|
|
|
|
* @uses $CFG
|
|
|
|
* @return hash-like object or single value
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function get_config($plugin=NULL, $name=NULL) {
|
2004-09-23 02:48:41 +00:00
|
|
|
|
2005-06-02 05:39:41 +00:00
|
|
|
global $CFG;
|
2002-11-10 08:43:44 +00:00
|
|
|
|
2005-06-02 05:39:41 +00:00
|
|
|
if (!empty($name)) { // the user is asking for a specific value
|
|
|
|
if (!empty($plugin)) {
|
|
|
|
return get_record('config_plugins', 'plugin' , $plugin, 'name', $name);
|
|
|
|
} else {
|
|
|
|
return get_record('config', 'name', $name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the user is after a recordset
|
|
|
|
if (!empty($plugin)) {
|
|
|
|
if ($configs=get_records('config_plugins', 'plugin', $plugin, '', 'name,value')) {
|
|
|
|
$configs = (array)$configs;
|
|
|
|
$localcfg = array();
|
|
|
|
foreach ($configs as $config) {
|
|
|
|
$localcfg[$config->name] = $config->value;
|
|
|
|
}
|
|
|
|
return (object)$localcfg;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2002-11-10 07:37:15 +00:00
|
|
|
} else {
|
2005-06-02 05:39:41 +00:00
|
|
|
// this was originally in setup.php
|
|
|
|
if ($configs = get_records('config')) {
|
|
|
|
$localcfg = (array)$CFG;
|
|
|
|
foreach ($configs as $config) {
|
|
|
|
if (!isset($localcfg[$config->name])) {
|
|
|
|
$localcfg[$config->name] = $config->value;
|
|
|
|
} else {
|
|
|
|
if ($localcfg[$config->name] != $config->value ) {
|
|
|
|
// complain if the DB has a different
|
|
|
|
// value than config.php does
|
|
|
|
error_log("\$CFG->{$config->name} in config.php ({$localcfg[$config->name]}) overrides database setting ({$config->value})");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$localcfg = (object)$localcfg;
|
|
|
|
return $localcfg;
|
|
|
|
} else {
|
|
|
|
// preserve $CFG if DB returns nothing or error
|
|
|
|
return $CFG;
|
|
|
|
}
|
|
|
|
|
2002-07-29 06:21:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-02 05:39:41 +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-12-01 21:47:02 +00:00
|
|
|
if(empty($USER) || empty($USER->id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
2005-07-12 03:09:25 +00:00
|
|
|
* @todo Add a better description and include usage examples. Add inline links to $USER and user functions in above line.
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
* @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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2005-01-06 12:50:03 +00:00
|
|
|
function set_user_preference($name, $value, $otheruser=NULL) {
|
2004-01-28 14:14:19 +00:00
|
|
|
|
|
|
|
global $USER;
|
|
|
|
|
2005-01-06 12:50:03 +00:00
|
|
|
if (empty($otheruser)){
|
|
|
|
if (!empty($USER) && !empty($USER->id)) {
|
2004-12-01 21:47:02 +00:00
|
|
|
$userid = $USER->id;
|
2005-01-06 12:50:03 +00:00
|
|
|
} else {
|
2004-12-01 21:47:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-01-06 12:50:03 +00:00
|
|
|
} else {
|
|
|
|
$userid = $otheruser;
|
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)) {
|
2005-01-06 12:50:03 +00:00
|
|
|
if (empty($otheruser) and !empty($USER)) {
|
2004-12-01 21:47:02 +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)) {
|
2005-01-06 12:50:03 +00:00
|
|
|
if (empty($otheruser) and !empty($USER)) {
|
2004-12-01 21:47:02 +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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 04:15:19 +00:00
|
|
|
*/
|
|
|
|
function unset_user_preference($name, $userid=NULL) {
|
|
|
|
|
|
|
|
global $USER;
|
|
|
|
|
2004-11-22 18:38:33 +00:00
|
|
|
if (empty($userid)){
|
2004-12-01 21:47:02 +00:00
|
|
|
if(!empty($USER) && !empty($USER->id)) {
|
|
|
|
$userid = $USER->id;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
2004-09-23 04:15:19 +00:00
|
|
|
}
|
|
|
|
|
2005-07-21 18:48:26 +00:00
|
|
|
//Delete the preference from $USER
|
|
|
|
if (isset($USER->preference[$name])) {
|
|
|
|
unset($USER->preference[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Then from DB
|
2004-09-23 04:15:19 +00:00
|
|
|
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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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-11-22 18:38:33 +00:00
|
|
|
if (empty($userid)){
|
2005-01-23 02:30:15 +00:00
|
|
|
if (!empty($USER) && !empty($USER->id)) {
|
|
|
|
$userid = NULL; // Continue with the current user below
|
|
|
|
} else {
|
|
|
|
return false; // No-one to set!
|
2004-12-01 21:47:02 +00:00
|
|
|
}
|
2004-09-23 03:56:53 +00:00
|
|
|
}
|
|
|
|
|
2004-01-28 14:14:19 +00:00
|
|
|
$return = true;
|
|
|
|
foreach ($prefarray as $name => $value) {
|
2004-12-01 21:47:02 +00:00
|
|
|
// 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
|
2004-11-22 18:38:33 +00:00
|
|
|
* all of the current user preferences as an array.
|
2004-09-23 02:48:41 +00:00
|
|
|
* 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
|
|
|
*
|
2005-07-12 03:09:25 +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 float $timezone ?
|
|
|
|
* @param bool $applydst ?
|
2005-02-21 08:55:40 +00:00
|
|
|
* @return int timestamp
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2005-01-12 11:17:18 +00:00
|
|
|
function make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0, $timezone=99, $applydst=true) {
|
2002-07-29 06:21:37 +00:00
|
|
|
|
2005-04-10 09:41:46 +00:00
|
|
|
$timezone = get_user_timezone_offset($timezone);
|
|
|
|
|
2003-01-13 02:42:57 +00:00
|
|
|
if (abs($timezone) > 13) {
|
2005-07-12 03:09:25 +00:00
|
|
|
$time = mktime((int)$hour, (int)$minute, (int)$second, (int)$month, (int)$day, (int)$year);
|
2003-01-13 02:37:47 +00:00
|
|
|
} else {
|
2005-07-12 03:09:25 +00:00
|
|
|
$time = gmmktime((int)$hour, (int)$minute, (int)$second, (int)$month, (int)$day, (int)$year);
|
2005-02-16 21:50:25 +00:00
|
|
|
$time = usertime($time, $timezone);
|
2005-05-01 16:50:35 +00:00
|
|
|
if($applydst) {
|
|
|
|
$time -= dst_offset_on($time);
|
|
|
|
}
|
2005-01-12 11:17:18 +00:00
|
|
|
}
|
|
|
|
|
2005-02-16 21:50:25 +00:00
|
|
|
return $time;
|
2005-02-17 21:58:45 +00:00
|
|
|
|
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-29 18:56:50 +00:00
|
|
|
* @uses MINSECS
|
|
|
|
* @uses HOURSECS
|
|
|
|
* @uses DAYSECS
|
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-11-22 18:38:33 +00:00
|
|
|
*
|
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-29 18:56:50 +00:00
|
|
|
* @uses HOURSECS
|
2005-02-21 08:55:40 +00:00
|
|
|
* @param int $date timestamp in GMT
|
|
|
|
* @param string $format strftime format
|
2005-02-25 06:08:40 +00:00
|
|
|
* @param float $timezone
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param bool $fixday If true (default) then the leading
|
2004-09-25 01:29:37 +00:00
|
|
|
* zero from %d is removed. If false then the leading zero is mantained.
|
|
|
|
* @return string
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2004-09-22 14:39:15 +00:00
|
|
|
function userdate($date, $format='', $timezone=99, $fixday = true) {
|
2002-07-02 07:02:28 +00:00
|
|
|
|
2005-03-10 14:10:21 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
static $strftimedaydatetime;
|
2005-02-19 02:49:31 +00:00
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
if ($format == '') {
|
2005-03-10 14:10:21 +00:00
|
|
|
if (empty($strftimedaydatetime)) {
|
|
|
|
$strftimedaydatetime = get_string('strftimedaydatetime');
|
|
|
|
}
|
|
|
|
$format = $strftimedaydatetime;
|
2002-06-05 14:05:59 +00:00
|
|
|
}
|
2002-12-02 01:35:18 +00:00
|
|
|
|
2005-05-12 06:37:24 +00:00
|
|
|
if (!empty($CFG->nofixday)) { // Config.php can force %d not to be fixed.
|
|
|
|
$fixday = false;
|
|
|
|
} else if ($fixday) {
|
|
|
|
$formatnoday = str_replace('%d', 'DD', $format);
|
2003-05-02 09:37:41 +00:00
|
|
|
$fixday = ($formatnoday != $format);
|
|
|
|
}
|
2003-01-15 10:55:54 +00:00
|
|
|
|
2005-02-18 13:44:14 +00:00
|
|
|
$date += dst_offset_on($date);
|
2005-02-17 22:43:18 +00:00
|
|
|
|
2005-04-08 07:42:50 +00:00
|
|
|
$timezone = get_user_timezone_offset($timezone);
|
2005-02-19 02:49:31 +00:00
|
|
|
|
|
|
|
if (abs($timezone) > 13) { /// Server time
|
2005-02-25 06:08:40 +00:00
|
|
|
if ($fixday) {
|
2005-02-19 02:49:31 +00:00
|
|
|
$datestring = strftime($formatnoday, $date);
|
|
|
|
$daystring = str_replace(' 0', '', strftime(' %d', $date));
|
|
|
|
$datestring = str_replace('DD', $daystring, $datestring);
|
|
|
|
} else {
|
|
|
|
$datestring = strftime($format, $date);
|
|
|
|
}
|
2005-02-18 13:44:14 +00:00
|
|
|
} else {
|
2005-02-19 02:49:31 +00:00
|
|
|
$date += (int)($timezone * 3600);
|
|
|
|
if ($fixday) {
|
|
|
|
$datestring = gmstrftime($formatnoday, $date);
|
|
|
|
$daystring = str_replace(' 0', '', gmstrftime(' %d', $date));
|
|
|
|
$datestring = str_replace('DD', $daystring, $datestring);
|
|
|
|
} else {
|
|
|
|
$datestring = gmstrftime($format, $date);
|
|
|
|
}
|
2005-02-18 13:44:14 +00:00
|
|
|
}
|
2005-02-19 02:49:31 +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
|
|
|
/**
|
2005-02-16 21:50:25 +00:00
|
|
|
* Given a $time timestamp in GMT (seconds since epoch),
|
2004-09-25 01:29:37 +00:00
|
|
|
* returns an array that represents the date in user time
|
2004-09-23 02:48:41 +00:00
|
|
|
*
|
2004-09-29 18:56:50 +00:00
|
|
|
* @uses HOURSECS
|
2005-02-16 21:50:25 +00:00
|
|
|
* @param int $time Timestamp in GMT
|
2005-07-12 03:09:25 +00:00
|
|
|
* @param float $timezone ?
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return array An array that represents the date in user time
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2005-02-16 21:50:25 +00:00
|
|
|
function usergetdate($time, $timezone=99) {
|
2002-08-04 16:20:30 +00:00
|
|
|
|
2005-04-08 07:42:50 +00:00
|
|
|
$timezone = get_user_timezone_offset($timezone);
|
2004-04-01 10:39:06 +00:00
|
|
|
|
2005-02-21 08:55:40 +00:00
|
|
|
if (abs($timezone) > 13) { // Server time
|
2005-02-22 06:52:42 +00:00
|
|
|
return getdate($time);
|
2005-02-25 06:08:40 +00:00
|
|
|
}
|
|
|
|
|
2005-02-21 08:55:40 +00:00
|
|
|
// There is no gmgetdate so we use gmdate instead
|
2005-02-16 22:55:13 +00:00
|
|
|
$time += dst_offset_on($time);
|
2005-02-21 08:55:40 +00:00
|
|
|
$time += intval((float)$timezone * HOURSECS);
|
2005-03-23 14:43:32 +00:00
|
|
|
|
|
|
|
$datestring = gmstrftime('%S_%M_%H_%d_%m_%Y_%w_%j_%A_%B', $time);
|
2005-02-16 22:55:13 +00:00
|
|
|
|
2005-01-12 11:17:18 +00:00
|
|
|
list(
|
|
|
|
$getdate['seconds'],
|
|
|
|
$getdate['minutes'],
|
|
|
|
$getdate['hours'],
|
|
|
|
$getdate['mday'],
|
|
|
|
$getdate['mon'],
|
|
|
|
$getdate['year'],
|
|
|
|
$getdate['wday'],
|
|
|
|
$getdate['yday'],
|
|
|
|
$getdate['weekday'],
|
|
|
|
$getdate['month']
|
2005-03-23 14:43:32 +00:00
|
|
|
) = explode('_', $datestring);
|
2005-01-12 11:17:18 +00:00
|
|
|
|
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-29 18:56:50 +00:00
|
|
|
* @uses HOURSECS
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $date Timestamp in GMT
|
2005-02-21 08:55:40 +00:00
|
|
|
* @param float $timezone
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return int
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2002-06-10 09:43:40 +00:00
|
|
|
function usertime($date, $timezone=99) {
|
2004-04-01 10:39:06 +00:00
|
|
|
|
2005-04-08 07:42:50 +00:00
|
|
|
$timezone = get_user_timezone_offset($timezone);
|
2005-04-08 05:39:53 +00:00
|
|
|
|
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.
|
|
|
|
*
|
2005-02-21 08:55:40 +00:00
|
|
|
* @param int $date Timestamp in GMT
|
|
|
|
* @param float $timezone ?
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return ?
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2002-06-10 14:01:30 +00:00
|
|
|
function usergetmidnight($date, $timezone=99) {
|
|
|
|
|
2005-04-08 07:42:50 +00:00
|
|
|
$timezone = get_user_timezone_offset($timezone);
|
2002-06-10 14:01:30 +00:00
|
|
|
$userdate = usergetdate($date, $timezone);
|
2002-08-08 17:28:30 +00:00
|
|
|
|
2005-02-16 22:55:13 +00:00
|
|
|
// Time of midnight of this user's day, in GMT
|
|
|
|
return make_timestamp($userdate['year'], $userdate['mon'], $userdate['mday'], 0, 0, 0, $timezone);
|
2002-06-10 14:01:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2002-06-10 09:43:40 +00:00
|
|
|
function usertimezone($timezone=99) {
|
|
|
|
|
2005-04-27 03:42:07 +00:00
|
|
|
$tz = get_user_timezone($timezone);
|
2004-04-01 12:27:37 +00:00
|
|
|
|
2005-04-27 03:42:07 +00:00
|
|
|
if (!is_float($tz)) {
|
|
|
|
return $tz;
|
2002-06-10 09:43:40 +00:00
|
|
|
}
|
2005-04-27 03:42:07 +00:00
|
|
|
|
|
|
|
if(abs($tz) > 13) { // Server time
|
|
|
|
return get_string('serverlocaltime');
|
|
|
|
}
|
|
|
|
|
|
|
|
if($tz == intval($tz)) {
|
|
|
|
// Don't show .0 for whole hours
|
|
|
|
$tz = intval($tz);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($tz == 0) {
|
2004-09-22 14:39:15 +00:00
|
|
|
return 'GMT';
|
2002-06-10 09:43:40 +00:00
|
|
|
}
|
2005-04-27 03:42:07 +00:00
|
|
|
else if($tz > 0) {
|
|
|
|
return 'GMT+'.$tz;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 'GMT'.$tz;
|
2002-06-10 09:43:40 +00:00
|
|
|
}
|
2005-04-27 03:42:07 +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
|
2005-02-21 08:55:40 +00:00
|
|
|
* @param float $tz The user's timezone
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return int
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2005-04-08 07:42:50 +00:00
|
|
|
function get_user_timezone_offset($tz = 99) {
|
2004-04-01 12:27:37 +00:00
|
|
|
|
2005-04-08 05:28:39 +00:00
|
|
|
global $USER, $CFG;
|
|
|
|
|
2005-04-08 20:32:40 +00:00
|
|
|
$tz = get_user_timezone($tz);
|
2005-04-10 09:31:21 +00:00
|
|
|
|
2005-04-09 08:44:49 +00:00
|
|
|
if (is_float($tz)) {
|
|
|
|
return $tz;
|
|
|
|
} else {
|
2005-04-08 20:32:40 +00:00
|
|
|
$tzrecord = get_timezone_record($tz);
|
2005-04-09 08:44:49 +00:00
|
|
|
if (empty($tzrecord)) {
|
2005-04-08 20:32:40 +00:00
|
|
|
return 99.0;
|
|
|
|
}
|
2005-04-14 12:20:09 +00:00
|
|
|
return (float)$tzrecord->gmtoff / HOURMINS;
|
2005-04-08 20:32:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-12 02:06:33 +00:00
|
|
|
/**
|
|
|
|
* ?
|
|
|
|
*
|
|
|
|
* @uses $USER
|
|
|
|
* @uses $CFG
|
|
|
|
* @param int $tz ?
|
|
|
|
* @return int
|
|
|
|
*/
|
2005-04-08 20:32:40 +00:00
|
|
|
function get_user_timezone($tz = 99) {
|
|
|
|
global $USER, $CFG;
|
2005-04-08 05:28:39 +00:00
|
|
|
|
2004-04-01 12:27:37 +00:00
|
|
|
$timezones = array(
|
2005-04-08 20:32:40 +00:00
|
|
|
$tz,
|
|
|
|
isset($CFG->forcetimezone) ? $CFG->forcetimezone : 99,
|
2005-04-08 05:28:39 +00:00
|
|
|
isset($USER->timezone) ? $USER->timezone : 99,
|
|
|
|
isset($CFG->timezone) ? $CFG->timezone : 99,
|
2004-04-01 12:27:37 +00:00
|
|
|
);
|
2005-04-08 05:28:39 +00:00
|
|
|
|
2005-04-08 20:32:40 +00:00
|
|
|
$tz = 99;
|
2005-04-08 05:28:39 +00:00
|
|
|
|
2005-04-08 20:32:40 +00:00
|
|
|
while(($tz == '' || $tz == 99) && $next = each($timezones)) {
|
|
|
|
$tz = $next['value'];
|
2005-04-08 05:28:39 +00:00
|
|
|
}
|
2005-04-08 20:32:40 +00:00
|
|
|
|
|
|
|
return is_numeric($tz) ? (float) $tz : $tz;
|
2005-04-08 05:28:39 +00:00
|
|
|
}
|
|
|
|
|
2005-07-12 02:06:33 +00:00
|
|
|
/**
|
|
|
|
* ?
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @uses $db
|
|
|
|
* @param string $timezonename ?
|
|
|
|
* @return object
|
|
|
|
*/
|
2005-04-08 05:28:39 +00:00
|
|
|
function get_timezone_record($timezonename) {
|
|
|
|
global $CFG, $db;
|
|
|
|
static $cache = NULL;
|
|
|
|
|
2005-04-10 18:27:15 +00:00
|
|
|
if ($cache === NULL) {
|
2005-04-08 05:28:39 +00:00
|
|
|
$cache = array();
|
|
|
|
}
|
|
|
|
|
2005-04-10 18:27:15 +00:00
|
|
|
if (isset($cache[$timezonename])) {
|
2005-04-08 05:28:39 +00:00
|
|
|
return $cache[$timezonename];
|
2004-04-01 12:27:37 +00:00
|
|
|
}
|
|
|
|
|
2005-04-11 12:07:49 +00:00
|
|
|
return get_record_sql('SELECT * FROM '.$CFG->prefix.'timezone
|
|
|
|
WHERE name = '.$db->qstr($timezonename).' ORDER BY year DESC', true);
|
2004-04-01 12:27:37 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2005-07-12 02:06:33 +00:00
|
|
|
/**
|
|
|
|
* ?
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @uses $USER
|
|
|
|
* @param ? $fromyear ?
|
|
|
|
* @param ? $to_year ?
|
|
|
|
* @return bool
|
|
|
|
*/
|
2005-03-26 18:43:58 +00:00
|
|
|
function calculate_user_dst_table($from_year = NULL, $to_year = NULL) {
|
2005-08-30 03:27:52 +00:00
|
|
|
global $CFG, $SESSION;
|
2005-02-17 21:58:45 +00:00
|
|
|
|
2005-04-09 10:12:30 +00:00
|
|
|
$usertz = get_user_timezone();
|
2005-02-19 03:14:06 +00:00
|
|
|
|
2005-04-09 10:12:30 +00:00
|
|
|
if (is_float($usertz)) {
|
|
|
|
// Trivial timezone, no DST
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-08-30 03:27:52 +00:00
|
|
|
if (!empty($SESSION->dst_offsettz) && $SESSION->dst_offsettz != $usertz) {
|
2005-04-09 10:12:30 +00:00
|
|
|
// We have precalculated values, but the user's effective TZ has changed in the meantime, so reset
|
2005-08-30 03:27:52 +00:00
|
|
|
unset($SESSION->dst_offsets);
|
|
|
|
unset($SESSION->dst_range);
|
2005-03-26 18:43:58 +00:00
|
|
|
}
|
|
|
|
|
2005-08-30 03:27:52 +00:00
|
|
|
if (!empty($SESSION->dst_offsets) && empty($from_year) && empty($to_year)) {
|
2005-03-26 18:43:58 +00:00
|
|
|
// Repeat calls which do not request specific year ranges stop here, we have already calculated the table
|
|
|
|
// This will be the return path most of the time, pretty light computationally
|
|
|
|
return true;
|
2005-02-17 21:58:45 +00:00
|
|
|
}
|
|
|
|
|
2005-03-26 18:43:58 +00:00
|
|
|
// Reaching here means we either need to extend our table or create it from scratch
|
2005-04-09 10:12:30 +00:00
|
|
|
|
|
|
|
// Remember which TZ we calculated these changes for
|
2005-08-30 03:27:52 +00:00
|
|
|
$SESSION->dst_offsettz = $usertz;
|
2005-04-09 10:12:30 +00:00
|
|
|
|
2005-08-30 03:27:52 +00:00
|
|
|
if(empty($SESSION->dst_offsets)) {
|
2005-03-26 18:43:58 +00:00
|
|
|
// If we 're creating from scratch, put the two guard elements in there
|
2005-08-30 03:27:52 +00:00
|
|
|
$SESSION->dst_offsets = array(1 => NULL, 0 => NULL);
|
2005-03-26 18:43:58 +00:00
|
|
|
}
|
2005-08-30 03:27:52 +00:00
|
|
|
if(empty($SESSION->dst_range)) {
|
2005-03-26 18:43:58 +00:00
|
|
|
// If creating from scratch
|
|
|
|
$from = max((empty($from_year) ? intval(date('Y')) - 3 : $from_year), 1971);
|
|
|
|
$to = min((empty($to_year) ? intval(date('Y')) + 3 : $to_year), 2035);
|
|
|
|
|
|
|
|
// Fill in the array with the extra years we need to process
|
|
|
|
$yearstoprocess = array();
|
|
|
|
for($i = $from; $i <= $to; ++$i) {
|
|
|
|
$yearstoprocess[] = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take note of which years we have processed for future calls
|
2005-08-30 03:27:52 +00:00
|
|
|
$SESSION->dst_range = array($from, $to);
|
2005-03-26 18:43:58 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// If needing to extend the table, do the same
|
|
|
|
$yearstoprocess = array();
|
|
|
|
|
2005-08-30 03:27:52 +00:00
|
|
|
$from = max((empty($from_year) ? $SESSION->dst_range[0] : $from_year), 1971);
|
|
|
|
$to = min((empty($to_year) ? $SESSION->dst_range[1] : $to_year), 2035);
|
2005-03-26 18:43:58 +00:00
|
|
|
|
2005-08-30 03:27:52 +00:00
|
|
|
if($from < $SESSION->dst_range[0]) {
|
2005-03-26 18:43:58 +00:00
|
|
|
// Take note of which years we need to process and then note that we have processed them for future calls
|
2005-08-30 03:27:52 +00:00
|
|
|
for($i = $from; $i < $SESSION->dst_range[0]; ++$i) {
|
2005-03-26 18:43:58 +00:00
|
|
|
$yearstoprocess[] = $i;
|
|
|
|
}
|
2005-08-30 03:27:52 +00:00
|
|
|
$SESSION->dst_range[0] = $from;
|
2005-03-26 18:43:58 +00:00
|
|
|
}
|
2005-08-30 03:27:52 +00:00
|
|
|
if($to > $SESSION->dst_range[1]) {
|
2005-03-26 18:43:58 +00:00
|
|
|
// Take note of which years we need to process and then note that we have processed them for future calls
|
2005-08-30 03:27:52 +00:00
|
|
|
for($i = $SESSION->dst_range[1] + 1; $i <= $to; ++$i) {
|
2005-03-26 18:43:58 +00:00
|
|
|
$yearstoprocess[] = $i;
|
|
|
|
}
|
2005-08-30 03:27:52 +00:00
|
|
|
$SESSION->dst_range[1] = $to;
|
2005-03-26 18:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(empty($yearstoprocess)) {
|
|
|
|
// This means that there was a call requesting a SMALLER range than we have already calculated
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// From now on, we know that the array has at least the two guard elements, and $yearstoprocess has the years we need
|
|
|
|
// Also, the array is sorted in descending timestamp order!
|
|
|
|
|
|
|
|
// Get DB data
|
2005-04-09 10:12:30 +00:00
|
|
|
$presetrecords = get_records('timezone', 'name', $usertz, 'year DESC', 'year, gmtoff, dstoff, dst_month, dst_startday, dst_weekday, dst_skipweeks, dst_time, std_month, std_startday, std_weekday, std_skipweeks, std_time');
|
2005-03-23 03:03:43 +00:00
|
|
|
if(empty($presetrecords)) {
|
|
|
|
return false;
|
|
|
|
}
|
2005-02-18 01:18:00 +00:00
|
|
|
|
2005-03-26 18:43:58 +00:00
|
|
|
// Remove ending guard (first element of the array)
|
2005-08-30 03:27:52 +00:00
|
|
|
reset($SESSION->dst_offsets);
|
|
|
|
unset($SESSION->dst_offsets[key($SESSION->dst_offsets)]);
|
2005-03-26 18:43:58 +00:00
|
|
|
|
|
|
|
// Add all required change timestamps
|
|
|
|
foreach($yearstoprocess as $y) {
|
|
|
|
// Find the record which is in effect for the year $y
|
|
|
|
foreach($presetrecords as $year => $preset) {
|
|
|
|
if($year <= $y) {
|
|
|
|
break;
|
2005-03-05 22:23:08 +00:00
|
|
|
}
|
2005-03-26 18:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$changes = dst_changes_for_year($y, $preset);
|
|
|
|
|
|
|
|
if($changes === NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if($changes['dst'] != 0) {
|
2005-08-30 03:27:52 +00:00
|
|
|
$SESSION->dst_offsets[$changes['dst']] = $preset->dstoff * MINSECS;
|
2005-03-26 18:43:58 +00:00
|
|
|
}
|
|
|
|
if($changes['std'] != 0) {
|
2005-08-30 03:27:52 +00:00
|
|
|
$SESSION->dst_offsets[$changes['std']] = 0;
|
2005-03-05 22:23:08 +00:00
|
|
|
}
|
2005-02-17 21:58:45 +00:00
|
|
|
}
|
2005-02-16 22:04:14 +00:00
|
|
|
|
2005-03-26 18:43:58 +00:00
|
|
|
// Put in a guard element at the top
|
2005-08-30 03:27:52 +00:00
|
|
|
$maxtimestamp = max(array_keys($SESSION->dst_offsets));
|
|
|
|
$SESSION->dst_offsets[($maxtimestamp + DAYSECS)] = NULL; // DAYSECS is arbitrary, any "small" number will do
|
2005-03-26 18:43:58 +00:00
|
|
|
|
|
|
|
// Sort again
|
2005-08-30 03:27:52 +00:00
|
|
|
krsort($SESSION->dst_offsets);
|
2005-03-26 18:43:58 +00:00
|
|
|
|
2005-03-23 03:03:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
2005-02-16 22:04:14 +00:00
|
|
|
|
2005-03-23 03:03:43 +00:00
|
|
|
function dst_changes_for_year($year, $timezone) {
|
2005-02-19 03:14:06 +00:00
|
|
|
|
2005-03-23 03:03:43 +00:00
|
|
|
if($timezone->dst_startday == 0 && $timezone->dst_weekday == 0 && $timezone->std_startday == 0 && $timezone->std_weekday == 0) {
|
|
|
|
return NULL;
|
2005-02-16 22:04:14 +00:00
|
|
|
}
|
2005-02-19 03:14:06 +00:00
|
|
|
|
2005-03-23 03:03:43 +00:00
|
|
|
$monthdaydst = find_day_in_month($timezone->dst_startday, $timezone->dst_weekday, $timezone->dst_month, $year);
|
|
|
|
$monthdaystd = find_day_in_month($timezone->std_startday, $timezone->std_weekday, $timezone->std_month, $year);
|
|
|
|
|
|
|
|
list($dst_hour, $dst_min) = explode(':', $timezone->dst_time);
|
|
|
|
list($std_hour, $std_min) = explode(':', $timezone->std_time);
|
2005-02-25 06:08:40 +00:00
|
|
|
|
2005-04-09 09:50:08 +00:00
|
|
|
$timedst = make_timestamp($year, $timezone->dst_month, $monthdaydst, 0, 0, 0, 99, false);
|
|
|
|
$timestd = make_timestamp($year, $timezone->std_month, $monthdaystd, 0, 0, 0, 99, false);
|
2005-03-26 18:43:58 +00:00
|
|
|
|
|
|
|
// Instead of putting hour and minute in make_timestamp(), we add them afterwards.
|
|
|
|
// This has the advantage of being able to have negative values for hour, i.e. for timezones
|
|
|
|
// where GMT time would be in the PREVIOUS day than the local one on which DST changes.
|
|
|
|
|
|
|
|
$timedst += $dst_hour * HOURSECS + $dst_min * MINSECS;
|
|
|
|
$timestd += $std_hour * HOURSECS + $std_min * MINSECS;
|
2005-02-16 22:04:14 +00:00
|
|
|
|
2005-03-23 03:03:43 +00:00
|
|
|
return array('dst' => $timedst, 0 => $timedst, 'std' => $timestd, 1 => $timestd);
|
2005-02-16 22:04:14 +00:00
|
|
|
}
|
|
|
|
|
2005-02-16 22:55:13 +00:00
|
|
|
// $time must NOT be compensated at all, it has to be a pure timestamp
|
|
|
|
function dst_offset_on($time) {
|
2005-08-30 03:27:52 +00:00
|
|
|
global $SESSION;
|
2005-02-16 22:55:13 +00:00
|
|
|
|
2005-08-30 03:27:52 +00:00
|
|
|
if(!calculate_user_dst_table() || empty($SESSION->dst_offsets)) {
|
2005-03-05 22:23:08 +00:00
|
|
|
return 0;
|
2005-02-17 21:58:45 +00:00
|
|
|
}
|
|
|
|
|
2005-08-30 03:27:52 +00:00
|
|
|
reset($SESSION->dst_offsets);
|
|
|
|
while(list($from, $offset) = each($SESSION->dst_offsets)) {
|
2005-03-05 23:57:19 +00:00
|
|
|
if($from <= $time) {
|
2005-03-05 22:23:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-26 18:43:58 +00:00
|
|
|
// This is the normal return path
|
|
|
|
if($offset !== NULL) {
|
|
|
|
return $offset;
|
2005-02-16 22:55:13 +00:00
|
|
|
}
|
|
|
|
|
2005-03-26 18:43:58 +00:00
|
|
|
// Reaching this point means we haven't calculated far enough, do it now:
|
|
|
|
// Calculate extra DST changes if needed and recurse. The recursion always
|
|
|
|
// moves toward the stopping condition, so will always end.
|
|
|
|
|
|
|
|
if($from == 0) {
|
2005-08-30 03:27:52 +00:00
|
|
|
// We need a year smaller than $SESSION->dst_range[0]
|
|
|
|
if($SESSION->dst_range[0] == 1971) {
|
2005-03-26 18:43:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-08-30 03:27:52 +00:00
|
|
|
calculate_user_dst_table($SESSION->dst_range[0] - 5, NULL);
|
2005-03-26 18:43:58 +00:00
|
|
|
return dst_offset_on($time);
|
|
|
|
}
|
|
|
|
else {
|
2005-08-30 03:27:52 +00:00
|
|
|
// We need a year larger than $SESSION->dst_range[1]
|
|
|
|
if($SESSION->dst_range[1] == 2035) {
|
2005-03-26 18:43:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-08-30 03:27:52 +00:00
|
|
|
calculate_user_dst_table(NULL, $SESSION->dst_range[1] + 5);
|
2005-03-26 18:43:58 +00:00
|
|
|
return dst_offset_on($time);
|
|
|
|
}
|
2005-02-17 21:58:45 +00:00
|
|
|
}
|
2005-02-16 22:55:13 +00:00
|
|
|
|
2005-03-18 04:38:30 +00:00
|
|
|
function find_day_in_month($startday, $weekday, $month, $year) {
|
2005-02-26 06:26:04 +00:00
|
|
|
|
|
|
|
$daysinmonth = days_in_month($month, $year);
|
|
|
|
|
2005-02-16 22:04:14 +00:00
|
|
|
if($weekday == -1) {
|
2005-03-18 04:38:30 +00:00
|
|
|
// Don't care about weekday, so return:
|
|
|
|
// abs($startday) if $startday != -1
|
|
|
|
// $daysinmonth otherwise
|
|
|
|
return ($startday == -1) ? $daysinmonth : abs($startday);
|
2005-02-26 06:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// From now on we 're looking for a specific weekday
|
|
|
|
|
2005-03-18 04:38:30 +00:00
|
|
|
// Give "end of month" its actual value, since we know it
|
|
|
|
if($startday == -1) {
|
|
|
|
$startday = -1 * $daysinmonth;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starting from day $startday, the sign is the direction
|
2005-02-26 06:26:04 +00:00
|
|
|
|
2005-03-18 04:38:30 +00:00
|
|
|
if($startday < 1) {
|
2005-02-26 06:26:04 +00:00
|
|
|
|
2005-03-18 04:38:30 +00:00
|
|
|
$startday = abs($startday);
|
2005-02-26 06:26:04 +00:00
|
|
|
$lastmonthweekday = strftime('%w', mktime(12, 0, 0, $month, $daysinmonth, $year, 0));
|
|
|
|
|
|
|
|
// This is the last such weekday of the month
|
|
|
|
$lastinmonth = $daysinmonth + $weekday - $lastmonthweekday;
|
|
|
|
if($lastinmonth > $daysinmonth) {
|
|
|
|
$lastinmonth -= 7;
|
2005-02-16 22:04:14 +00:00
|
|
|
}
|
2005-02-26 06:26:04 +00:00
|
|
|
|
2005-03-18 04:38:30 +00:00
|
|
|
// Find the first such weekday <= $startday
|
|
|
|
while($lastinmonth > $startday) {
|
2005-02-26 06:26:04 +00:00
|
|
|
$lastinmonth -= 7;
|
2005-02-16 22:04:14 +00:00
|
|
|
}
|
2005-02-26 06:26:04 +00:00
|
|
|
|
|
|
|
return $lastinmonth;
|
|
|
|
|
2005-02-16 22:04:14 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2005-03-18 04:38:30 +00:00
|
|
|
$indexweekday = strftime('%w', mktime(12, 0, 0, $month, $startday, $year, 0));
|
2005-02-16 22:04:14 +00:00
|
|
|
|
2005-02-26 06:26:04 +00:00
|
|
|
$diff = $weekday - $indexweekday;
|
|
|
|
if($diff < 0) {
|
|
|
|
$diff += 7;
|
2005-02-16 22:04:14 +00:00
|
|
|
}
|
|
|
|
|
2005-03-18 04:38:30 +00:00
|
|
|
// This is the first such weekday of the month equal to or after $startday
|
|
|
|
$firstfromindex = $startday + $diff;
|
2005-02-16 22:04:14 +00:00
|
|
|
|
2005-02-26 06:26:04 +00:00
|
|
|
return $firstfromindex;
|
|
|
|
|
|
|
|
}
|
2005-02-16 22:04:14 +00:00
|
|
|
}
|
|
|
|
|
2005-07-12 02:06:33 +00:00
|
|
|
/**
|
|
|
|
* Calculate the number of days in a given month
|
|
|
|
*
|
|
|
|
* @param int $month The month whose day count is sought
|
|
|
|
* @param int $year The year of the month whose day count is sought
|
|
|
|
* @return int
|
|
|
|
*/
|
2005-02-16 22:04:14 +00:00
|
|
|
function days_in_month($month, $year) {
|
|
|
|
return intval(date('t', mktime(12, 0, 0, $month, 1, $year, 0)));
|
|
|
|
}
|
|
|
|
|
2005-07-12 02:06:33 +00:00
|
|
|
/**
|
|
|
|
* Calculate the position in the week of a specific calendar day
|
|
|
|
*
|
|
|
|
* @param int $day The day of the date whose position in the week is sought
|
|
|
|
* @param int $month The month of the date whose position in the week is sought
|
|
|
|
* @param int $year The year of the date whose position in the week is sought
|
|
|
|
* @return int
|
|
|
|
*/
|
2005-02-26 06:26:04 +00:00
|
|
|
function dayofweek($day, $month, $year) {
|
|
|
|
// I wonder if this is any different from
|
|
|
|
// strftime('%w', mktime(12, 0, 0, $month, $daysinmonth, $year, 0));
|
|
|
|
return intval(date('w', mktime(12, 0, 0, $month, $day, $year, 0)));
|
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
/// USER AUTHENTICATION AND LOGIN ////////////////////////////////////////
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2005-07-12 02:06:33 +00:00
|
|
|
/**
|
|
|
|
* Makes sure that $USER->sesskey exists, if $USER itself exists. It sets a new sesskey
|
|
|
|
* if one does not already exist, but does not overwrite existing sesskeys. Returns the
|
|
|
|
* sesskey string if $USER exists, or boolean false if not.
|
|
|
|
*
|
|
|
|
* @uses $USER
|
|
|
|
* @return string
|
|
|
|
*/
|
2005-01-23 22:07:13 +00:00
|
|
|
function sesskey() {
|
2005-01-12 11:40:46 +00:00
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if(!isset($USER)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($USER->sesskey)) {
|
|
|
|
$USER->sesskey = random_string(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $USER->sesskey;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
2005-02-16 10:40:48 +00:00
|
|
|
* This function checks that the current user is logged in and has the
|
|
|
|
* required privileges
|
|
|
|
*
|
2004-09-23 02:48:41 +00:00
|
|
|
* This function checks that the current user is logged in, and optionally
|
2005-02-16 10:40:48 +00:00
|
|
|
* whether they are allowed to be in a particular course and view a particular
|
|
|
|
* course module.
|
|
|
|
* If they are not logged in, then it redirects them to the site login unless
|
2005-02-25 06:08:40 +00:00
|
|
|
* $autologinguest is set and {@link $CFG}->autologinguests is set to 1 in which
|
2005-02-16 10:40:48 +00:00
|
|
|
* case they are automatically logged in as guests.
|
|
|
|
* If $courseid is given and the user is not enrolled in that course then the
|
|
|
|
* user is redirected to the course enrolment page.
|
|
|
|
* If $cm is given and the coursemodule is hidden and the user is not a teacher
|
|
|
|
* in the course then the user is redirected to the course home page.
|
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
|
2005-02-16 10:40:48 +00:00
|
|
|
* @param int $courseid id of the course
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param bool $autologinguest
|
|
|
|
* @param object $cm course module object
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2005-02-16 10:40:48 +00:00
|
|
|
function require_login($courseid=0, $autologinguest=true, $cm=null) {
|
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
|
|
|
}
|
2005-02-17 06:15:22 +00:00
|
|
|
exit;
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
2002-12-30 15:30:05 +00:00
|
|
|
|
2004-09-20 09:08:57 +00:00
|
|
|
// check whether the user should be changing password
|
2004-11-29 05:30:36 +00:00
|
|
|
// reload_user_preferences(); // Why is this necessary? Seems wasteful. - MD
|
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'}){
|
2005-02-17 06:15:22 +00:00
|
|
|
$SESSION->wantsurl = $FULLME;
|
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-11-22 18:38:33 +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)) {
|
2005-02-17 06:15:22 +00:00
|
|
|
$SESSION->wantsurl = $FULLME;
|
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
|
|
|
}
|
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)
|
2004-11-22 18:38:33 +00:00
|
|
|
if (!empty($CFG->tracksessionip)) {
|
2004-09-27 14:35:37 +00:00
|
|
|
if ($USER->sessionIP != md5(getremoteaddr())) {
|
|
|
|
error(get_string('sessionipnomatch', 'error'));
|
|
|
|
}
|
|
|
|
}
|
2004-10-08 06:06:03 +00:00
|
|
|
|
|
|
|
// Make sure the USER has a sesskey set up. Used for checking script parameters.
|
2005-01-23 22:07:13 +00:00
|
|
|
sesskey();
|
2004-09-27 14:35:37 +00:00
|
|
|
|
2004-11-29 05:30:36 +00:00
|
|
|
// Check that the user has agreed to a site policy if there is one
|
|
|
|
if (!empty($CFG->sitepolicy)) {
|
|
|
|
if (!$USER->policyagreed) {
|
2004-11-29 06:22:12 +00:00
|
|
|
$SESSION->wantsurl = $FULLME;
|
2004-11-29 05:30:36 +00:00
|
|
|
redirect($CFG->wwwroot .'/user/policy.php');
|
|
|
|
}
|
2005-02-10 05:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the site is currently under maintenance, then print a message
|
|
|
|
if (!isadmin()) {
|
2005-05-11 08:57:14 +00:00
|
|
|
if (file_exists($CFG->dataroot.'/'.SITEID.'/maintenance.html')) {
|
2005-02-10 05:11:34 +00:00
|
|
|
print_maintenance_message();
|
2005-02-17 06:15:22 +00:00
|
|
|
exit;
|
2005-02-10 05:11:34 +00:00
|
|
|
}
|
2004-11-29 05:30:36 +00:00
|
|
|
}
|
|
|
|
|
2002-06-09 14:14:07 +00:00
|
|
|
// Next, check if the user can be in a particular course
|
|
|
|
if ($courseid) {
|
2005-02-16 10:40:48 +00:00
|
|
|
if ($courseid == SITEID) { // Anyone can be in the site course
|
|
|
|
if (isset($cm) and !$cm->visible and !isteacher(SITEID)) { // Not allowed to see module, send to course page
|
|
|
|
redirect($CFG->wwwroot.'/course/view.php?id='.$cm->course, get_string('activityiscurrentlyhidden'));
|
|
|
|
}
|
2005-02-25 06:08:40 +00:00
|
|
|
return;
|
2004-09-25 15:23:10 +00:00
|
|
|
}
|
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
|
|
|
}
|
2005-02-16 10:40:48 +00:00
|
|
|
if (isset($cm) and !$cm->visible and !isteacher($courseid)) { // Not allowed to see module, send to course page
|
|
|
|
redirect($CFG->wwwroot.'/course/view.php?id='.$cm->course, get_string('activityiscurrentlyhidden'));
|
|
|
|
}
|
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();
|
2005-02-13 11:43:05 +00:00
|
|
|
notice(get_string('coursehidden'), $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();
|
2005-02-14 00:03:21 +00:00
|
|
|
notice(get_string('guestsnotallowed', '', $course->fullname), "$CFG->wwwroot/login/index.php");
|
2002-08-21 12:54:27 +00:00
|
|
|
break;
|
|
|
|
case 1: // Guests allowed
|
2005-02-16 10:40:48 +00:00
|
|
|
if (isset($cm) and !$cm->visible) { // Not allowed to see module, send to course page
|
|
|
|
redirect($CFG->wwwroot.'/course/view.php?id='.$cm->course, get_string('activityiscurrentlyhidden'));
|
|
|
|
}
|
2002-08-21 12:54:27 +00:00
|
|
|
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
|
|
|
|
2005-02-05 16:40:38 +00:00
|
|
|
//User is not enrolled in the course, wants to access course content
|
|
|
|
//as a guest, and course setting allow unlimited guest access
|
|
|
|
//Code cribbed from course/loginas.php
|
|
|
|
if (strstr($FULLME,"username=guest") && ($course->guest==1)) {
|
2005-02-13 17:38:40 +00:00
|
|
|
$realuser = $USER->id;
|
|
|
|
$realname = fullname($USER, true);
|
|
|
|
$USER = guest_user();
|
|
|
|
$USER->loggedin = true;
|
|
|
|
$USER->site = $CFG->wwwroot;
|
|
|
|
$USER->realuser = $realuser;
|
2005-02-19 21:02:33 +00:00
|
|
|
$USER->sessionIP = md5(getremoteaddr()); // Store the current IP in the session
|
|
|
|
if (isset($SESSION->currentgroup)) { // Remember current cache setting for later
|
|
|
|
$SESSION->oldcurrentgroup = $SESSION->currentgroup;
|
|
|
|
unset($SESSION->currentgroup);
|
2005-02-13 17:38:40 +00:00
|
|
|
}
|
|
|
|
$guest_name = fullname($USER, true);
|
|
|
|
add_to_log($course->id, "course", "loginas", "../user/view.php?id=$course->id&$USER->id$", "$realname -> $guest_name");
|
2005-02-16 10:40:48 +00:00
|
|
|
if (isset($cm) and !$cm->visible) { // Not allowed to see module, send to course page
|
|
|
|
redirect($CFG->wwwroot.'/course/view.php?id='.$cm->course, get_string('activityiscurrentlyhidden'));
|
|
|
|
}
|
2005-02-13 17:38:40 +00:00
|
|
|
return;
|
2005-02-05 16:40:38 +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
|
2005-04-14 02:13:47 +00:00
|
|
|
* @param object $course The course object in question
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param bool $autologinguest Allow autologin guests if that is wanted
|
2005-04-14 02:13:47 +00:00
|
|
|
* @param object $cm Course activity module if known
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2005-02-16 10:40:48 +00:00
|
|
|
function require_course_login($course, $autologinguest=true, $cm=null) {
|
2004-08-22 14:38:47 +00:00
|
|
|
global $CFG;
|
2005-04-13 06:59:13 +00:00
|
|
|
if (!empty($CFG->forcelogin)) {
|
2005-02-13 17:38:40 +00:00
|
|
|
require_login();
|
2004-08-22 14:38:47 +00:00
|
|
|
}
|
2005-04-14 02:13:47 +00:00
|
|
|
if ($course->id != SITEID) {
|
2005-02-16 10:40:48 +00:00
|
|
|
require_login($course->id, $autologinguest, $cm);
|
2004-08-22 14:38:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2002-12-30 15:30:05 +00:00
|
|
|
function user_not_fully_set_up($user) {
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
return ($user->username != 'guest' and (empty($user->firstname) or empty($user->lastname) or empty($user->email) or over_bounce_threshold($user)));
|
|
|
|
}
|
|
|
|
|
|
|
|
function over_bounce_threshold($user) {
|
2005-02-25 06:08:40 +00:00
|
|
|
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
global $CFG;
|
2005-02-25 06:08:40 +00:00
|
|
|
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
if (empty($CFG->handlebounces)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// set sensible defaults
|
|
|
|
if (empty($CFG->minbounces)) {
|
|
|
|
$CFG->minbounces = 10;
|
|
|
|
}
|
|
|
|
if (empty($CFG->bounceratio)) {
|
|
|
|
$CFG->bounceratio = .20;
|
|
|
|
}
|
|
|
|
$bouncecount = 0;
|
|
|
|
$sendcount = 0;
|
|
|
|
if ($bounce = get_record('user_preferences','userid',$user->id,'name','email_bounce_count')) {
|
|
|
|
$bouncecount = $bounce->value;
|
|
|
|
}
|
|
|
|
if ($send = get_record('user_preferences','userid',$user->id,'name','email_send_count')) {
|
|
|
|
$sendcount = $send->value;
|
|
|
|
}
|
|
|
|
return ($bouncecount >= $CFG->minbounces && $bouncecount/$sendcount >= $CFG->bounceratio);
|
|
|
|
}
|
|
|
|
|
2005-02-25 06:08:40 +00:00
|
|
|
/**
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
* @param $user - object containing an id
|
|
|
|
* @param $reset - will reset the count to 0
|
|
|
|
*/
|
|
|
|
function set_send_count($user,$reset=false) {
|
2005-02-25 06:08:40 +00:00
|
|
|
if ($pref = get_record('user_preferences','userid',$user->id,'name','email_send_count')) {
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
$pref->value = (!empty($reset)) ? 0 : $pref->value+1;
|
|
|
|
update_record('user_preferences',$pref);
|
|
|
|
}
|
|
|
|
else if (!empty($reset)) { // if it's not there and we're resetting, don't bother.
|
|
|
|
// make a new one
|
|
|
|
$pref->name = 'email_send_count';
|
|
|
|
$pref->value = 1;
|
|
|
|
$pref->userid = $user->id;
|
2005-08-16 00:43:56 +00:00
|
|
|
insert_record('user_preferences',$pref, false);
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-25 06:08:40 +00:00
|
|
|
/**
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
* @param $user - object containing an id
|
|
|
|
* @param $reset - will reset the count to 0
|
|
|
|
*/
|
|
|
|
function set_bounce_count($user,$reset=false) {
|
2005-02-25 06:08:40 +00:00
|
|
|
if ($pref = get_record('user_preferences','userid',$user->id,'name','email_bounce_count')) {
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
$pref->value = (!empty($reset)) ? 0 : $pref->value+1;
|
|
|
|
update_record('user_preferences',$pref);
|
|
|
|
}
|
|
|
|
else if (!empty($reset)) { // if it's not there and we're resetting, don't bother.
|
|
|
|
// make a new one
|
|
|
|
$pref->name = 'email_bounce_count';
|
|
|
|
$pref->value = 1;
|
|
|
|
$pref->userid = $user->id;
|
2005-08-16 00:43:56 +00:00
|
|
|
insert_record('user_preferences',$pref, false);
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-24 22:21:28 +00:00
|
|
|
function sync_metacourses() {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2005-01-25 04:33:02 +00:00
|
|
|
if (!$courses = get_records_sql("SELECT DISTINCT parent_course,1 FROM {$CFG->prefix}course_meta")) {
|
2005-01-24 22:21:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-02-25 06:08:40 +00:00
|
|
|
|
2005-01-24 22:21:28 +00:00
|
|
|
foreach ($courses as $course) {
|
|
|
|
sync_metacourse($course->parent_course);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Goes through all enrolment records for the courses inside the metacourse and sync with them.
|
2005-02-25 06:08:40 +00:00
|
|
|
*/
|
2005-01-24 22:21:28 +00:00
|
|
|
|
|
|
|
function sync_metacourse($metacourseid) {
|
|
|
|
|
2005-08-23 05:43:02 +00:00
|
|
|
global $CFG;
|
2005-01-24 22:21:28 +00:00
|
|
|
|
|
|
|
if (!$metacourse = get_record("course","id",$metacourseid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-08-23 05:43:02 +00:00
|
|
|
if (count_records('course_meta','parent_course',$metacourseid) == 0) {
|
|
|
|
// if there are no child courses for this meta course, nuke the enrolments
|
2005-01-24 22:21:28 +00:00
|
|
|
if ($enrolments = get_records('user_students','course',$metacourseid,'','userid,1')) {
|
|
|
|
foreach ($enrolments as $enrolment) {
|
|
|
|
unenrol_student($enrolment->userid,$metacourseid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-08-23 05:43:02 +00:00
|
|
|
// first get the list of child courses
|
|
|
|
$c_courses = get_records('course_meta','parent_course',$metacourseid);
|
|
|
|
$instr = '';
|
|
|
|
foreach ($c_courses as $c) {
|
|
|
|
$instr .= $c->child_course.',';
|
|
|
|
}
|
|
|
|
$instr = substr($instr,0,-1);
|
2005-02-18 02:42:33 +00:00
|
|
|
|
2005-08-23 05:43:02 +00:00
|
|
|
// now get the list of valid enrolments in the child courses
|
|
|
|
$sql = 'SELECT DISTINCT userid,1 FROM '.$CFG->prefix.'user_students WHERE course IN ('.$instr.')';
|
|
|
|
$enrolments = get_records_sql($sql);
|
|
|
|
|
|
|
|
// put it into a nice array we can happily use array_diff on.
|
|
|
|
$ce = array();
|
|
|
|
if (!empty($enrolments)) {
|
|
|
|
foreach ($enrolments as $en) {
|
|
|
|
$ce[] = $en->userid;
|
2005-01-24 22:21:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-23 05:43:02 +00:00
|
|
|
// now get the list of current enrolments in the meta course.
|
|
|
|
$sql = 'SELECT userid,1 FROM '.$CFG->prefix.'user_students WHERE course = '.$metacourseid;
|
|
|
|
$enrolments = get_records_sql($sql);
|
2005-01-24 22:21:28 +00:00
|
|
|
|
2005-08-23 05:43:02 +00:00
|
|
|
$me = array();
|
|
|
|
if (!empty($enrolments)) {
|
|
|
|
foreach ($enrolments as $en) {
|
|
|
|
$me[] = $en->userid;
|
2005-01-24 22:21:28 +00:00
|
|
|
}
|
|
|
|
}
|
2005-02-25 06:08:40 +00:00
|
|
|
|
2005-08-23 05:43:02 +00:00
|
|
|
$enrolmentstodelete = array_diff($me,$ce);
|
|
|
|
$userstoadd = array_diff($ce,$me);
|
|
|
|
|
|
|
|
foreach ($enrolmentstodelete as $userid) {
|
|
|
|
unenrol_student($userid,$metacourseid);
|
|
|
|
}
|
|
|
|
foreach ($userstoadd as $userid) {
|
|
|
|
enrol_student($userid,$metacourseid,0,0,'metacourse');
|
|
|
|
}
|
|
|
|
|
2005-01-24 22:21:28 +00:00
|
|
|
// and next make sure that we have the right start time and end time (ie max and min) for them all.
|
|
|
|
if ($enrolments = get_records('user_students','course',$metacourseid,'','id,userid')) {
|
|
|
|
foreach ($enrolments as $enrol) {
|
|
|
|
if ($maxmin = get_record_sql("SELECT min(timestart) AS timestart, max(timeend) AS timeend
|
2005-08-23 05:43:02 +00:00
|
|
|
FROM {$CFG->prefix}user_students u,
|
|
|
|
{$CFG->prefix}course_meta mc
|
|
|
|
WHERE u.course = mc.child_course
|
|
|
|
AND userid = $enrol->userid
|
2005-01-24 22:21:28 +00:00
|
|
|
AND mc.parent_course = $metacourseid")) {
|
|
|
|
$enrol->timestart = $maxmin->timestart;
|
|
|
|
$enrol->timeend = $maxmin->timeend;
|
2005-08-23 05:43:02 +00:00
|
|
|
$enrol->enrol = 'metacourse'; // just in case it wasn't there earlier.
|
2005-01-24 22:21:28 +00:00
|
|
|
update_record('user_students',$enrol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2005-08-23 05:43:02 +00:00
|
|
|
|
2005-01-24 22:21:28 +00:00
|
|
|
}
|
|
|
|
|
2005-02-25 06:08:40 +00:00
|
|
|
/**
|
2005-01-24 22:21:28 +00:00
|
|
|
* Adds a record to the metacourse table and calls sync_metacoures
|
|
|
|
*/
|
|
|
|
function add_to_metacourse ($metacourseid, $courseid) {
|
2005-02-25 06:08:40 +00:00
|
|
|
|
2005-01-24 22:21:28 +00:00
|
|
|
if (!$metacourse = get_record("course","id",$metacourseid)) {
|
|
|
|
return false;
|
|
|
|
}
|
2005-02-25 06:08:40 +00:00
|
|
|
|
2005-01-24 22:21:28 +00:00
|
|
|
if (!$course = get_record("course","id",$courseid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-01-25 04:33:02 +00:00
|
|
|
if (!$record = get_record("course_meta","parent_course",$metacourseid,"child_course",$courseid)) {
|
2005-01-24 22:21:28 +00:00
|
|
|
$rec->parent_course = $metacourseid;
|
|
|
|
$rec->child_course = $courseid;
|
2005-01-25 04:33:02 +00:00
|
|
|
if (!insert_record('course_meta',$rec)) {
|
2005-01-24 22:21:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return sync_metacourse($metacourseid);
|
|
|
|
}
|
|
|
|
return true;
|
2005-02-25 06:08:40 +00:00
|
|
|
|
2005-01-24 22:21:28 +00:00
|
|
|
}
|
|
|
|
|
2005-02-25 06:08:40 +00:00
|
|
|
/**
|
2005-01-24 22:21:28 +00:00
|
|
|
* Removes the record from the metacourse table and calls sync_metacourse
|
|
|
|
*/
|
|
|
|
function remove_from_metacourse($metacourseid, $courseid) {
|
|
|
|
|
2005-01-25 04:33:02 +00:00
|
|
|
if (delete_records('course_meta','parent_course',$metacourseid,'child_course',$courseid)) {
|
2005-01-24 22:21:28 +00:00
|
|
|
return sync_metacourse($metacourseid);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-17 12:08:46 +00:00
|
|
|
/**
|
|
|
|
* Determines if a user is currently logged in
|
|
|
|
*
|
|
|
|
* @uses $USER
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2005-04-17 12:08:46 +00:00
|
|
|
*/
|
|
|
|
function isloggedin() {
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
return (!empty($USER->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @staticvar array $admins List of users who have been found to be admins by user id
|
|
|
|
* @staticvar array $nonadmins List of users who have been found not to be admins by user id
|
|
|
|
* @return bool
|
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;
|
2005-04-06 05:53:24 +00:00
|
|
|
static $admins, $nonadmins;
|
|
|
|
|
|
|
|
if (!isset($admins)) {
|
|
|
|
$admins = array();
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2005-04-06 06:20:59 +00:00
|
|
|
if (!empty($USER->id) and ($userid == $USER->id)) { // Check session cache
|
|
|
|
return !empty($USER->admin);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
2005-04-06 05:53:24 +00:00
|
|
|
* Determines if a user is a teacher (or better)
|
2004-09-23 02:48:41 +00:00
|
|
|
*
|
2005-01-23 21:38:01 +00:00
|
|
|
* @uses $USER
|
2005-07-12 02:06:33 +00:00
|
|
|
* @uses $CFG
|
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.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param bool $includeadmin If true this function will return true when it encounters an admin user.
|
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2005-02-08 07:45:53 +00:00
|
|
|
function isteacher($courseid=0, $userid=0, $includeadmin=true) {
|
2005-04-06 05:53:24 +00:00
|
|
|
/// Is the user able to access this course as a teacher?
|
2005-02-08 07:45:53 +00:00
|
|
|
global $USER, $CFG;
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2005-04-06 05:53:24 +00:00
|
|
|
if (empty($userid)) { // we are relying on $USER
|
|
|
|
if (empty($USER) or empty($USER->id)) { // not logged in so can't be a teacher
|
|
|
|
return false;
|
|
|
|
}
|
2005-09-25 17:30:51 +00:00
|
|
|
if (!empty($USER->studentview)) {
|
|
|
|
return false;
|
|
|
|
}
|
2005-04-06 06:20:59 +00:00
|
|
|
if (!empty($USER->teacher) and $courseid) { // look in session cache
|
|
|
|
if (!empty($USER->teacher[$courseid])) { // Explicitly a teacher, good
|
2005-09-25 17:30:51 +00:00
|
|
|
return true;
|
2005-04-06 06:20:59 +00:00
|
|
|
}
|
2005-04-06 05:53:24 +00:00
|
|
|
}
|
2005-04-06 06:20:59 +00:00
|
|
|
$userid = $USER->id; // we need to make further checks
|
2005-04-06 05:53:24 +00:00
|
|
|
}
|
|
|
|
|
2005-04-06 06:20:59 +00:00
|
|
|
if ($includeadmin and isadmin($userid)) { // admins can do anything the teacher can
|
2002-04-03 06:30:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-04-06 06:20:59 +00:00
|
|
|
if (empty($courseid)) { // should not happen, but we handle it
|
2005-02-08 07:45:53 +00:00
|
|
|
if (isadmin() or $CFG->debug > 7) {
|
2005-04-06 06:20:59 +00:00
|
|
|
notify('Coding error: isteacher() should not be used without a valid course id '.
|
|
|
|
'as argument. Please notify the developer for this module.');
|
2005-02-08 07:45:53 +00:00
|
|
|
}
|
2005-01-23 21:38:01 +00:00
|
|
|
return isteacherinanycourse($userid, $includeadmin);
|
|
|
|
}
|
|
|
|
|
2005-04-06 06:20:59 +00:00
|
|
|
/// Last resort, check the database
|
|
|
|
|
2005-01-23 21:38:01 +00:00
|
|
|
return record_exists('user_teachers', 'userid', $userid, 'course', $courseid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if a user is a teacher in any course, or an admin
|
|
|
|
*
|
|
|
|
* @uses $USER
|
|
|
|
* @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.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param bool $includeadmin If true this function will return true when it encounters an admin user.
|
|
|
|
* @return bool
|
2005-01-23 21:38:01 +00:00
|
|
|
*/
|
2005-04-06 05:53:24 +00:00
|
|
|
function isteacherinanycourse($userid=0, $includeadmin=true) {
|
2005-01-31 00:48:23 +00:00
|
|
|
global $USER;
|
|
|
|
|
2005-04-06 05:53:24 +00:00
|
|
|
if (empty($userid)) {
|
|
|
|
if (empty($USER) or empty($USER->id)) {
|
2005-01-23 21:38:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-04-06 06:20:59 +00:00
|
|
|
if (!empty($USER->teacher)) { // look in session cache
|
|
|
|
return true;
|
|
|
|
}
|
2005-01-23 21:38:01 +00:00
|
|
|
$userid = $USER->id;
|
2004-07-25 13:48:55 +00:00
|
|
|
}
|
|
|
|
|
2005-04-06 05:53:24 +00:00
|
|
|
if ($includeadmin and isadmin($userid)) { // admins can do anything
|
2005-01-31 00:48:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-23 21:38:01 +00:00
|
|
|
return record_exists('user_teachers', 'userid', $userid);
|
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.
|
2005-08-15 12:53:57 +00:00
|
|
|
* @param bool $ignorestudentview true = don't do check for studentview mode
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return boo
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2005-08-15 12:53:57 +00:00
|
|
|
function isteacheredit($courseid, $userid=0, $ignorestudentview=false) {
|
2003-08-16 05:19:24 +00:00
|
|
|
global $USER;
|
|
|
|
|
2005-08-15 12:53:57 +00:00
|
|
|
// we can't edit in studentview
|
|
|
|
if (!empty($USER->studentview) and !$ignorestudentview) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) {
|
2005-04-06 06:26:38 +00:00
|
|
|
if (empty($USER) or empty($USER->id)) { // not logged in so can't be a teacher
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (empty($USER->teacheredit)) { // we are relying on session cache
|
|
|
|
return false;
|
|
|
|
}
|
2003-08-16 05:19:24 +00:00
|
|
|
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-11-22 18:38:33 +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.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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
|
2004-11-22 18:38:33 +00:00
|
|
|
*
|
2004-09-23 02:48:41 +00:00
|
|
|
* 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
|
2004-11-22 18:38:33 +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.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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) {
|
2005-09-25 17:30:51 +00:00
|
|
|
return (!empty($USER->student[$courseid]) and empty($USER->studentview));
|
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-11-22 18:38:33 +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.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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
|
2004-11-22 18:38:33 +00:00
|
|
|
* to be used even if system settings specify one.
|
2005-07-12 03:09:25 +00:00
|
|
|
*
|
2004-09-23 02:48:41 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @uses $SESSION
|
2005-07-12 03:09:25 +00:00
|
|
|
* @param object $user A {@link $USER} object to get full name of
|
|
|
|
* @param bool $override If true then the name will be first name followed by last name rather than adhering to fullnamedisplay setting.
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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 '';
|
|
|
|
}
|
|
|
|
|
2005-05-10 06:18:30 +00:00
|
|
|
if (!$override) {
|
|
|
|
if (!empty($CFG->forcefirstname)) {
|
|
|
|
$user->firstname = $CFG->forcefirstname;
|
|
|
|
}
|
|
|
|
if (!empty($CFG->forcelastname)) {
|
|
|
|
$user->lastname = $CFG->forcelastname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2004-09-29 18:56:50 +00:00
|
|
|
* @uses DAYSECS
|
|
|
|
* @uses HOURSECS
|
2004-09-23 02:48:41 +00:00
|
|
|
* @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
|
|
|
|
2005-05-07 03:10:48 +00:00
|
|
|
if ($thing == 'guest') { // Ignore guest account
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 {
|
2005-05-07 03:10:48 +00:00
|
|
|
$thing = rc4decrypt($_COOKIE[$cookiename]);
|
|
|
|
return ($thing == 'guest') ? '': $thing; // Ignore guest account
|
2003-01-08 09:07:07 +00:00
|
|
|
}
|
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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
* @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
|
|
|
*/
|
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
|
2005-07-12 03:09:25 +00:00
|
|
|
* @return object 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)) {
|
Auth/LDAP
Bugfix - value truncation to fit Moodle database
- Added truncate_userinfo() to cleanup data coming from external auth
- Fixed auth_user_create() to truncate user info as appropriate
Auth_ldap_user_sync
- created external script that calls the function
- much faster update strategy on postgres and mysql: auth_sync_users now to uses bulk inserts into a temp table, and then use LEFT JOINs and plain old SELECTs to determine what users it has to insert.
- we now loop over smaller sets of data -- we are still memory-bound, but (a) it'll be easy to use LIMIT to manage that and (b) memory use is much lower now in all cases.
- postgres: phased commits in auth_user_sync() for the batch user upload phase
- Several feature and performance enhancements:
- if a value is removed from ldap, it will be cleared from moodle
- no-op updates (where the data does not change) are skipped
- if a user disappears and then reappears in LDAP in two separate calls to auth_user_sync(),the account will be marked deleted and then be revived. before, the account would have been deleted and created anew.
Multi-source ldap values:
The LDAP auth module now accepts a comma separated set of LDAP field names. When creating or updating a user record, auth/ldap will retrieve all the relevant fields. The right-most values overwrites all the others.
This is particularly useful when updating the user's email address from an LDAP source, which may contain the email address in one of several fields (traditionally: mail, mailForwardingAddress, mailAlternateAddress).
If a value is updated and is set to update external auth and this field is using this multi-source ldap configuration, the auth/ldap module will retrieve the old value, find which field it was sourced from, and update that field in LDAP. If it fails to find the original source of the value, it will log it in error_log.
Log of patchsets applied:
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-131
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-137
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-139
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-172
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-173
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-189
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-190
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-208
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-212
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-216
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-279
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-282
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-287
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-294
2004-11-22 07:46:10 +00:00
|
|
|
$newinfo = truncate_userinfo($newinfo);
|
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;
|
2005-07-14 15:18:51 +00:00
|
|
|
if(empty($CFG->{$newuser->auth.'_preventpassindb'})){ //Prevent passwords in Moodle's DB
|
|
|
|
$newuser->password = md5($password);
|
|
|
|
} else {
|
|
|
|
$newuser->password = 'not cached'; //Unusable 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)) {
|
2005-04-17 12:08:46 +00:00
|
|
|
$user = get_complete_user_data('username', $newuser->username);
|
2004-09-20 09:08:57 +00:00
|
|
|
if($CFG->{'auth_'.$newuser->auth.'_forcechangepassword'}){
|
2005-09-02 06:44:02 +00:00
|
|
|
set_user_preference('auth_forcepasswordchange', 1, $user->id);
|
2004-09-20 09:08:57 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2005-06-09 14:04:38 +00:00
|
|
|
$oldinfo = get_record('user', 'username', $username, '','','','', 'username, auth');
|
|
|
|
$authconfig = get_config('auth/' . $oldinfo->auth);
|
|
|
|
|
2004-09-20 09:08:57 +00:00
|
|
|
if ($newinfo = auth_get_userinfo($username)) {
|
2005-09-02 06:44:02 +00:00
|
|
|
$newinfo = truncate_userinfo($newinfo);
|
2004-09-20 09:08:57 +00:00
|
|
|
foreach ($newinfo as $key => $value){
|
2005-06-09 14:04:38 +00:00
|
|
|
$confkey = 'field_updatelocal_' . $key;
|
|
|
|
if (!empty($authconfig->$confkey) && $authconfig->$confkey === 'onlogin') {
|
2004-09-20 09:08:57 +00:00
|
|
|
$value = addslashes(stripslashes($value)); // Just in case
|
2005-09-02 06:44:02 +00:00
|
|
|
set_field('user', $key, $value, 'username', $username)
|
|
|
|
|| error_log("Error updating $key for $username");
|
2004-09-20 09:08:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-04-17 12:08:46 +00:00
|
|
|
return get_complete_user_data('username', $username);
|
2004-09-20 09:08:57 +00:00
|
|
|
}
|
2004-01-25 09:35:45 +00:00
|
|
|
|
Auth/LDAP
Bugfix - value truncation to fit Moodle database
- Added truncate_userinfo() to cleanup data coming from external auth
- Fixed auth_user_create() to truncate user info as appropriate
Auth_ldap_user_sync
- created external script that calls the function
- much faster update strategy on postgres and mysql: auth_sync_users now to uses bulk inserts into a temp table, and then use LEFT JOINs and plain old SELECTs to determine what users it has to insert.
- we now loop over smaller sets of data -- we are still memory-bound, but (a) it'll be easy to use LIMIT to manage that and (b) memory use is much lower now in all cases.
- postgres: phased commits in auth_user_sync() for the batch user upload phase
- Several feature and performance enhancements:
- if a value is removed from ldap, it will be cleared from moodle
- no-op updates (where the data does not change) are skipped
- if a user disappears and then reappears in LDAP in two separate calls to auth_user_sync(),the account will be marked deleted and then be revived. before, the account would have been deleted and created anew.
Multi-source ldap values:
The LDAP auth module now accepts a comma separated set of LDAP field names. When creating or updating a user record, auth/ldap will retrieve all the relevant fields. The right-most values overwrites all the others.
This is particularly useful when updating the user's email address from an LDAP source, which may contain the email address in one of several fields (traditionally: mail, mailForwardingAddress, mailAlternateAddress).
If a value is updated and is set to update external auth and this field is using this multi-source ldap configuration, the auth/ldap module will retrieve the old value, find which field it was sourced from, and update that field in LDAP. If it fails to find the original source of the value, it will log it in error_log.
Log of patchsets applied:
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-131
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-137
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-139
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-172
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-173
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-189
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-190
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-208
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-212
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-216
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-279
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-282
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-287
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-294
2004-11-22 07:46:10 +00:00
|
|
|
function truncate_userinfo($info) {
|
|
|
|
/// will truncate userinfo as it comes from auth_get_userinfo (from external auth)
|
|
|
|
/// which may have large fields
|
|
|
|
|
|
|
|
// define the limits
|
|
|
|
$limit = array(
|
|
|
|
'username' => 100,
|
2005-02-15 23:12:57 +00:00
|
|
|
'idnumber' => 64,
|
Auth/LDAP
Bugfix - value truncation to fit Moodle database
- Added truncate_userinfo() to cleanup data coming from external auth
- Fixed auth_user_create() to truncate user info as appropriate
Auth_ldap_user_sync
- created external script that calls the function
- much faster update strategy on postgres and mysql: auth_sync_users now to uses bulk inserts into a temp table, and then use LEFT JOINs and plain old SELECTs to determine what users it has to insert.
- we now loop over smaller sets of data -- we are still memory-bound, but (a) it'll be easy to use LIMIT to manage that and (b) memory use is much lower now in all cases.
- postgres: phased commits in auth_user_sync() for the batch user upload phase
- Several feature and performance enhancements:
- if a value is removed from ldap, it will be cleared from moodle
- no-op updates (where the data does not change) are skipped
- if a user disappears and then reappears in LDAP in two separate calls to auth_user_sync(),the account will be marked deleted and then be revived. before, the account would have been deleted and created anew.
Multi-source ldap values:
The LDAP auth module now accepts a comma separated set of LDAP field names. When creating or updating a user record, auth/ldap will retrieve all the relevant fields. The right-most values overwrites all the others.
This is particularly useful when updating the user's email address from an LDAP source, which may contain the email address in one of several fields (traditionally: mail, mailForwardingAddress, mailAlternateAddress).
If a value is updated and is set to update external auth and this field is using this multi-source ldap configuration, the auth/ldap module will retrieve the old value, find which field it was sourced from, and update that field in LDAP. If it fails to find the original source of the value, it will log it in error_log.
Log of patchsets applied:
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-131
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-137
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-139
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-172
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-173
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-189
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-190
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-208
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-212
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-216
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-279
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-282
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-287
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-294
2004-11-22 07:46:10 +00:00
|
|
|
'firstname' => 20,
|
|
|
|
'lastname' => 20,
|
|
|
|
'email' => 100,
|
|
|
|
'icq' => 15,
|
|
|
|
'phone1' => 20,
|
|
|
|
'phone2' => 20,
|
|
|
|
'institution' => 40,
|
|
|
|
'department' => 30,
|
|
|
|
'address' => 70,
|
|
|
|
'city' => 20,
|
|
|
|
'country' => 2,
|
|
|
|
'url' => 255,
|
|
|
|
);
|
2004-11-22 18:38:33 +00:00
|
|
|
|
Auth/LDAP
Bugfix - value truncation to fit Moodle database
- Added truncate_userinfo() to cleanup data coming from external auth
- Fixed auth_user_create() to truncate user info as appropriate
Auth_ldap_user_sync
- created external script that calls the function
- much faster update strategy on postgres and mysql: auth_sync_users now to uses bulk inserts into a temp table, and then use LEFT JOINs and plain old SELECTs to determine what users it has to insert.
- we now loop over smaller sets of data -- we are still memory-bound, but (a) it'll be easy to use LIMIT to manage that and (b) memory use is much lower now in all cases.
- postgres: phased commits in auth_user_sync() for the batch user upload phase
- Several feature and performance enhancements:
- if a value is removed from ldap, it will be cleared from moodle
- no-op updates (where the data does not change) are skipped
- if a user disappears and then reappears in LDAP in two separate calls to auth_user_sync(),the account will be marked deleted and then be revived. before, the account would have been deleted and created anew.
Multi-source ldap values:
The LDAP auth module now accepts a comma separated set of LDAP field names. When creating or updating a user record, auth/ldap will retrieve all the relevant fields. The right-most values overwrites all the others.
This is particularly useful when updating the user's email address from an LDAP source, which may contain the email address in one of several fields (traditionally: mail, mailForwardingAddress, mailAlternateAddress).
If a value is updated and is set to update external auth and this field is using this multi-source ldap configuration, the auth/ldap module will retrieve the old value, find which field it was sourced from, and update that field in LDAP. If it fails to find the original source of the value, it will log it in error_log.
Log of patchsets applied:
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-131
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-137
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-139
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-172
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-173
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-189
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-190
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-208
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-212
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-216
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-279
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-282
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-287
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-294
2004-11-22 07:46:10 +00:00
|
|
|
// apply where needed
|
|
|
|
foreach (array_keys($info) as $key) {
|
|
|
|
if (!empty($limit[$key])) {
|
2005-02-14 01:30:57 +00:00
|
|
|
$info[$key] = trim(substr($info[$key],0, $limit[$key]));
|
2004-11-22 18:38:33 +00:00
|
|
|
}
|
Auth/LDAP
Bugfix - value truncation to fit Moodle database
- Added truncate_userinfo() to cleanup data coming from external auth
- Fixed auth_user_create() to truncate user info as appropriate
Auth_ldap_user_sync
- created external script that calls the function
- much faster update strategy on postgres and mysql: auth_sync_users now to uses bulk inserts into a temp table, and then use LEFT JOINs and plain old SELECTs to determine what users it has to insert.
- we now loop over smaller sets of data -- we are still memory-bound, but (a) it'll be easy to use LIMIT to manage that and (b) memory use is much lower now in all cases.
- postgres: phased commits in auth_user_sync() for the batch user upload phase
- Several feature and performance enhancements:
- if a value is removed from ldap, it will be cleared from moodle
- no-op updates (where the data does not change) are skipped
- if a user disappears and then reappears in LDAP in two separate calls to auth_user_sync(),the account will be marked deleted and then be revived. before, the account would have been deleted and created anew.
Multi-source ldap values:
The LDAP auth module now accepts a comma separated set of LDAP field names. When creating or updating a user record, auth/ldap will retrieve all the relevant fields. The right-most values overwrites all the others.
This is particularly useful when updating the user's email address from an LDAP source, which may contain the email address in one of several fields (traditionally: mail, mailForwardingAddress, mailAlternateAddress).
If a value is updated and is set to update external auth and this field is using this multi-source ldap configuration, the auth/ldap module will retrieve the old value, find which field it was sourced from, and update that field in LDAP. If it fails to find the original source of the value, it will log it in error_log.
Log of patchsets applied:
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-131
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-137
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-139
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-172
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-173
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-189
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-190
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-208
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-212
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-216
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-279
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-282
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-287
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-294
2004-11-22 07:46:10 +00:00
|
|
|
}
|
2004-11-22 18:38:33 +00:00
|
|
|
|
Auth/LDAP
Bugfix - value truncation to fit Moodle database
- Added truncate_userinfo() to cleanup data coming from external auth
- Fixed auth_user_create() to truncate user info as appropriate
Auth_ldap_user_sync
- created external script that calls the function
- much faster update strategy on postgres and mysql: auth_sync_users now to uses bulk inserts into a temp table, and then use LEFT JOINs and plain old SELECTs to determine what users it has to insert.
- we now loop over smaller sets of data -- we are still memory-bound, but (a) it'll be easy to use LIMIT to manage that and (b) memory use is much lower now in all cases.
- postgres: phased commits in auth_user_sync() for the batch user upload phase
- Several feature and performance enhancements:
- if a value is removed from ldap, it will be cleared from moodle
- no-op updates (where the data does not change) are skipped
- if a user disappears and then reappears in LDAP in two separate calls to auth_user_sync(),the account will be marked deleted and then be revived. before, the account would have been deleted and created anew.
Multi-source ldap values:
The LDAP auth module now accepts a comma separated set of LDAP field names. When creating or updating a user record, auth/ldap will retrieve all the relevant fields. The right-most values overwrites all the others.
This is particularly useful when updating the user's email address from an LDAP source, which may contain the email address in one of several fields (traditionally: mail, mailForwardingAddress, mailAlternateAddress).
If a value is updated and is set to update external auth and this field is using this multi-source ldap configuration, the auth/ldap module will retrieve the old value, find which field it was sourced from, and update that field in LDAP. If it fails to find the original source of the value, it will log it in error_log.
Log of patchsets applied:
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-131
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-137
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-139
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-172
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-173
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-189
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-190
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-208
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-212
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-216
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-279
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-282
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-287
arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-294
2004-11-22 07:46:10 +00:00
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
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.
|
2004-11-22 18:38:33 +00:00
|
|
|
*
|
2004-09-23 02:48:41 +00:00
|
|
|
* Uses auth_ functions from the currently active auth module
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
2004-11-22 18:38:33 +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
|
|
|
|
2005-04-17 12:08:46 +00:00
|
|
|
if (!$user = get_complete_user_data('username', $username)) {
|
2005-01-28 16:59:43 +00:00
|
|
|
$user->id = 0; // Not a user
|
|
|
|
$user->auth = $CFG->auth;
|
|
|
|
}
|
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
|
2005-02-23 02:11:59 +00:00
|
|
|
if (!empty($user->id) && (isadmin($user->id) || 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
|
2005-02-17 09:28:27 +00:00
|
|
|
if ($user->id) { // User already exists in database
|
2004-08-31 12:33:20 +00:00
|
|
|
if (empty($user->auth)) { // For some reason auth isn't set yet
|
|
|
|
set_field('user', 'auth', $auth, 'username', $username);
|
|
|
|
}
|
2005-07-15 11:19:31 +00:00
|
|
|
if (empty($CFG->{$user->auth.'_preventpassindb'})){ //Calculate the password to update
|
|
|
|
$passfield = $md5password;
|
|
|
|
} else {
|
|
|
|
$passfield = 'not cached';
|
|
|
|
}
|
|
|
|
if ($passfield <> $user->password) { // Update local copy of password for reference
|
|
|
|
set_field('user', 'password', $passfield, 'username', $username); //Update password
|
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
|
2004-10-13 07:16:24 +00:00
|
|
|
$useriscreator = auth_iscreator($username);
|
|
|
|
if (!is_null($useriscreator)) {
|
|
|
|
if ($useriscreator) {
|
|
|
|
if (! record_exists('user_coursecreators', 'userid', $user->id)) {
|
|
|
|
$cdata->userid = $user->id;
|
|
|
|
if (! insert_record('user_coursecreators', $cdata)) {
|
|
|
|
error('Cannot add user to course creators.');
|
|
|
|
}
|
2004-08-16 15:41:57 +00:00
|
|
|
}
|
2004-10-13 07:16:24 +00:00
|
|
|
} else {
|
|
|
|
if (record_exists('user_coursecreators', 'userid', $user->id)) {
|
|
|
|
if (! delete_records('user_coursecreators', 'userid', $user->id)) {
|
|
|
|
error('Cannot remove user from course creators.');
|
|
|
|
}
|
2004-08-16 15:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
2004-11-22 18:38:33 +00:00
|
|
|
}
|
2004-08-16 15:41:57 +00:00
|
|
|
}
|
2005-07-15 04:26:18 +00:00
|
|
|
|
|
|
|
/// Log in to a second system if necessary
|
|
|
|
if (!empty($CFG->sso)) {
|
|
|
|
include_once($CFG->dirroot .'/sso/'. $CFG->sso .'/lib.php');
|
|
|
|
if (function_exists('sso_user_login')) {
|
|
|
|
if (!sso_user_login($username, $password)) { // Perform the signon process
|
|
|
|
notify('Second sign-on failed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2005-05-04 07:06:24 +00:00
|
|
|
add_to_log(0, 'login', 'error', 'index.php', $username);
|
2005-03-16 22:15:36 +00:00
|
|
|
error_log('[client '.$_SERVER['REMOTE_ADDR']."] $CFG->wwwroot Failed Login: $username ".$_SERVER['HTTP_USER_AGENT']);
|
2003-03-14 21:34:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2005-04-17 12:08:46 +00:00
|
|
|
/**
|
|
|
|
* Get a complete user record, which includes all the info
|
|
|
|
* in the user record, as well as membership information
|
|
|
|
* Intended for setting as $USER session variable
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @uses SITEID
|
|
|
|
* @param string $field The user field to be checked for a given value.
|
|
|
|
* @param string $value The value to match for $field.
|
|
|
|
* @return user A {@link $USER} object.
|
|
|
|
*/
|
|
|
|
function get_complete_user_data($field, $value) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!$field || !$value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get all the basic user data
|
|
|
|
|
|
|
|
if (! $user = get_record_select('user', $field .' = \''. $value .'\' AND deleted <> \'1\'')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add membership information
|
|
|
|
|
|
|
|
if ($admins = get_records('user_admins', 'userid', $user->id)) {
|
|
|
|
$user->admin = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user->student[SITEID] = isstudent(SITEID, $user->id);
|
|
|
|
|
|
|
|
/// Determine enrolments based on current enrolment module
|
|
|
|
|
|
|
|
require_once($CFG->dirroot .'/enrol/'. $CFG->enrol .'/enrol.php');
|
|
|
|
$enrol = new enrolment_plugin();
|
|
|
|
$enrol->get_student_courses($user);
|
|
|
|
$enrol->get_teacher_courses($user);
|
|
|
|
|
|
|
|
/// Get various settings and preferences
|
|
|
|
|
|
|
|
if ($displays = get_records('course_display', 'userid', $user->id)) {
|
|
|
|
foreach ($displays as $display) {
|
|
|
|
$user->display[$display->course] = $display->display;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($preferences = get_records('user_preferences', 'userid', $user->id)) {
|
|
|
|
foreach ($preferences as $preference) {
|
|
|
|
$user->preference[$preference->name] = $preference->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($groups = get_records('groups_members', 'userid', $user->id)) {
|
|
|
|
foreach ($groups as $groupmember) {
|
|
|
|
$courseid = get_field('groups', 'courseid', 'id', $groupmember->groupid);
|
|
|
|
$user->groupmember[$courseid] = $groupmember->groupid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Rewrite some variables if necessary
|
|
|
|
if (!empty($user->description)) {
|
|
|
|
$user->description = true; // No need to cart all of it around
|
|
|
|
}
|
|
|
|
if ($user->username == 'guest') {
|
|
|
|
$user->lang = $CFG->lang; // Guest language always same as site
|
|
|
|
$user->firstname = get_string('guestuser'); // Name always in current language
|
|
|
|
$user->lastname = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$user->loggedin = true;
|
|
|
|
$user->site = $CFG->wwwroot; // for added security, store the site in the session
|
|
|
|
$user->sesskey = random_string(10);
|
|
|
|
$user->sessionIP = md5(getremoteaddr()); // Store the current IP in the session
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_user_info_from_db($field, $value) { // For backward compatibility
|
|
|
|
return get_complete_user_data($field, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When logging in, this function is run to set certain preferences
|
|
|
|
* for the current SESSION
|
|
|
|
*/
|
|
|
|
function set_login_session_preferences() {
|
2005-05-28 06:39:38 +00:00
|
|
|
global $SESSION, $CFG;
|
2005-04-17 12:08:46 +00:00
|
|
|
|
|
|
|
$SESSION->justloggedin = true;
|
|
|
|
|
|
|
|
unset($SESSION->lang);
|
|
|
|
unset($SESSION->encoding);
|
|
|
|
$SESSION->encoding = get_string('thischarset');
|
|
|
|
|
|
|
|
// Restore the calendar filters, if saved
|
|
|
|
if (intval(get_user_preferences('calendar_persistflt', 0))) {
|
|
|
|
include_once($CFG->dirroot.'/calendar/lib.php');
|
|
|
|
calendar_set_filters_status(get_user_preferences('calendar_savedflt', 0xff));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-23 02:48:41 +00:00
|
|
|
/**
|
|
|
|
* Enrols (or re-enrols) a student in a given course
|
|
|
|
*
|
2005-08-26 06:11:38 +00:00
|
|
|
* NOTE: Defaults to 'manual' enrolment - enrolment plugins
|
|
|
|
* must set it explicitly.
|
|
|
|
*
|
2005-07-12 02:06:33 +00:00
|
|
|
* @uses $CFG
|
2004-09-25 01:29:37 +00:00
|
|
|
* @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.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param int $courseid The id of the course that is being viewed
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $timestart ?
|
|
|
|
* @param int $timeend ?
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param string $enrol ?
|
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2005-08-26 06:11:38 +00:00
|
|
|
function enrol_student($userid, $courseid, $timestart=0, $timeend=0, $enrol='manual') {
|
2004-07-29 14:54:25 +00:00
|
|
|
|
2004-11-18 06:40:11 +00:00
|
|
|
global $CFG;
|
|
|
|
|
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;
|
|
|
|
}
|
2005-01-24 22:21:28 +00:00
|
|
|
// enrol the student in any parent meta courses...
|
2005-07-12 02:06:33 +00:00
|
|
|
if ($parents = get_records('course_meta', 'child_course', $courseid)) {
|
2005-01-24 22:21:28 +00:00
|
|
|
foreach ($parents as $parent) {
|
2005-08-23 05:43:02 +00:00
|
|
|
enrol_student($userid, $parent->parent_course, $timestart, $timeend,'metacourse');
|
2005-01-24 22:21:28 +00:00
|
|
|
}
|
|
|
|
}
|
2005-01-30 09:14:41 +00:00
|
|
|
|
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-11-22 02:38:31 +00:00
|
|
|
$student->enrol = $enrol;
|
2004-09-22 14:39:15 +00:00
|
|
|
return update_record('user_students', $student);
|
2004-11-22 18:38:33 +00:00
|
|
|
|
2004-09-09 09:43:51 +00:00
|
|
|
} else {
|
2004-11-18 06:40:11 +00:00
|
|
|
require_once("$CFG->dirroot/mod/forum/lib.php");
|
2004-10-26 19:53:48 +00:00
|
|
|
forum_add_user($userid, $courseid);
|
|
|
|
|
2004-09-09 09:43:51 +00:00
|
|
|
$student->userid = $userid;
|
|
|
|
$student->course = $courseid;
|
|
|
|
$student->timestart = $timestart;
|
|
|
|
$student->timeend = $timeend;
|
|
|
|
$student->time = time();
|
2004-11-22 02:38:31 +00:00
|
|
|
$student->enrol = $enrol;
|
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.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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
|
|
|
}
|
2005-01-24 22:21:28 +00:00
|
|
|
// enrol the student in any parent meta courses...
|
2005-01-25 04:33:02 +00:00
|
|
|
if ($parents = get_records('course_meta','child_course',$courseid)) {
|
2005-01-24 22:21:28 +00:00
|
|
|
foreach ($parents as $parent) {
|
|
|
|
unenrol_student($userid, $parent->parent_course);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
*
|
2005-07-12 02:06:33 +00:00
|
|
|
* @uses $USER
|
2004-09-25 01:29:37 +00:00
|
|
|
* @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.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param int $courseid The id of the course that is being viewed, if any
|
2004-09-25 01:29:37 +00:00
|
|
|
* @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 ?
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param string $enrol ?
|
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-11-22 02:38:31 +00:00
|
|
|
function add_teacher($userid, $courseid, $editall=1, $role='', $timestart=0, $timeend=0, $enrol='manual') {
|
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;
|
2004-11-22 02:38:31 +00:00
|
|
|
$newteacher->enrol = $enrol;
|
2004-07-29 14:54:25 +00:00
|
|
|
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
|
2005-07-15 00:00:35 +00:00
|
|
|
require_once($CFG->dirroot.'/mod/forum/lib.php');
|
2004-08-29 19:17:27 +00:00
|
|
|
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
|
2004-11-22 18:38:33 +00:00
|
|
|
* @param int $userid The id of the user that is being tested against.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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-11-22 18:38:33 +00:00
|
|
|
* @param int $userid The id of the user that is being tested against.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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
|
|
|
|
*
|
2005-07-12 02:06:33 +00:00
|
|
|
* @uses $db
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $userid The id of the user that is being tested against.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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-11-22 18:38:33 +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-11-22 18:38:33 +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
|
|
|
|
*
|
2005-07-12 02:06:33 +00:00
|
|
|
* @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.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param bool $showfeedback Set this to false to suppress notifications from being printed as the functions performs its steps.
|
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2003-08-02 11:32:59 +00:00
|
|
|
function remove_course_contents($courseid, $showfeedback=true) {
|
|
|
|
|
2005-01-25 17:39:55 +00:00
|
|
|
global $CFG, $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 {
|
2005-05-29 17:27:00 +00:00
|
|
|
notify('Could not delete '. $modname .' instance '. $instance->id .' ('. format_string($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
|
|
|
}
|
|
|
|
|
2005-02-28 04:35:32 +00:00
|
|
|
// Delete course blocks
|
|
|
|
if (delete_records('block_instance', 'pagetype', PAGE_COURSE_VIEW, 'pageid', $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify($strdeleted .' block_instance');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-03-13 13:50:23 +00:00
|
|
|
// Delete gradebook stuff
|
|
|
|
|
2005-06-23 11:43:20 +00:00
|
|
|
if (delete_records("grade_category", "courseid", $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted grade categories");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
2005-03-13 13:50:23 +00:00
|
|
|
}
|
2005-06-23 11:43:20 +00:00
|
|
|
if (delete_records("grade_exceptions", "courseid", $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted grade exceptions");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
2005-03-13 13:50:23 +00:00
|
|
|
}
|
2005-06-23 11:43:20 +00:00
|
|
|
if (delete_records("grade_item", "courseid", $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted grade items");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
2005-03-13 13:50:23 +00:00
|
|
|
}
|
2005-06-23 11:43:20 +00:00
|
|
|
if (delete_records("grade_letter", "courseid", $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted grade letters");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
if (delete_records("grade_preferences", "courseid", $course->id)) {
|
|
|
|
if ($showfeedback) {
|
|
|
|
notify("$strdeleted grade preferences");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
2005-03-13 13:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-25 04:33:02 +00:00
|
|
|
if ($course->metacourse) {
|
|
|
|
delete_records("course_meta","parent_course",$course->id);
|
2005-01-24 22:21:28 +00:00
|
|
|
sync_metacourse($course->id); // have to do it here so the enrolments get nuked. sync_metacourses won't find it without the id.
|
|
|
|
if ($showfeedback) {
|
2005-01-25 04:33:02 +00:00
|
|
|
notify("$strdeleted course_meta");
|
2005-01-24 22:21:28 +00:00
|
|
|
}
|
2005-03-13 13:50:23 +00:00
|
|
|
} else {
|
2005-01-25 04:33:02 +00:00
|
|
|
if ($parents = get_records("course_meta","child_course",$course->id)) {
|
2005-01-24 22:21:28 +00:00
|
|
|
foreach ($parents as $parent) {
|
|
|
|
remove_from_metacourse($parent->parent_course,$parent->child_course); // this will do the unenrolments as well.
|
|
|
|
}
|
|
|
|
if ($showfeedback) {
|
2005-01-25 04:33:02 +00:00
|
|
|
notify("$strdeleted course_meta");
|
2005-01-24 22:21:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-02 11:32:59 +00:00
|
|
|
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 $SESSION
|
|
|
|
* @uses $CFG
|
2004-09-25 01:29:37 +00:00
|
|
|
* @param int $courseid The id of the course that is being viewed
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param bool $showfeedback Set this to false to suppress notifications from being printed as the functions performs its steps.
|
|
|
|
* @param bool $removestudents ?
|
|
|
|
* @param bool $removeteachers ?
|
|
|
|
* @param bool $removegroups ?
|
|
|
|
* @param bool $removeevents ?
|
|
|
|
* @param bool $removelogs ?
|
|
|
|
* @return bool
|
2004-09-23 02:48:41 +00:00
|
|
|
* @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) {
|
|
|
|
|
2005-01-25 17:39:55 +00:00
|
|
|
global $CFG, $USER, $SESSION;
|
2004-08-01 07:34:39 +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)');
|
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
|
|
|
|
|
|
|
/**
|
2005-07-12 02:06:33 +00:00
|
|
|
* Determines if the user a member of the given group
|
|
|
|
*
|
|
|
|
* @uses $USER
|
|
|
|
* @param int $groupid The group to check the membership of
|
|
|
|
* @param int $userid The user to check against the group
|
|
|
|
* @return bool
|
|
|
|
*/
|
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-12-14 08:05:49 +00:00
|
|
|
/**
|
|
|
|
* Add a user to a group, return true upon success or if user already a group member
|
|
|
|
*
|
2005-07-12 03:09:25 +00:00
|
|
|
* @param int $groupid The group id to add user to
|
|
|
|
* @param int $userid The user id to add to the group
|
|
|
|
* @return bool
|
2004-12-14 08:05:49 +00:00
|
|
|
*/
|
|
|
|
function add_user_to_group ($groupid, $userid) {
|
|
|
|
if (ismember($groupid, $userid)) return true;
|
|
|
|
$record->groupid = $groupid;
|
|
|
|
$record->userid = $userid;
|
2005-02-25 06:08:40 +00:00
|
|
|
$record->timeadded = time();
|
2004-12-14 08:05:49 +00:00
|
|
|
return (insert_record('groups_members', $record) !== false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-11 17:44:46 +00:00
|
|
|
/**
|
2005-07-12 03:09:25 +00:00
|
|
|
* Get the group ID of the current user in the given course
|
2004-09-25 01:29:37 +00:00
|
|
|
*
|
|
|
|
* @uses $USER
|
|
|
|
* @param int $courseid The course being examined - relates to id field in 'course' table.
|
2005-07-12 03:09:25 +00:00
|
|
|
* @return int
|
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
|
2005-07-12 03:09:25 +00:00
|
|
|
* @param object $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
|
|
|
|
*/
|
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
|
|
|
|
*
|
2005-07-12 02:06:33 +00:00
|
|
|
* @uses $USER
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $SESSION
|
|
|
|
* @param int $courseid The course being examined - relates to id field in 'course' table.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param bool $full If true, the return value is a full record object. If false, just the id of the record.
|
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])) {
|
2005-04-26 08:00:56 +00:00
|
|
|
if (empty($USER->groupmember[$courseid]) or isteacheredit($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-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 ?
|
2005-07-12 03:09:25 +00:00
|
|
|
* @return int|false
|
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
|
|
|
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
function generate_email_processing_address($modid,$modargs) {
|
|
|
|
global $CFG;
|
2005-02-10 07:53:52 +00:00
|
|
|
|
|
|
|
if (empty($CFG->siteidentifier)) { // Unique site identification code
|
|
|
|
set_config('siteidentifier', random_string(32));
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
}
|
2005-02-25 06:08:40 +00:00
|
|
|
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
$header = $CFG->mailprefix . substr(base64_encode(pack('C',$modid)),0,2).$modargs;
|
2005-05-30 23:26:17 +00:00
|
|
|
return $header . substr(md5($header.$CFG->siteidentifier),0,16).'@'.$CFG->maildomain;
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
}
|
|
|
|
|
2003-12-30 17:18:06 +00:00
|
|
|
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
function moodle_process_email($modargs,$body) {
|
|
|
|
// the first char should be an unencoded letter. We'll take this as an action
|
|
|
|
switch ($modargs{0}) {
|
|
|
|
case 'B': { // bounce
|
|
|
|
list(,$userid) = unpack('V',base64_decode(substr($modargs,1,8)));
|
|
|
|
if ($user = get_record_select("user","id=$userid","id,email")) {
|
|
|
|
// check the half md5 of their email
|
|
|
|
$md5check = substr(md5($user->email),0,16);
|
2005-06-13 00:17:16 +00:00
|
|
|
if ($md5check == substr($modargs, -16)) {
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
set_bounce_count($user);
|
|
|
|
}
|
|
|
|
// else maybe they've already changed it?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// maybe more later?
|
|
|
|
}
|
|
|
|
}
|
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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @uses $FULLME
|
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)
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param bool $usetrueaddress determines whether $from email address should
|
2004-09-25 01:29:37 +00:00
|
|
|
* be sent out. Will be overruled by user profile setting for maildisplay
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool|string Returns "true" if mail was sent OK, "emailstop" if email
|
2004-09-25 01:29:37 +00:00
|
|
|
* was blocked by user and "false" if there was another sort of error.
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
function email_to_user($user, $from, $subject, $messagetext, $messagehtml='', $attachment='', $attachname='', $usetrueaddress=true, $repyto='', $replytoname='') {
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-12-12 06:49:26 +00:00
|
|
|
global $CFG, $FULLME;
|
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;
|
|
|
|
}
|
2005-02-10 10:28:27 +00:00
|
|
|
if (!empty($course->theme)) { // Course language is defined
|
|
|
|
$CFG->coursetheme = $course->theme;
|
|
|
|
}
|
2004-02-09 08:49:54 +00:00
|
|
|
|
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
|
|
|
}
|
2005-02-25 06:08:40 +00:00
|
|
|
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
if (over_bounce_threshold($user)) {
|
|
|
|
error_log("User $user->id (".fullname($user).") is over bounce threshold! Not sending.");
|
|
|
|
return false;
|
|
|
|
}
|
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();
|
|
|
|
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
// make up an email address for handling bounces
|
|
|
|
if (!empty($CFG->handlebounces)) {
|
|
|
|
$modargs = 'B'.base64_encode(pack('V',$user->id)).substr(md5($user->email),0,16);
|
|
|
|
$mail->Sender = generate_email_processing_address(0,$modargs);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$mail->Sender = $adminuser->email;
|
2005-02-25 06:08:40 +00:00
|
|
|
}
|
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);
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
if (empty($replyto)) {
|
|
|
|
$mail->AddReplyTo($CFG->noreplyaddress,get_string('noreplyname'));
|
|
|
|
}
|
2004-06-06 12:49:55 +00:00
|
|
|
}
|
2005-02-25 06:08:40 +00:00
|
|
|
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
if (!empty($replyto)) {
|
|
|
|
$mail->AddReplyTo($replyto,$replytoname);
|
|
|
|
}
|
|
|
|
|
2005-05-30 18:10:57 +00:00
|
|
|
$mail->Subject = substr(stripslashes($subject), 0, 900);
|
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
|
|
|
|
2005-03-02 17:56:46 +00:00
|
|
|
if (!empty($from->priority)) {
|
|
|
|
$mail->Priority = $from->priority;
|
|
|
|
}
|
|
|
|
|
2005-02-05 01:36:55 +00:00
|
|
|
if ($messagehtml && $user->mailformat == 1) { // Don't ever send HTML to users who don't want it
|
2002-05-24 06:38:37 +00:00
|
|
|
$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 {
|
2005-03-07 11:32:03 +00:00
|
|
|
require_once($CFG->libdir.'/filelib.php');
|
2004-09-22 14:39:15 +00:00
|
|
|
$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()) {
|
First cut of email to module (or core) processing.
This patch contains:
* email_to_user will set the envelope sender to a special bounce processing address (based on $CFG settings)
* email_to_user will accept (and set) a reply-to header, to be generated by the module calling the function.
* new functions:
* generate_email_processing_address - ALWAYS use this to generate the reply-to header. reply-to header will look like this:
(LIMIT: 64 chars total)
prefix - EXACTLY four chars
encodeded, packed, moduleid (0 for core) (2 chars)
up to 42 chars for the modules to put anything they want it (can contain userid (or, eg for forum, postids to reply to), or anything really. 42 chars is ABSOLUTE LIMIT)
16 char hash (half an md5) of the first part of the address, together with a site "secret"
* moodle_process_email - any non-module email processing goes here (currently used for processing bounces)
* bounce handling:
* config settings for bounce threshold and ratio (and whether to handle bounces at all)
* if too many bounces occur against any given user, user_not_fully_set_up will force an email address change
* associated functions (over_bounce_threshold, set_send_count, set_bounce_count)
* handling emails to noreply address (see below)
* new script - admin/process_email.php
This script needs to be called from your mail program for anything starting with the 4 char prefix described above (and optionally, the noreply address)
It will bounce emails to the noreplyaddress, with a friendly "this is not a real email address" message
It will break down and unencode the email address into moduleid and validate the half md5 hash, and call $modname_process_email (if it exists). Arguments to these functions are: $modargs (any part of the email address that isn't the prefix, modid or the hash) and the contents of the email (read from STDIN).
* associated string changes/additions
* changes in config-dist.php to give clues as to how to set this up.
MODULE WRITERS!
take a look at new functions moodle_process_email and generate_email_processing_address in moodlelib.php for ideas about how to
* encode and unencode the arguments your module needs to do the processing
* how to deal with multiple "actions" for any given module.
Martin Langhoff <martin@catalyst.net.nz> will be writing up some PROPER documentation, containing amongst other things config settings for different mail servers (this was developed against Postfix). Feel free to email me with any feedback on the code or design, penny@catalyst.net.nz. Or post on the developer fourm.
2005-02-08 02:57:14 +00:00
|
|
|
set_send_count($user);
|
2002-05-24 06:38:37 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2004-09-22 14:39:15 +00:00
|
|
|
mtrace('ERROR: '. $mail->ErrorInfo);
|
2004-12-12 06:49:26 +00:00
|
|
|
add_to_log(SITEID, 'library', 'mailer', $FULLME, '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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool|string Returns "true" if mail was sent OK, "emailstop" if email
|
2004-09-25 01:29:37 +00:00
|
|
|
* 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();
|
|
|
|
|
2005-08-15 23:19:58 +00:00
|
|
|
$external = false;
|
|
|
|
if (!is_internal_auth($user->auth)) {
|
|
|
|
include_once($CFG->dirroot . '/auth/' . $user->auth . '/lib.php');
|
|
|
|
if (empty($CFG->{'auth_'.$user->auth.'_stdchangepassword'})
|
|
|
|
|| !function_exists('auth_user_update_password')) {
|
|
|
|
trigger_error("Attempt to reset user password for user $user->username with Auth $user->auth.");
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$external = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-23 03:07:01 +00:00
|
|
|
$newpassword = generate_password();
|
|
|
|
|
2005-08-15 23:19:58 +00:00
|
|
|
if ($external) {
|
|
|
|
if (!auth_user_update_password($user->username, $newpassword)) {
|
|
|
|
error("Could not set user password!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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;
|
2005-07-15 00:04:51 +00:00
|
|
|
$a->link = $CFG->httpswwwroot .'/login/change_password.php';
|
2004-09-22 14:39:15 +00:00
|
|
|
$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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool|string Returns "true" if mail was sent OK, "emailstop" if email
|
2004-09-25 01:29:37 +00:00
|
|
|
* 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();
|
|
|
|
|
2005-01-29 04:54:17 +00:00
|
|
|
$data->firstname = fullname($user);
|
2002-12-23 03:07:01 +00:00
|
|
|
$data->sitename = $site->fullname;
|
2004-09-22 14:39:15 +00:00
|
|
|
$data->admin = fullname($from) .' ('. $from->email .')';
|
2002-12-23 03:07:01 +00:00
|
|
|
|
2004-09-22 14:39:15 +00:00
|
|
|
$subject = get_string('emailconfirmationsubject', '', $site->fullname);
|
2002-12-23 03:07:01 +00:00
|
|
|
|
2005-03-01 07:06:42 +00:00
|
|
|
/// Make the text version a normal link for normal people
|
|
|
|
$data->link = $CFG->wwwroot .'/login/confirm.php?p='. $user->secret .'&s='. $user->username;
|
|
|
|
$message = get_string('emailconfirmation', '', $data);
|
|
|
|
|
|
|
|
/// Make the HTML version more XHTML happy (&)
|
|
|
|
$data->link = $CFG->wwwroot .'/login/confirm.php?p='. $user->secret .'&s='. $user->username;
|
|
|
|
$messagehtml = text_to_html(get_string('emailconfirmation', '', $data), false, false, true);
|
|
|
|
|
|
|
|
$user->mailformat = 1; // Always send HTML version as well
|
2004-05-13 03:09:47 +00:00
|
|
|
|
|
|
|
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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool|string Returns "true" if mail was sent OK, "emailstop" if email
|
2004-09-25 01:29:37 +00:00
|
|
|
* was blocked by user and "false" if there was another sort of error.
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
2003-05-04 03:00:52 +00:00
|
|
|
function send_password_change_confirmation_email($user) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$site = get_site();
|
|
|
|
$from = get_admin();
|
|
|
|
|
|
|
|
$data->firstname = $user->firstname;
|
|
|
|
$data->sitename = $site->fullname;
|
2005-07-15 00:04:51 +00:00
|
|
|
$data->link = $CFG->httpswwwroot .'/login/forgot_password.php?p='. $user->secret .'&s='. $user->username;
|
2004-09-22 14:39:15 +00:00
|
|
|
$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.
|
|
|
|
*
|
2005-07-12 03:09:25 +00:00
|
|
|
* @uses $CFG
|
|
|
|
* @param string $email Content of email
|
|
|
|
* @return string|false
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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
|
2005-07-12 02:06:33 +00:00
|
|
|
* param bool $shownotices If true then notification messages will be printed out on error.
|
2004-09-25 01:29:37 +00:00
|
|
|
* @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;
|
|
|
|
}
|
2005-01-10 15:26:43 +00:00
|
|
|
if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety
|
|
|
|
@fwrite($handle, "deny from all\r\n");
|
2005-02-25 06:08:40 +00:00
|
|
|
@fclose($handle);
|
2005-01-10 15:26:43 +00:00
|
|
|
}
|
2002-08-04 16:20:30 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2004-12-26 14:45:39 +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
|
|
|
*/
|
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
|
|
|
*/
|
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
|
|
|
/**
|
2005-07-12 03:09:25 +00:00
|
|
|
* Related to {@link get_max_upload_file_size()} - this function returns an
|
2004-09-23 02:48:41 +00:00
|
|
|
* 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-11-22 18:38:33 +00:00
|
|
|
* If left empty then the first element of the $_FILES array will be used
|
2004-09-23 05:10:21 +00:00
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @uses $_FILES
|
|
|
|
* @param array $filearray A 1-dimensional sub-array of the $_FILES array
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param bool $returnerror If true then a string error message will be returned. Otherwise the user will be notified of the error in a notify() call.
|
|
|
|
* @return bool|string
|
2004-09-23 02:48:41 +00:00
|
|
|
*/
|
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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param bool $descend ?
|
|
|
|
* @param bool $getdirs If true then (sub)directories are included in the output
|
|
|
|
* @param bool $getfiles If true then files are included in the output
|
2004-09-25 01:29:37 +00:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2005-04-19 15:11:44 +00:00
|
|
|
/**
|
|
|
|
* Removes the entire contents of a directory - does not delete the directory
|
|
|
|
* itself
|
|
|
|
*
|
|
|
|
* @param string $dirName - full path to directory
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool true if successful, false if error
|
2005-04-19 15:11:44 +00:00
|
|
|
**/
|
|
|
|
|
|
|
|
function delDirContents($dirName) {
|
|
|
|
if(empty($dirName)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(file_exists($dirName)) {
|
|
|
|
$dir = dir($dirName);
|
|
|
|
while($file = $dir->read()) {
|
|
|
|
if($file != '.' && $file != '..') {
|
|
|
|
if(is_dir($dirName.'/'.$file)) {
|
|
|
|
delDir($dirName.'/'.$file);
|
|
|
|
} else {
|
|
|
|
@unlink($dirName.'/'.$file) or die('File '.$dirName.'/'.$file.' couldn\'t be deleted!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$dir->close();
|
|
|
|
@rmdir($dirName) or die('Folder '.$dirName.' couldn\'t be deleted!');
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2005-09-05 05:33:02 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
// do it this way if we can, it's much faster
|
|
|
|
if (!empty($CFG->pathtodu) && is_executable(trim($CFG->pathtodu))) {
|
|
|
|
$command = trim($CFG->pathtodu).' -sk --apparent-size '.$rootdir;
|
|
|
|
exec($command,$output,$return);
|
|
|
|
if (is_array($output)) {
|
|
|
|
return get_real_size(intval($output[0]).'k'); // we told it to return k.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-02-10 13:26:41 +00:00
|
|
|
/*
|
|
|
|
* Convert high ascii characters into low ascii
|
|
|
|
* This code is from http://kalsey.com/2004/07/dirify_in_php/
|
2005-02-25 06:08:40 +00:00
|
|
|
*
|
2005-02-10 13:26:41 +00:00
|
|
|
*/
|
|
|
|
function convert_high_ascii($s) {
|
|
|
|
$HighASCII = array(
|
|
|
|
"!\xc0!" => 'A', # A`
|
|
|
|
"!\xe0!" => 'a', # a`
|
|
|
|
"!\xc1!" => 'A', # A'
|
|
|
|
"!\xe1!" => 'a', # a'
|
|
|
|
"!\xc2!" => 'A', # A^
|
|
|
|
"!\xe2!" => 'a', # a^
|
|
|
|
"!\xc4!" => 'Ae', # A:
|
|
|
|
"!\xe4!" => 'ae', # a:
|
|
|
|
"!\xc3!" => 'A', # A~
|
|
|
|
"!\xe3!" => 'a', # a~
|
|
|
|
"!\xc8!" => 'E', # E`
|
|
|
|
"!\xe8!" => 'e', # e`
|
|
|
|
"!\xc9!" => 'E', # E'
|
|
|
|
"!\xe9!" => 'e', # e'
|
|
|
|
"!\xca!" => 'E', # E^
|
|
|
|
"!\xea!" => 'e', # e^
|
|
|
|
"!\xcb!" => 'Ee', # E:
|
|
|
|
"!\xeb!" => 'ee', # e:
|
|
|
|
"!\xcc!" => 'I', # I`
|
|
|
|
"!\xec!" => 'i', # i`
|
|
|
|
"!\xcd!" => 'I', # I'
|
|
|
|
"!\xed!" => 'i', # i'
|
|
|
|
"!\xce!" => 'I', # I^
|
|
|
|
"!\xee!" => 'i', # i^
|
|
|
|
"!\xcf!" => 'Ie', # I:
|
|
|
|
"!\xef!" => 'ie', # i:
|
|
|
|
"!\xd2!" => 'O', # O`
|
|
|
|
"!\xf2!" => 'o', # o`
|
|
|
|
"!\xd3!" => 'O', # O'
|
|
|
|
"!\xf3!" => 'o', # o'
|
|
|
|
"!\xd4!" => 'O', # O^
|
|
|
|
"!\xf4!" => 'o', # o^
|
|
|
|
"!\xd6!" => 'Oe', # O:
|
|
|
|
"!\xf6!" => 'oe', # o:
|
|
|
|
"!\xd5!" => 'O', # O~
|
|
|
|
"!\xf5!" => 'o', # o~
|
|
|
|
"!\xd8!" => 'Oe', # O/
|
|
|
|
"!\xf8!" => 'oe', # o/
|
|
|
|
"!\xd9!" => 'U', # U`
|
|
|
|
"!\xf9!" => 'u', # u`
|
|
|
|
"!\xda!" => 'U', # U'
|
|
|
|
"!\xfa!" => 'u', # u'
|
|
|
|
"!\xdb!" => 'U', # U^
|
|
|
|
"!\xfb!" => 'u', # u^
|
|
|
|
"!\xdc!" => 'Ue', # U:
|
|
|
|
"!\xfc!" => 'ue', # u:
|
|
|
|
"!\xc7!" => 'C', # ,C
|
|
|
|
"!\xe7!" => 'c', # ,c
|
|
|
|
"!\xd1!" => 'N', # N~
|
|
|
|
"!\xf1!" => 'n', # n~
|
|
|
|
"!\xdf!" => 'ss'
|
|
|
|
);
|
|
|
|
$find = array_keys($HighASCII);
|
|
|
|
$replace = array_values($HighASCII);
|
|
|
|
$s = preg_replace($find,$replace,$s);
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
*/
|
2002-08-04 16:20:30 +00:00
|
|
|
function clean_filename($string) {
|
2005-02-10 13:26:41 +00:00
|
|
|
$string = convert_high_ascii($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
|
2005-01-27 07:10:41 +00:00
|
|
|
return $CFG->courselang;
|
2004-02-09 07:31:04 +00:00
|
|
|
|
2004-02-15 14:33:13 +00:00
|
|
|
} else if (!empty($SESSION->lang)) { // Session language can override other settings
|
2005-01-27 07:10:41 +00:00
|
|
|
return $SESSION->lang;
|
2003-01-19 12:32:55 +00:00
|
|
|
|
2004-02-15 14:33:13 +00:00
|
|
|
} else if (!empty($USER->lang)) { // User language can override site language
|
2005-01-27 07:10:41 +00:00
|
|
|
return $USER->lang;
|
2003-01-19 12:32:55 +00:00
|
|
|
|
2002-10-02 10:33:05 +00:00
|
|
|
} else {
|
2005-01-27 07:10:41 +00:00
|
|
|
return $CFG->lang;
|
2002-10-02 10:33:05 +00:00
|
|
|
}
|
|
|
|
}
|
2002-07-04 07:49:38 +00:00
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
2004-10-04 16:44:54 +00:00
|
|
|
* Prints out a translated string.
|
|
|
|
*
|
|
|
|
* Prints out a translated string using the return value from the {@link get_string()} function.
|
|
|
|
*
|
|
|
|
* Example usage of this function when the string is in the moodle.php file:<br>
|
|
|
|
* <code>
|
|
|
|
* echo '<strong>';
|
|
|
|
* print_string('wordforstudent');
|
|
|
|
* echo '</strong>';
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Example usage of this function when the string is not in the moodle.php file:<br>
|
|
|
|
* <code>
|
|
|
|
* echo '<h1>';
|
|
|
|
* print_string('typecourse', 'calendar');
|
|
|
|
* echo '</h1>';
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param string $identifier The key identifier for the localized string
|
|
|
|
* @param string $module The module where the key identifier is stored. If none is specified then moodle.php is used.
|
|
|
|
* @param mixed $a An object, string or number that can be used
|
|
|
|
* within translation strings
|
2004-11-22 18:38:33 +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);
|
|
|
|
}
|
|
|
|
|
2005-06-21 12:30:34 +00:00
|
|
|
/**
|
|
|
|
* fix up the optional data in get_string()/print_string() etc
|
|
|
|
* ensure possible sprintf() format characters are escaped correctly
|
|
|
|
* needs to handle arbitrary strings and objects
|
|
|
|
* @param mixed $a An object, string or number that can be used
|
|
|
|
* @return mixed the supplied parameter 'cleaned'
|
|
|
|
*/
|
2005-06-23 09:04:16 +00:00
|
|
|
function clean_getstring_data( $a ) {
|
2005-06-21 12:30:34 +00:00
|
|
|
if (is_string($a)) {
|
|
|
|
return str_replace( '%','%%',$a );
|
|
|
|
}
|
|
|
|
elseif (is_object($a)) {
|
|
|
|
$a_vars = get_object_vars( $a );
|
|
|
|
$new_a_vars = array();
|
|
|
|
foreach ($a_vars as $fname => $a_var) {
|
2005-06-23 09:04:16 +00:00
|
|
|
$new_a_vars[$fname] = clean_getstring_data( $a_var );
|
2005-06-21 12:30:34 +00:00
|
|
|
}
|
|
|
|
return (object)$new_a_vars;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-23 05:10:21 +00:00
|
|
|
/**
|
2004-10-04 16:44:54 +00:00
|
|
|
* Returns a localized string.
|
|
|
|
*
|
|
|
|
* Returns the translated string specified by $identifier as
|
2004-09-23 05:10:21 +00:00
|
|
|
* for $module. Uses the same format files as STphp.
|
|
|
|
* $a is an object, string or number that can be used
|
|
|
|
* within translation strings
|
|
|
|
*
|
|
|
|
* eg "hello \$a->firstname \$a->lastname"
|
|
|
|
* or "hello \$a"
|
|
|
|
*
|
2004-10-04 16:44:54 +00:00
|
|
|
* If you would like to directly echo the localized string use
|
|
|
|
* the function {@link print_string()}
|
|
|
|
*
|
|
|
|
* Example usage of this function involves finding the string you would
|
|
|
|
* like a local equivalent of and using its identifier and module information
|
|
|
|
* to retrive it.<br>
|
|
|
|
* If you open moodle/lang/en/moodle.php and look near line 1031
|
|
|
|
* you will find a string to prompt a user for their word for student
|
|
|
|
* <code>
|
|
|
|
* $string['wordforstudent'] = 'Your word for Student';
|
|
|
|
* </code>
|
2004-11-22 18:38:33 +00:00
|
|
|
* So if you want to display the string 'Your word for student'
|
|
|
|
* in any language that supports it on your site
|
2004-10-04 16:44:54 +00:00
|
|
|
* you just need to use the identifier 'wordforstudent'
|
|
|
|
* <code>
|
|
|
|
* $mystring = '<strong>'. get_string('wordforstudent') .'</strong>';
|
|
|
|
or
|
|
|
|
* </code>
|
2004-11-22 18:38:33 +00:00
|
|
|
* If the string you want is in another file you'd take a slightly
|
|
|
|
* different approach. Looking in moodle/lang/en/calendar.php you find
|
2004-10-04 16:44:54 +00:00
|
|
|
* around line 75:
|
|
|
|
* <code>
|
|
|
|
* $string['typecourse'] = 'Course event';
|
|
|
|
* </code>
|
2004-11-22 18:38:33 +00:00
|
|
|
* If you want to display the string "Course event" in any language
|
|
|
|
* supported you would use the identifier 'typecourse' and the module 'calendar'
|
2004-10-04 16:44:54 +00:00
|
|
|
* (because it is in the file calendar.php):
|
|
|
|
* <code>
|
|
|
|
* $mystring = '<h1>'. get_string('typecourse', 'calendar') .'</h1>';
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* As a last resort, should the identifier fail to map to a string
|
|
|
|
* the returned string will be [[ $identifier ]]
|
|
|
|
*
|
2004-09-23 05:10:21 +00:00
|
|
|
* @uses $CFG
|
2004-10-04 16:44:54 +00:00
|
|
|
* @param string $identifier The key identifier for the localized string
|
|
|
|
* @param string $module The module where the key identifier is stored. If none is specified then moodle.php is used.
|
|
|
|
* @param mixed $a An object, string or number that can be used
|
|
|
|
* within translation strings
|
|
|
|
* @return string The localized string.
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2004-11-22 18:38:33 +00:00
|
|
|
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)) {
|
2005-07-01 17:21:52 +00:00
|
|
|
if (is_object($course) and isset($course->lang)) {
|
2004-02-15 14:34:13 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2005-06-20 09:15:30 +00:00
|
|
|
// if $a happens to have % in it, double it so sprintf() doesn't break
|
2005-06-23 09:04:16 +00:00
|
|
|
if ($a) {
|
|
|
|
$a = clean_getstring_data( $a );
|
|
|
|
}
|
2005-06-20 09:15:30 +00:00
|
|
|
|
2005-01-10 14:11:13 +00:00
|
|
|
/// Define the two or three major locations of language strings for this module
|
2002-06-27 08:47:27 +00:00
|
|
|
|
2005-07-25 11:26:03 +00:00
|
|
|
if ($module == 'install') {
|
|
|
|
$locations = array( $CFG->dirroot.'/install/lang/', $CFG->dataroot.'/lang/', $CFG->dirroot.'/lang/' );
|
|
|
|
} else {
|
|
|
|
$locations = array( $CFG->dataroot.'/lang/', $CFG->dirroot.'/lang/' );
|
|
|
|
}
|
|
|
|
|
2005-01-10 14:11:13 +00:00
|
|
|
if ($module != 'moodle') {
|
2005-03-14 13:08:16 +00:00
|
|
|
if (strpos($module, 'block_') === 0) { // It's a block lang file
|
|
|
|
$locations[] = $CFG->dirroot .'/blocks/'.substr($module, 6).'/lang/';
|
|
|
|
} else { // It's a normal activity
|
|
|
|
$locations[] = $CFG->dirroot .'/mod/'.$module.'/lang/';
|
|
|
|
}
|
2002-06-27 08:47:27 +00:00
|
|
|
}
|
|
|
|
|
2005-01-10 14:11:13 +00:00
|
|
|
/// First check all the normal locations for the string in the current language
|
2004-05-24 09:19:59 +00:00
|
|
|
|
2005-01-10 14:11:13 +00:00
|
|
|
foreach ($locations as $location) {
|
2005-01-11 12:24:37 +00:00
|
|
|
$langfile = $location.$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
|
|
|
|
2005-01-10 14:11:13 +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
|
|
|
|
2005-01-10 14:11:13 +00:00
|
|
|
/// Is a parent language defined? If so, try to find this string in a parent language file
|
2004-04-01 10:11:20 +00:00
|
|
|
|
2005-01-10 14:11:13 +00:00
|
|
|
foreach ($locations as $location) {
|
2005-01-11 12:24:37 +00:00
|
|
|
$langfile = $location.$lang.'/moodle.php';
|
|
|
|
if (file_exists($langfile)) {
|
|
|
|
if ($result = get_string_from_file('parentlanguage', $langfile, "\$parentlang")) {
|
|
|
|
eval($result);
|
|
|
|
if (!empty($parentlang)) { // found it!
|
|
|
|
$langfile = $location.$parentlang.'/'.$module.'.php';
|
|
|
|
if (file_exists($langfile)) {
|
|
|
|
if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) {
|
|
|
|
eval($result);
|
|
|
|
return $resultstring;
|
|
|
|
}
|
2005-01-10 14:11:13 +00:00
|
|
|
}
|
2003-06-12 03:27:24 +00:00
|
|
|
}
|
2002-06-27 08:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-06-12 03:27:24 +00:00
|
|
|
|
2005-01-10 14:11:13 +00:00
|
|
|
/// Our only remaining option is to try English
|
2003-06-12 03:27:24 +00:00
|
|
|
|
2005-01-10 14:11:13 +00:00
|
|
|
foreach ($locations as $location) {
|
2005-01-11 12:24:37 +00:00
|
|
|
$langfile = $location.'en/'.$module.'.php';
|
2003-06-12 03:27:24 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-10 14:11:13 +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
|
|
|
*/
|
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();
|
|
|
|
|
2005-04-11 06:41:08 +00:00
|
|
|
if ( (!defined('FULLME') || FULLME !== 'cron')
|
|
|
|
&& !empty($CFG->langcache) && file_exists($CFG->dataroot .'/cache/languages')) {
|
|
|
|
// read from cache
|
|
|
|
$lines = file($CFG->dataroot .'/cache/languages');
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$line = trim($line);
|
|
|
|
if (preg_match('/^(\w+)\s+(.+)/', $line, $matches)) {
|
|
|
|
$languages[$matches[1]] = $matches[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($lines); unset($line); unset($matches);
|
|
|
|
return $languages;
|
|
|
|
}
|
|
|
|
|
2003-05-03 06:50:29 +00:00
|
|
|
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) {
|
2005-07-12 11:35:30 +00:00
|
|
|
if (file_exists($CFG->dirroot .'/lang/'. $lang .'/moodle.php')) {
|
|
|
|
include($CFG->dirroot .'/lang/'. $lang .'/moodle.php');
|
|
|
|
$languages[$lang] = $string['thislanguage'] .' ('. $lang .')';
|
|
|
|
unset($string);
|
|
|
|
}
|
2003-05-03 06:50:29 +00:00
|
|
|
}
|
2002-09-14 03:43:24 +00:00
|
|
|
}
|
2005-04-11 06:41:08 +00:00
|
|
|
if ( defined('FULLME') && FULLME === 'cron' && !empty($CFG->langcache)) {
|
|
|
|
if ($file = fopen($CFG->dataroot .'/cache/languages', 'w')) {
|
|
|
|
foreach ($languages as $key => $value) {
|
|
|
|
fwrite($file, "$key $value\n");
|
|
|
|
}
|
|
|
|
fclose($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2005-07-12 03:09:25 +00:00
|
|
|
* @return array
|
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;
|
|
|
|
}
|
|
|
|
|
2005-02-10 10:28:27 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of valid and compatible themes
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function get_list_of_themes() {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$themes = array();
|
|
|
|
|
|
|
|
if (!empty($CFG->themelist)) { // use admin's list of themes
|
|
|
|
$themelist = explode(',', $CFG->themelist);
|
|
|
|
} else {
|
|
|
|
$themelist = get_list_of_plugins("theme");
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($themelist as $key => $theme) {
|
|
|
|
if (!file_exists("$CFG->dirroot/theme/$theme/config.php")) { // bad folder
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
unset($THEME); // Note this is not the global one!! :-)
|
2005-02-13 22:40:21 +00:00
|
|
|
include("$CFG->dirroot/theme/$theme/config.php");
|
2005-02-10 10:28:27 +00:00
|
|
|
if (!isset($THEME->sheets)) { // Not a valid 1.5 theme
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$themes[$theme] = $theme;
|
|
|
|
}
|
|
|
|
asort($themes);
|
|
|
|
|
|
|
|
return $themes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
2005-07-12 03:09:25 +00:00
|
|
|
* @return array
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2004-11-22 18:38:33 +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;
|
|
|
|
}
|
|
|
|
|
2005-04-10 17:35:17 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of picture names in the current language
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
2005-07-12 03:09:25 +00:00
|
|
|
* @return array
|
2005-04-10 17:35:17 +00:00
|
|
|
*/
|
|
|
|
function get_list_of_timezones() {
|
|
|
|
global $CFG;
|
|
|
|
|
2005-04-10 17:51:48 +00:00
|
|
|
$timezones = array();
|
|
|
|
|
2005-05-20 00:50:45 +00:00
|
|
|
if ($rawtimezones = get_records_sql('SELECT MAX(id), name FROM '.$CFG->prefix.'timezone GROUP BY name')) {
|
2005-04-10 17:51:48 +00:00
|
|
|
foreach($rawtimezones as $timezone) {
|
|
|
|
if (!empty($timezone->name)) {
|
|
|
|
$timezones[$timezone->name] = get_string(strtolower($timezone->name), 'timezones');
|
|
|
|
if (substr($timezones[$timezone->name], 0, 1) == '[') { // No translation found
|
|
|
|
$timezones[$timezone->name] = $timezone->name;
|
|
|
|
}
|
2005-04-10 17:35:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-20 00:50:45 +00:00
|
|
|
asort($timezones);
|
2005-04-10 17:35:17 +00:00
|
|
|
|
|
|
|
for ($i = -13; $i <= 13; $i += .5) {
|
|
|
|
$tzstring = 'GMT';
|
|
|
|
if ($i < 0) {
|
2005-04-10 17:51:48 +00:00
|
|
|
$timezones[sprintf("%.1f", $i)] = $tzstring . $i;
|
2005-04-10 17:35:17 +00:00
|
|
|
} else if ($i > 0) {
|
2005-04-10 17:51:48 +00:00
|
|
|
$timezones[sprintf("%.1f", $i)] = $tzstring . '+' . $i;
|
2005-04-10 17:35:17 +00:00
|
|
|
} else {
|
2005-04-10 17:51:48 +00:00
|
|
|
$timezones[sprintf("%.1f", $i)] = $tzstring;
|
2005-04-10 17:35:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-10 17:51:48 +00:00
|
|
|
return $timezones;
|
2005-04-10 17:35:17 +00:00
|
|
|
}
|
|
|
|
|
2005-07-22 17:30:05 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of currencies in the current language
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @uses $USER
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function get_list_of_currencies() {
|
|
|
|
global $CFG, $USER;
|
|
|
|
|
|
|
|
$lang = current_language();
|
|
|
|
|
|
|
|
if (!file_exists($CFG->dirroot .'/lang/'. $lang .'/currencies.php')) {
|
|
|
|
if ($parentlang = get_string('parentlanguage')) {
|
|
|
|
if (file_exists($CFG->dirroot .'/lang/'. $parentlang .'/currencies.php')) {
|
|
|
|
$lang = $parentlang;
|
|
|
|
} else {
|
|
|
|
$lang = 'en'; // currencies.php must exist in this pack
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$lang = 'en'; // currencies.php must exist in this pack
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
include($CFG->dirroot .'/lang/'. $lang .'/currencies.php');
|
|
|
|
|
|
|
|
if (!empty($string)) {
|
|
|
|
asort($string);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-10 17:51:48 +00:00
|
|
|
|
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 ?
|
2005-07-12 02:06:33 +00:00
|
|
|
* @param bool $include ?
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return ?
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-11-22 18:38:33 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-11-18 02:55:06 +00:00
|
|
|
/**
|
2005-07-12 02:06:33 +00:00
|
|
|
* Function to raise the memory limit to a new value.
|
|
|
|
* Will respect the memory limit if it is higher, thus allowing
|
|
|
|
* settings in php.ini, apache conf or command line switches
|
|
|
|
* to override it
|
|
|
|
*
|
|
|
|
* The memory limit should be expressed with a string (eg:'64M')
|
|
|
|
*
|
|
|
|
* @param string $newlimit the new memory limit
|
|
|
|
* @return bool
|
|
|
|
*/
|
2004-11-18 02:55:06 +00:00
|
|
|
function raise_memory_limit ($newlimit) {
|
|
|
|
|
2004-11-22 18:38:33 +00:00
|
|
|
if (empty($newlimit)) {
|
2004-11-18 02:55:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2004-11-22 18:38:33 +00:00
|
|
|
|
2004-11-20 10:38:43 +00:00
|
|
|
$cur = @ini_get('memory_limit');
|
|
|
|
if (empty($cur)) {
|
|
|
|
// if php is compiled without --enable-memory-limits
|
|
|
|
// apparently memory_limit is set to ''
|
|
|
|
$cur=0;
|
|
|
|
} else {
|
|
|
|
if ($cur == -1){
|
|
|
|
return true; // unlimited mem!
|
|
|
|
}
|
2004-11-24 03:56:39 +00:00
|
|
|
$cur = get_real_size($cur);
|
2004-11-20 10:38:43 +00:00
|
|
|
}
|
2004-11-22 18:38:33 +00:00
|
|
|
|
2004-11-24 03:56:39 +00:00
|
|
|
$new = get_real_size($newlimit);
|
2004-11-18 02:55:06 +00:00
|
|
|
if ($new > $cur) {
|
|
|
|
ini_set('memory_limit', $newlimit);
|
2004-11-22 18:38:33 +00:00
|
|
|
return true;
|
2004-11-18 02:55:06 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2004-11-22 18:38:33 +00:00
|
|
|
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-10-10 01:07:47 +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
|
|
|
*/
|
|
|
|
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.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
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);
|
2005-03-10 06:04:29 +00:00
|
|
|
while (false !== ($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.
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2004-09-22 14:39:15 +00:00
|
|
|
function check_php_version($version='4.1.0') {
|
2005-05-09 17:24:03 +00:00
|
|
|
return (version_compare(phpversion(), $version) >= 0);
|
2002-08-23 02:14:19 +00:00
|
|
|
}
|
|
|
|
|
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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2004-11-22 18:38:33 +00:00
|
|
|
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-11-22 18:38:33 +00:00
|
|
|
if (substr_count($agent, 'Camino')) {
|
|
|
|
// MacOS X Camino support
|
|
|
|
$version = 20041110;
|
2003-10-29 08:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// the proper string - Gecko/CCYYMMDD Vendor/Version
|
2005-09-23 18:38:50 +00:00
|
|
|
// Faster version and work-a-round No IDN problem.
|
|
|
|
if (preg_match("/Gecko\/([0-9]+)/i", $agent, $match)) {
|
|
|
|
if ($match[1] > $version) {
|
2003-10-29 08:06:11 +00:00
|
|
|
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 ?
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 05:10:21 +00:00
|
|
|
* @todo Finish documenting this function
|
|
|
|
*/
|
2004-11-22 18:38:33 +00:00
|
|
|
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-11-22 18:38:33 +00:00
|
|
|
* Determines if the HTML editor is enabled.
|
2004-09-25 01:29:37 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
2004-11-22 18:38:33 +00:00
|
|
|
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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
2004-11-22 18:38:33 +00:00
|
|
|
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
|
2004-09-29 18:56:50 +00:00
|
|
|
* @uses HOURSECS
|
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');
|
2005-04-23 15:16:43 +00:00
|
|
|
if (!get_field('config', 'value', 'name', 'locale')) { // Make SURE there isn't one already
|
|
|
|
set_config('locale', $CFG->locale); // Cache it to save lookups in future
|
|
|
|
}
|
2004-06-21 13:07:44 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
*/
|
2004-11-22 18:38:33 +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;
|
|
|
|
}
|
|
|
|
|
2005-04-14 17:03:11 +00:00
|
|
|
/*
|
|
|
|
* Given some text (which may contain HTML) and an ideal length,
|
|
|
|
* this function truncates the text neatly on a word boundary if possible
|
|
|
|
*/
|
|
|
|
function shorten_text($text, $ideal=30) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
$tag = false;
|
|
|
|
$length = strlen($text);
|
|
|
|
$count = 0;
|
|
|
|
$stopzone = false;
|
|
|
|
$truncate = 0;
|
|
|
|
|
|
|
|
if ($length <= $ideal) {
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($i=0; $i<$length; $i++) {
|
|
|
|
$char = $text[$i];
|
|
|
|
|
|
|
|
switch ($char) {
|
|
|
|
case "<":
|
|
|
|
$tag = true;
|
|
|
|
break;
|
|
|
|
case ">":
|
|
|
|
$tag = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!$tag) {
|
|
|
|
if ($stopzone) {
|
|
|
|
if ($char == '.' or $char == ' ') {
|
|
|
|
$truncate = $i+1;
|
|
|
|
break 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!$stopzone) {
|
|
|
|
if ($count > $ideal) {
|
|
|
|
$stopzone = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$truncate) {
|
|
|
|
$truncate = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ellipse = ($truncate < $length) ? '...' : '';
|
|
|
|
|
|
|
|
return substr($text, 0, $truncate).$ellipse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-29 18:56:50 +00:00
|
|
|
* @uses WEEKSECS
|
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
|
|
|
|
*/
|
2004-11-22 18:38:33 +00:00
|
|
|
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
|
|
|
*/
|
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
|
|
|
|
*/
|
2004-11-22 18:38:33 +00:00
|
|
|
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-11-22 18:38:33 +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
|
|
|
|
*/
|
2004-11-22 18:38:33 +00:00
|
|
|
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-11-22 05:22:20 +00:00
|
|
|
function site_scale_used($scaleid,&$courses) {
|
2004-05-16 00:36:00 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$return = 0;
|
|
|
|
|
2004-11-22 05:22:20 +00:00
|
|
|
if (!is_array($courses) || count($courses) == 0) {
|
|
|
|
$courses = get_courses("all",false,"c.id,c.shortname");
|
|
|
|
}
|
|
|
|
|
2004-05-16 00:36:00 +00:00
|
|
|
if (!empty($scaleid)) {
|
2004-11-22 05:22:20 +00:00
|
|
|
if (is_array($courses) && count($courses) > 0) {
|
2004-05-16 00:36:00 +00:00
|
|
|
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
|
2005-07-12 02:06:33 +00:00
|
|
|
* @return bool
|
2004-09-25 01:29:37 +00:00
|
|
|
*/
|
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) {
|
2005-03-10 02:36:51 +00:00
|
|
|
break;
|
2004-07-08 07:52:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
2005-07-14 19:44:42 +00:00
|
|
|
/**
|
|
|
|
* This function sets the $HTTPSPAGEREQUIRED global
|
|
|
|
* (used in some parts of moodle to change some links)
|
|
|
|
* and calculate the proper wwwroot to be used
|
|
|
|
*
|
|
|
|
* By using this function properly, we can ensure 100% https-ized pages
|
|
|
|
* at our entire discretion (login, forgot_password, change_password)
|
|
|
|
*/
|
|
|
|
function httpsrequired() {
|
|
|
|
|
|
|
|
global $CFG, $HTTPSPAGEREQUIRED;
|
|
|
|
|
|
|
|
if (!empty($CFG->loginhttps)) {
|
|
|
|
$HTTPSPAGEREQUIRED = true;
|
2005-08-09 15:16:23 +00:00
|
|
|
$CFG->httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
|
2005-07-14 19:44:42 +00:00
|
|
|
} else {
|
|
|
|
$CFG->httpswwwroot = $CFG->wwwroot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2005-02-19 14:48:44 +00:00
|
|
|
function mtrace($string, $eol="\n", $sleep=0) {
|
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();
|
2005-02-19 14:48:44 +00:00
|
|
|
|
|
|
|
//delay to keep message on user's screen in case of subsequent redirect
|
|
|
|
if ($sleep) {
|
|
|
|
sleep($sleep);
|
|
|
|
}
|
2004-08-05 17:08:46 +00:00
|
|
|
}
|
|
|
|
|
2004-10-16 11:39:52 +00:00
|
|
|
//Replace 1 or more slashes or backslashes to 1 slash
|
2004-10-08 06:11:17 +00:00
|
|
|
function cleardoubleslashes ($path) {
|
|
|
|
return preg_replace('/(\/|\\\){1,}/','/',$path);
|
|
|
|
}
|
|
|
|
|
|
|
|
function zip_files ($originalfiles, $destination) {
|
2004-10-10 01:07:47 +00:00
|
|
|
//Zip an array of files/dirs to a destination zip file
|
|
|
|
//Both parameters must be FULL paths to the files/dirs
|
2004-10-08 06:11:17 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
//Extract everything from destination
|
|
|
|
$path_parts = pathinfo(cleardoubleslashes($destination));
|
2004-10-10 01:07:47 +00:00
|
|
|
$destpath = $path_parts["dirname"]; //The path of the zip file
|
|
|
|
$destfilename = $path_parts["basename"]; //The name of the zip file
|
|
|
|
$extension = $path_parts["extension"]; //The extension of the file
|
2004-10-08 06:11:17 +00:00
|
|
|
|
|
|
|
//If no file, error
|
2004-10-10 01:07:47 +00:00
|
|
|
if (empty($destfilename)) {
|
2004-10-08 06:11:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//If no extension, add it
|
2004-11-22 18:38:33 +00:00
|
|
|
if (empty($extension)) {
|
2004-10-08 06:11:17 +00:00
|
|
|
$extension = 'zip';
|
2004-10-10 01:07:47 +00:00
|
|
|
$destfilename = $destfilename.'.'.$extension;
|
2004-10-08 06:11:17 +00:00
|
|
|
}
|
|
|
|
|
2004-10-10 01:07:47 +00:00
|
|
|
//Check destination path exists
|
|
|
|
if (!is_dir($destpath)) {
|
2004-10-08 06:11:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-10-10 01:07:47 +00:00
|
|
|
//Check destination path is writable. TODO!!
|
2004-10-08 06:11:17 +00:00
|
|
|
|
2004-10-10 01:07:47 +00:00
|
|
|
//Clean destination filename
|
|
|
|
$destfilename = clean_filename($destfilename);
|
|
|
|
|
|
|
|
//Now check and prepare every file
|
|
|
|
$files = array();
|
|
|
|
$origpath = NULL;
|
|
|
|
|
|
|
|
foreach ($originalfiles as $file) { //Iterate over each file
|
|
|
|
//Check for every file
|
|
|
|
$tempfile = cleardoubleslashes($file); // no doubleslashes!
|
|
|
|
//Calculate the base path for all files if it isn't set
|
|
|
|
if ($origpath === NULL) {
|
|
|
|
$origpath = rtrim(cleardoubleslashes(dirname($tempfile)), "/");
|
|
|
|
}
|
|
|
|
//See if the file is readable
|
|
|
|
if (!is_readable($tempfile)) { //Is readable
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//See if the file/dir is in the same directory than the rest
|
|
|
|
if (rtrim(cleardoubleslashes(dirname($tempfile)), "/") != $origpath) {
|
|
|
|
continue;
|
2004-10-08 06:11:17 +00:00
|
|
|
}
|
2004-10-10 01:07:47 +00:00
|
|
|
//Add the file to the array
|
|
|
|
$files[] = $tempfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Everything is ready:
|
|
|
|
// -$origpath is the path where ALL the files to be compressed reside (dir).
|
|
|
|
// -$destpath is the destination path where the zip file will go (dir).
|
|
|
|
// -$files is an array of files/dirs to compress (fullpath)
|
|
|
|
// -$destfilename is the name of the zip file (without path)
|
|
|
|
|
|
|
|
//print_object($files); //Debug
|
|
|
|
|
|
|
|
if (empty($CFG->zip)) { // Use built-in php-based zip function
|
|
|
|
|
2004-10-08 06:11:17 +00:00
|
|
|
include_once("$CFG->libdir/pclzip/pclzip.lib.php");
|
2004-10-10 01:07:47 +00:00
|
|
|
$archive = new PclZip(cleardoubleslashes("$destpath/$destfilename"));
|
|
|
|
if (($list = $archive->create($files, PCLZIP_OPT_REMOVE_PATH,$origpath) == 0)) {
|
|
|
|
notice($archive->errorInfo(true));
|
|
|
|
return false;
|
2004-10-08 06:11:17 +00:00
|
|
|
}
|
2004-10-10 01:07:47 +00:00
|
|
|
|
2004-10-08 06:11:17 +00:00
|
|
|
} else { // Use external zip program
|
2004-10-10 01:07:47 +00:00
|
|
|
|
|
|
|
$filestozip = "";
|
|
|
|
foreach ($files as $filetozip) {
|
|
|
|
$filestozip .= escapeshellarg(basename($filetozip));
|
|
|
|
$filestozip .= " ";
|
2004-10-08 06:11:17 +00:00
|
|
|
}
|
2004-10-10 01:07:47 +00:00
|
|
|
//Construct the command
|
|
|
|
$separator = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? ' &' : ' ;';
|
|
|
|
$command = 'cd '.escapeshellarg($origpath).$separator.
|
|
|
|
escapeshellarg($CFG->zip).' -r '.
|
|
|
|
escapeshellarg(cleardoubleslashes("$destpath/$destfilename")).' '.$filestozip;
|
2004-10-10 23:25:57 +00:00
|
|
|
//All converted to backslashes in WIN
|
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
|
|
|
|
$command = str_replace('/','\\',$command);
|
|
|
|
}
|
2004-10-08 06:11:17 +00:00
|
|
|
Exec($command);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-10-16 16:33:40 +00:00
|
|
|
function unzip_file ($zipfile, $destination = '', $showstatus = true) {
|
2004-10-10 21:46:13 +00:00
|
|
|
//Unzip one zip file to a destination dir
|
|
|
|
//Both parameters must be FULL paths
|
|
|
|
//If destination isn't specified, it will be the
|
|
|
|
//SAME directory where the zip file resides.
|
|
|
|
|
|
|
|
global $CFG;
|
2004-11-22 18:38:33 +00:00
|
|
|
|
2004-10-10 21:46:13 +00:00
|
|
|
//Extract everything from zipfile
|
|
|
|
$path_parts = pathinfo(cleardoubleslashes($zipfile));
|
|
|
|
$zippath = $path_parts["dirname"]; //The path of the zip file
|
|
|
|
$zipfilename = $path_parts["basename"]; //The name of the zip file
|
|
|
|
$extension = $path_parts["extension"]; //The extension of the file
|
|
|
|
|
|
|
|
//If no file, error
|
|
|
|
if (empty($zipfilename)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//If no extension, error
|
|
|
|
if (empty($extension)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//If no destination, passed let's go with the same directory
|
|
|
|
if (empty($destination)) {
|
|
|
|
$destination = $zippath;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Clear $destination
|
|
|
|
$destpath = rtrim(cleardoubleslashes($destination), "/");
|
|
|
|
|
|
|
|
//Check destination path exists
|
|
|
|
if (!is_dir($destpath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check destination path is writable. TODO!!
|
|
|
|
|
|
|
|
//Everything is ready:
|
|
|
|
// -$zippath is the path where the zip file resides (dir)
|
|
|
|
// -$zipfilename is the name of the zip file (without path)
|
|
|
|
// -$destpath is the destination path where the zip file will uncompressed (dir)
|
|
|
|
|
|
|
|
if (empty($CFG->unzip)) { // Use built-in php-based unzip function
|
|
|
|
|
|
|
|
include_once("$CFG->libdir/pclzip/pclzip.lib.php");
|
|
|
|
$archive = new PclZip(cleardoubleslashes("$zippath/$zipfilename"));
|
|
|
|
if (!$list = $archive->extract(PCLZIP_OPT_PATH, $destpath,
|
2004-10-17 22:28:53 +00:00
|
|
|
PCLZIP_CB_PRE_EXTRACT, 'unzip_cleanfilename')) {
|
2004-10-10 21:46:13 +00:00
|
|
|
notice($archive->errorInfo(true));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else { // Use external unzip program
|
|
|
|
|
|
|
|
$separator = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? ' &' : ' ;';
|
|
|
|
$redirection = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? '' : ' 2>&1';
|
|
|
|
|
|
|
|
$command = 'cd '.escapeshellarg($zippath).$separator.
|
|
|
|
escapeshellarg($CFG->unzip).' -o '.
|
|
|
|
escapeshellarg(cleardoubleslashes("$zippath/$zipfilename")).' -d '.
|
|
|
|
escapeshellarg($destpath).$redirection;
|
2004-10-10 23:25:57 +00:00
|
|
|
//All converted to backslashes in WIN
|
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
|
|
|
|
$command = str_replace('/','\\',$command);
|
|
|
|
}
|
2004-10-10 21:46:13 +00:00
|
|
|
Exec($command,$list);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Display some info about the unzip execution
|
2004-10-16 16:33:40 +00:00
|
|
|
if ($showstatus) {
|
|
|
|
unzip_show_status($list,$destpath);
|
|
|
|
}
|
2004-11-22 18:38:33 +00:00
|
|
|
|
2004-10-10 21:46:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-10-17 22:28:53 +00:00
|
|
|
function unzip_cleanfilename ($p_event, &$p_header) {
|
2004-10-10 21:46:13 +00:00
|
|
|
//This function is used as callback in unzip_file() function
|
2004-10-17 22:28:53 +00:00
|
|
|
//to clean illegal characters for given platform and to prevent directory traversal.
|
|
|
|
//Produces the same result as info-zip unzip.
|
2004-10-17 23:44:16 +00:00
|
|
|
$p_header['filename'] = ereg_replace('[[:cntrl:]]', '', $p_header['filename']); //strip control chars first!
|
2004-10-17 22:28:53 +00:00
|
|
|
$p_header['filename'] = ereg_replace('\.\.+', '', $p_header['filename']); //directory traversal protection
|
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
|
|
|
|
$p_header['filename'] = ereg_replace('[:*"?<>|]', '_', $p_header['filename']); //replace illegal chars
|
|
|
|
$p_header['filename'] = ereg_replace('^([a-zA-Z])_', '\1:', $p_header['filename']); //repair drive letter
|
2004-10-10 21:46:13 +00:00
|
|
|
} else {
|
2004-10-17 22:28:53 +00:00
|
|
|
//Add filtering for other systems here
|
|
|
|
// BSD: none (tested)
|
|
|
|
// Linux: ??
|
|
|
|
// MacosX: ??
|
2004-11-22 18:38:33 +00:00
|
|
|
}
|
2004-10-17 22:28:53 +00:00
|
|
|
$p_header['filename'] = cleardoubleslashes($p_header['filename']); //normalize the slashes/backslashes
|
|
|
|
return 1;
|
2004-10-10 21:46:13 +00:00
|
|
|
}
|
|
|
|
|
2004-10-10 22:48:59 +00:00
|
|
|
function unzip_show_status ($list,$removepath) {
|
2004-10-10 21:46:13 +00:00
|
|
|
//This function shows the results of the unzip execution
|
|
|
|
//depending of the value of the $CFG->zip, results will be
|
|
|
|
//text or an array of files.
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (empty($CFG->unzip)) { // Use built-in php-based zip function
|
2004-10-10 22:48:59 +00:00
|
|
|
$strname = get_string("name");
|
|
|
|
$strsize = get_string("size");
|
|
|
|
$strmodified = get_string("modified");
|
|
|
|
$strstatus = get_string("status");
|
2005-03-13 13:50:23 +00:00
|
|
|
echo "<table cellpadding=\"4\" cellspacing=\"2\" border=\"0\" width=\"640\">";
|
|
|
|
echo "<tr><th class=\"header\" align=\"left\">$strname</th>";
|
|
|
|
echo "<th class=\"header\" align=\"right\">$strsize</th>";
|
|
|
|
echo "<th class=\"header\" align=\"right\">$strmodified</th>";
|
|
|
|
echo "<th class=\"header\" align=\"right\">$strstatus</th></tr>";
|
2004-10-10 21:46:13 +00:00
|
|
|
foreach ($list as $item) {
|
|
|
|
echo "<tr>";
|
2004-10-10 22:48:59 +00:00
|
|
|
$item['filename'] = str_replace(cleardoubleslashes($removepath).'/', "", $item['filename']);
|
2004-10-10 21:46:13 +00:00
|
|
|
print_cell("left", $item['filename']);
|
|
|
|
if (! $item['folder']) {
|
|
|
|
print_cell("right", display_size($item['size']));
|
|
|
|
} else {
|
|
|
|
echo "<td> </td>";
|
|
|
|
}
|
|
|
|
$filedate = userdate($item['mtime'], get_string("strftimedatetime"));
|
|
|
|
print_cell("right", $filedate);
|
|
|
|
print_cell("right", $item['status']);
|
|
|
|
echo "</tr>";
|
|
|
|
}
|
|
|
|
echo "</table>";
|
|
|
|
|
|
|
|
} else { // Use external zip program
|
|
|
|
print_simple_box_start("center");
|
2005-03-13 13:50:23 +00:00
|
|
|
echo "<pre>";
|
2004-10-10 21:46:13 +00:00
|
|
|
foreach ($list as $item) {
|
2004-10-16 11:39:52 +00:00
|
|
|
echo str_replace(cleardoubleslashes($removepath.'/'), '', $item).'<br />';
|
2004-10-10 21:46:13 +00:00
|
|
|
}
|
2005-03-13 13:50:23 +00:00
|
|
|
echo "</pre>";
|
2004-10-10 21:46:13 +00:00
|
|
|
print_simple_box_end();
|
|
|
|
}
|
|
|
|
}
|
2004-09-23 05:10:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns most reliable client address
|
2004-11-22 18:38:33 +00:00
|
|
|
*
|
2004-09-25 01:29:37 +00:00
|
|
|
* @return string The remote IP address
|
2004-09-23 05:10:21 +00:00
|
|
|
*/
|
|
|
|
function getremoteaddr() {
|
2005-04-01 07:40:07 +00:00
|
|
|
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
2005-08-18 01:44:47 +00:00
|
|
|
return cleanremoteaddr($_SERVER['HTTP_CLIENT_IP']);
|
2005-04-01 07:40:07 +00:00
|
|
|
}
|
|
|
|
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
2005-08-18 01:44:47 +00:00
|
|
|
return cleanremoteaddr($_SERVER['HTTP_X_FORWARDED_FOR']);
|
2005-04-01 07:40:07 +00:00
|
|
|
}
|
|
|
|
if (!empty($_SERVER['REMOTE_ADDR'])) {
|
2005-08-18 01:44:47 +00:00
|
|
|
return cleanremoteaddr($_SERVER['REMOTE_ADDR']);
|
2005-04-01 07:40:07 +00:00
|
|
|
}
|
|
|
|
return '';
|
2004-11-22 18:38:33 +00:00
|
|
|
}
|
2004-08-05 17:08:46 +00:00
|
|
|
|
2005-08-18 01:44:47 +00:00
|
|
|
/**
|
|
|
|
* Cleans a remote address ready to put into the log table
|
|
|
|
*/
|
|
|
|
function cleanremoteaddr($addr) {
|
2005-09-08 23:04:23 +00:00
|
|
|
$originaladdr = $addr;
|
|
|
|
$matches = array();
|
|
|
|
// first get all things that look like IP addresses.
|
|
|
|
if (!preg_match_all('/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/',$addr,$matches,PREG_SET_ORDER)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$goodmatches = array();
|
|
|
|
$lanmatches = array();
|
|
|
|
foreach ($matches as $match) {
|
|
|
|
// print_r($match);
|
|
|
|
// check to make sure it's not an internal address.
|
|
|
|
// the following are reserved for private lans...
|
|
|
|
// 10.0.0.0 - 10.255.255.255
|
|
|
|
// 172.16.0.0 - 172.31.255.255
|
|
|
|
// 192.168.0.0 - 192.168.255.255
|
|
|
|
// 169.254.0.0 -169.254.255.255
|
|
|
|
$bits = explode('.',$match[0]);
|
|
|
|
if (count($bits) != 4) {
|
|
|
|
// weird, preg match shouldn't give us it.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (($bits[0] == 10)
|
|
|
|
|| ($bits[0] == 172 && $bits[1] >= 16 && $bits[1] <= 31)
|
|
|
|
|| ($bits[0] == 192 && $bits[1] == 168)
|
|
|
|
|| ($bits[0] == 169 && $bits[1] == 254)) {
|
|
|
|
$lanmatches[] = $match[0];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// finally, it's ok
|
|
|
|
$goodmatches[] = $match[0];
|
|
|
|
}
|
|
|
|
if (!count($goodmatches)) {
|
|
|
|
// perhaps we have a lan match, it's probably better to return that.
|
|
|
|
if (!count($lanmatches)) {
|
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return array_pop($lanmatches);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count($goodmatches) == 1) {
|
|
|
|
return $goodmatches[0];
|
|
|
|
}
|
|
|
|
error_log("NOTICE: cleanremoteaddr gives us something funny: $originaladdr had ".count($goodmatches)." matches");
|
|
|
|
// we need to return something, so
|
|
|
|
return array_pop($goodmatches);
|
2005-08-18 01:44:47 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-16 22:15:36 +00:00
|
|
|
/**
|
|
|
|
* The clone keyword is only supported from PHP 5 onwards.
|
|
|
|
* The behaviour of $obj2 = $obj1 differs fundamentally
|
|
|
|
* between PHP 4 and PHP 5. In PHP 4 a copy of $obj1 was
|
|
|
|
* created, in PHP 5 $obj1 is referenced. To create a copy
|
|
|
|
* in PHP 5 the clone keyword was introduced. This function
|
|
|
|
* simulates this behaviour for PHP < 5.0.0.
|
|
|
|
* See also: http://mjtsai.com/blog/2004/07/15/php-5-object-references/
|
|
|
|
*
|
|
|
|
* @param object $obj
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
if(!check_php_version('5.0.0')) {
|
|
|
|
// the eval is needed to prevent PHP 5 from getting a parse error!
|
|
|
|
eval('
|
|
|
|
function clone($obj) {
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2004-12-30 12:54:22 +00:00
|
|
|
/**
|
2005-02-25 06:08:40 +00:00
|
|
|
* If new messages are waiting for the current user, then return
|
2004-12-30 12:54:22 +00:00
|
|
|
* Javascript code to create a popup window
|
|
|
|
*
|
|
|
|
* @return string Javascript code
|
|
|
|
*/
|
|
|
|
function message_popup_window() {
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
$popuplimit = 30; // Minimum seconds between popups
|
|
|
|
|
|
|
|
if (!defined('MESSAGE_WINDOW')) {
|
|
|
|
if (isset($USER->id)) {
|
|
|
|
if (!isset($USER->message_lastpopup)) {
|
|
|
|
$USER->message_lastpopup = 0;
|
|
|
|
}
|
|
|
|
if ((time() - $USER->message_lastpopup) > $popuplimit) { /// It's been long enough
|
|
|
|
if (get_user_preferences('message_showmessagewindow', 1) == 1) {
|
|
|
|
if (count_records_select('message', 'useridto = \''.$USER->id.'\' AND timecreated > \''.$USER->message_lastpopup.'\'')) {
|
|
|
|
$USER->message_lastpopup = time();
|
|
|
|
return '<script language="JavaScript" type="text/javascript">'."\n openpopup('/message/index.php', 'message', 'menubar=0,location=0,scrollbars,status,resizable,width=400,height=500', 0);\n</script>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2005-02-02 02:22:56 +00:00
|
|
|
// Used to make sure that $min <= $value <= $max
|
|
|
|
function bounded_number($min, $value, $max) {
|
|
|
|
if($value < $min) {
|
|
|
|
return $min;
|
|
|
|
}
|
|
|
|
if($value > $max) {
|
|
|
|
return $max;
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2005-04-01 10:00:18 +00:00
|
|
|
|
2005-04-06 07:34:05 +00:00
|
|
|
/**
|
|
|
|
*** get_performance_info() pairs up with init_performance_info()
|
|
|
|
*** loaded in setup.php. Returns an array with 'html' and 'txt'
|
|
|
|
*** values ready for use, and each of the individual stats provided
|
|
|
|
*** separately as well.
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
function get_performance_info() {
|
|
|
|
global $CFG, $PERF;
|
2005-04-01 10:00:18 +00:00
|
|
|
|
2005-04-06 07:34:05 +00:00
|
|
|
$info = array();
|
2005-04-16 17:02:05 +00:00
|
|
|
$info['html'] = ''; // holds userfriendly HTML representation
|
|
|
|
$info['txt'] = me() . ' '; // holds log-friendly representation
|
2005-04-01 10:00:18 +00:00
|
|
|
|
2005-04-06 07:34:05 +00:00
|
|
|
$info['realtime'] = microtime_diff($PERF->starttime, microtime());
|
|
|
|
|
|
|
|
$info['html'] .= '<span class="timeused">'.$info['realtime'].' secs</span> ';
|
|
|
|
$info['txt'] .= 'time: '.$info['realtime'].'s ';
|
2005-04-01 10:00:18 +00:00
|
|
|
|
|
|
|
if (function_exists('memory_get_usage')) {
|
2005-04-06 07:34:05 +00:00
|
|
|
$info['memory_total'] = memory_get_usage();
|
|
|
|
$info['memory_growth'] = memory_get_usage() - $PERF->startmemory;
|
|
|
|
$info['html'] .= '<span class="memoryused">RAM: '.display_size($info['memory_total']).'</span> ';
|
|
|
|
$info['txt'] .= 'memory_total: '.$info['memory_total'].'B (' . display_size($info['memory_total']).') memory_growth: '.$info['memory_growth'].'B ('.display_size($info['memory_growth']).') ';
|
2005-04-01 10:00:18 +00:00
|
|
|
}
|
2005-04-06 07:34:05 +00:00
|
|
|
|
|
|
|
$inc = get_included_files();
|
|
|
|
//error_log(print_r($inc,1));
|
|
|
|
$info['includecount'] = count($inc);
|
|
|
|
$info['html'] .= '<span class="included">Included '.$info['includecount'].' files</span> ';
|
|
|
|
$info['txt'] .= 'includecount: '.$info['includecount'].' ';
|
|
|
|
|
|
|
|
if (!empty($PERF->dbqueries)) {
|
|
|
|
$info['dbqueries'] = $PERF->dbqueries;
|
2005-04-07 00:11:28 +00:00
|
|
|
$info['html'] .= '<span class="dbqueries">DB queries '.$info['dbqueries'].'</span> ';
|
2005-04-06 07:34:05 +00:00
|
|
|
$info['txt'] .= 'dbqueries: '.$info['dbqueries'].' ';
|
2005-04-01 10:00:18 +00:00
|
|
|
}
|
|
|
|
|
2005-04-06 07:34:05 +00:00
|
|
|
if (!empty($PERF->logwrites)) {
|
|
|
|
$info['logwrites'] = $PERF->logwrites;
|
|
|
|
$info['html'] .= '<span class="logwrites">Log writes '.$info['logwrites'].'</span> ';
|
|
|
|
$info['txt'] .= 'logwrites: '.$info['logwrites'].' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (function_exists('posix_times')) {
|
|
|
|
$ptimes = posix_times();
|
|
|
|
foreach ($ptimes as $key => $val) {
|
|
|
|
$info[$key] = $ptimes[$key] - $PERF->startposixtimes[$key];
|
|
|
|
}
|
|
|
|
$info['html'] .= "<span class=\"posixtimes\">ticks: $info[ticks] user: $info[utime] sys: $info[stime] cuser: $info[cutime] csys: $info[cstime]</span> ";
|
|
|
|
$info['txt'] .= "ticks: $info[ticks] user: $info[utime] sys: $info[stime] cuser: $info[cutime] csys: $info[cstime] ";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the load average for the last minute
|
|
|
|
// /proc will only work under some linux configurations
|
|
|
|
// while uptime is there under MacOSX/Darwin and other unices
|
|
|
|
if (is_readable('/proc/loadavg') && $loadavg = @file('/proc/loadavg')) {
|
|
|
|
list($server_load) = explode(' ', $loadavg[0]);
|
|
|
|
unset($loadavg);
|
2005-04-08 05:01:09 +00:00
|
|
|
} else if ( function_exists('is_executable') && is_executable('/usr/bin/uptime') && $loadavg = `/usr/bin/uptime` ) {
|
2005-05-15 04:22:40 +00:00
|
|
|
if (preg_match('/load averages?: (\d+[\.:]\d+)/', $loadavg, $matches)) {
|
2005-04-06 07:34:05 +00:00
|
|
|
$server_load = $matches[1];
|
|
|
|
} else {
|
|
|
|
trigger_error('Could not parse uptime output!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($server_load)) {
|
|
|
|
$info['serverload'] = $server_load;
|
2005-05-15 04:22:40 +00:00
|
|
|
$info['html'] .= '<span class="serverload">Load average: '.$info['serverload'].'</span> ';
|
|
|
|
$info['txt'] .= 'serverload: '.$info['serverload'];
|
2005-04-01 10:00:18 +00:00
|
|
|
}
|
2005-04-06 07:34:05 +00:00
|
|
|
|
2005-04-01 10:00:18 +00:00
|
|
|
|
2005-04-19 20:45:15 +00:00
|
|
|
$info['html'] = '<div class="performanceinfo">'.$info['html'].'</div>';
|
2005-04-01 10:00:18 +00:00
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
2005-04-16 12:22:24 +00:00
|
|
|
if (!function_exists('file_get_contents')) {
|
|
|
|
function file_get_contents($file) {
|
|
|
|
$file = file($file);
|
|
|
|
return $file ? implode('', $file) : false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-20 07:29:28 +00:00
|
|
|
function remove_dir($dir, $content_only=false) {
|
|
|
|
// if content_only=true then delete all but
|
|
|
|
// the directory itself
|
|
|
|
|
|
|
|
$handle = opendir($dir);
|
|
|
|
while (false!==($item = readdir($handle))) {
|
|
|
|
if($item != '.' && $item != '..') {
|
|
|
|
if(is_dir($dir.'/'.$item)) {
|
|
|
|
remove_dir($dir.'/'.$item);
|
|
|
|
}else{
|
|
|
|
unlink($dir.'/'.$item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($handle);
|
|
|
|
if ($content_only) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return rmdir($dir);
|
|
|
|
}
|
|
|
|
|
2005-04-23 15:02:00 +00:00
|
|
|
function report_session_error() {
|
2005-09-07 04:23:37 +00:00
|
|
|
global $CFG, $FULLME;
|
2005-04-23 15:02:00 +00:00
|
|
|
if (empty($CFG->lang)) {
|
|
|
|
$CFG->lang = "en";
|
|
|
|
}
|
|
|
|
moodle_setlocale();
|
|
|
|
//clear session cookies
|
|
|
|
setcookie('MoodleSession'.$CFG->sessioncookie, '', time() - 3600, '/');
|
|
|
|
setcookie('MoodleSessionTest'.$CFG->sessioncookie, '', time() - 3600, '/');
|
|
|
|
//increment database error counters
|
|
|
|
if (isset($CFG->session_error_counter)) {
|
|
|
|
set_config('session_error_counter', 1 + $CFG->session_error_counter);
|
|
|
|
} else {
|
|
|
|
set_config('session_error_counter', 1);
|
|
|
|
}
|
2005-09-23 18:38:50 +00:00
|
|
|
redirect($FULLME, get_string('sessionerroruser2', 'error'), 5);
|
2005-04-23 15:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-13 16:23:10 +00:00
|
|
|
/**
|
|
|
|
* Detect if an object or a class contains a given property
|
|
|
|
* will take an actual object or the name of a class
|
|
|
|
* @param mix $obj Name of class or real object to test
|
|
|
|
* @param string $property name of property to find
|
|
|
|
* @return bool true if property exists
|
|
|
|
*/
|
|
|
|
function object_property_exists( $obj, $property ) {
|
2005-06-13 16:24:47 +00:00
|
|
|
if (is_string( $obj )) {
|
2005-06-13 16:23:10 +00:00
|
|
|
$properties = get_class_vars( $obj );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$properties = get_object_vars( $obj );
|
|
|
|
}
|
2005-06-13 19:07:15 +00:00
|
|
|
return array_key_exists( $property, $properties );
|
2005-06-13 16:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-14 15:35:23 +00:00
|
|
|
/**
|
|
|
|
* Detect a custom script replacement in the data directory that will
|
|
|
|
* replace an existing moodle script
|
|
|
|
* @param string $urlpath path to the original script
|
|
|
|
* @return string full path name if a custom script exists
|
|
|
|
* @return bool false if no custom script exists
|
|
|
|
*/
|
|
|
|
function custom_script_path($urlpath='') {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (empty($urlpath) or (strpos($CFG->wwwroot, $urlpath) === false) ) {
|
|
|
|
$urlpath = qualified_me();
|
|
|
|
if (!$urlpath) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip wwwroot out
|
|
|
|
$scriptpath = str_replace($CFG->wwwroot, $CFG->dataroot.'/customscripts', $urlpath);
|
|
|
|
|
|
|
|
/// Strip the query string out
|
|
|
|
$parts = parse_url($scriptpath);
|
|
|
|
$scriptpath = $parts['path'];
|
|
|
|
|
|
|
|
/// put an index.php on the end if no explicit script name present
|
|
|
|
if (rtrim($scriptpath, '/') != $scriptpath) {
|
|
|
|
$scriptpath .= 'index.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists($scriptpath)) {
|
|
|
|
return $scriptpath;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
?>
|