Programming in PHP

Even if nobody likes PHP1 (especially PHP 4), sometimes it may be a necessary evil imposed by the circumstances to do some scripting in this awful invention of the less noble parts of mankind. Having said that, the source code of a file-based “content management system” (ha ha) is listed below.

Micro CMS

A very simple Content Management System (CMS) written in PHP 4. Not very useful at a time when you can just use XMLHttpRequest on client-side. Anyways, you may want to save it as index.php along a directory content/ that stores the pages in HTML format and a directory template/ containing the template file:

The template is parsed and the string [% content %] will be replaced with the contents of the accessed page. Open http://www.example.com/index.php?page=main to show page main.html.

Source Code

<?php
/**
 * Micro CMS in PHP 4
 * Licence: Public Domain
 */

$content_dir   = "./content/";                  // Directory with HTML files.
$file_ext      = ".html";                       // File extension.
$default       = "main";                        // Name of default page (main.html).
$page_404      = "404";                         // Name of 404 page (404.html).
$template_file = "./template/template.html";    // Path to template file.

// Change the locale to your language.
setlocale(LC_ALL, "de_DE@euro", "de_DE", "de", "ge");
output_html($content_dir, $file_ext, $page_404, $template_file);

function get_page_env() {
    $page = "";

    if (isset($_GET["page"])) {
        $page = strtolower(htmlentities($_GET["page"], ENT_QUOTES));
        $page = preg_replace("#(\W[-])#", "", $page);
    }

    $page = ($page == "") ? $default : $page;

    return $page;
}

function get_parsed_template($template, $dict) {
    $parsed_template = $template;

    foreach ($dict as $key => $value) {
        $regex["name"] = "#\[%[[:space:]]*?(".strtolower($key)."){1,}?[[:space:]]*?%\]#si";
        $parsed_template = preg_replace($regex["name"], $value, $parsed_template);
    }

    return $parsed_template;
}

function open_file($file) {
    if (!file_exists($file))
        return false;

    $fh = fopen($file, "r");
    rewind($fh);
    $file_content = fread($fh, filesize($file));
    fclose($fh);

    return $file_content;
}

function output_html($content_dir, $file_ext, $page_404, $template_file) {
    $content_dir .= (substr($content_dir, strlen($content_dir) - 1) != "/") ? "/" : "";

    $page = get_page_env();
    $page_file = $content_dir . $page . $file_ext;

    if (!file_exists($page_file))
        $content = open_file($content_dir . $page_404 . $file_ext);
    else
        $content = open_file($page_file);

    $dict = array("content" => $content);

    if (!file_exists($template_file))
        die("Template file not found");

    $template = open_file($template_file);
    $parsed_template = get_parsed_template($template, $dict);

    echo $parsed_template;
}

?>

Template

A minimal template file may contain:

<!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">

    <head>
        <title>Example</title>
    </head>

    <body>
        <h1>Hello, World!</h1>
        <hr />
[% content %]
        <hr />
    </body>

</html>

Footnotes

1 Maybe because of things like this. Or this.

back