get.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. $RUNTIME_NOSETUPFS=true; //don't setup the fs yet
  3. // only need authentication apps
  4. $RUNTIME_APPTYPES=array('authentication');
  5. OC_App::loadApps($RUNTIME_APPTYPES);
  6. OCP\JSON::checkAppEnabled('files_sharing');
  7. require_once 'lib_share.php';
  8. //get the path of the shared file
  9. if (isset($_GET['token']) && $source = OC_Share::getSource($_GET['token'])) {
  10. $token = $_GET['token'];
  11. // TODO Manipulating the string may not be the best choice. Is there an alternative?
  12. $user = substr($source, 1, strpos($source, "/", 1) - 1);
  13. OC_Util::setupFS($user);
  14. $source = substr($source, strlen("/".$user."/files"));
  15. $subPath = isset( $_GET['path'] ) ? $_GET['path'] : '';
  16. $root = $source;
  17. $source .= $subPath;
  18. if (!OC_Filesystem::file_exists($source)) {
  19. header("HTTP/1.0 404 Not Found");
  20. $tmpl = new OCP\Template("", "404", "guest");
  21. $tmpl->assign("file", $subPath);
  22. $tmpl->printPage();
  23. exit;
  24. }
  25. if (OC_Filesystem::is_dir($source)) {
  26. $files = array();
  27. $rootLength = strlen($root);
  28. foreach (OC_Files::getdirectorycontent($source) as $i) {
  29. $i['date'] = OCP\Util::formatDate($i['mtime'] );
  30. if ($i['type'] == 'file') {
  31. $fileinfo = pathinfo($i['name']);
  32. $i['basename'] = $fileinfo['filename'];
  33. $i['extension'] = isset($fileinfo['extension']) ? ('.'.$fileinfo['extension']) : '';
  34. }
  35. $i['directory'] = substr($i['directory'], $rootLength);
  36. if ($i['directory'] == "/") {
  37. $i['directory'] = "";
  38. }
  39. $files[] = $i;
  40. }
  41. // Make breadcrumb
  42. $breadcrumb = array();
  43. $pathtohere = "";
  44. foreach (explode("/", $subPath) as $i) {
  45. if ($i != "") {
  46. $pathtohere .= "/$i";
  47. $breadcrumb[] = array("dir" => $pathtohere, "name" => $i);
  48. }
  49. }
  50. // Load the files we need
  51. OCP\Util::addStyle("files", "files");
  52. $breadcrumbNav = new OCP\Template("files", "part.breadcrumb", "");
  53. $breadcrumbNav->assign("breadcrumb", $breadcrumb);
  54. $breadcrumbNav->assign("baseURL", OCP\Util::linkTo("files_sharing", "get.php")."?token=".$token."&path=");
  55. $list = new OCP\Template("files", "part.list", "");
  56. $list->assign("files", $files);
  57. $list->assign("baseURL", OCP\Util::linkTo("files_sharing", "get.php")."?token=".$token."&path=");
  58. $list->assign("downloadURL", OCP\Util::linkTo("files_sharing", "get.php")."?token=".$token."&path=");
  59. $list->assign("readonly", true);
  60. $tmpl = new OCP\Template("files", "index", "user");
  61. $tmpl->assign("fileList", $list->fetchPage());
  62. $tmpl->assign("breadcrumb", $breadcrumbNav->fetchPage());
  63. $tmpl->assign("readonly", true);
  64. $tmpl->assign("allowZipDownload", false);
  65. $tmpl->assign("dir", 'shared dir');
  66. $tmpl->printPage();
  67. } else {
  68. //get time mimetype and set the headers
  69. $mimetype = OC_Filesystem::getMimeType($source);
  70. header("Content-Transfer-Encoding: binary");
  71. OCP\Response::disableCaching();
  72. header('Content-Disposition: attachment; filename="'.basename($source).'"');
  73. header("Content-Type: " . $mimetype);
  74. header("Content-Length: " . OC_Filesystem::filesize($source));
  75. //download the file
  76. @ob_clean();
  77. OC_Filesystem::readfile($source);
  78. }
  79. } else {
  80. header("HTTP/1.0 404 Not Found");
  81. $tmpl = new OCP\Template("", "404", "guest");
  82. $tmpl->printPage();
  83. die();
  84. }
  85. ?>