/
confidentcaptcha.php
166 lines (139 loc) · 5.23 KB
/
confidentcaptcha.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* @version 1.0.7
* @package Confident CAPTCHA
* @author Confident Technologies
* @author mail info@confidenttechnologies.com
* @link http://www.confidenttechnologies.com
* @copyright Copyright (C) 2010 Confident Technologies - All rights reserved.
* @license GNU/GPL
*/
// no direct access
defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.plugin.plugin' );
/**
* Example system plugin
*/
class plgSystemConfidentCAPTCHA extends JPlugin
{
/**
* Constructor
*
* For php4 compatability we must not use the __constructor as a constructor for plugins
* because func_get_args ( void ) returns a copy of all passed arguments NOT references.
* This causes problems with cross-referencing necessary for the observer design pattern.
*
* @access protected
* @paramobject $subject The object to observe
* @paramarray $config An array that holds the plugin configuration
* @since1.0
*/
function plgSystemConfidentCAPTCHA( &$subject, $config )
{
parent::__construct( $subject, $config );
require_once(dirname(__FILE__).'/confidentcaptcha/config.php');
require_once(dirname(__FILE__).'/confidentcaptcha/confidentcaptcha/ccap_api.php');
require_once(dirname(__FILE__).'/confidentcaptcha/confidentcaptcha/ccap_persist.php');
require_once(dirname(__FILE__).'/confidentcaptcha/confidentcaptcha/ccap_prod_open_policy.php');
$this->ccap_callback_url = null;
$this->ccap_options = array(
'display_style' => 'modal',
);
$ccap_api = new CCAP_API(
$this->params->get('customer_id')
,$this->params->get('site_id')
,$this->params->get('api_username')
,$this->params->get('api_password')
,$this->params->get('captcha_server_url','http://captcha.confidenttechnologies.com/')
,'20100910_JOOMLA_1.0.7'
);
$this->ccap_persist = new CCAP_PersistSession();
$this->ccap_policy = new CCAP_ProductionFailOpen($ccap_api, $this->ccap_persist);
}
// the captch check logic
function onAfterRoute()
{
// init vars
$app =& JFactory::getApplication();
$uri =& JFactory::getUri();
$option = JRequest::getCmd('option');
$task = JRequest::getCmd('task');
$check = false;
$redirect = $uri->toString();
// assign check to true for matching conditions
if ($this->params->get('enabled_site_contact',1) && $app->isSite() && $option == 'com_contact' && $task == 'submit') {
$check = true;
}
if ($this->params->get('enabled_site_registration',1) && $app->isSite() && $option == 'com_user' && $task == 'register_save') {
$redirect = JRoute::_('index.php?option=com_user&view=register');
$check = true;
}
if ($this->params->get('enabled_administrator_login',0) && $app->isAdmin() && $option == 'com_login' && $task == 'login') {
$check = true;
}
if ($check) {
if (!$this->onCaptchaFormSubmit()) {
$app->redirect($redirect);
die();
}
}
}
// the captch display logic
function onAfterDispatch()
{
// init vars
$app =& JFactory::getApplication();
$document =& JFactory::getDocument();
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
// defaults
$display = false;
$pattern = '<button class="button validate" type="submit">';
$renderer = 'component';
// assign display to true for matching conditions (override defaults)
if ($this->params->get('enabled_site_contact',1) && $app->isSite() && $option == 'com_contact' && $view == 'contact') {
$display = true;
}
if ($this->params->get('enabled_site_registration',1) && $app->isSite() && $option == 'com_user' && $view == 'register') {
$display = true;
}
if ($this->params->get('enabled_administrator_login',1) && $app->isAdmin() && $option == 'com_login') {
$display = true;
$pattern = '<div class="button_holder">';
}
if ($display) {
if ($captcha = $this->onCaptchaFormDisplay()) {
// could use DOM parser, regex, or teplate overrides as alternative methods to inject captcha
$buffer = $document->getBuffer($renderer);
$output = str_replace($pattern, $captcha.$pattern, $buffer);
$document->setBuffer($output, $renderer);
}
}
}
// wrapper for CC check captcha core ( onAfterRoute() )
function onCaptchaFormSubmit()
{
$app =& JFactory::getApplication();
$this->ccap_policy->start_captcha_page();
$check = $this->ccap_policy->check_form($_REQUEST);
if (!$check) {
$app->enqueueMessage('CAPTCHA failed - please try again.', 'error');
return false; // block form submit
}
return true; // allow form submit
}
// wrapper for CC create_captcha() method ( onAfterDispatch() )
function onCaptchaFormDisplay()
{
if ($this->params->get('load_jquery',1)) {
$document =& JFactory::getDocument();
$headData = $document->getHeadData();
$headData['scripts']['http://code.jquery.com/jquery-1.4.2.min.js'] = 'text/javascript';
$headData['script']['text/javascript'] = "jQuery.noConflict();\n" . $headData['script']['text/javascript'];
$document->setHeadData($headData);
}
$this->ccap_policy->reset();
$ccap_captcha = $this->ccap_policy->create_visual($this->ccap_callback_url, $this->ccap_options);
return $ccap_captcha;
}
}