source: extensions/captcha/main.inc.php @ 8030

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

Add captcha plugin.

File size: 2.3 KB
Line 
1<?php
2/*
3Plugin Name: Captcha
4Version: auto
5Description: Add captcha to registration form
6Plugin URI: auto
7Author: P@t
8Author URI: http://piwigo.org
9*/
10
11if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
12define('CAPTCHA_PATH' , PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/');
13
14global $conf;
15
16if (script_basename() == 'register'
17  and !empty($conf['captcha_publickey'])
18  and !empty($conf['captcha_privatekey']))
19{
20  include(CAPTCHA_PATH.'recaptchalib.php');
21  add_event_handler('loc_end_page_header', 'add_captcha');
22  add_event_handler('register_user_check', 'check_captcha');
23}
24
25if (script_basename() == 'admin')
26{
27  add_event_handler('get_admin_plugin_menu_links', 'captcha_plugin_admin_menu' );
28}
29
30function add_captcha()
31{
32  global $template, $conf;
33  $template->set_prefilter('register', 'captcha_prefilter');
34  $template->assign('CAPTCHA', recaptcha_get_html($conf['captcha_publickey'], get_plugin_data('captcha')));
35}
36
37function captcha_prefilter($content, $smarty)
38{
39  $search = '<p class="bottomButtons">';
40  $captcha = '<div>{$CAPTCHA}</div>';
41  $js = '<script type= "text/javascript">var RecaptchaOptions = {ldelim} theme: "white", lang : "{$lang_info.code}" };</script>';
42  $css = '{html_head}<style type="text/css">#recaptcha_area {ldelim} margin: 25px auto; }</style>{/html_head}';
43
44  return str_replace($search, $js."\n".$css."\n".$captcha."\n".$search, $content);
45}
46
47function check_captcha($errors)
48{
49  global $conf;
50
51  $resp = recaptcha_check_answer(
52    $conf['captcha_privatekey'],
53    $_SERVER["REMOTE_ADDR"],
54    $_POST["recaptcha_challenge_field"],
55    $_POST["recaptcha_response_field"]
56  );
57
58  if (!$resp->is_valid)
59  {
60    load_language('plugin.lang', CAPTCHA_PATH);
61    array_push($errors, l10n('Invalid Captcha'));
62    set_plugin_data('captcha', $resp->error);
63  }
64
65  return $errors;
66}
67
68function captcha_plugin_admin_menu($menu)
69{
70        global $page,$conf;
71
72        if ( (empty($conf['captcha_publickey']) or empty($conf['captcha_publickey']))
73    and in_array($page['page'], array('intro','plugins_list')) )
74        {
75                load_language('plugin.lang', CAPTCHA_PATH);
76                $page['errors'][] = l10n('You need to define Captcha keys');
77        }
78
79        array_push($menu,
80                        array(
81                                'NAME' => 'Captcha',
82                                'URL' => get_admin_plugin_menu_link(dirname(__FILE__).'/admin.php')
83                        )
84                );
85        return $menu;
86}
87
88?>
Note: See TracBrowser for help on using the repository browser.