Skip to content

Commit

Permalink
Add the util bits
Browse files Browse the repository at this point in the history
  • Loading branch information
csev committed Dec 29, 2017
1 parent 96346ba commit d40a7cb
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 0 deletions.
11 changes: 11 additions & 0 deletions util/.htaccess
@@ -0,0 +1,11 @@
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ tsugi.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
FallbackResource tsugi.php
</IfModule>
11 changes: 11 additions & 0 deletions util/blob_serve.php
@@ -0,0 +1,11 @@
<?php

if ( ! isset($CFG) ) return; // Only from within tsugi.php

use \Tsugi\Core\LTIX;
use \Tsugi\Blob\Access;

// Sanity checks
$LAUNCH = LTIX::requireData(array(LTIX::CONTEXT, LTIX::LINK));

Access::serveContent();
16 changes: 16 additions & 0 deletions util/heartbeat.php
@@ -0,0 +1,16 @@
<?php

if ( ! isset($CFG) ) return; // Only from within tsugi.php

$cookie = defined('COOKIE_SESSION');

$retval = \Tsugi\UI\Output::handleHeartBeat($cookie);

if ( isset($CFG->eventpushtime) && isset($CFG->eventpushcount) && $CFG->eventpushcount > 0 ) {
$events = \Tsugi\Core\Activity::pushCaliperEvents($CFG->eventpushtime, $CFG->eventpushcount, false);
if ( isset($events['count']) && $events['count'] > 0 ) {
error_log("Heartbeat events count=".$events['count']." time=".$events['seconds']);
}
$retval['events'] = $events;
}
echo(\Tsugi\Util\LTI::jsonIndent(json_encode($retval)));
24 changes: 24 additions & 0 deletions util/launch.php
@@ -0,0 +1,24 @@
<?php

if ( ! isset($CFG) ) return; // Only from within tsugi.php

use \Tsugi\Core\LTIX;

if ( isset($_GET['endpoint']) && isset($_GET['debug']) ) {
// All good
} else {
die('endpoint and debug are required');
}

$endpoint = $_GET['endpoint'];
$debug = $_GET['debug'] == 0;

// Grab the session
$LAUNCH = LTIX::requireData();

$debug = true;

$content = LTIX::getLaunchContent($endpoint, $debug);

echo($content);

65 changes: 65 additions & 0 deletions util/linkprofile.php
@@ -0,0 +1,65 @@
<?php

if ( ! isset($CFG) ) return; // Only from within tsugi.php

use \Tsugi\Core\LTIX;
use \Tsugi\UI\Output;
use \Tsugi\Crypt\SecureCookie;

Output::headerJson();

// TODO: Make these 500's

// Not turned on
if ( !isset($CFG->unify) || ! $CFG->unify ) {
echo(json_encode(array("error" => "Not enabled")));
return;
}

// Nothing for us to do
if ( ! isset($_GET[session_name()]) ) {
echo(json_encode(array("error" => "No session")));
return;
}

// Grab the session
$LAUNCH = LTIX::requireData();

// See if the LTI login can be linked to the site login...
if ( ! isset($_SESSION['lti']) ) {
echo(json_encode(array("error" => "Not an LTI Session")));
return;
}

if ( ! isset($_COOKIE[$CFG->cookiename]) ) {
echo(json_encode(array("error" => "No Cookie for login")));
return;
}

