Commit f3a30e3333
Unsigned
Layout: unified · split
ifconfig.php added +123
| @@ -0,0 +1,123 @@ | ||
| 1 | <?php | |
| 2 | /* | |
| 3 | * User connection info | |
| 4 | * | |
| 5 | * Version: 1.0 2014/03/06 | |
| 6 | * License: GPL v2 (https://www.gnu.org/licenses/gpl-2.0.html) | |
| 7 | * | |
| 8 | * Copyright (C) 2014 Sergey Briskin (http://briskin.org) | |
| 9 | * | |
| 10 | * This program is free software; you can redistribute it and/or | |
| 11 | * modify it under the terms of the GNU General Public License | |
| 12 | * as published by the Free Software Foundation; either version 2 | |
| 13 | * of the License, or (at your option) any later version. | |
| 14 | * | |
| 15 | * This program is distributed in the hope that it will be useful, | |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 18 | * GNU General Public License for more details. | |
| 19 | * | |
| 20 | * You should have received a copy of the GNU General Public License | |
| 21 | * along with this program; if not, write to the Free Software | |
| 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
| 23 | * | |
| 24 | */ | |
| 25 | ||
| 26 | // Create array of values | |
| 27 | $user = array( | |
| 28 | 'ip' => $_SERVER['REMOTE_ADDR'], | |
| 29 | 'host' => (isset($_SERVER['REMOTE_ADDR']) ? gethostbyaddr($_SERVER['REMOTE_ADDR']) : ""), | |
| 30 | 'port' => $_SERVER['REMOTE_PORT'], | |
| 31 | 'ua' => $_SERVER['HTTP_USER_AGENT'], | |
| 32 | 'lang' => $_SERVER['HTTP_ACCEPT_LANGUAGE'], | |
| 33 | 'mime' => $_SERVER['HTTP_ACCEPT'], | |
| 34 | 'encoding' => $_SERVER['HTTP_ACCEPT_ENCODING'], | |
| 35 | 'charset' => $_SERVER['HTTP_ACCEPT_CHARSET'], | |
| 36 | 'connection' => $_SERVER['HTTP_CONNECTION'], | |
| 37 | 'cache' => $_SERVER['HTTP_CACHE_CONTROL'], | |
| 38 | 'cookie' => $_SERVER['HTTP_COOKIE'], | |
| 39 | 'referer' => $_SERVER['HTTP_REFERER'], | |
| 40 | 'real_ip' => $_SERVER['HTTP_X_REAL_IP'], | |
| 41 | 'fwd_ip' => $_SERVER['HTTP_X_FORWARDED_FOR'], | |
| 42 | 'fwd_host' => (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? gethostbyaddr($_SERVER['HTTP_X_FORWARDED_FOR']) : ""), | |
| 43 | 'dnt' => $_SERVER['HTTP_DNT'] | |
| 44 | ); | |
| 45 | ||
| 46 | // Single HTTP-HEADER value request (ex. ifconfig.php?q=ip) | |
| 47 | $rqst=trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($_GET['q'])))))); | |
| 48 | ||
| 49 | // Output format (ex. ifconfig.php?f=xml) | |
| 50 | $type=trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($_GET['f'])))))); | |
| 51 | ||
| 52 | // Return Single value on request & die | |
| 53 | if (isset($rqst) && !empty($user[$rqst]) && array_key_exists($rqst, $user)) { | |
| 54 | die($user[$rqst]); | |
| 55 | } | |
| 56 | unset($rqst); | |
| 57 | ||
| 58 | // Return full output in one of supported formats (xml, json, plain text. default: html) | |
| 59 | if (isset($type) && ($type=="text")) { | |
| 60 | header('Content-Type: text/plain'); | |
| 61 | foreach($user as $key => $value) { | |
| 62 | echo $key.": ".$value."\n"; | |
| 63 | } | |
| 64 | die(); | |
| 65 | } elseif (isset($type) && ($type=="json")) { | |
| 66 | header('Content-Type: application/json'); | |
| 67 | die(json_encode($user)); | |
| 68 | } elseif (isset($type) && ($type=="xml")) { | |
| 69 | header('Content-Type: text/xml'); | |
| 70 | ||
| 71 | // Function for SimpleXML creation | |
| 72 | function array_to_xml(array $arr, SimpleXMLElement $xml) | |
| 73 | { | |
| 74 | foreach ($arr as $k => $v) { | |
| 75 | is_array($v) | |
| 76 | ? array_to_xml($v, $xml->addChild($k)) | |
| 77 | : $xml->addChild($k, $v); | |
| 78 | } | |
| 79 | return $xml; | |
| 80 | } | |
| 81 | echo array_to_xml($user, new SimpleXMLElement('<info/>'))->asXML(); | |
| 82 | ||
| 83 | } else { | |
| 84 | header('Content-Type: text/html'); | |
| 85 | ?><!DOCTYPE html> | |
| 86 | <html lang="en"> | |
| 87 | <head> | |
| 88 | <meta charset="UTF-8"> | |
| 89 | <title>info</title> | |
| 90 | <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/> | |
| 91 | <style> | |
| 92 | body { | |
| 93 | background: #FDFDFD; | |
| 94 | color: #111; | |
| 95 | font-size: .9em; | |
| 96 | } | |
| 97 | p { | |
| 98 | font-family: "Source Code Pro", "Courier New", "Consolas", "Terminal", "Droid Mono", fixed; | |
| 99 | line-height: 1em; | |
| 100 | } | |
| 101 | .small, .small > a { | |
| 102 | font-size: 9pt; | |
| 103 | color: #777; | |
| 104 | text-align: center; | |
| 105 | text-decoration: none; | |
| 106 | } | |
| 107 | </style> | |
| 108 | </head> | |
| 109 | <body> | |
| 110 | <? | |
| 111 | foreach($user as $key => $value) { | |
| 112 | echo ' <p id='.$key.'>'.$key.': '.$value.'</p>'."\n"; | |
| 113 | } | |
| 114 | ?> | |
| 115 | <br/> | |
| 116 | <p class="small">Copyright © 2014 <a href="http://briskin.org">Sergey Briskin</a>.<br/> | |
| 117 | <a href="https://github.com/sbriskin/ifconfig.php">Get Source code on GitHub</a></p> | |
| 118 | </body> | |
| 119 | </html> | |
| 120 | <? | |
| 121 | } | |
| 122 | die(); | |
| 123 | ?> | |