You are here

adsense_search.module in Google AdSense integration 7

Displays Google AdSense ads on Drupal pages.

This is the core module of the AdSense package, with the Drupal hooks and other administrative functions.

File

old/search/adsense_search.module
View source
<?php

/**
 * @file
 * Displays Google AdSense ads on Drupal pages.
 *
 * This is the core module of the AdSense package, with the Drupal hooks
 * and other administrative functions.
 */
define('ADSENSE_SEARCH_AD_BLOCK_DEFAULT', '');
define('ADSENSE_SEARCH_BUTTON_DEFAULT', 0);
define('ADSENSE_SEARCH_COLOR_BG_DEFAULT', '#FFFFFF');
define('ADSENSE_SEARCH_COLOR_BORDER_DEFAULT', '#336699');
define('ADSENSE_SEARCH_COLOR_BOX_BACKGROUND_DEFAULT', '#FFFFFF');
define('ADSENSE_SEARCH_COLOR_BOX_TEXT_DEFAULT', '#000000');
define('ADSENSE_SEARCH_COLOR_LIGHT_URL_DEFAULT', '#0000FF');
define('ADSENSE_SEARCH_COLOR_LOGO_BG_DEFAULT', '#336699');
define('ADSENSE_SEARCH_COLOR_TEXT_DEFAULT', '#000000');
define('ADSENSE_SEARCH_COLOR_TITLE_DEFAULT', '#0000FF');
define('ADSENSE_SEARCH_COLOR_URL_DEFAULT', '#008000');
define('ADSENSE_SEARCH_COLOR_VISITED_URL_DEFAULT', '#663399');
define('ADSENSE_SEARCH_COUNTRY_DEFAULT', 'www.google.com');
define('ADSENSE_SEARCH_DOMAIN_DEFAULT', '');
define('ADSENSE_SEARCH_ENCODING_DEFAULT', 'UTF-8');
define('ADSENSE_SEARCH_FRAME_WIDTH_DEFAULT', 800);
define('ADSENSE_SEARCH_LANGUAGE_DEFAULT', 'en');
define('ADSENSE_SEARCH_LOGO_DEFAULT', 'adsense_search_logo_google');
define('ADSENSE_SEARCH_NUMBER_BLOCKS_DEFAULT', 2);
define('ADSENSE_SEARCH_SAFE_MODE_DEFAULT', 0);
define('ADSENSE_SEARCH_TEXTBOX_LENGTH_DEFAULT', 31);

/**
 * Implements hook_menu().
 */
