<?php
ob_start();

define ( 'PHP_INFO_SHOW_USERNAMES' , false );
if ( ! empty( $_SERVER['AUTH_USER'] ) ) {
	$_SERVER['AUTH_USER'] = '*********';
}

if ( ! empty( $_SERVER['AUTH_PASSWORD'] ) ) {
	$_SERVER['AUTH_PASSWORD'] = '*********';
}


header( 'Cache-Control: no-cache, no store' );
header( 'Link: </phpinfo.css>; rel=preload; as=style', false );
#header( 'Link: </favicon.ico>; rel=icon', false );

error_reporting( E_ALL );
if ( isset ( $_COOKIE[ ini_get( 'session.name' ) ] ) ) {
	session_start();
}

?><!DOCTYPE HTML>
<html lang="de">
<head>
<meta charset="utf-8">
<link rel="Shortcut Icon" href="/favicon.ico" />
<link rel="Stylesheet" href="/phpinfo.css" />
<meta name="robots" content="noindex,noarchive,nosnippet,noodp,notranslate,noimageindex">
<title><?=$_SERVER['SERVER_NAME']; ?>: PHP-Informationen</title>
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<body>
<h1>🛠 <?=$_SERVER['SERVER_NAME']; ?>: PHP-Informationen<br><span class='subline'><a href="https://code.fastix.org/Projekte/PHP:Replacement%20f%c3%bcr%20phpinfo()/">Tool von Jörg Reinholz</a>, <a href='https://www.fastix.org/'>fastix WebDesign &amp; Consult, Kassel</a>.</span></h1>
<?php if ( isset( $_GET['show_ini_only'] ) && 'true' == $_GET['show_ini_only'] ) { ?>
<h4 id="INI"><a class="toplink" href="?">⮉</a>Ini-Einstellungen: Gelesene Dateien</h4>
<table>
<?php Printline ( 'Gelesene php.ini:' , php_ini_loaded_file() ); ?>


<?php
if ( $filelist = php_ini_scanned_files() ) {
    if ( strlen( $filelist ) > 0) {
?><tr><td> </td><td></td> </tr><?php
        $files = explode(',', $filelist);
        $i=0;
        foreach ($files as $f) {
            Printline ( 'danach (' . ++$i . '.)' , $f );
        }
    }
}
?></table><?php        
?>

<h4>Glesene Ini-Einstellungen:</h4>
<table class="showhover">
	<tbody>
<?php
	iniShow();
?>

	</tbody>
</table><?php
	echo '<h4><a class="toplink" href="#">⮉ </a>(nach oben)</h4>';
	echo "</body>\n</html>\n";
	exit;
} elseif ( isset( $_GET['show_constants_only'] ) && 'true' == $_GET['show_constants_only'] ) {
	echo '<h4><a class="toplink" href="?">⮉</a>Konstanten:</h4>'."\n";
	PrintHash( get_defined_constants( true ) );
	echo '<h4><a class="toplink" href="#">⮉ </a>(nach oben)</h4>';
	echo "</body>\n</html>\n";
	exit;
} elseif ( isset( $_GET['show_linuxlocales_only'] ) && 'true' == $_GET['show_linuxlocales_only'] ) {
	echo '<h4><a class="toplink" href="?">⮉</a>Alle installierten Linux-Locales:</h4>'."\n";
	if ( is_file( '/usr/bin/locale' ) ) {
		PrintHash ( ( new linuxLocales) -> getLocales(), true );
	} else {
		echo "<p>Fehler: Dies scheint kein Linux zu sein.</p>";
	}
	echo '<h4><a class="toplink" href="#">⮉ </a>(nach oben)</h4>';
	echo "</body>\n</html>\n";
	exit;
} elseif ( isset( $_GET['show_apache_only'] ) && 'true' == $_GET['show_apache_only'] ) {
	echo '<h4><a class="toplink" href="?">⮉</a>Apache-Version:</h4>'."\n";
	echo findApache();
	echo '<h4><a class="toplink" href="?">⮉</a>Apache-Module:</h4>'."\n";
	echo '<p>' . str_replace( "\n",', ', trim (`ls /etc/apache2/mods-enabled | grep -v '.conf' | sed -e "s/\.load//"` ) ) . '</p>';
	exit;
} elseif ( isset( $_GET['show_nginx_only'] ) && 'true' == $_GET['show_nginx_only'] ) {
	echo '<h4><a class="toplink" href="?">⮉</a>NGINX-Version:</h4>'."\n";
	echo findNginx();
	echo '<h4><a class="toplink" href="?">⮉</a>NGINX-Module:</h4>'."\n";
	echo '<ul><li>' . str_replace( "\n",'<li>', trim (`ls "/etc/nginx/modules-enabled/" | sed -e "s/\.conf//"  | sed -e "s/^[0-9-]*//"` ) ) . '</ul>';
	exit;	
} elseif ( isset( $_GET['show_system_only'] ) && 'true' == $_GET['show_system_only'] ) {
	echo '<h4><a class="toplink" href="?">⮉</a>Posix: System-Informationen:</h4>'."\n";
	PrintHash( posix_uname(), true );
	echo '<h4><a class="toplink" href="?">⮉</a>Posix: System-Limits:</h4>'."\n";
	PrintHash( posix_getrlimit(), true );
	echo '<h4><a class="toplink" href="#">⮉ </a>(nach oben)</h4>';
	echo "</body>\n</html>\n";
	exit;
}
?>
<nav><a href="#_SERVER">$_SERVER</a>
<?php if ( isset( $_ENV ) and array_keys( $_ENV ) ) { ?>
 | <a href="#_ENV">$_ENV</a>
<?php } ?>
 | <a href="#_GET">$_GET</a>
 | <a href="#_POST">$_POST</a>
 | <a href="#_COOKIE">$_COOKIE</a>
 | <a href="#_SESSION">$_SESSION</a>
 | <a href="#_REQUEST">$_REQUEST</a>
 | <a href="#_FILES">$_FILES</a>
 | <a href="#GLOBALS">$GLOBALS</a>
 </nav><nav>(Nur:) <a href="<?=$_SERVER['PHP_SELF'];?>?show_ini_only=true">Ini-Settings</a>
 | <a href="<?=$_SERVER['PHP_SELF'];?>?show_constants_only=true">Konstanten</a>
 <?php if ( is_file( '/usr/bin/locale' ) ) { ?>
 | <a href="?show_linuxlocales_only=true">Linux-locales</a>
 <?php
 }
