publicpreview.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Georg Ehrke <georg@owncloud.com>
  5. * @author Lukas Reschke <lukas@owncloud.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. OCP\JSON::checkAppEnabled('files_sharing');
  26. \OC_User::setIncognitoMode(true);
  27. $file = array_key_exists('file', $_GET) ? (string) $_GET['file'] : '';
  28. $maxX = array_key_exists('x', $_GET) ? (int) $_GET['x'] : '36';
  29. $maxY = array_key_exists('y', $_GET) ? (int) $_GET['y'] : '36';
  30. $scalingUp = array_key_exists('scalingup', $_GET) ? (bool) $_GET['scalingup'] : true;
  31. $token = array_key_exists('t', $_GET) ? (string) $_GET['t'] : '';
  32. $keepAspect = array_key_exists('a', $_GET) ? true : false;
  33. if($token === ''){
  34. \OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST);
  35. \OCP\Util::writeLog('core-preview', 'No token parameter was passed', \OCP\Util::DEBUG);
  36. exit;
  37. }
  38. $linkedItem = \OCP\Share::getShareByToken($token);
  39. if($linkedItem === false || ($linkedItem['item_type'] !== 'file' && $linkedItem['item_type'] !== 'folder')) {
  40. \OC_Response::setStatus(\OC_Response::STATUS_NOT_FOUND);
  41. \OCP\Util::writeLog('core-preview', 'Passed token parameter is not valid', \OCP\Util::DEBUG);
  42. exit;
  43. }
  44. if(!isset($linkedItem['uid_owner']) || !isset($linkedItem['file_source'])) {
  45. \OC_Response::setStatus(\OC_Response::STATUS_INTERNAL_SERVER_ERROR);
  46. \OCP\Util::writeLog('core-preview', 'Passed token seems to be valid, but it does not contain all necessary information . ("' . $token . '")', \OCP\Util::WARN);
  47. exit;
  48. }
  49. $rootLinkItem = OCP\Share::resolveReShare($linkedItem);
  50. $userId = $rootLinkItem['uid_owner'];
  51. OCP\JSON::checkUserExists($rootLinkItem['uid_owner']);
  52. \OC_Util::setupFS($userId);
  53. \OC\Files\Filesystem::initMountPoints($userId);
  54. $view = new \OC\Files\View('/' . $userId . '/files');
  55. $pathId = $linkedItem['file_source'];
  56. $path = $view->getPath($pathId);
  57. if($path === null) {
  58. \OC_Response::setStatus(\OC_Response::STATUS_NOT_FOUND);
  59. \OCP\Util::writeLog('core-preview', 'Could not resolve file for shared item', \OCP\Util::WARN);
  60. exit;
  61. }
  62. $pathInfo = $view->getFileInfo($path);
  63. $sharedFile = null;
  64. if($linkedItem['item_type'] === 'folder') {
  65. $isValid = \OC\Files\Filesystem::isValidPath($file);
  66. if(!$isValid) {
  67. \OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST);
  68. \OCP\Util::writeLog('core-preview', 'Passed filename is not valid, might be malicious (file:"' . $file . '";ip:"' . \OC::$server->getRequest()->getRemoteAddress() . '")', \OCP\Util::WARN);
  69. exit;
  70. }
  71. $sharedFile = \OC\Files\Filesystem::normalizePath($file);
  72. }
  73. if($linkedItem['item_type'] === 'file') {
  74. $parent = $pathInfo['parent'];
  75. $path = $view->getPath($parent);
  76. $sharedFile = $pathInfo['name'];
  77. }
  78. $path = \OC\Files\Filesystem::normalizePath($path, false);
  79. if(substr($path, 0, 1) === '/') {
  80. $path = substr($path, 1);
  81. }
  82. if($maxX === 0 || $maxY === 0) {
  83. \OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST);
  84. \OCP\Util::writeLog('core-preview', 'x and/or y set to 0', \OCP\Util::DEBUG);
  85. exit;
  86. }
  87. $root = 'files/' . $path;
  88. try{
  89. $preview = new \OC\Preview($userId, $root);
  90. $preview->setFile($sharedFile);
  91. $preview->setMaxX($maxX);
  92. $preview->setMaxY($maxY);
  93. $preview->setScalingUp($scalingUp);
  94. $preview->setKeepAspect($keepAspect);
  95. $preview->showPreview();
  96. } catch (\Exception $e) {
  97. \OC_Response::setStatus(\OC_Response::STATUS_INTERNAL_SERVER_ERROR);
  98. \OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::DEBUG);
  99. }