diff options
| author | root | 2015-04-01 23:53:58 +0200 |
|---|---|---|
| committer | root | 2015-04-01 23:53:58 +0200 |
| commit | a31396fb8c67f27a0055e4c4a6fa95e3cde24d1a (patch) | |
| tree | 8e5f72013ffe1fda6ab64514a79943f83d841e51 /ip | |
| parent | 6ec371bff3d3eda0c535fe773c292f66bd746a7a (diff) | |
| download | tools.iamfabulous.de-a31396fb8c67f27a0055e4c4a6fa95e3cde24d1a.tar.gz | |
Small improvements.
Diffstat (limited to 'ip')
| -rw-r--r-- | ip/ifconfig.php | 60 | ||||
| -rw-r--r-- | ip/index.php | 60 | ||||
| -rw-r--r-- | ip/template.php | 132 |
3 files changed, 252 insertions, 0 deletions
diff --git a/ip/ifconfig.php b/ip/ifconfig.php new file mode 100644 index 0000000..33ec5ba --- /dev/null +++ b/ip/ifconfig.php @@ -0,0 +1,60 @@ +<?php + +class Ifconfig { + + public $ip; + + public function __construct($ip){ + $this->ip=$ip; + } + + public function getHeader(){ + $headers = ''; + foreach ($_SERVER as $name => $value){ + if (substr($name, 0, 5) == 'HTTP_'){ + $headers[str_replace(' ', '-', ucwords( strtolower( str_replace( '_', ' ', substr( $name, 5) ) ) ) )] = $value; + } + } + return $headers; + } + + public function getAllGeoipRecords(){ + + $tmp = geoip_record_by_name( $this->ip ); + if( $tmp ){ + unset($tmp['dma_code']); + unset($tmp['area_code']); + } else { + $tmp = array ( "status" => "Data not available"); + } + + return $tmp; + } + + public function encodeJson($value){ + + if( empty($value) || is_null($value) || $value == "" ) + return json_encode($output = array( "status" => "failure" )); + else + $output = array( "status" => "success" ); + + if ( ! is_array($value) ){ + $output[] = $value; + } else { + $output = array_merge($output, $value); + } + + return json_encode($output); + } + + public function getReverseDNS($ip = NULL){ + if ( is_null($ip) ) + $ip = $this->ip; + + return gethostbyaddr($ip); + } + + public function __destruct(){ + return true; + } +} diff --git a/ip/index.php b/ip/index.php new file mode 100644 index 0000000..a390ec2 --- /dev/null +++ b/ip/index.php @@ -0,0 +1,60 @@ +<?php + +if ( strpos($_SERVER['HTTP_USER_AGENT'], 'curl') !== false ){ + if ( empty($_GET) ){ + echo $_SERVER["REMOTE_ADDR"]; + exit; + } +} + +if ( empty($_GET) ){ + header($_SERVER["SERVER_PROTOCOL"] . " 302 Moved"); + header("Location: http://".$_SERVER["HTTP_HOST"].str_replace("/index.php", "", $_SERVER["PHP_SELF"] )."/?ip=".$_SERVER["REMOTE_ADDR"]."&geoip=1&header=1&output=html"); + exit; +} + +if ( ! isset($_GET["ip"]) || $_GET["ip"] == "" ){ + $ip = $_SERVER["REMOTE_ADDR"]; +} else { + if( preg_match("/([0-9]+\.){3}[0-9]+/", $_GET["ip"]) ){ + $ip = $_GET["ip"]; + } else { + $ip = gethostbyname($_GET["ip"]); + } +} + +require 'ifconfig.php'; + +$if = new Ifconfig($ip); + +$result = array("ip" => $ip, "hostname" => $if->getReverseDNS()); + +if( isset($_GET["geoip"]) && $_GET["geoip"] != 0){ + $geoip = $result = array_merge($result, $if->getAllGeoipRecords()); +} + +if( isset($_GET["header"]) && $_GET["header"] != 0){ + $header = $if->getHeader(); + $result = array_merge($result, $header); +} + +if( ! isset($_GET["output"]) || $_GET["output"] == "" ){ + $_GET["output"] = "html"; +} + +switch($_GET["output"]){ + case("json"): + header("Content-Type: application/json"); + echo $if->encodeJson($result); + break; + case("html"): + require 'template.php'; + break; + case("php"): + header("Content-Type: text/plain"); + echo serialize($result); + break; + default: + require 'template.php'; + break; +} diff --git a/ip/template.php b/ip/template.php new file mode 100644 index 0000000..50e9b74 --- /dev/null +++ b/ip/template.php @@ -0,0 +1,132 @@ +<!doctype html> +<html> +<head> + <meta charset="utf-8"> + <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> + <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"> + <style> + <?php echo file_get_contents("../tools/style.css"); ?> + </style> + <noscript><style>.navbar{margin-bottom:0;}</style></noscript> + <title>IP API</title> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <link rel='shortcut icon' href='../tools/favicon.ico' type='image/x-icon'> +</head> +<body> + <?php require("../tools/navbar.php"); ?> +<div class="container"> + <div class="text-center"> + <div class="row"> + <h1>Ifconfig</h1> + <h3>Basic IP API/Look up.</h3> + <hr> + <?php if( isset($geoip) ){ + ?> + + <h3 class="text-left"><strong>IP API</strong></h3> + <dl class="dl-horizontal"> + <?php + foreach($geoip as $key => $value){ + echo "<dt>".str_replace('_', ' ', ucfirst($key))."</dt><dd class='text-left'>".$value."</dd>"; + } + ?> + </dl> + <?php + } + if ( isset($header) ){ + ?> + <h3 class="text-left"><strong>HTTP Header</strong></h3> + <dl class="dl-horizontal"> + <?php + foreach($header as $key => $value){ + echo "<dt>".$key."</dt><dd class='text-left'>".$value."</dd>"; + } + ?> + </dl> + <?php + } + ?> + + <form class="form-horizontal"> + <fieldset> + + <!-- Form Name --> + <legend></legend> + + <!-- Text input--> + <div class="form-group"> + <label class="col-md-4 control-label" for="input">IP/Domain</label> + <div class="col-md-4"> + <input id="input" name="ip" placeholder="Look up a host based on IP or FQDN." class="form-control input-md" type="text"> + <!--span class="help-block text-left">Look up a host based on IP or FQDN.</span--> + </div> + </div> + + + <div class="text-left"> + <div class="form-group"> + <label class="col-md-4 control-label" for="geoip">Show GeoIP Data</label> + <div class="col-md-4"> + <input id="geoip" name="geoip" value="1" type="checkbox" checked> + </div> + </div> + </div> + + <div class="text-left"> + <div class="form-group"> + <label class="col-md-4 control-label" for="requestheader">Show Request Header</label> + <div class="col-md-4"> + <input id="requestheader" name="header" value="1" type="checkbox" checked> + </div> + </div> + </div> + +<div class="text-center"> +<!-- Multiple Radios (inline) --> +<div class="form-group text-left"> + <label class="col-md-4 control-label" for="output"></label> + <!--label class="col-md-4" for="output">Output Format</label--> + <div class="col-md-4"> + <label class="radio-inline" for="output-0"> + <input name="output" id="output-0" value="html" checked="checked" type="radio"> + HTML + </label> + <label class="radio-inline" for="output-1"> + <input name="output" id="output-1" value="json" type="radio"> + JSON + </label> + <label class="radio-inline" for="output-2"> + <input name="output" id="output-2" value="php" type="radio"> + PHP (serialized) + </label> +<span class="help-block text-left">Choose output format.</span> + </div> +</div> +</div> + + <!-- Button --> + <div class="form-group"> + <label class="col-md-4 control-label" for=""></label> + <div class="col-md-4"> + <button id="" name="" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Look Up</button> + </div> + </div> + + <!--input type="hidden" name="output" value="html"--> + + </fieldset> + </form> + <hr> + + <h4>Command Line Interface: </h4> + <!--p class="">Command Line Interface: <code>$ curl http://tools.iamfabulous.de/ifconfig/</code></p--> + Get IP: <code>$ curl http://tools.iamfabulous.de/ifconfig/</code> => <?php echo $_SERVER['REMOTE_ADDR']; ?><br> + JSON: <code>$ curl http://tools.iamfabulous.de/ifconfig/?output=json</code><br> + Get everything: <code>$ curl http://tools.iamfabulous.de/ifconfig/?geoip=1&header=1&output=json</code> + <!--p class="">JSON: <code>$ curl http://tools.iamfabulous.de/ifconfig/&geoip=1&header=1&output=json</code></p--> + + </div> + </div> +</div> + + <?php require("../tools/footer.php"); ?> |
