You are here

adsense.module in Google AdSense integration 5

File

adsense.module
View source
<?php

// Copyright 2005 Khalid Baheyeldin http://2bits.com

/**
 * This is the array that holds all ad formats.
 * All it has is a multi-dimensional array containing the dimensions as a key, then
 * the description and suffix of Google's javascript ad code.
 * Currently, we do not support duplicate dimensions in ad formats (for example,
 * the same ad dimension cannot repeat for text and graphics, or for ads and ad links
 *
 * To add a new code:
 * - Make sure the dimensions are not in use by a different format
 * - Go to Google AdSense
 *   . Get the dimensions
 *   . Get the code (suffix after the dimensions)
 * - Add it below in ascending order (width first, then height)
 * @return returns an array of supported ad formats (desc: description, code: Google Ad code suffix)
 */
function adsense_ad_formats() {
  return array(
    '120x90' => array(
      'type' => ADSENSE_TYPE_LINK,
      'desc' => t('Ad links 4-links 120x90'),
      'code' => '_0ads_al',
    ),
    '120x240' => array(
      'type' => ADSENSE_TYPE_AD,
      'desc' => t('Vertical banner'),
      'code' => '_as',
    ),
    '120x600' => array(
      'type' => ADSENSE_TYPE_AD,
      'desc' => t('Skyscraper'),
      'code' => '_as',
    ),
    '125x125' => array(
      'type' => ADSENSE_TYPE_AD,
      'desc' => t('Button'),
      'code' => '_as',
    ),
    '160x90' => array(
      'type' => ADSENSE_TYPE_LINK,
      'desc' => t('Ad links 4-links 160x90'),
      'code' => '_0ads_al',
    ),
    '160x600' => array(
      'type' => ADSENSE_TYPE_AD,
      'desc' => t('Wide skyscraper'),
      'code' => '_as',
    ),
    '180x90' => array(
      'type' => ADSENSE_TYPE_LINK,
      'desc' => t('Ad links 4-links 180x90'),
      'code' => '_0ads_al',
    ),
    '180x150' => array(
      'type' => ADSENSE_TYPE_AD,
      'desc' => t('Small rectangle'),
      'code' => '_as',
    ),
    '200x200' => array(
      'type' => ADSENSE_TYPE_AD,
      'desc' => t('Small square'),
      'code' => '_as',
    ),
    '234x60' => array(
      'type' => ADSENSE_TYPE_AD,
      'desc' => t('Half banner'),
      'code' => '_as',
    ),
    '250x250' => array(
      'type' => ADSENSE_TYPE_AD,
      'desc' => t('Square'),
      'code' => '_as',
    ),
    '300x250' => array(
      'type' => ADSENSE_TYPE_AD,
      'desc' => t('Medium rectangle'),
      'code' => '_as',
    ),
    '336x280' => array(
      'type' => ADSENSE_TYPE_AD,
      'desc' => t('Large rectangle'),
      'code' => '_as',
    ),
    '468x15' => array(
      'type' => ADSENSE_TYPE_LINK,
      'desc' => t('Banner ad links 4-links'),
      'code' => '_0ads_al',
    ),
    '468x60' => array(
      'type' => ADSENSE_TYPE_AD,
      'desc' => t('Banner'),
      'code' => '_as',
    ),
    '728x15' => array(
      'type' => ADSENSE_TYPE_LINK,
      'desc' => t('Wide banner ad links 4-links'),
      'code' => '_0ads_al',
    ),
    '728x90' => array(
      'type' => ADSENSE_TYPE_AD,
      'desc' => t('Leaderboard'),
      'code' => '_as',
    ),
    ADSENSE_SEARCH_BOX => array(
      'type' => ADSENSE_TYPE_SEARCH,
      'desc' => t('AdSense for Search'),
      'code' => '',
    ),
  );
}

// Ad types, link or ad
define('ADSENSE_TYPE_LINK', 1);
define('ADSENSE_TYPE_AD', 2);
define('ADSENSE_TYPE_SEARCH', 3);
define('ADSENSE_MAX_GROUPS', 5);
define('ADSENSE_MAX_CHANNELS', 10);
define('ADSENSE_MAX_BLOCKS', 8);
define('ADSENSE_VISIBILITY', 'adsense_visibility');
define('ADSENSE_ACCESS_PAGES', 'adsense_access_pages');
define('ADSENSE_AD_TYPE', 'adsense_ad_type_');
define('ADSENSE_COLOR_TEXT', 'adsense_color_text_');
define('ADSENSE_COLOR_BORDER', 'adsense_color_border_');
define('ADSENSE_COLOR_BG', 'adsense_color_bg_');
define('ADSENSE_COLOR_LINK', 'adsense_color_link_');
define('ADSENSE_COLOR_URL', 'adsense_color_url_');
define('ADSENSE_ALT', 'adsense_alt_');
define('ADSENSE_ALT_INFO', 'adsense_alt_info_');
define('ADSENSE_AD_CHANNEL', 'adsense_ad_channel_');
define('ADSENSE_REVENUE_ENABLE', 'adsense_revenue_enable');
define('ADSENSE_PERCENTAGE_AUTHOR', 'adsense_percentage_author');
define('ADSENSE_PERCENTAGE_REFER', 'adsense_percentage_refer');
define('ADSENSE_NODE_TYPE', 'adsense_node_type_');
define('ADSENSE_DISABLE', 'adsense_disable');
define('ADSENSE_TEST_MODE', 'adsense_test_mode');
define('ADSENSE_PLACEHOLDER', 'adsense_placeholder');
define('ADSENSE_PLACEHOLDER_TEXT', 'adsense_placeholder_text');
define('ADSENSE_SECTION_TARGETING', 'adsense_section_targeting');
define('ADSENSE_CLICK_TRACKING', 'adsense_click_tracking');
define('ADSENSE_CLIENT_ID_PROFILE_FIELD', 'adsense_client_id_profile_field');
define('ADSENSE_AD_BLOCK', 'adsense_ad_block_');
define('ADSENSE_CLICK_TRACKING_NAME_RESOLVE', 'adsense_click_tracking_name_resolve');
define('ADSENSE_SECTION_START', '<!-- google_ad_section_start -->');
define('ADSENSE_SECTION_IGNORE', '<!-- google_ad_section_start(weight=ignore) -->');
define('ADSENSE_SECTION_END', '<!-- google_ad_section_end -->');
define('ADSENSE_SEARCH_RESULT_START', '<!-- Google Search Result Snippet Starts -->');
define('ADSENSE_SEARCH_RESULT_END', '<!-- Google Search Result Snippet Ends -->');
define('ADSENSE_SEARCH_COLOR_TEXT', 'adsense_search_color_text');
define('ADSENSE_SEARCH_COLOR_BORDER', 'adsense_search_color_border');
define('ADSENSE_SEARCH_COLOR_BG', 'adsense_search_color_bg');
define('ADSENSE_SEARCH_COLOR_TITLE', 'adsense_search_color_title');
define('ADSENSE_SEARCH_COLOR_URL', 'adsense_search_color_url');
define('ADSENSE_SEARCH_COLOR_VISITED_URL', 'adsense_search_color_visited_url');
define('ADSENSE_SEARCH_COLOR_LIGHT_URL', 'adsense_search_color_light_url');
define('ADSENSE_SEARCH_COLOR_BOX_BACKGROUND', 'adsense_search_color_box_background');
define('ADSENSE_SEARCH_COLOR_BOX_TEXT', 'adsense_search_color_box_text');
define('ADSENSE_SEARCH_COLOR_WHITE', 'white');
define('ADSENSE_SEARCH_COLOR_BLACK', '#000000');
define('ADSENSE_SEARCH_COLOR_GRAY', '#cccccc');
define('ADSENSE_SEARCH_DOMAIN', 'adsense_search_domain');
define('ADSENSE_SEARCH_LANGUAGE', 'adsense_search_language');
define('ADSENSE_SEARCH_ENCODING', 'adsense_search_encoding');
define('ADSENSE_SEARCH_COUNTRY', 'adsense_search_country');
define('ADSENSE_SEARCH_TEXTBOX_LENGTH', 'adsense_search_textbox_length');
define('ADSENSE_SEARCH_LOGO', 'adsense_search_logo');
define('ADSENSE_SEARCH_LOGO_GOOGLE', 'adsense_search_logo_google');
define('ADSENSE_SEARCH_LOGO_ABOVE_TEXTBOX', 'adsense_search_logo_above_textbox');
define('ADSENSE_SEARCH_LOGO_ON_BUTTON', 'adsense_search_logo_on_button');
define('ADSENSE_SEARCH_BUTTON', 'adsense_search_button');
define('ADSENSE_SEARCH_FRAME_WIDTH', 'adsense_search_frame_width');
define('ADSENSE_SEARCH_BOX', 'Search Box');
define('ADSENSE_SEARCH_PATH', 'adsense/search');
define('ADSENSE_SITE_SEARCH', '<!-- SiteSearch Google -->');
function adsense_get_ad_code($format) {
  $all_ads = adsense_ad_formats();
  return $all_ads[$format]['code'];
}

/**
 * Implementation of hook_help().
 */
function adsense_help($section) {
  switch ($section) {
    case 'admin/settings/adsense':
      return t('<p>@revision @date</p>', array(
        '@revision' => trim('$Revision$', ' $'),
        '@date' => trim('$Date$', ' $'),
      ));
  }
}

/**
 * Implementation of hook_menu().
 */
function adsense_menu($may_cache) {
  $items = array();
  $access = user_access('view clicks');
  if ($may_cache) {
    if (variable_get(ADSENSE_CLICK_TRACKING, 0)) {
      $items[] = array(
        'path' => 'admin/logs/adsense',
        'title' => t('AdSense clicks'),
        'description' => t('Track AdSense clicks.'),
        'callback' => 'adsense_click_log',
        'type' => MENU_NORMAL_ITEM,
        'access' => $access,
      );
      $items[] = array(
        'path' => 'admin/logs/adsense/top_pages',
        'title' => t('Top pages'),
        'callback' => 'adsense_click_top_pages',
        'type' => MENU_NORMAL_ITEM,
        'access' => $access,
      );
      $items[] = array(
        'path' => 'admin/logs/adsense/by_day',
        'title' => t('By day'),
        'callback' => 'adsense_click_by_day',
        'type' => MENU_NORMAL_ITEM,
        'access' => $access,
      );
    }
    $items[] = array(
      'path' => 'admin/settings/adsense',
      'title' => t('Google AdSense'),
      'description' => t('Configure Google AdSense.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'adsense_admin_settings',
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  else {
    $items[] = array(
      'path' => 'adsense/counter',
      'callback' => 'adsense_counter',
      'access' => true,
      'type' => MENU_CALLBACK,
    );
  }
  $items[] = array(
    'path' => ADSENSE_SEARCH_PATH,
    'callback' => 'adsense_search',
    'access' => user_access('access adsense search result page'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function adsense_perm() {
  return array(
    'hide adsense',
    'view clicks',
    'access adsense search result page',
  );
}
function adsense_admin_settings() {
  $form = array();
  cache_clear_all('adsense', 'cache', TRUE);

  // We only load the help text if we are in settings, to save memory
  include_once drupal_get_path('module', 'adsense') . '/adsense_help.inc';

  // Get the ad formats
  $headers = array(
    t('Ad type'),
    t('Dimensions'),
  );
  $rows = array();
  foreach (adsense_ad_formats() as $format => $data) {
    $rows[] = array(
      $data['desc'],
      $format,
    );
  }

  // Create the ad format text
  $help_text = theme('table', $headers, $rows);
  $form['help'] = array(
    '#type' => 'fieldset',
    '#collapsible' => true,
    '#collapsed' => true,
    '#title' => t('Help and instructions'),
  );
  $form['help']['help'] = array(
    '#type' => 'markup',
    '#value' => adsense_help_text($help_text),
  );
  $error_flag = false;
  if (!module_exists('profile')) {
    drupal_set_message('The profile module is not enabled. Please enable it and define a field as per instructions.', 'error');
    $error_flag = true;
  }
  if (!_adsense_get_site_owner_client_id()) {
    drupal_set_message('There is no site owner Google AdSense client ID defined. Please define a profile field, and select it below as per instructions, and enter a value for user id 1 as explained below.', 'error');
    $error_flag = true;
  }
  $form['required'] = array(
    '#type' => 'fieldset',
    '#collapsible' => true,
    '#collapsed' => false,
    '#title' => t('Required parameters'),
  );
  $form['required'][ADSENSE_CLIENT_ID_PROFILE_FIELD] = array(
    '#type' => 'select',
    '#title' => t('Google AdSense client ID profile field'),
    '#default_value' => variable_get(ADSENSE_CLIENT_ID_PROFILE_FIELD, 0),
    '#options' => _adsense_get_profile_fields(),
    '#required' => true,
    '#description' => t('This is the profile field that holds the AdSense Client ID for the site owner as well as (optionally) for site users who participate in revenue sharing. You must enabled the profile module and create a new field for this.'),
  );
  if ($error_flag) {
    return system_settings_form($form);
  }
  $form['types_colors'] = array(
    '#type' => 'fieldset',
    '#collapsible' => true,
    '#collapsed' => true,
    '#title' => t('Ad groups'),
  );
  for ($group = 1; $group < ADSENSE_MAX_GROUPS + 1; $group++) {
    $form['types_colors']['groups'][$group] = array(
      '#type' => 'fieldset',
      '#collapsible' => true,
      '#collapsed' => true,
      '#title' => t('Group @group attributes', array(
        '@group' => $group,
      )),
    );
    $form['types_colors']['groups'][$group][ADSENSE_AD_TYPE . $group] = array(
      '#type' => 'radios',
      '#title' => t('Ad type'),
      '#default_value' => variable_get(ADSENSE_AD_TYPE . $group, '000000'),
      '#options' => array(
        t('Text'),
        t('Image'),
        t('Both'),
      ),
    );
    $form['types_colors']['groups'][$group][ADSENSE_COLOR_TEXT . $group] = array(
      '#type' => 'textfield',
      '#title' => t('Ad text color'),
      '#default_value' => variable_get(ADSENSE_COLOR_TEXT . $group, '000000'),
      '#size' => 6,
      '#maxlength' => 6,
    );
    $form['types_colors']['groups'][$group][ADSENSE_COLOR_BORDER . $group] = array(
      '#type' => 'textfield',
      '#title' => t('Ad border color'),
      '#default_value' => variable_get(ADSENSE_COLOR_BORDER . $group, '336699'),
      '#size' => 6,
      '#maxlength' => 6,
    );
    $form['types_colors']['groups'][$group][ADSENSE_COLOR_BG . $group] = array(
      '#type' => 'textfield',
      '#title' => t('Ad background color'),
      '#default_value' => variable_get(ADSENSE_COLOR_BG . $group, 'FFFFFF'),
      '#size' => 6,
      '#maxlength' => 6,
    );
    $form['types_colors']['groups'][$group][ADSENSE_COLOR_LINK . $group] = array(
      '#type' => 'textfield',
      '#title' => t('Ad title color'),
      '#default_value' => variable_get(ADSENSE_COLOR_LINK . $group, '336699'),
      '#size' => 6,
      '#maxlength' => 6,
    );
    $form['types_colors']['groups'][$group][ADSENSE_COLOR_URL . $group] = array(
      '#type' => 'textfield',
      '#title' => t('Ad URL color'),
      '#default_value' => variable_get(ADSENSE_COLOR_URL . $group, 'FF8C00'),
      '#size' => 6,
      '#maxlength' => 6,
    );
    $form['types_colors']['groups'][$group][ADSENSE_ALT . $group] = array(
      '#type' => 'select',
      '#title' => t('Alternate URL color'),
      '#default_value' => variable_get(ADSENSE_ALT . $group, 0),
      '#options' => array(
        t('None'),
        t('Alternate URL'),
        t('Alternate color'),
      ),
    );
    $form['types_colors']['groups'][$group][ADSENSE_ALT_INFO . $group] = array(
      '#type' => 'textfield',
      '#title' => t('Alternate info'),
      '#default_value' => variable_get(ADSENSE_ALT_INFO . $group, ''),
      '#size' => 100,
      '#maxlength' => 100,
      '#description' => t('Enter either 6 letter alternate color code, or alternate URL to use'),
    );
  }
  $form['types_colors']['search'] = array(
    '#type' => 'fieldset',
    '#collapsible' => true,
    '#collapsed' => true,
    '#title' => t('AdSense for Search attributes'),
  );
  $form['types_colors']['search'][ADSENSE_SEARCH_COLOR_TEXT] = array(
    '#type' => 'textfield',
    '#title' => t('Text color'),
    '#default_value' => variable_get(ADSENSE_SEARCH_COLOR_TEXT, '000000'),
    '#size' => 6,
    '#maxlength' => 6,
  );
  $form['types_colors']['search'][ADSENSE_SEARCH_COLOR_BORDER] = array(
    '#type' => 'textfield',
    '#title' => t('Border color'),
    '#default_value' => variable_get(ADSENSE_SEARCH_COLOR_BORDER, '336699'),
    '#size' => 6,
    '#maxlength' => 6,
  );
  $form['types_colors']['search'][ADSENSE_SEARCH_COLOR_BG] = array(
    '#type' => 'textfield',
    '#title' => t('Background color'),
    '#default_value' => variable_get(ADSENSE_SEARCH_COLOR_BG, 'FFFFFF'),
    '#size' => 6,
    '#maxlength' => 6,
  );
  $form['types_colors']['search'][ADSENSE_SEARCH_COLOR_TITLE] = array(
    '#type' => 'textfield',
    '#title' => t('Title color'),
    '#default_value' => variable_get(ADSENSE_SEARCH_COLOR_TITLE, '0000FF'),
    '#size' => 6,
    '#maxlength' => 6,
  );
  $form['types_colors']['search'][ADSENSE_SEARCH_COLOR_URL] = array(
    '#type' => 'textfield',
    '#title' => t('URL color'),
    '#default_value' => variable_get(ADSENSE_SEARCH_COLOR_URL, '008000'),
    '#size' => 6,
    '#maxlength' => 6,
  );
  $form['types_colors']['search'][ADSENSE_SEARCH_COLOR_VISITED_URL] = array(
    '#type' => 'textfield',
    '#title' => t('Visited URL color'),
    '#default_value' => variable_get(ADSENSE_SEARCH_COLOR_VISITED_URL, '663399'),
    '#size' => 6,
    '#maxlength' => 6,
  );
  $form['types_colors']['search'][ADSENSE_SEARCH_COLOR_LIGHT_URL] = array(
    '#type' => 'textfield',
    '#title' => t('Light URL color'),
    '#default_value' => variable_get(ADSENSE_SEARCH_COLOR_LIGHT_URL, '0000FF'),
    '#size' => 6,
    '#maxlength' => 6,
  );
  $form['channels'] = array(
    '#type' => 'fieldset',
    '#collapsible' => true,
    '#collapsed' => true,
    '#title' => t('Custom channels'),
    '#description' => t('Enter up to !channels custom channels that you have configured in Google AdSense. If you are not using custom channels, or you are only using URL channels, then leave this empty.', array(
      '!channels' => ADSENSE_MAX_CHANNELS,
    )),
  );
  for ($channel = 1; $channel < ADSENSE_MAX_CHANNELS + 1; $channel++) {
    $form['channels'][ADSENSE_AD_CHANNEL . $channel] = array(
      '#type' => 'textfield',
      '#title' => t('Custom channel ID ') . $channel,
      '#default_value' => variable_get(ADSENSE_AD_CHANNEL . $channel, ''),
      '#size' => 30,
      '#maxlength' => 30,
    );
  }
  $form['revenue'] = array(
    '#type' => 'fieldset',
    '#collapsible' => true,
    '#collapsed' => true,
    '#title' => t('Revenue sharing options'),
  );
  $form['revenue'][ADSENSE_REVENUE_ENABLE] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable revenue sharing'),
    '#return_value' => 1,
    '#default_value' => variable_get(ADSENSE_REVENUE_ENABLE, 0),
    '#description' => t('Note that enabling this will disable AdSense code caching, which can cause more resource usage for busy sites. The referral feature requires the referral module to be installed.'),
  );
  $form['revenue'][ADSENSE_PERCENTAGE_AUTHOR] = array(
    '#type' => 'select',
    '#title' => t('Percentage of node views going to author'),
    '#default_value' => variable_get(ADSENSE_PERCENTAGE_AUTHOR, 0),
    '#options' => drupal_map_assoc(array(
      0,
      5,
      10,
      15,
      20,
      25,
      30,
      40,
      50,
      60,
      70,
      75,
      80,
      90,
      100,
    )),
  );
  if (module_exists('referral')) {
    $list = drupal_map_assoc(array(
      0,
      5,
      10,
      15,
      20,
      25,
      30,
      40,
      50,
      60,
      70,
      75,
      80,
      90,
      100,
    ));
    $form['revenue'][ADSENSE_PERCENTAGE_REFER] = array(
      '#type' => 'select',
      '#title' => t('Percentage of node views going to user who referred the author'),
      '#default_value' => variable_get(ADSENSE_PERCENTAGE_REFER, 0),
      '#options' => $list,
      '#description' => t('This feature requires the referral module to be installed.'),
    );
  }
  $form['revenue']['content'] = array(
    '#type' => 'fieldset',
    '#collapsible' => true,
    '#collapsed' => true,
    '#title' => t('Content types'),
    '#description' => t('Content types that have revenue sharing enabled.'),
  );
  foreach (node_get_types() as $type => $name) {
    $form['revenue']['content'][ADSENSE_NODE_TYPE . $type] = array(
      '#type' => 'checkbox',
      '#title' => $name->name,
      '#return_value' => 1,
      '#default_value' => variable_get(ADSENSE_NODE_TYPE . $type, 0),
    );
  }
  $form['visibility'] = array(
    '#type' => 'fieldset',
    '#collapsible' => true,
    '#collapsed' => true,
    '#title' => t('Visibility'),
  );
  $form['visibility']['adsense_visibility'] = array(
    '#type' => 'radios',
    '#title' => t('Show AdSense on specific pages'),
    '#default_value' => variable_get(ADSENSE_VISIBILITY, 0),
    '#options' => array(
      t('Show on every page except the listed pages.'),
      t('Show on only the listed pages.'),
    ),
  );
  $form['visibility']['adsense_access_pages'] = array(
    '#type' => 'textarea',
    '#title' => t('Pages'),
    '#default_value' => variable_get(ADSENSE_ACCESS_PAGES, ''),
    '#cols' => 70,
    '#rows' => 5,
    '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '<em>blog</em>' for the blog page and '<em>blog/*</em>' for every personal blog. '<em>&lt;front&gt;</em>' is the front page."),
  );
  $form['click_tracking'] = array(
    '#type' => 'fieldset',
    '#collapsible' => true,
    '#collapsed' => true,
    '#title' => t('Click tracking'),
  );
  $form['click_tracking'][ADSENSE_CLICK_TRACKING] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable AdSense click tracking?'),
    '#return_value' => 1,
    '#default_value' => variable_get(ADSENSE_CLICK_TRACKING, 0),
    '#description' => t('Logs IP and time of <a href=!url>AdSense clicks</a>. This can be very useful in tracking which of your pages generate the most clicks, as well as help if click fraud is suspected.', array(
      '!url' => url('admin/logs/adsense'),
    )),
  );
  $form['click_tracking'][ADSENSE_CLICK_TRACKING_NAME_RESOLVE] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable name resolution of IP addresses?'),
    '#return_value' => 1,
    '#default_value' => variable_get(ADSENSE_CLICK_TRACKING_NAME_RESOLVE, 0),
    '#description' => t('Display the domain name instead of the IP address. This can slow down the display of the logs page and tie up a PHP process for several seconds. On some hosting providers it may not be available.'),
  );
  $form['search'] = array(
    '#type' => 'fieldset',
    '#collapsible' => true,
    '#collapsed' => true,
    '#title' => t('AdSense Search'),
  );
  $form['search']['box'] = array(
    '#type' => 'fieldset',
    '#collapsible' => true,
    '#collapsed' => FALSE,
    '#title' => t('Search Box Options'),
    '#description' => t('Allows users to search the web or the specific site(s) of your choice. Enter the site\'s URL without the last \'/\''),
  );
  global $base_url;
  $form['search']['box'][ADSENSE_SEARCH_DOMAIN . '_0'] = array(
    '#type' => 'textfield',
    '#field_prefix' => 'http://',
    '#default_value' => variable_get(str_replace('http://', '', ADSENSE_SEARCH_DOMAIN . '_0'), str_replace('http://', '', $base_url)),
    '#size' => 32,
    '#maxlength' => 255,
  );
  $form['search']['box'][ADSENSE_SEARCH_DOMAIN . '_1'] = array(
    '#type' => 'textfield',
    '#field_prefix' => 'http://',
    '#default_value' => variable_get(str_replace('http://', '', ADSENSE_SEARCH_DOMAIN . '_1'), ''),
    '#size' => 32,
    '#maxlength' => 255,
  );
  $form['search']['box'][ADSENSE_SEARCH_DOMAIN . '_2'] = array(
    '#type' => 'textfield',
    '#field_prefix' => 'http://',
    '#default_value' => variable_get(str_replace('http://', '', ADSENSE_SEARCH_DOMAIN . '_2'), ''),
    '#size' => 32,
    '#maxlength' => 255,
  );
  $form['search']['box'][ADSENSE_SEARCH_LOGO] = array(
    '#type' => 'radios',
    '#title' => t('Logo Type'),
    '#default_value' => variable_get(ADSENSE_SEARCH_LOGO, ADSENSE_SEARCH_LOGO_GOOGLE),
    '#options' => array(
      ADSENSE_SEARCH_LOGO_GOOGLE => t('Google Logo'),
      ADSENSE_SEARCH_LOGO_ABOVE_TEXTBOX => t('Logo above text box'),
      ADSENSE_SEARCH_LOGO_ON_BUTTON => t('"Google Search" on button'),
    ),
  );
  $form['search']['box'][ADSENSE_SEARCH_BUTTON] = array(
    '#type' => 'checkbox',
    '#title' => t('Search button below text box'),
    '#return_value' => 1,
    '#default_value' => variable_get(ADSENSE_SEARCH_BUTTON, 0),
  );
  $box_background_color = variable_get(ADSENSE_SEARCH_COLOR_BOX_BACKGROUND, ADSENSE_SEARCH_COLOR_WHITE);
  $form['search']['box'][ADSENSE_SEARCH_COLOR_BOX_BACKGROUND] = array(
    '#type' => 'select',
    '#title' => t('Background Color'),
    '#default_value' => $box_background_color,
    '#options' => array(
      ADSENSE_SEARCH_COLOR_WHITE => t('White'),
      ADSENSE_SEARCH_COLOR_BLACK => t('Black'),
      ADSENSE_SEARCH_COLOR_GRAY => t('Gray'),
    ),
  );
  $box_text_color = variable_get(ADSENSE_SEARCH_COLOR_BOX_TEXT, ADSENSE_SEARCH_COLOR_BLACK);
  if ($box_background_color == ADSENSE_SEARCH_COLOR_BLACK) {
    $box_text_color = ADSENSE_SEARCH_COLOR_WHITE;
  }
  elseif ($box_background_color == ADSENSE_SEARCH_COLOR_WHITE) {
    $box_text_color = ADSENSE_SEARCH_COLOR_BLACK;
  }
  $form['search']['box'][ADSENSE_SEARCH_COLOR_BOX_TEXT] = array(
    '#type' => 'select',
    '#title' => t('Text Color'),
    '#default_value' => $box_text_color,
    '#options' => array(
      ADSENSE_SEARCH_COLOR_BLACK => t('Black'),
      ADSENSE_SEARCH_COLOR_WHITE => t('White'),
    ),
  );
  $form['search']['result'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Search Results Style'),
  );
  $form['search']['result'][ADSENSE_SEARCH_COUNTRY] = array(
    '#type' => 'select',
    '#title' => t('Country or territory for Google domain'),
    '#default_value' => variable_get(ADSENSE_SEARCH_COUNTRY, 'www.google.com'),
    '#options' => array(
      'www.google.co.uk' => t('United Kingdom'),
      'www.google.ca' => t('Canada'),
      'www.google.de' => t('Germany'),
      'www.google.co.jp' => t('Japan'),
      'www.google.fr' => t('France'),
      'www.google.com.au' => t('Australia'),
      'www.google.it' => t('Italy'),
      'www.google.at' => t('Austria'),
      'www.google.ch' => t('Switzerland'),
      'www.google.es' => t('Spain'),
      'www.google.nl' => t('Netherlands'),
      'www.google.be' => t('Belgium'),
      'www.google.dk' => t('Denmark'),
      'www.google.se' => t('Sweden'),
      'www.google.no' => t('Norway'),
      'www.google.com.br' => t('Brazil'),
      'www.google.pt' => t('Portugal'),
      'www.google.fi' => t('Finland'),
      'www.google.co.kr' => t('South Korea'),
      'www.google.cn' => t('China'),
      'www.google.com.hk' => t('Hong Kong'),
      'www.google.com.tw' => t('Taiwan'),
      'www.google.com.sg' => t('Singapore'),
      'www.google.com.af' => t('Afghanistan'),
      'www.google.com' => t('Albania'),
      'www.google.com' => t('Algeria'),
      'www.google.as' => t('American Samoa'),
      'www.google.com' => t('Andorra'),
      'www.google.com' => t('Angola'),
      'www.google.off.ai' => t('Anguilla'),
      'www.google.com' => t('Antarctica'),
      'www.google.com.ag' => t('Antigua and Barbuda'),
      'www.google.com.ar' => t('Argentina'),
      'www.google.am' => t('Armenia'),
      'www.google.com' => t('Aruba'),
      'www.google.az' => t('Azerbaijan'),
      'www.google.bs' => t('Bahamas'),
      'www.google.com.bh' => t('Bahrain'),
      'www.google.com.bd' => t('Bangladesh'),
      'www.google.com' => t('Barbados'),
      'www.google.com' => t('Belarus'),
      'www.google.com.bz' => t('Belize'),
      'www.google.com' => t('Benin'),
      'www.google.com' => t('Bermuda'),
      'www.google.com' => t('Bhutan'),
      'www.google.com.bo' => t('Bolivia'),
      'www.google.ba' => t('Bosnia and Herzegovina'),
      'www.google.co.bw' => t('Botswana'),
      'www.google.com' => t('Bouvet Island'),
      'www.google.com' => t('British Indian Ocean Territory'),
      'www.google.com.bn' => t('Brunei Darussalam'),
      'www.google.bg' => t('Bulgaria'),
      'www.google.com' => t('Burkina Faso'),
      'www.google.bi' => t('Burundi'),
      'www.google.com.kh' => t('Cambodia'),
      'www.google.com' => t('Cameroon'),
      'www.google.com' => t('Cape Verde'),
      'www.google.com' => t('Cayman Islands'),
      'www.google.com' => t('Central African Republic'),
      'www.google.com' => t('Chad'),
      'www.google.cl' => t('Chile'),
      'www.google.com' => t('Christmas Island'),
      'www.google.com' => t('Cocos (Keeling) Islands'),
      'www.google.com.co' => t('Colombia'),
      'www.google.com' => t('Comoros'),
      'www.google.cg' => t('Congo'),
      'www.google.cd' => t('Congo, Democratic Republic'),
      'www.google.co.ck' => t('Cook Islands'),
      'www.google.co.cr' => t('Costa Rica'),
      'www.google.ci' => t('Cote d&#39;Ivoire'),
      'www.google.hr' => t('Croatia'),
      'www.google.com.cu' => t('Cuba'),
      'www.google.com' => t('Cyprus'),
      'www.google.cz' => t('Czech Republic'),
      'www.google.dj' => t('Djibouti'),
      'www.google.dm' => t('Dominica'),
      'www.google.com.do' => t('Dominican Republic'),
      'www.google.tp' => t('East Timor'),
      'www.google.com.ec' => t('Ecuador'),
      'www.google.com.eg' => t('Egypt'),
      'www.google.com.sv' => t('El Salvador'),
      'www.google.com' => t('Equatorial Guinea'),
      'www.google.com' => t('Eritrea'),
      'www.google.ee' => t('Estonia'),
      'www.google.com.et' => t('Ethiopia'),
      'www.google.com' => t('Falkland Islands (Malvinas)'),
      'www.google.com' => t('Faroe Islands'),
      'www.google.com.fj' => t('Fiji'),
      'www.google.com' => t('French Guiana'),
      'www.google.com' => t('French Polynesia'),
      'www.google.com' => t('French Southern Territories'),
      'www.google.com' => t('Gabon'),
      'www.google.gm' => t('Gambia'),
      'www.google.ge' => t('Georgia'),
      'www.google.com' => t('Ghana'),
      'www.google.com.gi' => t('Gibraltar'),
      'www.google.gr' => t('Greece'),
      'www.google.gl' => t('Greenland'),
      'www.google.com' => t('Grenada'),
      'www.google.gp' => t('Guadeloupe'),
      'www.google.com' => t('Guam'),
      'www.google.com.gt' => t('Guatemala'),
      'www.google.com' => t('Guinea'),
      'www.google.com' => t('Guinea-Bissau'),
      'www.google.gy' => t('Guyana'),
      'www.google.ht' => t('Haiti'),
      'www.google.com' => t('Heard and McDonald Islands'),
      'www.google.hn' => t('Honduras'),
      'www.google.hu' => t('Hungary'),
      'www.google.is' => t('Iceland'),
      'www.google.co.in' => t('India'),
      'www.google.co.id' => t('Indonesia'),
      'www.google.com' => t('Iran'),
      'www.google.com' => t('Iraq'),
      'www.google.ie' => t('Ireland'),
      'www.google.co.il' => t('Israel'),
      'www.google.com.jm' => t('Jamaica'),
      'www.google.jo' => t('Jordan'),
      'www.google.kz' => t('Kazakhstan'),
      'www.google.co.ke' => t('Kenya'),
      'www.google.ki' => t('Kiribati'),
      'www.google.com' => t('Kuwait'),
      'www.google.kg' => t('Kyrgyzstan'),
      'www.google.la' => t('Lao People&#39;s Democratic Republic'),
      'www.google.lv' => t('Latvia'),
      'www.google.com' => t('Lebanon'),
      'www.google.co.ls' => t('Lesotho'),
      'www.google.com' => t('Liberia'),
      'www.google.com.ly' => t('Libya'),
      'www.google.li' => t('Liechtenstein'),
      'www.google.lt' => t('Lithuania'),
      'www.google.lu' => t('Luxembourg'),
      'www.google.com' => t('Macau'),
      'www.google.com' => t('Macedonia'),
      'www.google.com' => t('Madagascar'),
      'www.google.mw' => t('Malawi'),
      'www.google.com.my' => t('Malaysia'),
      'www.google.mv' => t('Maldives'),
      'www.google.com' => t('Mali'),
      'www.google.com.mt' => t('Malta'),
      'www.google.com' => t('Marshall Islands'),
      'www.google.com' => t('Martinique'),
      'www.google.com' => t('Mauritania'),
      'www.google.mu' => t('Mauritius'),
      'www.google.com' => t('Mayotte'),
      'www.google.com.mx' => t('Mexico'),
      'www.google.fm' => t('Micronesia'),
      'www.google.md' => t('Moldova'),
      'www.google.com' => t('Monaco'),
      'www.google.mn' => t('Mongolia'),
      'www.google.ms' => t('Montserrat'),
      'www.google.co.ma' => t('Morocco'),
      'www.google.com' => t('Mozambique'),
      'www.google.com' => t('Myanmar'),
      'www.google.com.na' => t('Namibia'),
      'www.google.nr' => t('Nauru'),
      'www.google.com.np' => t('Nepal'),
      'www.google.com' => t('Netherlands Antilles'),
      'www.google.com' => t('New Caledonia'),
      'www.google.co.nz' => t('New Zealand'),
      'www.google.com.ni' => t('Nicaragua'),
      'www.google.com' => t('Niger'),
      'www.google.com.ng' => t('Nigeria'),
      'www.google.nu' => t('Niue'),
      'www.google.com.nf' => t('Norfolk Island'),
      'www.google.com' => t('Northern Mariana Islands'),
      'www.google.com' => t('North Korea'),
      'www.google.com.om' => t('Oman'),
      'www.google.com.pk' => t('Pakistan'),
      'www.google.com' => t('Palau'),
      'www.google.com' => t('Palestinian Territory'),
      'www.google.com.pa' => t('Panama'),
      'www.google.com' => t('Papua New Guinea'),
      'www.google.com.py' => t('Paraguay'),
      'www.google.com.pe' => t('Peru'),
      'www.google.com.ph' => t('Philippines'),
      'www.google.pn' => t('Pitcairn'),
      'www.google.pl' => t('Poland'),
      'www.google.com.pr' => t('Puerto Rico'),
      'www.google.com.qa' => t('Qatar'),
      'www.google.com' => t('Reunion'),
      'www.google.ro' => t('Romania'),
      'www.google.ru' => t('Russian Federation'),
      'www.google.rw' => t('Rwanda'),
      'www.google.com' => t('Saint Kitts and Nevis'),
      'www.google.com' => t('Saint Lucia'),
      'www.google.com.vc' => t('Saint Vincent and the Grenadines'),
      'www.google.ws' => t('Samoa'),
      'www.google.sm' => t('San Marino'),
      'www.google.st' => t('Sao Tome and Principe'),
      'www.google.com.sa' => t('Saudi Arabia'),
      'www.google.sn' => t('Senegal'),
      'www.google.com' => t('Serbia and Montenegro'),
      'www.google.sc' => t('Seychelles'),
      'www.google.com' => t('Sierra Leone'),
      'www.google.sk' => t('Slovakia'),
      'www.google.si' => t('Slovenia'),
      'www.google.com.sb' => t('Solomon Islands'),
      'www.google.com' => t('Somalia'),
      'www.google.co.za' => t('South Africa'),
      'www.google.com' => t('South Georgia and The South Sandwich Islands'),
      'www.google.lk' => t('Sri Lanka'),
      'www.google.sh' => t('St. Helena'),
      'www.google.com' => t('St. Pierre and Miquelon'),
      'www.google.com' => t('Sudan'),
      'www.google.com' => t('Suriname'),
      'www.google.com' => t('Svalbard and Jan Mayen Islands'),
      'www.google.com' => t('Swaziland'),
      'www.google.com' => t('Syria'),
      'www.google.com.tj' => t('Tajikistan'),
      'www.google.com' => t('Tanzania'),
      'www.google.co.th' => t('Thailand'),
      'www.google.com' => t('Togo'),
      'www.google.tk' => t('Tokelau'),
      'www.google.to' => t('Tonga'),
      'www.google.tt' => t('Trinidad and Tobago'),
      'www.google.com' => t('Tunisia'),
      'www.google.com.tr' => t('Turkey'),
      'www.google.tm' => t('Turkmenistan'),
      'www.google.com' => t('Turks and Caicos Islands'),
      'www.google.com' => t('Tuvalu'),
      'www.google.co.ug' => t('Uganda'),
      'www.google.com.ua' => t('Ukraine'),
      'www.google.ae' => t('United Arab Emirates'),
      'www.google.com' => t('United States Minor Outlying Islands'),
      'www.google.com.uy' => t('Uruguay'),
      'www.google.co.uz' => t('Uzbekistan'),
      'www.google.vu' => t('Vanuatu'),
      'www.google.com' => t('Vatican'),
      'www.google.co.ve' => t('Venezuela'),
      'www.google.com.vn' => t('Viet Nam'),
      'www.google.vg' => t('Virgin Islands (British)'),
      'www.google.co.vi' => t('Virgin Islands (U.S.)'),
      'www.google.com' => t('Wallis and Futuna Islands'),
      'www.google.com' => t('Western Sahara'),
      'www.google.com' => t('Yemen'),
      'www.google.co.zm' => t('Zambia'),
      'www.google.co.zw' => t('Zimbabwe'),
      'www.google.com' => t('United States'),
    ),
  );
  $form['search']['box'][ADSENSE_SEARCH_LANGUAGE] = array(
    '#type' => 'select',
    '#title' => t('Site Language'),
    '#default_value' => variable_get(ADSENSE_SEARCH_LANGUAGE, 'en'),
    '#options' => array(
      'ar' => t('Arabic'),
      'bg' => t('Bulgarian'),
      'zh-CN' => t('Chinese (simplified)'),
      'zh-TW' => t('Chinese (traditional)'),
      'hr' => t('Croatian'),
      'cs' => t('Czech'),
      'da' => t('Danish'),
      'nl' => t('Dutch'),
      'en' => t('English'),
      'fi' => t('Finnish'),
      'fr' => t('French'),
      'de' => t('German'),
      'el' => t('Greek'),
      'iw' => t('Hebrew'),
      'hu' => t('Hungarian'),
      'in' => t('Indonesian'),
      'it' => t('Italian'),
      'ja' => t('Japanese'),
      'ko' => t('Korean'),
      'no' => t('Norwegian'),
      'pl' => t('Polish'),
      'pt' => t('Portuguese'),
      'ro' => t('Romanian'),
      'ru' => t('Russian'),
      'sk' => t('Slovak'),
      'es' => t('Spanish'),
      'sv' => t('Swedish'),
      'tr' => t('Turkish'),
    ),
  );
  $form['search']['box'][ADSENSE_SEARCH_ENCODING] = array(
    '#type' => 'select',
    '#title' => t('Site Encoding'),
    '#default_value' => variable_get(ADSENSE_SEARCH_ENCODING, 'UTF-8'),
    '#options' => array(
      'ISO-8859-1' => t('West European Latin-1 (ISO-8859-1)'),
      'ISO-8859-15' => t('West European Latin-9 (ISO-8859-15)'),
      'windows-1252' => t('Western (Windows-1252)'),
      'ISO-8859-10' => t('Nordic Latin-6 (ISO-8859-10)'),
      'ISO-8859-7' => t('Greek (ISO-8859-7)'),
      'Shift_JIS' => t('Japanese (Shift_JIS)'),
      'EUC-JP' => t('Japanese (EUC-JP)'),
      'ISO-2022-JP' => t('Japanese (ISO-2022-JP)'),
      'GB2312' => t('Chinese Simplified (GB2312)'),
      'GB18030' => t('Chinese Simplified (GB18030)'),
      'big5' => t('Chinese Traditional (Big5)'),
      'EUC-KR' => t('Korean (EUC-KR)'),
      'windows-874' => t('Thai (Windows-874)'),
      'windows-1258' => t('Vietnamese (Windows-1258)'),
      'ISO-8859-2' => t('Central European Latin-2 (ISO-8859-2)'),
      'windows-1250' => t('Central European (Windows-1250)'),
      'cp852' => t('Central European (CP852)'),
      'ISO-8859-9' => t('Turkish Latin-5 (ISO-8859-9)'),
      'windows-1254' => t('Turkish (Windows-1254)'),
      'ISO-8859-3' => t('South European Latin-3 (ISO-8859-3)'),
      'ISO-8859-8-I' => t('Hebrew (ISO-8859-8-I)'),
      'windows-1255' => t('Hebrew (Windows-1255)'),
      'windows-1256' => t('Arabic (Windows-1256)'),
      'ISO-8859-5' => t('Cyrillic (ISO-8859-5)'),
      'KOI8-R' => t('Cyrillic (KOI8-R)'),
      'windows-1251' => t('Cyrillic (Windows-1251)'),
      'cp-866' => t('Cyrillic/Russian (CP-866)'),
      'UTF-8' => t('Unicode (UTF-8)'),
    ),
  );
  $textbox_length = $textbox_length > 64 ? 64 : variable_get(ADSENSE_SEARCH_TEXTBOX_LENGTH, 15);
  $form['search']['box'][ADSENSE_SEARCH_TEXTBOX_LENGTH] = array(
    '#type' => 'textfield',
    '#title' => t('Length of text box (Max 64): '),
    '#default_value' => $textbox_length,
    '#size' => 2,
    '#maxlength' => 2,
  );
  $form['search']['result'][ADSENSE_SEARCH_FRAME_WIDTH] = array(
    '#type' => 'textfield',
    '#title' => t('Width of results area'),
    '#default_value' => variable_get(ADSENSE_SEARCH_FRAME_WIDTH, 500),
    '#size' => 3,
    '#maxlength' => 3,
  );
  $form['advanced'] = array(
    '#type' => 'fieldset',
    '#collapsible' => true,
    '#collapsed' => true,
    '#title' => t('Advanced options'),
  );
  $form['advanced'][ADSENSE_TEST_MODE] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable test mode?'),
    '#return_value' => 1,
    '#default_value' => variable_get(ADSENSE_TEST_MODE, 0),
    '#description' => t('This enables you to test the AdSense module settings. This can be useful in some situations: for example, testing whether revenue sharing is working properly or not without having to display real ads on your site. It is best to test this after you log out.'),
  );
  $form['advanced'][ADSENSE_DISABLE] = array(
    '#type' => 'checkbox',
    '#title' => t('Disable Google AdSense ads?'),
    '#return_value' => 1,
    '#default_value' => variable_get(ADSENSE_DISABLE, 0),
    '#description' => t('This disables all display of Google AdSense ads from your web site. This is useful in certain situations, such as site upgrades, or if you make a copy of the site for development and test purposes.'),
  );
  $form['advanced'][ADSENSE_PLACEHOLDER] = array(
    '#type' => 'checkbox',
    '#title' => t('Placeholder when ads are disabled?'),
    '#return_value' => 1,
    '#default_value' => variable_get(ADSENSE_PLACEHOLDER, 1),
    '#description' => t('This causes an empty box to be displayed in place of the ads when they are disabled.'),
  );
  $form['advanced'][ADSENSE_PLACEHOLDER_TEXT] = array(
    '#type' => 'textarea',
    '#title' => t('Placeholder text to display'),
    '#default_value' => variable_get(ADSENSE_PLACEHOLDER_TEXT, t('Google AdSense')),
    '#cols' => 70,
    '#rows' => 3,
    '#description' => t('Enter any text to display as a placeholder when ads are disabled.'),
  );
  $form['advanced'][ADSENSE_SECTION_TARGETING] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable AdSense section targeting?'),
    '#return_value' => 1,
    '#default_value' => variable_get(ADSENSE_SECTION_TARGETING, 1),
    '#description' => t('This better targets Google ads to the teaser and body of nodes.'),
  );
  return system_settings_form($form);
}
function adsense_display($format = '160x600', $group = 1, $channel = 1) {
  if (!_adsense_validate_dimensions($format)) {
    $ad = '<!--adsense: invalid format: ' . $format . '-->';
  }
  else {

    // This ad format is not cached, generate it
    // Check first if disabled or if we are at adsense limit or if this page doesn't allow adsense
    if (_adsense_check_if_enabled()) {
      if (_adsense_can_insert_another($format)) {
        if (_adsense_page_match()) {

          // Ad should be displayed
          // Check cache first
          $ad = $format != ADSENSE_SEARCH_BOX ? _adsense_get_ad($format, $group, $channel) : _adsense_get_searchbox($group, $channel);
        }
        else {
          $ad = '<!--adsense: page not in match list-->';
        }
      }
      else {
        $ad = '<!--adsense: ad limit reached for type-->';
      }
    }
    else {

      // Ads are disabled
      if (_adsense_page_match()) {
        if (_adsense_check_placeholder()) {
          $ad = '<!--adsense: placeholder-->' . _adsense_format_placeholder($format);
        }
        else {
          $ad = '<!--adsense: ads disabled -->';
        }
      }
    }
  }
  return $ad;
}
function _adsense_get_ad($format, $group, $channel) {
  $sharing = variable_get(ADSENSE_REVENUE_ENABLE, 0);
  if ($sharing) {
    $ad = _adsense_format($format, $group, $channel);
    return $ad;
  }
  $cache_key = "adsense-{$group}-{$format}-{$channel}";
  $cache = cache_get($cache_key);
  if ($cache) {

    // This ad format is cached, use it
    $ad = '<!--adsense: cached-->' . $cache->data;
  }
  else {

    // Not cached. Format an ad
    $ad = _adsense_format($format, $group, $channel);

    // Add it to cache
    cache_set($cache_key, 'cache', $ad, CACHE_PERMANENT);
  }
  return $ad;
}
function _adsense_check_if_enabled() {
  $client_id = adsense_get_client_id();
  $disable_ads = variable_get(ADSENSE_DISABLE, '0');
  if (!$client_id) {

    // Google AdSense Client ID is not configured
    return FALSE;
  }
  if ($disable_ads) {
    return FALSE;
  }
  if (variable_get(ADSENSE_TEST_MODE, 0)) {
    return TRUE;
  }
  if (user_access('hide adsense')) {
    return FALSE;
  }
  return TRUE;
}
function _adsense_check_placeholder() {
  if (variable_get(ADSENSE_PLACEHOLDER, '1')) {

    // Ads are globally disabled and a place holder is set
    return true;
  }
  else {
    return false;
  }
}
function _adsense_format_placeholder($format) {
  $width = _adsense_get_width($format);
  $height = _adsense_get_height($format);
  $placeholder_text = variable_get(ADSENSE_PLACEHOLDER_TEXT, t('Google AdSense'));
  $output = '<div style="';
  $output .= ' width:' . $width . 'px;';
  $output .= ' height:' . $height . 'px;';
  $output .= ' border:solid 1px">' . $placeholder_text . '</div>';
  return $output;
}
function _adsense_get_dimensions($format) {
  list($width, $height) = explode('x', $format);
  return array(
    'width' => $width,
    'height' => $height,
  );
}
function _adsense_get_height($format) {
  $dims = _adsense_get_dimensions($format);
  return $dims['height'];
}
function _adsense_get_width($format) {
  $dims = _adsense_get_dimensions($format);
  return $dims['width'];
}
function _adsense_validate_dimensions($format) {
  $all_ads = adsense_ad_formats();
  $ret = false;
  if ($all_ads[$format]['desc']) {
    $ret = true;
  }
  else {
    $ret = false;
  }
  return $ret;
}
function _adsense_validate_group($group = 1) {
  if ($group < 1 || $group > ADSENSE_MAX_GROUPS) {

    // Default to 1 if an invalid group is supplied
    return 1;
  }
  return $group;
}
function _adsense_validate_channel($channel = 1) {
  if ($channel < 1 || $channel > ADSENSE_MAX_CHANNELS) {

    // Default to 1 if an invalid channel is supplied
    return 1;
  }
  return $channel;
}
function _adsense_format($format, $group = 1, $channel = 1) {
  $group = _adsense_validate_group($group);
  $client = adsense_get_client_id();
  $text = variable_get(ADSENSE_COLOR_TEXT . $group, '000000');
  $border = variable_get(ADSENSE_COLOR_BORDER . $group, '336699');
  $bg = variable_get(ADSENSE_COLOR_BG . $group, 'FFFFFF');
  $link = variable_get(ADSENSE_COLOR_LINK . $group, '336699');
  $url = variable_get(ADSENSE_COLOR_URL . $group, 'FF8C00');
  $alt = variable_get(ADSENSE_ALT . $group, 0);
  $alt_info = variable_get(ADSENSE_ALT_INFO . $group, '');
  $channel = variable_get(ADSENSE_AD_CHANNEL . _adsense_validate_channel($channel), '');
  switch (variable_get(ADSENSE_AD_TYPE . $group, '0')) {
    case 2:
      $type = 'text_image';
      break;
    case 1:
      $type = 'image';
      break;
    default:
      $type = 'text';
      break;
  }
  $width = _adsense_get_width($format);
  $height = _adsense_get_height($format);
  $format = $width . 'x' . $height . adsense_get_ad_code($format);
  $output .= _adsense_add_nl('');
  $output .= _adsense_add_nl('<div class="adsense">');
  $output .= _adsense_add_nl('<script type="text/javascript"><!--');
  switch ($alt) {
    case 0:

      // disabled
      break;
    case 1:
      $output .= _adsense_add_nl('google_alternate_ad_url = "' . $alt_info . '";');
      break;
    case 2:
      $output .= _adsense_add_nl('google_alternate_color = "' . $alt_info . '";');
      break;
  }
  $output .= _adsense_add_nl('google_ad_client = "' . $client . '";');
  $output .= _adsense_add_nl('google_ad_type = "' . $type . '";');
  $output .= _adsense_add_nl('google_ad_channel = "' . $channel . '";');
  $output .= _adsense_add_nl('google_ad_width = ' . $width . ';');
  $output .= _adsense_add_nl('google_ad_height = ' . $height . ';');
  $output .= _adsense_add_nl('google_ad_format = "' . $format . '";');
  $output .= _adsense_add_nl('google_color_border = "' . $border . '";');
  $output .= _adsense_add_nl('google_color_bg = "' . $bg . '";');
  $output .= _adsense_add_nl('google_color_link = "' . $link . '";');
  $output .= _adsense_add_nl('google_color_url = "' . $url . '";');
  $output .= _adsense_add_nl('google_color_text = "' . $text . '";');
  $output .= _adsense_add_nl('//--></script>');
  $output .= _adsense_add_nl('<script type="text/javascript"');
  $output .= _adsense_add_nl(' src="http://pagead2.googlesyndication.com/pagead/show_ads.js">');
  $output .= _adsense_add_nl('</script>');
  $output .= _adsense_add_nl('</div>');
  if (variable_get(ADSENSE_TEST_MODE, 0)) {
    $text = "client={$client} format={$format} group={$group} channel={$channel} type={$type}";
    $output = '<div style="';
    $output .= ' width:' . $width . 'px;';
    $output .= ' height:' . $height . 'px;';
    $output .= ' border:solid 1px">' . $text . '</div>';
  }
  return $output;
}
function _adsense_add_nl($str) {
  return $str . "\n";
}

/**
 * Determine if AdSense has reached limit on this page. As per Google's
 * policies, a page can have up to 3 ad units and 3 link units.
 *
 * @return
 *   TRUE if we can insert another ad, FALSE if not allowed.
 */
function _adsense_can_insert_another($format = '160x600') {
  static $num_ads = array(
    ADSENSE_TYPE_AD => 0,
    ADSENSE_TYPE_LINK => 0,
    ADSENSE_TYPE_SEARCH => 0,
  );
  $max_ads = array(
    ADSENSE_TYPE_AD => 3,
    ADSENSE_TYPE_LINK => 3,
    ADSENSE_TYPE_SEARCH => 2,
  );
  $ad_list = adsense_ad_formats();
  $type = $ad_list[$format]['type'];
  if ($num_ads[$type] < $max_ads[$type]) {
    $num_ads[$type]++;
    $ret = TRUE;
  }
  else {
    $ret = FALSE;
  }
  return $ret;
}

/**
 * Determine if AdSense has permission to be used on the current page.
 *
 * @return
 *   TRUE if can render, FALSE if not allowed.
 */
function _adsense_page_match() {
  $page_match = FALSE;
  $visibility = (int) variable_get(ADSENSE_VISIBILITY, '0');
  $pages = variable_get(ADSENSE_ACCESS_PAGES, '');
  if ($pages) {

    // Specific pages are configured
    $path = drupal_get_path_alias($_GET['q']);
    $regexp = '/^(' . preg_replace(array(
      '/(\\r\\n?|\\n)/',
      '/\\\\\\*/',
      '/(^|\\|)\\\\<front\\\\>($|\\|)/',
    ), array(
      '|',
      '.*',
      '\\1' . variable_get('site_frontpage', 'node') . '\\2',
    ), preg_quote($pages, '/')) . ')$/';
    $page_match = !($visibility xor preg_match($regexp, $path));
  }
  else {

    // No pages are configured
    if ($visibility === 0) {

      // We are set to "Show on every pages except..."
      $page_match = TRUE;
    }
  }

  // Do not show ads on secure pages.
  // This is for two reasons:
  // Google would most probably not have indexed secure pages
  // and it also prevents warnings about mixed-content
  // Thanks to Brad Konia http://drupal.org/node/29585
  // Should be restricted when running on Apache only
  if (function_exists('apache_get_version')) {
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
      $page_match = FALSE;
    }
  }
  return $page_match;
}

/**
 * Implementation of hook_filter().
 */
function adsense_filter($op, $delta = 0, $format = -1, $text = '') {
  switch ($op) {
    case 'no cache':
      return TRUE;
    case 'list':
      return array(
        0 => t('AdSense tag'),
      );
    case 'description':
      return t('Substitutes an AdSense special tag with an ad.');
    case 'prepare':
      return $text;
    case 'process':
      return _adsense_process_tags($text);
  }
}
function _adsense_process_tags($text) {
  $patterns = array(
    'flexi' => '/\\[adsense:flexiblock:(\\d+)\\]/x',
    'block' => '/\\[adsense:block:(\\d+)\\]/x',
    'tags' => '/\\[adsense:(\\w+):(\\d+):(\\d+)\\]/x',
  );
  foreach ($patterns as $mode => $pattern) {
    if (preg_match_all($pattern, $text, $matches, PREG_SET_ORDER)) {
      foreach ($matches as $match) {
        switch ($mode) {
          case 'flexi':
            $ad = _adsense_tag_flexiblock($match[1]);
            break;
          case 'block':
            $ad = _adsense_tag_block($match[1]);
            break;
          case 'tags':

            // Tag is in [adsense:468x60:1:1] format
            $ad = adsense_display($match[1], $match[2], $match[3]);
        }

        // Replace the first occurance of the tag, in case we have the same
        // tag more than once.
        $str = '/\\' . $match[0] . '/';
        $text = preg_replace($str, $ad, $text, 1);
      }
    }
  }
  return $text;
}
function _adsense_tag_block($bid) {

  // Tag is in [adsense:block:1] format
  $block = block_block('view', $bid);
  return $block['content'];
}
function _adsense_tag_flexiblock($location) {
  if (module_exists('flexiblock')) {

    // Tag is in [adsense:flexiblock:1] format
    $blocks = theme('flexiblock_blocks', $location);
    foreach ($blocks as $block) {
      $ad = $block['content'];
    }
  }
  else {
    $ad = '<!--adsense: flexiblock not installed -->';
  }
  return $ad;
}

/**
 * Implementation of hook_block().
 */
function adsense_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      for ($count = 0; $count < ADSENSE_MAX_BLOCKS; $count++) {
        if ($ad = _adsense_get_block_config($count)) {
          $title = t('AdSense: ') . $ad[0];
        }
        else {
          $title = t('AdSense: unconfigured ') . $count;
        }
        $block[$count]['info'] = $title;
      }
      break;
    case 'configure':
      $ad = _adsense_get_block_config($delta);
      $ad_list[] = t('None');
      foreach (adsense_ad_formats() as $format => $data) {
        $ad_list[$format] = $format . ' : ' . $data['desc'];
      }
      for ($group = 1; $group < ADSENSE_MAX_GROUPS + 1; $group++) {
        $group_list[$group] = 'Group ' . $group;
      }
      $group_list[$group] = ADSENSE_SEARCH_BOX;
      for ($channel = 1; $channel < ADSENSE_MAX_CHANNELS + 1; $channel++) {
        $channel_list[$channel] = 'Channel ' . $channel;
      }
      $form['ad_format'] = array(
        '#type' => 'select',
        '#title' => t('Ad format'),
        '#default_value' => $ad ? $ad[0] : 1,
        '#options' => $ad_list,
      );
      $form['ad_group'] = array(
        '#type' => 'select',
        '#title' => t('Group'),
        '#default_value' => $ad ? $ad[1] : 1,
        '#options' => $group_list,
      );
      $form['ad_channel'] = array(
        '#type' => 'select',
        '#title' => t('Channel'),
        '#default_value' => $ad ? $ad[2] : 1,
        '#options' => $channel_list,
      );
      return $form;
    case 'save':
      $data = implode(':', array(
        $edit['ad_format'],
        $edit['ad_group'],
        $edit['ad_channel'],
      ));
      variable_set(ADSENSE_AD_BLOCK . $delta, $data);
      return;
    case 'view':
      $ad = _adsense_get_block_config($delta);
      $block['subject'] = '';
      $block['content'] = $ad ? adsense_display($ad[0], $ad[1], $ad[2]) : t('AdSense unconfigured block. <a href=!url>Click to configure.</a>', array(
        '!url' => url('admin/build/block/configure/adsense/' . $delta),
      ));
      break;
  }
  return $block;
}
function _adsense_get_block_config($delta = 0) {
  if ($data = variable_get(ADSENSE_AD_BLOCK . $delta, '')) {
    return explode(':', $data);
  }
  return false;
}
function adsense_filter_tips($delta, $format, $long = false) {
  return t('Use the special tag [adsense:<em>format</em>:<em>group</em>:<em>channel</em>] or [adsense:flexiblock:<em>location</em>] to display Google AdSense ads.');
}

/**
 * Implemenation of hook_nodeapi().
 */
function adsense_nodeapi(&$node, $op = 'view', $teaser, $page) {
  switch ($op) {
    case 'view':
      if (variable_get(ADSENSE_SECTION_TARGETING, 1)) {
        $node->content['adsense_start'] = array(
          '#value' => ADSENSE_SECTION_START,
          '#weight' => -1,
        );
        $node->content['adsense_end'] = array(
          '#value' => ADSENSE_SECTION_END,
          '#weight' => 1,
        );
      }
      if ($page) {
        adsense_get_client_id();
      }
      else {
        if ($uid = _adsense_check_blog()) {
          adsense_get_client_id();
        }
      }
  }
}
function _adsense_check_blog() {

  // Special handling for the path /blog/uid or its aliases
  $path = explode('/', drupal_get_normal_path($_GET['q']));
  if (is_array($path)) {
    if (count($path) == 2) {
      if ($path[0] == 'blog') {
        if (is_numeric($path[1])) {
          return $path[1];
        }
      }
    }
  }
  return false;
}
function adsense_get_client_id() {
  static $client = false;
  if ($client) {

    // Use the cached client id
    return $client;
  }

  // Get the site wide client ID
  $site_client = _adsense_get_site_owner_client_id();

  // Check if revenue sharing is enabled
  $revenue = variable_get(ADSENSE_REVENUE_ENABLE, 0);
  if (!$revenue) {

    // If not enabled, cache the site wide client ID
    return $site_client;
  }

  // Revenue sharing is enabled, need to now the node
  $path = explode('/', drupal_get_normal_path($_GET['q']));
  if (is_array($path) && $path[0] == 'node' && is_numeric($path[1])) {
    $node = node_load($path[1]);
  }
  else {
    return $site_client;
  }

  // Check the content type we are displaying
  $content = variable_get(ADSENSE_NODE_TYPE . $node->type, 0);
  if (!$content) {

    // Revenue sharing is disabled for this content, use the site wide client id
    $client = $site_client;
    return $client;
  }

  // Check the percentage and choose one
  $client = _adsense_choose_client($node->uid);
  return $client;
}
function _adsense_get_referral_uid($uid) {
  if (!module_exists('referral')) {
    return $uid;
  }
  $referral_uid = referral_get_user($uid);
  if (!$referral_uid) {

    // User was not referred by an existing user
    return false;
  }
  return $referral_uid;
}
function _adsense_choose_client($uid) {
  $revenue_enable = variable_get(ADSENSE_REVENUE_ENABLE, 0);
  $percent_author = variable_get(ADSENSE_PERCENTAGE_AUTHOR, 0);
  $percent_referral = variable_get(ADSENSE_PERCENTAGE_REFER, 0);
  $site_client = _adsense_get_site_owner_client_id();
  $author_client = _adsense_get_profile_client_id($uid);
  $referral_client = _adsense_get_profile_client_id(_adsense_get_referral_uid($uid));
  if (!$revenue_enable) {
    return $site_client;
  }
  if (!$percent_author || !$author_client) {
    return $site_client;
  }

  // Toss the dice and see who gets their ad displayed
  $random = mt_rand(1, 100);
  if ($random <= $percent_author) {
    $client = $author_client;
  }
  elseif ($random <= $percent_author + $percent_referral) {
    $client = $referral_client;
  }
  else {
    $client = $site_client;
  }

  // Last check to see that we have a valid client
  if (!$client) {
    $client = $site_client;
  }
  return $client;
}
function _adsense_get_site_owner_client_id() {

  // TODO
  // This should be expanded in the future to allow multiple site owners
  // identified by sharing a specific role. The percentage for each user
  // should be stored somewhere (profile?)
  // For the meantime, the first Drupal user is the owner of the site.
  return _adsense_get_profile_client_id(1);
}
function _adsense_get_profile_client_id($uid = 0) {
  $client_id = '';
  if (module_exists('profile')) {

    // Get the profile field for a certain user
    $profile_field = variable_get(ADSENSE_CLIENT_ID_PROFILE_FIELD, 0);
    $client_id = db_result(db_query('SELECT value FROM {profile_values}
      WHERE fid = %d AND uid = %d', $profile_field, $uid));
  }
  return $client_id;
}
function _adsense_get_profile_fields() {
  $profile = array(
    0 => 'None',
  );
  $result = db_query("SELECT fid, title FROM {profile_fields} ORDER BY fid");
  while ($row = db_fetch_object($result)) {
    $profile[$row->fid] = $row->title;
  }
  return $profile;
}

// AdSense click tracking functions

/**
 * Implementation of hook_footer().
 */
function adsense_footer($main = 0) {
  global $base_url;
  if (variable_get(ADSENSE_CLICK_TRACKING, 0)) {
    $path = base_path() . drupal_get_path('module', 'adsense') . '/adsense_click.js';
    $js = '<script type="text/javascript" src="' . $path . '"></script>';
    return $js;
  }
}
function adsense_counter() {
  if (variable_get(ADSENSE_CLICK_TRACKING, 0)) {
    $path = $_GET['u'];
    db_query("INSERT INTO {adsense_clicks} (ip, timestamp, path) values('%s', %d, '%s')", $_SERVER['REMOTE_ADDR'], time(), $path);
  }
}

/**
 * Menu callback; presents the "AdSense" log page.
 */
function adsense_click_log() {
  $header = array(
    array(
      'data' => t('Timestamp'),
      'field' => 'timestamp',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Path'),
      'field' => 'path',
    ),
    array(
      'data' => t('IP/host'),
      'field' => 'ip',
    ),
  );
  $name_resolve = variable_get(ADSENSE_CLICK_TRACKING_NAME_RESOLVE, 0);
  $sql = 'SELECT * FROM {adsense_clicks} ' . tablesort_sql($header);
  $result = pager_query($sql, 50);
  while ($log = db_fetch_object($result)) {
    if ($name_resolve) {
      $host = gethostbyaddr($log->ip);
    }
    else {
      $host = $log->ip;
    }
    $rows[] = array(
      array(
        'data' => format_date($log->timestamp, 'small'),
        'nowrap' => 'nowrap',
      ),
      _adsense_format_path($log->path),
      l($host, 'http://whois.domaintools.com/' . $log->ip),
    );
  }
  return theme('table', $header, $rows) . theme('pager', NULL, 50, 0);
}
function adsense_click_top_pages() {
  $header = array(
    array(
      'data' => t('Path'),
      'field' => 'path',
    ),
    array(
      'data' => t('Clicks'),
      'field' => 'count',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Last'),
      'field' => 'last',
    ),
  );
  $sql = 'SELECT path, COUNT(*) AS count, MAX(timestamp) AS last FROM {adsense_clicks} GROUP BY path' . tablesort_sql($header);
  $result = pager_query($sql, 50);
  while ($log = db_fetch_object($result)) {
    $rows[] = array(
      array(
        'data' => _adsense_format_path($log->path),
      ),
      array(
        'data' => $log->count,
        'align' => 'right',
      ),
      array(
        'data' => format_date($log->last, 'small'),
        'nowrap' => 'nowrap',
      ),
    );
  }
  return theme('table', $header, $rows) . theme('pager', NULL, 50, 0);
}
function adsense_click_by_day() {
  global $db_type;
  $header = array(
    array(
      'data' => t('Day'),
      'field' => 'day',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Clicks'),
      'field' => 'count',
    ),
  );
  switch ($db_type) {
    case 'mysql':
    case 'mysqli':
      $sql_count = "SELECT COUNT(DISTINCT(FROM_UNIXTIME(timestamp, '%y-%m-%%d'))) FROM {adsense_clicks}";
      $sql = "SELECT FROM_UNIXTIME(timestamp, '%Y-%m-%%d') AS day, COUNT(*) AS count FROM {adsense_clicks} GROUP BY day" . tablesort_sql($header);
      break;
    case 'pgsql':
      $sql_count = "SELECT COUNT(DISTINCT(TO_CHAR(timestamp, 'YYYY-MM-DD'))) FROM {adsense_clicks}";
      $sql = "SELECT TO_CHAR(timestamp, 'YYYY-MM-DD') AS day, COUNT(*) AS count FROM {adsense_clicks} GROUP BY day" . tablesort_sql($header);
      break;
  }
  $result = pager_query($sql, 50, 0, $sql_count);
  while ($log = db_fetch_object($result)) {
    $rows[] = array(
      $log->day,
      array(
        'data' => $log->count,
        'align' => 'right',
      ),
    );
  }
  return theme('table', $header, $rows) . theme('pager', NULL, 50, 0);
}
function _adsense_format_path($path, $width = 35) {
  global $base_url;
  $short_path = str_replace($base_url, '', $path);
  $title = truncate_utf8($short_path, $width, FALSE, TRUE);
  return l($title, $path);
}
function _adsense_get_searchbox($group = 1, $channel = 1) {
  $group = _adsense_validate_group($group);
  $client = adsense_get_client_id();
  $border = variable_get(ADSENSE_SEARCH_COLOR_BORDER, '336699');
  $title = variable_get(ADSENSE_SEARCH_COLOR_TITLE, '0000FF');
  $bg = variable_get(ADSENSE_SEARCH_COLOR_BG, 'FFFFFF');
  $text = variable_get(ADSENSE_SEARCH_COLOR_TEXT, '000000');
  $url = variable_get(ADSENSE_SEARCH_COLOR_URL, '008000');
  $visited = variable_get(ADSENSE_SEARCH_COLOR_VISITED_URL, '663399');
  $light = variable_get(ADSENSE_SEARCH_COLOR_LIGHT_URL, '0000FF');
  $logo = variable_get(ADSENSE_SEARCH_LOGO, ADSENSE_SEARCH_LOGO_GOOGLE);
  $search_button = variable_get(ADSENSE_SEARCH_BUTTON, 0);
  $box_text_color = variable_get(ADSENSE_SEARCH_COLOR_BOX_TEXT, ADSENSE_SEARCH_COLOR_BLACK);
  $box_background_color = variable_get(ADSENSE_SEARCH_COLOR_BOX_BACKGROUND, ADSENSE_SEARCH_COLOR_WHITE);
  $encoding = variable_get(ADSENSE_SEARCH_ENCODING, 'UTF-8');
  $channel = variable_get(ADSENSE_AD_CHANNEL . _adsense_validate_channel($channel), '');
  global $base_url;
  $domain_0 = variable_get(ADSENSE_SEARCH_DOMAIN . '_0', $base_url);
  $domain_1 = variable_get(ADSENSE_SEARCH_DOMAIN . '_1', '');
  $domain_2 = variable_get(ADSENSE_SEARCH_DOMAIN . '_2', '');
  $domain = $domain_1 ? "{$domain_0};{$domain_1}" : $domain_0;
  $domain = $domain_2 ? "{$domain_0};{$domain_2}" : $domain_0;
  $language = variable_get(ADSENSE_SEARCH_LANGUAGE, 'en');
  $textbox_length = variable_get(ADSENSE_SEARCH_TEXTBOX_LENGTH, 15);
  $output .= _adsense_add_nl('<div class="adsense adsense_search">');
  $output .= _adsense_add_nl(ADSENSE_SITE_SEARCH);
  $output .= _adsense_add_nl('<form method="get" action="http://' . $domain . '/' . ADSENSE_SEARCH_PATH . '" target="_top">');
  $output .= _adsense_add_nl('<table border="0" bgcolor="' . $box_background_color . '">');
  $output .= _adsense_add_nl('<tr><td nowrap="nowrap" valign="top" align="left" height="32">');
  if ($logo != ADSENSE_SEARCH_LOGO_ON_BUTTON) {
    $output .= _adsense_add_nl('<a href="http://www.google.com/">');
    if ($box_background_color == ADSENSE_SEARCH_COLOR_WHITE) {
      $logo_color = 'wht';
    }
    elseif ($box_background_color == ADSENSE_SEARCH_COLOR_BLACK) {
      $logo_color = 'blk';
    }
    elseif ($box_background_color == ADSENSE_SEARCH_COLOR_GRAY) {
      $logo_color = 'gry';
    }
    $output .= _adsense_add_nl('<img src="http://www.google.com/logos/Logo_25' . $logo_color . '.gif" border="0" alt="Google" align="middle" /></a>');
  }
  if ($logo == ADSENSE_SEARCH_LOGO_GOOGLE) {
    $output .= _adsense_add_nl('</td>');
    $output .= _adsense_add_nl('<td nowrap="nowrap">');
  }
  elseif ($logo == ADSENSE_SEARCH_LOGO_ABOVE_TEXTBOX) {
    $output .= _adsense_add_nl('<br/>');
  }
  $output .= _adsense_add_nl('<input type="hidden" name="domains" value="' . $domain . '" />');
  $output .= _adsense_add_nl('<label for="sbi" style="display: none">' . t('Enter your search terms') . '</label>');
  $output .= _adsense_add_nl('<input type="text" name="as_q" size="' . $textbox_length . '" maxlength="255" value="" id="sbi"></input>');
  if (!$search_button) {
    $output .= _adsense_add_nl('<label for="sbb" style="display: none">' . t('Submit search form') . '</label>');
    if ($logo != ADSENSE_SEARCH_LOGO_ON_BUTTON) {
      $output .= _adsense_add_nl('<input type="submit" name="sa" value="' . t('Search') . '" id="sbb"></input>');
    }
    else {
      $output .= _adsense_add_nl('<input type="submit" name="sa" value="' . t('Google Search') . '" id="sbb"></input>');
    }
  }
  $output .= _adsense_add_nl('</td></tr>');
  $output .= _adsense_add_nl('<tr>');
  if ($logo == ADSENSE_SEARCH_LOGO_GOOGLE) {
    $output .= _adsense_add_nl('<td>&nbsp;</td>');
  }
  $output .= _adsense_add_nl('<td nowrap="nowrap">');
  $output .= _adsense_add_nl('<table>');
  $output .= _adsense_add_nl('<tr>');
  $output .= _adsense_add_nl('<td>');
  $output .= _adsense_add_nl('<input type="radio" name="sitesearch" value="" id="ss0"></input>');
  $output .= _adsense_add_nl('<label for="ss0" title="' . t('Search the Web') . '"><font size="-1" color="' . $box_text_color . '">Web</font></label></td>');
  $output .= _adsense_add_nl('<td>');
  $output .= _adsense_add_nl('<input type="radio" name="sitesearch" value="' . $domain_0 . '" checked id="ss1"></input>');
  $output .= _adsense_add_nl('<label for="ss1" title="' . t('Search') . ' ' . $domain_0 . '"><font size="-1" color="' . $box_text_color . '">' . $domain_0 . '</font></label></td>');
  $output .= _adsense_add_nl('</tr>');
  if ($domain_1) {
    $output .= _adsense_add_nl('<tr>');
    $output .= _adsense_add_nl('<td>');
    $output .= _adsense_add_nl('<input type="radio" name="sitesearch" value="' . $domain_1 . '" id="ss2"></input>');
    $output .= _adsense_add_nl('<label for="ss2" title="Search ' . $domain_1 . '"><font size="-1" color="' . $box_text_color . '">' . $domain_1 . '</font></label></td>');
    if ($domain_2) {
      $output .= _adsense_add_nl('<td>');
      $output .= _adsense_add_nl('<input type="radio" name="sitesearch" value="' . $domain_2 . '" id="ss3"></input>');
      $output .= _adsense_add_nl('<label for="ss3" title="Search ' . $domain_2 . '"><font size="-1" color="' . $box_text_color . '">' . $domain_2 . '</font></label></td>');
    }
    else {
      $output .= _adsense_add_nl('<td></td>');
    }
    $output .= _adsense_add_nl('</tr>');
  }
  $output .= _adsense_add_nl('</table>');
  if ($search_button) {
    $output .= _adsense_add_nl('<label for="sbb" style="display: none">' . t('Submit search form') . '</label>');
    if ($logo != ADSENSE_SEARCH_LOGO_ON_BUTTON) {
      $output .= _adsense_add_nl('<input type="submit" name="sa" value="' . t('Search') . '" id="sbb"></input>');
    }
    else {
      $output .= _adsense_add_nl('<input type="submit" name="sa" value="' . t('Google Search') . '" id="sbb"></input>');
    }
  }
  $output .= _adsense_add_nl('<input type="hidden" name="client" value="' . $client . '"></input>');
  $output .= _adsense_add_nl('<input type="hidden" name="forid" value="1"></input>');
  $output .= _adsense_add_nl('<input type="hidden" name="channel" value="' . $channel . '"></input>');
  $output .= _adsense_add_nl('<input type="hidden" name="ie" value="' . $encoding . '"></input>');
  $output .= _adsense_add_nl('<input type="hidden" name="oe" value="' . $encoding . '"></input>');
  $output .= _adsense_add_nl('<input type="hidden" name="cof" value="GALT:#' . $url . ';GL:1;DIV:#' . $border . ';VLC:#' . $visited . ';AH:center;BGC:#' . $bg . ';LBGC:#' . $bg . ';ALC:#' . $title . ';LC:#' . $title . ';T:#' . $text . ';GFNT:#' . $light . ';GIMP:#' . $light . ';FORID:11"></input>');
  $output .= _adsense_add_nl('<input type="hidden" name="hl" value="' . $language . '"></input>');
  $output .= _adsense_add_nl('</td></tr></table>');
  $output .= _adsense_add_nl('</form>');
  $output .= _adsense_add_nl(ADSENSE_SITE_SEARCH);
  $output .= _adsense_add_nl('</div>');
  return $output;
}
function adsense_search($op = 'search', $keys = NULL) {
  global $base_url;
  if (!user_access('access adsense search result page')) {
    return;
  }
  switch ($op) {
    case 'name':
      return t('Google AdSense for Search');
    case 'search':

      // Log the search keys:
      watchdog('AdSense', t('Keyword') . ': <em>' . urldecode($_GET['as_q']) . '</em>', WATCHDOG_NOTICE, 'AdSense for Search');

      // Output search results
      $width = variable_get(ADSENSE_SEARCH_FRAME_WIDTH, '500');
      $domain = variable_get(ADSENSE_SEARCH_DOMAIN . '_0', $base_url);
      $country = variable_get(ADSENSE_SEARCH_COUNTRY, 'www.google.com');
      $output .= _adsense_add_nl(ADSENSE_SEARCH_RESULT_START);
      $output .= _adsense_add_nl('<div id="googleSearchUnitIframe"></div>');
      $output .= _adsense_add_nl('<script type="text/javascript">');
      $output .= _adsense_add_nl('  var googleSearchIframeName = \'googleSearchUnitIframe\';');
      $output .= _adsense_add_nl('  var googleSearchFrameWidth = ' . $width . ';');
      $output .= _adsense_add_nl('  var googleSearchFrameborder = 0 ;');
      $output .= _adsense_add_nl('  var googleSearchDomain = \'' . $country . '\';');
      $output .= _adsense_add_nl('</script>');
      $output .= _adsense_add_nl('<script type="text/javascript"');
      $output .= _adsense_add_nl('  src="http://www.google.com/afsonline/show_afs_search.js">');
      $output .= _adsense_add_nl('</script>');
      $output .= _adsense_add_nl(ADSENSE_SEARCH_RESULT_END);
      return $output;
  }
}

Functions

Namesort descending Description
adsense_admin_settings
adsense_ad_formats This is the array that holds all ad formats. All it has is a multi-dimensional array containing the dimensions as a key, then the description and suffix of Google's javascript ad code. Currently, we do not support duplicate dimensions in ad…
adsense_block Implementation of hook_block().
adsense_click_by_day
adsense_click_log Menu callback; presents the "AdSense" log page.
adsense_click_top_pages
adsense_counter
adsense_display
adsense_filter Implementation of hook_filter().
adsense_filter_tips
adsense_footer Implementation of hook_footer().
adsense_get_ad_code
adsense_get_client_id
adsense_help Implementation of hook_help().
adsense_menu Implementation of hook_menu().
adsense_nodeapi Implemenation of hook_nodeapi().
adsense_perm Implementation of hook_perm().
adsense_search
_adsense_add_nl
_adsense_can_insert_another Determine if AdSense has reached limit on this page. As per Google's policies, a page can have up to 3 ad units and 3 link units.
_adsense_check_blog
_adsense_check_if_enabled
_adsense_check_placeholder
_adsense_choose_client
_adsense_format
_adsense_format_path
_adsense_format_placeholder
_adsense_get_ad
_adsense_get_block_config
_adsense_get_dimensions
_adsense_get_height
_adsense_get_profile_client_id
_adsense_get_profile_fields
_adsense_get_referral_uid
_adsense_get_searchbox
_adsense_get_site_owner_client_id
_adsense_get_width
_adsense_page_match Determine if AdSense has permission to be used on the current page.
_adsense_process_tags
_adsense_tag_block
_adsense_tag_flexiblock
_adsense_validate_channel
_adsense_validate_dimensions
_adsense_validate_group

Constants