Skip to content

Commit

Permalink
Remove restoreLTISession()
Browse files Browse the repository at this point in the history
  • Loading branch information
csev committed Jun 10, 2024
1 parent a2dc131 commit 8b51484
Showing 1 changed file with 4 additions and 65 deletions.
69 changes: 4 additions & 65 deletions src/Core/LTIX.php
Original file line number Diff line number Diff line change
Expand Up @@ -1977,70 +1977,6 @@ public static function requireDataOverride($needed,
$pdox, $session_object, $current_url, $request_data);
}

/**
* Restore an LTI session and check if it worked
*
* If we are using memcached with php_serialize serialization,
* we take a wild guess that we might be having a race condition
* with memcache. So we wait a tic, and re-try the read.
*/
public static function restoreLTISession($session_id) {
global $CFG;

// You would think that this would just work :)
if ( session_id() == "" ) {
session_id($session_id);
session_start();
}

if ( U::get($_SESSION, 'lti') && U::get($_SESSION, 'lti_post') ) return;

// https://stackoverflow.com/questions/35728486/read-php-session-without-actually-starting-it
$serializer = ini_get('session.serialize_handler');
if ( ! isset($CFG->memcached) || U::isEmpty($CFG->memcached) || $serializer != 'php_serialize') return;

sleep(1);
try {
$servers = explode(',', $CFG->memcached);
$c = count($servers);
for ($i = 0; $i < $c; ++$i) {
$servers[$i] = explode(':', $servers[$i]);
}

$memcached = new \Memcached();
$memcached->addServers($servers);
$sessionPrefix = ini_get('memcached.sess_prefix');

$rawData = $memcached->get($sessionPrefix.$session_id);
if ( ! $rawData ) {
error_log("restoreLTISession - nothing to retrieve from memcached ".$session_id);
return;
}

// Keep unserialize() from issuing a notice
$data = $rawData ? @unserialize($rawData) : false;
if ( ! is_array($data) ) {
error_log("restoreLTISession - could not unserialize () ".$session_id);
return;
}

if ( count($data) < 1 ) {
error_log("restoreLTISession - empty memcached data ".$session_id);
return;
}

// Copy into session
$fields = "";
foreach($data as $k => $v) {
$_SESSION[$k] = $data[$k];
if ( strlen($fields) < 50 && is_string($data[$k]) ) $fields .= ' '.$k.'='.$data[$k];
}
error_log("restoreLTISession copied ".count($data)." ".$session_id.$fields);
} catch(\Exception $e) {
error_log("restoreLTISession exception ".$e->getMessage());
}
}

/**
* Internal method to handle the data setup
*/
Expand Down Expand Up @@ -2102,7 +2038,10 @@ public static function requireDataPrivate($needed=self::ALL,
if ( $newlaunch || isset($_POST[$sess]) || isset($_GET[$sess]) ) {
$session_id = $_POST[$sess] ?? $_GET[$sess] ?? null;
// Do our best to restore a session
if ( $session_id ) self::restoreLTISession($session_id);
if ( $session_id && session_id() == "" ) {
session_id($session_id);
session_start();
}
} else {
self::wrapped_session_flush($session_object);
self::send403();
Expand Down

0 comments on commit 8b51484

Please sign in to comment.