You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

public.php 7.2KB

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