You are here

function ad_redirect in Advertisement 6.3

Same name and namespace in other branches
  1. 5.2 ad.module \ad_redirect()
  2. 5 ad.module \ad_redirect()
  3. 6 ad.module \ad_redirect()
  4. 6.2 ad.module \ad_redirect()
  5. 7 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 191

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 = referer_uri();
  }
  db_query("INSERT INTO {ad_clicks} (aid, uid, status, hostname, user_agent, adgroup, extra, hostid, url, timestamp) VALUES (%d, %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', %d)", $aid, $user->uid, $status, ip_address(), $_SERVER['HTTP_USER_AGENT'], $group, $extra, $hostid, $url, time());
  watchdog('ad', 'Clicked %type ad aid %aid hostid %hostid.', array(
    '%type' => $adtype,
    '%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_result(db_query('SELECT adtype FROM {ads} WHERE aid = %d', $aid));
  $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('');
  }
}