From 1bc29aca95e6577351f0a5a54af097baebc25fbd Mon Sep 17 00:00:00 2001 From: Ezerous Date: Thu, 19 Jan 2017 17:04:19 +0200 Subject: [PATCH] Login works for all themes --- app/src/debug/java/mthmmy/utils/Report.java | 14 ++++ .../gr/thmmy/mthmmy/base/BaseActivity.java | 1 + .../thmmy/mthmmy/session/SessionManager.java | 74 +++++++++++++------ app/src/release/java/mthmmy.utils/Report.java | 5 ++ 4 files changed, 70 insertions(+), 24 deletions(-) diff --git a/app/src/debug/java/mthmmy/utils/Report.java b/app/src/debug/java/mthmmy/utils/Report.java index 68ab3c3b..6e6a906f 100644 --- a/app/src/debug/java/mthmmy/utils/Report.java +++ b/app/src/debug/java/mthmmy/utils/Report.java @@ -64,4 +64,18 @@ public class Report { Log.wtf(TAG,message + ": " + tr.getMessage(),tr); } + + /** + * Prints long messages in logcat (debug level). + */ + public static void longMessage(String TAG, String message) + { + int maxLogSize = 1000; + for(int i = 0; i <= message.length() / maxLogSize; i++) { + int start = i * maxLogSize; + int end = (i+1) * maxLogSize; + end = end > message.length() ? message.length() : end; + Report.d(TAG, message.substring(start, end)); + } + } } \ No newline at end of file diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index 492f84a1..e832301a 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -198,6 +198,7 @@ public abstract class BaseActivity extends AppCompatActivity drawer = new DrawerBuilder() .withActivity(this) .withToolbar(toolbar) + .withDrawerWidthDp(175) .withSliderBackgroundColor(ContextCompat.getColor(this, R.color.primary_light)) .withAccountHeader(accountHeader) .addDrawerItems(homeItem,loginLogoutItem,aboutItem) diff --git a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java index 15245e2f..a6bb4df5 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java +++ b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java @@ -1,6 +1,7 @@ package gr.thmmy.mthmmy.session; import android.content.SharedPreferences; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.franmontiel.persistentcookiejar.PersistentCookieJar; @@ -35,8 +36,8 @@ public class SessionManager { private static final String TAG = "SessionManager"; //Generic constants - public static final HttpUrl indexUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php"); - public static final HttpUrl forumUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php?action=forum"); + 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;theme=4"); private static final HttpUrl loginUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php?action=login2"); private static final String guestName = "Guest"; @@ -109,8 +110,9 @@ public class SessionManager { Response response = client.newCall(request).execute(); Document document = Jsoup.parse(response.body().string()); - Element logoutButton = document.getElementById("logoutbtn"); //Attempt to find logout button - if (logoutButton != null) //If logout button exists, login was successful + Elements unreadRepliesLinks = document.select("a[href=https://www.thmmy.gr/smf/index.php?action=unreadreplies]"); + + if (unreadRepliesLinks.size()>=2) //Normally it's just == 2, but who knows what can be posted by users { Report.i(TAG, "Login successful!"); setPersistentCookieSession(); //Store cookies @@ -126,7 +128,8 @@ public class SessionManager { } else 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; } else { @@ -254,7 +257,6 @@ public class SessionManager { 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() { return cookiePersistor.loadAll().get(0).toString(); } @@ -290,33 +292,57 @@ public class SessionManager { } @Nullable - private String extractUserName(Document doc) { - if (doc != null) { - Elements user = doc.select("div[id=myuser] > h3"); + private String extractUserName(@NonNull Document doc) { + //Scribbles2 Theme + Elements user = doc.select("div[id=myuser] > h3"); - if (user.size() == 1) { - String txt = user.first().ownText(); + if (user.size() == 1) { + String txt = user.first().ownText(); - Pattern pattern = Pattern.compile(", (.*?),"); - Matcher matcher = pattern.matcher(txt); - if (matcher.find()) - return matcher.group(1); + Pattern pattern = Pattern.compile(", (.*?),"); + Matcher matcher = pattern.matcher(txt); + if (matcher.find()) + 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; } + @Nullable - private String extractAvatarLink(Document doc) { - if (doc != null) { - Elements avatar = doc.select("#ava img"); + private String extractAvatarLink(@NonNull Document doc) { + Elements avatar = doc.getElementsByClass("avatar"); + if (!avatar.isEmpty()) + return avatar.first().attr("src"); - if (avatar.size() == 1) { - return avatar.attr("src"); - } - } - Report.w(TAG, "Extracting avatar's link failed!"); + Report.e(TAG, "Extracting avatar's link failed!"); + return null; + } + + @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; } //----------------------------------OTHER FUNCTIONS END----------------------------------------- diff --git a/app/src/release/java/mthmmy.utils/Report.java b/app/src/release/java/mthmmy.utils/Report.java index 25df4841..ce2ea89d 100644 --- a/app/src/release/java/mthmmy.utils/Report.java +++ b/app/src/release/java/mthmmy.utils/Report.java @@ -77,5 +77,10 @@ public class Report FirebaseCrash.report(tr); } + /** + * Does nothing in release. + */ + public static void longMessage(String TAG, String level, String message) {return;} + }