Skip to content

Instantly share code, notes, and snippets.

@twxxk
Created December 22, 2008 13:24
Show Gist options
  • Save twxxk/38981 to your computer and use it in GitHub Desktop.
Save twxxk/38981 to your computer and use it in GitHub Desktop.
<?php
/**
* phpinfox.php
* 単一ファイルでphpinfoおよび、入力したコードの実行結果を確認できる
* PHP5用
*
* @author twk
*/
/*
アクセス制御はできれば .htaccess 等に行った方が安全です。
設定例
<Files ~ "^phpinfox\.php$">
order deny,allow
deny from all
allow from 127.0.0.1
allow from 192.168.
</Files>
*/
/** アクセス可能なREMOTE_ADDRの値(前方一致)*/
$phpInfoxAllowedIps = array(
'127.0.0.1', '192.168.',
);
if (get_magic_quotes_gpc()) {
$in = array(&$_GET, &$_POST, &$_COOKIE);
while (list($k,$v) = each($in)) {
foreach ($v as $key => $val) {
if (!is_array($val)) {
$in[$k][$key] = stripslashes($val);
continue;
}
$in[] =& $in[$k][$key];
}
}
unset($in);
}
/**
* TODO PHP内でIPアドレスチェックできるようにする
* TODO PEARや各フレームワーク等の存在チェックと環境確認ができるようにする
* TODO Session等を使い入力履歴を管理できるようにする
* TODO フレームワーク内に組み込めるようにする(iframeを使わないオプション)
* TODO よく使う設定値のみを確認できるようにする
*/
if (!count(array_filter($phpInfoxAllowedIps,
create_function('$addr', 'return strpos($_SERVER["REMOTE_ADDR"], $addr) === 0;'))))
{
header('HTTP/1.0 403 Forbidden');
header('Content-Type: text/plain');
echo "Forbidden ({$_SERVER['REMOTE_ADDR']})";
exit;
}
$gView = array(
'escape' => true,
'parent' => !count($_POST),
'php' => '',
'result' => '',
);
function phpinfox()
{
global $gView;
$php = @$_POST['php'];
if (empty($php)) $php = <<<EOM
\$s = '';
\$s .= rand();
print_r(\$s);
EOM;
$result = '';
$escape = true;
// eval
ob_start();
try
{
$php = trim($php);
if ($php && substr($php, -1, 1) != ';')
$php .= ';';
eval($php);
}
catch (Exception $e)
{
$escape = false;
echo $e->getMessage() . $e->getTraceAsString();
}
$result .= ob_get_clean();
$gView = array(
'escape' => $escape,
'parent' => !count($_POST),
'php' => $php,
'result' => $result,
);
}
function h($var)
{
global $gView;
return $gView['escape'] ? htmlspecialchars($var) : $var;
}
phpinfox();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpinfox</title>
<style>
#phpinfox h1 { font-size: 18px; margin: 0px; background-color: #f0f0f0; }
#phpinfox h2 { font-size: 16px; margin: 0px; margin-right: 0.5em; }
#phpinfox textarea { width: 90%; height: 10em; }
#phpinfox iframe { border: solid 1px #444; width: 90%; }
</style>
</head>
<body>
<?php if ($gView['parent']) : ?>
<?php
// phpinfo handling
ob_start();
phpinfo();
$matches = array();
preg_match('%(<style[^>]*>.*?</style>).*?<body[^>]*>(.*)</body>%s', ob_get_clean(), $matches);
$phpinfo = $matches[1] . $matches[2];
// pear info handling
$pearInfo = 'with PEAR';
ob_start();
try
{
$b = @include_once 'PEAR.php';
if ($b)
$pearInfo = 'without PEAR';
// may produce Fatal error: Call to a member function getVersion() on a non-object
// in PEAR/Info.php on line 57
// $info = new PEAR_Info();
// $info->show();
}
catch (Exception $e){
// echo $e->getMessage() . $e->getTraceAsString();
}
// $pearInfo = ob_get_clean();
?>
<div id="phpinfox">
<h1>phpinfox</h1>
<form method="post" action="phpinfox.php" target="result">
<h2>PHP (<?php echo htmlspecialchars(PHP_VERSION), ' ', $pearInfo; ?>)</h2>
<textarea name="php"
><?php echo htmlspecialchars($gView['php']); ?></textarea><br />
<input type="submit" /><input type="reset" /> echo等で出力するのをお忘れなく。<br />
<br />
<h2>実行結果</h2>
<iframe name="result" src="phpinfox.php"></iframe>
</form>
</div>
<?php echo $phpinfo; ?>
<?php else : ?>
<?php echo h($gView['result']); ?>
<?php endif; ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment