|
@ -1,6 +1,7 @@ |
|
|
package gr.thmmy.mthmmy.session; |
|
|
package gr.thmmy.mthmmy.session; |
|
|
|
|
|
|
|
|
import android.content.SharedPreferences; |
|
|
import android.content.SharedPreferences; |
|
|
|
|
|
import android.support.annotation.NonNull; |
|
|
import android.support.annotation.Nullable; |
|
|
import android.support.annotation.Nullable; |
|
|
|
|
|
|
|
|
import com.franmontiel.persistentcookiejar.PersistentCookieJar; |
|
|
import com.franmontiel.persistentcookiejar.PersistentCookieJar; |
|
@ -35,8 +36,8 @@ public class SessionManager { |
|
|
private static final String TAG = "SessionManager"; |
|
|
private static final String TAG = "SessionManager"; |
|
|
|
|
|
|
|
|
//Generic constants
|
|
|
//Generic constants
|
|
|
public static final HttpUrl indexUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php"); |
|
|
public static final HttpUrl indexUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php?theme=4"); |
|
|
public static final HttpUrl forumUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php?action=forum"); |
|
|
public static final HttpUrl forumUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php?action=forum;theme=4"); |
|
|
private static final HttpUrl loginUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php?action=login2"); |
|
|
private static final HttpUrl loginUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php?action=login2"); |
|
|
private static final String guestName = "Guest"; |
|
|
private static final String guestName = "Guest"; |
|
|
|
|
|
|
|
@ -109,8 +110,9 @@ public class SessionManager { |
|
|
Response response = client.newCall(request).execute(); |
|
|
Response response = client.newCall(request).execute(); |
|
|
Document document = Jsoup.parse(response.body().string()); |
|
|
Document document = Jsoup.parse(response.body().string()); |
|
|
|
|
|
|
|
|
Element logoutButton = document.getElementById("logoutbtn"); //Attempt to find logout button
|
|
|
Elements unreadRepliesLinks = document.select("a[href=https://www.thmmy.gr/smf/index.php?action=unreadreplies]"); |
|
|
if (logoutButton != null) //If logout button exists, login was successful
|
|
|
|
|
|
|
|
|
if (unreadRepliesLinks.size()>=2) //Normally it's just == 2, but who knows what can be posted by users
|
|
|
{ |
|
|
{ |
|
|
Report.i(TAG, "Login successful!"); |
|
|
Report.i(TAG, "Login successful!"); |
|
|
setPersistentCookieSession(); //Store cookies
|
|
|
setPersistentCookieSession(); //Store cookies
|
|
@ -126,7 +128,8 @@ public class SessionManager { |
|
|
} else |
|
|
} else |
|
|
sharedPrefs.edit().putBoolean(HAS_AVATAR, false).apply(); |
|
|
sharedPrefs.edit().putBoolean(HAS_AVATAR, false).apply(); |
|
|
|
|
|
|
|
|
sharedPrefs.edit().putString(LOGOUT_LINK, HttpUrl.parse(logoutButton.attr("href")).toString()).apply(); |
|
|
|
|
|
|
|
|
sharedPrefs.edit().putString(LOGOUT_LINK, extractLogoutLink(document)).apply(); |
|
|
|
|
|
|
|
|
return SUCCESS; |
|
|
return SUCCESS; |
|
|
} else { |
|
|
} else { |
|
@ -254,7 +257,6 @@ public class SessionManager { |
|
|
return sharedPrefs.getBoolean(LOGIN_SCREEN_AS_DEFAULT, true); |
|
|
return sharedPrefs.getBoolean(LOGIN_SCREEN_AS_DEFAULT, true); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//TODO FIX METHOD, THIS MIGHT BE A SECURITY FLAW!! SEE ISSUE #2 MERGED WITH #16
|
|
|
|
|
|
public String getCookieHeader() { |
|
|
public String getCookieHeader() { |
|
|
return cookiePersistor.loadAll().get(0).toString(); |
|
|
return cookiePersistor.loadAll().get(0).toString(); |
|
|
} |
|
|
} |
|
@ -290,33 +292,57 @@ public class SessionManager { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Nullable |
|
|
@Nullable |
|
|
private String extractUserName(Document doc) { |
|
|
private String extractUserName(@NonNull Document doc) { |
|
|
if (doc != null) { |
|
|
//Scribbles2 Theme
|
|
|
Elements user = doc.select("div[id=myuser] > h3"); |
|
|
Elements user = doc.select("div[id=myuser] > h3"); |
|
|
|
|
|
|
|
|
if (user.size() == 1) { |
|
|
if (user.size() == 1) { |
|
|
String txt = user.first().ownText(); |
|
|
String txt = user.first().ownText(); |
|
|
|
|
|
|
|
|
Pattern pattern = Pattern.compile(", (.*?),"); |
|
|
Pattern pattern = Pattern.compile(", (.*?),"); |
|
|
Matcher matcher = pattern.matcher(txt); |
|
|
Matcher matcher = pattern.matcher(txt); |
|
|
if (matcher.find()) |
|
|
if (matcher.find()) |
|
|
return matcher.group(1); |
|
|
return matcher.group(1); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
//Helios_Multi and SMF_oneBlue
|
|
|
|
|
|
user = doc.select("td.smalltext[width=100%] b"); |
|
|
|
|
|
if (user.size() == 1) |
|
|
|
|
|
return user.first().ownText(); |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
//SMF Default Theme
|
|
|
|
|
|
user = doc.select("td.titlebg2[height=32] b"); |
|
|
|
|
|
if (user.size() == 1) |
|
|
|
|
|
return user.first().ownText(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
Report.w(TAG, "Extracting username failed!"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Report.e(TAG, "Extracting username failed!"); |
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
@Nullable |
|
|
private String extractAvatarLink(Document doc) { |
|
|
private String extractAvatarLink(@NonNull Document doc) { |
|
|
if (doc != null) { |
|
|
Elements avatar = doc.getElementsByClass("avatar"); |
|
|
Elements avatar = doc.select("#ava img"); |
|
|
if (!avatar.isEmpty()) |
|
|
|
|
|
return avatar.first().attr("src"); |
|
|
|
|
|
|
|
|
if (avatar.size() == 1) { |
|
|
Report.e(TAG, "Extracting avatar's link failed!"); |
|
|
return avatar.attr("src"); |
|
|
return null; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
Report.w(TAG, "Extracting avatar's link failed!"); |
|
|
@Nullable |
|
|
|
|
|
private String extractLogoutLink(@NonNull Document doc) { |
|
|
|
|
|
Elements logoutLink = doc.select("a[href^=https://www.thmmy.gr/smf/index.php?action=logout;sesc=]"); |
|
|
|
|
|
|
|
|
|
|
|
if (!logoutLink.isEmpty()) |
|
|
|
|
|
return logoutLink.first().attr("href"); |
|
|
|
|
|
|
|
|
|
|
|
Report.e(TAG, "Extracting logout link failed!"); |
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
//----------------------------------OTHER FUNCTIONS END-----------------------------------------
|
|
|
//----------------------------------OTHER FUNCTIONS END-----------------------------------------
|
|
|