|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Lukas Reschke <[email protected]> |
|
6
|
|
|
* @author Morris Jobke <[email protected]> |
|
7
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
8
|
|
|
* @author Thomas Müller <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* @license AGPL-3.0 |
|
11
|
|
|
* |
|
12
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
14
|
|
|
* as published by the Free Software Foundation. |
|
15
|
|
|
* |
|
16
|
|
|
* This program is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU Affero General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace OCA\Files_Sharing\Middleware; |
|
27
|
|
|
|
|
28
|
|
|
use OCP\App\IAppManager; |
|
29
|
|
|
use OCP\AppFramework\Http\NotFoundResponse; |
|
30
|
|
|
use OCP\AppFramework\Middleware; |
|
31
|
|
|
use OCP\Files\NotFoundException; |
|
32
|
|
|
use OCP\IConfig; |
|
33
|
|
|
use OCP\AppFramework\Utility\IControllerMethodReflector; |
|
34
|
|
|
use OCA\Files_Sharing\Exceptions\S2SException; |
|
35
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Checks whether the "sharing check" is enabled |
|
39
|
|
|
* |
|
40
|
|
|
* @package OCA\Files_Sharing\Middleware |
|
41
|
|
|
*/ |
|
42
|
|
|
class SharingCheckMiddleware extends Middleware { |
|
43
|
|
|
|
|
44
|
|
|
/** @var string */ |
|
45
|
|
|
protected $appName; |
|
46
|
|
|
/** @var IConfig */ |
|
47
|
|
|
protected $config; |
|
48
|
|
|
/** @var IAppManager */ |
|
49
|
|
|
protected $appManager; |
|
50
|
|
|
/** @var IControllerMethodReflector */ |
|
51
|
|
|
protected $reflector; |
|
52
|
|
|
|
|
53
|
|
|
/*** |
|
54
|
|
|
* @param string $appName |
|
55
|
|
|
* @param IConfig $config |
|
56
|
|
|
* @param IAppManager $appManager |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct($appName, |
|
59
|
|
|
IConfig $config, |
|
60
|
|
|
IAppManager $appManager, |
|
61
|
|
|
IControllerMethodReflector $reflector |
|
62
|
|
|
) { |
|
63
|
|
|
$this->appName = $appName; |
|
64
|
|
|
$this->config = $config; |
|
65
|
|
|
$this->appManager = $appManager; |
|
66
|
|
|
$this->reflector = $reflector; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Check if sharing is enabled before the controllers is executed |
|
71
|
|
|
* |
|
72
|
|
|
* @param \OCP\AppFramework\Controller $controller |
|
73
|
|
|
* @param string $methodName |
|
74
|
|
|
* @throws NotFoundException |
|
75
|
|
|
*/ |
|
76
|
|
|
public function beforeController($controller, $methodName) { |
|
77
|
|
|
if(!$this->isSharingEnabled()) { |
|
78
|
|
|
throw new NotFoundException('Sharing is disabled.'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($controller instanceof \OCA\Files_Sharing\Controllers\ExternalSharesController && |
|
82
|
|
|
!$this->externalSharesChecks()) { |
|
83
|
|
|
throw new S2SException('Federated sharing not allowed'); |
|
84
|
|
|
} else if ($controller instanceof \OCA\Files_Sharing\Controllers\ShareController && |
|
85
|
|
|
!$this->isLinkSharingEnabled()) { |
|
86
|
|
|
throw new NotFoundException('Link sharing is disabled'); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Return 404 page in case of a not found exception |
|
92
|
|
|
* |
|
93
|
|
|
* @param \OCP\AppFramework\Controller $controller |
|
94
|
|
|
* @param string $methodName |
|
95
|
|
|
* @param \Exception $exception |
|
96
|
|
|
* @return NotFoundResponse |
|
97
|
|
|
* @throws \Exception |
|
98
|
|
|
*/ |
|
99
|
|
|
public function afterException($controller, $methodName, \Exception $exception) { |
|
100
|
|
|
if(is_a($exception, '\OCP\Files\NotFoundException')) { |
|
101
|
|
|
return new NotFoundResponse(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (is_a($exception, '\OCA\Files_Sharing\Exceptions\S2SException')) { |
|
105
|
|
|
return new JSONResponse($exception->getMessage(), 405); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
throw $exception; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Checks for externalshares controller |
|
113
|
|
|
* @return bool |
|
114
|
|
|
*/ |
|
115
|
|
|
private function externalSharesChecks() { |
|
116
|
|
|
|
|
117
|
|
|
if (!$this->reflector->hasAnnotation('NoIncomingFederatedSharingRequired') && |
|
118
|
|
|
$this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes') !== 'yes') { |
|
119
|
|
|
return false; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if (!$this->reflector->hasAnnotation('NoOutgoingFederatedSharingRequired') && |
|
123
|
|
|
$this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') !== 'yes') { |
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return true; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Check whether sharing is enabled |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
|
|
private function isSharingEnabled() { |
|
135
|
|
|
// FIXME: This check is done here since the route is globally defined and not inside the files_sharing app |
|
136
|
|
|
// Check whether the sharing application is enabled |
|
137
|
|
|
if(!$this->appManager->isEnabledForUser($this->appName)) { |
|
138
|
|
|
return false; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return true; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Check if link sharing is allowed |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
|
|
private function isLinkSharingEnabled() { |
|
149
|
|
|
// Check if the shareAPI is enabled |
|
150
|
|
|
if ($this->config->getAppValue('core', 'shareapi_enabled', 'yes') !== 'yes') { |
|
151
|
|
|
return false; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// Check whether public sharing is enabled |
|
155
|
|
|
if($this->config->getAppValue('core', 'shareapi_allow_links', 'yes') !== 'yes') { |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return true; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: