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

/manager/controllers/default/security/login.class.php

https://github.com/francisreboucas/revolution
PHP | 248 lines | 161 code | 24 blank | 63 comment | 24 complexity | 8ea22d4b85c87e85a1df5859df7bb846 MD5 | raw file
  1. <?php
  2. /**
  3. * Loads the login screen
  4. *
  5. * @package modx
  6. * @subpackage manager.controllers
  7. */
  8. class SecurityLoginManagerController extends modManagerController {
  9. public $loadHeader = false;
  10. public $loadFooter = false;
  11. /**
  12. * Check for any permissions or requirements to load page
  13. * @return bool
  14. */
  15. public function checkPermissions() {
  16. return true;
  17. }
  18. /**
  19. * Register custom CSS/JS for the page
  20. * @return void
  21. */
  22. public function loadCustomCssJs() {}
  23. /**
  24. * Custom logic code here for setting placeholders, etc
  25. * @param array $scriptProperties
  26. * @return mixed
  27. */
  28. public function process(array $scriptProperties = array()) {
  29. $this->handleForgotLoginHash();
  30. $this->preserveReturnUrl();
  31. if (!empty($this->scriptProperties)) {
  32. $this->handlePost();
  33. }
  34. /* invoke OnManagerLoginFormPrerender event */
  35. $eventInfo= $this->modx->invokeEvent('OnManagerLoginFormPrerender');
  36. $eventInfo= is_array($eventInfo) ? implode("\n", $eventInfo) : (string) $eventInfo;
  37. $this->setPlaceholder('onManagerLoginFormPrerender', $eventInfo);
  38. $this->checkForActiveInstallation();
  39. $this->checkForAllowManagerForgotPassword();
  40. /* invoke OnManagerLoginFormRender event */
  41. $eventInfo= $this->modx->invokeEvent('OnManagerLoginFormRender');
  42. $eventInfo= is_array($eventInfo) ? implode("\n", $eventInfo) : (string) $eventInfo;
  43. $eventInfo= str_replace('\'','\\\'',$eventInfo);
  44. $this->setPlaceholder('onManagerLoginFormRender', $eventInfo);
  45. }
  46. public function checkForAllowManagerForgotPassword() {
  47. $allow = $this->modx->getOption('allow_manager_login_forgot_password',null,true);
  48. if ($allow) {
  49. $this->setPlaceholder('allow_forgot_password',true);
  50. }
  51. }
  52. /**
  53. * Handle and sanitize the forgot login hash, if existent
  54. *
  55. * @return void
  56. */
  57. public function handleForgotLoginHash() {
  58. if (isset($this->scriptProperties['modahsh'])) {
  59. $this->scriptProperties['modahsh'] = $this->modx->sanitizeString($this->scriptProperties['modahsh']);
  60. $this->setPlaceholder('modahsh',$this->scriptProperties['modahsh']);
  61. }
  62. }
  63. /**
  64. * If the user is coming from a specific mgr action, preserve the return URL and redirect post-login
  65. * @return void
  66. */
  67. public function preserveReturnUrl() {
  68. if (!empty($_SERVER['REQUEST_URI'])) {
  69. $chars = array("'",'"','(',')',';','>','<','!');
  70. $returnUrl = str_replace($chars,'',$_SERVER['REQUEST_URI']);
  71. $this->setPlaceholder('returnUrl',$returnUrl);
  72. }
  73. }
  74. /**
  75. * Check to see if there's an active installation in process; if so, notify the user.
  76. * @return void
  77. */
  78. public function checkForActiveInstallation() {
  79. if (isset($this->scriptProperties['installGoingOn'])) {
  80. $installGoingOn = $this->modx->sanitizeString($this->scriptProperties['installGoingOn']);
  81. }
  82. if (isset ($installGoingOn)) {
  83. switch ($installGoingOn) {
  84. case 1 : $this->setPlaceholder('login_message',$this->modx->lexicon('login_cancelled_install_in_progress').$this->modx->lexicon('login_message')); break;
  85. case 2 : $this->setPlaceholder('login_message',$this->modx->lexicon('login_cancelled_site_was_updated').$this->modx->lexicon('login_message')); break;
  86. }
  87. }
  88. }
  89. /**
  90. * Handle and sanitize any POST actions that come through
  91. *
  92. * @return void
  93. */
  94. public function handlePost() {
  95. $san = array("'",'"','(',')',';','>','<','../');
  96. foreach ($this->scriptProperties as $k => $v) {
  97. if (!in_array($k,array('returnUrl'))) {
  98. $this->scriptProperties[$k] = str_replace($san,'',$v);
  99. } else {
  100. $chars = array("'",'"','(',')',';','>','<','!','../');
  101. $this->scriptProperties[$k] = str_replace($chars,'',$v);
  102. }
  103. }
  104. /* handle login */
  105. if (!empty($this->scriptProperties['login'])) {
  106. $this->handleLogin();
  107. } else if (!empty($this->scriptProperties['forgotlogin']) && $this->modx->getOption('allow_manager_login_forgot_password',null,true)) {
  108. $this->handleForgotLogin();
  109. }
  110. $this->setPlaceholder('_post',$this->scriptProperties);
  111. }
  112. /**
  113. * Handle when a user attempts to log in
  114. * @return void
  115. */
  116. public function handleLogin() {
  117. $validated = true;
  118. /** @var modUser $user */
  119. $user = $this->modx->getObject('modUser',array(
  120. 'username' => $this->scriptProperties['username'],
  121. ));
  122. /* first if there's an activation hash, process that */
  123. if ($user) {
  124. if (array_key_exists('modahsh', $this->scriptProperties) && !empty($this->scriptProperties['modahsh'])) {
  125. $activated = $user->activatePassword($this->scriptProperties['modahsh']);
  126. if ($activated === false) {
  127. $this->modx->smarty->assign('error_message',$this->modx->lexicon('login_activation_key_err'));
  128. $validated = false;
  129. }
  130. }
  131. }
  132. if ($validated) {
  133. $response = $this->modx->runProcessor('security/login',$this->scriptProperties);
  134. if (($response instanceof modProcessorResponse) && !$response->isError()) {
  135. $url = !empty($this->scriptProperties['returnUrl']) ? $this->scriptProperties['returnUrl'] : $this->modx->getOption('manager_url',null,MODX_MANAGER_URL);
  136. $this->modx->sendRedirect(rtrim($url,'/'),'','','full');
  137. } else {
  138. $errors = $response->getAllErrors();
  139. $error_message = implode("\n",$errors);
  140. $this->setPlaceholder('error_message',$error_message);
  141. }
  142. }
  143. }
  144. /**
  145. * Handles the action when a user forgets their login
  146. *
  147. * @return void
  148. */
  149. public function handleForgotLogin() {
  150. $c = $this->modx->newQuery('modUser');
  151. $c->select(array('modUser.*','Profile.email','Profile.fullname'));
  152. $c->innerJoin('modUserProfile','Profile');
  153. $c->where(array(
  154. 'modUser.username' => $this->scriptProperties['username_reset'],
  155. ));
  156. /** @var modUser $user */
  157. $user = $this->modx->getObject('modUser',$c);
  158. if ($user) {
  159. $activationHash = md5(uniqid(md5($user->get('email') . '/' . $user->get('id')), true));
  160. $this->modx->getService('registry', 'registry.modRegistry');
  161. $this->modx->registry->getRegister('user', 'registry.modDbRegister');
  162. $this->modx->registry->user->connect();
  163. $this->modx->registry->user->subscribe('/pwd/reset/');
  164. $this->modx->registry->user->send('/pwd/reset/', array(md5($user->get('username')) => $activationHash), array('ttl' => 86400));
  165. $newPassword = $user->generatePassword();
  166. $user->set('cachepwd', $newPassword);
  167. $user->save();
  168. /* send activation email */
  169. $message = $this->modx->getOption('forgot_login_email');
  170. $placeholders = $user->toArray();
  171. $placeholders['url_scheme'] = $this->modx->getOption('url_scheme');
  172. $placeholders['http_host'] = $this->modx->getOption('http_host');
  173. $placeholders['manager_url'] = $this->modx->getOption('manager_url');
  174. $placeholders['hash'] = $activationHash;
  175. $placeholders['password'] = $newPassword;
  176. foreach ($placeholders as $k => $v) {
  177. $message = str_replace('[[+'.$k.']]',$v,$message);
  178. }
  179. $this->modx->getService('mail', 'mail.modPHPMailer');
  180. $this->modx->mail->set(modMail::MAIL_BODY, $message);
  181. $this->modx->mail->set(modMail::MAIL_FROM, $this->modx->getOption('emailsender'));
  182. $this->modx->mail->set(modMail::MAIL_FROM_NAME, $this->modx->getOption('site_name'));
  183. $this->modx->mail->set(modMail::MAIL_SENDER, $this->modx->getOption('emailsender'));
  184. $this->modx->mail->set(modMail::MAIL_SUBJECT, $this->modx->getOption('emailsubject'));
  185. $this->modx->mail->address('to', $user->get('email'),$user->get('fullname'));
  186. $this->modx->mail->address('reply-to', $this->modx->getOption('emailsender'));
  187. $this->modx->mail->setHTML(true);
  188. if (!$this->modx->mail->send()) {
  189. /* if for some reason error in email, tell user */
  190. $err = $this->modx->lexicon('error_sending_email_to').$user->get('email');
  191. $this->modx->log(modX::LOG_LEVEL_ERROR,$err);
  192. $this->setPlaceholder('error_message',$err);
  193. } else {
  194. $this->setPlaceholder('error_message',$this->modx->lexicon('login_password_reset_act_sent'));
  195. }
  196. $this->modx->mail->reset();
  197. } else {
  198. $this->setPlaceholder('error_message',$this->modx->lexicon('login_user_err_nf_email'));
  199. }
  200. }
  201. /**
  202. * Return the pagetitle
  203. *
  204. * @return string
  205. */
  206. public function getPageTitle() {
  207. return $this->modx->lexicon('login');
  208. }
  209. /**
  210. * Return the location of the template file
  211. * @return string
  212. */
  213. public function getTemplateFile() {
  214. return 'security/login.tpl';
  215. }
  216. /**
  217. * Specify the language topics to load
  218. * @return array
  219. */
  220. public function getLanguageTopics() {
  221. return array('login');
  222. }
  223. }