2018-08-02 13:33:06 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
|
|
/*
|
2018-08-14 05:53:24 +00:00
|
|
|
This file is part of FairEmail.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-14 05:53:24 +00:00
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
2018-08-02 13:33:06 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
2018-10-29 10:46:49 +00:00
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
2018-08-02 13:33:06 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2018-10-29 10:46:49 +00:00
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-12-31 08:04:33 +00:00
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
2018-08-02 13:33:06 +00:00
|
|
|
*/
|
|
|
|
|
|
2018-12-14 10:11:45 +01:00
|
|
|
import android.content.Context;
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.util.Base64;
|
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
|
import org.jsoup.nodes.Document;
|
2018-08-28 09:10:31 +00:00
|
|
|
import org.jsoup.nodes.Element;
|
2018-08-02 13:33:06 +00:00
|
|
|
import org.jsoup.nodes.Node;
|
|
|
|
|
import org.jsoup.nodes.TextNode;
|
2018-08-28 12:52:33 +00:00
|
|
|
import org.jsoup.safety.Whitelist;
|
2018-08-02 13:33:06 +00:00
|
|
|
import org.jsoup.select.NodeTraversor;
|
|
|
|
|
import org.jsoup.select.NodeVisitor;
|
|
|
|
|
|
2019-01-16 13:42:17 +00:00
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
|
import java.io.BufferedOutputStream;
|
2018-12-14 10:11:45 +01:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
2019-01-16 13:42:17 +00:00
|
|
|
import java.io.OutputStream;
|
2018-12-14 10:11:45 +01:00
|
|
|
import java.net.URL;
|
2019-01-05 11:17:33 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
2018-08-05 08:53:43 +00:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-09-12 11:54:16 +00:00
|
|
|
public class HtmlHelper {
|
2019-01-05 11:17:33 +00:00
|
|
|
private static final int PREVIEW_SIZE = 250;
|
2018-09-02 11:18:32 +00:00
|
|
|
private static Pattern pattern = Pattern.compile("([http|https]+://[\\w\\S(\\.|:|/)]+)");
|
2019-01-05 11:17:33 +00:00
|
|
|
private static final List<String> heads = Arrays.asList("p", "h1", "h2", "h3", "h4", "h5", "tr");
|
|
|
|
|
private static final List<String> tails = Arrays.asList("br", "dd", "dt", "p", "h1", "h2", "h3", "h4", "h5");
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-12-14 20:15:07 +01:00
|
|
|
static String getBody(String html) {
|
|
|
|
|
return Jsoup.parse(html).body().html();
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 15:19:07 +01:00
|
|
|
static String sanitize(String html, boolean quotes) {
|
2018-12-14 10:05:48 +01:00
|
|
|
Document document = Jsoup.parse(Jsoup.clean(html, Whitelist
|
|
|
|
|
.relaxed()
|
|
|
|
|
.addProtocols("img", "src", "cid")
|
|
|
|
|
.addProtocols("img", "src", "data")));
|
2018-11-24 12:27:44 +01:00
|
|
|
|
2018-09-12 11:54:16 +00:00
|
|
|
for (Element tr : document.select("tr"))
|
|
|
|
|
tr.after("<br>");
|
2018-11-24 12:27:44 +01:00
|
|
|
|
2018-11-24 12:42:21 +01:00
|
|
|
for (Element img : document.select("img")) {
|
|
|
|
|
boolean linked = false;
|
|
|
|
|
for (Element parent : img.parents())
|
|
|
|
|
if ("a".equals(parent.tagName())) {
|
|
|
|
|
linked = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!linked) {
|
2018-11-24 12:27:44 +01:00
|
|
|
String src = img.attr("src");
|
|
|
|
|
if (src.startsWith("http://") || src.startsWith("https://")) {
|
|
|
|
|
Element a = document.createElement("a");
|
|
|
|
|
a.attr("href", src);
|
|
|
|
|
img.replaceWith(a);
|
|
|
|
|
a.appendChild(img);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-24 12:42:21 +01:00
|
|
|
}
|
2018-11-24 12:27:44 +01:00
|
|
|
|
2018-12-21 15:19:07 +01:00
|
|
|
if (!quotes)
|
|
|
|
|
for (Element quote : document.select("blockquote"))
|
|
|
|
|
quote.text("…");
|
|
|
|
|
|
2018-09-12 11:54:16 +00:00
|
|
|
NodeTraversor.traverse(new NodeVisitor() {
|
|
|
|
|
@Override
|
|
|
|
|
public void head(Node node, int depth) {
|
|
|
|
|
if (node instanceof TextNode) {
|
|
|
|
|
String text = ((TextNode) node).text();
|
|
|
|
|
Matcher matcher = pattern.matcher(text);
|
|
|
|
|
while (matcher.find()) {
|
|
|
|
|
String ref = matcher.group();
|
|
|
|
|
text = text.replace(ref, String.format("<a href=\"%s\">%s</a>", ref, ref));
|
2018-09-02 11:18:32 +00:00
|
|
|
}
|
2018-09-12 11:54:16 +00:00
|
|
|
node.before(text);
|
|
|
|
|
((TextNode) node).text("");
|
2018-09-02 11:18:32 +00:00
|
|
|
}
|
2018-09-12 11:54:16 +00:00
|
|
|
}
|
2018-09-02 11:18:32 +00:00
|
|
|
|
2018-09-12 11:54:16 +00:00
|
|
|
@Override
|
|
|
|
|
public void tail(Node node, int depth) {
|
|
|
|
|
}
|
|
|
|
|
}, document.body());
|
2018-12-14 20:15:07 +01:00
|
|
|
|
2018-09-12 11:54:16 +00:00
|
|
|
return document.body().html();
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2018-12-14 10:11:45 +01:00
|
|
|
|
|
|
|
|
static Drawable decodeImage(String source, Context context, long id, boolean show) {
|
2018-12-30 14:35:19 +00:00
|
|
|
int px = Helper.dp2pixels(context, 48);
|
2018-12-14 10:11:45 +01:00
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(source)) {
|
|
|
|
|
Drawable d = context.getResources().getDrawable(R.drawable.baseline_broken_image_24, context.getTheme());
|
2019-01-27 11:17:58 +00:00
|
|
|
d.setBounds(0, 0, px, px);
|
2018-12-14 10:11:45 +01:00
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean embedded = source.startsWith("cid:");
|
|
|
|
|
boolean data = source.startsWith("data:");
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Image embedded=" + embedded + " data=" + data + " source=" + source);
|
2018-12-14 10:11:45 +01:00
|
|
|
|
|
|
|
|
if (show) {
|
|
|
|
|
// Embedded images
|
|
|
|
|
if (embedded) {
|
2019-01-07 07:43:50 +00:00
|
|
|
String cid = "<" + source.substring(4) + ">";
|
2018-12-14 10:11:45 +01:00
|
|
|
EntityAttachment attachment = DB.getInstance(context).attachment().getAttachment(id, cid);
|
2018-12-20 19:32:00 +01:00
|
|
|
if (attachment == null) {
|
|
|
|
|
Drawable d = context.getResources().getDrawable(R.drawable.baseline_broken_image_24, context.getTheme());
|
|
|
|
|
d.setBounds(0, 0, px, px);
|
|
|
|
|
return d;
|
|
|
|
|
} else if (!attachment.available) {
|
2018-12-14 10:11:45 +01:00
|
|
|
Drawable d = context.getResources().getDrawable(R.drawable.baseline_photo_library_24, context.getTheme());
|
|
|
|
|
d.setBounds(0, 0, px, px);
|
|
|
|
|
return d;
|
|
|
|
|
} else {
|
|
|
|
|
File file = EntityAttachment.getFile(context, attachment.id);
|
|
|
|
|
Drawable d = Drawable.createFromPath(file.getAbsolutePath());
|
|
|
|
|
if (d == null) {
|
|
|
|
|
d = context.getResources().getDrawable(R.drawable.baseline_broken_image_24, context.getTheme());
|
2019-01-27 11:17:58 +00:00
|
|
|
d.setBounds(0, 0, px, px);
|
2018-12-14 10:11:45 +01:00
|
|
|
} else
|
|
|
|
|
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
|
|
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Data URI
|
2018-12-20 16:45:42 +01:00
|
|
|
if (data)
|
|
|
|
|
try {
|
|
|
|
|
// "<img src=\"data:image/png;base64,iVBORw0KGgoAAA" +
|
|
|
|
|
// "ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4" +
|
|
|
|
|
// "//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU" +
|
|
|
|
|
// "5ErkJggg==\" alt=\"Red dot\" />";
|
|
|
|
|
|
|
|
|
|
String base64 = source.substring(source.indexOf(',') + 1);
|
|
|
|
|
byte[] bytes = Base64.decode(base64.getBytes(), 0);
|
|
|
|
|
|
|
|
|
|
Bitmap bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
|
|
|
|
|
if (bm == null)
|
|
|
|
|
throw new IllegalArgumentException("decode byte array failed");
|
|
|
|
|
|
|
|
|
|
Drawable d = new BitmapDrawable(context.getResources(), bm);
|
|
|
|
|
d.setBounds(0, 0, bm.getWidth(), bm.getHeight());
|
|
|
|
|
return d;
|
|
|
|
|
} catch (IllegalArgumentException ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(ex);
|
2018-12-20 16:45:42 +01:00
|
|
|
Drawable d = context.getResources().getDrawable(R.drawable.baseline_broken_image_24, context.getTheme());
|
2019-01-27 11:17:58 +00:00
|
|
|
d.setBounds(0, 0, px, px);
|
2018-12-20 16:45:42 +01:00
|
|
|
return d;
|
|
|
|
|
}
|
2018-12-14 10:11:45 +01:00
|
|
|
|
|
|
|
|
// Get cache folder
|
|
|
|
|
File dir = new File(context.getCacheDir(), "images");
|
|
|
|
|
if (!dir.exists())
|
|
|
|
|
dir.mkdir();
|
|
|
|
|
|
|
|
|
|
InputStream is = null;
|
2019-01-16 13:42:17 +00:00
|
|
|
OutputStream os = null;
|
2018-12-14 10:11:45 +01:00
|
|
|
try {
|
|
|
|
|
// Create unique file name
|
|
|
|
|
File file = new File(dir, id + "_" + source.hashCode());
|
|
|
|
|
|
|
|
|
|
// Get input stream
|
|
|
|
|
if (file.exists()) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Using cached " + file);
|
2019-01-16 13:42:17 +00:00
|
|
|
is = new BufferedInputStream(new FileInputStream(file));
|
2018-12-14 10:11:45 +01:00
|
|
|
} else {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Downloading " + source);
|
2019-01-27 11:17:58 +00:00
|
|
|
try {
|
|
|
|
|
is = new URL(source).openStream();
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
Log.w(ex);
|
|
|
|
|
Drawable d = context.getResources().getDrawable(R.drawable.baseline_cloud_off_24, context.getTheme());
|
|
|
|
|
d.setBounds(0, 0, px, px);
|
|
|
|
|
return d;
|
|
|
|
|
}
|
2018-12-14 10:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decode image from stream
|
|
|
|
|
Bitmap bm = BitmapFactory.decodeStream(is);
|
|
|
|
|
if (bm == null)
|
|
|
|
|
throw new IllegalArgumentException("decode stream failed");
|
|
|
|
|
|
|
|
|
|
// Cache bitmap
|
|
|
|
|
if (!file.exists()) {
|
2019-01-16 13:42:17 +00:00
|
|
|
os = new BufferedOutputStream(new FileOutputStream(file));
|
2018-12-14 10:11:45 +01:00
|
|
|
bm.compress(Bitmap.CompressFormat.PNG, 100, os);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create drawable from bitmap
|
|
|
|
|
Drawable d = new BitmapDrawable(context.getResources(), bm);
|
|
|
|
|
d.setBounds(0, 0, bm.getWidth(), bm.getHeight());
|
|
|
|
|
return d;
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
// Show warning icon
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(ex);
|
2018-12-14 10:11:45 +01:00
|
|
|
Drawable d = context.getResources().getDrawable(R.drawable.baseline_broken_image_24, context.getTheme());
|
2019-01-27 11:17:58 +00:00
|
|
|
d.setBounds(0, 0, px, px);
|
2018-12-14 10:11:45 +01:00
|
|
|
return d;
|
|
|
|
|
} finally {
|
|
|
|
|
// Close streams
|
|
|
|
|
if (is != null) {
|
|
|
|
|
try {
|
|
|
|
|
is.close();
|
|
|
|
|
} catch (IOException e) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(e);
|
2018-12-14 10:11:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (os != null) {
|
|
|
|
|
try {
|
|
|
|
|
os.close();
|
|
|
|
|
} catch (IOException e) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.w(e);
|
2018-12-14 10:11:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Show placeholder icon
|
|
|
|
|
int resid = (embedded || data ? R.drawable.baseline_photo_library_24 : R.drawable.baseline_image_24);
|
|
|
|
|
Drawable d = context.getResources().getDrawable(resid, context.getTheme());
|
|
|
|
|
d.setBounds(0, 0, px, px);
|
|
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-14 12:04:50 +01:00
|
|
|
|
2018-12-24 20:09:47 +00:00
|
|
|
static String getPreview(String body) {
|
|
|
|
|
String text = (body == null ? null : Jsoup.parse(body).text());
|
2019-01-05 11:17:33 +00:00
|
|
|
return (text == null ? null : text.substring(0, Math.min(text.length(), PREVIEW_SIZE)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static String getText(String html) {
|
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
NodeTraversor.traverse(new NodeVisitor() {
|
|
|
|
|
public void head(Node node, int depth) {
|
|
|
|
|
if (node instanceof TextNode)
|
|
|
|
|
sb.append(((TextNode) node).text());
|
|
|
|
|
else {
|
|
|
|
|
String name = node.nodeName();
|
|
|
|
|
if (name.equals("li"))
|
|
|
|
|
sb.append("\n * ");
|
|
|
|
|
else if (name.equals("dt"))
|
|
|
|
|
sb.append(" ");
|
|
|
|
|
else if (heads.contains(name))
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void tail(Node node, int depth) {
|
|
|
|
|
String name = node.nodeName();
|
|
|
|
|
if (tails.contains(name))
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
else if (name.equals("a"))
|
|
|
|
|
sb.append(" <").append(node.absUrl("href")).append(">");
|
|
|
|
|
}
|
|
|
|
|
}, Jsoup.parse(html));
|
|
|
|
|
|
|
|
|
|
return sb.toString();
|
2018-12-24 20:09:47 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|