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.
Completed
Push — master ( 560332...66e815 )
by Robert
11:59
created

UrlValidator::getClientOptions()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 18
cp 0
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 16
nc 8
nop 2
crap 20
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\validators;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
use yii\web\JsExpression;
13
use yii\helpers\Json;
14
15
/**
16
 * UrlValidator validates that the attribute value is a valid http or https URL.
17
 *
18
 * Note that this validator only checks if the URL scheme and host part are correct.
19
 * It does not check the remaining parts of a URL.
20
 *
21
 * @author Qiang Xue <[email protected]>
22
 * @since 2.0
23
 */
24
class UrlValidator extends Validator
25
{
26
    /**
27
     * @var string the regular expression used to validate the attribute value.
28
     * The pattern may contain a `{schemes}` token that will be replaced
29
     * by a regular expression which represents the [[validSchemes]].
30
     */
31
    public $pattern = '/^{schemes}:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(?::\d{1,5})?(?:$|[?\/#])/i';
32
    /**
33
     * @var array list of URI schemes which should be considered valid. By default, http and https
34
     * are considered to be valid schemes.
35
     */
36
    public $validSchemes = ['http', 'https'];
37
    /**
38
     * @var string the default URI scheme. If the input doesn't contain the scheme part, the default
39
     * scheme will be prepended to it (thus changing the input). Defaults to null, meaning a URL must
40
     * contain the scheme part.
41
     */
42
    public $defaultScheme;
43
    /**
44
     * @var bool whether validation process should take into account IDN (internationalized
45
     * domain names). Defaults to false meaning that validation of URLs containing IDN will always
46
     * fail. Note that in order to use IDN validation you have to install and enable `intl` PHP
47
     * extension, otherwise an exception would be thrown.
48
     */
49
    public $enableIDN = false;
50
51
52
    /**
53
     * @inheritdoc
54
     */
55 7
    public function init()
56
    {
57 7
        parent::init();
58 7
        if ($this->enableIDN && !function_exists('idn_to_ascii')) {
59
            throw new InvalidConfigException('In order to use IDN validation intl extension must be installed and enabled.');
60
        }
61 7
        if ($this->message === null) {
62 7
            $this->message = Yii::t('yii', '{attribute} is not a valid URL.');
63 7
        }
64 7
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 1
    public function validateAttribute($model, $attribute)
70
    {
71 1
        $value = $model->$attribute;
72 1
        $result = $this->validateValue($value);
73 1
        if (!empty($result)) {
74 1
            $this->addError($model, $attribute, $result[0], $result[1]);
75 1
        } elseif ($this->defaultScheme !== null && strpos($value, '://') === false) {
76 1
            $model->$attribute = $this->defaultScheme . '://' . $value;
77 1
        }
78 1
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83 7
    protected function validateValue($value)
84
    {
85
        // make sure the length is limited to avoid DOS attacks
86 7
        if (is_string($value) && strlen($value) < 2000) {
87 6
            if ($this->defaultScheme !== null && strpos($value, '://') === false) {
88 3
                $value = $this->defaultScheme . '://' . $value;
89 3
            }
90
91 6
            if (strpos($this->pattern, '{schemes}') !== false) {
92 5
                $pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern);
93 5
            } else {
94 1
                $pattern = $this->pattern;
95
            }
96
97 6
            if ($this->enableIDN) {
98 1
                $value = preg_replace_callback('/:\/\/([^\/]+)/', function ($matches) {
99 1
                    return '://' . idn_to_ascii($matches[1]);
100 1
                }, $value);
101 1
            }
102
103 6
            if (preg_match($pattern, $value)) {
104 6
                return null;
105
            }
106 3
        }
107
108 4
        return [$this->message, []];
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    public function clientValidateAttribute($model, $attribute, $view)
115
    {
116
        ValidationAsset::register($view);
117
        if ($this->enableIDN) {
118
            PunycodeAsset::register($view);
119
        }
120
        $options = $this->getClientOptions($model, $attribute);
121
122
        return 'yii.validation.url(value, messages, ' . Json::htmlEncode($options) . ');';
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128
    public function getClientOptions($model, $attribute)
129
    {
130
        if (strpos($this->pattern, '{schemes}') !== false) {
131
            $pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern);
132
        } else {
133
            $pattern = $this->pattern;
134
        }
135
136
        $options = [
137
            'pattern' => new JsExpression($pattern),
138
            'message' => Yii::$app->getI18n()->format($this->message, [
139
                'attribute' => $model->getAttributeLabel($attribute),
140
            ], Yii::$app->language),
141
            'enableIDN' => (bool) $this->enableIDN,
142
        ];
143
        if ($this->skipOnEmpty) {
144
            $options['skipOnEmpty'] = 1;
145
        }
146
        if ($this->defaultScheme !== null) {
147
            $options['defaultScheme'] = $this->defaultScheme;
148
        }
149
150
        return $options;
151
    }
152
}
153