Skip to content

Commit

Permalink
Add Captcha support. Add reCaptcha Plug-in.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnsl authored and dextercowley committed Dec 21, 2011
1 parent 0ecd79b commit e9a20b2
Show file tree
Hide file tree
Showing 16 changed files with 725 additions and 0 deletions.
13 changes: 13 additions & 0 deletions administrator/components/com_config/models/forms/application.xml
Expand Up @@ -610,6 +610,19 @@
required="true"
filter="cmd" />

<field
name="captcha"
type="plugins"
folder="captcha"
default="securimage"
label="COM_CONFIG_FIELD_DEFAULT_CAPTCHA_LABEL"
description="COM_CONFIG_FIELD_DEFAULT_CAPTCHA_DESC"
required="true"
filter="cmd" >
<option
value="0">JOPTION_DO_NOT_USE</option>
</field>

<field
name="access"
type="accesslevel"
Expand Down
14 changes: 14 additions & 0 deletions administrator/components/com_users/config.xml
Expand Up @@ -55,6 +55,20 @@
value="1">JYES</option>
</field>

<field
name="captcha"
type="plugins"
folder="captcha"
default=""
label="COM_USERS_CONFIG_FIELD_CAPTCHA_LABEL"
description="COM_USERS_CONFIG_FIELD_CAPTCHA_DESC"
filter="cmd" >
<option
value="0">JOPTION_DO_NOT_USE</option>
<option
value="">JOPTION_USE_DEFAULT</option>
</field>

<field
name="frontend_userparams"
type="radio"
Expand Down
2 changes: 2 additions & 0 deletions administrator/language/en-GB/en-GB.com_config.ini
Expand Up @@ -57,6 +57,8 @@ COM_CONFIG_FIELD_DEFAULT_ACCESS_LEVEL_DESC="Select the default access level for
COM_CONFIG_FIELD_DEFAULT_ACCESS_LEVEL_LABEL="Default Access Level"
COM_CONFIG_FIELD_DEFAULT_EDITOR_DESC="Select the default text editor for your site. Registered Users will be able to change their preference in their personal details if you allow that option."
COM_CONFIG_FIELD_DEFAULT_EDITOR_LABEL="Default Editor"
COM_CONFIG_FIELD_DEFAULT_CAPTCHA_DESC="Select the default captcha for your site."
COM_CONFIG_FIELD_DEFAULT_CAPTCHA_LABEL="Default Captcha"
COM_CONFIG_FIELD_DEFAULT_FEED_LIMIT_DESC="Select the number of content items to show in the feed(s)."
COM_CONFIG_FIELD_DEFAULT_FEED_LIMIT_LABEL="Default Feed Limit"
COM_CONFIG_FIELD_DEFAULT_LIST_LIMIT_DESC="Sets the default length of lists in the Control Panel for all users"
Expand Down
2 changes: 2 additions & 0 deletions administrator/language/en-GB/en-GB.com_users.ini
Expand Up @@ -20,6 +20,8 @@ COM_USERS_ASSIGNED_GROUPS="Assigned User Groups"
COM_USERS_CATEGORY_HEADING="Category"
COM_USERS_CONFIG_FIELD_ALLOWREGISTRATION_DESC="If set to Yes, new Users allowed to self-register."
COM_USERS_CONFIG_FIELD_ALLOWREGISTRATION_LABEL="Allow User Registration"
COM_USERS_CONFIG_FIELD_CAPTCHA_DESC="Select the captcha plugin that will be used in the registration form."
COM_USERS_CONFIG_FIELD_CAPTCHA_LABEL="Captcha"
COM_USERS_CONFIG_FIELD_FRONTEND_LANG_DESC="If 'Frontend User Parameters' is set to 'Show', users will be able to select their frontend language preference when registering. <br />This is specially handy in a multilanguage setting."
COM_USERS_CONFIG_FIELD_FRONTEND_LANG_LABEL="Frontend Language"
COM_USERS_CONFIG_FIELD_FRONTEND_USERPARAMS_DESC="If set to Show, Users will be able to select their language, editor, and Help Site preferences on their details screen when logged-in to the frontend."
Expand Down
48 changes: 48 additions & 0 deletions administrator/language/en-GB/en-GB.plg_captcha_recaptcha.ini
@@ -0,0 +1,48 @@
; $id:
; Joomla! Project
; Copyright (C) 2005 - 2009 Open Source Matters. All rights reserved.
; License http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
; Note : All ini files need to be saved as UTF-8

PLG_CAPTCHA_RECAPTCHA_XML_DESCRIPTION=""
PLG_CAPTCHA_RECAPTCHA="Captcha - ReCaptcha"

