· 8 years ago · Jan 15, 2018, 02:22 PM
1using UnityEngine;
2using UnityEngine.UI;
3using System.Text;
4using System.Security;
5using System.Collections;
6using System.Text.RegularExpressions; // needed for Regex
7
8public class bl_Register : MonoBehaviour {
9
10 public string SecretKey = "123456";
11 public string RegisterPHP_Url = "";
12 [Space(5)]
13 public InputField UserInput = null;
14 public InputField PasswordInput = null;
15 public InputField Re_PasswordInput = null;
16 [Space(5)]
17 public int MaxNameLenght = 15;
18
19 protected string UserName = "";
20 protected string Password = "";
21 protected string Re_Password = "";
22 private bool isRegister = false;
23
24 // Use this for initialization
25 void Start () {
26
27 if (UserInput != null)
28 {
29 UserInput.characterLimit = MaxNameLenght;
30 }
31 }
32
33 /// <summary>
34 ///
35 /// </summary>
36 void Update()
37 {
38 if (UserInput != null)
39 {
40 UserInput.text = Regex.Replace(UserInput.text, @"[^a-zA-Z0-9 ]", "");//not permit simbols
41 UserName = UserInput.text;
42 }
43 if (PasswordInput != null)
44 {
45 Password = PasswordInput.text;
46 }
47 if (Re_PasswordInput != null)
48 {
49 Re_Password = Re_PasswordInput.text;
50 }
51 }
52
53 /// <summary>
54 /// Register function to check.
55 /// </summary>
56 public void Register()
57 {
58 if (isRegister)//if alredy connect
59 return;
60
61 if (UserName != string.Empty && Re_Password != string.Empty && Password != string.Empty)
62 {
63 if (Password == Re_Password)
64 {
65 StartCoroutine(RegisterProcess());
66 bl_LoginManager.UpdateDescription("");
67 }
68 else
69 {
70 bl_LoginManager.UpdateDescription("Password does not match");
71 Debug.LogWarning("Password does not match");
72 }
73 }
74 else
75 {
76 if (UserName == string.Empty)
77 {
78 Debug.LogWarning("User Name is Emty");
79 }
80 if (Password == string.Empty)
81 {
82 Debug.LogWarning("Password is Emty");
83 }
84 if (Re_Password == string.Empty)
85 {
86 Debug.LogWarning("Re-Password is Emty");
87 }
88 bl_LoginManager.UpdateDescription("Complete all fields");
89 }
90 }
91
92 /// <summary>
93 /// Connect with database
94 /// </summary>
95 /// <returns></returns>
96 IEnumerator RegisterProcess()
97 {
98 if (isRegister)
99 yield return null;
100
101 isRegister = true;
102 bl_LoginManager.UpdateDescription("Registering...");
103 bl_LoginManager.LoadingCache.ChangeText("Request Register...", true);
104 //Used for security check for authorization to modify database
105 string hash = Md5Sum(UserName + Password + SecretKey).ToLower();
106
107 //Assigns the data we want to save
108 //Where -> Form.AddField("name" = matching name of value in SQL database
109 WWWForm mForm = new WWWForm();
110 mForm.AddField("name", UserName); // adds the player name to the form
111 mForm.AddField("password", Password); // adds the player password to the form
112 mForm.AddField("kills", 0); // adds the kill total to the form
113 mForm.AddField("deaths", 0); // adds the death Total to the form
114 mForm.AddField("score", 0); // adds the score Total to the form
115 mForm.AddField("uIP", bl_LoginManager.m_IP);
116 mForm.AddField("hash", hash); // adds the security hash for Authorization
117
118 //Creates instance of WWW to runs the PHP script to save data to mySQL database
119 WWW www = new WWW(RegisterPHP_Url, mForm);
120 bl_LoginManager.UpdateDescription("Processing...");
121 Debug.Log("Processing...");
122 yield return www;
123 bl_LoginManager.LoadingCache.ChangeText("Get response...", true,2f);
124 Debug.Log("" + www.text);
125 if (www.text == "Done")
126 {
127 Debug.Log("Registered Successfully.");
128 this.GetComponent<bl_LoginManager>().ShowLogin();
129 bl_LoginManager.UpdateDescription("Registered Successfully, Login Now");
130
131 }
132 else
133 {
134 Debug.Log(www.text);
135 bl_LoginManager.UpdateDescription(www.text);
136
137 }
138 isRegister = false;
139 }
140
141 /// <summary>
142 /// Md5s Security Features
143 /// </summary>
144 public string Md5Sum(string input)
145 {
146 System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
147 byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
148 byte[] hash = md5.ComputeHash(inputBytes);
149
150 StringBuilder sb = new StringBuilder();
151 for (int i = 0; i < hash.Length; i++) { sb.Append(hash[i].ToString("X2")); }
152 return sb.ToString();
153 }
154}