aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/Settings.java
blob: 8fe14615965ed1d7871798dfcb1e75d931f5a0a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Juick
 * Copyright (C) 2008-2013, Ugnich Anton
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.juick.www;

import com.juick.server.CrosspostQueries;
import com.juick.server.SubscriptionsQueries;
import com.juick.server.TagQueries;
import com.juick.server.UserQueries;
import com.juick.server.helpers.NotifyOpts;
import com.juick.server.helpers.UserInfo;
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 *
 * @author Ugnich Anton
 */
public class Settings {
    private static final Logger logger = LoggerFactory.getLogger(Settings.class);
    private final String imgPath;

    public Settings(String avatarsPath) {
        this.imgPath = avatarsPath;
    }

    protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, PebbleException {
        com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
        if (visitor.getUID() == 0) {
            response.sendRedirect("/login");
        }
        List<String> pages = Arrays.asList("main", "password", "about", "auth-email", "privacy");
        String page = request.getParameter("page");
        if (StringUtils.isEmpty(page) || !pages.contains(page)) {
            page = "main";
        }

        response.setContentType("text/html; charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            PebbleTemplate template = Utils.getEngine().getTemplate(String.format("views/settings_%s.html", page));
            Map<String, Object> context = new HashMap<>();
            context.put("title", "Настройки");
            context.put("visitor", visitor);
            context.put("tags", TagQueries.getPopularTags(sql));
            context.put("auths", UserQueries.getAuthCodes(sql, visitor));
            context.put("eopts", UserQueries.getEmailOpts(sql, visitor));
            context.put("ehash", UserQueries.getEmailHash(sql, visitor));
            context.put("emails", UserQueries.getEmails(sql, visitor));
            context.put("jids", UserQueries.getAllJIDs(sql, visitor));
            List<String> hours = IntStream.rangeClosed(0, 23).boxed()
                    .map(i -> StringUtils.leftPad(String.format("%d", i), 2, "0")).collect(Collectors.toList());
            context.put("hours", hours);
            context.put("fbstatus", CrosspostQueries.isFBCrossPostEnabled(sql, visitor.getUID()));
            context.put("twitter_name", CrosspostQueries.getTwitterName(sql, visitor.getUID()));
            context.put("telegram_name", CrosspostQueries.getTelegramName(sql, visitor.getUID()));
            context.put("notify_options", SubscriptionsQueries.getNotifyOptions(sql, visitor));
            context.put("userinfo", UserQueries.getUserInfo(sql, visitor));
            if (page.equals("auth-email")) {
                try {
                    String account = sql.queryForObject("SELECT account FROM auth WHERE user_id=? AND protocol='email' AND authcode=?",
                            String.class, visitor.getUID(), request.getParameter("code"));
                    sql.update("INSERT INTO emails(user_id,email) VALUES (?,?)", visitor.getUID(), account);
                    sql.update("DELETE FROM auth WHERE user_id=? AND authcode=?", visitor.getUID(), request.getParameter("code"));
                    context.put("result", "OK!");
                } catch (EmptyResultDataAccessException e) {
                    context.put("result", "Sorry, code unknown.");
                }
            }
            template.evaluate(out, context);
        }
    }

