You are here

function simplenews_confirm_subscription in Simplenews 6.2

Same name and namespace in other branches
  1. 5 simplenews.module \simplenews_confirm_subscription()
  2. 6 simplenews.subscription.inc \simplenews_confirm_subscription()
  3. 7.2 includes/simplenews.subscription.inc \simplenews_confirm_subscription()
  4. 7 includes/simplenews.subscription.inc \simplenews_confirm_subscription()

Menu callback: confirm the user's (un)subscription request

This function is called by clicking the confirm link in the confirmation email or the unsubscribe link in the footer of the newsletter. It handles both subscription addition and subscription removal.

Calling URLs are: newsletter/confirm/add newsletter/confirm/add/$HASH newsletter/confirm/remove newsletter/confirm/remove/$HASH

See also

simplenews_confirm_add_form()

simplenews_confirm_removal_form()

1 string reference to 'simplenews_confirm_subscription'
simplenews_menu in ./simplenews.module
Implementation of hook_menu().

File

includes/simplenews.subscription.inc, line 466
(Un)subscription and (un)subscription confirmation

Code

function simplenews_confirm_subscription() {
  $arguments = func_get_args();

  // add or remove
  $op1 = array_shift($arguments);

  // private key hash
  $op2 = array_shift($arguments);
  $md5 = drupal_substr($op2, 0, 10);
  list($snid, $tid) = explode('t', drupal_substr($op2, 10));
  $result = db_query('
    SELECT snid, mail
    FROM {simplenews_subscriptions}
    WHERE snid = %d', $snid);
  if (!($subs = db_fetch_object($result))) {
    drupal_not_found();
    return;
  }

  // Prevent search engines from indexing this page.
  drupal_set_html_head('<meta name="robots" content="noindex" />');
  if ($md5 == drupal_substr(md5($subs->mail . simplenews_private_key()), 0, 10)) {
    $newsletter = taxonomy_get_term($tid);

    // The confirmation page called with two arguments will display a confirmation question.
    // When called with three of more arguments the user will be directed to the
    // (un)subscribe confirmation page. The additional arguments will be passed on
    // to the confirmation page.
    if (empty($arguments)) {
      if ($op1 == 'remove') {
        return drupal_get_form('simplenews_confirm_removal_form', $subs->mail, $newsletter);
      }
      elseif ($op1 == 'add') {
        return drupal_get_form('simplenews_confirm_add_form', $subs->mail, $newsletter);
      }
    }
    else {
      if ($op1 == 'remove') {
        simplenews_unsubscribe_user($subs->mail, $tid, FALSE, 'website');
        if ($path = variable_get('simplenews_confirm_unsubscribe_page', '')) {
          $path = $path . '/' . implode('/', $arguments);
          drupal_goto($path);
        }
        drupal_set_message(t('%user was unsubscribed from the %newsletter mailing list.', array(
          '%user' => $subs->mail,
          '%newsletter' => $newsletter->name,
        )));
        drupal_goto(variable_get('site_frontpage', 'node'));
      }
      elseif ($op1 == 'add') {
        simplenews_subscribe_user($subs->mail, $tid, FALSE, 'website');
        if ($path = variable_get('simplenews_confirm_subscribe_page', '')) {
          $path = $path . '/' . implode('/', $arguments);
          drupal_goto($path);
        }
        drupal_set_message(t('%user was added to the %newsletter mailing list.', array(
          '%user' => $subs->mail,
          '%newsletter' => $newsletter->name,
        )));
        drupal_goto(variable_get('site_frontpage', 'node'));
      }
    }
  }

  // If md5 didn't match, do a not found.
  drupal_not_found();
  return;
}