Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. //Function to show an error in the "#error" element
  2. function throwError(err, isInternal, dafuq){
  3. if(!isInternal){
  4. err = "Error: " + err;
  5. }
  6. else{
  7. err = "Internal server error. Report this: " + err;
  8. }
  9. /*I don't know what the fuck went wrong over at the logIn() function. Whatever it is, I managed to fix it by checking the value
  10. after it had been thrown into the throwError function.*/
  11. if(dafuq === true){
  12. window.location = "accountpage/yourpage.php";
  13. return 1;
  14. }
  15. document.getElementById("error").innerHTML = err;
  16. }
  17.  
  18.  
  19. //This is for registering - there's some functions I can do on the client side, and I prefer client-side.
  20. function register(){
  21. var username = document.getElementById("username").value;
  22. var password = document.getElementById("password").value;
  23. var confirmpassword = document.getElementById("confirm-password").value;
  24. var emailaddress = document.getElementById("email-address").value;
  25. var usernameblacklist = ["thijs365", "toomuchram", "thijs", "tmr", "tristan", "wificable", "pcnerd512", "admin", "siteadmin"];
  26.  
  27.  
  28. //ALWAYS replace " and ' with \" and \' first
  29. //cuz, y'know, SQL injection
  30.  
  31. username = username.replace("'", "\'");
  32. username = username.replace('"', '\"');
  33.  
  34. password = password.replace("'", "\'");
  35. password = password.replace('"', '\"');
  36.  
  37. emailaddress = emailaddress.replace("'", "\'");
  38. emailaddress = emailaddress.replace('"', '\"');
  39.  
  40.  
  41. //Check if any of the values is empty.
  42. if(username === null || username === "" || username === undefined){
  43. throwError("Users should be able to identify you. That's a bit difficult without a username, right?");
  44. return 1;
  45. }
  46. if(password === null || password === "" || password === undefined){
  47. throwError("Not going to lie, that's some strong authentication.");
  48. return 1;
  49. }
  50. if(emailaddress === null || emailaddress === "" || emailaddress === undefined){
  51. throwError("Don't worry, I'm not like Facebook.");
  52. return 1;
  53. }
  54.  
  55.  
  56.  
  57. //Always check if the two passwords are the same.
  58. if(password !== confirmpassword){
  59. throwError("The two password aren't the same");
  60. return 1;
  61. }
  62. //Then check if the password's on a blacklist - because I'm a dickhead.
  63. //Create a temporary variable that holds a lowercased version of the username - I don't want someone to create tHijs365!
  64. var lowercaseusername = username.toLowerCase();
  65. //Also strip it from punctuation
  66. lowercaseusername = lowercaseusername.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
  67. //And fix it up
  68. lowercaseusername = lowercaseusername.replace(/\s{2,}/g," ");
  69. //source: https://stackoverflow.com/questions/4328500/how-can-i-strip-all-punctuation-from-a-string-in-javascript-using-regex#4328722
  70.  
  71. for(var i=0; i<usernameblacklist.length; i++){
  72. if(lowercaseusername == usernameblacklist[i]){
  73. throwError("This username is on the blacklist");
  74. return 1;
  75. }
  76. }
  77.  
  78. //I steal from SO all the time.
  79. //https://stackoverflow.com/questions/692196/post-request-javascript#25422754
  80.  
  81. var params = "username=" + username + "&password=" + password + "&emailaddress=" + emailaddress;
  82. var accountrequest = new XMLHttpRequest();
  83. accountrequest.open("POST", "register.php", true);
  84.  
  85. accountrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  86.  
  87. accountrequest.onreadystatechange = function() {//Call a function when the state changes.
  88. if(accountrequest.readyState == 4 && accountrequest.status == 200) {
  89. var errorStr = this.responseText;
  90. errorStr = errorStr.replace(/['"]+/g, '');
  91. if(errorStr == "success"){
  92. window.location = "login.html";
  93. }
  94. else{
  95. throwError(errorStr);
  96. }
  97. }
  98.  
  99.  
  100. }
  101. accountrequest.send(params);
  102. }
  103.  
  104. //Function for logging in
  105. //This is for error handling.
  106. function logIn(){
  107. var username = document.getElementById("username").value;
  108. var password = document.getElementById("password").value;
  109. var deletedAccounts = ["styrofoam head", "notsobot"];
  110.  
  111. //Replace " and ' with \" and \' first
  112.  
  113.  
  114. username = username.replace("'", "\'");
  115. username = username.replace('"', '\"');
  116.  
  117. password = password.replace("'", "\'");
  118. password = password.replace('"', '\"');
  119.  
  120. //Create a temporary variable that holds a lowercased version of the username
  121. var lowercaseusername = username.toLowerCase();
  122. //Also strip it from punctuation
  123. lowercaseusername = lowercaseusername.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
  124. //And fix it up
  125. lowercaseusername = lowercaseusername.replace(/\s{2,}/g," ");
  126. //source: https://stackoverflow.com/questions/4328500/how-can-i-strip-all-punctuation-from-a-string-in-javascript-using-regex#4328722
  127.  
  128. for(var i=0; i<deletedAccounts.length; i++){
  129. if(lowercaseusername == deletedAccounts[i]){
  130. throwError("This account has been deleted.");
  131. return 1;
  132. }
  133. }
  134.  
  135. //Then check for empty variables
  136. if(username === null || username === "" || username === undefined){
  137. throwError("Please input a username");
  138. return 1;
  139. }
  140. if(password === null || password === "" || password === undefined){
  141. throwError("Please input a password");
  142. return 1;
  143. }
  144.  
  145.  
  146. //https://stackoverflow.com/questions/692196/post-request-javascript#25422754
  147.  
  148. var params = "username=" + username + "&password=" + password;
  149. var accountrequest = new XMLHttpRequest();
  150. accountrequest.open("POST", "login.php", true);
  151.  
  152. accountrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  153.  
  154. accountrequest.onreadystatechange = function() {//Call a function when the state changes.
  155. if(accountrequest.readyState == 4 && accountrequest.status == 200) {
  156. var errorStr = this.responseText;
  157. errorStr = errorStr.replace(/['"]+/g, '');
  158.  
  159. if(errorStr == "success"){
  160. window.location = "accountpage/yourpage.php";
  161. }
  162. else{
  163. throwError(errorStr, false, true);
  164. }
  165. }
  166.  
  167.  
  168. }
  169. accountrequest.send(params);
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement