const express = require('express'); const path = require('path'); const Module = require('../../base/Module'); const RepositorySystem = require('../../systems/RepositorySystem'); const RoutingSystem = require('../../systems/RoutingSystem'); const MailSystem = require("../../systems/MailSystem"); const Config = require('../../systems/config'); const rp = require('request-promise'); /*let ConctactScheme = { fields: [ { key: "firstname", type: "text" }, { key: "lastname", type: "text" }, { key: "phone", type: "text" }, { key: "email", type: "text" }, { key: "description", type: "text" } ] }; */ class Conctact extends Module { constructor(){ super('/', [2,0,3,4,0], 'Contact'); //this.repository = RepositorySystem.create('Contact', ConctactScheme); this.router = express.Router(); this.regVerify = new RegExp("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"); this.init(); } init() { let routes = [ { type: 'custom', method: 'post', endpoint: '/contact', customFn: (req,res) => { let data = req.body this.verifyInfo(data) .then(() => { var html = this.createMailBody(data) MailSystem.send(Config.MAIL_SENDER,Config.MAIL_SENDER,'Contact',html) .then((data) => { res.sendStatus(200) }) }) .catch((e) => { console.log(e); res.send(400) }) } } ]; RoutingSystem.loadRoutes(routes, this.router); } verifyInfo(data) { let email = data.email; let recaptchaToken = data.captcha; return new Promise((resolve,reject) => { if(this.regVerify.test(email)){ resolve(); }else{ reject(); } }).then(() => { var options = { headers: {'content-type' : 'application/x-www-form-urlencoded'}, method: 'POST', uri: 'https://www.google.com/recaptcha/api/siteverify', body: "secret=" + Config.CAPTCHA_SECRET + "&response=" + recaptchaToken, json: false // Automatically stringifies the body to JSON }; return rp(options).then((res) => JSON.parse(res)) .then((data) => { if( data.success === true || data.success === "true" ) return data; throw new Error("Captcha is wrong"); }) }) } createMailBody(data){ let title = "

Αίτημα Επικοινωνίας

"; let body = ""+title+"

\ Ο χρήστης "+data.firstname +" "+data.lastname+" \ έγραψε:

\ "+data.description+"\
Στοιχεία Επικοινωνίας:
\

\

"; return body; } } module.exports = Conctact;