forked from osuripple/hanayo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
register.go
315 lines (279 loc) · 8.25 KB
/
register.go
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
package main
import (
"database/sql"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/asaskevich/govalidator"
"github.com/gin-gonic/gin"
"github.com/kawatapw/api/common"
)
func register(c *gin.Context) {
if getContext(c).User.ID != 0 {
resp403(c)
return
}
if c.Query("stopsign") != "1" {
u, _ := tryBotnets(c)
if u != "" {
simple(c, getSimpleByFilename("register/elmo.html"), nil, map[string]interface{}{
"Username": u,
})
return
}
}
registerResp(c)
}
func registerSubmit(c *gin.Context) {
if getContext(c).User.ID != 0 {
resp403(c)
return
}
// check registrations are enabled
if !registrationsEnabled() {
registerResp(c, errorMessage{T(c, "Sorry, it's not possible to register at the moment. Please try again later.")})
return
}
// check username is valid by our criteria
username := strings.TrimSpace(c.PostForm("username"))
if !usernameRegex.MatchString(username) {
registerResp(c, errorMessage{T(c, "Your username must contain alphanumerical characters, spaces, or any of <code>_[]-</code>")})
return
}
// check whether an username is e.g. cookiezi, shigetora, peppy, wubwoofwolf, loctav
if in(strings.ToLower(username), forbiddenUsernames) {
registerResp(c, errorMessage{T(c, "You're not allowed to register with that username.")})
return
}
// check email is valid
if !govalidator.IsEmail(c.PostForm("email")) {
registerResp(c, errorMessage{T(c, "Please pass a valid email address.")})
return
}
// passwords check (too short/too common)
if x := validatePassword(c.PostForm("password")); x != "" {
registerResp(c, errorMessage{T(c, x)})
return
}
// usernames with both _ and spaces are not allowed
if strings.Contains(username, "_") && strings.Contains(username, " ") {
registerResp(c, errorMessage{T(c, "An username can't contain both underscores and spaces.")})
return
}
// check whether username already exists
if db.QueryRow("SELECT 1 FROM users WHERE username_safe = ?", safeUsername(username)).
Scan(new(int)) != sql.ErrNoRows {
registerResp(c, errorMessage{T(c, "An user with that username already exists!")})
return
}
// check whether an user with that email already exists
if db.QueryRow("SELECT 1 FROM users WHERE email = ?", c.PostForm("email")).
Scan(new(int)) != sql.ErrNoRows {
registerResp(c, errorMessage{T(c, "An user with that email address already exists!")})
return
}
// recaptcha verify
if config.RecaptchaPrivate != "" && !recaptchaCheck(c) {
registerResp(c, errorMessage{T(c, "Captcha is invalid.")})
return
}
// The actual registration.
pass, err := generatePassword(c.PostForm("password"))
if err != nil {
resp500(c)
return
}
res, err := db.Exec(`INSERT INTO users(username, username_safe, password_md5, salt, email, register_datetime, privileges, password_version)
VALUES (?, ?, ?, '', ?, ?, ?, 2);`,
username, safeUsername(username), pass, c.PostForm("email"), time.Now().Unix(), common.UserPrivilegePendingVerification)
if err != nil {
registerResp(c, errorMessage{T(c, "Whoops, an error slipped in. You might have been registered, though. I don't know.")})
fmt.Printf(err.Error())
return
}
lid, _ := res.LastInsertId()
db.Exec("INSERT INTO `users_stats`(id, username, user_color, user_style, ranked_score_std, playcount_std, total_score_std, ranked_score_taiko, playcount_taiko, total_score_taiko, ranked_score_ctb, playcount_ctb, total_score_ctb, ranked_score_mania, playcount_mania, total_score_mania) VALUES (?, ?, 'black', '', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);", lid, username)
db.Exec("INSERT INTO `users_stats_relax`(id, username, user_color, user_style, ranked_score_std, playcount_std, total_score_std, ranked_score_taiko, playcount_taiko, total_score_taiko, ranked_score_ctb, playcount_ctb, total_score_ctb, ranked_score_mania, playcount_mania, total_score_mania) VALUES (?, ?, 'black', '', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);", lid, username)
db.Exec("INSERT INTO `users_prefrences`(id, score_overwrite_std, score_overwrite_taiko, score_overwrite_ctb, score_overwrite_mania, socreboard_display_classic, scoreboard_display_relax, auto_last_classic, auto_last_relax) VALUES (?, 0, 0, 0, 0, 0, 0, 0, 0);", lid)
setYCookie(int(lid), c)
logIP(c, int(lid))
rd.Incr("ripple:registered_users")
addMessage(c, successMessage{T(c, "You have been successfully registered on Kawata! You now need to verify your account.")})
getSession(c).Save()
c.Redirect(302, "/register/verify?u="+strconv.Itoa(int(lid)))
}
func registerResp(c *gin.Context, messages ...message) {
resp(c, 200, "register/register.html", &baseTemplateData{
TitleBar: "Register",
KyutGrill: "register.jpg",
Scripts: []string{"https://www.google.com/recaptcha/api.js"},
Messages: messages,
FormData: normaliseURLValues(c.Request.PostForm),
})
}
func registrationsEnabled() bool {
var enabled bool
db.QueryRow("SELECT value_int FROM system_settings WHERE name = 'registrations_enabled'").Scan(&enabled)
return enabled
}
func verifyAccount(c *gin.Context) {
if getContext(c).User.ID != 0 {
resp403(c)
return
}
i, ret := checkUInQS(c)
if ret {
return
}
sess := getSession(c)
var rPrivileges uint64
db.Get(&rPrivileges, "SELECT privileges FROM users WHERE id = ?", i)
if common.UserPrivileges(rPrivileges)&common.UserPrivilegePendingVerification == 0 {
addMessage(c, warningMessage{T(c, "Nope.")})
sess.Save()
c.Redirect(302, "/")
return
}
resp(c, 200, "register/verify.html", &baseTemplateData{
TitleBar: "Verify account",
HeadingOnRight: true,
KyutGrill: "welcome.jpg",
})
}
func welcome(c *gin.Context) {
if getContext(c).User.ID != 0 {
resp403(c)
return
}
i, ret := checkUInQS(c)
if ret {
return
}
var rPrivileges uint64
db.Get(&rPrivileges, "SELECT privileges FROM users WHERE id = ?", i)
if common.UserPrivileges(rPrivileges)&common.UserPrivilegePendingVerification > 0 {
c.Redirect(302, "/register/verify?u="+c.Query("u"))
return
}
t := T(c, "Welcome!")
if common.UserPrivileges(rPrivileges)&common.UserPrivilegeNormal == 0 {
// if the user has no UserNormal, it means they're banned = they multiaccounted
t = T(c, "Welcome back!")
}
resp(c, 200, "register/welcome.html", &baseTemplateData{
TitleBar: t,
HeadingOnRight: true,
KyutGrill: "welcome.jpg",
})
}
// Check User In Query Is Same As User In Y Cookie
func checkUInQS(c *gin.Context) (int, bool) {
sess := getSession(c)
i, _ := strconv.Atoi(c.Query("u"))
y, _ := c.Cookie("y")
err := db.QueryRow("SELECT 1 FROM identity_tokens WHERE token = ? AND userid = ?", y, i).Scan(new(int))
if err == sql.ErrNoRows {
addMessage(c, warningMessage{T(c, "Nope.")})
sess.Save()
c.Redirect(302, "/")
return 0, true
}
return i, false
}
func tryBotnets(c *gin.Context) (string, string) {
var username string
err := db.QueryRow("SELECT u.username FROM ip_user i INNER JOIN users u ON u.id = i.userid WHERE i.ip = ?", clientIP(c)).Scan(&username)
if err != nil {
if err != sql.ErrNoRows {
c.Error(err)
}
return "", ""
}
if username != "" {
return username, "IP"
}
cook, _ := c.Cookie("y")
err = db.QueryRow("SELECT u.username FROM identity_tokens i INNER JOIN users u ON u.id = i.userid WHERE i.token = ?",
cook).Scan(&username)
if err != nil {
if err != sql.ErrNoRows {
c.Error(err)
}
return "", ""
}
if username != "" {
return username, "username"
}
return "", ""
}
func in(s string, ss []string) bool {
for _, x := range ss {
if x == s {
return true
}
}
return false
}
var usernameRegex = regexp.MustCompile(`^[A-Za-z0-9 _\[\]-]{2,15}$`)
var forbiddenUsernames = []string{
"peppy",
"rrtyui",
"cookiezi",
"azer",
"loctav",
"banchobot",
"happystick",
"doomsday",
"sharingan33",
"andrea",
"cptnxn",
"reimu-desu",
"hvick225",
"_index",
"my aim sucks",
"kynan",
"rafis",
"sayonara-bye",
"thelewa",
"wubwoofwolf",
"millhioref",
"tom94",
"tillerino",
"clsw",
"spectator",
"exgon",
"axarious",
"angelsim",
"recia",
"nara",
"emperorpenguin83",
"bikko",
"xilver",
"vettel",
"kuu01",
"_yu68",
"tasuke912",
"dusk",
"ttobas",
"velperk",
"jakads",
"jhlee0133",
"abcdullah",
"yuko-",
"entozer",
"hdhr",
"ekoro",
"snowwhite",
"osuplayer111",
"musty",
"nero",
"elysion",
"ztrot",
"koreapenguin",
"fort",
"asphyxia",
"niko",
"shigetora",
}