hedera-web/vn/web/service.php

143 lines
3.1 KiB
PHP
Raw Normal View History

<?php
namespace Vn\Web;
require_once ('vn/lib/app.php');
2016-09-19 06:40:18 +00:00
require_once (__DIR__.'/db-session-handler.php');
use Vn\Lib;
use Vn\Lib\Locale;
use Vn\Db\Conn;
/**
* Thrown when user credentials could not be fetched.
**/
class SessionExpiredException extends Lib\UserException {}
/**
* Thrown when user credentials are invalid.
**/
class BadLoginException extends Lib\UserException {}
/**
* Thrown when client version is outdated.
**/
class OutdatedVersionException extends Lib\UserException {}
/**
* Main class for web applications.
**/
abstract class Service
{
protected $app;
function __construct ($app)
{
$this->app = $app;
}
/**
* Starts the user session.
**/
function startSession ()
{
2016-09-19 06:40:18 +00:00
$db = $this->app->getSysConn ();
ini_set ('session.cookie_secure', TRUE);
ini_set ('session.use_only_cookies', FALSE);
ini_set ('session.cookie_path', 'cookies');
ini_set ('session.hash_function', 'sha512');
2016-09-19 06:40:18 +00:00
session_set_save_handler (new DbSessionHandler ($db));
session_start ();
2016-09-19 06:40:18 +00:00
// Setting the locale
if (isset ($_SERVER['HTTP_ACCEPT_LANGUAGE']))
2016-09-19 06:40:18 +00:00
if (!isset ($_SESSION['httpLanguage'])
|| $_SESSION['httpLanguage'] != $_SERVER['HTTP_ACCEPT_LANGUAGE'])
{
2016-09-19 06:40:18 +00:00
$_SESSION['httpLanguage'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$regexp = '/([a-z]{1,4})(?:-[a-z]{1,4})?\s*(?:;\s*q\s*=\s*(?:1|0\.[0-9]+))?,?/i';
preg_match_all ($regexp, $_SERVER['HTTP_ACCEPT_LANGUAGE'], $languages);
foreach ($languages[1] as $lang)
if (stream_resolve_include_path ("locale/$lang"))
{
$_SESSION['lang'] = $lang;
break;
}
}
if (!isset ($_SESSION['lang']))
$_SESSION['lang'] = NULL;
Locale::set ($_SESSION['lang']);
// 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']);
$row = $db->getRow (
2016-09-19 06:40:18 +00:00
'CALL visitRegister (#, #, #, #, #, #, #, #, #)',
[
2016-09-19 06:40:18 +00:00
nullIf ($_COOKIE, 'vnVisit')
,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']))
{
2016-09-19 06:40:18 +00:00
setcookie ('vnVisit', $row['visit'], time () + 31536000); // 1 Year
$_SESSION['access'] = $row['access'];
}
else
$_SESSION['skipVisit'] = TRUE;
}
/**
* Checks if the HTTP connection is secure.
*
* @return boolean Return %TRUE if its secure, %FALSE otherwise
**/
function isHttps ()
{
return isset ($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on';
}
/**
* Obtains the application version number. It is based on de last
* modification date of the main script.
**/
function getVersion ()
{
return (int) strftime ('%G%m%d%H%M%S',
2016-08-30 07:43:47 +00:00
filectime ($_SERVER['SCRIPT_FILENAME']));
}
}
?>