173 private $settings =
null;
180 private $settingsChanged =
false;
187 private $dataConnector =
null;
196 public function __construct($key =
null, $dataConnector =
null, $autoEnable =
false)
199 if (empty($dataConnector)) {
202 $this->dataConnector = $dataConnector;
203 if (!is_null($key) && (strlen($key) > 0)) {
204 $this->load($key, $autoEnable);
218 $this->secret =
null;
219 $this->signatureMethod =
'HMAC-SHA1';
220 $this->ltiVersion =
null;
221 $this->consumerName =
null;
222 $this->consumerVersion =
null;
223 $this->consumerGuid =
null;
224 $this->profile =
null;
225 $this->toolProxy =
null;
226 $this->settings = array();
227 $this->
protected =
false;
228 $this->enabled =
false;
229 $this->enableFrom =
null;
230 $this->enableUntil =
null;
231 $this->lastAccess =
null;
233 $this->defaultEmail =
'';
234 $this->created =
null;
235 $this->updated =
null;
255 $ok = $this->dataConnector->saveToolConsumer($this);
257 $this->settingsChanged =
false;
268 public function delete()
270 return $this->dataConnector->deleteToolConsumer($this);
321 if (!empty($this->consumerVersion)) {
322 list($familyCode, $version) = explode(
'-', $this->consumerVersion, 2);
335 return $this->dataConnector;
348 if ($ok && !is_null($this->enableFrom)) {
349 $ok = $this->enableFrom <= $now;
351 if ($ok && !is_null($this->enableUntil)) {
352 $ok = $this->enableUntil > $now;
368 if (array_key_exists(
$name, $this->settings)) {
369 $value = $this->settings[
$name];
386 if ($value !== $old_value) {
387 if (!empty($value)) {
388 $this->settings[
$name] = $value;
390 unset($this->settings[
$name]);
392 $this->settingsChanged =
true;
403 return $this->settings;
413 $this->settings = $settings;
423 if ($this->settingsChanged) {
439 $has = !empty($this->
getSetting(
'custom_system_setting_url'));
441 $has = self::hasApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getConsumer()->
getFamilyCode());
457 if (!empty($this->
getSetting(
'custom_system_setting_url'))) {
458 $url = $this->
getSetting(
'custom_system_setting_url');
460 $settings = $service->get($mode);
461 $this->lastServiceRequest = $service->getHTTPMessage();
462 $ok = $settings !==
false;
464 if (!$ok && $this->hasApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getConsumer()->
getFamilyCode())) {
465 $className = $this->getApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getConsumer()->
getFamilyCode());
466 $hook =
new $className($this);
467 $settings = $hook->getToolSettings($mode, $simple);
483 if (!empty($this->
getSetting(
'custom_system_setting_url'))) {
484 $url = $this->
getSetting(
'custom_system_setting_url');
486 $ok = $service->set($settings);
487 $this->lastServiceRequest = $service->getHTTPMessage();
489 if (!$ok && $this->hasApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getConsumer()->
getFamilyCode())) {
490 $className = $this->getApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getConsumer()->
getFamilyCode());
491 $hook =
new $className($this);
492 $ok = $hook->setToolSettings($settings);
512 $params[
'lti_version'] = $version;
513 $params[
'lti_message_type'] = $type;
515 $params = $this->
addSignature($url, $params,
'POST', $type);
535 $header = $this->
addSignature($url, $data, $method, $type);
552 public function addSignature($endpoint, $data, $method =
'POST', $type =
null, $forTP =
false)
554 switch ($this->signatureMethod) {
560 return $this->addOAuthSignature($endpoint, $data, $method, $type);
580 $header = $this->
addSignature($service->endpoint, $data, $method, $format);
583 $http =
new HTTPMessage($service->endpoint, $method, $data, $header);
585 if ($http->send() && !empty($http->response)) {
586 $http->responseJson = json_decode($http->response);
587 $http->ok = !is_null($http->responseJson);
605 $toolConsumer->initialize();
606 $toolConsumer->setRecordId($id);
607 if (!$dataConnector->loadToolConsumer($toolConsumer)) {
608 $toolConsumer->initialize();
611 return $toolConsumer;
626 private function load($key, $autoEnable =
false)
629 $ok = $this->dataConnector->loadToolConsumer($this);
631 $this->enabled = $autoEnable;
642 private function addOAuthSignature($endpoint, $data, $method, $type)
645 if (is_array($data)) {
647 $params[
'oauth_callback'] =
'about:blank';
650 $queryString = parse_url($endpoint, PHP_URL_QUERY);
652 $params = array_merge_recursive($queryParams, $params);
654 if (!is_array($data)) {
656 switch ($this->signatureMethod) {
658 $hash = base64_encode(hash(
'sha224', $data, TRUE));
661 $hash = base64_encode(hash(
'sha256', $data, TRUE));
664 $hash = base64_encode(hash(
'sha384', $data, TRUE));
667 $hash = base64_encode(hash(
'sha512', $data, TRUE));
670 $hash = base64_encode(sha1($data, TRUE));
673 $params[
'oauth_body_hash'] = $hash;
677 switch ($this->signatureMethod) {
679 $hmacMethod =
new OAuth\OAuthSignatureMethod_HMAC_SHA224();
682 $hmacMethod =
new OAuth\OAuthSignatureMethod_HMAC_SHA256();
685 $hmacMethod =
new OAuth\OAuthSignatureMethod_HMAC_SHA384();
688 $hmacMethod =
new OAuth\OAuthSignatureMethod_HMAC_SHA512();
691 $hmacMethod =
new OAuth\OAuthSignatureMethod_HMAC_SHA1();
694 $oauthConsumer =
new OAuth\OAuthConsumer($this->key, $this->secret,
null);
696 $oauthReq->sign_request($hmacMethod, $oauthConsumer,
null);
697 if (!is_array($data)) {
698 $header = $oauthReq->to_header();
701 $header .=
"\nAccept: {$type}";
703 } elseif (isset($type)) {
704 $header .=
"\nContent-Type: {$type}";
705 $header .=
"\nContent-Length: " . strlen($data);
709 $params = $oauthReq->get_parameters();
710 foreach ($queryParams as $key => $value) {
711 if (!is_array($value)) {
712 if (!is_array($params[$key])) {
713 if ($params[$key] === $value) {
714 unset($params[$key]);
717 $params[$key] = array_diff($params[$key], array($value));
720 foreach ($value as $element) {
721 $params[$key] = array_diff($params[$key], array($value));
static from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=null)
pretty much a helper function to set up the request
static getDataConnector($db=null, $dbTableNamePrefix='', $type='')
Create data connector object.
Class to represent an HTTP message request.
static parse_parameters($input)
static getRandomString($length=8)
Generate a random string.