0
1
Fork 0
hedera-web-mindshore/vn/hedera/web.php

241 lines
4.7 KiB
PHP
Executable File

<?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
{
static $sysConn = NULL;
static $conn = NULL;
static $confIncluded = FALSE;
/**
* Initializes the Hedera web library.
**/
static function init ()
{
if (self::isHttps ())
ini_set ('session.cookie_secure', TRUE);
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']);
self::sysInit ();
$row = self::$sysConn->getRow (
'CALL visit_register (#, #, #, #, #, #, #, #, #)',
[
nullIf ($_COOKIE, 'hedera_visit')
,nullIf ($browser, 'platform')
,nullIf ($browser, 'browser')
,nullIf ($browser, 'version')
,nullIf ($browser, 'javascript')
,nullIf ($browser, 'cookies')
,isset ($agent) ? $agent : NULL
,isset ($ip) && $ip ? $ip : NULL
,nullIf ($_SERVER, 'HTTP_REFERER')
]
);
if (isset ($row['access']))
{
setcookie ('hedera_visit', $row['visit'], time () + 31536000); // 1 Year
$_SESSION['access'] = $row['access'];
}
else
$_SESSION['skipVisit'] = TRUE;
}
/**
* Opens the system database connection.
**/
static function sysInit ()
{
global $conf;
self::includeConf ();
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']
,$conf['db']['port']
);
}
static function isHttps ()
{
return isset ($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on';
}
/**
* Obtains the version of the library.
**/
static function getVersion ()
{
return (int) filectime (__FILE__);
}
/**
* Includes the configuration file.
**/
static function includeConf ()
{
global $conf;
if (self::$confIncluded)
return;
$configDir = _CONFIG_DIR .'/hedera-web';
$customFile = "$configDir/config.my.php";
if (!empty ($_SERVER['SERVER_NAME'])
&& preg_match ('/^[\w\-\.]+$/', $_SERVER['SERVER_NAME']))
{
$hostSplit = explode ('.', $_SERVER['SERVER_NAME']);
$hostFile = "$configDir/config.{$hostSplit[0]}.php";
}
if (isset ($hostFile) && file_exists ($hostFile))
$confFile = $hostFile;
elseif (file_exists ($customFile))
$confFile = $customFile;
else
$confFile = "$configDir/config.php";
$conf = require ($confFile);
self::$confIncluded = TRUE;
}
static function login ()
{
global $conf;
self::includeConf ();
$wasLoged = Auth::isLogged ();
$useCookies = TRUE;
$success = TRUE;
if (isset ($_POST['guest']))
{
self::sysInit ();
$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;
}
}
Auth::getCredentials ();
try {
self::$conn = new Conn ();
self::$conn->open (
$conf['db']['host']
,Auth::getUser ()
,Auth::getPassword ()
,$conf['db']['schema']
,$conf['db']['port']
);
self::$conn->query ('CALL user_session_start (#)', [session_id ()]);
self::$conn->query ('SET @lang = #', [\Vn\Lib\Locale::get ()]);
Auth::login (TRUE, $useCookies);
}
catch (\Exception $e)
{
self::$conn = NULL;
Auth::login (FALSE);
}
// Registering the user access
if (!$wasLoged)
unset ($_SESSION['visitUser']);
if (isset ($_SESSION['access'])
&& !isset ($_SESSION['visitUser']))
{
self::sysInit ();
$_SESSION['visitUser'] = self::$sysConn->getValue (
'CALL visit_user_new (#, #, #)',
[
$_SESSION['access']
,nullIf ($_SESSION, 'visitUser')
,session_id ()
]
);
if (!isset ($_SESSION['visitUnknown']) && !$success)
$_SESSION['visitUnknown'] = $_SESSION['visitUser'];
}
}
static function deinit ()
{
if (self::$conn)
self::$conn->query ('CALL user_session_end ()');
}
static function logout ()
{
$_SESSION['visitUser'] = nullIf ($_SESSION, 'visitUnknown');
Auth::logout ();
if (self::$conn)
{
self::$conn->query (
'DELETE FROM user_session_view '
.'WHERE connection_id = CONNECTION_ID()'
);
self::$conn->close ();
self::$conn = NULL;
}
}
}
?>