You are here

ad_external.module in Advertisement 5

Enhances the ad module to support externally hosted ads, served via IFrames. It is recommended that you configure "Administer >> Content management >> Ads >> Settings >> Global settings >> Display type" to "Raw". If you configure the "Display type" to IFrame, you will be serving IFrames within IFrames.

Copyright (c) 2007-2008. Jeremy Andrews <jeremy@kerneltrap.org>. All rights reserved.

File

external/ad_external.module
View source
<?php

/**
 * @file
 * Enhances the ad module to support externally hosted ads, served via IFrames.
 * It is recommended that you configure "Administer >> Content management >> 
 *  Ads >> Settings >> Global settings >> Display type" to "Raw".
 * If you configure the "Display type" to IFrame, you will be serving IFrames
 * within IFrames.
 *
 * Copyright (c) 2007-2008.
 *   Jeremy Andrews <jeremy@kerneltrap.org>.  All rights reserved.
 */

/**
 * Function used to display the selected ad.
 */
function ad_external_display_ad($ad) {
  global $base_url;

  // For now, we hard code this to only support caching with the file cache.
  // As more cache types are introduced, we'll expand this.  Ideally it should
  // be cache type agnostic, but that seems to be unrealistic.
  if (variable_get('ad_cache', 'none') == 'file') {
    $adserve = variable_get('adserve', '');
    $display_variables .= '&amp;c=file' . module_invoke('ad_cache_file', 'adcacheapi', 'display_variables', array());
    $external = url("{$base_url}/{$adserve}?o=external&amp;n={$ad->aid}&amp;{$display_variables}");
  }
  else {
    $external = db_result(db_query('SELECT url FROM {ad_external} WHERE aid = %d', $ad->aid));
  }
  $append = 'frameborder="' . variable_get('ad_iframe_frameborder', 0) . '" ';
  $append .= 'scrolling="' . variable_get('ad_iframe_scroll', 'auto') . '" ';
  $append .= "name=\"{$group}\" ";
  if ($height = variable_get('ad_iframe_height', '')) {
    $append .= "height=\"{$height}\" ";
  }
  if ($width = variable_get('ad_iframe_width', '')) {
    $append .= "width=\"{$width}\" ";
  }
  return "<div class=\"external-advertisement\" id=\"ad-{$ad->aid}\"><iframe src=\"{$external}\" {$append}></iframe></div>";
}

/**
 * Implementation of hook_help().
 */
function ad_external_help($path) {
  switch ($path) {
    case 'node/add/ad#external':
      $output = t('An external advertisement, displayed in an IFrame.');
      break;
  }
  return $output;
}
function ad_external_adapi($op, &$node) {
  switch ($op) {
    case 'load':
      return db_fetch_array(db_query('SELECT * FROM {ad_external} WHERE aid = %d', $node['aid']));
    case 'insert':
      db_query("INSERT INTO {ad_external} (aid, url) VALUES(%d, '%s')", $node->nid, $node->url);
      break;
    case 'update':
      db_query("UPDATE {ad_external} SET url = '%s' WHERE aid = %d", $node->url, $node->nid);
      break;
    case 'delete':
      db_query('DELETE FROM {ad_external} WHERE aid = %d', $node->nid);
      break;
    case 'form':
      return ad_external_node_form($node);
    case 'view':
      return ad_external_node_view($node);
    case 'redirect':

      // TODO: Would it ever make sense to have redirects for this ad type?
      watchdog('ad', t('Unexpected redirect attempt in external ad type.'));
      return;
    case 'type':
      return 'external';
  }
}

/**
 * Adapi helper function for displaying a node form.
 */
function ad_external_node_form(&$node) {
  $form = array();
  $form['ad_external'] = array(
    '#type' => 'fieldset',
    '#title' => t('External'),
    '#collapsible' => TRUE,
  );
  $form['ad_external']['preview'] = array(
    '#type' => 'markup',
    '#value' => ad_external_display_ad($node),
  );
  if ((arg(1) == 'add' || arg(2) == 'edit') && user_access('create advertisements')) {
    $form['ad_external']['url'] = array(
      '#type' => 'textfield',
      '#title' => t('External Source URL'),
      '#required' => TRUE,
      '#default_value' => $node->url,
      '#description' => t('Enter the complete URL where your external ad his hosted.  The URL must begin with http:// or https://.  For example, %url.', array(
        '%url' => t('http://www.sample.org/external/ad.php'),
      )),
    );
  }
  return $form;
}
function ad_external_node_view(&$node) {
  $node->content['ad'] = array(
    '#value' => theme('box', '', stripslashes(ad_external_display_ad($node))),
    '#weight' => -1,
  );
  $link = t('Links to !url.', array(
    '!url' => $node->url,
  ));
  $link = check_markup($link, $node->format, FALSE);
  $node->content['ad-link'] = array(
    '#value' => "<div class=\"links-to\">{$link}<div>",
    '#weight' => 1,
  );
}

/**
 * Download external pages and store them in the filecache.
 */
function ad_external_ad_build_cache() {
  $cache = array();
  $result = db_query('SELECT * FROM {ad_external}');
  while ($external = db_fetch_object($result)) {
    $contents = file_get_contents($external->url);
    if ($contents === FALSE) {

      // Failed to download external url, don't cache the error page.
      // TODO: Watchdog log this.
      // TODO: Would it be possible to obtain the old version from the cache,
      //       if we already downloaded this page once?
      continue;
    }
    $cache['ad_external'][$external->aid]['url'] = $external->url;
    $cache['ad_external'][$external->aid]['contents'] = $contents;
  }
  return $cache;
}

Functions

Namesort descending Description
ad_external_adapi
ad_external_ad_build_cache Download external pages and store them in the filecache.
ad_external_display_ad Function used to display the selected ad.
ad_external_help Implementation of hook_help().
ad_external_node_form Adapi helper function for displaying a node form.
ad_external_node_view