if ( findApache() ) {
?>
| <a href="?show_apache_only=true">Apache-Informationen</a>
<?php } else if ( findNginx() ) { ?>
| <a href="?show_nginx_only=true">NGINX-Informationen</a>
<?php } ?>

| <a href="?show_system_only=true">System-Informationen</a>
</nav>
<h4><a class="toplink" href="#">⮉</a>PHP-Version, SAPI und Module:</h4>
<?php
$pattern='/[0-9]+\.[0-9]+/';
preg_match ( $pattern , phpversion(), $matches );
if ( PHP_INFO_SHOW_USERNAMES ) {
?>
<p>PHP <?=$matches[0]; ?> läuft als «<?=php_sapi_name(); ?>» mit den Rechten des Benutzers «<?=posix_getpwuid( posix_geteuid() )['name'];?>» (EUID: <?=posix_geteuid();?>), Gruppe «<?=posix_getgrgid( posix_getegid() )['name'];?>» (EGID: <?=posix_getegid();?>).</p>
<?php }  else { ?>
<p>PHP <?=$matches[0]; ?> läuft als «<?=php_sapi_name(); ?>» mit der EUID: <?=posix_geteuid();?> / EGID: <?=posix_getegid();?>.</p>
<?php
}
unset( $pattern, $matches ); ?>
<p>Geladene PHP-Erweiterungen: «<?=implode("», «", get_loaded_extensions());?>»</p>

<?php showVars (); ?>
<h4><a class="toplink" href="#">⮉ </a>(nach oben)</h4>
</body>
</html><?php


function return_bytes( $val ) {
   $val = trim( $val );
   $last = strtolower( $val[strlen( $val )-1] );
   switch( $last ) {
       // The 'G' modifier is available since PHP 5.1.0
       case 'g':
           $val *= 1024*1024*1024;
       case 'm':
           $val *= 1024*1024;
       case 'k':
           $val *= 1024;
   }
   return $val;
}

function Printline ( $key, $val ) {
	echo "\n" . "	<tr>\n		<td>" . htmlentities($key) . "</td>\n		<td>";
	if ( is_array( $val ) ) {
		PrintHash ( $val, true );
	} else {
		$s = str_replace( ' ', 'ㅤ', htmlentities( $val ) );
		if ( $s ) echo '«' . str_replace( ' ', 'ㅤ', htmlentities( $val ) ) . '»';
	}
	echo "</td>\n\t</tr>";
}

function PrintHash ( $ar, $showhover=false ) {
	$DontShow = [ '_ENV',  '_GET', '_POST', '_COOKIE', '_SESSION', '_REQUEST', '_FILES', '_SERVER', 'GLOBALS' ];
	if ( isset($ar) && is_array( $ar ) && count( $ar ) ) {
		if ( $showhover ) {
			$class=" class='showhover'";
		} else {
			$class="";
		}
		echo '<table' . $class .  ">\n<thead>\n<tr><th>Schl&uuml;ssel</th><th>Wert</th></tr>\n</thead>\n<tbody>";
		foreach ( $ar as $key => $val ) {
			if ( ! in_array( $key, $DontShow ) ) {
				Printline ( $key, $val );
			}
		}

		echo "\n</tbody>\n</table>\n";
	} else {
			echo "(leer)";
	}
}

