dh_ackergaul
vor 3 Tagen 5bbf43c1b146439ab882815c12ed6292f1d7b4df
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
/**
 * HomeviewerController.js
 *
 * This controller handles home viewer pages
 */
 
const FurnplanInstanceHistoryService = require("../services/FurnplanInstaceHistoryService.js");
 
module.exports = {
 
    /*
     * Form
     */
    form: function (req, res) {
        return res.view();
    },
 
    form_post: function (req, res) {
        if (!req.param("number") || req.param("number").length === 0) {
            return res.json({ "status": "error", "message": req.__("fv.home.form.error.number.missing") });
        }
 
        const number = req.param("number").replace(" ", "");
 
        // use access manager for protection from brute force attacks
        AccessManagerService.create(req.auth_cookie);
 
        const am = req.auth_cookie.accessManager;
 
        am.setMaxTries(3).setPause(15);
 
        // if the user tried more than 3 times a wrong number
        const waitMessage = {
            "status": "error",
            "message": req.__("fv.home.form.error.number.wait"),
            "seconds": am.getSeconds()
        };
 
        if (!am.canTry()) return res.json(waitMessage);
 
        Project.findOne({ number: number }).exec(function (err, project) {
            try {
                if (err) throw err;
 
                if (project) {
 
                    if (project.expiresAt.setDate(project.expiresAt.getDate() + 1) < new Date()) {
                        am.failed();
                        // send message according to remaining tries
                        if (!am.canTry()) {
                            waitMessage.seconds = am.getSeconds();
                            return res.json(waitMessage);
                        }
                        return res.json({ "status": "error", "message": req.__("fv.home.form.error.expired") });
                    }
                    // delete access manager if everything went fine
                    delete am;
                    delete req.auth_cookie.accessManager;
 
                    return res.json({ "status": "ok", "path": "/homeviewer/" + project.identifier });
                }
                else {
                    throw "Project not Found";
                }
            }
            catch (e) {
                // increase try count if the number doesn't exist
                am.failed();
                // send message according to remaining tries
                if (!am.canTry()) {
                    waitMessage.seconds = am.getSeconds();
                    return res.json(waitMessage);
                }
                else {
                    return res.json({ "status": "error", "message": req.__("fv.home.form.error.number.wrong") });
                }
            }
        });
    },
 
    /**
     * Viewer page by guid (for uploaded data)
     */
    guid: async function (req, res) {
        let hideBackButton = false;
 
        AccessManagerService.create(req.auth_cookie);
 
        const am = req.auth_cookie.accessManager;
 
        am.setMaxTries(3).setPause(15);
 
        if (!am.canTry()) {
            return res.view("error/index", {
                data: {
                    title: "fv.error.title.error",
                    url: "/homeviewer/form",
                    message: "fv.error.to_many_reloads"
                }
            });
        }
 
        try {
            let configuration;
            let configId = "";
 
            const project = await Project.findOne({ $or: [{ identifier: req.param("guid") }, { number: req.param("guid") }] });
 
            if (!project) {
                Winston.warn("Can't load project with guid " + req.param("guid"));
            }
 
            const customerNo = project.get("customerNo");
 
            const query = await UseCaseConfigurationStorePermissionService.addStorePermission(
                {
                    customerNo,
                    usageIntention: "homeviewer"
                },
                customerNo
            );
 
            const customHomeviewerConfiguration = await UseCaseConfiguration.findOne(query);
 
            if (customHomeviewerConfiguration) {
                const newConfiguration = await ConfigurationManager.newConfiguration();
 
                configuration = customHomeviewerConfiguration.get("configuration");
                configuration = ConfigurationManager.merge(newConfiguration, configuration);
                configId = customHomeviewerConfiguration._id;
 
                hideBackButton = true;
            }
            else {
                configuration = await ConfigurationManager.getHomeviewerConfiguration();
 
                if (req.options.readOnly) {
                    configuration.toolbar_button_homeviewer_finish_planning = false;
                }
            }
 
            if (project.expiresAt.setDate(project.expiresAt.getDate() + 1) < new Date()) {
                throw Error("fv.home.form.error.expired");
            }
 
            // guid was valid, so authenticate the visitor anonymously if he isn't authenticated yet
            const user = req.user || await Opus.login(project.customerNo, "homeViewer", "homeViewer");
 
            user.data.cloudID = project.number;
 
            // add anonymous role and save user in session
            if (user.permissions.indexOf("anonymous") < 0) {
                user.permissions.push("anonymous");
            }
 
            await Session.update({ _id: req.session._id }, { $addToSet: { users: user } });
 
            // save hit to this project for usage statistics
            const hit = await Hit.create({
                client: req.headers["user-agent"],
                project: project
            });
 
            if (!hit) {
                throw Error("Creation of hit has failed for project guid " + req.param("guid"));
            }
 
            await FurnplanInstanceHistoryService.writeDocument(user.opusSessionId, user.customerNo, req.url, configId, "Homeviewer", req.headers["user-agent"] || req.headers["User-Agent"] || "");
 
            return res.view({
                project: project.toJSON(),
                g_contact: {
                    contactEmail: project.get("contactEmail"),
                    contactPhone: project.get("contactPhone"),
                    contactName: project.get("contactName")
                },
                isLocalRequest: Helper.isLocalRequest(req.connection.remoteAddress),
                g_auth_id: req.auth_cookie.id,
                g_configuration: configuration,
                g_sessionId: user.opusSessionId,
                g_language: req.query.lang || req.getLocale(),
                g_reCaptcha: Config.furnview.reCaptchaSiteKey || ""
            });
        }
        catch (e) {
            am.failed();
 
            // send message according to remaining tries
            if (!am.canTry()) {
                return res.view("error/index", {
                    data: {
                        title: "fv.error.title.error",
                        url: "/homeviewer/form",
                        message: e.message,
                        hideBackButton
                    }
                });
            }
 
            return res.view("error/index", {
                data: {
                    title: "fv.error.title.error",
                    url: "/homeviewer/form",
                    message: e.message || "fv.error.to_many_reloads",
                    hideBackButton
                }
            });
        }
    },
 
    sendRequest: async function (req, res) {
        req.body.toMyselfOnly = req.body.toMyselfOnly === "true";
 
        const user = req.user;
 
        if (!user) {
            return res.status(403).json({ status: "forbidden" });
        }
 
        try {
            const cleanNumber = req.body.number.replace(" ", "");
            const exportProject = await Project.findOne({ number: cleanNumber });
 
            const dealerMailLocals = {
                number: req.body.number,
                mail: exportProject.get("email"),
                name: req.body.firstName + " " + req.body.lastName,
                phone: req.body.phone,
                message: req.body.message || "-"
            };
 
            const customerMailLocals = {
                number: req.body.number,
                link: `https://view.furnplan.de/homeviewer/${exportProject.get("identifier")}`,
 
                name: req.body.firstName + " " + req.body.lastName,
                phone: req.body.phone,
                mail: exportProject.get("email"),
                message: req.body.message || "-",
 
                sellerName: exportProject.get("contactName"),
                sellerPhone: exportProject.get("contactPhone"),
                sellerMail: exportProject.get("contactEmail")
            };
 
            Mailer.sendHomeviewerRequest(exportProject.get("contactEmail"), exportProject.get("email"), dealerMailLocals, customerMailLocals, exportProject.get("uiLanguage"), req.body.toMyselfOnly);
 
            return res.json({ status: "ok" });
        }
        catch (e) {
            return res.status(404).json({ status: "not found" });
        }
    },
 
    shareByMail: async function (req, res) {
        const user = req.user;
 
        if (!user) {
            return res.status(403).json({ status: "forbidden" });
        }
 
        try {
            const cleanNumber = req.body.number.replace(" ", "");
            const exportProject = await Project.findOne({ number: cleanNumber });
 
            const locals = {
                number: req.body.number,
                link: `https://view.furnplan.de/shareviewer/${exportProject.get("identifier")}`
            };
 
            Mailer.shareHomeviewerByMail(exportProject.get("email"), locals, exportProject.get("uiLanguage"));
 
            return res.json({ status: "ok" });
        }
        catch (e) {
            return res.status(404).json({ status: "not found" });
        }
    }
};