/** * Send a service request to the tool consumer. * * @param Action $action * @param string $url URL to send request to * @return bool True if the request successfully obtained a response */ private function doLTI11Service(Action $action, $url) { $ok = false; $xmlRequest = $action->asXML(); // Calculate body hash $hash = base64_encode(sha1($xmlRequest, true)); $params = ['oauth_body_hash' => $hash]; // Add OAuth signature $consumer = new Consumer($this->consumer->getKey(), $this->consumer->secret, null); // Connect to tool consumer $response = ClientFactory::make()->sendSigned($url, 'POST', $xmlRequest, ['content-type' => 'application/xml']); // Parse XML response if ($response->isSuccessful()) { $response = $response->getWrappedResponse(); try { $extDoc = new DOMDocument(); $extDoc->loadXML((string) $response->getBody()); $this->extNodes = $this->domNodeToArray($extDoc->documentElement); if (isset($this->extNodes['imsx_POXHeader']['imsx_POXResponseHeaderInfo']['imsx_statusInfo']['imsx_codeMajor']) && $this->extNodes['imsx_POXHeader']['imsx_POXResponseHeaderInfo']['imsx_statusInfo']['imsx_codeMajor'] == 'success') { $ok = true; } } catch (Exception $e) { } } return $ok; }
/** * Check if a share arrangement is in place. * * @param array $requestBody * @throws Exception */ private function checkForShare(array $requestBody) { $doSaveResourceLink = true; $key = $this->resourceLink->primary_consumer_key; $id = $this->resourceLink->primary_resource_link_id; $shareRequest = isset($requestBody['custom_share_key']) && !empty($requestBody['custom_share_key']); if ($shareRequest) { if (!$this->allowSharing) { throw new Exception('Your sharing request has been refused because sharing is not being permitted'); } else { // Check if this is a new share key $share_key = new ResourceLinkShareKey($this->resourceLink, $requestBody['custom_share_key']); if (!is_null($share_key->primary_consumer_key) && !is_null($share_key->primary_resource_link_id)) { // Update resource link with sharing primary resource link details $key = $share_key->primary_consumer_key; $id = $share_key->primary_resource_link_id; if ($key == $this->consumer->getKey() && $id == $this->resourceLink->getId()) { throw new Exception('It is not possible to share your resource link with yourself'); } $this->resourceLink->primary_consumer_key = $key; $this->resourceLink->primary_resource_link_id = $id; $this->resourceLink->share_approved = $share_key->auto_approve; if (!$this->resourceLink->save()) { throw new Exception('An error occurred initialising your share arrangement'); } $doSaveResourceLink = false; $this->user->getResourceLink()->primary_consumer_key = $key; $this->user->getResourceLink()->primary_resource_link_id = $id; $this->user->getResourceLink()->share_approved = $share_key->auto_approve; $this->user->getResourceLink()->updated = time(); // Remove share key $share_key->delete(); } if (!is_null($key)) { throw new Exception('You have requested to share a resource link but none is available'); } if (is_null($this->user->getResourceLink()->share_approved) || !$this->user->getResourceLink()->share_approved) { throw new Exception('Your share request is waiting to be approved'); } } } else { if (!is_null($key)) { // Check no share is in place throw new Exception('You have not requested to share a resource link but an arrangement is currently in place'); } } // Look up primary resource link if (!is_null($key)) { $consumer = new ToolConsumer($key, $this->storage); // TODO: Move to load function if (is_null($consumer->created)) { throw new Exception('Unable to load tool consumer'); } $resource_link = new ResourceLink($consumer, $id); if (is_null($resource_link->created)) { throw new Exception('Unable to load resource link being shared'); } if ($doSaveResourceLink) { $this->resourceLink->save(); } $this->resourceLink = $resource_link; } }