// Contemplate: Do we care if the lti email matches the cookie email?
$ct = $_COOKIE[$CFG->cookiename];
$pieces = SecureCookie::extract($ct);
$lti = $_SESSION['lti'];
if ( count($pieces) != 3 ) {
echo(json_encode(array("error" => "Cookie decode failure")));
return;
}
if ( ! isset($lti['user_id']) ) {
echo(json_encode(array("error" => "user_id not found in LTI session")));
return;
}
// echo("Cookie user ".$pieces[0]. " Cookie email ".$pieces[1]." LTI User ".$lti['user_id']." LTI Profile ".$lti['profile_id']." LTI email ".$lti['user_email']."\n");
$row = $PDOX->rowDie("SELECT profile_id FROM {$CFG->dbprefix}lti_user WHERE user_id = :UID;",
array(':UID' => $pieces[0] )
);
if ( $row === false || ! isset($row['profile_id']) ) {
echo(json_encode(array("error" => "No profile_id for cookie user")));
return;
}
$stmt = $PDOX->queryDie("UPDATE {$CFG->dbprefix}lti_user SET profile_id = :PID WHERE user_id = :UID",
array(':UID' => $lti['user_id'], ':PID' => $row['profile_id'])
);
error_log("Updated Cookie user ".$pieces[0]. " email ".$pieces[1]." profile ".$row['profile_id']." LTI User ".$lti['user_id']." LTI email ".$lti['user_email']);

echo(json_encode(array("success" => "Profile linked")));
25 changes: 25 additions & 0 deletions util/tsugi.php
@@ -0,0 +1,25 @@
<?php

if ( isset($_GET[session_name()]) ) {
$cookie = false;
} else {
define('COOKIE_SESSION', true);
$cookie = true;
}
require_once('../config.php');

// Make PHP paths pretty .../install => install.php
$router = new Tsugi\Util\FileRouter();
$file = $router->fileCheck();
if ( $file ) {
require_once($file);
return;
}

// Add 404 Handling
http_response_code(404);
$OUTPUT->header();
$OUTPUT->bodyStart();
$OUTPUT->topNav();
echo("<h2>Page not found.</h2>\n");
$OUTPUT->footer();
74 changes: 74 additions & 0 deletions util/unsubscribe.php
@@ -0,0 +1,74 @@
<?php

if ( ! isset($CFG) ) return; // Only from within tsugi.php

use \Tsugi\Core\Mail;

$id = false;
$token = false;
if ( isset($_POST['id']) && isset($_POST['token']) ) {
$id = $_POST['id'] + 0;
$token = $_POST['token'];
error_log("Unsubscribe: $id, $token");
echo('You are unsubscribed. Thank you.');
// TODO: Actually unsubscribe
return;
}

if ( isset($_GET['id']) && isset($_GET['token']) ) {
$id = $_GET['id'] + 0;
$token = $_GET['token'];
}

if ( strlen($token) < 1 ) $token = false;

if ( $id === false || $token === false ) {
error_log("Unsubscribe missing id or token");
echo("Unsubscribe process requires both a 'id' and 'token parameter.");
return;
}
/*
require_once("db.php");
require_once("sqlutil.php");
$sql = "SELECT email,first,last,identity FROM Users WHERE id=$id";
$row = retrieve_one_row($sql);
if ( $row === false ) {
error_log("Unsubscribe user $id missing");
echo("Sorry, user $id not found");
return;
}
require_once("mail/maillib.php");
$check = Mail::computeCheck($row[3]);
if ( $token != $check ) {
echo("Sorry, token is not valid ");
error_log("Unsubscribe bad token=$token check=$check");
if ( isset($_SESSION["admin"]) ) echo($check);
return;
}
// We are past all the checks...
if ( isset($_POST['id']) ) {
$sql = "UPDATE Users SET subscribe=-1 WHERE id=$id";
$result = run_mysql_query($sql);
echo('You are unsubscribed. Thank you.');
error_log("Unsubscribed is=$id");
return;
}
*/

?>
<h2>Unsubscribing from E-Mail <?php echo($CFG->maildomain); ?></h2>
<p>If you want to unsubscribe from e-mail from
<a href="<?php echo($CFG->wwwroot); ?>"><?php echo($CFG->servicename); ?></a> press
"Unsubscribe" below.
</p>
<form method="post" action="unsubscribe">
<input type="hidden" name="id" value="<?php echo($id); ?>">
<input type="hidden" name="token" value="<?php echo(htmlencode($token)); ?>">
<input type="submit" value="Unsubscribe">
</form>
<p>
You can re-subscribe later if you like.
</p>

0 comments on commit d40a7cb

Please sign in to comment.