/*
Jorge - frontend for mod_logdb - ejabberd server-side message archive module.
Copyright (C) 2009 Zbigniew Zolkiewski
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
if (__FILE__==$_SERVER['SCRIPT_FILENAME']) {
header("Location: index.php?act=logout");
exit;
}
// turn on buffering
ob_start();
// send headers
header("content-type: text/html; charset=utf-8");
// error reporting to off
error_reporting(E_NONE);
require_once("func.php"); // functions
require_once("class.sessions.php"); // sessions handling
require_once("class.ejabberd_xmlrpc.php"); // rpc class
require_once("class.db.php"); // db_manager
require_once("class.roster.php"); // roster
require_once("class.helper.php"); // helper
require_once("config.php"); // read configuration
# SSL redirection if set to true in config.
if (SSL_REDIRECT === "true") {
if($_SERVER['HTTPS'] != 'on') {
$url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header('Location: '.$url);
ob_end_flush();
exit;
}
}
$sess = new session;
// Language support. Well thats the hard way...
$language_found = false;
if ($_GET['lng_sw']) {
debug(DEBUG,"Setting language");
$c_language = is_language_supported($_GET['lng_sw'],$language_support,1,true);
if ($c_language !== false) {
setcookie("jorge_language",$c_language,time()+2592000);
$sess->set('language',$c_language);
debug(DEBUG,"Language found, loading file: $c_language.php");
require("lang/".$c_language.".php");
$language_found = true;
}
else{
setcookie("jorge_language",$language_support[default_language][0],time()+2592000);
$sess->set('language',$language_support[default_language][0]);
debug(DEBUG,"Language not found in selection, using defaults");
require('lang/'.$language_support[default_language][0].'.php');
}
}
// get client addr
$rem_adre = $_SERVER['REMOTE_ADDR'];
// something for mac users
$mac_user = get_user_agent($_SERVER);
// location
$location=$_SERVER['PHP_SELF'];
// init html helper
$html = new render_html();
if (!preg_match("/index.php/i",$location)) {
if ($sess->get('vhost') === null) {
header("Location: index.php?act=logout");
}
if (array_key_exists($sess->get('vhost'), $vhosts) === false) {
header("Location: index.php?act=logout");
}
define(XMPP_HOST,$sess->get('vhost'));
$rpc_host = check_rpc_server($vhosts[XMPP_HOST],$rpc_port);
debug(DEBUG,"Active RPC host: $rpc_host");
// in case no RPC servers are available stop jorge
if ($rpc_host===false) {
print "
Currently service is unavailable. Please try again later. Please logout
";
exit;
}
// create rpc object
$ejabberd_rpc = new rpc_connector("$rpc_host","$rpc_port",XMPP_HOST);
$xmpp_host = str_replace(".","_", XMPP_HOST);
}
else{
// check if selected host exist in configuration
if (array_key_exists($_POST['vhost'], $vhosts) === true) {
$rpc_host = check_rpc_server($vhosts[$_POST['vhost']],$rpc_port);
debug(DEBUG,"Selecting RPC server during login: $rpc_host");
if ($rpc_host === false) {
print "
Currently service is unavailable. Please try again later. Please logout
";
exit;
}
else {
define(XMPP_HOST,$_POST['vhost']);
$ejabberd_rpc = new rpc_connector("$rpc_host","$rpc_port",XMPP_HOST);
$xmpp_host = str_replace(".","_", XMPP_HOST);
}
}
else{
unset($_POST['inpLogin']);
unset($_POST['inpPass']);
// Try to recreate db object...
if ($_GET['act'] === "logout") {
if ($sess->get('vhost')!="") {
$xmpp_host = str_replace(".","_", $sess->get('vhost'));
}
}
}
}
// create db_manager object
$db = new db_manager(MYSQL_HOST,MYSQL_NAME,MYSQL_USER,MYSQL_PASS,"mysql","$xmpp_host");
$db->set_debug(SQL_DEBUG);
// create encryption object
$enc = new url_crypt(ENC_KEY);
// username (token)
define(TOKEN,$sess->get('uid_l'));
//debug
debug(DEBUG,"User session:".TOKEN);
// authentication checks. Ensure if session data is not altered... (only when we are inside Jorge)
if (!preg_match("/index.php/i",$location)) {
if (check_registered_user($sess,$ejabberd_rpc,$enc) !== true) {
header("Location: index.php?act=logout");
exit;
}
// Load language file based on current session
debug(DEBUG,"Selecting initial language after authentication");
if ($sess->get('language')) {
// Validate language setting in session
if (is_language_supported($sess->get('language'),$language_support) === true) {
debug(DEBUG,"Language selection ok.");
require('lang/'.$sess->get('language').'.php');
}
else{
debug(DEBUG,"Language in session was altered! Overwritting value...");
require('lang/'.$language_support[default_language][0].'.php');
$sess->set('language',$language_support[default_language][0]);
}
}
// we need user_id but only if we are not in not_enabled mode:
if(!preg_match("/not_enabled.php/i",$_SERVER['PHP_SELF'])) {
$db->get_user_id(TOKEN);
$user_id = $db->result->user_id;
// create user_id instance
$db->set_user_id($user_id);
}
}
// check if user have admin rights
if (in_array(TOKEN, $vhosts_admins[XMPP_HOST]) === true) {
define(ADMIN_NAME,TOKEN);
}
// run only for admins
if (TOKEN === ADMIN_NAME) {
$time_start=getmicrotime();
}
// If language not set or not found in cookie, set default language
if (preg_match("/index.php/i",$location) OR preg_match("/not_enabled.php/i",$location)) {
// Set defaults only if language was not selected
if ($language_found !== true) {
if (is_language_supported($_COOKIE["jorge_language"],$language_support) === true) {
debug(DEBUG,"Setting language according to cookie");
require('lang/'.$_COOKIE["jorge_language"].'.php');
$sess->set('language',$_COOKIE["jorge_language"]);
}
else {
debug(DEBUG,"Language cookie not found, using defaults");
require('lang/'.$language_support[default_language][0].'.php');
$sess->set('language',$language_support[default_language][0]);
}
}
}
// Get language from session
$lang=$sess->get('language');
$html->headers('
');
if (preg_match("/main.php/i",$location)) {
$html->headers('
');
}
$html->headers('
');
// prevent loading includes as long as user is not admin.
if (TOKEN==ADMIN_NAME) {
$html->headers('');
}
$html->headers('
Jorge Beta
');
?>