public.php 7.3 KB

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