]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/recaptcha/recaptcha.php
change LACONICA to STATUSNET
[quix0rs-gnu-social.git] / plugins / recaptcha / recaptcha.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')) {
31     exit(1);
32 }
33
34 define('RECAPTCHA', '0.2');
35
36 class recaptcha extends Plugin
37 {
38     var $private_key;
39     var $public_key;
40     var $display_errors;
41     var $failed;
42     var $ssl;
43
44     function __construct($public_key, $private_key, $display_errors=false)
45     {
46         parent::__construct();
47         require_once(INSTALLDIR.'/plugins/recaptcha/recaptchalib.php');
48         $this->public_key = $public_key;
49         $this->private_key = $private_key; 
50         $this->display_errors = $display_errors;
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     function onStartShowHTML($action)
61     {
62         //XXX: Horrible hack to make Safari, FF2, and Chrome work with
63         //reChapcha. reChapcha beaks xhtml strict
64         header('Content-Type: text/html');
65
66         $action->extraHeaders();
67
68         $action->startXML('html');
69
70         $action->raw('<style type="text/css">#recaptcha_area{float:left;}</style>');
71         return false;
72     }
73
74     function onEndRegistrationFormData($action)
75     {
76         $action->elementStart('li');
77         $action->raw('<label for="recaptcha_area">Captcha</label>');
78         if($this->checkssl() === true){
79             $action->raw(recaptcha_get_html($this->public_key), null, true);
80         } else { 
81             $action->raw(recaptcha_get_html($this->public_key));
82         }
83         $action->elementEnd('li');
84         return true;
85     }
86
87     function onStartRegistrationTry($action)
88     {
89         $resp = recaptcha_check_answer ($this->private_key,
90                                         $_SERVER["REMOTE_ADDR"],
91                                         $action->trimmed('recaptcha_challenge_field'),
92                                         $action->trimmed('recaptcha_response_field'));
93
94         if (!$resp->is_valid) 
95         {
96             if($this->display_errors)
97             { 
98                 $action->showForm ("(reCAPTCHA said: " . $resp->error . ")");
99             }
100             $action->showForm("Captcha does not match!");
101             return false;
102         }
103     }
104 }