|
|
|
@ -2,6 +2,9 @@ package org.thoughtcrime.securesms.linkpreview;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
|
import android.text.Html;
|
|
|
|
|
import android.text.SpannableString;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.text.style.URLSpan;
|
|
|
|
@ -10,9 +13,14 @@ import android.text.util.Linkify;
|
|
|
|
|
import com.annimon.stream.Stream;
|
|
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.stickers.StickerUrl;
|
|
|
|
|
import org.thoughtcrime.securesms.util.DateUtils;
|
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
@ -20,10 +28,15 @@ import okhttp3.HttpUrl;
|
|
|
|
|
|
|
|
|
|
public final class LinkPreviewUtil {
|
|
|
|
|
|
|
|
|
|
private static final Pattern DOMAIN_PATTERN = Pattern.compile("^(https?://)?([^/]+).*$");
|
|
|
|
|
private static final Pattern ALL_ASCII_PATTERN = Pattern.compile("^[\\x00-\\x7F]*$");
|
|
|
|
|
private static final Pattern ALL_NON_ASCII_PATTERN = Pattern.compile("^[^\\x00-\\x7F]*$");
|
|
|
|
|
private static final Pattern STICKER_URL_PATTERN = Pattern.compile("^.*#pack_id=(.*)&pack_key=(.*)$");
|
|
|
|
|
private static final Pattern DOMAIN_PATTERN = Pattern.compile("^(https?://)?([^/]+).*$");
|
|
|
|
|
private static final Pattern ALL_ASCII_PATTERN = Pattern.compile("^[\\x00-\\x7F]*$");
|
|
|
|
|
private static final Pattern ALL_NON_ASCII_PATTERN = Pattern.compile("^[^\\x00-\\x7F]*$");
|
|
|
|
|
private static final Pattern OPEN_GRAPH_TAG_PATTERN = Pattern.compile("<\\s*meta[^>]*property\\s*=\\s*\"\\s*og:([^\"]+)\"[^>]*/?\\s*>");
|
|
|
|
|
private static final Pattern ARTICLE_TAG_PATTERN = Pattern.compile("<\\s*meta[^>]*property\\s*=\\s*\"\\s*article:([^\"]+)\"[^>]*/?\\s*>");
|
|
|
|
|
private static final Pattern OPEN_GRAPH_CONTENT_PATTERN = Pattern.compile("content\\s*=\\s*\"([^\"]*)\"");
|
|
|
|
|
private static final Pattern TITLE_PATTERN = Pattern.compile("<\\s*title[^>]*>(.*)<\\s*/title[^>]*>");
|
|
|
|
|
private static final Pattern FAVICON_PATTERN = Pattern.compile("<\\s*link[^>]*rel\\s*=\\s*\".*icon.*\"[^>]*>");
|
|
|
|
|
private static final Pattern FAVICON_HREF_PATTERN = Pattern.compile("href\\s*=\\s*\"([^\"]*)\"");
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return All whitelisted URLs in the source text.
|
|
|
|
@ -82,4 +95,126 @@ public final class LinkPreviewUtil {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static @NonNull OpenGraph parseOpenGraphFields(@Nullable String html) {
|
|
|
|
|
return parseOpenGraphFields(html, text -> Html.fromHtml(text).toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static @NonNull OpenGraph parseOpenGraphFields(@Nullable String html, @NonNull HtmlDecoder htmlDecoder) {
|
|
|
|
|
if (html == null) {
|
|
|
|
|
return new OpenGraph(Collections.emptyMap(), null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, String> openGraphTags = new HashMap<>();
|
|
|
|
|
Matcher openGraphMatcher = OPEN_GRAPH_TAG_PATTERN.matcher(html);
|
|
|
|
|
|
|
|
|
|
while (openGraphMatcher.find()) {
|
|
|
|
|
String tag = openGraphMatcher.group();
|
|
|
|
|
String property = openGraphMatcher.groupCount() > 0 ? openGraphMatcher.group(1) : null;
|
|
|
|
|
|
|
|
|
|
if (property != null) {
|
|
|
|
|
Matcher contentMatcher = OPEN_GRAPH_CONTENT_PATTERN.matcher(tag);
|
|
|
|
|
if (contentMatcher.find() && contentMatcher.groupCount() > 0) {
|
|
|
|
|
String content = htmlDecoder.fromEncoded(contentMatcher.group(1));
|
|
|
|
|
openGraphTags.put(property.toLowerCase(), content);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Matcher articleMatcher = ARTICLE_TAG_PATTERN.matcher(html);
|
|
|
|
|
|
|
|
|
|
while (articleMatcher.find()) {
|
|
|
|
|
String tag = articleMatcher.group();
|
|
|
|
|
String property = articleMatcher.groupCount() > 0 ? articleMatcher.group(1) : null;
|
|
|
|
|
|
|
|
|
|
if (property != null) {
|
|
|
|
|
Matcher contentMatcher = OPEN_GRAPH_CONTENT_PATTERN.matcher(tag);
|
|
|
|
|
if (contentMatcher.find() && contentMatcher.groupCount() > 0) {
|
|
|
|
|
String content = htmlDecoder.fromEncoded(contentMatcher.group(1));
|
|
|
|
|
openGraphTags.put(property.toLowerCase(), content);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String htmlTitle = "";
|
|
|
|
|
String faviconUrl = "";
|
|
|
|
|
|
|
|
|
|
Matcher titleMatcher = TITLE_PATTERN.matcher(html);
|
|
|
|
|
if (titleMatcher.find() && titleMatcher.groupCount() > 0) {
|
|
|
|
|
htmlTitle = htmlDecoder.fromEncoded(titleMatcher.group(1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Matcher faviconMatcher = FAVICON_PATTERN.matcher(html);
|
|
|
|
|
if (faviconMatcher.find()) {
|
|
|
|
|
Matcher faviconHrefMatcher = FAVICON_HREF_PATTERN.matcher(faviconMatcher.group());
|
|
|
|
|
if (faviconHrefMatcher.find() && faviconHrefMatcher.groupCount() > 0) {
|
|
|
|
|
faviconUrl = faviconHrefMatcher.group(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new OpenGraph(openGraphTags, htmlTitle, faviconUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static @Nullable String parseTopLevelDomain(@NonNull String domain) {
|
|
|
|
|
int periodIndex = domain.lastIndexOf(".");
|
|
|
|
|
|
|
|
|
|
if (periodIndex >= 0 && periodIndex < domain.length() - 1) {
|
|
|
|
|
return domain.substring(periodIndex + 1);
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final class OpenGraph {
|
|
|
|
|
|
|
|
|
|
private final Map<String, String> values;
|
|
|
|
|
|
|
|
|
|
private final @Nullable String htmlTitle;
|
|
|
|
|
private final @Nullable String faviconUrl;
|
|
|
|
|
|
|
|
|
|
private static final String KEY_TITLE = "title";
|
|
|
|
|
private static final String KEY_DESCRIPTION_URL = "description";
|
|
|
|
|
private static final String KEY_IMAGE_URL = "image";
|
|
|
|
|
private static final String KEY_PUBLISHED_TIME_1 = "published_time";
|
|
|
|
|
private static final String KEY_PUBLISHED_TIME_2 = "article:published_time";
|
|
|
|
|
private static final String KEY_MODIFIED_TIME_1 = "modified_time";
|
|
|
|
|
private static final String KEY_MODIFIED_TIME_2 = "article:modified_time";
|
|
|
|
|
|
|
|
|
|
public OpenGraph(@NonNull Map<String, String> values, @Nullable String htmlTitle, @Nullable String faviconUrl) {
|
|
|
|
|
this.values = values;
|
|
|
|
|
this.htmlTitle = htmlTitle;
|
|
|
|
|
this.faviconUrl = faviconUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public @NonNull Optional<String> getTitle() {
|
|
|
|
|
return Optional.of(Util.getFirstNonEmpty(values.get(KEY_TITLE), htmlTitle));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public @NonNull Optional<String> getImageUrl() {
|
|
|
|
|
return Optional.of(Util.getFirstNonEmpty(values.get(KEY_IMAGE_URL), faviconUrl));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressLint("ObsoleteSdkInt")
|
|
|
|
|
public long getDate() {
|
|
|
|
|
return Stream.of(values.get(KEY_PUBLISHED_TIME_1),
|
|
|
|
|
values.get(KEY_PUBLISHED_TIME_2),
|
|
|
|
|
values.get(KEY_MODIFIED_TIME_1),
|
|
|
|
|
values.get(KEY_MODIFIED_TIME_2))
|
|
|
|
|
.map(DateUtils::parseIso8601)
|
|
|
|
|
.filter(time -> time > 0)
|
|
|
|
|
.findFirst()
|
|
|
|
|
.orElse(0L);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public @NonNull
|
|
|
|
|
Optional<String> getDescription() {
|
|
|
|
|
return Optional.of(values.get(KEY_DESCRIPTION_URL));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface HtmlDecoder {
|
|
|
|
|
@NonNull String fromEncoded(@NonNull String html);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|