source: extensions/captcha/contactform.inc.php @ 17020

Last change on this file since 17020 was 8888, checked in by patdenice, 14 years ago

Add captcha to contactform (if installed).

File size: 1.5 KB
RevLine 
[8888]1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5include(CAPTCHA_PATH.'recaptchalib.php');
6add_event_handler('display_contactform', 'add_captcha');
7add_event_handler('check_contactform_params', 'check_captcha');
8
9function add_captcha()
10{
11  global $template, $conf;
12
13  if (!is_a_guest()) return;
14
15  $template->set_prefilter('cf_form', 'captcha_prefilter');
16  $template->set_filename('captcha', realpath(CAPTCHA_PATH.'captcha.tpl'));
17  $template->assign(array(
18    'CAPTCHA_HTML'  => recaptcha_get_html($conf['captcha_publickey'], get_plugin_data('captcha')),
19    'CAPTCHA_THEME' => $conf['captcha_theme'],
20    )
21  );
22  $template->assign_var_from_handle('CAPTCHA', 'captcha');
23}
24
25function captcha_prefilter($content, $smarty)
26{
27  $search = '
28      <tr>
29        <td class="contact-form-left">&nbsp;</td>
30        <td class="contact-form-right"><input class="submit" type="submit" value="{\'cf_submit\'|@translate}"></td>
31      </tr>';
32  return str_replace($search, '<tr><td></td><td class="contact-form-right">{$CAPTCHA}</td></tr>'."\n".$search, $content);
33}
34
35function check_captcha($infos)
36{
37  global $conf;
38
39  if (!is_a_guest()) return $infos;
40
41  $resp = recaptcha_check_answer(
42    $conf['captcha_privatekey'],
43    $_SERVER["REMOTE_ADDR"],
44    $_POST["recaptcha_challenge_field"],
45    $_POST["recaptcha_response_field"]
46  );
47
48  if (!$resp->is_valid)
49  {
50    load_language('plugin.lang', CAPTCHA_PATH);
51    array_push($infos['errors'], l10n('Invalid Captcha'));
52    set_plugin_data('captcha', $resp->error);
53  }
54
55  return $infos;
56}
57
58?>
Note: See TracBrowser for help on using the repository browser.