index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const express = require('express');
  2. const path = require('path');
  3. const Module = require('../../base/Module');
  4. const RepositorySystem = require('../../systems/RepositorySystem');
  5. const RoutingSystem = require('../../systems/RoutingSystem');
  6. const MailSystem = require("../../systems/MailSystem");
  7. const Config = require('../../systems/config');
  8. const rp = require('request-promise');
  9. /*let ConctactScheme = {
  10. fields: [
  11. {
  12. key: "firstname",
  13. type: "text"
  14. },
  15. {
  16. key: "lastname",
  17. type: "text"
  18. },
  19. {
  20. key: "phone",
  21. type: "text"
  22. },
  23. {
  24. key: "email",
  25. type: "text"
  26. },
  27. {
  28. key: "description",
  29. type: "text"
  30. }
  31. ]
  32. };
  33. */
  34. class Conctact extends Module {
  35. constructor(){
  36. super('/', [2,0,3,4,0], 'Contact');
  37. //this.repository = RepositorySystem.create('Contact', ConctactScheme);
  38. this.router = express.Router();
  39. 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])?)*$");
  40. this.init();
  41. }
  42. init() {
  43. let routes = [
  44. {
  45. type: 'custom',
  46. method: 'post',
  47. endpoint: '/contact',
  48. customFn: (req,res) => {
  49. let data = req.body
  50. this.verifyInfo(data)
  51. .then(() => {
  52. var html = this.createMailBody(data)
  53. MailSystem.send(Config.MAIL_SENDER,Config.MAIL_SENDER,'Contact',html)
  54. .then((data) => {
  55. res.sendStatus(200)
  56. })
  57. })
  58. .catch((e) => {
  59. console.log(e);
  60. res.send(400)
  61. })
  62. }
  63. }
  64. ];
  65. RoutingSystem.loadRoutes(routes, this.router);
  66. }
  67. verifyInfo(data) {
  68. let email = data.email;
  69. let recaptchaToken = data.captcha;
  70. return new Promise((resolve,reject) => {
  71. if(this.regVerify.test(email)){
  72. resolve();
  73. }else{
  74. reject();
  75. }
  76. }).then(() => {
  77. var options = {
  78. headers: {'content-type' : 'application/x-www-form-urlencoded'},
  79. method: 'POST',
  80. uri: 'https://www.google.com/recaptcha/api/siteverify',
  81. body: "secret=" + Config.CAPTCHA_SECRET + "&response=" + recaptchaToken,
  82. json: false // Automatically stringifies the body to JSON
  83. };
  84. return rp(options).then((res) => JSON.parse(res))
  85. .then((data) => {
  86. if( data.success === true || data.success === "true" )
  87. return data;
  88. throw new Error("Captcha is wrong");
  89. })
  90. })
  91. }
  92. createMailBody(data){
  93. let title = "<h2>Αίτημα Επικοινωνίας</h2>";
  94. let body = "<html>"+title+"<p> \
  95. Ο χρήστης "+data.firstname +" "+data.lastname+" \
  96. έγραψε:<br /><br />\
  97. "+data.description+"\
  98. <br /> Στοιχεία Επικοινωνίας: <br />\
  99. <ul>\
  100. <li> Τηλεφωνο :"+data.phone+"</li>\
  101. <li> e-mail: "+data.email+"</li>\
  102. </ul>\
  103. </p></html>";
  104. return body;
  105. }
  106. }
  107. module.exports = Conctact;