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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
<?php
/*
* Copyright 2009-2011 Florian "Bluewind" Pritz <bluewind@server-speed.net>
*
* Licensed under GPLv3
* (see COPYING for full license text)
*
*/
class File extends CI_Controller {
var $data = array();
var $var;
function __construct()
{
parent::__construct();
$this->load->library('migration');
if ( ! $this->migration->current()) {
show_error($this->migration->error_string());
}
$old_path = getenv("PATH");
putenv("PATH=$old_path:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin");
mb_internal_encoding('UTF-8');
$this->load->helper(array('form', 'filebin'));
$this->load->model('file_mod');
$this->load->model('muser');
$this->var->cli_client = false;
$this->file_mod->var->cli_client =& $this->var->cli_client;
$this->var->latest_client = false;
if (file_exists(FCPATH.'data/client/latest')) {
$this->var->latest_client = trim(file_get_contents(FCPATH.'data/client/latest'));
}
$this->var->cli_client = is_cli_client();
if ($this->var->cli_client) {
$this->var->view_dir = "file_plaintext";
} else {
$this->var->view_dir = "file";
}
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']) && $_SERVER['PHP_AUTH_USER'] && $_SERVER['PHP_AUTH_PW']) {
if (!$this->muser->login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) {
// TODO: better message
echo "login failed.\n";
exit;
}
}
$this->data['username'] = $this->muser->get_username();
$this->data['title'] = "FileBin";
}
function index()
{
// Try to guess what the user would like to do.
$id = $this->uri->segment(1);
if(isset($_FILES['file'])) {
$this->do_upload();
} elseif ($id != "file" && $this->file_mod->id_exists($id)) {
$this->file_mod->download();
} elseif ($id && $id != "file") {
$this->file_mod->non_existent();
} else {
$this->upload_form();
}
}
function client()
{
$this->data['title'] .= ' - Client';
if ($this->var->latest_client) {
$this->data['client_link'] = base_url().'data/client/fb-'.$this->var->latest_client.'.tar.gz';
} else {
$this->data['client_link'] = false;
}
$this->data['client_link_dir'] = base_url().'data/client/';
$this->data['client_link_deb'] = base_url().'data/client/deb/';
$this->data['client_link_slackware'] = base_url().'data/client/slackware/';
if (!$this->var->cli_client) {
$this->load->view($this->var->view_dir.'/header', $this->data);
}
$this->load->view($this->var->view_dir.'/client', $this->data);
if (!$this->var->cli_client) {
$this->load->view($this->var->view_dir.'/footer', $this->data);
}
}
function upload_form()
{
$this->data['title'] .= ' - Upload';
$this->data['small_upload_size'] = $this->config->item('small_upload_size');
$this->data['max_upload_size'] = $this->config->item('upload_max_size');
$this->data['upload_max_age'] = $this->config->item('upload_max_age')/60/60/24;
$this->data['contact_me_url'] = $this->config->item('contact_me_url');
$this->data['username'] = $this->muser->get_username();
$this->load->view($this->var->view_dir.'/header', $this->data);
$this->load->view($this->var->view_dir.'/upload_form', $this->data);
if ($this->var->cli_client) {
$this->client();
}
$this->load->view($this->var->view_dir.'/footer', $this->data);
}
// Allow CLI clients to query the server for the maxium filesize so they can
// stop the upload before wasting time and bandwith
function get_max_size()
{
echo $this->config->item('upload_max_size');
}
function upload_history()
{
$this->muser->require_access();
$user = $this->muser->get_userid();
$this->load->library("MemcacheLibrary");
if (! $cached = $this->memcachelibrary->get("history_".$this->var->view_dir."_".$user)) {
$query = array();
$lengths = array();
// key: database field name; value: display name
$fields = array(
"id" => "ID",
"filename" => "Filename",
"mimetype" => "Mimetype",
"date" => "Date",
"hash" => "Hash",
"filesize" => "Size"
);
$this->data['title'] .= ' - Upload history';
foreach($fields as $length_key => $value) {
$lengths[$length_key] = mb_strlen($value);
}
$query = $this->db->query("
SELECT ".implode(",", array_keys($fields))."
FROM files
WHERE user = ?
ORDER BY date
", array($user))->result_array();
foreach($query as $key => $item) {
$query[$key]["date"] = date("r", $item["date"]);
$query[$key]["filesize"] = format_bytes($item["filesize"]);
if ($this->var->cli_client) {
// Keep track of longest string to pad plaintext output correctly
foreach($fields as $length_key => $value) {
$len = mb_strlen($query[$key][$length_key]);
if ($len > $lengths[$length_key]) {
$lengths[$length_key] = $len;
}
}
}
}
$this->data["query"] = $query;
$this->data["lengths"] = $lengths;
$this->data["fields"] = $fields;
$cached = "";
$cached .= $this->load->view($this->var->view_dir.'/header', $this->data, true);
$cached .= $this->load->view($this->var->view_dir.'/upload_history', $this->data, true);
$cached .= $this->load->view($this->var->view_dir.'/footer', $this->data, true);
// disable for now. reenable if it causes too much load
//$this->memcachelibrary->set('history_'.$this->var->view_dir."_".$user, $cached, 42);
}
echo $cached;
}
// Allow users to delete their own IDs
function delete()
{
$this->muser->require_access();
$id = $this->uri->segment(3);
$this->data["id"] = $id;
$process = $this->input->post("process");
if ($this->var->cli_client) {
$process = true;
}
if ($id && !$this->file_mod->id_exists($id)) {
$this->output->set_status_header(404);
$this->data["msg"] = "Unknown ID.";
} elseif ($process) {
if ($this->file_mod->delete_id($id)) {
$this->load->view($this->var->view_dir.'/header', $this->data);
$this->load->view($this->var->view_dir.'/deleted', $this->data);
$this->load->view($this->var->view_dir.'/footer', $this->data);
return;
} else {
$this->data["msg"] = "Deletion failed. Do you really own that file?";
}
}
$this->data["filedata"] = $this->file_mod->get_filedata($id);
$this->data["can_delete"] = $this->data["filedata"]["user"] == $this->muser->get_userid();
$this->file_mod->display_info($id);
}
// Handle pastes
function do_paste()
{
$content = $this->input->post("content");
$filesize = strlen($content);
$filename = "stdin";
if(!$content) {
$this->output->set_status_header(400);
$this->data["msg"] = "Nothing was pasted, content is empty.";
$this->load->view($this->var->view_dir.'/header', $this->data);
$this->load->view($this->var->view_dir.'/upload_error', $this->data);
$this->load->view($this->var->view_dir.'/footer');
return;
}
if ($filesize > $this->config->item('upload_max_size')) {
$this->output->set_status_header(413);
$this->load->view($this->var->view_dir.'/header', $this->data);
$this->load->view($this->var->view_dir.'/too_big');
$this->load->view($this->var->view_dir.'/footer');
return;
}
$id = $this->file_mod->new_id();
$hash = md5($content);
$folder = $this->file_mod->folder($hash);
file_exists($folder) || mkdir ($folder);
$file = $this->file_mod->file($hash);
file_put_contents($file, $content);
chmod($file, 0600);
$this->file_mod->add_file($hash, $id, $filename);
$this->file_mod->show_url($id, false);
}
// Handles uploaded files
function do_upload()
{
$extension = $this->input->post('extension');
if(!isset($_FILES['file']) || $_FILES['file']['error'] !== 0) {
$this->output->set_status_header(400);
$errors = array(
0=>"There is no error, the file uploaded with success",
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder"
);
$this->data["msg"] = "Unknown error.";
if (isset($_FILES["file"])) {
$this->data["msg"] = $errors[$_FILES['file']['error']];
}
$this->load->view($this->var->view_dir.'/header', $this->data);
$this->load->view($this->var->view_dir.'/upload_error', $this->data);
$this->load->view($this->var->view_dir.'/footer');
return;
}
$filesize = filesize($_FILES['file']['tmp_name']);
if ($filesize > $this->config->item('upload_max_size')) {
$this->output->set_status_header(413);
$this->load->view($this->var->view_dir.'/header', $this->data);
$this->load->view($this->var->view_dir.'/too_big');
$this->load->view($this->var->view_dir.'/footer');
return;
}
$id = $this->file_mod->new_id();
$hash = md5_file($_FILES['file']['tmp_name']);
// work around a curl bug and allow the client to send the real filename base64 encoded
$filename = $this->input->post("filename");
if ($filename !== false) {
$filename = trim(base64_decode($filename, true), "\r\n\0\t\x0B");
}
// fall back if base64_decode failed
if ($filename === false) {
$filename = $_FILES['file']['name'];
}
$folder = $this->file_mod->folder($hash);
file_exists($folder) || mkdir ($folder);
$file = $this->file_mod->file($hash);
move_uploaded_file($_FILES['file']['tmp_name'], $file);
chmod($file, 0600);
$this->file_mod->add_file($hash, $id, $filename);
$this->file_mod->show_url($id, $extension);
}
function claim_id()
{
$this->muser->require_access();
$last_upload = $this->session->userdata("last_upload");
$id = $last_upload["id"];
$filedata = $this->file_mod->get_filedata($id);
if ($filedata["user"] != 0) {
show_error("Someone already owns '$id', can't reassign.");
}
$this->file_mod->adopt($id);
$this->session->unset_userdata("last_upload");
$this->file_mod->show_url($id, $last_upload["mode"]);
}
/* Functions below this comment can only be run via the CLI
* `php index.php file <function name>`
*/
// Removes old files
function cron()
{
if (!$this->input->is_cli_request()) return;
if ($this->config->item('upload_max_age') == 0) return;
$oldest_time = (time() - $this->config->item('upload_max_age'));
$oldest_session_time = (time() - $this->config->item("sess_expiration"));
$small_upload_size = $this->config->item('small_upload_size');
$query = $this->db->query('
SELECT hash, id, user
FROM files
WHERE date < ? OR (user = 0 AND date < ?)',
array($oldest_time, $oldest_session_time));
foreach($query->result_array() as $row) {
$file = $this->file_mod->file($row['hash']);
if (!file_exists($file)) {
$this->db->query('DELETE FROM files WHERE id = ? LIMIT 1', array($row['id']));
continue;
}
if ($row["user"] == 0 || filesize($file) > $small_upload_size) {
if (filemtime($file) < $oldest_time) {
unlink($file);
$this->db->query('DELETE FROM files WHERE hash = ?', array($row['hash']));
} else {
$this->db->query('DELETE FROM files WHERE id = ? LIMIT 1', array($row['id']));
}
}
}
}
/* remove files without database entries */
function clean_stale_files()
{
if (!$this->input->is_cli_request()) return;
$upload_path = $this->config->item("upload_path");
$outer_dh = opendir($upload_path);
while (($dir = readdir($outer_dh)) !== false) {
if (!is_dir($upload_path."/".$dir) || $dir == ".." || $dir == ".") {
continue;
}
$dh = opendir($upload_path."/".$dir);
$empty = true;
while (($file = readdir($dh)) !== false) {
if ($file == ".." || $file == ".") {
continue;
}
$query = $this->db->query("SELECT hash FROM files WHERE hash = ? LIMIT 1", array($file))->row_array();
if (empty($query)) {
unlink($upload_path."/".$dir."/".$file);
} else {
$empty = false;
}
}
closedir($dh);
if ($empty) {
rmdir($upload_path."/".$dir);
}
}
closedir($outer_dh);
}
function nuke_id()
{
if (!$this->input->is_cli_request()) return;
$id = $this->uri->segment(3);
$file_data = $this->file_mod->get_filedata($id);
if (empty($file_data)) {
echo "unknown id \"$id\"\n";
return;
}
$hash = $file_data["hash"];
$this->db->query("
DELETE FROM files
WHERE hash = ?
", array($hash));
unlink($this->file_mod->file($hash));
echo "removed hash \"$hash\"\n";
}
function update_file_sizes()
{
if (!$this->input->is_cli_request()) return;
$chunk = 500;
$total = $this->db->count_all("files");
for ($limit = 0; $limit < $total; $limit += $chunk) {
$query = $this->db->query("
SELECT hash
FROM files
GROUP BY hash
LIMIT $limit, $chunk
")->result_array();
foreach ($query as $key => $item) {
$hash = $item["hash"];
$filesize = intval(filesize($this->file_mod->file($hash)));
$this->db->query("
UPDATE files
SET filesize = ?
WHERE hash = ?
", array($filesize, $hash));
}
}
}
}
# vim: set noet:
|