Passed
Push — develop ( 1d94a2...f31b27 )
by Jens
02:50
created

FilesRouting::downloadFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 20
rs 9.4285
1
<?php
2
/**
3
 * User: jensk
4
 * Date: 30-1-2017
5
 * Time: 12:46
6
 */
7
8
namespace library\components\cms;
9
10
use library\cc\Request;
11
use library\components\CmsComponent;
12
13
class FilesRouting
14
{
15
	/**
16
	 * FilesRouting constructor.
17
	 *
18
	 * @param Request $request
19
	 * @param $relativeCmsUri
20
	 * @param CmsComponent $cmsComponent
21
	 */
22
	public function __construct($request, $relativeCmsUri, $cmsComponent)
23
	{
24
		if ($relativeCmsUri == '/files') {
25
			$cmsComponent->subTemplate = 'cms/files';
26
			$cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_FILES);
27
			$cmsComponent->setParameter(CmsComponent::PARAMETER_FILES, $cmsComponent->storage->getFiles());
28 View Code Duplication
		} elseif ($relativeCmsUri == '/files/new') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
			$cmsComponent->subTemplate = 'cms/files/form';
30
			$cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_FILES);
31
			if (isset($_FILES[CmsComponent::FILES_PARAMETER_FILE])) {
32
				$cmsComponent->storage->addFile($_FILES[CmsComponent::FILES_PARAMETER_FILE]);
33
				header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsComponent::PARAMETER_CMS_PREFIX) . '/files');
34
				exit;
35
			}
36
		} elseif ($relativeCmsUri == '/files/get' && isset($request::$get[CmsComponent::FILES_PARAMETER_FILE])) {
37
			$this->downloadFile($request::$get[CmsComponent::FILES_PARAMETER_FILE], $cmsComponent);
38
		} elseif ($relativeCmsUri == '/files/delete' && isset($request::$get[CmsComponent::FILES_PARAMETER_FILE])) {
39
			$cmsComponent->storage->deleteFileByName($request::$get[CmsComponent::FILES_PARAMETER_FILE]);
40
			header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsComponent::PARAMETER_CMS_PREFIX) . '/files');
41
			exit;
42
		}
43
	}
44
45
	/**
46
	 * @param $slug
47
	 * @param $cmsComponent
48
	 */
49
	private function downloadFile($slug, $cmsComponent)
50
	{
51
		$file = $cmsComponent->storage->getFileByName($slug);
52
		$path = realpath(__DIR__ . '/../../../www/files/');
53
		$quoted = sprintf('"%s"', addcslashes(basename($path . '/' . $file->file), '"\\'));
54
		$size = filesize($path . '/' . $file->file);
55
56
		header('Content-Description: File Transfer');
57
		header('Content-Type: ' . $file->type);
58
		header('Content-Disposition: attachment; filename=' . $quoted);
59
		header('Content-Transfer-Encoding: binary');
60
		header('Connection: Keep-Alive');
61
		header('Expires: 0');
62
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
63
		header('Pragma: public');
64
		header('Content-Length: ' . $size);
65
66
		readfile($path . '/' . $file->file);
67
		exit;
68
	}
69
70
}