summaryrefslogtreecommitdiffstats
path: root/apps/files_sharing/get.php
blob: 70a5162d382f055ad51ecf949b86ccc553070248 (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
80
81
82
83
84
85
86
87
88
<?php
$RUNTIME_NOSETUPFS=true; //don't setup the fs yet

// only need authentication apps
$RUNTIME_APPTYPES=array('authentication');
OC_App::loadApps($RUNTIME_APPTYPES);

OCP\JSON::checkAppEnabled('files_sharing');
require_once 'lib_share.php';

//get the path of the shared file
if (isset($_GET['token']) && $source = OC_Share::getSource($_GET['token'])) {
	$token = $_GET['token'];
	// TODO Manipulating the string may not be the best choice. Is there an alternative?
	$user = substr($source, 1, strpos($source, "/", 1) - 1);
	OC_Util::setupFS($user);
	$source = substr($source, strlen("/".$user."/files"));
	$subPath = isset( $_GET['path'] ) ? $_GET['path'] : '';
	$root = $source;
	$source .= $subPath;
	if (!OC_Filesystem::file_exists($source)) {
		header("HTTP/1.0 404 Not Found");
		$tmpl = new OCP\Template("", "404", "guest");
		$tmpl->assign("file", $subPath);
		$tmpl->printPage();
		exit;
	}
	if (OC_Filesystem::is_dir($source)) {
		$files = array();
		$rootLength = strlen($root);
		foreach (OC_Files::getdirectorycontent($source) as $i) {
			$i['date'] = OCP\Util::formatDate($i['mtime'] );
			if ($i['type'] == 'file') {
				$fileinfo = pathinfo($i['name']);
				$i['basename'] = $fileinfo['filename'];
				$i['extension'] = isset($fileinfo['extension']) ? ('.'.$fileinfo['extension']) : '';
			}
			$i['directory'] = substr($i['directory'], $rootLength);
			if ($i['directory'] == "/") {
				$i['directory'] = "";
			}
			$files[] = $i;
		}
		// Make breadcrumb
		$breadcrumb = array();
		$pathtohere = "";
		foreach (explode("/", $subPath) as $i) {
			if ($i != "") {
				$pathtohere .= "/$i";
				$breadcrumb[] = array("dir" => $pathtohere, "name" => $i);
			}
		}
		// Load the files we need
		OCP\Util::addStyle("files", "files");
		$breadcrumbNav = new OCP\Template("files", "part.breadcrumb", "");
		$breadcrumbNav->assign("breadcrumb", $breadcrumb);
		$breadcrumbNav->assign("baseURL", OCP\Util::linkTo("", "public.php")."?service=files&token=".$token."&path=");
		$list = new OCP\Template("files", "part.list", "");
		$list->assign("files", $files);
		$list->assign("baseURL", OCP\Util::linkTo("", "public.php")."?service=files&token=".$token."&path=");
		$list->assign("downloadURL", OCP\Util::linkTo("", "public.php")."?service=files&token=".$token."&path=");
		$list->assign("readonly", true);
		$tmpl = new OCP\Template("files", "index", "user");
		$tmpl->assign("fileList", $list->fetchPage(), false);
		$tmpl->assign("breadcrumb", $breadcrumbNav->fetchPage());
		$tmpl->assign("readonly", true);
		$tmpl->assign("allowZipDownload", false);
		$tmpl->assign("dir", 'shared dir');
		$tmpl->printPage();
	} else {
		//get time mimetype and set the headers
		$mimetype = OC_Filesystem::getMimeType($source);
		header("Content-Transfer-Encoding: binary");
		OCP\Response::disableCaching();
		header('Content-Disposition: attachment; filename="'.basename($source).'"');
		header("Content-Type: " . $mimetype);
		header("Content-Length: " . OC_Filesystem::filesize($source));
		//download the file
		@ob_clean();
		OCP\Util::emitHook('OC_Share', 'public-download', array('source'=>$source, 'token'=>$token));
		OC_Filesystem::readfile($source);
	}
} else {
	header("HTTP/1.0 404 Not Found");
	$tmpl = new OCP\Template("", "404", "guest");
	$tmpl->printPage();
	die();
}