package de.srsoftware.widerhall; import de.srsoftware.tools.translations.Translation; import de.srsoftware.widerhall.data.MailingList; import de.srsoftware.widerhall.data.User; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Part; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Map; import java.util.stream.Collectors; import static de.srsoftware.widerhall.Constants.*; public class Util { private static final MessageDigest SHA256 = getSha256(); private static final String EMAIL_PATTERN = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"; public static String urlEncode(Map data) { String params = data.entrySet() .stream() .map(entry -> encode(entry.getKey()) + "=" + encode(entry.getValue())) .collect(Collectors.joining("&")); return params; } private static String encode(Object value) { return URLEncoder.encode(value.toString(), StandardCharsets.UTF_8); } public static MessageDigest getSha256() { try { return MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } } public static String sha256(String s) { byte[] bytes = SHA256.digest(s.getBytes(StandardCharsets.UTF_8)); return hex(bytes); } private static String hex(byte[] bytes) { StringBuffer buf = new StringBuffer(bytes.length*2); for (var byt : bytes) buf.append(hex(byt)); return buf.toString(); } public static String hex(int b){ int lower = b & 0x0F; int upper = (b & 0xF0) >> 4; return (char)(upper < 10 ? '0'+upper : 'A'+upper-10)+""+(char)(lower < 10 ? '0'+lower : 'A'+lower-10); } public static String t(String tx, Object ... fills){ return Translation.get(Application.class,tx,fills); } public static boolean isEmail(String email) { return email.matches(EMAIL_PATTERN); } public static boolean simplePassword(String pass) { if (pass.length() < 6) return true; if (pass.length() < 8){ for (int i=0; i 0) value ^= flag; } return value; } public static User getUser(HttpServletRequest req) { var o = req.getSession().getAttribute(USER); return o instanceof User ? (User) o : null; } public static String getPath(HttpServletRequest req) { var path = req.getPathInfo(); return path == null ? INDEX : path.substring(1); } public static MailingList getMailingList(HttpServletRequest req) { var listEmail = req.getParameter(LIST); if (listEmail == null || listEmail.isBlank()) return null; return MailingList.load(listEmail); } public static boolean getCheckbox(HttpServletRequest req, String key) { return "on".equals(req.getParameter(key)); } /** * Return the primary text content of the message. */ public static String getText(Part p) throws MessagingException, IOException { // https://javaee.github.io/javamail/FAQ if (p.isMimeType("text/*")) return (String)p.getContent(); if (p.isMimeType("multipart/alternative")) { // prefer html text over plain text Multipart mp = (Multipart)p.getContent(); String text = null; for (int i = 0; i < mp.getCount(); i++) { Part bp = mp.getBodyPart(i); if (bp.isMimeType("text/plain")) { if (text == null) text = getText(bp); continue; } else if (bp.isMimeType("text/html")) { String s = getText(bp); if (s != null) return s; } else { return getText(bp); } } return text; } else if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart)p.getContent(); for (int i = 0; i < mp.getCount(); i++) { String s = getText(mp.getBodyPart(i)); if (s != null) return s; } } return null; } public static String dropEmail(String tx) { return tx.replaceAll( "[.\\-\\w]+@[.\\-\\w]+", "[email_removed]"); } }