pdo_options) && is_array($CFG->pdo_options)) { $PDOX = new \Tsugi\Util\PDOX($CFG->pdo, $CFG->dbuser, $CFG->dbpass, $CFG->pdo_options); } else { $PDOX = new \Tsugi\Util\PDOX($CFG->pdo, $CFG->dbuser, $CFG->dbpass); } $PDOX->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } else { try { $PDOX = new \Tsugi\Util\PDOX($CFG->pdo, $CFG->dbuser, $CFG->dbpass); $PDOX->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } catch(\PDOException $ex){ error_log("DB connection: ".$ex->getMessage()); die('Failure connecting to the database, see error log'); // with error_log } } if ( isset($CFG->slow_query) ) $PDOX->slow_query = $CFG->slow_query; $PDOX->sqlPatch = function($PDOX, $sql) { return \Tsugi\Core\SQLDialect::sqlPatch($PDOX, $sql); } ; // Add meta entries describing primary and logical keys for LTI tables $p = $CFG->dbprefix; $PDOX->addPDOXMeta("{$p}lms_plugins", array("pk" => "plugin_id", "lk" => array("plugin_path"))); $PDOX->addPDOXMeta("{$p}lti_key", array("pk" => "key_id", "lk" => array("key_sha256"))); $PDOX->addPDOXMeta("{$p}lti_context", array("pk" => "context_id", "lk" => array("context_sha256", "key_id"))); $PDOX->addPDOXMeta("{$p}context_images", array("pk" => "context_id")); $PDOX->addPDOXMeta("{$p}lti_link", array("pk" => "link_id", "lk" => array("link_sha256", "context_id"))); $PDOX->addPDOXMeta("{$p}lti_user", array("pk" => "user_id", "lk" => array("user_sha256", "subject_sha256", "key_id"))); $PDOX->addPDOXMeta("{$p}lti_membership", array("pk" => "membership_id", "lk" => array("user_id", "context_id"))); $PDOX->addPDOXMeta("{$p}lti_service", array("pk" => "service_id", "lk" => array("service_sha256", "key_id"))); $PDOX->addPDOXMeta("{$p}lti_result", array("pk" => "result_id", "lk" => array("user_id", "link_id"))); $PDOX->addPDOXMeta("{$p}lti_nonce", array("pk" => "none", "lk" => array("key_id", "nonce"))); $PDOX->addPDOXMeta("{$p}tsugi_string", array("pk" => "string_id", "lk" => array("domain", "string_sha256"))); $PDOX->addPDOXMeta("{$p}profile", array("pk" => "profile_id", "lk" => array("profile_sha256"))); $PDOX->addPDOXMeta("{$p}blob_blob", array("pk" => "blob_id", "lk" => array("blob_sha256"))); // Does not have a logical key $PDOX->addPDOXMeta("{$p}blob_file", array("pk" => "file_id")); return $PDOX; } /** * Silently check if this is a launch and if so, handle it and redirect * back to ourselves */ public static function launchCheck($needed=self::ALL, $request_data=false) { global $TSUGI_LAUNCH, $CFG; $needed = self::patchNeeded($needed); // Check if we are an LTI 1.1 or LTI 1.3 launch $LTI11 = false; $LTI13 = false; $detail = LTI13::isRequestDetail($request_data); if ( $detail === true ) { $LTI13 = true; } else { $lti11_request_data = $request_data; if ( $lti11_request_data === false ) $lti11_request_data = self::oauth_parameters(); $LTI11 = LTI::isRequestCheck($lti11_request_data); if ( is_string($LTI11) ) { self::abort_with_error_log($LTI11, $request_data); } } if ( $LTI11 === false && $LTI13 === false ) return $detail; $session_id = self::setupSession($needed, $request_data); if ( $session_id === false ) return false; // Redirect back to ourselves... $url = self::curPageUrl(); $location = U::addSession($url); session_write_close(); // To avoid any race conditions... if ( headers_sent() ) { echo('

Click to continue

'); } else { header('Location: '.$location); } exit(); } /** * Encrypt a secret to put into the session */ public static function encrypt_secret($secret) { global $CFG; if ( ! is_string($secret) ) return null; if ( ! is_string($CFG->cookiesecret) ) return null; if ( startsWith($secret,'AES::') ) return $secret; $encr = AesOpenSSL::encrypt($secret, $CFG->cookiesecret) ; return 'AES::'.$encr; } /** * Decrypt a secret from the session */ public static function decrypt_secret($secret) { global $CFG; if ( $secret === null || $secret === false ) return $secret; if ( ! startsWith($secret,'AES::') ) return $secret; if ( ! is_string($CFG->cookiesecret) ) return $secret; // Remove the AES:: prefix $secret = substr($secret, 5); // TODO: Switch to AesOpenSSL after time passes from March 1, 2022 // Parallel decryption to check for mismatches started December 15, 2025 $decr = AesOpenSSL::decrypt($secret, $CFG->cookiesecret) ; $decrlegacy = \Tsugi\Crypt\AesCtr::decrypt($secret, $CFG->cookiesecret, 256) ; if ( $decr !== $decrlegacy ) { error_log("Decryption mismatch: AesOpenSSL and AesCtr produced different results. secret: $secret"); } return $decrlegacy; } /** * Pull a keyed variable from the LTI data in the current session with default * * @deprecated Session access should be through the Launch Object */ public static function ltiParameter($varname, $default=false) { global $TSUGI_LAUNCH; if ( isset($TSUGI_LAUNCH) ) { return $TSUGI_LAUNCH->ltiParameter($varname, $default); } if ( ! isset($_SESSION) ) return $default; if ( ! isset($_SESSION[TSUGI_SESSION_LTI]) ) return $default; $lti = $_SESSION[TSUGI_SESSION_LTI]; if ( ! isset($lti[$varname]) ) return $default; return $lti[$varname]; } /** * Return the original $_POST array * * @deprecated Session access should be through the Launch Object */ public static function ltiRawPostArray() { global $TSUGI_LAUNCH; if ( isset($TSUGI_LAUNCH) ) { return $TSUGI_LAUNCH->ltiRawPostArray(); } if ( ! isset($_SESSION) ) return array(); if ( ! isset($_SESSION[TSUGI_SESSION_LTI_POST]) ) return array(); return($_SESSION[TSUGI_SESSION_LTI_POST]); } /** * Pull a keyed variable from the original LTI post data in the current session with default * * @deprecated Session access should be through the Launch Object */ public static function ltiRawParameter($varname, $default=false) { global $TSUGI_LAUNCH; if ( isset($TSUGI_LAUNCH) ) { return $TSUGI_LAUNCH->ltiRawParameter($varname, $default); } if ( ! isset($_SESSION) ) return $default; if ( ! isset($_SESSION[TSUGI_SESSION_LTI_POST]) ) return $default; $lti_post = $_SESSION[TSUGI_SESSION_LTI_POST]; if ( ! isset($lti_post[$varname]) ) return $default; return $lti_post[$varname]; } /** * Pull out a custom variable from the LTIX session. Do not * include the "custom_" prefix - this is automatic. * * @deprecated Session access should be through the Launch Object */ public static function ltiCustomGet($varname, $default=false) { return self::ltiRawParameter('custom_'.$varname, $default); } /** * The LTI parameter data * * This code is taken from OAuthRequest */ public static function oauth_parameters() { // Find request headers $request_headers = OAuthUtil::get_headers(); // Parse the query-string to find GET parameters if ( isset($_SERVER['QUERY_STRING']) ) { $parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']); } else { $parameters = array(); } // Add POST Parameters if they exist $parameters = array_merge($parameters, $_POST); // We have a Authorization-header with OAuth data. Parse the header // and add those overriding any duplicates from GET or POST if (isset($request_headers['Authorization']) && substr($request_headers['Authorization'], 0, 6) == "OAuth ") { $header_parameters = OAuthUtil::split_header( $request_headers['Authorization'] ); $parameters = array_merge($parameters, $header_parameters); } return $parameters; } // Put as much oomph into setting a cookie as we can public static function setCookieStrong($name, $value, $expires, $partitioned = true) { global $CFG; Net::setCookieStrong($name, $value, parse_url($CFG->wwwroot)['host'], $expires, $partitioned); } public static function getBrowserMark() { global $CFG, $TSUGI_BROWSER_MARK; $browser_mark = U::get($_COOKIE, self::BROWSER_MARK_COOKIE); if (is_string($browser_mark) && U::strlen($browser_mark) > 1 ) { // error_log('Got browser_mark '.$browser_mark."\n"); } else if ( is_string($TSUGI_BROWSER_MARK) && U::strlen($TSUGI_BROWSER_MARK) > 1 ) { $browser_mark = $TSUGI_BROWSER_MARK; } else { $browser_mark = uniqid(); $expires = time() + 1*30*24*3600; self::setCookieStrong( self::BROWSER_MARK_COOKIE, $browser_mark, $expires ); // error_log('Set browser_mark '.$browser_mark."\n"); } // Only check once $TSUGI_BROWSER_MARK = $browser_mark; return $browser_mark; } /** * Bind an ID token nonce to the authenticated state from oidc_login. * Returns true on success or an error string. This does not consume state: * postMessage/postverify may revisit the callback before it forwards to a tool. * JWT signature validation and the existing nonce replay check still apply. */ public static function checkOidcNonce($state, $nonce) { global $CFG; if ( ! is_string($state) || $state === '' ) { return 'Missing OIDC state'; } try { $decoded = JWT::decode($state, new \Firebase\JWT\Key($CFG->cookiesecret, 'HS256')); } catch (\Exception $e) { return 'Unable to decode OIDC state'; } if ( ! isset($decoded->time) || ! is_int($decoded->time) || abs($decoded->time - time()) > 60 ) { return 'Bad time in OIDC state'; } if ( ! isset($decoded->signature) || $decoded->signature !== self::getBrowserSignature() ) { return 'Invalid browser signature in OIDC state'; } if ( ! isset($decoded->nonce) || ! is_string($decoded->nonce) || $decoded->nonce === '' || ! is_string($nonce) || $nonce === '' || ! hash_equals($decoded->nonce, $nonce) ) { return 'Missing or mismatched OIDC nonce'; } return true; } /** * Extract all of the post data, set up data in tables, and set up session. */ public static function setupSession($needed=self::ALL, $request_data=false) { global $CFG, $TSUGI_LAUNCH; global $PDOX; $needed = self::patchNeeded($needed); // Check if we are an LTI 1.1 or LTI 1.3 launch $LTI11 = false; $LTI13 = false; $lti13_request_data = $request_data; if ( $lti13_request_data === false ) $lti11_request_data = $_POST; $detail = LTI13::isRequestDetail($request_data); if ( $detail === true ) { $LTI13 = true; $request_data = $lti13_request_data; } else { $lti11_request_data = $request_data; if ( $lti11_request_data === false ) $lti11_request_data = self::oauth_parameters(); $LTI11 = LTI::isRequestCheck($lti11_request_data); if ( is_string($LTI11) ) { self::abort_with_error_log($LTI11, $request_data); } $request_data = $lti11_request_data; } if ( $LTI11 === false && $LTI13 === false ) return $detail; // Pull LTI data out of the incoming $request_data and map into the same // keys that we use in our database (i.e. like $row) $post = false; if ( $LTI11 ) { $post = self::extractPost($needed, $request_data); } else if ( $LTI13 ) { $post = self::extractJWT($needed, $request_data); // echo("
\n");var_dump($post);echo("\n
\n"); } if ( ! is_array($post) ) { $msg = ''; if ( is_string($post) ) $msg = $post . ' '; self::abort_with_error_log('LTI Launch Error: '.$msg, $request_data); } $identity_message = LTI::ascii255First(array( 'OAuth consumer key' => U::get($post, 'key'), 'Resource link id' => U::get($post, 'link_id'), 'Context id' => U::get($post, 'context_id'), 'Deployment id' => U::get($post, 'deployment_id'), 'Issuer' => U::get($post, 'issuer_key'), 'Client id' => U::get($post, 'issuer_client'), )); if ( is_string($identity_message) ) { self::abort_with_error_log('This launch was not accepted. '.$identity_message); } // Tools also accept direct posts. Enforce nonce binding here before creating // a launch session, so bypassing oidc_launch cannot bypass its nonce check. if ( $LTI13 ) { $state = U::get($request_data === false ? $_POST : $request_data, 'state'); $nonce_check = self::checkOidcNonce($state, U::get($post, 'nonce')); if ( $nonce_check !== true ) { self::abort_with_error_log($nonce_check); } } // We make up a Session ID Key because we don't want a new one // each time the same user launches the same link. if ( !defined('COOKIE_SESSION') ) { $session_id = self::getCompositeKey($post, $CFG->sessionsalt); session_id($session_id); session_start(); } else if (U::isEmpty($_SESSION ?? [])) { session_start(); } $session_id = session_id(); // Since we might reuse session IDs, clean everything out except permanent stuff if ( !defined('COOKIE_SESSION') ) { $save_sess = array_merge([], $_SESSION ?? []); $_SESSION = []; foreach($save_sess as $key => $v ) { if ( strpos($key, 'tsugi_permanent_') !== 0 ) continue; $_SESSION[$key] = $v; } } $_SESSION['LAST_ACTIVITY'] = time(); // Copy the tsugi_nav into the session $sess_key = 'tsugi_top_nav_'.$CFG->wwwroot; if ( isset($request_data['ext_tsugi_top_nav']) ) { $_SESSION[$sess_key] = $request_data['ext_tsugi_top_nav']; } else { unset($_SESSION[$sess_key]); } // Read all of the data from the database with a very long // LEFT JOIN and get all the data we have back in the $row variable // $row = loadAllData($CFG->dbprefix, false, $post); $row = self::loadAllData($CFG->dbprefix, $CFG->dbprefix.'profile', $post); if ( ! $row || ! U::get($row, 'key_id') ) { if ( U::get($post,'key') ) { // LTI 1.1 self::abort_with_error_log('Launch could not find key: '.htmlentities(U::get($post,'key'))); } else { $suffix = ''; if ( U::isNotEmpty(U::get($post, 'issuer_client')) ) { $suffix .= ' client_id='.htmlentities(U::get($post, 'issuer_client')); } if ( U::isNotEmpty(U::get($post, 'deployment_id')) ) { $suffix .= ' deployment_id='.htmlentities(U::get($post, 'deployment_id')); } else { $suffix .= ' deployment_id=(missing from JWT)'; } self::abort_with_error_log('Launch could not find issuer: '.htmlentities(U::get($post,'issuer_key')).$suffix); } } // Copy the deployment_id into run=time data for later if ( $LTI13 && U::get($post,'deployment_id') ) { $row['deployment_id'] = $post['deployment_id']; } $delta = 0; if ( isset($request_data['oauth_timestamp']) ) { $server_time = $request_data['oauth_timestamp']+0; $delta = abs(time() - $server_time); if ( $delta > 480 ) { // More than four minutes is getting close error_log('Warning: Time skew, delta='.$delta.' sever_time='.$server_time.' our_time='.time()); } } // Check the nonce to make sure there is no reuse if ( $row['nonce'] !== null) { self::abort_with_error_log('OAuth nonce error key='.$post['key'].' nonce='.$row['nonce']); } // Use returned data to check the validity of the incoming request $raw_jwt = false; $jwt = false; if ( $LTI11 ) { $valid = LTI::verifyKeyAndSecret($post['key'],$row['secret'],self::curPageUrl(), $request_data); // If there is a new_secret it means an LTI2 re-registration is in progress and we // need to check both the current and new secret until the re-registration is committed if ( $valid !== true && U::isNotEmpty($row['new_secret']) && $row['new_secret'] != $row['secret']) { $valid = LTI::verifyKeyAndSecret($post['key'],$row['new_secret'],self::curPageUrl(), $request_data); if ( $valid ) { $row['secret'] = $row['new_secret']; } $row['new_secret'] = null; } if ( isset($TSUGI_LAUNCH) ) $TSUGI_LAUNCH->base_string = LTI::getLastOAuthBodyBaseString(); // TODO: Might want to add more flows here... if ( $valid !== true ) { if ( isset($TSUGI_LAUNCH) ) $TSUGI_LAUNCH->error_message = $valid; self::abort_with_error_log('OAuth validation fail key='.$post['key'].' delta='.$delta.' error='.$valid[0],$valid[1]); } } else { // LTI 1.3 $key_id = $row['key_id']; $issuer_key = $post['issuer_key']; $issuer_client = $post['issuer_client']; $deployment_id = $post['deployment_id']; if ( $key_id < 1 ) { $suffix = U::isNotEmpty($deployment_id) ? ' launch_deployment_id='.htmlentities($deployment_id) : ''; self::abort_with_error_log("Could not find tenant/key for $issuer_key / clientid=$issuer_client".$suffix); } $raw_jwt = LTI13::raw_jwt($request_data); $jwt = LTI13::parse_jwt($raw_jwt); $request_kid = isset($jwt->header->kid) ? $jwt->header->kid : null; $our_kid = $row['lti13_kid']; $our_keyset = $row['lti13_keyset']; $our_keyset_url = $row['lti13_keyset_url']; $public_key = $row['lti13_platform_pubkey']; $token_url = $row['lti13_token_url']; $public_key = self::getPlatformPublicKey($key_id, $request_kid, $our_kid, $public_key, $our_keyset_url, $our_keyset); $e = LTI13::verifyPublicKey($raw_jwt, $public_key, array($jwt->header->alg)); if ( $e !== true ) { self::abort_with_error_log('JWT validation fail key='.$issuer_key.' error='.$e->getMessage()); } // Check validity of LTI 1.1 transition data if it exists, if validation fails, // just ignore it - don't fail. Some LMS's seem to drop in an LTI 1.1 transition claim // with not real data "just in case". It it verifies, we are cool, if not ignore it. $lti11_transition_user_id = U::get($post, 'lti11_transition_user_id'); if ( U::isNotEmpty($lti11_transition_user_id) ) { $lti11_oauth_consumer_key = $row['key_key']; // From the join $lti11_oauth_consumer_secret = self::decrypt_secret($row['secret']); $check = LTI13::checkLTI11Transition($jwt->body, $lti11_oauth_consumer_key, $lti11_oauth_consumer_secret); if ( is_string($check) ) { error_log('LTI 1.1 Transition error: '.$check); unset($post['lti11_transition_user_id']); } else if ( ! $check ) { error_log('LTI 1.1 Transition signature mis-match key='.$lti11_oauth_consumer_key); unset($post['lti11_transition_user_id']); } } $row['lti13_token_url'] = $token_url; // Just copy across if ( U::get($post,'lti13_deeplink') ) $row['lti13_deeplink'] = $post['lti13_deeplink']; $_SESSION['tsugi_jwt'] = $jwt; } // Store the launch path // TODO: Make sure we like this $post['link_path'] = self::curPageUrl(); $actions = self::adjustData($CFG->dbprefix, $row, $post, $needed); $PDOX = self::getConnection(); // Record the nonce but first probabilistically check if we will clean out if ( $CFG->noncecheck > 0 ) { if ( (time() % $CFG->noncecheck) == 0 ) { $PDOX->queryDie("DELETE FROM {$CFG->dbprefix}lti_nonce WHERE created_at < DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL -{$CFG->noncetime} SECOND)"); // error_log("Nonce table cleanup done."); } $PDOX->queryDie("INSERT IGNORE INTO {$CFG->dbprefix}lti_nonce (key_id, nonce) VALUES ( :key_id, :nonce)", array( ':nonce' => $post['nonce'], ':key_id' => $row['key_id']) ); } // If there is an appropriate role override variable, we use that role if ( isset($row['role_override']) && isset($row['role']) && $row['role_override'] > $row['role'] ) { $row['role'] = $row['role_override']; } if ( isset($row['for_user_role_override']) && isset($row['for_user_role']) && $row['for_user_role_override'] > $row['for_user_role'] ) { $row['for_user_role'] = $row['for_user_role_override']; } // Update the login_at data and do analytics if requested // There are a lot of queryReturnError() calls because we don't want to // fail on "nice to have" analytics data. $start_time = $_SESSION['tsugi_permanent_start_time'] ?? false; if ( isset($row['user_id']) && $start_time === false ) { self::noteLoggedIn($row); // Only learner launches are logged if ( $CFG->launchactivity && isset($row['link_id']) && $row['link_id'] && $row['role'] == 0 ) { $link_activity = isset($row['link_activity']) ? $row['link_activity'] : null; $link_count = isset($row['link_count']) ? $row['link_count'] : 0; if ( $link_activity == null || $link_count == 0 ) { $sql = "INSERT IGNORE INTO {$CFG->dbprefix}lti_link_activity (link_id, event, link_count, updated_at) VALUES (:link_id, 0, 0, NOW())"; $stmt = $PDOX->queryReturnError($sql, array( ':link_id' => $row['link_id'] )); if ( ! $stmt->success ) { error_log("Unable to create activity record link=".$row['link_id']); } } $ent = new \Tsugi\Event\Entry(); if ( $link_activity ) $ent->deSerialize($link_activity); $ent->total = $link_count; $ent->click(); $activity = $ent->serialize(self::MAX_ACTIVITY); $sql = "UPDATE {$CFG->dbprefix}lti_link_activity SET activity=:activity, updated_at=NOW(), link_count=link_count+1 WHERE link_id = :link_id AND event = 0"; $stmt = $PDOX->queryReturnError($sql, array( ':link_id' => $row['link_id'], ':activity' => $activity )); if ( ! $stmt->success ) { error_log("Unable to update activity record link=".$row['link_id']); } // Now user activity $link_activity = isset($row['link_user_activity']) ? $row['link_user_activity'] : null; $link_count = isset($row['link_user_count']) ? $row['link_user_count'] : 0; if ( isset($row['user_id']) && $row['user_id'] ) { if ( $link_activity == null || $link_count == 0 ) { $sql = "INSERT IGNORE INTO {$CFG->dbprefix}lti_link_user_activity (link_id, user_id, event, link_user_count, updated_at) VALUES (:link_id, :user_id, 0, 0, NOW())"; $stmt = $PDOX->queryReturnError($sql, array( ':link_id' => $row['link_id'], ':user_id' => $row['user_id'] )); if ( ! $stmt->success ) { error_log("Unable to create user activity record link=".$row['user_id']); } } $ent = new \Tsugi\Event\Entry(); if ( $link_activity ) $ent->deSerialize($link_activity); $ent->total = $link_count; $ent->click(); $activity = $ent->serialize(self::MAX_ACTIVITY); $sql = "UPDATE {$CFG->dbprefix}lti_link_user_activity SET activity=:activity, updated_at=NOW(), link_user_count=link_user_count+1 WHERE link_id = :link_id AND user_id = :user_id AND event = 0"; $stmt = $PDOX->queryReturnError($sql, array( ':link_id' => $row['link_id'], ':user_id' => $row['user_id'], ':activity' => $activity )); if ( ! $stmt->success ) { error_log("Unable to update user activity record link=".$row['user_id']); } } } } // Make sure we debounce really fast relaunches $_SESSION['tsugi_permanent_start_time'] = time(); // Encrypt the secret before placing in the session $row['secret'] = self::encrypt_secret($row['secret']); // Put the information into the row variable and put row into session $_SESSION[TSUGI_SESSION_LTI] = $row; $_SESSION[TSUGI_SESSION_LTI_POST] = $request_data; // Always set/clear so a file-based launch cannot keep a leftover sandbox manifest_id. \Tsugi\Core\Manifest::rememberInSession($row['manifest_id'] ?? 0); if ( isset($_SERVER['HTTP_USER_AGENT']) ) { $_SESSION['HTTP_USER_AGENT'] = $_SERVER['HTTP_USER_AGENT']; } $ipaddr = Net::getIP(); if ( is_string($ipaddr) ) { $_SESSION['REMOTE_ADDR'] = $ipaddr; // Check our list of IP address history // TODO: decrypt $iphistory = U::get($_COOKIE, "TSUGI-HISTORY",''); // Add this IP Address to the Tsugi IP History if it is not there if ( strpos($iphistory, $ipaddr) === false ) { $iphistory .= '!' . $ipaddr; // TODO: encrypt setcookie('TSUGI-HISTORY',$iphistory, 0, '/'); // Expire 100 seconds ago } } $browser_mark = self::getBrowserMark(); $_SESSION['BROWSER_MARK'] = $browser_mark; self::ensureCsrfToken(true); // Save this to make sure the user does not wander unless we launched from the root $scp = $CFG->getScriptPath(); if ( U::isNotEmpty($scp) ) { $_SESSION['script_path'] = $CFG->getScriptPath(); } $breadcrumb = 'Launch,'; $breadcrumb .= isset($row['key_id']) ? $row['key_id'] : ''; $breadcrumb .= ','; $breadcrumb .= isset($row['user_id']) ? $row['user_id'] : ''; $breadcrumb .= ','; $breadcrumb .= isset($request_data['user_id']) ? str_replace(',',';', $request_data['user_id']) : ''; $breadcrumb .= ','; $breadcrumb .= $session_id; $breadcrumb .= ','; $breadcrumb .= self::curPageUrl(); $breadcrumb .= ','; $breadcrumb .= $_SESSION['email'] ?? ' '; error_log($breadcrumb); return $session_id; } /** * getPlatformPublicKey - Get the platform public key for the various sources * * This will check the current kid, the current keyset, and if nothing matches * re-load the keyset url, and check the new keyset url. If new information is retrieved, it * is cached until the kid chages for this issuer. */ public static function getPlatformPublicKey($key_id, $request_kid, $our_kid, $public_key, $our_keyset_url, $our_keyset) { global $PDOX, $CFG; if ( U::isNotEmpty($public_key) && $request_kid == $our_kid ) return $public_key; error_log("getPlatformPublicKey key_id=$key_id request_kid=$request_kid stored_kid=$our_kid"); // Make sure we have or update to the latest keyset if we have a keyset_url // and the kid is new to us. Use Net::doGet so Canvas (and similar) get a // User-Agent — bare file_get_contents sends none and receives HTTP 403. if ( U::isNotEmpty($our_keyset_url) ) { try { $our_keyset = Net::doGet($our_keyset_url); } catch (\Exception $e) { self::abort_with_error_log("Failure loading keyset from ".$our_keyset_url. " detail:".$e->getMessage()); } $http_code = Net::getLastHttpResponse(); if ( $our_keyset === false || ! Net::httpSuccess($http_code) ) { self::abort_with_error_log("Failure loading keyset from ".$our_keyset_url. " detail:HTTP ".$http_code." ".substr((string)$our_keyset,0,200)); } $decoded = json_decode($our_keyset); if ( $decoded && isset($decoded->keys) && is_array($decoded->keys) ) { $PDOX->queryDie("UPDATE {$CFG->dbprefix}lti_key SET lms_cache_keyset=:KS, updated_at=NOW() WHERE key_id = :ID", array(':ID' => $key_id, ':KS' => self::keysetCacheForUrl($our_keyset_url, $our_keyset)) ); error_log("Updated lms_cache_keyset $key_id from $our_keyset_url\n"); } else { self::abort_with_error_log("Failure loading keyset from ".$our_keyset_url, substr($our_keyset,0,1000)); } } // If we have a keyset, lets look for the new key if ( U::isNotEmpty($our_keyset) ) { $new_public_key = LTI13::extractKeyFromKeySet($our_keyset, $request_kid); if ( $new_public_key ) { $PDOX->queryDie("UPDATE {$CFG->dbprefix}lti_key SET lms_cache_pubkey=:PK, lms_cache_kid=:KID, updated_at=NOW() WHERE key_id = :ID", array(':ID' => $key_id, ':PK' => $new_public_key, ':KID' => $request_kid ) ); error_log("New lms_cache_pubkey $key_id\n$new_public_key"); return $new_public_key; } } // Despite our best efforts, we could not get a key - clear things out to enable reset $PDOX->queryDie("UPDATE {$CFG->dbprefix}lti_key SET lms_cache_pubkey=NULL, updated_at=NOW() WHERE key_id = :ID", array(':ID' => $key_id) ); if ( U::isNotEmpty($public_key) ) { error_log("Cleared lms_cache_pubkey key $key_id invalid kid"); self::abort_with_error_log("Invalid Key Id (header.kid), public key cleared"); } else { error_log("Could not find lms_cache_pubkey $key_id invalid kid"); self::abort_with_error_log("Invalid Key Id (header.kid), could not find public key"); } } /** * Pull the LTI POST data into our own data structure * * We follow our naming conventions that match the column names in * our lti_ tables. */ public static function extractPost($needed=self::ALL, $input=false) { // Unescape each time we use this stuff - someday we won't need this... $needed = self::patchNeeded($needed); if ( $input === false ) $input = $_POST; $FIXED = array(); foreach($input as $key => $value ) { if ( strpos($key, "custom_") === 0 ) { $newkey = substr($key,7); // Need to deal with custom_context_id=$Context.id if ( strpos($value,"$") === 0 ) { $short_value = strtolower(substr($value,1)); $short_value = str_replace('.','_',$short_value); if ( $newkey == $short_value ) { continue; } } if ( !isset($FIXED[$newkey]) ) $FIXED[$newkey] = $value; } $FIXED[$key] = $value; } $retval = array(); $retval['key'] = isset($FIXED['oauth_consumer_key']) ? $FIXED['oauth_consumer_key'] : null; $retval['nonce'] = isset($FIXED['oauth_nonce']) ? $FIXED['oauth_nonce'] : null; $link_id = isset($FIXED['resource_link_id']) ? $FIXED['resource_link_id'] : null; $link_id = isset($FIXED['custom_resource_link_id']) ? $FIXED['custom_resource_link_id'] : $link_id; $retval['link_id'] = $link_id; $user_id = isset($FIXED['person_sourcedid']) ? $FIXED['person_sourcedid'] : null; $user_id = isset($FIXED['user_id']) ? $FIXED['user_id'] : $user_id; $user_id = isset($FIXED['custom_user_id']) ? $FIXED['custom_user_id'] : $user_id; $retval['user_id'] = $user_id; $context_id = isset($FIXED['courseoffering_sourcedid']) ? $FIXED['courseoffering_sourcedid'] : null; $context_id = isset($FIXED['context_id']) ? $FIXED['context_id'] : $context_id; $context_id = isset($FIXED['custom_context_id']) ? $FIXED['custom_context_id'] : $context_id; $retval['context_id'] = $context_id; // Sanity checks if ( ! $retval['key'] ) return "Missing oauth_consumer_key"; if ( ! $retval['nonce'] ) return "Missing nonce"; if ( in_array(self::USER, $needed) && ! $retval['user_id'] ) return "Missing required user_id"; if ( in_array(self::CONTEXT, $needed) && ! $retval['context_id'] ) return "Missing required context_id"; if ( in_array(self::LINK, $needed) && ! $retval['link_id'] ) return "Missing required resource_link_id"; // LTI 1.x settings and Outcomes $retval['service'] = isset($FIXED['lis_outcome_service_url']) ? $FIXED['lis_outcome_service_url'] : null; $retval['sourcedid'] = isset($FIXED['lis_result_sourcedid']) ? $FIXED['lis_result_sourcedid'] : null; // LTI 2.x settings and Outcomes $retval['result_url'] = isset($FIXED['custom_result_url']) ? $FIXED['custom_result_url'] : null; $retval['link_settings_url'] = isset($FIXED['custom_link_settings_url']) ? $FIXED['custom_link_settings_url'] : null; $retval['context_settings_url'] = isset($FIXED['custom_context_settings_url']) ? $FIXED['custom_context_settings_url'] : null; // LTI 1.x / 2.x Service endpoints $retval['ext_memberships_id'] = isset($FIXED['ext_memberships_id']) ? $FIXED['ext_memberships_id'] : null; $retval['ext_memberships_url'] = isset($FIXED['ext_memberships_url']) ? $FIXED['ext_memberships_url'] : null; $retval['lineitems_url'] = isset($FIXED['lineitems_url']) ? $FIXED['lineitems_url'] : null; $retval['memberships_url'] = isset($FIXED['memberships_url']) ? $FIXED['memberships_url'] : null; // Copy the settings into session $retval['link_settings'] = isset($FIXED['link_settings']) ? $FIXED['link_settings'] : null; $retval['context_settings'] = isset($FIXED['context_settings']) ? $FIXED['context_settings'] : null; $retval['key_settings'] = isset($FIXED['key_settings']) ? $FIXED['key_settings'] : null; // Context $retval['context_title'] = isset($FIXED['context_title']) ? $FIXED['context_title'] : null; $retval['link_title'] = isset($FIXED['resource_link_title']) ? $FIXED['resource_link_title'] : null; $retval['user_locale'] = isset($FIXED['launch_presentation_locale']) ? $FIXED['launch_presentation_locale'] : null; // Getting email from LTI 1.x and LTI 2.x $retval['user_email'] = isset($FIXED['lis_person_contact_email_primary']) ? $FIXED['lis_person_contact_email_primary'] : null; $retval['user_email'] = isset($FIXED['custom_person_email_primary']) ? $FIXED['custom_person_email_primary'] : $retval['user_email']; $retval['user_image'] = isset($FIXED['user_image']) ? $FIXED['user_image'] : null; // Displayname from LTI 2.x if ( isset($FIXED['person_name_full']) ) { $retval['user_displayname'] = $FIXED['custom_person_name_full']; } else if ( isset($FIXED['custom_person_name_given']) && isset($FIXED['custom_person_name_family']) ) { $retval['user_displayname'] = $FIXED['custom_person_name_given'].' '.$FIXED['custom_person_name_family']; } else if ( isset($FIXED['custom_person_name_given']) ) { $retval['user_displayname'] = $FIXED['custom_person_name_given']; } else if ( isset($FIXED['custom_person_name_family']) ) { $retval['user_displayname'] = $FIXED['custom_person_name_family']; // Displayname from LTI 1.x } else if ( isset($FIXED['lis_person_name_full']) ) { $retval['user_displayname'] = $FIXED['lis_person_name_full']; } else if ( isset($FIXED['lis_person_name_given']) && isset($FIXED['lis_person_name_family']) ) { $retval['user_displayname'] = $FIXED['lis_person_name_given'].' '.$FIXED['lis_person_name_family']; } else if ( isset($FIXED['lis_person_name_given']) ) { $retval['user_displayname'] = $FIXED['lis_person_name_given']; } else if ( isset($FIXED['lis_person_name_family']) ) { $retval['user_displayname'] = $FIXED['lis_person_name_family']; } // Trim out repeated spaces and/or weird whitespace from the user_displayname if ( isset($retval['user_displayname']) ) { $retval['user_displayname'] = trim(preg_replace('/\s+/', ' ',$retval['user_displayname'])); } // Get the role $retval['role'] = self::ROLE_LEARNER; $roles = ''; if ( isset($FIXED['custom_membership_role']) ) { // From LTI 2.x $roles = $FIXED['custom_membership_role']; } else if ( isset($FIXED['roles']) ) { // From LTI 1.x $roles = $FIXED['roles']; } if ( U::isNotEmpty($roles) ) { $roles = strtolower($roles); if ( ! ( strpos($roles,'instructor') === false ) ) $retval['role'] = self::ROLE_INSTRUCTOR; if ( ! ( strpos($roles,'administrator') === false ) ) $retval['role'] = self::ROLE_ADMINISTRATOR; // Local superuser would be 10000 } // Get the theme data $retval['theme_base'] = isset($FIXED['theme_base']) ? $FIXED['theme_base'] : null; $retval['theme_dark_mode'] = isset($FIXED['theme_dark_mode']) ? $FIXED['theme_dark_mode'] : null; return $retval; } /** * Pull the LTI JWT data into our own data structure * * We follow our naming conventions that match the column names in * our lti_ tables. */ public static function extractJWT($needed=self::ALL, $input=false) { // Unescape each time we use this stuff - someday we won't need this... $needed = self::patchNeeded($needed); if ( $input === false ) $input = $_POST; $raw_jwt = LTI13::raw_jwt($input); $jwt = LTI13::parse_jwt($raw_jwt); if ( is_string($jwt) ) return "Could not extract jwt: ".$jwt; if ( ! $jwt ) return "Could not extract jwt"; $body = $jwt->body; $issuer_sha256 = LTI13::extract_issuer_key($jwt); $issuer_key = $jwt->body->iss; error_log("issuer_key=$issuer_key\n"); error_log("issuer_sha256=$issuer_sha256\n
\n"); $retval = array(); $retval['issuer_key'] = $issuer_key; $retval['issuer_sha256'] = $issuer_sha256; $retval['issuer_client'] = LTI13::client_id_from_aud( isset($body->aud) ? $body->aud : null, isset($body->azp) ? $body->azp : null ); if ( isset($body->nonce) ) $retval['nonce'] = $body->nonce; // Don't set user_id for LTI 1.3 if ( isset($body->sub) ) { $retval['user_subject'] = $body->sub; } // Pull in the legacy information - signatures will be checked later if ( isset($body->{LTI13::LTI11_TRANSITION_CLAIM}) ) { $lti11_transition = $body->{LTI13::LTI11_TRANSITION_CLAIM}; if ( isset($lti11_transition->user_id) && isset($lti11_transition->oauth_consumer_key) && isset($lti11_transition->oauth_consumer_key_sign) ) { $retval['lti11_transition_user_id'] = $lti11_transition->user_id; $retval['lti11_transition_oauth_consumer_key'] = $lti11_transition->oauth_consumer_key; $retval['lti11_transition_oauth_consumer_key_sign'] = $lti11_transition->oauth_consumer_key_sign; if (isset($lti11_transition->resource_link_id)) { $retval['lti11_transition_resource_link_id'] = $lti11_transition->resource_link_id; } } } // Handle the for_user claim if ( isset($body->{LTI13::FOR_USER_CLAIM}) ) { $for_user_claim = $body->{LTI13::FOR_USER_CLAIM}; $retval['for_user_subject'] = isset($for_user_claim->user_id) ? $for_user_claim->user_id : null; $retval['for_user_email'] = isset($for_user_claim->email) ? $for_user_claim->email : null; $retval['for_user_image'] = isset($for_user_claim->picture) ? $for_user_claim->picture : null; $retval['for_user_locale'] = isset($for_user_claim->locale) ? $for_user_claim->locale : null; $retval['for_user_displayname'] = self::displayNameFromClaim($for_user_claim); } // The rest of the claims $resource_link_claim = LTI13::RESOURCE_LINK_CLAIM; $context_id_claim = LTI13::CONTEXT_ID_CLAIM; $deployment_id_claim = LTI13::DEPLOYMENT_ID_CLAIM; if ( isset($body->{$resource_link_claim}->id) ) $retval['link_id'] = $body->{$resource_link_claim}->id; if ( isset($body->{$context_id_claim}->id) ) $retval['context_id'] = $body->{$context_id_claim}->id; if ( isset($body->{$deployment_id_claim}) ) $retval['deployment_id'] = $body->{$deployment_id_claim}; // Sanity checks $failures = array(); // Do the Jon Postel check LTI13::jonPostel($body, $failures); if ( ! U::get($retval,'issuer_key') ) $failures[] = "Could not deterimine key issuer_from iss/aud"; if ( U::isEmpty(U::get($retval,'issuer_client')) ) $failures[] = "Could not determine client_id from aud"; if ( ! U::get($retval,'nonce') ) $failures[] = "Missing nonce"; if ( in_array(self::USER, $needed) && ! ( U::get($retval,'user_id') || U::get($retval,'user_subject')) ) $failures[] = "Missing subject/user_id (sub)"; if ( in_array(self::CONTEXT, $needed) && ! U::get($retval,'context_id') ) $failures[] = "Missing context_id"; if ( in_array(self::LINK, $needed) && ! U::get($retval,'link_id') ) $failures[] = "Missing resource_link->id"; $failmsg = ''; if ( count($failures) > 0 ) { foreach($failures as $failure) { if ( U::isNotEmpty($failmsg) ) $failmsg .= ", \n"; $failmsg .= $failure; } error_log("Could not find all required items in body (link_id, user_id, context_id)"); error_log($failmsg); error_log(json_encode($body)); return $failmsg; } // Context $retval['context_title'] = isset($body->{$context_id_claim}->title) ? $body->{$context_id_claim}->title : null; $retval['link_title'] = isset($body->{$resource_link_claim}->title) ? $body->{$resource_link_claim}->title : null; $retval['user_locale'] = isset($body->locale) ? $body->locale : null; $retval['user_email'] = isset($body->email) ? $body->email : null; $retval['user_image'] = isset($body->picture) ? $body->picture : null; $retval['user_displayname'] = self::displayNameFromClaim($body); // Get the line item $retval['lti13_lineitem'] = null; if ( isset($body->{LTI13::ENDPOINT_CLAIM}) && isset($body->{LTI13::ENDPOINT_CLAIM}->lineitem) && is_string($body->{LTI13::ENDPOINT_CLAIM}->lineitem) ) { $retval['lti13_lineitem'] = $body->{LTI13::ENDPOINT_CLAIM}->lineitem; } // Get the line item $retval['lti13_lineitems'] = null; if ( isset($body->{LTI13::ENDPOINT_CLAIM}) && isset($body->{LTI13::ENDPOINT_CLAIM}->lineitems) && is_string($body->{LTI13::ENDPOINT_CLAIM}->lineitems) ) { $retval['lti13_lineitems'] = $body->{LTI13::ENDPOINT_CLAIM}->lineitems; } // Get the names and roles claim $retval['lti13_membership_url'] = null; if ( isset($body->{LTI13::NAMESANDROLES_CLAIM}) && isset($body->{LTI13::NAMESANDROLES_CLAIM}->context_memberships_url) && is_string($body->{LTI13::NAMESANDROLES_CLAIM}->context_memberships_url) && isset($body->{LTI13::NAMESANDROLES_CLAIM}->service_versions) && is_array($body->{LTI13::NAMESANDROLES_CLAIM}->service_versions) && in_array("2.0", $body->{LTI13::NAMESANDROLES_CLAIM}->service_versions) ) { $retval['lti13_membership_url'] = $body->{LTI13::NAMESANDROLES_CLAIM}->context_memberships_url; } // Get data from the groups claim $retval['lti13_context_groups_url'] = null; if ( isset($body->{LTI13::GROUPS_CLAIM}) && isset($body->{LTI13::GROUPS_CLAIM}->context_groups_url) && is_string($body->{LTI13::GROUPS_CLAIM}->context_groups_url) && isset($body->{LTI13::GROUPS_CLAIM}->service_versions) && is_array($body->{LTI13::GROUPS_CLAIM}->service_versions) && in_array("1.0", $body->{LTI13::GROUPS_CLAIM}->service_versions) ) { $retval['lti13_context_groups_url'] = $body->{LTI13::GROUPS_CLAIM}->context_groups_url; } // Get the error url... $retval['launch_presentation_return_url'] = null; if ( isset($body->{LTI13::PRESENTATION_CLAIM}) && isset($body->{LTI13::PRESENTATION_CLAIM}->return_url) ) { $retval['launch_presentation_return_url'] = $body->{LTI13::PRESENTATION_CLAIM}->return_url; } // Get the role $retval['role'] = self::ROLE_LEARNER; if ( isset($body->{LTI13::ROLES_CLAIM}) && is_array($body->{LTI13::ROLES_CLAIM}) ) { $roles = implode(':',$body->{LTI13::ROLES_CLAIM}); if ( U::isNotEmpty($roles) ) { $roles = strtolower($roles); if ( ! ( strpos($roles,'instructor') === false ) ) $retval['role'] = self::ROLE_INSTRUCTOR; if ( ! ( strpos($roles,'administrator') === false ) ) $retval['role'] = self::ROLE_ADMINISTRATOR; // Local superuser would be 10000 } } // Handle the DeepLink Claim $retval['lti13_deeplink'] = null; if ( isset($body->{LTI13::DEEPLINK_CLAIM}) ) { $retval['lti13_deeplink'] = $body->{LTI13::DEEPLINK_CLAIM}; } // Get the theme data $retval['theme_base'] = isset($body->theme_base) ? $body->theme_base : null; $retval['theme_dark_mode'] = isset($body->theme_dark_mode) ? $body->theme_dark_mode : null; return $retval; } /** * encode and sign a JWT with a bunch of parameters */ public static function encode_jwt($params) { $success = Keyset::getSigning($privkey, $kid); // error_log('kid='. $kid.' priv='.$privkey); $jws = LTI13::encode_jwt($params, $privkey, $kid); return $jws; } // Make sure to include the file in case multiple instances are running // on the same Operating System instance and they have not changed the // session secret. Also make these change based on sessionlifetime public static function getCompositeKey($post, $session_secret) { global $CFG; $key = U::get($post, 'issuer_key', U::get($post, 'key')); $user_info = U::get($post,'user_id') . U::get($post,'user_subject'); $bucket = 1800; if ( isset($CFG->sessionlifetime) && is_numeric($CFG->sessionlifetime) ) { $bucket = (int) ($CFG->sessionlifetime * 0.5); } $bucket = max(300, min(86400, $bucket)); $time = intval(time() / $bucket); $comp = $session_secret .'::'. $key .'::'. $post['context_id'] .'::'. U::get($post,'link_id') .'::'. $user_info .'::'. $time . $_SERVER['HTTP_USER_AGENT'] . '::' . __FILE__; return md5($comp); } /** * Get a display name from a LTI 1.3 user claim */ public static function displayNameFromClaim($claim) { if ( ! is_object($claim) ) return ''; if ( isset($claim->name) ) { $retval = $claim->name; } else if ( isset($claim->given_name) && isset($claim->family_name) ) { $retval = $claim->given_name . ' ' . $claim->family_name; } else if ( isset($claim->given_name) ) { $retval = $claim->given_name; } else if ( isset($claim->family_name) ) { $retval = $claim->family_name; } else { $retval = ''; // TODO: IS THIS RIGHT? } // Trim out repeated spaces and/or weird whitespace from the user_displayname $retval= trim(preg_replace('/\s+/', ' ',$retval)); return $retval; } /** * Columns and LEFT JOIN that load a registration beside its key. * Both strings are empty when lti_registration does not exist. * $keyAlias is the lti_key alias already used in the query. * * @param string $prefix * @param string $keyAlias * @return array{select: string, join: string} */ public static function inboundRegistrationSql($prefix, $keyAlias) { // TODO: Remove this lti_registration existence gate by February 2027. if ( ! self::ltiRegistrationTableExists($prefix) ) { return array('select' => '', 'join' => ''); } return array( 'select' => 'reg.issuer AS reg_issuer, reg.client_id AS reg_client_id, ' .'reg.oidc_auth_url AS reg_oidc_auth_url, reg.keyset_url AS reg_keyset_url, ' .'reg.token_url AS reg_token_url, reg.token_audience AS reg_token_audience', 'join' => "LEFT JOIN {$prefix}lti_registration AS reg ON reg.lti_registration_id = {$keyAlias}.lti_registration_id", ); } /** * True when this install has the inbound registration table. * A missing table means the upgrade has not run, so launch keeps using lti_key. */ public static function ltiRegistrationTableExists($prefix) { // TODO: Remove this lti_registration existence gate by February 2027. static $seen = array(); if ( array_key_exists($prefix, $seen) ) return $seen[$prefix]; $PDOX = self::getConnection(); $seen[$prefix] = $PDOX->metadata($prefix.'lti_registration') !== false; return $seen[$prefix]; } /** * Use the registration's platform fields, or leave the key's fields untouched. * * Both sets are already on $row. This is not a per-field coalesce. * The registration wins only when issuer, client id, OIDC URL, keyset URL, * and token URL are all set. Audience is copied with that set and may be null. * Deployment id stays on the key. The cached key is kept when the key's * own keyset URL matches, or when the cache was fetched from the * registration URL. Any other cache is dropped from this row so the next * fetch uses the registration URL. The key's stored platform fields stay * in the database. * * @param array $row * @return array */ public static function applyInboundRegistration(array $row) { $required = array('reg_issuer', 'reg_client_id', 'reg_oidc_auth_url', 'reg_keyset_url', 'reg_token_url'); foreach ( $required as $field ) { if ( ! U::isNotEmpty(U::get($row, $field)) ) { self::clearInboundRegistrationColumns($row); return $row; } } $keyset = U::get($row, 'lms_keyset_url'); $reg_keyset = trim((string) $row['reg_keyset_url']); $keyset_same = is_string($keyset) && trim($keyset) === $reg_keyset; $cache_same = self::cachedKeysetUrl(U::get($row, 'lms_cache_keyset')) === $reg_keyset; if ( ! $keyset_same && ! $cache_same ) { $row['lms_cache_keyset'] = null; $row['lms_cache_pubkey'] = null; $row['lms_cache_kid'] = null; } $row['lms_issuer'] = $row['reg_issuer']; $row['lms_client'] = $row['reg_client_id']; $row['lms_oidc_auth'] = $row['reg_oidc_auth_url']; $row['lms_keyset_url'] = $row['reg_keyset_url']; $row['lms_token_url'] = $row['reg_token_url']; $row['lms_token_audience'] = array_key_exists('reg_token_audience', $row) ? $row['reg_token_audience'] : null; self::logRegistrationConfig($row); self::clearInboundRegistrationColumns($row); return $row; } /** * Temporary. Remove this log by February 2027. * * @param array $row */ private static function logRegistrationConfig(array $row) { global $CFG; // TODO: Remove this registration-source log by February 2027. if ( ! isset($CFG) || ! is_object($CFG) || $CFG->getExtension('log_registration_keys', true) !== true ) { return; } $key_id = U::get($row, 'key_id'); error_log('LTI config taken from registration' .' key_id='.(is_numeric($key_id) ? (int) $key_id : '') .' issuer='.$row['reg_issuer'] .' client_id='.$row['reg_client_id']); } /** * Remember which URL a cached keyset came from without changing the key's own URL. * * @param mixed $url * @param mixed $keyset * @return mixed */ private static function keysetCacheForUrl($url, $keyset) { if ( ! is_string($keyset) || ! is_string($url) || trim($url) === '' ) { return $keyset; } $decoded = json_decode($keyset, true); if ( ! is_array($decoded) || ! isset($decoded['keys']) || ! is_array($decoded['keys']) ) { return $keyset; } $decoded['tsugi_keyset_url'] = trim($url); $encoded = json_encode($decoded, JSON_UNESCAPED_SLASHES); return is_string($encoded) ? $encoded : $keyset; } /** * @param mixed $keyset */ private static function cachedKeysetUrl($keyset) { if ( ! is_string($keyset) || $keyset === '' ) return null; $decoded = json_decode($keyset, true); if ( ! is_array($decoded) || ! isset($decoded['tsugi_keyset_url']) || ! is_string($decoded['tsugi_keyset_url']) ) { return null; } $url = trim($decoded['tsugi_keyset_url']); return $url === '' ? null : $url; } /** * @param array $row */ private static function clearInboundRegistrationColumns(array &$row) { unset( $row['reg_issuer'], $row['reg_client_id'], $row['reg_oidc_auth_url'], $row['reg_keyset_url'], $row['reg_token_url'], $row['reg_token_audience'] ); } /** * Load the data from our lti_ tables using one long LEFT JOIN * * This data may or may not exist - hence the use of the long * LEFT JOIN. */ public static function loadAllData($p, $profile_table, $post) { global $CFG; $PDOX = self::getConnection(); $errormode = $PDOX->getAttribute(\PDO::ATTR_ERRMODE); $PDOX->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $issuer_key = U::get($post, "issuer_key", false); $LTI13 = $issuer_key !== false; $for_user_subject = U::get($post, "for_user_subject", false); if ( $LTI13 ) { $sql = "SELECT k.deploy_key, u.subject_key, "; } else { $sql = "SELECT "; } $registration_sql = self::inboundRegistrationSql($p, 'k'); // TODO: Remove this lti_registration existence gate by February 2027. $have_registration = $registration_sql['join'] !== ''; $sql .= "k.key_id, k.key_key, k.secret, k.new_secret, k.settings_url AS key_settings_url, k.login_at AS key_login_at, k.settings AS key_settings, k.lms_issuer, k.lms_client, k.lms_oidc_auth, k.lms_keyset_url, k.lms_token_url, k.lms_token_audience, k.lms_cache_keyset, k.lms_cache_pubkey, k.lms_cache_kid,"; if ( $have_registration ) { $sql .= " ".$registration_sql['select'].","; } $sql .= " n.nonce, c.context_id, c.title AS context_title, context_sha256, c.context_key as context_key, c.settings_url AS context_settings_url, c.ext_memberships_id AS ext_memberships_id, c.ext_memberships_url AS ext_memberships_url, c.lineitems_url AS lineitems_url, c.memberships_url AS memberships_url, c.lti13_lineitems AS lti13_lineitems, c.lti13_membership_url AS lti13_membership_url, c.lti13_context_groups_url AS lti13_context_groups_url, c.settings AS context_settings, c.manifest_id AS manifest_id, c.plus_course AS plus_course, c.plus_launch_link_id AS plus_launch_link_id, c.lti13_deployment_id AS lti13_deployment_id, c.timezone AS context_timezone, c.short_title AS context_short_title, k.timezone AS key_timezone, k.plus AS key_plus, k.guid AS key_guid, c.guid AS context_guid, l.link_id, l.path AS link_path, l.title AS link_title, l.settings AS link_settings, l.settings_url AS link_settings_url, l.lti13_lineitem AS lti13_lineitem, l.upward AS upward, l.settings AS link_settings, l.guid AS link_guid, u.user_id, u.displayname AS user_displayname, u.email AS user_email, u.user_key AS user_key, u.image AS user_image, u.locale AS user_locale, u.subscribe AS subscribe, u.user_sha256 AS user_sha256, u.guid AS user_guid, m.membership_id, m.role, m.role_override, m.deleted AS membership_deleted, r.result_id, r.grade, r.result_url, r.sourcedid"; if ( $for_user_subject ) { $sql .= ", f.user_id AS for_user_id, f.displayname AS for_user_displayname, f.email AS for_user_email, f.user_key as for_user_key, f.image AS for_user_image, f.locale AS for_user_locale, f.subscribe AS for_subscribe, f.user_sha256 AS for_user_sha256, f.guid AS for_user_guid, fm.membership_id AS for_membership_id, fm.role AS for_user_role, fm.role_override AS for_user_role_override"; } if ( $profile_table ) { $sql .= ", p.profile_id, p.displayname AS profile_displayname, p.email AS profile_email, p.subscribe AS profile_subscribe, p.premium AS profile_premium, p.premium_at AS profile_premium_at, p.premium_json AS profile_premium_json"; } if ( isset($post['service']) ) { $sql .= ", s.service_id, s.service_key AS service"; } if ( $CFG->launchactivity ) { $sql .= ", a.link_count, a.activity AS link_activity, au.link_user_count, au.activity AS link_user_activity"; } $sql .="\nFROM {$p}lti_key AS k"; if ( $have_registration ) { $sql .= "\n".$registration_sql['join']; } // TODO: Collapse a few weeks after 05-27-2019 if ( $LTI13 ) { $user_and = "(u.user_sha256 = :user OR u.subject_sha256 = :subject)"; } else { $user_and = "u.user_sha256 = :user"; } // Add the JOINs $sql .= "\n LEFT JOIN {$p}lti_nonce AS n ON k.key_id = n.key_id AND n.nonce = :nonce LEFT JOIN {$p}lti_context AS c ON k.key_id = c.key_id AND c.context_sha256 = :context LEFT JOIN {$p}lti_link AS l ON c.context_id = l.context_id AND l.link_sha256 = :link LEFT JOIN {$p}lti_user AS u ON k.key_id = u.key_id AND $user_and LEFT JOIN {$p}lti_membership AS m ON u.user_id = m.user_id AND c.context_id = m.context_id LEFT JOIN {$p}lti_result AS r ON u.user_id = r.user_id AND l.link_id = r.link_id"; if ( $profile_table ) { $sql .= " LEFT JOIN {$profile_table} AS p ON u.profile_id = p.profile_id"; } if ( $for_user_subject ) { $sql .= " LEFT JOIN {$p}lti_user AS f ON f.key_id = k.key_id AND f.subject_sha256 = :for_user_subject_sha256 LEFT JOIN {$p}lti_membership AS fm ON f.user_id = m.user_id AND c.context_id = m.context_id"; // TODO: Afterwards - don't accept the user if it is not in the same context... } if ( isset($post['service']) ) { $sql .= " LEFT JOIN {$p}lti_service AS s ON k.key_id = s.key_id AND s.service_sha256 = :service"; } if ( $CFG->launchactivity ) { $sql .= " LEFT JOIN {$p}lti_link_activity AS a ON a.link_id = l.link_id AND a.event = 0 LEFT JOIN {$p}lti_link_user_activity AS au ON au.link_id = l.link_id AND au.user_id = u.user_id AND au.event = 0"; } // Add the WHERE clause if ( $LTI13 ) { // TODO: Index lms_issuer_sha256 // Match by issuer + client_id only; accept any deployment_id from the LMS. // The launch JWT's deployment_id is still copied into $row at runtime for LTI 1.3 // services (e.g. AGS token requests). // A complete registration is found by its own issuer and client. // An incomplete one stays on the key identity, so it cannot answer // for a different platform. A complete registration's old key // identity is not eligible either. $sql .= "\nWHERE ( "; if ( $have_registration ) { $sql .= "( ( reg.issuer IS NULL OR reg.issuer = '' OR reg.client_id IS NULL OR reg.client_id = '' OR reg.oidc_auth_url IS NULL OR reg.oidc_auth_url = '' OR reg.keyset_url IS NULL OR reg.keyset_url = '' OR reg.token_url IS NULL OR reg.token_url = '' ) AND (k.lms_issuer_sha256 IS NULL OR k.lms_issuer_sha256 = :issuer_sha256) AND k.lms_client = :issuer_client ) OR ( reg.issuer IS NOT NULL AND reg.issuer <> '' AND reg.client_id IS NOT NULL AND reg.client_id <> '' AND reg.oidc_auth_url IS NOT NULL AND reg.oidc_auth_url <> '' AND reg.keyset_url IS NOT NULL AND reg.keyset_url <> '' AND reg.token_url IS NOT NULL AND reg.token_url <> '' AND SHA2(reg.issuer, 256) = :issuer_sha256 AND reg.client_id = :issuer_client )"; } else { $sql .= "(k.lms_issuer_sha256 IS NULL OR k.lms_issuer_sha256 = :issuer_sha256) AND k.lms_client = :issuer_client"; } $sql .= " ) AND (k.deleted IS NULL OR k.deleted = 0) "; } else { $sql .= "\nWHERE k.key_sha256 = :key AND (k.deleted IS NULL OR k.deleted = 0)"; } // TODO: Fix this per SO - but wait until the migrations have run in production // https://stackoverflow.com/questions/44474250/which-is-better-in-mysql-an-ifnull-or-or-logic/44474286 // A soft-deleted membership is still loaded, for a file course or a // manifest course. adjustData() clears deleted when this launch arrives. // Filtering m.deleted here drops the whole row and rejects the launch. // A permanent block is a separate feature, not this filter. $sql .= "\nAND (c.deleted IS NULL OR c.deleted = 0) AND (l.deleted IS NULL OR l.deleted = 0) AND (u.deleted IS NULL OR u.deleted = 0) AND (r.deleted IS NULL OR r.deleted = 0)"; if ( $profile_table ) { $sql .= " AND (p.deleted IS NULL OR p.deleted = 0)"; } if ( isset($post['service']) ) { $sql .= " AND (s.deleted IS NULL OR s.deleted = 0)"; } if ( $LTI13 ) { $sql .= " ORDER BY k.key_id ASC "; } // There should only be one :) $sql .= " LIMIT 1\n"; // ContentItem does not need link_id if ( ! isset($post['link_id']) ) $post['link_id'] = null; // Compute user identity bits based on user_subject, user_id and LTI 1.1 transition id if present $post_user_subject = U::get($post, 'user_subject'); $subject_sha256 = U::isNotEmpty($post_user_subject) ? lti_sha256($post_user_subject) : null; // Allow for for the legacy user id $user_check = U::get($post, "user_id"); if ( U::isEmpty($user_check) ) { $user_check = U::get($post, 'lti11_transition_user_id', null); } $user_sha256 = U::isNotEmpty($user_check) ? lti_sha256($user_check) : null; $parms = array( ':nonce' => substr($post['nonce'],0,128), ':context' => lti_sha256($post['context_id']), ':link' => lti_sha256($post['link_id']), ':user' => $user_sha256, ); // Pick the correct public key, etc if ( $LTI13 ) { $parms[':subject'] = $subject_sha256; $parms[':issuer_sha256'] = $post["issuer_sha256"]; $parms[':issuer_client'] = $post["issuer_client"]; if ( $for_user_subject ) $parms[":for_user_subject_sha256"] = lti_sha256($for_user_subject); } else { $parms[':key'] = lti_sha256($post['key']); } if ( isset($post['service']) ) { $parms[':service'] = lti_sha256($post['service']); } // echo("
\n"); $zapsql = $sql; foreach($parms as $k => $v ) { $zapsql = str_replace($k, "'".$v."'", $zapsql); } echo("\n$zapsql\n"); // Debug
        // die(); // Debug
        $row = $PDOX->rowDie($sql, $parms);
        // echo("
