-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathregister.php
More file actions
416 lines (376 loc) · 17.2 KB
/
register.php
File metadata and controls
416 lines (376 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
/**
* Login
*
* Copyright 2010 by Jason Coward <jason@modxcms.com> and Shaun McCormick
* <shaun@modxcms.com>
*
* Login is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option) any
* later version.
*
* Login is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Login; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*
* @package login
*/
/**
* Handle register form
*
* @package login
* @subpackage processors
*/
class LoginRegisterProcessor extends LoginProcessor {
/** @var modUser $user */
public $user;
/** @var modUserProfile $profile */
public $profile;
/** @var array $userGroups */
public $userGroups = array();
public $persistParams = array();
public $live = true;
/**
* @return mixed
*/
public function process() {
$this->cleanseFields();
/* create user and profile */
$this->user = $this->modx->newObject('modUser');
$this->profile = $this->modx->newObject('modUserProfile');
if ($this->controller->getProperty('useExtended',true,'isset')) {
$this->setExtended();
}
$this->setUserFields();
/* save user */
if ($this->live) {
if (!$this->user->save()) {
$this->modx->log(modX::LOG_LEVEL_ERROR,'[Login] Could not save newly registered user: '.$this->user->get('id').' with username: '.$this->user->get('username'));
return $this->modx->lexicon('register.user_err_save');
}
}
$this->preparePersistentParameters();
/* send activation email (if chosen) */
$email = $this->profile->get('email');
$activation = $this->controller->getProperty('activation',true,'isset');
$activationResourceId = $this->controller->getProperty('activationResourceId','','isset');
$moderated = $this->checkForModeration();
if ($activation && !empty($email) && !empty($activationResourceId) && !$moderated) {
$this->sendActivationEmail();
} else if (!$moderated) {
$this->user->set('active',true);
if ($this->live) {
$this->user->save();
}
}
$this->runPostHooks();
$this->checkForModerationRedirect();
$this->checkForRegisteredRedirect();
$successMsg = $this->controller->getProperty('successMsg','');
$this->modx->toPlaceholder('error.message',$successMsg);
return true;
}
/**
* Remove any fields used for anti-spam, submission or moderation from the submission returns
* @return void
*/
public function cleanseFields() {
$this->dictionary->remove('nospam');
$this->dictionary->remove('blank');
$submitVar = $this->controller->getProperty('submitVar');
if (!empty($submitVar)) {
$this->dictionary->remove($submitVar);
}
}
/**
* If wanted, set extra values in the form to profile extended field
* @return void
*/
public function setExtended() {
/* first cut out regular and unwanted fields */
$excludeExtended = $this->controller->getProperty('excludeExtended','');
$excludeExtended = explode(',',$excludeExtended);
$profileFields = $this->profile->toArray();
$userFields = $this->user->toArray();
$extended = array();
$fields = $this->dictionary->toArray();
$fields = $this->filterAllowedFields($fields);
$userGroupField = $this->controller->getProperty('usergroupsField','');
foreach ($fields as $field => $value) {
if (!isset($profileFields[$field])
&& !isset($userFields[$field])
&& $field != 'password_confirm'
&& $field != 'passwordconfirm'
&& $field != $userGroupField
&& !in_array($field,$excludeExtended)
) {
$extended[$field] = $value;
}
}
/* now set extended data */
$this->profile->set('extended',$extended);
}
/**
* Setup the user data and create the user/profile objects
*
* @return void
*/
public function setUserFields() {
$fields = $this->dictionary->toArray();
/* allow overriding of class key */
if (empty($fields['class_key'])) $fields['class_key'] = 'modUser';
$fields = $this->filterAllowedFields($fields);
/* set user and profile */
$this->user->fromArray($fields);
$this->user->set('username',$fields[$this->controller->getProperty('usernameField','username')]);
$this->user->set('active',0);
$version = $this->modx->getVersionData();
/* 2.1.x+ */
if (version_compare($version['full_version'],'2.1.0-rc1') >= 0) {
$this->user->set('password',$fields[$this->controller->getProperty('passwordField','password')]);
} else { /* 2.0.x */
$this->user->set('password',md5($fields[$this->controller->getProperty('passwordField','password')]));
}
$this->profile->fromArray($fields);
$this->profile->set('email',$this->dictionary->get($this->controller->getProperty('emailField','email')));
$this->user->addOne($this->profile,'Profile');
/* add user groups, if set */
$userGroupsField = $this->controller->getProperty('usergroupsField','');
$userGroups = !empty($userGroupsField) && array_key_exists($userGroupsField,$fields) ? $fields[$userGroupsField] : array();
$this->setUserGroups($userGroups);
}
/**
* Return an array of filtered fields if the allowedFields property is set and prevents setting of certain fields
* @param array $fields
* @return array
*/
public function filterAllowedFields(array $fields = array()) {
$allowedFields = $this->controller->getProperty('allowedFields','');
if (!empty($allowedFields)) {
$allowedFields = is_array($allowedFields) ? $allowedFields : explode(',',$allowedFields);
$userGroupField = $this->controller->getProperty('usergroupsField','');
$usernameField = $this->controller->getProperty('usernameField','username');
$passwordField = $this->controller->getProperty('passwordField','password');
$emailField = $this->controller->getProperty('emailField','email');
array_push($allowedFields,$usernameField,$passwordField,'password_confirm',$emailField,'class_key');
if (!empty($userGroupField)) array_push($allowedFields,$userGroupField);
$allowedFields = array_unique($allowedFields);
foreach ($fields as $k => $v) {
if (!in_array($k,$allowedFields)) unset($fields[$k]);
}
}
return $fields;
}
/**
* If user groups were passed, set them here
* @param string $userGroups
* @return array
*/
public function setUserGroups($userGroups) {
$added = array();
/* if $userGroups set in form, override here; otherwise use snippet property */
$this->userGroups = !empty($userGroups) ? $userGroups : $this->controller->getProperty('usergroups', '');
if (!empty($this->userGroups)) {
$this->userGroups = is_array($this->userGroups) ? $this->userGroups : explode(',',$this->userGroups);
foreach ($this->userGroups as $userGroupMeta) {
$userGroupMeta = explode(':',$userGroupMeta);
if (empty($userGroupMeta[0])) continue;
/* get usergroup */
$pk = array();
$pk[intval($userGroupMeta[0]) > 0 ? 'id' : 'name'] = trim($userGroupMeta[0]);
/** @var modUserGroup $userGroup */
$userGroup = $this->modx->getObject('modUserGroup',$pk);
if (!$userGroup) continue;
/* get role */
$rolePk = !empty($userGroupMeta[1]) ? $userGroupMeta[1] : 'Member';
/** @var modUserGroupRole $role */
$role = $this->modx->getObject('modUserGroupRole',array('name' => $rolePk));
/* create membership */
/** @var modUserGroupMember $member */
$member = $this->modx->newObject('modUserGroupMember');
$member->set('member',0);
$member->set('user_group',$userGroup->get('id'));
if (!empty($role)) {
$member->set('role',$role->get('id'));
} else {
$member->set('role',1);
}
$this->user->addMany($member,'UserGroupMembers');
$added[] = $userGroup->get('name');
}
}
return $added;
}
/**
* Setup persistent parameters to go through the request cycle
* @return array
*/
public function preparePersistentParameters() {
$this->persistParams = $this->controller->getProperty('persistParams','');
if (!empty($this->persistParams)) $this->persistParams = $this->modx->fromJSON($this->persistParams);
if (empty($this->persistParams) || !is_array($this->persistParams)) $this->persistParams = array();
return $this->persistParams;
}
/**
* Send an activation email to the user with an encrypted username and password hash, to allow for secure
* activation processes that are not vulnerable to middle-man attacks.
*
* @return boolean
*/
public function sendActivationEmail() {
$emailProperties = $this->gatherActivationEmailProperties();
/* send either to user's email or a specified activation email */
$activationEmail = $this->controller->getProperty('activationEmail',$this->user->get('email'));
$subject = $this->controller->getProperty('activationEmailSubject',$this->modx->lexicon('register.activation_email_subject'));
return $this->login->sendEmail($activationEmail,$this->user->get('username'),$subject,$emailProperties);
}
/**
* Get all the properties for the activation email
* @return array
*/
public function gatherActivationEmailProperties() {
/* generate a password and encode it and the username into the url */
$pword = $this->login->generatePassword();
$confirmParams['lp'] = urlencode(base64_encode($pword));
$confirmParams['lu'] = urlencode(base64_encode($this->user->get('username')));
$confirmParams = array_merge($this->persistParams,$confirmParams);
/* if using redirectBack param, set here to allow dynamic redirection
* handling from other forms.
*/
$redirectBack = $this->modx->getOption('redirectBack',$_REQUEST,$this->controller->getProperty('redirectBack',''));
if (!empty($redirectBack)) {
$confirmParams['redirectBack'] = $redirectBack;
}
$redirectBackParams = $this->modx->getOption('redirectBackParams',$_REQUEST,$this->controller->getProperty('redirectBackParams',''));
if (!empty($redirectBackParams)) {
$confirmParams['redirectBackParams'] = $redirectBackParams;
}
/* generate confirmation url */
if ($this->login->inTestMode) {
$confirmUrl = $this->modx->makeUrl(1,'',$confirmParams,'full');
} else {
$confirmUrl = $this->modx->makeUrl($this->controller->getProperty('activationResourceId',1),'',$confirmParams,'full');
}
/* set confirmation email properties */
$emailTpl = $this->controller->getProperty('activationEmailTpl','lgnActivateEmailTpl');
$emailTplAlt = $this->controller->getProperty('activationEmailTplAlt','');
$emailTplType = $this->controller->getProperty('activationEmailTplType','modChunk');
$emailProperties = $this->user->toArray();
$emailProperties['confirmUrl'] = $confirmUrl;
$emailProperties['tpl'] = $emailTpl;
$emailProperties['tplAlt'] = $emailTplAlt;
$emailProperties['tplType'] = $emailTplType;
$emailProperties['password'] = $this->dictionary->get($this->controller->getProperty('passwordField','password'));
$emailProperties['emailFromName'] = $this->controller->getProperty('activationEmailFromName');
$emailProperties['emailFrom'] = $this->controller->getProperty('activationEmailFrom');
$emailProperties['emailReplyToName'] = $this->controller->getProperty('activationEmailReplyToName');
$emailProperties['emailReplyTo'] = $this->controller->getProperty('activationEmailReplyTo');
$this->setCachePassword($pword);
return $emailProperties;
}
public function setCachePassword($password) {
/* now set new password to registry to prevent middleman attacks.
* Will read from the registry on the confirmation page. */
$this->modx->getService('registry', 'registry.modRegistry');
$this->modx->registry->addRegister('login','registry.modFileRegister');
$this->modx->registry->login->connect();
$this->modx->registry->login->subscribe('/useractivation/');
$this->modx->registry->login->send('/useractivation/',array($this->user->get('username') => $password),array(
'ttl' => ($this->controller->getProperty('activationttl',180)*60),
));
/* set cachepwd here to prevent re-registration of inactive users */
$this->user->set('cachepwd',md5($password));
if ($this->live) {
$success = $this->user->save();
} else {
$success = true;
}
if (!$success) {
$this->modx->log(modX::LOG_LEVEL_ERROR,'[Login] Could not update cachepwd for activation for User: '.$this->user->get('username'));
}
return $success;
}
/**
* Check to see if a pre/post hook told Login to set the user to moderated (inactive) status
* @return boolean
*/
public function checkForModeration() {
$moderate = $this->dictionary->get('register.moderate');
return !empty($moderate);
}
/**
* Run any post-registration hooks
*
* @return void
*/
public function runPostHooks() {
$postHooks = $this->controller->getProperty('postHooks','');
$this->controller->loadHooks('postHooks');
$fields = $this->dictionary->toArray();
$fields['register.user'] =& $this->user;
$fields['register.profile'] =& $this->profile;
$fields['register.usergroups'] = $this->userGroups;
$this->controller->postHooks->loadMultiple($postHooks,$fields);
/* process hooks */
if ($this->controller->postHooks->hasErrors()) {
$errors = array();
$hookErrors = $this->controller->postHooks->getErrors();
foreach ($hookErrors as $key => $error) {
$errors[$key] = str_replace('[[+error]]',$error,$this->controller->getProperty('errTpl'));
}
$this->modx->toPlaceholders($errors,'error');
$errorMsg = $this->controller->postHooks->getErrorMessage();
$this->modx->toPlaceholder('message',$errorMsg,'error');
}
}
/**
* if a hook set the user as moderated, if set, send to an optional other moderation resource id
* @return boolean
*/
public function checkForModerationRedirect() {
$moderated = $this->checkForModeration();
if (!empty($moderated)) {
$moderatedResourceId = $this->controller->getProperty('moderatedResourceId','');
if (!empty($moderatedResourceId)) {
$persistParams = array_merge($this->persistParams,array(
'username' => $this->user->get('username'),
'email' => $this->profile->get('email'),
));
$url = $this->modx->makeUrl($moderatedResourceId,'',$persistParams,'full');
if (!$this->login->inTestMode) {
$this->modx->sendRedirect($url);
}
return true;
}
}
return false;
}
/**
* Check for a redirect if the user was successfully registered. If one found, redirect.
*
* @return boolean
*/
public function checkForRegisteredRedirect() {
/* if provided a redirect id, will redirect to that resource, with the
* GET params `username` and `email` for you to use */
$submittedResourceId = $this->controller->getProperty('submittedResourceId','');
if (!empty($submittedResourceId)) {
$persistParams = array_merge($this->persistParams,array(
'username' => $this->user->get('username'),
'email' => $this->profile->get('email'),
));
$url = $this->modx->makeUrl($submittedResourceId,'',$persistParams,'full');
if (!$this->login->inTestMode) {
$this->modx->sendRedirect($url);
}
return true;
}
return false;
}
}
return 'LoginRegisterProcessor';