aboutsummaryrefslogtreecommitdiffstats
path: root/mod/attach.php
blob: d0d3296e1c2518bdbc8032c81446a4ae912817fd (plain) (blame)
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
<?php

require_once('include/security.php');
require_once('include/attach.php');

function attach_init(&$a) {

	if(argc() < 2) {
		notice( t('Item not available.') . EOL);
		return;
	}

	$r = attach_by_hash(argv(1),((argc() > 2) ? intval(argv(2)) : 0));

	if(! $r['success']) {
		notice( $r['message'] . EOL);
		return;
	}

	$c = q("select channel_address from channel where channel_id = %d limit 1",
		intval($r['data']['uid'])
	);

	if(! $c)
		return;

	header('Content-type: ' . $r['data']['filetype']);
	header('Content-disposition: attachment; filename="' . $r['data']['filename'] . '"');
	if($r['data']['flags'] & ATTACH_FLAG_OS ) {
		$istream = fopen('store/' . $c[0]['channel_address'] . '/' . $r['data']['data'],'rb');
		$ostream = fopen('php://output','wb');
		if($istream && $ostream) {
			pipe_streams($istream,$ostream);
			fclose($istream);
			fclose($ostream);
		}
	}
	else
		echo $r['data']['data'];
	killme();

}