Mollom website or in the Mollom FAQ. For support, please consult the Mollom support page.", array('@mollom-website' => 'http://mollom.com', '@mollom-faq' => 'http://mollom.com/faq', '@mollom-support' => 'http://mollom.com/support')); } } function mollom_link($type, $object = NULL) { // We extend the comment links so people can send feedback: if ($type == 'comment' && user_access('administer comments') && _mollom_get_mode('comment_form')) { $links['comment_delete'] = array( 'title' => t('delete'), 'href' => "mollom/comment/$object->cid" ); return $links; } if ($type == 'node' && user_access('administer nodes') && _mollom_get_mode($object->type .'_node_form')) { $links['node_spam'] = array( 'title' => t('delete'), 'href' => "mollom/node/$object->nid" ); return $links; } } function mollom_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array( 'path' => 'mollom/comment', 'title' => t('Report and delete'), 'callback' => 'drupal_get_form', 'callback arguments' => array('mollom_report_comment'), 'access' => user_access('administer comments'), 'type' => MENU_CALLBACK); $items[] = array( 'path' => 'mollom/node', 'title' => t('Report and delete'), 'callback' => 'drupal_get_form', 'callback arguments' => array('mollom_report_node'), 'access' => user_access('administer nodes'), 'type' => MENU_CALLBACK); $items[] = array( 'path' => 'mollom/contact', 'title' => t('Report and delete'), 'callback' => 'drupal_get_form', 'callback arguments' => array('mollom_report_contact'), 'access' => TRUE, // Everyone can report contact form feedback 'type' => MENU_CALLBACK); $items[] = array( 'path' => 'admin/settings/mollom', 'description' => t('Mollom is a webservice that helps you manage your community.'), 'title' => t('Mollom'), 'callback' => 'drupal_get_form', 'callback arguments' => array('mollom_admin_settings'), 'access' => user_access('administer site configuration'), 'type' => MENU_NORMAL_ITEM ); // A menu callback that is used for AJAX purposes: $items[] = array( 'path' => 'mollom/captcha', 'title' => t('Request CAPTCHA'), 'callback' => 'mollom_ajax_callback', 'access' => TRUE, 'type' => MENU_CALLBACK); } return $items; } /** * Implementation of hook_perm(). */ function mollom_perm() { return array('post with no checking'); } /** * An AJAX callback to retrieve a CAPTCHA. */ function mollom_ajax_callback($type, $session_id) { // TODO: add error handling if ($type == 'audio') { $response = mollom('mollom.getAudioCaptcha', array('author_ip' => _mollom_ip_address(), 'session_id' => $session_id)); if ($response) { $output = ''; $output .= ' '; $output .= ' '; $output .= ''; $output .= ' ('. t('use image CAPTCHA') .')'; } } if ($type == 'image') { $response = mollom('mollom.getImageCaptcha', array('author_ip' => _mollom_ip_address(), 'session_id' => $session_id)); if ($response) { $output = 'Mollom CAPTCHA'; $output .= ' ('. t('play audio CAPTCHA') .')'; } } print $output; exit(); } /** * Helper function used to load a Mollom session ID. */ function mollom_get_data($did) { return db_fetch_object(db_query("SELECT * FROM {mollom} WHERE did = '%s'", $did)); } /** * Helper function used to store a Mollom session ID. */ function mollom_set_data($session, $quality, $did) { if (db_result(db_query("SELECT session FROM {mollom} WHERE did = '%s'", $did))) { db_query("UPDATE {mollom} SET session = '%s', quality = '%s' WHERE did = '%s'", $session, $quality, $did); } else { db_query("INSERT INTO {mollom} (session, quality, did) VALUES ('%s', '%s', '%s')", $session, $quality, $did); } } /** * Return a list of the possible feedback options for content. */ function _mollom_feedback_options() { return array( '#type' => 'radios', '#title' => t('Optionally report this to Mollom'), '#required' => TRUE, '#options' => array( 'none' => t("Don't send feedback to Mollom"), 'spam' => t('Report as spam or unsolicited advertising'), 'profanity' => t('Report as obscene, violent or profane content'), 'low-quality' => t('Report as low-quality content or writing'), 'unwanted' => t('Report as unwanted, taunting or off-topic content'), ), '#default_value' => 'none', '#description' => t("Mollom is a webservice that helps you moderate your site's content: see http://mollom.com for more information. By sending feedback to Mollom, you teach Mollom what content you like and what content you dislike. From that data, Mollom can do a better job."), ); } /** * This function is used to report a comment as feedback and to delete it. */ function mollom_report_comment($cid) { if ($comment = _comment_load($cid)) { $form['cid'] = array('#type' => 'value', '#value' => $cid); $form['feedback'] = _mollom_feedback_options(); return confirm_form($form, t('Are you sure you want to delete the comment and report it?'), $_GET['destination'] ? $_GET['destination'] : 'node/'. $comment->nid, t('This action cannot be undone.'), t('Delete'), t('Cancel')); } } /** * This function is used to delete a comment and to optionally send feedback to Mollom. */ function mollom_report_comment_submit($form_id, $form_values) { if ($form_values['confirm']) { if ($comment = _comment_load($form_values['cid'])) { // Load the Mollom session data: $data = mollom_get_data('comment-'. $comment->cid); // Provide feedback to Mollom if available: if ($data->session && $form_values['feedback']) { mollom('mollom.sendFeedback', array('session_id' => $data->session, 'feedback' => $form_values['feedback'])); } // Delete comment and its replies. _comment_delete_thread($comment); _comment_update_node_statistics($comment->nid); cache_clear_all(); drupal_set_message(t('The comment has been deleted.')); } } return "node/$comment->nid"; } /** * This function is used to delete a node and to optionally send feedback to Mollom. */ function mollom_report_node($nid) { if ($node = node_load($nid)) { $form['nid'] = array('#type' => 'value', '#value' => $node->nid); $form['feedback'] = _mollom_feedback_options(); return confirm_form($form, t('Are you sure you want to delete %title and report it?', array('%title' => $node->title)), $_GET['destination'] ? $_GET['destination'] : 'node/'. $node->nid, t('This action cannot be undone.'), t('Delete'), t('Cancel')); } } /** * This function is used to delete a node and to optionally send feedback to Mollom. */ function mollom_report_node_submit($form_id, $form_values) { if ($form_values['confirm']) { if ($node = node_load($form_values['nid'])) { // Load the Mollom session data: $data = mollom_get_data('node-'. $node->nid); // Provide feedback to Mollom if available: if ($data->session && $form_values['feedback']) { mollom('mollom.sendFeedback', array('session_id' => $data->session, 'feedback' => $form_values['feedback'])); } // Delete the node. Calling this function will delete any comments, // clear the cache and print a status message. node_delete($node->nid); } } return ''; } /** * This function adds a 'report as inappropriate' link to the e-mails that are sent * by the contact module. */ function mollom_mail_alter($key, $to, $subject, &$body, $from, $headers) { $response = $GLOBALS['mollom_response']; if ($key == 'contact-page-mail' && isset($response) && isset($response['session_id'])) { $body .= "\n\n". t('Report as inappropriate: @link', array('@link' => url('mollom/contact/'. $response['session_id'], NULL, NULL, TRUE))); } } /** * This function is used to report a contact form message as inappropriate. */ function mollom_report_contact($session) { $form['session'] = array('#type' => 'value', '#value' => $session); $form['feedback'] = _mollom_feedback_options(); return confirm_form($form, t('Are you sure you want to report the e-mail as inappropriate?'), $_GET['destination'] ? $_GET['destination'] : '', t('This action cannot be undone.'), t('Report as inappropriate'), t('Cancel')); } /** * This function is used to report a contact form message as inappropriate. */ function mollom_report_contact_submit($form_id, $form_values) { if ($form_values['feedback']) { mollom('mollom.sendFeedback', array('session_id' => $form_values['session'], 'feedback' => $form_values['feedback'])); drupal_set_message('The e-mail has been reported as inappropriate.'); } drupal_goto(); } /** * This function is called when a node is inserted. */ function mollom_nodeapi($node, $op) { if ($op == 'insert' && isset($GLOBALS['mollom_response']) && isset($GLOBALS['mollom_response']['session_id'])) { mollom_set_data($GLOBALS['mollom_response']['session_id'], $GLOBALS['mollom_response']['quality'], 'node-'. $node->nid); } } /** * This function is called when a comment is inserted. */ function mollom_comment($comment, $op) { if ($op == 'insert' && isset($GLOBALS['mollom_response']) && isset($GLOBALS['mollom_response']['session_id'])) { mollom_set_data($GLOBALS['mollom_response']['session_id'], $GLOBALS['mollom_response']['quality'], 'comment-'. $comment['cid']); } } /** * This function intercepts all forms in Drupal and Mollom enables them if * necessary. */ function mollom_form_alter($form_id, &$form) { // Catch all handlers -- this makes it easy to protect all forms // with Mollom. Site administrators don't have their content // checked with Mollom. if (!user_access('post with no checking')) { $form['#pre_render'] = array('mollom_captcha_prerender'); $form['#validate']['mollom_validate'] = array(); } // Hook into the mass comment administration page and add some // operations to communicate ham/spam to the XML-RPC server. if ($form_id == 'comment_admin_overview') { $form['options']['operation']['#options']['mollom-unpublish'] = t('Report the selected comments as spam and unpublish them'); $form['options']['operation']['#options']['mollom-delete'] = t('Report the selected comments as spam and delete them'); $form['#validate']['mollom_comment_admin_overview_submit'] = array(); } // Hook into the mass comment administration page and add some // operations to communicate ham/spam to the XML-RPC server. if ($form_id == 'node_admin_nodes') { $form['options']['operation']['#options']['mollom-unpublish'] = t('Report as spam and unpublish'); $form['options']['operation']['#options']['mollom-delete'] = t('Report as spam and delete'); $form['#validate']['mollom_node_admin_overview_submit'] = array(); } } function mollom_comment_admin_overview_submit($form_id, $form_values) { // The operation has the following format: mollom-, // where '' can be 'unpublish' or 'delete'. list($id, $operation) = explode('-', $form_values['operation']); if ($id == 'mollom') { foreach ($form_values['comments'] as $cid => $value) { if ($value) { // First, send the proper information to the XML-RPC server: if ($data = mollom_get_data('comment-'. $cid)) { mollom('mollom.sendFeedback', array('session_id' => $data->session, 'feedback' => 'spam')); } // Second, perform the proper operation on the comments: if ($comment = _comment_load($cid)) { if ($operation == 'unpublish') { db_query("UPDATE {comments} SET status = %d WHERE cid = %d", COMMENT_NOT_PUBLISHED, $cid); _comment_update_node_statistics($comment->nid); } else if ($operation == 'delete') { _comment_delete_thread($comment); _comment_update_node_statistics($comment->nid); } } } } // Clear the cache: cache_clear_all(); if ($operation == 'delete') { drupal_set_message(t('The selected comments have been reported as spam and are deleted.')); } else { drupal_set_message(t('The selected comments have been reported as spam and are unpublished.')); } } } function mollom_node_admin_overview_submit($form_id, $form_values) { // The operation has the following format: mollom-, // where '' can be 'unpublish' or 'delete'. list($id, $operation) = explode('-', $form_values['operation']); if ($id == 'mollom') { foreach ($form_values['nodes'] as $nid => $value) { if ($value) { if ($data = mollom_get_data('node-'. $nid)) { mollom('mollom.sendFeedback', array('session_id' => $data->session, 'feedback' => 'spam')); } if ($node = node_load($nid)) { if ($operation == 'unpublish') { db_query("UPDATE {node} SET status = 0 WHERE nid = %d", $nid); } else if ($operation == 'delete') { node_delete($nid); } } } } // Clear the cache: cache_clear_all(); if ($operation == 'delete') { drupal_set_message(t('The selected posts have been reported as spam and are deleted.')); } else { drupal_set_message(t('The selected posts have been reported as spam and are unpublished.')); } } } /** * This function will be called by mollom_validate to prepare the * XML-RPC data from the comment submission form's $form_values ... */ function mollom_data_contact_mail_page($form_values) { $data = array( 'post_title' => $form_values['subject'], 'post_body' => $form_values['message'], 'author_name' => $form_values['name'] ? $form_values['name'] : $user->name, 'author_mail' => $form_values['mail'] ? $form_values['mail'] : $user->mail, 'author_ip' => _mollom_ip_address(), ); return $data; } /** * This function will be called by mollom_validate to prepare the * XML-RPC data from the comment submission form's $form_values ... */ function mollom_data_contact_mail_user($form_values) { global $base_url, $user; $data = array( 'post_title' => $form_values['subject'], 'post_body' => $form_values['message'], 'author_name' => $form_values['name'] ? $form_values['name'] : $user->name, 'author_mail' => $form_values['mail'] ? $form_values['mail'] : $user->mail, 'author_ip' => _mollom_ip_address(), ); return $data; } /** * This function will be called by mollom_validate to prepare the * XML-RPC data from the comment submission form's $form_values ... */ function mollom_data_comment_form($form_values) { global $user; $data = array( 'post_title' => $form_values['subject'], 'post_body' => $form_values['comment'], 'author_name' => $form_values['name'] ? $form_values['name'] : $user->name, 'author_mail' => $form_values['mail'] ? $form_values['mail'] : $user->mail, 'author_url' => $form_values['homepage'], 'author_ip' => $form_values['cid'] ? '' : _mollom_ip_address(), ); return $data; } /** * This function will be called by mollom_validate to prepare the * XML-RPC data from the comment submission form's $form_values ... */ function mollom_data_node_form($form_values) { global $user; // Render the node so that all visible fields are prepared and // concatenated: $data = node_build_content((object)$form_values, FALSE, FALSE); $content = drupal_render($data->content); $data = array( 'post_title' => $form_values['title'], 'post_body' => $content, 'author_name' => $form_values['name'] ? $form_values['name'] : $user->name, 'author_mail' => $form_values['mail'] ? $form_values['mail'] : $user->mail, 'author_url' => $form_values['homepage'], 'author_ip' => $form_values['nid'] ? '' : _mollom_ip_address(), ); return $data; } /** * This function is a generic validate function that will protect any given * form as long there is a variable for it (i.e. as long it is configured to * be protected through Mollom). To protect a form in Drupal with Mollom, * just variable_set() the "mollom_$form_id" variable. */ function mollom_validate($form_id, $form_values) { $data = array(); $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_values); } else { $function = 'mollom_data_'. $form_id; if (function_exists($function)) { $data = $function($form_values); } } // Retreive the mode of protection that is required for this form: $mode = _mollom_get_mode($form_id); if ($mode == MOLLOM_MODE_ANALYSIS) { mollom_protect_form_analysis($form_values, $data); } else if ($mode == MOLLOM_MODE_CAPTCHA) { mollom_protect_form_captcha($form_values, $data); } } /** * Given a form ID, this function will return the strategy that is used * to protect this form. Could be MOLLOM_MODE_DISABLED (none), * MOLLOM_MODE_CAPTCHA (CAPTCHAs only) or MOLLOM_MODE_ANALYSIS (text * analysis with smart CAPTCHA support). */ function _mollom_get_mode($form_id) { if (variable_get('mollom_'. $form_id, MOLLOM_MODE_DISABLED)) { $forms = mollom_protectable_forms(); return $forms[$form_id]['mode']; } return MOLLOM_MODE_DISABLED; } /** * This function lists all the forms that you can protect with Mollom. * If you want to protect additional forms with Mollom add the form ID * to this list. */ function mollom_protectable_forms() { static $forms = NULL; if (!$forms) { if (module_exists('comment')) { $forms['comment_form'] = array( 'name' => 'comment form', 'mode' => MOLLOM_MODE_ANALYSIS); } if (module_exists('contact')) { $forms['contact_mail_page'] = array( 'name' => 'site-wide contact form', 'mode' => MOLLOM_MODE_ANALYSIS); $forms['contact_mail_user'] = array( 'name' => 'per-user contact forms', 'mode' => MOLLOM_MODE_ANALYSIS); } $forms['user_register'] = array( 'name' => 'user registration form', 'mode' => MOLLOM_MODE_CAPTCHA); $forms['user_pass'] = array( 'name' => 'user password request form', 'mode' => MOLLOM_MODE_CAPTCHA); // Add all the node types: $types = node_get_types('names'); foreach ($types as $type => $name) { $forms[$type .'_node_form'] = array( 'name' => strtolower($name) ." form", 'mode' => MOLLOM_MODE_ANALYSIS); } } return $forms; } // Temporary test code function _mollom_update_comments() { $result = db_query('SELECT * FROM comments LIMIT 1000'); while ($comment = db_fetch_object($result)) { $data = array( 'post_title' => $comment->subject, 'post_body' => $comment->comment, ); $response = mollom('mollom.checkContent', $data); if ($response['spam'] == MOLLOM_ANALYSIS_SPAM) { print "$comment->subject
$comment->comment
"; } else { print $response['spam']; } //mollom_set_data($response['session_id'], $response['quality'], 'comment-'. $comment->cid); } } function mollom_admin_settings() { // _mollom_update_comments(); $keys = variable_get('mollom_public_key', '') && variable_get('mollom_private_key', ''); if ($keys) { // Print a status message about the key: if (!$_POST) { // When a user visits the Mollom adminsitration page, we automatically // clear the server list. This will cause the client to fetch a fresh // server list from the server. variable_del('mollom_servers'); // Verify the key: _mollom_verify_key(); } $form['statistics'] = array( '#type' => 'fieldset', '#title' => t('Site usage statistics'), '#collapsible' => TRUE, ); $form['statistics']['message'] = array( '#value' => '
' ); $form['spam'] = array( '#type'=> 'fieldset', '#title' => t('Spam protection settings'), '#description' => '

