xref: /plugin/captcha/helper.php (revision 28c1464375855d34176c4c82b0af17bf2676f457)
177e00bf9SAndreas Gohr<?php
277e00bf9SAndreas Gohr/**
377e00bf9SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
477e00bf9SAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
577e00bf9SAndreas Gohr */
677e00bf9SAndreas Gohr// must be run within Dokuwiki
777e00bf9SAndreas Gohrif(!defined('DOKU_INC')) die();
877e00bf9SAndreas Gohrif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
977e00bf9SAndreas Gohrrequire_once(DOKU_INC.'inc/blowfish.php');
1077e00bf9SAndreas Gohr
1177e00bf9SAndreas Gohrclass helper_plugin_captcha extends DokuWiki_Plugin {
1277e00bf9SAndreas Gohr
1323d379a8SAndreas Gohr    protected $field_in = 'plugin__captcha';
1423d379a8SAndreas Gohr    protected $field_sec = 'plugin__captcha_secret';
1523d379a8SAndreas Gohr    protected $field_hp = 'plugin__captcha_honeypot';
1623d379a8SAndreas Gohr
1723d379a8SAndreas Gohr    /**
1823d379a8SAndreas Gohr     * Constructor. Initializes field names
1923d379a8SAndreas Gohr     */
20104ec268SAndreas Gohr    public function __construct() {
2123d379a8SAndreas Gohr        $this->field_in  = md5($this->_fixedIdent().$this->field_in);
2223d379a8SAndreas Gohr        $this->field_sec = md5($this->_fixedIdent().$this->field_sec);
2323d379a8SAndreas Gohr        $this->field_hp  = md5($this->_fixedIdent().$this->field_hp);
2423d379a8SAndreas Gohr    }
2523d379a8SAndreas Gohr
2677e00bf9SAndreas Gohr    /**
2777e00bf9SAndreas Gohr     * Check if the CAPTCHA should be used. Always check this before using the methods below.
2877e00bf9SAndreas Gohr     *
2977e00bf9SAndreas Gohr     * @return bool true when the CAPTCHA should be used
3077e00bf9SAndreas Gohr     */
31104ec268SAndreas Gohr    public function isEnabled() {
3277e00bf9SAndreas Gohr        if(!$this->getConf('forusers') && $_SERVER['REMOTE_USER']) return false;
3377e00bf9SAndreas Gohr        return true;
3477e00bf9SAndreas Gohr    }
3577e00bf9SAndreas Gohr
3677e00bf9SAndreas Gohr    /**
3777e00bf9SAndreas Gohr     * Returns the HTML to display the CAPTCHA with the chosen method
3877e00bf9SAndreas Gohr     */
39104ec268SAndreas Gohr    public function getHTML() {
4077e00bf9SAndreas Gohr        global $ID;
4177e00bf9SAndreas Gohr
4277e00bf9SAndreas Gohr        $rand = (float) (rand(0, 10000)) / 10000;
439e312724SAndreas Gohr        if($this->getConf('mode') == 'math') {
449e312724SAndreas Gohr            $code = $this->_generateMATH($this->_fixedIdent(), $rand);
459e312724SAndreas Gohr            $code = $code[0];
469e312724SAndreas Gohr            $text = $this->getLang('fillmath');
479e312724SAndreas Gohr        } else {
4877e00bf9SAndreas Gohr            $code = $this->_generateCAPTCHA($this->_fixedIdent(), $rand);
499e312724SAndreas Gohr            $text = $this->getLang('fillcaptcha');
509e312724SAndreas Gohr        }
5177e00bf9SAndreas Gohr        $secret = PMA_blowfish_encrypt($rand, auth_cookiesalt());
5277e00bf9SAndreas Gohr
53*28c14643SAndreas Gohr        $txtlen = $this->getConf('lettercount');
54*28c14643SAndreas Gohr
5577e00bf9SAndreas Gohr        $out = '';
5677e00bf9SAndreas Gohr        $out .= '<div id="plugin__captcha_wrapper">';
5723d379a8SAndreas Gohr        $out .= '<input type="hidden" name="'.$this->field_sec.'" value="'.hsc($secret).'" />';
589e312724SAndreas Gohr        $out .= '<label for="plugin__captcha">'.$text.'</label> ';
5923d379a8SAndreas Gohr
6077e00bf9SAndreas Gohr        switch($this->getConf('mode')) {
6177e00bf9SAndreas Gohr            case 'text':
629e312724SAndreas Gohr            case 'math':
6377e00bf9SAndreas Gohr                $out .= $code;
6477e00bf9SAndreas Gohr                break;
6577e00bf9SAndreas Gohr            case 'js':
6677e00bf9SAndreas Gohr                $out .= '<span id="plugin__captcha_code">'.$code.'</span>';
6777e00bf9SAndreas Gohr                break;
6877e00bf9SAndreas Gohr            case 'image':
6977e00bf9SAndreas Gohr                $out .= '<img src="'.DOKU_BASE.'lib/plugins/captcha/img.php?secret='.rawurlencode($secret).'&amp;id='.$ID.'" '.
7077e00bf9SAndreas Gohr                    ' width="'.$this->getConf('width').'" height="'.$this->getConf('height').'" alt="" /> ';
7177e00bf9SAndreas Gohr                break;
7277e00bf9SAndreas Gohr            case 'audio':
7377e00bf9SAndreas Gohr                $out .= '<img src="'.DOKU_BASE.'lib/plugins/captcha/img.php?secret='.rawurlencode($secret).'&amp;id='.$ID.'" '.
7477e00bf9SAndreas Gohr                    ' width="'.$this->getConf('width').'" height="'.$this->getConf('height').'" alt="" /> ';
7577e00bf9SAndreas Gohr                $out .= '<a href="'.DOKU_BASE.'lib/plugins/captcha/wav.php?secret='.rawurlencode($secret).'&amp;id='.$ID.'"'.
7677e00bf9SAndreas Gohr                    ' class="JSnocheck" title="'.$this->getLang('soundlink').'">';
7777e00bf9SAndreas Gohr                $out .= '<img src="'.DOKU_BASE.'lib/plugins/captcha/sound.png" width="16" height="16"'.
7877e00bf9SAndreas Gohr                    ' alt="'.$this->getLang('soundlink').'" /></a>';
7977e00bf9SAndreas Gohr                break;
8052e95008SAndreas Gohr            case 'figlet':
8152e95008SAndreas Gohr                require_once(dirname(__FILE__).'/figlet.php');
8252e95008SAndreas Gohr                $figlet = new phpFiglet();
8352e95008SAndreas Gohr                if($figlet->loadfont(dirname(__FILE__).'/figlet.flf')) {
8452e95008SAndreas Gohr                    $out .= '<pre>';
8552e95008SAndreas Gohr                    $out .= rtrim($figlet->fetch($code));
8652e95008SAndreas Gohr                    $out .= '</pre>';
8752e95008SAndreas Gohr                } else {
8852e95008SAndreas Gohr                    msg('Failed to load figlet.flf font file. CAPTCHA broken', -1);
8952e95008SAndreas Gohr                }
9052e95008SAndreas Gohr                break;
9177e00bf9SAndreas Gohr        }
92*28c14643SAndreas Gohr        $out .= ' <input type="text" size="'.$txtlen.'" maxlength="'.$txtlen.'" name="'.$this->field_in.'" class="edit" /> ';
9323d379a8SAndreas Gohr
9423d379a8SAndreas Gohr        // add honeypot field
9523d379a8SAndreas Gohr        $out .= '<label class="no">Please keep this field empty: <input type="text" name="'.$this->field_hp.'" /></label>';
9677e00bf9SAndreas Gohr        $out .= '</div>';
9777e00bf9SAndreas Gohr        return $out;
9877e00bf9SAndreas Gohr    }
9977e00bf9SAndreas Gohr
10077e00bf9SAndreas Gohr    /**
10177e00bf9SAndreas Gohr     * Checks if the the CAPTCHA was solved correctly
10277e00bf9SAndreas Gohr     *
10377e00bf9SAndreas Gohr     * @param  bool $msg when true, an error will be signalled through the msg() method
10477e00bf9SAndreas Gohr     * @return bool true when the answer was correct, otherwise false
10577e00bf9SAndreas Gohr     */
106104ec268SAndreas Gohr    public function check($msg = true) {
10777e00bf9SAndreas Gohr        // compare provided string with decrypted captcha
10823d379a8SAndreas Gohr        $rand = PMA_blowfish_decrypt($_REQUEST[$this->field_sec], auth_cookiesalt());
1099e312724SAndreas Gohr
1109e312724SAndreas Gohr        if($this->getConf('mode') == 'math') {
1119e312724SAndreas Gohr            $code = $this->_generateMATH($this->_fixedIdent(), $rand);
1129e312724SAndreas Gohr            $code = $code[1];
1139e312724SAndreas Gohr        } else {
11477e00bf9SAndreas Gohr            $code = $this->_generateCAPTCHA($this->_fixedIdent(), $rand);
1159e312724SAndreas Gohr        }
11677e00bf9SAndreas Gohr
11723d379a8SAndreas Gohr        if(!$_REQUEST[$this->field_sec] ||
11823d379a8SAndreas Gohr            !$_REQUEST[$this->field_in] ||
11923d379a8SAndreas Gohr            strtoupper($_REQUEST[$this->field_in]) != $code ||
12023d379a8SAndreas Gohr            trim($_REQUEST[$this->field_hp]) !== ''
12123d379a8SAndreas Gohr        ) {
12277e00bf9SAndreas Gohr            if($msg) msg($this->getLang('testfailed'), -1);
12377e00bf9SAndreas Gohr            return false;
12477e00bf9SAndreas Gohr        }
12577e00bf9SAndreas Gohr        return true;
12677e00bf9SAndreas Gohr    }
12777e00bf9SAndreas Gohr
12877e00bf9SAndreas Gohr    /**
12977e00bf9SAndreas Gohr     * Build a semi-secret fixed string identifying the current page and user
13077e00bf9SAndreas Gohr     *
13177e00bf9SAndreas Gohr     * This string is always the same for the current user when editing the same
13227d84d8dSAndreas Gohr     * page revision, but only for one day. Editing a page before midnight and saving
13327d84d8dSAndreas Gohr     * after midnight will result in a failed CAPTCHA once, but makes sure it can
13427d84d8dSAndreas Gohr     * not be reused which is especially important for the registration form where the
13527d84d8dSAndreas Gohr     * $ID usually won't change.
136104ec268SAndreas Gohr     *
137104ec268SAndreas Gohr     * @return string
13877e00bf9SAndreas Gohr     */
139104ec268SAndreas Gohr    public function _fixedIdent() {
14077e00bf9SAndreas Gohr        global $ID;
14177e00bf9SAndreas Gohr        $lm = @filemtime(wikiFN($ID));
14227d84d8dSAndreas Gohr        $td = date('Y-m-d');
14377e00bf9SAndreas Gohr        return auth_browseruid().
14477e00bf9SAndreas Gohr            auth_cookiesalt().
14527d84d8dSAndreas Gohr            $ID.$lm.$td;
14677e00bf9SAndreas Gohr    }
14777e00bf9SAndreas Gohr
14877e00bf9SAndreas Gohr    /**
149*28c14643SAndreas Gohr     * Generates a random char string
15077e00bf9SAndreas Gohr     *
151104ec268SAndreas Gohr     * @param $fixed string the fixed part, any string
152104ec268SAndreas Gohr     * @param $rand  float  some random number between 0 and 1
15323d379a8SAndreas Gohr     * @return string
15477e00bf9SAndreas Gohr     */
155104ec268SAndreas Gohr    public function _generateCAPTCHA($fixed, $rand) {
15677e00bf9SAndreas Gohr        $fixed   = hexdec(substr(md5($fixed), 5, 5)); // use part of the md5 to generate an int
15777e00bf9SAndreas Gohr        $numbers = md5($rand * $fixed); // combine both values
15877e00bf9SAndreas Gohr
15977e00bf9SAndreas Gohr        // now create the letters
16077e00bf9SAndreas Gohr        $code = '';
161*28c14643SAndreas Gohr        for($i = 0; $i < ($this->getConf('lettercount') * 2); $i += 2) {
16277e00bf9SAndreas Gohr            $code .= chr(floor(hexdec($numbers[$i].$numbers[$i + 1]) / 10) + 65);
16377e00bf9SAndreas Gohr        }
16477e00bf9SAndreas Gohr
16577e00bf9SAndreas Gohr        return $code;
16677e00bf9SAndreas Gohr    }
16777e00bf9SAndreas Gohr
1689e312724SAndreas Gohr    /**
1699e312724SAndreas Gohr     * Create a mathematical task and its result
1709e312724SAndreas Gohr     *
171104ec268SAndreas Gohr     * @param $fixed string the fixed part, any string
172104ec268SAndreas Gohr     * @param $rand  float  some random number between 0 and 1
173104ec268SAndreas Gohr     * @return array taks, result
1749e312724SAndreas Gohr     */
175104ec268SAndreas Gohr    protected function _generateMATH($fixed, $rand) {
1769e312724SAndreas Gohr        $fixed   = hexdec(substr(md5($fixed), 5, 5)); // use part of the md5 to generate an int
1779e312724SAndreas Gohr        $numbers = md5($rand * $fixed); // combine both values
1789e312724SAndreas Gohr
1799e312724SAndreas Gohr        // first letter is the operator (+/-)
1809e312724SAndreas Gohr        $op  = (hexdec($numbers[0]) > 8) ? -1 : 1;
1819e312724SAndreas Gohr        $num = array(hexdec($numbers[1].$numbers[2]), hexdec($numbers[3]));
1829e312724SAndreas Gohr
1839e312724SAndreas Gohr        // we only want positive results
1849e312724SAndreas Gohr        if(($op < 0) && ($num[0] < $num[1])) rsort($num);
1859e312724SAndreas Gohr
1869e312724SAndreas Gohr        // prepare result and task text
1879e312724SAndreas Gohr        $res  = $num[0] + ($num[1] * $op);
1889e312724SAndreas Gohr        $task = $num[0].(($op < 0) ? '&nbsp;-&nbsp;' : '&nbsp;+&nbsp;').$num[1].'&nbsp;=&nbsp;?';
1899e312724SAndreas Gohr
1909e312724SAndreas Gohr        return array($task, $res);
1919e312724SAndreas Gohr    }
19277e00bf9SAndreas Gohr
19377e00bf9SAndreas Gohr    /**
19477e00bf9SAndreas Gohr     * Create a CAPTCHA image
195104ec268SAndreas Gohr     *
196104ec268SAndreas Gohr     * @param string $text the letters to display
19777e00bf9SAndreas Gohr     */
198104ec268SAndreas Gohr    public function _imageCAPTCHA($text) {
19977e00bf9SAndreas Gohr        $w = $this->getConf('width');
20077e00bf9SAndreas Gohr        $h = $this->getConf('height');
20177e00bf9SAndreas Gohr
202dc091fd0SAndreas Gohr        $fonts = glob(dirname(__FILE__).'/fonts/*.ttf');
203dc091fd0SAndreas Gohr
20477e00bf9SAndreas Gohr        // create a white image
205*28c14643SAndreas Gohr        $img = imagecreatetruecolor($w, $h);
206*28c14643SAndreas Gohr        $white = imagecolorallocate($img, 255, 255, 255);
207*28c14643SAndreas Gohr        imagefill($img, 0, 0, $white);
20877e00bf9SAndreas Gohr
20977e00bf9SAndreas Gohr        // add some lines as background noise
21077e00bf9SAndreas Gohr        for($i = 0; $i < 30; $i++) {
21177e00bf9SAndreas Gohr            $color = imagecolorallocate($img, rand(100, 250), rand(100, 250), rand(100, 250));
21277e00bf9SAndreas Gohr            imageline($img, rand(0, $w), rand(0, $h), rand(0, $w), rand(0, $h), $color);
21377e00bf9SAndreas Gohr        }
21477e00bf9SAndreas Gohr
21577e00bf9SAndreas Gohr        // draw the letters
216*28c14643SAndreas Gohr        $txtlen = strlen($text);
217*28c14643SAndreas Gohr        for($i = 0; $i < $txtlen; $i++) {
218dc091fd0SAndreas Gohr            $font  = $fonts[array_rand($fonts)];
21977e00bf9SAndreas Gohr            $color = imagecolorallocate($img, rand(0, 100), rand(0, 100), rand(0, 100));
22077e00bf9SAndreas Gohr            $size  = rand(floor($h / 1.8), floor($h * 0.7));
22177e00bf9SAndreas Gohr            $angle = rand(-35, 35);
22277e00bf9SAndreas Gohr
223*28c14643SAndreas Gohr            $x       = ($w * 0.05) + $i * floor($w * 0.9 / $txtlen);
22477e00bf9SAndreas Gohr            $cheight = $size + ($size * 0.5);
22577e00bf9SAndreas Gohr            $y       = floor($h / 2 + $cheight / 3.8);
22677e00bf9SAndreas Gohr
22777e00bf9SAndreas Gohr            imagettftext($img, $size, $angle, $x, $y, $color, $font, $text[$i]);
22877e00bf9SAndreas Gohr        }
22977e00bf9SAndreas Gohr
23077e00bf9SAndreas Gohr        header("Content-type: image/png");
23177e00bf9SAndreas Gohr        imagepng($img);
23277e00bf9SAndreas Gohr        imagedestroy($img);
23377e00bf9SAndreas Gohr    }
23477e00bf9SAndreas Gohr
23577e00bf9SAndreas Gohr}
236