<?php
/*********************************************************
Script Name: Quick & Dirty PHP Source Printer
Author: Kaf Oseo
Author URI: http://szub.net
This PHP script is intended for printing out the source for
plugins of WordPress (http://wordpress.org), but is generic
enough that one can modify it to use with any PHP files.
You may need to modify the $path variable, but be cautious.
This script can potentially read any file on a site, so you
should restrict its access to directories and files that do
not hold sensitive information.
WORDPRESS USERS
(or those interested in how it works)
First of all, this script is not a plugin! Place source.php
in your WordPress home directory. If your plugins are found
in the default location (wp-content/plugins/), there should
be no need to change the $path variable.
Here is an example script URL:
http://my.wordpress.site/source.php?file=hello.php
Any PHP file in the plugin directory you aim at through the
"file=" query displays in the browser, gussied up thanks to
the syntax highlighting from the PHP command show_source().
To assign line numbers, add '&ln' to your query:
http://my.wordpress.site/source.php?file=hello.php&ln
Finally, when it comes to filenames, Be CaSe SeNsItIvE!
~Changelog:
R1.1.1 (4-Jul-2005)
Security release. Additional shoring up of path traversal
attack filtering.
R1.1 (3-Jul-2005)
Security release. Original path traversal attack filtering
could be bypassed. Thanks to Seth Alan Woolley for finding
the exploit.
R1 (24-Oct-2004)
First release.
**********************************************************/
/* Set $path to your WP plugins directory. */
$path = 'wp-content/plugins/';
if ((!isset($_GET['file'])) || ($_GET['file'] == '')) {
$title = 'No source file specified';
} else {
$file_get = $_GET['file'];
$file = (strstr($file_get, '..') == true) ? NULL : $file_get; // protect from site traversing
$plugin = $path.$file;
$title = 'Source for '.$file_get;
}
?>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<?php
if (!empty($_GET['file'])) {
if (is_null($file) || !file_exists($plugin)) {
echo '<h3>Cannot locate '.$file_get.'</h3>';
} else {
if (isset($_GET['ln'])) {
$source = highlight_file($plugin, true);
highlight_file_linenum($source);
} else {
show_source($plugin);
}
}
}
?>
</body>
</html>
<?php
/*
Thanks go to Aidan Lister for the line numbering function.
http://aidan.dotgeek.org/lib/?file=function.highlight_file_linenum.php
*/
function highlight_file_linenum($data, $funclink = true, $return = false) {
// Init
$data = explode ('<br />', $data);
$start = '<span style="color: #9a9;">';
$end = '</span>';
$i = 1;
$text = '';
// Loop
foreach ($data as $line) {
$text .= $start . $i . ' ' . $end .
str_replace("\n", '', $line) . "<br />\n";
++$i;
}
// Optional function linking
if ($funclink === true) {
$keyword_col = ini_get('highlight.keyword');
$manual = 'http://www.php.net/function.';
$text = preg_replace(
// Match a highlighted keyword
'~([\w_]+)(\s*</span>)'.
// Followed by a bracket
'(\s*<span\s+style="color: ' . $keyword_col . '">\s*\()~m',
// Replace with a link to the manual
'<a href="' . $manual . '$1">$1</a>$2$3', $text);
}
// Return mode
if ($return === false) {
echo $text;
} else {
return $text;
}
}
?>