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'); 47*df8afac4SAndreas Gohr } elseif($this->getConf('mode') == 'question') { 48*df8afac4SAndreas Gohr $text = $this->getConf('question'); 499e312724SAndreas Gohr } else { 5077e00bf9SAndreas Gohr $code = $this->_generateCAPTCHA($this->_fixedIdent(), $rand); 519e312724SAndreas Gohr $text = $this->getLang('fillcaptcha'); 529e312724SAndreas Gohr } 5377e00bf9SAndreas Gohr $secret = PMA_blowfish_encrypt($rand, auth_cookiesalt()); 5477e00bf9SAndreas Gohr 5528c14643SAndreas Gohr $txtlen = $this->getConf('lettercount'); 5628c14643SAndreas Gohr 5777e00bf9SAndreas Gohr $out = ''; 5877e00bf9SAndreas Gohr $out .= '<div id="plugin__captcha_wrapper">'; 5923d379a8SAndreas Gohr $out .= '<input type="hidden" name="'.$this->field_sec.'" value="'.hsc($secret).'" />'; 609e312724SAndreas Gohr $out .= '<label for="plugin__captcha">'.$text.'</label> '; 6123d379a8SAndreas Gohr 6277e00bf9SAndreas Gohr switch($this->getConf('mode')) { 639e312724SAndreas Gohr case 'math': 649d6f09afSAndreas Gohr case 'text': 659d6f09afSAndreas Gohr $out .= $this->_obfuscateText($code); 6677e00bf9SAndreas Gohr break; 6777e00bf9SAndreas Gohr case 'js': 689d6f09afSAndreas Gohr $out .= '<span id="plugin__captcha_code">'.$this->_obfuscateText($code).'</span>'; 6977e00bf9SAndreas Gohr break; 7077e00bf9SAndreas Gohr case 'image': 7177e00bf9SAndreas Gohr $out .= '<img src="'.DOKU_BASE.'lib/plugins/captcha/img.php?secret='.rawurlencode($secret).'&id='.$ID.'" '. 7277e00bf9SAndreas Gohr ' width="'.$this->getConf('width').'" height="'.$this->getConf('height').'" alt="" /> '; 7377e00bf9SAndreas Gohr break; 7477e00bf9SAndreas Gohr case 'audio': 7577e00bf9SAndreas Gohr $out .= '<img src="'.DOKU_BASE.'lib/plugins/captcha/img.php?secret='.rawurlencode($secret).'&id='.$ID.'" '. 7677e00bf9SAndreas Gohr ' width="'.$this->getConf('width').'" height="'.$this->getConf('height').'" alt="" /> '; 7777e00bf9SAndreas Gohr $out .= '<a href="'.DOKU_BASE.'lib/plugins/captcha/wav.php?secret='.rawurlencode($secret).'&id='.$ID.'"'. 7877e00bf9SAndreas Gohr ' class="JSnocheck" title="'.$this->getLang('soundlink').'">'; 7977e00bf9SAndreas Gohr $out .= '<img src="'.DOKU_BASE.'lib/plugins/captcha/sound.png" width="16" height="16"'. 8077e00bf9SAndreas Gohr ' alt="'.$this->getLang('soundlink').'" /></a>'; 8177e00bf9SAndreas Gohr break; 8252e95008SAndreas Gohr case 'figlet': 8352e95008SAndreas Gohr require_once(dirname(__FILE__).'/figlet.php'); 8452e95008SAndreas Gohr $figlet = new phpFiglet(); 8552e95008SAndreas Gohr if($figlet->loadfont(dirname(__FILE__).'/figlet.flf')) { 8652e95008SAndreas Gohr $out .= '<pre>'; 8752e95008SAndreas Gohr $out .= rtrim($figlet->fetch($code)); 8852e95008SAndreas Gohr $out .= '</pre>'; 8952e95008SAndreas Gohr } else { 9052e95008SAndreas Gohr msg('Failed to load figlet.flf font file. CAPTCHA broken', -1); 9152e95008SAndreas Gohr } 9252e95008SAndreas Gohr break; 9377e00bf9SAndreas Gohr } 94*df8afac4SAndreas Gohr $out .= ' <input type="text" size="'.$txtlen.'" name="'.$this->field_in.'" class="edit" /> '; 9523d379a8SAndreas Gohr 9623d379a8SAndreas Gohr // add honeypot field 979d63c05fSlainme $out .= '<label class="no">'.$this->getLang('honeypot').'<input type="text" name="'.$this->field_hp.'" /></label>'; 9877e00bf9SAndreas Gohr $out .= '</div>'; 9977e00bf9SAndreas Gohr return $out; 10077e00bf9SAndreas Gohr } 10177e00bf9SAndreas Gohr 10277e00bf9SAndreas Gohr /** 10377e00bf9SAndreas Gohr * Checks if the the CAPTCHA was solved correctly 10477e00bf9SAndreas Gohr * 10577e00bf9SAndreas Gohr * @param bool $msg when true, an error will be signalled through the msg() method 10677e00bf9SAndreas Gohr * @return bool true when the answer was correct, otherwise false 10777e00bf9SAndreas Gohr */ 108104ec268SAndreas Gohr public function check($msg = true) { 10977e00bf9SAndreas Gohr // compare provided string with decrypted captcha 11023d379a8SAndreas Gohr $rand = PMA_blowfish_decrypt($_REQUEST[$this->field_sec], auth_cookiesalt()); 1119e312724SAndreas Gohr 1129e312724SAndreas Gohr if($this->getConf('mode') == 'math') { 1139e312724SAndreas Gohr $code = $this->_generateMATH($this->_fixedIdent(), $rand); 1149e312724SAndreas Gohr $code = $code[1]; 115*df8afac4SAndreas Gohr } elseif($this->getConf('mode') == 'question') { 116*df8afac4SAndreas Gohr $code = $this->getConf('answer'); 1179e312724SAndreas Gohr } else { 11877e00bf9SAndreas Gohr $code = $this->_generateCAPTCHA($this->_fixedIdent(), $rand); 1199e312724SAndreas Gohr } 12077e00bf9SAndreas Gohr 12123d379a8SAndreas Gohr if(!$_REQUEST[$this->field_sec] || 12223d379a8SAndreas Gohr !$_REQUEST[$this->field_in] || 123*df8afac4SAndreas Gohr utf8_strtolower($_REQUEST[$this->field_in]) != utf8_strtolower($code) || 12423d379a8SAndreas Gohr trim($_REQUEST[$this->field_hp]) !== '' 12523d379a8SAndreas Gohr ) { 12677e00bf9SAndreas Gohr if($msg) msg($this->getLang('testfailed'), -1); 12777e00bf9SAndreas Gohr return false; 12877e00bf9SAndreas Gohr } 12977e00bf9SAndreas Gohr return true; 13077e00bf9SAndreas Gohr } 13177e00bf9SAndreas Gohr 13277e00bf9SAndreas Gohr /** 13377e00bf9SAndreas Gohr * Build a semi-secret fixed string identifying the current page and user 13477e00bf9SAndreas Gohr * 13577e00bf9SAndreas Gohr * This string is always the same for the current user when editing the same 13627d84d8dSAndreas Gohr * page revision, but only for one day. Editing a page before midnight and saving 13727d84d8dSAndreas Gohr * after midnight will result in a failed CAPTCHA once, but makes sure it can 13827d84d8dSAndreas Gohr * not be reused which is especially important for the registration form where the 13927d84d8dSAndreas Gohr * $ID usually won't change. 140104ec268SAndreas Gohr * 141104ec268SAndreas Gohr * @return string 14277e00bf9SAndreas Gohr */ 143104ec268SAndreas Gohr public function _fixedIdent() { 14477e00bf9SAndreas Gohr global $ID; 14577e00bf9SAndreas Gohr $lm = @filemtime(wikiFN($ID)); 14627d84d8dSAndreas Gohr $td = date('Y-m-d'); 14777e00bf9SAndreas Gohr return auth_browseruid(). 14877e00bf9SAndreas Gohr auth_cookiesalt(). 14927d84d8dSAndreas Gohr $ID.$lm.$td; 15077e00bf9SAndreas Gohr } 15177e00bf9SAndreas Gohr 15277e00bf9SAndreas Gohr /** 1539d6f09afSAndreas Gohr * Adds random space characters within the given text 1549d6f09afSAndreas Gohr * 1559d6f09afSAndreas Gohr * Keeps subsequent numbers without spaces (for math problem) 1569d6f09afSAndreas Gohr * 1579d6f09afSAndreas Gohr * @param $text 1589d6f09afSAndreas Gohr * @return string 1599d6f09afSAndreas Gohr */ 1609d6f09afSAndreas Gohr protected function _obfuscateText($text){ 1619d6f09afSAndreas Gohr $new = ''; 1629d6f09afSAndreas Gohr 1639d6f09afSAndreas Gohr $spaces = array( 1649d6f09afSAndreas Gohr "\r", 1659d6f09afSAndreas Gohr "\n", 1669d6f09afSAndreas Gohr "\r\n", 1679d6f09afSAndreas Gohr ' ', 1689d6f09afSAndreas Gohr "\xC2\xA0", // \u00A0 NO-BREAK SPACE 1699d6f09afSAndreas Gohr "\xE2\x80\x80", // \u2000 EN QUAD 1709d6f09afSAndreas Gohr "\xE2\x80\x81", // \u2001 EM QUAD 1719d6f09afSAndreas Gohr "\xE2\x80\x82", // \u2002 EN SPACE 1729d6f09afSAndreas Gohr // "\xE2\x80\x83", // \u2003 EM SPACE 1739d6f09afSAndreas Gohr "\xE2\x80\x84", // \u2004 THREE-PER-EM SPACE 1749d6f09afSAndreas Gohr "\xE2\x80\x85", // \u2005 FOUR-PER-EM SPACE 1759d6f09afSAndreas Gohr "\xE2\x80\x86", // \u2006 SIX-PER-EM SPACE 1769d6f09afSAndreas Gohr "\xE2\x80\x87", // \u2007 FIGURE SPACE 1779d6f09afSAndreas Gohr "\xE2\x80\x88", // \u2008 PUNCTUATION SPACE 1789d6f09afSAndreas Gohr "\xE2\x80\x89", // \u2009 THIN SPACE 1799d6f09afSAndreas Gohr "\xE2\x80\x8A", // \u200A HAIR SPACE 1809d6f09afSAndreas Gohr "\xE2\x80\xAF", // \u202F NARROW NO-BREAK SPACE 1819d6f09afSAndreas Gohr "\xE2\x81\x9F", // \u205F MEDIUM MATHEMATICAL SPACE 1829d6f09afSAndreas Gohr 1839d6f09afSAndreas Gohr "\xE1\xA0\x8E\r\n", // \u180E MONGOLIAN VOWEL SEPARATOR 1849d6f09afSAndreas Gohr "\xE2\x80\x8B\r\n", // \u200B ZERO WIDTH SPACE 1859d6f09afSAndreas Gohr "\xEF\xBB\xBF\r\n", // \uFEFF ZERO WIDTH NO-BREAK SPACE 1869d6f09afSAndreas Gohr ); 1879d6f09afSAndreas Gohr 1889d6f09afSAndreas Gohr $len = strlen($text); 1899d6f09afSAndreas Gohr for($i=0; $i<$len-1; $i++){ 1909d6f09afSAndreas Gohr $new .= $text{$i}; 1919d6f09afSAndreas Gohr 1929d6f09afSAndreas Gohr if(!is_numeric($text{$i+1})){ 1939d6f09afSAndreas Gohr $new .= $spaces[array_rand($spaces)]; 1949d6f09afSAndreas Gohr } 1959d6f09afSAndreas Gohr } 1969d6f09afSAndreas Gohr $new .= $text{$len-1}; 1979d6f09afSAndreas Gohr return $new; 1989d6f09afSAndreas Gohr } 1999d6f09afSAndreas Gohr 2009d6f09afSAndreas Gohr /** 20128c14643SAndreas Gohr * Generates a random char string 20277e00bf9SAndreas Gohr * 203104ec268SAndreas Gohr * @param $fixed string the fixed part, any string 204104ec268SAndreas Gohr * @param $rand float some random number between 0 and 1 20523d379a8SAndreas Gohr * @return string 20677e00bf9SAndreas Gohr */ 207104ec268SAndreas Gohr public function _generateCAPTCHA($fixed, $rand) { 20877e00bf9SAndreas Gohr $fixed = hexdec(substr(md5($fixed), 5, 5)); // use part of the md5 to generate an int 20977e00bf9SAndreas Gohr $numbers = md5($rand * $fixed); // combine both values 21077e00bf9SAndreas Gohr 21177e00bf9SAndreas Gohr // now create the letters 21277e00bf9SAndreas Gohr $code = ''; 21328c14643SAndreas Gohr for($i = 0; $i < ($this->getConf('lettercount') * 2); $i += 2) { 21477e00bf9SAndreas Gohr $code .= chr(floor(hexdec($numbers[$i].$numbers[$i + 1]) / 10) + 65); 21577e00bf9SAndreas Gohr } 21677e00bf9SAndreas Gohr 21777e00bf9SAndreas Gohr return $code; 21877e00bf9SAndreas Gohr } 21977e00bf9SAndreas Gohr 2209e312724SAndreas Gohr /** 2219e312724SAndreas Gohr * Create a mathematical task and its result 2229e312724SAndreas Gohr * 223104ec268SAndreas Gohr * @param $fixed string the fixed part, any string 224104ec268SAndreas Gohr * @param $rand float some random number between 0 and 1 225104ec268SAndreas Gohr * @return array taks, result 2269e312724SAndreas Gohr */ 227104ec268SAndreas Gohr protected function _generateMATH($fixed, $rand) { 2289e312724SAndreas Gohr $fixed = hexdec(substr(md5($fixed), 5, 5)); // use part of the md5 to generate an int 2299e312724SAndreas Gohr $numbers = md5($rand * $fixed); // combine both values 2309e312724SAndreas Gohr 2319e312724SAndreas Gohr // first letter is the operator (+/-) 2329e312724SAndreas Gohr $op = (hexdec($numbers[0]) > 8) ? -1 : 1; 2339e312724SAndreas Gohr $num = array(hexdec($numbers[1].$numbers[2]), hexdec($numbers[3])); 2349e312724SAndreas Gohr 2359e312724SAndreas Gohr // we only want positive results 2369e312724SAndreas Gohr if(($op < 0) && ($num[0] < $num[1])) rsort($num); 2379e312724SAndreas Gohr 2389e312724SAndreas Gohr // prepare result and task text 2399e312724SAndreas Gohr $res = $num[0] + ($num[1] * $op); 2409d6f09afSAndreas Gohr $task = $num[0].(($op < 0) ? '-' : '+').$num[1].'=?'; 2419e312724SAndreas Gohr 2429e312724SAndreas Gohr return array($task, $res); 2439e312724SAndreas Gohr } 24477e00bf9SAndreas Gohr 24577e00bf9SAndreas Gohr /** 24677e00bf9SAndreas Gohr * Create a CAPTCHA image 247104ec268SAndreas Gohr * 248104ec268SAndreas Gohr * @param string $text the letters to display 24977e00bf9SAndreas Gohr */ 250104ec268SAndreas Gohr public function _imageCAPTCHA($text) { 25177e00bf9SAndreas Gohr $w = $this->getConf('width'); 25277e00bf9SAndreas Gohr $h = $this->getConf('height'); 25377e00bf9SAndreas Gohr 254dc091fd0SAndreas Gohr $fonts = glob(dirname(__FILE__).'/fonts/*.ttf'); 255dc091fd0SAndreas Gohr 25677e00bf9SAndreas Gohr // create a white image 25728c14643SAndreas Gohr $img = imagecreatetruecolor($w, $h); 25828c14643SAndreas Gohr $white = imagecolorallocate($img, 255, 255, 255); 25928c14643SAndreas Gohr imagefill($img, 0, 0, $white); 26077e00bf9SAndreas Gohr 26177e00bf9SAndreas Gohr // add some lines as background noise 26277e00bf9SAndreas Gohr for($i = 0; $i < 30; $i++) { 26377e00bf9SAndreas Gohr $color = imagecolorallocate($img, rand(100, 250), rand(100, 250), rand(100, 250)); 26477e00bf9SAndreas Gohr imageline($img, rand(0, $w), rand(0, $h), rand(0, $w), rand(0, $h), $color); 26577e00bf9SAndreas Gohr } 26677e00bf9SAndreas Gohr 26777e00bf9SAndreas Gohr // draw the letters 26828c14643SAndreas Gohr $txtlen = strlen($text); 26928c14643SAndreas Gohr for($i = 0; $i < $txtlen; $i++) { 270dc091fd0SAndreas Gohr $font = $fonts[array_rand($fonts)]; 27177e00bf9SAndreas Gohr $color = imagecolorallocate($img, rand(0, 100), rand(0, 100), rand(0, 100)); 27277e00bf9SAndreas Gohr $size = rand(floor($h / 1.8), floor($h * 0.7)); 27377e00bf9SAndreas Gohr $angle = rand(-35, 35); 27477e00bf9SAndreas Gohr 27528c14643SAndreas Gohr $x = ($w * 0.05) + $i * floor($w * 0.9 / $txtlen); 27677e00bf9SAndreas Gohr $cheight = $size + ($size * 0.5); 27777e00bf9SAndreas Gohr $y = floor($h / 2 + $cheight / 3.8); 27877e00bf9SAndreas Gohr 27977e00bf9SAndreas Gohr imagettftext($img, $size, $angle, $x, $y, $color, $font, $text[$i]); 28077e00bf9SAndreas Gohr } 28177e00bf9SAndreas Gohr 28277e00bf9SAndreas Gohr header("Content-type: image/png"); 28377e00bf9SAndreas Gohr imagepng($img); 28477e00bf9SAndreas Gohr imagedestroy($img); 28577e00bf9SAndreas Gohr } 28677e00bf9SAndreas Gohr 28777e00bf9SAndreas Gohr} 288