Proyectos de Subversion Moodle

Rev

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

Rev 1 Rev 1441
Línea 48... Línea 48...
48
    public static function fromArray(array $cookies, string $domain): self
48
    public static function fromArray(array $cookies, string $domain): self
49
    {
49
    {
50
        $cookieJar = new self();
50
        $cookieJar = new self();
51
        foreach ($cookies as $name => $value) {
51
        foreach ($cookies as $name => $value) {
52
            $cookieJar->setCookie(new SetCookie([
52
            $cookieJar->setCookie(new SetCookie([
53
                'Domain'  => $domain,
53
                'Domain' => $domain,
54
                'Name'    => $name,
54
                'Name' => $name,
55
                'Value'   => $value,
55
                'Value' => $value,
56
                'Discard' => true
56
                'Discard' => true,
57
            ]));
57
            ]));
58
        }
58
        }
Línea 59... Línea 59...
59
 
59
 
60
        return $cookieJar;
60
        return $cookieJar;
Línea 94... Línea 94...
94
        }
94
        }
Línea 95... Línea 95...
95
 
95
 
96
        return null;
96
        return null;
Línea 97... Línea -...
97
    }
-
 
98
 
-
 
99
    /**
-
 
100
     * @inheritDoc
97
    }
101
     */
98
 
102
    public function toArray(): array
99
    public function toArray(): array