\n");var_dump($row);die();  // Debug
        if ( is_array($row) ) $row = self::applyInboundRegistration($row);

        if ( $LTI13 && is_array($row) ) {
            $row['issuer_key'] = $row['lms_issuer'];
            $row['issuer_client'] = $row['lms_client'];
            $row['lti13_kid'] = $row['lms_cache_kid'];
            $row['lti13_keyset_url'] = $row['lms_keyset_url'];
            $row['lti13_keyset'] = $row['lms_cache_keyset'];
            $row['lti13_platform_pubkey'] = $row['lms_cache_pubkey'];
            $row['lti13_token_url'] = $row['lms_token_url'];
            $row['lti13_token_audience'] = $row['lms_token_audience'];
        }

        // Restore ERRMODE
        $PDOX->setAttribute(\PDO::ATTR_ERRMODE, $errormode);

        return $row;
    }

    /**
     * Make sure that the data in our lti_ tables matches the POST data
     *
     * This routine compares the POST data to the data pulled from the
     * lti_ tables and goes through carefully INSERTing or UPDATING
     * all the nexessary data in the lti_ tables to make sure that
     * the lti_ table correctly match all the data from the incoming post.
     *
     * While this looks like a lot of INSERT and UPDATE statements,
     * the INSERT statements only run when we see a new user/course/link
     * for the first time and after that, we only update if something
     * changes.   So in a high percentage of launches we are not seeing
     * any new or updated data and so this code just falls through and
     * does absolutely no SQL.
     */
    public static function adjustData($p, &$row, $post, $needed) {

        $PDOX = self::getConnection();

        $errormode = $PDOX->getAttribute(\PDO::ATTR_ERRMODE);
        $PDOX->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

        $actions = array();

        // if we didn't get context_id from post, we can't update lti_context!

        // Since we can't do all this in a transaction, we need to handle duplicate keys
        // https://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html
        if ( $row['context_id'] === null && isset($post['context_id']) ) {
            $sql = "INSERT INTO {$p}lti_context
                ( guid, context_key, context_sha256, settings_url, title, key_id, created_at, updated_at ) VALUES
                ( UNHEX(:guid), :context_key, :context_sha256, :settings_url, :title, :key_id, NOW(), NOW() )
                ON DUPLICATE KEY UPDATE updated_at = NOW();";
            $context_settings_url = U::get($post, 'context_settings_url', null);
            $PDOX->queryDie($sql, array(
                ':guid' => Guid::uuid7(),
                ':context_key' => $post['context_id'],
                ':context_sha256' => lti_sha256($post['context_id']),
                ':settings_url' => $context_settings_url,
                ':title' => $post['context_title'],
                ':key_id' => $row['key_id']));
            $row['context_id'] = $PDOX->lastInsertId();
            $row['context_key'] = $post['context_id'];  // We rename this for all internal structures...
            $row['context_title'] = $post['context_title'];
            $row['context_settings_url'] = $context_settings_url;
            $actions[] = "=== Inserted context id=".$row['context_id']." ".$row['context_title'];
        }

        // if we didn't get context_id from post, we can't update lti_link either
        if ( $row['link_id'] === null && $row['context_id'] !== null && isset($post['link_id']) ) {
            $sql = "INSERT INTO {$p}lti_link
                ( guid, link_key, link_sha256, settings_url, title, context_id, path, created_at, updated_at ) VALUES
                    ( UNHEX(:guid), :link_key, :link_sha256, :settings_url, :title, :context_id, :path, NOW(), NOW() )
                    ON DUPLICATE KEY UPDATE updated_at = NOW();";
            $link_settings_url = U::get($post, 'link_settings_url', null);
            $PDOX->queryDie($sql, array(
                ':guid' => Guid::uuid7(),
                ':link_key' => $post['link_id'],
                ':link_sha256' => lti_sha256($post['link_id']),
                ':settings_url' => $link_settings_url,
                ':title' => $post['link_title'],
                ':context_id' => $row['context_id'],
                ':path' => $post['link_path']
            ));
            $row['link_id'] = $PDOX->lastInsertId();
            $row['link_title'] = $post['link_title'];
            $row['link_settings_url'] = $link_settings_url;
            $row['link_path'] = $post['link_path'];
            $actions[] = "=== Inserted link id=".$row['link_id']." ".$row['link_title'];
        }

        $user_displayname = isset($post['user_displayname']) ? $post['user_displayname'] : null;
        $user_email = isset($post['user_email']) ? $post['user_email'] : null;
        $user_image = isset($post['user_image']) ? $post['user_image'] : null;
        $user_locale = isset($post['user_locale']) ? $post['user_locale'] : null;
        $post_user_subject = isset($post['user_subject']) ? $post['user_subject'] : null;

        // $row['user_id'] is the primary key of the row
        // $post['user_id'] is the user_id from the launch
        // $post['user_subject'] is the user_subject from the launch
        // $row['subject_key'] is the subject from the row
        if ( $row['user_id'] === null && isset($post['user_id']) && U::isNotEmpty($post['user_id']) ) {
            $sql = "INSERT INTO {$p}lti_user
                /*PDOX pk: user_id lk: user_sha256,key_id */
                ( guid, user_key, user_sha256, displayname, email, image, locale, key_id, created_at, updated_at ) VALUES
                ( UNHEX(:guid), :user_key, :user_sha256, :displayname, :email, :image, :locale, :key_id, NOW(), NOW() )
                ON DUPLICATE KEY UPDATE updated_at = NOW();";
            $PDOX->queryDie($sql, array(
                ':guid' => Guid::uuid7(),
                ':user_key' => $post['user_id'],
                ':user_sha256' => lti_sha256($post['user_id']),
                ':displayname' => $user_displayname,
                ':email' => $user_email,
                ':image' => $user_image,
                ':locale' => $user_locale,
                ':key_id' => $row['key_id']));
            $row['user_id'] = $PDOX->lastInsertId();
            $row['user_email'] = $user_email;
            $row['user_displayname'] = $user_displayname;
            $row['user_image'] = $user_image;
            $row['user_locale'] = $user_locale;
            $row['user_key'] = $post['user_id'];

            $actions[] = "=== Inserted user id=".$row['user_id']." ".$row['user_email'];

            // TODO: Combine this into the previous query after user_subject migrations run - 28-May-2019
            if ( U::isNotEmpty($post_user_subject) ) {
                $sql = "UPDATE {$p}lti_user SET
                    subject_key = :subject_key,
                    subject_sha256 = :subject_sha256,
                    updated_at = NOW()
                    WHERE user_id = :UID";

                $stmt = $PDOX->queryReturnError($sql, array(
                    ':UID' => $row['user_id'],
                    ':subject_key' => $post_user_subject,
                    ':subject_sha256' => lti_sha256($post_user_subject),
                    )
                );

                if ( $stmt->success ) {
                    $row['subject_key'] = $post_user_subject;
                    $actions[] = "=== Added subject to new user id=".$row['user_id']." ".$row['user_email'];
                } else {
                    error_log("Unable to update user_subject - please upgrade database email=".$row['user_email']);
                }
            }
        }

        // An LTI 1.3 launch with a subject and no legacy user_id
        $lti11_transition_user_id = isset($post['lti11_transition_user_id']) ? $post['lti11_transition_user_id'] : null;
        $lti11_transition_user_id_sha256 = $lti11_transition_user_id === null ? null : lti_sha256($lti11_transition_user_id);
        if ( $row['user_id'] === null && U::isNotEmpty($post_user_subject) ) {
            $sql = "INSERT INTO {$p}lti_user
                /*PDOX pk: user_id lk: subject_sha256,user_sha256,key_id */
                ( guid, user_key, user_sha256, subject_key, subject_sha256, displayname, email, image, locale, key_id, created_at, updated_at ) VALUES
                ( UNHEX(:guid), :user_key, :user_sha256, :subject_key, :subject_sha256, :displayname, :email, :image, :locale, :key_id, NOW(), NOW() )
                ON DUPLICATE KEY UPDATE
                user_id=LAST_INSERT_ID(user_id), updated_at = NOW();";
            $PDOX->queryDie($sql, array(
                ':guid' => Guid::uuid7(),
                ':user_key' => $lti11_transition_user_id,
                ':user_sha256' => $lti11_transition_user_id_sha256,
                ':subject_key' => $post_user_subject,
                ':subject_sha256' => lti_sha256($post_user_subject),
                ':displayname' => $user_displayname,
                ':email' => $user_email,
                ':image' => $user_image,
                ':locale' => $user_locale,
                ':key_id' => $row['key_id']));
            $row['user_id'] = $PDOX->lastInsertId();
            $row['user_email'] = $user_email;
            $row['user_displayname'] = $user_displayname;
            $row['user_image'] = $user_image;
            $row['user_locale'] = $user_locale;
            $row['subject_key'] = $post_user_subject;

            $actions[] = "=== Inserted LTI1.3 user id=".$row['user_id']." ".$row['subject_key'];
        }

        // If we have a user subject and all of a we get a new transition id, keep it
        // Note that we will forever check for errors because of duplicate key possibilities
        if ( $row['user_id'] > 0 && U::isNotEmpty($post_user_subject) && U::isNotEmpty($lti11_transition_user_id) &&
            $row['user_key'] !=  $lti11_transition_user_id) {
            $sql = "UPDATE {$p}lti_user
                SET user_key = :user_key, user_sha256 = :user_sha256, updated_at = NOW()
                WHERE user_id = :uid";
            $stmt = $PDOX->queryReturnError($sql, array(
                ':uid' => $row['user_id'],
                ':user_key' => $lti11_transition_user_id,
                ':user_sha256' => $lti11_transition_user_id_sha256,
                )
            );

            if ( $stmt->success ) {
                $row['user_key'] = $lti11_transition_user_id;
                $actions[] = "=== Updated user_key for id=".$row['user_id']." subject=".$post_user_subject.
                " transition_user_uid=".$lti11_transition_user_id;
            } else {
                error_log("Unable to update user_key - transition problem subject=$post_user_subject user_id=".$row['user_id']);
            }
        }

        // If we already have a user_key and we just got a new post_user_subject
        // Always check and log errors in case the transition went badly and then was reconfigured
        $row_subject_key = U::get($row, 'subject_key');
        if ( $row['user_id'] > 0 && U::isNotEmpty($post_user_subject) && $post_user_subject != $row_subject_key ) {
            $sql = "UPDATE {$p}lti_user
                SET subject_key = :subject_key, subject_sha256 = :subject_sha256, updated_at = NOW()
                WHERE user_id = :uid";
            $stmt = $PDOX->queryReturnError($sql, array(
                ':uid' => $row['user_id'],
                ':subject_key' => $post_user_subject,
                ':subject_sha256' => lti_sha256($post_user_subject),
                )
            );
            if ( $stmt->success ) {
                $row['subject_key'] = $post_user_subject;
                $actions[] = "=== Updated subject_key for id=".$row['user_id']." post_user_subject=".$post_user_subject;
            } else {
                error_log("Unable to update subject_key - transition problems post_user_subject=".$post_user_subject." user_id=".$row['user_id']);
            }
        }

        if ( $row['membership_id'] === null && $row['context_id'] !== null && $row['user_id'] !== null ) {
            $sql = "INSERT INTO {$p}lti_membership
                ( context_id, user_id, role, created_at, updated_at ) VALUES
                ( :context_id, :user_id, :role, NOW(), NOW() )
                ON DUPLICATE KEY UPDATE
                membership_id = LAST_INSERT_ID(membership_id),
                deleted = 0, deleted_at = NULL, updated_at = NOW();";
            $PDOX->queryDie($sql, array(
                ':context_id' => $row['context_id'],
                ':user_id' => $row['user_id'],
                ':role' => $post['role']));
            $row['membership_id'] = $PDOX->lastInsertId();
            $row['role'] = $post['role'];
            $actions[] = "=== Inserted membership id=".$row['membership_id']." role=".$row['role'].
                " user=".$row['user_id']." context=".$row['context_id'];
        }

        if ( isset($post['service'])) {
            // We need to handle the case where the service URL changes but we already have a sourcedid
            // This is for LTI 1.x only as service is not used for LTI 2.x
            $oldserviceid = $row['service_id'];
            if ( $row['service_id'] === null && $post['service'] ) {
                $sql = "INSERT INTO {$p}lti_service
                    ( service_key, service_sha256, key_id, created_at, updated_at ) VALUES
                    ( :service_key, :service_sha256, :key_id, NOW(), NOW() )
                    ON DUPLICATE KEY UPDATE
                    service_id=LAST_INSERT_ID(service_id), updated_at = NOW();";
                $PDOX->queryDie($sql, array(
                    ':service_key' => $post['service'],
                    ':service_sha256' => lti_sha256($post['service']),
                    ':key_id' => $row['key_id']));
                $row['service_id'] = $PDOX->lastInsertId();
                $row['service'] = $post['service'];
                $actions[] = "=== Inserted service id=".$row['service_id']." ".$post['service'];
            }

            // If we just created a new service entry but we already had a result entry, update it
            // This is for LTI 1.x only as service is not used for LTI 2.x
            if ( $oldserviceid === null && $row['result_id'] !== null && $row['service_id'] !== null && $post['service'] ) {
                $sql = "UPDATE {$p}lti_result SET service_id = :service_id, updated_at = NOW() WHERE result_id = :result_id";
                $PDOX->queryDie($sql, array(
                    ':service_id' => $row['service_id'],
                    ':result_id' => $row['result_id']));
                $actions[] = "=== Updated result id=".$row['result_id']." service=".$row['service_id'];
            }
        }

        // We always insert a result row if we have a link - we will store
        // grades locally in this row - even if we cannot send grades
        if ( $row['result_id'] === null && $row['link_id'] !== null && $row['user_id'] !== null ) {
            $sql = "INSERT INTO {$p}lti_result
                ( link_id, user_id, created_at, updated_at ) VALUES
                ( :link_id, :user_id, NOW(), NOW() )
                ON DUPLICATE KEY UPDATE updated_at = NOW();";
            $PDOX->queryDie($sql, array(
                ':link_id' => $row['link_id'],
                ':user_id' => $row['user_id']));
            $row['result_id'] = $PDOX->lastInsertId();
            $actions[] = "=== Inserted result id=".$row['result_id'];
            GradeSync::ensureLineItem((int) $row['link_id']);
       }

        // Set these values to null if they were not in the post
        if ( ! isset($post['sourcedid']) ) $post['sourcedid'] = null;
        if ( ! isset($post['service']) ) $post['service'] = null;
        if ( ! isset($post['result_url']) ) $post['result_url'] = null;
        if ( ! isset($post['lti13_lineitem']) ) $post['lti13_lineitem'] = null;
        if ( ! isset($post['lti13_membership_url']) ) $post['lti13_membership_url'] = null;
        if ( ! isset($post['lti13_context_groups_url']) ) $post['lti13_context_groups_url'] = null;
        if ( ! isset($post['lti13_lineitems']) ) $post['lti13_lineitems'] = null;
        if ( ! isset($row['service']) ) {
            $row['service'] = null;
            $row['service_id'] = null;
        }

        // Here we handle updates to sourcedid or result_url including if we
        // just inserted the result row
        if ( $row['result_id'] != null &&
            ($post['sourcedid'] != $row['sourcedid'] || $post['result_url'] != $row['result_url'] ||
            $post['service'] != $row['service'] )
        ) {
            $sql = "UPDATE {$p}lti_result
                SET sourcedid = :sourcedid, result_url = :result_url, service_id = :service_id, updated_at = NOW()
                WHERE result_id = :result_id";
            $PDOX->queryDie($sql, array(
                ':result_url' => $post['result_url'],
                ':sourcedid' => $post['sourcedid'],
                ':service_id' => $row['service_id'],
                ':result_id' => $row['result_id']));
            $row['sourcedid'] = $post['sourcedid'];
            $row['service'] = $post['service'];
            $row['result_url'] = $post['result_url'];
            $actions[] = "=== Updated result id=".$row['result_id']." result_url=".$row['result_url'].
                " sourcedid=".$row['sourcedid']." service_id=".$row['service_id'];
            $sourcedid = $row['sourcedid'] === null ? '' : (string) $row['sourcedid'];
            if ( (int) ($row['upward'] ?? 0) === 0
                && (int) $row['link_id'] > 0 && (int) $row['service_id'] > 0 && $sourcedid !== '' ) {
                $sql = "UPDATE {$p}lti_link
                    SET upward = 1, updated_at = NOW()
                    WHERE link_id = :link_id AND upward = 0";
                $PDOX->queryDie($sql, array(':link_id' => $row['link_id']));
                $row['upward'] = 1;
                $actions[] = "=== Marked link id=".$row['link_id']." upward";
            }
        }

        // Here we handle lti13_lineitem
        if ( isset($row['link_id']) && isset($post['lti13_lineitem']) &&
            array_key_exists('lti13_lineitem',$row) && $post['lti13_lineitem'] != $row['lti13_lineitem'] ) {
            $sql = "UPDATE {$p}lti_link
                SET lti13_lineitem = :lti13_lineitem, updated_at = NOW()
                WHERE link_id = :link_id";
            $PDOX->queryDie($sql, array(
                ':lti13_lineitem' => $post['lti13_lineitem'],
                ':link_id' => $row['link_id']));
            $row['lti13_lineitem'] = $post['lti13_lineitem'];
            $actions[] = "=== Updated result id=".$row['result_id']." lti13_lineitem=".$row['lti13_lineitem'];
        }

        // A Plus course keeps course service URLs unless this request is /plus.
        // Other launches still overwrite them.
        $plusRefresh = PlusShell::mayRefreshCourse($row['plus_course'] ?? 0);

        // Here we handle lti13_membership_url
        if ( $plusRefresh && isset($row['context_id']) && isset($post['lti13_membership_url']) &&
            array_key_exists('lti13_membership_url',$row) && $post['lti13_membership_url'] != $row['lti13_membership_url'] ) {
            $sql = "UPDATE {$p}lti_context
                SET lti13_membership_url = :lti13_membership_url, updated_at = NOW()
                WHERE context_id = :context_id";
            $PDOX->queryDie($sql, array(
                ':lti13_membership_url' => $post['lti13_membership_url'],
                ':context_id' => $row['context_id']));
            $row['lti13_membership_url'] = $post['lti13_membership_url'];
            $actions[] = "=== Updated result id=".$row['result_id']." lti13_membership_url=".$row['lti13_membership_url'];
        }

        // Here we handle lti13_context_groups_url
        if ( $plusRefresh && isset($row['context_id']) && isset($post['lti13_context_groups_url']) &&
            array_key_exists('lti13_context_groups_url',$row) && $post['lti13_context_groups_url'] != $row['lti13_context_groups_url'] ) {
            $sql = "UPDATE {$p}lti_context
                SET lti13_context_groups_url = :lti13_context_groups_url, updated_at = NOW()
                WHERE context_id = :context_id";
            $PDOX->queryDie($sql, array(
                ':lti13_context_groups_url' => $post['lti13_context_groups_url'],
                ':context_id' => $row['context_id']));
            $row['lti13_context_groups_url'] = $post['lti13_context_groups_url'];
            $actions[] = "=== Updated result id=".$row['result_id']." lti13_context_groups_url=".$row['lti13_context_groups_url'];
        }


        // Here we handle lti13_lineitems
        if ( $plusRefresh && isset($row['context_id']) && isset($post['lti13_lineitems']) &&
            array_key_exists('lti13_lineitems',$row) && $post['lti13_lineitems'] != $row['lti13_lineitems'] ) {
            $sql = "UPDATE {$p}lti_context
                SET lti13_lineitems = :lti13_lineitems, updated_at = NOW()
                WHERE context_id = :context_id";
            $PDOX->queryDie($sql, array(
                ':lti13_lineitems' => $post['lti13_lineitems'],
                ':context_id' => $row['context_id']));
            $row['lti13_lineitems'] = $post['lti13_lineitems'];
            $actions[] = "=== Updated result id=".$row['result_id']." lti13_lineitems=".$row['lti13_lineitems'];
        }

        if ( $plusRefresh && (int) ($row['plus_course'] ?? 0) === 1 && isset($row['context_id']) ) {
            $deployment = U::get($post, 'deployment_id');
            if ( is_string($deployment) && $deployment !== '' && $deployment !== ($row['lti13_deployment_id'] ?? null) ) {
                $sql = "UPDATE {$p}lti_context
                    SET lti13_deployment_id = :deployment, updated_at = NOW()
                    WHERE context_id = :context_id";
                $PDOX->queryDie($sql, array(
                    ':deployment' => $deployment,
                    ':context_id' => $row['context_id']));
                $row['lti13_deployment_id'] = $deployment;
                $actions[] = "=== Updated context=".$row['context_id']." lti13_deployment_id=".$deployment;
            }
        }

        // Here we handle updates to context_title, link_title, user_displayname, user_email, or role
        if ( isset($row['context_id']) && isset($post['context_title']) && $post['context_title'] != $row['context_title']
            && PlusShell::mayReplaceTitle($row['plus_course'] ?? 0, $row['context_title'] ?? '') ) {
            $sql = "UPDATE {$p}lti_context SET title = :title, updated_at = NOW() WHERE context_id = :context_id";
            $PDOX->queryDie($sql, array(
                ':title' => $post['context_title'],
                ':context_id' => $row['context_id']));
            $row['context_title'] = $post['context_title'];
            $actions[] = "=== Updated context=".$row['context_id']." title=".$post['context_title'];
        }

        // Grab the context scoped service URLs...
        $context_services = array('ext_memberships_id', 'ext_memberships_url', 'lineitems_url', 'memberships_url');
        if ( $plusRefresh && isset($row['context_id']) ) {
            foreach($context_services as $context_service ) {
                if ( isset($post[$context_service]) && $post[$context_service] != $row[$context_service] ) {
                    $sql = "UPDATE {$p}lti_context SET {$context_service} = :value, updated_at = NOW()
                        WHERE context_id = :context_id";
                    $PDOX->queryDie($sql, array(
                        ':value' => $post[$context_service],
                        ':context_id' => $row['context_id']));
                    $row[$context_service] = $post[$context_service];
                    $actions[] = "=== Updated context=".$row['context_id']." {$context_service}=".$post[$context_service];
                }
            }
        }

        if ( isset($row['link_id']) && isset($post['link_title']) && $post['link_title'] != $row['link_title'] ) {
            $sql = "UPDATE {$p}lti_link SET title = :title, updated_at = NOW() WHERE link_id = :link_id";
            $PDOX->queryDie($sql, array(
                ':title' => $post['link_title'],
                ':link_id' => $row['link_id']));
            $row['link_title'] = $post['link_title'];
            $actions[] = "=== Updated link=".$row['link_id']." title=".$post['link_title'];
        }

        if ( isset($row['link_id']) && isset($post['link_path']) && $post['link_path'] != $row['link_path'] ) {
            $sql = "UPDATE {$p}lti_link SET path = :path, updated_at = NOW() WHERE link_id = :link_id";
            $PDOX->queryDie($sql, array(
                ':path' => $post['link_path'],
                ':link_id' => $row['link_id']));
            $row['link_path'] = $post['link_path'];
            $actions[] = "=== Updated link=".$row['link_id']." path=".$post['link_path'];
        }

        // Grab the user scoped fields...
        $user_fields = array('displayname', 'email', 'image', 'locale');
        if ( isset($row['user_id']) ) {
            foreach($user_fields as $u_field ) {
                $user_field = 'user_'.$u_field;
                if ( isset($post[$user_field]) && $post[$user_field] != $row[$user_field] && U::isNotEmpty($post[$user_field]) ) {
                    $sql = "UPDATE {$p}lti_user SET {$u_field} = :value, updated_at = NOW() WHERE user_id = :user_id";
                    $PDOX->queryDie($sql, array(
                        ':value' => $post[$user_field],
                        ':user_id' => $row['user_id']));
                    $row[$user_field] = $post[$user_field];
                    $actions[] = "=== Updated user=".$row['user_id']." {$user_field}=".$post[$user_field];
                }
            }
        }

        if ( isset($row['membership_id']) && (int) ($row['membership_deleted'] ?? 0) === 1 ) {
            $sql = "UPDATE {$p}lti_membership
                SET deleted = 0, deleted_at = NULL, updated_at = NOW()
                WHERE membership_id = :membership_id";
            $PDOX->queryDie($sql, array(
                ':membership_id' => $row['membership_id']));
            $row['membership_deleted'] = 0;
            $actions[] = "=== Restored membership id=".$row['membership_id'];
        }

        if ( isset($row['membership_id']) && isset($post['role']) && $post['role'] != $row['role'] ) {
            $sql = "UPDATE {$p}lti_membership SET role = :role, updated_at = NOW() WHERE membership_id = :membership_id";
            $PDOX->queryDie($sql, array(
                ':role' => $post['role'],
                ':membership_id' => $row['membership_id']));
            $row['role'] = $post['role'];
            $actions[] = "=== Updated membership=".$row['membership_id']." role=".$post['role'];
        }

        // Restore ERRMODE
        $PDOX->setAttribute(\PDO::ATTR_ERRMODE, $errormode);
        return $actions;
    }

    /**
     * Patch the value for the list of needed features
     *
     * Note - causes no harm if called more than once.
     */
    private static function patchNeeded($needed) {
        if ( $needed == self::NONE ) $needed = array();
        if ( $needed == self::ALL ) {
            $needed = array(self::CONTEXT, self::LINK, self::USER);
        }
        if ( is_string($needed) ) $needed = array($needed);
        return $needed;
    }

    /**
     * Optionally handle launch and/or set up the LTI session and global variables
     *
     * This will set up as much of the $USER, $CONTEXT, $LINK,
     * and $RESULT data as it can including leaving them all null
     * if this is called on a request with no LTI launch and no LTI
     * data in the session.  This functions as and performs a
     * PHP session_start().
     *
     * @return Launch A Tsugi Launch object.
     */
    public static function session_start() {
        return self::requireDataPrivate(self::NONE);
    }

    /**
     * Handle launch and/or set up the LTI session and global variables
     *
     * Make sure we have the values we need in the LTI session
     * This routine will not start a session if none exists.  It will
     * die is there if no session_name() (PHPSESSID) cookie or
     * parameter.  No need to create any fresh sessions here.
     *
     * @param $needed (optional, mixed)  Indicates which of
     * the data structures are * needed. If this is omitted,
     * this assumes that CONTEXT, LINK, and USER data are required.
     * If LTIX::NONE is present, then none of the three are rquired.
     * If some combination of the three are needed, this accepts
     * an array of the LTIX::CONTEXT, LTIX: LINK, and LTIX::USER
     * can be passed in.
     *
     * @return Launch A Tsugi Launch object.
     */
    public static function requireData($needed=self::ALL) {
        return self::requireDataPrivate($needed);
    }

    /**
     * Handle the launch, but with the caller given the chance to override defaults
     *
     * @param $pdox array - aproperly initialized  PDO object.  Can be null.
     * @param $request_data array - This must merge the $_POST, $_GET,
     * and OAuth Header data (in that order - header data has highest priority).
     * See self::oauth_parameters() for the way this data is normally pulled
     * from the three sources and merged into a single array.  Can be null.
     */
    public static function requireDataOverride($needed,
        $pdox, $current_url, $request_data)
    {
        return self::requireDataPrivate($needed,
            $pdox, $current_url, $request_data);
    }

    /**
     * Internal method to handle the data setup
     */
    public static function requireDataPrivate($needed=self::ALL,
        $pdox=null, $current_url=null, $request_data=null)
    {
        global $CFG, $TSUGI_LAUNCH;
        global $OUTPUT, $USER, $CONTEXT, $LINK, $RESULT, $ROSTER;

        // Always mark the browser
        $browser_mark = self::getBrowserMark();

        if ( $request_data == null ) $request_data = self::oauth_parameters();

        $TSUGI_LAUNCH = new \Tsugi\Core\Launch();
        if ( isset($OUTPUT) && is_object($OUTPUT) && get_class($OUTPUT) == 'Tsugi\UI\Output' ) {
            $TSUGI_LAUNCH->output = $OUTPUT;
            $OUTPUT->launch = $TSUGI_LAUNCH;
        } else {
            $OUTPUT = new \Tsugi\UI\Output();
            $TSUGI_LAUNCH->output = $OUTPUT;
            $OUTPUT->launch = $TSUGI_LAUNCH;
        }

        $USER = null;
        $PROFILE = null;
        $CONTEXT = null;
        $LINK = null;
        $RESULT = null;
        $ROSTER = null;

        // Make sure to initialize the global connection object
        if ( $pdox === null ) {
            $PDOX = self::getConnection();
        } else {
            $PDOX = $pdox;
        }
        $TSUGI_LAUNCH->pdox = $PDOX;

        $needed = self::patchNeeded($needed);

        // Check to see if this is LTI Launch Authorization Flow
        if ( self::launchAuthorizationFlow($request_data) ) return;

        // Check if we are processing an LTI launch.  If so, handle it
        $newlaunch = self::launchCheck($needed, $request_data);
        $detail = $newlaunch;  // If we got a string back, save it
        $newlaunch = $newlaunch === true;   // A string is "false"

        // If launchCheck comes back with a true, it means someone above us
        // needs to do the redirect
        if ( $newlaunch ) {
            return $TSUGI_LAUNCH;
        }

        // Check to see if the session already exists.
        $sess = session_name();
        if ( ini_get('session.use_cookies') == '0' ) {
            if ( $newlaunch || isset($_POST[$sess]) || isset($_GET[$sess]) ) {
                $session_id = $_POST[$sess] ?? $_GET[$sess] ?? null;
                if ( $session_id && session_id() == "" ) {
                    session_id($session_id);
                    session_start();
                }
            } else if ( count($needed) > 0 ) {
                $_SESSION = [];
                self::send403();
                $msg = 'Session is missing, invalid or expired - please re-launch';
                if ( is_string($detail) ) $msg .= ". Detail: ".$detail;
                self::abort_with_error_log($msg,
                    U::get($_SERVER, 'HTTP_REFERER', Net::getIP())
                );
            }
        }

        if ( session_id() == "" ) {
            session_start();
        }

        if ( count($needed) > 0 && ($_SESSION[TSUGI_SESSION_LTI] ?? null) === null ) {
            $_SESSION = [];
            self::abort_with_error_log('Tool session missing or expired - please re-launch ',
                U::get($_SERVER, 'HTTP_REFERER', Net::getIP())
            );
        }

        $trusted = self::checkReferer() || self::checkCSRF();

        $session_agent = $_SESSION['HTTP_USER_AGENT'] ?? null;
        if ( count($needed) > 0 && (!$trusted) && $session_agent != null ) {
            if ( (!isset($_SERVER['HTTP_USER_AGENT'])) ||
                $_SERVER['HTTP_USER_AGENT'] != $session_agent ) {
                $_SESSION = [];
                self::send403();
                self::abort_with_error_log("Session has expired", "HTTP_USER_AGENT ".
                    (($session_agent !== null ) ? $session_agent : 'Empty Session user agent') .
                    ' ::: '.
                    (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Empty browser user agent'),
                false,'DIE:');
            }
        }

        // Check our list of IP address history
        // TODO: decrypt
        $iphistory = U::get($_COOKIE, "TSUGI-HISTORY");

        // We only check the first three octets as some systems wander through the addresses on
        // class C - Perhaps it is even NAT - who knows - but we forgive those on the same Class C
        $session_addr = $_SESSION['REMOTE_ADDR'] ?? null;
        $ipaddr = Net::getIP();
        $browser_mark = self::getBrowserMark();
        $session_browser_mark = $_SESSION['BROWSER_MARK'] ?? null;
        if ( count($needed) > 0 && (!$trusted) &&  $session_addr && $ipaddr &&
            Net::isRoutable($session_addr) && Net::isRoutable($ipaddr) ) {
            $sess_pieces = explode('.',$session_addr);
            $serv_pieces = explode('.',$ipaddr);
            if ( count($sess_pieces) == 4 && count($serv_pieces) == 4 ) {
                if ( $sess_pieces[0] != $serv_pieces[0] || $sess_pieces[1] != $serv_pieces[1] ||
                    $sess_pieces[2] != $serv_pieces[2] ) {
                    if ( strpos($iphistory, $session_addr) !== false ) {
                        error_log("IP Address changed, session_addr=".  $session_addr.' current='.$ipaddr." but trusting iphistory=".$iphistory);
                        $_SESSION['REMOTE_ADDR'] = $ipaddr;
                        // Add new IP Address to the Tsugi IP History if it is not there
                        if ( strpos($iphistory, $ipaddr) === false ) {
                            $iphistory .= '!' . $ipaddr;
                            // TODO: encrypt
                            setcookie('TSUGI-HISTORY',$iphistory, 0, '/');
                        }

                    } else if ( $browser_mark == $session_browser_mark ) {
                        error_log("IP address change, trusting browser mark ".$browser_mark);
                        $_SESSION['REMOTE_ADDR'] = $ipaddr;
                    } else {
                        // Need to clear out session data
                        $_SESSION = [];
                        self::send403();
                        self::abort_with_error_log('Session address has expired', "session_addr=".
                            $session_addr.' current='.$ipaddr.' iphistory='.$iphistory, 'DIE:');
                    }
                }
            }
        }

        // Check to see if the user has navigated to a new place in the hierarchy
        $session_script = $_SESSION['script_path'] ?? null;
        if ( $session_script !== null &&
            (! endsWith(Output::getUtilUrl(''), $CFG->getScriptPath()) ) &&
            (! startsWith('api', $CFG->getScriptPath()) ) &&
            strpos($CFG->getScriptPath(), $session_script ) !== 0 ) {
            $_SESSION = [];
            self::send403();
            self::abort_with_error_log('Improper navigation detected', "script_path ".
                $session_script.' /  '.$CFG->getScriptPath(), 'DIE:');
        }

        // Check to see if the session needs to be extended due to this request
        self::checkHeartBeat();

        $_SESSION['HEARTBEAT_COUNT'] = 0;

        // Site URLs (buildmenu): leave leftover /courses/{id} sandbox, use Google-login course.
        // A non-Google, non-Plus site URL is not the stored launch course. Drop the
        // top-level course fields. A Plus session keeps its course. The launch row
        // stays for a later /courses/{id} match.
        if ( class_exists('\Tsugi\Controllers\Courses') ) {
            \Tsugi\Controllers\Courses::restoreSiteLoginContext();
            \Tsugi\Controllers\Courses::releaseUnmountedCourse();
        }

        $LTI = $_SESSION[TSUGI_SESSION_LTI] ?? null;
        if ( self::launchContextApplies(is_array($LTI) ? $LTI : null) ) {
            self::rejectDeletedContext($PDOX, $LTI);
        }
        if ( count($needed) == 0 && $LTI === null ) {
            self::provisionReqScope(null);
            return $TSUGI_LAUNCH;
        }

        if ( is_array($needed) ) {
            foreach ( $needed as $feature ) {
                if ( isset($LTI[$feature]) ) continue;
                self::abort_with_error_log("This tool requires an LTI launch parameter:".$feature);
            }
        }

        $launch = self::buildLaunch($LTI);
        self::provisionReqScope($LTI);
        return $launch;
    }

    /**
     * True when this request's course is the course stored on the launch row.
     *
     * A cookieless tool launch is that row. A cookie /courses/{id} request
     * matches when the URL id is the row's context_id. Any other cookie URL
     * matches only when $_SESSION['context_id'] is that same id. A request
     * course of 0 does not match.
     *
     * @param mixed $LTI
     */
    public static function launchContextApplies($LTI) {
        $ltiContext = is_array($LTI) ? (int) ($LTI['context_id'] ?? 0) : 0;
        if ( ! defined('COOKIE_SESSION') ) {
            return $ltiContext > 0;
        }
        $requestContext = 0;
        if ( class_exists('\Tsugi\Controllers\Courses')
                && \Tsugi\Controllers\Courses::isCourseMountedRequest() ) {
            $requestContext = \Tsugi\Controllers\Courses::courseIdFromRequest();
        } else {
            $requestContext = (int) ($_SESSION['context_id'] ?? 0);
        }
        if ( $ltiContext < 1 ) {
            return $requestContext > 0;
        }
        return $requestContext === $ltiContext;
    }

    /**
     * Fill ReqScope from the open session without changing launch globals.
     *
     * A cookieless tool session is read from the stored launch row, including
     * the link when that row has one. A cookie site session is read from
     * $_SESSION['id'] and $_SESSION['context_id']. The link is copied only when
     * the stored launch names that same course. A missing user or course is
     * stored as null.
     *
     * @param mixed $LTI
     * @return void
     */
    private static function provisionReqScope($LTI) {
        $origin = defined('COOKIE_SESSION') ? ReqScope::ORIGIN_SITE : ReqScope::ORIGIN_LTI;
        $user_id = 0;
        $context_id = 0;
        $link_id = null;
        $lti_user = 0;
        $lti_context = 0;
        $lti_link = 0;
        if ( is_array($LTI) ) {
            $lti_user = (int) ($LTI['user_id'] ?? 0);
            $lti_context = (int) ($LTI['context_id'] ?? 0);
            $lti_link = (int) ($LTI['link_id'] ?? 0);
        }
        if ( $origin === ReqScope::ORIGIN_LTI ) {
            $user_id = $lti_user;
            $context_id = $lti_context;
            if ( $lti_link > 0 ) {
                $link_id = $lti_link;
            }
        } else {
            if ( isset($_SESSION['id']) ) {
                $user_id = (int) $_SESSION['id'];
            }
            if ( $user_id < 1 && $lti_user > 0 ) {
                $user_id = $lti_user;
            }
            if ( self::launchContextApplies($LTI) ) {
                if ( class_exists('\Tsugi\Controllers\Courses')
                        && \Tsugi\Controllers\Courses::isCourseMountedRequest() ) {
                    $context_id = \Tsugi\Controllers\Courses::courseIdFromRequest();
                } else if ( isset($_SESSION['context_id']) ) {
                    $context_id = (int) $_SESSION['context_id'];
                }
                if ( $lti_link > 0 && $lti_context > 0 && $lti_context === $context_id ) {
                    $link_id = $lti_link;
                }
            }
        }
        $post = $_SESSION[TSUGI_SESSION_LTI_POST] ?? null;
        ReqScope::noteLtiLaunch(is_array($LTI) ? $LTI : null, is_array($post) ? $post : null);
        try {
            ReqScope::provision($user_id, $context_id, $link_id, $origin);
        } catch ( \Throwable $ex ) {
            error_log('ReqScope provision failed: '.$ex->getMessage());
        }
    }

    /**
     * A session launch for a soft-deleted course must not be rebuilt.
     *
     * Login, CSRF, and the stored LTI return URL stay. Only the course is dropped.
     *
     * @param object $PDOX
     * @param mixed $LTI
     * @return void
     */
    private static function rejectDeletedContext($PDOX, $LTI) {
        global $CFG;

        if ( ! is_array($LTI) ) {
            return;
        }
        $context_id = (int) ($LTI['context_id'] ?? 0);
        if ( $context_id < 1 || ! is_object($PDOX) ) {
            return;
        }
        $row = $PDOX->rowDie(
            "SELECT context_id FROM {$CFG->dbprefix}lti_context
             WHERE context_id = :CID AND (deleted IS NULL OR deleted = 0)",
            array(':CID' => $context_id)
        );
        if ( is_array($row) ) {
            return;
        }
        if ( class_exists('\Tsugi\Controllers\Courses') ) {
            \Tsugi\Controllers\Courses::dropActiveContext($context_id);
        }
        self::send403();
        self::abort_with_error_log('Course has been deleted');
    }

    /**
     * Keep a repaired guid on the launch row so the next request does not query again.
     *
     * The constructor returns hex. The launch row keeps the BINARY(16) form.
     *
     * @param string $alias
     * @param string|null $hex
     */
    private static function rememberBinaryGuid($alias, $hex) {
        if ( ! is_string($hex) || ! preg_match('/^[0-9a-f]{32}$/', $hex) ) {
            return;
        }
        if ( ! isset($_SESSION[TSUGI_SESSION_LTI]) || ! is_array($_SESSION[TSUGI_SESSION_LTI]) ) {
            return;
        }
        $current = $_SESSION[TSUGI_SESSION_LTI][$alias] ?? null;
        if ( is_string($current) && strlen($current) === 16 ) {
            return;
        }
        $binary = hex2bin($hex);
        if ( ! is_string($binary) || strlen($binary) !== 16 ) {
            return;
        }
        $_SESSION[TSUGI_SESSION_LTI][$alias] = $binary;
    }

    public static function buildLaunch($LTI) {
        global $CFG, $TSUGI_LAUNCH, $TSUGI_KEY;
        global $OUTPUT, $USER, $PROFILE, $CONTEXT, $LINK, $RESULT, $ROSTER;

        if ( ! isset($TSUGI_LAUNCH) ) {
            $TSUGI_LAUNCH = new \Tsugi\Core\Launch();
        }

        $TSUGI_LAUNCH->membership = null;

        $lti_row = is_array($LTI) ? $LTI : array();
        $PROFILE = \Tsugi\Core\Profile::fromLaunchRow($lti_row, $TSUGI_LAUNCH);
        $TSUGI_LAUNCH->profile = $PROFILE;

        // Populate the $USER $CONTEXT and $LINK objects
        if ( isset($LTI['user_id']) && ! is_object($USER) ) {
            $USER = new \Tsugi\Core\User($LTI);
            self::rememberBinaryGuid('user_guid', $USER->guid);
            $USER->launch = $TSUGI_LAUNCH;
            $USER->id = $LTI['user_id'];
            $USER->key = $LTI['user_key'];
            $email = $LTI['user_email'] ?? ($LTI['email'] ?? null);
            if ( $email !== null ) $USER->email = $email;
            $display = $LTI['user_displayname'] ?? ($LTI['displayname'] ?? null);
            if ( $display !== null ) {
                $USER->displayname = $display;
                $pieces = explode(' ',$USER->displayname);
                if ( count($pieces) > 0 ) $USER->firstname = $pieces[0];
                if ( count($pieces) > 1 ) $USER->lastname = $pieces[count($pieces)-1];
            }
            $image = $LTI['user_image'] ?? ($LTI['image'] ?? null);
            if ( $image !== null ) $USER->image = $image;
            $locale = $LTI['user_locale'] ?? ($LTI['locale'] ?? null);
            if ( $locale ) $USER->locale = $locale;
            if ( $USER->locale ) {
                I18N::setLocale($USER->locale);
            }
            $USER->instructor = isset($LTI['role']) && $LTI['role'] != 0 ;
            $USER->admin = isset($LTI['role']) && $LTI['role'] >= self::ROLE_ADMINISTRATOR;
            $TSUGI_LAUNCH->user = $USER;

            $MEMBERSHIP = new \Tsugi\Core\Membership();
            $MEMBERSHIP->launch = $TSUGI_LAUNCH;
            if ( isset($LTI['membership_id']) ) {
                $MEMBERSHIP->id = $LTI['membership_id'] + 0;
            }
            if ( isset($LTI['role']) ) {
                $MEMBERSHIP->role = $LTI['role'] + 0;
            }
            $TSUGI_LAUNCH->membership = $MEMBERSHIP;
        }

        $TSUGI_LAUNCH->for_user = null;
        if ( isset($LTI['for_user_id']) ) {
            $forRow = array('user_id' => $LTI['for_user_id']);
            if ( array_key_exists('for_user_guid', $LTI) ) {
                $forRow['user_guid'] = $LTI['for_user_guid'];
            }
            $for_user = new \Tsugi\Core\User($forRow);
            self::rememberBinaryGuid('for_user_guid', $for_user->guid);
            $for_user->launch = $TSUGI_LAUNCH;
            $for_user->id = $LTI['for_user_id'];
            $for_user->key = $LTI['for_user_key'];
            if (isset($LTI['for_user_email']) ) $for_user->email = $LTI['for_user_email'];
            if (isset($LTI['for_user_displayname']) ) {
                $for_user->displayname = $LTI['for_user_displayname'];
                $pieces = explode(' ',$for_user->displayname);
                if ( count($pieces) > 0 ) $for_user->firstname = $pieces[0];
                if ( count($pieces) > 1 ) $for_user->lastname = $pieces[count($pieces)-1];
            }
            if (isset($LTI['for_user_image']) ) $for_user->image = $LTI['for_user_image'];
            if (isset($LTI['for_user_locale']) ) $for_user->locale = $LTI['for_user_locale'];
            if ( $for_user->locale ) {
                I18N::setLocale($for_user->locale);
            }
            $for_user->instructor = isset($LTI['for_user_role']) && $LTI['for_user_role'] != 0 ;
            $for_user->admin = isset($LTI['for_user_role']) && $LTI['for_user_role'] >= self::ROLE_ADMINISTRATOR;
            $TSUGI_LAUNCH->for_user = $for_user;
        }

        if ( isset($LTI['key_id']) && ! is_object($TSUGI_KEY) ) {
            $TSUGI_KEY = new \Tsugi\Core\Key($LTI);
            self::rememberBinaryGuid('key_guid', $TSUGI_KEY->guid);
            $TSUGI_KEY->launch = $TSUGI_LAUNCH;
            $TSUGI_KEY->id = $LTI['key_id'];
            if (isset($LTI['key_title']) ) $TSUGI_KEY->title = $LTI['key_title'];
            if (isset($LTI['key_key']) ) $TSUGI_KEY->key = $LTI['key_key'];
            if (isset($LTI['secret']) ) $TSUGI_KEY->secret = $LTI['secret'];
            $TSUGI_LAUNCH->key = $TSUGI_KEY;
        }

        $courseApplies = self::launchContextApplies(is_array($LTI) ? $LTI : null);

        if ( $courseApplies && isset($LTI['context_id']) && ! is_object($CONTEXT) ) {
            $CONTEXT = new \Tsugi\Core\Context($LTI);
            self::rememberBinaryGuid('context_guid', $CONTEXT->guid);
            $CONTEXT->launch = $TSUGI_LAUNCH;
            $CONTEXT->id = $LTI['context_id'];
            if (isset($LTI['context_title']) ) $CONTEXT->title = $LTI['context_title'];
            if (isset($LTI['key_key']) ) $CONTEXT->key = $LTI['key_key'];
            if (isset($LTI['secret']) ) $CONTEXT->secret = $LTI['secret'];
            if (isset($LTI['context_key']) ) $CONTEXT->context_id = $LTI['context_key'];
            $TSUGI_LAUNCH->context = $CONTEXT;
        }

        if ( $courseApplies && isset($LTI['link_id']) && ! is_object($LINK) ) {
            $LINK = new \Tsugi\Core\Link($LTI);
            self::rememberBinaryGuid('link_guid', $LINK->guid);
            $LINK->launch = $TSUGI_LAUNCH;
            $LINK->id = $LTI['link_id'];
            if (isset($LTI['link_title']) ) $LINK->title = $LTI['link_title'];
            if (isset($LTI['link_count']) ) $LINK->activity = $LTI['link_count']+0;
            if (isset($LTI['link_user_count']) ) $LINK->user_activity = $LTI['link_user_count']+0;
            if (isset($LTI['result_id']) ) $LINK->result_id = $LTI['result_id'];
            if ( isset($LTI['grade']) && $LTI['grade'] !== null && $LTI['grade'] !== false && $LTI['grade'] !== '' ) {
                $LINK->grade = $LTI['grade'];
            }

            // Check to see if we are supposed to use SHA256 for this link
            $settings_method = $LINK->settingsGet('oauth_signature_method');
            $post_data = $_SESSION[TSUGI_SESSION_LTI_POST] ?? null;
            $launch_method = null;
            if ( is_array($post_data) ) $launch_method = U::get($post_data, 'oauth_signature_method');

            // error_log("sm=$settings_method lm=$launch_method");
            if ( $settings_method == $launch_method ) {
                // All good
            } else if ( ! $settings_method && $launch_method == 'HMAC-SHA1' ) {
                // All good
            } else {
                // error_log("Setting oauth_signature_method=$launch_method");
                $LINK->settingsSet('oauth_signature_method', $launch_method);
            }

            // The activity (Don't make global to avoid bad habits)
            $TSUGI_LAUNCH->link = $LINK;
        }

        if ( $courseApplies && isset($LTI['result_id']) && ! is_object($RESULT) ) {
            $RESULT = new \Tsugi\Core\Result();
            $RESULT->launch = $TSUGI_LAUNCH;
            $RESULT->id = $LTI['result_id'];
            if (isset($LTI['grade']) ) $RESULT->grade = $LTI['grade'];
            $TSUGI_LAUNCH->result = $RESULT;
        }

        if ( $courseApplies && $TSUGI_LAUNCH->ltiRawParameter(LTIConstants::EXT_CONTEXT_MEMBERSHIP_ID) && ! is_object($ROSTER)) {
            $ROSTER = new \Tsugi\Core\Roster();
            $ROSTER->id = $TSUGI_LAUNCH->ltiRawParameter(LTIConstants::EXT_CONTEXT_MEMBERSHIP_ID);
            $ROSTER->url = $TSUGI_LAUNCH->ltiRawParameter(LTIConstants::EXT_CONTEXT_MEMBERSHIP_URL);
        }

        if ( isset($LTI['lti13_deeplink']) && is_object($LTI['lti13_deeplink']) ) {
            $TSUGI_LAUNCH->deeplink = new DeepLinkRequest($LTI['lti13_deeplink']);
        }

        // Return the Launch structure
        return $TSUGI_LAUNCH;
    }

    /**
     * Handle the optional LTI pre 1.3 Launch Authorization flow
     */
    public static function launchAuthorizationFlow($request_data)
    {
        global $CFG, $PDOX;

        $key = U::get($request_data, 'oauth_consumer_key');
        if ( ! $key ) return false;
        if ( U::get($request_data, 'oauth_signature_method') && U::get($request_data, 'oauth_timestamp') &&
            U::get($request_data, 'oauth_nonce') && U::get($request_data, 'oauth_version') &&
            U::get($request_data, 'oauth_signature') ) {
            // pass
        } else {
            return false;
        }

        // Handle second half of the pre LTI 1.3 Launch Authorization Flow
        $tool_state = U::get($request_data, 'tool_state');
        $my_tool_state = self::getBrowserSignature();
        if ( $tool_state && $tool_state != $my_tool_state ) {
            error_log("Mismatch tool_state, Incoming state $tool_state computed $my_tool_state");
            self::abort_with_error_log('Mismatched tool_state');
        }

        // Handle first half of the pre LTI 1.3 Launch Authorization Flow
        $relaunch_url = U::get($request_data, 'relaunch_url');
        if ( ! $relaunch_url ) return false;

        $key_sha256 = U::lti_sha256($key);
        $row = $PDOX->rowDie("SELECT key_id, key_key, secret
            FROM {$CFG->dbprefix}lti_key WHERE key_sha256 = :SHA",
            array(':SHA' => $key_sha256) );
        if ( ! $row ) {
            self::abort_with_error_log('Could not load oauth_consumer_key');
        }

        $valid = LTI::verifyKeyAndSecret($key,$row['secret'],self::curPageUrl(), $request_data);
        if ( $valid !== true ) {
            self::abort_with_error_log('OAuth validation fail key='.$key.' error='.$valid[0],$valid[1]);
        }

        $platform_state = U::get($request_data, 'platform_state');
        if ( $platform_state) $relaunch_url = U::add_url_parm($relaunch_url, "platform_state", $platform_state);
        $tool_state = self::getBrowserSignature();
        $relaunch_url = U::add_url_parm($relaunch_url, "tool_state", $tool_state);
        echo("Relaunching ".$relaunch_url);

        if ( headers_sent() ) {
            echo('

Click to continue

'); } else { header('Location: '.$relaunch_url); } return true; } /** * Dump out the internal data structures adssociated with the * current launch. Best if used within a pre tag. */ public static function var_dump() { global $USER, $CONTEXT, $LINK, $RESULT; echo('$USER:'."\n"); if ( ! isset($USER) ) { echo("Not set\n"); } else { var_dump($USER); } echo('$CONTEXT:'."\n"); if ( ! isset($CONTEXT) ) { echo("Not set\n"); } else { var_dump($CONTEXT); } echo('$LINK:'."\n"); if ( ! isset($LINK) ) { echo("Not set\n"); } else { var_dump($LINK); } echo('$RESULT:'."\n"); if ( ! isset($RESULT) ) { echo("Not set\n"); } else { var_dump($RESULT); } echo("\n
\n"); } /** * Load the grade for a particular row and update our local copy (Deprecated - moved to Result) * * Call the right LTI service to retrieve the server's grade and * update our local cached copy of the server_grade and the date * retrieved. This routine pulls the key and secret from the LTIX * session to avoid crossing cross tenant boundaries. * * @param $row An optional array with the data that has the result_id, sourcedid, * and service (url) if this is not present, the data is pulled from the LTI * session for the current user/link combination. * @param $debug_log An (optional) array (by reference) that returns the * steps that were taken. * Each entry is an array with the [0] element a message and an optional [1] * element as some detail (i.e. like a POST body) * * @return mixed If this work this returns a float. If not you get * a string with an error. * */ public static function gradeGet($row=false, &$debug_log=false) { global $RESULT; if ( isset($RESULT) ) return $RESULT->gradeGet($row,$debug_log); return 'LTIX::gradeGet $RESULT not set'; } /** * Send a grade and update our local copy (Deprecated - moved to Result) * * Call the right LTI service to send a new grade up to the server. * update our local cached copy of the server_grade and the date * retrieved. This routine pulls the key and secret from the LTIX * session to avoid crossing cross tenant boundaries. * * @param $grade A new grade - floating point number between 0.0 and 1.0 * @param $row An optional array with the data that has the result_id, sourcedid, * and service (url) if this is not present, the data is pulled from the LTI * session for the current user/link combination. * @param $debug_log An (optional) array (by reference) that returns the * steps that were taken. * Each entry is an array with the [0] element a message and an optional [1] * element as some detail (i.e. like a POST body) * * @return mixed If this works it returns true. If not, you get * a string with an error. * */ public static function gradeSend($grade, $row=false, &$debug_log=false) { global $RESULT; if ( isset($RESULT) ) return $RESULT->gradeSend($grade,$row,$debug_log); return 'LTIX::gradeSend $RESULT not set'; } /** * Send a grade applying the due date logic and only increasing grades (Deprecated - moved to Result) * * Puts messages in the session for a redirect. * * @param $gradetosend - The grade in the range 0.0 .. 1.0 * @param $oldgrade - The previous grade in the range 0.0 .. 1.0 (optional) * @param $dueDate - The due date for this assignment */ public static function gradeSendDueDate($gradetosend, $oldgrade=false, $dueDate=false) { global $RESULT; if ( isset($RESULT) ) return $RESULT->gradeSendDueDate($gradetosend,$oldgrade,$dueDate); return 'LTIX::gradeSendDueDate $RESULT not set'; } /** * signParameters - Look up the key and secret and call the underlying code in LTI */ public static function signParameters($oldparms, $endpoint, $method, $submit_text = false, $org_id = false, $org_desc = false) { $oauth_consumer_key = self::ltiParameter('key_key'); $oauth_consumer_secret = self::decrypt_secret(self::ltiParameter('secret')); return LTI::signParameters($oldparms, $endpoint, $method, $oauth_consumer_key, $oauth_consumer_secret, $submit_text, $org_id, $org_desc); } /** * Send settings to the LMS using the simple JSON approach */ public static function settingsSend($settings, $settings_url, &$debug_log=false) { $key_key = self::ltiParameter('key_key'); $secret = self::decrypt_secret(self::ltiParameter('secret')); $retval = LTI::sendJSONSettings($settings, $settings_url, $key_key, $secret, $debug_log); return $retval; } /** * Send a JSON Body to a URL after looking up the key and secret * * @param $method The HTTP Method to use * @param $postBody * @param $content_type * @param $service_url * @param bool $debug_log * @return mixed */ public static function jsonSend($method, $postBody, $content_type, $service_url, &$debug_log=false) { $key_key = self::ltiParameter('key_key'); $secret = self::decrypt_secret(self::ltiParameter('secret')); $retval = LTI::sendJSONBody($method, $postBody, $content_type, $service_url, $key_key, $secret, $debug_log); return $retval; } /** * ltiLinkUrl - Returns true if we can return LTI Links for this launch * * @return string The content_item_return_url or false */ public static function ltiLinkUrl($postdata=false) { return LTI::ltiLinkUrl(self::ltiRawPostArray()); } /** * getKeySecretForLaunch - Retrieve a Key/Secret for a Launch * * @param $url - The url to lookup */ public static function getKeySecretForLaunch($url) { global $CFG, $CONTEXT; $PDOX = self::getConnection(); $host = parse_url($url, PHP_URL_HOST); $port = parse_url($url, PHP_URL_PORT); $key_id = self::ltiParameter('key_id', null); if ( $key_id == null ) return false; $sql = "SELECT consumer_key, secret FROM {$CFG->dbprefix}lti_domain WHERE domain = :DOM AND key_id = :KID"; $values = array(":DOM" => $host, ":KID" => $key_id); if ( isset($CONTEXT->id) ) { $sql .= " AND (context_id IS NULL OR context_id = :CID) ORDER BY context_id DESC"; $values[':CID'] = $CONTEXT->id; } $row = $PDOX->rowDie($sql, $values); if ( $row === false ) { error_log("Unable to key/secret key_id=$key_id url=$url"); return false; } $row['key'] = $row['consumer_key']; return $row; } /** * curPageUrl - Returns the URL to the currently executing script with query string * * This is useful when we want to do OAuth where we need the exact * incoming path but our host, protocol, and port might be messed up * by a proxy or CDN. * * URL Result * http://x.com/data http://x.com/data * http://x.com/data/index.php http://x.com/data/index.php * http://x.com/data/index.php?y=1 http://x.com/data/index.php?y=1 */ public static function curPageUrl() { return self::curPageUrlBase() . $_SERVER['REQUEST_URI']; } /** * curPageUrlNoQuery - Returns the URL to the currently executing query without query string * * URL Result * http://x.com/data http://x.com/data * http://x.com/data/keys http://x.com/data/keys * http://x.com/data/keys?x=1 http://x.com/data/keys */ public static function curPageUrlNoQuery() { return self::removeQueryString(self::curPageUrlBase() . $_SERVER['REQUEST_URI']); } /** * removeQueryString - Drop a query string from a url */ public static function removeQueryString($url) { $pos = strpos($url, '?'); if ( $pos === false ) return $url; $url = substr($url,0,$pos); return $url; } /** * curPageUrlFolder - Returns the URL to the folder currently executing * * This is useful when rest-style files want to link back to "index.php" * Note - this will not go up to a parent. * * URL Result * http://x.com/data/ http://x.com/data/ * http://x.com/data/keys http://x.com/data/ */ public static function curPageUrlFolder() { $folder = self::curPageUrlBase() . $_SERVER['REQUEST_URI']; $folder = self::removeQueryString($folder); if ( preg_match('/\/$/', $folder) ) return $folder; return dirname($folder); } /** * curPageUrlScript - Returns the URL to the currently executing script * * This is useful when we want to make a URL to another script at this location. * Often we use this with str_replace(). * * URL Result * http://x.com/data http://x.com/data/index.php * http://x.com/data/index.php http://x.com/data/index.php * http://x.com/data/index.php?y=1 http://x.com/data/index.php * * http://stackoverflow.com/questions/279966/php-self-vs-path-info-vs-script-name-vs-request-uri * * http://example.com/bob * REQUEST_URI = /bob * PHP_SELF = /bob/index.php */ public static function curPageUrlScript() { return self::curPageUrlBase() . $_SERVER['PHP_SELF']; } /** * curPageUrlBase - Returns the protocol, host, and port for the current URL * * This is useful when we are running behind a proxy like ngrok * or CloudFlare. These proxies will accept with the http or * https version of the URL but our web server will likely only * se the incoming request as http. So we need to fall back * to $CFG->wwwroot and reconstruct the right URL from there. * Since the wwwroot might have some of the request URI, like * * http://tsugi.ngrok.com/tsugi * * We need to parse the wwwroot and put things back together. * * URL Result * http://x.com/data http://x.com * http://x.com/data/index.php http://x.com * http://x.com/data/index.php?y=1 http://x.com * * @return string The current page protocol, host, and optionally port URL */ public static function curPageUrlBase() { global $CFG; $pieces = parse_url($CFG->wwwroot); if ( isset($pieces['scheme']) ) { $scheme = $pieces['scheme']; } else { $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") ? 'http' : 'https'; } if ( isset($pieces['port']) ) { $port = ':'.$pieces['port']; } else { $port = ''; if ( $_SERVER['SERVER_PORT'] != "80" && $_SERVER['SERVER_PORT'] != "443" && strpos(':', $_SERVER['HTTP_HOST']) < 0 ) { $port = ':' . $_SERVER['SERVER_PORT'] ; } } $host = isset($pieces['host']) ? $pieces['host'] : $_SERVER['HTTP_HOST']; $http_url = $scheme . '://' . $host . $port; return $http_url; } // See if we need to extend our session (heartbeat) // http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes private static function checkHeartBeat() { global $CFG; if ( session_id() == "" ) return; if ( isset($CFG->sessionlifetime) ) { if ( isset($_SESSION['LAST_ACTIVITY']) ) { $heartbeat = $CFG->sessionlifetime/4; $ellapsed = time() - $_SESSION['LAST_ACTIVITY']; if ( $ellapsed > $heartbeat ) { $_SESSION['LAST_ACTIVITY'] = time(); $filename = isset($_SERVER['SCRIPT_FILENAME']) ? $_SERVER['SCRIPT_FILENAME'] : ''; error_log("Heartbeat ".session_id().' '.$ellapsed.' '.$filename); } } else { $_SESSION['LAST_ACTIVITY'] = time(); } } } private static function send403() { header("HTTP/1.1 403 Forbidden"); } // Returns true for a good referrer and false if we could not verify it private static function checkReferer() { global $CFG; return isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'],$CFG->wwwroot) === 0 ; } /** * Session CSRF token for same-origin HTML forms (not LTI launches or signed webhooks). * * @param bool $rotate When true, always mint a new token (login / LTI launch). * @return string */ public static function ensureCsrfToken($rotate = false) { $token = $_SESSION['CSRF_TOKEN'] ?? ''; if ( $rotate || ! is_string($token) || $token === '' ) { $token = bin2hex(random_bytes(16)); $_SESSION['CSRF_TOKEN'] = $token; } return $token; } // Returns true for a good CSRF and false if we could not verify it private static function checkCSRF() { global $CFG; $token = $_SESSION['CSRF_TOKEN'] ?? null; if ( ! $token ) return false; if ( isset($_POST['CSRF_TOKEN']) && $token == $_POST['CSRF_TOKEN'] ) return true; $headers = array_change_key_case(apache_request_headers()); if ( isset($headers['x-csrf-token']) && $token == $headers['x-csrf-token'] ) return true; if ( isset($headers['x-csrftoken']) && $token == $headers['x-csrftoken'] ) return true; return false; } /** * getCoreLaunchData - Get the launch data common across launch types */ public static function getCoreLaunchData() { global $CFG, $CONTEXT, $USER, $CONTEXT, $LINK; $ltiProps = array(); $ltiProps[LTIConstants::LTI_VERSION] = LTIConstants::LTI_VERSION_1; $ltiProps[LTIConstants::CONTEXT_ID] = $CONTEXT->id; $ltiProps[LTIConstants::ROLES] = $USER->instructor ? LTIConstants::ROLE_INSTRUCTOR : LTIConstants::ROLE_LEARNER; $ltiProps[LTIConstants::USER_ID] = $USER->id; $ltiProps[LTIConstants::LIS_PERSON_NAME_FULL] = $USER->displayname; $ltiProps[LTIConstants::LIS_PERSON_CONTACT_EMAIL_PRIMARY] = $USER->email; $ltiProps['tool_consumer_instance_guid'] = $CFG->wwwroot; $ltiProps['tool_consumer_instance_description'] = $CFG->servicename; return $ltiProps; } /** * getLaunchData - Get the launch data for a normal LTI 1.x launch */ public static function getLaunchData() { global $CFG, $CONTEXT, $USER, $CONTEXT, $LINK; $ltiProps = self::getCoreLaunchData(); $ltiProps[LTIConstants::LTI_MESSAGE_TYPE] = LTIConstants::LTI_MESSAGE_TYPE_BASICLTILAUNCHREQUEST; $ltiProps[LTIConstants::RESOURCE_LINK_ID] = $LINK->id; $ltiProps['tool_consumer_instance_guid'] = $CFG->wwwroot; $ltiProps['tool_consumer_instance_description'] = $CFG->servicename; return $ltiProps; } /** * getLaunchData - Get the launch data for am LTI ContentItem launch */ public static function getContentItem($contentReturn, $dataProps) { global $CFG, $CONTEXT, $USER, $CONTEXT, $LINK; $ltiProps = self::getCoreLaunchData(); $ltiProps[LTIConstants::LTI_MESSAGE_TYPE] = LTIConstants::CONTENT_ITEM_SELECTION_REQUEST; $ltiProps[LTIConstants::ACCEPT_MEDIA_TYPES] = LTIConstants::MEDIA_LTILINKITEM; $ltiProps[LTIConstants::ACCEPT_PRESENTATION_DOCUMENT_TARGETS] = "iframe,window"; // Nice to add overlay $ltiProps[LTIConstants::ACCEPT_UNSIGNED] = "true"; $ltiProps[LTIConstants::ACCEPT_MULTIPLE] = "false"; $ltiProps[LTIConstants::ACCEPT_COPY_ADVICE] = "false"; // ??? $ltiProps[LTIConstants::AUTO_CREATE] = "true"; $ltiProps[LTIConstants::CAN_CONFIRM] = "false"; $ltiProps[LTIConstants::CONTENT_ITEM_RETURN_URL] = $contentReturn; $ltiProps[LTIConstants::LAUNCH_PRESENTATION_RETURN_URL] = $contentReturn; // This is needed to trigger WarpWire to send us back the link $ltiProps['tool_consumer_info_product_family_code'] = 'canvas'; $ltiProps['custom_canvas_course_id'] = $CONTEXT->id; return $ltiProps; } /** * getLaunchData - Get the launch data for am LTI ContentItem launch */ public static function getLaunchUrl($endpoint, $debug=false) { $launchurl = Output::getUtilUrl('/launch.php?debug='); $launchurl .= ($debug) ? '1':'0'; $launchurl .= '&endpoint='; $launchurl .= urlencode($endpoint); return $launchurl; } /** * getLaunchContent - Get the launch data for am LTI ContentItem launch */ public static function getLaunchContent($endpoint, $debug=false) { $info = LTIX::getKeySecretForLaunch($endpoint); if ( $info === false ) { return '

Unable to load key/secret for '.htmlentities($endpoint)."

\n"; } $key = $info['key']; $secret = self::decrypt_secret($info['secret']); $parms = LTIX::getLaunchData(); $parms = LTI::signParameters($parms, $endpoint, "POST", $key, $secret, "Button"); $content = LTI::postLaunchHTML($parms, $endpoint, false); return $content; } /** * We are aborting this request. If we are processing a launch request, redirect back. * If this is a loss of session, show an error page that checks for things like * sessionStorage to find the best place to redirect back to. */ public static function abort_with_error_log($msg, $extra=false, $prefix="DIE:") { global $CFG; // If we are actually processing a launch request, or have a valid session $error_return_url = isset($_POST['launch_presentation_error_return_url']) ? $_POST['launch_presentation_error_return_url'] : null; if ( ! $error_return_url && isset($_SESSION[TSUGI_SESSION_LTI_POST]['launch_presentation_error_return_url']) ) { $error_return_url = $_SESSION[TSUGI_SESSION_LTI_POST]['launch_presentation_error_return_url']; } $return_url = isset($_POST['launch_presentation_return_url']) ? $_POST['launch_presentation_return_url'] : null; if ( ! $return_url && isset($_SESSION[TSUGI_SESSION_LTI_POST]['launch_presentation_return_url']) ) { $return_url = $_SESSION[TSUGI_SESSION_LTI_POST]['launch_presentation_return_url']; } if ( is_string($error_return_url) && U::strlen(trim($error_return_url)) > 0 ) { $return_url = trim($error_return_url); } if ( is_array($extra) ) $extra = Output::safe_var_dump($extra); // make the msg a bit friendlier if ( ! headers_sent() ) { header('X-Tsugi-Test-Harness: https://www.tsugi.org/lti-test/'); header('X-Tsugi-Base-String-Checker: https://www.tsugi.org/lti-test/basecheck.php'); if ( $extra ) { header('X-Tsugi-Error-Detail: '.str_replace("\n"," -- ",$extra)); } } $headers = headers_list(); $json = false; foreach($headers as $header ) { $header = strtolower($header); if ( stripos($header, 'content-type:') === 0 && stripos($header, 'json') > 0 ) $json = true; } if ( $json ) { $oldcode = http_response_code(); if ( ! is_numeric($oldcode) || $oldcode < 400 || $oldcode > 700 ) http_response_code(403); $response = new \stdClass(); $response->status = "error"; if ( $extra ) $response->tsugi_detail = $extra; $response->tsugi_tester = 'https://www.tsugi.org/lti-test/'; $response->base_string = 'https://www.tsugi.org/lti-test/basecheck.php'; echo(json_encode($response)); error_log($prefix.' '.$msg.' '.$extra); exit(); } // If we are not processing a launch request, or do not have a valid session, or were not provided // with a return URL, show an error page. if ($return_url === null) { error_log($prefix.' '.$msg.' '.$extra); print_stack_trace(); if ( isset($_SESSION[TSUGI_SESSION_LTI_POST]) ) { // We have a session, but it is somehow broken Output::htmlError("The LTI Launch Errors", $msg, $extra); } else { Output::htmlError("LTI Session is Missing or Expired", $msg, $extra); } exit(); } $return_url .= ( strpos($return_url,'?') > 0 ) ? '&' : '?'; $return_url .= 'lti_errormsg=' . urlencode($msg); if ( $extra !== false ) { if ( strlen($extra) < 200 ) $return_url .= '&detail=' . urlencode($extra); } header("Location: ".$return_url); error_log($prefix.' '.$msg.' '.$extra); exit(); } /** * Update the login_at fields as appropriate */ public static function noteLoggedIn($row) { global $CFG, $PDOX; if ( ! isset($row['user_id']) ) return; if ( ! isset($PDOX) || ! $PDOX ) return; if ( Net::getIP() !== NULL ) { $sql = "UPDATE {$CFG->dbprefix}lti_user SET login_at=NOW(), login_count=login_count+1, ipaddr=:IP WHERE user_id = :user_id"; $stmt = $PDOX->queryReturnError($sql, array( ':IP' => Net::getIP(), ':user_id' => $row['user_id'])); } else { $sql = "UPDATE {$CFG->dbprefix}lti_user SET login_at=NOW(), login_count=login_count+1 WHERE user_id = :user_id"; $stmt = $PDOX->queryReturnError($sql, array( ':user_id' => $row['user_id'])); } if ( ! $stmt->success ) { error_log("Unable to update login_at user_id=".$row['user_id']); } if ( isset($row['context_id']) ) { $sql = "UPDATE {$CFG->dbprefix}lti_context SET login_at=NOW(), login_count=login_count+1 WHERE context_id = :context_id"; $stmt = $PDOX->queryReturnError($sql, array( ':context_id' => $row['context_id'])); if ( ! $stmt->success ) { error_log("Unable to update login_at context_id=".$row['context_id']); } } // We do an update of login_at for the key if ( array_key_exists('key_id', $row) ) { $sql = "UPDATE {$CFG->dbprefix}lti_key SET login_at=NOW(),login_count=login_count+1 WHERE key_id = :key_id"; $stmt = $PDOX->queryReturnError($sql, array( ':key_id' => $row['key_id'])); if ( ! $stmt->success ) { error_log("Unable to update login_at key_id=".$row['context_id']); } } } /** * populateRoster * * If the LTI Extension: Context Memberships Service is supported in the launch, get the memberships * information and insert the information into lti_user and lti_membership * * @param bool $groups, whether or not to get groups in the Memberships * @param bool $insert, whether or not to insert the members found into lti_user, lti_membership * @return bool true if successful, false if not possible */ public static function populateRoster($groups=false, $insert=false) { global $ROSTER, $CFG, $TSUGI_LAUNCH; if(!is_object($ROSTER)) { return false; } if (filter_var($ROSTER->url, FILTER_VALIDATE_URL) === FALSE) { return false; } $encryptedSecret = self::ltiParameter('secret'); $key = self::ltiParameter('key_key'); $response = LTI::getContextMemberships($ROSTER->id, $ROSTER->url, $key, self::decrypt_secret($encryptedSecret), $groups); if($response != false) { $ROSTER->data = $response; if ($insert) { $PDOX = self::getConnection(); $p = $CFG->dbprefix; $errormode = $PDOX->getAttribute(\PDO::ATTR_ERRMODE); $PDOX->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); // Set up user $insertUser = "INSERT INTO {$p}lti_user ( guid, user_key, user_sha256, displayname, email, locale, json, key_id, created_at, updated_at ) VALUES "; // Set up membership $insertMembership = "INSERT INTO {$p}lti_membership ( context_id, user_id, role, created_at, updated_at ) VALUES "; $key = $PDOX->rowDie("SELECT key_id FROM {$CFG->dbprefix}lti_key WHERE key_key = :key", array(':key' => $TSUGI_LAUNCH->context->key)); if (! $key ) return false; $insertUserArray = []; $insertUserMembershipArray = []; foreach($ROSTER->data as $user) { array_push($insertUserArray, "(UNHEX('".Guid::uuid7()."'), '". $user['user_id'] ."', '". lti_sha256($user['user_id']) ."', '". $user['user_name'] ."', '". $user['user_email'] ."', '". ($TSUGI_LAUNCH->context->launch->user->locale ? $TSUGI_LAUNCH->context->launch->user->locale : ''). "', ". "'{\"sourcedId\": \"". $user['lis_result_sourcedid'] ."\"}', ". $key['key_id'] .", NOW(), NOW() )"); array_push($insertUserMembershipArray, "(". $TSUGI_LAUNCH->context->id .", ". "(select user_id from {$p}lti_user where user_key = '". $user['user_id'] ."' limit 1), ". ($user['role'] == "Instructor" ? LTIX::ROLE_INSTRUCTOR : LTIX::ROLE_LEARNER) .", NOW(), NOW() )"); } $PDOX->queryDie($insertUser . implode(',', $insertUserArray) ." ON DUPLICATE KEY UPDATE " ." displayname = VALUES(displayname)" ." ,email = VALUES(email)" ." ,updated_at = NOW()" ." ,json = IF(JSON_VALID(json), JSON_SET(json, '$.sourcedId', JSON_EXTRACT(VALUES(json),'$.sourcedId')), VALUES(json))"); // ." ,json = REPLACE(CONCAT(IFNULL(json,'{}'), VALUES(json)), '}{', ',')"); $PDOX->queryDie($insertMembership . implode(',', $insertUserMembershipArray). " ON DUPLICATE KEY UPDATE role = VALUES(role), updated_at=NOW()"); } // if insert } // if response valid return true; } public static function getBrowserSignature() { $concat = self::getBrowserSignatureRaw(); $h = hash('sha256', $concat); return $h; } // As the anti-trackers gain traction cookies come and go - so don't depend on them public static function getBrowserSignatureRaw() { global $CFG; $look_at = array( 'x-forwarded-proto', 'x-forwarded-port', 'host', 'accept-encoding', 'cf-ipcountry', 'user-agent', 'accept', 'accept-language'); $headers = \Tsugi\Util\U::apache_request_headers(); // Living behind Cloudflare, IP adresses seem to change // $concat = \Tsugi\Util\Net::getIP(); $concat = ""; if ( isset($CFG->cookiepad) ) $concat .= ':::' . $CFG->cookiepad; if ( isset($CFG->cookiesecret) ) $concat .= ':::' . $CFG->cookiesecret; $used = array(); ksort($headers); foreach($headers as $k => $v ) { if ( ! in_array(strtolower($k), $look_at) ) continue; if ( is_string($v) ) { $used[$k] = $v; $concat .= ':::' . $k . '=' . $v; continue; } } return $concat; } public static function getTsugiStateCookieName() { return "tsugi-state-lti-advantage"; } /** Compute the kid has value from a public key * * @param pubkey The public key */ public static function getKidForKey($pubkey) { return hash('sha256', trim($pubkey)); } /** Retrieve the context and link ids for a previous installation of the tool, such as * if it were imported from another site during a site import. Returns false, if not. * @return array[ * 'context_id' => number, * 'link_id' => number * ] | false */ public static function getLatestHistoryIds() { global $CFG, $LAUNCH, $PDOX; $resource_history = $LAUNCH->ltiCustomGet("resourcelink_id_history", '$ResourceLink.id.history'); if ($resource_history != '$ResourceLink.id.history') { // Split the link_key and use the most recent one $link_key_arr = explode(',', $resource_history); $prev_link_key = end($link_key_arr); // Get the previous context and link ids $linkquery = "SELECT context_id, link_id FROM {$CFG->dbprefix}lti_link WHERE link_key = :link_key;"; $linkarr = array(':link_key' => $prev_link_key); return $PDOX->rowDie($linkquery, $linkarr); } else { return false; } } }