· 7 years ago · Jun 28, 2018, 07:48 PM
1<%@include file="/apps/codebase/global.jsp" %>
2
3<div class="captcha-wrap">
4 <div id="recaptcha${properties.reCaptchaId}" class="g-recaptcha"></div>
5<script>
6
7 /* Renders captchas. Runs @ async defer (callback on recaptcha src script in 'basepage/footer.jsp') */
8 function recaptchaCallback() {
9 var cC = $('.g-recaptcha').length; // captcha count
10 if ( cC >= 1 && cC <= 6) { // recaptcha1 loads when cC is 1-6
11 grecaptcha.render('recaptcha1', {
12 'sitekey' : '<PUBLIC_KEY>'
13 });
14 }
15 if ( cC >= 2 && cC <= 6) {
16 grecaptcha.render('recaptcha2', {
17 'sitekey' : '<PUBLIC_KEY>'
18 });
19 }
20 /* Additional if statements for rendering 1 to 6 captchas on the same page */
21
22</script>
23
24<script src="https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit" defer></script>
25
26<?xml version="1.0" encoding="UTF-8"?>
27<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
28 jcr:primaryType="cq:Dialog"
29 title="Captcha Configuration"
30 xtype="dialog">
31 <items
32 jcr:primaryType="cq:Widget"
33 xtype="tabpanel">
34 <items jcr:primaryType="cq:WidgetCollection">
35 <tab0
36 jcr:primaryType="cq:Panel"
37 title="Captcha">
38 <items jcr:primaryType="cq:WidgetCollection">
39 <reCaptchaId
40 jcr:primaryType="cq:Widget"
41 fieldDescription="Must be 1, 2, 3, 4, 5 or 6. Must go in order, i.e. don't only have a 1, 2, and 4 on the page or it won't work. Will not work with 7+ either."
42 fieldLabel="ReCaptcha ID"
43 name="./reCaptchaId"
44 xtype="textfield" />
45 </items>
46 </tab0>
47 <tab1
48 jcr:primaryType="nt:unstructured"
49 title="Constraints"
50 xtype="panel">
51 <items jcr:primaryType="cq:WidgetCollection">
52 <required
53 jcr:primaryType="cq:Widget"
54 fieldLabel="Required"
55 inputValue="true"
56 name="./required"
57 type="checkbox"
58 xtype="selection"/>
59 <requiredMessage
60 jcr:primaryType="cq:Widget"
61 fieldLabel="Required Message"
62 name="./requiredMessage"
63 xtype="textarea"/>
64 <constraintType
65 jcr:primaryType="cq:Widget"
66 fieldLabel="Constraint"
67 name="./constraintType"
68 options="/etc/designs/codebase/options/constraints.json"
69 type="select"
70 xtype="selection"/>
71 <constraintMessage
72 jcr:primaryType="cq:Widget"
73 fieldLabel="Constraint Message"
74 name="./constraintMessage"
75 xtype="textarea"/>
76 </items>
77 </tab1>
78 </items>
79 </items>
80</jcr:root>
81
82[
83 {"value":"","text":"None"},
84 {"value":"codebase/components/content/formelements/constraints/captcha","text":"Captcha"}
85]
86
87<%@page session="false" %><%
88%><%@page import="java.io.BufferedReader,
89 java.io.InputStream;
90 java.text.ParsePosition,
91 java.io.InputStreamReader,
92 java.net.URL,
93 java.nio.charset.Charset,
94 org.json.JSONObject,
95 com.day.cq.wcm.foundation.forms.FieldHelper,
96 com.day.cq.wcm.foundation.forms.FieldDescription,
97 com.day.cq.wcm.foundation.forms.FormsHelper,
98 com.day.cq.wcm.foundation.forms.ValidationInfo"%><%
99%><%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %>
100
101<sling:defineObjects/><%
102
103 // Get field description and force its name
104 FieldDescription desc = FieldHelper.getConstraintFieldDescription(slingRequest);
105 desc.setName(":cq:recaptcha");
106
107 // Check if a value has been provided
108 if (FieldHelper.checkRequired(slingRequest, desc)) {
109 final String response = request.getParameter("g-recaptcha-response");
110 final String secretKey = "<PRIVATE_KEY>";
111
112 private static boolean isCaptchaValid(String secretKey, String response) {
113 try {
114 String url = "https://www.google.com/recaptcha/api/siteverify?"
115 + "secret=" + secretKey
116 + "&response=" + response;
117 InputStream res = new URL(url).openStream();
118 BufferedReader rd = new BufferedReader(new InputStreamReader(res, Charset.forName("UTF-8")));
119
120 StringBuilder sb = new StringBuilder();
121 int cp;
122 while ((cp = rd.read()) != -1) {
123 sb.append((char) cp);
124 }
125 String jsonText = sb.toString();
126 res.close();
127
128 JSONObject json = new JSONObject(jsonText);
129 return json.getBoolean("success");
130 } catch (Exception e) {
131 return false;
132 }
133 }
134 if (!isCaptchaValid("<PRIVATE_KEY>", request.getParameter("g-recaptcha-response"))) {
135 ValidationInfo.addConstraintError(slingRequest, desc);
136 }
137 }
138
139%>