|
@ -47,6 +47,7 @@ public class SessionManager { |
|
|
public static final int CANCELLED = 4; |
|
|
public static final int CANCELLED = 4; |
|
|
public static final int CONNECTION_ERROR = 5; |
|
|
public static final int CONNECTION_ERROR = 5; |
|
|
public static final int EXCEPTION = 6; |
|
|
public static final int EXCEPTION = 6; |
|
|
|
|
|
public static final int BANNED_USER = 7; |
|
|
|
|
|
|
|
|
// Client & Cookies
|
|
|
// Client & Cookies
|
|
|
private final OkHttpClient client; |
|
|
private final OkHttpClient client; |
|
@ -114,8 +115,8 @@ public class SessionManager { |
|
|
setPersistentCookieSession(); //Store cookies
|
|
|
setPersistentCookieSession(); //Store cookies
|
|
|
|
|
|
|
|
|
//Edit SharedPreferences, save session's data
|
|
|
//Edit SharedPreferences, save session's data
|
|
|
|
|
|
setLoginScreenAsDefault(false); |
|
|
sharedPrefs.edit().putBoolean(LOGGED_IN, true).apply(); |
|
|
sharedPrefs.edit().putBoolean(LOGGED_IN, true).apply(); |
|
|
sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, false).apply(); |
|
|
|
|
|
sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply(); |
|
|
sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply(); |
|
|
String avatar = extractAvatarLink(document); |
|
|
String avatar = extractAvatarLink(document); |
|
|
if (avatar != null) { |
|
|
if (avatar != null) { |
|
@ -133,17 +134,23 @@ public class SessionManager { |
|
|
|
|
|
|
|
|
//Investigate login failure
|
|
|
//Investigate login failure
|
|
|
Elements error = document.select("b:contains(That username does not exist.)"); |
|
|
Elements error = document.select("b:contains(That username does not exist.)"); |
|
|
if (error.size() == 1) { //Wrong username
|
|
|
if (error.size() > 0) { //Wrong username
|
|
|
Timber.i("Wrong Username"); |
|
|
Timber.i("Wrong Username"); |
|
|
return WRONG_USER; |
|
|
return WRONG_USER; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
error = document.select("body:contains(Password incorrect)"); |
|
|
error = document.select("body:contains(Password incorrect)"); |
|
|
if (error.size() == 1) { //Wrong password
|
|
|
if (error.size() > 0) { //Wrong password
|
|
|
Timber.i("Wrong Password"); |
|
|
Timber.i("Wrong Password"); |
|
|
return WRONG_PASSWORD; |
|
|
return WRONG_PASSWORD; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
error = document.select("body:contains(you are banned from using this forum!),body:contains(έχετε αποκλειστεί από αυτή τη δημόσια συζήτηση!)"); |
|
|
|
|
|
if (error.size() > 0) { //User is banned
|
|
|
|
|
|
Timber.i("User is banned"); |
|
|
|
|
|
return BANNED_USER; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//Other error e.g. session was reset server-side
|
|
|
//Other error e.g. session was reset server-side
|
|
|
clearSessionData(); //Clear invalid saved data
|
|
|
clearSessionData(); //Clear invalid saved data
|
|
|
return FAILURE; |
|
|
return FAILURE; |
|
@ -180,7 +187,7 @@ public class SessionManager { |
|
|
} else if (isLoginScreenDefault()) |
|
|
} else if (isLoginScreenDefault()) |
|
|
return; |
|
|
return; |
|
|
|
|
|
|
|
|
sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, true).apply(); |
|
|
setLoginScreenAsDefault(true); |
|
|
clearSessionData(); |
|
|
clearSessionData(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -190,7 +197,7 @@ public class SessionManager { |
|
|
public void guestLogin() { |
|
|
public void guestLogin() { |
|
|
Timber.i("Continuing as a guest, as chosen by the user."); |
|
|
Timber.i("Continuing as a guest, as chosen by the user."); |
|
|
clearSessionData(); |
|
|
clearSessionData(); |
|
|
sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, false).apply(); |
|
|
setLoginScreenAsDefault(false); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -219,7 +226,7 @@ public class SessionManager { |
|
|
return FAILURE; |
|
|
return FAILURE; |
|
|
} |
|
|
} |
|
|
} catch (IOException e) { |
|
|
} catch (IOException e) { |
|
|
Timber.w("Logout IOException", e); |
|
|
Timber.w(e, "Logout IOException"); |
|
|
return CONNECTION_ERROR; |
|
|
return CONNECTION_ERROR; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
Timber.e(e, "Logout Exception"); |
|
|
Timber.e(e, "Logout Exception"); |
|
@ -253,16 +260,17 @@ public class SessionManager { |
|
|
return sharedPrefs.getBoolean(LOGIN_SCREEN_AS_DEFAULT, true); |
|
|
return sharedPrefs.getBoolean(LOGIN_SCREEN_AS_DEFAULT, true); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public String getCookieHeader() { |
|
|
|
|
|
return cookiePersistor.loadAll().get(0).toString(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------GETTERS END---------------------------------------------
|
|
|
//--------------------------------------GETTERS END---------------------------------------------
|
|
|
|
|
|
|
|
|
//------------------------------------OTHER FUNCTIONS-------------------------------------------
|
|
|
//------------------------------------OTHER FUNCTIONS-------------------------------------------
|
|
|
private boolean validateRetrievedCookies() { |
|
|
private boolean validateRetrievedCookies() { |
|
|
List<Cookie> cookieList = cookieJar.loadForRequest(indexUrl); |
|
|
List<Cookie> cookieList = cookieJar.loadForRequest(indexUrl); |
|
|
return (cookieList.size() == 2) && (cookieList.get(0).name().equals("THMMYgrC00ki3")) && (cookieList.get(1).name().equals("PHPSESSID")); |
|
|
for(Cookie cookie: cookieList) |
|
|
|
|
|
{ |
|
|
|
|
|
if(cookie.name().equals("THMMYgrC00ki3")) |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Call validateRetrievedCookies() first
|
|
|
// Call validateRetrievedCookies() first
|
|
@ -288,6 +296,10 @@ public class SessionManager { |
|
|
Timber.i("Session data cleared."); |
|
|
Timber.i("Session data cleared."); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void setLoginScreenAsDefault(boolean b){ |
|
|
|
|
|
sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, b).apply(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@NonNull |
|
|
@NonNull |
|
|
private String extractUserName(@NonNull Document doc) { |
|
|
private String extractUserName(@NonNull Document doc) { |
|
|
//Scribbles2 Theme
|
|
|
//Scribbles2 Theme
|
|
|