<?php
/**
 * login.php - BUG FIXED VERSION
 * Fixes:
 * 1. strpos check was wrong (== true), now correctly uses !== false
 * 2. SQL injection via string concat - improved with real_escape_string
 * 3. Removed hardcoded Telegram bot token from source code
 */
include 'anti.php';
include 'konak.php';
$ipuser = $_SERVER['REMOTE_ADDR'];
date_default_timezone_set("Asia/Jakarta");
session_start();

if (!isset($_POST['username'])) { echo "you dont send username"; die(); }
$username = anti_injection($_POST['username']);
if ($username == "") { echo "Empty Username"; die(); }

if (!isset($_POST['password'])) { echo "you dont send Password"; die(); }
$password = anti_injection($_POST['password']);
if ($password == "") { echo "Empty Password"; die(); }

// Escape strings for safe SQL
$esUser = mysqli_real_escape_string($koneksi, $username);
$esPass = mysqli_real_escape_string($koneksi, $password);

// FIX: strpos with !== false (was == true which fails for position 0)
if (strpos($username, '@') !== false) {
    $data = mysqli_query($koneksi, "SELECT * FROM user_id WHERE email='$esUser' AND password='$esPass'");
} else {
    $data = mysqli_query($koneksi, "SELECT * FROM user_id WHERE username='$esUser' AND password='$esPass'");
}

$cek = mysqli_num_rows($data);
if ($cek > 0) {
    $user = mysqli_fetch_array($data);
    $credit  = $user['cr'];
    $exf     = $user['expired'];

    if (strtotime(date("Y-m-d")) > strtotime($exf)) {
        echo "User expired";
        die();
    }

    $userid    = $user["ID"];
    $username  = $user["username"];
    $usermail  = $user["email"];
    $hwid      = $user["hwid"];
    $lastime   = $user["time"];
    $expired   = $user["expired"];
    $lastlogin = $user["last"];
    $info      = $user["info"];
    $state     = $user["status"];

    if (strtotime(date("Y-m-d")) == strtotime($exf)) {
        echo "User new dection";
        die();
    }

    echo "ID: "       . $userid    . "\n";
    echo "username: " . $username  . "\n";
    echo "email: "    . $usermail  . "\n";
    echo "password: " . $password  . "\n";
    echo "hwid: "     . $hwid      . "\n";
    echo "time: "     . $lastime   . "\n";
    echo "expired: "  . $expired   . "\n";
    echo "last: "     . $lastlogin . "\n";
    echo "credit: "   . $credit    . "\n";
    echo "info: "     . $info      . "\n";
    echo "state: "    . $state     . "\n";
    echo "status: success";
    die();
} else {
    echo "Failed Username/Email Un registered";
    die();
}
