]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Recaptcha/RecaptchaPlugin.php
some formatting changes to make inblobs work
[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 define('RECAPTCHA', '0.2');
35
36 require_once(INSTALLDIR.'/plugins/Recaptcha/recaptchalib.php');
37
38 class RecaptchaPlugin extends Plugin
39 {
40     var $private_key;
41     var $public_key;
42     var $display_errors;
43     var $failed;
44     var $ssl;
45
46     function onInitializePlugin(){
47         if(!isset($this->private_key)) {
48             common_log(LOG_ERR, 'Recaptcha: Must specify private_key in config.php');
49         }
50         if(!isset($this->public_key)) {
51             common_log(LOG_ERR, 'Recaptcha: Must specify public_key in config.php');
52         }
53     }
54
55     function checkssl(){
56         if(common_config('site', 'ssl') === 'sometimes' || common_config('site', 'ssl') === 'always') {
57             return true;
58         }
59         return false;
60     }
61
62
63     function onEndRegistrationFormData($action)
64     {
65         $action->style('#recaptcha_area{float:left;}');
66         $action->elementStart('li');
67         $action->raw('<label for="recaptcha_area">Captcha</label>');
68         if($this->checkssl() === true) {
69             $action->raw(recaptcha_get_html($this->public_key), null, true);
70         } else { 
71             $action->raw(recaptcha_get_html($this->public_key));
72         }
73         $action->elementEnd('li');
74         return true;
75     }
76
77     function onStartRegistrationTry($action)
78     {
79         $resp = recaptcha_check_answer ($this->private_key,
80                                         $_SERVER["REMOTE_ADDR"],
81                                         $action->trimmed('recaptcha_challenge_field'),
82                                         $action->trimmed('recaptcha_response_field'));
83
84         if (!$resp->is_valid) {
85             if($this->display_errors) {
86                 $action->showForm ("(reCAPTCHA error: " . $resp->error . ")");
87             }
88             $action->showForm("Captcha does not match!");
89             return false;
90         }
91     }
92 }