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