'. 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 occassion 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_STATUS_BLOCK), // we default to treating everything as inappropriate '#options' => array( MOLLOM_STATUS_BLOCK => t('Block all submissions on the protected forms until the server problems are resolved'), MOLLOM_STATUS_ACCEPT => t('Leave all forms unprotected and accept all submissions') ), '#description' => t('In the event that Mollom stopped servicing your requests, i.e. because a certain usage quota was reached, 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 this problem is 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); } 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 currenly 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', t('Problem with spam filter: @errno - %error_msg', array('@errno' => xmlrpc_errno(), '%error_msg' => xmlrpc_error_msg())), WATCHDOG_ERROR); } } function mollom_protect_form_analysis(&$form, $data) { if ($form['op']) { $mollom = $_POST['session-id'] ? array('session_id' => $_POST['session-id']) : array(); $result = mollom('mollom.checkContent', $data + $mollom); if (isset($result['spam'])) { // We make the response available to other form API handlers: $GLOBALS['mollom_response'] = $result; if ($result['spam'] == MOLLOM_ANALYSIS_HAM) { watchdog('mollom', t('Ham: %message', array('%message' => $data['post_body']))); } else if ($result['spam'] == MOLLOM_ANALYSIS_SPAM) { form_set_error('analysis', t('Your submission has triggered the installed spam filter and will not be accepted.')); watchdog('mollom', t('Spam: %message', array('%message' => $data['post_body']))); } else { if ($_POST['captcha-solution']) { // Check the CAPTCHA result: $result = mollom('mollom.checkCaptcha', $data + array('session_id' => $_POST['session-id'], 'solution' => $_POST['captcha-solution'], 'author_ip' => _mollom_ip_address())); if (is_bool($result)) { if ($result) { $GLOBALS['mollom_response']['spam'] = MOLLOM_ANALYSIS_HAM; watchdog('mollom', t('Correct CAPTCHA: %message', array('%message' => $data['post_body']))); } else { form_set_error('captcha', t('The entered CAPTCHA solution is not correct. We generated a new CAPTCHA so please try again.')); watchdog('mollom', t('Incorrect CAPTCHA: %message', array('%message' => $data['post_body']))); } } else { _mollom_fallback(); } } else { form_set_error('analysis', t('We are sorry, but the spam filter on this site decided that your submission could be spam. Please fill in the CAPTCHA below to get your submission accepted.')); watchdog('mollom', t('Unsure: %message', array('%message' => $data['post_body']))); } } } else { _mollom_fallback(); } } } function mollom_protect_form_captcha(&$form, $data) { if ($_POST['session-id']) { if ($_POST['captcha-solution']) { // Check the CAPTCHA result: $result = mollom('mollom.checkCaptcha', $data + array('session_id' => $_POST['session-id'], 'captcha_result' => $_POST['captcha-solution'], 'author_ip' => _mollom_ip_address())); if (is_bool($result)) { // We make the response available to other form API handlers: $GLOBALS['mollom_response']['session_id'] = $_POST['session-id']; if (!$result) { form_set_error('captcha', t('The entered CAPTCHA solution is not correct. We generated a new CAPTCHA so please try again.')); // Generate a new CAPTCHA: $GLOBALS['mollom_captcha'] = mollom('mollom.getImageCaptcha', array('author_ip' => _mollom_ip_address())); } } else { _mollom_fallback(); } } else { form_set_error('captcha', t('The CAPTCHA field is required.')); // Generate a new CAPTCHA: $GLOBALS['mollom_captcha'] = mollom('mollom.getImageCaptcha', array('author_ip' => _mollom_ip_address())); } } else { // Generate a new CAPTCHA: $GLOBALS['mollom_captcha'] = mollom('mollom.getImageCaptcha', array('author_ip' => _mollom_ip_address())); } } function mollom_captcha_prerender($form_id, &$form) { $response = $GLOBALS['mollom_response']; $mode = _mollom_get_mode($form_id); if ($mode == MOLLOM_MODE_ANALYSIS && $response['spam'] == MOLLOM_ANALYSIS_UNSURE) { $response = mollom('mollom.getImageCaptcha', array('author_ip' => _mollom_ip_address(), 'session_id' => $response['session_id'])); $captcha = TRUE; } else if ($mode == MOLLOM_MODE_CAPTCHA) { $response = mollom('mollom.getImageCaptcha', array('author_ip' => _mollom_ip_address())); $captcha = TRUE; } if (isset($response['session_id'])) { $form['session-id'] = array( '#type' => 'hidden', '#name' => 'session-id', '#id' => 'edit-session-id', '#value' => $response['session_id'], ); if ($captcha) { // Include the Javascript that allows the user to switch to an // AUDIO captcha instead: $url = base_path() . (variable_get('clean_url', 0) ? '' : 'index.php?q='); drupal_add_js(array('mollom_base_url' => $url), 'setting'); drupal_add_js(drupal_get_path('module', 'mollom') .'/mollom.js'); // We add an image captcha to the form. We can't do this from the // validate hook, hence the need for the #pre_render trick. In // Drupal 6, we can simply use $form_state['rebuild'] = TRUE in // the validate-hook, and rebuild the form from there but in // Drupal 5, we have to use this prerender-hook hack. // Because we're already past the prepare state of the form API, // we have a little bit more work to do ...: $form['captcha-solution'] = array( '#type' => 'textfield', '#name' => 'captcha-solution', '#id' => 'edit-captcha-solution', '#parents' => array(), '#title' => t('CAPTCHA'), '#prefix' => '', '#required' => TRUE, '#size' => 10, '#value' => $form['#post']['captcha'], '#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' => min($form['submit']['#weight'], $form['preview']['#weight']) + 1); // Move the preview and/or submit button below the captcha: $form['preview']['#weight'] += 2; $form['submit']['#weight'] += 2; // The weights changed so we re-sort the array: uasort($form, "_element_sort"); } } } function _mollom_verify_key() { $status = mollom('mollom.verifyKey'); $message = 'We contacted the Mollom servers to verify your keys'; if (xmlrpc_errno()) { drupal_set_message(t("$message: %error (ERROR)", array('%error' => xmlrpc_error_msg())), 'error'); } else { if ($status) { drupal_set_message(t("$message: the Mollom services are operating correctly. We are now blocking spam.")); } 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: http://mollom.com/user."), 'error'); } } } // FIXME: this function can be removed in Drupal 6. function _mollom_ip_address() { $ip_address = $_SERVER['REMOTE_ADDR']; if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) { // If there are several arguments, we need to check the most // recently added one, ie the last one. $ip_address = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])); } return $ip_address; } /** * 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); } 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()); if ($errno = xmlrpc_errno()) { watchdog('mollom', t('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); } 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 everything failed, we reset the server list to force Mollom to request a new list: variable_set('mollom_servers', array()); } /** * 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()); // 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)))) ); // Store everything in an array. Elsewhere in the code, we'll add the // acutal data before we pass it onto the XML-RPC library: $data['public_key'] = $public_key; $data['time'] = $time; $data['hash'] = $hash; return $data; }