ILIAS  release_8 Revision v8.24
Token.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27class Token
28{
29 use Hasher;
30
31 private bool $locked = false;
36 private string $uri;
37 private string $access_key;
39
40 public function __construct(
41 string $uri,
42 string $access_key,
43 ?\ILIAS\ResourceStorage\Consumer\StreamAccess\TokenStream $stream = null
44 ) {
45 $this->uri = $uri;
46 $this->access_key = $access_key;
47 $this->stream = $stream;
48 $this->sanityCheck();
49 }
50
51 private function sanityCheck(): void
52 {
53 if (!$this->hasInMemoryStream() && !is_file($this->uri)) {
54 throw new \InvalidArgumentException('File does not exist');
55 }
56 // Check relative path
57 $relative_uri = $this->unhash($this->access_key);
58 if (strpos($this->uri, $relative_uri) === false) {
59 throw new \InvalidArgumentException('Access key does not match');
60 }
61 if ($this->stream !== null && $this->stream->getMetadata()['uri'] !== $this->uri) {
62 throw new \InvalidArgumentException('Stream does not match');
63 }
64 if ($this->stream !== null && strpos((string)$this->stream->getMetadata()['uri'], $relative_uri) === false) {
65 throw new \InvalidArgumentException('Stream does not match');
66 }
67 }
68
72 private function unlock(UnlockKey $unlock_key): bool
73 {
74 return $this->locked = $this->encrypt->check($this, $unlock_key);
75 }
76
77 private function isLocked(): bool
78 {
79 return $this->locked;
80 }
81
82
83 public function hasStreamableStream(): bool
84 {
85 return $this->stream !== null && !$this->hasInMemoryStream();
86 }
87
88
89 public function getAccessKey(): string
90 {
91 return $this->access_key;
92 }
93
94 public function resolveStream(): TokenStream
95 {
96 if ($this->locked) {
97 throw new \BadMethodCallException('Token is locked und must be unlocked before resolving the stream.');
98 }
99
100 if ($this->stream === null) {
101 $this->stream = new TokenStream(fopen($this->uri, 'rb'));
102 }
103 $this->sanityCheck();
104
105 return $this->stream;
106 }
107
108 public function hasInMemoryStream(): bool
109 {
110 return $this->uri === StreamAccess::PHP_MEMORY;
111 }
112}
unlock(UnlockKey $unlock_key)
@description This Method will be used to unlock Streams in the future and will be a replacement for W...
Definition: Token.php:72
ILIAS ResourceStorage Consumer StreamAccess TokenStream $stream
Definition: Token.php:38
__construct(string $uri, string $access_key, ?\ILIAS\ResourceStorage\Consumer\StreamAccess\TokenStream $stream=null)
Definition: Token.php:40
Class ChatMainBarProvider \MainMenu\Provider.