From d0491a1feffcd11ba574e098ef93e3aac417739c Mon Sep 17 00:00:00 2001 From: julmis Date: Tue, 13 Sep 2005 11:49:40 +0000 Subject: [PATCH] Faster version check for Gecko and work-a-round for "No IDN" problem. --- lib/moodlelib.php | 101 +++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 51 deletions(-) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index a15fcb6d3b4..e2fcb9fb932 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -321,7 +321,7 @@ function clean_param($param, $options) { } if ($options & PARAM_CLEANHTML) { - $param = stripslashes($param); // Remove any slashes + $param = stripslashes($param); // Remove any slashes $param = clean_text($param); // Sweep for scripts, etc $param = trim($param); // Sweep for scripts, etc } @@ -404,9 +404,9 @@ function optional_variable(&$var, $default=0) { * * Set a key/value pair in both this session's {@link $CFG} global variable * and in the 'config' database table for future sessions. - * - * Can also be used to update keys for plugin-scoped configs in config_plugin table. - * In that case it doesn't affect $CFG. + * + * Can also be used to update keys for plugin-scoped configs in config_plugin table. + * In that case it doesn't affect $CFG. * * @param string $name the key to set * @param string $value the value to set @@ -421,7 +421,7 @@ function set_config($name, $value, $plugin=NULL) { 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 { @@ -442,15 +442,15 @@ function set_config($name, $value, $plugin=NULL) { } /** - * Get configuration values from the global config table + * 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. + * existing values. * - * @param string $plugin - * @param string $name + * @param string $plugin + * @param string $name * @uses $CFG * @return hash-like object or single value * @@ -487,21 +487,21 @@ function get_config($plugin=NULL, $name=NULL) { if (!isset($localcfg[$config->name])) { $localcfg[$config->name] = $config->value; } else { - if ($localcfg[$config->name] != $config->value ) { + 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; } - + } } @@ -609,7 +609,7 @@ function unset_user_preference($name, $userid=NULL) { if (isset($USER->preference[$name])) { unset($USER->preference[$name]); } - + //Then from DB return delete_records('user_preferences', 'userid', $userid, 'name', $name); } @@ -952,7 +952,7 @@ function usertimezone($timezone=99) { else { return 'GMT'.$tz; } - + } /** @@ -1012,7 +1012,7 @@ function get_timezone_record($timezonename) { return $cache[$timezonename]; } - return get_record_sql('SELECT * FROM '.$CFG->prefix.'timezone + return get_record_sql('SELECT * FROM '.$CFG->prefix.'timezone WHERE name = '.$db->qstr($timezonename).' ORDER BY year DESC', true); } @@ -1238,7 +1238,7 @@ function find_day_in_month($startday, $weekday, $month, $year) { } return $lastinmonth; - + } else { @@ -1673,7 +1673,7 @@ function sync_metacourse($metacourseid) { // 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)) { @@ -1702,14 +1702,14 @@ function sync_metacourse($metacourseid) { foreach ($userstoadd as $userid) { enrol_student($userid,$metacourseid,0,0,'metacourse'); } - + // 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 FROM {$CFG->prefix}user_students u, - {$CFG->prefix}course_meta mc - WHERE u.course = mc.child_course + {$CFG->prefix}course_meta mc + WHERE u.course = mc.child_course AND userid = $enrol->userid AND mc.parent_course = $metacourseid")) { $enrol->timestart = $maxmin->timestart; @@ -2249,7 +2249,7 @@ function update_user_record($username) { $confkey = 'field_updatelocal_' . $key; if (!empty($authconfig->$confkey) && $authconfig->$confkey === 'onlogin') { $value = addslashes(stripslashes($value)); // Just in case - set_field('user', $key, $value, 'username', $username) + set_field('user', $key, $value, 'username', $username) || error_log("Error updating $key for $username"); } } @@ -2432,7 +2432,7 @@ function authenticate_user_login($username, $password) { * * @uses $CFG * @uses SITEID - * @param string $field The user field to be checked for a given value. + * @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. */ @@ -2509,7 +2509,7 @@ 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 */ @@ -2533,9 +2533,9 @@ function set_login_session_preferences() { /** * Enrols (or re-enrols) a student in a given course * - * NOTE: Defaults to 'manual' enrolment - enrolment plugins + * NOTE: Defaults to 'manual' enrolment - enrolment plugins * must set it explicitly. - * + * * @param int $courseid The id of the course that is being viewed * @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user. * @param int $timestart ? @@ -2948,35 +2948,35 @@ function remove_course_contents($courseid, $showfeedback=true) { if (delete_records("grade_category", "courseid", $course->id)) { if ($showfeedback) { - notify("$strdeleted grade categories"); + notify("$strdeleted grade categories"); } } else { $result = false; } if (delete_records("grade_exceptions", "courseid", $course->id)) { if ($showfeedback) { - notify("$strdeleted grade exceptions"); + notify("$strdeleted grade exceptions"); } } else { $result = false; } if (delete_records("grade_item", "courseid", $course->id)) { if ($showfeedback) { - notify("$strdeleted grade items"); + notify("$strdeleted grade items"); } } else { $result = false; } if (delete_records("grade_letter", "courseid", $course->id)) { if ($showfeedback) { - notify("$strdeleted grade letters"); + notify("$strdeleted grade letters"); } } else { $result = false; } if (delete_records("grade_preferences", "courseid", $course->id)) { if ($showfeedback) { - notify("$strdeleted grade preferences"); + notify("$strdeleted grade preferences"); } } else { $result = false; @@ -3557,7 +3557,7 @@ function reset_password_and_mail($user) { $external = false; if (!is_internal_auth($user->auth)) { include_once($CFG->dirroot . '/auth/' . $user->auth . '/lib.php'); - if (empty($CFG->{'auth_'.$user->auth.'_stdchangepassword'}) + 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; @@ -4004,7 +4004,7 @@ function get_directory_list($rootdir, $excludefile='', $descend=true, $getdirs=f * @param string $dirName - full path to directory * @return boolean true if successful, false if error **/ - + function delDirContents($dirName) { if(empty($dirName)) { return true; @@ -4048,7 +4048,7 @@ function get_directory_size($rootdir, $excludefile='') { return get_real_size(intval($output[0]).'k'); // we told it to return k. } } - + $size = 0; if (!is_dir($rootdir)) { // Must be a directory @@ -4295,7 +4295,7 @@ function clean_getstring_data( $a ) { $new_a_vars[$fname] = clean_getstring_data( $a_var ); } return (object)$new_a_vars; - } + } else { return $a; } @@ -4544,7 +4544,7 @@ function get_list_of_languages() { fwrite($file, "$key $value\n"); } fclose($file); - } + } } @@ -5076,11 +5076,10 @@ function check_php_version($version='4.1.0') { } // the proper string - Gecko/CCYYMMDD Vendor/Version - if (ereg("^([a-zA-Z]+)/([0-9]+\.[0-9]+) \((.*)\) (.*)$", $agent, $match)) { - if (ereg("^([Gecko]+)/([0-9]+)",$match[4], $reldate)) { - if ($reldate[2] > $version) { - return true; - } + // Faster version and work-a-round No IDN problem. + if (preg_match("/Gecko\/([0-9]+)/i", $agent, $match)) { + if ($match[1] > $version) { + return true; } } break; @@ -5424,7 +5423,7 @@ function random_string ($length=15) { } /* - * Given some text (which may contain HTML) and an ideal length, + * 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) { @@ -5796,7 +5795,7 @@ function address_in_subnet($addr, $subnetstr) { } /** - * This function sets the $HTTPSPAGEREQUIRED global + * This function sets the $HTTPSPAGEREQUIRED global * (used in some parts of moodle to change some links) * and calculate the proper wwwroot to be used * @@ -6097,7 +6096,7 @@ function unzip_show_status ($list,$removepath) { return ''; } -/** +/** * Cleans a remote address ready to put into the log table */ function cleanremoteaddr($addr) { @@ -6116,13 +6115,13 @@ function cleanremoteaddr($addr) { // 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 + // 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) + 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)) { @@ -6139,7 +6138,7 @@ function cleanremoteaddr($addr) { } else { return array_pop($lanmatches); } - } + } if (count($goodmatches) == 1) { return $goodmatches[0]; } @@ -6229,7 +6228,7 @@ function bounded_number($min, $value, $max) { /** *** get_performance_info() pairs up with init_performance_info() - *** loaded in setup.php. Returns an array with 'html' and 'txt' + *** 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. *** @@ -6242,7 +6241,7 @@ function get_performance_info() { $info['txt'] = me() . ' '; // holds log-friendly representation $info['realtime'] = microtime_diff($PERF->starttime, microtime()); - + $info['html'] .= ''.$info['realtime'].' secs '; $info['txt'] .= 'time: '.$info['realtime'].'s '; @@ -6274,7 +6273,7 @@ function get_performance_info() { if (function_exists('posix_times')) { $ptimes = posix_times(); foreach ($ptimes as $key => $val) { - $info[$key] = $ptimes[$key] - $PERF->startposixtimes[$key]; + $info[$key] = $ptimes[$key] - $PERF->startposixtimes[$key]; } $info['html'] .= "ticks: $info[ticks] user: $info[utime] sys: $info[stime] cuser: $info[cutime] csys: $info[cstime] "; $info['txt'] .= "ticks: $info[ticks] user: $info[utime] sys: $info[stime] cuser: $info[cutime] csys: $info[cstime] "; @@ -6299,7 +6298,7 @@ function get_performance_info() { $info['html'] .= 'Load average: '.$info['serverload'].' '; $info['txt'] .= 'serverload: '.$info['serverload']; } - + $info['html'] = '
'.$info['html'].'
'; return $info; @@ -6328,7 +6327,7 @@ function remove_dir($dir, $content_only=false) { } } closedir($handle); - if ($content_only) { + if ($content_only) { return true; } return rmdir($dir);