Open main menu

Extension:DNSlookup

MediaWiki extensions manual
OOjs UI icon advanced.svg
DNSlookup
Release status: beta
Implementation Parser extension
Description Parser and tag functions to perform DNS lookups on hostnames or IP addresses
Author(s) (Jbrandttalk)
Latest version 0.2
License GPL
Download Extension:DNSlookup#Code
Translate the DNSlookup extension if it is available at translatewiki.net
Check usage and version matrix.

What can this extension do?Edit

This extension adds a very simple DNS lookup using the PHP gethostbyname or gethostbyaddr functions depending on if it is called with a hostname or an IP address. It can be used either through a parser function or through a tag.

UsageEdit

{{ #dnslookup: www.example.com }}
{{ #dnslookup: 127.0.0.1 }}
<dns lookup='www.example.com' \>
<dns lookup='127.0.0.1' \>
<dns>www.example.com</dns>
<dns>127.0.0.1</dns>

It will print the IP address or the reverse lookup of the IP address given. If the parameter does not resolve, it will print an error.

Download instructionsEdit

Please cut and paste the code found below and place it in $IP/extensions/DNSlookup.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

InstallationEdit

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/DNSlookup.php");

CodeEdit

<?php

$wgExtensionCredits['parserhook'][] = array(
     'name' => 'DNSlookup',
     'author' =>'Josh Brandt', 
     'url' => 'http://www.mediawiki.org/wiki/Extension:DNSlookup', 
     'description' => 'Adds simple dnslookup functions for either hostnames or IP addresses.',
     'version' => '0.2',
      );
 
# Define a setup function
$wgHooks['ParserFirstCallInit'][] = 'efDNSlookup_Setup';
# Add a hook to initialise the magic word
$wgHooks['LanguageGetMagic'][]       = 'efDNSlookup_Magic';

 
function efDNSlookup_Setup() {
        global $wgParser;
        # Set a function hook associating the "example" magic word with our function
        $wgParser->setFunctionHook( 'dnslookup', 'efDNSlookup_Render' );
        $wgParser->setHook( 'dns', 'efDNSlookup_Tag');
        return true;
}
 
function efDNSlookup_Magic( &$magicWords, $langCode ) {
        $magicWords['dnslookup'] = array( 0, 'dnslookup' );
        return true;
}

function efDNSlookup_wrapper( $lookup = '' ) {
        if (($binIp = ip2long($lookup)) === false) { 
          # we were given something that isn't a valid IP address-- try it as a hostname.
          $output = gethostbyname($lookup);
          # gethostbyname returns waht it was given if it doesn't resolve. 
          # so, if the paramter is the same as the return, we complain that
          # it doesn't resolve.
          if ( $lookup == $output ) {
               $output = "<span style=\"color: red\">".
                               "Invalid DNS lookup: ".$lookup."</span>";
            }
        } else { 
          # we were given what looks like a valid IP address, so let's find the reverse.
          $output = gethostbyaddr($lookup);
          # gethostbyaddr will return the address if it doesn't find a reverse for it.
          # since we may be building a URL around this, we go ahead and return
          # the address even if it doesn't resolve. 
        } 

        return $output;

}
 
function efDNSlookup_Render( &$parser, $param1 = '' ) {
        # Disable caching in case the address is changing.
        $parser->disableCache();
        $efDNSlookupreturn=efDNSlookup_wrapper($param1);
        return ($efDNSlookupreturn);
}

function efDNSlookup_Tag ( $text, $args, &$parser ) {
        $parser->disableCache();
        $attr = array();    
        # if we have specified a lookup attribute, we look that up and return.
        foreach( $args as $name => $value ) {
            if ( $name == 'lookup') 
                $output=efDNSlookup_wrapper($value);
                return($output);
        }            
        # if we haven't, we try the text instead.
        $output=efDNSlookup_wrapper($text);
        return($output);

}