<?php
/**
 * GeniXCMS - Content Management System.
 *
 * PHP Based Content Management System and Framework
 *
 * @since 0.0.1 build date 20140928
 *
 * @version 1.1.12
 *
 * @link https://github.com/semplon/GeniXCMS
 * 
 *
 * @author Puguh Wijayanto <metalgenix@gmail.com>
 * @copyright 2014-2021 Puguh Wijayanto
 * @license http://www.opensource.org/licenses/mit-license.php MIT
 */

date_default_timezone_set('UTC');

define('GX_PATH', realpath(__DIR__.'/'));
define('GX_LIB', GX_PATH.'/lib/');



require 'autoload.php';
new Session();

require 'config.php';
new Db();


$page = "login";
$data = [];
if (isset($_POST['login'])) {
    /*check if username is exist or not */
    $username = Typo::cleanX(Typo::strip(trim($_POST['username'])));
    $sql = sprintf("SELECT `userid`,`status` FROM `user` WHERE `userid` = '%s'", $username);
    $usr = Db::result($sql);
    $c = Db::$num_rows;
    //echo $c;
    //print_r($usr);
    if ($c == '1') {
        /* get user password */
        $pass = User::randpass($_POST['password']);
        $sql = "SELECT `pass`,`group` FROM `user` WHERE `userid` = '{$username}'";

        $l = Db::result($sql);
        $c = Db::$num_rows;

        foreach ($l as $v) {
            //print_r($v);
            $p = $v->pass;
            $g = $v->group;
        }
        //echo $p;
        if (isset($p)) {
            if ($p == $pass) {
                // session_regenerate_id();
                $vars = array(
                            'username' => $username,
                            'loggedin' => true,
                            'group' => $g,
                        );
                Session::set_session($vars);
                /*
                $_SESSION['username'] = $_POST['username'];
                $_SESSION['login'] = "true";
                $_SESSION['group'] = $group;
                */
                //print_r($_SESSION);
                $data['alertSuccess'][] = "User Loggd In";
            } elseif ($p != $pass) {
                $data['alertDanger'][] = "Password didn't match";
            }
        }
    } elseif ($c == '0') {
        $data['alertDanger'][] = "User Not Found";
    }
}

include("header.php");
echo '<div class="container">';

if( isset($data['alertDanger']) ) {
    foreach($data['alertDanger'] as $k => $v ) {
        echo $v . "<br >";
    }
}
if (!User::isLoggedin()) {
    ?>

    <div style="max-width: 302px; margin-left: auto; margin-right: auto; margin-top: 20px; margin-bottom: 60px ">
    <form class="form-signin" role="form" method="post">
    <h2 class="form-signin-heading">Login</h2>
    <div class="form-group">
        <div class="input-group">
            <div class="input-group-prepend">
                <div class="input-group-text">
                    <i class="fa fa-user"></i>
                </div>
            </div> 
            <input type="text" name="username" class="form-control" placeholder="Username" required autofocus>
        </div>
    </div>
    <div class="form-group">
        <div class="input-group">
            <div class="input-group-prepend">
                <div class="input-group-text">
                    <i class="fa fa-key"></i>
                </div>
            </div> 
            <input type="password" name="password" class="form-control" placeholder="Password" required>
        </div>
    </div>

        <button class="btn btn-lg btn-success btn-block" name="login" type="submit">
            <i class="fa fa-sign-in"></i> Login
        </button>
    </form>

    </div>

<?php
} else {
    $backto = isset($_GET['backto']) ? Typo::cleanX($_GET['backto']): "kelola.php"; 
    header("Location: kelola.php");
}
echo '</div>';

include("footer.php");

?>
