<?php
/*****************************************************
* Meine Visitenkarte (Card) Version 1 *
*****************************************************
* Simples Beispiel eines Web-CMS von Stefan K. Baur *
*****************************************************/
// -- CONFIG ----------------------
define("USERNAME", "test"); // bitte ändern!
define("PASSWORD", "test"); // bitte ändern!
define("ROOT", "index.php"); // Einstiegspunkt.
define("CONTENT_FILE", "./content.txt"); // Content-Datei
// -- POST-Paramters --------------
$do = $_POST["do"];
$username = $_POST["username"];
$password = $_POST["password"];
$title = $_POST["title"];
$code = $_POST["code"];
$owner = $_POST["owner"];
$streat = $_POST["streat"];
$zip = $_POST["zip"];
$city = $_POST["city"];
$labels = $_POST["labels"];
$text = $_POST["text"];
// -- FUNCTIONS -------------------
// Liest den Inhalt der Content-Datei als String.
function readContent() {
$content = "";
if (file_exists(CONTENT_FILE)) {
$fileHandle = fopen(CONTENT_FILE, "r");
while (!feof($fileHandle)) {
$content .= fgets($fileHandle, 1024);
}
fclose($fileHandle);
}
return $content;
}
// Schreibt einen String in die Content-Datei.
function writeContent($content) {
$fileHandle = fopen(CONTENT_FILE, "w");
fwrite($fileHandle, $content, strlen($content));
fclose($fileHandle);
// Anschließend Zugriffsberechtigungen setzen
chmod(CONTENT_FILE, 0600);
}
// Befreit einen String von speziellen Sonderzeichen.
function strip($value, $full = true) {
$result = stripslashes($value);
if ($full) $result = str_replace("\"", "'", $result);
return str_replace(">", ">", str_replace("<", "<", $result));
}
// -- CLASSES ---------------------
class Info {}
/* Die Visitenkarte. */
class Card {
var $title = "";
var $code = "";
var $owner = "";
var $streat = "";
var $zip = "";
var $city = "";
var $infos = null; // Array of Info
function Card() {
// Default-Einstellungen.
$this->title = "Meine Visitenkarte!";
$this->code = "<a href=\"http://www.stefan-baur.de\">Zur HP!</a>";
$this->owner = "Stefan Baur";
$this->streat = "Sperlingstraße 6";
$this->zip = "90459";
$this->city = "Nürnberg";
$this->infos = array();
$info = new Info(); // Nur ein Beispiel-Info.
$info->label = "Web-CMS-Beispiel";
$info->text = "Dies ist ein total simples Web-CMS-Beispiel.";
$this->infos[] = $info;
}
function editVars() {
// Initialisierung mit globalen Variablen, vergleiche outEdit()
global $title, $owner, $streat, $zip, $city, $code, $labels, $text;
$this->title = strip($title);
$this->owner = strip($owner);
$this->streat = strip($streat);
$this->zip = strip($zip);
$this->city = strip($city);
$this->code = strip($code, false);
$this->infos = array();
for ($i = 0; $i < count($text); $i++) {
if ($text[$i]) { // Es werden nur Infos hinzugefügt, die Text haben.
$info = new Info();
$info->label = strip($labels[$i]);
$info->text = strip($text[$i], false);
$this->infos[] = $info;
}
}
}
/* Methoden zur Ausgabe von HTML */
function out($string, $spaces = 0, $eol = "\r\n") {
echo str_repeat(" ", $spaces) . $string . $eol;
}
function build($login) {
// Ausgabe des vollständigen HTML-Dokuments.
$this->out("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"");
$this->out("\"http://www.w3.org/TR/html4/loose.dtd\">", 6);
$this->out("<html>");
$this->outHtmlHead(1);
$this->outHtmlBody($login, 1);
$this->out("</html>");
}
function outHtmlHead($s = 0) {
$this->out("<head>", $s);
$this->out("<title>" . $this->title . "</title>", $s + 1);
$this->out("<meta http-equiv=\"content-language\" content=\"de\">", $s + 1);
$this->out("<meta http-equiv=\"content-type\"", $s + 1, "");
$this->out(" content=\"text/html; charset=ISO-8859-1\">", 0);
$this->out("<meta http-equiv=\"content-style-type\"", $s + 1, "");
$this->out(" content=\"text/css\">", 0);
$this->out("<style type=\"text/css\">", $s + 1);
$this->out("<!--", $s + 1);
$this->outCSS($s + 2);
$this->out("-->", $s + 1);
$this->out("</style>", $s + 1);
$this->out("</head>", $s);
}
function outCss($s = 0) {
$this->out("* {", $s);
$this->out("font-family : Verdana, Arial;", $s + 1);
$this->out("}", $s);
$this->out("body {", $s);
$this->out("padding : 60px;", $s + 1);
$this->out("}", $s);
$this->out("input, textarea {", $s);
$this->out("margin : 2px;", $s + 1);
$this->out("font-family : \"Courier New\";", $s + 1);
$this->out("font-size : 11pt;", $s + 1);
$this->out("}", $s);
}
function outHtmlBody($login, $s = 0) {
$this->out("<body>", $s);
// Titel als Hauptüberschrift ausgeben.
$this->out("<h1>" . $this->title . "</h1>", $s + 1);
$this->out("<table width=\"100%\">", $s + 1);
$this->out("<tr>", $s + 2);
// Adresse ausgeben.
$this->out("<td>", $s + 3);
$this->out("<p>", $s + 4);
$this->out("<b>" . $this->owner . "</b>", $s + 5);
$this->out("<br>", $s + 5);
$this->out($this->streat, $s + 5);
$this->out("<br>", $s + 5);
$this->out($this->zip, $s + 5);
$this->out($this->city, $s + 5);
$this->out("</p>", $s + 4);
$this->out("</td>", $s + 3);
// Code (HTML) ausgeben.
$this->out("<td>" . $this->code . "</td>", $s + 3);
$this->out("</tr>", $s + 2);
$this->out("</table>", $s + 1);
// Die Infoliste ausgeben.
foreach ($this->infos as $info) {
$this->out("<hr>", $s + 1);
if ($info->label) {
$this->out("<h2>" . $info->label . "</h2>", $s + 1);
}
$this->out("<p>" . $info->text . "</p>", $s + 1);
}
// Ausgaben für Benutzerinteraktion.
if ($login) {
$this->outEdit($s + 1);
$this->outLogout($s + 1);
} else {
$this->outLogin($s + 1);
}
$this->out("</body>", $s);
}
function outInput($hint, $type, $name, $value, $s = 0) {
// HTML-Input-Tag
if ($hint) $this->out("<small>" . $hint . ":</small><br>", $s);
$input = "<input";
if ($type) $input .= " type=\"" . $type . "\"";
if ($name) $input .= " name=\"" . $name . "\"";
if ($value) $input .= " value=\"" . $value . "\"";
$input .= ">";
$this->out($input, $s);
}
function outTextarea($hint, $name, $value, $s = 0) {
// HTML-Textarea-Tag
if ($hint) $this->out("<small>" . $hint . ":</small><br>", $s);
$textarea1 = "<textarea cols=\"80\" rows=\"5\"";
if ($name) $textarea1 .= " name=\"" . $name . "\"";
$this->out($textarea1, $s);
$val = str_replace(">", ">", str_replace("<", "<", $value));
$textarea2 = ">" . $val . "</textarea>";
$this->out($textarea2, $s + 1);
}
function outEdit($s = 0) {
$this->out("<hr>", $s);
$this->out("<h2>Content bearbeiten</h2>", $s);
$this->out("<form action=\"" . ROOT . "\" method=\"POST\">", $s);
$this->outInput("", "hidden", "do", "edit", $s + 1);
$this->outInput("", "hidden", "username", USERNAME, $s + 1);
$this->outInput("", "hidden", "password", PASSWORD, $s + 1);
// Editierbare Variablen: titel, owner, streat, ...
$this->outInput("Titel", "text", "title", $this->title, $s + 1);
$this->out("<br>", $s + 1);
$this->outInput("Inhaber", "text", "owner", $this->owner, $s + 1);
$this->out("<br>", $s + 1);
$this->outInput("Straße", "text", "streat", $this->streat, $s + 1);
$this->out("<br>", $s + 1);
$this->outInput("Ort", "text", "zip", $this->zip, $s + 1);
$this->outInput("", "text", "city", $this->city, $s + 1);
$this->out("<br>", $s + 1);
$this->outTextarea("Code", "code", $this->code, $s + 1);
$this->out("<br>", $s + 1);
// Editierbare Infos.
$num = 0;
foreach ($this->infos as $info) {
$hint = ($num + 1) . ". Info";
$this->outInput($hint, "text", "labels[" . $num . "]", $info->label, $s + 1);
$this->out("<br>", $s + 1);
$this->outTextarea("", "text[" . $num . "]", $info->text, $s + 1);
$this->out("<br>", $s + 1);
$num++;
}
// Zum Hinzufügen eines neuen Infos eine weitere Info-Eingabemöglichkeit.
$this->outInput("Info hinzufügen", "text", "labels[" . $num . "]", "", $s + 1);
$this->out("<br>", $s + 1);
$this->outTextarea("", "text[" . $num . "]", "", $s + 1);
$this->out("<br>", $s + 1);
// Änderungen werden mit diesem Buttom wirksam, siehe CONTROLLER
$this->out("<br>", $s + 1);
$this->outInput("", "submit", "", "Alles speichern", $s + 1);
$this->out("</form>", $s);
}
function outLogout($s) {
// Der Benutzer kann sich ausloggen.
$this->out("<hr>", $s);
$this->out("<h2>Logout</h2>", $s);
$this->out("<a href=\"" . ROOT . "\">Abmelden</a>", $s);
}
function outLogin($s) {
// Der Benutzer kann sich einloggen.
$this->out("<hr>", $s);
$this->out("<h2>Login</h2>", $s);
$this->out("<form action=\"" . ROOT . "\" method=\"POST\">", $s);
$this->outInput("", "hidden", "do", "login", $s + 1);
$this->outInput("", "text", "username", "", $s + 1);
$this->outInput("", "password", "password", "", $s + 1);
$this->outInput("", "submit", "", "Anmelden", $s + 1);
$this->out("</form>", $s);
}
}
// -- CONTROLLER ------------------ (Hauptprogramm)
// Vistenkarte mit Default-Einstellungen erzeugen.
$card = new Card();
// Visitenkarte aus Datei laden, falls vorhanden,
// ansonsten Default-Einstellungen in die Datei serialisieren.
$content = readContent();
if ($content) $card = unserialize($content);
else writeContent(serialize($card));
// Falls korrekte Login-Daten vorliegen,
// kann der Benutzer die Visitenkarte bearbeiten.
if (($username == USERNAME) && ($password == PASSWORD)) {
if ($do == "login") {
$card->build(true); // Visitenkarte in Bearbeiten-Modus anzeigen.
} elseif ($do == "edit") {
$card->editVars(); // Daten des HTML-Formulars lesen.
writeContent(serialize($card)); // Neue Daten speichern.
$card->build(true); // Visitenkarte in Bearbeiten-Modus anzeigen.
} else {
$card->build(false); // Visitenkarte anzeigen.
}
} else {
$card->build(false); // Standard-Ausgabe der Visitenkarte.
}
?>