User:AlphaLaser/ExternalImgExtension

From Unofficial BOINC Wiki

Jump to: navigation, search

Contents

[edit] About

Adds a special tag <external_img> that links an off-site image to the wiki. The image must pass a URL whitelist and namespace test, so the wiki admin can restrict what and where the external images can be placed. This extension originated as a method to get dynamic stats signatures on user pages on a wiki. Now that it is configurable, it can be used for many other purposes as well, such using an off-site image host.

[edit] Demo

For an example of this code in action, see here. The following wiki markup creates that page:

<external_img>http://boincstats.com/signature/user_805748.gif</external_img>

[edit] Installation

Create a file in the ./extensions directory named ExternalImg.php, and paste the following code in it.

./extensions/ExternalImg.php

<?php

/**
 * ExternalImg.php
 *
 * External image extension, allowing a wiki to link to external images using a namespace and URL whitelist.
 *
 * @author InfiniSoft
 */

$wgExtensionFunctions[] = 'ExternalImgExtension';

$wgExtensionCredits['parserhook'][] = array(
  'name' => 'ExternalImgExtension',
  'url' => 'http://boinc-wiki.ath.cx/User:AlphaLaser/ExternalImgExtension'
);

function ExternalImgExtension() {
  global $wgParser;
  $wgParser->setHook('external_img', 'renderExternalImg');
}

function renderExternalImg($input, $argv, &$parser) {
  global $wgExternalImgWhitelist, $wgExternalImgNamespaces;
  
  $out = '';

  if (preg_match('/^(http:\/\/){1}(www\.)?(.+)$/i', $input, $matches) &&
    in_array($parser->mTitle->getNamespace(), $wgExternalImgNamespaces)) {
    $imgUrl = array_pop($matches);
    foreach ($wgExternalImgWhitelist as $url) {
      if (stristr($imgUrl, $url) == $imgUrl) {
        $out = '<img src="' . $input . '" alt="' . $input . '" />';
        break;
      }
    }
  }

  return $out;
  
}

?>

Next, add the following lines of code to the ./LocalSettings.php file.

./LocalSettings.php

/**
 * ExternalImg configuration
 */
# A list of allowed hostnames/paths.  Do not place a www prefix if it exists.
$wgExternalImgWhitelist = array(
    'boincstats.com/signature/',
    'boinc.mundayweb.com/one/',
    'boincsynergy.com/images/stats/',
    'esea.dk/esea/boincschedulers.php'
);

# A list of allowed namespaces for external images.
# For a list of namespace constants look in ./includes/Defines.php
$wgExternalImgNamespaces = array(
    NS_USER,
    NS_USER_TALK
);

include("$IP/extensions/ExternalImg.php");

[edit] Configuration

The allowed URLs and namespaces are set in ./LocalSettings.php. $wgExternalImgWhitelist must include at least the full hostname without the www. prefix, and optionally the path where the allowed images can be linked from. $wgExternalImgNamespaces defines the namespaces where external images are allowed. The numeric IDs are used so you will need to look in the ./includes/Defines.php for the namespace constants. Custom namespace IDs are not tested but should work fine here also.

Personal tools