| 1 |
<?php
|
| 2 |
// $Id: adsense.module,v 1.92 2009/06/04 22:02:03 jcnventura Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Displays Google AdSense ads on Drupal pages
|
| 7 |
*
|
| 8 |
* This is the core module of the AdSense package, with the Drupal hooks
|
| 9 |
* and other administrative functions.
|
| 10 |
*/
|
| 11 |
|
| 12 |
// Ad types, link or ad
|
| 13 |
define('ADSENSE_TYPE_LINK', 1);
|
| 14 |
define('ADSENSE_TYPE_AD', 2);
|
| 15 |
define('ADSENSE_TYPE_SEARCH', 3);
|
| 16 |
|
| 17 |
define('ADSENSE_MAX_CHANNELS', 10);
|
| 18 |
define('ADSENSE_AD_CHANNEL_DEFAULT', '');
|
| 19 |
|
| 20 |
define('ADSENSE_ACCESS_PAGES_DEFAULT', '');
|
| 21 |
define('ADSENSE_BASIC_ID_DEFAULT', '');
|
| 22 |
define('ADSENSE_DISABLE_DEFAULT', 0);
|
| 23 |
define('ADSENSE_ID_MODULE_DEFAULT', 'adsense_basic');
|
| 24 |
define('ADSENSE_PLACEHOLDER_DEFAULT', 1);
|
| 25 |
define('ADSENSE_PLACEHOLDER_TEXT_DEFAULT', 'Google AdSense');
|
| 26 |
define('ADSENSE_SECTION_TARGETING_DEFAULT', 1);
|
| 27 |
define('ADSENSE_TEST_MODE_DEFAULT', 0);
|
| 28 |
define('ADSENSE_VISIBILITY_DEFAULT', 0);
|
| 29 |
|
| 30 |
define('ADSENSE_SECRET_ADTEST_DEFAULT', 0);
|
| 31 |
define('ADSENSE_SECRET_LANGUAGE_DEFAULT', '');
|
| 32 |
|
| 33 |
/**
|
| 34 |
* This is the array that holds all ad formats.
|
| 35 |
*
|
| 36 |
* All it has is a multi-dimensional array indexed by a key, containing the ad type,
|
| 37 |
* the description, Google's javascript ad code and the dimensions.
|
| 38 |
*
|
| 39 |
* To add a new code:
|
| 40 |
* - Make sure the key is not in use by a different format
|
| 41 |
* - Go to Google AdSense
|
| 42 |
* . Get the dimensions
|
| 43 |
* . Get the code
|
| 44 |
* - Add it below
|
| 45 |
*
|
| 46 |
* @param $key
|
| 47 |
* (optional) ad key for which the format is needed
|
| 48 |
* @return
|
| 49 |
* if no key is provided: array of supported ad formats as an array (type, desc(ription), code, width and height)
|
| 50 |
* if a key is provided, the array containing the ad format for that key, or NULL if there is no ad with that key
|
| 51 |
*/
|
| 52 |
function adsense_ad_formats($key = NULL) {
|
| 53 |
$ads = array(
|
| 54 |
'120x90' => array('type' => ADSENSE_TYPE_LINK, 'desc' => t('4-links 120x90'), 'code' => '120x90_0ads_al', 'width' => 120, 'height' => 90),
|
| 55 |
'160x90' => array('type' => ADSENSE_TYPE_LINK, 'desc' => t('4-links 160x90'), 'code' => '160x90_0ads_al', 'width' => 160, 'height' => 90),
|
| 56 |
'180x90' => array('type' => ADSENSE_TYPE_LINK, 'desc' => t('4-links 180x90'), 'code' => '180x90_0ads_al', 'width' => 180, 'height' => 90),
|
| 57 |
'200x90' => array('type' => ADSENSE_TYPE_LINK, 'desc' => t('4-links 200x90'), 'code' => '200x90_0ads_al', 'width' => 200, 'height' => 90),
|
| 58 |
'468x15' => array('type' => ADSENSE_TYPE_LINK, 'desc' => t('4-links 468x15'), 'code' => '468x15_0ads_al', 'width' => 468, 'height' => 15),
|
| 59 |
'728x15' => array('type' => ADSENSE_TYPE_LINK, 'desc' => t('4-links 728x15'), 'code' => '728x15_0ads_al', 'width' => 728, 'height' => 15),
|
| 60 |
'120x90_5' => array('type' => ADSENSE_TYPE_LINK, 'desc' => t('5-links 120x90'), 'code' => '120x90_0ads_al_s', 'width' => 120, 'height' => 90),
|
| 61 |
'160x90_5' => array('type' => ADSENSE_TYPE_LINK, 'desc' => t('5-links 160x90'), 'code' => '160x90_0ads_al_s', 'width' => 160, 'height' => 90),
|
| 62 |
'180x90_5' => array('type' => ADSENSE_TYPE_LINK, 'desc' => t('5-links 180x90'), 'code' => '180x90_0ads_al_s', 'width' => 180, 'height' => 90),
|
| 63 |
'200x90_5' => array('type' => ADSENSE_TYPE_LINK, 'desc' => t('5-links 200x90'), 'code' => '200x90_0ads_al_s', 'width' => 200, 'height' => 90),
|
| 64 |
'468x15_5' => array('type' => ADSENSE_TYPE_LINK, 'desc' => t('5-links 468x15'), 'code' => '468x15_0ads_al_s', 'width' => 468, 'height' => 15),
|
| 65 |
'728x15_5' => array('type' => ADSENSE_TYPE_LINK, 'desc' => t('5-links 728x15'), 'code' => '728x15_0ads_al_s', 'width' => 728, 'height' => 15),
|
| 66 |
'120x240' => array('type' => ADSENSE_TYPE_AD, 'desc' => t('Vertical banner'), 'code' => '120x240_as', 'width' => 120, 'height' => 240),
|
| 67 |
'120x600' => array('type' => ADSENSE_TYPE_AD, 'desc' => t('Skyscraper'), 'code' => '120x600_as', 'width' => 120, 'height' => 600),
|
| 68 |
'125x125' => array('type' => ADSENSE_TYPE_AD, 'desc' => t('Button'), 'code' => '125x125_as', 'width' => 125, 'height' => 125),
|
| 69 |
'160x600' => array('type' => ADSENSE_TYPE_AD, 'desc' => t('Wide Skyscraper'), 'code' => '160x600_as', 'width' => 160, 'height' => 600),
|
| 70 |
'180x150' => array('type' => ADSENSE_TYPE_AD, 'desc' => t('Small Rectangle'), 'code' => '180x150_as', 'width' => 180, 'height' => 150),
|
| 71 |
'200x200' => array('type' => ADSENSE_TYPE_AD, 'desc' => t('Small Square'), 'code' => '200x200_as', 'width' => 200, 'height' => 200),
|
| 72 |
'234x60' => array('type' => ADSENSE_TYPE_AD, 'desc' => t('Half Banner'), 'code' => '234x60_as', 'width' => 234, 'height' => 60),
|
| 73 |
'250x250' => array('type' => ADSENSE_TYPE_AD, 'desc' => t('Square'), 'code' => '250x250_as', 'width' => 250, 'height' => 250),
|
| 74 |
'300x250' => array('type' => ADSENSE_TYPE_AD, 'desc' => t('Medium Rectangle'), 'code' => '300x250_as', 'width' => 300, 'height' => 250),
|
| 75 |
'336x280' => array('type' => ADSENSE_TYPE_AD, 'desc' => t('Large Rectangle'), 'code' => '336x280_as', 'width' => 336, 'height' => 280),
|
| 76 |
'468x60' => array('type' => ADSENSE_TYPE_AD, 'desc' => t('Banner'), 'code' => '468x60_as', 'width' => 468, 'height' => 60),
|
| 77 |
'728x90' => array('type' => ADSENSE_TYPE_AD, 'desc' => t('Leaderboard'), 'code' => '728x90_as', 'width' => 728, 'height' => 90),
|
| 78 |
);
|
| 79 |
|
| 80 |
if (!empty($key)) {
|
| 81 |
if (array_key_exists($key, $ads)) {
|
| 82 |
return $ads[$key];
|
| 83 |
}
|
| 84 |
elseif ($key == 'Search Box') {
|
| 85 |
return array('type' => ADSENSE_TYPE_SEARCH, 'desc' => t('AdSense for Search'));
|
| 86 |
}
|
| 87 |
else {
|
| 88 |
return NULL;
|
| 89 |
}
|
| 90 |
}
|
| 91 |
|
| 92 |
return $ads;
|
| 93 |
}
|
| 94 |
|
| 95 |
/**
|
| 96 |
* Implementation of hook_perm().
|
| 97 |
*/
|
| 98 |
function adsense_perm() {
|
| 99 |
return array('administer adsense', 'hide adsense', 'use PHP for ad visibility');
|
| 100 |
}
|
| 101 |
|
| 102 |
/**
|
| 103 |
* Implementation of hook_theme().
|
| 104 |
*/
|
| 105 |
function adsense_theme() {
|
| 106 |
return array(
|
| 107 |
'adsense_ad' => array(
|
| 108 |
'arguments' => array('ad' => NULL, 'module' => NULL),
|
| 109 |
),
|
| 110 |
);
|
| 111 |
}
|
| 112 |
|
| 113 |
/**
|
| 114 |
* Implementation of hook_menu().
|
| 115 |
*/
|
| 116 |
function adsense_menu() {
|
| 117 |
$items = array();
|
| 118 |
|
| 119 |
$items['admin/settings/adsense'] = array(
|
| 120 |
'title' => 'AdSense',
|
| 121 |
'description' => 'Configure Google AdSense Ads.',
|
| 122 |
'page callback' => 'drupal_get_form',
|
| 123 |
'page arguments' => array('adsense_main_settings'),
|
| 124 |
'access arguments' => array('administer adsense'),
|
| 125 |
'file' => 'adsense.admin.inc',
|
| 126 |
);
|
| 127 |
$items['admin/settings/adsense/main'] = array(
|
| 128 |
'title' => 'Settings',
|
| 129 |
'weight' => 10,
|
| 130 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 131 |
);
|
| 132 |
$items['admin/settings/adsense/publisher'] = array(
|
| 133 |
'title' => 'Publisher ID',
|
| 134 |
'page callback' => 'drupal_get_form',
|
| 135 |
'page arguments' => array('adsense_id_settings'),
|
| 136 |
'access arguments' => array('administer adsense'),
|
| 137 |
'weight' => 0,
|
| 138 |
'type' => MENU_LOCAL_TASK,
|
| 139 |
'file' => 'adsense.admin.inc',
|
| 140 |
);
|
| 141 |
$items['admin/settings/adsense/publisher/site'] = array(
|
| 142 |
'title' => 'Site ID',
|
| 143 |
'weight' => -1,
|
| 144 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 145 |
);
|
| 146 |
|
| 147 |
return $items;
|
| 148 |
}
|
| 149 |
|
| 150 |
/**
|
| 151 |
* Implementation of hook_requirements().
|
| 152 |
*/
|
| 153 |
function adsense_requirements($phase) {
|
| 154 |
$requirements = array();
|
| 155 |
$t = get_t();
|
| 156 |
switch ($phase) {
|
| 157 |
// At runtime, make sure that we have a publisher ID
|
| 158 |
case 'runtime':
|
| 159 |
$basic_id = variable_get('adsense_basic_id', ADSENSE_BASIC_ID_DEFAULT);
|
| 160 |
if (empty($basic_id)) {
|
| 161 |
$requirements['adsense_basic_id'] = array(
|
| 162 |
'title' => $t('AdSense'),
|
| 163 |
'value' => $t('Publisher ID is not set.'),
|
| 164 |
'description' => $t('Please configure it in the <a href="@url">AdSense settings page</a>.', array('@url' => url('admin/settings/adsense/publisher'))),
|
| 165 |
'severity' => REQUIREMENT_ERROR,
|
| 166 |
);
|
| 167 |
}
|
| 168 |
break;
|
| 169 |
}
|
| 170 |
return $requirements;
|
| 171 |
}
|
| 172 |
|
| 173 |
/**
|
| 174 |
* Implementation of hook_filter().
|
| 175 |
*/
|
| 176 |
function adsense_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 177 |
switch ($op) {
|
| 178 |
case 'list':
|
| 179 |
return array(0 => t('AdSense tag'));
|
| 180 |
case 'no cache':
|
| 181 |
return TRUE;
|
| 182 |
case 'description':
|
| 183 |
return t('Substitutes an AdSense special tag with an ad.');
|
| 184 |
case 'process':
|
| 185 |
return _adsense_process_tags($text);
|
| 186 |
default:
|
| 187 |
return $text;
|
| 188 |
}
|
| 189 |
}
|
| 190 |
|
| 191 |
/**
|
| 192 |
* Implementation of hook_nodeapi().
|
| 193 |
*/
|
| 194 |
function adsense_nodeapi(&$node, $op = 'view', $teaser, $page) {
|
| 195 |
switch ($op) {
|
| 196 |
case 'view':
|
| 197 |
if (variable_get('adsense_section_targeting', ADSENSE_SECTION_TARGETING_DEFAULT)) {
|
| 198 |
$node->content['adsense_start'] = array(
|
| 199 |
'#value' => '<!-- google_ad_section_start -->',
|
| 200 |
'#weight' => -5,
|
| 201 |
);
|
| 202 |
$node->content['adsense_end'] = array(
|
| 203 |
'#value' => '<!-- google_ad_section_end -->',
|
| 204 |
'#weight' => 5,
|
| 205 |
);
|
| 206 |
}
|
| 207 |
}
|
| 208 |
}
|
| 209 |
|
| 210 |
/**
|
| 211 |
* Implementation of hook_form_filter_admin_format_form_alter().
|
| 212 |
*/
|
| 213 |
function adsense_form_filter_admin_format_form_alter(&$form, $form_state) {
|
| 214 |
// In Drupal <= 6.9 (or later) the HTML corrector has a bug that causes problems with the use of the AdSense tag filter
|
| 215 |
sscanf(VERSION, "%d.%d", $major, $minor);
|
| 216 |
if (($major == 6) && ($minor <= 99)) {
|
| 217 |
if ((empty($form_state['post']) && $form['filters']['adsense/0']['#default_value'] && $form['filters']['filter/3']['#default_value']) ||
|
| 218 |
((!empty($form_state['post'])) && ($form_state['post']['filters']['adsense/0'] == '1') && ($form_state['post']['filters']['filter/3'] == '1'))) {
|
| 219 |
drupal_set_message(t('The HTML corrector filter has a bug that causes problems with the use of the AdSense tag. Disabling the HTML corrector filter is recommended.'), 'warning', TRUE);
|
| 220 |
}
|
| 221 |
}
|
| 222 |
}
|
| 223 |
|
| 224 |
/**
|
| 225 |
* Implementation of hook_filter_tips().
|
| 226 |
*/
|
| 227 |
function adsense_filter_tips($delta, $format, $long = FALSE) {
|
| 228 |
return t('Use the special tag [adsense:<em>format</em>:<em>slot</em>] or [adsense:<em>format</em>:<em>[group]</em>:<em>[channel]</em><em>[:slot]</em>] or [adsense:block:<em>location</em>] to display Google AdSense ads.');
|
| 229 |
}
|
| 230 |
|
| 231 |
/**
|
| 232 |
* Helper function to process the adsense input filter
|
| 233 |
*
|
| 234 |
* @param $text
|
| 235 |
* text of the node being processed
|
| 236 |
* @return
|
| 237 |
* modified text with the adsense tags replaced by Google AdSense ads
|
| 238 |
* @see adsense_filter()
|
| 239 |
* @see adsense_display()
|
| 240 |
*/
|
| 241 |
function _adsense_process_tags($text) {
|
| 242 |
$patterns = array(
|
| 243 |
'block' => '/\[adsense:block:([^\]]+)\]/x',
|
| 244 |
'oldtag' => '/\[adsense:([^:]+):(\d*):(\d*):?(\w*)\]/x',
|
| 245 |
'tag' => '/\[adsense:([^:]+):([^\]]+)\]/x',
|
| 246 |
);
|
| 247 |
|
| 248 |
foreach ($patterns as $mode => $pattern) {
|
| 249 |
if (preg_match_all($pattern, $text, $matches, PREG_SET_ORDER)) {
|
| 250 |
foreach ($matches as $match) {
|
| 251 |
switch ($mode) {
|
| 252 |
case 'block':
|
| 253 |
$mods = array(
|
| 254 |
'adsense_managed',
|
| 255 |
'adsense_cse',
|
| 256 |
'adsense_oldcode',
|
| 257 |
'adsense_search',
|
| 258 |
);
|
| 259 |
foreach ($mods as $module) {
|
| 260 |
$module_blocks = module_invoke($module, 'block', 'list');
|
| 261 |
if ($module_blocks) {
|
| 262 |
foreach ($module_blocks as $delta => $block) {
|
| 263 |
if ($block['info'] == $match[1]) {
|
| 264 |
// Found the block with the same name as the passed arg
|
| 265 |
$block = module_invoke($module, 'block', 'view', $delta);
|
| 266 |
$ad = $block['content'];
|
| 267 |
}
|
| 268 |
}
|
| 269 |
}
|
| 270 |
}
|
| 271 |
break;
|
| 272 |
case 'oldtag':
|
| 273 |
// If not specified, default group and channel to 1
|
| 274 |
if (empty($match[2])) {
|
| 275 |
$match[2] = 1;
|
| 276 |
}
|
| 277 |
if (empty($match[3])) {
|
| 278 |
$match[3] = 1;
|
| 279 |
}
|
| 280 |
$args = array(
|
| 281 |
'format' => $match[1],
|
| 282 |
'group' => $match[2],
|
| 283 |
'channel' => $match[3],
|
| 284 |
'slot' => $match[4],
|
| 285 |
);
|
| 286 |
$ad = adsense_display($args);
|
| 287 |
unset($args);
|
| 288 |
break;
|
| 289 |
case 'tag':
|
| 290 |
$args = array(
|
| 291 |
'format' => $match[1],
|
| 292 |
'slot' => $match[2],
|
| 293 |
);
|
| 294 |
$ad = adsense_display($args);
|
| 295 |
unset($args);
|
| 296 |
break;
|
| 297 |
}
|
| 298 |
// Replace the first occurance of the tag, in case we have the same
|
| 299 |
// tag more than once.
|
| 300 |
$str = '/\\'. $match[0] .'/';
|
| 301 |
$text = preg_replace($str, $ad, $text, 1);
|
| 302 |
}
|
| 303 |
}
|
| 304 |
}
|
| 305 |
|
| 306 |
return $text;
|
| 307 |
}
|
| 308 |
|
| 309 |
/**
|
| 310 |
* Provides the Google AdSense Publisher ID / slot ID to be used in the ad
|
| 311 |
*
|
| 312 |
* If revenue sharing modules are installed, this function will call the
|
| 313 |
* appropriate function in those modules.
|
| 314 |
*
|
| 315 |
* @param $format
|
| 316 |
* format of the ad being generated (optional)
|
| 317 |
* @return
|
| 318 |
* If the format parameter is supplied, array with 'client' and 'slot'
|
| 319 |
* fields, otherwise just the Publisher ID string is returned
|
| 320 |
*/
|
| 321 |
function adsense_get_client_slot_id($format = NULL) {
|
| 322 |
// Get the configured function
|
| 323 |
$function = variable_get('adsense_id_module', ADSENSE_ID_MODULE_DEFAULT);
|
| 324 |
|
| 325 |
if ($function != ADSENSE_ID_MODULE_DEFAULT) {
|
| 326 |
// Call the function
|
| 327 |
if (function_exists($function)) {
|
| 328 |
$client_id = $function('client_id', $format);
|
| 329 |
if ($client_id) {
|
| 330 |
return $client_id;
|
| 331 |
}
|
| 332 |
}
|
| 333 |
}
|
| 334 |
return variable_get('adsense_basic_id', ADSENSE_BASIC_ID_DEFAULT);
|
| 335 |
}
|
| 336 |
|
| 337 |
/**
|
| 338 |
* Generates the Google AdSense Ad
|
| 339 |
*
|
| 340 |
* This function is capable of handling two types of arguments:
|
| 341 |
* 1. an array of arguments (format, group, channel or slot)
|
| 342 |
* 2. 0 to 4 arguments:
|
| 343 |
* - 1st arg: format (default '160x600')
|
| 344 |
* - 2nd arg: group (default 1)
|
| 345 |
* - 3rd arg: channel (default 1)
|
| 346 |
* - 4th arg: slot (default '')
|
| 347 |
*
|
| 348 |
* A valid format must always be provided. If a slot is provided, the ad is generated by the
|
| 349 |
* new format modules, if not then the 'old' format modules are attempted.
|
| 350 |
*
|
| 351 |
* @return
|
| 352 |
* Publisher ID string
|
| 353 |
* @see adsense_ad_formats()
|
| 354 |
* @see _adsense_page_match()
|
| 355 |
* @see _adsense_check_if_enabled()
|
| 356 |
* @see _adsense_format_box()
|
| 357 |
* @see _adsense_can_insert_another()
|
| 358 |
* @see _adsense_cse_get_searchbox()
|
| 359 |
* @see _adsense_search_get_searchbox()
|
| 360 |
* @see _adsense_managed_get_ad()
|
| 361 |
* @see _adsense_oldcode_get_ad()
|
| 362 |
*/
|
| 363 |
function adsense_display() {
|
| 364 |
$numargs = func_num_args();
|
| 365 |
if (($numargs == 1) && is_array(func_get_arg(0))) {
|
| 366 |
$args = func_get_arg(0);
|
| 367 |
}
|
| 368 |
else {
|
| 369 |
// handle the 'old' method of calling this function
|
| 370 |
// adsense_display($format = '160x600', $group = 1, $channel = 1, $slot = '', $referral = 0, $cpa = '')
|
| 371 |
|
| 372 |
$args['format'] = '160x600';
|
| 373 |
$args['group'] = 1;
|
| 374 |
$args['channel'] = 1;
|
| 375 |
switch ($numargs) {
|
| 376 |
case 6:
|
| 377 |
// cpa isn't used anymore
|
| 378 |
case 5:
|
| 379 |
// referral is obsolete
|
| 380 |
case 4:
|
| 381 |
$args['slot'] = func_get_arg(3);
|
| 382 |
case 3:
|
| 383 |
$args['channel'] = func_get_arg(2);
|
| 384 |
case 2:
|
| 385 |
$args['group'] = func_get_arg(1);
|
| 386 |
case 1:
|
| 387 |
$args['format'] = func_get_arg(0);
|
| 388 |
}
|
| 389 |
}
|
| 390 |
|
| 391 |
$ad = adsense_ad_formats($args['format']);
|
| 392 |
|
| 393 |
if ($ad === NULL) {
|
| 394 |
$ad = '<!--adsense: invalid format: '. $args['format'] .'-->';
|
| 395 |
}
|
| 396 |
elseif (!_adsense_page_match()) {
|
| 397 |
// Check first if disabled or if we are at adsense limit or if this page doesn't allow adsense
|
| 398 |
$ad = '<!--adsense: page not in match list-->';
|
| 399 |
}
|
| 400 |
elseif (!_adsense_check_if_enabled()) {
|
| 401 |
global $user;
|
| 402 |
|
| 403 |
// Ads are disabled
|
| 404 |
if (variable_get('adsense_placeholder', ADSENSE_PLACEHOLDER_DEFAULT) || ($user->uid == 1)) {
|
| 405 |
$width = array_key_exists('width', $ad) ? $ad['width'] : 0;
|
| 406 |
$height = array_key_exists('height', $ad) ? $ad['height'] : 0;
|
| 407 |
$text = variable_get('adsense_placeholder_text', ADSENSE_PLACEHOLDER_TEXT_DEFAULT) ." ${ad['desc']}";
|
| 408 |
if ($user->uid == 1) {
|
| 409 |
$text = 'Ads disabled for site admin<br />'. $text;
|
| 410 |
}
|
| 411 |
|
| 412 |
$ad = "<!--adsense: placeholder-->\n". _adsense_format_box($text, $width, $height);
|
| 413 |
}
|
| 414 |
else {
|
| 415 |
$ad = '<!--adsense: ads disabled -->';
|
| 416 |
}
|
| 417 |
}
|
| 418 |
elseif (!_adsense_can_insert_another($ad['type'])) {
|
| 419 |
$ad = '<!--adsense: ad limit reached for type-->';
|
| 420 |
}
|
| 421 |
else {
|
| 422 |
// If site Slot ID for this ad was passed, pass the format as argument
|
| 423 |
// in case Publisher ID modules are enabled that can return different
|
| 424 |
// Slot IDs per ad format
|
| 425 |
$client_id_arg = !empty($args['slot']) ? $args['format'] : NULL;
|
| 426 |
$client = adsense_get_client_slot_id($client_id_arg);
|
| 427 |
|
| 428 |
if (is_array($client)) {
|
| 429 |
// An array was received, use that Slot ID
|
| 430 |
$slot = $client['slot'];
|
| 431 |
$client = $client['client'];
|
| 432 |
}
|
| 433 |
elseif (isset($args['slot'])) {
|
| 434 |
// Use the original site Slot ID
|
| 435 |
$slot = $args['slot'];
|
| 436 |
}
|
| 437 |
|
| 438 |
// Ad should be displayed
|
| 439 |
switch ($args['format']) {
|
| 440 |
case 'Search Box':
|
| 441 |
if (!empty($slot) && module_exists('adsense_cse')) {
|
| 442 |
$ad = _adsense_cse_get_searchbox($client, $slot);
|
| 443 |
$module = 'adsense_cse';
|
| 444 |
}
|
| 445 |
elseif (module_exists('adsense_search')) {
|
| 446 |
$ad = _adsense_search_get_searchbox($client, $args['channel']);
|
| 447 |
$module = 'adsense_search';
|
| 448 |
}
|
| 449 |
else {
|
| 450 |
$ad = '<!--adsense: no AdSense for Search module found-->';
|
| 451 |
}
|
| 452 |
break;
|
| 453 |
default:
|
| 454 |
if (!empty($slot) && module_exists('adsense_managed')) {
|
| 455 |
$ad = _adsense_managed_get_ad($args['format'], $client, $slot);
|
| 456 |
$module = 'adsense_managed';
|
| 457 |
}
|
| 458 |
elseif (module_exists('adsense_oldcode')) {
|
| 459 |
$ad = _adsense_oldcode_get_ad($args['format'], $client, $args['group'], $args['channel']);
|
| 460 |
$module = 'adsense_oldcode';
|
| 461 |
}
|
| 462 |
else {
|
| 463 |
$ad = '<!--adsense: no AdSense for Content module found-->';
|
| 464 |
}
|
| 465 |
|
| 466 |
$ad = theme('adsense_ad', $ad, $module);
|
| 467 |
|
| 468 |
break;
|
| 469 |
}
|
| 470 |
// Remove empty lines
|
| 471 |
$ad = str_replace("\n\n", "\n", $ad);
|
| 472 |
}
|
| 473 |
|
| 474 |
return $ad;
|
| 475 |
}
|
| 476 |
|
| 477 |
/**
|
| 478 |
* Default AdSense ad unit theming. Simply add a div with the adsense and $module classes
|
| 479 |
*
|
| 480 |
* @param $ad
|
| 481 |
* string with the generated ad unit
|
| 482 |
* @param $module
|
| 483 |
* module used to generate the ad
|
| 484 |
*
|
| 485 |
* @return
|
| 486 |
* string with the modified ad unit
|
| 487 |
* @ingroup themeable
|
| 488 |
*/
|
| 489 |
function theme_adsense_ad($ad, $module) {
|
| 490 |
return "<div class='adsense $module'>\n$ad\n</div>";
|
| 491 |
}
|
| 492 |
|
| 493 |
/**
|
| 494 |
* Helper function to verify if ads are currently enabled
|
| 495 |
*
|
| 496 |
* @return
|
| 497 |
* TRUE if ad display is enabled, FALSE otherwise
|
| 498 |
*/
|
| 499 |
function _adsense_check_if_enabled() {
|
| 500 |
if (!variable_get('adsense_basic_id', ADSENSE_BASIC_ID_DEFAULT)) {
|
| 501 |
// Google AdSense Publisher ID is not configured
|
| 502 |
return FALSE;
|
| 503 |
}
|
| 504 |
if (variable_get('adsense_disable', ADSENSE_DISABLE_DEFAULT)) {
|
| 505 |
return FALSE;
|
| 506 |
}
|
| 507 |
if (variable_get('adsense_test_mode', ADSENSE_TEST_MODE_DEFAULT)) {
|
| 508 |
return TRUE;
|
| 509 |
}
|
| 510 |
if (variable_get('adsense_secret_adtest', ADSENSE_SECRET_ADTEST_DEFAULT)) {
|
| 511 |
return TRUE;
|
| 512 |
}
|
| 513 |
if (user_access('hide adsense')) {
|
| 514 |
return FALSE;
|
| 515 |
}
|
| 516 |
|
| 517 |
return TRUE;
|
| 518 |
}
|
| 519 |
|
| 520 |
/**
|
| 521 |
* Determine if AdSense has reached limit on this page. As per Google's
|
| 522 |
* policies, a page can have up to 3 ad units and 3 link units.
|
| 523 |
*
|
| 524 |
* @return
|
| 525 |
* TRUE if we can insert another ad, FALSE if not allowed.
|
| 526 |
*/
|
| 527 |
function _adsense_can_insert_another($type = ADSENSE_TYPE_AD) {
|
| 528 |
static $num_ads = array(
|
| 529 |
ADSENSE_TYPE_AD => 0,
|
| 530 |
ADSENSE_TYPE_LINK => 0,
|
| 531 |
ADSENSE_TYPE_SEARCH => 0,
|
| 532 |
);
|
| 533 |
|
| 534 |
$max_ads = array(
|
| 535 |
ADSENSE_TYPE_AD => 3,
|
| 536 |
ADSENSE_TYPE_LINK => 3,
|
| 537 |
ADSENSE_TYPE_SEARCH => 2,
|
| 538 |
);
|
| 539 |
|
| 540 |
if ($num_ads[$type] < $max_ads[$type]) {
|
| 541 |
$num_ads[$type]++;
|
| 542 |
return TRUE;
|
| 543 |
}
|
| 544 |
|
| 545 |
return FALSE;
|
| 546 |
}
|
| 547 |
|
| 548 |
/**
|
| 549 |
* Determine if AdSense has permission to be used on the current page.
|
| 550 |
*
|
| 551 |
* @return
|
| 552 |
* TRUE if can render, FALSE if not allowed.
|
| 553 |
*/
|
| 554 |
function _adsense_page_match() {
|
| 555 |
// Do not show ads on secure pages.
|
| 556 |
// This is for two reasons:
|
| 557 |
// Google would most probably not have indexed secure pages
|
| 558 |
// and it also prevents warnings about mixed-content
|
| 559 |
// Thanks to Brad Konia http://drupal.org/node/29585
|
| 560 |
// Should be restricted when running on Apache only
|
| 561 |
if (isset($_SERVER['SERVER_SOFTWARE']) && (stristr($_SERVER['SERVER_SOFTWARE'], 'Apache') !== FALSE) &&
|
| 562 |
isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on')) {
|
| 563 |
return FALSE;
|
| 564 |
}
|
| 565 |
|
| 566 |
$pages = variable_get('adsense_access_pages', ADSENSE_ACCESS_PAGES_DEFAULT);
|
| 567 |
$visibility = variable_get('adsense_visibility', ADSENSE_VISIBILITY_DEFAULT);
|
| 568 |
|
| 569 |
if ($pages) {
|
| 570 |
if ($visibility == 2) {
|
| 571 |
return drupal_eval($pages);
|
| 572 |
}
|
| 573 |
$path = drupal_get_path_alias($_GET['q']);
|
| 574 |
$page_match = drupal_match_path($path, $pages);
|
| 575 |
if ($path != $_GET['q']) {
|
| 576 |
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
|
| 577 |
}
|
| 578 |
|
| 579 |
return !($visibility xor $page_match);
|
| 580 |
}
|
| 581 |
else {
|
| 582 |
return !$visibility;
|
| 583 |
}
|
| 584 |
}
|
| 585 |
|
| 586 |
/**
|
| 587 |
* Generate a box to display instead of the ad when it is disabled
|
| 588 |
*
|
| 589 |
* @return
|
| 590 |
* string with the HTML text to create the box
|
| 591 |
*/
|
| 592 |
function _adsense_format_box($text, $width, $height) {
|
| 593 |
$dimensions = ((!empty($width)) && (!empty($height)))
|
| 594 |
? " width:". $width ."px; height:". $height ."px;" : "";
|
| 595 |
|
| 596 |
return "<div class='adsense' style='text-align:center;display: table-cell;vertical-align:middle;border:solid 1px;${dimensions}'>${text}</div>";
|
| 597 |
}
|