/
register.php
212 lines (168 loc) · 7.15 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
<?
require 'script_start.php';
if(isset($LoggedUser)) {
//Silly user, what are you doing here!
header('Location: index.php');
die();
}
include(SERVER_ROOT.'/classes/class_validate.php');
$Val=NEW VALIDATE;
if(!empty($_REQUEST['confirm'])) {
// Confirm registration
$DB->query("SELECT ID FROM users_main WHERE torrent_pass='".db_string($_REQUEST['confirm'])."' AND Enabled='0'");
list($UserID)=$DB->next_record();
if($UserID) {
$DB->query("UPDATE users_main SET Enabled='1' WHERE ID='$UserID'");
$Cache->increment('stats_user_count');
include('step2.php');
}
} elseif(OPEN_REGISTRATION || !empty($_REQUEST['invite'])) {
$Val->SetFields('username',true,'regex', 'You did not enter a valid username.',array('regex'=>'/^[a-z0-9_?]{1,20}$/iD'));
$Val->SetFields('email',true,'email', 'You did not enter a valid email address.');
$Val->SetFields('password',true,'string', 'You did not enter a valid password (6 - 40 characters).',array('minlength'=>6,'maxlength'=>40));
$Val->SetFields('confirm_password',true,'compare', 'Your passwords do not match.',array('comparefield'=>'password'));
$Val->SetFields('readrules',true,'checkbox', 'You did not check the box that says you will read the rules.');
$Val->SetFields('readwiki',true,'checkbox', 'You did not check the box that says you will read the wiki.');
$Val->SetFields('agereq',true,'checkbox', 'You did not check the box that says you are 13 or older.');
//$Val->SetFields('captcha',true,'string', 'You did not enter a captcha code.',array('minlength'=>6,'maxlength'=>6));
if(!empty($_POST['submit'])) {
// User has submitted registration form
$Err=$Val->ValidateForm($_REQUEST);
/*
if(!$Err && strtolower($_SESSION['captcha'])!=strtolower($_REQUEST['captcha'])) {
$Err="You did not enter the correct captcha code.";
}
*/
if(!$Err) {
$DB->query("SELECT COUNT(ID) FROM users_main WHERE Username LIKE '".db_string(trim($_POST['username']))."'");
list($UserCount)=$DB->next_record();
if($UserCount) {
$Err = "There is already someone registered with that username.";
$_REQUEST['username']='';
}
if($_POST['invite']) {
$DB->query("SELECT InviterID, Email FROM invites WHERE InviteKey='".db_string($_REQUEST['invite'])."'");
if($DB->record_count() == 0) {
$Err = 'Invite does not exist.';
$InviterID=0;
} else {
list($InviterID, $InviteEmail) = $DB->next_record();
}
} else {
$InviterID=0;
}
}
if(!$Err) {
$Secret=make_secret();
$torrent_pass=make_secret();
//Previously SELECT COUNT(ID) FROM users_main, which is a lot slower.
$DB->query("SELECT ID FROM users_main LIMIT 1");
$UserCount = $DB->record_count();
if($UserCount == 0) {
$NewInstall = true;
$Class = SYSOP;
$Enabled = '1';
} else {
$NewInstall = false;
$Class = USER;
$Enabled = '0';
}
$ipcc = geoip($_SERVER['REMOTE_ADDR']);
$DB->query("INSERT INTO users_main
(Username,Email,PassHash,Secret,IP,PermissionID,Enabled,Invites,ipcc) VALUES
('".db_string(trim($_POST['username']))."','".db_string($_POST['email'])."','".db_string(make_hash($_POST['password'],$Secret))."','".db_string($Secret)."','".db_string($_SERVER['REMOTE_ADDR'])."','".$Class."','".$Enabled."','".STARTING_INVITES."', '$ipcc')");
$UserID = $DB->inserted_id();
//User created, delete invite. If things break after this point then it's better to have a broken account to fix, or a 'free' invite floating around that can be reused
$DB->query("DELETE FROM invites WHERE InviteKey='".db_string($_REQUEST['invite'])."'");
$DB->query("SELECT ID FROM stylesheets WHERE `Default`='1'");
list($StyleID) = $DB->next_record();
$AuthKey = make_secret();
$DB->query("INSERT INTO users_info (UserID, StyleID,AuthKey, Inviter, JoinDate) VALUES ('$UserID','$StyleID','".db_string($AuthKey)."', '$InviterID', '".sqltime()."')");
$DB->query("INSERT INTO users_history_ips
(UserID, IP, StartTime) VALUES
('$UserID', '".db_string($_SERVER['REMOTE_ADDR'])."', '".sqltime()."')");
$DB->query("INSERT INTO users_history_emails
(UserID, Email, Time, IP) VALUES
('$UserID', '".db_string($_REQUEST['email'])."', '0000-00-00 00:00:00', '".db_string($_SERVER['REMOTE_ADDR'])."')");
if ($_REQUEST['email'] != $InviteEmail) {
$DB->query("INSERT INTO users_history_emails
(UserID, Email, Time, IP) VALUES
('$UserID', '$InviteEmail', '".sqltime()."', '".db_string($_SERVER['REMOTE_ADDR'])."')");
}
// Manage invite trees, delete invite
if($InviterID !== NULL) {
$DB->query("SELECT
TreePosition, TreeID, TreeLevel
FROM invite_tree WHERE UserID='$InviterID'");
list($InviterTreePosition, $TreeID, $TreeLevel) = $DB->next_record();
// If the inviter doesn't have an invite tree
// Note - this should never happen unless you've transfered from another db, like we have
if($DB->record_count() == 0) {
$DB->query("SELECT MAX(TreeID)+1 FROM invite_tree");
list($TreeID) = $DB->next_record();
$DB->query("INSERT INTO invite_tree
(UserID, InviterID, TreePosition, TreeID, TreeLevel)
VALUES ('$InviterID', '0', '1', '$TreeID', '1')");
$TreePosition = 2;
$TreeLevel = 2;
} else {
$DB->query("SELECT
TreePosition
FROM invite_tree
WHERE TreePosition>'$InviterTreePosition'
AND TreeLevel<='$TreeLevel'
AND TreeID='$TreeID'
ORDER BY TreePosition
LIMIT 1");
list($TreePosition) = $DB->next_record();
if($TreePosition) {
$DB->query("UPDATE invite_tree SET TreePosition=TreePosition+1 WHERE TreeID='$TreeID' AND TreePosition>='$TreePosition'");
} else {
$DB->query("SELECT
TreePosition+1
FROM invite_tree
WHERE TreeID='$TreeID'
ORDER BY TreePosition DESC
LIMIT 1");
list($TreePosition) = $DB->next_record();
}
$TreeLevel++;
// Create invite tree record
$DB->query("INSERT INTO invite_tree
(UserID, InviterID, TreePosition, TreeID, TreeLevel) VALUES
('$UserID', '$InviterID', '$TreePosition', '$TreeID', '$TreeLevel')");
}
} else { // No inviter (open registration)
$DB->query("SELECT MAX(TreeID) FROM invite_tree");
list($TreeID) = $DB->next_record();
$TreeID++;
$InviterID = 0;
$TreePosition=1;
$TreeLevel=1;
}
include('class_templates.php');
$TPL=NEW TEMPLATE;
$TPL->open('new_registration.tpl');
$TPL->set('Username',$_REQUEST['username']);
$TPL->set('TorrentKey',$torrent_pass);
$TPL->set('SITE_NAME',SITE_NAME);
$TPL->set('SITE_URL',SITE_URL);
send_email($_REQUEST['email'],'New account confirmation at '.SITE_NAME,$TPL->get(),'noreply');
$Sent=1;
}
} elseif(isset($_GET['invite']) && !empty($_GET['invite'])) {
// If they haven't submitted the form, check to see if their invite is good
$DB->query("SELECT InviteKey FROM invites WHERE InviteKey='".db_string($_GET['invite'])."'");
if($DB->record_count() == 0){
error('Invite not found!');
}
}
include('step1.php');
} elseif(!OPEN_REGISTRATION) {
if (isset($_GET['welcome'])) {
include('code.php');
} else {
include('closed.php');
}
}
?>