1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl2.html)
4 * @author     Adrian Schlegel <adrian.schlegel@liip.ch>
5 * @author     Martin Gross <martin@pc-coholic.de>
6 *
7 */
8
9if(!defined('DOKU_INC')) die();
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(dirname(__FILE__).'/lib/autoload.php');
12
13class helper_plugin_recaptcha2 extends DokuWiki_Plugin {
14
15    /**
16     * Check if the reCAPTCHA should be used. Always check this before using the methods below.
17     *
18     * @return bool true when the reCAPTCHA should be used
19     */
20    function isEnabled(){
21        if(!$this->getConf('forusers') && $_SERVER['REMOTE_USER']) return false;
22        return true;
23    }
24
25    /**
26     * check the validity of the recaptcha
27     *
28     * @return obj (@see ReCaptchaResponse)
29     */
30    function check() {
31        // Check the recaptcha answer and only submit if correct
32      	$recaptcha = new \ReCaptcha\ReCaptcha($this->getConf('privatekey'));
33      	$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
34      	return $resp;
35    }
36
37
38    /**
39     * return the html code for the recaptcha block
40     * @return string
41     */
42    function getHTML($editblock) {
43        $lang = $this->getConf('lang') ? $this->getConf('lang') : $conf['lang'];
44       	$stylewidth = $editblock ? "100%" : "75%";
45      	$captchahtml = '<div class="g-recaptcha" data-sitekey="' . $this->getConf('publickey') . '" data-theme="' . $this->getConf('theme') . '" style="margin: 0 auto; display: block; width: '. $stylewidth . ';"></div>
46            <script type="text/javascript"
47                    src="https://www.google.com/recaptcha/api.js?hl=' . $lang . '">
48            </script>
49            <br>';
50       	return $captchahtml;
51    }
52}
53