'spread/js', 'callback' => 'spread_js', 'type' => MENU_CALLBACK, 'access' => user_access('use spread form'), ); $items[] = array( 'path' => 'admin/settings/spread', 'title' => t('Spread') , 'description' => t('Manage Spread e-mail messages'), 'callback' => 'drupal_get_form', 'callback arguments' => array('spread_admin_settings'), 'access' => user_access('administer site configuration'), ); } return $items; } /** * Implementation of hook_perm(). */ function spread_perm() { return array('use spread form'); } function spread_admin_settings() { $form = array(); $form['spread']['subject'] = array( '#title' => t('Subject'), '#type' => 'textfield', '#required' => TRUE, '#default_value' => variable_get('spread_subject', t('You need to discover this!')), '#description' => t('The subject of the e-mail which will be sent.'), ); $form['spread']['body'] = array( '#title' => t('Body'), '#type' => 'textarea', '#required' => TRUE, '#default_value' => variable_get('spread_body', t("Hi,\n!sender_email thinks that you should review this web page:\n!url")), '#description' => t("This is the message which will be sent. You can use the following placeholders to adjust it:
!sender_email: user e-mail who spread the word
!url: web page from which the user spread the word"), ); $form['spread']['how_many']= array( '#title' => t("How many e-mail fields?"), '#type' => 'textfield', '#required' => TRUE, '#default_value' => variable_get('spread_how_many', 3), '#description' => t('This is the number of e-mail fields which will be generated on the spread block form.'), '#size' => 2, '#maxlength' => '2', ); return system_settings_form($form); } function spread_admin_settings_validate($form_id, $form_values) { // Is entered value for field count is number? if (!is_numeric($form_values['how_many'])) { form_set_error('how_many', t('You must enter a numeric value')); } if ((int)$form_values['how_many'] == 0) { form_set_error('how_many', t('0 seems to be not enough!')); } } function spread_admin_settings_submit($form_id, $form_values) { variable_set('spread_subject', $form_values['subject']); variable_set('spread_body', $form_values['body']); variable_set('spread_how_many', $form_values['how_many']); drupal_set_message(t('Configuration saved.')); } function spread_block($op = 'list', $delta = 0, $edit = array()) { switch ($op) { case 'list': $blocks[0]['info'] = t('Spread form'); return $blocks; case 'view': switch ($delta) { case 0: // Checking user_access if (user_access('use spread form')) { $block['subject'] = t('Spread the word'); $block['content'] = drupal_get_form('spread_form'); drupal_add_js('Drupal.basePath = "'. base_path() .'"', 'inline'); drupal_add_js("Drupal.q = '". $_GET['q'] ."'", 'inline'); drupal_add_js(drupal_get_path('module', 'spread') .'/spread.js'); drupal_add_css(drupal_get_path('module', 'spread') .'/spread.css'); } break; } return $block; } } function spread_form() { // We need this to get user's email global $user; $form['thank_you'] = array( '#prefix' => "
", '#value' => t('Thank you for spreading the word!'), '#suffix' => "
", ); $form['friends_label'] = array( '#prefix' => '' ); for ($i = 0; $i < variable_get('spread_how_many', 3); $i++) { $form['to'][] = array( '#type' => 'textfield', '#size' => 20, '#name' => 'to[]', '#default_value' => $_POST['to'][$i], ); } $form['from'] = array( '#type' => 'textfield', '#title' => t('Your e-mail'), '#size' => 20, '#default_value' => $user->mail, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Spread the word'), ); return $form ; } function spread_form_validate($form_id, $form_values) { $error = FALSE; foreach ($_POST['to'] as $key => $mail) { $form_values['to'][] = $mail; if (!valid_email_address($mail) && trim($mail) != '') { $error = TRUE; unset($_POST['to'][$key]); } if (trim($mail) == '') { unset($_POST['to'][$key]); } } if (!valid_email_address($form_values['from'])) { $error = TRUE; } if ($error == TRUE) { form_set_error('', t('E-mail adresses must be valid')); } } function spread_form_submit($form_id, &$form_values) { spread_send_email($_POST['to'], $form_values['from']); } function spread_send_email($to = array(), $from = NULL, $js = FALSE) { $subject = variable_get("spread_subject", NULL); $body = variable_get("spread_body", NULL); $headers = array('Content-Type' => 'text/html; charset=UTF-8; format=flowed'); if (!$js) { $url = url($_GET['q'], $query = NULL, $fragment = NULL, $absolute = TRUE); } else { $url = url($_POST['url'], $query = NULL, $fragment = NULL, $absolute = TRUE); } foreach ($to as $mail) { $placeholders = array( "!sender_email" => $from, "!url" => $url, '!mail' => $mail, ); drupal_mail("spread", $mail, check_plain(t($subject, $placeholders), FILTER_FORMAT_DEFAULT, FALSE), check_markup(t($body, $placeholders), FILTER_FORMAT_DEFAULT, FALSE), $from, $headers); $watchdog_message = t("!sender_email has spread the word to !mail on !url", $placeholders); watchdog('spread', $watchdog_message, $severity = WATCHDOG_NOTICE); } if (!$js) { drupal_set_message(t('Thank you for spreading the word!')); } } function spread_js() { global $user; if (!user_access('use spread form')) { die(); } if (($emails = spread_js_validate($_POST['to'], $_POST['from'])) != FALSE) { spread_send_email($emails['to'], $emails['from'], $js = TRUE); print "TRUE"; } else { print "FALSE"; } die(); } function spread_js_validate($adresses = array(), $from = NULL) { $error = FALSE; foreach ($adresses as $key => $mail) { $to[] = $mail; if (!valid_email_address($mail) && trim($mail) != '') { $error = TRUE; unset($adresses[$key]); } if (trim($mail) == '') { unset($adresses[$key]); } } if (!valid_email_address($from)) { $error = TRUE; } if (count($adresses) < 1) { $error = TRUE; } if ($error == TRUE) { print "FALSE"; // Stop here, email aren't valid die(); } return array( 'to' => $adresses, 'from' => $from, ); }