File: sfWidgetFormReCaptcha.class.php

package info (click to toggle)
netbeans 7.0.1%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 625,684 kB
  • sloc: java: 4,186,825; xml: 465,705; php: 38,485; cpp: 19,252; ansic: 13,667; jsp: 10,961; sh: 9,710; sql: 1,469; makefile: 991; haskell: 698; objc: 288; perl: 265; fortran: 262; yacc: 30; awk: 17; lex: 11; asm: 4
file content (81 lines) | stat: -rw-r--r-- 2,822 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php

/*
 * This file is part of the symfony package.
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfWidgetFormReCaptcha renders a ReCaptcha widget.
 *
 * This widget uses ReCaptcha: http://recaptcha.net/
 *
 * The ReCaptcha API documentation can be found at http://recaptcha.net/apidocs/captcha/
 *
 * To be able to use this widget, you need an API key: http://recaptcha.net/api/getkey
 *
 * As it's not possible to change the name of ReCaptcha fields, you will have to add them manually
 * when binding a form from an HTTP request.
 *
 * Here's a typical usage when embedding a captcha in a form with a contact[%s] name format:
 *
 *    $captcha = array(
 *      'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),
 *      'recaptcha_response_field'  => $request->getParameter('recaptcha_response_field'),
 *    );
 *    $this->form->bind(array_merge($request->getParameter('contact'), array('captcha' => $captcha)));
 *
 * @package    symfony
 * @subpackage widget
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: sfWidgetFormReCaptcha.class.php 7285 2008-02-03 09:50:35Z fabien $
 */
class sfWidgetFormReCaptcha extends sfWidgetForm
{
  /**
   * Configures the current widget.
   *
   * Available options:
   *
   *  * public_key:     The ReCaptcha public key
   *  * use_ssl:        Whether to use SSL or not (false by default)
   *  * server_url:     The URL for the HTTP API
   *  * server_url_ssl: The URL for the HTTPS API (when use_ssl is true)
   *
   * @see sfWidgetForm
   */
  public function configure($options = array(), $messages = array())
  {
    $this->addRequiredOption('public_key');

    $this->addOption('use_ssl', false);
    $this->addOption('server_url', 'http://api.recaptcha.net');
    $this->addOption('server_url_ssl', 'https://api-secure.recaptcha.net');
  }

  /**
   * @see sfWidgetForm
   */
  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    $server = $this->getServerUrl();
    $key = $this->getOption('public_key');

    return sprintf('
    <script type="text/javascript" src="%s/challenge?k=%s"></script>
    <noscript>
      <iframe src="%s/noscript?k=%s" height="300" width="500" frameborder="0"></iframe><br />
      <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
      <input type="hidden" name="recaptcha_response_field" value="manual_challenge" />
    </noscript>
    ', $server, $key, $server, $key);
  }

  protected function getServerUrl()
  {
    return $this->getOption('use_ssl') ? $this->getOption('server_url_ssl') : $this->getOption('server_url');
  }
}