*/ class action_plugin_recaptcha extends DokuWiki_Action_Plugin { private $recaptchaLangs = array('en', 'nl', 'fr', 'de', 'pt', 'ru', 'es', 'tr'); /** * get plugin info * */ function getInfo() { return confToHash(dirname(__FILE__).'../info.txt'); } /** * register an event hook * */ function register(&$controller) { // only register the hooks if the necessary config paramters exist if($this->getConf('publickey') && $this->getConf('privatekey')) { $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'preprocess', array()); // new hook $controller->register_hook('HTML_REGISTERFORM_OUTPUT', 'BEFORE', $this, 'insert', array('oldhook' => false)); // old hook $controller->register_hook('HTML_REGISTERFORM_INJECTION', 'BEFORE', $this, 'insert', array('oldhook' => true)); // old hook $controller->register_hook('HTML_EDITFORM_INJECTION', 'BEFORE', $this, 'insert', array('editform' => true, 'oldhook' => true)); // new hook $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'insert', array('editform' => true, 'oldhook' => false)); } } /** * insert html code for recaptcha into the register form * * @param obj $event * @param array $param */ function insert(&$event, $param) { global $conf; // do nothing if logged in user and no CAPTCHA required if(!$this->getConf('forusers') && $_SERVER['REMOTE_USER']){ return; } $recaptcha = '
'; // see first if a language is defined for the plugin, if not try to use the language defined for dokuwiki $lang = $this->getConf('lang') ? $this->getConf('lang') : (in_array($conf['lang'], $this->recaptchaLangs) ? $conf['lang'] : 'en'); $recaptcha .= ""; $recaptcha .= recaptcha_get_html($this->getConf('publickey')); if($param['oldhook']) { echo $recaptcha; } else { $pos = $event->data->findElementByAttribute('type','submit'); $event->data->insertElement($pos++, $recaptcha); } } /** * process the answer to the captcha * * @param obj $event * @param array $param * */ function preprocess(&$event, $param) { // get and clean the action $act = $this->_act_clean($event->data); // do nothing if logged in user and no CAPTCHA required if(!$this->getConf('forusers') && $_SERVER['REMOTE_USER']){ return(0); } if (($act == 'register' && $_POST['save']) || ($act == 'show' && $_REQUEST['comment'] == 'add')) { $resp = recaptcha_check_answer ($this->getConf('privatekey'), $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); if (!$resp->is_valid) { msg($this->getLang('testfailed'),-1); $_POST['save'] = 0; #for register return 'recapchta not valid'; } return (0); } return 'unknown action'; } /** * Pre-Sanitize the action command * * Similar to act_clean in action.php but simplified and without * error messages * (taken from Andreas Gohrs captcha plugin) */ function _act_clean($act){ // check if the action was given as array key if(is_array($act)){ list($act) = array_keys($act); } //remove all bad chars $act = strtolower($act); $act = preg_replace('/[^a-z_]+/','',$act); return $act; } } //end of action class