/** * Generates the captcha values * * @return array */ protected function _generate() { if ($this->captcha_generated === TRUE) { return; } # Choose the first number randomly between 6 and 10. This is to stop the answer being negative. $numberOne = mt_rand(6, 9); # Choose the second number randomly between 0 and 5. $numberTwo = mt_rand(1, 5); # Choose the operator randomly from the array. $captchaOperator = $this->captcha_operator_convert[mt_rand(0, count($this->captcha_operator_convert) - 1)]; # Get the equation in textual form to show to the user. $this->captcha_text = $numberOne . ' ' . $captchaOperator . ' ' . $numberTwo; # Evaluate the equation and get the result. $this->captcha_result = $numberOne - $numberTwo; # Session-Way (only one form at a time) - must be a component then //$this->Session->write('Captcha.result', $result); # DB-Way (several forms possible, high security via IP-Based max limits) // the following should be done in a component and passed to the view/helper // $Captcha = ClassRegistry::init('Captcha'); // $Captcha->new(); $Captcha->update(); etc # Timestamp-SessionID-Hash-Way (several forms possible, not as secure) $this->captcha_hash = SimpleCaptcha::buildHash(array('timestamp' => time(), 'result' => $this->captcha_result), $this->options); $this->captcha_generated = TRUE; return; }
/** * Validates captcha calculation * * flood protection by false fields and math code * TODO: build in floodProtection (max Trials etc) * TODO: SESSION based one as alternative * * @param array $data * @return bool */ protected function _validateCaptcha($data) { if (!isset($data['captcha'])) { # form inputs missing? SPAM! return $this->error('Captcha content missing'); } $captcha_params = array('timestamp' => $data['captcha_time'], 'result' => $data['captcha']); $hash = SimpleCaptcha::buildHash($captcha_params, $this->settings[$this->Model->alias]); if ($data['captcha_hash'] == $hash) { return true; } # wrong captcha content or session expired return $this->error('Captcha incorrect', 'SubmittedResult = \'' . $data['captcha'] . '\''); }