Extension:AdSense

From MediaWiki.org

Jump to: navigation, search
Zeichen 206.svg WARNING: the code or configuration described here poses a major security risk.

Problem: Vulnerable to Cross-site scripting attacks, because it passes user input directly to the browser. This may lead to user accounts being hijacked, among other things.
Solution: strictly validate user input and/or apply escaping to all characters that have a special meaning in HTML
Signed: See talk page Bryan 19:31, 25 July 2008 (UTC)

       

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
AdSense

Release status: experimental

Implementation  Tag
Description This extension allows you to put AdSense ads in your articles.
Author(s)  Naoise Golden
License No license specified
Download see below
Example  live demo

check usage (experimental)

AdSense is a simple extension which allows you to put AdSense ads in your MediaWiki articles.

Contents

[edit] Usage

Please change the code below to include your own publishing code and channel.

<center>
<adsense>
google_ad_client    = 'pub-XXXXXXXXXXXXXXXX';
google_ad_width     = 728;
google_ad_height    = 90;
google_ad_format    = '728x90_as';
google_ad_type      = 'text_image';
google_ad_channel   = '3267063621';
google_color_border = 'FFFFFF';
google_color_bg     = 'FFFFFF';
google_color_link   = '3D81EE';
google_color_text   = '000000';
google_color_url    = '3D81EE';
</adsense>
</center>

For securing your code against anonymous changes, create a template for the AdSense content, or for each type of ad you want to create. Call these Template:728x90_as or some naming strategy so you know the type of ad you are including into your article. Each template should then be protected so that only SysOps can edit and move the templates. Then include your template in any article by using the wiki markup {{728x90_as}}

[edit] Source code

Source code of extensions/adsense.php:

<?php
 
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
 
/**
 * Adsense MediaWiki extension
 *
 * Firstly create a file called adsense.php and place this in the extensions
 * directory - copy all this code to that file.
 * To activate the extension, include it from your LocalSettings.php
 * with: require_once 'extensions/adsense.php';
 *
 * PHP versions 5
 *
 * LICENSE: This source file is subject to version 3.0 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
 * the PHP License and are unable to obtain it through the web, please
 * send a note to license@php.net so we can mail you a copy immediately.
 *
 * @category   Wiki
 * @package    wiki
 * @author     Naoise Golden Santos
 * @copyright  2006 Naoise Golden Santos
 * @license    http://www.php.net/license/3_0.txt PHP License 3.0
 * @version    SVN: $Id: $
 * @link       http://www.goldensantos.com/blog/?p=7
 * 
 * <code>
 * <adsense> 
 * google_ad_client    = "pub-XXXXXXXXXXXXXXXX";
 * google_ad_width     = 728;
 * google_ad_height    = 90;
 * google_ad_format    = "728x90_as";
 * google_ad_type      = "text_image";
 * google_ad_channel   = "3267063621";
 * google_color_border = "FFFFFF";
 * google_color_bg     = "FFFFFF";
 * google_color_link   = "3D81EE";
 * google_color_text   = "000000";
 * google_color_url    = "3D81EE";
 * google_pub_enabled  = "true";
 * </adsense>
 * </code>
 */
 
// Extension credits that show up on Special:Version
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'AdSense',
        'author' => 'Naoise Golden Santos',
        'url' => 'http://www.mediawiki.org/wiki/Extension:AdSense',
        'description' => 'Allows you to put AdSense ads in your articles.'
);
 
/*
 * @todo Document 
 */
$wgExtensionFunctions[] = 'wfAdsense';
 
/**
 * @todo Document
 */
function wfAdsense() {
    global $wgParser;
 
    // registers the <adsense> extension with the WikiText parser
    $wgParser->setHook('adsense', 'renderAdsense');
}
 
/**
 * @todo Document
 */
function renderAdsense($input, $argv) {
    $output  = '<script type="text/javascript">/* <![CDATA[ */';
    $output .= $input;
    $output .= '/* ]]> */</script>';
    $output .= '<script type="text/javascript"';
    $output .= ' src="http://pagead2.googlesyndication.com/pagead/show_ads.js">';
    $output .= '</script>';
 
    return $output;
}

[edit] Bugs

  1. BUG - When you store a Google Ad in its own template and try to include it within another template, a <p> tag is introduced in front of the $input variable.
  2. BUG - Extension talk:AdSense#Raw JavaScript code

[edit] See also