public function executeCaptcha(sfWebRequest $request) { $options = sfContext::getInstance()->getStorage()->read('captcha/options'); $phrase = sfContext::getInstance()->getStorage()->read('captcha/phrase'); $this->forward404If(sfPEARcaptchaUtil::getHash($options, $phrase) != $request->getParameter('hash')); if ($options === null) { $options = array(); } $options['phrase'] = $phrase; $captcha = Text_CAPTCHA::factory('Image'); $captcha->init($options); if ($options['output'] == 'resource') { $this->getResponse()->setContentType('image/png'); imagepng($captcha->getCAPTCHA()); } else { $this->getResponse()->setContentType('image/' . $options['output']); $this->getResponse()->setContent($captcha->getCAPTCHA()); } return sfView::NONE; }
/** * Renders the captcha for widget. * * @param string $name The element name * @param string $value The this widget is checked if value is not null * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes * @param array $errors An array of errors for the field * * @return string An HTML tag string */ public function renderCaptcha($name, $value = null, $attributes = array(), $errors = array()) { sfContext::getInstance()->getConfiguration()->loadHelpers('Url'); $fontPath = realpath($this->getOption('font_file')); if ($fontPath === false) { $fontPath = sfConfig::get('sf_data_dir') . '/font'; } $options = array('width' => $this->getOption('width'), 'height' => $this->getOption('height'), 'output' => $this->getOption('output'), 'imageOptions' => array('font_size' => $this->getOption('font_size'), 'font_path' => $fontPath, 'font_file' => basename($this->getOption('font_file')), 'text_color' => $this->getOption('text_color'), 'lines_color' => $this->getOption('lines_color'), 'background_color' => $this->getOption('background_color'), 'antialias' => $this->getOption('antialias'))); $captcha = Text_CAPTCHA::factory('Image'); $captcha->init($options); @session_start(); sfContext::getInstance()->getStorage()->write('captcha/options', $options); sfContext::getInstance()->getStorage()->write('captcha/phrase', $captcha->getPhrase()); $imgAttributes = array_merge($attributes, array('id' => $this->generateId($name) . '_captcha', 'class' => 'captcha', 'src' => url_for('captcha_image', array('hash' => sfPEARcaptchaUtil::getHash($options, $captcha->getPhrase()))))); return $this->renderTag('img', $imgAttributes); }