Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using MySql.Data.MySqlClient;
  10. using System.Security.Cryptography;
  11.  
  12. namespace registration
  13. {
  14.     public partial class registration_panel : Form
  15.     {
  16.         public registration_panel()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void registration_button_Click(object sender, EventArgs e)
  22.         {
  23.             if (username_box.Text == "" ||
  24.                 password_box.Text == "" ||
  25.                 repassword_box.Text == "" ||
  26.                 email_box.Text == "")
  27.             {
  28.                 username_box.Text = "Kérem, töltse ki a mezőket!";
  29.             }
  30.             else
  31.             {
  32.                 if (username_box.Text == "Kérem, töltse ki a mezőket!!" ||
  33.                     username_box.Text == "A művelet sikeres volt!" ||
  34.                     username_box.Text == "A művelet nem elvégezhető!" ||
  35.                     username_box.Text == "Már létező név!" ||
  36.                     username_box.Text == "Túl rövid/hosszú  felhasználónév!" ||
  37.                     password_box.Text == "Túl rövid/hosszú  a jelszó!" ||
  38.                     repassword_box.Text == "Nem egyezik a jelszóval!" ||
  39.                     email_box.Text == "Már létező email!" ||
  40.                     email_box.Text == "Túl rövid/hosszú  e-mail cím!" ||
  41.                     email_box.Text == "Helytelen e-mail cím!")
  42.                 {
  43.                     username_box.Text = "";
  44.                     password_box.Text = "";
  45.                     repassword_box.Text = "";
  46.                     email_box.Text = "";
  47.                 }
  48.                 else
  49.                 {
  50.                     string_muvelet vizsgal = new string_muvelet();
  51.                     keres keres = new keres();
  52.                     hash hash = new hash();
  53.                     connection connect = new connection();
  54.                     MySqlCommand insert = new MySqlCommand();
  55.  
  56.                     if (keres.username_keres(username_box.Text) && keres.email_keres(email_box.Text))
  57.                     {
  58.                         if (vizsgal.string_meret(username_box.Text) > 6 && vizsgal.string_meret(username_box.Text) < 32)
  59.                         {
  60.                             if (vizsgal.string_tartalmaz(email_box.Text, '@'))
  61.                             {
  62.                                 if (vizsgal.string_meret(email_box.Text) > 6 && vizsgal.string_meret(email_box.Text) < 40)
  63.                                 {
  64.                                     if (password_box.Text == repassword_box.Text)
  65.                                     {
  66.                                         string username = username_box.Text;
  67.                                         string email = email_box.Text;
  68.                                         string password = hash.sha_pass_hash(username,password_box.Text);
  69.                                         if (vizsgal.string_meret(password_box.Text) > 6 && vizsgal.string_meret(password) < 60)
  70.                                         {
  71.                                             /*FŐRÉSZ - eleje*/
  72.  
  73.                                             int expansion = 2;
  74.                                             if (check_wotlk.Checked)
  75.                                             {
  76.                                                 expansion = 2;
  77.                                             }
  78.                                             else
  79.                                             {
  80.                                                 if (check_tbc.Checked)
  81.                                                 {
  82.                                                     expansion = 1;
  83.                                                 }
  84.                                                 else if(check_classic.Checked)
  85.                                                 {
  86.                                                     expansion = 0;
  87.                                                 }
  88.                                             }
  89.  
  90.                                             connect.connection_open_realmd();
  91.  
  92.                                             try
  93.                                             {
  94.                                                 insert.Connection = connect.connection_realmd;
  95.                                                 insert.CommandText = "INSERT INTO account (username,sha_pass_hash,email,expansion) VALUES (?UN,?SP,?EM,?EX)";
  96.                                                 insert.Parameters.Add(new MySqlParameter("UN", username));
  97.                                                 insert.Parameters.Add(new MySqlParameter("SP", password));
  98.                                                 insert.Parameters.Add(new MySqlParameter("EM", email));
  99.                                                 insert.Parameters.Add(new MySqlParameter("EX", expansion));
  100.                                                 insert.ExecuteNonQuery();
  101.                                                 connect.connection_close_realmd();
  102.  
  103.                                                 username_box.Text = "A művelet sikeres volt!";
  104.                                                 password_box.Text = "";
  105.                                                 repassword_box.Text = "";
  106.                                                 email_box.Text = "";
  107.                                             }
  108.                                             catch
  109.                                             {
  110.                                                 username_box.Text = "A művelet nem elvégezhető!";
  111.                                             }
  112.  
  113.                                             /*FŐRÉSZ - vége*/
  114.                                         }
  115.                                         else
  116.                                         {
  117.                                             password_box.Text = "Túl rövid/hosszú  a jelszó!";
  118.                                         }
  119.                                     }
  120.                                     else
  121.                                     {
  122.                                         repassword_box.Text = "Nem egyezik a jelszóval!";
  123.                                     }
  124.                                 }
  125.                                 else
  126.                                 {
  127.                                     email_box.Text = "Túl rövid/hosszú  e-mail cím!";
  128.                                 }
  129.                             }
  130.                             else
  131.                             {
  132.                                 email_box.Text = "Helytelen e-mail cím!";
  133.                             }
  134.                         }
  135.                         else
  136.                         {
  137.                             username_box.Text = "Túl rövid/hosszú  felhasználónév!";
  138.                         }
  139.                     }
  140.                     else
  141.                     {
  142.                         username_box.Text = "Már létező név!";
  143.                         email_box.Text = "Már létező e-mail cím!";
  144.                     }
  145.                 }
  146.             }
  147.         }
  148.     }
  149.  
  150.     /* Osztályok */
  151.  
  152.     public class string_muvelet
  153.     {
  154.         /* Stringekkel kapcsolatos műveletek osztálya */
  155.  
  156.         /* Metódusai: */
  157.         /* string_meret(string)       - Megmondja hogy hány karakterből áll a string */
  158.         /* string_tartalmaz(string, karakter)   - Megvizsgálja, hogy tartalmaz-e valamilyen karaktert a string */
  159.  
  160.         public int string_meret(string str)
  161.         {
  162.             int counter = 0;
  163.             for (int i = 0; i < str.Length; i++)
  164.             {
  165.                 char chr = str[i];
  166.                 counter++;
  167.             }
  168.             return counter;
  169.         }
  170.  
  171.         public bool string_tartalmaz(string str, char karakter)
  172.         {
  173.             bool success = false;
  174.             for (int i = 0; i < str.Length; i++)
  175.             {
  176.                 char chr = str[i];
  177.                 if (chr == karakter)
  178.                 {
  179.                     success = true;
  180.                     break;
  181.                 }
  182.             }
  183.  
  184.             if (success)
  185.             {
  186.                 return true;
  187.             }
  188.             else
  189.             {
  190.                 return false;
  191.             }
  192.         }
  193.     }
  194.  
  195.     public class hash
  196.     {
  197.         /* Hashelésért felelős osztály */
  198.  
  199.         /* Metódusai: */
  200.         /* sha_pass_hash(felhasználónév, jelszó)    - A felhasználónév és a jelszót egybeolvasztva haseli*/
  201.  
  202.         public string sha_pass_hash(string user, string pass)
  203.         {
  204.             string password = user.ToUpper() + ":" + pass.ToUpper();
  205.             Encoding enc = Encoding.UTF8;
  206.             byte[] password_bytes = enc.GetBytes(password);
  207.             SHA1CryptoServiceProvider password_hash = new SHA1CryptoServiceProvider();
  208.             password = BitConverter.ToString(password_hash.ComputeHash(password_bytes)).Replace("-", "");
  209.             return password;
  210.         }
  211.     }
  212.  
  213.     public class keres
  214.     {
  215.         /* Az adatbázisban való keresésért felelős osztály */
  216.  
  217.         /* Metódusai: */
  218.         /* username_keres(felhasználó)                  - Megnézi hogy létezik-e már a felhasználó */
  219.         /* email_keres(email)                           - Megnézi hogy ltéezik-e már az email */
  220.         /* common_keres(mit, honnan, hol, holparaméter) - Az általunk megadott paraméterek alapján bármit megnéz, hogy létezik-e */
  221.  
  222.         public bool username_keres(string user)
  223.         {
  224.             connection connect = new connection();
  225.             connect.connection_open_realmd();
  226.  
  227.             MySqlCommand username_keres = new MySqlCommand();
  228.             username_keres.Connection = connect.connection_realmd;
  229.             username_keres.CommandText = "SELECT id FROM account WHERE username=?UN";
  230.             username_keres.Parameters.Add(new MySqlParameter("UN", user));
  231.             MySqlDataReader reader = username_keres.ExecuteReader();
  232.  
  233.             if (reader.Read())
  234.             {
  235.                 connect.connection_close_realmd();
  236.                 return false;
  237.             }
  238.             else
  239.             {
  240.                 connect.connection_close_realmd();
  241.                 return true;
  242.             }
  243.         }
  244.  
  245.         public bool email_keres(string email)
  246.         {
  247.             connection connect = new connection();
  248.             connect.connection_open_realmd();
  249.  
  250.             MySqlCommand email_keres = new MySqlCommand();
  251.             email_keres.Connection = connect.connection_realmd;
  252.             email_keres.CommandText = "SELECT id FROM account WHERE username=?EM";
  253.             email_keres.Parameters.Add(new MySqlParameter("EM", email));
  254.             MySqlDataReader reader = email_keres.ExecuteReader();
  255.  
  256.             if (reader.Read())
  257.             {
  258.                 connect.connection_close_realmd();
  259.                 return false;
  260.             }
  261.             else
  262.             {
  263.                 connect.connection_close_realmd();
  264.                 return true;
  265.             }
  266.         }
  267.  
  268.         public bool common_keres(string mit, string honnan, string hol, string holp)
  269.         {
  270.             connection connect = new connection();
  271.             connect.connection_open_realmd();
  272.  
  273.             MySqlCommand common_keres = new MySqlCommand();
  274.             common_keres.Connection = connect.connection_realmd;
  275.             common_keres.CommandText = "SELECT ?MI FROM ?HN WHERE ?HH=?HO";
  276.             common_keres.Parameters.Add(new MySqlParameter("MI", mit));
  277.             common_keres.Parameters.Add(new MySqlParameter("HN", honnan));
  278.             common_keres.Parameters.Add(new MySqlParameter("HH", hol));
  279.             common_keres.Parameters.Add(new MySqlParameter("HO", holp));
  280.             MySqlDataReader reader = common_keres.ExecuteReader();
  281.  
  282.             if (reader.Read())
  283.             {
  284.                 connect.connection_close_realmd();
  285.                 return false;
  286.             }
  287.             else
  288.             {
  289.                 connect.connection_close_realmd();
  290.                 return true;
  291.             }
  292.         }
  293.     }
  294.  
  295.     public class connection
  296.     {
  297.         /* A kapcsolatteremtésért felelős osztály */
  298.  
  299.         /* Metódusai: */
  300.         /* connection_open_realmd()      - Realmd adatbázis megnyitása */
  301.         /* connection_close_realmd()     - Realmd adatbázis bezárása*/
  302.         /* connection_open_characters()  - Chacaters adatbázis megnyitása */
  303.         /* connection_close_characters() - Characters adatbázis bezárása */
  304.  
  305.         static bool connection_check_realmd = false;
  306.         static bool connection_check_characters = false;
  307.  
  308.         static string connection_info_realmd =
  309.         "SERVER=localhost;" +           // Szerver elérése
  310.         "DATABASE=realmd;" +            // Adatbázis neve
  311.         "UID=root;" +                   // Felhasználónév
  312.         "PASSWORD=root;";               // Jelszó
  313.  
  314.         static string connection_info_characters =
  315.         "SERVER=localhost;" +           // Szerver elérése
  316.         "DATABASE=characters;" +        // Adatbázis neve
  317.         "UID=root;" +                   // Felhasználónév
  318.         "PASSWORD=root;";               // Jelszó
  319.  
  320.         public MySqlConnection connection_realmd = new MySqlConnection(connection_info_realmd);
  321.         public MySqlConnection connection_characters = new MySqlConnection(connection_info_characters);
  322.  
  323.         public void connection_open_realmd()
  324.         {
  325.             if (!connection_check_realmd)
  326.             {
  327.                 try
  328.                 {
  329.                     connection_realmd.Open();
  330.                     connection_check_realmd = true;
  331.                 }
  332.                 catch
  333.                 {
  334.                     MessageBox.Show("Error #000");
  335.                     // Error #000 - Nem tudott kapcsolódni a MySQL adatbázishoz
  336.                 }
  337.             }
  338.         }
  339.  
  340.         public void connection_close_realmd()
  341.         {
  342.             if (connection_check_realmd)
  343.             {
  344.                 connection_realmd.Close();
  345.                 connection_check_realmd = false;
  346.             }
  347.         }
  348.  
  349.         public void connection_open_characters()
  350.         {
  351.             if (!connection_check_characters)
  352.             {
  353.                 try
  354.                 {
  355.                     connection_characters.Open();
  356.                     connection_check_characters = true;
  357.                 }
  358.                 catch
  359.                 {
  360.                     MessageBox.Show("Error #000");
  361.                     // Error #000 - Nem tudott kapcsolódni a MySQL adatbázishoz
  362.                 }
  363.             }
  364.         }
  365.  
  366.         public void connection_close_characters()
  367.         {
  368.             if (connection_check_characters)
  369.             {
  370.                 connection_characters.Close();
  371.                 connection_check_characters = false;
  372.             }
  373.         }
  374.     }
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement