GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

UrlValidator::isUrlAccessible()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 12
nc 4
nop 1
1
<?php
2
3
/**
4
 * This file is part of the PHPMongo package.
5
 *
6
 * (c) Dmytro Sokil <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sokil\Mongo\Validator;
13
14
use Sokil\Mongo\Structure;
15
16
class UrlValidator extends \Sokil\Mongo\Validator
17
{
18
19
    public function validateField(Structure $document, $fieldName, array $params)
20
    {
21
        $value = $document->get($fieldName);
22
        
23
        // check only if set
24
        if (!$value) {
25
            return;
26
        }
27
28
        // check if url valid
29
        $isValidUrl = (bool) filter_var($value, FILTER_VALIDATE_URL);
30
        if (!$isValidUrl) {
31
            if (!isset($params['message'])) {
32
                $params['message'] = 'Value of field "'
33
                    . $fieldName
34
                    . '" is not valid url in model '
35
                    . get_called_class();
36
            }
37
38
            $document->addError($fieldName, $this->getName(), $params['message']);
39
            return;
40
        }
41
        
42
        // ping not required - so url is valid
43
        if (empty($params['ping'])) {
44
            return;
45
        }
46
        
47
        // ping required
48
        $dnsRecordExists = dns_get_record(parse_url($value, PHP_URL_HOST));
49
50
        // network not allowed
51
        if ($dnsRecordExists === false) {
52
            throw new \RuntimeException('Error getting DNS record to validated url');
53
        }
54
55
        // empty array - host not found
56
        if (is_array($dnsRecordExists) && empty($dnsRecordExists)) {
57
            if (!isset($params['message'])) {
58
                $params['message'] = 'Value of field "'
59
                    . $fieldName
60
                    . '" is valid url but host is unreachable in model '
61
                    . get_called_class();
62
            }
63
64
            $document->addError($fieldName, $this->getName(), $params['message']);
65
            return;
66
        }
67
68
        if ($this->isUrlAccessible($value)) {
69
            return;
70
        }
71
        
72
        if (!isset($params['message'])) {
73
            $params['message'] = 'Value of field "'
74
                . $fieldName
75
                . '" is valid url but page not found '
76
                . get_called_class();
77
        }
78
79
        $document->addError($fieldName, $this->getName(), $params['message']);
80
    }
81
82
    private function isUrlAccessible($url)
83
    {
84
        $headers = get_headers($url, true);
85
86
        $isAccessible = false;
87
        $i = 0;
88
        while (true) {
89
            if (false !== strpos($headers[$i], '200')) {
90
                $isAccessible = true;
91
                break;
92
            }
93
94
            $i++;
95
            if (!isset($headers[$i])) {
96
                break;
97
            }
98
        }
99
100
        // page is accessible
101
        return $isAccessible;
102
    }
103
}
104