|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 require('../config.php'); 00028 00029 redirect_if_major_upgrade_required(); 00030 00031 $testsession = optional_param('testsession', 0, PARAM_INT); // test session works properly 00032 $cancel = optional_param('cancel', 0, PARAM_BOOL); // redirect to frontpage, needed for loginhttps 00033 00034 if ($cancel) { 00035 redirect(new moodle_url('/')); 00036 } 00037 00038 //HTTPS is required in this page when $CFG->loginhttps enabled 00039 $PAGE->https_required(); 00040 00041 $context = get_context_instance(CONTEXT_SYSTEM); 00042 $PAGE->set_url("$CFG->httpswwwroot/login/index.php"); 00043 $PAGE->set_context($context); 00044 $PAGE->set_pagelayout('login'); 00045 00047 $errormsg = ''; 00048 $errorcode = 0; 00049 00050 // login page requested session test 00051 if ($testsession) { 00052 if ($testsession == $USER->id) { 00053 if (isset($SESSION->wantsurl)) { 00054 $urltogo = $SESSION->wantsurl; 00055 } else { 00056 $urltogo = $CFG->wwwroot.'/'; 00057 } 00058 unset($SESSION->wantsurl); 00059 redirect($urltogo); 00060 } else { 00061 // TODO: try to find out what is the exact reason why sessions do not work 00062 $errormsg = get_string("cookiesnotenabled"); 00063 $errorcode = 1; 00064 } 00065 } 00066 00068 if (!empty($SESSION->has_timed_out)) { 00069 $session_has_timed_out = true; 00070 unset($SESSION->has_timed_out); 00071 } else { 00072 $session_has_timed_out = false; 00073 } 00074 00076 $frm = false; 00077 $user = false; 00078 00079 $authsequence = get_enabled_auth_plugins(true); // auths, in sequence 00080 foreach($authsequence as $authname) { 00081 $authplugin = get_auth_plugin($authname); 00082 $authplugin->loginpage_hook(); 00083 } 00084 00085 00087 $site = get_site(); 00088 00089 $loginsite = get_string("loginsite"); 00090 $PAGE->navbar->add($loginsite); 00091 00092 if ($user !== false or $frm !== false or $errormsg !== '') { 00093 // some auth plugin already supplied full user, fake form data or prevented user login with error message 00094 00095 } else if (!empty($SESSION->wantsurl) && file_exists($CFG->dirroot.'/login/weblinkauth.php')) { 00096 // Handles the case of another Moodle site linking into a page on this site 00097 //TODO: move weblink into own auth plugin 00098 include($CFG->dirroot.'/login/weblinkauth.php'); 00099 if (function_exists('weblink_auth')) { 00100 $user = weblink_auth($SESSION->wantsurl); 00101 } 00102 if ($user) { 00103 $frm->username = $user->username; 00104 } else { 00105 $frm = data_submitted(); 00106 } 00107 00108 } else { 00109 $frm = data_submitted(); 00110 } 00111 00113 00114 if ($frm and isset($frm->username)) { // Login WITH cookies 00115 00116 $frm->username = trim(moodle_strtolower($frm->username)); 00117 00118 if (is_enabled_auth('none') ) { 00119 if ($frm->username !== clean_param($frm->username, PARAM_USERNAME)) { 00120 $errormsg = get_string('username').': '.get_string("invalidusername"); 00121 $errorcode = 2; 00122 $user = null; 00123 } 00124 } 00125 00126 if ($user) { 00127 //user already supplied by aut plugin prelogin hook 00128 } else if (($frm->username == 'guest') and empty($CFG->guestloginbutton)) { 00129 $user = false; 00130 $frm = false; 00131 } else { 00132 if (empty($errormsg)) { 00133 $user = authenticate_user_login($frm->username, $frm->password); 00134 } 00135 } 00136 00137 // Intercept 'restored' users to provide them with info & reset password 00138 if (!$user and $frm and is_restored_user($frm->username)) { 00139 $PAGE->set_title(get_string('restoredaccount')); 00140 $PAGE->set_heading($site->fullname); 00141 echo $OUTPUT->header(); 00142 echo $OUTPUT->heading(get_string('restoredaccount')); 00143 echo $OUTPUT->box(get_string('restoredaccountinfo'), 'generalbox boxaligncenter'); 00144 require_once('restored_password_form.php'); // Use our "supplanter" login_forgot_password_form. MDL-20846 00145 $form = new login_forgot_password_form('forgot_password.php', array('username' => $frm->username)); 00146 $form->display(); 00147 echo $OUTPUT->footer(); 00148 die; 00149 } 00150 00151 update_login_count(); 00152 00153 if ($user) { 00154 00155 // language setup 00156 if (isguestuser($user)) { 00157 // no predefined language for guests - use existing session or default site lang 00158 unset($user->lang); 00159 00160 } else if (!empty($user->lang)) { 00161 // unset previous session language - use user preference instead 00162 unset($SESSION->lang); 00163 } 00164 00165 if (empty($user->confirmed)) { // This account was never confirmed 00166 $PAGE->set_title(get_string("mustconfirm")); 00167 $PAGE->set_heading($site->fullname); 00168 echo $OUTPUT->header(); 00169 echo $OUTPUT->heading(get_string("mustconfirm")); 00170 echo $OUTPUT->box(get_string("emailconfirmsent", "", $user->email), "generalbox boxaligncenter"); 00171 echo $OUTPUT->footer(); 00172 die; 00173 } 00174 00176 add_to_log(SITEID, 'user', 'login', "view.php?id=$USER->id&course=".SITEID, 00177 $user->id, 0, $user->id); 00178 complete_user_login($user); 00179 00180 // sets the username cookie 00181 if (!empty($CFG->nolastloggedin)) { 00182 // do not store last logged in user in cookie 00183 // auth plugins can temporarily override this from loginpage_hook() 00184 // do not save $CFG->nolastloggedin in database! 00185 00186 } else if (empty($CFG->rememberusername) or ($CFG->rememberusername == 2 and empty($frm->rememberusername))) { 00187 // no permanent cookies, delete old one if exists 00188 set_moodle_cookie(''); 00189 00190 } else { 00191 set_moodle_cookie($USER->username); 00192 } 00193 00195 if (user_not_fully_set_up($USER)) { 00196 $urltogo = $CFG->wwwroot.'/user/edit.php'; 00197 // We don't delete $SESSION->wantsurl yet, so we get there later 00198 00199 } else if (isset($SESSION->wantsurl) and (strpos($SESSION->wantsurl, $CFG->wwwroot) === 0 or strpos($SESSION->wantsurl, str_replace('http://', 'https://', $CFG->wwwroot)) === 0)) { 00200 $urltogo = $SESSION->wantsurl; 00201 unset($SESSION->wantsurl); 00202 00203 } else { 00204 // no wantsurl stored or external - go to homepage 00205 $urltogo = $CFG->wwwroot.'/'; 00206 unset($SESSION->wantsurl); 00207 } 00208 00210 if (!empty($CFG->defaulthomepage) && $CFG->defaulthomepage == HOMEPAGE_MY && !is_siteadmin() && !isguestuser()) { 00211 if ($urltogo == $CFG->wwwroot or $urltogo == $CFG->wwwroot.'/' or $urltogo == $CFG->wwwroot.'/index.php') { 00212 $urltogo = $CFG->wwwroot.'/my/'; 00213 } 00214 } 00215 00216 00219 $userauth = get_auth_plugin($USER->auth); 00220 if (!empty($userauth->config->expiration) and $userauth->config->expiration == 1) { 00221 if ($userauth->can_change_password()) { 00222 $passwordchangeurl = $userauth->change_password_url(); 00223 if (!$passwordchangeurl) { 00224 $passwordchangeurl = $CFG->httpswwwroot.'/login/change_password.php'; 00225 } 00226 } else { 00227 $passwordchangeurl = $CFG->httpswwwroot.'/login/change_password.php'; 00228 } 00229 $days2expire = $userauth->password_expire($USER->username); 00230 $PAGE->set_title("$site->fullname: $loginsite"); 00231 $PAGE->set_heading("$site->fullname"); 00232 if (intval($days2expire) > 0 && intval($days2expire) < intval($userauth->config->expiration_warning)) { 00233 echo $OUTPUT->header(); 00234 echo $OUTPUT->confirm(get_string('auth_passwordwillexpire', 'auth', $days2expire), $passwordchangeurl, $urltogo); 00235 echo $OUTPUT->footer(); 00236 exit; 00237 } elseif (intval($days2expire) < 0 ) { 00238 echo $OUTPUT->header(); 00239 echo $OUTPUT->confirm(get_string('auth_passwordisexpired', 'auth'), $passwordchangeurl, $urltogo); 00240 echo $OUTPUT->footer(); 00241 exit; 00242 } 00243 } 00244 00245 reset_login_count(); 00246 00247 // test the session actually works by redirecting to self 00248 $SESSION->wantsurl = $urltogo; 00249 redirect(new moodle_url(get_login_url(), array('testsession'=>$USER->id))); 00250 00251 } else { 00252 if (empty($errormsg)) { 00253 $errormsg = get_string("invalidlogin"); 00254 $errorcode = 3; 00255 } 00256 } 00257 } 00258 00260 if ($session_has_timed_out and !data_submitted()) { 00261 $errormsg = get_string('sessionerroruser', 'error'); 00262 $errorcode = 4; 00263 } 00264 00266 00267 if (empty($SESSION->wantsurl)) { 00268 $SESSION->wantsurl = (array_key_exists('HTTP_REFERER',$_SERVER) && 00269 $_SERVER["HTTP_REFERER"] != $CFG->wwwroot && 00270 $_SERVER["HTTP_REFERER"] != $CFG->wwwroot.'/' && 00271 $_SERVER["HTTP_REFERER"] != $CFG->httpswwwroot.'/login/' && 00272 $_SERVER["HTTP_REFERER"] != $CFG->httpswwwroot.'/login/index.php') 00273 ? $_SERVER["HTTP_REFERER"] : NULL; 00274 } 00275 00277 if (!empty($CFG->alternateloginurl)) { 00278 $loginurl = $CFG->alternateloginurl; 00279 00280 if (strpos($SESSION->wantsurl, $loginurl) === 0) { 00281 //we do not want to return to alternate url 00282 $SESSION->wantsurl = NULL; 00283 } 00284 00285 if ($errorcode) { 00286 if (strpos($loginurl, '?') === false) { 00287 $loginurl .= '?'; 00288 } else { 00289 $loginurl .= '&'; 00290 } 00291 $loginurl .= 'errorcode='.$errorcode; 00292 } 00293 00294 redirect($loginurl); 00295 } 00296 00297 // make sure we really are on the https page when https login required 00298 $PAGE->verify_https_required(); 00299 00301 00302 if (!isset($frm) or !is_object($frm)) { 00303 $frm = new stdClass(); 00304 } 00305 00306 if (empty($frm->username) && $authsequence[0] != 'shibboleth') { // See bug 5184 00307 if (!empty($_GET["username"])) { 00308 $frm->username = clean_param($_GET["username"], PARAM_RAW); // we do not want data from _POST here 00309 } else { 00310 $frm->username = get_moodle_cookie(); 00311 } 00312 00313 $frm->password = ""; 00314 } 00315 00316 if (!empty($frm->username)) { 00317 $focus = "password"; 00318 } else { 00319 $focus = "username"; 00320 } 00321 00322 if (!empty($CFG->registerauth) or is_enabled_auth('none') or !empty($CFG->auth_instructions)) { 00323 $show_instructions = true; 00324 } else { 00325 $show_instructions = false; 00326 } 00327 00328 $potentialidps = array(); 00329 foreach($authsequence as $authname) { 00330 $authplugin = get_auth_plugin($authname); 00331 $potentialidps = array_merge($potentialidps, $authplugin->loginpage_idp_list($SESSION->wantsurl)); 00332 } 00333 00334 $PAGE->set_title("$site->fullname: $loginsite"); 00335 $PAGE->set_heading("$site->fullname"); 00336 00337 echo $OUTPUT->header(); 00338 00339 if (isloggedin() and !isguestuser()) { 00340 // prevent logging when already logged in, we do not want them to relogin by accident because sesskey would be changed 00341 echo $OUTPUT->box_start(); 00342 $logout = new single_button(new moodle_url($CFG->httpswwwroot.'/login/logout.php', array('sesskey'=>sesskey(),'loginpage'=>1)), get_string('logout'), 'post'); 00343 $continue = new single_button(new moodle_url($CFG->httpswwwroot.'/login/index.php', array('cancel'=>1)), get_string('cancel'), 'get'); 00344 echo $OUTPUT->confirm(get_string('alreadyloggedin', 'error', fullname($USER)), $logout, $continue); 00345 echo $OUTPUT->box_end(); 00346 } else { 00347 include("index_form.html"); 00348 if (!empty($CFG->loginpageautofocus)) { 00349 //focus username or password 00350 $PAGE->requires->js_init_call('M.util.focus_login_form', null, true); 00351 } 00352 } 00353 00354 echo $OUTPUT->footer();