This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Openpp\NotificationHubsRest\NotificationHub; |
||
4 | |||
5 | use Openpp\NotificationHubsRest\Notification\NotificationInterface; |
||
6 | use Openpp\NotificationHubsRest\Registration\GcmRegistration; |
||
7 | use Openpp\NotificationHubsRest\Registration\RegistrationInterface; |
||
8 | |||
9 | class NotificationHub |
||
10 | { |
||
11 | const API_VERSION = '?api-version=2013-08'; |
||
12 | |||
13 | const METHOD_GET = 'GET'; |
||
14 | const METHOD_POST = 'POST'; |
||
15 | const METHOD_PUT = 'PUT'; |
||
16 | const METHOD_DELETE = 'DELETE'; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $endpoint; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $hubPath; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $sasKeyName; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $sasKeyValue; |
||
37 | |||
38 | /** |
||
39 | * Initializes a new NotificationHub. |
||
40 | * |
||
41 | * @param string $connectionString |
||
42 | * @param string $hubPath |
||
43 | */ |
||
44 | public function __construct($connectionString, $hubPath) |
||
45 | { |
||
46 | $this->hubPath = $hubPath; |
||
47 | $this->parseConnectionString($connectionString); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Send a Notification. |
||
52 | * |
||
53 | * @param NotificationInterface $notification |
||
54 | */ |
||
55 | public function sendNotification(NotificationInterface $notification) |
||
56 | { |
||
57 | $uri = $notification->buildUri($this->endpoint, $this->hubPath).self::API_VERSION; |
||
58 | |||
59 | $token = $this->generateSasToken($uri); |
||
60 | $headers = array_merge(['Authorization: '.$token], $notification->getHeaders()); |
||
61 | |||
62 | $this->request(self::METHOD_POST, $uri, $headers, $notification->getPayload()); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Create Registration. |
||
67 | * |
||
68 | * @param RegistrationInterface $registration |
||
69 | * |
||
70 | * @throws \RuntimeException |
||
71 | * |
||
72 | * @return mixed |
||
73 | */ |
||
74 | View Code Duplication | public function createRegistration(RegistrationInterface $registration) |
|
0 ignored issues
–
show
|
|||
75 | { |
||
76 | if ($registration->getRegistrationId() || $registration->getETag()) { |
||
77 | throw new \RuntimeException('Registration ID and ETag must be empty.'); |
||
78 | } |
||
79 | |||
80 | $uri = $registration->buildUri($this->endpoint, $this->hubPath).self::API_VERSION; |
||
81 | |||
82 | $token = $this->generateSasToken($uri); |
||
83 | $headers = array_merge(['Authorization: '.$token], $registration->getHeaders()); |
||
84 | |||
85 | $response = $this->request(self::METHOD_POST, $uri, $headers, $registration->getPayload()); |
||
86 | |||
87 | return $registration->scrapeResponse($response); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Create or Update Registration. |
||
92 | * |
||
93 | * @param RegistrationInterface $registration |
||
94 | * |
||
95 | * @throws \RuntimeException |
||
96 | * |
||
97 | * @return mixed |
||
98 | */ |
||
99 | View Code Duplication | public function updateRegistration(RegistrationInterface $registration) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
100 | { |
||
101 | if (!$registration->getRegistrationId()) { |
||
102 | throw new \RuntimeException('Registration ID is mandatory.'); |
||
103 | } |
||
104 | |||
105 | $uri = $registration->buildUri($this->endpoint, $this->hubPath).self::API_VERSION; |
||
106 | |||
107 | $token = $this->generateSasToken($uri); |
||
108 | $headers = array_merge(['Authorization: '.$token], $registration->getHeaders()); |
||
109 | |||
110 | $response = $this->request(self::METHOD_PUT, $uri, $headers, $registration->getPayload()); |
||
111 | |||
112 | return $registration->scrapeResponse($response); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Delete Registration. |
||
117 | * |
||
118 | * @param ApiContentInterface $registration |
||
119 | * |
||
120 | * @throws \RuntimeException |
||
121 | */ |
||
122 | View Code Duplication | public function deleteRegistration(RegistrationInterface $registration) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
123 | { |
||
124 | if (!$registration->getRegistrationId() || !$registration->getETag()) { |
||
125 | throw new \RuntimeException('Registration ID and ETag are mandatory.'); |
||
126 | } |
||
127 | |||
128 | $uri = $registration->buildUri($this->endpoint, $this->hubPath).self::API_VERSION; |
||
129 | |||
130 | $token = $this->generateSasToken($uri); |
||
131 | $headers = array_merge(['Authorization: '.$token], $registration->getHeaders()); |
||
132 | |||
133 | $this->request(self::METHOD_DELETE, $uri, $headers); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Read Registration. |
||
138 | * |
||
139 | * @param RegistrationInterface $registration |
||
140 | * |
||
141 | * @throws \RuntimeException |
||
142 | * |
||
143 | * @return mixed |
||
144 | */ |
||
145 | View Code Duplication | public function readRegistration(RegistrationInterface $registration) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
146 | { |
||
147 | if (!$registration->getRegistrationId()) { |
||
148 | throw new \RuntimeException('Registration ID is mandatory.'); |
||
149 | } |
||
150 | |||
151 | $uri = $registration->buildUri($this->endpoint, $this->hubPath).self::API_VERSION; |
||
152 | |||
153 | $token = $this->generateSasToken($uri); |
||
154 | $headers = array_merge(['Authorization: '.$token], $registration->getHeaders()); |
||
155 | |||
156 | $response = $this->request(self::METHOD_GET, $uri, $headers); |
||
157 | |||
158 | return $registration->scrapeResponse($response); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Read All Registrations of a Channel. |
||
163 | * |
||
164 | * @param RegistrationInterface $registration |
||
165 | * |
||
166 | * @throws \RuntimeException |
||
167 | * |
||
168 | * @return mixed |
||
169 | */ |
||
170 | public function readAllRegistrationsOfAChannel(RegistrationInterface $registration) |
||
171 | { |
||
172 | if (!$registration->getToken()) { |
||
173 | throw new \RuntimeException('Token is mandatory.'); |
||
174 | } |
||
175 | |||
176 | $uri = $registration->buildUri($this->endpoint, $this->hubPath).self::API_VERSION. |
||
177 | '&$filter='.urlencode($registration->getTokenTag().' eq \''.$registration->getToken().'\''); |
||
178 | |||
179 | $token = $this->generateSasToken($uri); |
||
180 | $headers = array_merge(['Authorization: '.$token], $registration->getHeaders()); |
||
181 | |||
182 | $response = $this->request(self::METHOD_GET, $uri, $headers); |
||
183 | |||
184 | $dom = new \DOMDocument(); |
||
185 | $dom->loadXML($response); |
||
186 | |||
187 | $registrations = []; |
||
188 | foreach ($dom->getElementsByTagName('entry') as $entry) { |
||
189 | $registrations[] = $registration->scrapeResponse($dom->saveXML($entry)); |
||
190 | } |
||
191 | |||
192 | return $registrations; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Create Registration ID. |
||
197 | * |
||
198 | * @return string Registration ID |
||
199 | */ |
||
200 | public function createRegistrationId() |
||
201 | { |
||
202 | $registration = new GcmRegistration(); |
||
203 | // build uri |
||
204 | $uri = $this->endpoint.$this->hubPath.'/registrationIDs/'; |
||
205 | |||
206 | $token = $this->generateSasToken($uri); |
||
207 | $headers = array_merge(['Authorization: '.$token], $registration->getHeaders()); |
||
208 | $headers = array_merge(['Content-length: 0'], $headers); |
||
209 | |||
210 | $response = $this->request(self::METHOD_POST, $uri.self::API_VERSION, $headers, null, true); |
||
211 | |||
212 | preg_match( |
||
213 | '#'.$uri.'([^?]+)'.preg_quote(self::API_VERSION).'#', |
||
214 | $response, |
||
215 | $matches |
||
216 | ); |
||
217 | |||
218 | return $matches[1]; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Send the request to API. |
||
223 | * |
||
224 | * @param string $method |
||
225 | * @param string $uri |
||
226 | * @param array $headers |
||
227 | * @param string $payload |
||
228 | * @param bool $responseHeader |
||
229 | * |
||
230 | * @throws \RuntimeException |
||
231 | * |
||
232 | * @return string |
||
233 | * |
||
234 | * @codeCoverageIgnore |
||
235 | */ |
||
236 | protected function request($method, $uri, $headers, $payload = null, $responseHeader = false) |
||
237 | { |
||
238 | $ch = curl_init($uri); |
||
239 | |||
240 | $options = [ |
||
241 | CURLOPT_RETURNTRANSFER => true, |
||
242 | CURLOPT_SSL_VERIFYPEER => false, |
||
243 | CURLOPT_HEADER => $responseHeader, |
||
244 | CURLOPT_HTTPHEADER => $headers, |
||
245 | ]; |
||
246 | |||
247 | $options[CURLOPT_CUSTOMREQUEST] = $method; |
||
248 | |||
249 | if (!is_null($payload)) { |
||
250 | $options[CURLOPT_POSTFIELDS] = $payload; |
||
251 | } |
||
252 | |||
253 | curl_setopt_array($ch, $options); |
||
254 | |||
255 | $response = curl_exec($ch); |
||
256 | |||
257 | if (false === $response) { |
||
258 | throw new \RuntimeException(curl_error($ch)); |
||
259 | } |
||
260 | |||
261 | $info = curl_getinfo($ch); |
||
262 | if (200 != $info['http_code'] && 201 != $info['http_code']) { |
||
263 | throw new \RuntimeException('Error sending request: '.$info['http_code'].' msg: '.$response); |
||
264 | } |
||
265 | |||
266 | return $response; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Parses the connection string. |
||
271 | * |
||
272 | * @param string $connectionString |
||
273 | * |
||
274 | * @throws \RuntimeException |
||
275 | */ |
||
276 | private function parseConnectionString($connectionString) |
||
277 | { |
||
278 | $parts = explode(';', $connectionString); |
||
279 | if (3 != count($parts)) { |
||
280 | throw new \RuntimeException('Error parsing connection string: '.$connectionString); |
||
281 | } |
||
282 | |||
283 | foreach ($parts as $part) { |
||
284 | if (0 === strpos($part, 'Endpoint')) { |
||
285 | $this->endpoint = 'https'.substr($part, 11); |
||
286 | } elseif (0 === strpos($part, 'SharedAccessKeyName')) { |
||
287 | $this->sasKeyName = substr($part, 20); |
||
288 | } elseif (0 === strpos($part, 'SharedAccessKey')) { |
||
289 | $this->sasKeyValue = substr($part, 16); |
||
290 | } |
||
291 | } |
||
292 | |||
293 | if (!$this->endpoint || !$this->sasKeyName || !$this->sasKeyValue) { |
||
294 | throw new \RuntimeException('Invalid connection string: '.$connectionString); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Generates the SAS token. |
||
300 | * |
||
301 | * @param string $uri |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | private function generateSasToken($uri) |
||
306 | { |
||
307 | $targetUri = strtolower(rawurlencode(strtolower($uri))); |
||
308 | $expires = time(); |
||
309 | $expiresInMins = 60; |
||
310 | $expires = $expires + $expiresInMins * 60; |
||
311 | $toSign = $targetUri."\n".$expires; |
||
312 | $signature = rawurlencode(base64_encode(hash_hmac('sha256', $toSign, $this->sasKeyValue, true))); |
||
313 | $token = 'SharedAccessSignature sr='.$targetUri.'&sig='.$signature.'&se='.$expires.'&skn='.$this->sasKeyName; |
||
314 | |||
315 | return $token; |
||
316 | } |
||
317 | } |
||
318 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.