<?phpnamespaceROF;
classMessageForm{
//OBJECT variablesstaticprotected$beganSession = FALSE;
staticprotected$sessionKeys = array("title"=>"ro-login-title",
"message" => "ro-login-message",
"links" => "ro-login-links",
"time" => "ro-login-message-time");
staticpublicfunctiondisplaySession(){
$active = self::getCurrent();
$active->display();
}
//STATIC functions//staticpublicfunctiongetCurrent(){
self::startSession();
$validSession = TRUE;
foreach (self::$sessionKeysas$name=>$key){
if (!isset($_SESSION[$key])){
$validSession = FALSE;
}
}
if (!$validSession){
$message = new MessageForm("Session Error",
"There is no active session, so a message cannot be displayed. "
);
}
if (time()-5*60>$_SESSION[self::getKey('time')]){
$message = new MessageForm("Session Ended",
"There may have been an error message previously, but it is no longer available. "
);
return$message;
}
$message = new MessageForm($_SESSION[self::getKey('title')],
$_SESSION[self::getKey('message')],
$_SESSION[self::getKey('links')]
);
return$message;
}
staticprivatefunctionstartSession(){
if (session_status()===PHP_SESSION_DISABLED){
thrownew \Exception("Session could not be started due to PHP settings, so messages cannot be displayed.");
} elseif (session_status()===PHP_SESSION_NONE){
$started = session_start();
if (!$started)thrownew \Exception("Session failed to start so message could not be displayed.");
self::$beganSession = TRUE;
}
$_SESSION[self::getKey('time')] = time();
}
staticprivatefunctionendSession(){
self::startSession();
foreach (self::$sessionKeysas$key=>$value){
if (isset($_SESSION[$value])){
unset($_SESSION[$value]);
}
}
if (self::$beganSession)session_destroy();
}
staticprivatefunctiongetKey($keyName){
if (!isset(self::$sessionKeys[$keyName])){
thrownew \Exception("Key '{$keyName}' is not a valid session variable for RO-User library.");
}
returnself::$sessionKeys[$keyName];
}
/**
* $title and $message are strings
* $links is an associative array where the key is the title for the user and the value is the url that it directs the user to.
*/publicfunction__construct($title,$message,$links=[]){
//INTEGRITY check//if (!is_array($links))thrownew \Exception("The 3rd value, '\$links', must be an array with key set to the user-friendly "
."title and value set to the url the link goes to.");
self::startSession();
$_SESSION[self::getKey('title')] = $title;
$_SESSION[self::getKey('links')] = $links;
$_SESSION[self::getKey('message')] = $message;
}
//OBJECT, PROTECTED methods//protectedfunctiongetTitle(){
return$_SESSION[self::getKey('title')];
}
protectedfunctiongetMessage(){
return$_SESSION[self::getKey('message')];
}
protectedfunctiongetLinks(){
return$_SESSION[self::getKey('links')];
}
protectedfunctiongetLinksHtml(){
$links = $this->getLinks();
$html = "";
foreach ($linksas$name=>$url){
$html .= "<a href=\"{$url}\">{$name}</a><br>";
}
return$html;
}
publicfunctiondisplay(){
?>
<h1><?=$this->getTitle()?></h1>
<p>
<?=$this->getMessage()?>
</p>
<p>
<?=$this->getLinksHtml()?>
</p><?phpself::endSession();
}
publicfunctionredirect($messageFormUrl='/message.php'){
if ($messageFormUrl=='/message.php'){
$protocol = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on' || $_SERVER['HTTPS'] === 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ? 'https' : 'http';
$domain_name = $_SERVER['HTTP_HOST'];
$site_address = $protocol."://".$domain_name;
$messageFormUrl = $site_address.$messageFormUrl;
}
header("Location: ".$messageFormUrl);
exit;
}
}