summaryrefslogtreecommitdiff
blob: 04f80361bdb4a56158560eab98072d826fcb2639 (plain)
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
<?php
function init_upload() {
	global $S, $request;
	if (!(isset($request['build'], $request['key'], $_FILES['file']) && preg_match('/^[a-zA-Z0-9]{6}$/', $request['build']) && preg_match('/^[a-zA-Z0-9]{30}$/', $request['key']))) {
		debug('upload', 'missing or malformed input');
		return '404';
	}
	$r=$S['pdo']->query('SELECT * FROM `builds` WHERE `id`="'.$request['build'].'"');
	if ($r->rowCount() == 0) {
		debug('upload', 'build not found');
		return '404';
	}
	$build=new sql_build($r->fetch(PDO::FETCH_ASSOC));
	$opts=$build->get_opts();
	if (!(isset($opts['uploadkey']) && $opts['uploadkey'] == $request['key'])) {
		debug('upload', 'invalid upload key');
		return '404';
	}
	debug('upload', 'error code: '.$_FILES['file']['error']);
	if ($_FILES['file']['error'] != UPLOAD_ERR_OK) {
		return '404';
	}
	debug('upload', 'Got uploaded file '.$_FILES['file']['name'].' at '.$_FILES['file']['tmp_name']);
	$name=basename($_FILES['file']['name']);
	$ext=substr($name, strpos($name, '.'));
	debug('upload', $_FILES['file']['tmp_name'].' -> '.COMPLETED."/build-$build->id$ext");
	if (!is_writable(COMPLETED)) {
		debug('upload', 'Web server needs write permissions for '.COMPLETED);
		return '404';
	}
	if (!move_uploaded_file($_FILES['file']['tmp_name'], COMPLETED."/build-$build->id$ext")) {
		debug('Move file failed');
		return '404';
	}
	return array('title' => 'Upload Successful');
}
function body_upload() {
	echo print_success('Upload successful');
}
?>