ILIAS  release_8 Revision v8.24
Service.php
Go to the documentation of this file.
1<?php
2
20
23use ILIAS\LTI\ToolProvider\Http\HTTPMessage;
25
34{
40 public bool $unsigned = false;
41
47 protected ?string $endpoint = null;
48
54 protected ?string $scope = null;
55
61 protected ?string $mediaType = null;
62
68 private ?Platform $platform = null;
69
75 private ?HTTPMessage $http = null;
76
82 public function __construct(Platform $platform, string $endpoint)
83 {
84 $this->platform = $platform;
85 $this->endpoint = $endpoint;
86 }
87
88 // /**
89 // * Get tool consumer.
90 // *
91 // * @deprecated Use getPlatform() instead
92 // * @see Service::getPlatform()
93 // *
94 // * @return ToolConsumer Consumer for this service
95 // */
96 // public function getConsumer()
97 // {
98 // Util::logDebug('Method ceLTIc\LTI\Service::getConsumer() has been deprecated; please use ceLTIc\LTI\Service::getPlatform() instead.',
99 // true);
100 // return $this->getPlatform();
101 // }
102
108 public function getPlatform(): ?Platform
109 {
110 return $this->platform;
111 }
112
118 public function getScope(): ?string
119 {
120 return $this->scope;
121 }
122
130 public function send(string $method, array $parameters = array(), string $body = null): ?HTTPMessage
131 {
133 if (!empty($parameters)) {
134 if (strpos($url, '?') === false) {
135 $sep = '?';
136 } else {
137 $sep = '&';
138 }
139 foreach ($parameters as $name => $value) {
140 $url .= $sep . urlencode($name) . '=' . urlencode($value);
141 $sep = '&';
142 }
143 }
144 $header = null;
145 $retry = !$this->platform->useOAuth1();
146 $newToken = false;
147 $retried = false;
148 do {
149 if (!$this->unsigned) {
150 $accessToken = $this->platform->getAccessToken();
151 if (!$this->platform->useOAuth1()) {
152 if (empty($accessToken)) {
153 $accessToken = new AccessToken($this->platform);
154 $this->platform->setAccessToken($accessToken);
155 }
156 if (!$accessToken->hasScope($this->scope)) {
157 $accessToken->get($this->scope);
158 $newToken = true;
159 if (!$accessToken->hasScope($this->scope)) { // Try obtaining a token for just this scope
160 $accessToken->expires = time();
161 $accessToken->get($this->scope, true);
162 $retried = true;
163 if (!$accessToken->hasScope($this->scope)) {
164 if (empty($this->http)) {
165 $this->http = new HttpMessage($url);
166 $this->http->error = "Unable to obtain an access token for scope: {$this->scope}";
167 }
168 break;
169 }
170 }
171 }
172 }
173 $header = $this->platform->signServiceRequest($url, $method, $this->mediaType, $body);
174 }
175 // Connect to platform and parse JSON response
176 //UK changed from
177 // $this->http = new HttpMessage($url, $method, $body, $header);
178 // to
179 $this->http = new \ILIAS\LTI\ToolProvider\Http\HttpMessage($url, $method, $body, $header);
180 if ($this->http->send() && !empty($this->http->response)) {
181 $this->http->responseJson = json_decode($this->http->response);
182 $this->http->ok = !is_null($this->http->responseJson);
183 }
184 $retry = $retry && !$retried && !$this->http->ok;
185 if ($retry) {
186 if (!$newToken) { // Invalidate existing token to force a new one to be obtained
187 $accessToken->expires = time();
188 $newToken = true;
189 } elseif (count($accessToken->scopes) !== 1) { // Try obtaining a token for just this scope
190 $accessToken->expires = time();
191 $accessToken->get($this->scope, true);
192 $retried = true;
193 } else {
194 $retry = false;
195 }
196 }
197 } while ($retry);
198
199 return $this->http;
200 }
201
207 public function getHttpMessage(): ?HTTPMessage
208 {
209 return $this->http;
210 }
211
212 ###
213 ### PROTECTED METHODS
214 ###
215
222 protected function parseContextsInArray(object $contexts, array $arr): array
223 {
224 if (is_array($contexts)) {
225 $contextdefs = array();
226 foreach ($contexts as $context) {
227 if (is_object($context)) {
228 $contextdefs = array_merge(get_object_vars($context), $contexts);
229 }
230 }
231 $parsed = array();
232 foreach ($arr as $key => $value) {
233 $parts = explode(':', $value, 2);
234 if (count($parts) > 1) {
235 if (array_key_exists($parts[0], $contextdefs)) {
236 $parsed[$key] = $contextdefs[$parts[0]] . $parts[1];
237 break;
238 }
239 }
240 $parsed[$key] = $value;
241 }
242 } else {
243 $parsed = $arr;
244 }
245
246 return $parsed;
247 }
248}
Class to represent an HTTP message.
Definition: AccessToken.php:33
Class to represent a platform.
Definition: Platform.php:36
Class to implement a service.
Definition: Service.php:34
bool $unsigned
Whether service request should be sent unsigned.
Definition: Service.php:40
__construct(Platform $platform, string $endpoint)
Class constructor.
Definition: Service.php:82
string $endpoint
Service endpoint.
Definition: Service.php:47
HTTPMessage $http
HttpMessage object for last service request.
Definition: Service.php:75
getHttpMessage()
Get HttpMessage object for last request.
Definition: Service.php:207
send(string $method, array $parameters=array(), string $body=null)
Send a service request.
Definition: Service.php:130
string $scope
Service access scope.
Definition: Service.php:54
parseContextsInArray(object $contexts, array $arr)
Parse the JSON for context references.
Definition: Service.php:222
string $mediaType
Media type of message body.
Definition: Service.php:61
Platform $platform
Platform for this service request.
Definition: Service.php:68
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:64
if($format !==null) $name
Definition: metadata.php:247
static http()
Fetches the global http state from ILIAS.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
string $key
Consumer key/client ID value.
Definition: System.php:193
$url
$context
Definition: webdav.php:29