note 87688 deleted from function.highlight-file by mazzanet

From: Date: Wed, 17 Dec 2008 10:52:43 +0000
Subject: note 87688 deleted from function.highlight-file by mazzanet
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-148247@lists.php.net to get a copy of this message
Note Submitter: norman dot freudenberg at gmx dot de ---- 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>

« previous php.notes (#148247) next »