'. t("Mollom can be used to block all sorts of spam received by your website. Your Drupal site will send data you want checked for spam to the Mollom servers, which will reply with either 'spam' or 'ham' (not spam). If Mollom is not fully confident in its decision, it will ask the user to fill out a CAPTCHA. On the rare occasion that Mollom asks the poster to fill out a CAPTCHA, Mollom assumes that all legitimate posters will take the extra time to fill out this CAPTCHA. Using the CAPTCHA, Mollom avoids legitimate messages being incorrectly classified as spam and it eliminates the need to moderate messages that Mollom decided to block. Administrators can still inspect the logs to see what Mollom has blocked.", array('@logs' => url('admin/reports/dblog'))) .'
'. ''. t("To perform its service, Mollom processes, stores and compares the data submitted by your site's visitors as explained in our Web Service Privacy Policy. As the controller of the data being processed, it is your responsibility to inform your website's visitors, and to obtain appropriate consent from them to allow Mollom to process their data.") .'
'. ''. t("More information about how Mollom works, is available on the \"How Mollom works\" page and the Mollom FAQ.", array('@mollom-workings' => 'http://mollom.com/how-mollom-works', '@mollom-faq' => 'http://mollom.com/faq')) .'
', '#collapsible' => TRUE, ); $forms = _mollom_protectable_forms(); foreach ($forms as $formid => $details) { $name = 'mollom_'. $formid; $form['spam'][$name] = array( '#type' => 'checkbox', '#title' => t('Protect @name', array('@name' => $details['name'])), '#default_value' => variable_get($name, MOLLOM_MODE_DISABLED), ); } $form['server'] = array( '#type' => 'fieldset', '#title' => t('Server settings'), '#collapsible' => TRUE, '#collapsed' => $keys, ); $form['server']['mollom_fallback'] = array( '#type' => 'radios', '#title' => t('Fallback strategy'), '#default_value' => variable_get('mollom_fallback', MOLLOM_FALLBACK_BLOCK), // we default to treating everything as inappropriate '#options' => array( MOLLOM_FALLBACK_BLOCK => t('Block all submissions on the protected forms until the server problems are resolved'), MOLLOM_FALLBACK_ACCEPT => t('Leave all forms unprotected and accept all submissions') ), '#description' => t('In the very unlikely event that the Mollom servers are down or otherwise unreachable, no text analysis can be performed and no CAPTCHAs can be generated. At that point, your Drupal site will use the configured fallback strategy. You can choose to blindly accept all submissions without spam checking, or you can choose to block all submissions until the server or connection problems are resolved.'), ); } $form['access-keys'] = array( '#type'=> 'fieldset', '#title' => t('Mollom access keys'), '#description' => t('In order to use Mollom, you need a public and a private key. Visit http://mollom.com/user and create a user account to obtain a private and a public access key.'), '#collapsible' => TRUE, '#collapsed' => $keys, ); $form['access-keys']['mollom_public_key'] = array( '#type' => 'textfield', '#title' => t('Public key'), '#default_value' => variable_get('mollom_public_key', ''), '#description' => t('The public key is used to uniquely identify you.'), '#required' => TRUE ); $form['access-keys']['mollom_private_key'] = array( '#type' => 'textfield', '#title' => t('Private key'), '#default_value' => variable_get('mollom_private_key', ''), '#description' => t('The private key is used to prevent someone from hijacking your requests. It is like a password and should never be shared with anyone.'), '#required' => TRUE, ); return system_settings_form($form); } /** * A helper function that returns the OpenID identifiers associated with the specified user account. */ function _mollom_get_openid($account) { if (isset($account->uid)) { $result = db_query("SELECT * FROM {authmap} WHERE module = 'openid' AND uid = %d", $account->uid); $ids = array(); while ($identity = db_fetch_object($result)) { $ids[] = $identity->authname; } if (count($ids)) { return implode($ids, ' '); } } } function _mollom_fallback() { $spam = variable_get('mollom_fallback', MOLLOM_FALLBACK_BLOCK); if ($spam == MOLLOM_FALLBACK_BLOCK) { form_set_error('mollom', t("The spam filter that is installed on this site is currently not available. Per the site's policy, we are unable to accept new submissions until that problem is resolved. Please try resubmitting the form in a couple minutes.")); } if (xmlrpc_errno()) { watchdog('mollom', 'Problem with spam filter: @errno - %error_msg', array('@errno' => xmlrpc_errno(), '%error_msg' => xmlrpc_error_msg()), WATCHDOG_ERROR); } } /** * This the a form API validator function that will be called to perform text analysis on a form. */ function mollom_validate_analysis($form, &$form_state) { _mollom_debug("mollom_validate_analysis for '". $form_state['values']['form_id'] ."'"); $data = array(); $form_id = $form_state['values']['form_id']; $pos = strpos($form_id, '_node_form'); if ($pos !== FALSE) { // The node forms use dynamic form IDs so we had to create a special // case for these. $data = mollom_data_node_form($form_state['values']); } else { $function = 'mollom_data_'. $form_id; if (function_exists($function)) { $data = $function($form_state['values']); } } if (isset($form_state['values']['op'])) { $mollom = isset($form_state['values']['session_id']) ? array('session_id' => $form_state['values']['session_id']) : array(); $result = mollom('mollom.checkContent', $data + $mollom); if (isset($result['session_id']) && isset($result['spam'])) { _mollom_debug("mollom_validate_analysis retrieved spam status ". $result['spam'] ." and session ID '". $result['session_id'] ."'"); // Store the session ID that Mollom returned and make sure that it persists across page requests: _mollom_register_session_id($form_state, $result['session_id']); // Check the spam results and act accordingly: if ($result['spam'] == MOLLOM_ANALYSIS_HAM) { // Keep track of the response so we can use it later on to save the data in the database: $GLOBALS['mollom_response'] = $result; watchdog('mollom', 'Ham: %message', array('%message' => $data['post_body'])); } else if ($result['spam'] == MOLLOM_ANALYSIS_SPAM) { form_set_error('mollom', t('Your submission has triggered the installed spam filter and will not be accepted.')); watchdog('mollom', 'Spam: %message', array('%message' => $data['post_body'])); } else { form_set_error('mollom', t('We are sorry, but the spam filter on this site decided that your submission could be spam. Please fill in the CAPTCHA first.')); watchdog('mollom', 'Unsure: %message', array('%message' => $data['post_body'])); _mollom_register_captcha($form_state); } } else { _mollom_fallback(); } } } /** * This the a form API validator function that will be called to check a CAPTCHA on a form. */ function mollom_validate_captcha(&$form, &$form_state) { _mollom_debug("mollom_validate_captcha for '". $form_state['values']['form_id'] ."'"); if (isset($form_state['values']['session_id'])) { if (isset($form_state['values']['captcha'])) { // Check the CAPTCHA result: $result = mollom('mollom.checkCaptcha', array('session_id' => $form_state['values']['session_id'], 'captcha_result' => $form_state['values']['captcha'], 'author_ip' => ip_address())); _mollom_debug("mollom_validate_captcha the captcha result was ". (int)$result); if ($result == FALSE) { watchdog('mollom', 'Incorrect CAPTCHA'); form_set_error('captcha', t('The entered CAPTCHA solution is not correct. We generated a new CAPTCHA so please try again.')); _mollom_register_captcha($form_state); } else { // Keep track of the response so we can use it later on to save the data in the database: $GLOBALS['mollom_response']['session_id'] = $form_state['values']['session_id']; // Pass FALSE so we actually remove the CAPTCHA from the form: _mollom_register_captcha($form_state, FALSE); // TODO: this break the 'request password form' -- it shouldn't rebuild the form. Removing this breaks the comment form. watchdog('mollom', 'Correct CAPTCHA'); } } else { form_set_error('captcha', t('The CAPTCHA field is required.')); _mollom_register_captcha($form_state); } } else { // Mark the form so that we generate a new CAPTCHA. _mollom_register_captcha($form_state); } } /** * This is a helper function to insert the CAPTCHA into the form. We restore * the context at the right time (which happens to be when hook _form_process * is called). */ function mollom_form_process($form, $edit, &$form_state, $complete_form) { if (isset($form_state['mollom'])) { if (isset($form_state['mollom']['post'])) { _mollom_debug("_mollom_form_process found CAPTCHA request"); $form['#post'] = $form_state['mollom']['post']; } } return $form; } /** * This is a help function that registers a Mollom session ID so that * on the next page, we can insert the session ID in the form. */ // TODO: is this function required? function _mollom_register_session_id(&$form_state, $session_id) { _mollom_debug("_mollom_register_session_id with session ID '$session_id'"); $form_state['values']['session_id'] = $session_id; // Required to make things flow ... } /** * This function inserts a Mollom session ID into the form. It is * called during the construction of the form, just before the form * is rendered. */ function _mollom_insert_session_id(&$form, $form_state) { // The function '_mollom_insert_session_id' can be called as the // result of a POST operation or as the result of an internal form // API operation. Depending on the caller, the session ID can be // found in a different location. // TODO: is this something we can make more consistent in Drupal 7? if (isset($form_state['values']) && isset($form_state['values']['session_id'])) { _mollom_debug("_mollom_insert_session_id with session ID '". $form_state['values']['session_id'] ."'"); $form['session_id'] = array( '#type' => 'hidden', '#value' => $form_state['values']['session_id'], ); } if (isset($form_state['post']) && isset($form_state['post']['session_id'])) { _mollom_debug("_mollom_insert_session_id with session ID '". $form_state['post']['session_id'] ."'"); $form['session_id'] = array( '#type' => 'hidden', '#value' => $form_state['post']['session_id'], ); } } /** * This is a helper function that registers the form data so that on * the next page, we can insert the CAPTCHA on the form. We store * the form's context in $form_state['mollom'] and ask the form to * rebuild itself. The rebuilding code will take the saved context * and use it in the rebuild form: see mollom_form_process(). This * is a bit of a hack but the form API code is a little braindead * when it comes to dynamic forms. Let's try to make this better in * Drupal 7 and beyond. */ function _mollom_register_captcha(&$form_state, $captcha = TRUE) { $form_id = $form_state['values']['form_id']; _mollom_debug("_mollom_register_captcha for $form_id with captcha ". (int)$captcha); $form_state['mollom']['post'] = $_POST; // This is quite a hack ... $form_state['mollom']['captcha'] = $captcha; // Whether to add or remove the CAPTCHA ... $mode = _mollom_get_mode($form_id); if ($mode == MOLLOM_MODE_ANALYSIS) { $form_state['rebuild'] = TRUE; } } /** * This function inserts a CAPTCHA into the form. It is called * during the construction of the form, just before the form * is rendered. */ function _mollom_insert_captcha(&$form, $form_state) { _mollom_debug("_mollom_insert_captcha"); // Prepare the author's IP: $data['author_ip'] = ip_address(); // The function '_mollom_insert_captcha' can be called as the result // of a POST operation or as the result of an internal form API // operation. Depending on the caller, the session ID can be found // in a different location. // TODO: is this something we can make more consistent in Drupal 7? if (isset($form_state['values']) && isset($form_state['values']['session_id'])) { $data['session_id'] = $form_state['values']['session_id']; } if (isset($form_state['post']) && isset($form_state['post']['session_id'])) { $data['session_id'] = $form_state['post']['session_id']; } // Request a CAPTCHA -- we always default to an image CAPTCHA: $response = mollom('mollom.getImageCaptcha', $data); if (isset($response['session_id']) && isset($response['url'])) { _mollom_debug("_mollom_insert_captcha retrieved URL '". $response['url'] ."' and session ID '". $response['session_id'] ."'"); // Include the JavaScript that allows the user to switch to an // AUDIO captcha instead: drupal_add_js(drupal_get_path('module', 'mollom') .'/mollom.js'); // Compute the weight of the CAPTCHA so we can position it in the form. $weight = 99999; foreach (element_children($form) as $key) { // Iterate over the form elements looking for buttons: if (isset($form[$key]['#type']) && ($form[$key]['#type'] == 'submit' || $form[$key]['#type'] == 'button')) { // For each button, slightly increase their weight to allocate // room for the CAPTCHA: if (isset($form[$key]['#weight'])) { $form[$key]['#weight'] = $form[$key]['#weight'] + 0.0002; } else { $form[$key]['#weight'] = 1.0002; } // We want to position the CAPTCHA just before the first button so // we make the CAPTCHA's weight slightly lighter than the lightest // button's weight. $weight = min($weight, $form[$key]['#weight'] - 0.0001); } } // Add the session ID to the form: $form['session_id'] = array( '#type' => 'hidden', '#name' => 'session_id', '#id' => 'edit-session-id', '#value' => $response['session_id'], ); // Add the CAPTCHA to the form: $form['captcha'] = array( '#type' => 'textfield', '#name' => 'captcha', '#id' => 'edit-captcha', '#title' => t('CAPTCHA'), '#prefix' => '', '#required' => TRUE, '#size' => 10, '#description' => t("Type in the characters shown in the above; if you can't read them, submit the form and a new image will be generated."), '#weight' => $weight); // Sort the form before rendering: unset($form['#sorted']); } } /** * This function will contact Mollom to verify the configured key pair. */ function _mollom_verify_key() { $status = mollom('mollom.verifyKey'); $message = t('We contacted the Mollom servers to verify your keys'); if ($status) { drupal_set_message(t('@message: the Mollom services are operating correctly. We are now blocking spam.', array('@message' => $message))); } else { drupal_set_message(t('@message: your keys do not exist or are no longer valid. Please visit the user settings page on the Mollom website again: @mollom-user.', array('@message' => $message, '@mollom-user' => 'http://mollom.com/user')), 'error'); } } /** * Call a remote procedure at the Mollom server. This function will * automatically add the information required to authenticate against * Mollom. */ function mollom($method, $data = array()) { // Construct the server URL: $public_key = variable_get('mollom_public_key', ''); // Retrieve the list of Mollom servers from the database: $servers = variable_get('mollom_servers', NULL); if ($servers == NULL) { // Retrieve a list of valid Mollom servers from mollom.com: $servers = xmlrpc('http://xmlrpc.mollom.com/'. MOLLOM_API_VERSION, 'mollom.getServerList', _mollom_authentication()); // Store the list of servers in the database: variable_set('mollom_servers', $servers); // Log this for debuging purposes:: watchdog('mollom', 'The list of available Mollom servers was set to @servers.', array('@servers' => print_r($servers, TRUE))); } if (is_array($servers)) { // Send the request to the first server, if that fails, try the other servers in the list: foreach ($servers as $server) { $result = xmlrpc($server .'/'. MOLLOM_API_VERSION, $method, $data + _mollom_authentication()); // Debug output: if (isset($data['session_id'])) { _mollom_debug("called $method at server $server with session ID '". $data['session_id'] ."'"); } else { _mollom_debug("called $method at server $server with no session ID"); } if ($errno = xmlrpc_errno()) { watchdog('mollom', 'Error @errno: %server - %message - %method -@data', array('@errno' => xmlrpc_errno(), '%server' => $server, '%message' => xmlrpc_error_msg(), '%method' => $method, '@data' => print_r($data, TRUE)), WATCHDOG_ERROR); if ($errno == MOLLOM_REFRESH) { // Retrieve a list of valid Mollom servers from mollom.com: $servers = xmlrpc('http://xmlrpc.mollom.com/'. MOLLOM_API_VERSION, 'mollom.getServerList', _mollom_authentication()); // Store the updated list of servers in the database: variable_set('mollom_servers', $servers); // Log this for debuging purposes:: watchdog('mollom', 'The list of available Mollom servers was set to @servers.', array('@servers' => print_r($servers, TRUE))); } else if ($errno == MOLLOM_ERROR) { return $result; } else if ($errno == MOLLOM_REDIRECT) { // Do nothing, we select the next client automatically. } // Reset the XMLRPC error: xmlrpc_error(0); // FIXME: this is crazy. } else { return $result; } } } // If none of the servers worked, activate the fallback mechanism: _mollom_fallback(); // If everything failed, we reset the server list to force Mollom to request a new list: variable_set('mollom_servers', array()); // Report this error: watchdog('mollom', 'No Mollom servers could be reached or all servers returned an error -- the server list was emptied.', WATCHDOG_ERROR); } /** * This function generate an array with all the information required to * authenticate against Mollom. To prevent that requests are forged and * that you are impersonated, each request is signed with a hash computed * based on a private key and a timestamp. * * Both the client and the server share the secret key that is used to * create the authentication hash based on a timestamp. They both hash * the timestamp with the secret key, and if the hashes match, the * authenticity of the message has been validated. * * To avoid that someone can intercept a (hash, timestamp)-pair and * use that to impersonate a client, Mollom will reject the request * when the timestamp is more than 15 minutes off. * * Make sure your server's time is synchronized with the world clocks, * and that you don't share your private key with anyone else. */ function _mollom_authentication() { $public_key = variable_get('mollom_public_key', ''); $private_key = variable_get('mollom_private_key', ''); // Generate a timestamp according to the dateTime format (http://www.w3.org/TR/xmlschema-2/#dateTime): $time = gmdate("Y-m-d\TH:i:s.\\0\\0\\0O", time()); // Generate a random number: $nonce = md5(mt_rand()); // Calculate a HMAC-SHA1 according to RFC2104 (http://www.ietf.org/rfc/rfc2104.txt): $hash = base64_encode( pack("H*", sha1((str_pad($private_key, 64, chr(0x00)) ^ (str_repeat(chr(0x5c), 64))) . pack("H*", sha1((str_pad($private_key, 64, chr(0x00)) ^ (str_repeat(chr(0x36), 64))) . $time . ':' . $nonce . ':' . $private_key)))) ); // Store everything in an array. Elsewhere in the code, we'll add the // actual data before we pass it onto the XML-RPC library: $data['public_key'] = $public_key; $data['time'] = $time; $data['hash'] = $hash; $data['nonce'] = $nonce; return $data; } /* * This is a helper function for developers to help debug the form API workflow in this module. * Uncomment the function body to activate. */ function _mollom_debug($message) { // print $message .'