You are here

function simplenews_confirm_combined in Simplenews 7.2

Menu callback: confirm a combined confirmation request.

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

Parameters

$snid: The subscriber id.

$timestamp: The timestamp of the request.

$hash: The confirmation hash.

See also

simplenews_confirm_add_form()

simplenews_confirm_removal_form()

1 string reference to 'simplenews_confirm_combined'
simplenews_menu in ./simplenews.module
Implements hook_menu().

File

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

Code

function simplenews_confirm_combined($snid, $timestamp, $hash) {
  $arguments = array_slice(func_get_args(), 3);

  // Prevent search engines from indexing this page.
  $noindex = array(
    '#tag' => 'meta',
    '#attributes' => array(
      'name' => 'robots',
      'content' => 'noindex',
    ),
  );
  drupal_add_html_head($noindex, 'simplenews-noindex');
  $subscriber = simplenews_subscriber_load($snid);

  // Redirect and display message if no changes are available.
  if ($subscriber && empty($subscriber->changes)) {
    drupal_set_message(t('All changes to your subscriptions where already applied. No changes made.'));
    drupal_goto(variable_get('site_frontpage', 'node'));
  }
  if ($subscriber && $hash == simplenews_generate_hash($subscriber->mail, 'combined' . serialize($subscriber->changes), $timestamp)) {

    // If the hash is valid but timestamp is too old, display form to request
    // a new hash.
    if ($timestamp < REQUEST_TIME - variable_get('simplenews_hash_expiration', 86400)) {
      $context = array(
        'simplenews_subscriber' => $subscriber,
      );
      return drupal_get_form('simplenews_request_hash', 'subscribe_combined', $context);
    }

    // When called with additional 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)) {
      return drupal_get_form('simplenews_confirm_multi_form', $subscriber);
    }
    else {

      // Redirect and display message if no changes are available.
      foreach ($subscriber->changes as $newsletter_id => $action) {
        if ($action == 'subscribe') {
          simplenews_subscribe($subscriber->mail, $newsletter_id, FALSE, 'website');
        }
        elseif ($action == 'unsubscribe') {
          simplenews_unsubscribe($subscriber->mail, $newsletter_id, FALSE, 'website');
        }
      }

      // Clear changes.
      $subscriber->changes = array();
      simplenews_subscriber_save($subscriber);
      drupal_set_message(t('Subscription changes confirmed for %user.', array(
        '%user' => $subscriber->mail,
      )));
      drupal_goto(variable_get('site_frontpage', 'node'));
    }
  }
  return MENU_NOT_FOUND;
}