LTI Integration Library 4.10.3
PHP class library for building LTI integrations
 
Loading...
Searching...
No Matches
DataConnector.php
1<?php
2
4
15
26{
27
31 const PLATFORM_TABLE_NAME = 'lti2_consumer';
32
39 const CONSUMER_TABLE_NAME = self::PLATFORM_TABLE_NAME;
40
44 const CONTEXT_TABLE_NAME = 'lti2_context';
45
49 const RESOURCE_LINK_TABLE_NAME = 'lti2_resource_link';
50
54 const USER_RESULT_TABLE_NAME = 'lti2_user_result';
55
59 const RESOURCE_LINK_SHARE_KEY_TABLE_NAME = 'lti2_share_key';
60
64 const NONCE_TABLE_NAME = 'lti2_nonce';
65
69 const ACCESS_TOKEN_TABLE_NAME = 'lti2_access_token';
70
74 const TOOL_TABLE_NAME = 'lti2_tool';
75
81 protected $db = null;
82
88 protected $dbTableNamePrefix = '';
89
95 protected $dateFormat = 'Y-m-d';
96
102 protected $timeFormat = 'H:i:s';
103
109 private static $memcache = null;
110
117 protected function __construct($db, $dbTableNamePrefix = '')
118 {
119 $this->db = $db;
120 $this->dbTableNamePrefix = $dbTableNamePrefix;
121 }
122
131 public static function useMemcache($host = null, $port = -1)
132 {
133 if (is_null($host)) {
134 $useMemcache = !empty(self::$memcache);
135 } else {
136 $useMemcache = !empty($host);
137 if ($useMemcache) {
138 if (!class_exists('Memcache')) {
139 $useMemcache = false;
140 Util::logError("Memcache extension not installed");
141 } else {
142 if ($port < 0) {
143 self::$memcache = memcache_connect($host);
144 } else {
145 self::$memcache = memcache_connect($host, $port);
146 }
147 $useMemcache = !empty(self::$memcache);
148 if (!$useMemcache) {
149 if ($port < 0) {
150 Util::logError("Unable to connect to memcache at {$host}");
151 } else {
152 Util::logError("Unable to connect to memcache at {$host}:{$port}");
153 }
154 }
155 }
156 }
157 if (!$useMemcache) {
158 self::$memcache = null;
159 }
160 }
161
162 return $useMemcache;
163 }
164
165###
166### Platform methods
167###
168
179 public function loadToolConsumer($consumer)
180 {
181 Util::logDebug('Method ceLTIc\LTI\DataConnector\DataConnector::loadToolConsumer() has been deprecated; please use ceLTIc\LTI\DataConnector\DataConnector::loadPlatform() instead.',
182 true);
183 return $this->loadPlatform($consumer);
184 }
185
196 public function saveToolConsumer($consumer)
197 {
198 Util::logDebug('Method ceLTIc\LTI\DataConnector\DataConnector::saveToolConsumer() has been deprecated; please use ceLTIc\LTI\DataConnector\DataConnector::savePlatform() instead.',
199 true);
200 return $this->savePlatform($consumer);
201 }
202
213 public function deleteToolConsumer($consumer)
214 {
215 Util::logDebug('Method ceLTIc\LTI\DataConnector\DataConnector::deleteToolConsumer() has been deprecated; please use ceLTIc\LTI\DataConnector\DataConnector::deletePlatform() instead.',
216 true);
217 return $this->deletePlatform($consumer);
218 }
219
228 public function getToolConsumers()
229 {
230 Util::logDebug('Method ceLTIc\LTI\DataConnector\DataConnector::getToolConsumers() has been deprecated; please use ceLTIc\LTI\DataConnector\DataConnector::getPlatforms() instead.',
231 true);
232 return $this->getPlatforms();
233 }
234
242 public function loadPlatform($platform)
243 {
244 $platform->secret = 'secret';
245 $platform->enabled = true;
246 $now = time();
247 $platform->created = $now;
248 $platform->updated = $now;
249
250 return true;
251 }
252
260 public function savePlatform($platform)
261 {
262 $platform->updated = time();
263
264 return true;
265 }
266
274 public function deletePlatform($platform)
275 {
276 $platform->initialize();
277
278 return true;
279 }
280
286 public function getPlatforms()
287 {
288 return array();
289 }
290
291###
292### Context methods
293###
294
302 public function loadContext($context)
303 {
304 $now = time();
305 $context->created = $now;
306 $context->updated = $now;
307
308 return true;
309 }
310
318 public function saveContext($context)
319 {
320 $context->updated = time();
321
322 return true;
323 }
324
332 public function deleteContext($context)
333 {
334 $context->initialize();
335
336 return true;
337 }
338
339###
340### ResourceLink methods
341###
342
350 public function loadResourceLink($resourceLink)
351 {
352 $now = time();
353 $resourceLink->created = $now;
354 $resourceLink->updated = $now;
355
356 return true;
357 }
358
366 public function saveResourceLink($resourceLink)
367 {
368 $resourceLink->updated = time();
369
370 return true;
371 }
372
380 public function deleteResourceLink($resourceLink)
381 {
382 $resourceLink->initialize();
383
384 return true;
385 }
386
399 public function getUserResultSourcedIDsResourceLink($resourceLink, $localOnly, $idScope)
400 {
401 return array();
402 }
403
411 public function getSharesResourceLink($resourceLink)
412 {
413 return array();
414 }
415
416###
417### PlatformNonce methods
418###
419
430 public function loadConsumerNonce($nonce)
431 {
432 Util::logDebug('Method ceLTIc\LTI\DataConnector\DataConnector::loadConsumerNonce() has been deprecated; please use ceLTIc\LTI\DataConnector\DataConnector::loadPlatformNonce() instead.',
433 true);
434 return $this->loadPlatformNonce($nonce);
435 }
436
447 public function saveConsumerNonce($nonce)
448 {
449 Util::logDebug('Method ceLTIc\LTI\DataConnector\DataConnector::saveConsumerNonce() has been deprecated; please use ceLTIc\LTI\DataConnector\DataConnector::savePlatformNonce() instead.',
450 true);
451 return $this->savePlatformNonce($nonce);
452 }
453
464 public function deleteConsumerNonce($nonce)
465 {
466 Util::logDebug('Method ceLTIc\LTI\DataConnector\DataConnector::deleteConsumerNonce() has been deprecated; please use ceLTIc\LTI\DataConnector\DataConnector::deletePlatformNonce() instead.',
467 true);
468 return $this->deletePlatformNonce($nonce);
469 }
470
478 public function loadPlatformNonce($nonce)
479 {
480 $ok = false; // assume the nonce does not already exist
481 if (!empty(self::$memcache)) {
482 $id = $nonce->getPlatform()->getRecordId();
483 $value = $nonce->getValue();
484 $name = self::NONCE_TABLE_NAME . "_{$id}_{$value}";
485 $ok = self::$memcache->get($name) !== false;
486 }
487
488 return $ok;
489 }
490
498 public function savePlatformNonce($nonce)
499 {
500 $ok = true; // assume the nonce was saved
501 if (!empty(self::$memcache)) {
502 $ok = false;
503 $id = $nonce->getPlatform()->getRecordId();
504 $value = $nonce->getValue();
505 $expires = $nonce->expires;
506 $name = self::NONCE_TABLE_NAME . "_{$id}_{$value}";
507 $current = self::$memcache->get($name);
508 if ($current === false) {
509 $ok = self::$memcache->set($name, true, 0, $expires);
510 }
511 }
512
513 return $ok;
514 }
515
523 public function deletePlatformNonce($nonce)
524 {
525 $ok = true; // assume the nonce was deleted
526 if (!empty(self::$memcache)) {
527 $id = $nonce->getPlatform()->getRecordId();
528 $value = $nonce->getValue();
529 $name = self::NONCE_TABLE_NAME . "_{$id}_{$value}";
530 $ok = self::$memcache->get($name);
531 if ($ok !== false) {
532 $ok = self::$memcache->delete($name);
533 }
534 }
535
536 return $ok;
537 }
538
539###
540### AccessToken methods
541###
542
550 public function loadAccessToken($accessToken)
551 {
552 $ok = false; // assume the access token does not already exist
553 if (!empty(self::$memcache)) {
554 $id = $accessToken->getPlatform()->getRecordId();
555 $value = $accessToken->token;
556 $name = self::ACCESS_TOKEN_TABLE_NAME . "_{$id}_{$value}";
557 $current = self::$memcache->get($name);
558 $ok = is_array($current);
559 if ($ok) {
560 $accessToken->scopes = $current['scopes'];
561 $accessToken->token = $current['token'];
562 $accessToken->expires = $current['expires'];
563 $accessToken->created = $current['created'];
564 $accessToken->updated = $current['updated'];
565 }
566 }
567
568 return $ok;
569 }
570
578 public function saveAccessToken($accessToken)
579 {
580 $ok = true; // assume the access token was saved
581 if (!empty(self::$memcache)) {
582 $ok = false;
583 $id = $accessToken->getPlatform()->getRecordId();
584 $value = $accessToken->token;
585 $expires = $accessToken->expires;
586 $name = self::ACCESS_TOKEN_TABLE_NAME . "_{$id}_{$value}";
587 $current = self::$memcache->get($name);
588 if ($current === false) {
589 $current = array(
590 'scopes' => $accessToken->scopes,
591 'token' => $value,
592 'expires' => $expires,
593 'created' => $accessToken->created,
594 'updated' => $accessToken->updated
595 );
596 $ok = self::$memcache->set($name, $current, 0, $expires);
597 }
598 }
599
600 return $ok;
601 }
602
603###
604### ResourceLinkShareKey methods
605###
606
614 public function loadResourceLinkShareKey($shareKey)
615 {
616 return true;
617 }
618
626 public function saveResourceLinkShareKey($shareKey)
627 {
628 return true;
629 }
630
638 public function deleteResourceLinkShareKey($shareKey)
639 {
640 return true;
641 }
642
643###
644### UserResult methods
645###
646
654 public function loadUserResult($userresult)
655 {
656 $now = time();
657 $userresult->created = $now;
658 $userresult->updated = $now;
659
660 return true;
661 }
662
670 public function saveUserResult($userresult)
671 {
672 $userresult->updated = time();
673
674 return true;
675 }
676
684 public function deleteUserResult($userresult)
685 {
686 $userresult->initialize();
687
688 return true;
689 }
690
691###
692### Tool methods
693###
694
702 public function loadTool($tool)
703 {
704 $tool->secret = 'secret';
705 $tool->enabled = true;
706 $now = time();
707 $tool->created = $now;
708 $tool->updated = $now;
709
710 return true;
711 }
712
720 public function saveTool($tool)
721 {
722 $tool->updated = time();
723
724 return true;
725 }
726
734 public function deleteTool($tool)
735 {
736 $tool->initialize();
737
738 return true;
739 }
740
746 public function getTools()
747 {
748 return array();
749 }
750
751###
752### Other methods
753###
754
771 public static function getDataConnector($db = null, $dbTableNamePrefix = '', $type = '')
772 {
773 if (is_null($dbTableNamePrefix)) {
775 }
776 if (!is_null($db) && empty($type)) {
777 if (is_object($db)) {
778 $type = get_class($db);
779 } elseif (is_resource($db)) {
780 $type = strtok(get_resource_type($db), ' ');
781 }
782 }
783 $type = strtolower($type);
784 if ($type === 'pdo') {
785 if ($db->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'pgsql') {
786 $type .= '_pgsql';
787 } elseif ($db->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'oci') {
788 $type .= '_oci';
789 }
790 }
791 if (!empty($type)) {
792 $type = "DataConnector_{$type}";
793 } else {
794 $type = 'DataConnector';
795 }
796 $type = "\\ceLTIc\\LTI\\DataConnector\\{$type}";
797 $dataConnector = new $type($db, $dbTableNamePrefix);
798
799 return $dataConnector;
800 }
801
814 public static function getRandomString($length = 8)
815 {
816 Util::logDebug('Method ceLTIc\LTI\DataConnector::getRandomString() has been deprecated; please use ceLTIc\LTI\Util::getRandomString() instead.',
817 true);
818 return Util::getRandomString($length);
819 }
820
832 public function escape($value, $addQuotes = true)
833 {
834 return static::quoted($value, $addQuotes);
835 }
836
848 public static function quoted($value, $addQuotes = true)
849 {
850 if (is_null($value)) {
851 $value = 'null';
852 } else {
853 $value = str_replace('\'', '\'\'', $value);
854 if ($addQuotes) {
855 $value = "'{$value}'";
856 }
857 }
858
859 return $value;
860 }
861
868 protected function fixPlatformSettings($platform, $isSave)
869 {
870 if (!$isSave) {
871 $platform->authorizationServerId = $platform->getSetting('_authorization_server_id', $platform->authorizationServerId);
872 $platform->setSetting('_authorization_server_id');
873 $platform->authenticationUrl = $platform->getSetting('_authentication_request_url', $platform->authenticationUrl);
874 $platform->setSetting('_authentication_request_url');
875 $platform->accessTokenUrl = $platform->getSetting('_oauth2_access_token_url', $platform->accessTokenUrl);
876 $platform->setSetting('_oauth2_access_token_url');
877 $platform->jku = $platform->getSetting('_jku', $platform->jku);
878 $platform->setSetting('_jku');
879 $platform->encryptionMethod = $platform->getSetting('_encryption_method', $platform->encryptionMethod);
880 $platform->setSetting('_encryption_method');
881 $platform->debugMode = $platform->getSetting('_debug', $platform->debugMode ? 'true' : 'false') === 'true';
882 $platform->setSetting('_debug');
883 if ($platform->debugMode) {
885 }
886 } else {
887 $platform->setSetting('_authorization_server_id',
888 !empty($platform->authorizationServerId) ? $platform->authorizationServerId : null);
889 $platform->setSetting('_authentication_request_url',
890 !empty($platform->authenticationUrl) ? $platform->authenticationUrl : null);
891 $platform->setSetting('_oauth2_access_token_url', !empty($platform->accessTokenUrl) ? $platform->accessTokenUrl : null);
892 $platform->setSetting('_jku', !empty($platform->jku) ? $platform->jku : null);
893 $platform->setSetting('_encryption_method', !empty($platform->encryptionMethod) ? $platform->encryptionMethod : null);
894 $platform->setSetting('_debug', $platform->debugMode ? 'true' : null);
895 }
896 }
897
904 protected function fixToolSettings($tool, $isSave)
905 {
906 if (!$isSave) {
907 $tool->encryptionMethod = $tool->getSetting('_encryption_method', $tool->encryptionMethod);
908 $tool->setSetting('_encryption_method');
909 $tool->debugMode = $tool->getSetting('_debug', $tool->debugMode ? 'true' : 'false') === 'true';
910 $tool->setSetting('_debug');
911 if ($tool->debugMode) {
913 }
914 } else {
915 $tool->setSetting('_encryption_method', !empty($tool->encryptionMethod) ? $tool->encryptionMethod : null);
916 $tool->setSetting('_debug', $tool->debugMode ? 'true' : null);
917 }
918 }
919
920}
Class to represent an HTTP message.
Class to represent a platform context.
Definition Context.php:18
Class to provide a connection to a persistent store for LTI objects.
loadResourceLinkShareKey($shareKey)
Load resource link share key object.
escape($value, $addQuotes=true)
Escape a string for use in a database query.
saveUserResult($userresult)
Save user object.
loadContext($context)
Load context object.
deleteTool($tool)
Delete tool object.
const RESOURCE_LINK_SHARE_KEY_TABLE_NAME
Default name for database table used to store resource link share keys.
const CONTEXT_TABLE_NAME
Default name for database table used to store contexts.
savePlatform($platform)
Save platform object.
saveConsumerNonce($nonce)
Save nonce object.
const NONCE_TABLE_NAME
Default name for database table used to store nonce values.
getToolConsumers()
Load tool consumer objects.
deletePlatform($platform)
Delete platform object.
saveAccessToken($accessToken)
Save access token object.
deletePlatformNonce($nonce)
Delete nonce object.
static getDataConnector($db=null, $dbTableNamePrefix='', $type='')
Create data connector object.
saveResourceLinkShareKey($shareKey)
Save resource link share key object.
deleteConsumerNonce($nonce)
Delete nonce object.
saveContext($context)
Save context object.
loadPlatformNonce($nonce)
Load nonce object.
static getRandomString($length=8)
Generate a random string.
loadAccessToken($accessToken)
Load access token object.
loadConsumerNonce($nonce)
Load nonce object.
const PLATFORM_TABLE_NAME
Default name for database table used to store platforms.
const ACCESS_TOKEN_TABLE_NAME
Default name for database table used to store access token values.
$dbTableNamePrefix
Prefix for database table names.
saveToolConsumer($consumer)
Save tool consumer object.
fixToolSettings($tool, $isSave)
Adjust the settings for any tool properties being stored as a setting value.
loadPlatform($platform)
Load platform object.
getUserResultSourcedIDsResourceLink($resourceLink, $localOnly, $idScope)
Get array of user objects.
__construct($db, $dbTableNamePrefix='')
Class constructor.
$dateFormat
SQL date format (default = 'Y-m-d')
$timeFormat
SQL time format (default = 'H:i:s')
getSharesResourceLink($resourceLink)
Get array of shares defined for this resource link.
loadResourceLink($resourceLink)
Load resource link object.
static quoted($value, $addQuotes=true)
Quote a string for use in a database query.
savePlatformNonce($nonce)
Save nonce object.
const CONSUMER_TABLE_NAME
Default name for database table used to store platforms.
saveResourceLink($resourceLink)
Save resource link object.
loadToolConsumer($consumer)
Load tool consumer object.
deleteContext($context)
Delete context object.
loadUserResult($userresult)
Load user object.
static useMemcache($host=null, $port=-1)
Set/check whether memcached should be used when available.
const USER_RESULT_TABLE_NAME
Default name for database table used to store users.
deleteUserResult($userresult)
Delete user object.
deleteResourceLinkShareKey($shareKey)
Delete resource link share key object.
const RESOURCE_LINK_TABLE_NAME
Default name for database table used to store resource links.
const TOOL_TABLE_NAME
Default name for database table used to store tools.
fixPlatformSettings($platform, $isSave)
Adjust the settings for any platform properties being stored as a setting value.
deleteToolConsumer($consumer)
Delete tool consumer object.
deleteResourceLink($resourceLink)
Delete resource link object.
Class to represent a platform nonce.
Class to represent a platform.
Definition Platform.php:18
Class to represent a platform resource link share key.
Class to represent a platform resource link share.
Class to represent an LTI Tool.
Definition Tool.php:24
Class to represent a platform user association with a resource link.
Class to implement utility methods.
Definition Util.php:15
static getRandomString($length=8)
Generate a random string.
Definition Util.php:523
static $logLevel
Current logging level.
Definition Util.php:202
static logError($message, $showSource=true)
Log an error message.
Definition Util.php:248
const LOGLEVEL_DEBUG
Log all messages.
Definition Util.php:159
static logDebug($message, $showSource=false)
Log a debug message.
Definition Util.php:274