Subversion Repositories vgwhois

Rev

Rev 5 | Rev 68 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/php
<?php

#
#  VGWhoIs (ViaThinkSoft Global WhoIs, a fork of generic Whois / gwhois)
#  Subprogram: OID over Whois
#
#  (c) 2011-2019 by Daniel Marschall, ViaThinkSoft <info@daniel-marschall.de>
#
#  License: https://www.gnu.org/licenses/gpl-2.0.html (GPL version 2)
#

# OID resolution over WhoIs
# Not really whois, but handy!

# TODO: weitere oid repositories einbinden
# TODO: tabs
# TODO: der encoding anzeigen
# TODO ALVESTRAND_NO fertig machen (404 erkennung, formatierung usw)

require_once __DIR__ . '/../../shared/php_includes/common_functions.inc.php';

define('WALK_UP',            true);
define('ASK_FRANCE_TELECOM', true);
define('ASK_ALVESTRAND_NO',  false);

$domain = $argv[1];

# normalize to "2.999", removing leading zeros, urn:oid: and oid: as well as leading dot
$domain = normalize_oid($domain, false);

// Check if the OID is correct and handle the special case of the root zone "oid:."
$test_oid = explode('.', $domain, 2);
if ($domain === '') {
        echo "You cannot lookup the root zone of the OIDs.\n";
        echo "Please choose at least one of these root OIDs:\n";
        echo "- itu-t(0)\n";
        echo "- iso(1)\n";
        echo "- joint-iso-itu-t(2)\n\n";
        # exit(1);
        exit;
} else {
        $first_arc = $test_oid[0];
        $oid_ok = ($first_arc === '0') || ($first_arc === '1') || ($first_arc === '2');
        if (!$oid_ok) {
                echo "The OID is invalid. The root arc needs to be 0, 1 or 2\n\n";
                # exit(1);
                exit;
        }
}

ob_start();

if (ASK_FRANCE_TELECOM) {
        check_oidinfo_com($domain);
}

if (ASK_ALVESTRAND_NO) {
        if (ASK_FRANCE_TELECOM) {
                echo "\n\n";
                echo "% ===========================================================\n";
                echo "% ===========================================================\n";
                echo "\n";
        }
        check_alvestrand_no($domain);
}

$x = ob_get_contents();
ob_end_clean();

$x = wordwrap($x, 75, "\n", false);
echo html_entity_decode($x, ENT_QUOTES, 'utf-8');
#echo $x;

# ----------------- FRANCE TELECOM OID REPOSITORY

# Callback for Link-Replacing
function fto_link_replacing_cb($treffer) {
        # '[$4] &lt;$2&gt;'

        $name = $treffer[4];
        $link = $treffer[2];

        if (trim(strip_tags($name)) == '') {
                return '';
        }

        # relative -> absolute links
        if (substr($link, 0, 3) == '../') {
                $link = 'http://oid-info.com/'.$link;
        } else if ($link[0] == '/') {
                $link = 'http://oid-info.com'.$link;
        }

        return $name . ' &lt;' . $link . '&gt;';
}

