/[drupal]/contributions/modules/recaptcha/recaptcha.module
ViewVC logotype

Diff of /contributions/modules/recaptcha/recaptcha.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.13.2.4.2.11.2.9, Tue Jul 28 15:50:18 2009 UTC revision 1.13.2.4.2.11.2.10, Mon Aug 10 17:50:02 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  /* $Id: recaptcha.module,v 1.13.2.4.2.11.2.8 2009/07/28 15:44:09 robloach Exp $ */  /* $Id: recaptcha.module,v 1.13.2.4.2.11.2.9 2009/07/28 15:50:18 robloach Exp $ */
3    
4  /**  /**
5   * @file   * @file
# Line 49  function recaptcha_menu() { Line 49  function recaptcha_menu() {
49      'page arguments' => array('recaptcha_admin_settings'),      'page arguments' => array('recaptcha_admin_settings'),
50      'access arguments' => array('administer recaptcha'),      'access arguments' => array('administer recaptcha'),
51      'type' => MENU_LOCAL_TASK,      'type' => MENU_LOCAL_TASK,
52        'file' => 'recaptcha.admin.inc',
53    );    );
54    return $items;    return $items;
55  }  }
# Line 61  function recaptcha_perm() { Line 62  function recaptcha_perm() {
62  }  }
63    
64  /**  /**
  * Form callback; administrative settings for reCaptcha.  
  */  
 function recaptcha_admin_settings() {  
   // Load the recaptcha library.  
   recaptcha_load_library();  
   
   $form = array();  
   $form['recaptcha_public_key'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Public Key'),  
     '#default_value' => variable_get('recaptcha_public_key', ''),  
     '#maxlength' => 40,  
     '#description' => t('The public key given to you when you <a href="@url" target="_blank">registered at reCAPTCHA.net</a>.', array('@url' => url(recaptcha_get_signup_url($_SERVER['SERVER_NAME'], variable_get('site_name', ''))))),  
     '#required' => TRUE,  
    );  
   $form['recaptcha_private_key'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Private Key'),  
     '#default_value' => variable_get('recaptcha_private_key', ''),  
     '#maxlength' => 40,  
     '#description' => t('The private key given to you when you <a href="@url" target="_blank">registered at reCAPTCHA.net</a>.', array('@url' => url(recaptcha_get_signup_url($_SERVER['SERVER_NAME'], variable_get('site_name', ''))))),  
     '#required' => TRUE,  
   );  
   $form['recaptcha_secure_connection'] = array(  
     '#type' => 'checkbox',  
     '#title' => t('Secure Connection'),  
     '#default_value' => variable_get('recaptcha_secure_connection', FALSE),  
     '#description' => t('Connect to the reCAPTCHA server using a secure connection.'),  
   );  
   $form['recaptcha_theme_settings'] = array(  
     '#type' => 'fieldset',  
     '#title' => t('Theme Settings'),  
     '#collapsible' => TRUE,  
     '#collapsed' => TRUE,  
   );  
   $form['recaptcha_theme_settings']['recaptcha_theme'] = array(  
     '#type' => 'select',  
     '#title' => t('Theme'),  
     '#description' => t('Defines which theme to use for reCAPTCHA.'),  
     '#options' => array(  
       'red' => t('Red'),  
       'white' => t('White'),  
       'blackglass' => t('Black Glass'),  
       'clean' => t('Clean'),  
       'custom' => t('Custom'),  
     ),  
     '#default_value' => variable_get('recaptcha_theme', 'red'),  
     '#required' => TRUE,  
   );  
   $form['recaptcha_theme_settings']['recaptcha_tabindex'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Tab Index'),  
     '#description' => t('Sets a <a href="@tabindex" target="_blank">tabindex</a> for the reCAPTCHA text box. If other elements in the form use a tabindex, this should be set so that navigation is easier for the user.', array('@tabindex' => 'http://www.w3.org/TR/html4/interact/forms.html#adef-tabindex')),  
     '#default_value' => variable_get('recaptcha_tabindex', ''),  
     '#size' => 4,  
   );  
   
   return system_settings_form($form);  
 }  
   
 /**  
  * Validation function for recaptcha_admin_settings().  
  *  
  * @see recaptcha_admin_settings()  
  */  
 function recaptcha_admin_settings_validate($form, &$form_state) {  
   $tabindex = $form_state['values']['recaptcha_tabindex'];  
   if (!empty($tabindex) && !is_numeric($tabindex)) {  
     form_set_error('recaptcha_tabindex', t('The Tab Index must be an integer.'));  
   }  
 }  
   
 /**  
  * Load the recaptcha library.  
  */  
 function recaptcha_load_library() {  
   $path = drupal_get_path('module', 'recaptcha');  
   require_once($path . '/recaptcha.inc');  
   if (!include_once($path . '/recaptcha/recaptchalib.php')) {  
     _recaptcha_library_not_found();  
   }  
 }  
   
 /**  
65   * Implementation of hook_captcha().   * Implementation of hook_captcha().
66   */   */
67  function recaptcha_captcha() {  function recaptcha_captcha() {
# Line 156  function recaptcha_captcha() { Line 73  function recaptcha_captcha() {
73    
74      case 'generate':      case 'generate':
75        $captcha_type = array_shift($args);        $captcha_type = array_shift($args);
76        $result = array();        $captcha = array();
77        if ($captcha_type == 'reCAPTCHA') {        if ($captcha_type == 'reCAPTCHA') {
78          // Load the recaptcha library.          // Load the recaptcha library.
79          recaptcha_load_library();          _recaptcha_load_library();
   
         static $_recaptcha_jsadded = FALSE;  
80    
81          // Check if reCAPTCHA is available and show Math if not.          // Check if reCAPTCHA is available and show Math if not.
82          $connect = @fsockopen(RECAPTCHA_VERIFY_SERVER, 80);          $connect = @fsockopen(RECAPTCHA_VERIFY_SERVER, 80);
# Line 178  function recaptcha_captcha() { Line 93  function recaptcha_captcha() {
93          $recaptcha_form_value = NULL;          $recaptcha_form_value = NULL;
94    
95          // Construct the Javascript, but only display it once.          // Construct the Javascript, but only display it once.
96            static $_recaptcha_jsadded = FALSE;
97          if ($_recaptcha_jsadded == FALSE) {          if ($_recaptcha_jsadded == FALSE) {
98            $_recaptcha_jsadded = TRUE;            $_recaptcha_jsadded = TRUE;
99            $recaptcha_options = array(            $recaptcha_options = array(
# Line 206  function recaptcha_captcha() { Line 122  function recaptcha_captcha() {
122          }          }
123    
124          // Create the form. Captcha requires TRUE to be returned in solution.          // Create the form. Captcha requires TRUE to be returned in solution.
125          $result['solution'] = TRUE;          $captcha['solution'] = TRUE;
126            $captcha['captcha_validate'] = 'recaptcha_captcha_validation';
127          $html = recaptcha_get_html($recaptcha_public_key, NULL, $recaptcha_secure_connection);          $html = recaptcha_get_html($recaptcha_public_key, NULL, $recaptcha_secure_connection);
128          $result['form']['captcha_response'] = array(          $captcha['form'] = array(
129            '#type' => 'item',            '#type' => 'item',
           '#required' => TRUE,  
130            '#value' => ($recaptcha_form_value ? '<div id="recaptcha_custom_theme_widget">'. $recaptcha_form_value .'</div>' : '') . $html,            '#value' => ($recaptcha_form_value ? '<div id="recaptcha_custom_theme_widget">'. $recaptcha_form_value .'</div>' : '') . $html,
           // Make sure this 'item' form element is handled as an input element  
           // so that a '#process' callback can be attached.  
           '#input' => TRUE,  
           '#process' => array('recaptcha_process'),  
131          );          );
132        }        }
133        return $result;        return $captcha;
134      }
135    }
136    
137    /**
138     * CAPTCHA Callback; Validates the reCAPTCHA code.
139     */
140    function recaptcha_captcha_validation($solution = NULL, $response = NULL) {
141      if (isset($_POST['recaptcha_challenge_field']) && isset($_POST['recaptcha_response_field'])) {
142        $resp = recaptcha_check_answer(
143          variable_get('recaptcha_private_key', ''),
144          $_SERVER['REMOTE_ADDR'],
145          $_POST['recaptcha_challenge_field'],
146          $_POST['recaptcha_response_field']
147        );
148        return $resp->is_valid;
149    }    }
150      return FALSE;
151  }  }
152    
153  /**  /**
# Line 260  EOT; Line 188  EOT;
188  }  }
189    
190  /**  /**
191   * Process function: fetch the validation result from reCAPTCHA server and pass it to the Drupal CAPTCHA API.   * Load the recaptcha library.
192   */   */
193  function recaptcha_process($element, $edit, &$form_state, $complete_form) {  function _recaptcha_load_library() {
194    if (isset($_POST['recaptcha_challenge_field']) && isset($_POST['recaptcha_response_field'])) {    module_load_include('inc', 'recaptcha');
195      $resp = recaptcha_check_answer(    if (@!include_once(drupal_get_path('module', 'recaptcha') . '/recaptcha/recaptchalib.php')) {
196        variable_get('recaptcha_private_key', ''),      _recaptcha_library_not_found();
       $_SERVER['REMOTE_ADDR'],  
       $_POST['recaptcha_challenge_field'],  
       $_POST['recaptcha_response_field']  
     );  
     if ($resp->is_valid) {  
       $element['#value'] = TRUE;  
     }  
     else {  
       form_set_error('captcha_response', t('The reCAPTCHA code you entered was incorrect.'));  
       $element['#value'] = FALSE;  
     }  
197    }    }
   return $element;  
198  }  }

Legend:
Removed from v.1.13.2.4.2.11.2.9  
changed lines
  Added in v.1.13.2.4.2.11.2.10

  ViewVC Help
Powered by ViewVC 1.1.2