ILIAS  trunk Revision v11.0_alpha-2645-g16283d3b3f8
HandlerService.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\StaticURL\Handler;
22 
25 use ILIAS\Data\URI;
31 
36 {
40  private array $handlers = [];
42 
43  public function __construct(
44  private RequestBuilder $request_builder,
45  private Context $context,
46  Handler ...$handlers,
47  ) {
48  $this->response_factory = new Factory($context);
49  foreach ($handlers as $handler) {
50  $this->handlers[$handler->getNamespace()] = $handler;
51  }
52  }
53 
57  public function performRedirect(URI $base_uri): void
58  {
59  $http = $this->context->http();
60 
61  $request = $this->request_builder->buildRequest(
62  $http,
63  $this->context->refinery(),
65  );
66  if (!$request instanceof Request) {
67  throw new \RuntimeException('No request could be built');
68  }
69 
70  $handler = $this->handlers[$request->getNamespace()] ?? null;
71  if (!$handler instanceof Handler) {
72  throw new \InvalidArgumentException('No handler found for namespace ' . $request->getNamespace());
73  }
74  $response = $handler->handle($request, $this->context, $this->response_factory);
75  if (!$response->targetCanBeReached()) {
76  throw new \RuntimeException(
77  'Handler ' . $handler->getNamespace() . ' did not return a URI'
78  ); // TODO: we shoud redirect somewhere
79  }
80 
81  $uri_builder = new StandardURIBuilder(ILIAS_HTTP_PATH, false);
82 
83  switch (true) {
84  case $response instanceof MaybeCanHandlerAfterLogin:
85  $target = $uri_builder->buildTarget(
86  $request->getNamespace(),
87  $request->getReferenceId(),
88  $request->getAdditionalParameters()
89  );
90  $full_uri = $base_uri . "/login.php?target=";
91  $full_uri .= str_replace('/', '_', rtrim($target, '/')); // TODO: ILIAS currently need this like this
92  if (!$this->context->isUserLoggedIn()) {
93  $full_uri .= '&cmd=force_login&lang=' . $this->context->getUserLanguage();
94  }
95  $full_uri = $this->appendUnknownParameters($this->context, $full_uri); // Read the comment below
96  break;
97  case $response instanceof CannotReach:
98  $this->context->mainTemplate()->setOnScreenMessage(
99  'failure',
100  $this->context->lng()->txt('permission_denied'),
101  true
102  );
103  $full_uri = $base_uri . '/index.php';
104  break;
105  default:
106  // Perform Redirect
107  $uri_path = $response->getURIPath() ?? '';
108  $base_path = $base_uri->getPath() ?? '';
109  if ($base_path !== '' && $base_path !== '/') {
110  $uri_path = str_replace(rtrim($base_path, '/') . '/', '', $uri_path);
111  }
112  $full_uri = $base_uri . '/' . trim((string) $uri_path, '/');
113  break;
114  }
115 
116  $http->saveResponse(
117  $http->response()->withAddedHeader('Location', $full_uri)
118  );
119  $http->sendResponse();
120  $http->close();
121  }
122 
127  private function appendUnknownParameters(Context $context, string $full_uri): string
128  {
129  if ($context->http()->wrapper()->query()->has('soap_pw')) {
130  return \ilUtil::appendUrlParameterString(
131  $full_uri,
132  'soap_pw=' . $context->http()->wrapper()->query()->retrieve(
133  'soap_pw',
134  $context->refinery()->kindlyTo()->string()
135  )
136  );
137  }
138  if ($context->http()->wrapper()->query()->has('ext_uid')) {
139  return \ilUtil::appendUrlParameterString(
140  $full_uri,
141  'ext_uid=' . $context->http()->wrapper()->query()->retrieve(
142  'ext_uid',
143  $context->refinery()->kindlyTo()->string()
144  )
145  );
146  }
147 
148  return $full_uri;
149  }
150 }
$context
Definition: webdav.php:31
$response
Definition: xapitoken.php:93
$http
Definition: deliver.php:30
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$handler
Definition: oai.php:29
appendUnknownParameters(Context $context, string $full_uri)
__construct(private RequestBuilder $request_builder, private Context $context, Handler ... $handlers,)