-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopendb.php
207 lines (168 loc) · 6.53 KB
/
opendb.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
<?php
if ($_SERVER["REQUEST_URI"] == "/opendb.php") {
header("Location: nope.php");
die();
}
require("config.php");
$connection = new mysqli($mysql_host, $mysql_user, $mysql_pass, $mysql_data) or die("Could not connect!");
register_shutdown_function("exiting");
function exiting() {
record();
mysqli_close($connection);
}
$LoginStatusOK = 1 << 0; //Login is successful
$LoginStatusUserPass = 1 << 1; //Incorrect username/password
$LoginStatusBadCreds = 1 << 2; //Username/password missing
$LoginStatusAccess0 = 1 << 3; //Normal access
$LoginStatusAccess1 = 1 << 4; //Editor access
$LoginStatusAccess2 = 1 << 5; //Homepage editor access
$LoginStatusAccess3 = 1 << 6; //Administrator access
$LoginStatusCanEdit = 1 << 7; //Can edit pages
$LoginStatusCanHome = 1 << 8; //Can edit home page
$LoginStatusSeeMore = 1 << 9; //Allows for viewing of last IP + more
function loginStatus($username = NULL, $passhash = NULL) {
global $connection, $LoginStatusOK, $LoginStatusUserPass, $LoginStatusBadCreds, $LoginStatusAccess0, $LoginStatusAccess1, $LoginStatusAccess2, $LoginStatusAccess3, $LoginStatusCanEdit, $LoginStatusCanHome, $LoginStatusSeeMore;
$ret = 0;
if ($username == NULL || $passhash == NULL) {
if (!isset($_COOKIE["username"]) || !isset($_COOKIE["session"]))
$ret = $ret | $LoginStatusBadCreds;
else {
$username = $_COOKIE["username"];
$passhash = $_COOKIE["session"];
}
}
//Escaping required
$username = strToLower($connection->escape_string($username));
$password = $connection->escape_string($passhash);
$query = "SELECT `passhash`, `access` FROM `users` WHERE `username` = '$username'";
$access = 0;
$serverPass = "";
if ($statement = $connection->prepare($query)) {
$statement->execute();
$statement->bind_result($serverPass, $access);
$result = $connection->query($query);
if ($statement->fetch() === false)
$ret = $ret | $LoginStatusUserPass;
$statement->close();
}
$access = intVal($access);
if ($passhash !== $serverPass)
$ret = $ret | $LoginStatusUserPass;
else
$ret = $ret | $LoginStatusOK;
switch ($access) {
case 0:
$ret = $ret | $LoginStatusAccess0;
break;
case 1:
$ret = $ret | $LoginStatusAccess0;
$ret = $ret | $LoginStatusAccess1;
$ret = $ret | $LoginStatusCanEdit;
break;
case 2:
$ret = $ret | $LoginStatusAccess0;
$ret = $ret | $LoginStatusAccess1;
$ret = $ret | $LoginStatusAccess2;
$ret = $ret | $LoginStatusCanEdit;
$ret = $ret | $LoginStatusCanHome;
break;
case 3:
$ret = $ret | $LoginStatusAccess0;
$ret = $ret | $LoginStatusAccess1;
$ret = $ret | $LoginStatusAccess2;
$ret = $ret | $LoginStatusAccess3;
$ret = $ret | $LoginStatusCanEdit;
$ret = $ret | $LoginStatusCanHome;
$ret = $ret | $LoginStatusSeeMore;
break;
}
if ($ret & $LoginStatusOK) {
$query = "UPDATE `users` SET `lastlogin` = CURRENT_TIMESTAMP WHERE `username` = '$username'";
$connection->query($query);
}
return $ret;
}
$globalLogin = loginStatus();
function getElement($num) {
global $globalLogin, $connection;
$pagename = strToLower($connection->escape_string(basename($_SERVER["PHP_SELF"], ".php")));
$num = intVal($num);
$query = "SELECT `value`, `type`, `color`" . ($globalLogin & $LoginStatusSeeMore ? ", `lastUpdate`, `lastUser`, `lastIP` " : " ") . "FROM `$pagename-index` WHERE `field` = $num;";
$value = null;
$type = null;
$color = null;
$lastUpdate = null;
$lastUser = null;
$lastIP = null;
if ($statement = $connection->prepare($query)) {
$statement->execute();
if ($globalLogin & $LoginStatusSeeMore)
$statement->bind_result($value, $type, $color, $lastUpdate, $lastUser, $lastIP);
else
$statement->bind_result($value, $type, $color);
$result = $connection->query($query);
if ($statement->fetch() === false)
return array("text" => "", "type" => "text", "color" => "000000");
$statement->close();
} else
return array("text" => "", "type" => "text", "color" => "000000");
if ($globalLogin & $LoginStatusSeeMore)
return array("text" => $value, "type" => $type, "color" => $color, "lastUpdate" => $lastUpdate, "lastUser" => $lastUser, "lastIP" => $lastIP);
else
return array("text" => $value, "type" => $type, "color" => $color);
}
function getAllElements($page = NULL) {
if ($page == NULL)
$page = $_SERVER["PHP_SELF"];
global $globalLogin, $connection, $LoginStatusSeeMore;
$pagename = strToLower($connection->escape_string(basename($page, ".php")));
$query = "SELECT `field`, `value`, `type`, `color`" . ($globalLogin & $LoginStatusSeeMore ? ", `lastUpdate`, `lastUser`, `lastIP` " : " ") . "FROM `$pagename-index`";
$value = null;
$number = null;
$type = null;
$color = null;
$lastUpdate = null;
$lastUser = null;
$lastIP = null;
$return = array();
if ($statement = $connection->prepare($query)) {
$statement->execute();
if ($globalLogin & $LoginStatusSeeMore)
$statement->bind_result($number, $value, $type, $color, $lastUpdate, $lastUser, $lastIP);
else
$statement->bind_result($number, $value, $type, $color);
$returns = 0;
while ($statement->fetch()) {
if ($globalLogin & $LoginStatusSeeMore)
$return[$returns ++] = array("text" => $value, "type" => $type, "color" => $color, "number" => $number, "lastUpdate" => $lastUpdate, "lastUser" => $lastUser, "lastIP" => $lastIP);
else
$return[$returns ++] = array("text" => $value, "number" => $number, "type" => $type, "color" => $color);
}
$statement->close();
} else
return null;
return $return;
}
function tag($num) {
$element = getElement($num);
return html_entity_decode($element["text"]);
}
function record() {
global $connection;
$ip = $_SERVER["REMOTE_ADDR"];
$query = "SELECT `hits` FROM `hits` WHERE `ip` = '$ip'";
$hits = 0;
if ($statement = $connection->prepare($query)) {
$statement->execute();
$statement->bind_result($hits);
if (!($statement->fetch())) {
$query = "INSERT INTO `hits` (`hits`, `ip`, `firstHit`) VALUES (0, '$ip', CURRENT_TIMESTAMP)";
$connection->query($query);
}
$statement->close();
}
$hits = intVal($hits) + 1;
$query = "UPDATE `hits` SET `hits` = $hits, `lastHit` = CURRENT_TIMESTAMP WHERE `ip` = '$ip' LIMIT 1";
$connection->query($query);
}
?>