]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/recaptcha/recaptcha.php
Merge branch '0.8.x' of git@gitorious.org:laconica/mainline into 0.8.x
[quix0rs-gnu-social.git] / plugins / recaptcha / recaptcha.php
1 <?php
2 /**
3  * Laconica, 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   Laconica
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://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
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             '-//W3C//DTD XHTML 1.0 Strict//EN',
70             'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
71
72         $action->raw('<style type="text/css">#recaptcha_area{float:left;}</style>');
73         return false;
74     }
75
76     function onEndRegistrationFormData($action)
77     {
78         $action->elementStart('li');
79         $action->raw('<label for="recaptcha_area">Captcha</label>');
80         if($this->checkssl() === true){
81             $action->raw(recaptcha_get_html($this->public_key), null, true);
82         } else { 
83             $action->raw(recaptcha_get_html($this->public_key));
84         }
85         $action->elementEnd('li');
86         return true;
87     }
88
89     function onStartRegistrationTry($action)
90     {
91         $resp = recaptcha_check_answer ($this->private_key,
92                                         $_SERVER["REMOTE_ADDR"],
93                                         $action->trimmed('recaptcha_challenge_field'),
94                                         $action->trimmed('recaptcha_response_field'));
95
96         if (!$resp->is_valid) 
97         {
98             if($this->display_errors)
99             { 
100                 $action->showForm ("(reCAPTCHA said: " . $resp->error . ")");
101             }
102             $action->showForm("Captcha does not match!");
103             return false;
104         }
105     }
106 }