    protected void doPost(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, PebbleException {
        com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
        if (visitor.getUID() == 0) {
            response.sendError(400);
            return;
        }
        List<String> pages = Arrays.asList("main", "password", "about", "email", "email-add", "email-del",
                "email-subscr", "auth-email", "privacy", "jid-del", "twitter-del", "telegram-del", "facebook-disable",
                "facebook-enable", "vk-del");
        String page = request.getParameter("page");
        if (StringUtils.isEmpty(page) || !pages.contains(page)) {
            response.sendError(400);
            return;
        }
        String result = "";
        switch (page) {
            case "password":
                if (UserQueries.updatePassword(sql, visitor, request.getParameter("password"))) {
                    result = "<p>Password has been changed.</p>";
                    String hash = com.juick.server.UserQueries.getHashByUID(sql, visitor.getUID());
                    Cookie c = new Cookie("hash", hash);
                    c.setMaxAge(365 * 24 * 60 * 60);
                    response.addCookie(c);
                }
                break;
            case "main":
                NotifyOpts opts = new NotifyOpts();
                opts.setRepliesEnabled(StringUtils.isNotEmpty(request.getParameter("jnotify")));
                opts.setSubscriptionsEnabled(StringUtils.isNotEmpty(request.getParameter("subscr_notify")));
                opts.setRecommendationsEnabled(StringUtils.isNotEmpty(request.getParameter("recomm")));
                if (SubscriptionsQueries.setNotifyOptions(sql, visitor, opts)) {
                    result = "<p>Notification options has been updated</p>";
                }
                break;
            case "about":
                UserInfo info = new UserInfo();
                info.setFullName(request.getParameter("fullname"));
                info.setCountry(request.getParameter("country"));
                info.setUrl(request.getParameter("url"));
                info.setDescription(request.getParameter("descr"));
                String avatarTmpPath = Utils.receiveMultiPartFile(request, "avatar");
                if (StringUtils.isNotEmpty(avatarTmpPath)) {
                    String originalExtension = FilenameUtils.getExtension(avatarTmpPath);
                    String originalName = String.format("%s.%s", visitor.getUID(), originalExtension);
                    String targetName = String.format("%s.png", visitor.getUID());
                    Path ao = Paths.get(imgPath, "ao", originalName);
                    Path a = Paths.get(imgPath, "a", targetName);
                    Path as = Paths.get(imgPath, "as", targetName);
                    Files.move(Paths.get(Utils.getTmpDir(), avatarTmpPath), ao, StandardCopyOption.REPLACE_EXISTING);
                    Thumbnails.of(ao.toFile()).size(96, 96).toFile(a.toFile());
                    Thumbnails.of(ao.toFile()).size(32, 32).toFile(as.toFile());
                }
                if (UserQueries.updateUserInfo(sql, visitor, info)) {
                    result = String.format("<p>Your info is updated.</p><p><a href='/%s/'>Back to blog</a>.</p>", visitor.getUName());
                }
                break;
            case "jid-del":
                String[] params = request.getParameter("delete").split(";", 2);
                int res = -1;
                if (params[0].equals("xmpp")) {
                    res = sql.update("DELETE FROM jids WHERE user_id=? AND jid=?", visitor.getUID(), params[1]);
                } else if (params[0].equals("xmpp-unauth")) {
                    res = sql.update("DELETE FROM auth WHERE user_id=? AND protocol='xmpp' AND account=?", visitor.getUID(), params[1]);
                }
                if (res == 1) {
                    result = "<p>Deleted. <a href=\"/settings\">Back</a>.</p>";
                } else {
                    result = "<p>Error</p>";
                }
                break;
            case "email":
                String newHash = UserQueries.updateSecretEmail(sql, visitor);
                if (StringUtils.isNotEmpty(newHash)) {
                    result = String.format("<p>New secret email: <strong>%s@mail.juick.com</strong></p>" +
                    "<p><a href=\"/settings\">Back</a>.</p>", newHash);
                } else {
                    response.sendError(500);
                    return;
                }
                break;
            case "email-add":
                try {
                    sql.queryForObject("SELECT authcode FROM auth WHERE user_id=? AND protocol='email' " +
                            "AND account=?", String.class, visitor.getUID(), request.getParameter("account"));
                } catch (EmptyResultDataAccessException e) {
                    String authCode = UserQueries.generateHash(8);
                    if (sql.update("INSERT INTO auth(user_id,protocol,account,authcode) VALUES (?,'email',?,?)",
                            visitor.getUID(), request.getParameter("account"), authCode) > 0) {
                        Session session = Session.getDefaultInstance(System.getProperties());
                        try {
                            MimeMessage message = new MimeMessage(session);
                            message.setFrom(new InternetAddress("noreply@mail.juick.com"));
                            message.addRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("account")));
                            message.setSubject("Juick authorization link");
                            message.setText(String.format("Follow link to attach this email to Juick account:\n" +
                                    "http://juick.com/settings?page=auth-email&code=%s\n\n" +
                                    "If you don't know, what this mean - just ignore this mail.\n", authCode));
                            Transport.send(message);
                            result = "<p>Authorization link has been sent to your email. Follow it to proceed.</p>" +
                                    "<p><a href=\"/settings\">Back</a></p>";

                        } catch (MessagingException ex) {
                            logger.error("mail exception", ex);
                            response.sendError(500);
                            return;
                        }
                    }
                }
                break;
            case "email-del":
                if (sql.update("DELETE FROM emails WHERE user_id=? AND email=?", visitor.getUID(), request.getParameter("account")) > 0) {
                    result = "<p>Deleted. <a href=\"/settings\">Back</a>.</p>";
                } else {
                    result = "<p>An error occured while deleting.</p>";
                }
                break;
            case "email-subscr":
                sql.update("UPDATE emails SET subscr_hour=NULL WHERE user_id=?", visitor.getUID());
                String email = request.getParameter("account");
                if (StringUtils.isNotEmpty(email)) {
                    sql.update("UPDATE emails SET subscr_hour=? WHERE user_id=? AND email=?",
                            request.getParameter("time"), visitor.getUID(), email);
                    result = String.format("<p>Saved! Will send to <strong>%s</strong> at <strong>%s:00 GMT</strong>." +
                            "</p><p><a href=\"/settings\">Back</a></p>", email, request.getParameter("time"));
                } else {
                    result = "<p>Disabled.</p><p><a href=\"/settings\">Back</a></p>";
                }
                break;
            case "twitter-del":
                sql.update("DELETE FROM twitter WHERE user_id=?", visitor.getUID());
                sql.update("DELETE FROM subscr_users WHERE user_id=? AND suser_id=1741", visitor.getUID());
                for (Cookie cookie : request.getCookies()) {
                    if (cookie.getName().equals("request_token")) {
                        cookie.setMaxAge(0);
                        response.addCookie(cookie);
                    }
                    if (cookie.getName().equals("request_token_secret")) {
                        cookie.setMaxAge(0);
                        response.addCookie(cookie);
                    }
                }
                result = "<p><a href=\"/settings\">Back</a></p>";
                break;
            case "telegram-del":
                sql.update("DELETE FROM telegram WHERE user_id=?", visitor.getUID());
                result = "<p><a href=\"/settings\">Back</a></p>";
                break;
            case "facebook-disable":
                sql.update("UPDATE facebook SET crosspost=0 WHERE user_id=?", visitor.getUID());
                sql.update("DELETE FROM subscr_users WHERE user_id=? AND suser_id=5863", visitor.getUID());
                result = "<p><a href=\"/settings\">Back</a></p>";
                break;
            case "facebook-enable":
                sql.update("UPDATE facebook SET crosspost=1 WHERE user_id=?", visitor.getUID());
                sql.update("INSERT INTO subscr_users(user_id,suser_id,jid,active) VALUES (?,5863,'juick@facebook.juick.com',1)", visitor.getUID());
                result = "<p><a href=\"/settings\">Back</a></p>";
                break;
            case "vk-del":
                sql.update("DELETE FROM vk WHERE user_id=?", visitor.getUID());
                result = "<p><a href=\"/settings\">Back</a></p>";
                break;
            default:
                response.sendError(400);
                return;
        }
        response.setContentType("text/html; charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            PebbleTemplate template = Utils.getEngine().getTemplate("views/settings_result.html");
            Map<String, Object> context = new HashMap<>();
            context.put("title", "Настройки");
            context.put("visitor", visitor);
            context.put("result", result);
            template.evaluate(out, context);
        }
    }
}