Source files | Examples | Dependencies | Parameters | Function arguments
VIEWSRC
The VIEWSRC PHP script generates XHTML
code for displaying "typewriter-styled" (mono-spaced) source code, wrapped in
a <div>
block which maintains the visual appearance of the
page. This script is typically used in order to display the source code within
the same page as, say, the documentation, and has the advantage that it does
not rely on any Java scripts for dynamic change of contents. The XHTML code
generated by the VIEWSRC script conforms to the XHTML 1.0 Strict specification
and makes sure that any special characters present in the code are properly
html:icized before display.
Source files
viewsrc.php
[11 kB]
The entire PHP code for the VIEWSRC script, containing all necessary
routines and inclusions of external resources in one single file.
Current revision is v.1.8, as of 25/12/2010.
Copyright © Fredrik Jonsson 2006-2024, under
GNU General Public
License
[ download |
view source ]
Using the VIEWSRC script
Add the following block into the (X)HTML code for the web page in which you would like to have the the file contents displayed as code (here using the default "en" language option):
<?php include "viewsrc.php"; viewsrc("en"); ?>
Here, the "en" (for English) may be replaced by "sv" (for Swedish).
The viewsrc()
block will remain silent without any action until
a valid MD5 checksum appears
as input value to the 'viewsrc' parameter in the URI. If the checksum is found
to match that of a valid file in the list of allowed files for display, this
will generate a monospaced output of the contents of the file. Example of a
valid URI employing viewsrc
, where the input value ("338...68") is
the MD5 checksum of the filename 'cv.tex
':
http://jonsson.eu/about/?viewsrc=338066b225e308fc980f5a7327765c68
The page you get via this URI is constructed using the following PHP code (with
the viewsrc
blocks emphasized);
<?php
$title="....";
$keywords="....";
$siteroot=$_SERVER['DOCUMENT_ROOT'];
if (($lang=$_GET["lang"])=="") $lang=en; // English default
include $siteroot."/php/pageheader.php";
include $siteroot."/php/navbar.php";
include $siteroot."/php/viewsrc.php";
include $siteroot."/php/viewimg.php";
include $siteroot."/php/langswitch.php";
include $siteroot."/php/prevpage.php";
include $siteroot."/php/footer.php";
pageheader($lang,$title,$keywords);
?>
<body id="oneColLayout">
<div id="container">
<?php
navbar($siteroot,$lang);
viewsrc($lang);
viewimg($lang);
?>
<div id="primaryContent" class="biography" >
<?php langswitch($lang); ?>
. . . .
. . . .
. . . .
<?php prevpage($lang); ?>
</div> <!-- End of primaryContent div -->
<div id="sideContent">
. . . .
. . . .
. . . .
</div> <!-- End of sideContent div -->
<?php footer($lang); ?>
</div>
</body>
</html>
The viewsrc()
script will then typically generate (X)HTML output
of the following form:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<body id="oneColLayout">
<div id="sourceView">
<p><b>Contents of file 'cv.tex':</b></p>
<pre>
<tt>
<code>
[Your code in 'cv.tex' goes here.]
</code>
</tt>
</pre>
</div> <!-- End of sourceView div -->
</body>
</html>
Dependencies
For proper operation, the viewsrc()
function relies on the
presence of the following PHP scripts, publicly available at
http://jonsson.eu/programs/php/:
Parameters and configuration
All user-customizable parameters of the script are present immediately after the initial header in viewsrc.php. The significance of the parameters are as follows:
$display_linenumbers
Set the $display_linenumbers parameter to 'true' to have the viewsrc script displaying line numbers along the code to be displayed. Otherwise, the input source will be displayed clean, without any linenumbering.
$allowedfiles[...]
Defines the files allowed for display by the VIEWSRC script. These are set as global-level character strings which are accessed by
$GLOBALS[...]
. All files valid for viewing are here hardcoded as a list directly in the PHP source, to avoid any potential security issues from loading data from external text files (unless these files are already protected by the PHP engine, that is to say). For further details, see "PHP in a Nutshell" by Paul Hudson (O'Reilly, 2005). Example:$allowedfiles[1]="Makefile"; $allowedfiles[2]="magbragg.w"; . . . $allowedfiles[49]="koch/Makefile";
Function arguments
In the viewsrc($lang)
function, the significance of the input
argument is as follows:
$lang
Determines the language to use for headers in generation of the quote(s). Accepted values are "en" (for English) or "sv" (for Swedish).