@@ -126,6 +126,9 @@ |
||
126 | 126 | return 's' . str_pad($lastNumber + 1, 2, '0', STR_PAD_LEFT); |
127 | 127 | } |
128 | 128 | |
129 | + /** |
|
130 | + * @param string $value |
|
131 | + */ |
|
129 | 132 | private function getServersConfig($value) { |
130 | 133 | $regex = '/' . $value . '$/S'; |
131 | 134 |
@@ -37,125 +37,125 @@ discard block |
||
37 | 37 | |
38 | 38 | class Helper { |
39 | 39 | |
40 | - /** @var IConfig */ |
|
41 | - private $config; |
|
42 | - |
|
43 | - /** |
|
44 | - * Helper constructor. |
|
45 | - * |
|
46 | - * @param IConfig $config |
|
47 | - */ |
|
48 | - public function __construct(IConfig $config) { |
|
49 | - $this->config = $config; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * returns prefixes for each saved LDAP/AD server configuration. |
|
54 | - * @param bool $activeConfigurations optional, whether only active configuration shall be |
|
55 | - * retrieved, defaults to false |
|
56 | - * @return array with a list of the available prefixes |
|
57 | - * |
|
58 | - * Configuration prefixes are used to set up configurations for n LDAP or |
|
59 | - * AD servers. Since configuration is stored in the database, table |
|
60 | - * appconfig under appid user_ldap, the common identifiers in column |
|
61 | - * 'configkey' have a prefix. The prefix for the very first server |
|
62 | - * configuration is empty. |
|
63 | - * Configkey Examples: |
|
64 | - * Server 1: ldap_login_filter |
|
65 | - * Server 2: s1_ldap_login_filter |
|
66 | - * Server 3: s2_ldap_login_filter |
|
67 | - * |
|
68 | - * The prefix needs to be passed to the constructor of Connection class, |
|
69 | - * except the default (first) server shall be connected to. |
|
70 | - * |
|
71 | - */ |
|
72 | - public function getServerConfigurationPrefixes($activeConfigurations = false) { |
|
73 | - $referenceConfigkey = 'ldap_configuration_active'; |
|
74 | - |
|
75 | - $keys = $this->getServersConfig($referenceConfigkey); |
|
76 | - |
|
77 | - $prefixes = []; |
|
78 | - foreach ($keys as $key) { |
|
79 | - if ($activeConfigurations && $this->config->getAppValue('user_ldap', $key, '0') !== '1') { |
|
80 | - continue; |
|
81 | - } |
|
82 | - |
|
83 | - $len = strlen($key) - strlen($referenceConfigkey); |
|
84 | - $prefixes[] = substr($key, 0, $len); |
|
85 | - } |
|
86 | - |
|
87 | - return $prefixes; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * |
|
92 | - * determines the host for every configured connection |
|
93 | - * @return array an array with configprefix as keys |
|
94 | - * |
|
95 | - */ |
|
96 | - public function getServerConfigurationHosts() { |
|
97 | - $referenceConfigkey = 'ldap_host'; |
|
98 | - |
|
99 | - $keys = $this->getServersConfig($referenceConfigkey); |
|
100 | - |
|
101 | - $result = array(); |
|
102 | - foreach($keys as $key) { |
|
103 | - $len = strlen($key) - strlen($referenceConfigkey); |
|
104 | - $prefix = substr($key, 0, $len); |
|
105 | - $result[$prefix] = $this->config->getAppValue('user_ldap', $key); |
|
106 | - } |
|
107 | - |
|
108 | - return $result; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * return the next available configuration prefix |
|
113 | - * |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - public function getNextServerConfigurationPrefix() { |
|
117 | - $serverConnections = $this->getServerConfigurationPrefixes(); |
|
118 | - |
|
119 | - if(count($serverConnections) === 0) { |
|
120 | - return 's01'; |
|
121 | - } |
|
122 | - |
|
123 | - sort($serverConnections); |
|
124 | - $lastKey = array_pop($serverConnections); |
|
125 | - $lastNumber = intval(str_replace('s', '', $lastKey)); |
|
126 | - return 's' . str_pad($lastNumber + 1, 2, '0', STR_PAD_LEFT); |
|
127 | - } |
|
128 | - |
|
129 | - private function getServersConfig($value) { |
|
130 | - $regex = '/' . $value . '$/S'; |
|
131 | - |
|
132 | - $keys = $this->config->getAppKeys('user_ldap'); |
|
133 | - $result = []; |
|
134 | - foreach ($keys as $key) { |
|
135 | - if (preg_match($regex, $key) === 1) { |
|
136 | - $result[] = $key; |
|
137 | - } |
|
138 | - } |
|
139 | - |
|
140 | - return $result; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * deletes a given saved LDAP/AD server configuration. |
|
145 | - * @param string $prefix the configuration prefix of the config to delete |
|
146 | - * @return bool true on success, false otherwise |
|
147 | - */ |
|
148 | - public function deleteServerConfiguration($prefix) { |
|
149 | - if(!in_array($prefix, self::getServerConfigurationPrefixes())) { |
|
150 | - return false; |
|
151 | - } |
|
152 | - |
|
153 | - $saveOtherConfigurations = ''; |
|
154 | - if(empty($prefix)) { |
|
155 | - $saveOtherConfigurations = 'AND `configkey` NOT LIKE \'s%\''; |
|
156 | - } |
|
157 | - |
|
158 | - $query = \OCP\DB::prepare(' |
|
40 | + /** @var IConfig */ |
|
41 | + private $config; |
|
42 | + |
|
43 | + /** |
|
44 | + * Helper constructor. |
|
45 | + * |
|
46 | + * @param IConfig $config |
|
47 | + */ |
|
48 | + public function __construct(IConfig $config) { |
|
49 | + $this->config = $config; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * returns prefixes for each saved LDAP/AD server configuration. |
|
54 | + * @param bool $activeConfigurations optional, whether only active configuration shall be |
|
55 | + * retrieved, defaults to false |
|
56 | + * @return array with a list of the available prefixes |
|
57 | + * |
|
58 | + * Configuration prefixes are used to set up configurations for n LDAP or |
|
59 | + * AD servers. Since configuration is stored in the database, table |
|
60 | + * appconfig under appid user_ldap, the common identifiers in column |
|
61 | + * 'configkey' have a prefix. The prefix for the very first server |
|
62 | + * configuration is empty. |
|
63 | + * Configkey Examples: |
|
64 | + * Server 1: ldap_login_filter |
|
65 | + * Server 2: s1_ldap_login_filter |
|
66 | + * Server 3: s2_ldap_login_filter |
|
67 | + * |
|
68 | + * The prefix needs to be passed to the constructor of Connection class, |
|
69 | + * except the default (first) server shall be connected to. |
|
70 | + * |
|
71 | + */ |
|
72 | + public function getServerConfigurationPrefixes($activeConfigurations = false) { |
|
73 | + $referenceConfigkey = 'ldap_configuration_active'; |
|
74 | + |
|
75 | + $keys = $this->getServersConfig($referenceConfigkey); |
|
76 | + |
|
77 | + $prefixes = []; |
|
78 | + foreach ($keys as $key) { |
|
79 | + if ($activeConfigurations && $this->config->getAppValue('user_ldap', $key, '0') !== '1') { |
|
80 | + continue; |
|
81 | + } |
|
82 | + |
|
83 | + $len = strlen($key) - strlen($referenceConfigkey); |
|
84 | + $prefixes[] = substr($key, 0, $len); |
|
85 | + } |
|
86 | + |
|
87 | + return $prefixes; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * |
|
92 | + * determines the host for every configured connection |
|
93 | + * @return array an array with configprefix as keys |
|
94 | + * |
|
95 | + */ |
|
96 | + public function getServerConfigurationHosts() { |
|
97 | + $referenceConfigkey = 'ldap_host'; |
|
98 | + |
|
99 | + $keys = $this->getServersConfig($referenceConfigkey); |
|
100 | + |
|
101 | + $result = array(); |
|
102 | + foreach($keys as $key) { |
|
103 | + $len = strlen($key) - strlen($referenceConfigkey); |
|
104 | + $prefix = substr($key, 0, $len); |
|
105 | + $result[$prefix] = $this->config->getAppValue('user_ldap', $key); |
|
106 | + } |
|
107 | + |
|
108 | + return $result; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * return the next available configuration prefix |
|
113 | + * |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + public function getNextServerConfigurationPrefix() { |
|
117 | + $serverConnections = $this->getServerConfigurationPrefixes(); |
|
118 | + |
|
119 | + if(count($serverConnections) === 0) { |
|
120 | + return 's01'; |
|
121 | + } |
|
122 | + |
|
123 | + sort($serverConnections); |
|
124 | + $lastKey = array_pop($serverConnections); |
|
125 | + $lastNumber = intval(str_replace('s', '', $lastKey)); |
|
126 | + return 's' . str_pad($lastNumber + 1, 2, '0', STR_PAD_LEFT); |
|
127 | + } |
|
128 | + |
|
129 | + private function getServersConfig($value) { |
|
130 | + $regex = '/' . $value . '$/S'; |
|
131 | + |
|
132 | + $keys = $this->config->getAppKeys('user_ldap'); |
|
133 | + $result = []; |
|
134 | + foreach ($keys as $key) { |
|
135 | + if (preg_match($regex, $key) === 1) { |
|
136 | + $result[] = $key; |
|
137 | + } |
|
138 | + } |
|
139 | + |
|
140 | + return $result; |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * deletes a given saved LDAP/AD server configuration. |
|
145 | + * @param string $prefix the configuration prefix of the config to delete |
|
146 | + * @return bool true on success, false otherwise |
|
147 | + */ |
|
148 | + public function deleteServerConfiguration($prefix) { |
|
149 | + if(!in_array($prefix, self::getServerConfigurationPrefixes())) { |
|
150 | + return false; |
|
151 | + } |
|
152 | + |
|
153 | + $saveOtherConfigurations = ''; |
|
154 | + if(empty($prefix)) { |
|
155 | + $saveOtherConfigurations = 'AND `configkey` NOT LIKE \'s%\''; |
|
156 | + } |
|
157 | + |
|
158 | + $query = \OCP\DB::prepare(' |
|
159 | 159 | DELETE |
160 | 160 | FROM `*PREFIX*appconfig` |
161 | 161 | WHERE `configkey` LIKE ? |
@@ -163,149 +163,149 @@ discard block |
||
163 | 163 | AND `appid` = \'user_ldap\' |
164 | 164 | AND `configkey` NOT IN (\'enabled\', \'installed_version\', \'types\', \'bgjUpdateGroupsLastRun\') |
165 | 165 | '); |
166 | - $delRows = $query->execute(array($prefix.'%')); |
|
167 | - |
|
168 | - if(\OCP\DB::isError($delRows)) { |
|
169 | - return false; |
|
170 | - } |
|
171 | - |
|
172 | - if($delRows === 0) { |
|
173 | - return false; |
|
174 | - } |
|
175 | - |
|
176 | - return true; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * checks whether there is one or more disabled LDAP configurations |
|
181 | - * @throws \Exception |
|
182 | - * @return bool |
|
183 | - */ |
|
184 | - public function haveDisabledConfigurations() { |
|
185 | - $all = $this->getServerConfigurationPrefixes(false); |
|
186 | - $active = $this->getServerConfigurationPrefixes(true); |
|
187 | - |
|
188 | - if(!is_array($all) || !is_array($active)) { |
|
189 | - throw new \Exception('Unexpected Return Value'); |
|
190 | - } |
|
191 | - |
|
192 | - return count($all) !== count($active) || count($all) === 0; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * extracts the domain from a given URL |
|
197 | - * @param string $url the URL |
|
198 | - * @return string|false domain as string on success, false otherwise |
|
199 | - */ |
|
200 | - public function getDomainFromURL($url) { |
|
201 | - $uinfo = parse_url($url); |
|
202 | - if(!is_array($uinfo)) { |
|
203 | - return false; |
|
204 | - } |
|
205 | - |
|
206 | - $domain = false; |
|
207 | - if(isset($uinfo['host'])) { |
|
208 | - $domain = $uinfo['host']; |
|
209 | - } else if(isset($uinfo['path'])) { |
|
210 | - $domain = $uinfo['path']; |
|
211 | - } |
|
212 | - |
|
213 | - return $domain; |
|
214 | - } |
|
166 | + $delRows = $query->execute(array($prefix.'%')); |
|
167 | + |
|
168 | + if(\OCP\DB::isError($delRows)) { |
|
169 | + return false; |
|
170 | + } |
|
171 | + |
|
172 | + if($delRows === 0) { |
|
173 | + return false; |
|
174 | + } |
|
175 | + |
|
176 | + return true; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * checks whether there is one or more disabled LDAP configurations |
|
181 | + * @throws \Exception |
|
182 | + * @return bool |
|
183 | + */ |
|
184 | + public function haveDisabledConfigurations() { |
|
185 | + $all = $this->getServerConfigurationPrefixes(false); |
|
186 | + $active = $this->getServerConfigurationPrefixes(true); |
|
187 | + |
|
188 | + if(!is_array($all) || !is_array($active)) { |
|
189 | + throw new \Exception('Unexpected Return Value'); |
|
190 | + } |
|
191 | + |
|
192 | + return count($all) !== count($active) || count($all) === 0; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * extracts the domain from a given URL |
|
197 | + * @param string $url the URL |
|
198 | + * @return string|false domain as string on success, false otherwise |
|
199 | + */ |
|
200 | + public function getDomainFromURL($url) { |
|
201 | + $uinfo = parse_url($url); |
|
202 | + if(!is_array($uinfo)) { |
|
203 | + return false; |
|
204 | + } |
|
205 | + |
|
206 | + $domain = false; |
|
207 | + if(isset($uinfo['host'])) { |
|
208 | + $domain = $uinfo['host']; |
|
209 | + } else if(isset($uinfo['path'])) { |
|
210 | + $domain = $uinfo['path']; |
|
211 | + } |
|
212 | + |
|
213 | + return $domain; |
|
214 | + } |
|
215 | 215 | |
216 | - /** |
|
217 | - * |
|
218 | - * Set the LDAPProvider in the config |
|
219 | - * |
|
220 | - */ |
|
221 | - public function setLDAPProvider() { |
|
222 | - $current = \OC::$server->getConfig()->getSystemValue('ldapProviderFactory', null); |
|
223 | - if(is_null($current)) { |
|
224 | - \OC::$server->getConfig()->setSystemValue('ldapProviderFactory', '\\OCA\\User_LDAP\\LDAPProviderFactory'); |
|
225 | - } |
|
226 | - } |
|
216 | + /** |
|
217 | + * |
|
218 | + * Set the LDAPProvider in the config |
|
219 | + * |
|
220 | + */ |
|
221 | + public function setLDAPProvider() { |
|
222 | + $current = \OC::$server->getConfig()->getSystemValue('ldapProviderFactory', null); |
|
223 | + if(is_null($current)) { |
|
224 | + \OC::$server->getConfig()->setSystemValue('ldapProviderFactory', '\\OCA\\User_LDAP\\LDAPProviderFactory'); |
|
225 | + } |
|
226 | + } |
|
227 | 227 | |
228 | - /** |
|
229 | - * sanitizes a DN received from the LDAP server |
|
230 | - * @param array $dn the DN in question |
|
231 | - * @return array|string the sanitized DN |
|
232 | - */ |
|
233 | - public function sanitizeDN($dn) { |
|
234 | - //treating multiple base DNs |
|
235 | - if(is_array($dn)) { |
|
236 | - $result = array(); |
|
237 | - foreach($dn as $singleDN) { |
|
238 | - $result[] = $this->sanitizeDN($singleDN); |
|
239 | - } |
|
240 | - return $result; |
|
241 | - } |
|
242 | - |
|
243 | - //OID sometimes gives back DNs with whitespace after the comma |
|
244 | - // a la "uid=foo, cn=bar, dn=..." We need to tackle this! |
|
245 | - $dn = preg_replace('/([^\\\]),(\s+)/u', '\1,', $dn); |
|
246 | - |
|
247 | - //make comparisons and everything work |
|
248 | - $dn = mb_strtolower($dn, 'UTF-8'); |
|
249 | - |
|
250 | - //escape DN values according to RFC 2253 – this is already done by ldap_explode_dn |
|
251 | - //to use the DN in search filters, \ needs to be escaped to \5c additionally |
|
252 | - //to use them in bases, we convert them back to simple backslashes in readAttribute() |
|
253 | - $replacements = array( |
|
254 | - '\,' => '\5c2C', |
|
255 | - '\=' => '\5c3D', |
|
256 | - '\+' => '\5c2B', |
|
257 | - '\<' => '\5c3C', |
|
258 | - '\>' => '\5c3E', |
|
259 | - '\;' => '\5c3B', |
|
260 | - '\"' => '\5c22', |
|
261 | - '\#' => '\5c23', |
|
262 | - '(' => '\28', |
|
263 | - ')' => '\29', |
|
264 | - '*' => '\2A', |
|
265 | - ); |
|
266 | - $dn = str_replace(array_keys($replacements), array_values($replacements), $dn); |
|
267 | - |
|
268 | - return $dn; |
|
269 | - } |
|
228 | + /** |
|
229 | + * sanitizes a DN received from the LDAP server |
|
230 | + * @param array $dn the DN in question |
|
231 | + * @return array|string the sanitized DN |
|
232 | + */ |
|
233 | + public function sanitizeDN($dn) { |
|
234 | + //treating multiple base DNs |
|
235 | + if(is_array($dn)) { |
|
236 | + $result = array(); |
|
237 | + foreach($dn as $singleDN) { |
|
238 | + $result[] = $this->sanitizeDN($singleDN); |
|
239 | + } |
|
240 | + return $result; |
|
241 | + } |
|
242 | + |
|
243 | + //OID sometimes gives back DNs with whitespace after the comma |
|
244 | + // a la "uid=foo, cn=bar, dn=..." We need to tackle this! |
|
245 | + $dn = preg_replace('/([^\\\]),(\s+)/u', '\1,', $dn); |
|
246 | + |
|
247 | + //make comparisons and everything work |
|
248 | + $dn = mb_strtolower($dn, 'UTF-8'); |
|
249 | + |
|
250 | + //escape DN values according to RFC 2253 – this is already done by ldap_explode_dn |
|
251 | + //to use the DN in search filters, \ needs to be escaped to \5c additionally |
|
252 | + //to use them in bases, we convert them back to simple backslashes in readAttribute() |
|
253 | + $replacements = array( |
|
254 | + '\,' => '\5c2C', |
|
255 | + '\=' => '\5c3D', |
|
256 | + '\+' => '\5c2B', |
|
257 | + '\<' => '\5c3C', |
|
258 | + '\>' => '\5c3E', |
|
259 | + '\;' => '\5c3B', |
|
260 | + '\"' => '\5c22', |
|
261 | + '\#' => '\5c23', |
|
262 | + '(' => '\28', |
|
263 | + ')' => '\29', |
|
264 | + '*' => '\2A', |
|
265 | + ); |
|
266 | + $dn = str_replace(array_keys($replacements), array_values($replacements), $dn); |
|
267 | + |
|
268 | + return $dn; |
|
269 | + } |
|
270 | 270 | |
271 | - /** |
|
272 | - * converts a stored DN so it can be used as base parameter for LDAP queries, internally we store them for usage in LDAP filters |
|
273 | - * @param string $dn the DN |
|
274 | - * @return string |
|
275 | - */ |
|
276 | - public function DNasBaseParameter($dn) { |
|
277 | - return str_ireplace('\\5c', '\\', $dn); |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * listens to a hook thrown by server2server sharing and replaces the given |
|
282 | - * login name by a username, if it matches an LDAP user. |
|
283 | - * |
|
284 | - * @param array $param |
|
285 | - * @throws \Exception |
|
286 | - */ |
|
287 | - public static function loginName2UserName($param) { |
|
288 | - if(!isset($param['uid'])) { |
|
289 | - throw new \Exception('key uid is expected to be set in $param'); |
|
290 | - } |
|
291 | - |
|
292 | - //ain't it ironic? |
|
293 | - $helper = new Helper(\OC::$server->getConfig()); |
|
294 | - |
|
295 | - $configPrefixes = $helper->getServerConfigurationPrefixes(true); |
|
296 | - $ldapWrapper = new LDAP(); |
|
297 | - $ocConfig = \OC::$server->getConfig(); |
|
298 | - $notificationManager = \OC::$server->getNotificationManager(); |
|
299 | - |
|
300 | - $userSession = \OC::$server->getUserSession(); |
|
301 | - $userPluginManager = \OC::$server->query('LDAPUserPluginManager'); |
|
302 | - |
|
303 | - $userBackend = new User_Proxy( |
|
304 | - $configPrefixes, $ldapWrapper, $ocConfig, $notificationManager, $userSession, $userPluginManager |
|
305 | - ); |
|
306 | - $uid = $userBackend->loginName2UserName($param['uid'] ); |
|
307 | - if($uid !== false) { |
|
308 | - $param['uid'] = $uid; |
|
309 | - } |
|
310 | - } |
|
271 | + /** |
|
272 | + * converts a stored DN so it can be used as base parameter for LDAP queries, internally we store them for usage in LDAP filters |
|
273 | + * @param string $dn the DN |
|
274 | + * @return string |
|
275 | + */ |
|
276 | + public function DNasBaseParameter($dn) { |
|
277 | + return str_ireplace('\\5c', '\\', $dn); |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * listens to a hook thrown by server2server sharing and replaces the given |
|
282 | + * login name by a username, if it matches an LDAP user. |
|
283 | + * |
|
284 | + * @param array $param |
|
285 | + * @throws \Exception |
|
286 | + */ |
|
287 | + public static function loginName2UserName($param) { |
|
288 | + if(!isset($param['uid'])) { |
|
289 | + throw new \Exception('key uid is expected to be set in $param'); |
|
290 | + } |
|
291 | + |
|
292 | + //ain't it ironic? |
|
293 | + $helper = new Helper(\OC::$server->getConfig()); |
|
294 | + |
|
295 | + $configPrefixes = $helper->getServerConfigurationPrefixes(true); |
|
296 | + $ldapWrapper = new LDAP(); |
|
297 | + $ocConfig = \OC::$server->getConfig(); |
|
298 | + $notificationManager = \OC::$server->getNotificationManager(); |
|
299 | + |
|
300 | + $userSession = \OC::$server->getUserSession(); |
|
301 | + $userPluginManager = \OC::$server->query('LDAPUserPluginManager'); |
|
302 | + |
|
303 | + $userBackend = new User_Proxy( |
|
304 | + $configPrefixes, $ldapWrapper, $ocConfig, $notificationManager, $userSession, $userPluginManager |
|
305 | + ); |
|
306 | + $uid = $userBackend->loginName2UserName($param['uid'] ); |
|
307 | + if($uid !== false) { |
|
308 | + $param['uid'] = $uid; |
|
309 | + } |
|
310 | + } |
|
311 | 311 | } |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | $keys = $this->getServersConfig($referenceConfigkey); |
100 | 100 | |
101 | 101 | $result = array(); |
102 | - foreach($keys as $key) { |
|
102 | + foreach ($keys as $key) { |
|
103 | 103 | $len = strlen($key) - strlen($referenceConfigkey); |
104 | 104 | $prefix = substr($key, 0, $len); |
105 | 105 | $result[$prefix] = $this->config->getAppValue('user_ldap', $key); |
@@ -116,18 +116,18 @@ discard block |
||
116 | 116 | public function getNextServerConfigurationPrefix() { |
117 | 117 | $serverConnections = $this->getServerConfigurationPrefixes(); |
118 | 118 | |
119 | - if(count($serverConnections) === 0) { |
|
119 | + if (count($serverConnections) === 0) { |
|
120 | 120 | return 's01'; |
121 | 121 | } |
122 | 122 | |
123 | 123 | sort($serverConnections); |
124 | 124 | $lastKey = array_pop($serverConnections); |
125 | 125 | $lastNumber = intval(str_replace('s', '', $lastKey)); |
126 | - return 's' . str_pad($lastNumber + 1, 2, '0', STR_PAD_LEFT); |
|
126 | + return 's'.str_pad($lastNumber + 1, 2, '0', STR_PAD_LEFT); |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | private function getServersConfig($value) { |
130 | - $regex = '/' . $value . '$/S'; |
|
130 | + $regex = '/'.$value.'$/S'; |
|
131 | 131 | |
132 | 132 | $keys = $this->config->getAppKeys('user_ldap'); |
133 | 133 | $result = []; |
@@ -146,12 +146,12 @@ discard block |
||
146 | 146 | * @return bool true on success, false otherwise |
147 | 147 | */ |
148 | 148 | public function deleteServerConfiguration($prefix) { |
149 | - if(!in_array($prefix, self::getServerConfigurationPrefixes())) { |
|
149 | + if (!in_array($prefix, self::getServerConfigurationPrefixes())) { |
|
150 | 150 | return false; |
151 | 151 | } |
152 | 152 | |
153 | 153 | $saveOtherConfigurations = ''; |
154 | - if(empty($prefix)) { |
|
154 | + if (empty($prefix)) { |
|
155 | 155 | $saveOtherConfigurations = 'AND `configkey` NOT LIKE \'s%\''; |
156 | 156 | } |
157 | 157 | |
@@ -165,11 +165,11 @@ discard block |
||
165 | 165 | '); |
166 | 166 | $delRows = $query->execute(array($prefix.'%')); |
167 | 167 | |
168 | - if(\OCP\DB::isError($delRows)) { |
|
168 | + if (\OCP\DB::isError($delRows)) { |
|
169 | 169 | return false; |
170 | 170 | } |
171 | 171 | |
172 | - if($delRows === 0) { |
|
172 | + if ($delRows === 0) { |
|
173 | 173 | return false; |
174 | 174 | } |
175 | 175 | |
@@ -185,7 +185,7 @@ discard block |
||
185 | 185 | $all = $this->getServerConfigurationPrefixes(false); |
186 | 186 | $active = $this->getServerConfigurationPrefixes(true); |
187 | 187 | |
188 | - if(!is_array($all) || !is_array($active)) { |
|
188 | + if (!is_array($all) || !is_array($active)) { |
|
189 | 189 | throw new \Exception('Unexpected Return Value'); |
190 | 190 | } |
191 | 191 | |
@@ -199,14 +199,14 @@ discard block |
||
199 | 199 | */ |
200 | 200 | public function getDomainFromURL($url) { |
201 | 201 | $uinfo = parse_url($url); |
202 | - if(!is_array($uinfo)) { |
|
202 | + if (!is_array($uinfo)) { |
|
203 | 203 | return false; |
204 | 204 | } |
205 | 205 | |
206 | 206 | $domain = false; |
207 | - if(isset($uinfo['host'])) { |
|
207 | + if (isset($uinfo['host'])) { |
|
208 | 208 | $domain = $uinfo['host']; |
209 | - } else if(isset($uinfo['path'])) { |
|
209 | + } else if (isset($uinfo['path'])) { |
|
210 | 210 | $domain = $uinfo['path']; |
211 | 211 | } |
212 | 212 | |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | */ |
221 | 221 | public function setLDAPProvider() { |
222 | 222 | $current = \OC::$server->getConfig()->getSystemValue('ldapProviderFactory', null); |
223 | - if(is_null($current)) { |
|
223 | + if (is_null($current)) { |
|
224 | 224 | \OC::$server->getConfig()->setSystemValue('ldapProviderFactory', '\\OCA\\User_LDAP\\LDAPProviderFactory'); |
225 | 225 | } |
226 | 226 | } |
@@ -232,9 +232,9 @@ discard block |
||
232 | 232 | */ |
233 | 233 | public function sanitizeDN($dn) { |
234 | 234 | //treating multiple base DNs |
235 | - if(is_array($dn)) { |
|
235 | + if (is_array($dn)) { |
|
236 | 236 | $result = array(); |
237 | - foreach($dn as $singleDN) { |
|
237 | + foreach ($dn as $singleDN) { |
|
238 | 238 | $result[] = $this->sanitizeDN($singleDN); |
239 | 239 | } |
240 | 240 | return $result; |
@@ -285,7 +285,7 @@ discard block |
||
285 | 285 | * @throws \Exception |
286 | 286 | */ |
287 | 287 | public static function loginName2UserName($param) { |
288 | - if(!isset($param['uid'])) { |
|
288 | + if (!isset($param['uid'])) { |
|
289 | 289 | throw new \Exception('key uid is expected to be set in $param'); |
290 | 290 | } |
291 | 291 | |
@@ -300,11 +300,11 @@ discard block |
||
300 | 300 | $userSession = \OC::$server->getUserSession(); |
301 | 301 | $userPluginManager = \OC::$server->query('LDAPUserPluginManager'); |
302 | 302 | |
303 | - $userBackend = new User_Proxy( |
|
303 | + $userBackend = new User_Proxy( |
|
304 | 304 | $configPrefixes, $ldapWrapper, $ocConfig, $notificationManager, $userSession, $userPluginManager |
305 | 305 | ); |
306 | - $uid = $userBackend->loginName2UserName($param['uid'] ); |
|
307 | - if($uid !== false) { |
|
306 | + $uid = $userBackend->loginName2UserName($param['uid']); |
|
307 | + if ($uid !== false) { |
|
308 | 308 | $param['uid'] = $uid; |
309 | 309 | } |
310 | 310 | } |
@@ -40,284 +40,284 @@ |
||
40 | 40 | |
41 | 41 | class Storage extends Wrapper { |
42 | 42 | |
43 | - private $mountPoint; |
|
44 | - // remember already deleted files to avoid infinite loops if the trash bin |
|
45 | - // move files across storages |
|
46 | - private $deletedFiles = array(); |
|
47 | - |
|
48 | - /** |
|
49 | - * Disable trash logic |
|
50 | - * |
|
51 | - * @var bool |
|
52 | - */ |
|
53 | - private static $disableTrash = false; |
|
54 | - |
|
55 | - /** |
|
56 | - * remember which file/folder was moved out of s shared folder |
|
57 | - * in this case we want to add a copy to the owners trash bin |
|
58 | - * |
|
59 | - * @var array |
|
60 | - */ |
|
61 | - private static $moveOutOfSharedFolder = []; |
|
62 | - |
|
63 | - /** @var IUserManager */ |
|
64 | - private $userManager; |
|
65 | - |
|
66 | - /** @var ILogger */ |
|
67 | - private $logger; |
|
68 | - |
|
69 | - /** @var EventDispatcher */ |
|
70 | - private $eventDispatcher; |
|
71 | - |
|
72 | - /** @var IRootFolder */ |
|
73 | - private $rootFolder; |
|
74 | - |
|
75 | - /** |
|
76 | - * Storage constructor. |
|
77 | - * |
|
78 | - * @param array $parameters |
|
79 | - * @param IUserManager|null $userManager |
|
80 | - * @param ILogger|null $logger |
|
81 | - * @param EventDispatcher|null $eventDispatcher |
|
82 | - * @param IRootFolder|null $rootFolder |
|
83 | - */ |
|
84 | - public function __construct($parameters, |
|
85 | - IUserManager $userManager = null, |
|
86 | - ILogger $logger = null, |
|
87 | - EventDispatcher $eventDispatcher = null, |
|
88 | - IRootFolder $rootFolder = null) { |
|
89 | - $this->mountPoint = $parameters['mountPoint']; |
|
90 | - $this->userManager = $userManager; |
|
91 | - $this->logger = $logger; |
|
92 | - $this->eventDispatcher = $eventDispatcher; |
|
93 | - $this->rootFolder = $rootFolder; |
|
94 | - parent::__construct($parameters); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @internal |
|
99 | - */ |
|
100 | - public static function preRenameHook($params) { |
|
101 | - // in cross-storage cases, a rename is a copy + unlink, |
|
102 | - // that last unlink must not go to trash, only exception: |
|
103 | - // if the file was moved from a shared storage to a local folder, |
|
104 | - // in this case the owner should get a copy in his trash bin so that |
|
105 | - // they can restore the files again |
|
106 | - |
|
107 | - $oldPath = $params['oldpath']; |
|
108 | - $newPath = dirname($params['newpath']); |
|
109 | - $currentUser = \OC::$server->getUserSession()->getUser(); |
|
110 | - |
|
111 | - $fileMovedOutOfSharedFolder = false; |
|
112 | - |
|
113 | - try { |
|
114 | - if ($currentUser) { |
|
115 | - $currentUserId = $currentUser->getUID(); |
|
116 | - |
|
117 | - $view = new View($currentUserId . '/files'); |
|
118 | - $fileInfo = $view->getFileInfo($oldPath); |
|
119 | - if ($fileInfo) { |
|
120 | - $sourceStorage = $fileInfo->getStorage(); |
|
121 | - $sourceOwner = $view->getOwner($oldPath); |
|
122 | - $targetOwner = $view->getOwner($newPath); |
|
123 | - |
|
124 | - if ($sourceOwner !== $targetOwner |
|
125 | - && $sourceStorage->instanceOfStorage('OCA\Files_Sharing\SharedStorage') |
|
126 | - ) { |
|
127 | - $fileMovedOutOfSharedFolder = true; |
|
128 | - } |
|
129 | - } |
|
130 | - } |
|
131 | - } catch (\Exception $e) { |
|
132 | - // do nothing, in this case we just disable the trashbin and continue |
|
133 | - \OC::$server->getLogger()->logException($e, [ |
|
134 | - 'message' => 'Trashbin storage could not check if a file was moved out of a shared folder.', |
|
135 | - 'level' => \OCP\Util::DEBUG, |
|
136 | - 'app' => 'files_trashbin', |
|
137 | - ]); |
|
138 | - } |
|
139 | - |
|
140 | - if($fileMovedOutOfSharedFolder) { |
|
141 | - self::$moveOutOfSharedFolder['/' . $currentUserId . '/files' . $oldPath] = true; |
|
142 | - } else { |
|
143 | - self::$disableTrash = true; |
|
144 | - } |
|
145 | - |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * @internal |
|
150 | - */ |
|
151 | - public static function postRenameHook($params) { |
|
152 | - self::$disableTrash = false; |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Rename path1 to path2 by calling the wrapped storage. |
|
157 | - * |
|
158 | - * @param string $path1 first path |
|
159 | - * @param string $path2 second path |
|
160 | - * @return bool |
|
161 | - */ |
|
162 | - public function rename($path1, $path2) { |
|
163 | - $result = $this->storage->rename($path1, $path2); |
|
164 | - if ($result === false) { |
|
165 | - // when rename failed, the post_rename hook isn't triggered, |
|
166 | - // but we still want to reenable the trash logic |
|
167 | - self::$disableTrash = false; |
|
168 | - } |
|
169 | - return $result; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Deletes the given file by moving it into the trashbin. |
|
174 | - * |
|
175 | - * @param string $path path of file or folder to delete |
|
176 | - * |
|
177 | - * @return bool true if the operation succeeded, false otherwise |
|
178 | - */ |
|
179 | - public function unlink($path) { |
|
180 | - try { |
|
181 | - if (isset(self::$moveOutOfSharedFolder[$this->mountPoint . $path])) { |
|
182 | - $result = $this->doDelete($path, 'unlink', true); |
|
183 | - unset(self::$moveOutOfSharedFolder[$this->mountPoint . $path]); |
|
184 | - } else { |
|
185 | - $result = $this->doDelete($path, 'unlink'); |
|
186 | - } |
|
187 | - } catch (GenericEncryptionException $e) { |
|
188 | - // in case of a encryption exception we delete the file right away |
|
189 | - $this->logger->info( |
|
190 | - "Can't move file" . $path . |
|
191 | - "to the trash bin, therefore it was deleted right away"); |
|
192 | - |
|
193 | - $result = $this->storage->unlink($path); |
|
194 | - } |
|
195 | - |
|
196 | - return $result; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Deletes the given folder by moving it into the trashbin. |
|
201 | - * |
|
202 | - * @param string $path path of folder to delete |
|
203 | - * |
|
204 | - * @return bool true if the operation succeeded, false otherwise |
|
205 | - */ |
|
206 | - public function rmdir($path) { |
|
207 | - if (isset(self::$moveOutOfSharedFolder[$this->mountPoint . $path])) { |
|
208 | - $result = $this->doDelete($path, 'rmdir', true); |
|
209 | - unset(self::$moveOutOfSharedFolder[$this->mountPoint . $path]); |
|
210 | - } else { |
|
211 | - $result = $this->doDelete($path, 'rmdir'); |
|
212 | - } |
|
213 | - |
|
214 | - return $result; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * check if it is a file located in data/user/files only files in the |
|
219 | - * 'files' directory should be moved to the trash |
|
220 | - * |
|
221 | - * @param $path |
|
222 | - * @return bool |
|
223 | - */ |
|
224 | - protected function shouldMoveToTrash($path){ |
|
225 | - |
|
226 | - // check if there is a app which want to disable the trash bin for this file |
|
227 | - $fileId = $this->storage->getCache()->getId($path); |
|
228 | - $nodes = $this->rootFolder->getById($fileId); |
|
229 | - foreach ($nodes as $node) { |
|
230 | - $event = $this->createMoveToTrashEvent($node); |
|
231 | - $this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event); |
|
232 | - if ($event->shouldMoveToTrashBin() === false) { |
|
233 | - return false; |
|
234 | - } |
|
235 | - } |
|
236 | - |
|
237 | - $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path); |
|
238 | - $parts = explode('/', $normalized); |
|
239 | - if (count($parts) < 4) { |
|
240 | - return false; |
|
241 | - } |
|
242 | - |
|
243 | - if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) { |
|
244 | - return true; |
|
245 | - } |
|
246 | - |
|
247 | - return false; |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * get move to trash event |
|
252 | - * |
|
253 | - * @param Node $node |
|
254 | - * @return MoveToTrashEvent |
|
255 | - */ |
|
256 | - protected function createMoveToTrashEvent(Node $node) { |
|
257 | - return new MoveToTrashEvent($node); |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Run the delete operation with the given method |
|
262 | - * |
|
263 | - * @param string $path path of file or folder to delete |
|
264 | - * @param string $method either "unlink" or "rmdir" |
|
265 | - * @param bool $ownerOnly delete for owner only (if file gets moved out of a shared folder) |
|
266 | - * |
|
267 | - * @return bool true if the operation succeeded, false otherwise |
|
268 | - */ |
|
269 | - private function doDelete($path, $method, $ownerOnly = false) { |
|
270 | - if (self::$disableTrash |
|
271 | - || !\OC_App::isEnabled('files_trashbin') |
|
272 | - || (pathinfo($path, PATHINFO_EXTENSION) === 'part') |
|
273 | - || $this->shouldMoveToTrash($path) === false |
|
274 | - ) { |
|
275 | - return call_user_func_array([$this->storage, $method], [$path]); |
|
276 | - } |
|
277 | - |
|
278 | - // check permissions before we continue, this is especially important for |
|
279 | - // shared files |
|
280 | - if (!$this->isDeletable($path)) { |
|
281 | - return false; |
|
282 | - } |
|
283 | - |
|
284 | - $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path, true, false, true); |
|
285 | - $result = true; |
|
286 | - $view = Filesystem::getView(); |
|
287 | - if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) { |
|
288 | - $this->deletedFiles[$normalized] = $normalized; |
|
289 | - if ($filesPath = $view->getRelativePath($normalized)) { |
|
290 | - $filesPath = trim($filesPath, '/'); |
|
291 | - $result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath, $ownerOnly); |
|
292 | - // in cross-storage cases the file will be copied |
|
293 | - // but not deleted, so we delete it here |
|
294 | - if ($result) { |
|
295 | - call_user_func_array([$this->storage, $method], [$path]); |
|
296 | - } |
|
297 | - } else { |
|
298 | - $result = call_user_func_array([$this->storage, $method], [$path]); |
|
299 | - } |
|
300 | - unset($this->deletedFiles[$normalized]); |
|
301 | - } else if ($this->storage->file_exists($path)) { |
|
302 | - $result = call_user_func_array([$this->storage, $method], [$path]); |
|
303 | - } |
|
304 | - |
|
305 | - return $result; |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * Setup the storate wrapper callback |
|
310 | - */ |
|
311 | - public static function setupStorage() { |
|
312 | - \OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) { |
|
313 | - return new \OCA\Files_Trashbin\Storage( |
|
314 | - array('storage' => $storage, 'mountPoint' => $mountPoint), |
|
315 | - \OC::$server->getUserManager(), |
|
316 | - \OC::$server->getLogger(), |
|
317 | - \OC::$server->getEventDispatcher(), |
|
318 | - \OC::$server->getLazyRootFolder() |
|
319 | - ); |
|
320 | - }, 1); |
|
321 | - } |
|
43 | + private $mountPoint; |
|
44 | + // remember already deleted files to avoid infinite loops if the trash bin |
|
45 | + // move files across storages |
|
46 | + private $deletedFiles = array(); |
|
47 | + |
|
48 | + /** |
|
49 | + * Disable trash logic |
|
50 | + * |
|
51 | + * @var bool |
|
52 | + */ |
|
53 | + private static $disableTrash = false; |
|
54 | + |
|
55 | + /** |
|
56 | + * remember which file/folder was moved out of s shared folder |
|
57 | + * in this case we want to add a copy to the owners trash bin |
|
58 | + * |
|
59 | + * @var array |
|
60 | + */ |
|
61 | + private static $moveOutOfSharedFolder = []; |
|
62 | + |
|
63 | + /** @var IUserManager */ |
|
64 | + private $userManager; |
|
65 | + |
|
66 | + /** @var ILogger */ |
|
67 | + private $logger; |
|
68 | + |
|
69 | + /** @var EventDispatcher */ |
|
70 | + private $eventDispatcher; |
|
71 | + |
|
72 | + /** @var IRootFolder */ |
|
73 | + private $rootFolder; |
|
74 | + |
|
75 | + /** |
|
76 | + * Storage constructor. |
|
77 | + * |
|
78 | + * @param array $parameters |
|
79 | + * @param IUserManager|null $userManager |
|
80 | + * @param ILogger|null $logger |
|
81 | + * @param EventDispatcher|null $eventDispatcher |
|
82 | + * @param IRootFolder|null $rootFolder |
|
83 | + */ |
|
84 | + public function __construct($parameters, |
|
85 | + IUserManager $userManager = null, |
|
86 | + ILogger $logger = null, |
|
87 | + EventDispatcher $eventDispatcher = null, |
|
88 | + IRootFolder $rootFolder = null) { |
|
89 | + $this->mountPoint = $parameters['mountPoint']; |
|
90 | + $this->userManager = $userManager; |
|
91 | + $this->logger = $logger; |
|
92 | + $this->eventDispatcher = $eventDispatcher; |
|
93 | + $this->rootFolder = $rootFolder; |
|
94 | + parent::__construct($parameters); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @internal |
|
99 | + */ |
|
100 | + public static function preRenameHook($params) { |
|
101 | + // in cross-storage cases, a rename is a copy + unlink, |
|
102 | + // that last unlink must not go to trash, only exception: |
|
103 | + // if the file was moved from a shared storage to a local folder, |
|
104 | + // in this case the owner should get a copy in his trash bin so that |
|
105 | + // they can restore the files again |
|
106 | + |
|
107 | + $oldPath = $params['oldpath']; |
|
108 | + $newPath = dirname($params['newpath']); |
|
109 | + $currentUser = \OC::$server->getUserSession()->getUser(); |
|
110 | + |
|
111 | + $fileMovedOutOfSharedFolder = false; |
|
112 | + |
|
113 | + try { |
|
114 | + if ($currentUser) { |
|
115 | + $currentUserId = $currentUser->getUID(); |
|
116 | + |
|
117 | + $view = new View($currentUserId . '/files'); |
|
118 | + $fileInfo = $view->getFileInfo($oldPath); |
|
119 | + if ($fileInfo) { |
|
120 | + $sourceStorage = $fileInfo->getStorage(); |
|
121 | + $sourceOwner = $view->getOwner($oldPath); |
|
122 | + $targetOwner = $view->getOwner($newPath); |
|
123 | + |
|
124 | + if ($sourceOwner !== $targetOwner |
|
125 | + && $sourceStorage->instanceOfStorage('OCA\Files_Sharing\SharedStorage') |
|
126 | + ) { |
|
127 | + $fileMovedOutOfSharedFolder = true; |
|
128 | + } |
|
129 | + } |
|
130 | + } |
|
131 | + } catch (\Exception $e) { |
|
132 | + // do nothing, in this case we just disable the trashbin and continue |
|
133 | + \OC::$server->getLogger()->logException($e, [ |
|
134 | + 'message' => 'Trashbin storage could not check if a file was moved out of a shared folder.', |
|
135 | + 'level' => \OCP\Util::DEBUG, |
|
136 | + 'app' => 'files_trashbin', |
|
137 | + ]); |
|
138 | + } |
|
139 | + |
|
140 | + if($fileMovedOutOfSharedFolder) { |
|
141 | + self::$moveOutOfSharedFolder['/' . $currentUserId . '/files' . $oldPath] = true; |
|
142 | + } else { |
|
143 | + self::$disableTrash = true; |
|
144 | + } |
|
145 | + |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * @internal |
|
150 | + */ |
|
151 | + public static function postRenameHook($params) { |
|
152 | + self::$disableTrash = false; |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Rename path1 to path2 by calling the wrapped storage. |
|
157 | + * |
|
158 | + * @param string $path1 first path |
|
159 | + * @param string $path2 second path |
|
160 | + * @return bool |
|
161 | + */ |
|
162 | + public function rename($path1, $path2) { |
|
163 | + $result = $this->storage->rename($path1, $path2); |
|
164 | + if ($result === false) { |
|
165 | + // when rename failed, the post_rename hook isn't triggered, |
|
166 | + // but we still want to reenable the trash logic |
|
167 | + self::$disableTrash = false; |
|
168 | + } |
|
169 | + return $result; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Deletes the given file by moving it into the trashbin. |
|
174 | + * |
|
175 | + * @param string $path path of file or folder to delete |
|
176 | + * |
|
177 | + * @return bool true if the operation succeeded, false otherwise |
|
178 | + */ |
|
179 | + public function unlink($path) { |
|
180 | + try { |
|
181 | + if (isset(self::$moveOutOfSharedFolder[$this->mountPoint . $path])) { |
|
182 | + $result = $this->doDelete($path, 'unlink', true); |
|
183 | + unset(self::$moveOutOfSharedFolder[$this->mountPoint . $path]); |
|
184 | + } else { |
|
185 | + $result = $this->doDelete($path, 'unlink'); |
|
186 | + } |
|
187 | + } catch (GenericEncryptionException $e) { |
|
188 | + // in case of a encryption exception we delete the file right away |
|
189 | + $this->logger->info( |
|
190 | + "Can't move file" . $path . |
|
191 | + "to the trash bin, therefore it was deleted right away"); |
|
192 | + |
|
193 | + $result = $this->storage->unlink($path); |
|
194 | + } |
|
195 | + |
|
196 | + return $result; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Deletes the given folder by moving it into the trashbin. |
|
201 | + * |
|
202 | + * @param string $path path of folder to delete |
|
203 | + * |
|
204 | + * @return bool true if the operation succeeded, false otherwise |
|
205 | + */ |
|
206 | + public function rmdir($path) { |
|
207 | + if (isset(self::$moveOutOfSharedFolder[$this->mountPoint . $path])) { |
|
208 | + $result = $this->doDelete($path, 'rmdir', true); |
|
209 | + unset(self::$moveOutOfSharedFolder[$this->mountPoint . $path]); |
|
210 | + } else { |
|
211 | + $result = $this->doDelete($path, 'rmdir'); |
|
212 | + } |
|
213 | + |
|
214 | + return $result; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * check if it is a file located in data/user/files only files in the |
|
219 | + * 'files' directory should be moved to the trash |
|
220 | + * |
|
221 | + * @param $path |
|
222 | + * @return bool |
|
223 | + */ |
|
224 | + protected function shouldMoveToTrash($path){ |
|
225 | + |
|
226 | + // check if there is a app which want to disable the trash bin for this file |
|
227 | + $fileId = $this->storage->getCache()->getId($path); |
|
228 | + $nodes = $this->rootFolder->getById($fileId); |
|
229 | + foreach ($nodes as $node) { |
|
230 | + $event = $this->createMoveToTrashEvent($node); |
|
231 | + $this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event); |
|
232 | + if ($event->shouldMoveToTrashBin() === false) { |
|
233 | + return false; |
|
234 | + } |
|
235 | + } |
|
236 | + |
|
237 | + $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path); |
|
238 | + $parts = explode('/', $normalized); |
|
239 | + if (count($parts) < 4) { |
|
240 | + return false; |
|
241 | + } |
|
242 | + |
|
243 | + if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) { |
|
244 | + return true; |
|
245 | + } |
|
246 | + |
|
247 | + return false; |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * get move to trash event |
|
252 | + * |
|
253 | + * @param Node $node |
|
254 | + * @return MoveToTrashEvent |
|
255 | + */ |
|
256 | + protected function createMoveToTrashEvent(Node $node) { |
|
257 | + return new MoveToTrashEvent($node); |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Run the delete operation with the given method |
|
262 | + * |
|
263 | + * @param string $path path of file or folder to delete |
|
264 | + * @param string $method either "unlink" or "rmdir" |
|
265 | + * @param bool $ownerOnly delete for owner only (if file gets moved out of a shared folder) |
|
266 | + * |
|
267 | + * @return bool true if the operation succeeded, false otherwise |
|
268 | + */ |
|
269 | + private function doDelete($path, $method, $ownerOnly = false) { |
|
270 | + if (self::$disableTrash |
|
271 | + || !\OC_App::isEnabled('files_trashbin') |
|
272 | + || (pathinfo($path, PATHINFO_EXTENSION) === 'part') |
|
273 | + || $this->shouldMoveToTrash($path) === false |
|
274 | + ) { |
|
275 | + return call_user_func_array([$this->storage, $method], [$path]); |
|
276 | + } |
|
277 | + |
|
278 | + // check permissions before we continue, this is especially important for |
|
279 | + // shared files |
|
280 | + if (!$this->isDeletable($path)) { |
|
281 | + return false; |
|
282 | + } |
|
283 | + |
|
284 | + $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path, true, false, true); |
|
285 | + $result = true; |
|
286 | + $view = Filesystem::getView(); |
|
287 | + if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) { |
|
288 | + $this->deletedFiles[$normalized] = $normalized; |
|
289 | + if ($filesPath = $view->getRelativePath($normalized)) { |
|
290 | + $filesPath = trim($filesPath, '/'); |
|
291 | + $result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath, $ownerOnly); |
|
292 | + // in cross-storage cases the file will be copied |
|
293 | + // but not deleted, so we delete it here |
|
294 | + if ($result) { |
|
295 | + call_user_func_array([$this->storage, $method], [$path]); |
|
296 | + } |
|
297 | + } else { |
|
298 | + $result = call_user_func_array([$this->storage, $method], [$path]); |
|
299 | + } |
|
300 | + unset($this->deletedFiles[$normalized]); |
|
301 | + } else if ($this->storage->file_exists($path)) { |
|
302 | + $result = call_user_func_array([$this->storage, $method], [$path]); |
|
303 | + } |
|
304 | + |
|
305 | + return $result; |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * Setup the storate wrapper callback |
|
310 | + */ |
|
311 | + public static function setupStorage() { |
|
312 | + \OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) { |
|
313 | + return new \OCA\Files_Trashbin\Storage( |
|
314 | + array('storage' => $storage, 'mountPoint' => $mountPoint), |
|
315 | + \OC::$server->getUserManager(), |
|
316 | + \OC::$server->getLogger(), |
|
317 | + \OC::$server->getEventDispatcher(), |
|
318 | + \OC::$server->getLazyRootFolder() |
|
319 | + ); |
|
320 | + }, 1); |
|
321 | + } |
|
322 | 322 | |
323 | 323 | } |
@@ -31,122 +31,122 @@ |
||
31 | 31 | |
32 | 32 | class Converter { |
33 | 33 | |
34 | - /** @var AccountManager */ |
|
35 | - private $accountManager; |
|
36 | - |
|
37 | - /** |
|
38 | - * Converter constructor. |
|
39 | - * |
|
40 | - * @param AccountManager $accountManager |
|
41 | - */ |
|
42 | - public function __construct(AccountManager $accountManager) { |
|
43 | - $this->accountManager = $accountManager; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * @param IUser $user |
|
48 | - * @return VCard|null |
|
49 | - */ |
|
50 | - public function createCardFromUser(IUser $user) { |
|
51 | - |
|
52 | - $userData = $this->accountManager->getUser($user); |
|
53 | - |
|
54 | - $uid = $user->getUID(); |
|
55 | - $cloudId = $user->getCloudId(); |
|
56 | - $image = $this->getAvatarImage($user); |
|
57 | - |
|
58 | - $vCard = new VCard(); |
|
59 | - $vCard->VERSION = '3.0'; |
|
60 | - $vCard->UID = $uid; |
|
61 | - |
|
62 | - $publish = false; |
|
63 | - |
|
64 | - if ($image !== null && isset($userData[AccountManager::PROPERTY_AVATAR])) { |
|
65 | - $userData[AccountManager::PROPERTY_AVATAR]['value'] = true; |
|
66 | - } |
|
67 | - |
|
68 | - foreach ($userData as $property => $value) { |
|
69 | - |
|
70 | - $shareWithTrustedServers = |
|
71 | - $value['scope'] === AccountManager::VISIBILITY_CONTACTS_ONLY || |
|
72 | - $value['scope'] === AccountManager::VISIBILITY_PUBLIC; |
|
73 | - |
|
74 | - $emptyValue = !isset($value['value']) || $value['value'] === ''; |
|
75 | - |
|
76 | - if ($shareWithTrustedServers && !$emptyValue) { |
|
77 | - $publish = true; |
|
78 | - switch ($property) { |
|
79 | - case AccountManager::PROPERTY_DISPLAYNAME: |
|
80 | - $vCard->add(new Text($vCard, 'FN', $value['value'])); |
|
81 | - $vCard->add(new Text($vCard, 'N', $this->splitFullName($value['value']))); |
|
82 | - break; |
|
83 | - case AccountManager::PROPERTY_AVATAR: |
|
84 | - if ($image !== null) { |
|
85 | - $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); |
|
86 | - } |
|
87 | - break; |
|
88 | - case AccountManager::PROPERTY_EMAIL: |
|
89 | - $vCard->add(new Text($vCard, 'EMAIL', $value['value'], ['TYPE' => 'OTHER'])); |
|
90 | - break; |
|
91 | - case AccountManager::PROPERTY_WEBSITE: |
|
92 | - $vCard->add(new Text($vCard, 'URL', $value['value'])); |
|
93 | - break; |
|
94 | - case AccountManager::PROPERTY_PHONE: |
|
95 | - $vCard->add(new Text($vCard, 'TEL', $value['value'], ['TYPE' => 'OTHER'])); |
|
96 | - break; |
|
97 | - case AccountManager::PROPERTY_ADDRESS: |
|
98 | - $vCard->add(new Text($vCard, 'ADR', $value['value'], ['TYPE' => 'OTHER'])); |
|
99 | - break; |
|
100 | - case AccountManager::PROPERTY_TWITTER: |
|
101 | - $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $value['value'], ['TYPE' => 'TWITTER'])); |
|
102 | - break; |
|
103 | - } |
|
104 | - } |
|
105 | - } |
|
106 | - |
|
107 | - if ($publish && !empty($cloudId)) { |
|
108 | - $vCard->add(new Text($vCard, 'CLOUD', $cloudId)); |
|
109 | - $vCard->validate(); |
|
110 | - return $vCard; |
|
111 | - } |
|
112 | - |
|
113 | - return null; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * @param string $fullName |
|
118 | - * @return string[] |
|
119 | - */ |
|
120 | - public function splitFullName($fullName) { |
|
121 | - // Very basic western style parsing. I'm not gonna implement |
|
122 | - // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;) |
|
123 | - |
|
124 | - $elements = explode(' ', $fullName); |
|
125 | - $result = ['', '', '', '', '']; |
|
126 | - if (count($elements) > 2) { |
|
127 | - $result[0] = implode(' ', array_slice($elements, count($elements)-1)); |
|
128 | - $result[1] = $elements[0]; |
|
129 | - $result[2] = implode(' ', array_slice($elements, 1, count($elements)-2)); |
|
130 | - } elseif (count($elements) === 2) { |
|
131 | - $result[0] = $elements[1]; |
|
132 | - $result[1] = $elements[0]; |
|
133 | - } else { |
|
134 | - $result[0] = $elements[0]; |
|
135 | - } |
|
136 | - |
|
137 | - return $result; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * @param IUser $user |
|
142 | - * @return null|IImage |
|
143 | - */ |
|
144 | - private function getAvatarImage(IUser $user) { |
|
145 | - try { |
|
146 | - return $user->getAvatarImage(-1); |
|
147 | - } catch (\Exception $ex) { |
|
148 | - return null; |
|
149 | - } |
|
150 | - } |
|
34 | + /** @var AccountManager */ |
|
35 | + private $accountManager; |
|
36 | + |
|
37 | + /** |
|
38 | + * Converter constructor. |
|
39 | + * |
|
40 | + * @param AccountManager $accountManager |
|
41 | + */ |
|
42 | + public function __construct(AccountManager $accountManager) { |
|
43 | + $this->accountManager = $accountManager; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * @param IUser $user |
|
48 | + * @return VCard|null |
|
49 | + */ |
|
50 | + public function createCardFromUser(IUser $user) { |
|
51 | + |
|
52 | + $userData = $this->accountManager->getUser($user); |
|
53 | + |
|
54 | + $uid = $user->getUID(); |
|
55 | + $cloudId = $user->getCloudId(); |
|
56 | + $image = $this->getAvatarImage($user); |
|
57 | + |
|
58 | + $vCard = new VCard(); |
|
59 | + $vCard->VERSION = '3.0'; |
|
60 | + $vCard->UID = $uid; |
|
61 | + |
|
62 | + $publish = false; |
|
63 | + |
|
64 | + if ($image !== null && isset($userData[AccountManager::PROPERTY_AVATAR])) { |
|
65 | + $userData[AccountManager::PROPERTY_AVATAR]['value'] = true; |
|
66 | + } |
|
67 | + |
|
68 | + foreach ($userData as $property => $value) { |
|
69 | + |
|
70 | + $shareWithTrustedServers = |
|
71 | + $value['scope'] === AccountManager::VISIBILITY_CONTACTS_ONLY || |
|
72 | + $value['scope'] === AccountManager::VISIBILITY_PUBLIC; |
|
73 | + |
|
74 | + $emptyValue = !isset($value['value']) || $value['value'] === ''; |
|
75 | + |
|
76 | + if ($shareWithTrustedServers && !$emptyValue) { |
|
77 | + $publish = true; |
|
78 | + switch ($property) { |
|
79 | + case AccountManager::PROPERTY_DISPLAYNAME: |
|
80 | + $vCard->add(new Text($vCard, 'FN', $value['value'])); |
|
81 | + $vCard->add(new Text($vCard, 'N', $this->splitFullName($value['value']))); |
|
82 | + break; |
|
83 | + case AccountManager::PROPERTY_AVATAR: |
|
84 | + if ($image !== null) { |
|
85 | + $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); |
|
86 | + } |
|
87 | + break; |
|
88 | + case AccountManager::PROPERTY_EMAIL: |
|
89 | + $vCard->add(new Text($vCard, 'EMAIL', $value['value'], ['TYPE' => 'OTHER'])); |
|
90 | + break; |
|
91 | + case AccountManager::PROPERTY_WEBSITE: |
|
92 | + $vCard->add(new Text($vCard, 'URL', $value['value'])); |
|
93 | + break; |
|
94 | + case AccountManager::PROPERTY_PHONE: |
|
95 | + $vCard->add(new Text($vCard, 'TEL', $value['value'], ['TYPE' => 'OTHER'])); |
|
96 | + break; |
|
97 | + case AccountManager::PROPERTY_ADDRESS: |
|
98 | + $vCard->add(new Text($vCard, 'ADR', $value['value'], ['TYPE' => 'OTHER'])); |
|
99 | + break; |
|
100 | + case AccountManager::PROPERTY_TWITTER: |
|
101 | + $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $value['value'], ['TYPE' => 'TWITTER'])); |
|
102 | + break; |
|
103 | + } |
|
104 | + } |
|
105 | + } |
|
106 | + |
|
107 | + if ($publish && !empty($cloudId)) { |
|
108 | + $vCard->add(new Text($vCard, 'CLOUD', $cloudId)); |
|
109 | + $vCard->validate(); |
|
110 | + return $vCard; |
|
111 | + } |
|
112 | + |
|
113 | + return null; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * @param string $fullName |
|
118 | + * @return string[] |
|
119 | + */ |
|
120 | + public function splitFullName($fullName) { |
|
121 | + // Very basic western style parsing. I'm not gonna implement |
|
122 | + // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;) |
|
123 | + |
|
124 | + $elements = explode(' ', $fullName); |
|
125 | + $result = ['', '', '', '', '']; |
|
126 | + if (count($elements) > 2) { |
|
127 | + $result[0] = implode(' ', array_slice($elements, count($elements)-1)); |
|
128 | + $result[1] = $elements[0]; |
|
129 | + $result[2] = implode(' ', array_slice($elements, 1, count($elements)-2)); |
|
130 | + } elseif (count($elements) === 2) { |
|
131 | + $result[0] = $elements[1]; |
|
132 | + $result[1] = $elements[0]; |
|
133 | + } else { |
|
134 | + $result[0] = $elements[0]; |
|
135 | + } |
|
136 | + |
|
137 | + return $result; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * @param IUser $user |
|
142 | + * @return null|IImage |
|
143 | + */ |
|
144 | + private function getAvatarImage(IUser $user) { |
|
145 | + try { |
|
146 | + return $user->getAvatarImage(-1); |
|
147 | + } catch (\Exception $ex) { |
|
148 | + return null; |
|
149 | + } |
|
150 | + } |
|
151 | 151 | |
152 | 152 | } |
@@ -50,407 +50,407 @@ |
||
50 | 50 | |
51 | 51 | class FilesPlugin extends ServerPlugin { |
52 | 52 | |
53 | - // namespace |
|
54 | - const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
55 | - const NS_NEXTCLOUD = 'http://nextcloud.org/ns'; |
|
56 | - const FILEID_PROPERTYNAME = '{http://owncloud.org/ns}id'; |
|
57 | - const INTERNAL_FILEID_PROPERTYNAME = '{http://owncloud.org/ns}fileid'; |
|
58 | - const PERMISSIONS_PROPERTYNAME = '{http://owncloud.org/ns}permissions'; |
|
59 | - const SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-collaboration-services.org/ns}share-permissions'; |
|
60 | - const DOWNLOADURL_PROPERTYNAME = '{http://owncloud.org/ns}downloadURL'; |
|
61 | - const SIZE_PROPERTYNAME = '{http://owncloud.org/ns}size'; |
|
62 | - const GETETAG_PROPERTYNAME = '{DAV:}getetag'; |
|
63 | - const LASTMODIFIED_PROPERTYNAME = '{DAV:}lastmodified'; |
|
64 | - const OWNER_ID_PROPERTYNAME = '{http://owncloud.org/ns}owner-id'; |
|
65 | - const OWNER_DISPLAY_NAME_PROPERTYNAME = '{http://owncloud.org/ns}owner-display-name'; |
|
66 | - const CHECKSUMS_PROPERTYNAME = '{http://owncloud.org/ns}checksums'; |
|
67 | - const DATA_FINGERPRINT_PROPERTYNAME = '{http://owncloud.org/ns}data-fingerprint'; |
|
68 | - const HAS_PREVIEW_PROPERTYNAME = '{http://nextcloud.org/ns}has-preview'; |
|
69 | - const MOUNT_TYPE_PROPERTYNAME = '{http://nextcloud.org/ns}mount-type'; |
|
70 | - const IS_ENCRYPTED_PROPERTYNAME = '{http://nextcloud.org/ns}is-encrypted'; |
|
71 | - |
|
72 | - /** |
|
73 | - * Reference to main server object |
|
74 | - * |
|
75 | - * @var \Sabre\DAV\Server |
|
76 | - */ |
|
77 | - private $server; |
|
78 | - |
|
79 | - /** |
|
80 | - * @var Tree |
|
81 | - */ |
|
82 | - private $tree; |
|
83 | - |
|
84 | - /** |
|
85 | - * Whether this is public webdav. |
|
86 | - * If true, some returned information will be stripped off. |
|
87 | - * |
|
88 | - * @var bool |
|
89 | - */ |
|
90 | - private $isPublic; |
|
91 | - |
|
92 | - /** |
|
93 | - * @var bool |
|
94 | - */ |
|
95 | - private $downloadAttachment; |
|
96 | - |
|
97 | - /** |
|
98 | - * @var IConfig |
|
99 | - */ |
|
100 | - private $config; |
|
101 | - |
|
102 | - /** |
|
103 | - * @var IRequest |
|
104 | - */ |
|
105 | - private $request; |
|
106 | - |
|
107 | - /** |
|
108 | - * @var IPreview |
|
109 | - */ |
|
110 | - private $previewManager; |
|
111 | - |
|
112 | - /** |
|
113 | - * @param Tree $tree |
|
114 | - * @param IConfig $config |
|
115 | - * @param IRequest $request |
|
116 | - * @param IPreview $previewManager |
|
117 | - * @param bool $isPublic |
|
118 | - * @param bool $downloadAttachment |
|
119 | - */ |
|
120 | - public function __construct(Tree $tree, |
|
121 | - IConfig $config, |
|
122 | - IRequest $request, |
|
123 | - IPreview $previewManager, |
|
124 | - $isPublic = false, |
|
125 | - $downloadAttachment = true) { |
|
126 | - $this->tree = $tree; |
|
127 | - $this->config = $config; |
|
128 | - $this->request = $request; |
|
129 | - $this->isPublic = $isPublic; |
|
130 | - $this->downloadAttachment = $downloadAttachment; |
|
131 | - $this->previewManager = $previewManager; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * This initializes the plugin. |
|
136 | - * |
|
137 | - * This function is called by \Sabre\DAV\Server, after |
|
138 | - * addPlugin is called. |
|
139 | - * |
|
140 | - * This method should set up the required event subscriptions. |
|
141 | - * |
|
142 | - * @param \Sabre\DAV\Server $server |
|
143 | - * @return void |
|
144 | - */ |
|
145 | - public function initialize(\Sabre\DAV\Server $server) { |
|
146 | - $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc'; |
|
147 | - $server->xml->namespaceMap[self::NS_NEXTCLOUD] = 'nc'; |
|
148 | - $server->protectedProperties[] = self::FILEID_PROPERTYNAME; |
|
149 | - $server->protectedProperties[] = self::INTERNAL_FILEID_PROPERTYNAME; |
|
150 | - $server->protectedProperties[] = self::PERMISSIONS_PROPERTYNAME; |
|
151 | - $server->protectedProperties[] = self::SHARE_PERMISSIONS_PROPERTYNAME; |
|
152 | - $server->protectedProperties[] = self::SIZE_PROPERTYNAME; |
|
153 | - $server->protectedProperties[] = self::DOWNLOADURL_PROPERTYNAME; |
|
154 | - $server->protectedProperties[] = self::OWNER_ID_PROPERTYNAME; |
|
155 | - $server->protectedProperties[] = self::OWNER_DISPLAY_NAME_PROPERTYNAME; |
|
156 | - $server->protectedProperties[] = self::CHECKSUMS_PROPERTYNAME; |
|
157 | - $server->protectedProperties[] = self::DATA_FINGERPRINT_PROPERTYNAME; |
|
158 | - $server->protectedProperties[] = self::HAS_PREVIEW_PROPERTYNAME; |
|
159 | - $server->protectedProperties[] = self::MOUNT_TYPE_PROPERTYNAME; |
|
160 | - $server->protectedProperties[] = self::IS_ENCRYPTED_PROPERTYNAME; |
|
161 | - |
|
162 | - // normally these cannot be changed (RFC4918), but we want them modifiable through PROPPATCH |
|
163 | - $allowedProperties = ['{DAV:}getetag']; |
|
164 | - $server->protectedProperties = array_diff($server->protectedProperties, $allowedProperties); |
|
165 | - |
|
166 | - $this->server = $server; |
|
167 | - $this->server->on('propFind', array($this, 'handleGetProperties')); |
|
168 | - $this->server->on('propPatch', array($this, 'handleUpdateProperties')); |
|
169 | - $this->server->on('afterBind', array($this, 'sendFileIdHeader')); |
|
170 | - $this->server->on('afterWriteContent', array($this, 'sendFileIdHeader')); |
|
171 | - $this->server->on('afterMethod:GET', [$this,'httpGet']); |
|
172 | - $this->server->on('afterMethod:GET', array($this, 'handleDownloadToken')); |
|
173 | - $this->server->on('afterResponse', function($request, ResponseInterface $response) { |
|
174 | - $body = $response->getBody(); |
|
175 | - if (is_resource($body)) { |
|
176 | - fclose($body); |
|
177 | - } |
|
178 | - }); |
|
179 | - $this->server->on('beforeMove', [$this, 'checkMove']); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Plugin that checks if a move can actually be performed. |
|
184 | - * |
|
185 | - * @param string $source source path |
|
186 | - * @param string $destination destination path |
|
187 | - * @throws Forbidden |
|
188 | - * @throws NotFound |
|
189 | - */ |
|
190 | - function checkMove($source, $destination) { |
|
191 | - $sourceNode = $this->tree->getNodeForPath($source); |
|
192 | - if (!$sourceNode instanceof Node) { |
|
193 | - return; |
|
194 | - } |
|
195 | - list($sourceDir,) = \Sabre\Uri\split($source); |
|
196 | - list($destinationDir,) = \Sabre\Uri\split($destination); |
|
197 | - |
|
198 | - if ($sourceDir !== $destinationDir) { |
|
199 | - $sourceNodeFileInfo = $sourceNode->getFileInfo(); |
|
200 | - if ($sourceNodeFileInfo === null) { |
|
201 | - throw new NotFound($source . ' does not exist'); |
|
202 | - } |
|
203 | - |
|
204 | - if (!$sourceNodeFileInfo->isDeletable()) { |
|
205 | - throw new Forbidden($source . " cannot be deleted"); |
|
206 | - } |
|
207 | - } |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * This sets a cookie to be able to recognize the start of the download |
|
212 | - * the content must not be longer than 32 characters and must only contain |
|
213 | - * alphanumeric characters |
|
214 | - * |
|
215 | - * @param RequestInterface $request |
|
216 | - * @param ResponseInterface $response |
|
217 | - */ |
|
218 | - function handleDownloadToken(RequestInterface $request, ResponseInterface $response) { |
|
219 | - $queryParams = $request->getQueryParameters(); |
|
220 | - |
|
221 | - /** |
|
222 | - * this sets a cookie to be able to recognize the start of the download |
|
223 | - * the content must not be longer than 32 characters and must only contain |
|
224 | - * alphanumeric characters |
|
225 | - */ |
|
226 | - if (isset($queryParams['downloadStartSecret'])) { |
|
227 | - $token = $queryParams['downloadStartSecret']; |
|
228 | - if (!isset($token[32]) |
|
229 | - && preg_match('!^[a-zA-Z0-9]+$!', $token) === 1) { |
|
230 | - // FIXME: use $response->setHeader() instead |
|
231 | - setcookie('ocDownloadStarted', $token, time() + 20, '/'); |
|
232 | - } |
|
233 | - } |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * Add headers to file download |
|
238 | - * |
|
239 | - * @param RequestInterface $request |
|
240 | - * @param ResponseInterface $response |
|
241 | - */ |
|
242 | - function httpGet(RequestInterface $request, ResponseInterface $response) { |
|
243 | - // Only handle valid files |
|
244 | - $node = $this->tree->getNodeForPath($request->getPath()); |
|
245 | - if (!($node instanceof IFile)) return; |
|
246 | - |
|
247 | - // adds a 'Content-Disposition: attachment' header in case no disposition |
|
248 | - // header has been set before |
|
249 | - if ($this->downloadAttachment && |
|
250 | - $response->getHeader('Content-Disposition') === null) { |
|
251 | - $filename = $node->getName(); |
|
252 | - if ($this->request->isUserAgent( |
|
253 | - [ |
|
254 | - Request::USER_AGENT_IE, |
|
255 | - Request::USER_AGENT_ANDROID_MOBILE_CHROME, |
|
256 | - Request::USER_AGENT_FREEBOX, |
|
257 | - ])) { |
|
258 | - $response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"'); |
|
259 | - } else { |
|
260 | - $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename) |
|
261 | - . '; filename="' . rawurlencode($filename) . '"'); |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - if ($node instanceof \OCA\DAV\Connector\Sabre\File) { |
|
266 | - //Add OC-Checksum header |
|
267 | - /** @var $node File */ |
|
268 | - $checksum = $node->getChecksum(); |
|
269 | - if ($checksum !== null && $checksum !== '') { |
|
270 | - $response->addHeader('OC-Checksum', $checksum); |
|
271 | - } |
|
272 | - } |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * Adds all ownCloud-specific properties |
|
277 | - * |
|
278 | - * @param PropFind $propFind |
|
279 | - * @param \Sabre\DAV\INode $node |
|
280 | - * @return void |
|
281 | - */ |
|
282 | - public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) { |
|
283 | - |
|
284 | - $httpRequest = $this->server->httpRequest; |
|
285 | - |
|
286 | - if ($node instanceof \OCA\DAV\Connector\Sabre\Node) { |
|
287 | - /** |
|
288 | - * This was disabled, because it made dir listing throw an exception, |
|
289 | - * so users were unable to navigate into folders where one subitem |
|
290 | - * is blocked by the files_accesscontrol app, see: |
|
291 | - * https://github.com/nextcloud/files_accesscontrol/issues/65 |
|
53 | + // namespace |
|
54 | + const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
55 | + const NS_NEXTCLOUD = 'http://nextcloud.org/ns'; |
|
56 | + const FILEID_PROPERTYNAME = '{http://owncloud.org/ns}id'; |
|
57 | + const INTERNAL_FILEID_PROPERTYNAME = '{http://owncloud.org/ns}fileid'; |
|
58 | + const PERMISSIONS_PROPERTYNAME = '{http://owncloud.org/ns}permissions'; |
|
59 | + const SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-collaboration-services.org/ns}share-permissions'; |
|
60 | + const DOWNLOADURL_PROPERTYNAME = '{http://owncloud.org/ns}downloadURL'; |
|
61 | + const SIZE_PROPERTYNAME = '{http://owncloud.org/ns}size'; |
|
62 | + const GETETAG_PROPERTYNAME = '{DAV:}getetag'; |
|
63 | + const LASTMODIFIED_PROPERTYNAME = '{DAV:}lastmodified'; |
|
64 | + const OWNER_ID_PROPERTYNAME = '{http://owncloud.org/ns}owner-id'; |
|
65 | + const OWNER_DISPLAY_NAME_PROPERTYNAME = '{http://owncloud.org/ns}owner-display-name'; |
|
66 | + const CHECKSUMS_PROPERTYNAME = '{http://owncloud.org/ns}checksums'; |
|
67 | + const DATA_FINGERPRINT_PROPERTYNAME = '{http://owncloud.org/ns}data-fingerprint'; |
|
68 | + const HAS_PREVIEW_PROPERTYNAME = '{http://nextcloud.org/ns}has-preview'; |
|
69 | + const MOUNT_TYPE_PROPERTYNAME = '{http://nextcloud.org/ns}mount-type'; |
|
70 | + const IS_ENCRYPTED_PROPERTYNAME = '{http://nextcloud.org/ns}is-encrypted'; |
|
71 | + |
|
72 | + /** |
|
73 | + * Reference to main server object |
|
74 | + * |
|
75 | + * @var \Sabre\DAV\Server |
|
76 | + */ |
|
77 | + private $server; |
|
78 | + |
|
79 | + /** |
|
80 | + * @var Tree |
|
81 | + */ |
|
82 | + private $tree; |
|
83 | + |
|
84 | + /** |
|
85 | + * Whether this is public webdav. |
|
86 | + * If true, some returned information will be stripped off. |
|
87 | + * |
|
88 | + * @var bool |
|
89 | + */ |
|
90 | + private $isPublic; |
|
91 | + |
|
92 | + /** |
|
93 | + * @var bool |
|
94 | + */ |
|
95 | + private $downloadAttachment; |
|
96 | + |
|
97 | + /** |
|
98 | + * @var IConfig |
|
99 | + */ |
|
100 | + private $config; |
|
101 | + |
|
102 | + /** |
|
103 | + * @var IRequest |
|
104 | + */ |
|
105 | + private $request; |
|
106 | + |
|
107 | + /** |
|
108 | + * @var IPreview |
|
109 | + */ |
|
110 | + private $previewManager; |
|
111 | + |
|
112 | + /** |
|
113 | + * @param Tree $tree |
|
114 | + * @param IConfig $config |
|
115 | + * @param IRequest $request |
|
116 | + * @param IPreview $previewManager |
|
117 | + * @param bool $isPublic |
|
118 | + * @param bool $downloadAttachment |
|
119 | + */ |
|
120 | + public function __construct(Tree $tree, |
|
121 | + IConfig $config, |
|
122 | + IRequest $request, |
|
123 | + IPreview $previewManager, |
|
124 | + $isPublic = false, |
|
125 | + $downloadAttachment = true) { |
|
126 | + $this->tree = $tree; |
|
127 | + $this->config = $config; |
|
128 | + $this->request = $request; |
|
129 | + $this->isPublic = $isPublic; |
|
130 | + $this->downloadAttachment = $downloadAttachment; |
|
131 | + $this->previewManager = $previewManager; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * This initializes the plugin. |
|
136 | + * |
|
137 | + * This function is called by \Sabre\DAV\Server, after |
|
138 | + * addPlugin is called. |
|
139 | + * |
|
140 | + * This method should set up the required event subscriptions. |
|
141 | + * |
|
142 | + * @param \Sabre\DAV\Server $server |
|
143 | + * @return void |
|
144 | + */ |
|
145 | + public function initialize(\Sabre\DAV\Server $server) { |
|
146 | + $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc'; |
|
147 | + $server->xml->namespaceMap[self::NS_NEXTCLOUD] = 'nc'; |
|
148 | + $server->protectedProperties[] = self::FILEID_PROPERTYNAME; |
|
149 | + $server->protectedProperties[] = self::INTERNAL_FILEID_PROPERTYNAME; |
|
150 | + $server->protectedProperties[] = self::PERMISSIONS_PROPERTYNAME; |
|
151 | + $server->protectedProperties[] = self::SHARE_PERMISSIONS_PROPERTYNAME; |
|
152 | + $server->protectedProperties[] = self::SIZE_PROPERTYNAME; |
|
153 | + $server->protectedProperties[] = self::DOWNLOADURL_PROPERTYNAME; |
|
154 | + $server->protectedProperties[] = self::OWNER_ID_PROPERTYNAME; |
|
155 | + $server->protectedProperties[] = self::OWNER_DISPLAY_NAME_PROPERTYNAME; |
|
156 | + $server->protectedProperties[] = self::CHECKSUMS_PROPERTYNAME; |
|
157 | + $server->protectedProperties[] = self::DATA_FINGERPRINT_PROPERTYNAME; |
|
158 | + $server->protectedProperties[] = self::HAS_PREVIEW_PROPERTYNAME; |
|
159 | + $server->protectedProperties[] = self::MOUNT_TYPE_PROPERTYNAME; |
|
160 | + $server->protectedProperties[] = self::IS_ENCRYPTED_PROPERTYNAME; |
|
161 | + |
|
162 | + // normally these cannot be changed (RFC4918), but we want them modifiable through PROPPATCH |
|
163 | + $allowedProperties = ['{DAV:}getetag']; |
|
164 | + $server->protectedProperties = array_diff($server->protectedProperties, $allowedProperties); |
|
165 | + |
|
166 | + $this->server = $server; |
|
167 | + $this->server->on('propFind', array($this, 'handleGetProperties')); |
|
168 | + $this->server->on('propPatch', array($this, 'handleUpdateProperties')); |
|
169 | + $this->server->on('afterBind', array($this, 'sendFileIdHeader')); |
|
170 | + $this->server->on('afterWriteContent', array($this, 'sendFileIdHeader')); |
|
171 | + $this->server->on('afterMethod:GET', [$this,'httpGet']); |
|
172 | + $this->server->on('afterMethod:GET', array($this, 'handleDownloadToken')); |
|
173 | + $this->server->on('afterResponse', function($request, ResponseInterface $response) { |
|
174 | + $body = $response->getBody(); |
|
175 | + if (is_resource($body)) { |
|
176 | + fclose($body); |
|
177 | + } |
|
178 | + }); |
|
179 | + $this->server->on('beforeMove', [$this, 'checkMove']); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Plugin that checks if a move can actually be performed. |
|
184 | + * |
|
185 | + * @param string $source source path |
|
186 | + * @param string $destination destination path |
|
187 | + * @throws Forbidden |
|
188 | + * @throws NotFound |
|
189 | + */ |
|
190 | + function checkMove($source, $destination) { |
|
191 | + $sourceNode = $this->tree->getNodeForPath($source); |
|
192 | + if (!$sourceNode instanceof Node) { |
|
193 | + return; |
|
194 | + } |
|
195 | + list($sourceDir,) = \Sabre\Uri\split($source); |
|
196 | + list($destinationDir,) = \Sabre\Uri\split($destination); |
|
197 | + |
|
198 | + if ($sourceDir !== $destinationDir) { |
|
199 | + $sourceNodeFileInfo = $sourceNode->getFileInfo(); |
|
200 | + if ($sourceNodeFileInfo === null) { |
|
201 | + throw new NotFound($source . ' does not exist'); |
|
202 | + } |
|
203 | + |
|
204 | + if (!$sourceNodeFileInfo->isDeletable()) { |
|
205 | + throw new Forbidden($source . " cannot be deleted"); |
|
206 | + } |
|
207 | + } |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * This sets a cookie to be able to recognize the start of the download |
|
212 | + * the content must not be longer than 32 characters and must only contain |
|
213 | + * alphanumeric characters |
|
214 | + * |
|
215 | + * @param RequestInterface $request |
|
216 | + * @param ResponseInterface $response |
|
217 | + */ |
|
218 | + function handleDownloadToken(RequestInterface $request, ResponseInterface $response) { |
|
219 | + $queryParams = $request->getQueryParameters(); |
|
220 | + |
|
221 | + /** |
|
222 | + * this sets a cookie to be able to recognize the start of the download |
|
223 | + * the content must not be longer than 32 characters and must only contain |
|
224 | + * alphanumeric characters |
|
225 | + */ |
|
226 | + if (isset($queryParams['downloadStartSecret'])) { |
|
227 | + $token = $queryParams['downloadStartSecret']; |
|
228 | + if (!isset($token[32]) |
|
229 | + && preg_match('!^[a-zA-Z0-9]+$!', $token) === 1) { |
|
230 | + // FIXME: use $response->setHeader() instead |
|
231 | + setcookie('ocDownloadStarted', $token, time() + 20, '/'); |
|
232 | + } |
|
233 | + } |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * Add headers to file download |
|
238 | + * |
|
239 | + * @param RequestInterface $request |
|
240 | + * @param ResponseInterface $response |
|
241 | + */ |
|
242 | + function httpGet(RequestInterface $request, ResponseInterface $response) { |
|
243 | + // Only handle valid files |
|
244 | + $node = $this->tree->getNodeForPath($request->getPath()); |
|
245 | + if (!($node instanceof IFile)) return; |
|
246 | + |
|
247 | + // adds a 'Content-Disposition: attachment' header in case no disposition |
|
248 | + // header has been set before |
|
249 | + if ($this->downloadAttachment && |
|
250 | + $response->getHeader('Content-Disposition') === null) { |
|
251 | + $filename = $node->getName(); |
|
252 | + if ($this->request->isUserAgent( |
|
253 | + [ |
|
254 | + Request::USER_AGENT_IE, |
|
255 | + Request::USER_AGENT_ANDROID_MOBILE_CHROME, |
|
256 | + Request::USER_AGENT_FREEBOX, |
|
257 | + ])) { |
|
258 | + $response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"'); |
|
259 | + } else { |
|
260 | + $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename) |
|
261 | + . '; filename="' . rawurlencode($filename) . '"'); |
|
262 | + } |
|
263 | + } |
|
264 | + |
|
265 | + if ($node instanceof \OCA\DAV\Connector\Sabre\File) { |
|
266 | + //Add OC-Checksum header |
|
267 | + /** @var $node File */ |
|
268 | + $checksum = $node->getChecksum(); |
|
269 | + if ($checksum !== null && $checksum !== '') { |
|
270 | + $response->addHeader('OC-Checksum', $checksum); |
|
271 | + } |
|
272 | + } |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * Adds all ownCloud-specific properties |
|
277 | + * |
|
278 | + * @param PropFind $propFind |
|
279 | + * @param \Sabre\DAV\INode $node |
|
280 | + * @return void |
|
281 | + */ |
|
282 | + public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) { |
|
283 | + |
|
284 | + $httpRequest = $this->server->httpRequest; |
|
285 | + |
|
286 | + if ($node instanceof \OCA\DAV\Connector\Sabre\Node) { |
|
287 | + /** |
|
288 | + * This was disabled, because it made dir listing throw an exception, |
|
289 | + * so users were unable to navigate into folders where one subitem |
|
290 | + * is blocked by the files_accesscontrol app, see: |
|
291 | + * https://github.com/nextcloud/files_accesscontrol/issues/65 |
|
292 | 292 | if (!$node->getFileInfo()->isReadable()) { |
293 | 293 | // avoid detecting files through this means |
294 | 294 | throw new NotFound(); |
295 | 295 | } |
296 | - */ |
|
297 | - |
|
298 | - $propFind->handle(self::FILEID_PROPERTYNAME, function() use ($node) { |
|
299 | - return $node->getFileId(); |
|
300 | - }); |
|
301 | - |
|
302 | - $propFind->handle(self::INTERNAL_FILEID_PROPERTYNAME, function() use ($node) { |
|
303 | - return $node->getInternalFileId(); |
|
304 | - }); |
|
305 | - |
|
306 | - $propFind->handle(self::PERMISSIONS_PROPERTYNAME, function() use ($node) { |
|
307 | - $perms = $node->getDavPermissions(); |
|
308 | - if ($this->isPublic) { |
|
309 | - // remove mount information |
|
310 | - $perms = str_replace(['S', 'M'], '', $perms); |
|
311 | - } |
|
312 | - return $perms; |
|
313 | - }); |
|
314 | - |
|
315 | - $propFind->handle(self::SHARE_PERMISSIONS_PROPERTYNAME, function() use ($node, $httpRequest) { |
|
316 | - return $node->getSharePermissions( |
|
317 | - $httpRequest->getRawServerValue('PHP_AUTH_USER') |
|
318 | - ); |
|
319 | - }); |
|
320 | - |
|
321 | - $propFind->handle(self::GETETAG_PROPERTYNAME, function() use ($node) { |
|
322 | - return $node->getETag(); |
|
323 | - }); |
|
324 | - |
|
325 | - $propFind->handle(self::OWNER_ID_PROPERTYNAME, function() use ($node) { |
|
326 | - $owner = $node->getOwner(); |
|
327 | - if (!$owner) { |
|
328 | - return null; |
|
329 | - } else { |
|
330 | - return $owner->getUID(); |
|
331 | - } |
|
332 | - }); |
|
333 | - $propFind->handle(self::OWNER_DISPLAY_NAME_PROPERTYNAME, function() use ($node) { |
|
334 | - $owner = $node->getOwner(); |
|
335 | - if (!$owner) { |
|
336 | - return null; |
|
337 | - } else { |
|
338 | - return $owner->getDisplayName(); |
|
339 | - } |
|
340 | - }); |
|
341 | - |
|
342 | - $propFind->handle(self::IS_ENCRYPTED_PROPERTYNAME, function() use ($node) { |
|
343 | - return $node->getFileInfo()->isEncrypted() ? '1' : '0'; |
|
344 | - }); |
|
345 | - |
|
346 | - $propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function () use ($node) { |
|
347 | - return json_encode($this->previewManager->isAvailable($node->getFileInfo())); |
|
348 | - }); |
|
349 | - $propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) { |
|
350 | - return $node->getSize(); |
|
351 | - }); |
|
352 | - $propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function () use ($node) { |
|
353 | - return $node->getFileInfo()->getMountPoint()->getMountType(); |
|
354 | - }); |
|
355 | - } |
|
356 | - |
|
357 | - if ($node instanceof \OCA\DAV\Connector\Sabre\Node) { |
|
358 | - $propFind->handle(self::DATA_FINGERPRINT_PROPERTYNAME, function() use ($node) { |
|
359 | - return $this->config->getSystemValue('data-fingerprint', ''); |
|
360 | - }); |
|
361 | - } |
|
362 | - |
|
363 | - if ($node instanceof \OCA\DAV\Connector\Sabre\File) { |
|
364 | - $propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function() use ($node) { |
|
365 | - /** @var $node \OCA\DAV\Connector\Sabre\File */ |
|
366 | - try { |
|
367 | - $directDownloadUrl = $node->getDirectDownload(); |
|
368 | - if (isset($directDownloadUrl['url'])) { |
|
369 | - return $directDownloadUrl['url']; |
|
370 | - } |
|
371 | - } catch (StorageNotAvailableException $e) { |
|
372 | - return false; |
|
373 | - } catch (ForbiddenException $e) { |
|
374 | - return false; |
|
375 | - } |
|
376 | - return false; |
|
377 | - }); |
|
378 | - |
|
379 | - $propFind->handle(self::CHECKSUMS_PROPERTYNAME, function() use ($node) { |
|
380 | - $checksum = $node->getChecksum(); |
|
381 | - if ($checksum === NULL || $checksum === '') { |
|
382 | - return null; |
|
383 | - } |
|
384 | - |
|
385 | - return new ChecksumList($checksum); |
|
386 | - }); |
|
387 | - |
|
388 | - } |
|
389 | - |
|
390 | - if ($node instanceof \OCA\DAV\Connector\Sabre\Directory) { |
|
391 | - $propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) { |
|
392 | - return $node->getSize(); |
|
393 | - }); |
|
394 | - } |
|
395 | - } |
|
396 | - |
|
397 | - /** |
|
398 | - * Update ownCloud-specific properties |
|
399 | - * |
|
400 | - * @param string $path |
|
401 | - * @param PropPatch $propPatch |
|
402 | - * |
|
403 | - * @return void |
|
404 | - */ |
|
405 | - public function handleUpdateProperties($path, PropPatch $propPatch) { |
|
406 | - $node = $this->tree->getNodeForPath($path); |
|
407 | - if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) { |
|
408 | - return; |
|
409 | - } |
|
410 | - |
|
411 | - $propPatch->handle(self::LASTMODIFIED_PROPERTYNAME, function($time) use ($node) { |
|
412 | - if (empty($time)) { |
|
413 | - return false; |
|
414 | - } |
|
415 | - $node->touch($time); |
|
416 | - return true; |
|
417 | - }); |
|
418 | - $propPatch->handle(self::GETETAG_PROPERTYNAME, function($etag) use ($node) { |
|
419 | - if (empty($etag)) { |
|
420 | - return false; |
|
421 | - } |
|
422 | - if ($node->setEtag($etag) !== -1) { |
|
423 | - return true; |
|
424 | - } |
|
425 | - return false; |
|
426 | - }); |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * @param string $filePath |
|
431 | - * @param \Sabre\DAV\INode $node |
|
432 | - * @throws \Sabre\DAV\Exception\BadRequest |
|
433 | - */ |
|
434 | - public function sendFileIdHeader($filePath, \Sabre\DAV\INode $node = null) { |
|
435 | - // chunked upload handling |
|
436 | - if (isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
437 | - list($path, $name) = \Sabre\Uri\split($filePath); |
|
438 | - $info = \OC_FileChunking::decodeName($name); |
|
439 | - if (!empty($info)) { |
|
440 | - $filePath = $path . '/' . $info['name']; |
|
441 | - } |
|
442 | - } |
|
443 | - |
|
444 | - // we get the node for the given $filePath here because in case of afterCreateFile $node is the parent folder |
|
445 | - if (!$this->server->tree->nodeExists($filePath)) { |
|
446 | - return; |
|
447 | - } |
|
448 | - $node = $this->server->tree->getNodeForPath($filePath); |
|
449 | - if ($node instanceof \OCA\DAV\Connector\Sabre\Node) { |
|
450 | - $fileId = $node->getFileId(); |
|
451 | - if (!is_null($fileId)) { |
|
452 | - $this->server->httpResponse->setHeader('OC-FileId', $fileId); |
|
453 | - } |
|
454 | - } |
|
455 | - } |
|
296 | + */ |
|
297 | + |
|
298 | + $propFind->handle(self::FILEID_PROPERTYNAME, function() use ($node) { |
|
299 | + return $node->getFileId(); |
|
300 | + }); |
|
301 | + |
|
302 | + $propFind->handle(self::INTERNAL_FILEID_PROPERTYNAME, function() use ($node) { |
|
303 | + return $node->getInternalFileId(); |
|
304 | + }); |
|
305 | + |
|
306 | + $propFind->handle(self::PERMISSIONS_PROPERTYNAME, function() use ($node) { |
|
307 | + $perms = $node->getDavPermissions(); |
|
308 | + if ($this->isPublic) { |
|
309 | + // remove mount information |
|
310 | + $perms = str_replace(['S', 'M'], '', $perms); |
|
311 | + } |
|
312 | + return $perms; |
|
313 | + }); |
|
314 | + |
|
315 | + $propFind->handle(self::SHARE_PERMISSIONS_PROPERTYNAME, function() use ($node, $httpRequest) { |
|
316 | + return $node->getSharePermissions( |
|
317 | + $httpRequest->getRawServerValue('PHP_AUTH_USER') |
|
318 | + ); |
|
319 | + }); |
|
320 | + |
|
321 | + $propFind->handle(self::GETETAG_PROPERTYNAME, function() use ($node) { |
|
322 | + return $node->getETag(); |
|
323 | + }); |
|
324 | + |
|
325 | + $propFind->handle(self::OWNER_ID_PROPERTYNAME, function() use ($node) { |
|
326 | + $owner = $node->getOwner(); |
|
327 | + if (!$owner) { |
|
328 | + return null; |
|
329 | + } else { |
|
330 | + return $owner->getUID(); |
|
331 | + } |
|
332 | + }); |
|
333 | + $propFind->handle(self::OWNER_DISPLAY_NAME_PROPERTYNAME, function() use ($node) { |
|
334 | + $owner = $node->getOwner(); |
|
335 | + if (!$owner) { |
|
336 | + return null; |
|
337 | + } else { |
|
338 | + return $owner->getDisplayName(); |
|
339 | + } |
|
340 | + }); |
|
341 | + |
|
342 | + $propFind->handle(self::IS_ENCRYPTED_PROPERTYNAME, function() use ($node) { |
|
343 | + return $node->getFileInfo()->isEncrypted() ? '1' : '0'; |
|
344 | + }); |
|
345 | + |
|
346 | + $propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function () use ($node) { |
|
347 | + return json_encode($this->previewManager->isAvailable($node->getFileInfo())); |
|
348 | + }); |
|
349 | + $propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) { |
|
350 | + return $node->getSize(); |
|
351 | + }); |
|
352 | + $propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function () use ($node) { |
|
353 | + return $node->getFileInfo()->getMountPoint()->getMountType(); |
|
354 | + }); |
|
355 | + } |
|
356 | + |
|
357 | + if ($node instanceof \OCA\DAV\Connector\Sabre\Node) { |
|
358 | + $propFind->handle(self::DATA_FINGERPRINT_PROPERTYNAME, function() use ($node) { |
|
359 | + return $this->config->getSystemValue('data-fingerprint', ''); |
|
360 | + }); |
|
361 | + } |
|
362 | + |
|
363 | + if ($node instanceof \OCA\DAV\Connector\Sabre\File) { |
|
364 | + $propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function() use ($node) { |
|
365 | + /** @var $node \OCA\DAV\Connector\Sabre\File */ |
|
366 | + try { |
|
367 | + $directDownloadUrl = $node->getDirectDownload(); |
|
368 | + if (isset($directDownloadUrl['url'])) { |
|
369 | + return $directDownloadUrl['url']; |
|
370 | + } |
|
371 | + } catch (StorageNotAvailableException $e) { |
|
372 | + return false; |
|
373 | + } catch (ForbiddenException $e) { |
|
374 | + return false; |
|
375 | + } |
|
376 | + return false; |
|
377 | + }); |
|
378 | + |
|
379 | + $propFind->handle(self::CHECKSUMS_PROPERTYNAME, function() use ($node) { |
|
380 | + $checksum = $node->getChecksum(); |
|
381 | + if ($checksum === NULL || $checksum === '') { |
|
382 | + return null; |
|
383 | + } |
|
384 | + |
|
385 | + return new ChecksumList($checksum); |
|
386 | + }); |
|
387 | + |
|
388 | + } |
|
389 | + |
|
390 | + if ($node instanceof \OCA\DAV\Connector\Sabre\Directory) { |
|
391 | + $propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) { |
|
392 | + return $node->getSize(); |
|
393 | + }); |
|
394 | + } |
|
395 | + } |
|
396 | + |
|
397 | + /** |
|
398 | + * Update ownCloud-specific properties |
|
399 | + * |
|
400 | + * @param string $path |
|
401 | + * @param PropPatch $propPatch |
|
402 | + * |
|
403 | + * @return void |
|
404 | + */ |
|
405 | + public function handleUpdateProperties($path, PropPatch $propPatch) { |
|
406 | + $node = $this->tree->getNodeForPath($path); |
|
407 | + if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) { |
|
408 | + return; |
|
409 | + } |
|
410 | + |
|
411 | + $propPatch->handle(self::LASTMODIFIED_PROPERTYNAME, function($time) use ($node) { |
|
412 | + if (empty($time)) { |
|
413 | + return false; |
|
414 | + } |
|
415 | + $node->touch($time); |
|
416 | + return true; |
|
417 | + }); |
|
418 | + $propPatch->handle(self::GETETAG_PROPERTYNAME, function($etag) use ($node) { |
|
419 | + if (empty($etag)) { |
|
420 | + return false; |
|
421 | + } |
|
422 | + if ($node->setEtag($etag) !== -1) { |
|
423 | + return true; |
|
424 | + } |
|
425 | + return false; |
|
426 | + }); |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * @param string $filePath |
|
431 | + * @param \Sabre\DAV\INode $node |
|
432 | + * @throws \Sabre\DAV\Exception\BadRequest |
|
433 | + */ |
|
434 | + public function sendFileIdHeader($filePath, \Sabre\DAV\INode $node = null) { |
|
435 | + // chunked upload handling |
|
436 | + if (isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
437 | + list($path, $name) = \Sabre\Uri\split($filePath); |
|
438 | + $info = \OC_FileChunking::decodeName($name); |
|
439 | + if (!empty($info)) { |
|
440 | + $filePath = $path . '/' . $info['name']; |
|
441 | + } |
|
442 | + } |
|
443 | + |
|
444 | + // we get the node for the given $filePath here because in case of afterCreateFile $node is the parent folder |
|
445 | + if (!$this->server->tree->nodeExists($filePath)) { |
|
446 | + return; |
|
447 | + } |
|
448 | + $node = $this->server->tree->getNodeForPath($filePath); |
|
449 | + if ($node instanceof \OCA\DAV\Connector\Sabre\Node) { |
|
450 | + $fileId = $node->getFileId(); |
|
451 | + if (!is_null($fileId)) { |
|
452 | + $this->server->httpResponse->setHeader('OC-FileId', $fileId); |
|
453 | + } |
|
454 | + } |
|
455 | + } |
|
456 | 456 | } |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | $this->server->on('propPatch', array($this, 'handleUpdateProperties')); |
169 | 169 | $this->server->on('afterBind', array($this, 'sendFileIdHeader')); |
170 | 170 | $this->server->on('afterWriteContent', array($this, 'sendFileIdHeader')); |
171 | - $this->server->on('afterMethod:GET', [$this,'httpGet']); |
|
171 | + $this->server->on('afterMethod:GET', [$this, 'httpGet']); |
|
172 | 172 | $this->server->on('afterMethod:GET', array($this, 'handleDownloadToken')); |
173 | 173 | $this->server->on('afterResponse', function($request, ResponseInterface $response) { |
174 | 174 | $body = $response->getBody(); |
@@ -198,11 +198,11 @@ discard block |
||
198 | 198 | if ($sourceDir !== $destinationDir) { |
199 | 199 | $sourceNodeFileInfo = $sourceNode->getFileInfo(); |
200 | 200 | if ($sourceNodeFileInfo === null) { |
201 | - throw new NotFound($source . ' does not exist'); |
|
201 | + throw new NotFound($source.' does not exist'); |
|
202 | 202 | } |
203 | 203 | |
204 | 204 | if (!$sourceNodeFileInfo->isDeletable()) { |
205 | - throw new Forbidden($source . " cannot be deleted"); |
|
205 | + throw new Forbidden($source." cannot be deleted"); |
|
206 | 206 | } |
207 | 207 | } |
208 | 208 | } |
@@ -255,10 +255,10 @@ discard block |
||
255 | 255 | Request::USER_AGENT_ANDROID_MOBILE_CHROME, |
256 | 256 | Request::USER_AGENT_FREEBOX, |
257 | 257 | ])) { |
258 | - $response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"'); |
|
258 | + $response->addHeader('Content-Disposition', 'attachment; filename="'.rawurlencode($filename).'"'); |
|
259 | 259 | } else { |
260 | - $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename) |
|
261 | - . '; filename="' . rawurlencode($filename) . '"'); |
|
260 | + $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\''.rawurlencode($filename) |
|
261 | + . '; filename="'.rawurlencode($filename).'"'); |
|
262 | 262 | } |
263 | 263 | } |
264 | 264 | |
@@ -343,13 +343,13 @@ discard block |
||
343 | 343 | return $node->getFileInfo()->isEncrypted() ? '1' : '0'; |
344 | 344 | }); |
345 | 345 | |
346 | - $propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function () use ($node) { |
|
346 | + $propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function() use ($node) { |
|
347 | 347 | return json_encode($this->previewManager->isAvailable($node->getFileInfo())); |
348 | 348 | }); |
349 | 349 | $propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) { |
350 | 350 | return $node->getSize(); |
351 | 351 | }); |
352 | - $propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function () use ($node) { |
|
352 | + $propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function() use ($node) { |
|
353 | 353 | return $node->getFileInfo()->getMountPoint()->getMountType(); |
354 | 354 | }); |
355 | 355 | } |
@@ -437,7 +437,7 @@ discard block |
||
437 | 437 | list($path, $name) = \Sabre\Uri\split($filePath); |
438 | 438 | $info = \OC_FileChunking::decodeName($name); |
439 | 439 | if (!empty($info)) { |
440 | - $filePath = $path . '/' . $info['name']; |
|
440 | + $filePath = $path.'/'.$info['name']; |
|
441 | 441 | } |
442 | 442 | } |
443 | 443 |
@@ -31,110 +31,110 @@ |
||
31 | 31 | |
32 | 32 | class BackupCodesProvider implements IProvider { |
33 | 33 | |
34 | - /** @var string */ |
|
35 | - private $appName; |
|
36 | - |
|
37 | - /** @var BackupCodeStorage */ |
|
38 | - private $storage; |
|
39 | - |
|
40 | - /** @var IL10N */ |
|
41 | - private $l10n; |
|
42 | - |
|
43 | - /** @var AppManager */ |
|
44 | - private $appManager; |
|
45 | - |
|
46 | - /** |
|
47 | - * @param string $appName |
|
48 | - * @param BackupCodeStorage $storage |
|
49 | - * @param IL10N $l10n |
|
50 | - * @param AppManager $appManager |
|
51 | - */ |
|
52 | - public function __construct($appName, BackupCodeStorage $storage, IL10N $l10n, AppManager $appManager) { |
|
53 | - $this->appName = $appName; |
|
54 | - $this->l10n = $l10n; |
|
55 | - $this->storage = $storage; |
|
56 | - $this->appManager = $appManager; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Get unique identifier of this 2FA provider |
|
61 | - * |
|
62 | - * @return string |
|
63 | - */ |
|
64 | - public function getId() { |
|
65 | - return 'backup_codes'; |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Get the display name for selecting the 2FA provider |
|
70 | - * |
|
71 | - * @return string |
|
72 | - */ |
|
73 | - public function getDisplayName() { |
|
74 | - return $this->l10n->t('Backup code'); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Get the description for selecting the 2FA provider |
|
79 | - * |
|
80 | - * @return string |
|
81 | - */ |
|
82 | - public function getDescription() { |
|
83 | - return $this->l10n->t('Use backup code'); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Get the template for rending the 2FA provider view |
|
88 | - * |
|
89 | - * @param IUser $user |
|
90 | - * @return Template |
|
91 | - */ |
|
92 | - public function getTemplate(IUser $user) { |
|
93 | - return new Template('twofactor_backupcodes', 'challenge'); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Verify the given challenge |
|
98 | - * |
|
99 | - * @param IUser $user |
|
100 | - * @param string $challenge |
|
101 | - */ |
|
102 | - public function verifyChallenge(IUser $user, $challenge) { |
|
103 | - return $this->storage->validateCode($user, $challenge); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Decides whether 2FA is enabled for the given user |
|
108 | - * |
|
109 | - * @param IUser $user |
|
110 | - * @return boolean |
|
111 | - */ |
|
112 | - public function isTwoFactorAuthEnabledForUser(IUser $user) { |
|
113 | - return $this->storage->hasBackupCodes($user); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Determine whether backup codes should be active or not |
|
118 | - * |
|
119 | - * Backup codes only make sense if at least one 2FA provider is active, |
|
120 | - * hence this method checks all enabled apps on whether they provide 2FA |
|
121 | - * functionality or not. If there's at least one app, backup codes are |
|
122 | - * enabled on the personal settings page. |
|
123 | - * |
|
124 | - * @param IUser $user |
|
125 | - * @return boolean |
|
126 | - */ |
|
127 | - public function isActive(IUser $user) { |
|
128 | - $appIds = array_filter($this->appManager->getEnabledAppsForUser($user), function($appId) { |
|
129 | - return $appId !== $this->appName; |
|
130 | - }); |
|
131 | - foreach ($appIds as $appId) { |
|
132 | - $info = $this->appManager->getAppInfo($appId); |
|
133 | - if (isset($info['two-factor-providers']) && count($info['two-factor-providers']) > 0) { |
|
134 | - return true; |
|
135 | - } |
|
136 | - } |
|
137 | - return false; |
|
138 | - } |
|
34 | + /** @var string */ |
|
35 | + private $appName; |
|
36 | + |
|
37 | + /** @var BackupCodeStorage */ |
|
38 | + private $storage; |
|
39 | + |
|
40 | + /** @var IL10N */ |
|
41 | + private $l10n; |
|
42 | + |
|
43 | + /** @var AppManager */ |
|
44 | + private $appManager; |
|
45 | + |
|
46 | + /** |
|
47 | + * @param string $appName |
|
48 | + * @param BackupCodeStorage $storage |
|
49 | + * @param IL10N $l10n |
|
50 | + * @param AppManager $appManager |
|
51 | + */ |
|
52 | + public function __construct($appName, BackupCodeStorage $storage, IL10N $l10n, AppManager $appManager) { |
|
53 | + $this->appName = $appName; |
|
54 | + $this->l10n = $l10n; |
|
55 | + $this->storage = $storage; |
|
56 | + $this->appManager = $appManager; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Get unique identifier of this 2FA provider |
|
61 | + * |
|
62 | + * @return string |
|
63 | + */ |
|
64 | + public function getId() { |
|
65 | + return 'backup_codes'; |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Get the display name for selecting the 2FA provider |
|
70 | + * |
|
71 | + * @return string |
|
72 | + */ |
|
73 | + public function getDisplayName() { |
|
74 | + return $this->l10n->t('Backup code'); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Get the description for selecting the 2FA provider |
|
79 | + * |
|
80 | + * @return string |
|
81 | + */ |
|
82 | + public function getDescription() { |
|
83 | + return $this->l10n->t('Use backup code'); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Get the template for rending the 2FA provider view |
|
88 | + * |
|
89 | + * @param IUser $user |
|
90 | + * @return Template |
|
91 | + */ |
|
92 | + public function getTemplate(IUser $user) { |
|
93 | + return new Template('twofactor_backupcodes', 'challenge'); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Verify the given challenge |
|
98 | + * |
|
99 | + * @param IUser $user |
|
100 | + * @param string $challenge |
|
101 | + */ |
|
102 | + public function verifyChallenge(IUser $user, $challenge) { |
|
103 | + return $this->storage->validateCode($user, $challenge); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Decides whether 2FA is enabled for the given user |
|
108 | + * |
|
109 | + * @param IUser $user |
|
110 | + * @return boolean |
|
111 | + */ |
|
112 | + public function isTwoFactorAuthEnabledForUser(IUser $user) { |
|
113 | + return $this->storage->hasBackupCodes($user); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Determine whether backup codes should be active or not |
|
118 | + * |
|
119 | + * Backup codes only make sense if at least one 2FA provider is active, |
|
120 | + * hence this method checks all enabled apps on whether they provide 2FA |
|
121 | + * functionality or not. If there's at least one app, backup codes are |
|
122 | + * enabled on the personal settings page. |
|
123 | + * |
|
124 | + * @param IUser $user |
|
125 | + * @return boolean |
|
126 | + */ |
|
127 | + public function isActive(IUser $user) { |
|
128 | + $appIds = array_filter($this->appManager->getEnabledAppsForUser($user), function($appId) { |
|
129 | + return $appId !== $this->appName; |
|
130 | + }); |
|
131 | + foreach ($appIds as $appId) { |
|
132 | + $info = $this->appManager->getAppInfo($appId); |
|
133 | + if (isset($info['two-factor-providers']) && count($info['two-factor-providers']) > 0) { |
|
134 | + return true; |
|
135 | + } |
|
136 | + } |
|
137 | + return false; |
|
138 | + } |
|
139 | 139 | |
140 | 140 | } |
@@ -30,77 +30,77 @@ |
||
30 | 30 | |
31 | 31 | class Folder extends File implements \OCP\Share_Backend_Collection { |
32 | 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 = array(); |
|
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']; |
|
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 = array(); |
|
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 | 56 | |
57 | - $result[] = $share; |
|
58 | - } |
|
59 | - } |
|
60 | - $parent = $this->getParentId($parent); |
|
61 | - } |
|
57 | + $result[] = $share; |
|
58 | + } |
|
59 | + } |
|
60 | + $parent = $this->getParentId($parent); |
|
61 | + } |
|
62 | 62 | |
63 | - return $result; |
|
64 | - } |
|
63 | + return $result; |
|
64 | + } |
|
65 | 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(array($child)); |
|
75 | - $row = $result->fetchRow(); |
|
76 | - return $row ? $row['parent'] : null; |
|
77 | - } |
|
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(array($child)); |
|
75 | + $row = $result->fetchRow(); |
|
76 | + return $row ? $row['parent'] : null; |
|
77 | + } |
|
78 | 78 | |
79 | - public function getChildren($itemSource) { |
|
80 | - $children = array(); |
|
81 | - $parents = array($itemSource); |
|
82 | - $query = \OCP\DB::prepare('SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = ?'); |
|
83 | - $result = $query->execute(array('httpd/unix-directory')); |
|
84 | - if ($row = $result->fetchRow()) { |
|
85 | - $mimetype = (int) $row['id']; |
|
86 | - } else { |
|
87 | - $mimetype = -1; |
|
88 | - } |
|
89 | - while (!empty($parents)) { |
|
90 | - $parents = "'".implode("','", $parents)."'"; |
|
91 | - $query = \OCP\DB::prepare('SELECT `fileid`, `name`, `mimetype` FROM `*PREFIX*filecache`' |
|
92 | - .' WHERE `parent` IN ('.$parents.')'); |
|
93 | - $result = $query->execute(); |
|
94 | - $parents = array(); |
|
95 | - while ($file = $result->fetchRow()) { |
|
96 | - $children[] = array('source' => $file['fileid'], 'file_path' => $file['name']); |
|
97 | - // If a child folder is found look inside it |
|
98 | - if ((int) $file['mimetype'] === $mimetype) { |
|
99 | - $parents[] = $file['fileid']; |
|
100 | - } |
|
101 | - } |
|
102 | - } |
|
103 | - return $children; |
|
104 | - } |
|
79 | + public function getChildren($itemSource) { |
|
80 | + $children = array(); |
|
81 | + $parents = array($itemSource); |
|
82 | + $query = \OCP\DB::prepare('SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = ?'); |
|
83 | + $result = $query->execute(array('httpd/unix-directory')); |
|
84 | + if ($row = $result->fetchRow()) { |
|
85 | + $mimetype = (int) $row['id']; |
|
86 | + } else { |
|
87 | + $mimetype = -1; |
|
88 | + } |
|
89 | + while (!empty($parents)) { |
|
90 | + $parents = "'".implode("','", $parents)."'"; |
|
91 | + $query = \OCP\DB::prepare('SELECT `fileid`, `name`, `mimetype` FROM `*PREFIX*filecache`' |
|
92 | + .' WHERE `parent` IN ('.$parents.')'); |
|
93 | + $result = $query->execute(); |
|
94 | + $parents = array(); |
|
95 | + while ($file = $result->fetchRow()) { |
|
96 | + $children[] = array('source' => $file['fileid'], 'file_path' => $file['name']); |
|
97 | + // If a child folder is found look inside it |
|
98 | + if ((int) $file['mimetype'] === $mimetype) { |
|
99 | + $parents[] = $file['fileid']; |
|
100 | + } |
|
101 | + } |
|
102 | + } |
|
103 | + return $children; |
|
104 | + } |
|
105 | 105 | |
106 | 106 | } |
@@ -39,204 +39,204 @@ |
||
39 | 39 | use OCP\Share\IManager; |
40 | 40 | |
41 | 41 | class ShareesAPIController extends OCSController { |
42 | - /** @var IConfig */ |
|
43 | - protected $config; |
|
44 | - |
|
45 | - /** @var IURLGenerator */ |
|
46 | - protected $urlGenerator; |
|
47 | - |
|
48 | - /** @var IManager */ |
|
49 | - protected $shareManager; |
|
50 | - |
|
51 | - /** @var bool */ |
|
52 | - protected $shareWithGroupOnly = false; |
|
53 | - |
|
54 | - /** @var bool */ |
|
55 | - protected $shareeEnumeration = true; |
|
56 | - |
|
57 | - /** @var int */ |
|
58 | - protected $offset = 0; |
|
59 | - |
|
60 | - /** @var int */ |
|
61 | - protected $limit = 10; |
|
62 | - |
|
63 | - /** @var array */ |
|
64 | - protected $result = [ |
|
65 | - 'exact' => [ |
|
66 | - 'users' => [], |
|
67 | - 'groups' => [], |
|
68 | - 'remotes' => [], |
|
69 | - 'emails' => [], |
|
70 | - 'circles' => [], |
|
71 | - ], |
|
72 | - 'users' => [], |
|
73 | - 'groups' => [], |
|
74 | - 'remotes' => [], |
|
75 | - 'emails' => [], |
|
76 | - 'lookup' => [], |
|
77 | - 'circles' => [], |
|
78 | - ]; |
|
79 | - |
|
80 | - protected $reachedEndFor = []; |
|
81 | - /** @var ISearch */ |
|
82 | - private $collaboratorSearch; |
|
83 | - |
|
84 | - /** |
|
85 | - * @param string $appName |
|
86 | - * @param IRequest $request |
|
87 | - * @param IConfig $config |
|
88 | - * @param IURLGenerator $urlGenerator |
|
89 | - * @param IManager $shareManager |
|
90 | - * @param ISearch $collaboratorSearch |
|
91 | - */ |
|
92 | - public function __construct( |
|
93 | - $appName, |
|
94 | - IRequest $request, |
|
95 | - IConfig $config, |
|
96 | - IURLGenerator $urlGenerator, |
|
97 | - IManager $shareManager, |
|
98 | - ISearch $collaboratorSearch |
|
99 | - ) { |
|
100 | - parent::__construct($appName, $request); |
|
101 | - |
|
102 | - $this->config = $config; |
|
103 | - $this->urlGenerator = $urlGenerator; |
|
104 | - $this->shareManager = $shareManager; |
|
105 | - $this->collaboratorSearch = $collaboratorSearch; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * @NoAdminRequired |
|
110 | - * |
|
111 | - * @param string $search |
|
112 | - * @param string $itemType |
|
113 | - * @param int $page |
|
114 | - * @param int $perPage |
|
115 | - * @param int|int[] $shareType |
|
116 | - * @param bool $lookup |
|
117 | - * @return DataResponse |
|
118 | - * @throws OCSBadRequestException |
|
119 | - */ |
|
120 | - public function search($search = '', $itemType = null, $page = 1, $perPage = 200, $shareType = null, $lookup = true) { |
|
121 | - |
|
122 | - // only search for string larger than a given threshold |
|
123 | - $threshold = (int)$this->config->getSystemValue('sharing.minSearchStringLength', 0); |
|
124 | - if (strlen($search) < $threshold) { |
|
125 | - return new DataResponse($this->result); |
|
126 | - } |
|
127 | - |
|
128 | - // never return more than the max. number of results configured in the config.php |
|
129 | - $maxResults = (int)$this->config->getSystemValue('sharing.maxAutocompleteResults', 0); |
|
130 | - if ($maxResults > 0) { |
|
131 | - $perPage = min($perPage, $maxResults); |
|
132 | - } |
|
133 | - if ($perPage <= 0) { |
|
134 | - throw new OCSBadRequestException('Invalid perPage argument'); |
|
135 | - } |
|
136 | - if ($page <= 0) { |
|
137 | - throw new OCSBadRequestException('Invalid page'); |
|
138 | - } |
|
139 | - |
|
140 | - $shareTypes = [ |
|
141 | - Share::SHARE_TYPE_USER, |
|
142 | - ]; |
|
143 | - |
|
144 | - if ($itemType === null) { |
|
145 | - throw new OCSBadRequestException('Missing itemType'); |
|
146 | - } elseif ($itemType === 'file' || $itemType === 'folder') { |
|
147 | - if ($this->shareManager->allowGroupSharing()) { |
|
148 | - $shareTypes[] = Share::SHARE_TYPE_GROUP; |
|
149 | - } |
|
150 | - |
|
151 | - if ($this->isRemoteSharingAllowed($itemType)) { |
|
152 | - $shareTypes[] = Share::SHARE_TYPE_REMOTE; |
|
153 | - } |
|
154 | - |
|
155 | - if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { |
|
156 | - $shareTypes[] = Share::SHARE_TYPE_EMAIL; |
|
157 | - } |
|
158 | - } else { |
|
159 | - $shareTypes[] = Share::SHARE_TYPE_GROUP; |
|
160 | - $shareTypes[] = Share::SHARE_TYPE_EMAIL; |
|
161 | - } |
|
162 | - |
|
163 | - // FIXME: DI |
|
164 | - if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
165 | - $shareTypes[] = Share::SHARE_TYPE_CIRCLE; |
|
166 | - } |
|
167 | - |
|
168 | - if (isset($_GET['shareType']) && is_array($_GET['shareType'])) { |
|
169 | - $shareTypes = array_intersect($shareTypes, $_GET['shareType']); |
|
170 | - sort($shareTypes); |
|
171 | - } else if (is_numeric($shareType)) { |
|
172 | - $shareTypes = array_intersect($shareTypes, [(int) $shareType]); |
|
173 | - sort($shareTypes); |
|
174 | - } |
|
175 | - |
|
176 | - $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
177 | - $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
178 | - $this->limit = (int) $perPage; |
|
179 | - $this->offset = $perPage * ($page - 1); |
|
180 | - |
|
181 | - list($result, $hasMoreResults) = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset); |
|
182 | - |
|
183 | - // extra treatment for 'exact' subarray, with a single merge expected keys might be lost |
|
184 | - if(isset($result['exact'])) { |
|
185 | - $result['exact'] = array_merge($this->result['exact'], $result['exact']); |
|
186 | - } |
|
187 | - $this->result = array_merge($this->result, $result); |
|
188 | - $response = new DataResponse($this->result); |
|
189 | - |
|
190 | - if ($hasMoreResults) { |
|
191 | - $response->addHeader('Link', $this->getPaginationLink($page, [ |
|
192 | - 'search' => $search, |
|
193 | - 'itemType' => $itemType, |
|
194 | - 'shareType' => $shareTypes, |
|
195 | - 'perPage' => $perPage, |
|
196 | - ])); |
|
197 | - } |
|
198 | - |
|
199 | - return $response; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Method to get out the static call for better testing |
|
204 | - * |
|
205 | - * @param string $itemType |
|
206 | - * @return bool |
|
207 | - */ |
|
208 | - protected function isRemoteSharingAllowed($itemType) { |
|
209 | - try { |
|
210 | - // FIXME: static foo makes unit testing unnecessarily difficult |
|
211 | - $backend = \OC\Share\Share::getBackend($itemType); |
|
212 | - return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE); |
|
213 | - } catch (\Exception $e) { |
|
214 | - return false; |
|
215 | - } |
|
216 | - } |
|
217 | - |
|
218 | - |
|
219 | - /** |
|
220 | - * Generates a bunch of pagination links for the current page |
|
221 | - * |
|
222 | - * @param int $page Current page |
|
223 | - * @param array $params Parameters for the URL |
|
224 | - * @return string |
|
225 | - */ |
|
226 | - protected function getPaginationLink($page, array $params) { |
|
227 | - if ($this->isV2()) { |
|
228 | - $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
229 | - } else { |
|
230 | - $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
231 | - } |
|
232 | - $params['page'] = $page + 1; |
|
233 | - return '<' . $url . http_build_query($params) . '>; rel="next"'; |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * @return bool |
|
238 | - */ |
|
239 | - protected function isV2() { |
|
240 | - return $this->request->getScriptName() === '/ocs/v2.php'; |
|
241 | - } |
|
42 | + /** @var IConfig */ |
|
43 | + protected $config; |
|
44 | + |
|
45 | + /** @var IURLGenerator */ |
|
46 | + protected $urlGenerator; |
|
47 | + |
|
48 | + /** @var IManager */ |
|
49 | + protected $shareManager; |
|
50 | + |
|
51 | + /** @var bool */ |
|
52 | + protected $shareWithGroupOnly = false; |
|
53 | + |
|
54 | + /** @var bool */ |
|
55 | + protected $shareeEnumeration = true; |
|
56 | + |
|
57 | + /** @var int */ |
|
58 | + protected $offset = 0; |
|
59 | + |
|
60 | + /** @var int */ |
|
61 | + protected $limit = 10; |
|
62 | + |
|
63 | + /** @var array */ |
|
64 | + protected $result = [ |
|
65 | + 'exact' => [ |
|
66 | + 'users' => [], |
|
67 | + 'groups' => [], |
|
68 | + 'remotes' => [], |
|
69 | + 'emails' => [], |
|
70 | + 'circles' => [], |
|
71 | + ], |
|
72 | + 'users' => [], |
|
73 | + 'groups' => [], |
|
74 | + 'remotes' => [], |
|
75 | + 'emails' => [], |
|
76 | + 'lookup' => [], |
|
77 | + 'circles' => [], |
|
78 | + ]; |
|
79 | + |
|
80 | + protected $reachedEndFor = []; |
|
81 | + /** @var ISearch */ |
|
82 | + private $collaboratorSearch; |
|
83 | + |
|
84 | + /** |
|
85 | + * @param string $appName |
|
86 | + * @param IRequest $request |
|
87 | + * @param IConfig $config |
|
88 | + * @param IURLGenerator $urlGenerator |
|
89 | + * @param IManager $shareManager |
|
90 | + * @param ISearch $collaboratorSearch |
|
91 | + */ |
|
92 | + public function __construct( |
|
93 | + $appName, |
|
94 | + IRequest $request, |
|
95 | + IConfig $config, |
|
96 | + IURLGenerator $urlGenerator, |
|
97 | + IManager $shareManager, |
|
98 | + ISearch $collaboratorSearch |
|
99 | + ) { |
|
100 | + parent::__construct($appName, $request); |
|
101 | + |
|
102 | + $this->config = $config; |
|
103 | + $this->urlGenerator = $urlGenerator; |
|
104 | + $this->shareManager = $shareManager; |
|
105 | + $this->collaboratorSearch = $collaboratorSearch; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * @NoAdminRequired |
|
110 | + * |
|
111 | + * @param string $search |
|
112 | + * @param string $itemType |
|
113 | + * @param int $page |
|
114 | + * @param int $perPage |
|
115 | + * @param int|int[] $shareType |
|
116 | + * @param bool $lookup |
|
117 | + * @return DataResponse |
|
118 | + * @throws OCSBadRequestException |
|
119 | + */ |
|
120 | + public function search($search = '', $itemType = null, $page = 1, $perPage = 200, $shareType = null, $lookup = true) { |
|
121 | + |
|
122 | + // only search for string larger than a given threshold |
|
123 | + $threshold = (int)$this->config->getSystemValue('sharing.minSearchStringLength', 0); |
|
124 | + if (strlen($search) < $threshold) { |
|
125 | + return new DataResponse($this->result); |
|
126 | + } |
|
127 | + |
|
128 | + // never return more than the max. number of results configured in the config.php |
|
129 | + $maxResults = (int)$this->config->getSystemValue('sharing.maxAutocompleteResults', 0); |
|
130 | + if ($maxResults > 0) { |
|
131 | + $perPage = min($perPage, $maxResults); |
|
132 | + } |
|
133 | + if ($perPage <= 0) { |
|
134 | + throw new OCSBadRequestException('Invalid perPage argument'); |
|
135 | + } |
|
136 | + if ($page <= 0) { |
|
137 | + throw new OCSBadRequestException('Invalid page'); |
|
138 | + } |
|
139 | + |
|
140 | + $shareTypes = [ |
|
141 | + Share::SHARE_TYPE_USER, |
|
142 | + ]; |
|
143 | + |
|
144 | + if ($itemType === null) { |
|
145 | + throw new OCSBadRequestException('Missing itemType'); |
|
146 | + } elseif ($itemType === 'file' || $itemType === 'folder') { |
|
147 | + if ($this->shareManager->allowGroupSharing()) { |
|
148 | + $shareTypes[] = Share::SHARE_TYPE_GROUP; |
|
149 | + } |
|
150 | + |
|
151 | + if ($this->isRemoteSharingAllowed($itemType)) { |
|
152 | + $shareTypes[] = Share::SHARE_TYPE_REMOTE; |
|
153 | + } |
|
154 | + |
|
155 | + if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { |
|
156 | + $shareTypes[] = Share::SHARE_TYPE_EMAIL; |
|
157 | + } |
|
158 | + } else { |
|
159 | + $shareTypes[] = Share::SHARE_TYPE_GROUP; |
|
160 | + $shareTypes[] = Share::SHARE_TYPE_EMAIL; |
|
161 | + } |
|
162 | + |
|
163 | + // FIXME: DI |
|
164 | + if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
165 | + $shareTypes[] = Share::SHARE_TYPE_CIRCLE; |
|
166 | + } |
|
167 | + |
|
168 | + if (isset($_GET['shareType']) && is_array($_GET['shareType'])) { |
|
169 | + $shareTypes = array_intersect($shareTypes, $_GET['shareType']); |
|
170 | + sort($shareTypes); |
|
171 | + } else if (is_numeric($shareType)) { |
|
172 | + $shareTypes = array_intersect($shareTypes, [(int) $shareType]); |
|
173 | + sort($shareTypes); |
|
174 | + } |
|
175 | + |
|
176 | + $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
177 | + $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
178 | + $this->limit = (int) $perPage; |
|
179 | + $this->offset = $perPage * ($page - 1); |
|
180 | + |
|
181 | + list($result, $hasMoreResults) = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset); |
|
182 | + |
|
183 | + // extra treatment for 'exact' subarray, with a single merge expected keys might be lost |
|
184 | + if(isset($result['exact'])) { |
|
185 | + $result['exact'] = array_merge($this->result['exact'], $result['exact']); |
|
186 | + } |
|
187 | + $this->result = array_merge($this->result, $result); |
|
188 | + $response = new DataResponse($this->result); |
|
189 | + |
|
190 | + if ($hasMoreResults) { |
|
191 | + $response->addHeader('Link', $this->getPaginationLink($page, [ |
|
192 | + 'search' => $search, |
|
193 | + 'itemType' => $itemType, |
|
194 | + 'shareType' => $shareTypes, |
|
195 | + 'perPage' => $perPage, |
|
196 | + ])); |
|
197 | + } |
|
198 | + |
|
199 | + return $response; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Method to get out the static call for better testing |
|
204 | + * |
|
205 | + * @param string $itemType |
|
206 | + * @return bool |
|
207 | + */ |
|
208 | + protected function isRemoteSharingAllowed($itemType) { |
|
209 | + try { |
|
210 | + // FIXME: static foo makes unit testing unnecessarily difficult |
|
211 | + $backend = \OC\Share\Share::getBackend($itemType); |
|
212 | + return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE); |
|
213 | + } catch (\Exception $e) { |
|
214 | + return false; |
|
215 | + } |
|
216 | + } |
|
217 | + |
|
218 | + |
|
219 | + /** |
|
220 | + * Generates a bunch of pagination links for the current page |
|
221 | + * |
|
222 | + * @param int $page Current page |
|
223 | + * @param array $params Parameters for the URL |
|
224 | + * @return string |
|
225 | + */ |
|
226 | + protected function getPaginationLink($page, array $params) { |
|
227 | + if ($this->isV2()) { |
|
228 | + $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
229 | + } else { |
|
230 | + $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
231 | + } |
|
232 | + $params['page'] = $page + 1; |
|
233 | + return '<' . $url . http_build_query($params) . '>; rel="next"'; |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * @return bool |
|
238 | + */ |
|
239 | + protected function isV2() { |
|
240 | + return $this->request->getScriptName() === '/ocs/v2.php'; |
|
241 | + } |
|
242 | 242 | } |
@@ -120,13 +120,13 @@ discard block |
||
120 | 120 | public function search($search = '', $itemType = null, $page = 1, $perPage = 200, $shareType = null, $lookup = true) { |
121 | 121 | |
122 | 122 | // only search for string larger than a given threshold |
123 | - $threshold = (int)$this->config->getSystemValue('sharing.minSearchStringLength', 0); |
|
123 | + $threshold = (int) $this->config->getSystemValue('sharing.minSearchStringLength', 0); |
|
124 | 124 | if (strlen($search) < $threshold) { |
125 | 125 | return new DataResponse($this->result); |
126 | 126 | } |
127 | 127 | |
128 | 128 | // never return more than the max. number of results configured in the config.php |
129 | - $maxResults = (int)$this->config->getSystemValue('sharing.maxAutocompleteResults', 0); |
|
129 | + $maxResults = (int) $this->config->getSystemValue('sharing.maxAutocompleteResults', 0); |
|
130 | 130 | if ($maxResults > 0) { |
131 | 131 | $perPage = min($perPage, $maxResults); |
132 | 132 | } |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | list($result, $hasMoreResults) = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset); |
182 | 182 | |
183 | 183 | // extra treatment for 'exact' subarray, with a single merge expected keys might be lost |
184 | - if(isset($result['exact'])) { |
|
184 | + if (isset($result['exact'])) { |
|
185 | 185 | $result['exact'] = array_merge($this->result['exact'], $result['exact']); |
186 | 186 | } |
187 | 187 | $this->result = array_merge($this->result, $result); |
@@ -225,12 +225,12 @@ discard block |
||
225 | 225 | */ |
226 | 226 | protected function getPaginationLink($page, array $params) { |
227 | 227 | if ($this->isV2()) { |
228 | - $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
228 | + $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees').'?'; |
|
229 | 229 | } else { |
230 | - $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
230 | + $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees').'?'; |
|
231 | 231 | } |
232 | 232 | $params['page'] = $page + 1; |
233 | - return '<' . $url . http_build_query($params) . '>; rel="next"'; |
|
233 | + return '<'.$url.http_build_query($params).'>; rel="next"'; |
|
234 | 234 | } |
235 | 235 | |
236 | 236 | /** |
@@ -40,104 +40,104 @@ |
||
40 | 40 | * (aka personal storages) |
41 | 41 | */ |
42 | 42 | class UserStoragesService extends StoragesService { |
43 | - use UserTrait; |
|
43 | + use UserTrait; |
|
44 | 44 | |
45 | - /** |
|
46 | - * Create a user storages service |
|
47 | - * |
|
48 | - * @param BackendService $backendService |
|
49 | - * @param DBConfigService $dbConfig |
|
50 | - * @param IUserSession $userSession user session |
|
51 | - * @param IUserMountCache $userMountCache |
|
52 | - */ |
|
53 | - public function __construct( |
|
54 | - BackendService $backendService, |
|
55 | - DBConfigService $dbConfig, |
|
56 | - IUserSession $userSession, |
|
57 | - IUserMountCache $userMountCache |
|
58 | - ) { |
|
59 | - $this->userSession = $userSession; |
|
60 | - parent::__construct($backendService, $dbConfig, $userMountCache); |
|
61 | - } |
|
45 | + /** |
|
46 | + * Create a user storages service |
|
47 | + * |
|
48 | + * @param BackendService $backendService |
|
49 | + * @param DBConfigService $dbConfig |
|
50 | + * @param IUserSession $userSession user session |
|
51 | + * @param IUserMountCache $userMountCache |
|
52 | + */ |
|
53 | + public function __construct( |
|
54 | + BackendService $backendService, |
|
55 | + DBConfigService $dbConfig, |
|
56 | + IUserSession $userSession, |
|
57 | + IUserMountCache $userMountCache |
|
58 | + ) { |
|
59 | + $this->userSession = $userSession; |
|
60 | + parent::__construct($backendService, $dbConfig, $userMountCache); |
|
61 | + } |
|
62 | 62 | |
63 | - protected function readDBConfig() { |
|
64 | - return $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID()); |
|
65 | - } |
|
63 | + protected function readDBConfig() { |
|
64 | + return $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID()); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Triggers $signal for all applicable users of the given |
|
69 | - * storage |
|
70 | - * |
|
71 | - * @param StorageConfig $storage storage data |
|
72 | - * @param string $signal signal to trigger |
|
73 | - */ |
|
74 | - protected function triggerHooks(StorageConfig $storage, $signal) { |
|
75 | - $user = $this->getUser()->getUID(); |
|
67 | + /** |
|
68 | + * Triggers $signal for all applicable users of the given |
|
69 | + * storage |
|
70 | + * |
|
71 | + * @param StorageConfig $storage storage data |
|
72 | + * @param string $signal signal to trigger |
|
73 | + */ |
|
74 | + protected function triggerHooks(StorageConfig $storage, $signal) { |
|
75 | + $user = $this->getUser()->getUID(); |
|
76 | 76 | |
77 | - // trigger hook for the current user |
|
78 | - $this->triggerApplicableHooks( |
|
79 | - $signal, |
|
80 | - $storage->getMountPoint(), |
|
81 | - \OC_Mount_Config::MOUNT_TYPE_USER, |
|
82 | - [$user] |
|
83 | - ); |
|
84 | - } |
|
77 | + // trigger hook for the current user |
|
78 | + $this->triggerApplicableHooks( |
|
79 | + $signal, |
|
80 | + $storage->getMountPoint(), |
|
81 | + \OC_Mount_Config::MOUNT_TYPE_USER, |
|
82 | + [$user] |
|
83 | + ); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Triggers signal_create_mount or signal_delete_mount to |
|
88 | - * accommodate for additions/deletions in applicableUsers |
|
89 | - * and applicableGroups fields. |
|
90 | - * |
|
91 | - * @param StorageConfig $oldStorage old storage data |
|
92 | - * @param StorageConfig $newStorage new storage data |
|
93 | - */ |
|
94 | - protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) { |
|
95 | - // if mount point changed, it's like a deletion + creation |
|
96 | - if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) { |
|
97 | - $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount); |
|
98 | - $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
99 | - } |
|
100 | - } |
|
86 | + /** |
|
87 | + * Triggers signal_create_mount or signal_delete_mount to |
|
88 | + * accommodate for additions/deletions in applicableUsers |
|
89 | + * and applicableGroups fields. |
|
90 | + * |
|
91 | + * @param StorageConfig $oldStorage old storage data |
|
92 | + * @param StorageConfig $newStorage new storage data |
|
93 | + */ |
|
94 | + protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) { |
|
95 | + // if mount point changed, it's like a deletion + creation |
|
96 | + if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) { |
|
97 | + $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount); |
|
98 | + $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
99 | + } |
|
100 | + } |
|
101 | 101 | |
102 | - protected function getType() { |
|
103 | - return DBConfigService::MOUNT_TYPE_PERSONAl; |
|
104 | - } |
|
102 | + protected function getType() { |
|
103 | + return DBConfigService::MOUNT_TYPE_PERSONAl; |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Add new storage to the configuration |
|
108 | - * |
|
109 | - * @param StorageConfig $newStorage storage attributes |
|
110 | - * |
|
111 | - * @return StorageConfig storage config, with added id |
|
112 | - */ |
|
113 | - public function addStorage(StorageConfig $newStorage) { |
|
114 | - $newStorage->setApplicableUsers([$this->getUser()->getUID()]); |
|
115 | - return parent::addStorage($newStorage); |
|
116 | - } |
|
106 | + /** |
|
107 | + * Add new storage to the configuration |
|
108 | + * |
|
109 | + * @param StorageConfig $newStorage storage attributes |
|
110 | + * |
|
111 | + * @return StorageConfig storage config, with added id |
|
112 | + */ |
|
113 | + public function addStorage(StorageConfig $newStorage) { |
|
114 | + $newStorage->setApplicableUsers([$this->getUser()->getUID()]); |
|
115 | + return parent::addStorage($newStorage); |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * Update storage to the configuration |
|
120 | - * |
|
121 | - * @param StorageConfig $updatedStorage storage attributes |
|
122 | - * |
|
123 | - * @return StorageConfig storage config |
|
124 | - * @throws NotFoundException if the given storage does not exist in the config |
|
125 | - */ |
|
126 | - public function updateStorage(StorageConfig $updatedStorage) { |
|
127 | - $updatedStorage->setApplicableUsers([$this->getUser()->getUID()]); |
|
128 | - return parent::updateStorage($updatedStorage); |
|
129 | - } |
|
118 | + /** |
|
119 | + * Update storage to the configuration |
|
120 | + * |
|
121 | + * @param StorageConfig $updatedStorage storage attributes |
|
122 | + * |
|
123 | + * @return StorageConfig storage config |
|
124 | + * @throws NotFoundException if the given storage does not exist in the config |
|
125 | + */ |
|
126 | + public function updateStorage(StorageConfig $updatedStorage) { |
|
127 | + $updatedStorage->setApplicableUsers([$this->getUser()->getUID()]); |
|
128 | + return parent::updateStorage($updatedStorage); |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * Get the visibility type for this controller, used in validation |
|
133 | - * |
|
134 | - * @return string BackendService::VISIBILITY_* constants |
|
135 | - */ |
|
136 | - public function getVisibilityType() { |
|
137 | - return BackendService::VISIBILITY_PERSONAL; |
|
138 | - } |
|
131 | + /** |
|
132 | + * Get the visibility type for this controller, used in validation |
|
133 | + * |
|
134 | + * @return string BackendService::VISIBILITY_* constants |
|
135 | + */ |
|
136 | + public function getVisibilityType() { |
|
137 | + return BackendService::VISIBILITY_PERSONAL; |
|
138 | + } |
|
139 | 139 | |
140 | - protected function isApplicable(StorageConfig $config) { |
|
141 | - return ($config->getApplicableUsers() === [$this->getUser()->getUID()]) && $config->getType() === StorageConfig::MOUNT_TYPE_PERSONAl; |
|
142 | - } |
|
140 | + protected function isApplicable(StorageConfig $config) { |
|
141 | + return ($config->getApplicableUsers() === [$this->getUser()->getUID()]) && $config->getType() === StorageConfig::MOUNT_TYPE_PERSONAl; |
|
142 | + } |
|
143 | 143 | } |
@@ -37,166 +37,166 @@ |
||
37 | 37 | use OCP\PreConditionNotMetException; |
38 | 38 | |
39 | 39 | class Util { |
40 | - /** |
|
41 | - * @var View |
|
42 | - */ |
|
43 | - private $files; |
|
44 | - /** |
|
45 | - * @var Crypt |
|
46 | - */ |
|
47 | - private $crypt; |
|
48 | - /** |
|
49 | - * @var ILogger |
|
50 | - */ |
|
51 | - private $logger; |
|
52 | - /** |
|
53 | - * @var bool|IUser |
|
54 | - */ |
|
55 | - private $user; |
|
56 | - /** |
|
57 | - * @var IConfig |
|
58 | - */ |
|
59 | - private $config; |
|
60 | - /** |
|
61 | - * @var IUserManager |
|
62 | - */ |
|
63 | - private $userManager; |
|
64 | - |
|
65 | - /** |
|
66 | - * Util constructor. |
|
67 | - * |
|
68 | - * @param View $files |
|
69 | - * @param Crypt $crypt |
|
70 | - * @param ILogger $logger |
|
71 | - * @param IUserSession $userSession |
|
72 | - * @param IConfig $config |
|
73 | - * @param IUserManager $userManager |
|
74 | - */ |
|
75 | - public function __construct(View $files, |
|
76 | - Crypt $crypt, |
|
77 | - ILogger $logger, |
|
78 | - IUserSession $userSession, |
|
79 | - IConfig $config, |
|
80 | - IUserManager $userManager |
|
81 | - ) { |
|
82 | - $this->files = $files; |
|
83 | - $this->crypt = $crypt; |
|
84 | - $this->logger = $logger; |
|
85 | - $this->user = $userSession && $userSession->isLoggedIn() ? $userSession->getUser() : false; |
|
86 | - $this->config = $config; |
|
87 | - $this->userManager = $userManager; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * check if recovery key is enabled for user |
|
92 | - * |
|
93 | - * @param string $uid |
|
94 | - * @return bool |
|
95 | - */ |
|
96 | - public function isRecoveryEnabledForUser($uid) { |
|
97 | - $recoveryMode = $this->config->getUserValue($uid, |
|
98 | - 'encryption', |
|
99 | - 'recoveryEnabled', |
|
100 | - '0'); |
|
101 | - |
|
102 | - return ($recoveryMode === '1'); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * check if the home storage should be encrypted |
|
107 | - * |
|
108 | - * @return bool |
|
109 | - */ |
|
110 | - public function shouldEncryptHomeStorage() { |
|
111 | - $encryptHomeStorage = $this->config->getAppValue( |
|
112 | - 'encryption', |
|
113 | - 'encryptHomeStorage', |
|
114 | - '1' |
|
115 | - ); |
|
116 | - |
|
117 | - return ($encryptHomeStorage === '1'); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * set the home storage encryption on/off |
|
122 | - * |
|
123 | - * @param bool $encryptHomeStorage |
|
124 | - */ |
|
125 | - public function setEncryptHomeStorage($encryptHomeStorage) { |
|
126 | - $value = $encryptHomeStorage ? '1' : '0'; |
|
127 | - $this->config->setAppValue( |
|
128 | - 'encryption', |
|
129 | - 'encryptHomeStorage', |
|
130 | - $value |
|
131 | - ); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * check if master key is enabled |
|
136 | - * |
|
137 | - * @return bool |
|
138 | - */ |
|
139 | - public function isMasterKeyEnabled() { |
|
140 | - $userMasterKey = $this->config->getAppValue('encryption', 'useMasterKey', '1'); |
|
141 | - return ($userMasterKey === '1'); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * @param $enabled |
|
146 | - * @return bool |
|
147 | - */ |
|
148 | - public function setRecoveryForUser($enabled) { |
|
149 | - $value = $enabled ? '1' : '0'; |
|
150 | - |
|
151 | - try { |
|
152 | - $this->config->setUserValue($this->user->getUID(), |
|
153 | - 'encryption', |
|
154 | - 'recoveryEnabled', |
|
155 | - $value); |
|
156 | - return true; |
|
157 | - } catch (PreConditionNotMetException $e) { |
|
158 | - return false; |
|
159 | - } |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * @param string $uid |
|
164 | - * @return bool |
|
165 | - */ |
|
166 | - public function userHasFiles($uid) { |
|
167 | - return $this->files->file_exists($uid . '/files'); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * get owner from give path, path relative to data/ expected |
|
172 | - * |
|
173 | - * @param string $path relative to data/ |
|
174 | - * @return string |
|
175 | - * @throws \BadMethodCallException |
|
176 | - */ |
|
177 | - public function getOwner($path) { |
|
178 | - $owner = ''; |
|
179 | - $parts = explode('/', $path, 3); |
|
180 | - if (count($parts) > 1) { |
|
181 | - $owner = $parts[1]; |
|
182 | - if ($this->userManager->userExists($owner) === false) { |
|
183 | - throw new \BadMethodCallException('Unknown user: ' . |
|
184 | - 'method expects path to a user folder relative to the data folder'); |
|
185 | - } |
|
186 | - |
|
187 | - } |
|
188 | - |
|
189 | - return $owner; |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * get storage of path |
|
194 | - * |
|
195 | - * @param string $path |
|
196 | - * @return \OC\Files\Storage\Storage |
|
197 | - */ |
|
198 | - public function getStorage($path) { |
|
199 | - return $this->files->getMount($path)->getStorage(); |
|
200 | - } |
|
40 | + /** |
|
41 | + * @var View |
|
42 | + */ |
|
43 | + private $files; |
|
44 | + /** |
|
45 | + * @var Crypt |
|
46 | + */ |
|
47 | + private $crypt; |
|
48 | + /** |
|
49 | + * @var ILogger |
|
50 | + */ |
|
51 | + private $logger; |
|
52 | + /** |
|
53 | + * @var bool|IUser |
|
54 | + */ |
|
55 | + private $user; |
|
56 | + /** |
|
57 | + * @var IConfig |
|
58 | + */ |
|
59 | + private $config; |
|
60 | + /** |
|
61 | + * @var IUserManager |
|
62 | + */ |
|
63 | + private $userManager; |
|
64 | + |
|
65 | + /** |
|
66 | + * Util constructor. |
|
67 | + * |
|
68 | + * @param View $files |
|
69 | + * @param Crypt $crypt |
|
70 | + * @param ILogger $logger |
|
71 | + * @param IUserSession $userSession |
|
72 | + * @param IConfig $config |
|
73 | + * @param IUserManager $userManager |
|
74 | + */ |
|
75 | + public function __construct(View $files, |
|
76 | + Crypt $crypt, |
|
77 | + ILogger $logger, |
|
78 | + IUserSession $userSession, |
|
79 | + IConfig $config, |
|
80 | + IUserManager $userManager |
|
81 | + ) { |
|
82 | + $this->files = $files; |
|
83 | + $this->crypt = $crypt; |
|
84 | + $this->logger = $logger; |
|
85 | + $this->user = $userSession && $userSession->isLoggedIn() ? $userSession->getUser() : false; |
|
86 | + $this->config = $config; |
|
87 | + $this->userManager = $userManager; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * check if recovery key is enabled for user |
|
92 | + * |
|
93 | + * @param string $uid |
|
94 | + * @return bool |
|
95 | + */ |
|
96 | + public function isRecoveryEnabledForUser($uid) { |
|
97 | + $recoveryMode = $this->config->getUserValue($uid, |
|
98 | + 'encryption', |
|
99 | + 'recoveryEnabled', |
|
100 | + '0'); |
|
101 | + |
|
102 | + return ($recoveryMode === '1'); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * check if the home storage should be encrypted |
|
107 | + * |
|
108 | + * @return bool |
|
109 | + */ |
|
110 | + public function shouldEncryptHomeStorage() { |
|
111 | + $encryptHomeStorage = $this->config->getAppValue( |
|
112 | + 'encryption', |
|
113 | + 'encryptHomeStorage', |
|
114 | + '1' |
|
115 | + ); |
|
116 | + |
|
117 | + return ($encryptHomeStorage === '1'); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * set the home storage encryption on/off |
|
122 | + * |
|
123 | + * @param bool $encryptHomeStorage |
|
124 | + */ |
|
125 | + public function setEncryptHomeStorage($encryptHomeStorage) { |
|
126 | + $value = $encryptHomeStorage ? '1' : '0'; |
|
127 | + $this->config->setAppValue( |
|
128 | + 'encryption', |
|
129 | + 'encryptHomeStorage', |
|
130 | + $value |
|
131 | + ); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * check if master key is enabled |
|
136 | + * |
|
137 | + * @return bool |
|
138 | + */ |
|
139 | + public function isMasterKeyEnabled() { |
|
140 | + $userMasterKey = $this->config->getAppValue('encryption', 'useMasterKey', '1'); |
|
141 | + return ($userMasterKey === '1'); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * @param $enabled |
|
146 | + * @return bool |
|
147 | + */ |
|
148 | + public function setRecoveryForUser($enabled) { |
|
149 | + $value = $enabled ? '1' : '0'; |
|
150 | + |
|
151 | + try { |
|
152 | + $this->config->setUserValue($this->user->getUID(), |
|
153 | + 'encryption', |
|
154 | + 'recoveryEnabled', |
|
155 | + $value); |
|
156 | + return true; |
|
157 | + } catch (PreConditionNotMetException $e) { |
|
158 | + return false; |
|
159 | + } |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * @param string $uid |
|
164 | + * @return bool |
|
165 | + */ |
|
166 | + public function userHasFiles($uid) { |
|
167 | + return $this->files->file_exists($uid . '/files'); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * get owner from give path, path relative to data/ expected |
|
172 | + * |
|
173 | + * @param string $path relative to data/ |
|
174 | + * @return string |
|
175 | + * @throws \BadMethodCallException |
|
176 | + */ |
|
177 | + public function getOwner($path) { |
|
178 | + $owner = ''; |
|
179 | + $parts = explode('/', $path, 3); |
|
180 | + if (count($parts) > 1) { |
|
181 | + $owner = $parts[1]; |
|
182 | + if ($this->userManager->userExists($owner) === false) { |
|
183 | + throw new \BadMethodCallException('Unknown user: ' . |
|
184 | + 'method expects path to a user folder relative to the data folder'); |
|
185 | + } |
|
186 | + |
|
187 | + } |
|
188 | + |
|
189 | + return $owner; |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * get storage of path |
|
194 | + * |
|
195 | + * @param string $path |
|
196 | + * @return \OC\Files\Storage\Storage |
|
197 | + */ |
|
198 | + public function getStorage($path) { |
|
199 | + return $this->files->getMount($path)->getStorage(); |
|
200 | + } |
|
201 | 201 | |
202 | 202 | } |