Files
2021-02-16 23:07:41 +01:00

255 lines
9.3 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Security.Cryptography;
using System.Drawing.Text;
using System.Runtime.InteropServices;
namespace PokerStarsBotClientv2
{
public partial class LoginForm : Form
{
// Fields
PrivateFontCollection FontAwesomeSolid2 = new PrivateFontCollection();
// Constructor
public LoginForm()
{
InitializeComponent();
LoadCreedentials();
this.Text += " v" + Globals.LocalVersion;
HelperMethods.GetConfigFromDB();
HelperMethods.InitCustomFont(Properties.Resources.Font_Awesome_5_Free_Solid_900, Globals.FontAwesomeSolid);
InitFontAwesomeIcons();
}
// Methods
private void InitFontAwesomeIcons()
{
// User
iconPictureBoxUser.Font = new Font(Globals.FontAwesomeSolid.Families[0], iconPictureBoxUser.Font.Size);
iconPictureBoxUser.Text = "\uf007";
// Lock
iconPictureBoxPassword.Font = new Font(Globals.FontAwesomeSolid.Families[0], iconPictureBoxPassword.Font.Size);
iconPictureBoxPassword.Text = "\uf023";
}
private void InitCustomFont(byte[] font, PrivateFontCollection FontCollection)
{
int fontLength = font.Length;
byte[] fontdata = font; // create a buffer to read in to
System.IntPtr data = Marshal.AllocCoTaskMem(fontLength); // create an unsafe memory block for the font data
Marshal.Copy(fontdata, 0, data, fontLength); // copy the bytes to the unsafe memory block
FontCollection.AddMemoryFont(data, fontLength); // pass the font to the font collection
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
CompareVersions();
}
private void CompareVersions()
{
// Version comparing
var result = Globals.RecentVersion.CompareTo(Globals.LocalVersion);
if (result > 0)
{
DialogResult dialogResult = MessageBox.Show("There is a newer client version available:\n\nYour version: " + Globals.LocalVersion + "\nNewest version: " + Globals.RecentVersion + "\n\nPlease update asap.\nOpen website now?", "Update PkrStarsBot", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
System.Diagnostics.Process.Start(Globals.PkrStarsBotURL);
}
Application.Exit();
}
}
private void checkBoxRememberMe_CheckedChanged(object sender, EventArgs e)
{
// Delete credentials is checkbox is not checked
if (checkBoxRememberMe.Checked == false)
{
Properties.Settings.Default.UserName = string.Empty;
Properties.Settings.Default.Password = string.Empty;
Properties.Settings.Default.Save();
}
}
private void LoadCreedentials()
{
if (Properties.Settings.Default.UserName != string.Empty && Properties.Settings.Default.Password != string.Empty)
{
textBoxUser.Text = Properties.Settings.Default.UserName;
textBoxPassword.Text = Properties.Settings.Default.Password;
checkBoxRememberMe.Checked = true;
EnableDisableLoginButton();
}
}
public void EnableDisableLoginButton()
{
if (string.IsNullOrWhiteSpace(textBoxUser.Text) || string.IsNullOrWhiteSpace(textBoxPassword.Text))
{
btnLogin.Enabled = false;
}
else
{
btnLogin.Enabled = true;
}
}
public string CalculateMD5Hash(string input)
{
// Calculate MD5 hash from input
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
// Convert byte array to hex string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
// Connect to DB
var dbCon = DBConnection.Instance();
if (dbCon.IsConnect())
{
// Username & password
string Username = textBoxUser.Text;
string Password = textBoxPassword.Text;
string PasswordMD5 = CalculateMD5Hash(Password);
string Count1 = "";
// Query
string query = "SELECT count(1) FROM `reg_users` WHERE `EMail` = @UserName AND `Password` = @PassWord;";
// SQL: Check username & password
var cmd = new MySqlCommand(query, dbCon.Connection);
cmd.Parameters.AddWithValue("@UserName", Username);
cmd.Parameters.AddWithValue("@PassWord", PasswordMD5);
var reader = cmd.ExecuteReader();
while (reader.Read())
{
Count1 = reader.GetString(0);
}
reader.Close();
// Login successful
if (Count1 == "1")
{
// Save credentials if checked
if (checkBoxRememberMe.Checked == true)
{
Properties.Settings.Default.UserName = Username;
Properties.Settings.Default.Password = Password;
Properties.Settings.Default.Save();
}
else
{
Properties.Settings.Default.UserName = Username;
Properties.Settings.Default.Save();
}
// Hide Login, show up main
this.Hide();
MainForm MainForm = new MainForm();
MainForm.Show();
}
else if (Count1 == "0") // Login unsuccessful
{
MessageBox.Show("LOGIN FAILED", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// Close DB connection
dbCon.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void textBoxUser_KeyUp(object sender, KeyEventArgs e)
{
EnableDisableLoginButton();
}
private void textBoxPassword_KeyUp(object sender, KeyEventArgs e)
{
EnableDisableLoginButton();
}
private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void z(object sender, EventArgs e)
{
}
}
}
/*namespace Data
{
public class DBConnection
{
// Fields
private string mysqlServer = "zinomedia.de";
private string mysqlUser = "pkrstarsbot";
private string mysqlPassword = "ichpkrstarsbot#1337";
private string mysqlDatabase = "pkrstarsbot";
private MySqlConnection connection = null;
public MySqlConnection Connection
{
get { return connection; }
}
private static DBConnection _instance = null;
public static DBConnection Instance()
{
if (_instance == null)
_instance = new DBConnection();
return _instance;
}
public bool IsConnect()
{
bool result = true;
if (Connection == null)
{
if ((mysqlDatabase ?? mysqlPassword ?? mysqlServer ?? mysqlUser) == null)
{
return false;
}
string connstring = string.Format(@"server={0};user id={1};persistsecurityinfo=True;database={2};password={3}", mysqlServer, mysqlUser, mysqlDatabase, mysqlPassword);
connection = new MySqlConnection(connstring);
connection.Open();
result = true;
}
return result;
}
public void Close()
{
connection.Close();
_instance = null;
}
}
}*/