-
Notifications
You must be signed in to change notification settings - Fork 75
/
register.php
447 lines (383 loc) · 14 KB
/
register.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
<?php
// PHP 4.1.0 may have issues with the above code.
unset ( $_SESSION['webcal_login'] );
unset ( $_SESSION['webcalendar_session'] );
require_once 'includes/translate.php';
require_once 'includes/classes/WebCalendar.php';
$WebCalendar = new WebCalendar( __FILE__ );
require_once 'includes/config.php';
require_once 'includes/dbi4php.php';
require_once 'includes/formvars.php';
require_once 'includes/functions.php';
$WebCalendar->initializeFirstPhase();
require_once "includes/$user_inc";
require_once 'includes/access.php';
require_once 'includes/gradient.php';
$WebCalendar->initializeSecondPhase();
load_global_settings();
// TODO make this an option for external users.
$htmlmail = false;
load_user_preferences('guest');
$WebCalendar->setLanguage();
require_once 'includes/classes/WebCalMailer.php';
$mail = new WebCalMailer;
/*
From the documentation, the following settings are important here.
Allow self-registration ($ALLOW_SELF_REGISTRATION = Y):
If enabled, new users are permitted to setup their own accounts and log into WebCalendar
without admin intervention. Use with caution!
Restrict self-registration to blacklist ($SELF_REGISTRATION_BLACKLIST = Y):
If enabled, admin can configure the includes/blacklist.php to restrict or permit
self-registration based on the user's IP. This will restrict access to existing
users or new user's set up by admin. Details and examples are available in the top
of the file.
Generate passwords and send to new users ($SELF_REGISTRATION_FULL = Y):
If enabled, self-registration user's will be emailed a randomly generated password
that they can then use to access WebCalendar normally. Hopefully, this will prevent
some spammers and hackers from misusing the self-registration process.
*/
$appStr = generate_application_name();
$notauth = print_not_auth();
$error = (empty($ALLOW_SELF_REGISTRATION) || $ALLOW_SELF_REGISTRATION != 'Y'
? $notauth : '');
if (empty($SELF_REGISTRATION_FULL) || $SELF_REGISTRATION_FULL != 'Y')
$SELF_REGISTRATION_FULL = 'N';
// Do email the user their password? Or do we allow them to set it here?
$form_control = ($SELF_REGISTRATION_FULL == 'Y' ? 'email' : 'full');
/**
* See if username and email are unique.
*
* param: $isWhat string What are we looking for; user login or user email?
* param: $isWher string Where are we looking; "login" or "email"?
*
* Return true if all is OK.
*/
function checks($isWhat, $isWher)
{
global $control, $error;
if (!strlen($isWhat)) {
$error = ($isWher == 'login'
? translate('Username cannot be blank.')
: translate('Email address cannot be blank.'));
return false;
}
$res = dbi_execute('SELECT cal_' . $isWher . ' FROM webcal_user
WHERE cal_' . $isWher . ' = ?', [$isWhat]);
if ($res) {
$row = dbi_fetch_row($res);
if ($row[0] == $isWhat) {
$control = '';
$error = ($isWher == 'login'
? translate('Username already exists.')
: translate('Email address already exists.'));
return false;
}
}
return true;
}
/**
* Generate unique password.
*/
function generate_password()
{
$pass = '';
$pass_length = 8;
$salt = 'abchefghjkmnpqrstuvwxyz0123456789';
srand((float) microtime() * 1000000);
$i = 0;
while ($i < $pass_length) {
$pass .= substr($salt, rand() % 33, 1);
$i++;
}
return $pass;
}
$uemail =
$ufirstname =
$ulastname =
$upassword1 =
$upassword2 =
$user = '';
// We can limit what domain is allowed to self register.
// $self_registration_domain should have this format "192.168.220.0:255.255.240.0";
$valid_ip = validate_domain();
if (empty($valid_ip))
$error = $notauth;
// We could make $control a unique value if necessary.
$control = getPostValue('control');
$illegalCharStr = translate('Illegal characters in login XXX.');
if (empty($error) && !empty($control)) {
$uemail = getPostValue('uemail');
$ufirstname = getPostValue('ufirstname');
$uis_admin = 'N';
$ulastname = getPostValue('ulastname');
$user = trim(getPostValue('user'));
if ($user != addslashes($user))
$error = str_replace('XXX', htmlentities($user), $illegalCharStr);
// Check to make sure user doesn't already exist.
checks($user, 'login');
// Check to make sure email address doesn't already exist.
checks($uemail, 'email');
}
if (empty($error) && !empty($control)) {
if ($control == 'full') {
// Process full account addition.
$upassword1 = getPostValue('upassword1');
$upassword2 = getPostValue('upassword2');
// Do some checking of user info.
if (!empty($user) && !empty($upassword1)) {
$user = trim($user);
if ($user != addslashes($user))
$error = str_replace('XXX', htmlentities($user), $illegalCharStr);
} elseif ($upassword1 != $upassword2) {
$control = '';
$error = translate('The passwords were not identical.');
}
if (empty($error)) {
if (user_add_user(
$user,
$upassword1,
$ufirstname,
$ulastname,
$uemail,
$uis_admin
) ) {
echo "Success: $user"; exit;
activity_log(
0,
'system',
$user,
LOG_NEWUSER_FULL,
translate('New user via self-registration.')
);
} else {
$error = dbi_error();
}
}
} elseif ($control == 'email') {
// Process account info for email submission.
// Need to generate unique passwords and email them to the new user.
$new_pass = generate_password();
// TODO allow admin to approve account and emails prior to processing.
user_add_user(
$user,
$new_pass,
$ufirstname,
$ulastname,
$uemail,
$uis_admin
);
$tempName = trim($ufirstname . ' ' . $ulastname);
$msg = str_replace(
', XXX.',
(strlen($tempName) ? ', ' . $tempName . '.' : '.'),
translate('Hello, XXX.')
) . "\n\n"
. translate('A new WebCalendar account has been set up for you.')
. "\n\n"
. str_replace('XXX', $user, translate('Your username is XXX.'))
. "\n\n"
. str_replace('XXX', $new_pass, translate('Your password is XXX.'))
. "\n\n"
. str_replace(
'XXX',
$appStr,
translate('Please visit XXX to log in and start using your account!')
)
. "\n";
// Add URL to event
$url = getServerUrl() . 'login.php';
if ($htmlmail == 'Y')
$url = activate_urls($url);
$msg .= "\n\n" . $url;
$msg .= "\n\n"
. translate('You may change your password after logging in the first time.')
. "\n\n" . translate('If you received this email in error') . "\n\n";
$adminStr = translate('Administrator', true);
$name = $appStr . ' ' . translate('Welcome') . ': ' . $ufirstname;
// Send via WebCalMailer class.
$mail->WC_Send($adminStr, $uemail, $ufirstname . ' '
. $ulastname, $name, $msg, $htmlmail, $EMAIL_FALLBACK_FROM);
activity_log(
0,
'system',
$user,
LOG_NEWUSER_EMAIL,
translate('New user via email.')
);
}
}
echo send_doctype($appStr);
echo $ASSETS;
echo "<script>\n";
?>
var validform = false, xlate = [];
xlate['inputPassword'] = '<?php etranslate('You have not entered a password.', true);?>';
xlate['noBlankUsername'] = '<?php etranslate('Username cannot be blank.', true);?>';
xlate['noBlankEmail'] = '<?php etranslate('Email cannot be blank.', true);?>';
xlate['passwordsNoMatch'] = '<?php etranslate('The passwords were not identical.', true);?>';
</script>
<link href="css_cacher.php?login=__public__" rel="stylesheet">
<link href="includes/css/styles.css" rel="stylesheet">
<?php
// Print custom header (since we do not call print_header function)
if (!empty($CUSTOM_SCRIPT) && $CUSTOM_SCRIPT == 'Y') {
load_template($login, 'S');
}
?>
</head>
<body id="register" class="container-fluid">
<div class="container">
<h2><?php echo $appStr . ' ' . translate('Registration');?></h2>
<!-- Error Alert -->
<div id="main-dialog-alert" class="alert alert-danger" style="<?php echo empty($error)? "display: none" : "display: block"?>">
<span id="infoMessage"><?php echo translate('Error'). ": $error";?></span>
<button type="button" class="close" onclick="$('.alert').hide()">×</button>
</div>
<?php if (!empty($control)) { ?>
<div class="row">
<div><?php etranslate('Welcome to WebCalendar');?></div>
</div>
<?php if ($SELF_REGISTRATION_FULL == 'Y') { ?>
<div class="row">
<div><?php etranslate('Your email should arrive shortly.');?><br>
<a href="login.php"><?php etranslate('Return to Login screen');?></a></div>
</div>
<?php } ?>
<?php } else { ?>
<form action="register.php" method="post" onSubmit="return valid_form()"
name="selfreg">
<input type="hidden" name="control" value="<?php echo $form_control;?>">
<div class="form-group row">
<label for="login" class="text-info"><?php etranslate('Username')?>:</label><br>
<input type="text" name="user" id="user" class="form-control" value="<?php echo htmlspecialchars($user);?>"
size="20" maxlength="20" onchange="valid_form();">
</div>
<div class="form-group row">
<label for="login" class="text-info"><?php etranslate('First Name')?>:</label><br>
<input type="text" name="ufirstname" id="ufirstname" class="form-control" value="<?php echo htmlspecialchars($ufirstname);?>"
size="25" maxlength="25">
</div>
<div class="form-group row">
<label for="login" class="text-info"><?php etranslate('Last Name')?>:</label><br>
<input type="text" name="ulastname" id="ulastname" class="form-control" value="<?php echo htmlspecialchars($ulastname);?>"
size="25" maxlength="25">
</div>
<div class="form-group row">
<label for="login" class="text-info"><?php etranslate('E-mail address')?>:</label><br>
<input type="text" name="uemail" id="uemail" class="form-control" value="<?php echo htmlspecialchars($uemail);?>"
size="40" maxlength="75" onchange="valid_form();">
</div>
<?php if ($SELF_REGISTRATION_FULL != 'Y') { ?>
<div class="form-group row">
<label for="login" class="text-info"><?php etranslate('Password')?>:</label><br>
<input type="password" name="upassword1" id="upassword1" class="form-control" value="<?php echo htmlspecialchars($upassword1);?>"
size="30" maxlength="32" onchange="valid_form();">
</div>
<div class="form-group row">
<label for="login" class="text-info"><?php etranslate('Password (again)')?>:</label><br>
<input type="password" name="upassword2" id="upassword2" class="form-control" value="<?php echo htmlspecialchars($upassword2);?>"
size="30" maxlength="32" onchange="valid_form();">
</div>
<?php } else { ?>
<div class="form-group row">
<?php etranslate('Your account information will be emailed to you.'); ?>
</div>
<?php } ?>
<div class="form-group row justify-content-md-center">
<button id="submitButton" type="submit" class="btn btn-primary"><?php etranslate('Submit');?></button>
</div>
</form>
<?php } ?>
<br>
<span class="cookies"><?php etranslate('cookies-note');?></span><br>
<hr>
<a href="<?php echo $PROGRAM_URL;?>" id="programname"><?php echo $PROGRAM_NAME;?></a>
<?php
// Print custom trailer (since we do not call print_trailer function).
if (!empty($CUSTOM_TRAILER) && $CUSTOM_TRAILER == 'Y') {
$res = dbi_execute('SELECT cal_template_text FROM webcal_report_template
WHERE cal_template_type = \'T\' and cal_report_id = 0');
if ($res) {
if ($row = dbi_fetch_row($res))
echo $row[0];
dbi_free_result($res);
}
}
?>
</div>
<script>
var validform = false;
function valid_form() {
validform = true;
document.querySelector(':button[type="submit"]').prop('disabled', false);
if ($('#upassword1').length && $('#upassword1').val().length == 0) {
$('#infoMessage').html(xlate['inputPassword']);
$('#main-dialog-alert').show();
validform = false;
}
if ($('#upassword1').length && $('#upassword1').val().length == 0) {
$('#infoMessage').html(xlate['inputPassword']);
$('#main-dialog-alert').show();
validform = false;
}
if ($('#user').val().length == 0) {
$('#infoMessage').html(xlate['noBlankUsername']);
$('#main-dialog-alert').show();
validform = false;
}
if ($('#uemail').val().length == 0) {
$('#infoMessage').html(xlate['noBlankEmail']);
$('#main-dialog-alert').show();
validform = false;
}
if ($('#upassword1').length && $('#upassword1').val() != $('#upassword2').val()) {
$('#infoMessage').html(xlate['passwordsNoMatch']);
$('#main-dialog-alert').show();
validform = false;
}
// Only do AJAX checks if we still think form is valid.
if (validform) {
checkers('user', 'register');
checkers('uemail', 'email');
}
document.querySelector(':button[type="submit"]').prop('disabled', !validform);
return validform;
}
function checkers(formfield, params) {
var val = $('#' + formfield).val();
$.post('ajax.php', {
page: params,
name: val
},
function(data, status) {
var stringified = JSON.stringify(data);
console.log("checkers Data: " + stringified + "\nStatus: " + status);
try {
var response = jQuery.parseJSON(stringified);
console.log('checkers response=' + response);
} catch (err) {
alert('<?php etranslate('Error'); ?>: <?php etranslate('JSON error'); ?> - ' + err);
return;
}
if (response) {
$('#infoMessage').html('<?php etranslate('Error'); ?>: ' + response);
$('#main-dialog-alert').show();
validform = false;
if(!validform) {
document.querySelector(':button[type="submit"]').prop('disabled', true);
}
return;
}
if(!validform) {
document.querySelector(':button[type="submit"]').prop('disabled', true);
} else {
document.querySelector('#main-dialog-alert').hide();
}
});
}
$(document).ready(function() {
valid_form();
});
</script>
</body>
</html>