/** * If there is a new session for this user/server, append the token request to the page */ public static function appendTokenRequest() { $cmd = self::$cmd; $session = LigminchaGlobalSession::getCurrent(); // If there is a current session, if ($session) { // If the session is newly created, get an SSO cookie under ligmincha.org for this session ID // - newly created sessions have no expiry // - this is done by appending a 1x1pixel iFrame to the output that will request a token cookie from ligmincha.org if ($session->flag(LG_NEW)) { // We always set a local cookie as well so we can get the current session ID from it self::setCookie($session->id); // Otherwise we need to make the request to the master in the iFrame if (!LigminchaGlobalServer::getCurrent()->isMaster) { $url = LigminchaGlobalServer::masterDomain(); $iframe = "<iframe src=\"http://{$url}?{$cmd}={$session->id}\" frameborder=\"0\" width=\"1\" height=\"1\"></iframe>"; $app = JFactory::getApplication('site'); $app->setBody(str_replace('</body>', "{$iframe}\n</body>", $app->getBody())); lgDebug("SSO cookie request iFrame added to page ({$url})", $session); } // Set the expiry to a longer time that distributed sessions last // - after it expires, user needs to come back to have another made (may not need to log in again) $session->expire = LigminchaGlobalObject::timestamp() + LG_SESSION_DURATION; // Now that the session is real it can route $session->flag(LG_LOCAL, false); // Write changes to the session object into the distributed database $session->update(); } } }
/** * Destroy current session */ public static function delCurrent() { // Delete the global object if ($session = LigminchaGlobalSession::getCurrent()) { lgDebug('Session destroyed', $session); LigminchaGlobalDistributed::del(array('id' => $session->id)); } // Delete the SSO cookie LigminchaGlobalSSO::delCookie(); }
/** * Called after a user has successfully completed the login process */ public function onUserAfterLogin($options) { if (array_key_exists('user', $options)) { LigminchaGlobalSession::getCurrent(); } }
/** * Send all queued sync-objects */ public static function sendQueue() { // Get all LG_SYNC items, bail if none if (!($queue = LigminchaGlobalSync::select())) { return false; } // Make data streams for each target from the sync objects $streams = array(); $server = LigminchaGlobalServer::getCurrent()->id; $session = LigminchaGlobalSession::getCurrent() ? LigminchaGlobalSession::getCurrent()->id : 0; foreach ($queue as $sync) { $target = LigminchaGlobalServer::newFromId($sync->ref1)->id; if (array_key_exists($target, $streams)) { $streams[$target][] = $sync; } else { $streams[$target] = array(0, $server, $session, $sync); } } //print '<pre>'; print_r($streams); print '</pre>'; // Encode and send each stream foreach ($streams as $target => $stream) { // Get the target domain from it's tag $url = LigminchaGlobalServer::newFromId($target)->tag; // Zip up the data in JSON format // TODO: encrypt using shared secret or public key $data = self::encodeData($stream); // If we're standalone or the master, ensure no data is routed to the master, mark it as successful so the sync objects are cleared if (LigminchaGlobalServer::getCurrent()->isMaster && $url == LigminchaGlobalServer::masterDomain()) { $result = LG_SUCCESS; } else { $result = self::post($url, array(self::$cmd => $data)); } // If result is success, remove all sync objects destined for this target server if ($result == LG_SUCCESS) { LigminchaGlobalDistributed::del(array('type' => LG_SYNC, 'ref1' => $target), false, true); } else { die(lgDebug("Failed to post outgoing sync data to {$url} result({$result})")); } } return true; }
} // Load the LigminchaGlobal framework if it's not already installed if (!defined('LG_VERSION')) { require __DIR__ . "/distributed.php"; require __DIR__ . "/object.php"; require __DIR__ . "/sync.php"; require __DIR__ . "/server.php"; require __DIR__ . "/user.php"; require __DIR__ . "/session.php"; require __DIR__ . "/version.php"; require __DIR__ . "/log.php"; require __DIR__ . "/sso.php"; new LigminchaGlobalSSO(); new LigminchaGlobalDistributed(); } $session = LigminchaGlobalSession::getCurrent() ? LigminchaGlobalSession::getCurrent()->id : 0; $types = array(LG_SERVER, LG_LOG); if ($session) { $types[] = LG_USER; $types[] = LG_SESSION; } $objects = LigminchaGlobalObject::select(array('type' => $types)); $wgOut->addJsConfigVars('GlobalObjects', $objects); $wgOut->addJsConfigVars('session', $session); $wgOut->addJsConfigVars('toolbar', true); $wgOut->addJsConfigVars('wgServer', "http://{$lgGlobalAppDomain}"); $wgOut->addJsConfigVars('wsPort', 1729); $wgOut->addJsConfigVars('wsRewrite', true); // Generate the toolbar head content $lgToolbarHead = "<!-- LigminchaGlobal toolbar head start -->\n\t<link rel=\"stylesheet\" href=\"http://{$lgGlobalAppDomain}/styles/toolbar.css\" />\n\t<script type=\"text/javascript\" src=\"http://{$lgGlobalAppDomain}/resources/fakemediawiki.js\"></script>\n\t<script type=\"text/javascript\">\r\t\t{$lgScript}\t</script>\n\t<script type=\"text/javascript\" src=\"http://{$lgGlobalAppDomain}/resources/crypto.js\"></script>\n\t<script type=\"text/javascript\" src=\"http://{$lgGlobalAppDomain}/resources/underscore.js\"></script>\n\t<script type=\"text/javascript\" src=\"http://{$lgGlobalAppDomain}/resources/backbone.js\"></script>\n\t<script type=\"text/javascript\" src=\"http://{$lgGlobalAppDomain}/resources/WebSocket/websocket.js\"></script>\n\t<script type=\"text/javascript\" src=\"http://{$lgGlobalAppDomain}/distributed.js\"></script>\n\t<script type=\"text/javascript\" src=\"http://{$lgGlobalAppDomain}/object.js\"></script>\n\t<script type=\"text/javascript\" src=\"http://{$lgGlobalAppDomain}/server.js\"></script>\n\t<script type=\"text/javascript\" src=\"http://{$lgGlobalAppDomain}/user.js\"></script>\n\t<script type=\"text/javascript\" src=\"http://{$lgGlobalAppDomain}/session.js\"></script>\n\t<script type=\"text/javascript\" src=\"http://{$lgGlobalAppDomain}/version.js\"></script>\n\t<script type=\"text/javascript\" src=\"http://{$lgGlobalAppDomain}/util.js\"></script>\n\t<script type=\"text/javascript\" src=\"http://{$lgGlobalAppDomain}/main.js\"></script>\n<!-- LigminchaGlobal toolbar head end -->"; // Generate the toolbar body content