]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Recaptcha/RecaptchaPlugin.php
i18n/L10n review, extension credits added.
[quix0rs-gnu-social.git] / plugins / Recaptcha / RecaptchaPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to show reCaptcha when a user registers
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
24  * @author    Eric Helgeson <erichelgeson@gmail.com>
25  * @copyright 2009
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/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once(INSTALLDIR.'/plugins/Recaptcha/recaptchalib.php');
35
36 class RecaptchaPlugin extends Plugin
37 {
38     var $private_key;
39     var $public_key;
40     var $display_errors;
41     var $failed;
42     var $ssl;
43
44     function onInitializePlugin(){
45         if(!isset($this->private_key)) {
46             common_log(LOG_ERR, 'Recaptcha: Must specify private_key in config.php');
47         }
48         if(!isset($this->public_key)) {
49             common_log(LOG_ERR, 'Recaptcha: Must specify public_key in config.php');
50         }
51     }
52
53     function checkssl(){
54         if(common_config('site', 'ssl') === 'sometimes' || common_config('site', 'ssl') === 'always') {
55             return true;
56         }
57         return false;
58     }
59
60
61     function onEndRegistrationFormData($action)
62     {
63         $action->elementStart('li');
64         $action->raw('<label for="recaptcha">'._m('Captcha').'</label>');
65
66         // AJAX API will fill this div out.
67         // We're calling that instead of the regular one so we stay compatible
68         // with application/xml+xhtml output as for mobile.
69         $action->element('div', array('id' => 'recaptcha'));
70         $action->elementEnd('li');
71
72         $action->recaptchaPluginNeedsOutput = true;
73         return true;
74     }
75
76     function onEndShowScripts($action)
77     {
78         if (isset($action->recaptchaPluginNeedsOutput) && $action->recaptchaPluginNeedsOutput) {
79             // Load the AJAX API
80             if ($this->checkssl()) {
81                 $url = "https://api-secure.recaptcha.net/js/recaptcha_ajax.js";
82             } else {
83                 $url = "http://api.recaptcha.net/js/recaptcha_ajax.js";
84             }
85             $action->script($url);
86
87             // And when we're ready, fill out the captcha!
88             $key = json_encode($this->public_key);
89             $action->inlinescript("\$(function(){Recaptcha.create($key, 'recaptcha');});");
90         }
91         return true;
92     }
93
94     function onStartRegistrationTry($action)
95     {
96         $resp = recaptcha_check_answer ($this->private_key,
97                                         $_SERVER["REMOTE_ADDR"],
98                                         $action->trimmed('recaptcha_challenge_field'),
99                                         $action->trimmed('recaptcha_response_field'));
100
101         if (!$resp->is_valid) {
102             if($this->display_errors) {
103                 $action->showForm(sprintf(_("(reCAPTCHA error: %s)", $resp->error)));
104             }
105             $action->showForm(_m("Captcha does not match!"));
106             return false;
107         }
108     }
109
110     function onPluginVersion(&$versions)
111     {
112         $versions[] = array('name' => 'Recaptcha',
113                             'version' => STATUSNET_VERSION,
114                             'author' => 'Eric Helgeson',
115                             'homepage' => 'http://status.net/wiki/Plugin:Recaptcha',
116                             'rawdescription' =>
117                             _m('Uses <a href="http://recaptcha.org/">Recaptcha</a> service to add a  '.
118                                'captcha to the registration page.'));
119         return true;
120     }
121 }