summaryrefslogtreecommitdiff
path: root/forum/utils/file.php
blob: b1f4e504418a3c7e78d1cb81241334122ed78635 (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
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
<?php
 
include_once("config.php");
include_once($UTIL_DIR . "/mimetypes.php");
include_once($UTIL_DIR . "/files.php");
include_once($UTIL_DIR . "/imagecache.php");

function getFile($fid)
{
	global $DATA_DIR, $PERMSTORE, $MIME_TYPES;
	$files = new Files($DATA_DIR . "/files.xml");
	$file = $files->getFile($fid);
	
	$filename = $PERMSTORE . "/" . $file->fid;
	
	$download = false;
	foreach($MIME_TYPES as $m) {
		if($m->name == $file->mimetype) $download = !$m->show;
	}
	
	//header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header('Content-Description: File Transfer');
	header('Content-Type: ' . $file->mimetype);
	header('Content-Length: ' . filesize($filename));
	
	if($download) header('Content-Disposition: attachment; filename=' . basename($file->name));
	else header('Content-Disposition: inline; filename=' . basename($file->name));
	
	readfile($filename); 
}

function getFilePreview($fid)
{
	global $DATA_DIR, $UTIL_DIR, $PERMSTORE, $MIME_TYPES;
	$files = new Files($DATA_DIR . "/files.xml");
	$file = $files->getFile($fid);
	
	$filename = $PERMSTORE . "/" . $file->fid;
	
	if(strstr($file->mimetype, "image/")) {
		header('Content-Description: File Transfer');
		header('Content-Type: ' . $file->mimetype);

		switch($file->mimetype) {
		case "image/png":
			$image = imagecreatefrompng($filename);
			$image = rescale($image);
			imagepng($image);
			break;
		case "image/jpeg":
			$image = imagecreatefromjpeg($filename);
			$image = rescale($image);
			imagejpeg($image);
			echo "@";
			break;
		case "image/gif":
			$image = imagecreatefromgif($filename);
			$image = rescale($image);
			imagegif($image);
			break;
		}

		imagedestroy($image);
	} else {
		header('Content-Description: File Transfer');
		header("Content-type: image/png");

		$im = @imagecreate(8 + strlen($file->name) * 5, 20)
			or die("Cannot Initialize new GD image stream");
		$background_color = imagecolorallocate($im, 0, 0, 0);
		$text_color = imagecolorallocate($im, 233, 14, 91);
		imagestring($im, 1, 5, 5,  $file->name, $text_color);
		imagepng($im);

		imagedestroy($im);
	}
}

?>