<?php
/*******************************************************************************
 *
 * LEIDEN OPEN VARIATION DATABASE (LOVD)
 *
 * Created     : 2006-04-20
 * Modified    : 2007-05-14
 * For LOVD    : 2.0-beta-03
 *
 * Access      : Public
 * Purpose     : Load session data if currently logged in.
 *
 * Copyright   : 2004-2006 Leiden University Medical Center; http://www.LUMC.nl/
 * Programmer  : Ing. Ivo F.A.C. Fokkema <I.F.A.C.Fokkema@LUMC.nl>
 * Last edited : Ing. Ivo F.A.C. Fokkema <I.F.A.C.Fokkema@LUMC.nl>
 *
 *
 * This file is part of LOVD.
 *
 * LOVD 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.
 *
 * LOVD 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 LOVD; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *************/

// Don't allow direct access.
if (!defined('ROOT_PATH')) {
    exit;
}

// If logged in, load account data.
$_SUBS = false;
$_AUTH = false;

if (isset($_SESSION['subs']) && is_array($_SESSION['subs'])) {
    $_SESSION['subs'] = @mysql_fetch_assoc(mysql_query('SELECT * FROM ' . TABLE_SUBS . ' WHERE username = "' . $_SESSION['subs']['username'] . '" AND password = "' . $_SESSION['subs']['password'] . '" AND login_attempts < 3 AND deleted = 0'));
    if (is_array($_SESSION['subs'])) {
        $_SUBS = & $_SESSION['subs'];
        $_AUTH = & $_SUBS;
        $_AUTH['name'] = $_AUTH['firstname'] . ' ' . $_AUTH['lastname'];
        $_AUTH['level'] = 0;
        define('HAS_SUBS', true);
    }

} elseif (isset($_SESSION['auth']) && is_array($_SESSION['auth'])) {
    $_SESSION['auth'] = @mysql_fetch_assoc(mysql_query('SELECT * FROM ' . TABLE_USERS . ' WHERE username = "' . $_SESSION['auth']['username'] . '" AND password = "' . $_SESSION['auth']['password'] . '" AND login_attempts < 3 AND deleted = 0'));
    if (is_array($_SESSION['auth'])) {
        $_AUTH = & $_SESSION['auth'];
        define('HAS_AUTH', true);
        $_SESSION['currdb'] = $_AUTH['current_db'];

        // Load curated DBs.
        $_AUTH['curates'] = array();
        $q = mysql_query('SELECT symbol FROM ' . TABLE_CURATES . ' WHERE userid = "' . $_AUTH['userid'] . '"');
        while ($r = mysql_fetch_row($q)) {
            $_AUTH['curates'][] = $r[0];
        }
    }
}

if (!defined('HAS_AUTH')) {
    define('HAS_AUTH', false);
} if (!defined('HAS_SUBS')) {
    define('HAS_SUBS', false);
}

// IP based blocking.
if (HAS_AUTH && $_AUTH['allowed_ip']) {
    if (!lovd_validateIP($_AUTH['allowed_ip'], $_SERVER['REMOTE_ADDR'])) {
        // Not all of the inc-init code has been executed right now. So we need
        // to fill some variables, to prevent errors thrown out.
        if ($_SESSION['currdb']) {
            define('GENE_COUNT', 1);
        } else {
            define('GENE_COUNT', 0);
        }
        $_SESSION['currdb'] = '';
        // FIXME; this is basically a quick fix. Should we fix this?
        // Log the user out. Careful, because HAS_AUTH is still defined!
        session_destroy();
        $_AUTH = false;

        if (!defined('_INC_TOP_INCLUDED_')) {
            if (is_readable('inc-top.php')) {
                require 'inc-top.php';
            } else {
                require ROOT_PATH . 'inc-top.php';
            }
        }

        lovd_showInfoTable('Your current IP address does not allow you access using this username.', 'stop');

        if (defined('_INC_TOP_INCLUDED_')) {
            if (is_readable('inc-bot.php')) {
                require 'inc-bot.php';
            } else {
                require ROOT_PATH . 'inc-bot.php';
            }
        } elseif (defined('_INC_TOP_CLEAN_INCLUDED_')) {
            if (is_readable('inc-bot-clean.php')) {
                require 'inc-bot-clean.php';
            } else {
                require ROOT_PATH . 'inc-bot-clean.php';
            }
        }
        exit;
    }
}




if (!$_SUBS && !$_AUTH) {
    // We need to check for cookies, so set whatever and check whether it's there later...
    if (!isset($_COOKIE['lovd_cookie_check'])) {
        setcookie('lovd_cookie_check', 'OK');
    }
}
?>
