ConfirmRegister.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Login
  4. *
  5. * Copyright 2010 by Shaun McCormick <shaun+login@modx.com>
  6. *
  7. * Login is free software; you can redistribute it and/or modify it under the
  8. * terms of the GNU General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * Login is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * Login; if not, write to the Free Software Foundation, Inc., 59 Temple
  18. * Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * @package login
  21. */
  22. /**
  23. * Confirms a User's Registration after activation
  24. *
  25. * @package login
  26. * @subpackage controllers
  27. */
  28. class LoginConfirmRegisterController extends LoginController {
  29. /** @var string $username */
  30. public $username;
  31. /** @var string $password */
  32. public $password;
  33. /** @var modUser $user */
  34. public $user;
  35. public function initialize() {
  36. $this->setDefaultProperties(array(
  37. 'authenticate' => true,
  38. 'authenticateContexts' => $this->modx->context->get('key'),
  39. 'errorPage' => false,
  40. 'redirectTo' => false,
  41. 'redirectParams' => '',
  42. 'redirectBack' => false,
  43. 'redirectBackParams' => '',
  44. 'redirectUnsetDefaultParams' => false,
  45. ));
  46. }
  47. public function process() {
  48. $this->verifyManifest();
  49. $this->getUser();
  50. $this->validatePassword();
  51. $this->onBeforeUserActivate();
  52. /* activate user */
  53. $this->user->set('active',1);
  54. $this->user->_fields['cachepwd'] = '';
  55. $this->user->setDirty('cachepwd');
  56. if (!$this->user->save()) {
  57. $this->modx->log(modX::LOG_LEVEL_ERROR,'[Register] Could not save activated user: '.$this->user->get('username'));
  58. return '';
  59. }
  60. /* invoke OnUserActivate event */
  61. $this->modx->invokeEvent('OnUserActivate',array(
  62. 'user' => &$this->user,
  63. ));
  64. $this->addSessionContexts();
  65. $this->redirectBack();
  66. return '';
  67. }
  68. /**
  69. * Verify that the username/password hashes were correctly sent (base64 encoded in URL) to prevent middle-man attacks.
  70. *
  71. * @access public
  72. * @return boolean
  73. */
  74. public function verifyManifest() {
  75. $verified = false;
  76. if (empty($_REQUEST['lp']) || empty($_REQUEST['lu'])) {
  77. $this->redirectAfterFailure();
  78. } else {
  79. // get username and password from query params
  80. $this->username = $this->login->base64url_decode($_REQUEST['lu']);
  81. $this->password = $this->login->base64url_decode($_REQUEST['lp']);
  82. $verified = true;
  83. }
  84. return $verified;
  85. }
  86. /**
  87. * Validate we have correct user
  88. * @return modUser
  89. */
  90. public function getUser() {
  91. $this->user = $this->modx->getObject('modUser',array('username' => $this->username));
  92. if ($this->user == null) {
  93. $this->redirectAfterFailure();
  94. } elseif ($this->user->get('active')) {
  95. $activePage = $this->getProperty('activePage', false, 'isset');
  96. $this->redirectAfterFailure($activePage);
  97. }
  98. return $this->user;
  99. }
  100. /**
  101. * Handle the redirection after a failed verification
  102. * @param mixed $id Resource ID to redirect to
  103. */
  104. public function redirectAfterFailure($id = null) {
  105. $errorPage = (is_null($id)) ? $this->getProperty('errorPage', false, 'isset') : $id;
  106. if (!empty($errorPage)) {
  107. $url = $this->modx->makeUrl($errorPage,'','','full');
  108. $this->modx->sendRedirect($url);
  109. } else {
  110. $this->modx->sendErrorPage();
  111. }
  112. }
  113. /**
  114. * Validate password to prevent middleman attacks
  115. * @return boolean
  116. */
  117. public function validatePassword() {
  118. $this->modx->getService('registry', 'registry.modRegistry');
  119. $this->modx->registry->addRegister('login','registry.modFileRegister');
  120. $this->modx->registry->login->connect();
  121. $this->modx->registry->login->subscribe('/useractivation/'.$this->user->get('username'));
  122. $msgs = $this->modx->registry->login->read();
  123. if (empty($msgs)) $this->modx->sendErrorPage();
  124. $found = false;
  125. foreach ($msgs as $msg) {
  126. if ($msg == $this->password) {
  127. $found = true;
  128. }
  129. }
  130. if (!$found) {
  131. $this->redirectAfterFailure();
  132. }
  133. return $found;
  134. }
  135. /**
  136. * Invoke OnBeforeUserActivateEvent, if result returns anything, do not proceed
  137. * @return boolean
  138. */
  139. public function onBeforeUserActivate() {
  140. $success = true;
  141. $result = $this->modx->invokeEvent('OnBeforeUserActivate',array(
  142. 'user' => &$this->user,
  143. ));
  144. $preventActivation = $this->login->getEventResult($result);
  145. if (!empty($preventActivation)) {
  146. $success = false;
  147. $this->modx->log(modX::LOG_LEVEL_ERROR,'[Register] OnBeforeUserActivate event prevented activation for "'.$this->user->get('username').'" by returning false: '.$preventActivation);
  148. $this->redirectAfterFailure();
  149. }
  150. return $success;
  151. }
  152. /**
  153. * Login the user to the specified contexts
  154. * @return void
  155. */
  156. public function addSessionContexts() {
  157. if ($this->getProperty('authenticate',true,'isset')) {
  158. $this->modx->user =& $this->user;
  159. $this->modx->getUser();
  160. $contexts = $this->getProperty('authenticateContexts',$this->modx->context->get('key'));
  161. $contexts = explode(',',$contexts);
  162. foreach ($contexts as $ctx) {
  163. $this->modx->user->addSessionContext($ctx);
  164. }
  165. }
  166. }
  167. /**
  168. * If wanting to redirect after confirmed registration (for shopping carts)
  169. * Also allow &redirectBack parameter sent in confirmation email to redirect
  170. * to a form requiring registration
  171. */
  172. public function redirectBack() {
  173. $redirectBack = $this->modx->getOption('redirectBack',$_REQUEST,$this->getProperty('redirectBack',false,'isset'));
  174. $redirectBackParams = $this->modx->getOption('redirectBackParams',$_REQUEST,$this->getProperty('redirectBackParams',''));
  175. if (!empty($redirectBackParams)) {
  176. $redirectBackParams = $this->login->decodeParams($redirectBackParams);
  177. }
  178. $redirectTo = $this->getProperty('redirectTo',$redirectBack);
  179. if (!empty($redirectTo)) {
  180. /* allow custom redirection params */
  181. $redirectParams = $this->getProperty('redirectParams',$redirectBackParams);
  182. if (!empty($redirectParams) && !is_array($redirectParams)) $redirectParams = $this->modx->fromJSON($redirectParams);
  183. if (empty($redirectParams) || !is_array($redirectParams)) $redirectParams = array();
  184. /* handle persist params from Register snippet */
  185. $redirectUnsetDefaultParams = (boolean) $this->getProperty('redirectUnsetDefaultParams', 0, 'isset');
  186. if(!$redirectUnsetDefaultParams) {
  187. $persistParams = $_GET;
  188. unset($persistParams['lp'],$persistParams['lu'],$persistParams['id']);
  189. $persistParams['username'] = $this->user->get('username');
  190. $persistParams['userid'] = $this->user->get('id');
  191. $redirectParams = array_merge($redirectParams,$persistParams);
  192. unset($redirectParams[$this->modx->getOption('request_param_alias',null,'q')],$redirectParams['redirectBack']);
  193. }
  194. /* redirect user */
  195. $url = $this->modx->makeUrl($redirectTo,'',$redirectParams,'full');
  196. $this->modx->sendRedirect($url);
  197. }
  198. }
  199. }
  200. return 'LoginConfirmRegisterController';