103
    {
100
    {
104
        return \array_map(static function (SetCookie $cookie): array {
101
        return \array_map(static function (SetCookie $cookie): array {
105
            return $cookie->toArray();
102
            return $cookie->toArray();
Línea 106... Línea -...
106
        }, $this->getIterator()->getArrayCopy());
-
 
107
    }
-
 
108
 
-
 
109
    /**
103
        }, $this->getIterator()->getArrayCopy());
110
     * @inheritDoc
104
    }
111
     */
105
 
112
    public function clear(?string $domain = null, ?string $path = null, ?string $name = null): void
106
    public function clear(?string $domain = null, ?string $path = null, ?string $name = null): void
-
 
107
    {
113
    {
108
        if (!$domain) {
114
        if (!$domain) {
109
            $this->cookies = [];
115
            $this->cookies = [];
110
 
116
            return;
111
            return;
117
        } elseif (!$path) {
112
        } elseif (!$path) {
Línea 123... Línea 118...
123
            );
118
            );
124
        } elseif (!$name) {
119
        } elseif (!$name) {
125
            $this->cookies = \array_filter(
120
            $this->cookies = \array_filter(
126
                $this->cookies,
121
                $this->cookies,
127
                static function (SetCookie $cookie) use ($path, $domain): bool {
122
                static function (SetCookie $cookie) use ($path, $domain): bool {
128
                    return !($cookie->matchesPath($path) &&
123
                    return !($cookie->matchesPath($path)
129
                        $cookie->matchesDomain($domain));
124
                        && $cookie->matchesDomain($domain));
130
                }
125
                }
131
            );
126
            );
132
        } else {
127
        } else {
133
            $this->cookies = \array_filter(
128
            $this->cookies = \array_filter(
134
                $this->cookies,
129
                $this->cookies,
135
                static function (SetCookie $cookie) use ($path, $domain, $name) {
130
                static function (SetCookie $cookie) use ($path, $domain, $name) {
136
                    return !($cookie->getName() == $name &&
131
                    return !($cookie->getName() == $name
137
                        $cookie->matchesPath($path) &&
132
                        && $cookie->matchesPath($path)
138
                        $cookie->matchesDomain($domain));
133
                        && $cookie->matchesDomain($domain));
139
                }
134
                }
140
            );
135
            );
141
        }
136
        }
142
    }
137
    }
Línea 143... Línea -...
143
 
-
 
144
    /**
-
 
145
     * @inheritDoc
-
 
146
     */
138
 
147
    public function clearSessionCookies(): void
139
    public function clearSessionCookies(): void
148
    {
140
    {
149
        $this->cookies = \array_filter(
141
        $this->cookies = \array_filter(
150
            $this->cookies,
142
            $this->cookies,
151
            static function (SetCookie $cookie): bool {
143
            static function (SetCookie $cookie): bool {
152
                return !$cookie->getDiscard() && $cookie->getExpires();
144
                return !$cookie->getDiscard() && $cookie->getExpires();
153
            }
145
            }
154
        );
146
        );
Línea 155... Línea -...
155
    }
-
 
156
 
-
 
157
    /**
-
 
158
     * @inheritDoc
147
    }
159
     */
148
 
160
    public function setCookie(SetCookie $cookie): bool
149
    public function setCookie(SetCookie $cookie): bool
161
    {
150
    {
162
        // If the name string is empty (but not 0), ignore the set-cookie
151
        // If the name string is empty (but not 0), ignore the set-cookie
Línea 168... Línea 157...
168
 
157
 
169
        // Only allow cookies with set and valid domain, name, value
158
        // Only allow cookies with set and valid domain, name, value
170
        $result = $cookie->validate();
159
        $result = $cookie->validate();
171
        if ($result !== true) {
160
        if ($result !== true) {
172
            if ($this->strictMode) {
161
            if ($this->strictMode) {
173
                throw new \RuntimeException('Invalid cookie: ' . $result);
162
                throw new \RuntimeException('Invalid cookie: '.$result);
174
            }
163
            }
-
 
164
            $this->removeCookieIfEmpty($cookie);
175
            $this->removeCookieIfEmpty($cookie);
165
 
176
            return false;
166
            return false;
Línea 177... Línea 167...
177
        }
167
        }
178
 
168
 
179
        // Resolve conflicts with previously set cookies
169
        // Resolve conflicts with previously set cookies
180
        foreach ($this->cookies as $i => $c) {
170
        foreach ($this->cookies as $i => $c) {
181
            // Two cookies are identical, when their path, and domain are
171
            // Two cookies are identical, when their path, and domain are
182
            // identical.
172
            // identical.
183
            if ($c->getPath() != $cookie->getPath() ||
173
            if ($c->getPath() != $cookie->getPath()
184
                $c->getDomain() != $cookie->getDomain() ||
174
                || $c->getDomain() != $cookie->getDomain()
185
                $c->getName() != $cookie->getName()
175
                || $c->getName() != $cookie->getName()
186
            ) {
176
            ) {
Línea 187... Línea 177...
187
                continue;
177
                continue;
Línea 251... Línea 241...
251
    }
241
    }
Línea 252... Línea 242...
252
 
242
 
253
    /**
243
    /**
254
     * Computes cookie path following RFC 6265 section 5.1.4
244
     * Computes cookie path following RFC 6265 section 5.1.4
255
     *
245
     *
256
     * @link https://tools.ietf.org/html/rfc6265#section-5.1.4
246
     * @see https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4
257
     */
247
     */
258
    private function getCookiePathFromRequest(RequestInterface $request): string
248
    private function getCookiePathFromRequest(RequestInterface $request): string
259
    {
249
    {
260
        $uriPath = $request->getUri()->getPath();
250
        $uriPath = $request->getUri()->getPath();
Línea 282... Línea 272...
282
        $scheme = $uri->getScheme();
272
        $scheme = $uri->getScheme();
283
        $host = $uri->getHost();
273
        $host = $uri->getHost();
284
        $path = $uri->getPath() ?: '/';
274
        $path = $uri->getPath() ?: '/';
Línea 285... Línea 275...
285
 
275
 
286
        foreach ($this->cookies as $cookie) {
276
        foreach ($this->cookies as $cookie) {
287
            if ($cookie->matchesPath($path) &&
277
            if ($cookie->matchesPath($path)
288
                $cookie->matchesDomain($host) &&
278
                && $cookie->matchesDomain($host)
289
                !$cookie->isExpired() &&
279
                && !$cookie->isExpired()
290
                (!$cookie->getSecure() || $scheme === 'https')
280
                && (!$cookie->getSecure() || $scheme === 'https')
291
            ) {
281
            ) {
292
                $values[] = $cookie->getName() . '='
282
                $values[] = $cookie->getName().'='
293
                    . $cookie->getValue();
283
                    .$cookie->getValue();
294
            }
284
            }
Línea 295... Línea 285...
295
        }
285
        }
296
 
286