public.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. // Load other apps for file previews
  3. OC_App::loadApps();
  4. if (\OC_Appconfig::getValue('core', 'shareapi_allow_links', 'yes') !== 'yes') {
  5. header('HTTP/1.0 404 Not Found');
  6. $tmpl = new OCP\Template('', '404', 'guest');
  7. $tmpl->printPage();
  8. exit();
  9. }
  10. function fileCmp($a, $b) {
  11. if ($a['type'] == 'dir' and $b['type'] != 'dir') {
  12. return -1;
  13. } elseif ($a['type'] != 'dir' and $b['type'] == 'dir') {
  14. return 1;
  15. } else {
  16. return strnatcasecmp($a['name'], $b['name']);
  17. }
  18. }
  19. function determineIcon($file, $sharingRoot, $sharingToken) {
  20. // for folders we simply reuse the files logic
  21. if($file['type'] == 'dir') {
  22. return \OCA\Files\Helper::determineIcon($file);
  23. }
  24. $relativePath = substr($file['path'], 6);
  25. $relativePath = substr($relativePath, strlen($sharingRoot));
  26. if($file['isPreviewAvailable']) {
  27. return OCP\publicPreview_icon($relativePath, $sharingToken) . '&c=' . $file['etag'];
  28. }
  29. return OCP\mimetype_icon($file['mimetype']);
  30. }
  31. if (isset($_GET['t'])) {
  32. $token = $_GET['t'];
  33. $linkItem = OCP\Share::getShareByToken($token);
  34. if (is_array($linkItem) && isset($linkItem['uid_owner'])) {
  35. // seems to be a valid share
  36. $type = $linkItem['item_type'];
  37. $fileSource = $linkItem['file_source'];
  38. $shareOwner = $linkItem['uid_owner'];
  39. $path = null;
  40. $rootLinkItem = OCP\Share::resolveReShare($linkItem);
  41. $fileOwner = $rootLinkItem['uid_owner'];
  42. if (isset($fileOwner)) {
  43. OC_Util::tearDownFS();
  44. OC_Util::setupFS($fileOwner);
  45. $path = \OC\Files\Filesystem::getPath($linkItem['file_source']);
  46. }
  47. }
  48. }
  49. if (isset($path)) {
  50. if (!isset($linkItem['item_type'])) {
  51. OCP\Util::writeLog('share', 'No item type set for share id: ' . $linkItem['id'], \OCP\Util::ERROR);
  52. header('HTTP/1.0 404 Not Found');
  53. $tmpl = new OCP\Template('', '404', 'guest');
  54. $tmpl->printPage();
  55. exit();
  56. }
  57. if (isset($linkItem['share_with'])) {
  58. // Authenticate share_with
  59. $url = OCP\Util::linkToPublic('files') . '&t=' . $token;
  60. if (isset($_GET['file'])) {
  61. $url .= '&file=' . urlencode($_GET['file']);
  62. } else {
  63. if (isset($_GET['dir'])) {
  64. $url .= '&dir=' . urlencode($_GET['dir']);
  65. }
  66. }
  67. if (isset($_POST['password'])) {
  68. $password = $_POST['password'];
  69. if ($linkItem['share_type'] == OCP\Share::SHARE_TYPE_LINK) {
  70. // Check Password
  71. $forcePortable = (CRYPT_BLOWFISH != 1);
  72. $hasher = new PasswordHash(8, $forcePortable);
  73. if (!($hasher->CheckPassword($password.OC_Config::getValue('passwordsalt', ''),
  74. $linkItem['share_with']))) {
  75. OCP\Util::addStyle('files_sharing', 'authenticate');
  76. $tmpl = new OCP\Template('files_sharing', 'authenticate', 'guest');
  77. $tmpl->assign('URL', $url);
  78. $tmpl->assign('wrongpw', true);
  79. $tmpl->printPage();
  80. exit();
  81. } else {
  82. // Save item id in session for future requests
  83. \OC::$session->set('public_link_authenticated', $linkItem['id']);
  84. }
  85. } else {
  86. OCP\Util::writeLog('share', 'Unknown share type '.$linkItem['share_type']
  87. .' for share id '.$linkItem['id'], \OCP\Util::ERROR);
  88. header('HTTP/1.0 404 Not Found');
  89. $tmpl = new OCP\Template('', '404', 'guest');
  90. $tmpl->printPage();
  91. exit();
  92. }
  93. } else {
  94. // Check if item id is set in session
  95. if ( ! \OC::$session->exists('public_link_authenticated')
  96. || \OC::$session->get('public_link_authenticated') !== $linkItem['id']
  97. ) {
  98. // Prompt for password
  99. OCP\Util::addStyle('files_sharing', 'authenticate');
  100. $tmpl = new OCP\Template('files_sharing', 'authenticate', 'guest');
  101. $tmpl->assign('URL', $url);
  102. $tmpl->printPage();
  103. exit();
  104. }
  105. }
  106. }
  107. $basePath = $path;
  108. if (isset($_GET['path']) && \OC\Files\Filesystem::isReadable($basePath . $_GET['path'])) {
  109. $getPath = \OC\Files\Filesystem::normalizePath($_GET['path']);
  110. $path .= $getPath;
  111. } else {
  112. $getPath = '';
  113. }
  114. $dir = dirname($path);
  115. $file = basename($path);
  116. // Download the file
  117. if (isset($_GET['download'])) {
  118. if (isset($_GET['files'])) { // download selected files
  119. $files = urldecode($_GET['files']);
  120. $files_list = json_decode($files);
  121. // in case we get only a single file
  122. if ($files_list === NULL ) {
  123. $files_list = array($files);
  124. }
  125. OC_Files::get($path, $files_list, $_SERVER['REQUEST_METHOD'] == 'HEAD');
  126. } else {
  127. OC_Files::get($dir, $file, $_SERVER['REQUEST_METHOD'] == 'HEAD');
  128. }
  129. exit();
  130. } else {
  131. OCP\Util::addScript('files', 'file-upload');
  132. OCP\Util::addStyle('files_sharing', 'public');
  133. OCP\Util::addScript('files_sharing', 'public');
  134. OCP\Util::addScript('files', 'fileactions');
  135. OCP\Util::addScript('files', 'jquery.iframe-transport');
  136. OCP\Util::addScript('files', 'jquery.fileupload');
  137. $maxUploadFilesize=OCP\Util::maxUploadFilesize($path);
  138. $tmpl = new OCP\Template('files_sharing', 'public', 'base');
  139. $tmpl->assign('uidOwner', $shareOwner);
  140. $tmpl->assign('displayName', \OCP\User::getDisplayName($shareOwner));
  141. $tmpl->assign('filename', $file);
  142. $tmpl->assign('directory_path', $linkItem['file_target']);
  143. $tmpl->assign('mimetype', \OC\Files\Filesystem::getMimeType($path));
  144. $tmpl->assign('fileTarget', basename($linkItem['file_target']));
  145. $tmpl->assign('dirToken', $linkItem['token']);
  146. $tmpl->assign('sharingToken', $token);
  147. $tmpl->assign('disableSharing', true);
  148. $allowPublicUploadEnabled = (bool) ($linkItem['permissions'] & OCP\PERMISSION_CREATE);
  149. if (OC_Appconfig::getValue('core', 'shareapi_allow_public_upload', 'yes') === 'no') {
  150. $allowPublicUploadEnabled = false;
  151. }
  152. if ($linkItem['item_type'] !== 'folder') {
  153. $allowPublicUploadEnabled = false;
  154. }
  155. $tmpl->assign('allowPublicUploadEnabled', $allowPublicUploadEnabled);
  156. $tmpl->assign('uploadMaxFilesize', $maxUploadFilesize);
  157. $tmpl->assign('uploadMaxHumanFilesize', OCP\Util::humanFileSize($maxUploadFilesize));
  158. $urlLinkIdentifiers= (isset($token)?'&t='.$token:'')
  159. .(isset($_GET['dir'])?'&dir='.$_GET['dir']:'')
  160. .(isset($_GET['file'])?'&file='.$_GET['file']:'');
  161. // Show file list
  162. if (\OC\Files\Filesystem::is_dir($path)) {
  163. $tmpl->assign('dir', $getPath);
  164. OCP\Util::addStyle('files', 'files');
  165. OCP\Util::addStyle('files', 'upload');
  166. OCP\Util::addScript('files', 'files');
  167. OCP\Util::addScript('files', 'filelist');
  168. OCP\Util::addscript('files', 'keyboardshortcuts');
  169. $files = array();
  170. $rootLength = strlen($basePath) + 1;
  171. $totalSize = 0;
  172. foreach (\OC\Files\Filesystem::getDirectoryContent($path) as $i) {
  173. $totalSize += $i['size'];
  174. $i['date'] = OCP\Util::formatDate($i['mtime']);
  175. if ($i['type'] == 'file') {
  176. $fileinfo = pathinfo($i['name']);
  177. $i['basename'] = $fileinfo['filename'];
  178. if (!empty($fileinfo['extension'])) {
  179. $i['extension'] = '.' . $fileinfo['extension'];
  180. } else {
  181. $i['extension'] = '';
  182. }
  183. $i['isPreviewAvailable'] = \OC::$server->getPreviewManager()->isMimeSupported($i['mimetype']);
  184. }
  185. $i['directory'] = $getPath;
  186. $i['permissions'] = OCP\PERMISSION_READ;
  187. $i['icon'] = determineIcon($i, $basePath, $token);
  188. $files[] = $i;
  189. }
  190. usort($files, "fileCmp");
  191. // Make breadcrumb
  192. $breadcrumb = array();
  193. $pathtohere = '';
  194. foreach (explode('/', $getPath) as $i) {
  195. if ($i != '') {
  196. $pathtohere .= '/' . $i;
  197. $breadcrumb[] = array('dir' => $pathtohere, 'name' => $i);
  198. }
  199. }
  200. $list = new OCP\Template('files', 'part.list', '');
  201. $list->assign('files', $files);
  202. $list->assign('baseURL', OCP\Util::linkToPublic('files') . $urlLinkIdentifiers . '&path=');
  203. $list->assign('downloadURL',
  204. OCP\Util::linkToPublic('files') . $urlLinkIdentifiers . '&download&path=');
  205. $list->assign('isPublic', true);
  206. $list->assign('sharingtoken', $token);
  207. $list->assign('sharingroot', $basePath);
  208. $breadcrumbNav = new OCP\Template('files', 'part.breadcrumb', '');
  209. $breadcrumbNav->assign('breadcrumb', $breadcrumb);
  210. $breadcrumbNav->assign('baseURL', OCP\Util::linkToPublic('files') . $urlLinkIdentifiers . '&path=');
  211. $maxUploadFilesize=OCP\Util::maxUploadFilesize($path);
  212. $fileHeader = (!isset($files) or count($files) > 0);
  213. $emptyContent = ($allowPublicUploadEnabled and !$fileHeader);
  214. $folder = new OCP\Template('files', 'index', '');
  215. $folder->assign('fileList', $list->fetchPage());
  216. $folder->assign('breadcrumb', $breadcrumbNav->fetchPage());
  217. $folder->assign('dir', $getPath);
  218. $folder->assign('isCreatable', false);
  219. $folder->assign('permissions', OCP\PERMISSION_READ);
  220. $folder->assign('isPublic',true);
  221. $folder->assign('publicUploadEnabled', 'no');
  222. $folder->assign('files', $files);
  223. $folder->assign('uploadMaxFilesize', $maxUploadFilesize);
  224. $folder->assign('uploadMaxHumanFilesize', OCP\Util::humanFileSize($maxUploadFilesize));
  225. $folder->assign('allowZipDownload', intval(OCP\Config::getSystemValue('allowZipDownload', true)));
  226. $folder->assign('usedSpacePercent', 0);
  227. $folder->assign('fileHeader', $fileHeader);
  228. $folder->assign('disableSharing', true);
  229. $folder->assign('trash', false);
  230. $folder->assign('emptyContent', $emptyContent);
  231. $folder->assign('ajaxLoad', false);
  232. $tmpl->assign('folder', $folder->fetchPage());
  233. $maxInputFileSize = OCP\Config::getSystemValue('maxZipInputSize', OCP\Util::computerFileSize('800 MB'));
  234. $allowZip = OCP\Config::getSystemValue('allowZipDownload', true)
  235. && ( $maxInputFileSize === 0 || $totalSize <= $maxInputFileSize);
  236. $tmpl->assign('allowZipDownload', intval($allowZip));
  237. $tmpl->assign('downloadURL',
  238. OCP\Util::linkToPublic('files') . $urlLinkIdentifiers . '&download&path=' . urlencode($getPath));
  239. } else {
  240. $tmpl->assign('dir', $dir);
  241. // Show file preview if viewer is available
  242. if ($type == 'file') {
  243. $tmpl->assign('downloadURL', OCP\Util::linkToPublic('files') . $urlLinkIdentifiers . '&download');
  244. } else {
  245. $tmpl->assign('downloadURL', OCP\Util::linkToPublic('files')
  246. .$urlLinkIdentifiers.'&download&path='.urlencode($getPath));
  247. }
  248. }
  249. $tmpl->printPage();
  250. }
  251. exit();
  252. } else {
  253. OCP\Util::writeLog('share', 'could not resolve linkItem', \OCP\Util::DEBUG);
  254. }
  255. $errorTemplate = new OCP\Template('files_sharing', 'part.404', '');
  256. $errorContent = $errorTemplate->fetchPage();
  257. header('HTTP/1.0 404 Not Found');
  258. OCP\Util::addStyle('files_sharing', '404');
  259. $tmpl = new OCP\Template('', '404', 'guest');
  260. $tmpl->assign('content', $errorContent);
  261. $tmpl->printPage();