-
Notifications
You must be signed in to change notification settings - Fork 12
/
addkey.php
201 lines (155 loc) · 4.7 KB
/
addkey.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
<?php
include("config.php");
include("functions.php");
include("session.php");
include("userclass.php");
if(!isset($_SESSION['temphash'])) {
$_SESSION['temphash'] = hash("sha256", genrand());
}
$loggedin = $session->isLoggedIn();
$hash = $_SESSION['temphash'];
if($loggedin) {
//$user = getUserInfo($_SESSION['user']);
$user = new User();
$user->login($session->user);
$title = "device manager - synccit";
} else {
header("Location: index.php");
exit;
}
if(isset($_REQUEST['do']) && isset($_REQUEST['code']) && $_REQUEST['do'] == "remove") {
$code = $_REQUEST['code'];
if(strcmp($hash, $_GET['hash']) == 0) {
$sql = "DELETE FROM `authcodes`
WHERE
`userid` = '".$mysql->real_escape_string($user->id)."'
AND
`username` = '".$mysql->real_escape_string($user->username)."'
AND
`authhash` = '".$mysql->real_escape_string($code)."'
LIMIT 1
;";
if($res = $mysql->query($sql)) {
$error = "device key removed";
} else {
//$error = "unable to remove key";
}
}
$_SESSION['temphash'] = hash("sha256", genrand());
$hash = $_SESSION['temphash'];
}
if(isset($_POST['submit']) and strcmp($hash, $_POST['hash']) == 0) {
if(isset($_POST['device']) ) {
$key = genrand();
$sql = "INSERT INTO `authcodes` (
`id`,
`userid`,
`username`,
`authhash`,
`description`,
`created`
) VALUES (
NULL,
'".$mysql->real_escape_string($user->id)."',
'".$mysql->real_escape_string($user->username)."',
'".$key."',
'".$mysql->real_escape_string($_POST['device'])."',
'".time()."'
)";
if($res = $mysql->query($sql)) {
$error = "device key added";
} else {
$error = "database error";
}
}
$_SESSION['temphash'] = hash("sha256", genrand());
$hash = $_SESSION['temphash'];
}
$sql = "SELECT * FROM `authcodes` WHERE `userid` = '".$mysql->real_escape_string($user->id)."' ORDER BY `created` DESC";
$res = $mysql->query($sql);
// this could be a separate class, but I'm pretty sure this is the only time it'll be used
$codes = array();
$i=0;
while($row = $res->fetch_assoc()) {
$codes[$i++] = array(
"id" => $row['id'],
"description" => $row['description'],
"code" => $row['authhash']
);
}
htmlHeader($title, $loggedin);
?>
<div class="threecol">
<h2>device manager</h2>
<p class="deviceinfo">
<br />
<span class="bold">
username
</span>
<br />
<span class="mono">
<?php echo htmlspecialchars($user->username); ?>
</span>
<br />
</p>
<p class="deviceinfo">
<br />
<span class="bold">
API location
</span>
<br />
<span class="monosmall">
<?php echo $apiloc; ?>
</span>
<br />
</p>
</div>
<div class="sixcol">
<div class="devicetable">
<table>
<thead>
<tr>
<td>device name</td>
<td>auth code</td>
<td>rm</td>
</tr>
</thead>
<tbody>
<?php
for($i=0;$i<count($codes);$i++) {
$url = str_replace("@k", $codes[$i]['code'], DEVICESRMURL);
$url = str_replace("@h", $hash, $url);
echo "<tr>
<td>";
echo strip_tags($codes[$i]['description']);
echo "</td> ";
echo "<td class=\"authcode\">";
echo $codes[$i]['code'];
echo "</td>";
echo "<td class=\"delete\">";
echo "<a href=\"$url\"
title=\"remove device key\"
onClick=\"return confirm('Are you sure you want to delete the key? Anything using this key will stop working')\">";
echo "[x]</a></td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</div>
<div class="threecol last">
<div class="adddevice">
<br />
<span class="adddevicetitle">add device</span>
<span class="error"><?php echo $error; ?></span>
<form action="<?php echo DEVICESURL; ?>" method="post">
<input type="hidden" name="hash" value="<?php echo $hash; ?>" />
<input type="text" id="device" name="device" value="" class="text" placeholder="device name" />
<br />
<input type="submit" name="submit" value="add device"/>
</form>
</div>
</div>
<?php
htmlFooter();