Advertisement
Guest User

Untitled

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