/
admin.php
executable file
·200 lines (199 loc) · 6.83 KB
/
admin.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
<?php
require_once('config.php');
require_once('functions.php');
session_start();
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Downloads</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<h1>Downloads</h1>
<a href="admin.php">Index</a>
|
<a href="admin.php?p=log">Log</a>
<?php
if(isset($_SESSION['message']) && $_SESSION['message']) {
print '<p id="message">'.$_SESSION['message'].'</p>';
unset($_SESSION['message']);
}
if(!$_SESSION['ADMIN']) {
if(isset($_POST['username']) and isset($_POST['password'])) {
if($config['admin_name'] == $_POST['username'] and $config['admin_pass'] == $_POST['password']) {
$_SESSION['ADMIN'] = true;
} else {
$_SESSION['message'] = "Incorrect login information.";
}
header("Location: admin.php");
exit;
}
?>
<p>You need to log in first.</p>
<form action="admin.php" method="POST">
<label for="username">Username</label>
<input name="username" id="username"><br>
<label for="password">Password</label>
<input name="password" id="password" type="password"><br>
<input type="submit" name="submit" id="submit" value="Log in">
</form>
<?php
} else {
$page = isset($_GET['p']) ? $_GET['p'] : '';
switch($page) {
case 'file':
$id = $_GET['id'];
if(isset($_POST['submit'])) {
// save!
if(!file_exists($config['file_base'].'/'.$_POST['file'])) {
$_SESSION['message'] = "That file doesn't exist.";
header("Location: admin.php?p=file&id=".$id);
exit;
} else {
if($id=='new') {
$id = query("INSERT INTO files (file, active) VALUES (%s, %d)", array($_POST['file'], $_POST['active'] == 1 ? 1 : 0), QUERY_ID);
} else {
query("UPDATE files SET file=%s, active=%d WHERE id = %d", array($_POST['file'], $_POST['active'] == 1 ? 1 : 0, $id), QUERY_NONE);
}
}
header("Location: admin.php");
exit;
}
if($id == 'new') {
$file = array('file'=>'', 'active'=>1);
} else {
$file = query("SELECT * FROM files WHERE id = %d", $id, QUERY_INIT);
}
?>
<form method="POST">
<label for="file">File</label>
<input name="file" id="file"<?if($file['file']) {?> value="<?=$file['file']?>"<?}?>><br>
<label for="active">Active?</label>
<input name="active" id="active" type="checkbox" value="1"<?if($file['active']){?> checked="checked"<?}?>><br>
<input name="submit" id="submit" type="submit" value="save">
</form>
<?php
break;
case 'csv':
ob_end_clean();
// set headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"tokens.csv\"");
header("Content-Transfer-Encoding: binary");
print get_tokens_as_csv($_SESSION['tokens']);
exit;
break;
case 'tokenlist':
$file = $_GET['file'];
if(isset($_POST['submit'])) {
if(is_numeric($_POST['howmany'])) {
$_SESSION['tokens'] = create_tokens($file, $_POST['howmany'], $_POST['uses'], $_POST['expires']);
$_SESSION['message'] = "Created ".count($_SESSION['tokens'])." tokens. <a href=\"admin.php?p=csv\">Download as CSV</a>?";
} else {
$_SESSION['message'] = "'How many?' should be a number";
}
header("Location: admin.php?p=tokenlist&file=".$file);
exit;
}
$tokens = query("SELECT * FROM tokens WHERE file = %d ORDER BY created", $file);
?>
<h2>Generate new tokens</h2>
<form method="POST">
<label for="howmany">How many?</label><input id="howmany" name="howmany"><br>
<label for="uses">Uses</label><input id="uses" name="uses" value="1"><br>
<label for="expires">Expires</label><input id="expires" name="expires" value="2029-12-31 00:00:00"><br>
<input name="submit" id="submit" type="submit" value="Generate!">
</form>
<h2>Existing tokens</h2>
<table>
<tr><th>token</th><th>uses remaining</th><th>initial uses</th><th>created</th><th>expires</th></tr>
<?php
foreach($tokens as $t) {
?>
<tr>
<td><a href="admin.php?p=token&token=<?=$t['token']?>"><?=$t['token']?></a></td>
<td><?=$t['uses_remaining']?></td>
<td><?=$t['initial_uses']?></td>
<td><?=$t['created']?></td>
<td><?=$t['expires']?></td>
</tr>
<?php
}
break;
case 'token':
if(isset($_POST['submit'])) {
$token = $_GET['token'];
query("UPDATE tokens SET uses_remaining = %d, expires = %s WHERE token = %s", array($_POST['uses_remaining'], $_POST['expires'], $token), QUERY_NONE);
header("Location: admin.php?p=token&token=".$token);
exit;
}
$token = query("SELECT * FROM tokens WHERE token = %s", $_GET['token'], QUERY_INIT);
?>
<form method="POST">
Token: <?=$token['token']?><br>
Initial uses: <?=$token['initial_uses']?><br>
<label for="uses_remaining">Uses remaining</label><input name="uses_remaining" id="uses_remaining" value="<?=$token['uses_remaining']?>"><br>
Created: <?=$token['created']?><br>
<label for="expires">Expires</label><input name="expires" id="expires" value="<?=$token['expires']?>"><br>
<input name="submit" id="submit" type="submit" value="Save">
</form>
<?php
break;
case 'log':
$entries = query("SELECT log.*, files.file FROM log LEFT JOIN files ON (files.id = log.fileid) ORDER BY id ASC");
?>
<h2>Log</h2>
<table>
<tr><th>file</th><th>token</th><th>time</th><th>ip address</th></tr>
<?php
foreach($entries as $entry) {
?>
<tr>
<td><a href="admin.php?p=file&id=<?=$entry['fileid']?>"><?=$entry['file']?></a></td>
<td><?=$entry['token']?></td>
<td><?=$entry['time_used']?></td>
<td><?=long2ip($entry['ip_address'])?></td>
</tr>
<?php
}
?>
</table>
<?php
break;
case 'index':
default:
$files = query("SELECT * FROM files ORDER BY file");
?>
<h2>All files</h2>
<table>
<tr><th>file</th><th>size</th><th>type</th><th>active</th><th>tokens</th><th>downloads so far</th><th>downloads unclaimed</th></tr>
<?php
foreach($files as $f) {
$tokens = query("SELECT COUNT(*) as count, SUM(uses_remaining) as remaining FROM tokens WHERE file = %d", $f['id'], QUERY_INIT);
$downloads = query("SELECT COUNT(*) FROM log l LEFT JOIN tokens t ON (l.token = t.token) LEFT JOIN files f ON (t.file = f.id) WHERE f.id = %d", $f['id'], QUERY_SINGLEVALUE);
?>
<tr>
<td><a href="admin.php?p=file&id=<?=$f['id']?>"><?=$f['file']?></a></td>
<td><?=filesize($config['file_base'].'/'.$f['file'])?></td>
<td><?=get_mime_type($config['file_base'].'/'.$f['file'])?></td>
<td><?=($f['active'] ? 'yes' : 'no')?></td>
<td><a href="admin.php?p=tokenlist&file=<?=$f['id']?>"><?=$tokens['count']?></a></td>
<td><?=$downloads?></td>
<td><?=$tokens['remaining']?></td>
</tr>
<?php } ?>
</table>
<a href="admin.php?p=file&id=new">New</a>
<?php
break;
}
}
?>
</body>
</html>