if (get_string_manager()->translation_exists($lang, false)) { $SESSION->lang = $lang; } } unset($lang); setup_lang_from_browser(); if (empty($CFG->lang)) { if (empty($SESSION->lang)) { $CFG->lang = 'en'; } else { $CFG->lang = $SESSION->lang; } } // Set the default site locale, a lot of the stuff may depend on this // it is definitely too late to call this first in require_login()! moodle_setlocale(); if (!empty($CFG->debugvalidators) and !empty($CFG->guestloginbutton)) { if ($CFG->theme == 'standard' or $CFG->theme == 'standardwhite') { // Temporary measure to help with XHTML validation if (isset($_SERVER['HTTP_USER_AGENT']) and empty($USER->id)) { // Allow W3CValidator in as user called w3cvalidator (or guest) if (strpos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') !== false or strpos($_SERVER['HTTP_USER_AGENT'], 'Cynthia') !== false) { if ($user = get_complete_user_data("username", "w3cvalidator")) { $user->ignoresesskey = true; } else { $user = guest_user(); } session_set_user($user); } } }
/** * Set the current course. This sets both $PAGE->course and $COURSE. It also * sets the right theme and locale. * * Normally you don't need to call this function yourself, require_login will * call it for you if you pass a $course to it. You can use this function * on pages that do need to call require_login(). * * Sets $PAGE->context to the course context, if it is not already set. * * @param stdClass $course the course to set as the global course. * @throws coding_exception */ public function set_course($course) { global $COURSE, $PAGE, $CFG, $SITE; if (empty($course->id)) { throw new coding_exception('$course passed to moodle_page::set_course does not look like a proper course object.'); } $this->ensure_theme_not_set(); if (!empty($this->_course->id) && $this->_course->id != $course->id) { $this->_categories = null; } $this->_course = clone $course; if ($this === $PAGE) { $COURSE = $this->_course; moodle_setlocale(); } if (!$this->_context) { $this->set_context(context_course::instance($this->_course->id)); } // Notify course format that this page is set for the course. if ($this->_course->id != $SITE->id) { require_once $CFG->dirroot . '/course/lib.php'; $courseformat = course_get_format($this->_course); $this->add_body_class('format-' . $courseformat->get_format()); $courseformat->page_set_course($this); } else { $this->add_body_class('format-site'); } }
/** * Set the current course. This sets both $PAGE->course and $COURSE. It also * sets the right theme and locale. * * Normally you don't need to call this function yourself, require_login will * call it for you if you pass a $course to it. You can use this function * on pages that do need to call require_login(). * * Sets $PAGE->context to the course context, if it is not already set. * * @param object the course to set as the global course. */ public function set_course($course) { global $COURSE, $PAGE; if (empty($course->id)) { throw new coding_exception('$course passed to moodle_page::set_course does not look like a proper course object.'); } $this->ensure_theme_not_set(); if (!empty($this->_course->id) && $this->_course->id != $course->id) { $this->_categories = null; } $this->_course = clone($course); if ($this === $PAGE) { $COURSE = $this->_course; moodle_setlocale(); } if (!$this->_context) { $this->set_context(get_context_instance(CONTEXT_COURSE, $this->_course->id)); } }
function report_session_error() { global $CFG, $FULLME; if (empty($CFG->lang)) { $CFG->lang = "en"; } // Set up default theme and locale theme_setup(); moodle_setlocale(); //clear session cookies if (check_php_version('5.2.0')) { //PHP 5.2.0 setcookie('MoodleSession' . $CFG->sessioncookie, '', time() - 3600, $CFG->sessioncookiepath, $CFG->sessioncookiedomain, $CFG->cookiesecure, $CFG->cookiehttponly); setcookie('MoodleSessionTest' . $CFG->sessioncookie, '', time() - 3600, $CFG->sessioncookiepath, $CFG->sessioncookiedomain, $CFG->cookiesecure, $CFG->cookiehttponly); } else { setcookie('MoodleSession' . $CFG->sessioncookie, '', time() - 3600, $CFG->sessioncookiepath, $CFG->sessioncookiedomain, $CFG->cookiesecure); setcookie('MoodleSessionTest' . $CFG->sessioncookie, '', time() - 3600, $CFG->sessioncookiepath, $CFG->sessioncookiedomain, $CFG->cookiesecure); } //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); } redirect($FULLME, get_string('sessionerroruser2', 'error'), 5); }
/** * Force the current language to get strings and dates localised in the given language. * * After calling this function, all strings will be provided in the given language * until this function is called again, or equivalent code is run. * * @param string $language * @return string previous $SESSION->forcelang value */ function force_current_language($language) { global $SESSION; $sessionforcelang = isset($SESSION->forcelang) ? $SESSION->forcelang : ''; if ($language !== $sessionforcelang) { // Seting forcelang to null or an empty string disables it's effect. if (empty($language) || get_string_manager()->translation_exists($language, false)) { $SESSION->forcelang = $language; moodle_setlocale(); } } return $sessionforcelang; }
/** * Notify person responsible for enrolments that some user enrolments will be expired soon, * it is called only if notification of enrollers (aka teachers) is enabled in course. * * This is called repeatedly every day for each course if there are any pending expiration * in the expiration threshold. * * @param int $eid * @param array $users * @param bool $verbose */ protected function notify_expiry_enroller($eid, $users, $verbose) { global $DB, $SESSION; $name = $this->get_name(); $instance = $DB->get_record('enrol', array('id' => $eid, 'enrol' => $name)); $context = context_course::instance($instance->courseid); $course = $DB->get_record('course', array('id' => $instance->courseid)); $enroller = $this->get_enroller($instance->id); $admin = get_admin(); // Some nasty hackery to get strings and dates localised for target user. $sessionlang = isset($SESSION->lang) ? $SESSION->lang : null; if (get_string_manager()->translation_exists($enroller->lang, false)) { $SESSION->lang = $enroller->lang; moodle_setlocale(); } foreach ($users as $key => $info) { $users[$key] = '* ' . $info['fullname'] . ' - ' . userdate($info['timeend'], '', $enroller->timezone); } $a = new stdClass(); $a->course = format_string($course->fullname, true, array('context' => $context)); $a->threshold = get_string('numdays', '', $instance->expirythreshold / (60 * 60 * 24)); $a->users = implode("\n", $users); $a->extendurl = (string) new moodle_url('/enrol/users.php', array('id' => $instance->courseid)); $subject = get_string('expirymessageenrollersubject', 'enrol_' . $name, $a); $body = get_string('expirymessageenrollerbody', 'enrol_' . $name, $a); $message = new stdClass(); $message->notification = 1; $message->component = 'enrol_' . $name; $message->name = 'expiry_notification'; $message->userfrom = $admin; $message->userto = $enroller; $message->subject = $subject; $message->fullmessage = $body; $message->fullmessageformat = FORMAT_MARKDOWN; $message->fullmessagehtml = markdown_to_html($body); $message->smallmessage = $subject; $message->contexturlname = $a->course; $message->contexturl = $a->extendurl; if (message_send($message)) { if ($verbose) { mtrace(" notifying user {$enroller->id} about all expiring {$name} enrolments in course {$instance->courseid}"); } } else { if ($verbose) { mtrace(" error notifying user {$enroller->id} about all expiring {$name} enrolments in course {$instance->courseid}"); } } if ($SESSION->lang !== $sessionlang) { $SESSION->lang = $sessionlang; moodle_setlocale(); } }
function dimdim_force_language($lang) { /// This function prepares moodle to operate in given language /// usable when $nomoodlecookie = true; /// BEWARE: there must be no $course, $USER or $SESSION global $CFG; if (!empty($CFG->courselang)) { unset($CFG->courselang); } if (!empty($CFG->locale)) { unset($CFG->locale); } $CFG->lang = $lang; moodle_setlocale(); }
/** * Process user enrolment line. * * @param progress_trace $trace * @param string $action * @param int $roleid * @param stdClass $user * @param stdClass $course * @param int $timestart * @param int $timeend * @param bool $buffer_if_future */ protected function process_records(progress_trace $trace, $action, $roleid, $user, $course, $timestart, $timeend, $buffer_if_future = true) { global $CFG, $DB, $SESSION; // Check if timestart is for future processing. if ($timestart > time() and $buffer_if_future) { // Populate into enrol_flatfile table as a future role to be assigned by cron. // Note: since 2.0 future enrolments do not cause problems if you disable guest access. $future_en = new stdClass(); $future_en->action = $action; $future_en->roleid = $roleid; $future_en->userid = $user->id; $future_en->courseid = $course->id; $future_en->timestart = $timestart; $future_en->timeend = $timeend; $future_en->timemodified = time(); $DB->insert_record('enrol_flatfile', $future_en); $trace->output("User {$user->id} will be enrolled later into course {$course->id} using role {$roleid} ({$timestart}, {$timeend})", 1); return; } $context = context_course::instance($course->id); if ($action === 'add') { // Clear the buffer just in case there were some future enrolments. $DB->delete_records('enrol_flatfile', array('userid' => $user->id, 'courseid' => $course->id, 'roleid' => $roleid)); $instance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'flatfile')); if (empty($instance)) { // Only add an enrol instance to the course if non-existent. $enrolid = $this->add_instance($course); $instance = $DB->get_record('enrol', array('id' => $enrolid)); } $notify = false; if ($ue = $DB->get_record('user_enrolments', array('enrolid' => $instance->id, 'userid' => $user->id))) { // Update only. $this->update_user_enrol($instance, $user->id, ENROL_USER_ACTIVE, $roleid, $timestart, $timeend); if (!$DB->record_exists('role_assignments', array('contextid' => $context->id, 'roleid' => $roleid, 'userid' => $user->id, 'component' => 'enrol_flatfile', 'itemid' => $instance->id))) { role_assign($roleid, $user->id, $context->id, 'enrol_flatfile', $instance->id); } $trace->output("User {$user->id} enrolment updated in course {$course->id} using role {$roleid} ({$timestart}, {$timeend})", 1); } else { // Enrol the user with this plugin instance. $this->enrol_user($instance, $user->id, $roleid, $timestart, $timeend); $trace->output("User {$user->id} enrolled in course {$course->id} using role {$roleid} ({$timestart}, {$timeend})", 1); $notify = true; } if ($notify and $this->get_config('mailstudents')) { // Some nasty hackery to get strings and dates localised for target user. $sessionlang = isset($SESSION->lang) ? $SESSION->lang : null; if (get_string_manager()->translation_exists($user->lang, false)) { $SESSION->lang = $user->lang; moodle_setlocale(); } // Send welcome notification to enrolled users. $a = new stdClass(); $a->coursename = format_string($course->fullname, true, array('context' => $context)); $a->profileurl = "{$CFG->wwwroot}/user/view.php?id={$user->id}&course={$course->id}"; $subject = get_string('enrolmentnew', 'enrol', format_string($course->shortname, true, array('context' => $context))); $eventdata = new stdClass(); $eventdata->modulename = 'moodle'; $eventdata->component = 'enrol_flatfile'; $eventdata->name = 'flatfile_enrolment'; $eventdata->userfrom = $this->get_enroller($course->id); $eventdata->userto = $user; $eventdata->subject = $subject; $eventdata->fullmessage = get_string('welcometocoursetext', '', $a); $eventdata->fullmessageformat = FORMAT_PLAIN; $eventdata->fullmessagehtml = ''; $eventdata->smallmessage = ''; if (message_send($eventdata)) { $trace->output("Notified enrolled user", 1); } else { $trace->output("Failed to notify enrolled user", 1); } if ($SESSION->lang !== $sessionlang) { $SESSION->lang = $sessionlang; moodle_setlocale(); } } if ($notify and $this->get_config('mailteachers', 0)) { // Notify person responsible for enrolments. $enroller = $this->get_enroller($course->id); // Some nasty hackery to get strings and dates localised for target user. $sessionlang = isset($SESSION->lang) ? $SESSION->lang : null; if (get_string_manager()->translation_exists($enroller->lang, false)) { $SESSION->lang = $enroller->lang; moodle_setlocale(); } $a = new stdClass(); $a->course = format_string($course->fullname, true, array('context' => $context)); $a->user = fullname($user); $subject = get_string('enrolmentnew', 'enrol', format_string($course->shortname, true, array('context' => $context))); $eventdata = new stdClass(); $eventdata->modulename = 'moodle'; $eventdata->component = 'enrol_flatfile'; $eventdata->name = 'flatfile_enrolment'; $eventdata->userfrom = get_admin(); $eventdata->userto = $enroller; $eventdata->subject = $subject; $eventdata->fullmessage = get_string('enrolmentnewuser', 'enrol', $a); $eventdata->fullmessageformat = FORMAT_PLAIN; $eventdata->fullmessagehtml = ''; $eventdata->smallmessage = ''; if (message_send($eventdata)) { $trace->output("Notified enroller {$eventdata->userto->id}", 1); } else { $trace->output("Failed to notify enroller {$eventdata->userto->id}", 1); } if ($SESSION->lang !== $sessionlang) { $SESSION->lang = $sessionlang; moodle_setlocale(); } } return; } else { if ($action === 'del') { // Clear the buffer just in case there were some future enrolments. $DB->delete_records('enrol_flatfile', array('userid' => $user->id, 'courseid' => $course->id, 'roleid' => $roleid)); $action = $this->get_config('unenrolaction'); if ($action == ENROL_EXT_REMOVED_KEEP) { $trace->output("del action is ignored", 1); return; } // Loops through all enrolment methods, try to unenrol if roleid somehow matches. $instances = $DB->get_records('enrol', array('courseid' => $course->id)); $unenrolled = false; foreach ($instances as $instance) { if (!($ue = $DB->get_record('user_enrolments', array('enrolid' => $instance->id, 'userid' => $user->id)))) { continue; } if ($instance->enrol === 'flatfile') { $plugin = $this; } else { if (!enrol_is_enabled($instance->enrol)) { continue; } if (!($plugin = enrol_get_plugin($instance->enrol))) { continue; } if (!$plugin->allow_unenrol_user($instance, $ue)) { continue; } } // For some reason the del action includes a role name, this complicates everything. $componentroles = array(); $manualroles = array(); $ras = $DB->get_records('role_assignments', array('userid' => $user->id, 'contextid' => $context->id)); foreach ($ras as $ra) { if ($ra->component === '') { $manualroles[$ra->roleid] = $ra->roleid; } else { if ($ra->component === 'enrol_' . $instance->enrol and $ra->itemid == $instance->id) { $componentroles[$ra->roleid] = $ra->roleid; } } } if ($componentroles and !isset($componentroles[$roleid])) { // Do not unenrol using this method, user has some other protected role! continue; } else { if (empty($ras)) { // If user does not have any roles then let's just suspend as many methods as possible. } else { if (!$plugin->roles_protected()) { if (!$componentroles and $manualroles and !isset($manualroles[$roleid])) { // Most likely we want to keep users enrolled because they have some other course roles. continue; } } } } if ($action == ENROL_EXT_REMOVED_UNENROL) { $unenrolled = true; if (!$plugin->roles_protected()) { role_unassign_all(array('contextid' => $context->id, 'userid' => $user->id, 'roleid' => $roleid, 'component' => '', 'itemid' => 0), true); } $plugin->unenrol_user($instance, $user->id); $trace->output("User {$user->id} was unenrolled from course {$course->id} (enrol_{$instance->enrol})", 1); } else { if ($action == ENROL_EXT_REMOVED_SUSPENDNOROLES) { if ($plugin->allow_manage($instance)) { if ($ue->status == ENROL_USER_ACTIVE) { $unenrolled = true; $plugin->update_user_enrol($instance, $user->id, ENROL_USER_SUSPENDED); if (!$plugin->roles_protected()) { role_unassign_all(array('contextid' => $context->id, 'userid' => $user->id, 'component' => 'enrol_' . $instance->enrol, 'itemid' => $instance->id), true); role_unassign_all(array('contextid' => $context->id, 'userid' => $user->id, 'roleid' => $roleid, 'component' => '', 'itemid' => 0), true); } $trace->output("User {$user->id} enrolment was suspended in course {$course->id} (enrol_{$instance->enrol})", 1); } } } } } if (!$unenrolled) { if (0 == $DB->count_records('role_assignments', array('userid' => $user->id, 'contextid' => $context->id))) { role_unassign_all(array('contextid' => $context->id, 'userid' => $user->id, 'component' => '', 'itemid' => 0), true); } $trace->output("User {$user->id} (with role {$roleid}) not unenrolled from course {$course->id}", 1); } return; } } }
/** * Lockout user and send notification email. * * @param stdClass $user */ function login_lock_account($user) { global $CFG, $SESSION; if ($user->mnethostid != $CFG->mnet_localhost_id) { return; } if (isguestuser($user)) { return; } if (get_user_preferences('login_lockout_ignored', 0, $user)) { // This user can not be locked out. return; } $alreadylockedout = get_user_preferences('login_lockout', 0, $user); set_user_preference('login_lockout', time(), $user); if ($alreadylockedout == 0) { $secret = random_string(15); set_user_preference('login_lockout_secret', $secret, $user); // Some nasty hackery to get strings and dates localised for target user. $sessionlang = isset($SESSION->lang) ? $SESSION->lang : null; if (get_string_manager()->translation_exists($user->lang, false)) { $SESSION->lang = $user->lang; moodle_setlocale(); } $site = get_site(); $supportuser = core_user::get_support_user(); $data = new stdClass(); $data->firstname = $user->firstname; $data->lastname = $user->lastname; $data->username = $user->username; $data->sitename = format_string($site->fullname); $data->link = $CFG->wwwroot . '/login/unlock_account.php?u=' . $user->id . '&s=' . $secret; $data->admin = generate_email_signoff(); $message = get_string('lockoutemailbody', 'admin', $data); $subject = get_string('lockoutemailsubject', 'admin', format_string($site->fullname)); if ($message) { // Directly email rather than using the messaging system to ensure its not routed to a popup or jabber. email_to_user($user, $supportuser, $subject, $message); } if ($SESSION->lang !== $sessionlang) { $SESSION->lang = $sessionlang; moodle_setlocale(); } } }
/** * This method is called when the page first moves out of the STATE_BEFORE_HEADER * state. This is our last change to initialise things. */ protected function starting_output() { global $SITE, $CFG; if (empty($CFG->rolesactive)) { $this->_course = new stdClass(); $this->_course->id = 1; moodle_setlocale(); theme_setup(); return; } if (!$this->_course) { $this->set_course($SITE); } $this->initialise_standard_body_classes(); $this->blocks->load_blocks(); }