public.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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);
  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('disableSharing', true);
  147. $allowPublicUploadEnabled = (bool) ($linkItem['permissions'] & OCP\PERMISSION_CREATE);
  148. if (\OCP\App::isEnabled('files_encryption')) {
  149. $allowPublicUploadEnabled = false;
  150. }
  151. if (OC_Appconfig::getValue('core', 'shareapi_allow_public_upload', 'yes') === 'no') {
  152. $allowPublicUploadEnabled = false;
  153. }
  154. if ($linkItem['item_type'] !== 'folder') {
  155. $allowPublicUploadEnabled = false;
  156. }
  157. $tmpl->assign('allowPublicUploadEnabled', $allowPublicUploadEnabled);
  158. $tmpl->assign('uploadMaxFilesize', $maxUploadFilesize);
  159. $tmpl->assign('uploadMaxHumanFilesize', OCP\Util::humanFileSize($maxUploadFilesize));
  160. $urlLinkIdentifiers= (isset($token)?'&t='.$token:'')
  161. .(isset($_GET['dir'])?'&dir='.$_GET['dir']:'')
  162. .(isset($_GET['file'])?'&file='.$_GET['file']:'');
  163. // Show file list
  164. if (\OC\Files\Filesystem::is_dir($path)) {
  165. $tmpl->assign('dir', $getPath);
  166. OCP\Util::addStyle('files', 'files');
  167. OCP\Util::addStyle('files', 'upload');
  168. OCP\Util::addScript('files', 'files');
  169. OCP\Util::addScript('files', 'filelist');
  170. OCP\Util::addscript('files', 'keyboardshortcuts');
  171. $files = array();
  172. $rootLength = strlen($basePath) + 1;
  173. $totalSize = 0;
  174. foreach (\OC\Files\Filesystem::getDirectoryContent($path) as $i) {
  175. $totalSize += $i['size'];
  176. $i['date'] = OCP\Util::formatDate($i['mtime']);
  177. if ($i['type'] == 'file') {
  178. $fileinfo = pathinfo($i['name']);
  179. $i['basename'] = $fileinfo['filename'];
  180. if (!empty($fileinfo['extension'])) {
  181. $i['extension'] = '.' . $fileinfo['extension'];
  182. } else {
  183. $i['extension'] = '';
  184. }
  185. $i['isPreviewAvailable'] = \OC::$server->getPreviewManager()->isMimeSupported($i['mimetype']);
  186. }
  187. $i['directory'] = $getPath;
  188. $i['permissions'] = OCP\PERMISSION_READ;
  189. $i['icon'] = determineIcon($i, $basePath, $token);
  190. $files[] = $i;
  191. }
  192. usort($files, "fileCmp");
  193. // Make breadcrumb
  194. $breadcrumb = array();
  195. $pathtohere = '';
  196. foreach (explode('/', $getPath) as $i) {
  197. if ($i != '') {
  198. $pathtohere .= '/' . $i;
  199. $breadcrumb[] = array('dir' => $pathtohere, 'name' => $i);
  200. }
  201. }
  202. $list = new OCP\Template('files', 'part.list', '');
  203. $list->assign('files', $files);
  204. $list->assign('baseURL', OCP\Util::linkToPublic('files') . $urlLinkIdentifiers . '&path=');
  205. $list->assign('downloadURL',
  206. OCP\Util::linkToPublic('files') . $urlLinkIdentifiers . '&download&path=');
  207. $list->assign('isPublic', true);
  208. $list->assign('sharingtoken', $token);
  209. $list->assign('sharingroot', $basePath);
  210. $breadcrumbNav = new OCP\Template('files', 'part.breadcrumb', '');
  211. $breadcrumbNav->assign('breadcrumb', $breadcrumb);
  212. $breadcrumbNav->assign('baseURL', OCP\Util::linkToPublic('files') . $urlLinkIdentifiers . '&path=');
  213. $maxUploadFilesize=OCP\Util::maxUploadFilesize($path);
  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. $tmpl->assign('folder', $folder->fetchPage());
  228. $maxInputFileSize = OCP\Config::getSystemValue('maxZipInputSize', OCP\Util::computerFileSize('800 MB'));
  229. $allowZip = OCP\Config::getSystemValue('allowZipDownload', true)
  230. && ( $maxInputFileSize === 0 || $totalSize <= $maxInputFileSize);
  231. $tmpl->assign('allowZipDownload', intval($allowZip));
  232. $tmpl->assign('downloadURL',
  233. OCP\Util::linkToPublic('files') . $urlLinkIdentifiers . '&download&path=' . urlencode($getPath));
  234. } else {
  235. $tmpl->assign('dir', $dir);
  236. // Show file preview if viewer is available
  237. if ($type == 'file') {
  238. $tmpl->assign('downloadURL', OCP\Util::linkToPublic('files') . $urlLinkIdentifiers . '&download');
  239. } else {
  240. $tmpl->assign('downloadURL', OCP\Util::linkToPublic('files')
  241. .$urlLinkIdentifiers.'&download&path='.urlencode($getPath));
  242. }
  243. }
  244. $tmpl->printPage();
  245. }
  246. exit();
  247. } else {
  248. OCP\Util::writeLog('share', 'could not resolve linkItem', \OCP\Util::DEBUG);
  249. }
  250. $errorTemplate = new OCP\Template('files_sharing', 'part.404', '');
  251. $errorContent = $errorTemplate->fetchPage();
  252. header('HTTP/1.0 404 Not Found');
  253. OCP\Util::addStyle('files_sharing', '404');
  254. $tmpl = new OCP\Template('', '404', 'guest');
  255. $tmpl->assign('content', $errorContent);
  256. $tmpl->printPage();