-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathManageRegistration.php
560 lines (484 loc) · 18.3 KB
/
ManageRegistration.php
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
/**
* This file allows the administrator to set registration and policy settings
* as well as allow them to register new members themselves.
*
* @package ElkArte Forum
* @copyright ElkArte Forum contributors
* @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
*
* This file contains code covered by:
* copyright: 2011 Simple Machines (http://www.simplemachines.org)
*
* @version 2.0 dev
*
*/
namespace ElkArte\AdminController;
use ElkArte\AbstractController;
use ElkArte\Action;
use ElkArte\Agreement;
use ElkArte\Errors\ErrorContext;
use ElkArte\Exceptions\Exception;
use ElkArte\Helper\FileFunctions;
use ElkArte\Helper\TokenHash;
use ElkArte\Helper\Util;
use ElkArte\Languages\Txt;
use ElkArte\PrivacyPolicy;
use ElkArte\SettingsForm\SettingsForm;
/**
* ManageRegistration admin controller: handles the registration pages
*
* - allow admins (or moderators with moderate_forum permission)
* to register a new member,
* - to see and edit the registration agreement,
* - to set up reserved words for forum names.
*
* @package Registration
*/
class ManageRegistration extends AbstractController
{
/**
* Entrance point for the registration center, it checks permissions and forwards
* to the right method based on the sub-action.
*
* - Accessed by ?action=admin;area=regcenter.
* - Requires either the moderate_forum or the admin_forum permission.
*
* @event integrate_sa_manage_registrations add new registration sub actions
* @uses Login language file
* @uses Register template.
* @see AbstractController::action_index()
*/
public function action_index()
{
global $context, $txt;
// Loading, always loading.
Txt::load('Login');
theme()->getTemplates()->load('Register');
loadJavascriptFile('register.js');
$subActions = array(
'register' => array(
'controller' => $this,
'function' => 'action_register',
'permission' => 'moderate_forum',
),
'agreement' => array(
'controller' => $this,
'function' => 'action_agreement',
'permission' => 'admin_forum',
),
'privacypol' => array(
'controller' => $this,
'function' => 'action_privacypol',
'permission' => 'admin_forum',
),
'reservednames' => array(
'controller' => $this,
'function' => 'action_reservednames',
'permission' => 'admin_forum',
),
'settings' => array(
'controller' => $this,
'function' => 'action_registerSettings_display',
'permission' => 'admin_forum',
),
);
// Action controller
$action = new Action('manage_registrations');
// Work out which to call... call integrate_sa_manage_registrations
$subAction = $action->initialize($subActions, 'register');
// Final bits
$context['page_title'] = $txt['maintain_title'];
$context['sub_action'] = $subAction;
// Next create the tabs for the template.
$context[$context['admin_menu_name']]['object']->prepareTabData([
'title' => 'registration_center',
'help' => 'registrations',
'description' => 'admin_settings_desc',
'tabs' => [
'register' => [
'description' => $txt['admin_register_desc'],
],
'agreement' => [
'description' => $txt['registration_agreement_desc'],
],
'privacypol' => [
'description' => $txt['privacy_policy_desc'],
],
'reservednames' => [
'description' => $txt['admin_reserved_desc'],
],
'settings' => [
'description' => $txt['admin_settings_desc'],
]
]
]);
// Call the right function for this sub-action.
$action->dispatch($subAction);
}
/**
* This function allows the admin to register a new member by hand.
*
* - It also allows assigning a primary group to the member being registered.
* - Accessed by ?action=admin;area=regcenter;sa=register
* - Requires the moderate_forum permission.
*
* @uses Register template, admin_register sub-template.
*/
public function action_register()
{
global $txt, $context;
if (!empty($this->_req->post->regSubmit))
{
checkSession();
validateToken('admin-regc');
// @todo move this to a filter/sanitation class
foreach ($this->_req->post as $key => $value)
{
if (!is_array($value))
{
$this->_req->post[$key] = Util::htmltrim__recursive(str_replace(array("\n", "\r"), '', $value));
}
}
// Generate a password
if (empty($this->_req->post->password) || !is_string($this->_req->post->password) || trim($this->_req->post->password) === '')
{
$tokenizer = new TokenHash();
$password = $tokenizer->generate_hash(14);
}
else
{
$password = $this->_req->post->password;
}
$regOptions = array(
'interface' => 'admin',
'username' => $this->_req->post->user,
'email' => $this->_req->post->email,
'password' => $password,
'password_check' => $password,
'check_reserved_name' => true,
'check_password_strength' => true,
'check_email_ban' => false,
'send_welcome_email' => isset($this->_req->post->emailPassword),
'require' => isset($this->_req->post->emailActivate) ? 'activation' : 'nothing',
'memberGroup' => empty($this->_req->post->group) || !allowedTo('manage_membergroups') ? 0 : (int) $this->_req->post->group,
'ip' => '127.0.0.1',
'ip2' => '127.0.0.1',
'auth_method' => 'password',
);
require_once(SUBSDIR . '/Members.subs.php');
$reg_errors = ErrorContext::context('register', 0);
$memberID = registerMember($regOptions, 'register');
// If there are "important" errors, and you are not an admin: log the first error
// Otherwise grab all of them and don't log anything
$error_severity = $reg_errors->hasErrors(1) && !$this->user->is_admin ? 1 : null;
foreach ($reg_errors->prepareErrors($error_severity) as $error)
{
throw new Exception($error, $error_severity === null ? false : 'general');
}
if (!empty($memberID))
{
$context['new_member'] = array(
'id' => $memberID,
'name' => $this->_req->post->user,
'href' => getUrl('profile', ['action' => 'profile', 'u' => $memberID, 'name' => $this->_req->post->user]),
'link' => '<a href="' . getUrl('profile', ['action' => 'profile', 'u' => $memberID, 'name' => $this->_req->post->user]) . '">' . $this->_req->post->user . '</a>',
);
$context['registration_done'] = sprintf($txt['admin_register_done'], $context['new_member']['link']);
}
}
// Load the assignable member groups.
if (allowedTo('manage_membergroups'))
{
require_once(SUBSDIR . '/Membergroups.subs.php');
$includes = allowedTo('admin_forum') ? array('admin', 'globalmod', 'member') : array('globalmod', 'member', 'custom');
$groups = array();
$membergroups = getBasicMembergroupData($includes, array('hidden', 'protected'));
foreach ($membergroups as $membergroup)
{
$groups[$membergroup['id']] = $membergroup['name'];
}
$context['member_groups'] = $groups;
}
else
{
$context['member_groups'] = array();
}
// Basic stuff.
loadJavascriptFile('ext/mailcheck.min.js');
theme()->addInlineJavascript('disableAutoComplete();
$("input[type=email]").on("blur", function(event) {
$(this).mailcheck({
suggested: function(element, suggestion) {
$("#suggestion").html("' . $txt['register_did_you'] . ' <b><i>" + suggestion.full + "</b></i>");
element.addClass("check_input");
},
empty: function(element) {
$("#suggestion").html("");
element.removeClass("check_input");
}
});
});', true);
$context['sub_template'] = 'admin_register';
$context['page_title'] = $txt['registration_center'];
createToken('admin-regc');
}
/**
* Allows the administrator to edit the registration agreement, and choose whether
* it should be shown or not.
*
* - It writes and saves the agreement to the appropriate file.
* - Accessed by ?action=admin;area=regcenter;sa=agreement.
* - Requires the admin_forum permission.
*
* @uses Admin template and the edit_agreement sub template.
*/
public function action_agreement()
{
// I hereby agree not to be a lazy bum.
global $txt, $context, $modSettings;
// By default, we look at Languages/Agreement/English.txt.
$context['current_agreement'] = 'English';
// Is there more than one to edit?
$context['editable_agreements'] = array('' => $txt['admin_agreement_default']);
// Get our languages.
$languages = getLanguages();
// Try to figure out if we have more agreements.
$fileFunc = FileFunctions::instance();
foreach ($languages as $lang)
{
if ($fileFunc->fileExists(SOURCEDIR . '/ElkArte/Languages/Agreement/' . $lang['name'] . '.txt'))
{
$context['editable_agreements'][$lang['filename']] = $lang['name'];
// Are we editing this?
if ($this->_req->getPost('agree_lang') === $lang['filename'])
{
$context['current_agreement'] = $lang['name'];
break;
}
}
}
$context['warning'] = '';
$agreement = new Agreement($context['current_agreement']);
// Saving the edit
if (isset($this->_req->post->save, $this->_req->post->agreement))
{
checkSession();
validateToken('admin-rega');
// Off it goes to the agreement file.
$success = $agreement->save($this->_req->post->agreement, !empty($this->_req->post->checkboxAcceptAgreement));
if (!empty($this->_req->post->checkboxAcceptAgreement))
{
if ($success === false)
{
$context['warning'] .= $txt['agreement_backup_not_writable'] . '<br />';
}
else
{
updateSettings(array('agreementRevision' => $success));
}
}
updateSettings(array('requireAgreement' => !empty($this->_req->post->requireAgreement), 'checkboxAgreement' => !empty($this->_req->post->checkboxAgreement)));
}
$context['agreement'] = Util::htmlspecialchars($agreement->getPlainText(false));
$context['warning'] .= $agreement->isWritable() ? '' : $txt['agreement_not_writable'];
$context['require_agreement'] = !empty($modSettings['requireAgreement']);
$context['checkbox_agreement'] = !empty($modSettings['checkboxAgreement']);
$context['sub_template'] = 'edit_agreement';
$context['subaction'] = 'agreement';
$context['agreement_show_options'] = true;
$context['page_title'] = $txt['registration_agreement'];
createToken('admin-rega');
}
/**
* Allows the administrator to edit the privacy policy, and choose whether
* it should be shown or not.
*
* - It writes and saves the privacy policy to the appropriate privacy policy file.
* - Accessed by ?action=admin;area=regcenter;sa=privacypol
* - Requires the admin_forum permission.
*
* @uses Admin template and the edit_agreement sub template.
*/
public function action_privacypol()
{
// I hereby agree not to be a lazy bum.
global $txt, $context, $modSettings;
// By default we look at Languages/PrivacyPolicy/English.txt.
$context['current_agreement'] = 'English';
// Is there more than one to edit?
$context['editable_agreements'] = array('' => $txt['admin_agreement_default']);
// Get our installed languages.
$languages = getLanguages();
// Try to figure out if we have more agreements.
$fileFunc = FileFunctions::instance();
foreach ($languages as $lang)
{
if ($fileFunc->fileExists(SOURCEDIR . '/ElkArte/Languages/PrivacyPolicy/' . $lang['name'] . '.txt'))
{
$context['editable_agreements'][$lang['filename']] = $lang['name'];
// Are we editing this?
if ($this->_req->getPost('agree_lang') === $lang['filename'])
{
$context['current_agreement'] = $lang['filename'];
break;
}
}
}
$context['warning'] = '';
$privacypol = new PrivacyPolicy($context['current_agreement']);
if (isset($this->_req->post->save, $this->_req->post->agreement))
{
checkSession();
validateToken('admin-rega');
// Off it goes to the agreement file.
$success = $privacypol->save($this->_req->post->agreement, !empty($this->_req->post->checkboxAcceptAgreement));
if (!empty($this->_req->post->checkboxAcceptAgreement))
{
if ($success === false)
{
$context['warning'] .= $txt['privacypol_backup_not_writable'] . '<br />';
}
else
{
updateSettings(array('privacypolicyRevision' => $success));
}
}
updateSettings(array('requirePrivacypolicy' => !empty($this->_req->post->requireAgreement)));
}
$context['agreement'] = Util::htmlspecialchars($privacypol->getPlainText(false));
$context['warning'] .= $privacypol->isWritable() ? '' : $txt['privacypol_not_writable'];
$context['require_agreement'] = !empty($modSettings['requirePrivacypolicy']);
$context['sub_template'] = 'edit_agreement';
$context['subaction'] = 'privacypol';
$context['page_title'] = $txt['privacy_policy'];
// These overrides are here to be able to reuse the template in a simple way without having to change much.
$txt['admin_agreement'] = $txt['admin_privacypol'];
$txt['admin_checkbox_accept_agreement'] = $txt['admin_checkbox_accept_privacypol'];
$txt['confirm_request_accept_agreement'] = $txt['confirm_request_accept_privacy_policy'];
createToken('admin-rega');
}
/**
* Set the names under which users are not allowed to register.
*
* - Accessed by ?action=admin;area=regcenter;sa=reservednames.
* - Requires the admin_forum permission.
*
* @uses Register template, reserved_words sub-template.
*/
public function action_reservednames()
{
global $txt, $context, $modSettings;
// Submitting new reserved words.
if (!empty($this->_req->post->save_reserved_names))
{
checkSession();
validateToken('admin-regr');
// Set all the options....
updateSettings(array(
'reserveWord' => (isset($this->_req->post->matchword) ? '1' : '0'),
'reserveCase' => (isset($this->_req->post->matchcase) ? '1' : '0'),
'reserveUser' => (isset($this->_req->post->matchuser) ? '1' : '0'),
'reserveName' => (isset($this->_req->post->matchname) ? '1' : '0'),
'reserveNames' => str_replace("\r", '', $this->_req->post->reserved)
));
}
// Get the reserved word options and words.
$modSettings['reserveNames'] = str_replace('\n', "\n", $modSettings['reserveNames']);
$context['reserved_words'] = explode("\n", $modSettings['reserveNames']);
$context['reserved_word_options'] = array();
$context['reserved_word_options']['match_word'] = $modSettings['reserveWord'] == '1';
$context['reserved_word_options']['match_case'] = $modSettings['reserveCase'] == '1';
$context['reserved_word_options']['match_user'] = $modSettings['reserveUser'] == '1';
$context['reserved_word_options']['match_name'] = $modSettings['reserveName'] == '1';
// Ready the template......
$context['sub_template'] = 'edit_reserved_words';
$context['page_title'] = $txt['admin_reserved_set'];
createToken('admin-regr');
}
/**
* This function handles registration settings, and provides a few pretty stats too while it's at it.
*
* - General registration settings and Coppa compliance settings.
* - Accessed by ?action=admin;area=regcenter;sa=settings.
* - Requires the admin_forum permission.
*
* @event integrate_save_registration_settings
*/
public function action_registerSettings_display()
{
global $txt, $context, $modSettings;
// Initialize the form
$settingsForm = new SettingsForm(SettingsForm::DB_ADAPTER);
// Initialize it with our settings
$settingsForm->setConfigVars($this->_settings());
// Setup the template
$context['sub_template'] = 'show_settings';
$context['page_title'] = $txt['registration_center'];
if (isset($this->_req->query->save))
{
checkSession();
// Are there some contacts missing?
if (!empty($this->_req->post->coppaAge) && !empty($this->_req->post->coppaType) && empty($this->_req->post->coppaPost) && empty($this->_req->post->coppaFax))
{
throw new Exception('admin_setting_coppa_require_contact');
}
// Post needs to take into account line breaks.
$this->_req->post->coppaPost = str_replace("\n", '<br />', empty($this->_req->post->coppaPost) ? '' : $this->_req->post->coppaPost);
call_integration_hook('integrate_save_registration_settings');
$settingsForm->setConfigValues((array) $this->_req->post);
$settingsForm->save();
redirectexit('action=admin;area=regcenter;sa=settings');
}
$context['post_url'] = getUrl('admin', ['action' => 'admin', 'area' => 'regcenter', 'save', 'sa' => 'settings']);
$context['settings_title'] = $txt['settings'];
// Define some javascript for COPPA.
theme()->addInlineJavascript('
function checkCoppa()
{
let coppaDisabled = document.getElementById(\'coppaAge\').value == 0;
document.getElementById(\'coppaType\').disabled = coppaDisabled;
let disableContacts = coppaDisabled || document.getElementById(\'coppaType\').options[document.getElementById(\'coppaType\').selectedIndex].value != 1;
document.getElementById(\'coppaPost\').disabled = disableContacts;
document.getElementById(\'coppaFax\').disabled = disableContacts;
document.getElementById(\'coppaPhone\').disabled = disableContacts;
}
checkCoppa();', true);
// Turn the postal address into something suitable for a textbox.
$modSettings['coppaPost'] = empty($modSettings['coppaPost']) ? '' : preg_replace('~<br ?/?>~', "\n", $modSettings['coppaPost']);
$settingsForm->prepare();
}
/**
* Return configuration settings for new members registration.
*
* @event integrate_modify_registration_settings
*/
private function _settings()
{
global $txt;
$config_vars = array(
array('select', 'registration_method', array($txt['setting_registration_standard'], $txt['setting_registration_activate'], $txt['setting_registration_approval'], $txt['setting_registration_disabled'])),
array('check', 'notify_new_registration'),
array('check', 'force_accept_agreement'),
array('check', 'force_accept_privacy_policy'),
array('check', 'send_welcomeEmail'),
array('check', 'show_DisplayNameOnRegistration'),
'',
array('int', 'coppaAge', 'subtext' => $txt['setting_coppaAge_desc'], 'onchange' => 'checkCoppa();', 'onkeyup' => 'checkCoppa();'),
array('select', 'coppaType', array($txt['setting_coppaType_reject'], $txt['setting_coppaType_approval']), 'onchange' => 'checkCoppa();'),
array('large_text', 'coppaPost', 'subtext' => $txt['setting_coppaPost_desc']),
array('text', 'coppaFax'),
array('text', 'coppaPhone'),
);
// Add new settings with a nice hook, makes them available for admin settings search as well
call_integration_hook('integrate_modify_registration_settings', array(&$config_vars));
return $config_vars;
}
/**
* Return the registration settings for use in admin search
*/
public function settings_search()
{
return $this->_settings();
}
}