/** * Load tool consumer object. * * @param ToolConsumer $consumer ToolConsumer object * * @return boolean True if the tool consumer object was successfully loaded */ public function loadToolConsumer($consumer) { $ok = false; if (!empty($consumer->getRecordId())) { $sql = 'SELECT consumer_pk, name, consumer_key256, consumer_key, secret, lti_version, ' . 'consumer_name, consumer_version, consumer_guid, ' . 'profile, tool_proxy, settings, protected, enabled, ' . 'enable_from, enable_until, last_access, created, updated ' . "FROM {$this->dbTableNamePrefix}" . DataConnector::CONSUMER_TABLE_NAME . ' ' . 'WHERE consumer_pk = :id'; $query = $this->db->prepare($sql); $id = $consumer->getRecordId(); $query->bindValue('id', $id, PDO::PARAM_INT); } else { $sql = 'SELECT consumer_pk, name, consumer_key256, consumer_key, secret, lti_version, ' . 'consumer_name, consumer_version, consumer_guid, ' . 'profile, tool_proxy, settings, protected, enabled, ' . 'enable_from, enable_until, last_access, created, updated ' . "FROM {$this->dbTableNamePrefix}" . DataConnector::CONSUMER_TABLE_NAME . ' ' . 'WHERE consumer_key256 = :key256'; $query = $this->db->prepare($sql); $key256 = DataConnector::getConsumerKey($consumer->getKey()); $query->bindValue('key256', $key256, PDO::PARAM_STR); } if ($query->execute()) { while ($row = $query->fetch(PDO::FETCH_ASSOC)) { $row = array_change_key_case($row); if (empty($key256) || empty($row['consumer_key']) || $consumer->getKey() === $row['consumer_key']) { $consumer->setRecordId(intval($row['consumer_pk'])); $consumer->name = $row['name']; $consumer->setkey(empty($row['consumer_key']) ? $row['consumer_key256'] : $row['consumer_key']); $consumer->secret = $row['secret']; $consumer->ltiVersion = $row['lti_version']; $consumer->consumerName = $row['consumer_name']; $consumer->consumerVersion = $row['consumer_version']; $consumer->consumerGuid = $row['consumer_guid']; $consumer->profile = json_decode($row['profile']); $consumer->toolProxy = $row['tool_proxy']; $settings = unserialize($row['settings']); if (!is_array($settings)) { $settings = array(); } $consumer->setSettings($settings); $consumer->protected = intval($row['protected']) === 1; $consumer->enabled = intval($row['enabled']) === 1; $consumer->enableFrom = null; if (!is_null($row['enable_from'])) { $consumer->enableFrom = strtotime($row['enable_from']); } $consumer->enableUntil = null; if (!is_null($row['enable_until'])) { $consumer->enableUntil = strtotime($row['enable_until']); } $consumer->lastAccess = null; if (!is_null($row['last_access'])) { $consumer->lastAccess = strtotime($row['last_access']); } $consumer->created = strtotime($row['created']); $consumer->updated = strtotime($row['updated']); $ok = true; break; } } } return $ok; }
/** * Load the tool proxy from the database. * * @param string $id The tool proxy id value * * @return boolean True if the tool proxy was successfully loaded */ private function load($id) { $this->initialize(); $this->id = $id; $ok = $this->dataConnector->loadToolProxy($this); if (!$ok) { $this->enabled = $autoEnable; } return $ok; }
/** * Load the resource link share key from the database. */ private function load() { $this->initialize(); $this->dataConnector->loadResourceLinkShareKey($this); if (!is_null($this->id)) { $this->length = strlen($this->id); } if (!is_null($this->expires)) { $this->life = ($this->expires - time()) / 60 / 60; } }
/** * Save user object. * * @param User $user User object * * @return boolean True if the user object was successfully saved */ public function saveUser($user) { $time = time(); $now = date("{$this->dateFormat} {$this->timeFormat}", $time); if (is_null($user->created)) { $sql = sprintf("INSERT INTO {$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . ' (resource_link_pk, ' . 'lti_user_id, lti_result_sourcedid, created, updated) ' . 'VALUES (%d, %s, %s, %s, %s)', $user->getResourceLink()->getRecordId(), DataConnector::quoted($user->getId(ToolProvider\ToolProvider::ID_SCOPE_ID_ONLY)), DataConnector::quoted($user->ltiResultSourcedId), DataConnector::quoted($now), DataConnector::quoted($now)); } else { $sql = sprintf("UPDATE {$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . ' ' . 'SET lti_result_sourcedid = %s, updated = %s ' . 'WHERE (user_pk = %d)', DataConnector::quoted($user->ltiResultSourcedId), DataConnector::quoted($now), $user->getRecordId()); } $ok = mysql_query($sql); if ($ok) { if (is_null($user->created)) { $user->setRecordId(mysql_insert_id()); $user->created = $time; } $user->updated = $time; } return $ok; }
/** * Send the tool proxy to the Tool Consumer * * @return boolean True if the tool proxy was accepted */ public function doToolProxyService() { // Create tool proxy $toolProxyService = $this->findService('application/vnd.ims.lti.v2.toolproxy+json', array('POST')); $secret = DataConnector::getRandomString(12); $toolProxy = new MediaType\ToolProxy($this, $toolProxyService, $secret); $http = $this->consumer->doServiceRequest($toolProxyService, 'POST', 'application/vnd.ims.lti.v2.toolproxy+json', json_encode($toolProxy)); $ok = $http->ok && $http->status == 201 && isset($http->responseJson->tool_proxy_guid) && strlen($http->responseJson->tool_proxy_guid) > 0; if ($ok) { $this->consumer->setKey($http->responseJson->tool_proxy_guid); $this->consumer->secret = $toolProxy->security_contract->shared_secret; $this->consumer->toolProxy = json_encode($toolProxy); $this->consumer->save(); } return $ok; }
/** * Save tool consumer object. * * @param ToolConsumer $consumer Consumer object * @return boolean True if the tool consumer object was successfully saved */ public function saveToolConsumer($consumer) { global $DB; $key = $consumer->getKey(); $key256 = DataConnector::getConsumerKey($key); if ($key === $key256) { $key = null; } $protected = $consumer->protected ? 1 : 0; $enabled = $consumer->enabled ? 1 : 0; $profile = !empty($consumer->profile) ? json_encode($consumer->profile) : null; $settingsvalue = serialize($consumer->getSettings()); $now = time(); $consumer->updated = $now; $data = ['consumerkey256' => $key256, 'consumerkey' => $key, 'name' => $consumer->name, 'secret' => $consumer->secret, 'ltiversion' => $consumer->ltiVersion, 'consumername' => $consumer->consumerName, 'consumerversion' => $consumer->consumerVersion, 'consumerguid' => $consumer->consumerGuid, 'profile' => $profile, 'toolproxy' => $consumer->toolProxy, 'settings' => $settingsvalue, 'protected' => $protected, 'enabled' => $enabled, 'enablefrom' => $consumer->enableFrom, 'enableuntil' => $consumer->enableUntil, 'lastaccess' => $consumer->lastAccess, 'updated' => $consumer->updated]; $id = $consumer->getRecordId(); if (empty($id)) { $consumer->created = $now; $data['created'] = $consumer->created; $id = $DB->insert_record($this->consumertable, (object) $data); if ($id) { $consumer->setRecordId($id); return true; } } else { $data['id'] = $id; return $DB->update_record($this->consumertable, (object) $data); } return false; }
/** * Load the tool consumer from the database by its record ID. * * @param string $id The consumer key record ID * @param DataConnector $dataConnector Database connection object * * @return object ToolConsumer The tool consumer object */ public static function fromRecordId($id, $dataConnector) { $toolConsumer = new ToolConsumer(null, $dataConnector); $toolConsumer->initialize(); $toolConsumer->setRecordId($id); if (!$dataConnector->loadToolConsumer($toolConsumer)) { $toolConsumer->initialize(); } return $toolConsumer; }