adsense.module

Tracking 6.x-1.x branch
  1. drupal
    1. 6 contributions/adsense/adsense.module

Displays Google AdSense ads on Drupal pages

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

Constants

NameDescription
ADSENSE_ACCESS_PAGES_DEFAULT
ADSENSE_AD_CHANNEL_DEFAULT
ADSENSE_BASIC_ID_DEFAULT
ADSENSE_DISABLE_DEFAULT
ADSENSE_ID_MODULE_DEFAULT
ADSENSE_MAX_CHANNELS
ADSENSE_PLACEHOLDER_DEFAULT
ADSENSE_PLACEHOLDER_TEXT_DEFAULT
ADSENSE_SECRET_ADTEST_DEFAULT
ADSENSE_SECRET_LANGUAGE_DEFAULT
ADSENSE_SECTION_TARGETING_DEFAULT
ADSENSE_TEST_MODE_DEFAULT
ADSENSE_TYPE_AD
ADSENSE_TYPE_LINK
ADSENSE_TYPE_SEARCH
ADSENSE_VISIBILITY_DEFAULT

Functions & methods

NameDescription
adsense_ad_formatsThis is the array that holds all ad formats.
adsense_blockImplementation of hook_block().
adsense_displayGenerates the Google AdSense Ad
adsense_filterImplementation of hook_filter().
adsense_filter_tipsImplementation of hook_filter_tips().
adsense_form_filter_admin_format_form_alterImplementation of hook_form_filter_admin_format_form_alter().
adsense_get_client_slot_idProvides the Google AdSense Publisher ID / slot ID to be used in the ad
adsense_menuImplementation of hook_menu().
adsense_nodeapiImplementation of hook_nodeapi().
adsense_permImplementation of hook_perm().
adsense_requirementsImplementation of hook_requirements().
adsense_themeImplementation of hook_theme().
theme_adsense_adDefault AdSense ad unit theming. Simply add a div with the adsense and $module classes
_adsense_can_insert_anotherDetermine 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_if_enabledHelper function to verify if ads are currently enabled
_adsense_format_boxGenerate a box to display instead of the ad when it is disabled
_adsense_page_matchDetermine if AdSense has permission to be used on the current page.
_adsense_process_tagsHelper function to process the adsense input filter

File

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