function adsense_search_menu() {
  $items = array();
  $items['admin/config/services/adsense/search'] = array(
    'title' => 'Old Search',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'adsense_search_settings',
    ),
    'access arguments' => array(
      'administer adsense',
    ),
    'weight' => 4,
    'type' => MENU_LOCAL_TASK,
    'file' => 'adsense_search.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_block_info().
 */
function adsense_search_block_info() {
  $blocks = array();
  $max = variable_get('adsense_search_number_blocks', ADSENSE_SEARCH_NUMBER_BLOCKS_DEFAULT);
  for ($count = 0; $count < $max; $count++) {
    if ($ad = _adsense_search_get_block_config($count)) {
      $title = $ad[0];
    }
    else {
      $title = t('AdSense Search: unconfigured !d', array(
        '!d' => $count + 1,
      ));
    }
    $blocks[$count] = array(
      'info' => $title,
      'cache' => DRUPAL_NO_CACHE,
    );
  }
  return $blocks;
}

/**
 * Implements hook_block_configure().
 */
function adsense_search_block_configure($delta) {
  $ad = _adsense_search_get_block_config($delta);
  $channel_list[''] = t('None');
  for ($channel = 1; $channel <= ADSENSE_MAX_CHANNELS; $channel++) {
    $channel_list[$channel] = $channel . ' : ' . variable_get('adsense_ad_channel_' . $channel, ADSENSE_AD_CHANNEL_DEFAULT);
  }
  $form['info'] = array(
    '#type' => 'textfield',
    '#title' => t('Block description'),
    '#default_value' => $ad ? $ad[0] : '',
    '#maxlength' => 64,
    '#description' => t('A brief description of your block. Used on the <a href="@overview">block overview page</a>.', array(
      '@overview' => url('admin/structure/block'),
    )),
    '#required' => TRUE,
    '#weight' => -19,
  );
  $form['ad_channel'] = array(
    '#type' => 'select',
    '#title' => t('Channel'),
    '#default_value' => $ad ? $ad[1] : '',
    '#options' => $channel_list,
  );
  return $form;
}

/**
 * Implements hook_block_save().
 */
function adsense_search_block_save($delta, $edit) {
  $data = implode(':', array(
    urlencode(check_plain($edit['info'])),
    check_plain($edit['ad_channel']),
  ));
  variable_set('adsense_search_ad_block_' . $delta, $data);
}

/**
 * Implements hook_block_view().
 */
function adsense_search_block_view($delta) {
  $block = array();
  if (_adsense_page_match()) {
    $ad = _adsense_search_get_block_config($delta);
    $block['content'] = $ad ? adsense_display(array(
      'title' => $ad[0],
      'format' => 'Search Box',
      'channel' => $ad[1],
    )) : _adsense_configure_block_message(url('admin/structure/block/manage/adsense_search/' . $delta));
  }
  return $block;
}

/**
 * Configuration of the provided block.
 *
 * @param int $delta
 *   Block number.
 *
 * @return array|bool
 *   array with the block configuration or FALSE if no such block was found
 */
function _adsense_search_get_block_config($delta = 0) {
  if ($data = variable_get('adsense_search_ad_block_' . $delta, ADSENSE_SEARCH_AD_BLOCK_DEFAULT)) {
    $ad = explode(':', $data);
    $ad[0] = urldecode($ad[0]);
    return $ad;
  }
  return FALSE;
}

/**
 * Generates the search box.
 *
 * @param int $client
 *   Publisher ID.
 * @param int $channel
 *   Channel ID of the configured Ad Channel (optional).
 *
 * @return string
 *   HTML with the search input form
 */
function _adsense_search_get_searchbox($client, $channel = NULL) {
  global $base_url;
  $country = variable_get('adsense_search_country', ADSENSE_SEARCH_COUNTRY_DEFAULT);
  $border = variable_get('adsense_search_color_border', ADSENSE_SEARCH_COLOR_BORDER_DEFAULT);
  $title = variable_get('adsense_search_color_title', ADSENSE_SEARCH_COLOR_TITLE_DEFAULT);
  $bg = variable_get('adsense_search_color_bg', ADSENSE_SEARCH_COLOR_BG_DEFAULT);
  $text = variable_get('adsense_search_color_text', ADSENSE_SEARCH_COLOR_TEXT_DEFAULT);
  $url = variable_get('adsense_search_color_url', ADSENSE_SEARCH_COLOR_URL_DEFAULT);
  $visited = variable_get('adsense_search_color_visited_url', ADSENSE_SEARCH_COLOR_VISITED_URL_DEFAULT);
  $light = variable_get('adsense_search_color_light_url', ADSENSE_SEARCH_COLOR_LIGHT_URL_DEFAULT);
  $logobg = variable_get('adsense_search_color_logo_bg', ADSENSE_SEARCH_COLOR_LOGO_BG_DEFAULT);
  $logo = variable_get('adsense_search_logo', ADSENSE_SEARCH_LOGO_DEFAULT);
  $search_button = variable_get('adsense_search_button', ADSENSE_SEARCH_BUTTON_DEFAULT);
  $box_text_color = variable_get('adsense_search_color_box_text', ADSENSE_SEARCH_COLOR_BOX_TEXT_DEFAULT);
  $box_background_color = variable_get('adsense_search_color_box_background', ADSENSE_SEARCH_COLOR_BOX_BACKGROUND_DEFAULT);
  $encoding = variable_get('adsense_search_encoding', ADSENSE_SEARCH_ENCODING_DEFAULT);
  $domain_0 = variable_get('adsense_search_domain_0', str_replace('http://', '', $base_url));
  $domain_1 = variable_get('adsense_search_domain_1', ADSENSE_SEARCH_DOMAIN_DEFAULT);
  $domain_2 = variable_get('adsense_search_domain_2', ADSENSE_SEARCH_DOMAIN_DEFAULT);
  $domain = $domain_1 ? "{$domain_0};{$domain_1}" : $domain_0;
  $domain = $domain_2 ? "{$domain};{$domain_2}" : $domain;
  $language = variable_get('adsense_search_language', ADSENSE_SEARCH_LANGUAGE_DEFAULT);
  $textbox_length = variable_get('adsense_search_textbox_length', ADSENSE_SEARCH_TEXTBOX_LENGTH_DEFAULT);
  if ($logo != 'adsense_search_logo_on_button') {
    $btn = t('Search');
    if ($box_background_color == '#000000') {
      $logo_color = 'blk';
    }
    elseif ($box_background_color == '#CCCCCC') {
      $logo_color = 'gry';
    }
    else {
      $logo_color = 'wht';
    }
    $part1 = "<a href=\"http://www.google.com/\">\n" . "<img src=\"http://www.google.com/logos/Logo_25{$logo_color}.gif\" border=\"0\" alt=\"Google\" align=\"middle\"></img></a>";
    $part3 = "";
    if ($logo == 'adsense_search_logo_google') {
      $part1 .= "\n</td>\n<td nowrap=\"nowrap\">";
      $part3 = "<td>&nbsp;</td>";
    }
    elseif ($logo == 'adsense_search_logo_above_textbox') {
      $part1 .= "\n<br/>";
    }
  }
  else {
    $btn = t('Google Search');
    $part1 = "</td>\n<td nowrap=\"nowrap\">";
    $part3 = "<td>&nbsp;</td>";
  }
  $button_html = "<label for=\"sbb\" style=\"display: none\">Submit search form</label>\n" . "<input type=\"submit\" name=\"sa\" value=\"{$btn}\" id=\"sbb\"></input>";
  if (!$search_button) {
    $part2 = $button_html;
    $part5 = "";
  }
  else {
    $part5 = $button_html;
    $part2 = "";
  }

  // Searched domains >=1 (2 or 3).
  if ($domain_1) {
    $part4 = "<tr>\n<td>\n<input type=\"radio\" name=\"sitesearch\" value=\"{$domain_1}\" id=\"ss2\"></input>\n" . "<label for=\"ss2\" title=\"Search {$domain_1}\"><font size=\"-1\" color=\"{$box_text_color}\">{$domain_1}</font></label></td>\n";
    if ($domain_2) {
      $part4 .= "<td>\n<input type=\"radio\" name=\"sitesearch\" value=\"{$domain_2}\" id=\"ss3\"></input>\n" . "<label for=\"ss3\" title=\"Search {$domain_2}\"><font size=\"-1\" color=\"{$box_text_color}\">{$domain_2}</font></label></td>";
    }
    else {
      $part4 .= "<td></td>";
    }
    $part4 .= "\n</tr>";
  }
  else {
    $part4 = "";
  }

  // AdSense for Search channel.
  $channel = variable_get('adsense_ad_channel_' . $channel, ADSENSE_AD_CHANNEL_DEFAULT);
  if (!empty($channel)) {
    $part6 = "<input type=\"hidden\" name=\"channel\" value=\"{$channel}\"></input>";
  }
  else {
    $part6 = "";
  }

  // Is SafeSearch active?
  $part7 = variable_get('adsense_search_safe_mode', ADSENSE_SEARCH_SAFE_MODE_DEFAULT) ? '<input type="hidden" name="safe" value="active"></input>' : '';
  $output = <<<SEARCH_TXT
<!-- SiteSearch Google -->
<form method="get" action="http://{<span class="php-variable">$country</span>}/custom" target="_top">
<table border="0" bgcolor="{<span class="php-variable">$box_background_color</span>}">
<tr><td nowrap="nowrap" valign="top" align="left" height="32">
{<span class="php-variable">$part1</span>}
<input type="hidden" name="domains" value="{<span class="php-variable">$domain</span>}"></input>
<label for="sbi" style="display: none">Enter your search terms</label>
<input type="text" name="as_q" size="{<span class="php-variable">$textbox_length</span>}" maxlength="255" value="" id="sbi"></input>
{<span class="php-variable">$part2</span>}
</td></tr>
<tr>
{<span class="php-variable">$part3</span>}
<td nowrap="nowrap">
<table>
<tr>
<td>
<input type="radio" name="sitesearch" value="" checked id="ss0"></input>
<label for="ss0" title="Search the Web"><font size="-1" color="{<span class="php-variable">$box_text_color</span>}">Web</font></label></td>
<td>
<input type="radio" name="sitesearch" value="{<span class="php-variable">$domain_0</span>}" id="ss1"></input>
<label for="ss1" title="Search {<span class="php-variable">$domain_0</span>}"><font size="-1" color="{<span class="php-variable">$box_text_color</span>}">{<span class="php-variable">$domain_0</span>}</font></label></td>
</tr>
{<span class="php-variable">$part4</span>}
</table>
{<span class="php-variable">$part5</span>}
<input type="hidden" name="client" value="{<span class="php-variable">$client</span>}"></input>
<input type="hidden" name="forid" value="1"></input>
{<span class="php-variable">$part6</span>}
<input type="hidden" name="ie" value="{<span class="php-variable">$encoding</span>}"></input>
<input type="hidden" name="oe" value="{<span class="php-variable">$encoding</span>}"></input>
{<span class="php-variable">$part7</span>}
<input type="hidden" name="cof" value="GALT:{<span class="php-variable">$url</span>};GL:1;DIV:{<span class="php-variable">$border</span>};VLC:{<span class="php-variable">$visited</span>};AH:center;BGC:{<span class="php-variable">$bg</span>};LBGC:{<span class="php-variable">$logobg</span>};ALC:{<span class="php-variable">$title</span>};LC:{<span class="php-variable">$title</span>};T:{<span class="php-variable">$text</span>};GFNT:{<span class="php-variable">$light</span>};GIMP:{<span class="php-variable">$light</span>};FORID:11"></input>
<input type="hidden" name="hl" value="{<span class="php-variable">$language</span>}"></input>
</td></tr></table>
</form>
<!-- SiteSearch Google -->
SEARCH_TXT;
  return $output;
}