--- a/index.php Wed Oct 26 20:56:15 2011 +0200
+++ b/index.php Wed Oct 26 21:44:35 2011 +0200
@@ -1,92 +1,83 @@
-<?php
-
-// configuration
-const FILE = "var/abook.txt";
-
-set_error_handler(create_function(
- '$errno, $errstr, $errfile, $errline, $errcontext',
- 'if (!error_reporting()) return;
- throw new ErrorException($errstr, 0, $errno, $errfile, $errline);'));
-
-// no user serviceable parts below!
-
-function __autoload($class) {
- require_once "class.$class.php";
-}
-
-try {
- //$abook = new Address_Book_DB("mysql:host=localhost;dbname=abook", "hans", "x");
- $abook = new Address_Book_DB("sqlite:var/abook.sqlite");
- switch (@$_REQUEST['action']) {
- case 'add': $abook->add_entry($_REQUEST);
- break;
- case 'search': $entries = $abook->search_entries($_REQUEST['pattern']);
- break;
- }
-
-}
-catch (ErrorException $e) {
- print "INTERNAL ERROR: " . $e->getMessage();
-}
-catch (Exception $e) {
- print "Application error: " . $e->getMessage();
- exit;
-}
-
-?>
-<!-- HTML -->
-<html>
-
-<style type="text/css">
- form label { display:block; float:left; width:10ex; }
-</style>
-
-<body>
-
-<? if (@$_REQUEST['action'] == 'add') { ?>
- [ <a href="<?=$_SERVER['PHP_SELF']?>">Home</a> ]
-<? } else { ?>
- [ <a href="<?=$_SERVER['PHP_SELF']?>?action=add">Add Entries</a> ]
-<? } ?>
-
-<h1>Adressbuch</h1>
-
-<? if (@$_REQUEST['action'] == 'add') { ?>
- <p>
- <form>
- <label for=name>Name</label>
- <input id=name type=text name=name /><br>
- <label for=tel>Telefon</label>
- <input id=tel type=text name=tel /><br>
- <label for=mail>Mail</label>
- <input id=mail type=text name=mail /><br>
- <input type=hidden name=action value=add />
- <input type=submit />
- </form>
-
-<? } else { ?>
-
- <form>
- <input type=text name=pattern onChange="submit()" />
- <input type=hidden name=action value=search />
- <noscript><input type=submit /></noscript>
- </form>
-
- <? if (@$entries) { ?>
- <table>
- <tr><th>Name<th>Telefon<th>Mail</tr>
- <? foreach ($entries as $entry) { ?>
- <tr>
- <td><?=$entry['name']?>
- <td><?=$entry['tel']?>
- <td><?=$entry['mail']?>
- </tr>
- <? } ?>
- </table>
- <? } else { ?>
- Sorry.
- <? } ?>
-
-<? } ?>
-
-</body>
+<?php
+
+const FILE = "var/abook.txt";
+const DB = "sqlite:var/abook.sqlite";
+const ABOOK = DB;
+
+set_error_handler(create_function(
+ '$errno, $errstr, $errfile, $errline, $errcontext',
+ 'if (!error_reporting()) return;
+ throw new ErrorException($errstr, 0, $errno, $errfile, $errline);'), -1);
+
+spl_autoload_register(create_function(
+ '$class',
+ '$file = "class.$class.php";
+ if (!file_exists($file)) return;
+ require_once $file;'));
+
+require_once 'Twig-1.3.0/lib/Twig/Autoloader.php';
+Twig_Autoloader::register();
+$twig = new Twig_Environment(new Twig_Loader_Filesystem("templates"));
+
+if (isset($_REQUEST['action'])) {
+
+ try {
+ if (preg_match('/^[\w]+\//', ABOOK))
+ $abook = new Address_Book_File(FILE);
+ else
+ $abook = new Address_Book_DB(DB);
+
+ switch (@$_REQUEST['action']) {
+ // Nach dem Eintragen bleiben wir auf der Eintragsseite,
+ // aber wir verhindern Duplikate, die mit RELOAD passieren
+ case 'add': if ($abook->add_entry($_REQUEST)) {
+ header("Location: $_SERVER[PHP_SELF]?action=add");
+ exit(0);
+ }
+ echo $twig->render("add.html", array());
+ exit;
+ break;
+
+ // Suchen…
+ case 'search': $entries = null;
+ $error = null;
+ try {
+ $entries = $abook->search_entries($_REQUEST['pattern']);
+ }
+ catch (Address_Book_Exception $e) {
+ $error = $e->getMessage();
+ }
+
+ if (@$_REQUEST['format'] == 'table') {
+ if (is_numeric($_REQUEST['max'])) {
+ $left = count($entries) - $_REQUEST['max'];
+ if ($left > 0)
+ $entries = array_slice($entries, 0, $_REQUEST['max']);
+ else
+ $left = NULL;
+ }
+ echo $twig->render("results.html",
+ array('entries' => $entries,
+ 'left' => $left, 'error' => $error));
+ exit;
+ }
+ echo $twig->render("search.html",
+ array('entries' => $entries, 'error' => $error));
+ exit;
+ }
+ break;
+
+ }
+ catch (Address_Book_Exception $e) {
+ header("Content-Type: text/plain; charset=utf-8");
+ print "Address Book Exception: " . $e->getMessage();
+ exit;
+ }
+ catch (Exception $e) {
+ header("Content-Type: text/plain; charset=utf-8");
+ print "Ohoh\n\n" . $e;
+ exit;
+ }
+}
+
+echo $twig->render("search.html", array());