FireBot

modx-confirmregister

Oct 4th, 2011
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.82 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /**
  5.  * Register
  6.  *
  7.  * Copyright 2010 by Shaun McCormick <[email protected]>
  8.  *
  9.  * Register is free software; you can redistribute it and/or modify it under the
  10.  * terms of the GNU General Public License as published by the Free Software
  11.  * Foundation; either version 2 of the License, or (at your option) any later
  12.  * version.
  13.  *
  14.  * Register is distributed in the hope that it will be useful, but WITHOUT ANY
  15.  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  16.  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License along with
  19.  * Register; if not, write to the Free Software Foundation, Inc., 59 Temple
  20.  * Place, Suite 330, Boston, MA 02111-1307 USA
  21.  *
  22.  * @package login
  23.  */
  24. /**
  25.  * MODx Confirm Register Activation Snippet. Snippet to place on an activation
  26.  * page that the user using the Register snippet would be sent to via the
  27.  * activation email.
  28.  *
  29.  * @package login
  30.  */
  31. $model_path = $modx->getOption('core_path').'components/login/model/login/';
  32. $Login = $modx->getService('login','Login',$model_path,$scriptProperties);
  33. $modx->lexicon->load('login:register');
  34.  
  35. /* get default properties */
  36. $errorPage = $modx->getOption('errorPage',$scriptProperties,false);
  37.  
  38. /* get user from query params */
  39. if (empty($_REQUEST['lp']) || empty($_REQUEST['lu'])) {
  40.     if (!empty($errorPage)) {
  41.         $url = $modx->makeUrl($errorPage,'','','full');
  42.         $modx->sendRedirect($url);
  43.     } else {
  44.         $modx->sendErrorPage();
  45.     }
  46. }
  47. $username = base64_decode(urldecode(rawurldecode($_REQUEST['lu'])));
  48. $password = base64_decode(urldecode(rawurldecode($_REQUEST['lp'])));
  49.  
  50. /* validate we have correct user */
  51. $user = $modx->getObject('modUser',array('username' => $username));
  52. if ($user == null || $user->get('active')) {
  53.     if (!empty($errorPage)) {
  54.         $url = $modx->makeUrl($errorPage,'','','full');
  55.         $modx->sendRedirect($url);
  56.     } else {
  57.         $modx->sendErrorPage();
  58.     }
  59. }
  60.  
  61. /* validate password to prevent middleman attacks */
  62. $modx->getService('registry', 'registry.modRegistry');
  63. $modx->registry->addRegister('login','registry.modFileRegister');
  64. $modx->registry->login->connect();
  65. $modx->registry->login->subscribe('/useractivation/'.$user->get('username'));
  66. $msgs = $modx->registry->login->read();
  67. if (empty($msgs)) $modx->sendErrorPage();
  68. $found = false;
  69. foreach ($msgs as $msg) {
  70.     if ($msg == $password) $found = true;
  71. }
  72. if (!$found) {
  73.     if (!empty($errorPage)) {
  74.         $url = $modx->makeUrl($errorPage,'','','full');
  75.         $modx->sendRedirect($url);
  76.     } else {
  77.         $modx->sendErrorPage();
  78.     }
  79. }
  80.  
  81. /* invoke OnBeforeUserActivateEvent, if result returns anything, do not proceed */
  82. $result = $modx->invokeEvent('OnBeforeUserActivate',array(
  83.     'user' => &$user,
  84. ));
  85. $preventActivation = $Login->getEventResult($result);
  86. if (!empty($preventActivation)) {
  87.     $modx->log(modX::LOG_LEVEL_ERROR,'[Register] OnBeforeUserActivate event prevented activation for "'.$user->get('username').'" by returning false: '.$preventActivation);
  88.     if (!empty($errorPage)) {
  89.         $url = $modx->makeUrl($errorPage,'','','full');
  90.         $modx->sendRedirect($url);
  91.     } else {
  92.         $modx->sendErrorPage();
  93.     }
  94. }
  95.  
  96. /* activate user */
  97. $user->set('active',1);
  98. $user->set('cachepwd','');
  99. if (!$user->save()) {
  100.     $modx->log(modX::LOG_LEVEL_ERROR,'[Register] Could not save activated user: '.$user->get('username'));
  101.     return '';
  102. }
  103.  
  104. /* invoke OnUserActivate event */
  105. $modx->invokeEvent('OnUserActivate',array(
  106.     'user' => &$user,
  107. ));
  108.  
  109. /* authenticate user to contexts */
  110. if ($modx->getOption('authenticate',$scriptProperties,true)) {
  111.     $modx->user =& $user;
  112.     $modx->getUser();
  113.     $contexts = !empty($scriptProperties['authenticateContexts']) ? $scriptProperties['authenticateContexts'] : $modx->context->get('key');
  114.     $contexts = explode(',',$contexts);
  115.     foreach ($contexts as $ctx) {
  116.         $modx->user->addSessionContext($ctx);
  117.     }
  118. }
  119.  
  120. $profile = $user->getOne('Profile');
  121. $extended = $profile->get('extended');
  122.  
  123. $extended['joined_date'] = time();
  124. $profile->set('extended', $extended);
  125. $profile->save();
  126.  
  127. /* if wanting to redirect after confirmed registration (for shopping carts)
  128.  * Also allow &redirectBack parameter sent in confirmation email to redirect
  129.  * to a form requiring registration
  130.  */
  131. $redirectBack = $modx->getOption('redirectBack',$_REQUEST,$modx->getOption('redirectBack',$scriptProperties,''));
  132. $redirectBackParams = $redirectBackParams = $modx->getOption('redirectBackParams',$_REQUEST,$modx->getOption('redirectBackParams',$scriptProperties,''));
  133. if (!empty($redirectBackParams)) {
  134.     $redirectBackParams = $Login->decodeParams($redirectBackParams);
  135. }
  136.  
  137. $redirectTo = !empty($scriptProperties['redirectTo']) ? $scriptProperties['redirectTo'] : $redirectBack;
  138. if (!empty($redirectTo)) {
  139.     /* allow custom redirection params */
  140.     $redirectParams = !empty($scriptProperties['redirectParams']) ? $scriptProperties['redirectParams'] : $redirectBackParams;
  141.     if (!empty($redirectParams) && !is_array($redirectParams)) $redirectParams = $modx->fromJSON($redirectParams);
  142.     if (empty($redirectParams) || !is_array($redirectParams)) $redirectParams = array();
  143.  
  144.     /* handle persist params from Register snippet */
  145.     $persistParams = $_GET;
  146.     unset($persistParams['lp'],$persistParams['lu']);
  147.     $persistParams['username'] = $user->get('username');
  148.     $persistParams['userid'] = $user->get('id');    
  149.     $redirectParams = array_merge($redirectParams,$persistParams);
  150.     unset($redirectParams[$modx->getOption('request_param_alias',null,'q')],$redirectParams['redirectBack']);
  151.  
  152.     /* redirect user */
  153.     $url = $modx->makeUrl($redirectTo,'',$redirectParams);
  154.     $modx->sendRedirect($url);
  155. }
  156.  
  157. return '';
Advertisement
Add Comment
Please, Sign In to add comment