; Params
PLG_RECAPTCHA_PUBLIC_KEY_LABEL="Public Key"
PLG_RECAPTCHA_PUBLIC_KEY_DESC="Used in the JavaScript code that is served to your users."
PLG_RECAPTCHA_PRIVATE_KEY_LABEL="Private Key"
PLG_RECAPTCHA_PRIVATE_KEY_DESC="Used in the communication between your server and the ReCaptha server. Be sure to keep it a secret."
PLG_RECAPTCHA_THEME_LABEL="Theme"
PLG_RECAPTCHA_THEME_DESC="Defines which theme to use for reCAPTCHA."
PLG_RECAPTCHA_THEME_RED="Red"
PLG_RECAPTCHA_THEME_WHITE="White"
PLG_RECAPTCHA_THEME_BLACKGLASS="BlackGlass"
PLG_RECAPTCHA_THEME_CLEAN="Clean"
PLG_RECAPTCHA_LANG_LABEL="Language"
PLG_RECAPTCHA_LANG_DESC="Select the language for the reCAPTCHA. If default is set and the language file has custom translation, it will be used."

; Error messages
PLG_RECAPTCHA_ERROR_NO_PRIVATE_KEY="You must provide a Private Key. Insert one in the Plug-in Manager"
PLG_RECAPTCHA_ERROR_NO_PUBLIC_KEY="You must provide a Public Key. Insert one in the <a href="_QQ_"administrator/index.php?option=com_plugins&view=plugins&filter_folder=captcha"_QQ_" rel="_QQ_"nofollow"_QQ_">Plug-in Manager</a>"
PLG_RECAPTCHA_ERROR_EMPTY_SOLUTION="Empty solution not allowed."
PLG_RECAPTCHA_ERROR_NO_IP="For security reasons, you must pass the remote ip to reCAPTCHA"
PLG_RECAPTCHA_ERROR_UNKNOWN="Unknown error."
PLG_RECAPTCHA_ERROR_INVALID_SITE_PUBLIC_KEY="We weren't able to verify the public key."
PLG_RECAPTCHA_ERROR_INVALID_SITE_PRIVATE_KEY="We weren't able to verify the private key."
PLG_RECAPTCHA_ERROR_INVALID_REQUEST_COOKIE="The challenge parameter of the verify script was incorrect."
PLG_RECAPTCHA_ERROR_INCORRECT_CAPTCHA_SOL="The CAPTCHA solution was incorrect."
PLG_RECAPTCHA_ERROR_VERIFY_PARAMS_INCORRECT="The parameters to /verify were incorrect, make sure you are passing all the required parameters."
PLG_RECAPTCHA_ERROR_INVALID_REFERRER="reCAPTCHA API keys are tied to a specific domain name for security reasons."
PLG_RECAPTCHA_ERROR_RECAPTCHA_NOT_REACHABLE="Unable to contact the reCAPTCHA verify server."

; Uncomment(remove the ";" from the beginning of the line) the following lines if reCAPTCHA is not avaiable in your language
;PLG_RECAPTCHA_CUSTOM_LANG="true"
;PLG_RECAPTCHA_INSTRUCTIONS_VISUAL="Type the two words:"
;PLG_RECAPTCHA_INSTRUCTIONS_AUDIO="Type what you hear:"
;PLG_RECAPTCHA_PLAY_AGAIN="Play sound again"
;PLG_RECAPTCHA_CANT_HEAR_THIS="Download sound as MP3"
;PLG_RECAPTCHA_VISUAL_CHALLENGE="Get a visual challenge"
;PLG_RECAPTCHA_AUDIO_CHALLENGE="Get an audio challenge"
;PLG_RECAPTCHA_REFRESH_BTN="Get a new challenge"
;PLG_RECAPTCHA_HELP_BTN="Help"
;PLG_RECAPTCHA_INCORRECT_TRY_AGAIN="Incorrect. Try again."
@@ -0,0 +1,8 @@
; $id:
; Joomla! Project
; Copyright (C) 2005 - 2009 Open Source Matters. All rights reserved.
; License http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
; Note : All ini files need to be saved as UTF-8

PLG_CAPTCHA_RECAPTCHA_XML_DESCRIPTION=""
PLG_CAPTCHA_RECAPTCHA="Captcha - ReCaptcha"
8 changes: 8 additions & 0 deletions components/com_users/models/forms/registration.xml
Expand Up @@ -75,5 +75,13 @@
size="30"
validate="equals"
/>

<field
name="captcha"
type="captcha"
label="COM_USERS_REGISTER_CAPTCHA_LABEL"
description="COM_USERS_REGISTER_CAPTCHA_DESC"
validate="captcha"
/>
</fieldset>
</form>
14 changes: 14 additions & 0 deletions components/com_users/models/registration.php
Expand Up @@ -267,6 +267,20 @@ protected function preprocessForm(JForm $form, $data, $group = 'user')
{
$form->loadFile('sitelang',false);
}

// Deal with captcha
$params = $this->getState('params');
$captcha = $params->get('captcha');
if ($captcha === 0)
{
// FIXME: for some reason this don't remove the Label only the field
$form->removeField('captcha');
}
else
{
$form->setFieldAttribute('captcha', 'plugin', $captcha);
}

parent::preprocessForm($form, $data, $group);
}

