123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 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 = "<h2>Αίτημα Επικοινωνίας</h2>";
- let body = "<html>"+title+"<p> \
- Ο χρήστης "+data.firstname +" "+data.lastname+" \
- έγραψε:<br /><br />\
- "+data.description+"\
- <br /> Στοιχεία Επικοινωνίας: <br />\
- <ul>\
- <li> Τηλεφωνο :"+data.phone+"</li>\
- <li> e-mail: "+data.email+"</li>\
- </ul>\
- </p></html>";
- return body;
- }
- }
- module.exports = Conctact;
|