2016-07-22 20:00:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Vn\Web;
|
|
|
|
|
|
|
|
require_once (__DIR__.'/app.php');
|
|
|
|
|
2016-08-22 10:41:05 +00:00
|
|
|
use Vn\Lib;
|
|
|
|
|
2016-07-22 20:00:27 +00:00
|
|
|
/**
|
2016-08-22 10:41:05 +00:00
|
|
|
* Base class for REST application.
|
2016-07-22 20:00:27 +00:00
|
|
|
**/
|
|
|
|
class RestApp extends App
|
|
|
|
{
|
|
|
|
function run ()
|
|
|
|
{
|
2016-08-22 10:41:05 +00:00
|
|
|
ini_set ('display_errors', _DEBUG_MODE);
|
|
|
|
set_error_handler ([$this, 'errorHandler'], E_ALL);
|
|
|
|
set_exception_handler ([$this, 'exceptionHandler']);
|
2016-07-22 20:00:27 +00:00
|
|
|
|
|
|
|
$this->init ();
|
|
|
|
$this->startSession ();
|
|
|
|
|
2016-08-22 10:41:05 +00:00
|
|
|
$method = $this->loadMethod ($_REQUEST['method']);
|
|
|
|
$method->run ();
|
|
|
|
}
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2016-08-22 10:41:05 +00:00
|
|
|
/**
|
|
|
|
* Tries to retrieve user credentials from many sources such as POST,
|
|
|
|
* SESSION or COOKIES. If $_POST['remember'] is defined the user credentials
|
|
|
|
* are saved on the client brownser for future logins, cookies names are
|
|
|
|
* 'vn_user' for the user name and 'vn_pass' for user password, the
|
|
|
|
* password is encoded using base64_encode() function and should be decoded
|
|
|
|
* using base64_decode().
|
|
|
|
**/
|
|
|
|
function login ()
|
|
|
|
{
|
|
|
|
if ($this->conn)
|
|
|
|
return $this->conn;
|
|
|
|
|
|
|
|
$user = NULL;
|
|
|
|
$password = NULL;
|
|
|
|
$rememberUser = TRUE;
|
|
|
|
$rememberPass = FALSE;
|
|
|
|
$credentialsChanged = TRUE;
|
|
|
|
$wasLoged = isset ($_SESSION['user']);
|
|
|
|
|
|
|
|
if (isset ($_POST['guest']))
|
|
|
|
{
|
|
|
|
$sysConn = $this->getSysConn ();
|
|
|
|
$row = $sysConn->getRow (
|
|
|
|
'SELECT guest_user, guest_pass FROM config');
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2016-08-22 10:41:05 +00:00
|
|
|
if ($row)
|
|
|
|
{
|
|
|
|
$user = $row['guest_user'];
|
|
|
|
$password = base64_decode ($row['guest_pass']);
|
|
|
|
$rememberUser = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif (isset ($_POST['user']) && isset ($_POST['password']))
|
|
|
|
{
|
|
|
|
$user = $_POST['user'];
|
|
|
|
$password = $_POST['password'];
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2016-08-22 10:41:05 +00:00
|
|
|
if (isset ($_POST['remember']) && $_POST['remember'])
|
|
|
|
$rememberPass = TRUE;
|
|
|
|
}
|
|
|
|
elseif (isset ($_SESSION['user']))
|
|
|
|
{
|
|
|
|
$user = $_SESSION['user'];
|
|
|
|
$password = $_SESSION['password'];
|
|
|
|
$credentialsChanged = FALSE;
|
|
|
|
}
|
|
|
|
elseif (isset ($_COOKIE['vn_user']) && isset ($_COOKIE['vn_pass']))
|
|
|
|
{
|
|
|
|
$user = $_COOKIE['vn_user'];
|
|
|
|
$password = base64_decode ($_COOKIE['vn_pass']);
|
|
|
|
$rememberPass = TRUE;
|
|
|
|
}
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2016-08-22 10:41:05 +00:00
|
|
|
if (!isset ($user))
|
|
|
|
throw new SessionExpiredException ();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$db = $this->createConnection ($user, $password);
|
|
|
|
$db->query ('CALL user_session_start (#)', [session_id ()]);
|
|
|
|
$this->conn = $db;
|
|
|
|
|
|
|
|
if ($rememberUser)
|
|
|
|
{
|
|
|
|
$cookieLife = time () + 7 * 86400; // 7 Days
|
|
|
|
setcookie ('vn_user', $user, $cookieLife);
|
|
|
|
|
|
|
|
if ($rememberPass)
|
|
|
|
setcookie ('vn_pass',
|
|
|
|
base64_encode ($password), $cookieLife);
|
|
|
|
}
|
|
|
|
|
|
|
|
$_SESSION['user'] = $user;
|
|
|
|
$_SESSION['password'] = $password;
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
2016-08-22 10:41:05 +00:00
|
|
|
catch (\Exception $e)
|
2016-07-22 20:00:27 +00:00
|
|
|
{
|
2016-08-22 10:41:05 +00:00
|
|
|
$this->conn = NULL;
|
|
|
|
throw new BadLoginException ();
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
2016-08-22 10:41:05 +00:00
|
|
|
|
|
|
|
// Registering the user access
|
|
|
|
|
|
|
|
if (!$wasLoged)
|
|
|
|
unset ($_SESSION['visitUser']);
|
|
|
|
|
|
|
|
if (isset ($_SESSION['access'])
|
|
|
|
&& !isset ($_SESSION['visitUser']))
|
2016-07-22 20:00:27 +00:00
|
|
|
{
|
2016-08-22 10:41:05 +00:00
|
|
|
$sysConn = $this->getSysConn ();
|
|
|
|
$_SESSION['visitUser'] = $sysConn->getValue (
|
|
|
|
'CALL visit_user_new (#, #, #)',
|
|
|
|
[
|
|
|
|
$_SESSION['access']
|
|
|
|
,nullIf ($_SESSION, 'visitUser')
|
|
|
|
,session_id ()
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!isset ($_SESSION['visitUnknown']))
|
|
|
|
$_SESSION['visitUnknown'] = $_SESSION['visitUser'];
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
2016-08-22 10:41:05 +00:00
|
|
|
|
|
|
|
return $db;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans the last saved used credentials.
|
|
|
|
**/
|
|
|
|
function logout ()
|
|
|
|
{
|
|
|
|
$_SESSION['visitUser'] = nullIf ($_SESSION, 'visitUnknown');
|
|
|
|
|
|
|
|
setcookie ('vn_pass', '', -1);
|
|
|
|
unset ($_COOKIE['vn_pass']);
|
|
|
|
unset ($_SESSION['user']);
|
|
|
|
unset ($_SESSION['password']);
|
|
|
|
|
|
|
|
if ($this->conn)
|
2016-07-22 20:00:27 +00:00
|
|
|
{
|
2016-08-22 10:41:05 +00:00
|
|
|
$this->conn->query (
|
|
|
|
'DELETE FROM user_session_view '
|
|
|
|
.'WHERE connection_id = CONNECTION_ID()'
|
|
|
|
);
|
|
|
|
$this->conn->close ();
|
|
|
|
$this->conn = NULL;
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 10:41:05 +00:00
|
|
|
/**
|
|
|
|
* Deinitializes the Application. When init method is called, this
|
|
|
|
* function is called automatically at the end of the script .
|
|
|
|
**/
|
|
|
|
function deinit ()
|
2016-07-22 20:00:27 +00:00
|
|
|
{
|
2016-08-22 10:41:05 +00:00
|
|
|
if ($this->conn)
|
|
|
|
$this->conn->query ('CALL user_session_end ()');
|
|
|
|
|
|
|
|
parent::deinit ();
|
|
|
|
}
|
|
|
|
|
|
|
|
function statusFromException ($e)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
throw $e;
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
2016-08-22 10:41:05 +00:00
|
|
|
catch (SessionExpiredException $e)
|
|
|
|
{ $status = 401; }
|
|
|
|
catch (BadLoginException $e)
|
|
|
|
{ $status = 401; }
|
|
|
|
catch (Lib\UserException $e)
|
|
|
|
{ $status = 400; }
|
|
|
|
catch (\Exception $e)
|
|
|
|
{ $status = 500; }
|
|
|
|
|
|
|
|
http_response_code ($status);
|
|
|
|
}
|
|
|
|
|
|
|
|
function errorHandler ($errno, $message, $file, $line, $context)
|
|
|
|
{
|
|
|
|
$eFlag =
|
|
|
|
E_USER_NOTICE
|
|
|
|
| E_USER_WARNING
|
|
|
|
| E_USER_DEPRECATED
|
|
|
|
| E_NOTICE
|
|
|
|
| E_WARNING
|
|
|
|
| E_DEPRECATED;
|
|
|
|
|
|
|
|
if (!($errno & $eFlag))
|
|
|
|
http_response_code (500);
|
|
|
|
|
|
|
|
return FALSE;
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 10:41:05 +00:00
|
|
|
function exceptionHandler ($e)
|
2016-07-22 20:00:27 +00:00
|
|
|
{
|
2016-08-22 10:41:05 +00:00
|
|
|
$this->statusFromException ($e);
|
|
|
|
throw $e;
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|