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.

helper.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace OCA\Files_Sharing;
  3. use OC_Config;
  4. use PasswordHash;
  5. class Helper {
  6. /**
  7. * Sets up the filesystem and user for public sharing
  8. * @param string $token string share token
  9. * @param string $relativePath optional path relative to the share
  10. * @param string $password optional password
  11. */
  12. public static function setupFromToken($token, $relativePath = null, $password = null) {
  13. \OC_User::setIncognitoMode(true);
  14. $linkItem = \OCP\Share::getShareByToken($token);
  15. if($linkItem === false || ($linkItem['item_type'] !== 'file' && $linkItem['item_type'] !== 'folder')) {
  16. \OC_Response::setStatus(404);
  17. \OC_Log::write('core-preview', 'Passed token parameter is not valid', \OC_Log::DEBUG);
  18. exit;
  19. }
  20. if(!isset($linkItem['uid_owner']) || !isset($linkItem['file_source'])) {
  21. \OC_Response::setStatus(500);
  22. \OC_Log::write('core-preview', 'Passed token seems to be valid, but it does not contain all necessary information . ("' . $token . '")', \OC_Log::WARN);
  23. exit;
  24. }
  25. $rootLinkItem = \OCP\Share::resolveReShare($linkItem);
  26. $path = null;
  27. if (isset($rootLinkItem['uid_owner'])) {
  28. \OCP\JSON::checkUserExists($rootLinkItem['uid_owner']);
  29. \OC_Util::tearDownFS();
  30. \OC_Util::setupFS($rootLinkItem['uid_owner']);
  31. $path = \OC\Files\Filesystem::getPath($linkItem['file_source']);
  32. }
  33. if ($path === null) {
  34. \OCP\Util::writeLog('share', 'could not resolve linkItem', \OCP\Util::DEBUG);
  35. \OC_Response::setStatus(404);
  36. \OCP\JSON::error(array('success' => false));
  37. exit();
  38. }
  39. if (!isset($linkItem['item_type'])) {
  40. \OCP\Util::writeLog('share', 'No item type set for share id: ' . $linkItem['id'], \OCP\Util::ERROR);
  41. \OC_Response::setStatus(404);
  42. \OCP\JSON::error(array('success' => false));
  43. exit();
  44. }
  45. if (isset($linkItem['share_with'])) {
  46. if (!self::authenticate($linkItem, $password)) {
  47. \OC_Response::setStatus(403);
  48. \OCP\JSON::error(array('success' => false));
  49. exit();
  50. }
  51. }
  52. $basePath = $path;
  53. if ($relativePath !== null && \OC\Files\Filesystem::isReadable($basePath . $relativePath)) {
  54. $path .= \OC\Files\Filesystem::normalizePath($relativePath);
  55. }
  56. return array(
  57. 'linkItem' => $linkItem,
  58. 'basePath' => $basePath,
  59. 'realPath' => $path
  60. );
  61. }
  62. /**
  63. * Authenticate link item with the given password
  64. * or with the session if no password was given.
  65. * @param array $linkItem link item array
  66. * @param string $password optional password
  67. *
  68. * @return boolean true if authorized, false otherwise
  69. */
  70. public static function authenticate($linkItem, $password) {
  71. if ($password !== null) {
  72. if ($linkItem['share_type'] == \OCP\Share::SHARE_TYPE_LINK) {
  73. // Check Password
  74. $forcePortable = (CRYPT_BLOWFISH != 1);
  75. $hasher = new PasswordHash(8, $forcePortable);
  76. if (!($hasher->CheckPassword($password.OC_Config::getValue('passwordsalt', ''),
  77. $linkItem['share_with']))) {
  78. return false;
  79. } else {
  80. // Save item id in session for future requests
  81. \OC::$session->set('public_link_authenticated', $linkItem['id']);
  82. }
  83. } else {
  84. \OCP\Util::writeLog('share', 'Unknown share type '.$linkItem['share_type']
  85. .' for share id '.$linkItem['id'], \OCP\Util::ERROR);
  86. return false;
  87. }
  88. }
  89. else {
  90. // not authenticated ?
  91. if ( ! \OC::$session->exists('public_link_authenticated')
  92. || \OC::$session->get('public_link_authenticated') !== $linkItem['id']) {
  93. return false;
  94. }
  95. }
  96. return true;
  97. }
  98. public static function getSharesFromItem($target) {
  99. $result = array();
  100. $owner = \OC\Files\Filesystem::getOwner($target);
  101. \OC\Files\Filesystem::initMountPoints($owner);
  102. $info = \OC\Files\Filesystem::getFileInfo($target);
  103. $ownerView = new \OC\Files\View('/'.$owner.'/files');
  104. if ( $owner != \OCP\User::getUser() ) {
  105. $path = $ownerView->getPath($info['fileid']);
  106. } else {
  107. $path = $target;
  108. }
  109. $ids = array();
  110. while ($path !== dirname($path)) {
  111. $info = $ownerView->getFileInfo($path);
  112. if ($info instanceof \OC\Files\FileInfo) {
  113. $ids[] = $info['fileid'];
  114. } else {
  115. \OCP\Util::writeLog('sharing', 'No fileinfo available for: ' . $path, \OCP\Util::WARN);
  116. }
  117. $path = dirname($path);
  118. }
  119. if (!empty($ids)) {
  120. $idList = array_chunk($ids, 99, true);
  121. foreach ($idList as $subList) {
  122. $statement = "SELECT `share_with`, `share_type`, `file_target` FROM `*PREFIX*share` WHERE `file_source` IN (" . implode(',', $subList) . ") AND `share_type` IN (0, 1, 2)";
  123. $query = \OCP\DB::prepare($statement);
  124. $r = $query->execute();
  125. $result = array_merge($result, $r->fetchAll());
  126. }
  127. }
  128. return $result;
  129. }
  130. public static function getUidAndFilename($filename) {
  131. $uid = \OC\Files\Filesystem::getOwner($filename);
  132. \OC\Files\Filesystem::initMountPoints($uid);
  133. if ( $uid != \OCP\User::getUser() ) {
  134. $info = \OC\Files\Filesystem::getFileInfo($filename);
  135. $ownerView = new \OC\Files\View('/'.$uid.'/files');
  136. $filename = $ownerView->getPath($info['fileid']);
  137. }
  138. return array($uid, $filename);
  139. }
  140. /**
  141. * @brief Format a path to be relative to the /user/files/ directory
  142. * @param string $path the absolute path
  143. * @return string e.g. turns '/admin/files/test.txt' into 'test.txt'
  144. */
  145. public static function stripUserFilesPath($path) {
  146. $trimmed = ltrim($path, '/');
  147. $split = explode('/', $trimmed);
  148. // it is not a file relative to data/user/files
  149. if (count($split) < 3 || $split[1] !== 'files') {
  150. return false;
  151. }
  152. $sliced = array_slice($split, 2);
  153. $relPath = implode('/', $sliced);
  154. return $relPath;
  155. }
  156. }