function check_oidinfo_com($oid) {
        $url = "http://oid-info.com/get/$oid";

        $x = file_get_contents2($url);

        $father = fto_find_father($x);

        $err = 'The following error must be corrected to take your request into account:';

        if (strpos($x, $err) !== false) {
                echo "\nError:\n"; # TODO: warum \n am anfang?

                preg_match_all('|<font color="#ff0000">(.*)</font>|ismU', $x, $m);

                $x = implode("\n", $m[1]);

                $x = strip_tags($x);

                $x = trim($x);

                echo $x;

                if (WALK_UP && ($father !== false)) {
                        echo "\n\n";
                        echo "% ===========================================================\n";
                        echo "% Requesting father OID '$father'\n";
                        echo check_oidinfo_com($father);
                }

                return;
        } else {
                if ((strpos($x, '<td>(<a href="/faq.htm#17">ASN.1</a> notation)</td>') === false) &&
                        (strpos($x, 'This OID description has not yet been validated') === false)) {
                        echo "\nUnknown error! Please try again later.\n";

                        if (WALK_UP && ($father !== false)) {
                                echo "\n\n";
                                echo "% ===========================================================\n";
                                echo "% Requesting father OID '$father'\n";
                                echo check_oidinfo_com($father);
                        }

                        return;
                }
        }

        if (strpos($x, 'This OID description has not yet been validated') !== false) {
                // <i>This OID description has not yet been validated by the OID repository administrator and/or the registrant of the superior OID.</i> 
                preg_match_all('|<i>(.+?)</i>|', $x, $m);
                echo $m[1][0];

                if (WALK_UP && ($father !== false)) {
                        echo "\n\n";
                        echo "% ===========================================================\n";
                        echo "% Requesting father OID '$father'\n";
                        echo check_oidinfo_com($father);
                }

                return;
        }

        $x = str_replace('<strong>Short URL for this page</strong>'."\n:", '', $x);
        $x = str_replace('<font size="-2">Webmaster</font></td>', '</td>', $x);
        $x = str_replace('<strong>Disclaimer:</strong>', "\n\n".'<strong>Disclaimer:</strong>'."\n\n", $x);

        $del = '<tr bgcolor="#CCCCCC">';
        $ary = explode($del, $x, 2);
        if (isset($ary[1])) $x = $ary[1];

        echo "% Parsing via regex from '$url'\n\n";

        $x = preg_replace_callback('|<a\s([^>]*?)href="(.*)"([^>]*?)>(.*)</a>|ismU', 'fto_link_replacing_cb', $x);

        $x = strip_tags($x);

        $x = str_replace('&nbsp;', ' ', $x);
        $x = str_replace('&nbsp', ' ', $x);

        $x = html_entity_decode($x);

        $x = preg_replace("| +|", ' ', $x);
        $x = preg_replace("|\n *|", "\n", $x);
        $x = preg_replace("| *\n|", "\n", $x);
        $x = preg_replace("|\n+|", "\n", $x);

        $x = preg_replace('|<mailto:(.*)&(.*)>|ismU', '<$1@$2>', $x);
        $x = str_replace("<#top>\n", '', $x);

        $x = str_replace('(OID-IRI <http://oid-info.com/faq.htm#iri> notation)', '(OID-IRI notation)', $x);
        $x = str_replace('(ASN.1 <http://oid-info.com/faq.htm#17> notation)', '(ASN.1 notation)', $x);
        $x = str_replace('(dot <http://oid-info.com/faq.htm#14> notation)', '(dot notation)', $x);
        $x = str_replace(' <http://oid-info.com/faq.htm#iri>', '', $x);


        $prev_swap_lines = array(
                '(ASN.1 notation)',
                '(dot notation)',
                '(OID-IRI notation)',
        );

        $ary = explode("\n", $x);
        foreach ($prev_swap_lines as $s) {
                foreach ($ary as $n => $l) {
                        if ($l == $s) {
                                $tmp = $ary[$n-1];
                                $ary[$n-1] = $ary[$n];
                                $ary[$n] = $tmp;
                        }
                }
        }
        $x = implode("\n", $ary);

        foreach ($prev_swap_lines as $s) {
                $sx = str_replace('(', '', $s);
                $sx = str_replace(')', '', $sx);
                $x = str_replace("$s\n", "$sx: ", $x);
        }

        $ary = explode("\n", $x);
        $c = count($ary);
        $tmp = $ary[$c-2];
        $ary[$c-2] = "Last modified:\n$tmp";
        $x = implode("\n", $ary);

        $x = preg_replace("|\nFirst Registration Authority$|ismU", "\nFirst Registration Authority:", $x);
        $x = preg_replace("|\nCurrent Registration Authority$|ismU", "\nCurrent Registration Authority:", $x);

        $special_words = array(
                'Name:',
                'Address:',
                'Phone:',
                'Fax:',
                'Creation date:'
        );

        foreach ($special_words as $s) {
                $x = str_replace("$s\n", "$s ", $x);
        }

        $special_words = array(
                'OID:',
                'Information:',
                'Description:',
                'Disclaimer:',
                'Last modified:',
                'First Registration Authority:',
                'Current Registration Authority:'
        );

        foreach ($special_words as $s) {
                $x = str_replace("\n$s\n", "\n\n".strtoupper($s)."\n\n", $x);
        }

        $x = preg_replace("/To contact the (first|current) Registration Authority, replace \"&\" by \"@\" in the email address\n/ismU", '', $x);
        $x = preg_replace("/Short URL for this page:\n/ismU", '', $x);

        $x = trim($x);

$x = utf8_encode($x);

        echo $x;

        if (WALK_UP && ($father !== false)) {
                echo "\n\n";
                echo "% ===========================================================\n";
                echo "% Requesting father OID '$father'\n";
                echo check_oidinfo_com($father);
        }
}

