summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Spragg <adam@spra.gg>2018-06-19 16:35:15 +0100
committerAdam Spragg <adam@spra.gg>2018-06-19 16:35:15 +0100
commit2f5472519016b3092dea07d6f041b1cb74c3cd13 (patch)
tree404cf9eef936a67e91b5dc1a6a066fd5f336d366
parent65c5c4043fe05ed4d94f3530c405363d3aee541c (diff)
Output the page head/header/footer with PHP
Allows putting all the repeated stuff in one place (DRY!), so that if it needs changing it only needs to change once. Start the name of the included file with "@", and disallow the retrieval of files beginning with "@" (returning 404) so that it can't be downloaded, because it won't output anything meaningful. Why "@"? Because it's about the only ASCII punctuation character that doesn't mean anything special to most shells (bash), or programs, and is therefore easy to use as a filename on the command line. I almost went with "+", but it turns out that nvim treats arguments beginning with "+" as options. So "@" it is.
-rw-r--r--apache2.conf4
-rw-r--r--html/@include.php59
-rw-r--r--html/about22
-rw-r--r--html/contact21
-rw-r--r--html/index.php26
-rw-r--r--html/privacy21
-rw-r--r--html/projects/index.php21
-rw-r--r--rawdog/page.template17
8 files changed, 87 insertions, 104 deletions
diff --git a/apache2.conf b/apache2.conf
index fdf764a..7408ee8 100644
--- a/apache2.conf
+++ b/apache2.conf
@@ -157,6 +157,10 @@
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
RewriteRule !\. - [H=application/x-httpd-php]
+ # Disallow access to files/directories whose names begin with "@", returning a 404.
+ # This makes those files appear to not exist.
+ RewriteRule "/@" "-" [R=404,L]
+
<Location /obs>
AuthType Basic
AuthName "Private area"
diff --git a/html/@include.php b/html/@include.php
new file mode 100644
index 0000000..2cf50d3
--- /dev/null
+++ b/html/@include.php
@@ -0,0 +1,59 @@
+<?php
+
+/// Indent all the lines in a string with a given number of tabs
+function indent($s, $n, $indent_first_line = false) {
+ $s = preg_replace("/^/m", str_repeat("\t", $n), $s);
+
+ if (!$indent_first_line) {
+ $s = substr($s, $n);
+ }
+
+ return $s;
+}
+
+/// Get the standard <head> elements for the site
+function site_head($title) {
+ return "<title>{$title}</title>\n"
+ . "<link rel=\"stylesheet\" href=\"/style/main.css\" />\n"
+ . "<!--\n"
+ . " Hi there. Nice to see you here. Feel free to have a look around. Also check out the /about\n"
+ . " page while you're at it. It's got a little bit more info about the site and the resources\n"
+ . " used.\n"
+ . "-->\n";
+}
+
+/// Get a single top-level navigation item for the site
+function site_navitem($url, $text, $hide = false) {
+ $selected = $url == $_SERVER['REQUEST_URI'];
+
+ $hbeg = $hide && !$selected ? '!--' : '';
+ $hend = $hide && !$selected ? '--' : '';
+ $sel = $selected ? ' class="selected"' : '';
+
+ return "<{$hbeg}li{$sel}><a href=\"{$url}\">{$text}</a></li{$hend}>";
+}
+
+/// Get the standard page header elements for the site
+function site_header($title) {
+ return "<h1 class=\"test\">{$title}</h1>\n"
+ . "<nav>\n"
+ . "\t<ul>\n"
+ . "\t\t" . site_navitem('/', 'Home') . "\n"
+ . "\t\t" . site_navitem('/projects', 'Projects') . "\n"
+ . "\t\t" . site_navitem('/blog', 'Blog') . "\n"
+ . "\t\t" . site_navitem('/privacy', 'Privacy') . "\n"
+ . "\t\t" . site_navitem('/contact', 'Contact') . "\n"
+ . "\t\t" . site_navitem('/about', 'About', true) . "\n"
+ . "\t</ul>\n"
+ . "</nav>\n";
+}
+
+/// Get the standard page footer elements for the site
+function site_footer() {
+ return "<hr />\n"
+ . "<p>Spragg Software Services Ltd is registered in England, No. 11248242.\n"
+ . " Registered office: 82 Upper Hanover Street, Sheffield, S3 7RQ.\n"
+ . " VAT reg No. 295343283.</p>\n";
+}
+
+?>
diff --git a/html/about b/html/about
index b44e7b2..cace69a 100644
--- a/html/about
+++ b/html/about
@@ -1,8 +1,7 @@
-<!DOCTYPE html>
+<?php include '@include.php'; ?><!DOCTYPE html>
<html lang="en">
<head>
- <title>About - Spragg Software Services Ltd.</title>
- <link rel="stylesheet" href="/style/main.css" />
+ <?php echo indent(site_head('About - Spragg Software Services Ltd.'), 2); ?>
<style>
h1.test {
font-family: "DejaVuSansMono", serif;
@@ -11,17 +10,7 @@
</head>
<body>
<header>
- <h1 class="test">SSSL / About</h1>
- <nav>
- <ul>
- <li><a href="/">Home</a></li>
- <li><a href="/projects">Projects</a></li>
- <li><a href="/blog">Blog</a></li>
- <li><a href="/privacy">Privacy</a></li>
- <li><a href="/contact">Contact</a></li>
- <!--li class="selected"><a href="/about">About</a></li-->
- </ul>
- </nav>
+ <?php echo indent(site_header('SSSL / About'), 3); ?>
</header>
<main>
<h2><img src="/img/icons/Faenza/mimetypes/32/text-x-copying.png" alt="©" /> 3rd party resources</h2>
@@ -86,10 +75,7 @@
If there are any such sites out there, I'd be interested in knowing about them.</p>
</main>
<footer>
- <hr />
- <p>Spragg Software Services Ltd is registered in England, No. 11248242.
- Registered office: 82 Upper Hanover Street, Sheffield, S3 7RQ.
- VAT reg No. 295343283.</p>
+ <?php echo indent(site_footer(), 3); ?>
</footer>
</body>
</html>
diff --git a/html/contact b/html/contact
index cd2bf0d..632113a 100644
--- a/html/contact
+++ b/html/contact
@@ -1,21 +1,11 @@
-<!DOCTYPE html>
+<?php include '@include.php'; ?><!DOCTYPE html>
<html lang="en">
<head>
- <title>Contact - Spragg Software Services Ltd.</title>
- <link rel="stylesheet" href="/style/main.css" />
+ <?php echo indent(site_head('Contact - Spragg Software Services Ltd.'), 2); ?>
</head>
<body>
<header>
- <h1>Contact</h1>
- <nav>
- <ul>
- <li><a href="/">Home</a></li>
- <li><a href="/projects">Projects</a></li>
- <li><a href="/blog">Blog</a></li>
- <li><a href="/privacy">Privacy</a></li>
- <li class="selected"><a href="/contact">Contact</a></li>
- </ul>
- </nav>
+ <?php echo indent(site_header('Contact'), 3); ?>
</header>
<main>
<h2><img src="/img/icons/Faenza/mimetypes/32/message.png" alt="•" /> Contact</h2>
@@ -26,10 +16,7 @@
that's an issue, but most of what I do can be done remotely.</p>
</main>
<footer>
- <hr />
- <p>Spragg Software Services Ltd is registered in England, No. 11248242.
- Registered office: 82 Upper Hanover Street, Sheffield, S3 7RQ.
- VAT reg No. 295343283.</p>
+ <?php echo indent(site_footer(), 3); ?>
</footer>
</body>
</html>
diff --git a/html/index.php b/html/index.php
index 89ea644..29e0cfa 100644
--- a/html/index.php
+++ b/html/index.php
@@ -1,26 +1,11 @@
-<!DOCTYPE html>
+<?php include '@include.php'; ?><!DOCTYPE html>
<html lang="en">
<head>
- <title>Spragg Software Services Ltd.</title>
- <link rel="stylesheet" href="/style/main.css" />
- <!--
- Hi there. Nice to see you here. Feel free to have a look around. Also check out the /about page
- while you're at it. It's got a little bit more info about the site and the resources used.
- -->
+ <?php echo indent(site_head('Spragg Software Services Ltd.'), 2); ?>
</head>
<body>
<header>
- <h1>Spragg Software Services Ltd.</h1>
- <nav>
- <ul>
- <li class="selected"><a href="/">Home</a></li>
- <li><a href="/projects">Projects</a></li>
- <li><a href="/blog">Blog</a></li>
- <li><a href="/privacy">Privacy</a></li>
- <li><a href="/contact">Contact</a></li>
- <!--li><a href="/about">About</a></li-->
- </ul>
- </nav>
+ <?php echo indent(site_header('Spragg Software Services Ltd.'), 3); ?>
</header>
<main>
<h2><img src="/img/icons/Faenza/mimetypes/32/text-x-source.png" alt="•" /> Software customisation</h2>
@@ -62,10 +47,7 @@
able to lend a hand with other platforms too.</p>
</main>
<footer>
- <hr />
- <p>Spragg Software Services Ltd is registered in England, No. 11248242.
- Registered office: 82 Upper Hanover Street, Sheffield, S3 7RQ.
- VAT reg No. 295343283.</p>
+ <?php echo indent(site_footer(), 3); ?>
</footer>
</body>
</html>
diff --git a/html/privacy b/html/privacy
index b2fb82a..6d338fd 100644
--- a/html/privacy
+++ b/html/privacy
@@ -1,21 +1,11 @@
-<!DOCTYPE html>
+<?php include '@include.php'; ?><!DOCTYPE html>
<html lang="en">
<head>
- <title>Privacy - Spragg Software Services Ltd.</title>
- <link rel="stylesheet" href="/style/main.css" />
+ <?php echo indent(site_head('Privacy - Spragg Software Services Ltd.'), 2); ?>
</head>
<body>
<header>
- <h1>Privacy</h1>
- <nav>
- <ul>
- <li><a href="/">Home</a></li>
- <li><a href="/projects">Projects</a></li>
- <li><a href="/blog">Blog</a></li>
- <li class="selected"><a href="/privacy">Privacy</a></li>
- <li><a href="/contact">Contact</a></li>
- </ul>
- </nav>
+ <?php echo indent(site_header('Privacy'), 3); ?>
</header>
<main>
<h2><img src="/img/icons/oxygen/base/32x32/apps/preferences-web-browser-cookies.png" alt="•" /> Cookies</h2>
@@ -68,10 +58,7 @@
<a href="mailto:webmaster@spragg-ssl.co.uk">webmaster@spragg-ssl.co.uk</a>.</p>
</main>
<footer>
- <hr />
- <p>Spragg Software Services Ltd is registered in England, No. 11248242.
- Registered office: 82 Upper Hanover Street, Sheffield, S3 7RQ.
- VAT reg No. 295343283.</p>
+ <?php echo indent(site_footer(), 3); ?>
</footer>
</body>
</html>
diff --git a/html/projects/index.php b/html/projects/index.php
index 9911071..bc3d4a6 100644
--- a/html/projects/index.php
+++ b/html/projects/index.php
@@ -1,30 +1,17 @@
-<!DOCTYPE html>
+<?php include '../@include.php'; ?><!DOCTYPE html>
<html lang="en">
<head>
- <title>Spragg Software Services Ltd.</title>
- <link rel="stylesheet" href="/style/main.css" />
+ <?php echo indent(site_head('Projects - Spragg Software Services Ltd.'), 2); ?>
</head>
<body>
<header>
- <h1>Spragg Software Services Ltd - Projects</h1>
- <nav>
- <ul>
- <li><a href="/">Home</a></li>
- <li class="selected"><a href="/projects">Projects</a></li>
- <li><a href="/blog">Blog</a></li>
- <li><a href="/privacy">Privacy</a></li>
- <li><a href="/contact">Contact</a></li>
- </ul>
- </nav>
+ <?php echo indent(site_header('Projects'), 3); ?>
</header>
<main>
<p>To be continued...</p>
</main>
<footer>
- <hr />
- <p>Spragg Software Services Ltd is registered in England, No. 11248242.
- Registered office: 82 Upper Hanover Street, Sheffield, S3 7RQ.
- VAT reg No. 295343283.</p>
+ <?php echo indent(site_footer(), 3); ?>
</footer>
</body>
</html>
diff --git a/rawdog/page.template b/rawdog/page.template
index 4c0528b..050f719 100644
--- a/rawdog/page.template
+++ b/rawdog/page.template
@@ -1,23 +1,13 @@
-<!DOCTYPE html>
+<?php include '@include.php'; ?><!DOCTYPE html>
<html lang="en">
<head>
- <title>Blog</title>
- <link rel="stylesheet" href="/style/main.css" type="text/css" />
+ <?php echo indent(site_head('Blog - Spragg Software Services Ltd.'), 2); ?>
<meta name="robots" content="noindex,nofollow,noarchive">
__refresh__
</head>
<body>
<header>
- <h1>Blog</h1>
- <nav>
- <ul>
- <li><a href="/">Home</a></li>
- <li><a href="/projects">Projects</a></li>
- <li class="selected"><a href="/blog">Blog</a></li>
- <li><a href="/privacy">Privacy</a></li>
- <li><a href="/contact">Contact</a></li>
- </ul>
- </nav>
+ <?php echo indent(site_header('Blog'), 3); ?>
</header>
<main>
<section class="items">__items__</section>
@@ -29,6 +19,7 @@
<footer>
<p id="aboutrawdog">Generated by <a href="http://offog.org/code/rawdog.html">rawdog</a> version
__version__</p>
+ <?php echo indent(site_footer(), 3); ?>
</footer>
</body>
</html>