Expand Down
1 change: 1 addition & 0 deletions installation/configuration.php-dist
Expand Up @@ -25,6 +25,7 @@ class JConfig {
public $offline_image = '';
public $sitename = 'Joomla!'; // Name of Joomla site
public $editor = 'tinymce';
public $captcha = 'securimage';
public $list_limit = '20';
public $root_user = '42';
public $access = '1';
Expand Down
1 change: 1 addition & 0 deletions installation/models/configuration.php
Expand Up @@ -53,6 +53,7 @@ function _createConfiguration($options)
$registry->set('offline_image', '');
$registry->set('sitename', $options->site_name);
$registry->set('editor', 'tinymce');
$registry->set('captcha', 'recaptcha');
$registry->set('list_limit', 20);
$registry->set('access', 1);

Expand Down
2 changes: 2 additions & 0 deletions language/en-GB/en-GB.com_users.ini
Expand Up @@ -82,6 +82,8 @@ COM_USERS_REGISTER_EMAIL1_MESSAGE="The email address you entered is already in u
COM_USERS_REGISTER_EMAIL2_DESC="Confirm your email address"
COM_USERS_REGISTER_EMAIL2_LABEL="Confirm email Address:"
COM_USERS_REGISTER_EMAIL2_MESSAGE="The email addresses you entered do not match. Please enter your email address in the email address field and confirm your entry by entering it in the confirm email field."
COM_USERS_REGISTER_CAPTCHA_LABEL="Captcha"
COM_USERS_REGISTER_CAPTCHA_DESC="Type in the textbox what you see in the image."
COM_USERS_REGISTER_NAME_DESC="Enter your full name"
COM_USERS_REGISTER_NAME_LABEL="Name:"
COM_USERS_REGISTER_NAME_MESSAGE="The name you entered is not valid."
Expand Down
167 changes: 167 additions & 0 deletions libraries/cms/captcha/captcha.php
@@ -0,0 +1,167 @@
<?php
/**
* @version $Id: captcha.php
* @package Joomla.Framework
* @subpackage Cache
* @copyright Copyright (C) 2005 - 2009 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

// No direct access
defined('JPATH_BASE') or die;

/**
* Joomla! Captcha base object
*
* @abstract
* @package Joomla.Framework
* @subpackage Captcha
* @since 1.6
*/
class JCaptcha extends JObservable
{
/**
* Captcha Plugin object
*
* @var object
*/
protected $_captcha;

/**
* Editor Plugin name
*
* @var string
*/
protected $_name;

/**
* Class constructor.
*
*/
public function __construct($captcha, $options)
{
$this->_name = $captcha;
$this->_load($options);
}

/**
* Returns the global Captcha object, only creating it
* if it doesn't already exist.
*
* @param string $editor The editor to use.
* @return object The JCaptcha object.
*/
public static function getInstance($captcha = '', $options = array())
{
static $instances;

if (is_null($instances)) $instances = array();

$signature = md5(serialize(array($captcha, $options)));

if (empty($instances[$signature])) {
$instances[$signature] = new JCaptcha($captcha, $options);
}

return $instances[$signature];
}

public function initialise($id)
{
$args['id'] = $id ;
$args['event'] = 'onInit';

try
{
$this->_captcha->update($args);
}
catch (Exception $e)
{
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
return false;
}

return true;
}

/**
* Get the HTML for the captcha.
*
* @return the return value of the function "onDisplay" of the selected Plugin.
* @since 1.6
*/
public function display($name, $id, $class = '')
{
// Check if captcha is already loaded.
if(is_null(($this->_captcha))) {
return;
}

// Initialise the Captcha.
if(!$this->initialise($id)) {
return;
}

$args['name'] = $name;
$args['id'] = $id ? $id : $name;
$args['class'] = $class ? 'class="'.$class.'"' : '';
$args['event'] = 'onDisplay';

return $this->_captcha->update($args);
}

/**
* Checks if the answer is correct.
*
* @return the return value of the function "onCheckAnswer" of the selected Plugin.
* @since 1.6
*/
public function checkAnswer($code)
{
//check if captcha is already loaded
if (is_null(($this->_captcha))) {
return;
}

$args['code'] = $code;
$args['event'] = 'onCheckAnswer';

return $this->_captcha->update($args);
}

/**
* Load the Captcha.
*
* @since 1.6
*/
protected function _load($options)
{
jimport('joomla.filesystem.file');

// Build the path to the needed captcha plugin
$name = JFilterInput::getInstance()->clean($this->_name, 'cmd');
$path = JPATH_SITE.'/plugins/captcha/'.$name.'/'.$name.'.php';

if (!JFile::exists($path))
{
$path = JPATH_SITE.'/plugins/captcha/'.$name.'.php';
if (!JFile::exists($path))
{
throw new Exception(JText::sprintf('JLIB_CAPTCHA_ERROR_PLUGIN_NOT_FOUND', $name));
}
}

// Require plugin file
require_once $path;

// Get the plugin
$plugin = JPluginHelper::getPlugin('captcha', $this->_name);
if (!$plugin) throw new Exception(JText::sprintf('JLIB_CAPTCHA_ERROR_LOADING', $name));
$params = new JRegistry($plugin->params);
$plugin->params = $params;

// Build captcha plugin classname
$name = 'plgCaptcha'.$this->_name;
$this->_captcha = new $name($this, (array)$plugin, $options);
}
}

0 comments on commit e9a20b2

Please sign in to comment.