Failed Conditions
Pull Request — master (#55)
by Sander
02:10
created

lib/ShareBackend/NextNoteShareBackend.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Nextcloud - NextNote
4
 *
5
 * @copyright Copyright (c) 2015, Ben Curtis <[email protected]>
6
 * @copyright Copyright (c) 2017, Sander Brand ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\NextNote\ShareBackend;
25
26
use OC\Share\Share;
27
use OCA\NextNote\Db\NextNote;
28
use OCA\NextNote\Fixtures\ShareFix;
29
use \OCP\Share_Backend;
30
31
class NextNoteShareBackend implements Share_Backend {
32
33
	private $db;
34
	private static $defaultPermissions = 31;
35
	public function __construct() {
36
		$this->db = \OC::$server->getDatabaseConnection();
37
	}
38
39
	public function getSharedNotes(){
40
		return Share::getItemsSharedWith('nextnote', 'populated_shares');
41
	}
42
43
	/**
44
	 * Check if this $itemSource exist for the user
45
	 * @param string $itemSource
46
	 * @param string $uidOwner Owner of the item
47
	 * @return boolean|null Source
48
	 *
49
	 * Return false if the item does not exist for the user
50
	 * @since 5.0.0
51
	 */
52
	public function isValidSource($itemSource, $uidOwner) {
53
		// todo: real test
54
		// id => 1, has admin 
55
		// has owner this note?
56
		return true;
57
	}
58
	
59
	/**
60
	 * Get a unique name of the item for the specified user
61
	 * @param string $itemSource
62
	 * @param string|false $shareWith User the item is being shared with
63
	 * @param array|null $exclude List of similar item names already existing as shared items @deprecated since version OC7
64
	 * @return string Target name
65
	 *
66
	 * This function needs to verify that the user does not already have an item with this name.
67
	 * If it does generate a new name e.g. name_#
68
	 * @since 5.0.0
69
	 */
70
	public function generateTarget($itemSource, $shareWith, $exclude = null) {
71
		// note id (should be unique)
72
		return $itemSource;
73
	}
74
75
	/**
76
	 * Converts the shared item sources back into the item in the specified format
77
	 * @param array $items Shared items
78
	 * @param int $format
79
	 * @return array
80
	 *
81
	 * The items array is a 3-dimensional array with the item_source as the
82
	 * first key and the share id as the second key to an array with the share
83
	 * info.
84
	 *
85
	 * The key/value pairs included in the share info depend on the function originally called:
86
	 * If called by getItem(s)Shared: id, item_type, item, item_source,
87
	 * share_type, share_with, permissions, stime, file_source
88
	 *
89
	 * If called by getItem(s)SharedWith: id, item_type, item, item_source,
90
	 * item_target, share_type, share_with, permissions, stime, file_source,
91
	 * file_target
92
	 *
93
	 * This function allows the backend to control the output of shared items with custom formats.
94
	 * It is only called through calls to the public getItem(s)Shared(With) functions.
95
	 * @since 5.0.0
96
	 */
97
	public function formatItems($items, $format, $parameters = null) {
98
		if ($format === 'shares') {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $format (integer) and 'shares' (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
99
			return $items;
100
		}
101
102
		// get the ownnote ids
103
		$ids = Array();
104
		foreach($items as $item) {
105
			$ids[] = $item['item_source'];
106
		}
107
108
		// get notes from database
109
		$select_clause = "SELECT id, uid, name, grouping, mtime, deleted FROM *PREFIX*nextnote WHERE id in (";
110
		$select_clause .= implode(',', $ids);
111
		$select_clause .= ") ORDER BY id";
112
		$q = $this->db->executeQuery($select_clause, array());
113
		//$query = \OCP\DB::prepare($select_clause);
114
		$results = $q->fetchAll();
115
116
		// add permissions to items
117
		if ($format === 'populated_shares') {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $format (integer) and 'populated_shares' (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
118
			$full_items = Array();
119
			foreach($results as $index => $result) {
120
				$full_items[] = array_merge($items[$index], $result);
121
			}
122
			$results = $full_items;
123
		}
124
125
		return $results;
126
	}
127
	
128
	/**
129
	 * Check if a given share type is allowd by the back-end
130
	 *
131
	 * @param int $shareType share type
132
	 * @return boolean
133
	 *
134
	 * The back-end can enable/disable specific share types. Just return true if
135
	 * the back-end doesn't provide any specific settings for it and want to allow
136
	 * all share types defined by the share API
137
	 * @since 8.0.0
138
	 */
139
	public function isShareTypeAllowed($shareType) {
140
		return true;
141
	}
142
143
	/**
144
	 * Check if the current user has the requested permission.
145
	 * For permissions
146
	 * @see \OCP\Constants
147
	 * @param $permission
148
	 * @param $note NextNote
149
	 * @return bool|int
150
	 */
151
	public function checkPermissions($permission, $note) {
152
		// gather information
153
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
154
		// owner is allowed to do everything
155
		if ($uid === $note->getUid()) {
156
			return true;
157
		}
158
159
		// check share permissions
160
		$gotPerm = ShareFix::getPermissions('nextnote', $note->getId(), $uid);
161
162
		return $gotPerm & $permission;
163
	}
164
}