1 | <?php |
||
40 | class Push { |
||
41 | /** @var IDBConnection */ |
||
42 | protected $db; |
||
43 | /** @var INotificationManager */ |
||
44 | protected $notificationManager; |
||
45 | /** @var IConfig */ |
||
46 | protected $config; |
||
47 | /** @var IProvider */ |
||
48 | protected $tokenProvider; |
||
49 | /** @var Manager */ |
||
50 | private $keyManager; |
||
51 | /** @var IUserManager */ |
||
52 | private $userManager; |
||
53 | /** @var IClientService */ |
||
54 | protected $clientService; |
||
55 | /** @var ILogger */ |
||
56 | protected $log; |
||
57 | |||
58 | 11 | public function __construct(IDBConnection $connection, INotificationManager $notificationManager, IConfig $config, IProvider $tokenProvider, Manager $keyManager, IUserManager $userManager, IClientService $clientService, ILogger $log) { |
|
68 | |||
69 | /** |
||
70 | * @param INotification $notification |
||
71 | */ |
||
72 | 7 | public function pushToDevice(INotification $notification) { |
|
73 | 7 | $user = $this->userManager->get($notification->getUser()); |
|
74 | 7 | if (!($user instanceof IUser)) { |
|
|
|||
75 | 1 | return; |
|
76 | } |
||
77 | |||
78 | 6 | $devices = $this->getDevicesForUser($notification->getUser()); |
|
79 | 6 | if (empty($devices)) { |
|
80 | 1 | return; |
|
81 | } |
||
82 | |||
83 | 5 | $language = $this->config->getUserValue($notification->getUser(), 'core', 'lang', 'en'); |
|
84 | try { |
||
85 | 5 | $notification = $this->notificationManager->prepare($notification, $language); |
|
86 | 5 | } catch (\InvalidArgumentException $e) { |
|
87 | 1 | return; |
|
88 | } |
||
89 | |||
90 | 4 | $userKey = $this->keyManager->getKey($user); |
|
91 | |||
92 | 4 | $pushNotifications = []; |
|
93 | 4 | foreach ($devices as $device) { |
|
94 | try { |
||
95 | 4 | $payload = json_encode($this->encryptAndSign($userKey, $device, $notification)); |
|
96 | |||
97 | 2 | $proxyServer = rtrim($device['proxyserver'], '/'); |
|
98 | 2 | if (!isset($pushNotifications[$proxyServer])) { |
|
99 | 2 | $pushNotifications[$proxyServer] = []; |
|
100 | 2 | } |
|
101 | 2 | $pushNotifications[$proxyServer][] = $payload; |
|
102 | 4 | } catch (InvalidTokenException $e) { |
|
103 | // Token does not exist anymore, should drop the push device entry |
||
104 | 1 | $this->deletePushToken($device['token']); |
|
105 | 2 | } catch (\InvalidArgumentException $e) { |
|
106 | // Failed to encrypt message for device: public key is invalid |
||
107 | 1 | $this->deletePushToken($device['token']); |
|
108 | } |
||
109 | 4 | } |
|
110 | |||
111 | 4 | if (empty($pushNotifications)) { |
|
112 | 2 | return; |
|
113 | } |
||
114 | |||
115 | 2 | $client = $this->clientService->newClient(); |
|
116 | 2 | foreach ($pushNotifications as $proxyServer => $notifications) { |
|
117 | try { |
||
118 | 2 | $response = $client->post($proxyServer . '/notifications', [ |
|
119 | 'body' => [ |
||
120 | 2 | 'notifications' => $notifications, |
|
121 | 2 | ], |
|
122 | 2 | ]); |
|
123 | 2 | } catch (\Exception $e) { |
|
124 | 2 | $this->log->logException($e, [ |
|
125 | 2 | 'app' => 'notifications', |
|
126 | 2 | ]); |
|
127 | 2 | continue; |
|
128 | } |
||
129 | |||
130 | 2 | $status = $response->getStatusCode(); |
|
131 | 2 | if ($status !== Http::STATUS_OK && $status !== Http::STATUS_SERVICE_UNAVAILABLE) { |
|
132 | 2 | $body = $response->getBody(); |
|
133 | 2 | $this->log->error('Could not send notification to push server [{url}]: {error}',[ |
|
134 | 2 | 'error' => is_string($body) ? $body : 'no reason given', |
|
135 | 2 | 'url' => $proxyServer, |
|
136 | 2 | 'app' => 'notifications', |
|
137 | 2 | ]); |
|
138 | 2 | } else if ($status === Http::STATUS_SERVICE_UNAVAILABLE && $this->config->getSystemValue('debug', false)) { |
|
139 | 1 | $body = $response->getBody(); |
|
140 | 1 | $this->log->debug('Could not send notification to push server [{url}]: {error}',[ |
|
141 | 1 | 'error' => is_string($body) ? $body : 'no reason given', |
|
142 | 1 | 'url' => $proxyServer, |
|
143 | 1 | 'app' => 'notifications', |
|
144 | 1 | ]); |
|
145 | 1 | } |
|
146 | 2 | } |
|
147 | 2 | } |
|
148 | |||
149 | /** |
||
150 | * @param Key $userKey |
||
151 | * @param array $device |
||
152 | * @param INotification $notification |
||
153 | * @return array |
||
154 | * @throws InvalidTokenException |
||
155 | * @throws \InvalidArgumentException |
||
156 | */ |
||
157 | protected function encryptAndSign(Key $userKey, array $device, INotification $notification) { |
||
158 | // Check if the token is still valid... |
||
159 | $this->tokenProvider->getTokenById($device['token']); |
||
160 | |||
161 | $data = [ |
||
162 | 'app' => $notification->getApp(), |
||
163 | 'subject' => $notification->getParsedSubject(), |
||
164 | ]; |
||
165 | |||
166 | if (!openssl_public_encrypt(json_encode($data), $encryptedSubject, $device['devicepublickey'], OPENSSL_PKCS1_PADDING)) { |
||
167 | $this->log->error(openssl_error_string(), ['app' => 'notifications']); |
||
168 | throw new \InvalidArgumentException('Failed to encrypt message for device'); |
||
169 | } |
||
170 | |||
171 | openssl_sign($encryptedSubject, $signature, $userKey->getPrivate(), OPENSSL_ALGO_SHA512); |
||
172 | $base64EncryptedSubject = base64_encode(hash('sha512', $encryptedSubject, true)); |
||
173 | $base64Signature = base64_encode($signature); |
||
174 | |||
175 | return [ |
||
176 | 'deviceIdentifier' => $device['deviceidentifier'], |
||
177 | 'pushTokenHash' => $device['pushtokenhash'], |
||
178 | 'subject' => $base64EncryptedSubject, |
||
179 | 'signature' => $base64Signature, |
||
180 | ]; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param string $uid |
||
185 | * @return array[] |
||
186 | */ |
||
187 | protected function getDevicesForUser($uid) { |
||
199 | |||
200 | /** |
||
201 | * @param int $tokenId |
||
202 | * @return bool |
||
203 | */ |
||
204 | protected function deletePushToken($tokenId) { |
||
211 | } |
||
212 |
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.