ILIAS  release_8 Revision v8.24
TokenFactory.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24
30{
31 use Hasher;
32
33 private string $storage_dir;
34
35 public function __construct(string $storage_dir)
36 {
37 $this->storage_dir = $storage_dir;
38 }
39
40 public function check(string $access_key): Token
41 {
42 $uri = $this->unhash($access_key);
43 $expanded_uri = $this->expandUri($uri);
44 $this->checkURI($expanded_uri);
45
46 return new Token($expanded_uri, $access_key);
47 }
48
49 public function lease(FileStream $stream, bool $with_stream_attached = false): Token
50 {
51 $uri = $stream->getMetadata()['uri'] ?? '';
52 $this->checkURI($uri);
53 $reduced_uri = $this->reduceURI($uri);
54 $access_key = $this->hash($reduced_uri);
55
56 if ($with_stream_attached) {
57 $token_stream = new TokenStream($stream->detach());
58
59 return new Token($uri, $access_key, $token_stream);
60 }
61
62 return new Token($uri, $access_key);
63 }
64
65
66 private function checkURI(string $uri): void
67 {
68 if (!is_dir($this->storage_dir)) {
69 throw new \InvalidArgumentException('Storage directory does not exist');
70 }
71 if ($this->isMemoryStream($uri)) {
72 return;
73 }
74
75 if (
76 strpos($uri, $this->storage_dir) === false
77 || !is_readable($uri)
78 || !file_exists($uri)
79 ) {
80 throw new \InvalidArgumentException("Invalid access key or path: $uri");
81 }
82 }
83
84 private function reduceURI(string $uri): string
85 {
86 if (!$this->isMemoryStream($uri)) {
87 return str_replace($this->storage_dir, '', $uri);
88 }
89 return $uri;
90 }
91
92 private function expandURI(string $uri): string
93 {
94 if (!$this->isMemoryStream($uri)) {
95 return $this->storage_dir . $uri;
96 }
97 return $uri;
98 }
99
100 protected function isMemoryStream(string $path): bool
101 {
103 }
104}
lease(FileStream $stream, bool $with_stream_attached=false)
$path
Definition: ltiservices.php:32