2001-11-22 06:23:56 +00:00
|
|
|
<?PHP // $Id$
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// //
|
|
|
|
// moodlelib.php //
|
|
|
|
// //
|
|
|
|
// Main library file of miscellaneous general-purpose Moodle functions //
|
|
|
|
// //
|
|
|
|
// Other main libraries: //
|
|
|
|
// //
|
|
|
|
// weblib.php - functions that produce web output //
|
|
|
|
// datalib.php - functions that access the database //
|
|
|
|
// //
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// //
|
|
|
|
// NOTICE OF COPYRIGHT //
|
|
|
|
// //
|
|
|
|
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
|
|
|
|
// http://moodle.com //
|
|
|
|
// //
|
|
|
|
// Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com //
|
|
|
|
// //
|
|
|
|
// 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
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
/// PARAMETER HANDLING ////////////////////////////////////////////////////
|
2002-08-04 16:20:30 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function require_variable($var) {
|
|
|
|
/// Variable must be present
|
|
|
|
if (! isset($var)) {
|
|
|
|
error("A required parameter was missing");
|
2002-08-04 16:20:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function optional_variable(&$var, $default=0) {
|
|
|
|
/// Variable may be present, if not then set a default
|
|
|
|
if (! isset($var)) {
|
|
|
|
$var = $default;
|
2002-08-04 16:20:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function set_config($name, $value) {
|
|
|
|
/// No need for get_config because they are usually always available in $CFG
|
2002-11-10 08:43:44 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
if (get_field("config", "name", "name", $name)) {
|
|
|
|
return set_field("config", "value", $value, "name", $name);
|
2002-11-10 07:37:15 +00:00
|
|
|
} else {
|
2002-12-20 14:44:14 +00:00
|
|
|
$config->name = $name;
|
|
|
|
$config->value = $value;
|
|
|
|
return insert_record("config", $config);
|
2002-07-29 06:21:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
/// FUNCTIONS FOR HANDLING TIME ////////////////////////////////////////////
|
2002-07-29 06:21:37 +00:00
|
|
|
|
|
|
|
function make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Given date parts in user time, produce a GMT timestamp
|
2002-07-29 06:21:37 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
return mktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year);
|
2002-07-29 06:21:37 +00:00
|
|
|
}
|
|
|
|
|
2002-10-03 04:04:59 +00:00
|
|
|
function format_time($totalsecs, $str=NULL) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Given an amount of time in seconds, returns string
|
|
|
|
/// formatted nicely as months, days, hours etc as needed
|
2002-06-08 06:47:33 +00:00
|
|
|
|
2002-08-04 16:20:30 +00:00
|
|
|
$totalsecs = abs($totalsecs);
|
2002-06-08 06:47:33 +00:00
|
|
|
|
2002-10-03 04:04:59 +00:00
|
|
|
if (!$str) { // Create the str structure the slow way
|
|
|
|
$str->day = get_string("day");
|
|
|
|
$str->days = get_string("days");
|
|
|
|
$str->hour = get_string("hour");
|
|
|
|
$str->hours = get_string("hours");
|
|
|
|
$str->min = get_string("min");
|
|
|
|
$str->mins = get_string("mins");
|
|
|
|
$str->sec = get_string("sec");
|
|
|
|
$str->secs = get_string("secs");
|
|
|
|
}
|
|
|
|
|
|
|
|
$days = floor($totalsecs/86400);
|
2002-08-04 16:20:30 +00:00
|
|
|
$remainder = $totalsecs - ($days*86400);
|
2002-10-03 04:04:59 +00:00
|
|
|
$hours = floor($remainder/3600);
|
2002-08-04 16:20:30 +00:00
|
|
|
$remainder = $remainder - ($hours*3600);
|
2002-10-03 04:04:59 +00:00
|
|
|
$mins = floor($remainder/60);
|
|
|
|
$secs = $remainder - ($mins*60);
|
|
|
|
|
|
|
|
$ss = ($secs == 1) ? $str->sec : $str->secs;
|
|
|
|
$sm = ($mins == 1) ? $str->min : $str->mins;
|
|
|
|
$sh = ($hours == 1) ? $str->hour : $str->hours;
|
|
|
|
$sd = ($days == 1) ? $str->day : $str->days;
|
|
|
|
|
2002-12-29 17:32:32 +00:00
|
|
|
$odays = "";
|
|
|
|
$ohours = "";
|
|
|
|
$omins = "";
|
|
|
|
$osecs = "";
|
|
|
|
|
2002-10-03 04:04:59 +00:00
|
|
|
if ($days) $odays = "$days $sd";
|
|
|
|
if ($hours) $ohours = "$hours $sh";
|
|
|
|
if ($mins) $omins = "$mins $sm";
|
|
|
|
if ($secs) $osecs = "$secs $ss";
|
2002-08-04 16:20:30 +00:00
|
|
|
|
|
|
|
if ($days) return "$odays $ohours";
|
|
|
|
if ($hours) return "$ohours $omins";
|
|
|
|
if ($mins) return "$omins $osecs";
|
|
|
|
if ($secs) return "$osecs";
|
|
|
|
return get_string("now");
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-06-05 14:05:59 +00:00
|
|
|
function userdate($date, $format="", $timezone=99) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Returns a formatted string that represents a date in user time
|
|
|
|
/// WARNING: note that the format is for strftime(), not date().
|
|
|
|
/// 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 below is just getting rid of these leading
|
|
|
|
/// zeroes as efficiently as possible.
|
2002-07-02 07:02:28 +00:00
|
|
|
|
2002-06-05 03:15:30 +00:00
|
|
|
global $USER;
|
|
|
|
|
2002-06-05 14:05:59 +00:00
|
|
|
if ($format == "") {
|
2002-12-02 01:35:18 +00:00
|
|
|
$format = "%A, %d %B %Y, %I:%M %p";
|
|
|
|
$formatnoday = "%A, DD %B %Y, %I:%M %p";
|
|
|
|
$fixday = true;
|
|
|
|
} else {
|
|
|
|
$formatnoday = str_replace("%d", "DD", $format);
|
|
|
|
$fixday = ($formatnoday != $format);
|
2002-06-05 14:05:59 +00:00
|
|
|
}
|
2002-12-02 01:35:18 +00:00
|
|
|
|
2002-06-05 14:05:59 +00:00
|
|
|
if ($timezone == 99) {
|
2002-08-21 13:55:49 +00:00
|
|
|
if (isset($USER->timezone)) {
|
|
|
|
$timezone = (float)$USER->timezone;
|
|
|
|
}
|
2002-06-05 14:05:59 +00:00
|
|
|
}
|
2002-11-15 02:46:48 +00:00
|
|
|
if (abs($timezone) > 13) {
|
2002-12-02 01:35:18 +00:00
|
|
|
if ($fixday) {
|
|
|
|
$datestring = strftime($formatnoday, $date);
|
|
|
|
$daystring = str_replace(" 0", "", strftime(" %d", $date));
|
|
|
|
$datestring = str_replace("DD", $daystring, $datestring);
|
|
|
|
} else {
|
|
|
|
$datestring = strftime($format, $date);
|
|
|
|
}
|
2002-12-02 01:13:14 +00:00
|
|
|
} else {
|
2002-12-02 01:35:18 +00:00
|
|
|
if ($fixday) {
|
|
|
|
$datestring = gmstrftime($formatnoday, $date + (int)($timezone * 3600));
|
2002-12-20 14:44:14 +00:00
|
|
|
$daystring = str_replace(" 0", "", gmstrftime(" %d", $date));
|
2002-12-02 01:35:18 +00:00
|
|
|
$datestring = str_replace("DD", $daystring, $datestring);
|
|
|
|
} else {
|
|
|
|
$datestring = gmstrftime($format, $date + (int)($timezone * 3600));
|
|
|
|
}
|
2002-06-05 03:15:30 +00:00
|
|
|
}
|
2002-12-02 01:13:14 +00:00
|
|
|
|
2002-12-02 01:35:18 +00:00
|
|
|
return $datestring;
|
2002-06-05 03:15:30 +00:00
|
|
|
}
|
|
|
|
|
2002-06-05 14:05:59 +00:00
|
|
|
function usergetdate($date, $timezone=99) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Given a $date timestamp in GMT, returns an array
|
|
|
|
/// that represents the date in user time
|
2002-08-04 16:20:30 +00:00
|
|
|
|
2002-06-05 03:15:30 +00:00
|
|
|
global $USER;
|
|
|
|
|
2002-06-05 14:05:59 +00:00
|
|
|
if ($timezone == 99) {
|
|
|
|
$timezone = (float)$USER->timezone;
|
|
|
|
}
|
2002-11-15 02:46:48 +00:00
|
|
|
if (abs($timezone) > 13) {
|
2002-06-05 03:15:30 +00:00
|
|
|
return getdate($date);
|
|
|
|
}
|
2002-06-11 04:04:45 +00:00
|
|
|
//There is no gmgetdate so I have to fake it...
|
|
|
|
$date = $date + (int)($timezone * 3600);
|
|
|
|
$getdate["seconds"] = gmstrftime("%S", $date);
|
|
|
|
$getdate["minutes"] = gmstrftime("%M", $date);
|
|
|
|
$getdate["hours"] = gmstrftime("%H", $date);
|
|
|
|
$getdate["mday"] = gmstrftime("%d", $date);
|
|
|
|
$getdate["wday"] = gmstrftime("%u", $date);
|
|
|
|
$getdate["mon"] = gmstrftime("%m", $date);
|
|
|
|
$getdate["year"] = gmstrftime("%Y", $date);
|
|
|
|
$getdate["yday"] = gmstrftime("%j", $date);
|
|
|
|
$getdate["weekday"] = gmstrftime("%A", $date);
|
|
|
|
$getdate["month"] = gmstrftime("%B", $date);
|
|
|
|
return $getdate;
|
2002-06-10 09:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function usertime($date, $timezone=99) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Given a GMT timestamp (seconds since epoch), offsets it by
|
|
|
|
/// the timezone. eg 3pm in India is 3pm GMT - 7 * 3600 seconds
|
2002-06-10 09:43:40 +00:00
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if ($timezone == 99) {
|
|
|
|
$timezone = (float)$USER->timezone;
|
|
|
|
}
|
2002-11-15 02:46:48 +00:00
|
|
|
if (abs($timezone) > 13) {
|
2002-06-10 09:43:40 +00:00
|
|
|
return $date;
|
|
|
|
}
|
|
|
|
return $date - (int)($timezone * 3600);
|
|
|
|
}
|
|
|
|
|
2002-06-10 14:01:30 +00:00
|
|
|
function usergetmidnight($date, $timezone=99) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Given a time, return the GMT timestamp of the most recent midnight
|
|
|
|
/// for the current user.
|
2002-06-10 14:01:30 +00:00
|
|
|
global $USER;
|
|
|
|
|
2002-08-08 17:28:30 +00:00
|
|
|
if ($timezone == 99) {
|
|
|
|
$timezone = (float)$USER->timezone;
|
|
|
|
}
|
|
|
|
|
2002-06-10 14:01:30 +00:00
|
|
|
$userdate = usergetdate($date, $timezone);
|
2002-08-08 17:28:30 +00:00
|
|
|
|
2002-11-15 02:46:48 +00:00
|
|
|
if (abs($timezone) > 13) {
|
2002-08-08 17:28:30 +00:00
|
|
|
return mktime(0, 0, 0, $userdate["mon"], $userdate["mday"], $userdate["year"]);
|
|
|
|
}
|
|
|
|
|
2002-06-10 14:01:30 +00:00
|
|
|
$timemidnight = gmmktime (0, 0, 0, $userdate["mon"], $userdate["mday"], $userdate["year"]);
|
|
|
|
return usertime($timemidnight, $timezone); // Time of midnight of this user's day, in GMT
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-06-10 09:43:40 +00:00
|
|
|
function usertimezone($timezone=99) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Returns a string that prints the user's timezone
|
2002-06-10 09:43:40 +00:00
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if ($timezone == 99) {
|
|
|
|
$timezone = (float)$USER->timezone;
|
|
|
|
}
|
2002-11-15 02:46:48 +00:00
|
|
|
if (abs($timezone) > 13) {
|
2002-06-10 09:43:40 +00:00
|
|
|
return "server time";
|
|
|
|
}
|
|
|
|
if (abs($timezone) < 0.5) {
|
|
|
|
return "GMT";
|
|
|
|
}
|
|
|
|
if ($timezone > 0) {
|
|
|
|
return "GMT+$timezone";
|
|
|
|
} else {
|
|
|
|
return "GMT$timezone";
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
/// USER AUTHENTICATION AND LOGIN ////////////////////////////////////////
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-06-09 14:14:07 +00:00
|
|
|
function require_login($courseid=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// This function checks that the current user is logged in, and optionally
|
|
|
|
/// whether they are "logged in" or allowed to be in a particular course.
|
|
|
|
/// If not, then it redirects them to the site login or course enrolment.
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
global $CFG, $SESSION, $USER, $FULLME, $HTTP_REFERER, $PHPSESSID;
|
|
|
|
|
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;
|
2002-08-21 12:54:27 +00:00
|
|
|
$SESSION->fromurl = $HTTP_REFERER;
|
2002-08-06 17:23:45 +00:00
|
|
|
save_session("SESSION");
|
2002-08-28 15:07:46 +00:00
|
|
|
$USER = NULL;
|
|
|
|
save_session("USER");
|
2001-11-22 06:23:56 +00:00
|
|
|
if ($PHPSESSID) { // Cookies not enabled.
|
2002-09-15 06:06:32 +00:00
|
|
|
redirect("$CFG->wwwroot/login/index.php?PHPSESSID=$PHPSESSID");
|
2001-11-22 06:23:56 +00:00
|
|
|
} else {
|
2002-09-15 06:06:32 +00:00
|
|
|
redirect("$CFG->wwwroot/login/index.php");
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
die;
|
|
|
|
}
|
2002-12-30 15:30:05 +00:00
|
|
|
|
|
|
|
// Check that the user account is properly set up
|
|
|
|
if (user_not_fully_set_up($USER)) {
|
|
|
|
$site = get_site();
|
|
|
|
redirect("$CFG->wwwroot/user/edit.php?id=$USER->id&course=$site->id");
|
|
|
|
die;
|
|
|
|
}
|
2002-06-09 14:14:07 +00:00
|
|
|
|
|
|
|
// Next, check if the user can be in a particular course
|
|
|
|
if ($courseid) {
|
2002-12-29 17:32:32 +00:00
|
|
|
if (!empty($USER->student[$courseid]) or !empty($USER->teacher[$courseid]) or !empty($USER->admin)) {
|
2002-11-05 16:38:02 +00:00
|
|
|
if (isset($USER->realuser)) { // Make sure the REAL person can also access this course
|
|
|
|
if (!isteacher($courseid, $USER->realuser)) {
|
|
|
|
print_header();
|
2002-11-05 16:47:30 +00:00
|
|
|
notice(get_string("studentnotallowed", "", "$USER->firstname $USER->lastname"), $CFG->wwwroot);
|
2002-11-05 16:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else { // just update their last login time
|
2002-08-11 05:07:15 +00:00
|
|
|
update_user_in_db();
|
|
|
|
}
|
2002-09-26 03:51:31 +00:00
|
|
|
if (!$USER->email) { // User logged in, but has not set up profile!
|
|
|
|
// This can occur with external authentication
|
|
|
|
redirect("$CFG->wwwroot/user/edit.php?id=$USER->id&course=$courseid");
|
|
|
|
die;
|
|
|
|
}
|
2002-06-09 14:14:07 +00:00
|
|
|
return; // user is a member of this course.
|
|
|
|
}
|
|
|
|
if (! $course = get_record("course", "id", $courseid)) {
|
|
|
|
error("That course doesn't exist");
|
|
|
|
}
|
2002-08-21 12:54:27 +00:00
|
|
|
if ($USER->username == "guest") {
|
|
|
|
switch ($course->guest) {
|
|
|
|
case 0: // Guests not allowed
|
|
|
|
print_header();
|
|
|
|
notice(get_string("guestsnotallowed", "", $course->fullname));
|
|
|
|
break;
|
|
|
|
case 1: // Guests allowed
|
|
|
|
update_user_in_db();
|
|
|
|
return;
|
|
|
|
case 2: // Guests allowed with key (drop through)
|
|
|
|
break;
|
|
|
|
}
|
2002-06-09 14:14:07 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-08-21 12:54:27 +00:00
|
|
|
// Currently not enrolled in the course, so see if they want to enrol
|
2002-06-09 14:14:07 +00:00
|
|
|
$SESSION->wantsurl = $FULLME;
|
2002-08-06 17:23:45 +00:00
|
|
|
save_session("SESSION");
|
2002-06-09 14:14:07 +00:00
|
|
|
redirect("$CFG->wwwroot/course/enrol.php?id=$courseid");
|
|
|
|
die;
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2002-12-23 03:07:01 +00:00
|
|
|
function update_user_login_times() {
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
$USER->lastlogin = $user->lastlogin = $USER->currentlogin;
|
|
|
|
$USER->currentlogin = $user->currentlogin = time();
|
|
|
|
save_session("USER");
|
|
|
|
|
|
|
|
$user->id = $USER->id;
|
|
|
|
|
|
|
|
return update_record("user", $user);
|
|
|
|
}
|
|
|
|
|
2002-12-30 15:30:05 +00:00
|
|
|
function user_not_fully_set_up($user) {
|
|
|
|
return (empty($user->firstname) or empty($user->lastname) or empty($user->email));
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
function update_login_count() {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Keeps track of login attempts
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
global $SESSION;
|
|
|
|
|
|
|
|
$max_logins = 10;
|
|
|
|
|
|
|
|
if (empty($SESSION->logincount)) {
|
|
|
|
$SESSION->logincount = 1;
|
|
|
|
} else {
|
|
|
|
$SESSION->logincount++;
|
|
|
|
}
|
2002-08-06 17:23:45 +00:00
|
|
|
save_session("SESSION");
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
if ($SESSION->logincount > $max_logins) {
|
2002-12-20 14:44:14 +00:00
|
|
|
unset($SESSION->wantsurl);
|
|
|
|
save_session("SESSION");
|
2002-12-23 03:07:01 +00:00
|
|
|
error(get_string("errortoomanylogins"));
|
2002-09-22 14:06:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function reset_login_count() {
|
|
|
|
/// Resets login attempts
|
|
|
|
global $SESSION;
|
2002-09-22 14:06:38 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
$SESSION->logincount = 0;
|
|
|
|
save_session("SESSION");
|
2002-09-22 14:06:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
function isadmin($userid=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Is the user an admin?
|
2001-11-22 06:23:56 +00:00
|
|
|
global $USER;
|
|
|
|
|
2002-12-29 14:41:03 +00:00
|
|
|
if (empty($USER)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($userid)) {
|
2002-12-23 09:39:26 +00:00
|
|
|
return record_exists("user_admins", "userid", $USER->id);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2002-12-23 09:39:26 +00:00
|
|
|
return record_exists("user_admins", "userid", $userid);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2002-07-29 06:52:48 +00:00
|
|
|
function isteacher($courseid, $userid=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Is the user a teacher or admin?
|
2001-11-22 06:23:56 +00:00
|
|
|
global $USER;
|
|
|
|
|
2002-04-03 06:30:09 +00:00
|
|
|
if (isadmin($userid)) { // admins can do anything the teacher can
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
if (!$userid) {
|
2002-12-29 14:41:03 +00:00
|
|
|
return !empty($USER->teacher[$courseid]);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2002-12-23 09:39:26 +00:00
|
|
|
return record_exists("user_teachers", "userid", $userid, "course", $courseid);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-29 06:52:48 +00:00
|
|
|
function isstudent($courseid, $userid=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Is the user a student in this course?
|
2001-11-22 06:23:56 +00:00
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if (!$userid) {
|
2003-01-01 06:40:31 +00:00
|
|
|
return !empty($USER->student[$courseid]);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2002-12-23 09:39:26 +00:00
|
|
|
// $timenow = time(); // todo: add time check below
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-12-23 09:39:26 +00:00
|
|
|
return record_exists("user_students", "userid", $userid, "course", $courseid);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2002-06-09 14:14:07 +00:00
|
|
|
function isguest($userid=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Is the user a guest?
|
2002-06-09 14:14:07 +00:00
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if (!$userid) {
|
|
|
|
return ($USER->username == "guest");
|
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
return record_exists("user", "id", $userid, "username", "guest");
|
2002-06-09 14:14:07 +00:00
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2002-07-29 15:45:46 +00:00
|
|
|
function isediting($courseid, $user=NULL) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Is the current user in editing mode?
|
2002-07-29 15:45:46 +00:00
|
|
|
global $USER;
|
|
|
|
if (!$user){
|
|
|
|
$user = $USER;
|
|
|
|
}
|
2002-12-29 17:32:32 +00:00
|
|
|
if (empty($user->editing)) {
|
|
|
|
return false;
|
|
|
|
}
|
2002-07-29 15:45:46 +00:00
|
|
|
return ($user->editing and isteacher($courseid, $user->id));
|
|
|
|
}
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
function set_moodle_cookie($thing) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Sets a moodle cookie with an encrypted string
|
2002-12-24 10:07:45 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$cookiename = "MOODLEID{$CFG->prefix}";
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
$days = 60;
|
|
|
|
$seconds = 60*60*24*$days;
|
|
|
|
|
2002-12-24 10:07:45 +00:00
|
|
|
setCookie($cookiename, "", time() - 3600, "/");
|
|
|
|
setCookie($cookiename, rc4encrypt($thing), time()+$seconds, "/");
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function get_moodle_cookie() {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Gets a moodle cookie with an encrypted string
|
2002-12-24 10:07:45 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2002-12-24 10:08:24 +00:00
|
|
|
$cookiename = "MOODLEID{$CFG->prefix}";
|
2002-12-24 10:07:45 +00:00
|
|
|
|
|
|
|
global $$cookiename;
|
|
|
|
|
|
|
|
return rc4decrypt($$cookiename);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-06 17:23:45 +00:00
|
|
|
function save_session($VAR) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Copies temporary session variable to permanent session variable
|
|
|
|
/// eg $_SESSION["USER"] = $USER;
|
2002-08-06 17:23:45 +00:00
|
|
|
global $$VAR;
|
|
|
|
$_SESSION[$VAR] = $$VAR;
|
|
|
|
}
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-09-26 07:03:22 +00:00
|
|
|
function create_user_record($username, $password) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Creates a bare-bones user record
|
2002-10-05 16:49:42 +00:00
|
|
|
global $REMOTE_ADDR, $CFG;
|
|
|
|
|
2002-11-21 11:28:46 +00:00
|
|
|
if (function_exists(auth_get_userinfo)) {
|
2002-10-05 16:49:42 +00:00
|
|
|
if ($newinfo = auth_get_userinfo($username)) {
|
2002-11-21 07:37:21 +00:00
|
|
|
foreach ($newinfo as $key => $value){
|
2002-10-05 16:49:42 +00:00
|
|
|
$newuser->$key = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-09-26 07:03:22 +00:00
|
|
|
$newuser->username = $username;
|
|
|
|
$newuser->password = md5($password);
|
2002-10-18 09:00:09 +00:00
|
|
|
$newuser->lang = $CFG->lang;
|
2002-09-26 07:03:22 +00:00
|
|
|
$newuser->confirmed = 1;
|
|
|
|
$newuser->lastIP = $REMOTE_ADDR;
|
|
|
|
$newuser->timemodified = time();
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-09-26 07:03:22 +00:00
|
|
|
if (insert_record("user", $newuser)) {
|
|
|
|
return get_user_info_from_db("username", $username);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function authenticate_user_login($username, $password) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Given a username and password, this function looks them
|
|
|
|
/// up using the currently selected authentication mechanism,
|
|
|
|
/// and if the authentication is successful, it returns a
|
|
|
|
/// valid $user object from the 'user' table.
|
|
|
|
///
|
|
|
|
/// Uses auth_ functions from the currently active auth module
|
2002-09-26 07:03:22 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2002-11-22 08:43:35 +00:00
|
|
|
$md5password = md5($password);
|
|
|
|
|
2002-09-26 07:03:22 +00:00
|
|
|
if (!isset($CFG->auth)) {
|
|
|
|
$CFG->auth = "email"; // Default authentication module
|
|
|
|
}
|
|
|
|
|
2002-11-22 08:43:35 +00:00
|
|
|
if ($username == "guest") {
|
|
|
|
$CFG->auth = "none"; // Guest account always internal
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is the admin, then just use internal methods
|
2002-11-22 09:38:15 +00:00
|
|
|
// Doing this first (even though it's less efficient) because
|
|
|
|
// the chosen authentication method might hang and lock the
|
|
|
|
// admin out.
|
2002-12-20 14:44:14 +00:00
|
|
|
if (adminlogin($username, $md5password)) {
|
2002-11-22 08:43:35 +00:00
|
|
|
return get_user_info_from_db("username", $username);
|
|
|
|
}
|
|
|
|
|
2002-11-22 09:38:15 +00:00
|
|
|
// OK, the user is a normal user, so try and authenticate them
|
2002-10-05 16:49:42 +00:00
|
|
|
require_once("$CFG->dirroot/auth/$CFG->auth/lib.php");
|
2002-09-26 07:03:22 +00:00
|
|
|
|
|
|
|
if (auth_user_login($username, $password)) { // Successful authentication
|
|
|
|
|
|
|
|
if ($user = get_user_info_from_db("username", $username)) {
|
2002-11-22 09:38:15 +00:00
|
|
|
if ($md5password <> $user->password) { // Update local copy of password for reference
|
2002-11-22 08:43:35 +00:00
|
|
|
set_field("user", "password", $md5password, "username", $username);
|
2002-09-26 07:03:22 +00:00
|
|
|
}
|
|
|
|
return $user;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return create_user_record($username, $password);
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
2002-11-21 12:39:33 +00:00
|
|
|
|
2002-09-26 07:03:22 +00:00
|
|
|
return false;
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2002-09-26 07:03:22 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function enrol_student($user, $course) {
|
|
|
|
/// Enrols a student in a given course
|
|
|
|
global $db;
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-12-23 09:39:26 +00:00
|
|
|
$record->userid = $user;
|
2002-12-20 14:44:14 +00:00
|
|
|
$record->course = $course;
|
|
|
|
$record->start = 0;
|
|
|
|
$record->end = 0;
|
|
|
|
$record->time = time();
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-12-23 09:39:26 +00:00
|
|
|
return insert_record("user_students", $record);
|
2002-12-11 14:59:37 +00:00
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function unenrol_student($user, $course=0) {
|
|
|
|
/// Unenrols a student from a given course
|
|
|
|
global $db;
|
2002-12-11 14:59:37 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($course) {
|
|
|
|
/// First delete any crucial stuff that might still send mail
|
|
|
|
if ($forums = get_records("forum", "course", $course)) {
|
|
|
|
foreach ($forums as $forum) {
|
2002-12-23 09:39:26 +00:00
|
|
|
delete_records("forum_subscriptions", "forum", $forum->id, "userid", $user);
|
2002-09-22 03:01:17 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
2002-12-23 09:39:26 +00:00
|
|
|
return delete_records("user_students", "userid", $user, "course", $course);
|
2002-12-20 14:44:14 +00:00
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
} else {
|
2002-12-23 09:39:26 +00:00
|
|
|
delete_records("forum_subscriptions", "userid", $user);
|
|
|
|
return delete_records("user_students", "userid", $user);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function remove_teacher($user, $course=0) {
|
|
|
|
/// Removes a teacher from a given course (or ALL courses)
|
|
|
|
/// Does not delete the user account
|
|
|
|
global $db;
|
2002-08-02 17:41:14 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($course) {
|
|
|
|
/// First delete any crucial stuff that might still send mail
|
|
|
|
if ($forums = get_records("forum", "course", $course)) {
|
|
|
|
foreach ($forums as $forum) {
|
2002-12-23 09:39:26 +00:00
|
|
|
delete_records("forum_subscriptions", "forum", $forum->id, "userid", $user);
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
|
|
|
}
|
2002-12-23 09:39:26 +00:00
|
|
|
return delete_records("user_teachers", "userid", $user, "course", $course);
|
2002-08-02 17:41:14 +00:00
|
|
|
} else {
|
2002-12-23 09:39:26 +00:00
|
|
|
delete_records("forum_subscriptions", "userid", $user);
|
|
|
|
return delete_records("user_teachers", "userid", $user);
|
2002-08-02 17:41:14 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function remove_admin($user) {
|
|
|
|
/// Removes an admin from a site
|
|
|
|
global $db;
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-12-23 09:39:26 +00:00
|
|
|
return delete_records("user_admins", "userid", $user);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// CORRESPONDENCE ////////////////////////////////////////////////
|
|
|
|
|
2002-06-05 14:05:59 +00:00
|
|
|
function email_to_user($user, $from, $subject, $messagetext, $messagehtml="", $attachment="", $attachname="") {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// user - a user record as an object
|
|
|
|
/// from - a user record as an object
|
|
|
|
/// subject - plain text subject line of the email
|
|
|
|
/// messagetext - plain text version of the message
|
|
|
|
/// messagehtml - complete html version of the message (optional)
|
|
|
|
/// attachment - a file on the filesystem, relative to $CFG->dataroot
|
|
|
|
/// attachname - the name of the file (extension indicates MIME)
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-05-31 09:20:39 +00:00
|
|
|
global $CFG, $_SERVER;
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-05-24 06:38:37 +00:00
|
|
|
include_once("$CFG->libdir/phpmailer/class.phpmailer.php");
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-06-05 14:05:59 +00:00
|
|
|
if (!$user) {
|
2001-11-22 06:23:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$mail = new phpmailer;
|
|
|
|
|
2002-10-23 12:12:01 +00:00
|
|
|
$mail->Version = "Moodle $CFG->version"; // mailer version
|
2002-05-24 06:38:37 +00:00
|
|
|
$mail->PluginDir = "$CFG->libdir/phpmailer/"; // plugin directory (eg smtp plugin)
|
2002-09-21 08:39:54 +00:00
|
|
|
|
2002-11-14 11:45:14 +00:00
|
|
|
|
2002-11-14 11:48:01 +00:00
|
|
|
if (current_language() != "en") {
|
|
|
|
$mail->CharSet = get_string("thischarset");
|
2002-11-14 11:45:14 +00:00
|
|
|
}
|
|
|
|
|
2002-06-06 15:21:30 +00:00
|
|
|
if ($CFG->smtphosts) {
|
2002-06-07 03:57:38 +00:00
|
|
|
$mail->IsSMTP(); // use SMTP directly
|
|
|
|
$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
|
|
|
} else {
|
2002-06-07 03:57:38 +00:00
|
|
|
$mail->IsMail(); // use PHP mail() = sendmail
|
2002-06-06 15:21:30 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-05-24 06:38:37 +00:00
|
|
|
$mail->From = "$from->email";
|
|
|
|
$mail->FromName = "$from->firstname $from->lastname";
|
|
|
|
$mail->Subject = stripslashes($subject);
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-08-04 16:20:30 +00:00
|
|
|
$mail->AddAddress("$user->email", "$user->firstname $user->lastname");
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
$mail->WordWrap = 70; // set word wrap
|
|
|
|
|
2002-05-24 06:38:37 +00:00
|
|
|
if ($messagehtml) {
|
|
|
|
$mail->IsHTML(true);
|
|
|
|
$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
|
2002-05-31 09:20:39 +00:00
|
|
|
$adminuser = get_admin();
|
|
|
|
$mail->AddAddress("$adminuser->email", "$adminuser->firstname $adminuser->lastname");
|
|
|
|
$mail->AddStringAttachment("Error in attachment. User attempted to attach a filename with a unsafe name.", "error.txt", "8bit", "text/plain");
|
2002-05-24 06:38:37 +00:00
|
|
|
} else {
|
|
|
|
include_once("$CFG->dirroot/files/mimetypes.php");
|
|
|
|
$mimetype = mimeinfo("type", $attachname);
|
|
|
|
$mail->AddAttachment("$CFG->dataroot/$attachment", "$attachname", "base64", "$mimetype");
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2002-05-24 06:38:37 +00:00
|
|
|
if ($mail->Send()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2002-05-31 09:20:39 +00:00
|
|
|
echo "ERROR: $mail->ErrorInfo\n";
|
|
|
|
$site = get_site();
|
|
|
|
add_to_log($site->id, "library", "mailer", $_SERVER["REQUEST_URI"], "ERROR: $mail->ErrorInfo");
|
2001-11-22 06:23:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-23 03:07:01 +00:00
|
|
|
function reset_password_and_mail($user) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$site = get_site();
|
|
|
|
$from = get_admin();
|
|
|
|
|
|
|
|
$newpassword = generate_password();
|
|
|
|
|
|
|
|
if (! set_field("user", "password", md5($newpassword), "id", $user->id) ) {
|
|
|
|
error("Could not set user password!");
|
|
|
|
}
|
|
|
|
|
|
|
|
$a->firstname = $user->firstname;
|
|
|
|
$a->sitename = $site->fullname;
|
|
|
|
$a->username = $user->username;
|
|
|
|
$a->newpassword = $newpassword;
|
|
|
|
$a->link = "$CFG->wwwroot/login/change_password.php";
|
|
|
|
$a->signoff = "$from->firstname $from->lastname ($from->email)";
|
|
|
|
|
|
|
|
$message = get_string("newpasswordtext", "", $a);
|
|
|
|
|
|
|
|
$subject = "$site->fullname: ".get_string("changedpassword");
|
|
|
|
|
|
|
|
return email_to_user($user, $from, $subject, $message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function send_confirmation_email($user) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$site = get_site();
|
|
|
|
$from = get_admin();
|
|
|
|
|
|
|
|
$data->firstname = $user->firstname;
|
|
|
|
$data->sitename = $site->fullname;
|
|
|
|
$data->link = "$CFG->wwwroot/login/confirm.php?p=$user->secret&s=$user->username";
|
|
|
|
$data->admin = "$from->firstname $from->lastname ($from->email)";
|
|
|
|
|
|
|
|
$message = get_string("emailconfirmation", "", $data);
|
|
|
|
$subject = "$site->fullname account confirmation";
|
|
|
|
|
|
|
|
return email_to_user($user, $from, $subject, $message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-24 06:38:37 +00:00
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
/// FILE HANDLING /////////////////////////////////////////////
|
|
|
|
|
2002-08-04 16:20:30 +00:00
|
|
|
function make_upload_directory($directory) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// $directory = a string of directory names under $CFG->dataroot
|
|
|
|
/// eg stuff/assignment/1
|
|
|
|
/// Returns full directory if successful, false if not
|
2002-08-04 16:20:30 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$currdir = $CFG->dataroot;
|
|
|
|
if (!file_exists($currdir)) {
|
|
|
|
if (! mkdir($currdir, 0750)) {
|
|
|
|
notify("ERROR: You need to create the directory $currdir with web server write access");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$dirarray = explode("/", $directory);
|
|
|
|
|
|
|
|
foreach ($dirarray as $dir) {
|
|
|
|
$currdir = "$currdir/$dir";
|
|
|
|
if (! file_exists($currdir)) {
|
|
|
|
if (! mkdir($currdir, 0750)) {
|
|
|
|
notify("ERROR: Could not find or create a directory ($currdir)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $currdir;
|
|
|
|
}
|
|
|
|
|
2002-08-28 13:07:10 +00:00
|
|
|
function make_mod_upload_directory($courseid) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Makes an upload directory for a particular module
|
2002-08-28 13:07:10 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (! $moddata = make_upload_directory("$courseid/$CFG->moddata")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$strreadme = get_string("readme");
|
|
|
|
|
|
|
|
if (file_exists("$CFG->dirroot/lang/$CFG->lang/docs/module_files.txt")) {
|
|
|
|
copy("$CFG->dirroot/lang/$CFG->lang/docs/module_files.txt", "$moddata/$strreadme.txt");
|
|
|
|
} else {
|
|
|
|
copy("$CFG->dirroot/lang/en/docs/module_files.txt", "$moddata/$strreadme.txt");
|
|
|
|
}
|
|
|
|
return $moddata;
|
|
|
|
}
|
|
|
|
|
2002-08-04 16:20:30 +00:00
|
|
|
|
2002-09-01 14:29:17 +00:00
|
|
|
function valid_uploaded_file($newfile) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Returns current name of file on disk if true
|
2002-12-29 17:32:32 +00:00
|
|
|
if (empty($newfile)) {
|
|
|
|
return "";
|
|
|
|
}
|
2002-09-01 14:29:17 +00:00
|
|
|
if (is_uploaded_file($newfile['tmp_name']) and $newfile['size'] > 0) {
|
|
|
|
return $newfile['tmp_name'];
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_max_upload_file_size() {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Returns the maximum size for uploading files
|
2002-09-01 14:29:17 +00:00
|
|
|
if (! $filesize = ini_get("upload_max_filesize")) {
|
|
|
|
$filesize = "5M";
|
|
|
|
}
|
|
|
|
return get_real_size($filesize);
|
|
|
|
}
|
|
|
|
|
2002-09-07 14:57:33 +00:00
|
|
|
function get_directory_list($rootdir, $excludefile="", $descend=true) {
|
2002-12-20 14:44:14 +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
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
$dirs = array();
|
|
|
|
|
2002-09-01 14:29:17 +00:00
|
|
|
$dir = opendir($rootdir);
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-11-10 07:37:15 +00:00
|
|
|
if (!$dir) {
|
|
|
|
notify("Error: unable to read this directory! : $rootdir");
|
|
|
|
return $dirs;
|
|
|
|
}
|
|
|
|
|
2002-08-28 13:07:10 +00:00
|
|
|
while ($file = readdir($dir)) {
|
2002-09-07 14:57:33 +00:00
|
|
|
if ($file != "." and $file != ".." and $file != "CVS" and $file != $excludefile) {
|
2002-08-28 13:07:10 +00:00
|
|
|
$fullfile = $rootdir."/".$file;
|
2002-09-07 14:57:33 +00:00
|
|
|
if ($descend and filetype($fullfile) == "dir") {
|
|
|
|
$subdirs = get_directory_list($fullfile, $excludefile, $descend);
|
2001-11-22 06:23:56 +00:00
|
|
|
foreach ($subdirs as $subdir) {
|
|
|
|
$dirs[] = $file."/".$subdir;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$dirs[] = $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2002-08-07 08:26:48 +00:00
|
|
|
function get_real_size($size=0) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Converts numbers like 10M into bytes
|
2002-08-07 08:26:48 +00:00
|
|
|
if (!$size) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$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;
|
|
|
|
$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;
|
|
|
|
|
|
|
|
while (list($key) = each($scan)) {
|
|
|
|
if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) {
|
|
|
|
$size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $size;
|
|
|
|
}
|
|
|
|
|
2002-09-01 14:29:17 +00:00
|
|
|
function display_size($size) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Converts bytes into display form
|
2002-09-01 14:29:17 +00:00
|
|
|
if ($size >= 1073741824) {
|
|
|
|
$size = round($size / 1073741824 * 10) / 10 . "Gb";
|
|
|
|
} else if ($size >= 1048576) {
|
|
|
|
$size = round($size / 1048576 * 10) / 10 . "Mb";
|
|
|
|
} else if ($size >= 1024) {
|
|
|
|
$size = round($size / 1024 * 10) / 10 . "Kb";
|
|
|
|
} else {
|
|
|
|
$size = $size . "b";
|
|
|
|
}
|
|
|
|
return $size;
|
|
|
|
}
|
|
|
|
|
2002-08-04 16:20:30 +00:00
|
|
|
function clean_filename($string) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Cleans a given filename by removing suspicious or troublesome characters
|
2002-11-26 02:11:05 +00:00
|
|
|
$string = stripslashes($string);
|
2002-08-04 16:20:30 +00:00
|
|
|
$string = eregi_replace("\.\.", "", $string);
|
|
|
|
$string = eregi_replace("[^([:alnum:]|\.)]", "_", $string);
|
|
|
|
return eregi_replace("_+", "_", $string);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-27 08:47:27 +00:00
|
|
|
/// STRING TRANSLATION ////////////////////////////////////////
|
|
|
|
|
2002-10-02 10:33:05 +00:00
|
|
|
function current_language() {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Returns the code for the current language
|
2002-10-02 10:33:05 +00:00
|
|
|
global $CFG, $USER;
|
|
|
|
|
|
|
|
if (isset($USER->lang)) { // User language can override site language
|
|
|
|
return $USER->lang;
|
|
|
|
} else {
|
|
|
|
return $CFG->lang;
|
|
|
|
}
|
|
|
|
}
|
2002-07-04 07:49:38 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function print_string($identifier, $module="", $a=NULL) {
|
|
|
|
/// Given a string to translate - prints it out.
|
|
|
|
echo get_string($identifier, $module, $a);
|
|
|
|
}
|
|
|
|
|
2002-07-11 05:30:10 +00:00
|
|
|
function get_string($identifier, $module="", $a=NULL) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Return the translated string specified by $identifier as
|
|
|
|
/// for $module. Uses the same format files as STphp.
|
|
|
|
/// $a is an object, string or number that can be used
|
|
|
|
/// within translation strings
|
|
|
|
///
|
|
|
|
/// eg "hello \$a->firstname \$a->lastname"
|
|
|
|
/// or "hello \$a"
|
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
|
|
|
|
2002-10-02 10:33:05 +00:00
|
|
|
$lang = current_language();
|
2002-06-27 08:47:27 +00:00
|
|
|
|
2002-08-02 09:24:50 +00:00
|
|
|
if ($module == "") {
|
|
|
|
$module = "moodle";
|
2002-06-27 08:47:27 +00:00
|
|
|
}
|
|
|
|
|
2002-08-02 09:24:50 +00:00
|
|
|
$langpath = "$CFG->dirroot/lang";
|
|
|
|
$langfile = "$langpath/$lang/$module.php";
|
2002-06-27 08:47:27 +00:00
|
|
|
|
|
|
|
if (!file_exists($langfile)) { // try English instead
|
2002-08-02 09:24:50 +00:00
|
|
|
$langfile = "$langpath/en/$module.php";
|
2002-06-27 08:47:27 +00:00
|
|
|
if (!file_exists($langfile)) {
|
2002-08-02 09:24:50 +00:00
|
|
|
return "ERROR: No lang file ($langpath/en/$module.php)!";
|
2002-06-27 08:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) {
|
|
|
|
|
|
|
|
eval($result);
|
|
|
|
return $resultstring;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if ($lang == "en") {
|
2002-09-01 14:29:17 +00:00
|
|
|
return "[['$identifier']]";
|
2002-06-27 08:47:27 +00:00
|
|
|
|
|
|
|
} else { // Try looking in the english file.
|
2002-08-02 09:24:50 +00:00
|
|
|
$langfile = "$langpath/en/$module.php";
|
2002-06-27 08:47:27 +00:00
|
|
|
if (!file_exists($langfile)) {
|
2002-08-02 09:24:50 +00:00
|
|
|
return "ERROR: No lang file ($langpath/en/$module.php)!";
|
2002-06-27 08:47:27 +00:00
|
|
|
}
|
|
|
|
if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) {
|
|
|
|
eval($result);
|
|
|
|
return $resultstring;
|
|
|
|
} else {
|
2002-09-01 14:29:17 +00:00
|
|
|
return "[['$identifier']]";
|
2002-06-27 08:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function get_string_from_file($identifier, $langfile, $destination) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// This function is only used from get_string().
|
2002-06-27 08:47:27 +00:00
|
|
|
include ($langfile);
|
|
|
|
|
|
|
|
if (!isset ($string[$identifier])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-07-11 05:30:10 +00:00
|
|
|
return "$destination = sprintf(\"".$string[$identifier]."\");";
|
2002-06-27 08:47:27 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
|
2002-09-14 03:43:24 +00:00
|
|
|
function get_list_of_languages() {
|
|
|
|
/// Returns a list of language codes and their full names
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!$langdirs = get_list_of_plugins("lang")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($langdirs as $lang) {
|
|
|
|
include("$CFG->dirroot/lang/$lang/moodle.php");
|
|
|
|
$languages[$lang] = $string["thislanguage"]." ($lang)";
|
|
|
|
unset($string);
|
|
|
|
}
|
|
|
|
return $languages;
|
|
|
|
}
|
|
|
|
|
2002-12-29 14:41:03 +00:00
|
|
|
function document_file($file, $include=true) {
|
|
|
|
/// Can include a given document file (depends on second
|
|
|
|
/// parameter) or just return info about it
|
|
|
|
|
2002-12-29 16:26:44 +00:00
|
|
|
global $CFG, $SESSION;
|
2002-12-29 14:41:03 +00:00
|
|
|
|
|
|
|
if (empty($file)) {
|
|
|
|
echo "Error 404";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-12-29 16:26:44 +00:00
|
|
|
if (!empty($SESSION->doclang)) {
|
|
|
|
$lang = $SESSION->doclang;
|
|
|
|
} else {
|
|
|
|
$lang = current_language();
|
|
|
|
}
|
|
|
|
|
2002-12-29 14:41:03 +00:00
|
|
|
$file = clean_filename($file);
|
|
|
|
|
|
|
|
$info->filepath = "$CFG->dirroot/lang/$lang/docs/$file";
|
|
|
|
$info->urlpath = "$CFG->wwwroot/lang/$lang/docs/$file";
|
|
|
|
|
|
|
|
if (!file_exists($info->filepath)) {
|
|
|
|
$info->filepath = "$CFG->dirroot/lang/en/docs/$file";
|
|
|
|
$info->urlpath = "$CFG->wwwroot/lang/en/docs/$file";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($include) {
|
|
|
|
include($info->filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
2002-09-14 03:43:24 +00:00
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
/// ENCRYPTION ////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function rc4encrypt($data) {
|
|
|
|
$password = "nfgjeingjk";
|
|
|
|
return endecrypt($password, $data, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
function rc4decrypt($data) {
|
|
|
|
$password = "nfgjeingjk";
|
|
|
|
return endecrypt($password, $data, "de");
|
|
|
|
}
|
|
|
|
|
|
|
|
function endecrypt ($pwd, $data, $case) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Based on a class by Mukul Sabharwal [mukulsabharwal@yahoo.com]
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
if ($case == 'de') {
|
|
|
|
$data = urldecode($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
$key[] = "";
|
|
|
|
$box[] = "";
|
|
|
|
$temp_swap = "";
|
|
|
|
$pwd_length = 0;
|
|
|
|
|
|
|
|
$pwd_length = strlen($pwd);
|
|
|
|
|
|
|
|
for ($i = 0; $i <= 255; $i++) {
|
|
|
|
$key[$i] = ord(substr($pwd, ($i % $pwd_length), 1));
|
|
|
|
$box[$i] = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
$x = 0;
|
|
|
|
|
|
|
|
for ($i = 0; $i <= 255; $i++) {
|
|
|
|
$x = ($x + $box[$i] + $key[$i]) % 256;
|
|
|
|
$temp_swap = $box[$i];
|
|
|
|
$box[$i] = $box[$x];
|
|
|
|
$box[$x] = $temp_swap;
|
|
|
|
}
|
|
|
|
|
|
|
|
$temp = "";
|
|
|
|
$k = "";
|
|
|
|
|
|
|
|
$cipherby = "";
|
|
|
|
$cipher = "";
|
|
|
|
|
|
|
|
$a = 0;
|
|
|
|
$j = 0;
|
|
|
|
|
|
|
|
for ($i = 0; $i < strlen($data); $i++) {
|
|
|
|
$a = ($a + 1) % 256;
|
|
|
|
$j = ($j + $box[$a]) % 256;
|
|
|
|
$temp = $box[$a];
|
|
|
|
$box[$a] = $box[$j];
|
|
|
|
$box[$j] = $temp;
|
|
|
|
$k = $box[(($box[$a] + $box[$j]) % 256)];
|
|
|
|
$cipherby = ord(substr($data, $i, 1)) ^ $k;
|
|
|
|
$cipher .= chr($cipherby);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($case == 'de') {
|
|
|
|
$cipher = urldecode(urlencode($cipher));
|
|
|
|
} else {
|
|
|
|
$cipher = urlencode($cipher);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cipher;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
/// ENVIRONMENT CHECKING ////////////////////////////////////////////////////////////
|
2002-08-17 13:01:06 +00:00
|
|
|
|
2002-12-23 03:07:01 +00:00
|
|
|
function get_list_of_plugins($plugin="mod") {
|
|
|
|
/// Lists plugin directories within some directory
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$basedir = opendir("$CFG->dirroot/$plugin");
|
|
|
|
while ($dir = readdir($basedir)) {
|
|
|
|
if ($dir == "." || $dir == ".." || $dir == "CVS") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (filetype("$CFG->dirroot/$plugin/$dir") != "dir") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$plugins[] = $dir;
|
|
|
|
}
|
|
|
|
if ($plugins) {
|
|
|
|
asort($plugins);
|
|
|
|
}
|
|
|
|
return $plugins;
|
|
|
|
}
|
|
|
|
|
2002-08-23 02:14:19 +00:00
|
|
|
function check_php_version($version="4.1.0") {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Returns true is the current version of PHP is greater that the specified one
|
2002-08-23 02:14:19 +00:00
|
|
|
$minversion = intval(str_replace(".", "", $version));
|
|
|
|
$curversion = intval(str_replace(".", "", phpversion()));
|
|
|
|
return ($curversion >= $minversion);
|
|
|
|
}
|
|
|
|
|
2002-10-10 07:26:10 +00:00
|
|
|
function check_browser_version($brand="MSIE", $version=5.5) {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Checks to see if is a browser matches the specified
|
|
|
|
/// brand and is equal or better version.
|
2002-10-10 07:26:10 +00:00
|
|
|
global $HTTP_USER_AGENT;
|
|
|
|
|
|
|
|
if (!$HTTP_USER_AGENT) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$string = explode(";", $HTTP_USER_AGENT);
|
|
|
|
if (!isset($string[1])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$string = explode(" ", trim($string[1]));
|
|
|
|
if (!isset($string[0]) and !isset($string[1])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($string[0] == $brand and (float)$string[1] >= $version ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function can_use_richtext_editor() {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Is the richedit editor enabled?
|
2002-10-10 07:26:10 +00:00
|
|
|
global $USER, $CFG;
|
2002-10-16 04:53:44 +00:00
|
|
|
if ($USER->htmleditor and $CFG->htmleditor) {
|
|
|
|
return check_browser_version("MSIE", 5.5);
|
|
|
|
}
|
|
|
|
return false;
|
2002-10-10 07:26:10 +00:00
|
|
|
}
|
|
|
|
|
2002-09-19 12:01:55 +00:00
|
|
|
function check_gd_version() {
|
2002-12-20 14:44:14 +00:00
|
|
|
/// Hack to find out the GD version by parsing phpinfo output
|
2002-09-19 12:01:55 +00:00
|
|
|
ob_start();
|
2002-12-15 02:40:25 +00:00
|
|
|
phpinfo(8);
|
2002-09-19 12:01:55 +00:00
|
|
|
$phpinfo = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
$phpinfo = explode("\n",$phpinfo);
|
|
|
|
|
|
|
|
$gdversion = 0;
|
|
|
|
|
2002-12-28 14:45:54 +00:00
|
|
|
|
2002-09-19 12:01:55 +00:00
|
|
|
foreach ($phpinfo as $text) {
|
2002-12-28 14:45:54 +00:00
|
|
|
$parts = explode('</td>',$text);
|
2002-09-19 12:01:55 +00:00
|
|
|
foreach ($parts as $key => $val) {
|
2002-12-28 14:45:54 +00:00
|
|
|
$parts[$key] = trim(strip_tags($val));
|
2002-09-19 12:01:55 +00:00
|
|
|
}
|
2002-12-28 14:45:54 +00:00
|
|
|
if ($parts[0] == "GD Version") {
|
|
|
|
if (substr_count($parts[1], "2.0")) {
|
|
|
|
$parts[1] = "2.0";
|
|
|
|
}
|
2002-09-19 12:01:55 +00:00
|
|
|
$gdversion = intval($parts[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $gdversion; // 1, 2 or 0
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-10-10 07:26:10 +00:00
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function moodle_needs_upgrading() {
|
|
|
|
/// Checks version numbers of Main code and all modules to see
|
|
|
|
/// if there are any mismatches ... returns true or false
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
include_once("$CFG->dirroot/version.php"); # defines $version and upgrades
|
|
|
|
if ($CFG->version) {
|
|
|
|
if ($version > $CFG->version) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ($mods = get_list_of_plugins("mod")) {
|
|
|
|
foreach ($mods as $mod) {
|
|
|
|
$fullmod = "$CFG->dirroot/mod/$mod";
|
|
|
|
unset($module);
|
|
|
|
include_once("$fullmod/version.php"); # defines $module with version etc
|
|
|
|
if ($currmodule = get_record("modules", "name", $mod)) {
|
|
|
|
if ($module->version > $currmodule->version) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// MISCELLANEOUS ////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function count_words($string) {
|
|
|
|
/// Words are defined as things between whitespace
|
|
|
|
$string = strip_tags($string);
|
|
|
|
return count(preg_split("/\w\b/", $string)) - 1;
|
|
|
|
}
|
|
|
|
|
2002-12-23 03:07:01 +00:00
|
|
|
function random_string ($length=15) {
|
|
|
|
$pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
$pool .= "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
$pool .= "0123456789";
|
|
|
|
$poollen = strlen($pool);
|
|
|
|
mt_srand ((double) microtime() * 1000000);
|
|
|
|
$string = "";
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
|
$string .= substr($pool, (mt_rand()%($poollen)), 1);
|
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-20 14:44:14 +00:00
|
|
|
function getweek ($startdate, $thedate) {
|
|
|
|
/// Given dates in seconds, how many weeks is the date from startdate
|
|
|
|
/// The first week is 1, the second 2 etc ...
|
|
|
|
|
|
|
|
if ($thedate < $startdate) { // error
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return floor(($thedate - $startdate) / 604800.0) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generate_password($maxlen=10) {
|
|
|
|
/// returns a randomly generated password of length $maxlen. inspired by
|
|
|
|
/// http://www.phpbuilder.com/columns/jesus19990502.php3
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$fillers = "1234567890!$-+";
|
|
|
|
$wordlist = file($CFG->wordlist);
|
|
|
|
|
|
|
|
srand((double) microtime() * 1000000);
|
|
|
|
$word1 = trim($wordlist[rand(0, count($wordlist) - 1)]);
|
|
|
|
$word2 = trim($wordlist[rand(0, count($wordlist) - 1)]);
|
|
|
|
$filler1 = $fillers[rand(0, strlen($fillers) - 1)];
|
|
|
|
|
|
|
|
return substr($word1 . $filler1 . $word2, 0, $maxlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
function format_float($num, $places=0) {
|
|
|
|
/// Given a float, prints it nicely
|
|
|
|
return sprintf("%.$places"."f", $num);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-10 07:26:10 +00:00
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
?>
|