Commit a6e95021 authored by Aurel's avatar Aurel

add Captcha field, work done by Pierre


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@32007 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 7c2a23dd
This diff is collapsed.
# -*- coding: utf-8 -*-
#---------------------------------------------------------------------
# Python module for easy utilization of http://captchas.net
#
# For documentation look at http://captchas.net/sample/python/
#
# Written by Sebastian Wilhelmi <seppi@seppi.de> and
# Felix Holderied <felix@holderied.de>
# This file is in the public domain.
#
# ChangeLog:
#
# 2010-01-15: Adapt to ERP5 : a lot of code had to be removed or changed.
# Most of the work must be done in another class.
#
# 2006-09-08: Add new optional parameters alphabet, letters
# height an width. Add audio_url.
#
# 2006-03-01: Only delete the random string from the repository in
# case of a successful verification.
#
# 2006-02-14: Add new image() method returning an HTML/JavaScript
# snippet providing a fault tolerant service.
#
# 2005-06-02: Initial version.
#
#---------------------------------------------------------------------
import md5
import random
class CaptchasDotNet:
def __init__ (self, client, secret,
alphabet = 'abcdefghkmnopqrstuvwxyz',
letters = 6,
width = 240,
height = 80
):
self.__client = client
self.__secret = secret
self.__alphabet = alphabet
self.__letters = letters
self.__width = width
self.__height = height
# Return a random string
def random_string (self):
# The random string shall consist of small letters, big letters
# and digits.
letters = "abcdefghijklmnopqrstuvwxyz"
letters += letters.upper () + "0123456789"
# The random starts out empty, then 40 random possible characters
# are appended.
random_string = ''
for i in range (40):
random_string += random.choice (letters)
# Return the random string.
return random_string
def image_url (self, random, base = 'http://image.captchas.net/'):
url = base
url += '?client=%s&amp;random=%s' % (self.__client, random)
if self.__alphabet != "abcdefghijklmnopqrstuvwxyz":
url += '&amp;alphabet=%s' % self.__alphabet
if self.__letters != 6:
url += '&amp;letters=%s' % self.__letters
if self.__width != 240:
url += '&amp;width=%s' % self.__width
if self.__height != 80:
url += '&amp;height=%s' % self.__height
return url
def audio_url (self, random, base = 'http://audio.captchas.net/'):
url = base
url += '?client=%s&amp;random=%s' % (self.__client, random)
if self.__alphabet != "abcdefghijklmnopqrstuvwxyz":
url += '&amp;alphabet=%s' % self.__alphabet
if self.__letters != 6:
url += '&amp;letters=%s' % self.__letters
return url
def image (self, random, id = 'captchas.net'):
return '''
<a href="http://captchas.net"><img
style="border: none; vertical-align: bottom"
id="%s" src="%s" width="%d" height="%d"
alt="The CAPTCHA image" /></a>
<script type="text/javascript">
<!--
function captchas_image_error (image)
{
if (!image.timeout) return true;
image.src = image.src.replace (/^http:\/\/image\.captchas\.net/,
'http://image.backup.captchas.net');
return captchas_image_loaded (image);
}
function captchas_image_loaded (image)
{
if (!image.timeout) return true;
window.clearTimeout (image.timeout);
image.timeout = false;
return true;
}
var image = document.getElementById ('%s');
image.onerror = function() {return captchas_image_error (image);};
image.onload = function() {return captchas_image_loaded (image);};
image.timeout
= window.setTimeout(
"captchas_image_error (document.getElementById ('%s'))",
10000);
image.src = image.src;
//-->
</script>''' % (id, self.image_url (random), self.__width, self.__height, id, id)
def get_answer (self, random ):
# The format of the password.
password_alphabet = self.__alphabet
password_length = self.__letters
# Calculate the MD5 digest of the concatenation of secret key and
# random string.
encryption_base = self.__secret + random
if (password_alphabet != "abcdefghijklmnopqrstuvwxyz") or (password_length != 6):
encryption_base += ":" + password_alphabet + ":" + str(password_length)
digest = md5.new (encryption_base).digest ()
# Compute password
correct_password = ''
for pos in range (password_length):
letter_num = ord (digest[pos]) % len (password_alphabet)
correct_password += password_alphabet[letter_num]
return correct_password
\ No newline at end of file
......@@ -45,6 +45,7 @@ from Tool import SelectionTool
import OOoChart, PDFTemplate, Report, PDFForm, ParallelListField
import PlanningBox, POSBox, FormBox, EditorField, ProxyField, DurationField
import RelationField, ImageField, MultiRelationField, MultiLinkField, InputButtonField
import CaptchaField
import PreferenceTool
from Products.Formulator.FieldRegistry import FieldRegistry
......@@ -143,6 +144,8 @@ def initialize( context ):
'www/StringField.gif')
FieldRegistry.registerField(OOoChart.OOoChart,
'www/StringField.gif')
FieldRegistry.registerField(CaptchaField.CaptchaField,
'www/StringField.gif')
# some helper fields
FieldRegistry.registerField(HelperFields.ListTextAreaField)
......
<dtml-var manage_page_header>
<dtml-let help_product="'Formulator'" help_topic=meta_type>
<dtml-var manage_tabs>
</dtml-let>
<p class="form-help">
Surcharge <dtml-var meta_type> properties here.
</p>
<form action="manage_edit" method="POST">
<table cellspacing="0" cellpadding="2" border="0">
<!-- First, display normal properties -->
<!-- see: Formulator/dtml/fieldEdit.dtml -->
<dtml-in "form.get_groups()">
<dtml-let group=sequence-item fields="form.get_fields_in_group(group)">
<dtml-if fields>
<tr>
<td colspan="3" class="form-title">
Captcha Widget properties
</td>
</tr>
<dtml-var fieldListHeader>
<dtml-let current_field="this()">
<dtml-in fields>
<dtml-let field=sequence-item field_id="field.id"
value="current_field.get_orig_value(field_id)"
override="current_field.get_override(field_id)"
tales="current_field.get_tales(field_id)">
<tr>
<td align="left" valign="top">
<div class="form-label">
<dtml-if "tales or override">[</dtml-if><dtml-var "field.title()"><dtml-if "field.has_value('required') and field.get_value('required')">*</dtml-if><dtml-if "tales or override">]</dtml-if>
</div>
</td>
<td align="left" valign="top">
<dtml-var "field.render(value)">
</td>
<td><div class="form-element">
<dtml-var "field.meta_type">
</div></td>
</tr>
</dtml-let>
</dtml-in>
</dtml-let>
</dtml-if>
</dtml-let>
</dtml-in>
<!-- Then, display captcha-specific properties -->
<dtml-let current_field="this()">
<dtml-in "this().getCaptchaCustomPropertyList()" prefix="captcha">
<dtml-var expr="captcha_item">
<dtml-let field="captcha_item.get_real_field()" field_id="field.id"
value="current_field.get_orig_value(field_id)"
override="current_field.get_override(field_id)"
tales="current_field.get_tales(field_id)">
<tr>
<td align="left" valign="top">
<div class="form-label">
<dtml-if "tales or override">[</dtml-if><dtml-var "field.title()"><dtml-if "field.has_value('required') and field.get_value('required')">*</dtml-if><dtml-if "tales or override">]</dtml-if>
</div>
</td>
<td align="left" valign="top">
<dtml-var "field.render(value)">
</td>
<td><div class="form-element">
<dtml-var "field.meta_type">
</div></td>
</tr>
</dtml-let>
</dtml-in>
</dtml-let>
<tr>
<td align="left" valign="top">
<div class="form-element">
<input class="form-element" type="submit" name="submit"
value="Save Changes" />
</div>
</td>
</tr>
</table>
</form>
<dtml-var manage_page_footer>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment