Typo3 - Captcha

Aus Wikizone
Wechseln zu: Navigation, Suche

Captcha soll Formulare gegen automatisierte Spam-Angriffe schützen. Der Benutzer muß einen Zahlen oder Buchstabencode, der als verzerrtes oder verfremdetes Bild vorliegt eingeben. Bei falscher Eingabe wird das Formular nicht akzeptiert.

Es gibt im Moment (5.4.2006) zwei Extensions für Typo3. 1. Captcha Libary von Kasper 2. free captcha von Rolland Stanislas


Captcha Libary von Kasper

Die Captcha Libary wird als Extension geladen und steht dann als php-Erweiterung für die Frontendplugins zur Verfügung.

Es muß also Code in die Betroffenen Extensions eingebracht werden um diese aufzurüsten. Dabei wird der Captcha-Code in einer Session Variablen gespeichert und später mit der Eingabe des Users verglichen.

So geht's:

Captcha Bild erzeugen

Beispiel 1 allgemein

if (t3lib_extMgm::isLoaded('captcha')){
  $captchaHTMLoutput = '<img src="'.t3lib_extMgm::siteRelPath('captcha').'captcha/captcha.php" alt="" />';
} else {
  $captchaHTMLoutput = '';
}

Beispiel 2 ve_guestbook

in den beiden Arrays $substituteArray müssen neue Marker angelegt werden:

$substituteArray = array('firstname', 'surname', 'email', 'homepage', 'place', 'entry', 'entrycomment', 'submit','captcharesponse','captcha');

In der Funktion displayForm()


### Captcha Beginn ###
# In Funktion displayForm() Marker befüllen
  if (t3lib_extMgm::isLoaded('captcha'))  {
  #creating any random string to workarround browser caching of old captcha image
    $anything = date("d.m.Y-H:i:s",time());
    $captchaHTMLoutput = '<img src="'.t3lib_extMgm::siteRelPath('captcha').'captcha/captcha.php?'.$anything.'" alt="No Spam!" />';
  } else {
    $captchaHTMLoutput = '';
}
$markerArray['###CAPTCHA_INSERT###'] = $captchaHTMLoutput;
### Ende ###

2. Formularfeld einfügen

Beispiel 1

<input type="text" size=30 name="TIPFORM[captcharesponse]" value="">

Beispiel 2 - ve_guestbook Im Template werden die neuen Marker und das Input angelegt:

Keine Ahnung wofür der ###FORM_CAPTCHARESPONSE### Marker ist. Braucht man wahrscheinlich nur für Felder bei denen man die Werte bei Falscheingabe wieder reinschreibt (also hier wohl gar nicht).

<table>
<!--...-->
  <tr>
    <td valign="top" class="content" colspan="2">Bitte geben sie folgenden Code ein:</td>
  <tr>
  <td valign="top" class="content">###CAPTCHA_INSERT###
    <span class="content">###FORM_CAPTCHARESPONSE###</span> </td>
  <td valign="top" class="content"> 
    <input class="content" type="text" name="tx_veguestbook_pi1[captcharesponse]" value=""></td>
</table>

3. Abfrage realisieren

Beispiel 1

if (t3lib_extMgm::isLoaded('captcha')){
  session_start();
  $captchaStr = $_SESSION['tx_captcha_string'];//String auslesen
  $_SESSION['tx_captcha_string'] = '';
} else {
  $captchaStr = -1;//String zurücksetzen damit er nicht mehrmals benutzt werden kann
}

Bsp.2 (für Gästebuch Alt-Brettheim)

In Funktion checkForm() ganz an den Anfang

	# Getting the Captcha String
	if (t3lib_extMgm::isLoaded('captcha'))  {
    session_start();
    $captchaStr = $_SESSION['tx_captcha_string'];
    $_SESSION['tx_captcha_string'] = '';
	} else {
    $captchaStr = -1;
	}

4. Eingabe überprüfen

Beispiel 1

if (... ($captchaStr===-1 || ($captchaStr && $tipData['captchaResponse']===$captchaStr)) ...) {

...

}

Bsp2 (Alt-Brettheim)

	# Evaluating the captcha string
	#if ($this->postvars['captcharesponse'] != $captchaStr) {
	if (strcasecmp($this->postvars['captcharesponse'], $captchaStr) != 0) {
		$error .= '<li>Antispamcode war leider falsch, bitte nochmal probieren!</li>';
	}

Captcha Libary anpassen

