Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 9... Línea 9...
9
{
9
{
10
    /**
10
    /**
11
     * @var array
11
     * @var array
12
     */
12
     */
13
    private static $defaults = [
13
    private static $defaults = [
14
        'Name'     => null,
14
        'Name' => null,
15
        'Value'    => null,
15
        'Value' => null,
16
        'Domain'   => null,
16
        'Domain' => null,
17
        'Path'     => '/',
17
        'Path' => '/',
18
        'Max-Age'  => null,
18
        'Max-Age' => null,
19
        'Expires'  => null,
19
        'Expires' => null,
20
        'Secure'   => false,
20
        'Secure' => false,
21
        'Discard'  => false,
21
        'Discard' => false,
22
        'HttpOnly' => false
22
        'HttpOnly' => false,
23
    ];
23
    ];
Línea 24... Línea 24...
24
 
24
 
25
    /**
25
    /**
26
     * @var array Cookie data
26
     * @var array Cookie data
Línea 56... Línea 56...
56
                $data['Name'] = $key;
56
                $data['Name'] = $key;
57
                $data['Value'] = $value;
57
                $data['Value'] = $value;
58
            } else {
58
            } else {
59
                foreach (\array_keys(self::$defaults) as $search) {
59
                foreach (\array_keys(self::$defaults) as $search) {
60
                    if (!\strcasecmp($search, $key)) {
60
                    if (!\strcasecmp($search, $key)) {
-
 
61
                        if ($search === 'Max-Age') {
-
 
62
                            if (is_numeric($value)) {
-
 
63
                                $data[$search] = (int) $value;
-
 
64
                            }
-
 
65
                        } else {
61
                        $data[$search] = $value;
66
                            $data[$search] = $value;
-
 
67
                        }
62
                        continue 2;
68
                        continue 2;
63
                    }
69
                    }
64
                }
70
                }
65
                $data[$key] = $value;
71
                $data[$key] = $value;
66
            }
72
            }
Línea 72... Línea 78...
72
    /**
78
    /**
73
     * @param array $data Array of cookie data provided by a Cookie parser
79
     * @param array $data Array of cookie data provided by a Cookie parser
74
     */
80
     */
75
    public function __construct(array $data = [])
81
    public function __construct(array $data = [])
76
    {
82
    {
-
 
83
        $this->data = self::$defaults;
-
 
84
 
-
 
85
        if (isset($data['Name'])) {
-
 
86
            $this->setName($data['Name']);
-
 
87
        }
-
 
88
 
-
 
89
        if (isset($data['Value'])) {
-
 
90
            $this->setValue($data['Value']);
-
 
91
        }
-
 
92
 
-
 
93
        if (isset($data['Domain'])) {
-
 
94
            $this->setDomain($data['Domain']);
-
 
95
        }
-
 
96
 
-
 
97
        if (isset($data['Path'])) {
-
 
98
            $this->setPath($data['Path']);
-
 
99
        }
-
 
100
 
-
 
101
        if (isset($data['Max-Age'])) {
-
 
102
            $this->setMaxAge($data['Max-Age']);
-
 
103
        }
-
 
104
 
-
 
105
        if (isset($data['Expires'])) {
-
 
106
            $this->setExpires($data['Expires']);
-
 
107
        }
-
 
108
 
-
 
109
        if (isset($data['Secure'])) {
77
        /** @var array|null $replaced will be null in case of replace error */
110
            $this->setSecure($data['Secure']);
-
 
111
        }
-
 
112
 
-
 
113
        if (isset($data['Discard'])) {
78
        $replaced = \array_replace(self::$defaults, $data);
114
            $this->setDiscard($data['Discard']);
-
 
115
        }
-
 
116
 
79
        if ($replaced === null) {
117
        if (isset($data['HttpOnly'])) {
-
 
118
            $this->setHttpOnly($data['HttpOnly']);
-
 
119
        }
-
 
120
 
80
            throw new \InvalidArgumentException('Unable to replace the default values for the Cookie.');
121
        // Set the remaining values that don't have extra validation logic
-
 
122
        foreach (array_diff(array_keys($data), array_keys(self::$defaults)) as $key) {
-
 
123
            $this->data[$key] = $data[$key];
81
        }
124
        }
Línea 82... Línea -...
82
 
-
 
83
        $this->data = $replaced;
125
 
84
        // Extract the Expires value and turn it into a UNIX timestamp if needed
126
        // Extract the Expires value and turn it into a UNIX timestamp if needed
85
        if (!$this->getExpires() && $this->getMaxAge()) {
127
        if (!$this->getExpires() && $this->getMaxAge()) {
86
            // Calculate the Expires date
128
            // Calculate the Expires date
87
            $this->setExpires(\time() + $this->getMaxAge());
129
            $this->setExpires(\time() + $this->getMaxAge());
Línea 90... Línea 132...
90
        }
132
        }
91
    }
133
    }
Línea 92... Línea 134...
92
 
134
 
93
    public function __toString()
135
    public function __toString()
94
    {
136
    {
95
        $str = $this->data['Name'] . '=' . ($this->data['Value'] ?? '') . '; ';
137
        $str = $this->data['Name'].'='.($this->data['Value'] ?? '').'; ';
96
        foreach ($this->data as $k => $v) {
138
        foreach ($this->data as $k => $v) {
97
            if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== false) {
139
            if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== false) {
98
                if ($k === 'Expires') {
140
                if ($k === 'Expires') {
99
                    $str .= 'Expires=' . \gmdate('D, d M Y H:i:s \G\M\T', $v) . '; ';
141
                    $str .= 'Expires='.\gmdate('D, d M Y H:i:s \G\M\T', $v).'; ';
100
                } else {
142
                } else {
101
                    $str .= ($v === true ? $k : "{$k}={$v}") . '; ';
143
                    $str .= ($v === true ? $k : "{$k}={$v}").'; ';
102
                }
144
                }
103
            }
145
            }
Línea 104... Línea 146...
104
        }
146
        }
Línea 376... Línea 418...
376
        if (null === $cookieDomain) {
418
        if (null === $cookieDomain) {
377
            return true;
419
            return true;
378
        }
420
        }
Línea 379... Línea 421...
379
 
421
 
380
        // Remove the leading '.' as per spec in RFC 6265.
422
        // Remove the leading '.' as per spec in RFC 6265.
381
        // https://tools.ietf.org/html/rfc6265#section-5.2.3
423
        // https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.3
Línea 382... Línea 424...
382
        $cookieDomain = \ltrim(\strtolower($cookieDomain), '.');
424
        $cookieDomain = \ltrim(\strtolower($cookieDomain), '.');
Línea 383... Línea 425...
383
 
425
 
384
        $domain = \strtolower($domain);
426
        $domain = \strtolower($domain);
385
 
427
 
386
        // Domain not set or exact match.
428
        // Domain not set or exact match.
Línea 387... Línea 429...
387
        if ('' === $cookieDomain || $domain === $cookieDomain) {
429
        if ('' === $cookieDomain || $domain === $cookieDomain) {
388
            return true;
430
            return true;
389
        }
431
        }
390
 
432
 
391
        // Matching the subdomain according to RFC 6265.
433
        // Matching the subdomain according to RFC 6265.
Línea 392... Línea 434...
392
        // https://tools.ietf.org/html/rfc6265#section-5.1.3
434
        // https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3
393
        if (\filter_var($domain, \FILTER_VALIDATE_IP)) {
435
        if (\filter_var($domain, \FILTER_VALIDATE_IP)) {
Línea 394... Línea 436...
394
            return false;
436
            return false;
395
        }
437
        }
396
 
438
 
Línea 421... Línea 463...
421
        if (\preg_match(
463
        if (\preg_match(
422
            '/[\x00-\x20\x22\x28-\x29\x2c\x2f\x3a-\x40\x5c\x7b\x7d\x7f]/',
464
            '/[\x00-\x20\x22\x28-\x29\x2c\x2f\x3a-\x40\x5c\x7b\x7d\x7f]/',
423
            $name
465
            $name
424
        )) {
466
        )) {
425
            return 'Cookie name must not contain invalid characters: ASCII '
467
            return 'Cookie name must not contain invalid characters: ASCII '
426
                . 'Control characters (0-31;127), space, tab and the '
468
                .'Control characters (0-31;127), space, tab and the '
427
                . 'following characters: ()<>@,;:\"/?={}';
469
                .'following characters: ()<>@,;:\"/?={}';
428
        }
470
        }
Línea 429... Línea 471...
429
 
471
 
430
        // Value must not be null. 0 and empty string are valid. Empty strings
472
        // Value must not be null. 0 and empty string are valid. Empty strings
431
        // are technically against RFC 6265, but known to happen in the wild.
473
        // are technically against RFC 6265, but known to happen in the wild.