download   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 108
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setFile() 0 11 3
A getMimeType() 0 7 2
A getFileSize() 0 4 1
B sendDownload() 0 30 3
1
<?php
2
3
namespace florinp\messenger\libs;
4
5
use InvalidArgumentException;
6
class download {
7
8
	/**
9
	 * File directory
10
	 * @var string
11
	 */
12
	protected $directory;
13
14
	/**
15
	 * File name
16
	 * @var string
17
	 */
18
	protected $filename;
19
20
	/**
21
	 * File pointer
22
	 * @var resource
23
	 */
24
	protected $file;
25
26
	/**
27
	 * File full path
28
	 * @var string
29
	 */
30
	protected $full_path;
31
32
	/**
33
	 * @param string $phpbb_root_path phpBB root path
34
	 */
35
	public function __construct($phpbb_root_path) {
36
		$this->directory = $phpbb_root_path.'store/messenger/files';
37
	}
38
39
40
	/**
41
	 * Set the filename
42
	 * @param string $filename File name
43
	 * @throws InvalidArgumentException when the file not exist or is not readable
44
	 */
45
	public function setFile($filename) {
46
		$file_full_path = $this->directory.'/'.$filename;
47
		if (!is_file($file_full_path)) {
48
			throw new InvalidArgumentException("File does not exist");
49
		} else if (!is_readable($file_full_path)) {
50
			throw new InvalidArgumentException("File to download is not readable.");
51
		}
52
		$this->filename = $filename;
53
		$this->file = fopen($file_full_path, 'rb');
54
		$this->full_path = $file_full_path;
55
	}
56
57
	/**
58
	 * Get file mime type
59
	 * @return string
60
	 */
61
	private function getMimeType() {
62
		$fileExtension = pathinfo($this->filename, PATHINFO_EXTENSION);
63
		$mimeTypeHelper = Mimetypes::getInstance();
64
		$mimeType = $mimeTypeHelper->fromExtension($fileExtension);
65
66
		return !is_null($mimeType) ? $mimeType : 'application/force-download';
67
	}
68
69
	/**
70
	 * Get file size
71
	 * @return int
72
	 */
73
	private function getFileSize() {
74
		$stat = fstat($this->file);
75
		return $stat['size'];
76
	}
77
78
	/**
79
	 * Sends the download to browser
80
	 * @param bool|true $forceDownload
81
	 */
82
	public function sendDownload($forceDownload = true) {
83
		if (headers_sent()) {
84
			throw new \RuntimeException("Cannot send file to the browser, since the headers were already sent");
85
		}
86
		$mimeType = $this->getMimeType();
0 ignored issues
show
Unused Code introduced by
$mimeType is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
87
		$fileSize = $this->getFileSize();
88
89
		ini_set('display_errors', 1);
90
		error_reporting(E_ALL);
91
92
		header("Pragma: public");
93
		header("Expires: 0");
94
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
95
		header("Cache-Control: private", false);
96
		header("Content-Type: application/octet-stream");
97
98
		if ($forceDownload) {
99
			header("Content-Disposition: attachment; filename=\"{$this->filename}\";");
100
		} else {
101
			header("Content-Disposition: filename=\"{$this->filename}\";");
102
		}
103
104
		header("Content-Transfer-Encoding: binary");
105
		header("Content-Length: {$fileSize}");
106
107
		@ob_clean();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
108
109
		rewind($this->file);
110
		fpassthru($this->file);
111
	}
112
113
}