download.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Piotr Filiciak <piotr@filiciak.pl>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. // Check if we are a user
  31. OC_Util::checkLoggedIn();
  32. \OC::$server->getSession()->close();
  33. $files = isset($_GET['files']) ? (string)$_GET['files'] : '';
  34. $dir = isset($_GET['dir']) ? (string)$_GET['dir'] : '';
  35. $files_list = json_decode($files);
  36. // in case we get only a single file
  37. if (!is_array($files_list)) {
  38. $files_list = [$files];
  39. }
  40. /**
  41. * @psalm-taint-escape cookie
  42. */
  43. function cleanCookieInput(string $value): string {
  44. if (strlen($value) > 32) {
  45. return '';
  46. }
  47. if (preg_match('!^[a-zA-Z0-9]+$!', $_GET['downloadStartSecret']) !== 1) {
  48. return '';
  49. }
  50. return $value;
  51. }
  52. /**
  53. * this sets a cookie to be able to recognize the start of the download
  54. * the content must not be longer than 32 characters and must only contain
  55. * alphanumeric characters
  56. */
  57. if (isset($_GET['downloadStartSecret'])) {
  58. $value = cleanCookieInput($_GET['downloadStartSecret']);
  59. if ($value !== '') {
  60. setcookie('ocDownloadStarted', $value, time() + 20, '/');
  61. }
  62. }
  63. $server_params = [ 'head' => \OC::$server->getRequest()->getMethod() === 'HEAD' ];
  64. /**
  65. * Http range requests support
  66. */
  67. if (isset($_SERVER['HTTP_RANGE'])) {
  68. $server_params['range'] = \OC::$server->getRequest()->getHeader('Range');
  69. }
  70. OC_Files::get($dir, $files_list, $server_params);