Files
DomFramework/auth.php
2016-12-09 14:47:48 +00:00

209 lines
8.0 KiB
PHP

<?php
/** DomFramework
@package domframework
@author Dominique Fournier <dominique@fournier38.fr> */
/** User authentication (abstract class) */
class auth
{
/** The application name */
public $appName = null;
/** Display the authentication page
* The message is displayed to the user in case of error
* The url is the caller url to go back if authentication is correct
* @param string $baseURL The URL base to use for the links
* @param string|null $message Message to display to the user
* @param string|null $url URL to go back after successful authentication
* @param mixed $alreadyAuth If the user is already authenticated, the value
* will be displayed if the user is coming on the page.
*/
public function pageHTML ($baseURL, $message="", $url="", $alreadyAuth=false)
{
$res = "";
$res .= "<!DOCTYPE html>\n";
$res .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" ";
$res .= " lang=\"en\">\n";
$res .= "<head>\n";
$res .= "<title>".dgettext("domframework", "Sign in")."</title>\n";
$res .= "<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'";
$res .= " />\n";
$res .= " <style type='text/css'>\n";
$res .= "body { padding-top: 40px; padding-bottom: 40px;
font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif;
background-attachment: fixed; background-position: top;
background-image: radial-gradient(ellipse at center -60px,
#9da1ac 0%,#1c202a 100%);
background-color: #eee; }\n";
$res .= ".form-signin { max-width: 430px;padding:15px;margin:0 auto;}\n";
$res .= ".form-signin .form-signin-heading, .form-signin .checkbox {";
$res .= " margin-bottom: 10px; }\n";
$res .= ".form-signin .checkbox { font-weight: normal; }\n";
$res .= ".form-signin .form-control {";
$res .= "position: relative; font-size: 16px; height: auto;";
$res .= "padding: 10px; -webkit-box-sizing: border-box;";
$res .= "-moz-box-sizing: border-box; box-sizing: border-box; }\n";
$res .= ".form-signin .form-control:focus { z-index: 2; }\n";
$res .= ".container { background: white; border-radius:10px;
border-top: none; width:300px;
margin-left: auto; margin-right: auto;
text-align:center;
padding: 20px;}\n";
$res .= "input[type='text'], input[type='password'] {
display:inline-block;
border-radius : 4px;
border: 1px solid #cccccc;
margin: 0px;
height:10px;
padding-top: 10px;
margin-bottom: 20px;
background-color:white;}\n";
$res .= "input[type='submit'], a {
text-decoration: none;
display: inline-block;
padding: 10px 32px;
color: #fff;
background-color: #337ab7;
border-color: #2e6da4;
border: 1px solid #2e6da4;
font-size: 14px;
font-weight: 400;
vertical-align: middle;
border-radius : 4px;
margin-bottom : 20px;
}\n";
$res .= ".alert {
color: #a94442;
background-color: #f2dede;
border: 1px solid #ebccd1;
border-radius: 4px;
padding: 15px;
}\n";
$res .= "h1 { color: #bbb }\n";
$res .= "h2 { padding-top: 0px; padding-bottom: 30px;}\n";
$res .= " </style>\n";
$res .= " </head>\n";
$res .= " <body>\n";
$res .= "<div class='container'>\n";
if ($this->appName !== null)
$res .= "<h1>".$this->appName."</h1>\n";
$res .= " <form class='form-signin' role='form' method='post' ";
$res .= "action='";
if ($url === "/")
$url = "";
if ($url === "")
$res .= "#";
else
$res .= $baseURL."authentication/$url";
$res .= "'>\n";
if ($alreadyAuth === false)
{
$res .= " <h2 class='form-signin-heading'>".dgettext("domframework",
"Please sign in");
$res .= "</h2>\n";
$res .= " <input type='text' class='form-control' name='email' ";
$res .= "placeholder='".dgettext("domframework",
"Email address")."' required autofocus/>\n";
$res .= " <input type='password' class='form-control' name='password'";
$res .= " placeholder='".dgettext("domframework",
"Password")."' required/>\n";
$res .= " <input type='submit' value='".dgettext("domframework",
"Sign in")."'/>\n";
}
else
{
$res .= " <h2 class='form-signin-heading'>".dgettext("domframework",
"Already sign in");
$res .= "</h2>\n";
if (is_string ($alreadyAuth))
{
$res .= "<h3>".dgettext ("domframework", "With login:")."</h3>\n";
$res .= " <h4>$alreadyAuth</h4>\n";
}
$res .= " <p><a href='". $baseURL."authentication/logout'>".
dgettext ("domframework", "Logout")."</a>\n";
if ($url !== "")
$res .= "<a href='$baseURL$url'>".
dgettext ("domframework", "Go back to the calling page").
"</a>\n";
$res .="</p>\n";
}
if ($message !== "" && $message !== null)
$res .= " <div class='alert alert-danger'>$message</div>\n";
$res .= " </form>\n";
$res .= "</div>\n";
$res .= "</body>\n";
$res .= "</html>\n";
return $res;
}
/** Establish the connection to authentication server
*/
public function connect ()
{
throw new Exception (dgettext("domframework",
"No connect to authentication available"),
405);
}
/** Check if the email and password are correct
* Return TRUE if the authentication is correct
* Return an exception if there is a problem
* @param string $email Email to authenticate
* @param string $password Password to authenticate
*/
public function authentication ($email, $password)
{
throw new exception (dgettext("domframework",
"No authentication available"), 405);
}
/** Return all the parameters recorded for the authenticate user
*/
public function getdetails ()
{
throw new exception (dgettext("domframework",
"No getdetails available"), 405);
}
/** Method to change the password
* @param string $oldpassword The old password (to check if the user have the
* rights to change the password)
* @param string $newpassword The new password to be recorded
*/
public function changepassword ($oldpassword, $newpassword)
{
throw new exception (dgettext("domframework",
"No password change available"), 405);
}
/** Method to overwrite the password (without oldpassword check)
* Must be reserved to the administrators. For the users, use changepassword
* method
* @param string $email the user identifier to select
* @param string $newpassword The new password to be recorded
*/
public function overwritepassword ($email, $newpassword)
{
throw new exception (dgettext("domframework",
"No password overwrite available"), 405);
}
/** List all the users available in the database
* Return firstname, lastname, mail, with mail is an array
*/
public function listusers ()
{
throw new exception (dgettext("domframework",
"No List User available"), 405);
}
/** Method to disconnect the authenticated user
*/
public function logout ()
{
throw new exception (dgettext("domframework",
"No logout method available"), 405);
}
}