function fto_find_father($content) {

        # Works for...
        # ... root OIDS, e.g. 2
        # ... existing OIDs, e.g. 2.999
        # ... non existing OIDs, 2.999.1 and 2.999.1.2
        # ... orphan OIDs, e.g. 1.3.6.1.4.1.311.1.1.3.1.2
        # ... non validated OIDs...

        $content = str_replace('<img src="/images/bullet.gif" width="10" height="8" border="0"><a href="/basic-search.htm">', '', $content);

        // For non validated OIDs
        $content = str_replace('<br><img src="/images/bullet.gif" width="10" height="8" border="0"><a href="/cgi-bin/manage?father_oid=', '', $content);

        $ary = explode('<font size="+2" color="#FF5500"><strong>OID description</strong></font>', $content, 2);
        $content = $ary[0];

        preg_match_all('|<img src="/images/bullet.gif" width="10" height="8" border="0"><a href="(.*)">|ismU', $content, $m);

        $c = count($m[1]);

        if ($c == 0) return false;

        $last = $m[1][$c-1];
        $last = str_replace('/cgi-bin/manage?action=create&oid=', '', $last);
        $last = str_replace('http://www.oid-info.com/get/', '', $last);
        $last = str_replace('http://oid-info.com/get/', '', $last);

        return $last;
}

# ----------------- ALVESTRAND.NO
# Note that oid-info.com usually contains all OIDs in alvestrands repository.

# todo: + submissions checken (not proofread)
# todo: 404
# todo: fertig
# todo: <p> als \n werten, alles andere als nichts

# Callback for Link-Replacing
function alv_link_replacing_cb($treffer) {
        # '[$4] &lt;$2&gt;'

        $name = $treffer[4];
        $link = $treffer[2];

        if (trim(strip_tags($name)) == '') {
                return '';
        }

        # relative -> absolute links
        if (substr($link, 0, 3) == '../') {
                $link = 'http://www.alvestrand.no/'.$link;
        } else if ($link[0] == '/') {
                $link = 'http://www.alvestrand.no'.$link;
        }

        return $name . ' &lt;' . $link . '&gt;';
}

function check_alvestrand_no($oid) {
        $oid = convert_to_dot($oid);

        $father = oid_up($oid);
        if ($father == $oid) $father = false;

        $url = "http://www.alvestrand.no/objectid/$oid.html";

        $x = file_get_contents2($url);

        echo "% Parsing via regex from '$url'\n\n";

        $x = preg_replace_callback('|<a\s([^>]*?)href="(.*)"([^>]*?)>(.*)</a>|ismU', 'alv_link_replacing_cb', $x);

        $x = strip_tags($x);

        $x = str_replace('&nbsp;', ' ', $x);
        $x = str_replace('&nbsp', ' ', $x);

        $x = html_entity_decode($x);

        $x = preg_replace("| +|", ' ', $x);
        $x = preg_replace("|\n *|", "\n", $x);
        $x = preg_replace("| *\n|", "\n", $x);
        $x = preg_replace("|\n+|", "\n", $x);

        $x = preg_replace('|<mailto:(.*)&(.*)>|ismU', '<$1@$2>', $x);

        $x = preg_replace('|Superior references(.*)Incoming OIDs <submissions> that have not been proofread yet|ismU', '', $x);

        $x = trim($x);

        echo $x;

        if (WALK_UP && ($father !== false)) {
                echo "\n\n";
                echo "% ===========================================================\n";
                echo "% Requesting father OID '$father'\n";
                echo check_alvestrand_no($father);
        }
}

function oid_up($oid) {
        $p = strrpos($oid, '.');
        if ($p === false) return $oid;
        return substr($oid, 0, $p);
}

function convert_to_dot($oid) {
        // {joint-iso-itu-t(2) example(999) 1 2 3} --> 2.999.1.2.3
        // 2.999 --> 2.999
        // joint-iso-itu-t.999 --> 2.999

        $oid = str_replace('.', ' ', $oid);

        # Standardized identifiers
        $oid = " $oid ";
        $oid = str_replace(' ccitt ', '0 ', $oid);
        $oid = str_replace(' itu-r ', '0 ', $oid);
        $oid = str_replace(' itu-t ', '0 ', $oid);
        $oid = str_replace(' iso ', '1 ', $oid);
        $oid = str_replace(' joint-iso-ccitt ', '2 ', $oid);
        $oid = str_replace(' joint-iso-itu-t ', '2 ', $oid);

        $oid = preg_replace("|([^0123456789 ]*)|ismU", "", $oid);
        $oid = trim($oid);
        $oid = str_replace(' ', '.', $oid);
        return $oid;
}