helper.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace OCA\Files_Sharing;
  3. use OC_Config;
  4. class Helper {
  5. public static function registerHooks() {
  6. \OCP\Util::connectHook('OC_Filesystem', 'setup', '\OC\Files\Storage\Shared', 'setup');
  7. \OCP\Util::connectHook('OC_Filesystem', 'setup', '\OCA\Files_Sharing\External\Manager', 'setup');
  8. \OCP\Util::connectHook('OC_Filesystem', 'post_write', '\OC\Files\Cache\Shared_Updater', 'writeHook');
  9. \OCP\Util::connectHook('OC_Filesystem', 'post_delete', '\OC\Files\Cache\Shared_Updater', 'postDeleteHook');
  10. \OCP\Util::connectHook('OC_Filesystem', 'delete', '\OC\Files\Cache\Shared_Updater', 'deleteHook');
  11. \OCP\Util::connectHook('OC_Filesystem', 'post_rename', '\OC\Files\Cache\Shared_Updater', 'renameHook');
  12. \OCP\Util::connectHook('OC_Appconfig', 'post_set_value', '\OCA\Files\Share\Maintainer', 'configChangeHook');
  13. \OCP\Util::connectHook('OCP\Share', 'post_shared', '\OC\Files\Cache\Shared_Updater', 'postShareHook');
  14. \OCP\Util::connectHook('OCP\Share', 'post_unshare', '\OC\Files\Cache\Shared_Updater', 'postUnshareHook');
  15. \OCP\Util::connectHook('OCP\Share', 'post_unshareFromSelf', '\OC\Files\Cache\Shared_Updater', 'postUnshareFromSelfHook');
  16. }
  17. /**
  18. * Sets up the filesystem and user for public sharing
  19. * @param string $token string share token
  20. * @param string $relativePath optional path relative to the share
  21. * @param string $password optional password
  22. */
  23. public static function setupFromToken($token, $relativePath = null, $password = null) {
  24. \OC_User::setIncognitoMode(true);
  25. $linkItem = \OCP\Share::getShareByToken($token, !$password);
  26. if($linkItem === false || ($linkItem['item_type'] !== 'file' && $linkItem['item_type'] !== 'folder')) {
  27. \OC_Response::setStatus(404);
  28. \OC_Log::write('core-preview', 'Passed token parameter is not valid', \OC_Log::DEBUG);
  29. exit;
  30. }
  31. if(!isset($linkItem['uid_owner']) || !isset($linkItem['file_source'])) {
  32. \OC_Response::setStatus(500);
  33. \OC_Log::write('core-preview', 'Passed token seems to be valid, but it does not contain all necessary information . ("' . $token . '")', \OC_Log::WARN);
  34. exit;
  35. }
  36. $rootLinkItem = \OCP\Share::resolveReShare($linkItem);
  37. $path = null;
  38. if (isset($rootLinkItem['uid_owner'])) {
  39. \OCP\JSON::checkUserExists($rootLinkItem['uid_owner']);
  40. \OC_Util::tearDownFS();
  41. \OC_Util::setupFS($rootLinkItem['uid_owner']);
  42. $path = \OC\Files\Filesystem::getPath($linkItem['file_source']);
  43. }
  44. if ($path === null) {
  45. \OCP\Util::writeLog('share', 'could not resolve linkItem', \OCP\Util::DEBUG);
  46. \OC_Response::setStatus(404);
  47. \OCP\JSON::error(array('success' => false));
  48. exit();
  49. }
  50. if (!isset($linkItem['item_type'])) {
  51. \OCP\Util::writeLog('share', 'No item type set for share id: ' . $linkItem['id'], \OCP\Util::ERROR);
  52. \OC_Response::setStatus(404);
  53. \OCP\JSON::error(array('success' => false));
  54. exit();
  55. }
  56. if (isset($linkItem['share_with'])) {
  57. if (!self::authenticate($linkItem, $password)) {
  58. \OC_Response::setStatus(403);
  59. \OCP\JSON::error(array('success' => false));
  60. exit();
  61. }
  62. }
  63. $basePath = $path;
  64. if ($relativePath !== null && \OC\Files\Filesystem::isReadable($basePath . $relativePath)) {
  65. $path .= \OC\Files\Filesystem::normalizePath($relativePath);
  66. }
  67. return array(
  68. 'linkItem' => $linkItem,
  69. 'basePath' => $basePath,
  70. 'realPath' => $path
  71. );
  72. }
  73. /**
  74. * Authenticate link item with the given password
  75. * or with the session if no password was given.
  76. * @param array $linkItem link item array
  77. * @param string $password optional password
  78. *
  79. * @return boolean true if authorized, false otherwise
  80. */
  81. public static function authenticate($linkItem, $password = null) {
  82. if ($password !== null) {
  83. if ($linkItem['share_type'] == \OCP\Share::SHARE_TYPE_LINK) {
  84. // Check Password
  85. $newHash = '';
  86. if(\OC::$server->getHasher()->verify($password, $linkItem['share_with'], $newHash)) {
  87. // Save item id in session for future requests
  88. \OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']);
  89. /**
  90. * FIXME: Migrate old hashes to new hash format
  91. * Due to the fact that there is no reasonable functionality to update the password
  92. * of an existing share no migration is yet performed there.
  93. * The only possibility is to update the existing share which will result in a new
  94. * share ID and is a major hack.
  95. *
  96. * In the future the migration should be performed once there is a proper method
  97. * to update the share's password. (for example `$share->updatePassword($password)`
  98. *
  99. * @link https://github.com/owncloud/core/issues/10671
  100. */
  101. if(!empty($newHash)) {
  102. }
  103. } else {
  104. return false;
  105. }
  106. } else {
  107. \OCP\Util::writeLog('share', 'Unknown share type '.$linkItem['share_type']
  108. .' for share id '.$linkItem['id'], \OCP\Util::ERROR);
  109. return false;
  110. }
  111. }
  112. else {
  113. // not authenticated ?
  114. if ( ! \OC::$server->getSession()->exists('public_link_authenticated')
  115. || \OC::$server->getSession()->get('public_link_authenticated') !== $linkItem['id']) {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. public static function getSharesFromItem($target) {
  122. $result = array();
  123. $owner = \OC\Files\Filesystem::getOwner($target);
  124. \OC\Files\Filesystem::initMountPoints($owner);
  125. $info = \OC\Files\Filesystem::getFileInfo($target);
  126. $ownerView = new \OC\Files\View('/'.$owner.'/files');
  127. if ( $owner != \OCP\User::getUser() ) {
  128. $path = $ownerView->getPath($info['fileid']);
  129. } else {
  130. $path = $target;
  131. }
  132. $ids = array();
  133. while ($path !== dirname($path)) {
  134. $info = $ownerView->getFileInfo($path);
  135. if ($info instanceof \OC\Files\FileInfo) {
  136. $ids[] = $info['fileid'];
  137. } else {
  138. \OCP\Util::writeLog('sharing', 'No fileinfo available for: ' . $path, \OCP\Util::WARN);
  139. }
  140. $path = dirname($path);
  141. }
  142. if (!empty($ids)) {
  143. $idList = array_chunk($ids, 99, true);
  144. foreach ($idList as $subList) {
  145. $statement = "SELECT `share_with`, `share_type`, `file_target` FROM `*PREFIX*share` WHERE `file_source` IN (" . implode(',', $subList) . ") AND `share_type` IN (0, 1, 2)";
  146. $query = \OCP\DB::prepare($statement);
  147. $r = $query->execute();
  148. $result = array_merge($result, $r->fetchAll());
  149. }
  150. }
  151. return $result;
  152. }
  153. public static function getUidAndFilename($filename) {
  154. $uid = \OC\Files\Filesystem::getOwner($filename);
  155. \OC\Files\Filesystem::initMountPoints($uid);
  156. if ( $uid != \OCP\User::getUser() ) {
  157. $info = \OC\Files\Filesystem::getFileInfo($filename);
  158. $ownerView = new \OC\Files\View('/'.$uid.'/files');
  159. $filename = $ownerView->getPath($info['fileid']);
  160. }
  161. return array($uid, $filename);
  162. }
  163. /**
  164. * Format a path to be relative to the /user/files/ directory
  165. * @param string $path the absolute path
  166. * @return string e.g. turns '/admin/files/test.txt' into 'test.txt'
  167. */
  168. public static function stripUserFilesPath($path) {
  169. $trimmed = ltrim($path, '/');
  170. $split = explode('/', $trimmed);
  171. // it is not a file relative to data/user/files
  172. if (count($split) < 3 || $split[1] !== 'files') {
  173. return false;
  174. }
  175. $sliced = array_slice($split, 2);
  176. $relPath = implode('/', $sliced);
  177. return $relPath;
  178. }
  179. /**
  180. * check if file name already exists and generate unique target
  181. *
  182. * @param string $path
  183. * @param array $excludeList
  184. * @param \OC\Files\View $view
  185. * @return string $path
  186. */
  187. public static function generateUniqueTarget($path, $excludeList, $view) {
  188. $pathinfo = pathinfo($path);
  189. $ext = (isset($pathinfo['extension'])) ? '.'.$pathinfo['extension'] : '';
  190. $name = $pathinfo['filename'];
  191. $dir = $pathinfo['dirname'];
  192. $i = 2;
  193. while ($view->file_exists($path) || in_array($path, $excludeList)) {
  194. $path = \OC\Files\Filesystem::normalizePath($dir . '/' . $name . ' ('.$i.')' . $ext);
  195. $i++;
  196. }
  197. return $path;
  198. }
  199. /**
  200. * allow users from other ownCloud instances to mount public links share by this instance
  201. * @return bool
  202. */
  203. public static function isOutgoingServer2serverShareEnabled() {
  204. $appConfig = \OC::$server->getAppConfig();
  205. $result = $appConfig->getValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes');
  206. return ($result === 'yes') ? true : false;
  207. }
  208. /**
  209. * allow user to mount public links from onther ownClouds
  210. * @return bool
  211. */
  212. public static function isIncomingServer2serverShareEnabled() {
  213. $appConfig = \OC::$server->getAppConfig();
  214. $result = $appConfig->getValue('files_sharing', 'incoming_server2server_share_enabled', 'yes');
  215. return ($result === 'yes') ? true : false;
  216. }
  217. /**
  218. * get default share folder
  219. *
  220. * @return string
  221. */
  222. public static function getShareFolder() {
  223. $shareFolder = \OCP\Config::getSystemValue('share_folder', '/');
  224. return \OC\Files\Filesystem::normalizePath($shareFolder);
  225. }
  226. /**
  227. * set default share folder
  228. *
  229. * @param string $shareFolder
  230. */
  231. public static function setShareFolder($shareFolder) {
  232. \OCP\Config::setSystemValue('share_folder', $shareFolder);
  233. }
  234. }