1<?php
2/**
3 * @author Bart Visscher <bartv@thisnet.nl>
4 * @author Björn Schießle <bjoern@schiessle.org>
5 * @author Joas Schilling <coding@schilljs.com>
6 * @author Michael Gapczynski <GapczynskiM@gmail.com>
7 * @author Morris Jobke <hey@morrisjobke.de>
8 * @author Robin McCorkell <robin@mccorkell.me.uk>
9 * @author Roeland Jago Douma <rullzer@owncloud.com>
10 * @author Thomas Müller <thomas.mueller@tmit.eu>
11 *
12 * @copyright Copyright (c) 2018, ownCloud GmbH
13 * @license AGPL-3.0
14 *
15 * This code is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Affero General Public License, version 3,
17 * as published by the Free Software Foundation.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Affero General Public License for more details.
23 *
24 * You should have received a copy of the GNU Affero General Public License, version 3,
25 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26 *
27 */
28
29namespace OCA\Files_Sharing\ShareBackend;
30
31class Folder extends File implements \OCP\Share_Backend_Collection {
32
33	/**
34	 * get shared parents
35	 *
36	 * @param int $itemSource item source ID
37	 * @param string $shareWith with whom should the item be shared
38	 * @param string $owner owner of the item
39	 * @return array with shares
40	 */
41	public function getParents($itemSource, $shareWith = null, $owner = null) {
42		$result = [];
43		$parent = $this->getParentId($itemSource);
44		while ($parent) {
45			$shares = \OCP\Share::getItemSharedWithUser('folder', $parent, $shareWith, $owner);
46			if ($shares) {
47				foreach ($shares as $share) {
48					$name = \basename($share['path']);
49					$share['collection']['path'] = $name;
50					$share['collection']['item_type'] = 'folder';
51					$share['file_path'] = $name;
52					$displayNameOwner = \OCP\User::getDisplayName($share['uid_owner']);
53					$displayNameShareWith = \OCP\User::getDisplayName($share['share_with']);
54					$share['displayname_owner'] = ($displayNameOwner) ? $displayNameOwner : $share['uid_owner'];
55					$share['share_with_displayname'] = ($displayNameShareWith) ? $displayNameShareWith : $share['uid_owner'];
56
57					$result[] = $share;
58				}
59			}
60			$parent = $this->getParentId($parent);
61		}
62
63		return $result;
64	}
65
66	/**
67	 * get file cache ID of parent
68	 *
69	 * @param int $child file cache ID of child
70	 * @return mixed parent ID or null
71	 */
72	private function getParentId($child) {
73		$query = \OCP\DB::prepare('SELECT `parent` FROM `*PREFIX*filecache` WHERE `fileid` = ?');
74		$result = $query->execute([$child]);
75		$row = $result->fetchRow();
76		$parent = ($row) ? $row['parent'] : null;
77
78		return $parent;
79	}
80
81	public function getChildren($itemSource) {
82		$children = [];
83		$parents = [$itemSource];
84		$query = \OCP\DB::prepare('SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = ?');
85		$result = $query->execute(['httpd/unix-directory']);
86		if ($row = $result->fetchRow()) {
87			$mimetype = $row['id'];
88		} else {
89			$mimetype = -1;
90		}
91		while (!empty($parents)) {
92			$parents = "'".\implode("','", $parents)."'";
93			$query = \OCP\DB::prepare('SELECT `fileid`, `name`, `mimetype` FROM `*PREFIX*filecache`'
94				.' WHERE `parent` IN ('.$parents.')');
95			$result = $query->execute();
96			$parents = [];
97			while ($file = $result->fetchRow()) {
98				$children[] = ['source' => $file['fileid'], 'file_path' => $file['name']];
99				// If a child folder is found look inside it
100				if ($file['mimetype'] == $mimetype) {
101					$parents[] = $file['fileid'];
102				}
103			}
104		}
105		return $children;
106	}
107}
108