note 87688 added to function.highlight-file
| From: | norman dot freudenberg at gmx dot de | Date: | Tue, 16 Dec 2008 23:38:34 +0000 |
| Subject: | note 87688 added to function.highlight-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-148236@lists.php.net to get a copy of this message | ||
I wrote a small script which shows the source code of a file, highlighted, auto detecting decoding
and with line numbers.
But it becomes the power with the mod_rewrite of apache2.
Just add ?source after a file and the source will be displayed.
Example: example.com/testing.php?source
The .htaccess File
-----------------------------
RewriteEngine on # Enable mod_rewrite
RewriteCond %{REQUEST_FILENAME} -f # Be sure, that the requestet file exists
RewriteCond %{REQUEST_FILENAME} !show.php # Don`t open the source of show.php itself
RewriteCond %{QUERY_STRING} source # Only show source, If "?source" is the Query
RewriteRule (.*) show.php?filename=$1 [L] # Lets handle the highlighting by PHP Script
The show.php File
------------------------------
<?php
header('content-type: text/html; charset=utf-8;'); // Alway send highlighted Source as
UTF-8
function file_get_contents_utf8($fn) { // Source: http://php.net/manual/de/function.file-get-contents.php#85008
$content = file_get_contents($fn);
return mb_convert_encoding($content, 'UTF-8',
mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}
$error=false;
if(isset($_GET['filename'])) { // Perhabs add here some security checks to permit
reading files like .htpasswd and so on....
$filename=$_GET['filename'];
$string=file_get_contents_utf8($filename);
$lines=count(file($filename));
$title="Sourcecode of ".$filename;
}else {
header("HTTP/1.0 404 Not Found");
$title="Not allowed!";
$error="Do not open this Script directly!";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title><?php echo $title?></title>
<style type="text/css">
.num {
float: left;
color: gray;
text-align: right;
margin-right: 6pt;
padding-right: 6pt;
border-right: 1px solid gray;}
</style>
</head>
<body>
<?php if($error): ?>
<h1 style="color:red">WARNING!</h1>
<div style="background-color: gray; border: 2px red solid; padding: 10px; margin-left:
auto; margin-right:auto;margin-top: 50px;text-align: center;"><?php echo $error
?></div>
<?php else: ?>
<h1>Sourcecode of <?php echo filename?></h1>
<?php echo '<code class="num">', implode(range(1,$lines), '<br
/>'), '</code>'; // Source http://php.net/manual/de/function.highlight-file.php#74018
highlight_string($string); ?>
<?php endif;?>
</body>
</html>
----
Server IP: 69.147.83.197
Probable Submitter: 91.112.104.34
----
Manual Page -- http://www.php.net/manual/en/function.highlight-file.php
Edit -- https://master.php.net/note/edit/87688
Del: integrated -- https://master.php.net/note/delete/87688/integrated
Del: useless -- https://master.php.net/note/delete/87688/useless
Del: bad code -- https://master.php.net/note/delete/87688/bad+code
Del: spam -- https://master.php.net/note/delete/87688/spam
Del: non-english -- https://master.php.net/note/delete/87688/non-english
Del: in docs -- https://master.php.net/note/delete/87688/in+docs
Del: other reasons-- https://master.php.net/note/delete/87688
Reject -- https://master.php.net/note/reject/87688
Search -- https://master.php.net/manage/user-notes.php