summaryrefslogtreecommitdiff
blob: 759b5c47e3303d6cb24c1f606fb4ade052198938 (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
41
42
43
44
45
46
47
48
49
<?php
function init_builds_download(&$S) {
	if (!isset($S['user'])) {
		return 'login';
	}
	if (!(isset($_REQUEST['build']) && strlen($_REQUEST['build']) == 6 && ctype_alnum($_REQUEST['build']))) {
		debug('builds_download', 'No build or badly formatted build requested');
		return '404';
	}
	$r=query('SELECT * FROM `builds` WHERE `id`="'.$_REQUEST['build'].'"');
	if ($r->rowCount() == 0) {
		debug('builds_download', 'build not found or not owned by user');
		return '404';
	}
	$build=new sql_build($r->fetch(PDO::FETCH_ASSOC));
	if ($build->visibility == 'private' && !owner_or_admin($build->owner)) {
		debug('builds_download', 'Permission denied');
		return '404';
	}
	$files=glob(COMPLETED.'/build-'.$build->id.'.*');
	if (count($files)) {
		if (count($files) > 1) {
			debug('builds_download', 'extraneous file(s) found - don\'t know which to give them');
			return '404';
		}
		$S['builds_download']['file']=$files[0];
		if (!is_readable($S['builds_download']['file'])) {
			debug('builds_download', 'found file, but no read perms');
			return '404';
		}
		$ext=substr($S['builds_download']['file'], strpos($S['builds_download']['file'], '.'));
	} else {
		debug('builds_download', 'image file not found');
		return '404';
	}
	$S['builds_download']['dl']=new sql_download($build->id, $S['user']->id, time());
	contenttype('application/octet-stream');
	header('Content-Length: '.filesize($S['builds_download']['file']));
	header('Content-Description: File Transfer');
	header('Content-Transfer-Encoding: binary');
	header('Content-Disposition: attachment; filename="'.(isset($build->name) && strlen($build->name)?str_replace('"', '\"', $build->name):'ingenue-'.$build->id).$ext);
}
function body_builds_download(&$S) {
	ob_end_flush(); // At least in 5.3, we get a 500 Internal Server Error without this
	readfile($S['builds_download']['file']);
	// Log the download to db after sending data so hopefully HEAD requests won't artificially inflate the # of dls
	$S['builds_download']['dl']->write();
}
?>