ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
Handler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29use Psr\Http\Message\ServerRequestInterface;
33
37class Handler implements AliasedHandler
38{
40 private const string NAMESPACE_SHORT = 's';
41 private const string NAMESPACE_LONG = 'shortlink';
42 private const string NAMESPACE_ALTERNATIVE = 'to';
43
44 public function __construct()
45 {
46 }
47
48 public function getNamespace(): string
49 {
51 }
52
53 public function getNamespaceAliasses(): array
54 {
55 return array_diff(
56 [self::NAMESPACE_LONG, self::NAMESPACE_ALTERNATIVE, self::NAMESPACE_SHORT],
57 [$this->getNamespace()]
58 );
59 }
60
61 public function canHandle(Request $request): bool
62 {
63 if ($request->getNamespace() === $this->getNamespace()) {
64 return true;
65 }
66 return in_array(
67 $request->getNamespace(),
68 $this->getNamespaceAliasses()
69 );
70 }
71
72 public function handle(Request $request, Context $context, Factory $response_factory): Response
73 {
74 global $DIC;
75 $repository = new RepositoryDB(
76 $DIC->database(),
78 $DIC->repositoryTree()
79 )
80 );
81 $alias = $request->getAdditionalParameters()[0] ?? '';
82 if (!$repository->has($alias)) {
83 return $response_factory->cannot();
84 }
85
86 if (($shortlink = $repository->getByAlias($alias)) === null) {
87 return $response_factory->cannot();
88 };
89
90 if (!$shortlink->isActive()) {
91 return $response_factory->cannot();
92 }
93
94 $target_resolver = new TargetLinkResolver(
95 $DIC['static_url']->builder(),
96 $DIC[\ILIAS\Data\Factory::class]
97 );
98
99 $target = $target_resolver->resolve(
100 $shortlink
101 );
102 if ($target instanceof URI) {
103 $target = $target->getPath();
104 }
105
106 if (empty($target)) {
107 return $response_factory->cannot();
108 }
109
110 $repository->increaseUsage(
111 $shortlink
112 );
113
114 return $response_factory->can(
115 $target,
116 $this->mustShift($context->http()->request())
117 );
118 }
119
120 protected function mustShift(ServerRequestInterface $request): bool
121 {
122 $requested_uri = $request->getUri()->getPath();
123
124 return !str_contains($requested_uri, StandardURIBuilder::SHORT)
125 && !str_contains($requested_uri, StandardURIBuilder::LONG);
126 }
127
128}
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
can(string $uri_path, bool $shift=false)
Definition: Factory.php:50
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26
$context
Definition: webdav.php:31