You are here

function adserve_cache_display in Advertisement 5.2

Same name and namespace in other branches
  1. 6.3 adcache.inc \adserve_cache_display()
  2. 6.2 adcache.inc \adserve_cache_display()
  3. 7 adcache.inc \adserve_cache_display()

Default function for displaying advertisements. This is not generally replaced by ad cache modules.

File

./adcache.inc, line 441

Code

function adserve_cache_display($ids) {
  $output = '';
  $ads = 0;
  foreach ($ids as $id) {
    $ad = adserve_cache('display_ad', $id);
    _debug_echo('ad: ' . htmlentities($ad));

    // if displaying multiple ads, separate each with a div
    if ($output) {
      $group = adserve_variable('group');
      $output .= "<div class=\"advertisement-space\" id=\"space-{$group}-{$ads}\"></div>";
    }

    // display advertisement
    $output .= $ad;

    // increment counters
    if (adserve_variable('ad_display') == 'raw') {
      $output .= ad_display_image($ad);
    }
    else {
      adserve_cache('increment', 'view', $id);
    }
    $ads++;
  }
  if (empty($ids)) {
    adserve_variable('error', TRUE);
    $output = 'No active ads were found in ' . adserve_variable('group');
    adserve_cache('increment', 'count', NULL);
  }

  // close/update cache, if necessary
  adserve_cache('close');

  // update the dynamic portion of the output
  $hostid = adserve_variable('hostid');
  $group = adserve_variable('group');
  $extra = adserve_variable('extra');
  if ($url = htmlentities(adserve_variable('url'))) {
    $replace = "/{$group}/{$extra}/{$hostid}?u={$url}";
  }
  else {
    $replace = "/{$group}/{$extra}/{$hostid}";
  }
  $output = preg_replace('&/@HOSTID___&', $replace, $output);

  // there was an error, hide the output in comments
  if (adserve_variable('error')) {
    $output = "<!-- {$output} -->";
  }

  // allow custom text to be displayed before and after advertisement
  $init_text = adserve_invoke_hook('init_text', 'append');
  $exit_text = adserve_invoke_hook('exit_text', 'append');
  $output = $init_text . $output . $exit_text;

  // TODO: turn all of these into hooks
  switch (adserve_variable('ad_display')) {
    case 'javascript':
    default:
      $output = str_replace(array(
        "\r",
        "\n",
        "<",
        ">",
        "&",
      ), array(
        '\\r',
        '\\n',
        '\\x3c',
        '\\x3e',
        '\\x26',
      ), addslashes($output));
      if (!adserve_variable('debug')) {

        // Tell the web browser not to cache this script so the ad refreshes
        // each time the page is viewed.
        // Expires in the past:
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

        // Last load:
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');

        // HTTP 1.1:
        header('Cache-Control: no-store, no-cache, must-revalidate');
        header('Cache-Control: post-check=0, pre-check=0', false);

        // HTTP 1.0:
        header('Pragma: no-cache');

        // Output is a JavaScript:
        header('Content-Type: application/x-javascript; charset=utf-8');
      }
      print "document.write('{$output}');";
      exit(0);
    case 'iframe':
    case 'jquery':
      if (!adserve_variable('debug')) {

        // Tell the web browser not to cache this frame so the ad refreshes
        // each time the page is viewed.
        // Expires in the past:
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

        // Last load:
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');

        // HTTP 1.1:
        header('Cache-Control: no-store, no-cache, must-revalidate');
        header('Cache-Control: post-check=0, pre-check=0', false);

        // HTTP 1.0:
        header('Pragma: no-cache');
      }
      else {
        _debug_echo('Output: ' . htmlentities($output));
      }
      print "{$output}";
      exit(0);
    case 'raw':
      _debug_echo('Output: ' . htmlentities($output));
      chdir(adserve_variable('ad_dir'));
      return $output;
  }
  _debug_echo('Output: ' . htmlentities($output));
  return $output;
}