<?php
/**
 * /config/config.php
 * @version 1.0
 * @desc configuration
 * @author Fándly Gergő Zoltán (gergo@systemtest.tk, systemtest.tk)
 * @copy 2018 Fándly Gergő Zoltán
 * License:
    Systemtest.tk website's.
    Copyright (C) 2018  Fándly Gergő Zoltán

    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 3 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, see <https://www.gnu.org/licenses/>.
 **/


const VERSION="0.5";

/*
 * Includes
 */
require_once("lib/loginManager/loginManager.php");
require_once("lib/PasswordStorage.php");
require_once("lib/functions.php");

$config=parse_ini_file("config.ini", true);

/*
 * Regionals
 */
date_default_timezone_set($config['general']['timezone']);
mb_internal_encoding("UTF-8");

/*
 * Language files
 */
$langstr="";
if(isset($_GET['setlang'])){
    $langstr=$_GET['setlang'];
    setcookie("language", $langstr, time() + 90*86000);
}
else if(isset($_COOKIE['language'])){
    $langstr=$_COOKIE['language'];
}
else{
    $langstr=$config['language']['default'];
}
if(!in_array($langstr, $config['language']['available'])){
    $langstr=$config['language']['default'];
}
$langcode="";
if($langstr=="en_US"){
    $langcode="eng";
}
else if($langstr=="hu_HU"){
    $langcode="hun";
}
else if($langstr=="ro_RO"){
    $langcode="rou";
}
$lang=parse_ini_file("lang/".$langstr.".ini", false);

/*
 * DB setup
 */
$db=new PDO($config['database']['type'].":host=".$config['database']['host'].";dbname=".$config['database']['name'].";charset=utf8", $config['database']['user'], $config['database']['password']);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);

/*
 * UTF8 BOM
 */
$BOM=chr(239).chr(187).chr(191);

/*
 * DEBUG
 */
if($config['general']['debug']){
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    ini_set("display_errors", true);
}

/*
 * Login manager
 */
class handler implements lmHandler{
    public function handle($state, $target=0){
        global $db;
        switch($state){
            case lmStates::LOGIN_FAILED:
                functions::setError(1);
                functions::safeReload();
                break;
            case lmStates::LOGIN_OK:
                $sql=$db->prepare("SELECT id, username, fullname, email, accesslevel, quota, orderer FROM users WHERE id=:id");
                $sql->execute(array(":id"=>$target));
                $res=$sql->fetch(PDO::FETCH_ASSOC);
                $_SESSION['id']=$res['id'];
                $_SESSION['username']=$res['username'];
                $_SESSION['fullname']=$res['fullname'];
                $_SESSION['email']=$res['email'];
                $_SESSION['accesslevel']=$res['accesslevel'];
                $_SESSION['quota']=$res['quota'];
                $_SESSION['orderer']=$res['orderer'];
                header("Location: /userarea");
                break;
            case lmStates::CAPTCHA_FAILED:
                functions::setError(2);
                functions::safeReload();
                break;
            case lmStates::BANNED:
                functions::setError(3);
                functions::safeReload();
                break;
            case lmStates::FORGET_DONE:
                functions::setMessage(1);
                functions::safeReload();
                break;
            case lmStates::LOGOUT_DONE:
                functions::setMessage(2);
                functions::safeReload();
                break;
        }
    }
}
class password implements lmPassword{
    public function verifyPassword($cleartext, $database){
        if($database==""){
            return false;
        }
        return PasswordStorage::verify_password($cleartext, $database);
    }
}
class twoFactor implements lmTwoFactor{
    public function secondFactor($uid){
        return true;
    }
}

$lm=new loginManager(new lmConfig($db, $config['login']['session_lifetime'], $config['login']['captcha_enable'], $config['login']['captcha_after'], $config['login']['captcha_sitekey'], $config['login']['captcha_secretkey'], $config['login']['ban_enable'], $config['login']['ban_after'], $config['login']['ban_time'], $config['login']['look'], $config['login']['remember_enable'], $config['login']['remember_time'], lmStates::AUTH_UNAME), new handler(), new password(), new twoFactor());
$lm->init();
