forked from srophe/srophe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.xql
144 lines (135 loc) · 6.55 KB
/
email.xql
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
xquery version "3.1";
(:~
: Build email form returns. error or sucess message to ajax function.
: Use reCaptcha to filter out spam.
:)
import module namespace config="http://srophe.org/srophe/config" at "config.xqm";
import module namespace http="http://expath.org/ns/http-client";
declare namespace httpclient = "http://exist-db.org/xquery/httpclient";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace mail="http://exist-db.org/xquery/mail";
declare namespace request="http://exist-db.org/xquery/request";
declare option exist:serialize "method=xml media-type=text/xml indent=yes";
declare variable $VALIDATE_URI as xs:anyURI := xs:anyURI("https://www.google.com/recaptcha/api/siteverify");
(: Get reCaptcha private key for authentication :)
declare variable $secret-key := if($config:get-access-config//*:recaptcha-secret-key-variable != '') then
environment-variable($config:get-access-config//*:recaptcha-secret-key-variable/text())
else $config:get-access-config//*:recaptcha-secret-key/text();
(: Validate reCaptcha :)
declare function local:recaptcha() {
let $client-ip := request:get-header("X-Real-IP")
let $response := http:send-request(<http:request http-version="1.1" href="{xs:anyURI(concat($VALIDATE_URI,
'?secret=',string($secret-key),
'&response=',request:get-parameter("g-recaptcha-response",()),
'&remoteip=',$client-ip))}" method="post">
</http:request>)[2]
let $payload := util:base64-decode($response)
let $json-data := parse-json($payload)
return
if($json-data?success = true()) then true()
else false()
};
declare function local:email-list(){
let $list :=
if(request:get-parameter('formID','') != '') then
request:get-parameter('formID','')
else if(request:get-parameter('collection','') != '') then
request:get-parameter('collection','')
else ()
return
if($list != '') then
if($config:get-access-config/descendant::contact[@listID = $list]) then
for $contact in $config:get-access-config/descendant::contact[@listID = $list]/child::*[not(local-name(.) = 'from')]
return element { fn:local-name($contact) } {$contact/text()}
else
for $contact in $config:get-access-config/descendant::contact[1]/child::*[not(local-name(.) = 'from')]
return element { fn:local-name($contact) } {$contact/text()}
else
for $contact in $config:get-access-config/descendant::contact[1]/child::*[not(local-name(.) = 'from')]
return
element { fn:local-name($contact) } {$contact/text()}
};
declare function local:build-message(){
let $rec-uri := if(request:get-parameter('id','')) then concat('for ',request:get-parameter('id','')) else ()
let $from := if($config:get-access-config//*:contact[not(@listID)]//*:from) then
concat('<',$config:get-access-config//*:contact[not(@listID)]//*:from[1]//text(),'>')
else concat('<',$config:get-access-config//*:contact[not(@listID)]/child::*[1]//text(),'>')
return
<mail>
<from>{$config:app-title}  {$from}</from>
{local:email-list()}
<subject>{request:get-parameter('subject','')}</subject>
<message>
<xhtml>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{request:get-parameter('subject','')}</title>
</head>
<body>
<p>Name: {request:get-parameter('name','')}</p>
<p>e-mail: {request:get-parameter('email','')}</p>
<p>Subject: {request:get-parameter('subject','')}  {$rec-uri}</p>
<p>{$rec-uri}</p>
<p>{request:get-parameter('comments','')}</p>
</body>
</html>
</xhtml>
</message>
</mail>
};
declare function local:spam(){
<mail>
<from>{$config:app-title}  [email protected]</from>
<to>[email protected]</to>
<subject>{request:get-parameter('subject','')}</subject>
<message>
<xhtml>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Possible spam event from Syriaca.org, production site.</title>
</head>
<body>
<p>This was triggered by a possible spam event. from: {request:get-parameter('email','')}</p>
<p>Name: {request:get-parameter('name','')}</p>
<p>e-mail: {request:get-parameter('email','')}</p>
<p>{request:get-parameter('comments','')}</p>
</body>
</html>
</xhtml>
</message>
</mail>
};
let $cache := current-dateTime()
let $smtp := if($config:get-access-config//*:smtp/text() != '') then $config:get-access-config//*:smtp/text() else ()
let $timediff := (util:system-time() - xs:time(request:get-parameter('formLoaded',''))) div xs:dayTimeDuration("PT1S")
return
if(exists(request:get-parameter('url','')) and request:get-parameter('url','') != '') then
if(mail:send-email(local:spam(),$smtp, ()) ) then
<h4>Thank you. Your message has been sent. t1</h4>
else
<h4>Could not send message.</h4>
else if($timediff lt 10) then
if(mail:send-email(local:spam(),$smtp, ()) ) then
<h4>Thank you. Your message has been sent. t1</h4>
else
<h4>Could not send message.</h4>
else if(exists(request:get-parameter('email','')) and request:get-parameter('email','') != '') then
if(exists(request:get-parameter('comments','')) and request:get-parameter('comments','') != '') then
if($secret-key != '') then
if(local:recaptcha() = true()) then
let $mail := local:build-message()
return
if(mail:send-email($mail,$smtp, ()) ) then
<h4>Thank you. Your message has been sent.</h4>
else
<h4>Could not send message.</h4>
else 'Recaptcha fail'
else
let $mail := local:build-message()
return
if(mail:send-email($mail,$smtp, ()) ) then
<h4>Thank you. Your message has been sent.</h4>
else
<h4>Could not send message.</h4>
else <h4>Incomplete form.</h4>
else <h4>Incomplete form.</h4>