ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
ToolConsumer.php
Go to the documentation of this file.
1<?php
2
4
9
20{
21
27 public $name = null;
33 public $secret = null;
39 public $ltiVersion = null;
45 public $consumerName = null;
51 public $consumerVersion = null;
57 public $consumerGuid = null;
63 public $cssPath = null;
69 public $protected = false;
75 public $enabled = false;
81 public $enableFrom = null;
87 public $enableUntil = null;
93 public $lastAccess = null;
105 public $defaultEmail = '';
111 public $settings = null;
117 public $created = null;
123 public $updated = null;
124
130 private $id = null;
136 private $key = null;
142 private $settingsChanged = false;
148 private $dataConnector = null;
149
157 public function __construct($key = null, $dataConnector = null, $autoEnable = false)
158 {
159
160 $this->initialize();
161 if (empty($dataConnector)) {
163 }
164 $this->dataConnector = $dataConnector;
165 if (!empty($key)) {
166 $this->load($key, $autoEnable);
167 } else {
168 $this->secret = DataConnector::getRandomString(32);
169 }
170
171 }
172
176 public function initialize()
177 {
178
179 $this->id = null;
180 $this->key = null;
181 $this->name = null;
182 $this->secret = null;
183 $this->ltiVersion = null;
184 $this->consumerName = null;
185 $this->consumerVersion = null;
186 $this->consumerGuid = null;
187 $this->profile = null;
188 $this->toolProxy = null;
189 $this->settings = array();
190 $this->protected = false;
191 $this->enabled = false;
192 $this->enableFrom = null;
193 $this->enableUntil = null;
194 $this->lastAccess = null;
195 $this->idScope = ToolProvider::ID_SCOPE_ID_ONLY;
196 $this->defaultEmail = '';
197 $this->created = null;
198 $this->updated = null;
199
200 }
201
207 public function initialise()
208 {
209
210 $this->initialize();
211
212 }
213
219 public function save()
220 {
221
222 $ok = $this->dataConnector->saveToolConsumer($this);
223 if ($ok) {
224 $this->settingsChanged = false;
225 }
226
227 return $ok;
228
229 }
230
236 public function delete()
237 {
238
239 return $this->dataConnector->deleteToolConsumer($this);
240
241 }
242
248 public function getRecordId()
249 {
250
251 return $this->id;
252
253 }
254
260 public function setRecordId($id)
261 {
262
263 $this->id = $id;
264
265 }
266
272 public function getKey()
273 {
274
275 return $this->key;
276
277 }
278
284 public function setKey($key)
285 {
286
287 $this->key = $key;
288
289 }
290
296 public function getDataConnector()
297 {
298
300
301 }
302
308 public function getIsAvailable()
309 {
310
312
313 $now = time();
314 if ($ok && !is_null($this->enableFrom)) {
315 $ok = $this->enableFrom <= $now;
316 }
317 if ($ok && !is_null($this->enableUntil)) {
318 $ok = $this->enableUntil > $now;
319 }
320
321 return $ok;
322
323 }
324
333 public function getSetting($name, $default = '')
334 {
335
336 if (array_key_exists($name, $this->settings)) {
337 $value = $this->settings[$name];
338 } else {
339 $value = $default;
340 }
341
342 return $value;
343
344 }
345
352 public function setSetting($name, $value = null)
353 {
354
355 $old_value = $this->getSetting($name);
356 if ($value !== $old_value) {
357 if (!empty($value)) {
358 $this->settings[$name] = $value;
359 } else {
360 unset($this->settings[$name]);
361 }
362 $this->settingsChanged = true;
363 }
364
365 }
366
372 public function getSettings()
373 {
374
375 return $this->settings;
376
377 }
378
384 public function setSettings($settings)
385 {
386
387 $this->settings = $settings;
388
389 }
390
396 public function saveSettings()
397 {
398
399 if ($this->settingsChanged) {
400 $ok = $this->save();
401 } else {
402 $ok = true;
403 }
404
405 return $ok;
406
407 }
408
414 public function hasToolSettingsService()
415 {
416
417 $url = $this->getSetting('custom_system_setting_url');
418
419 return !empty($url);
420
421 }
422
430 public function getToolSettings($simple = true)
431 {
432
433 $url = $this->getSetting('custom_system_setting_url');
434 $service = new Service\ToolSettings($this, $url, $simple);
435 $response = $service->get();
436
437 return $response;
438
439 }
440
448 public function setToolSettings($settings = array())
449 {
450
451 $url = $this->getSetting('custom_system_setting_url');
452 $service = new Service\ToolSettings($this, $url);
454
455 return $response;
456
457 }
458
470 {
471
472 if (!empty($url)) {
473// Check for query parameters which need to be included in the signature
474 $queryParams = array();
475 $queryString = parse_url($url, PHP_URL_QUERY);
476 if (!is_null($queryString)) {
477 $queryItems = explode('&', $queryString);
478 foreach ($queryItems as $item) {
479 if (strpos($item, '=') !== false) {
480 list($name, $value) = explode('=', $item);
481 $queryParams[urldecode($name)] = urldecode($value);
482 } else {
483 $queryParams[urldecode($item)] = '';
484 }
485 }
486 }
487 $params = $params + $queryParams;
488// Add standard parameters
489 $params['lti_version'] = $version;
490 $params['lti_message_type'] = $type;
491 $params['oauth_callback'] = 'about:blank';
492// Add OAuth signature
493 $hmacMethod = new OAuth\OAuthSignatureMethod_HMAC_SHA1();
494 $consumer = new OAuth\OAuthConsumer($this->getKey(), $this->secret, null);
496 $req->sign_request($hmacMethod, $consumer, null);
497 $params = $req->get_parameters();
498// Remove parameters being passed on the query string
499 foreach (array_keys($queryParams) as $name) {
500 unset($params[$name]);
501 }
502 }
503
504 return $params;
505
506 }
507
513 public static function addSignature($endpoint, $consumerKey, $consumerSecret, $data, $method = 'POST', $type = null)
514 {
515
516 $params = array();
517 if (is_array($data)) {
518 $params = $data;
519 }
520// Check for query parameters which need to be included in the signature
521 $queryParams = array();
522 $queryString = parse_url($endpoint, PHP_URL_QUERY);
523 if (!is_null($queryString)) {
524 $queryItems = explode('&', $queryString);
525 foreach ($queryItems as $item) {
526 if (strpos($item, '=') !== false) {
527 list($name, $value) = explode('=', $item);
528 $queryParams[urldecode($name)] = urldecode($value);
529 } else {
530 $queryParams[urldecode($item)] = '';
531 }
532 }
533 $params = $params + $queryParams;
534 }
535
536 if (!is_array($data)) {
537// Calculate body hash
538 $hash = base64_encode(sha1($data, true));
539 $params['oauth_body_hash'] = $hash;
540 }
541
542// Add OAuth signature
543 $hmacMethod = new OAuth\OAuthSignatureMethod_HMAC_SHA1();
544 $oauthConsumer = new OAuth\OAuthConsumer($consumerKey, $consumerSecret, null);
545 $oauthReq = OAuth\OAuthRequest::from_consumer_and_token($oauthConsumer, null, $method, $endpoint, $params);
546 $oauthReq->sign_request($hmacMethod, $oauthConsumer, null);
547 $params = $oauthReq->get_parameters();
548// Remove parameters being passed on the query string
549 foreach (array_keys($queryParams) as $name) {
550 unset($params[$name]);
551 }
552
553 if (!is_array($data)) {
554 $header = $oauthReq->to_header();
555 if (empty($data)) {
556 if (!empty($type)) {
557 $header .= "\nAccept: {$type}";
558 }
559 } else if (isset($type)) {
560 $header .= "\nContent-Type: {$type}";
561 $header .= "\nContent-Length: " . strlen($data);
562 }
563 return $header;
564 } else {
565 return $params;
566 }
567
568 }
569
580 public function doServiceRequest($service, $method, $format, $data)
581 {
582
583 $header = ToolConsumer::addSignature($service->endpoint, $this->getKey(), $this->secret, $data, $method, $format);
584
585// Connect to tool consumer
586 $http = new HTTPMessage($service->endpoint, $method, $data, $header);
587// Parse JSON response
588 if ($http->send() && !empty($http->response)) {
589 $http->responseJson = json_decode($http->response);
590 $http->ok = !is_null($http->responseJson);
591 }
592
593 return $http;
594
595 }
596
605 public static function fromRecordId($id, $dataConnector)
606 {
607
608 $toolConsumer = new ToolConsumer(null, $dataConnector);
609
610 $toolConsumer->initialize();
611 $toolConsumer->setRecordId($id);
612 if (!$dataConnector->loadToolConsumer($toolConsumer)) {
613 $toolConsumer->initialize();
614 }
615
616 return $toolConsumer;
617
618 }
619
620
621###
622### PRIVATE METHOD
623###
624
633 private function load($key, $autoEnable = false)
634 {
635
636 $this->key = $key;
637 $ok = $this->dataConnector->loadToolConsumer($this);
638 if (!$ok) {
639 $this->enabled = $autoEnable;
640 }
641
642 return $ok;
643
644 }
645
646}
$endpoint
An exception for terminatinating execution or to throw for unit testing.
Class to represent an HTTP message.
Definition: HTTPMessage.php:15
Class to represent an OAuth Consumer.
static from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=null)
pretty much a helper function to set up the request
Class to represent an OAuth HMAC_SHA1 signature method.
Class to provide a connection to a persistent store for LTI objects.
static getRandomString($length=8)
Generate a random string.
static getDataConnector($dbTableNamePrefix='', $db=null, $type='')
Create data connector object.
Class to implement the Tool Settings service.
Class to represent a tool consumer.
$enabled
Whether the tool consumer instance is enabled to accept incoming connection requests.
getToolSettings($simple=true)
Get Tool Settings.
load($key, $autoEnable=false)
Load the tool consumer from the database.
getSettings()
Get an array of all setting values.
setRecordId($id)
Sets the tool consumer record ID.
signParameters($url, $type, $version, $params)
Add the OAuth signature to an LTI message.
getDataConnector()
Get the data connector.
save()
Save the tool consumer to the database.
hasToolSettingsService()
Check if the Tool Settings service is supported.
setKey($key)
Set the tool consumer key.
$ltiVersion
LTI version (as reported by last tool consumer connection).
static addSignature($endpoint, $consumerKey, $consumerSecret, $data, $method='POST', $type=null)
Add the OAuth signature to an array of message parameters or to a header string.
$consumerVersion
Tool consumer version (as reported by last tool consumer connection).
$consumerGuid
Tool consumer GUID (as reported by first tool consumer connection).
$updated
Date/time when the object was last updated.
$consumerName
Name of tool consumer (as reported by last tool consumer connection).
$settings
Setting values (LTI parameters, custom parameters and local parameters).
static fromRecordId($id, $dataConnector)
Load the tool consumer from the database by its record ID.
$enableUntil
Date/time until which the tool consumer instance is enabled to accept incoming connection requests.
$protected
Whether the tool consumer instance is protected by matching the consumer_guid value in incoming reque...
setSetting($name, $value=null)
Set a setting value.
$idScope
Default scope to use when generating an Id value for a user.
getIsAvailable()
Is the consumer key available to accept launch requests?
getKey()
Get the tool consumer key.
initialise()
Initialise the tool consumer.
$lastAccess
Date of last connection from this tool consumer.
setSettings($settings)
Set an array of all setting values.
initialize()
Initialise the tool consumer.
$dataConnector
Data connector object or string.
$settingsChanged
Whether the settings value have changed since last saved.
__construct($key=null, $dataConnector=null, $autoEnable=false)
Class constructor.
getSetting($name, $default='')
Get a setting value.
$defaultEmail
Default email address (or email domain) to use when no email address is provided for a user.
$enableFrom
Date/time from which the the tool consumer instance is enabled to accept incoming connection requests...
$name
Local name of tool consumer.
$cssPath
Optional CSS path (as reported by last tool consumer connection).
setToolSettings($settings=array())
Perform a Tool Settings service request.
$created
Date/time when the object was created.
doServiceRequest($service, $method, $format, $data)
Perform a service request.
getRecordId()
Get the tool consumer record ID.
const ID_SCOPE_ID_ONLY
Use ID value only.
$consumer
Definition: demo.php:30
$req
Definition: getUserInfo.php:20
$service
Definition: login.php:15
$format
Definition: metadata.php:141
$type
$url
$response
$http
Definition: raiseError.php:7
settings()
Definition: settings.php:2
$params
Definition: disable.php:11