1 | <?php |
||
38 | class PushController extends OCSController { |
||
39 | |||
40 | /** @var IDBConnection */ |
||
41 | private $db; |
||
42 | |||
43 | /** @var ISession */ |
||
44 | private $session; |
||
45 | |||
46 | /** @var IUserSession */ |
||
47 | private $userSession; |
||
48 | |||
49 | /** @var IProvider */ |
||
50 | private $tokenProvider; |
||
51 | |||
52 | /** @var Manager */ |
||
53 | private $identityProof; |
||
54 | |||
55 | /** |
||
56 | * @param string $appName |
||
57 | * @param IRequest $request |
||
58 | * @param IDBConnection $db |
||
59 | * @param ISession $session |
||
60 | * @param IUserSession $userSession |
||
61 | * @param IProvider $tokenProvider |
||
62 | * @param Manager $identityProof |
||
63 | */ |
||
64 | 22 | public function __construct($appName, IRequest $request, IDBConnection $db, ISession $session, IUserSession $userSession, IProvider $tokenProvider, Manager $identityProof) { |
|
73 | |||
74 | /** |
||
75 | * @NoAdminRequired |
||
76 | * |
||
77 | * @param string $pushTokenHash |
||
78 | * @param string $devicePublicKey |
||
79 | * @param string $proxyServer |
||
80 | * @return DataResponse |
||
81 | */ |
||
82 | 15 | public function registerDevice($pushTokenHash, $devicePublicKey, $proxyServer) { |
|
83 | 15 | $user = $this->userSession->getUser(); |
|
84 | 15 | if (!$user instanceof IUser) { |
|
|
|||
85 | 1 | return new DataResponse([], Http::STATUS_UNAUTHORIZED); |
|
86 | } |
||
87 | |||
88 | 14 | if (!preg_match('/^([a-f0-9]{128})$/', $pushTokenHash)) { |
|
89 | 3 | return new DataResponse(['message' => 'INVALID_PUSHTOKEN_HASH'], Http::STATUS_BAD_REQUEST); |
|
90 | } |
||
91 | |||
92 | if ( |
||
93 | 11 | ((strlen($devicePublicKey) !== 450 || strpos($devicePublicKey, "\n" . '-----END PUBLIC KEY-----') !== 425) && |
|
94 | 4 | (strlen($devicePublicKey) !== 451 || strpos($devicePublicKey, "\n" . '-----END PUBLIC KEY-----' . "\n") !== 425)) || |
|
95 | 11 | strpos($devicePublicKey, '-----BEGIN PUBLIC KEY-----' . "\n") !== 0) { |
|
96 | 3 | return new DataResponse(['message' => 'INVALID_DEVICE_KEY'], Http::STATUS_BAD_REQUEST); |
|
97 | } |
||
98 | |||
99 | if ( |
||
100 | 8 | !filter_var($proxyServer, FILTER_VALIDATE_URL) || |
|
101 | 5 | strlen($proxyServer) > 256 || |
|
102 | 8 | !preg_match('/^(https\:\/\/|http\:\/\/localhost(\:[0-9]{0,5})?\/)/', $proxyServer) |
|
103 | ) { |
||
104 | 3 | return new DataResponse(['message' => 'INVALID_PROXY_SERVER'], Http::STATUS_BAD_REQUEST); |
|
105 | } |
||
106 | |||
107 | 5 | $tokenId = $this->session->get('token-id'); |
|
108 | try { |
||
109 | 5 | $token = $this->tokenProvider->getTokenById($tokenId); |
|
110 | 3 | } catch (InvalidTokenException $e) { |
|
111 | 3 | return new DataResponse(['message' => 'INVALID_SESSION_TOKEN'], Http::STATUS_BAD_REQUEST); |
|
112 | } |
||
113 | |||
114 | 2 | $key = $this->identityProof->getKey($user); |
|
115 | |||
116 | 2 | $deviceIdentifier = json_encode([$user->getCloudId(), $token->getId()]); |
|
117 | 2 | openssl_sign($deviceIdentifier, $signature, $key->getPrivate(), OPENSSL_ALGO_SHA512); |
|
118 | 2 | $deviceIdentifier = base64_encode(hash('sha512', $deviceIdentifier, true)); |
|
119 | |||
120 | 2 | $appType = 'unknown'; |
|
121 | 2 | if ($this->request->isUserAgent([ |
|
122 | 2 | IRequest::USER_AGENT_TALK_ANDROID, |
|
123 | 2 | IRequest::USER_AGENT_TALK_IOS, |
|
124 | ])) { |
||
125 | $appType = 'talk'; |
||
126 | 2 | } else if ($this->request->isUserAgent([ |
|
127 | 2 | IRequest::USER_AGENT_CLIENT_ANDROID, |
|
128 | 2 | IRequest::USER_AGENT_CLIENT_IOS, |
|
129 | ])) { |
||
130 | $appType = 'nextcloud'; |
||
131 | } |
||
132 | |||
133 | 2 | $created = $this->savePushToken($user, $token, $deviceIdentifier, $devicePublicKey, $pushTokenHash, $proxyServer, $appType); |
|
134 | |||
135 | 2 | return new DataResponse([ |
|
136 | 2 | 'publicKey' => $key->getPublic(), |
|
137 | 2 | 'deviceIdentifier' => $deviceIdentifier, |
|
138 | 2 | 'signature' => base64_encode($signature), |
|
139 | 2 | ], $created ? Http::STATUS_CREATED : Http::STATUS_OK); |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * @NoAdminRequired |
||
144 | * |
||
145 | * @return DataResponse |
||
146 | */ |
||
147 | 5 | public function removeDevice() { |
|
148 | 5 | $user = $this->userSession->getUser(); |
|
149 | 5 | if (!$user instanceof IUser) { |
|
150 | 1 | return new DataResponse([], Http::STATUS_UNAUTHORIZED); |
|
151 | } |
||
152 | |||
153 | 4 | $tokenId = $this->session->get('token-id'); |
|
154 | try { |
||
155 | 4 | $token = $this->tokenProvider->getTokenById($tokenId); |
|
156 | 2 | } catch (InvalidTokenException $e) { |
|
157 | 2 | return new DataResponse(['message' => 'INVALID_SESSION_TOKEN'], Http::STATUS_BAD_REQUEST); |
|
158 | } |
||
159 | |||
160 | 2 | if ($this->deletePushToken($user, $token)) { |
|
161 | 1 | return new DataResponse([], Http::STATUS_ACCEPTED); |
|
162 | } |
||
163 | |||
164 | 1 | return new DataResponse([], Http::STATUS_OK); |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param IUser $user |
||
169 | * @param IToken $token |
||
170 | * @param string $deviceIdentifier |
||
171 | * @param string $devicePublicKey |
||
172 | * @param string $pushTokenHash |
||
173 | * @param string $proxyServer |
||
174 | * @param string $appType |
||
175 | * @return bool If the hash was new to the database |
||
176 | */ |
||
177 | protected function savePushToken(IUser $user, IToken $token, $deviceIdentifier, $devicePublicKey, $pushTokenHash, $proxyServer, $appType) { |
||
178 | $query = $this->db->getQueryBuilder(); |
||
179 | $query->select('*') |
||
180 | ->from('notifications_pushtokens') |
||
181 | ->where($query->expr()->eq('uid', $query->createNamedParameter($user->getUID()))) |
||
182 | ->andWhere($query->expr()->eq('token', $query->createNamedParameter($token->getId()))); |
||
183 | $result = $query->execute(); |
||
184 | $row = $result->fetch(); |
||
185 | $result->closeCursor(); |
||
186 | |||
187 | if (!$row) { |
||
188 | return $this->insertPushToken($user, $token, $deviceIdentifier, $devicePublicKey, $pushTokenHash, $proxyServer, $appType); |
||
189 | } |
||
190 | |||
191 | return $this->updatePushToken($user, $token, $devicePublicKey, $pushTokenHash, $proxyServer, $appType); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param IUser $user |
||
196 | * @param IToken $token |
||
197 | * @param string $deviceIdentifier |
||
198 | * @param string $devicePublicKey |
||
199 | * @param string $pushTokenHash |
||
200 | * @param string $proxyServer |
||
201 | * @param string $appType |
||
202 | * @return bool If the entry was created |
||
203 | */ |
||
204 | protected function insertPushToken(IUser $user, IToken $token, $deviceIdentifier, $devicePublicKey, $pushTokenHash, $proxyServer, $appType) { |
||
221 | |||
222 | /** |
||
223 | * @param IUser $user |
||
224 | * @param IToken $token |
||
225 | * @param string $devicePublicKey |
||
226 | * @param string $pushTokenHash |
||
227 | * @param string $proxyServer |
||
228 | * @param string $appType |
||
229 | * @return bool If the entry was updated |
||
230 | */ |
||
231 | protected function updatePushToken(IUser $user, IToken $token, $devicePublicKey, $pushTokenHash, $proxyServer, $appType) { |
||
232 | $devicePublicKeyHash = hash('sha512', $devicePublicKey); |
||
233 | |||
234 | $query = $this->db->getQueryBuilder(); |
||
235 | $query->update('notifications_pushtokens') |
||
236 | ->set('devicepublickey', $query->createNamedParameter($devicePublicKey)) |
||
237 | ->set('devicepublickeyhash', $query->createNamedParameter($devicePublicKeyHash)) |
||
238 | ->set('pushtokenhash', $query->createNamedParameter($pushTokenHash)) |
||
239 | ->set('proxyserver', $query->createNamedParameter($proxyServer)) |
||
240 | ->set('apptype', $query->createNamedParameter($appType)) |
||
241 | ->where($query->expr()->eq('uid', $query->createNamedParameter($user->getUID()))) |
||
242 | ->andWhere($query->expr()->eq('token', $query->createNamedParameter($token->getId(), IQueryBuilder::PARAM_INT))); |
||
243 | |||
244 | return $query->execute() !== 0; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @param IUser $user |
||
249 | * @param IToken $token |
||
250 | * @return bool If the entry was deleted |
||
251 | */ |
||
252 | protected function deletePushToken(IUser $user, IToken $token) { |
||
260 | } |
||
261 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.