Blame view

sources/apps/files_sharing/public.php 7.09 KB
03e52840d   Kload   Init
1
2
3
  <?php
  // Load other apps for file previews
  OC_App::loadApps();
6d9380f96   Cédric Dupont   Update sources OC...
4
5
6
  $appConfig = \OC::$server->getAppConfig();
  
  if ($appConfig->getValue('core', 'shareapi_allow_links', 'yes') !== 'yes') {
31b7f2792   Kload   Upgrade to ownclo...
7
8
9
10
11
  	header('HTTP/1.0 404 Not Found');
  	$tmpl = new OCP\Template('', '404', 'guest');
  	$tmpl->printPage();
  	exit();
  }
03e52840d   Kload   Init
12
13
  if (isset($_GET['t'])) {
  	$token = $_GET['t'];
a293d369c   Kload   Update sources to...
14
  	$linkItem = OCP\Share::getShareByToken($token, false);
03e52840d   Kload   Init
15
16
17
18
19
20
21
  	if (is_array($linkItem) && isset($linkItem['uid_owner'])) {
  		// seems to be a valid share
  		$type = $linkItem['item_type'];
  		$fileSource = $linkItem['file_source'];
  		$shareOwner = $linkItem['uid_owner'];
  		$path = null;
  		$rootLinkItem = OCP\Share::resolveReShare($linkItem);
a293d369c   Kload   Update sources to...
22
23
  		if (isset($rootLinkItem['uid_owner'])) {
  			OCP\JSON::checkUserExists($rootLinkItem['uid_owner']);
03e52840d   Kload   Init
24
  			OC_Util::tearDownFS();
a293d369c   Kload   Update sources to...
25
  			OC_Util::setupFS($rootLinkItem['uid_owner']);
03e52840d   Kload   Init
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
  			$path = \OC\Files\Filesystem::getPath($linkItem['file_source']);
  		}
  	}
  }
  if (isset($path)) {
  	if (!isset($linkItem['item_type'])) {
  		OCP\Util::writeLog('share', 'No item type set for share id: ' . $linkItem['id'], \OCP\Util::ERROR);
  		header('HTTP/1.0 404 Not Found');
  		$tmpl = new OCP\Template('', '404', 'guest');
  		$tmpl->printPage();
  		exit();
  	}
  	if (isset($linkItem['share_with'])) {
  		// Authenticate share_with
  		$url = OCP\Util::linkToPublic('files') . '&t=' . $token;
  		if (isset($_GET['file'])) {
  			$url .= '&file=' . urlencode($_GET['file']);
  		} else {
  			if (isset($_GET['dir'])) {
  				$url .= '&dir=' . urlencode($_GET['dir']);
  			}
  		}
  		if (isset($_POST['password'])) {
  			$password = $_POST['password'];
  			if ($linkItem['share_type'] == OCP\Share::SHARE_TYPE_LINK) {
  				// Check Password
  				$forcePortable = (CRYPT_BLOWFISH != 1);
  				$hasher = new PasswordHash(8, $forcePortable);
  				if (!($hasher->CheckPassword($password.OC_Config::getValue('passwordsalt', ''),
  											 $linkItem['share_with']))) {
31b7f2792   Kload   Upgrade to ownclo...
56
  					OCP\Util::addStyle('files_sharing', 'authenticate');
03e52840d   Kload   Init
57
58
59
60
61
62
63
  					$tmpl = new OCP\Template('files_sharing', 'authenticate', 'guest');
  					$tmpl->assign('URL', $url);
  					$tmpl->assign('wrongpw', true);
  					$tmpl->printPage();
  					exit();
  				} else {
  					// Save item id in session for future requests
31b7f2792   Kload   Upgrade to ownclo...
64
  					\OC::$session->set('public_link_authenticated', $linkItem['id']);
03e52840d   Kload   Init
65
66
67
68
69
70
71
72
73
74
75
76
  				}
  			} else {
  				OCP\Util::writeLog('share', 'Unknown share type '.$linkItem['share_type']
  										   .' for share id '.$linkItem['id'], \OCP\Util::ERROR);
  				header('HTTP/1.0 404 Not Found');
  				$tmpl = new OCP\Template('', '404', 'guest');
  				$tmpl->printPage();
  				exit();
  			}
  
  		} else {
  			// Check if item id is set in session
31b7f2792   Kload   Upgrade to ownclo...
77
78
  			if ( ! \OC::$session->exists('public_link_authenticated')
  				|| \OC::$session->get('public_link_authenticated') !== $linkItem['id']
03e52840d   Kload   Init
79
80
  			) {
  				// Prompt for password
31b7f2792   Kload   Upgrade to ownclo...
81
  				OCP\Util::addStyle('files_sharing', 'authenticate');
03e52840d   Kload   Init
82
83
84
85
86
87
88
89
  				$tmpl = new OCP\Template('files_sharing', 'authenticate', 'guest');
  				$tmpl->assign('URL', $url);
  				$tmpl->printPage();
  				exit();
  			}
  		}
  	}
  	$basePath = $path;
6d9380f96   Cédric Dupont   Update sources OC...
90
  	$rootName = basename($path);
03e52840d   Kload   Init
91
92
93
94
95
96
97
98
99
100
  	if (isset($_GET['path']) && \OC\Files\Filesystem::isReadable($basePath . $_GET['path'])) {
  		$getPath = \OC\Files\Filesystem::normalizePath($_GET['path']);
  		$path .= $getPath;
  	} else {
  		$getPath = '';
  	}
  	$dir = dirname($path);
  	$file = basename($path);
  	// Download the file
  	if (isset($_GET['download'])) {
f7d878ff1   kload   [enh] Update to 7...
101
102
103
104
  		if (!\OCP\App::isEnabled('files_encryption')) {
  			// encryption app requires the session to store the keys in
  			\OC::$server->getSession()->close();
  		}
03e52840d   Kload   Init
105
106
107
108
109
110
111
  		if (isset($_GET['files'])) { // download selected files
  			$files = urldecode($_GET['files']);
  			$files_list = json_decode($files);
  			// in case we get only a single file
  			if ($files_list === NULL ) {
  				$files_list = array($files);
  			}
31b7f2792   Kload   Upgrade to ownclo...
112
  			OC_Files::get($path, $files_list, $_SERVER['REQUEST_METHOD'] == 'HEAD');
03e52840d   Kload   Init
113
  		} else {
31b7f2792   Kload   Upgrade to ownclo...
114
  			OC_Files::get($dir, $file, $_SERVER['REQUEST_METHOD'] == 'HEAD');
03e52840d   Kload   Init
115
116
117
118
119
  		}
  		exit();
  	} else {
  		OCP\Util::addScript('files', 'file-upload');
  		OCP\Util::addStyle('files_sharing', 'public');
a293d369c   Kload   Update sources to...
120
  		OCP\Util::addStyle('files_sharing', 'mobile');
03e52840d   Kload   Init
121
122
123
124
125
126
  		OCP\Util::addScript('files_sharing', 'public');
  		OCP\Util::addScript('files', 'fileactions');
  		OCP\Util::addScript('files', 'jquery.iframe-transport');
  		OCP\Util::addScript('files', 'jquery.fileupload');
  		$maxUploadFilesize=OCP\Util::maxUploadFilesize($path);
  		$tmpl = new OCP\Template('files_sharing', 'public', 'base');
03e52840d   Kload   Init
127
128
129
130
  		$tmpl->assign('displayName', \OCP\User::getDisplayName($shareOwner));
  		$tmpl->assign('filename', $file);
  		$tmpl->assign('directory_path', $linkItem['file_target']);
  		$tmpl->assign('mimetype', \OC\Files\Filesystem::getMimeType($path));
03e52840d   Kload   Init
131
  		$tmpl->assign('dirToken', $linkItem['token']);
31b7f2792   Kload   Upgrade to ownclo...
132
  		$tmpl->assign('sharingToken', $token);
6d9380f96   Cédric Dupont   Update sources OC...
133
  		$tmpl->assign('protected', isset($linkItem['share_with']) ? 'true' : 'false');
03e52840d   Kload   Init
134
135
136
137
138
139
140
141
142
  
  		$urlLinkIdentifiers= (isset($token)?'&t='.$token:'')
  							.(isset($_GET['dir'])?'&dir='.$_GET['dir']:'')
  							.(isset($_GET['file'])?'&file='.$_GET['file']:'');
  		// Show file list
  		if (\OC\Files\Filesystem::is_dir($path)) {
  			$tmpl->assign('dir', $getPath);
  
  			OCP\Util::addStyle('files', 'files');
31b7f2792   Kload   Upgrade to ownclo...
143
  			OCP\Util::addStyle('files', 'upload');
6d9380f96   Cédric Dupont   Update sources OC...
144
145
  			OCP\Util::addScript('files', 'filesummary');
  			OCP\Util::addScript('files', 'breadcrumb');
03e52840d   Kload   Init
146
147
148
149
150
  			OCP\Util::addScript('files', 'files');
  			OCP\Util::addScript('files', 'filelist');
  			OCP\Util::addscript('files', 'keyboardshortcuts');
  			$files = array();
  			$rootLength = strlen($basePath) + 1;
03e52840d   Kload   Init
151
  			$maxUploadFilesize=OCP\Util::maxUploadFilesize($path);
6d9380f96   Cédric Dupont   Update sources OC...
152
153
154
155
  
  			$freeSpace=OCP\Util::freeSpace($path);
  			$uploadLimit=OCP\Util::uploadLimit();
  			$folder = new OCP\Template('files', 'list', '');
03e52840d   Kload   Init
156
  			$folder->assign('dir', $getPath);
a293d369c   Kload   Update sources to...
157
  			$folder->assign('dirToken', $linkItem['token']);
03e52840d   Kload   Init
158
  			$folder->assign('permissions', OCP\PERMISSION_READ);
6d9380f96   Cédric Dupont   Update sources OC...
159
  			$folder->assign('isPublic', true);
03e52840d   Kload   Init
160
161
162
163
  			$folder->assign('publicUploadEnabled', 'no');
  			$folder->assign('files', $files);
  			$folder->assign('uploadMaxFilesize', $maxUploadFilesize);
  			$folder->assign('uploadMaxHumanFilesize', OCP\Util::humanFileSize($maxUploadFilesize));
6d9380f96   Cédric Dupont   Update sources OC...
164
165
  			$folder->assign('freeSpace', $freeSpace);
  			$folder->assign('uploadLimit', $uploadLimit); // PHP upload limit
03e52840d   Kload   Init
166
  			$folder->assign('usedSpacePercent', 0);
31b7f2792   Kload   Upgrade to ownclo...
167
  			$folder->assign('trash', false);
03e52840d   Kload   Init
168
  			$tmpl->assign('folder', $folder->fetchPage());
03e52840d   Kload   Init
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  			$tmpl->assign('downloadURL',
  				OCP\Util::linkToPublic('files') . $urlLinkIdentifiers . '&download&path=' . urlencode($getPath));
  		} else {
  			$tmpl->assign('dir', $dir);
  
  			// Show file preview if viewer is available
  			if ($type == 'file') {
  				$tmpl->assign('downloadURL', OCP\Util::linkToPublic('files') . $urlLinkIdentifiers . '&download');
  			} else {
  				$tmpl->assign('downloadURL', OCP\Util::linkToPublic('files')
  										.$urlLinkIdentifiers.'&download&path='.urlencode($getPath));
  			}
  		}
  		$tmpl->printPage();
  	}
  	exit();
  } else {
  	OCP\Util::writeLog('share', 'could not resolve linkItem', \OCP\Util::DEBUG);
  }
31b7f2792   Kload   Upgrade to ownclo...
188
189
190
  
  $errorTemplate = new OCP\Template('files_sharing', 'part.404', '');
  $errorContent = $errorTemplate->fetchPage();
03e52840d   Kload   Init
191
  header('HTTP/1.0 404 Not Found');
31b7f2792   Kload   Upgrade to ownclo...
192
  OCP\Util::addStyle('files_sharing', '404');
03e52840d   Kload   Init
193
  $tmpl = new OCP\Template('', '404', 'guest');
31b7f2792   Kload   Upgrade to ownclo...
194
  $tmpl->assign('content', $errorContent);
03e52840d   Kload   Init
195
  $tmpl->printPage();