Conditions | 26 |
Paths | 494 |
Total Lines | 126 |
Code Lines | 81 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
190 | private function queryWebcalFeed(array $subscription, array &$mutations) { |
||
191 | $client = $this->clientService->newClient(); |
||
192 | |||
193 | $didBreak301Chain = false; |
||
194 | $latestLocation = null; |
||
195 | |||
196 | $handlerStack = HandlerStack::create(); |
||
197 | $handlerStack->push(Middleware::mapRequest(function (RequestInterface $request) { |
||
198 | return $request |
||
199 | ->withHeader('Accept', 'text/calendar, application/calendar+json, application/calendar+xml') |
||
200 | ->withHeader('User-Agent', 'Nextcloud Webcal Crawler'); |
||
201 | })); |
||
202 | $handlerStack->push(Middleware::mapResponse(function(ResponseInterface $response) use (&$didBreak301Chain, &$latestLocation) { |
||
203 | if (!$didBreak301Chain) { |
||
204 | if ($response->getStatusCode() !== 301) { |
||
205 | $didBreak301Chain = true; |
||
206 | } else { |
||
207 | $latestLocation = $response->getHeader('Location'); |
||
208 | } |
||
209 | } |
||
210 | return $response; |
||
211 | })); |
||
212 | |||
213 | $allowLocalAccess = $this->config->getAppValue('dav', 'webcalAllowLocalAccess', 'no'); |
||
214 | $subscriptionId = $subscription['id']; |
||
215 | $url = $this->cleanURL($subscription['source']); |
||
216 | if ($url === null) { |
||
217 | return null; |
||
218 | } |
||
219 | |||
220 | if ($allowLocalAccess !== 'yes') { |
||
221 | $host = strtolower(parse_url($url, PHP_URL_HOST)); |
||
222 | // remove brackets from IPv6 addresses |
||
223 | if (strpos($host, '[') === 0 && substr($host, -1) === ']') { |
||
224 | $host = substr($host, 1, -1); |
||
225 | } |
||
226 | |||
227 | // Disallow localhost and local network |
||
228 | if ($host === 'localhost' || substr($host, -6) === '.local' || substr($host, -10) === '.localhost') { |
||
229 | $this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules"); |
||
230 | return null; |
||
231 | } |
||
232 | |||
233 | // Disallow hostname only |
||
234 | if (substr_count($host, '.') === 0) { |
||
235 | $this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules"); |
||
236 | return null; |
||
237 | } |
||
238 | |||
239 | if ((bool)filter_var($host, FILTER_VALIDATE_IP) && !filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
||
240 | $this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules"); |
||
241 | return null; |
||
242 | } |
||
243 | |||
244 | // Also check for IPv6 IPv4 nesting, because that's not covered by filter_var |
||
245 | if ((bool)filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && substr_count($host, '.') > 0) { |
||
246 | $delimiter = strrpos($host, ':'); // Get last colon |
||
247 | $ipv4Address = substr($host, $delimiter + 1); |
||
248 | |||
249 | if (!filter_var($ipv4Address, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
||
250 | $this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules"); |
||
251 | return null; |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | |||
256 | try { |
||
257 | $params = [ |
||
258 | 'allow_redirects' => [ |
||
259 | 'redirects' => 10 |
||
260 | ], |
||
261 | 'handler' => $handlerStack, |
||
262 | ]; |
||
263 | |||
264 | $user = parse_url($subscription['source'], PHP_URL_USER); |
||
265 | $pass = parse_url($subscription['source'], PHP_URL_PASS); |
||
266 | if ($user !== null && $pass !== null) { |
||
267 | $params['auth'] = [$user, $pass]; |
||
268 | } |
||
269 | |||
270 | $response = $client->get($url, $params); |
||
271 | $body = $response->getBody(); |
||
272 | |||
273 | if ($latestLocation) { |
||
274 | $mutations['{http://calendarserver.org/ns/}source'] = new Href($latestLocation); |
||
275 | } |
||
276 | |||
277 | $contentType = $response->getHeader('Content-Type'); |
||
278 | $contentType = explode(';', $contentType, 2)[0]; |
||
279 | switch($contentType) { |
||
280 | case 'application/calendar+json': |
||
281 | try { |
||
282 | $jCalendar = Reader::readJson($body, Reader::OPTION_FORGIVING); |
||
283 | } catch(Exception $ex) { |
||
284 | // In case of a parsing error return null |
||
285 | $this->logger->debug("Subscription $subscriptionId could not be parsed"); |
||
286 | return null; |
||
287 | } |
||
288 | return $jCalendar->serialize(); |
||
289 | |||
290 | case 'application/calendar+xml': |
||
291 | try { |
||
292 | $xCalendar = Reader::readXML($body); |
||
293 | } catch(Exception $ex) { |
||
294 | // In case of a parsing error return null |
||
295 | $this->logger->debug("Subscription $subscriptionId could not be parsed"); |
||
296 | return null; |
||
297 | } |
||
298 | return $xCalendar->serialize(); |
||
299 | |||
300 | case 'text/calendar': |
||
301 | default: |
||
302 | try { |
||
303 | $vCalendar = Reader::read($body); |
||
304 | } catch(Exception $ex) { |
||
305 | // In case of a parsing error return null |
||
306 | $this->logger->debug("Subscription $subscriptionId could not be parsed"); |
||
307 | return null; |
||
308 | } |
||
309 | return $vCalendar->serialize(); |
||
310 | } |
||
311 | } catch(Exception $ex) { |
||
312 | $this->logger->logException($ex); |
||
313 | $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a network error"); |
||
314 | |||
315 | return null; |
||
316 | } |
||
425 |