Guest User

Untitled

a guest
Feb 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. public void login(final String username, final String password, final String url) {
  2. Log.d("HBmobile", "Passed to Login");
  3. Log.d("HBmobile", "Passed URL - " + url);
  4. final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  5. final Button button = (Button) findViewById(R.id.button1);
  6. final EditText username2 = (EditText) findViewById(R.id.editText1);
  7. final EditText password2 = (EditText) findViewById(R.id.editText2);
  8.  
  9. if ( htuser == null ) {
  10. final boolean htaccess = preferences.getBoolean("htcheck", false);
  11. final String htuser = preferences.getString("htuser", "");
  12. final String htpass = preferences.getString("htpass", "");
  13. }
  14. Credentials creds = null;
  15. if ( htaccess == true ) {
  16. creds = new UsernamePasswordCredentials(htuser, htpass);
  17. Log.d("HBmobile", "htaccess set - " + creds.toString());
  18. }
  19. Log.d("HBmobile", htuser + " : " + htpass);
  20. username2.setEnabled(false);
  21. password2.setEnabled(false);
  22. button.setEnabled(false);
  23.  
  24. int response2 = 0;
  25. String reply = null;
  26.  
  27. URL url2 = null;
  28. try {
  29. if (username == "TOKEN") {
  30. url2 = new URL(url + "/admin/api.php?&token=" + password + "&call=getHostBillversion" );
  31. } else {
  32. url2 = new URL(url + "/admin/api.php?&action=login&username=" + username + "&password=" + password);
  33. }
  34. Log.d("HBmobile", "New URL" + url2.toString());
  35. } catch (MalformedURLException e1) {
  36.  
  37. e1.printStackTrace();
  38. }
  39. InputStream httpcontent=null;
  40. try{
  41. HttpGet httpGet = new HttpGet(url2.toURI());
  42. DefaultHttpClient httpclient = new DefaultHttpClient();
  43. if ( htaccess == true ) {
  44. httpclient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), creds);
  45. }
  46. HttpResponse response = httpclient.execute(httpGet);
  47. response2 = response.getStatusLine().getStatusCode();
  48. if( response2 == 404) {
  49. Toast.makeText(getBaseContext(), "Not a valid HostBill Address.", Toast.LENGTH_SHORT).show();
  50. //loggingin.dismiss();
  51. AlertDialog alertDialog = new AlertDialog.Builder(main.this).create();
  52. alertDialog.setTitle("Error...");
  53. alertDialog.setMessage("An invalid URL has been set.\nPlease check the URL again.\nError: Status Code 404");
  54. alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
  55. public void onClick(DialogInterface dialog, int which) {
  56. }
  57. });
  58. alertDialog.show();
  59. username2.setEnabled(true);
  60. password2.setEnabled(true);
  61. button.setEnabled(true);
  62. reply = "blanks";
  63. } else if (response2 == 200) {
  64. httpcontent = response.getEntity().getContent();
  65. BufferedReader rd = new BufferedReader(new InputStreamReader(httpcontent), 4096);
  66. String line;
  67. StringBuilder sb = new StringBuilder();
  68. while ((line = rd.readLine()) != null) {
  69. sb.append(line);
  70. }
  71. rd.close();
  72. reply = new String(sb.toString());
  73. response2 = response.getStatusLine().getStatusCode();
  74. } else if (response2 == 401) {
  75. Log.d("HBmobile", "401");
  76.  
  77. final Dialog dialog = new Dialog(main.this);
  78. dialog.setContentView(R.layout.htaccessdialog);
  79. dialog.setTitle("401 - Authorization Required.");
  80. dialog.setCancelable(true);
  81. TextView message = (TextView) dialog.findViewById(R.id.textView3);
  82. final EditText htusername = (EditText) dialog.findViewById(R.id.editText1);
  83. final EditText htpassword = (EditText) dialog.findViewById(R.id.editText2);
  84.  
  85. Button login = (Button) dialog.findViewById(R.id.button1);
  86. login.setOnClickListener(new OnClickListener() {
  87. public void onClick(View v) {
  88. htaccess = true;
  89. htuser = htusername.getText().toString();
  90. htpass = htpassword.getText().toString();
  91. Log.d("HBmobile", htuser + " : " + htpass);
  92. login(username, password, url);
  93. dialog.dismiss();
  94. }
  95. });
  96. Button cancel = (Button) dialog.findViewById(R.id.button2);
  97. cancel.setOnClickListener(new OnClickListener() {
  98. public void onClick(View v) {
  99. dialog.dismiss();
  100. }
  101. });
  102. dialog.show();
  103. reply = "blanks";
  104. }
  105. Log.e("reply", reply);
  106. } catch (Exception e) {
  107. //loggingin.dismiss();
  108. Toast.makeText(getBaseContext(), "Invalid URL", Toast.LENGTH_SHORT);
  109. reply = "blanks";
  110. }
  111. if( reply.contains("success")){
  112. //loggingin.dismiss();
  113. int requestCode = 0;
  114. Intent i= new Intent(main.this, Home.class);
  115. Bundle b = new Bundle();
  116. if ( username == "TOKEN") {
  117. b.putString("token", password);
  118. } else {
  119. String token = reply.substring(reply.indexOf("token"),reply.indexOf(",\"call\""));
  120. token = token.substring(8);
  121. token = token.replace("\"", "");
  122. b.putString("token", token);
  123. }
  124. if ( htaccess = true ) {
  125. b.putBoolean("htaccess", true);
  126. b.putString("htuser", htuser);
  127. b.putString("htpass", htpass);
  128. } else {
  129. b.putBoolean("htaccess", false);
  130. }
  131. b.putString("url", url);
  132. i.putExtras(b);
  133. startActivityForResult(i, requestCode);
  134. } else if( reply.contains("error")){
  135. //loggingin.dismiss();
  136. if( reply.contains("ip_banned_login")){
  137. Toast.makeText(getBaseContext(), "Your IP has been banned.", Toast.LENGTH_LONG).show();
  138. } else if( reply.contains("invalid_token_foroperation")) {
  139. Toast.makeText(getBaseContext(), "Previous Session has expired.", Toast.LENGTH_LONG).show();
  140. } else {
  141. Toast.makeText(getBaseContext(), "Username/Password incorrect", Toast.LENGTH_SHORT).show();
  142. }
  143. preferences.edit().remove("HB_TOKEN");
  144. username2.setEnabled(true);
  145. password2.setEnabled(true);
  146. button.setEnabled(true);
  147. } else {
  148. //loggingin.dismiss();
  149. Toast.makeText(getBaseContext(), "An unknown error occurred.", Toast.LENGTH_LONG).show();
  150. username2.setEnabled(true);
  151. password2.setEnabled(true);
  152. button.setEnabled(true);
  153. }
  154. }
Add Comment
Please, Sign In to add comment