root/trunk/plugins/recaptcha.php

Revision 780, 3.8 KB (checked in by michiel, 7 months ago)

Add reCAPTCHA plugin.

For more info see http://recaptcha.net

Line 
1<?php
2Class recaptcha extends MvBlog_plugin implements MvBlog_pluginiface {
3    /* variables */
4    public $name    = "recaptcha";
5    public $author  = "Michiel van Baak";
6    public $license = "GPL";
7    public $website = "http://www.mvblog.org";
8    public $description = "reCAPTCHA captcha plugin.";
9
10    private $_mvblog;
11    private $_settings = array(
12        "recaptcha_pubkey"  => "",
13        "recaptcha_privkey" => ""
14    );
15
16    /* methods */
17    public function __construct(&$mvblog) {
18        $this->addHook("captcha_output", "show_captcha");
19        $this->addHook("captcha_check", "check_captcha");
20        $this->_mvblog =& $mvblog;
21    }
22
23    public function activate() {
24        /* populate some settings */
25        $sql = "SELECT * FROM settings WHERE settingname LIKE 'recaptcha_%'";
26        $res = $this->_mvblog->db->query($sql);
27        while ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
28            $this->_settings[$row["settingname"]] = $row["settingvalue"];
29        }
30
31    }
32
33    public function deactivate() {
34        /* destroy settings */
35        unset($this->_settings);
36    }
37
38    public function show_captcha() {
39        require_once("recaptcha_files/recaptchalib.php");
40        return recaptcha_get_html($this->_settings["recaptcha_pubkey"]);
41    }
42
43    public function check_captcha($data) {
44        require_once("recaptcha_files/recaptchalib.php");
45        $res = recaptcha_check_answer($this->_settings["recaptcha_privkey"],
46            $_SERVER["REMOTE_ADDR"],
47            $_POST["recaptcha_challenge_field"],
48            $_POST["recaptcha_response_field"]);
49        return $res->is_valid;
50    }
51    /* show_settings {{{ */
52    public function show_settings() {
53        $output  = "<form name=\"recaptcha_settings\" method=\"post\" action=\"index.php\">";
54        $output .= "<input type=\"hidden\" name=\"action\" value=\"save_plugin_setting\" />";
55        $output .= "<input type=\"hidden\" name=\"plugin\" value=\"recaptcha\" />";
56        $output .= "<table style=\"width: 220px;\"><tr>";
57        $output .= "<td>".gettext("Public reCAPTCHA key")."</td>";
58        $output .= "<td><input type=\"text\" name=\"recaptcha_pubkey\" value=\"".$this->_settings["recaptcha_pubkey"]."\" /></td>";
59        $output .= "</tr><tr>";
60        $output .= "<td>".gettext("Private reCAPTCHA key")."</td>";
61        $output .= "<td><input type=\"text\" name=\"recaptcha_privkey\" value=\"".$this->_settings["recaptcha_privkey"]."\" /></td>";
62        $output .= "</tr><tr>";
63        $output .= "<td colspan=\"2\"><input type=\"submit\" value=\"".gettext("Save")."\" /></td>";
64        $output .= "</tr></table>";
65
66        $output .= "</form>";
67        echo $output;
68    }
69    /* }}} */
70    /* save_setting {{{ */
71    public function save_setting($requestdata) {
72        /* first look if the setting is already there */
73        $sql = "SELECT COUNT(*) FROM settings WHERE settingname = 'recaptcha_privkey'";
74        $res = $this->_mvblog->db->query($sql);
75        $row = $res->fetchRow();
76        if ($row[0]) {
77            /* yes, so update */
78            $sql = sprintf("UPDATE settings SET settingvalue='%s' WHERE settingname='recaptcha_privkey'", $requestdata["recaptcha_privkey"]);
79        } else {
80            /* no, so insert */
81            $sql = sprintf("INSERT INTO settings (settingname, settingvalue) VALUES ('recaptcha_privkey', '%s')", $requestdata["recaptcha_privkey"]);
82        }
83        $res = $this->_mvblog->db->exec($sql);
84        $this->_settings["recaptcha_privkey"] = sprintf("%s", $requestdata["recaptcha_privkey"]);
85        /* first look if the setting is already there */
86        $sql = "SELECT COUNT(*) FROM settings WHERE settingname = 'recaptcha_pubkey'";
87        $res = $this->_mvblog->db->query($sql);
88        $row = $res->fetchRow();
89        if ($row[0]) {
90            /* yes, so update */
91            $sql = sprintf("UPDATE settings SET settingvalue='%s' WHERE settingname='recaptcha_pubkey'", $requestdata["recaptcha_pubkey"]);
92        } else {
93            /* no, so insert */
94            $sql = sprintf("INSERT INTO settings (settingname, settingvalue) VALUES ('recaptcha_pubkey', '%s')", $requestdata["recaptcha_pubkey"]);
95        }
96        $res = $this->_mvblog->db->exec($sql);
97        $this->_settings["recaptcha_pubkey"] = sprintf("%s", $requestdata["recaptcha_pubkey"]);
98        $this->show_settings();
99    }
100    /* }}} */
101}
102?>
Note: See TracBrowser for help on using the browser.