-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.php
236 lines (201 loc) · 5.52 KB
/
common.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
<?php
session_start();
require_once("config.php");
require_once("trueskill.php");
require_once("libui.php");
require_once("ui.php");
require_once("warlightapi.php");
require_once("widgets.php");
require_once("/usr/lib/phpdatabase/database.php");
require_once("/usr/lib/phpmail/mimemail.php");
$database = new MysqlConnection();
$database->open($config["dbHost"], $config["dbUser"], $config["dbPass"], $config["dbName"]);
if (isset($_SESSION["userID"])) {
if (!db()->stdExists("users", array("userID"=>$_SESSION["userID"]))) {
session_destroy();
}
}
function db()
{
return $GLOBALS["database"];
}
function get($key)
{
if (isset($_GET[$key])) {
return $_GET[$key];
}
return null;
}
function post($key)
{
if (isset($_POST[$key])) {
return $_POST[$key];
}
return null;
}
function pageNumber()
{
$page = get("page");
if ($page !== null && ctype_digit($page)) {
return $page;
} else {
return 1;
}
}
function redirect($url)
{
header("location: $url");
die();
}
function isLoggedIn()
{
return isset($_SESSION["userID"]);
}
function currentUserID()
{
if(!isLoggedIn()) {
return null;
}
return $_SESSION["userID"];
}
function requireLogin($target = null)
{
if(!isLoggedIn()) {
if ($target === null) {
redirect("index.php");
} else {
redirect($GLOBALS["config"]["loginUrl"] . "&state=" . urlencode($target));
}
}
}
function requireAuthentication($target)
{
if (!isset($_SESSION["token"])) {
if ($target === null) {
redirect($GLOBALS["config"]["loginUrl"]);
} else {
redirect($GLOBALS["config"]["loginUrl"] . "&state=" . urlencode($target));
}
}
}
function startsWith($string, $prefix)
{
return substr($string, 0, strlen($prefix)) === $prefix;
}
function error404()
{
header("HTTP/1.1 404 Not Found");
die("The requested page could not be found.");
}
function checkLadder($ladderID)
{
if (!db()->stdExists("ladders", array("ladderID"=>$ladderID))) {
error404();
}
}
function checkLadderPlayer($ladderID, $userID)
{
checkLadder($ladderID);
if(!db()->stdExists("ladderPlayers", array("userID"=>$userID, "ladderID"=>$ladderID))) {
error404();
}
}
function checkLadderMod($ladderID)
{
checkLadder($ladderID);
if (!isMod($ladderID)) error404();
}
function isMod($ladderID, $userID = null) {
if($userID === null) {
$userID = currentUserID();
}
if($userID === null) {
return false;
}
return db()->stdExists("ladderAdmins", array("ladderID"=>$ladderID, "userID"=>$userID));
}
function initPlayerRank($ladderID, $userID, $rating = null)
{
db()->startTransaction();
if($rating === null) {
$rating = db()->stdGet("ladderPlayers", array("userID"=>$userID, "ladderID"=>$ladderID), "rating");
}
$ladderIDSql = db()->addSlashes($ladderID);
$rankRecord = db()->query("
SELECT MAX(rank) as newRank
FROM ladderPlayers
WHERE ladderID='$ladderIDSql'
AND rating > {$rating}
")->fetchArray();
$rank = $rankRecord["newRank"];
if($rank === null) {
$rank = 0;
}
db()->setQuery("UPDATE ladderPlayers SET rank = rank + 1 WHERE ladderID = '$ladderID' AND rank > $rank");
db()->stdSet("ladderPlayers", array("userID"=>$userID, "ladderID"=>$ladderID), array("rank"=>$rank + 1));
db()->commitTransaction();
}
function removePlayerRank($ladderID, $userID)
{
db()->startTransaction();
$rank = db()->stdGet("ladderPlayers", array("userID"=>$userID, "ladderID"=>$ladderID), "rank");
$ladderIDSql = db()->addSlashes($ladderID);
db()->setQuery("UPDATE ladderPlayers SET rank = rank - 1 WHERE ladderID = '$ladderID' AND rank > $rank");
db()->stdSet("ladderPlayers", array("userID"=>$userID, "ladderID"=>$ladderID), array("rank"=>0));
db()->commitTransaction();
}
function sendConfirmationEmail($userID)
{
$userData = db()->stdGet("users", array("userID"=>$userID), array("name", "email", "emailConfirmation"));
$name = $userData["name"];
$email = $userData["email"];
$confirmation = $userData["emailConfirmation"];
if ($email === null || $confirmation === null) {
return;
}
$nameHtml = htmlentities($name);
$emailHtml = htmlentities($email);
$link = "{$GLOBALS["config"]["baseUrl"]}confirmemail.php?player=$userID&confirmation=$confirmation";
$linkHtml = htmlentities($link);
$mail = new mimemail();
$mail->addReceiver($email, $name);
$mail->setSender($GLOBALS["config"]["email"], "warladder.net");
$mail->setSubject("Confirm your address for warladder.net");
$mail->setHtmlMessage(<<<MESSAGE
<p>Hi $nameHtml,</p>
<p>Somebody (and we hope it was you) configured your email address <$emailHtml> for the warladder.net registration of $nameHtml. Please click <a href="$linkHtml">this link</a> to confirm that you want this.</p>
<p>If you do not want to do this, simply ignore this message.</p>
MESSAGE
,
<<<MESSAGE
Hi $name,
Somebody (and we hope it was you) configured your email address <$email> for the warladder.net registration of $name. Please visit $link to confirm that you want this.
If you do not want to do this, simply ignore this message.
MESSAGE
);
$mail->send();
}
function initUserEmail($userID, $email)
{
if ($email === "") {
$email = null;
}
if ($email === null) {
$confirmation = null;
} else {
$characters = "0123456789qwertyuioplkjhgfdsazxcvbnm";
while (true) {
$confirmation = "";
for ($i = 0; $i < 50; $i++) {
$confirmation .= $characters[rand(0, strlen($characters) - 1)];
}
if (!db()->stdExists("users", array("userID"=>$userID, "emailConfirmation"=>$confirmation))) {
break;
}
}
}
db()->stdSet("users", array("userID"=>$userID), array("email"=>$email, "emailConfirmation"=>$confirmation));
if ($confirmation !== null) {
sendConfirmationEmail($userID);
}
}