You are here

function ad_redirect in Advertisement 7

Same name and namespace in other branches
  1. 5.2 ad.module \ad_redirect()
  2. 5 ad.module \ad_redirect()
  3. 6.3 ad.module \ad_redirect()
  4. 6 ad.module \ad_redirect()
  5. 6.2 ad.module \ad_redirect()

Update click counter then redirect host to ad's target URL.

1 string reference to 'ad_redirect'
ad_menu in ./ad.module
Implementation of hook_menu().

File

./ad.module, line 238

Code

function ad_redirect($aid, $group = NULL) {
  global $user;
  $hostid = isset($_GET['hostid']) ? $_GET['hostid'] : '';
  $extra = isset($_GET['extra']) ? $_GET['extra'] : '';
  if (function_exists('click_filter_status')) {
    $status = click_filter_status($aid, $hostid);
  }
  else {
    $status = 0;
  }

  // Allow source url to be passed in.
  $url = isset($_GET['u']) ? $_GET['u'] : '';
  if (!isset($url) || !valid_url($url)) {
    $url = $_SERVER['HTTP_REFERER'];
  }
  db_insert('ad_clicks')
    ->fields(array(
    'aid' => $aid,
    'uid' => $user->uid,
    'status' => $status,
    'hostname' => ip_address(),
    'user_agent' => $_SERVER['HTTP_USER_AGENT'],
    'adgroup' => $group,
    'extra' => $extra,
    'hostid' => $hostid,
    'url' => $url,
    'timestamp' => time(),
  ))
    ->execute();
  watchdog('ad', 'Clicked ad aid %aid hostid %hostid.', array(
    '%aid' => $aid,
    '%hostid' => $hostid,
  ));
  if (function_exists('click_filter_status') && $status == CLICK_VALID) {
    ad_statistics_increment($aid, 'click', $group, $hostid);
  }
  else {
    if (!function_exists('click_filter_status')) {

      // We're not filtering clicks, so all clicks are valid.
      ad_statistics_increment($aid, 'click', $group, $hostid);
    }
  }

  // Determine where we're supposed to redirect the user.
  $adtype = db_query('SELECT adtype FROM {ads} WHERE aid = :aid', array(
    ':aid' => $aid,
  ))
    ->fetchField();
  $node = new stdClass();
  $node->nid = $node->aid = $aid;
  $node->hostid = $hostid;
  $url = module_invoke('ad_' . $adtype, 'adapi', 'redirect', $node);
  if (isset($url)) {
    header('Location: ' . $url);
  }
  else {
    watchdog('ad', 'Ad redirection failed for aid %aid hostid %hostid, failed to load destination URL. ', array(
      '%aid' => $aid,
      '%hostid' => $hostid,
    ));
    drupal_goto('');
  }
}