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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Files_Sharing;
  30. use OC\Files\Filesystem;
  31. use OC\Files\View;
  32. use OCP\Files\NotFoundException;
  33. use OCP\Share\Exceptions\ShareNotFound;
  34. use OCP\User;
  35. class Helper {
  36. public static function registerHooks() {
  37. \OCP\Util::connectHook('OC_Filesystem', 'post_rename', '\OCA\Files_Sharing\Updater', 'renameHook');
  38. \OCP\Util::connectHook('OC_Filesystem', 'post_delete', '\OCA\Files_Sharing\Hooks', 'unshareChildren');
  39. \OCP\Util::connectHook('OC_User', 'post_deleteUser', '\OCA\Files_Sharing\Hooks', 'deleteUser');
  40. }
  41. /**
  42. * Sets up the filesystem and user for public sharing
  43. * @param string $token string share token
  44. * @param string $relativePath optional path relative to the share
  45. * @param string $password optional password
  46. * @return array
  47. */
  48. public static function setupFromToken($token, $relativePath = null, $password = null) {
  49. \OC_User::setIncognitoMode(true);
  50. $shareManager = \OC::$server->getShareManager();
  51. try {
  52. $share = $shareManager->getShareByToken($token);
  53. } catch (ShareNotFound $e) {
  54. \OC_Response::setStatus(404);
  55. \OCP\Util::writeLog('core-preview', 'Passed token parameter is not valid', \OCP\Util::DEBUG);
  56. exit;
  57. }
  58. \OCP\JSON::checkUserExists($share->getShareOwner());
  59. \OC_Util::tearDownFS();
  60. \OC_Util::setupFS($share->getShareOwner());
  61. try {
  62. $path = Filesystem::getPath($share->getNodeId());
  63. } catch (NotFoundException $e) {
  64. \OCP\Util::writeLog('share', 'could not resolve linkItem', \OCP\Util::DEBUG);
  65. \OC_Response::setStatus(404);
  66. \OCP\JSON::error(array('success' => false));
  67. exit();
  68. }
  69. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK && $share->getPassword() !== null) {
  70. if (!self::authenticate($share, $password)) {
  71. \OC_Response::setStatus(403);
  72. \OCP\JSON::error(array('success' => false));
  73. exit();
  74. }
  75. }
  76. $basePath = $path;
  77. if ($relativePath !== null && Filesystem::isReadable($basePath . $relativePath)) {
  78. $path .= Filesystem::normalizePath($relativePath);
  79. }
  80. return array(
  81. 'share' => $share,
  82. 'basePath' => $basePath,
  83. 'realPath' => $path
  84. );
  85. }
  86. /**
  87. * Authenticate link item with the given password
  88. * or with the session if no password was given.
  89. * @param \OCP\Share\IShare $share
  90. * @param string $password optional password
  91. *
  92. * @return boolean true if authorized, false otherwise
  93. */
  94. public static function authenticate($share, $password = null) {
  95. $shareManager = \OC::$server->getShareManager();
  96. if ($password !== null) {
  97. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
  98. if ($shareManager->checkPassword($share, $password)) {
  99. \OC::$server->getSession()->set('public_link_authenticated', (string)$share->getId());
  100. return true;
  101. }
  102. }
  103. } else {
  104. // not authenticated ?
  105. if (\OC::$server->getSession()->exists('public_link_authenticated')
  106. && \OC::$server->getSession()->get('public_link_authenticated') !== (string)$share->getId()) {
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. public static function getSharesFromItem($target) {
  113. $result = array();
  114. $owner = Filesystem::getOwner($target);
  115. Filesystem::initMountPoints($owner);
  116. $info = Filesystem::getFileInfo($target);
  117. $ownerView = new View('/'.$owner.'/files');
  118. if ( $owner !== User::getUser() ) {
  119. $path = $ownerView->getPath($info['fileid']);
  120. } else {
  121. $path = $target;
  122. }
  123. $ids = array();
  124. while ($path !== dirname($path)) {
  125. $info = $ownerView->getFileInfo($path);
  126. if ($info instanceof \OC\Files\FileInfo) {
  127. $ids[] = $info['fileid'];
  128. } else {
  129. \OCP\Util::writeLog('sharing', 'No fileinfo available for: ' . $path, \OCP\Util::WARN);
  130. }
  131. $path = dirname($path);
  132. }
  133. if (!empty($ids)) {
  134. $idList = array_chunk($ids, 99, true);
  135. foreach ($idList as $subList) {
  136. $statement = "SELECT `share_with`, `share_type`, `file_target` FROM `*PREFIX*share` WHERE `file_source` IN (" . implode(',', $subList) . ") AND `share_type` IN (0, 1, 2)";
  137. $query = \OCP\DB::prepare($statement);
  138. $r = $query->execute();
  139. $result = array_merge($result, $r->fetchAll());
  140. }
  141. }
  142. return $result;
  143. }
  144. /**
  145. * get the UID of the owner of the file and the path to the file relative to
  146. * owners files folder
  147. *
  148. * @param $filename
  149. * @return array
  150. * @throws \OC\User\NoUserException
  151. */
  152. public static function getUidAndFilename($filename) {
  153. $uid = Filesystem::getOwner($filename);
  154. $userManager = \OC::$server->getUserManager();
  155. // if the user with the UID doesn't exists, e.g. because the UID points
  156. // to a remote user with a federated cloud ID we use the current logged-in
  157. // user. We need a valid local user to create the share
  158. if (!$userManager->userExists($uid)) {
  159. $uid = User::getUser();
  160. }
  161. Filesystem::initMountPoints($uid);
  162. if ( $uid !== User::getUser() ) {
  163. $info = Filesystem::getFileInfo($filename);
  164. $ownerView = new View('/'.$uid.'/files');
  165. try {
  166. $filename = $ownerView->getPath($info['fileid']);
  167. } catch (NotFoundException $e) {
  168. $filename = null;
  169. }
  170. }
  171. return [$uid, $filename];
  172. }
  173. /**
  174. * Format a path to be relative to the /user/files/ directory
  175. * @param string $path the absolute path
  176. * @return string e.g. turns '/admin/files/test.txt' into 'test.txt'
  177. */
  178. public static function stripUserFilesPath($path) {
  179. $trimmed = ltrim($path, '/');
  180. $split = explode('/', $trimmed);
  181. // it is not a file relative to data/user/files
  182. if (count($split) < 3 || $split[1] !== 'files') {
  183. return false;
  184. }
  185. $sliced = array_slice($split, 2);
  186. $relPath = implode('/', $sliced);
  187. return $relPath;
  188. }
  189. /**
  190. * check if file name already exists and generate unique target
  191. *
  192. * @param string $path
  193. * @param array $excludeList
  194. * @param View $view
  195. * @return string $path
  196. */
  197. public static function generateUniqueTarget($path, $excludeList, $view) {
  198. $pathinfo = pathinfo($path);
  199. $ext = (isset($pathinfo['extension'])) ? '.'.$pathinfo['extension'] : '';
  200. $name = $pathinfo['filename'];
  201. $dir = $pathinfo['dirname'];
  202. $i = 2;
  203. while ($view->file_exists($path) || in_array($path, $excludeList)) {
  204. $path = Filesystem::normalizePath($dir . '/' . $name . ' ('.$i.')' . $ext);
  205. $i++;
  206. }
  207. return $path;
  208. }
  209. /**
  210. * get default share folder
  211. *
  212. * @param \OC\Files\View
  213. * @return string
  214. */
  215. public static function getShareFolder($view = null) {
  216. if ($view === null) {
  217. $view = Filesystem::getView();
  218. }
  219. $shareFolder = \OC::$server->getConfig()->getSystemValue('share_folder', '/');
  220. $shareFolder = Filesystem::normalizePath($shareFolder);
  221. if (!$view->file_exists($shareFolder)) {
  222. $dir = '';
  223. $subdirs = explode('/', $shareFolder);
  224. foreach ($subdirs as $subdir) {
  225. $dir = $dir . '/' . $subdir;
  226. if (!$view->is_dir($dir)) {
  227. $view->mkdir($dir);
  228. }
  229. }
  230. }
  231. return $shareFolder;
  232. }
  233. /**
  234. * set default share folder
  235. *
  236. * @param string $shareFolder
  237. */
  238. public static function setShareFolder($shareFolder) {
  239. \OC::$server->getConfig()->setSystemValue('share_folder', $shareFolder);
  240. }
  241. }