1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace fkooman\RemoteStorage; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use fkooman\RemoteStorage\Http\Exception\HttpException; |
7
|
|
|
use fkooman\RemoteStorage\Http\FormAuthentication; |
8
|
|
|
use fkooman\RemoteStorage\Http\Request; |
9
|
|
|
use fkooman\RemoteStorage\Http\Response; |
10
|
|
|
use fkooman\RemoteStorage\Http\SessionInterface; |
11
|
|
|
use fkooman\RemoteStorage\OAuth\BearerAuthentication; |
12
|
|
|
use fkooman\RemoteStorage\OAuth\OAuthModule; |
13
|
|
|
use fkooman\RemoteStorage\OAuth\TokenStorage; |
14
|
|
|
use PDO; |
15
|
|
|
|
16
|
|
|
class Controller |
17
|
|
|
{ |
18
|
|
|
/** @var TwigTpl */ |
19
|
|
|
private $templateManager; |
20
|
|
|
|
21
|
|
|
/** @var ApiModule */ |
22
|
|
|
private $apiModule; |
23
|
|
|
|
24
|
|
|
/** @var UiModule */ |
25
|
|
|
private $uiModule; |
26
|
|
|
|
27
|
|
|
/** @var WebfingerModule */ |
28
|
|
|
private $webfingerModule; |
29
|
|
|
|
30
|
|
|
/** @var OAuthModule */ |
31
|
|
|
private $oauthModule; |
32
|
|
|
|
33
|
|
|
/** @var array */ |
34
|
|
|
private $auth = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $configFile |
|
|
|
|
38
|
|
|
* @param string $storageRoot |
|
|
|
|
39
|
|
|
* @param string $dbDsn |
|
|
|
|
40
|
|
|
* @param array $templateFolders |
|
|
|
|
41
|
|
|
*/ |
42
|
|
|
public function __construct($appDir, SessionInterface $session, RandomInterface $random, DateTime $dateTime) |
43
|
|
|
{ |
44
|
|
|
$config = Config::fromFile(sprintf('%s/config/server.yaml', $appDir)); |
45
|
|
|
$serverMode = $config->serverMode; |
|
|
|
|
46
|
|
|
|
47
|
|
|
$this->templateManager = new TwigTpl( |
48
|
|
|
[ |
49
|
|
|
sprintf('%s/views', $appDir), |
50
|
|
|
sprintf('%s/config/views', $appDir), |
51
|
|
|
], |
52
|
|
|
'development' !== $serverMode ? sprintf('%s/data/tpl', $appDir) : null |
53
|
|
|
); |
54
|
|
|
$this->templateManager->setDefault( |
55
|
|
|
[ |
56
|
|
|
'serverMode' => $serverMode, |
57
|
|
|
] |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$db = new PDO(sprintf('sqlite:%s/data/rs.sqlite', $appDir)); |
61
|
|
|
$metaDataStorage = new MetadataStorage($db); |
62
|
|
|
$metaDataStorage->init(); |
63
|
|
|
|
64
|
|
|
$tokenStorage = new TokenStorage($db); |
65
|
|
|
$tokenStorage->init(); |
66
|
|
|
|
67
|
|
|
$remoteStorage = new RemoteStorage( |
68
|
|
|
$metaDataStorage, |
69
|
|
|
new DocumentStorage(sprintf('%s/data/storage', $appDir)) |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$this->apiModule = new ApiModule($remoteStorage, $config->serverMode); |
|
|
|
|
73
|
|
|
$this->uiModule = new UiModule($remoteStorage, $this->templateManager, $tokenStorage); |
74
|
|
|
$this->webfingerModule = new WebfingerModule($config->serverMode); |
|
|
|
|
75
|
|
|
$this->oauthModule = new OAuthModule($this->templateManager, $tokenStorage, $random, $dateTime); |
76
|
|
|
|
77
|
|
|
$session->setSecureOnly('development' !== $serverMode); |
78
|
|
|
$this->auth['form'] = new FormAuthentication($session, $this->templateManager, $config->Users->asArray()); |
|
|
|
|
79
|
|
|
$this->auth['bearer'] = new BearerAuthentication($tokenStorage); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function run(Request $request) |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
|
|
switch ($request->getRequestMethod()) { |
86
|
|
|
case 'GET': |
87
|
|
|
return $this->handleGet($request); |
88
|
|
|
case 'POST': |
89
|
|
|
return $this->handlePost($request); |
90
|
|
View Code Duplication |
case 'PUT': |
|
|
|
|
91
|
|
|
$tokenInfo = $this->auth['bearer']->requireAuth($request); |
92
|
|
|
|
93
|
|
|
return $this->apiModule->put($request, $tokenInfo); |
94
|
|
View Code Duplication |
case 'DELETE': |
|
|
|
|
95
|
|
|
$tokenInfo = $this->auth['bearer']->requireAuth($request); |
96
|
|
|
|
97
|
|
|
return $this->apiModule->delete($request, $tokenInfo); |
98
|
|
|
case 'OPTIONS': |
99
|
|
|
return $this->apiModule->options($request); |
100
|
|
View Code Duplication |
case 'HEAD': |
|
|
|
|
101
|
|
|
$tokenInfo = $this->auth['bearer']->optionalAuth($request); |
102
|
|
|
|
103
|
|
|
return $this->apiModule->head($request, $tokenInfo); |
104
|
|
|
default: |
105
|
|
|
throw new HttpException('method not allowed', 405); |
106
|
|
|
} |
107
|
|
|
} catch (HttpException $e) { |
108
|
|
|
if ($request->isBrowser()) { |
109
|
|
|
$response = new Response($e->getCode(), 'text/html'); |
110
|
|
|
$response->setBody( |
111
|
|
|
$this->templateManager->render( |
112
|
|
|
'errorPage', |
113
|
|
|
[ |
114
|
|
|
'code' => $e->getCode(), |
115
|
|
|
'message' => $e->getMessage(), |
116
|
|
|
] |
117
|
|
|
) |
118
|
|
|
); |
119
|
|
|
} else { |
120
|
|
|
// not a browser |
121
|
|
|
$response = new Response($e->getCode(), 'application/json'); |
122
|
|
|
$response->setBody(json_encode(['error' => $e->getMessage()])); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
foreach ($e->getResponseHeaders() as $key => $value) { |
126
|
|
|
$response->addHeader($key, $value); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $response; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function handleGet(Request $request) |
134
|
|
|
{ |
135
|
|
|
switch ($request->getPathInfo()) { |
136
|
|
|
case '/.well-known/webfinger': |
|
|
|
|
137
|
|
|
|
138
|
|
|
return $this->webfingerModule->getWebfinger($request); |
139
|
|
View Code Duplication |
case '/authorize': |
|
|
|
|
140
|
|
|
$userId = $this->auth['form']->requireAuth($request); |
141
|
|
|
if ($userId instanceof Response) { |
142
|
|
|
return $userId; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this->oauthModule->getAuthorize($request, $userId); |
146
|
|
|
case '/': |
147
|
|
|
$userId = $this->auth['form']->optionalAuth($request); |
148
|
|
|
|
149
|
|
|
return $this->uiModule->getRootPage($request, $userId); |
150
|
|
View Code Duplication |
case '/account': |
|
|
|
|
151
|
|
|
$userId = $this->auth['form']->requireAuth($request); |
152
|
|
|
if ($userId instanceof Response) { |
153
|
|
|
return $userId; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->uiModule->getAccountPage($request, $userId); |
157
|
|
|
default: |
158
|
|
|
$tokenInfo = $this->auth['bearer']->optionalAuth($request); |
159
|
|
|
|
160
|
|
|
return $this->apiModule->get($request, $tokenInfo); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private function handlePost(Request $request) |
165
|
|
|
{ |
166
|
|
|
switch ($request->getPathInfo()) { |
167
|
|
|
case '/account': |
168
|
|
|
$userId = $this->auth['form']->requireAuth($request); |
169
|
|
|
|
170
|
|
|
return $this->uiModule->postAccountPage($request, $userId); |
171
|
|
|
case '/authorize': |
172
|
|
|
$userId = $this->auth['form']->requireAuth($request); |
173
|
|
|
|
174
|
|
|
return $this->oauthModule->postAuthorize($request, $userId); |
175
|
|
|
case '/authenticate': |
176
|
|
|
return $this->auth['form']->verifyAuth($request); |
177
|
|
|
case '/logout': |
178
|
|
|
return $this->auth['form']->logout($request); |
179
|
|
|
default: |
180
|
|
|
throw new HttpException('not found', 404); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.