2015-01-23 13:09:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Vn\Hedera;
|
|
|
|
|
|
|
|
require_once ('vn/hedera/init.php');
|
|
|
|
require_once ('vn/db/db.php');
|
|
|
|
require_once ('vn/web/auth.php');
|
|
|
|
require_once ('vn/web/locale.php');
|
|
|
|
require_once ('vn/hedera/util.php');
|
|
|
|
|
|
|
|
use Vn\Db\Conn;
|
|
|
|
use Vn\Web\Locale;
|
|
|
|
use Vn\Web\Auth;
|
|
|
|
|
|
|
|
class Web
|
|
|
|
{
|
2015-03-06 23:33:54 +00:00
|
|
|
static $sysConn = NULL;
|
2015-02-17 11:48:53 +00:00
|
|
|
static $conn = NULL;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-06 23:33:54 +00:00
|
|
|
/**
|
|
|
|
* Initializes the Hedera web library.
|
|
|
|
**/
|
2015-01-23 13:09:30 +00:00
|
|
|
static function init ()
|
2015-03-06 23:33:54 +00:00
|
|
|
{
|
2015-01-23 13:09:30 +00:00
|
|
|
session_start ();
|
|
|
|
|
|
|
|
// Setting the locale
|
|
|
|
|
|
|
|
Locale::init ();
|
|
|
|
|
|
|
|
// Registering the visit
|
|
|
|
|
|
|
|
if (!isset ($_COOKIE['PHPSESSID'])
|
|
|
|
|| isset ($_SESSION['access'])
|
|
|
|
|| isset ($_SESSION['skipVisit']))
|
|
|
|
return;
|
|
|
|
|
|
|
|
$agent = $_SERVER['HTTP_USER_AGENT'];
|
|
|
|
$browser = get_browser ($agent, TRUE);
|
|
|
|
|
|
|
|
if (isset ($browser['crawler']) && $browser['crawler'])
|
|
|
|
{
|
|
|
|
$_SESSION['skipVisit'] = TRUE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset ($_SERVER['REMOTE_ADDR']))
|
|
|
|
$ip = ip2long ($_SERVER['REMOTE_ADDR']);
|
|
|
|
|
2015-03-06 23:33:54 +00:00
|
|
|
self::sysInit ();
|
|
|
|
|
2015-02-17 11:48:53 +00:00
|
|
|
$row = self::$sysConn->getRow (
|
2015-01-23 13:09:30 +00:00
|
|
|
'CALL visit_register (#, #, #, #, #, #, #, #, #)',
|
|
|
|
[
|
2015-02-17 11:48:53 +00:00
|
|
|
nullIf ($_COOKIE, 'hedera_visit')
|
|
|
|
,nullIf ($browser, 'platform')
|
|
|
|
,nullIf ($browser, 'browser')
|
|
|
|
,nullIf ($browser, 'version')
|
|
|
|
,nullIf ($browser, 'javascript')
|
|
|
|
,nullIf ($browser, 'cookies')
|
2015-01-23 13:09:30 +00:00
|
|
|
,isset ($agent) ? $agent : NULL
|
|
|
|
,isset ($ip) && $ip ? $ip : NULL
|
2015-02-17 11:48:53 +00:00
|
|
|
,nullIf ($_SERVER, 'HTTP_REFERER')
|
2015-01-23 13:09:30 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset ($row['access']))
|
|
|
|
{
|
|
|
|
setcookie ('hedera_visit', $row['visit'], time () + 31536000); // 1 Year
|
|
|
|
$_SESSION['access'] = $row['access'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$_SESSION['skipVisit'] = TRUE;
|
|
|
|
}
|
2015-03-06 23:33:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens the system database connection.
|
|
|
|
**/
|
|
|
|
static function sysInit ()
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-06 23:33:54 +00:00
|
|
|
global $conf;
|
|
|
|
|
|
|
|
if (self::$sysConn)
|
|
|
|
return;
|
|
|
|
|
|
|
|
self::$sysConn = new Conn ();
|
|
|
|
self::$sysConn->open (
|
|
|
|
'p:'. $conf['db']['host']
|
|
|
|
,$conf['db']['user']
|
|
|
|
,base64_decode ($conf['db']['pass'])
|
|
|
|
,$conf['db']['schema']
|
2015-01-23 13:09:30 +00:00
|
|
|
);
|
|
|
|
}
|
2015-03-06 23:33:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains the version of the library.
|
|
|
|
**/
|
|
|
|
static function getVersion ()
|
|
|
|
{
|
|
|
|
return (int) filemtime (__FILE__);
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
static function login ()
|
|
|
|
{
|
|
|
|
global $conf;
|
|
|
|
|
|
|
|
$wasLoged = Auth::isLogged ();
|
|
|
|
$useCookies = TRUE;
|
|
|
|
$success = TRUE;
|
|
|
|
|
|
|
|
if (isset ($_GET['guest']))
|
|
|
|
{
|
|
|
|
$row = self::$sysConn->getRow (
|
|
|
|
'SELECT guest_user, guest_pass FROM config');
|
|
|
|
|
|
|
|
if ($row)
|
|
|
|
{
|
|
|
|
$_POST['user'] = $row['guest_user'];
|
|
|
|
$_POST['password'] = base64_decode ($row['guest_pass']);
|
|
|
|
$useCookies = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Auth::getCredentials ();
|
2015-02-17 11:48:53 +00:00
|
|
|
|
|
|
|
self::$conn = new Conn ();
|
2015-02-08 15:38:38 +00:00
|
|
|
self::$conn->open (
|
|
|
|
$conf['db']['host']
|
2015-01-23 13:09:30 +00:00
|
|
|
,Auth::getUser ()
|
|
|
|
,Auth::getPassword ()
|
|
|
|
,$conf['db']['schema']
|
|
|
|
);
|
|
|
|
|
2015-03-06 23:33:54 +00:00
|
|
|
self::$conn->query ('CALL user_session_start (#)',
|
|
|
|
[session_id()]);
|
|
|
|
|
2015-02-17 11:48:53 +00:00
|
|
|
Auth::login ($useCookies);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
2015-03-06 23:33:54 +00:00
|
|
|
self::$conn = NULL;
|
2015-01-23 13:09:30 +00:00
|
|
|
$success = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Registering the user access
|
|
|
|
|
2015-03-06 23:33:54 +00:00
|
|
|
if ($success && !$wasLoged)
|
|
|
|
unset ($_SESSION['visitUser']);
|
|
|
|
|
|
|
|
if (isset ($_SESSION['access'])
|
|
|
|
&& !isset ($_SESSION['visitUser']))
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-06 23:33:54 +00:00
|
|
|
self::sysInit ();
|
2015-02-17 11:48:53 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
$_SESSION['visitUser'] = self::$sysConn->getValue (
|
2015-03-06 23:33:54 +00:00
|
|
|
'CALL visit_user_new (#, #, #)',
|
2015-01-23 13:09:30 +00:00
|
|
|
[
|
2015-02-17 11:48:53 +00:00
|
|
|
$_SESSION['access']
|
|
|
|
,nullIf ($_SESSION, 'visitUser')
|
2015-01-23 13:09:30 +00:00
|
|
|
,session_id ()
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!isset ($_SESSION['visitUnknown']) && !$success)
|
|
|
|
$_SESSION['visitUnknown'] = $_SESSION['visitUser'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
2015-03-06 23:33:54 +00:00
|
|
|
|
|
|
|
static function deinit ()
|
|
|
|
{
|
|
|
|
if (self::$conn)
|
|
|
|
self::$conn->query ('CALL user_session_end ()');
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
static function logout ()
|
|
|
|
{
|
2015-02-17 11:48:53 +00:00
|
|
|
$_SESSION['visitUser'] = nullIf ($_SESSION, 'visitUnknown');
|
2015-01-23 13:09:30 +00:00
|
|
|
Auth::logout ();
|
2015-02-17 11:48:53 +00:00
|
|
|
|
|
|
|
if (self::$conn)
|
|
|
|
{
|
2015-03-06 23:33:54 +00:00
|
|
|
self::$conn->query (
|
|
|
|
'DELETE FROM user_session_view '
|
|
|
|
.'WHERE connection_id = CONNECTION_ID()'
|
|
|
|
);
|
|
|
|
|
2015-02-17 11:48:53 +00:00
|
|
|
self::$conn->close ();
|
|
|
|
self::$conn = NULL;
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|