mocha/php/mocha/include/BeforeLaunchEvent.inc.php
2023-12-19 20:56:26 -05:00

260 lines
5.5 KiB
PHP

<?php
use Mocha\Core\KnownAttributeGuids;
use Mocha\Core\KnownInstanceGuids;
use Mocha\Core\KnownRelationshipGuids;
use Mocha\Oms\MySQLDatabaseOms;
use Phast\System;
function mocha_get_oms()
{
global $oms;
if ($oms === null)
{
$oms = new MySQLDatabaseOms(System::GetConfigurationValue("Database.ServerName"), System::GetConfigurationValue("Database.PortNumber"), System::GetConfigurationValue("Database.DatabaseName"), System::GetConfigurationValue("Database.UserName"), System::GetConfigurationValue("Database.Password"));
}
return $oms;
}
function mocha_init_spot_timer($pg)
{
$literalSpotTimer = $pg->GetControlByID("literalSpotTimer");
if (file_exists("/etc/mocha/suvstart"))
{
$suv_start_time = trim(file_get_contents("/etc/mocha/suvstart"));
$suv_start_time_d = new \DateTime($suv_start_time);
$suv_start_time_d->setTimezone(new \DateTimeZone("UTC"));
// this can be adjusted?
$suv_end_time_d = $suv_start_time_d->add(new \DateInterval("PT10H"));
$suv_end_time_d->setTimezone(new \DateTimeZone("UTC"));
$suv_end_time = $suv_end_time_d->format('Y-m-d\TH:i:s\Z');
$suv_current_time_d = new \DateTime();
$interval = $suv_end_time_d->diff($suv_current_time_d);
$literalSpotTimer->Content = $interval->h . ":" . $interval->i;
$spotTimerInitializationScript = $pg->Page->GetControlByID("spotTimerInitializationScript");
$spotTimerInitializationScript->Content = <<<EOT
<script type="text/javascript">
var spot_end_time = new Date('$suv_end_time');
window.datediff = function(earlierDate, laterDate)
{
var interval = { };
var a = earlierDate, b = laterDate;
console.log(earlierDate);
console.log(laterDate);
const _MS_PER_DAY = 1000 * 60 * 60 * 24;
// Discard the time and time-zone information.
const utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate(), a.getHours(), a.getMinutes(), a.getSeconds());
const utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate(), b.getHours(), b.getMinutes(), b.getSeconds());
console.log(utc2);
console.log(utc1);
var diffMS = (utc2 - utc1);
console.log(diffMS);
var secs = (diffMS / 1000) % 60;
var mins = (diffMS / (1000 * 60)) % 60;
var hrs = (diffMS / (1000 * 60 * 60)) % 24;
interval =
{
"raw": diffMS,
"seconds": Math.floor(secs),
"minutes": Math.floor(mins),
"hours": Math.floor(hrs)
};
return interval;
};
window.spot_timer_tick = function()
{
var spot_cur_time = new Date();
var diff = datediff(spot_cur_time, spot_end_time);
console.log(diff);
document.getElementById('spot_timer').innerHTML = diff.hours + ':' + diff.minutes.toString().padStart(2, '0');
window.setTimeout(spot_timer_tick, 60000);
};
window.addEventListener("load", function()
{
spot_timer_tick();
});
</script>
EOT;
}
}
System::$BeforeGetTenantNameHandler = function()
{
$path = System::GetVirtualPath(false);
if ($path[0] === "madi")
{
return $path[2];
}
return $path[0];
};
function requiresLogin($path)
{
if ($path != null)
{
if ($path == "ots/{tenant}/services/{serviceName}/{version}/{command}")
{
return false;
}
}
/*
if (count($path) > 0)
{
if ($path[0] == "ots"
|| $path[0] == "suv"
|| $path[0] == "ccx")
{
echo("no");
return false;
}
}
*/
return true;
}
System::$BeforeLaunchEventHandler = function($path)
{
/**
* @var MySQLDatabaseOms
*/
$oms = mocha_get_oms();
if (count($path) >= 1)
{
if ($path[0] == "suv")
{
if (count($path) == 2 && ($path[1] == "suvinfo.html" || $path[1] == "phpinfo.html"))
{
//
return true;
}
else
{
header("HTTP/1.1 404 Not Found");
echo ("Not Found");
return false;
}
}
else if ($path[0] == "scripts")
{
return true;
}
}
if (count($path) == 0)
{
System::Redirect("~/" . System::$Configuration["Application.DefaultTenant"], true);
return false;
}
if (count($path) == 1)
{
if ($path[0] != "invalid-url")
{
System::Redirect("~/" . $path[0] . "/d/home.htmld", true);
return false;
}
else
{
return true;
}
}
if ($path[0] == "madi")
{
$currentTenantName = $path[2];
}
else if ($path[0] == "ots")
{
$currentTenantName = $path[1];
}
else
{
$currentTenantName = $path[0];
}
$currentTenant = $oms->getTenantByName($currentTenantName);
if ($currentTenant == null)
{
System::Redirect("~/invalid-url", true);
return false;
}
else
{
$oms->setTenant($currentTenant);
}
$currentUser = null;
if (isset($_SESSION["user_token_" . $currentTenant->ID]))
{
// SELECT FROM `Users` WHERE `Token` = $_SESSION["user_token"]
$insts = $oms->getInstancesByAttributes(array
(
KnownAttributeGuids::Token => $_SESSION["user_token_" . $currentTenant->ID]
));
$currentUser = null;
$loginEntry = null;
if (count($insts) == 1)
{
$loginEntry = $insts[0];
if ($loginEntry != null)
{
$users = $oms->getRelatedInstances($loginEntry, $oms->getInstanceByGlobalIdentifier(KnownRelationshipGuids::User_Login__has__User));
if (count($users) == 1)
{
$currentUser = $users[0];
}
}
if ($currentUser === null)
{
echo("returned null");
die();
}
}
}
$page = null;
$pathVars = null;
System::ParsePathVariablesIntoPage($page, $pathVars);
if ($currentUser === null)
{
if (count($path) >= 4)
{
if ($path[0] == "madi" && $path[1] == "authgwy")
{
return true;
}
}
if ($page != null)
{
if (requiresLogin($page->FileName))
{
echo($page->FileName);
$_SESSION["login_return"] = $path;
System::RedirectToLoginPage(true);
return false;
}
}
}
return true;
};
?>