publicpreview.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Georg Ehrke georg@ownCloud.com
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. OCP\JSON::checkAppEnabled('files_sharing');
  9. \OC_User::setIncognitoMode(true);
  10. $file = array_key_exists('file', $_GET) ? (string) $_GET['file'] : '';
  11. $maxX = array_key_exists('x', $_GET) ? (int) $_GET['x'] : '36';
  12. $maxY = array_key_exists('y', $_GET) ? (int) $_GET['y'] : '36';
  13. $scalingUp = array_key_exists('scalingup', $_GET) ? (bool) $_GET['scalingup'] : true;
  14. $token = array_key_exists('t', $_GET) ? (string) $_GET['t'] : '';
  15. $keepAspect = array_key_exists('a', $_GET) ? true : false;
  16. if($token === ''){
  17. \OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST);
  18. \OC_Log::write('core-preview', 'No token parameter was passed', \OC_Log::DEBUG);
  19. exit;
  20. }
  21. $linkedItem = \OCP\Share::getShareByToken($token);
  22. if($linkedItem === false || ($linkedItem['item_type'] !== 'file' && $linkedItem['item_type'] !== 'folder')) {
  23. \OC_Response::setStatus(\OC_Response::STATUS_NOT_FOUND);
  24. \OC_Log::write('core-preview', 'Passed token parameter is not valid', \OC_Log::DEBUG);
  25. exit;
  26. }
  27. if(!isset($linkedItem['uid_owner']) || !isset($linkedItem['file_source'])) {
  28. \OC_Response::setStatus(\OC_Response::STATUS_INTERNAL_SERVER_ERROR);
  29. \OC_Log::write('core-preview', 'Passed token seems to be valid, but it does not contain all necessary information . ("' . $token . '")', \OC_Log::WARN);
  30. exit;
  31. }
  32. $rootLinkItem = OCP\Share::resolveReShare($linkedItem);
  33. $userId = $rootLinkItem['uid_owner'];
  34. OCP\JSON::checkUserExists($rootLinkItem['uid_owner']);
  35. \OC_Util::setupFS($userId);
  36. \OC\Files\Filesystem::initMountPoints($userId);
  37. $view = new \OC\Files\View('/' . $userId . '/files');
  38. $pathId = $linkedItem['file_source'];
  39. $path = $view->getPath($pathId);
  40. $pathInfo = $view->getFileInfo($path);
  41. $sharedFile = null;
  42. if($linkedItem['item_type'] === 'folder') {
  43. $isValid = \OC\Files\Filesystem::isValidPath($file);
  44. if(!$isValid) {
  45. \OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST);
  46. \OC_Log::write('core-preview', 'Passed filename is not valid, might be malicious (file:"' . $file . '";ip:"' . $_SERVER['REMOTE_ADDR'] . '")', \OC_Log::WARN);
  47. exit;
  48. }
  49. $sharedFile = \OC\Files\Filesystem::normalizePath($file);
  50. }
  51. if($linkedItem['item_type'] === 'file') {
  52. $parent = $pathInfo['parent'];
  53. $path = $view->getPath($parent);
  54. $sharedFile = $pathInfo['name'];
  55. }
  56. $path = \OC\Files\Filesystem::normalizePath($path, false);
  57. if(substr($path, 0, 1) === '/') {
  58. $path = substr($path, 1);
  59. }
  60. if($maxX === 0 || $maxY === 0) {
  61. \OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST);
  62. \OC_Log::write('core-preview', 'x and/or y set to 0', \OC_Log::DEBUG);
  63. exit;
  64. }
  65. $root = 'files/' . $path;
  66. try{
  67. $preview = new \OC\Preview($userId, $root);
  68. $preview->setFile($sharedFile);
  69. $preview->setMaxX($maxX);
  70. $preview->setMaxY($maxY);
  71. $preview->setScalingUp($scalingUp);
  72. $preview->setKeepAspect($keepAspect);
  73. $preview->showPreview();
  74. } catch (\Exception $e) {
  75. \OC_Response::setStatus(\OC_Response::STATUS_INTERNAL_SERVER_ERROR);
  76. \OC_Log::write('core', $e->getmessage(), \OC_Log::DEBUG);
  77. }