UQ Students should read the Disclaimer & Warning
Note: This page dates from 2005, and is kept for historical purposes.
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>How this site works</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
.wrong {
background: #FF9999;
}
body {
background: url(_img/DSC04989.jpg) fixed center;
font-family: "Arial Unicode MS", Arial, Helvetica, sans-serif;
}
th, td, textarea {
border: 1px solid #000000;
padding: 0 1ex;
background: transparent;
overflow: hidden;
}
table {
border: none;
}
acronym {
border-bottom: 1px dashed #0000CC;
cursor: help;
}
/* Mobile rectification 2020-10-25 */
@media screen and (max-width: 768px) {
textarea {
max-width: calc(100vw - 57px);
height: auto !important;
overflow: auto;
}
}
-->
</style>
</head>
<body>
<h1>How this site works</h1>
<p><a href="#templates" title="Site Template system">Templates</a> | <a href="#comments" title="Site comments system">Comments</a> | <a href="#code" title="Code examples">Working
Code</a></p>
<h2 id="templates">Templates</h2>
<p>Apache's <acronym title="Uniform Resource Locator">URL</acronym> rewriting
engine, <a href="http://httpd.apache.org/docs/mod/mod_rewrite.html" title="External link - Apache's mod_rewrite module">mod_rewrite</a>,
is used to send all <acronym title="Uniform Resource Locator">URL</acronym>'s
to an index.php file. This is the mod_rewrite used on this site.</p>
<p>
<textarea cols="80" rows="12" readonly="readonly" title="mod_rewrite code">RewriteEngine on
Options +FollowSymlinks
RewriteBase /
# Allow subdirectories
RewriteRule ^(.*/.*) - [L]
# Allow plain index.php?blah addresses
RewriteRule ^(index.php.*) - [L]
# Replace blah with index.php?blah
RewriteRule ^(.+)$ index.php?$1
</textarea>
</p>
<p>The <acronym title="Uniform Resource Locator">URL</acronym>'s are parsed by
the <acronym title="PHP Hypertext Preprocessor (server-side scripting language)">PHP</acronym>
file, and split around any periods in the <acronym title="Uniform Resource Locator">URL</acronym>
and stored in an array.</p>
<p>
<textarea cols="80" rows="3" readonly="readonly" title="query string parsing code">
if ($_SERVER['QUERY_STRING']) {
$array = explode(".",$_SERVER['QUERY_STRING']);
}
</textarea>
</p>
<p>Care must always be taken to ensure that anything able to be externally passed
into a file isn’t malicious. This function attempts to find <acronym title="PHP Hypertext Preprocessor (server-side scripting language)">PHP</acronym>
and <acronym title="Hyper Text Markup Language">HTML</acronym> files, and
returns a default <acronym title="PHP Hypertext Preprocessor (server-side scripting language)">PHP</acronym>
file if none are found. This can prevent anyone attempting to access anything
unusual.</p>
<p>
<textarea cols="80" rows="7" readonly="readonly" title="file verification code">
function verify_file($file) {
switch(true) {
case file_exists($file.'.php'): return $file.'.php';
case file_exists($file.'.html'): return $file.'.html';
default: return 'default.php';
}
}</textarea>
</p>
<p>Files are stored as <acronym title="Extensible Hypertext Markup Language">XHTML</acronym>
or <acronym title="eXtensible Markup Language">XML</acronym>. <acronym title="Extensible Hypertext Markup Language">XHTML</acronym>
files are parsed for their body, style and title tags and these tags are then
included into the resulting output. This allows a simple template system where
the content can be created with any <acronym title="Extensible Hypertext Markup Language">XHTML</acronym>
capable editor. I have found this to be the simplest way of creating a consistent
and easily updateable style across a site.</p>
<p>
<textarea cols="80" rows="24" readonly="readonly" title="tag parsing code">
// Parse $file for tags
function include_tag($tag, $file) {
if (!file_exists($file)) {
$file = verify_file($file);
}
$data = file_get_contents($file);
preg_match(
"/<$tag.*>(.+)<\/$tag>/sU",
$data,
$matches
);
eval('?>'.$matches[1]);
}
// Title
print '<title>n e d m a r t i n - UNI - ';
include_tag('title',$file);
print '</title>';
// Style
if ($type == 'default') {
print '<style type="text/css">';
include_tag('style',$file);
print '</style>';
</textarea>
</p>
<p>There are several advantages and limitations to this approach.</p>
<p>Disadvantages include:</p>
<ul>
<li>Slightly more processing is required to parse a file, rather than serving
straight <acronym title="Hyper Text Markup Language">HTML</acronym></li>
<li>Initial design is more complex</li>
</ul>
<p> Advantages include:</p>
<ul>
<li>Easily updateable</li>
<li>Easy to maintain</li>
<li>Consistent across an entire site</li>
<li>Content generation possible with the majority of “webpage”
<acronym title="Hyper Text Markup Language">HTML</acronym> editors, as
well as by hand</li>
<li>Foreign content assumes a consistent style after parsing</li>
<li>Possible to create complex file hierarchies without the user being aware
</li>
<li> Allows the logical file system to change without affecting existing <acronym title="Uniform Resource Locator">URL</acronym>’s</li>
<li> Does not limit <acronym title="Uniform Resource Locator">URL</acronym>’s
to <acronym title="Hyper Text Markup Language">HTML</acronym>, <acronym title="PHP Hypertext Preprocessor (server-side scripting language)">PHP</acronym>
or any fixed extension. In fact, using <a href="this-site">this-site</a>,
<a href="this-site.html">this-site.html</a>, <a href="this-site.php">this-site.php</a>
and even <a href="this-site.no-known-file.extension">this-site.no-known-file.extension</a>
as links will not affect the page that is displayed.</li>
<li>Allows an easy upgrade from existing non-template sites, as existing “legacy”
<acronym title="Uniform Resource Locator">URL</acronym>’s will continue
to work alongside the newer <acronym title="Uniform Resource Locator">URL</acronym>’s,
and not all existing pages need be updated at the same time as various
sections can simply be parsed out of those pages and into the new template.</li>
</ul>
<h2 id="comments">Comments</h2>
<p>This site uses an <acronym title="eXtensible Stylesheet Language">XSL</acronym>
based comments system.</p>
<p><acronym title="PHP Hypertext Preprocessor (server-side scripting language)">PHP</acronym>
is used to run an <acronym title="eXtensible Stylesheet Language">XSL</acronym>
processor, as below. The comment, author and various other information is
passed to this processor, which in turn applies a style sheet to it, producing
an <acronym title="eXtensible Markup Language">XML</acronym> output file.</p>
<p>
<textarea cols="80" rows="40" readonly="readonly" title="PHP code to create XSL processor">
// create array containing comment information
$params = array
(
'page' => $page, // page information
'ip' => $ip, // author's IP
'author' => $author, // author's name
'comment' => $comment, // the comment
'date' => $date // the time and date
);
// create an xslt processor
$xp = xslt_create();
if
(
xslt_process
(
$xp, // xslt processor
'saved-comments.xml', // input file
'save-comments.xsl', // xslt stylesheet
'temporary.xml', // output file
array(), // dummy array
$params // input information
)
)
{
// rename output file over the top of input file, performing
// atomic update and ensuring two users don't overwrite each other
rename('temporary.xml', 'saved-comments.xml');
// print success message
$print = 'Your comment has been saved and your IP logged as '.$ip;
}
// print error if it failed
else $print = 'Error: An unknown error has occurred while processing your
comment.';
// free the xslt processor
xslt_free($xp);
</textarea>
</p>
<p>This is a slightly simplified version of the <acronym title="Extensible Stylesheet Language Transformations">XSLT</acronym>
style sheet used to save comments.</p>
<p>
<textarea cols="80" rows="38" readonly="readonly" title="XSL Style Sheet to save comments to an XML file">
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright © 2002 Ned Martin
http://copyright.the-i.org/
Saves XML comments file
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8"/>
<xsl:param name="page">page</xsl:param>
<xsl:param name="ip">ip</xsl:param>
<xsl:param name="author">author</xsl:param>
<xsl:param name="comment">comment</xsl:param>
<xsl:param name="date">date</xsl:param>
<xsl:template match="comments">
<comments>
<xsl:for-each select="comment">
<xsl:copy-of select="."/>
</xsl:for-each>
<comment>
<xsl:attribute name="page">
<xsl:value-of select="$page"/>
</xsl:attribute>
<xsl:attribute name="ip">
<xsl:value-of select="$ip"/>
</xsl:attribute>
<xsl:attribute name="author">
<xsl:value-of select="$author"/>
</xsl:attribute>
<xsl:attribute name="date">
<xsl:value-of select="$date"/>
</xsl:attribute>
<xsl:value-of select="$comment"/>
</comment>
</comments>
</xsl:template>
</xsl:stylesheet>
</textarea>
</p>
<p>A very similar process is used to display comments. <acronym title="PHP Hypertext Preprocessor (server-side scripting language)">PHP</acronym>
is once again used to run an <acronym title="Extensible Stylesheet Language Transformations">XSLT</acronym>
processor, and the page information passed to that processor. Once again,
an <acronym title="Extensible Stylesheet Language Transformations">XSLT</acronym>
style sheet is applied to this information, using the previously stored <acronym title="eXtensible Markup Language">XML</acronym>
comments file, and the output displayed in the browser.</p>
<p>
<textarea cols="80" rows="22" readonly="readonly" title="PHP code to create XSL processor to display comments">
// Parse comments file for comments
$params = array
(
'page' => $page, // page information
);
// create xslt processor
$xp = xslt_create();
$result = xslt_process
(
$xp, '_src/comments.xml', // input file
'_src/show-comments.xsl', // xslt stylesheet
NULL, array(), // dummy array
$params // page information
);
// evaluate and print out the results
eval('?>'.$result);
// free xslt processor
xslt_free($xp);
</textarea>
</p>
<p>This is a simplified version of the <acronym title="Extensible Stylesheet Language Transformations">XSLT</acronym>
style sheet used to display comments.</p>
<p>
<textarea cols="80" rows="23" readonly="readonly" title="XSL Style Sheet to display comments"><?xml version="1.0" encoding="utf-8"?>
<!--
Copyright © 2003 Ned Martin
http://copyright.the-i.org/
Shows comments
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:param name="page">blah/blah2.php</xsl:param>
<xsl:template match="comments">
<xsl:if test="comment[@page = $page]">
<dl title="Comments">
<xsl:for-each select="comment[@page = $page]">
<dd>
<xsl:value-of select="."/>
</dd>
</xsl:for-each>
</dl>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
</textarea>
</p>
<h2 id="code">Working Code</h2>
<p style="color:red">Update from the year 2011… Sadly, XSLT is annoying and not especially useful for anything particularly complex. I no longer use it, and have had to remove the below links.</p>
<p><strong>Journal Site Code</strong><br />
Working examples of <acronym title="Extensible Stylesheet Language Transformations">XSLT</acronym>
style sheets used in my <a href="http://nedmartin.org/journal/" title="Journal - Ned Martin, Who he is, what he does, and why">journal</a>
can be viewed <a href="#http://nedmartin.org/journal/xsl/" title="XSL Style Sheets used to create my journal">here</a>.</p>
<p><strong>Amused Site Code</strong><br />
Several examples of the working code used to generate my <a href="#http://amused.the-i.org/" title="Amused - Because someone has to be">amused
site</a> are available.</p>
<ul>
<li><a href="#http://amused.the-i.org/_xml/source.php" title="Amused site code"><acronym title="PHP Hypertext Preprocessor (server-side scripting language)">PHP</acronym>
template and comment supporting code</a></li>
<li><a href="#http://amused.the-i.org/_xml/save-comments.xsl" title="Amused site save comments code"><acronym title="Extensible Stylesheet Language Transformations" >XSLT
</acronym>to save a new comment</a></li>
<li><a href="#http://amused.the-i.org/_xml/show-comments.xsl" title="Amused site single page show comments code"><acronym title="Extensible Stylesheet Language Transformations">XSLT</acronym>
to show a single per-page comment</a> as at the bottom of <a href="#http://amused.the-i.org/#comments" title="Amused site front page showing comments">here</a></li>
<li><a href="#http://amused.the-i.org/_xml/comments.xsl" title="Amused site show all comments code"><acronym title="Extensible Stylesheet Language Transformations">XSLT</acronym>
to display and sort all comments</a> as shown <a href="#http://amused.the-i.org/comments" title="Amused site comments">here</a></li>
<li> <acronym title="eXtensible Markup Language"><a href="#http://amused.the-i.org/_xml/comments.xml" title="Amused site comments file">XML</a></acronym><a href="#http://amused.the-i.org/_xml/comments.xml">
comments file that stores the comments</a></li>
<li><a href="#http://amused.the-i.org/_xml/comments.xsd" title="Amused site comments file schema">Comments file schema</a></li>
<li><a href="#http://amused.the-i.org/_xml/comments-back.xml" title="Amused site temporary comments file">Temporary
<acronym title="eXtensible Markup Language">XML</acronym> comments file</a></li>
</ul>
<p>Please remember that this code is <a href="http://the-i.org/copyright" title="Contact and Copyright Information">©
Copyright</a></p>
<p> </p>
</body>
</html>