chg: moved recaptcha from claustra to core

This commit is contained in:
Tobi Schäfer 2018-05-19 09:56:24 +02:00
parent 62bd495564
commit 3f9d03a6f6
5 changed files with 59 additions and 4 deletions

45
code/Global/Recaptcha.js Normal file
View file

@ -0,0 +1,45 @@
// The Antville Project
// http://code.google.com/p/antville
//
// Copyright 20012014 by the Workers of Antville.
//
// Licensed under the Apache License, Version 2.0 (the ``License'');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an ``AS IS'' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Defines the Antville reCAPTCHA prototype.
* @see http://www.google.com/recaptcha
*/
Recaptcha = {};
Recaptcha.verify = function (data) {
var secret = getProperty('recaptcha.secret');
if (session.user || !secret) return;
var response = req.postParams['g-recaptcha-response'];
var ip = req.data.remotehost;
var mime = getURL('https://www.google.com/recaptcha/api/siteverify?secret=' + secret + '&response=' + response + '&remoteip=' + ip);
var json = JSON.parse(new java.lang.String(mime.content));
if (!json.success) {
throw Error(gettext('Do Androids dream of electric sheep?'));
}
};
Recaptcha.render = function(context, skin) {
var secret = getProperty('recaptcha.secret');
if (session.user || !context || !secret) return;
if (!skin) skin = '$Members#recaptcha';
return context.renderSkinAsString(skin);
};

View file

@ -125,6 +125,7 @@
</div>
<% if <% root.termsStory %> is null then '' else <% members.skin $Members#terms %> %>
<% if <% root.privacyStory %> is null then '' then <% members.skin $Members#privacy %> %>
<% param.recaptcha prefix="<div class='uk-form-row'>" suffix="</div>" %>
<div class='uk-form-row uk-margin-top'>
<button class='uk-button uk-button-primary' type="submit" id="submit" name="register" value="1" tabindex=6>
<% gettext Register %>
@ -132,7 +133,7 @@
<a href='<% members.href login %>' class="uk-button uk-button-link" tabindex=7><% gettext Cancel %></a>
</div>
</form>
<script type="text/javascript">
<script>
$('form#register').submit(function() {
var token = '<% session.token %>';
var password = $('#password').val();
@ -165,7 +166,7 @@
<% if <% root.termsStory %> is null then '' else <% members.skin $Members#terms %> %>
<% if <% root.privacyStory %> is null then '' then <% members.skin $Members#privacy %> %>
<div class='uk-form-row uk-margin-top'>
<button class='uk-button uk-button-primary' type="submit"" name="accept" value="1" tabindex=6>
<button class='uk-button uk-button-primary' type="submit" name="accept" value="1" tabindex=6>
<% gettext Accept %>
</button>
<a href='<% site.href %>' class="uk-button uk-button-link" tabindex=7><% gettext Cancel %></a>
@ -257,3 +258,7 @@
<a href='?name=<% param.name encoding=form %>'><i class='uk-icon-plus'></i></a>
</td>
</tr>
<% #recaptcha %>
<div class='uk-margin-top g-recaptcha' data-sitekey='<% property recaptcha.sitekey %>'></div>
<script src='https://www.google.com/recaptcha/api.js?hl=<% site.locale %>'></script>

View file

@ -88,6 +88,7 @@ Members.prototype.main_action = function() {
Members.prototype.register_action = function() {
if (req.postParams.register) {
try {
Recaptcha.verify(req.postParams);
if (root.termsStory && !req.postParams.terms) throw Error('Please accept the terms and conditions.');
if (root.privacyStory && !req.postParams.privacy) throw Error('Please accept the data privacy statement.');
var title = res.handlers.site.title;
@ -107,7 +108,8 @@ Members.prototype.register_action = function() {
session.data.token = User.getSalt();
res.data.action = this.href(req.action);
res.data.title = gettext('Register');
res.data.body = this.renderSkinAsString('$Members#register');
var param = { recaptcha: Recaptcha.render(this) };
res.data.body = this.renderSkinAsString('$Members#register', param);
this._parent.renderSkin('Site#page');
return;
}

View file

@ -70,6 +70,7 @@
<textarea class='uk-width-1-1' rows="15" class="formText" wrap="virtual" name="text"><% request.text encoding="form" %></textarea>
</div>
</div>
<% param.recaptcha prefix="<div class='uk-form-row'>" suffix='</div>' %>
<div class='uk-form-row'>
<button class='uk-button uk-button-primary' type="submit" id="submit" name="send" value="1" tabindex=4>
<% gettext Send %>

View file

@ -192,6 +192,7 @@ Membership.prototype.contact_action = function() {
if (!req.postParams.text) {
throw Error(gettext('Please enter the message text.'));
}
Recaptcha.verify(req.postParams);
this.notify(req.action, this.creator.email, session.user ?
gettext('[{0}] Message from user {1}', root.title, session.user.name) :
gettext('[{0}] Message from anonymous user', root.title));
@ -206,7 +207,8 @@ Membership.prototype.contact_action = function() {
res.data.action = this.href(req.action);
res.data.title = gettext('Contact {0}', this.name);
res.data.body = this.renderSkinAsString('$Membership#contact');
var param = { recaptcha: Recaptcha.render(res.handlers.members) };
res.data.body = this.renderSkinAsString('$Membership#contact', param);
this.site.renderSkin('Site#page');
return;
}