Captcha Library (captcha) captcha/captcha.php

Captcha in Mailformplus

Ich verwende MailFormPlus und Captcha. Bei MailFormPlus musst du ja ein Template erstellen, das die Struktur des Formulars wiedergibt. An gewünschter Stelle fügst du innerhalb der FORM-Tags "###CAPTCHA###" ein (das erzeugt das Bild von Captcha). Dann noch ein input-Feld erzeugt mit name-Attribut. Dem name-Attribut weißt du einen eindeutigen Namen zu. Der Name muss jetzt noch mit dem Captcha-Bild verknüpft werden. Das machst du im Template der Seite im Setup. Du musst ins Setup nur noch folgendes eintragen:

plugin.tx_thmailformplus_pi1.captchaFieldname = name_des_inputfeldes

Fertig.

Beispiel für Template:

 ###CAPTCHA###<br />
 <input typ2="text" name="projektdatenbank_captcha_input" value="###value_captcha###" /><br />
 <span style="font-size:0.8em;">Spam-Sicherheitsabfrage</span>

(Quelle: typo3.net Verfasst am: 20.04.2006 [16:09] )

Captcha im Standard Formular

Zusammen mit der Extension nd_antispam läßt sich das Captcha auch im Standard Formular einbinden.


Man kann eine Fehlerseite anlegen, auf die bei einer falschen Eingabe verwiesen wird. Ein Inhaltselement in der Art.

<p>Sie haben einen Fehler bei der Eingabe des Spamcheck-Codes gemacht. <a href="http://www.grauff.de/?id=258"><br />Bitte versuchen Sie es hier noch einmal.</a></p>

oder auch Spamcheck Error Your spamcheck-code was not correct, please try again.

wirds tun.


Im Formular braucht man dann dazu folgenden Code:

 | errorpage=hidden | http://www.grauff.com/index.php?id=335
 | label | Spamschutz - geben Sie folgenden Code ein:
 | captcha_enabled=hidden | 1

Captcha Einstellungen

Ein ordentliches Set ist folgendes (Extension Manager Konfiguration, weiteres Typoscript ist nicht notwendig):

onfiguration:
(Notice: You may need to clear the cache after configuration of the extension. This is required if the extension adds TypoScript depending on these settings.)

Use TTF	[useTTF]
When set the captcha will get generated using TTF font rendering functions which will result in much more readable captchas
1
Default: 0

Image width	[imgWidth]
The width of the captcha image
100
Default: 95

Image height	[imgHeight]
The height of the captcha image
30
Default: 25

Captcha chars	[captchaChars]
The number of characters of which the captcha consists
5
Default: 5

No numbers	[noNumbers]
No digits (0-9) will get used for the capcha string
1
Default: 0

Bold text	[bold]
When set the text of the captcha will be somewhat bold
0
Default: 0

No lowercase characters	[noLower]
No lowercase characters (a-z) will get used for the capcha string
0
Default: 0

No uppercase characters	[noUpper]
No uppercase characters (a-z) will get used for the capcha string
1
Default: 0

Letter spacing	[letterSpacing]
The number of pixels between the start of each character (or their distance when in TTF mode, should be 4-6 in TTF mode)
4
Default: 16

Angle	[angle]
The angle in degree which each letter is rotate maximally clock-, or counterclock-wise
35
Default: 20

X Difference	[diffx]
The number of pixels which each letter is maximally shifted to the left or right

Default: 0

Y Difference	[diffy]
The number of pixels which each letter is maximally shifted up or down
3
Default: 2

X Offset	[xpos]
The numer of pixels which the first character is away from the top left corner in horizontal direction
4
Default: 3

Y Offset	[ypos]
The numer of pixels which the first character is away from the top left corner in vertical direction
5
Default: 4

Noises	[noises]
How many 'obfuscating' ellipses are drawn onto the background of the image

Default: 6

Background color	[backcolor]
The background color for the captcha

Default: #f4f4f4

Text color	[textcolor]
The text color for the captcha

Default: #000000

Obfuscation color	[obfusccolor]
The color of the obfuscating ellipses

Default: #c0c0c0
#b0b0b0
Font size	[fontSize]
The font size in pixels (Just for TTF mode)

Default: 16

Font file	[fontFile]
The font file to use in TTF mode (relative to the root of the TYPO3 installation)

Default:

Exclude chars	[excludeChars]
All characters listed here will not get used for the captcha

Default: gijloGIJLO0169