3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to show reCaptcha when a user registers
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Eric Helgeson <erichelgeson@gmail.com>
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 require_once(INSTALLDIR.'/plugins/Recaptcha/recaptchalib.php');
36 class RecaptchaPlugin extends Plugin
44 function onInitializePlugin()
46 if(!isset($this->private_key)) {
47 common_log(LOG_ERR, 'Recaptcha: Must specify private_key in config.php');
49 if(!isset($this->public_key)) {
50 common_log(LOG_ERR, 'Recaptcha: Must specify public_key in config.php');
54 function onEndRegistrationFormData($action)
56 $action->elementStart('li');
57 $action->raw('<label for="recaptcha">'._m('Captcha').'</label>');
59 // AJAX API will fill this div out.
60 // We're calling that instead of the regular one so we stay compatible
61 // with application/xml+xhtml output as for mobile.
62 $action->element('div', array('id' => 'recaptcha'));
63 $action->elementEnd('li');
65 $action->recaptchaPluginNeedsOutput = true;
69 function onEndShowScripts($action)
71 if (isset($action->recaptchaPluginNeedsOutput) && $action->recaptchaPluginNeedsOutput) {
73 if (StatusNet::isHTTPS()) {
74 $url = "https://api-secure.recaptcha.net/js/recaptcha_ajax.js";
76 $url = "http://api.recaptcha.net/js/recaptcha_ajax.js";
78 $action->script($url);
80 // And when we're ready, fill out the captcha!
81 $key = json_encode($this->public_key);
82 $action->inlinescript("\$(function(){Recaptcha.create($key, 'recaptcha');});");
87 function onStartRegistrationTry($action)
89 $resp = recaptcha_check_answer ($this->private_key,
90 $_SERVER["REMOTE_ADDR"],
91 $action->trimmed('recaptcha_challenge_field'),
92 $action->trimmed('recaptcha_response_field'));
94 if (!$resp->is_valid) {
95 if($this->display_errors) {
96 $action->showForm(sprintf(_("(reCAPTCHA error: %s)", $resp->error)));
98 $action->showForm(_m("Captcha does not match!"));
103 function onPluginVersion(&$versions)
105 $versions[] = array('name' => 'Recaptcha',
106 'version' => STATUSNET_VERSION,
107 'author' => 'Eric Helgeson',
108 'homepage' => 'http://status.net/wiki/Plugin:Recaptcha',
110 _m('Uses <a href="http://recaptcha.org/">Recaptcha</a> service to add a '.
111 'captcha to the registration page.'));