| 1 | <?php |
|---|
| 2 | Class recaptcha extends MvBlog_plugin implements MvBlog_pluginiface { |
|---|
| 3 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 71 | public function save_setting($requestdata) { |
|---|
| 72 | |
|---|
| 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 | |
|---|
| 78 | $sql = sprintf("UPDATE settings SET settingvalue='%s' WHERE settingname='recaptcha_privkey'", $requestdata["recaptcha_privkey"]); |
|---|
| 79 | } else { |
|---|
| 80 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 91 | $sql = sprintf("UPDATE settings SET settingvalue='%s' WHERE settingname='recaptcha_pubkey'", $requestdata["recaptcha_pubkey"]); |
|---|
| 92 | } else { |
|---|
| 93 | |
|---|
| 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 | ?> |
|---|