PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/login/index.php

https://bitbucket.org/ciceidev/cicei_moodle_conditional_activities
PHP | 328 lines | 230 code | 67 blank | 31 comment | 88 complexity | 49386ff3670cfa30973a2b43f23422d8 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php // $Id$
  2. require_once("../config.php");
  3. // check if major upgrade needed - also present in /index.php
  4. if ((int)$CFG->version < 2006101100) { //1.7 or older
  5. @require_logout();
  6. redirect("$CFG->wwwroot/$CFG->admin/");
  7. }
  8. $loginguest = optional_param('loginguest', 0, PARAM_BOOL); // determines whether visitors are logged in as guest automatically
  9. $testcookies = optional_param('testcookies', 0, PARAM_BOOL); // request cookie test
  10. //initialize variables
  11. $errormsg = '';
  12. $errorcode = 0;
  13. /// Check for timed out sessions
  14. if (!empty($SESSION->has_timed_out)) {
  15. $session_has_timed_out = true;
  16. $SESSION->has_timed_out = false;
  17. } else {
  18. $session_has_timed_out = false;
  19. }
  20. /// Check if the guest user exists. If not, create one.
  21. if (! record_exists('user', 'username', 'guest', 'mnethostid', $CFG->mnet_localhost_id)) {
  22. if (! $guest = create_guest_record()) {
  23. notify('Could not create guest user record !!!');
  24. }
  25. }
  26. // setup and verify auth settings
  27. if (!isset($CFG->registerauth)) {
  28. set_config('registerauth', '');
  29. }
  30. if (!isset($CFG->auth_instructions)) {
  31. set_config('auth_instructions', '');
  32. }
  33. // auth plugins may override these - SSO anyone?
  34. $frm = false;
  35. $user = false;
  36. $authsequence = get_enabled_auth_plugins(true); // auths, in sequence
  37. foreach($authsequence as $authname) {
  38. $authplugin = get_auth_plugin($authname);
  39. $authplugin->loginpage_hook();
  40. }
  41. //HTTPS is potentially required in this page
  42. httpsrequired();
  43. /// Define variables used in page
  44. if (!$site = get_site()) {
  45. error("No site found!");
  46. }
  47. if (empty($CFG->langmenu)) {
  48. $langmenu = "";
  49. } else {
  50. $currlang = current_language();
  51. $langs = get_list_of_languages();
  52. $langlabel = get_accesshide(get_string('language'));
  53. $langmenu = popup_form ("$CFG->httpswwwroot/login/index.php?lang=", $langs, "chooselang", $currlang, "", "", "", true, 'self', $langlabel);
  54. }
  55. $loginsite = get_string("loginsite");
  56. $navlinks = array(array('name' => $loginsite, 'link' => null, 'type' => 'misc'));
  57. $navigation = build_navigation($navlinks);
  58. if ($user !== false or $frm !== false) {
  59. // some auth plugin already supplied these
  60. } else if ((!empty($SESSION->wantsurl) and strstr($SESSION->wantsurl,'username=guest')) or $loginguest) {
  61. /// Log in as guest automatically (idea from Zbigniew Fiedorowicz)
  62. $frm->username = 'guest';
  63. $frm->password = 'guest';
  64. } else if (!empty($SESSION->wantsurl) && file_exists($CFG->dirroot.'/login/weblinkauth.php')) {
  65. // Handles the case of another Moodle site linking into a page on this site
  66. //TODO: move weblink into own auth plugin
  67. include($CFG->dirroot.'/login/weblinkauth.php');
  68. if (function_exists('weblink_auth')) {
  69. $user = weblink_auth($SESSION->wantsurl);
  70. }
  71. if ($user) {
  72. $frm->username = $user->username;
  73. } else {
  74. $frm = data_submitted();
  75. }
  76. } else {
  77. $frm = data_submitted();
  78. }
  79. /// Check if the user has actually submitted login data to us
  80. if (empty($CFG->usesid) and $testcookies and (get_moodle_cookie() == '')) { // Login without cookie when test requested
  81. $errormsg = get_string("cookiesnotenabled");
  82. $errorcode = 1;
  83. } else if ($frm) { // Login WITH cookies
  84. $frm->username = trim(moodle_strtolower($frm->username));
  85. if (is_enabled_auth('none') && empty($CFG->extendedusernamechars)) {
  86. $string = eregi_replace("[^(-\.[:alnum:])]", "", $frm->username);
  87. if (strcmp($frm->username, $string)) {
  88. $errormsg = get_string('username').': '.get_string("alphanumerical");
  89. $errorcode = 2;
  90. $user = null;
  91. }
  92. }
  93. if ($user) {
  94. //user already supplied by aut plugin prelogin hook
  95. } else if (($frm->username == 'guest') and empty($CFG->guestloginbutton)) {
  96. $user = false; /// Can't log in as guest if guest button is disabled
  97. $frm = false;
  98. } else {
  99. if (empty($errormsg)) {
  100. $user = authenticate_user_login($frm->username, $frm->password);
  101. }
  102. }
  103. // Intercept 'restored' users to provide them with info & reset password
  104. if (!$user and $frm and is_restored_user($frm->username)) {
  105. print_header("$site->fullname: $loginsite", $site->fullname, $navigation, '',
  106. '', true, '<div class="langmenu">'.$langmenu.'</div>');
  107. print_heading(get_string('restoredaccount'));
  108. print_simple_box(get_string('restoredaccountinfo'), 'center', '70%');
  109. require_once('restored_password_form.php'); // Use our "supplanter" login_forgot_password_form. MDL-20846
  110. $form = new login_forgot_password_form('forgot_password.php', array('username' => $frm->username));
  111. $form->display();
  112. print_footer();
  113. die;
  114. }
  115. update_login_count();
  116. if ($user) {
  117. // language setup
  118. if ($user->username == 'guest') {
  119. // no predefined language for guests - use existing session or default site lang
  120. unset($user->lang);
  121. } else if (!empty($user->lang)) {
  122. // unset previous session language - use user preference instead
  123. unset($SESSION->lang);
  124. }
  125. if (empty($user->confirmed)) { // This account was never confirmed
  126. print_header(get_string("mustconfirm"), get_string("mustconfirm") );
  127. print_heading(get_string("mustconfirm"));
  128. print_simple_box(get_string("emailconfirmsent", "", $user->email), "center");
  129. print_footer();
  130. die;
  131. }
  132. if ($frm->password == 'changeme') {
  133. //force the change
  134. set_user_preference('auth_forcepasswordchange', true, $user->id);
  135. }
  136. /// Let's get them all set up.
  137. add_to_log(SITEID, 'user', 'login', "view.php?id=$USER->id&course=".SITEID,
  138. $user->id, 0, $user->id);
  139. $USER = complete_user_login($user);
  140. /// Prepare redirection
  141. if (user_not_fully_set_up($USER)) {
  142. $urltogo = $CFG->wwwroot.'/user/edit.php';
  143. // We don't delete $SESSION->wantsurl yet, so we get there later
  144. } else if (isset($SESSION->wantsurl) and (strpos($SESSION->wantsurl, $CFG->wwwroot) === 0 or strpos($SESSION->wantsurl, str_replace('http://', 'https://', $CFG->wwwroot)) === 0)) {
  145. $urltogo = $SESSION->wantsurl; /// Because it's an address in this site
  146. unset($SESSION->wantsurl);
  147. } else {
  148. // no wantsurl stored or external - go to homepage
  149. $urltogo = $CFG->wwwroot.'/';
  150. unset($SESSION->wantsurl);
  151. }
  152. /// Go to my-moodle page instead of homepage if mymoodleredirect enabled
  153. if (!has_capability('moodle/site:config',get_context_instance(CONTEXT_SYSTEM)) and !empty($CFG->mymoodleredirect) and !isguest()) {
  154. if ($urltogo == $CFG->wwwroot or $urltogo == $CFG->wwwroot.'/' or $urltogo == $CFG->wwwroot.'/index.php') {
  155. $urltogo = $CFG->wwwroot.'/my/';
  156. }
  157. }
  158. /// check if user password has expired
  159. /// Currently supported only for ldap-authentication module
  160. $userauth = get_auth_plugin($USER->auth);
  161. if (!empty($userauth->config->expiration) and $userauth->config->expiration == 1) {
  162. if ($userauth->can_change_password()) {
  163. $passwordchangeurl = $userauth->change_password_url();
  164. if(!$passwordchangeurl) {
  165. $passwordchangeurl = $CFG->httpswwwroot.'/login/change_password.php';
  166. }
  167. } else {
  168. $passwordchangeurl = $CFG->httpswwwroot.'/login/change_password.php';
  169. }
  170. $days2expire = $userauth->password_expire($USER->username);
  171. if (intval($days2expire) > 0 && intval($days2expire) < intval($userauth->config->expiration_warning)) {
  172. print_header("$site->fullname: $loginsite", "$site->fullname", $navigation, '', '', true, "<div class=\"langmenu\">$langmenu</div>");
  173. notice_yesno(get_string('auth_passwordwillexpire', 'auth', $days2expire), $passwordchangeurl, $urltogo);
  174. print_footer();
  175. exit;
  176. } elseif (intval($days2expire) < 0 ) {
  177. print_header("$site->fullname: $loginsite", "$site->fullname", $navigation, '', '', true, "<div class=\"langmenu\">$langmenu</div>");
  178. notice_yesno(get_string('auth_passwordisexpired', 'auth'), $passwordchangeurl, $urltogo);
  179. print_footer();
  180. exit;
  181. }
  182. }
  183. reset_login_count();
  184. redirect($urltogo);
  185. exit;
  186. } else {
  187. if (empty($errormsg)) {
  188. $errormsg = get_string("invalidlogin");
  189. $errorcode = 3;
  190. }
  191. if ( !empty($CFG->mnet_dispatcher_mode)
  192. && $CFG->mnet_dispatcher_mode === 'strict'
  193. && is_enabled_auth('mnet')
  194. && record_exists_sql("SELECT h.id FROM {$CFG->prefix}mnet_host h
  195. INNER JOIN {$CFG->prefix}mnet_host2service m ON h.id=m.hostid
  196. INNER JOIN {$CFG->prefix}mnet_service s ON s.id=m.serviceid
  197. WHERE s.name='sso_sp' AND h.deleted=0 AND m.publish = 1")
  198. && record_exists_select('user', "username = '{$frm->username}' AND mnethostid != {$CFG->mnet_localhost_id}")
  199. ) {
  200. $errormsg .= get_string('loginlinkmnetuser', 'mnet', "mnet_email.php?u=$frm->username");
  201. }
  202. }
  203. }
  204. /// Detect problems with timedout sessions
  205. if ($session_has_timed_out and !data_submitted()) {
  206. $errormsg = get_string('sessionerroruser', 'error');
  207. $errorcode = 4;
  208. }
  209. /// First, let's remember where the user was trying to get to before they got here
  210. if (empty($SESSION->wantsurl)) {
  211. $SESSION->wantsurl = (array_key_exists('HTTP_REFERER',$_SERVER) &&
  212. $_SERVER["HTTP_REFERER"] != $CFG->wwwroot &&
  213. $_SERVER["HTTP_REFERER"] != $CFG->wwwroot.'/' &&
  214. $_SERVER["HTTP_REFERER"] != $CFG->httpswwwroot.'/login/' &&
  215. $_SERVER["HTTP_REFERER"] != $CFG->httpswwwroot.'/login/index.php')
  216. ? $_SERVER["HTTP_REFERER"] : NULL;
  217. }
  218. /// Redirect to alternative login URL if needed
  219. if (!empty($CFG->alternateloginurl)) {
  220. $loginurl = $CFG->alternateloginurl;
  221. if (strpos($SESSION->wantsurl, $loginurl) === 0) {
  222. //we do not want to return to alternate url
  223. $SESSION->wantsurl = NULL;
  224. }
  225. if ($errorcode) {
  226. if (strpos($loginurl, '?') === false) {
  227. $loginurl .= '?';
  228. } else {
  229. $loginurl .= '&';
  230. }
  231. $loginurl .= 'errorcode='.$errorcode;
  232. }
  233. redirect($loginurl);
  234. }
  235. /// Generate the login page with forms
  236. if (get_moodle_cookie() == '') {
  237. set_moodle_cookie('nobody'); // To help search for cookies
  238. }
  239. if (empty($frm->username) && $authsequence[0] != 'shibboleth') { // See bug 5184
  240. if (!empty($_GET["username"])) {
  241. $frm->username = $_GET["username"];
  242. } else {
  243. $frm->username = get_moodle_cookie() === 'nobody' ? '' : get_moodle_cookie();
  244. }
  245. $frm->password = "";
  246. }
  247. if (!empty($frm->username)) {
  248. $focus = "password";
  249. } else {
  250. $focus = "username";
  251. }
  252. if (!empty($CFG->registerauth) or is_enabled_auth('none') or !empty($CFG->auth_instructions)) {
  253. $show_instructions = true;
  254. } else {
  255. $show_instructions = false;
  256. }
  257. print_header("$site->fullname: $loginsite", $site->fullname, $navigation, $focus,
  258. '', true, '<div class="langmenu">'.$langmenu.'</div>');
  259. include("index_form.html");
  260. print_footer();
  261. ?>