]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Recaptcha/RecaptchaPlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[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     {
46         if(!isset($this->private_key)) {
47             common_log(LOG_ERR, 'Recaptcha: Must specify private_key in config.php');
48         }
49         if(!isset($this->public_key)) {
50             common_log(LOG_ERR, 'Recaptcha: Must specify public_key in config.php');
51         }
52     }
53
54     function onEndRegistrationFormData($action)
55     {
56         $action->elementStart('li');
57         // TRANS: Field label.
58         $action->raw('<label for="recaptcha">'._m('Captcha').'</label>');
59
60         // AJAX API will fill this div out.
61         // We're calling that instead of the regular one so we stay compatible
62         // with application/xml+xhtml output as for mobile.
63         $action->element('div', array('id' => 'recaptcha'));
64         $action->elementEnd('li');
65
66         $action->recaptchaPluginNeedsOutput = true;
67         return true;
68     }
69
70     function onEndShowScripts(Action $action)
71     {
72         if (isset($action->recaptchaPluginNeedsOutput) && $action->recaptchaPluginNeedsOutput) {
73             // Load the AJAX API
74             if (GNUsocial::isHTTPS()) {
75                 $url = "https://www.google.com/recaptcha/api/js/recaptcha_ajax.js";
76             } else {
77                 $url = "http://www.google.com/recaptcha/api/js/recaptcha_ajax.js";
78             }
79             $action->script($url);
80
81             // And when we're ready, fill out the captcha!
82             $key = json_encode($this->public_key);
83             $action->inlinescript("\$(function(){Recaptcha.create($key, 'recaptcha');});");
84         }
85         return true;
86     }
87
88     /**
89      * Called when someone tries to register.
90      *
91      * We check the IP here to determine if it goes over any of our
92      * configured limits.
93      *
94      * @param Action $action Action that is being executed
95      *
96      * @return boolean hook value
97      */
98     function onStartRegistrationTry(Action $action)
99     {
100         $resp = recaptcha_check_answer ($this->private_key,
101                                         $_SERVER["REMOTE_ADDR"],
102                                         $action->trimmed('recaptcha_challenge_field'),
103                                         $action->trimmed('recaptcha_response_field'));
104
105         if (!$resp->is_valid) {
106             if($this->display_errors) {
107                 // TRANS: Error message displayed if there is in error communicating with the
108                 // TRANS: reCAPTCHA server. %s is the error.
109                 $action->showForm(sprintf(_m('(reCAPTCHA error: %s)', $resp->error)));
110             }
111             // TRANS: Error message displayed if a provided captcha response does not match.
112             $action->showForm(_m('Captcha does not match!'));
113             return false;
114         }
115     }
116
117     function onPluginVersion(array &$versions)
118     {
119         $versions[] = array('name' => 'Recaptcha',
120                             'version' => GNUSOCIAL_VERSION,
121                             'author' => 'Eric Helgeson',
122                             'homepage' => 'http://status.net/wiki/Plugin:Recaptcha',
123                             'rawdescription' =>
124                             // TRANS: Plugin description.
125                             _m('Uses <a href="http://recaptcha.org/">Recaptcha</a> service to add a  '.
126                                'captcha to the registration page.'));
127         return true;
128     }
129 }