PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/core/model/modx/processors/security/login.php

https://github.com/gregorysmart/MODx
PHP | 178 lines | 156 code | 13 blank | 9 comment | 54 complexity | 1f82224e6c9036eed87e24f0315a7577 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Properly log in the user and set up the session.
  4. *
  5. * @package modx
  6. * @subpackage processors.security
  7. */
  8. if (!isset($modx->lexicon) || !is_object($modx->lexicon)) {
  9. $modx->getService('lexicon','modLexicon');
  10. }
  11. $modx->lexicon->load('login');
  12. $username = $scriptProperties['username'];
  13. $givenPassword = $scriptProperties['password'];
  14. $rememberme= isset ($scriptProperties['rememberme']) ? ($scriptProperties['rememberme'] == 'on' || $scriptProperties['rememberme'] == true) : false;
  15. $lifetime= (integer) $modx->getOption('lifetime', $scriptProperties, $modx->getOption('session_cookie_lifetime', null, 0));
  16. $loginContext= isset ($scriptProperties['login_context']) ? $scriptProperties['login_context'] : $modx->context->get('key');
  17. $onBeforeLoginParams = array(
  18. 'username' => $username,
  19. 'password' => $givenPassword,
  20. 'attributes' => array(
  21. 'rememberme' => & $rememberme,
  22. 'lifetime' => & $lifetime,
  23. 'loginContext' => & $loginContext
  24. )
  25. );
  26. $rt = false; /* $rt will be an array if the event fires */
  27. if ($loginContext == 'mgr') {
  28. $rt = $modx->invokeEvent("OnBeforeManagerLogin", $onBeforeLoginParams);
  29. } else {
  30. $rt = $modx->invokeEvent("OnBeforeWebLogin", $onBeforeLoginParams);
  31. }
  32. /* If the event fired, loop through the event array and fail if there's an error message */
  33. if (is_array($rt)) {
  34. foreach ($rt as $key => $value) { /* php4 compatible */
  35. if ($value !== true) {
  36. return $modx->error->failure($value);
  37. }
  38. }
  39. unset($key,$value);
  40. }
  41. $user= $modx->getObjectGraph('modUser', '{"Profile":{},"UserSettings":{}}', array ('modUser.username' => $username));
  42. if (!$user) {
  43. $ru = $modx->invokeEvent("OnUserNotFound", array(
  44. 'user' => &$user,
  45. 'username' => $username,
  46. 'password' => $password,
  47. array (
  48. 'rememberme' => $rememberme,
  49. 'lifetime' => $lifetime,
  50. 'loginContext' => $loginContext,
  51. )
  52. ));
  53. if (!empty($ru)) {
  54. foreach ($ru as $obj) {
  55. if (is_object($obj) && $obj instanceof modUser) {
  56. $user = $obj;
  57. break;
  58. }
  59. }
  60. }
  61. if (!is_object($user) || !($user instanceof modUser)) {
  62. return $modx->error->failure($modx->lexicon('login_cannot_locate_account'));
  63. }
  64. }
  65. if (!$user->get('active')) {
  66. return $modx->error->failure($modx->lexicon('login_user_inactive'));
  67. }
  68. $up= & $user->Profile;
  69. $us= & $user->UserSettings;
  70. foreach ($us as $settingPK => $setting) {
  71. $sname= $setting->get('key');
  72. $$sname= $setting->get('value');
  73. }
  74. if ($up->get('failed_logins') >= $modx->getOption('failed_login_attempts') && $up->get('blockeduntil') > time()) {
  75. return $modx->error->failure($modx->lexicon('login_blocked_too_many_attempts'));
  76. }
  77. if ($up->get('failedlogincount') >= $modx->getOption('failed_login_attempts') && $up->get('blockeduntil') < time()) {
  78. $up->set('failedlogincount', 0);
  79. $up->set('blockeduntil', time() - 1);
  80. $up->save();
  81. }
  82. if ($up->get('blocked')) {
  83. return $modx->error->failure($modx->lexicon('login_blocked_admin'));
  84. }
  85. if ($up->get('blockeduntil') > time()) {
  86. return $modx->error->failure($modx->lexicon('login_blocked_error'));
  87. }
  88. if ($up->get('blockedafter') > 0 && $up->get('blockedafter') < time()) {
  89. return $modx->error->failure($modx->lexicon('login_blocked_error'));
  90. }
  91. if (isset ($allowed_ip) && $allowed_ip) {
  92. if (($hostname = gethostbyaddr($_SERVER['REMOTE_ADDR'])) && ($hostname != $_SERVER['REMOTE_ADDR'])) {
  93. if (gethostbyname($hostname) != $_SERVER['REMOTE_ADDR']) {
  94. return $modx->error->failure($modx->lexicon('login_hostname_error'));
  95. }
  96. }
  97. if (!in_array($_SERVER['REMOTE_ADDR'], explode(',', str_replace(' ', '', $allowed_ip)))) {
  98. return $modx->error->failure($modx->lexicon('login_blocked_ip'));
  99. }
  100. }
  101. if (isset ($allowed_days) && $allowed_days) {
  102. $date = getdate();
  103. $day = $date['wday'] + 1;
  104. if (strpos($allowed_days, "{$day}") === false) {
  105. return $modx->error->failure($modx->lexicon('login_blocked_time'));
  106. }
  107. }
  108. $loginAttributes = array(
  109. "user" => & $user,
  110. "password" => $givenPassword,
  111. "rememberme" => $rememberme,
  112. "lifetime" => $lifetime
  113. );
  114. if ($loginContext == 'mgr') {
  115. $rt = $modx->invokeEvent("OnManagerAuthentication", $loginAttributes);
  116. } else {
  117. $rt = $modx->invokeEvent("OnWebAuthentication", $loginAttributes);
  118. }
  119. /* check if plugin authenticated the user */
  120. if (!$rt || (is_array($rt) && !in_array(true, $rt))) {
  121. /* check user password - local authentication */
  122. if($user->get('password') != md5($givenPassword)) {
  123. return $modx->error->failure($modx->lexicon('login_username_password_incorrect'));
  124. }
  125. }
  126. $user->addSessionContext($loginContext);
  127. if ($rememberme) {
  128. $_SESSION['modx.' . $loginContext . '.session.cookie.lifetime']= $lifetime;
  129. } else {
  130. $_SESSION['modx.' . $loginContext . '.session.cookie.lifetime']= 0;
  131. }
  132. $postLoginAttributes = array(
  133. 'user' => $user,
  134. 'attributes' => array(
  135. 'rememberme' => $rememberme,
  136. 'lifetime' => $lifetime,
  137. 'loginContext' => $loginContext
  138. )
  139. );
  140. if ($loginContext == 'mgr') {
  141. $rt = $modx->invokeEvent("OnManagerLogin", $postLoginAttributes);
  142. } else {
  143. $modx->invokeEvent("OnWebLogin", $postLoginAttributes);
  144. }
  145. $returnUrl = isset($scriptProperties['returnUrl']) ? $scriptProperties['returnUrl'] : '';
  146. $response = array('url' => $returnUrl);
  147. switch ($loginContext) {
  148. case 'mgr':
  149. $manager_login_startup_url = $modx->getOption('manager_url', null, $returnUrl);
  150. if (!empty($manager_login_startup)) {
  151. $manager_login_startup= intval($manager_login_startup);
  152. if ($manager_login_startup) $manager_login_startup_url .= '?id=' . $manager_login_startup;
  153. }
  154. $response= array('url' => $manager_login_startup_url);
  155. break;
  156. case 'web':
  157. default:
  158. $login_startup_url = $returnUrl;
  159. if (!empty($login_startup)) {
  160. $login_startup = intval($login_startup);
  161. if ($login_startup) $login_startup_url = $modx->makeUrl($login_startup, $loginContext, '', 'full');
  162. }
  163. $response= array('url' => $login_startup_url);
  164. }
  165. return $modx->error->success('', $response);