forked from userfrosting/UserFrosting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_create_user.php
194 lines (171 loc) · 6 KB
/
user_create_user.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
<?php
/*
UserFrosting Version: 0.1
By Alex Weissman
Copyright (c) 2014
Based on the UserCake user management system, v2.0.2.
Copyright (c) 2009-2012
UserFrosting, like UserCake, is 100% free and open-source.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// Request method: POST
require_once("models/config.php");
set_error_handler('logAllErrors');
if (!securePage($_SERVER['PHP_SELF'])){
addAlert("danger", "Whoops, looks like you don't have permission to create an account.");
if (isset($_POST['ajaxMode']) and $_POST['ajaxMode'] == "true" ){
echo json_encode(array("errors" => 1, "successes" => 0));
} else {
header("Location: " . getReferralPage());
}
exit();
}
if (!fetchUserDetails(NULL, NULL, '1')){
addAlert("danger", lang("MASTER_ACCOUNT_NOT_EXISTS"));
header("Location: install/register_root.php");
exit();
}
// If registration is disabled, send them back to the home page with an error message
if (!$can_register){
addAlert("danger", lang("ACCOUNT_REGISTRATION_DISABLED"));
if (isset($_POST['ajaxMode']) and $_POST['ajaxMode'] == "true" ){
echo json_encode(array("errors" => 1, "successes" => 0));
} else {
header("Location: login.php");
}
exit();
}
//Prevent the user visiting the logged in page if he/she is already logged in
if(isUserLoggedIn()) {
addAlert("danger", "I'm sorry, you cannot register for an account while logged in. Please log out first.");
if (isset($_POST['ajaxMode']) and $_POST['ajaxMode'] == "true" ){
echo json_encode(array("errors" => 1, "successes" => 0));
} else {
header("Location: account.php");
}
exit();
}
//Forms posted
if(!empty($_POST))
{
$new_user_id = "";
$errors = array();
$email = trim($_POST["email"]);
$username = trim($_POST["username"]);
$displayname = trim($_POST["displayname"]);
$password = trim($_POST["password"]);
$confirm_pass = trim($_POST["passwordc"]);
$captcha = md5($_POST["captcha"]);
if ($captcha != $_SESSION['captcha'])
{
$errors[] = lang("CAPTCHA_FAIL");
}
if(minMaxRange(1,25,$username))
{
$errors[] = lang("ACCOUNT_USER_CHAR_LIMIT",array(1,25));
}
if(!ctype_alnum($username)){
$errors[] = lang("ACCOUNT_USER_INVALID_CHARACTERS");
}
if(minMaxRange(1,50,$displayname))
{
$errors[] = lang("ACCOUNT_DISPLAY_CHAR_LIMIT",array(1,50));
}
if(!isValidName($displayname)){
$errors[] = lang("ACCOUNT_DISPLAY_INVALID_CHARACTERS");
}
if(minMaxRange(8,50,$password) && minMaxRange(8,50,$confirm_pass))
{
$errors[] = lang("ACCOUNT_PASS_CHAR_LIMIT",array(8,50));
}
else if($password != $confirm_pass)
{
$errors[] = lang("ACCOUNT_PASS_MISMATCH");
}
if(!isValidEmail($email))
{
$errors[] = lang("ACCOUNT_INVALID_EMAIL");
}
//End data validation
if(count($errors) == 0)
{
//Construct a user object
$user = new User($username, $displayname, $new_user_title, $password, $email);
//Checking this flag tells us whether there were any errors such as possible data duplication occured
if(!$user->status)
{
if($user->username_taken) $errors[] = lang("ACCOUNT_USERNAME_IN_USE",array($username));
if($user->displayname_taken) $errors[] = lang("ACCOUNT_DISPLAYNAME_IN_USE",array($displayname));
if($user->email_taken) $errors[] = lang("ACCOUNT_EMAIL_IN_USE",array($email));
}
else
{
//Attempt to add the user to the database, carry out finishing tasks like emailing the user (if required)
//Attempt to add the user to the database, carry out finishing tasks like emailing the user (if required)
$new_user_id = $user->userCakeAddUser();
if($new_user_id == -1)
{
if($user->mail_failure) $errors[] = lang("MAIL_ERROR");
if($user->sql_failure) $errors[] = lang("SQL_ERROR");
}
}
}
// If everything went well, add default permissions for the new user
if(count($errors) == 0) {
// Get default permissions
$permissions = fetchAllPermissions();
$add = array();
foreach ($permissions as $permission){
if ($permission['is_default'] == 1) {
$permission_id = $permission['id'];
$add[$permission_id] = $permission_id;
}
}
if ($addition_count = addPermission($add, $new_user_id)){
// Uncomment this if you want self-registered users to know about permission groups
//$successes[] = lang("ACCOUNT_PERMISSION_ADDED", array ($addition_count));
}
else {
$errors[] = lang("SQL_ERROR");
}
}
if(count($errors) == 0) {
$successes[] = $user->success;
}
}
restore_error_handler();
foreach ($errors as $error){
addAlert("danger", $error);
}
foreach ($successes as $success){
addAlert("success", $success);
}
// Send successfully registered users to the login page, while errors should return them to the registration page.
if (isset($_POST['ajaxMode']) and $_POST['ajaxMode'] == "true" ){
echo json_encode(array(
"errors" => count($errors),
"successes" => count($successes)));
} else {
if(count($errors) == 0) {
header('Location: login.php');
exit();
} else {
header('Location: register.php');
exit();
}
}
?>