class linuxLocales {
	private $all;
	function __construct () {
		if (! is_file( '/usr/bin/locale' ) ) {
				return false;
		}
		$rows = explode( "\n", `/usr/bin/locale -a` );
		foreach ( $rows as $row ) {
			if ( $row = trim( $row ) ) {
				$this -> all[] = $row;
			}
		}
	}
	function getLocales( $grep='' ) {
		if ( $grep ) {
			$pattern = '/^' .  $grep . '.*$/';
			$ar =  preg_grep( $pattern,  $this -> all );
			return $ar;
		} else {
			return $this -> all;
		}
	}
}

function iniShow () {
	$arIni = ini_get_all( NULL, true );
	foreach ( array_keys ( $arIni ) as $key ) {
		if ( in_array( $key, getForbiddenIniSettings() )  ){
			echo '
			<tr>
				<td><strong>' . htmlentities( $key ) . '</strong></td>
				<td>***********</td>';
		} elseif ( '' !==  $arIni[$key]['global_value']  && '' !== $arIni[$key]['local_value'] ) {
			echo '
			<tr>
				<td><strong>' . htmlentities( $key ) . '</strong></td>';

			if ( $arIni[$key]['global_value'] == $arIni[$key]['local_value'] )  {
				echo '
				<td>' . htmlentities( str_replace(',', ', ', strval( $arIni[$key]['global_value'] ) ) ) . '</td>';
			} else {
					echo '
				<td><strong>Lokal gültig: '  . htmlentities( str_replace( ',', ', ', $arIni[$key]['local_value']  ) ) . '( wird benutzt)</strong><br>
Global gesetzt: ' . htmlentities( str_replace(',', ', ', strval( $arIni[$key]['local_value'] ) ) ) . '</td>';
			}
		}
	}
}

function showVars () {
	$ar = [ '_GET', '_POST', '_COOKIE', '_SESSION', '_REQUEST', '_FILES', '_SERVER', 'GLOBALS' ];
	if ( isset( $_ENV ) && array_keys( $_ENV ) ) {
		$new = count( $ar );
		$ar[$new] = $ar[ $new - 1 ];
		$ar[ $new - 1 ] = '_ENV';
	}
	foreach ( $ar as $varname ) {
		echo '<h4 id="' . $varname . '"><a class="toplink" href="#">⮉</a>Globale Variable «$' . $varname . '»:</h4>'."\n";

		if ( '_GET' == $varname ) {
			echo '<h5>HTTP_QUERY_STRING:</h5>';
			if ( isset( $_SERVER['QUERY_STRING'] ) && $data = $_SERVER['QUERY_STRING'] ) {
				echo '<pre>' . htmlspecialchars( $data ) . '</pre>';
			} else {
				echo '<div>(leer)</div>';
			}
			echo '<h5>Generiertes $_GET:</h5>';
		}

		if ( '_POST' == $varname ) {
			echo '<h5>php://input :</h5>';
			if ( $data = file_get_contents( 'php://input' ) ) {
				echo '<pre>' . htmlspecialchars( $data ) . '</pre>';
			} else {
				echo '<div>(leer)</div>';
			}
			echo '<h5>Generiertes $_POST:</h5>';
		}
		if ( 'GLOBALS' == $varname ) {
			$t=$ar; array_pop($t);
			echo '<p><strong>Hinweis:</strong> Die enthaltenen Superglobalen «$';
			echo implode('», «$', $t );
			echo '» werden hier nicht erneut angezeigt.</p>';
			unset( $t );
		}

		@eval( 'PrintHash( $' . $varname . ', ( $varname !== "GLOBALS" ) );');

		#$val=$$varname; PrintHash( ( $val), ( $val !== "GLOBALS" ) );
		#PrintHash( $$varname, ( $varname !== "GLOBALS" ) );
	}
}

function findApache() {
	$apache = false;
	foreach ( [
		'/usr/sbin/apache2',
		'/usr/sbin/apache',
		'/usr/sbin/httpd',
		'/usr/sbin/httpd2'
	] as $t ) {
		if ( is_file( $t ) ) {
				$apache = $t;
				unset( $t );
				break;
		}
	}

	if ( $apache && is_file('/etc/apache2/envvars' ) ) {
		$apache = ". /etc/apache2/envvars; $apache";
		return '<p>' . str_replace( "\n", "<br>", `$apache -v` ) . '</p>';
	} else {
		return false;
	}
}

function findNginx() {
	$nginx = false;
	if ( ! is_file( '/usr/sbin/nginx' ) ) {
		return false;
	}
	return '<p>' . $_SERVER['SERVER_SOFTWARE']. '</p>';
}


function getForbiddenIniSettings() {
	return array(
		'mysql.default_user',
		'mysql.default_password',
		'mysqli.default_pw',
		'mysqli.default_user',
		'odbc.default_pw',
		'odbc.default_user'
	);
}
