-
Notifications
You must be signed in to change notification settings - Fork 6
/
team.php
362 lines (288 loc) · 8.44 KB
/
team.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
<?php
//Author: Kadir CETINKAYA - breakv0id@0xdeffbeef
require_once("db.php");
class Teams
{
private $msg;
function __construct()
{
global $lteam;
$this->msg = $lteam;
}
function register($name, $pass, $mail, $members)
{
global $db;
if(preg_match("/\s/", $name) == true) //Whitespace in name
throw new Exception($this->msg[0]);
if(preg_match("/\s/", $mail) == true) //Whitespace in mail
throw new Exception($this->msg[1]);
if(is_array($members) == false) //Bad Request
throw new Exception($this->msg[2]);
if(strlen($name)>30 || strlen($mail)>40 || strlen($pass)>30)
throw new Exception($this->msg[2]);
$name = $name;
$mail = $mail;
$sql = "SELECT * FROM `teams` WHERE `name`=? or `mail`=?";
$stmt = $db->prepare($sql);
$stmt->bind_param("ss", $name, $mail);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows!=0) //Credentials in use
throw new Exception($this->msg[3]);
$stmt->close();
$salt = generateSalt();
$hash = hashPass($salt, $pass);
$verify = base64_encode(generateSalt());
$salt = base64_encode($salt);
$members = implode('<%#:#%>', $members);
$sql = "INSERT INTO `teams` (`name`, `mail`, `pass`, `salt`, `members`, `status`, `regdate`, `verification`) ";
$sql.= "VALUES(?, ?, ?, ?, ?, 0, NOW(), ?);";
$stmt = $db->prepare($sql);
$stmt->bind_param("ssssss", $name, $mail, $hash, $salt, $members, $verify);
$stmt->execute();
$stmt->close();
$text = $this->parse($this->msg[7], array("\$name"=>urlencode($name), "\$verify"=>urlencode($verify),"\$pass"=>$pass));
sendMail($mail, $this->msg[6]/*Validation*/, $text);
return true;
}
function login($name, $pass)
{
global $db;
if(preg_match("/\s/", $name) == true) //Whitespace in name
throw new Exception($this->msg[0]);
$id = 0;
$salt = "";
$hash = "";
$status = 0;
$sql = "SELECT `id`, `salt`, `pass`, `status` FROM `teams` WHERE `name`=?";
$stmt = $db->prepare($sql);
$stmt->bind_param("s", $name);
$stmt->bind_result($id, $salt, $hash, $status);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows!=1) //Wrong credential
throw new Exception($this->msg[4]);
$stmt->fetch();
$stmt->close();
$salt = base64_decode($salt);
if(hashPass($salt, $pass)!==$hash) //Wrong credential
throw new Exception($this->msg[4]);
if($status === 0) //Email verification
throw new Exception($this->msg[5]);
$salt = generateSalt();
$hash = hashPass($salt, $pass);
$salt = base64_encode($salt);
$sql = "UPDATE `teams` SET `pass`=?, `salt`=? WHERE `id`=?";
$stmt = $db->prepare($sql);
$stmt->bind_param("ssi", $hash, $salt, $id);
$stmt->execute();
$stmt->close();
return $this->getTeamID($id);
}
function parse($string, $data)
{
$res = $string;
foreach($data as $key => $value)
eval($key ." = '".$value."';");
eval("\$res = \"".$string."\";");
return $res;
}
function getTeamID($id)
{
return new Team($id);
}
function getTeamMail($email)
{
global $db;
global $lang;
$email = $email;
$id = 0;
$sql = "SELECT `id` FROM `teams` WHERE `mail`=?";
$stmt = $db->prepare($sql);
$stmt->bind_param("s", $email);
$stmt->bind_result($id);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
if($stmt->num_rows!=1)
throw new Exception($lang['team'][9]);
$stmt->close();
return $this->getTeamID($id);
}
function getTeamName($name)
{
global $lang;
global $db;
$id = 0;
$sql = "SELECT `id` FROM `teams` WHERE `name`=?";
$stmt = $db->prepare($sql);
$stmt->bind_param("s", $name);
$stmt->bind_result($id);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
if($stmt->num_rows!=1)
throw new Exception($lang['team'][10]);
$stmt->close();
return $this->getTeamID($id);
}
function getTeams($adm=false)
{
global $db;
if($adm)
$sql = "SELECT `id` FROM `teams` WHERE `status`=1 ORDER BY `realscore` DESC";
else
$sql = "SELECT `id` FROM `teams` WHERE `status`=1 ORDER BY `score` DESC";
$res = $db->query($sql);
$teams = array();
while($tmp = $res->fetch_array())
$teams[] = $this->getTeamID($tmp[0]);
return $teams;
}
}
class Team
{
public $name = "";
public $members = "";
public $id = 0;
public $score = 0;
public $realscore = 0;
public $mail = "";
public $locale = "";
public $rank = 0;
public $status = 0;
function __construct($id)
{
global $db;
global $lang;
$name = "";
$sql = "SELECT `name`, `members`, `score`, `realscore`, `mail`, `lang`, `status` FROM `teams` WHERE `id` = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->bind_result($this->name, $this->members, $this->score, $this->realscore, $this->mail, $this->locale, $this->status);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
if($stmt->num_rows!=1) //Wrong ID :C
throw new Exception($lang['team'][8]);
$stmt->close();
$sql = "SELECT count(`id`) FROM `teams` WHERE `score`>? AND `status`=1";
$stmt = $db->prepare($sql);
$stmt->bind_param("i", $this->score);
$stmt->bind_result($this->rank);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
$this->rank++;
$this->id = (int)$id;
$this->members = explode("<%#:#%>", $this->members);
foreach($this->members as $key=>$value)
$this->mebers[$key] = htmlspecialchars($value, ENT_QUOTES);
$this->name=htmlspecialchars($this->name, ENT_QUOTES);
$this->mail=htmlspecialchars($this->mail, ENT_QUOTES);
}
function update($pass="")
{
global $db;
global $lang;
$sql = "UPDATE `teams` SET `score` = ?, `realscore` = ?, `lang` = ?, `members`=? WHERE `id` = ?";
$stmt = $db->prepare($sql);
$members = $this->members;
$members = implode("<%#:#%>", $members);
$stmt->bind_param("iissi", $this->score, $this->realscore, $this->locale, $members, $this->id);
$stmt->execute();
$stmt->close();
if(strlen($pass)>30)
throw new Exception($lang['team'][3]);
if(strlen($pass)>0)
{
$salt = generateSalt();
$hash = hashPass($salt, $pass);
$salt = base64_encode($salt);
$sql = "UPDATE `teams` SET `pass`=?, `salt`=? WHERE `id`=?";
$stmt = $db->prepare($sql);
$stmt->bind_param("ssi", $hash, $salt, $this->id);
$stmt->execute();
$stmt->close();
}
return true;
}
function updScore()
{
global $db;
$sql = "SELECT `score`, `realscore` FROM `teams` WHERE `id`=?";
$stmt = $db->prepare($sql);
$stmt->bind_param("i", $this->id);
$stmt->bind_result($this->score, $this->realscore);
$stmt->execute();
$stmt->close();
}
function upd($pass="")
{
global $db;
$sql = "UPDATE `teams` SET `score` = ?, `realscore` = ?, `lang` = ?, `members`=?, `name`=?, `mail`=?, `status`=? WHERE `id` = ?";
$stmt = $db->prepare($sql);
$name = $this->name;
$members = $this->members;
$mail = $this->mail;
$members = implode("<%#:#%>", $members);
$stmt->bind_param("iissssii", $this->score, $this->realscore, $this->locale, $members, $name, $mail, $this->status, $this->id);
$stmt->execute();
$stmt->close();
if(strlen($pass)>0)
{
$salt = generateSalt();
$hash = hashPass($salt, $pass);
$salt = base64_encode($salt);
$sql = "UPDATE `teams` SET `pass`=?, `salt`=? WHERE `id`=?";
$stmt = $db->prepare($sql);
$stmt->bind_param("ssi", $hash, $salt, $this->id);
$stmt->execute();
$stmt->close();
}
return true;
}
function newVerify()
{
global $db;
$salt = base64_encode(generateSalt());
$sql = "UPDATE `teams` SET `verification`=? WHERE `id`=?";
$stmt = $db->prepare($sql);
$stmt->bind_param("si", $salt, $this->id);
$stmt->execute();
$stmt->close();
return $salt;
}
function Mail($subject, $text)
{
sendMail($this->mail, $subject, $text);
}
function newPass($pass, $verify)
{
global $lang;
global $db;
$id = 0;
$sql = "SELECT `id` FROM `teams` WHERE `id`=? AND `verification`=?";
$stmt = $db->prepare($sql);
$stmt->bind_param("is", $this->id, $verify);
$stmt->bind_result($id);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
if($stmt->num_rows!=1 || $id !== $this->id)
throw new Exception($lang['team'][11]);
$stmt->close();
if(strlen($pass)<1)
throw new Exception($lang['register'][3]);
$salt = generateSalt();
$hash = hashPass($salt, $pass);
$salt = base64_encode($salt);
$sql = "UPDATE `teams` SET `pass`=?, `salt`=? WHERE `id`=?";
$stmt = $db->prepare($sql);
$stmt->bind_param("ssi", $hash, $salt, $this->id);
$stmt->execute();
$stmt->close();
return true;
}
}
?>