--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore Tue Oct 25 16:44:53 2011 +0200
@@ -0,0 +1,4 @@
+syntax: glob
+index.php~
+var/abook.txt
+.index.php.swp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/index.php Tue Oct 25 16:44:53 2011 +0200
@@ -0,0 +1,133 @@
+<?php
+
+const FILE = "var/abook.txt"; // parent dir must exist
+const RS = "\n";
+const FS = "\t";
+
+function open_db($file) {
+ @mkdir(dirname($file));
+
+ $fh = fopen($file, "a+b");
+ flock($fh, LOCK_EX);
+ $stat = fstat($fh);
+ if ($stat['size'] == 0) {
+ fputs($fh, join(FS, array("Hans Hanson", "0815", "hans@hanson.de"))
+ . RS);
+ }
+ flock($fh, LOCK_SH);
+ fseek($fh, 0, SEEK_SET);
+
+ return $fh;
+}
+
+function get_entries($fh) {
+ $entries = array();
+ fseek($fh, 0, SEEK_SET);
+ while($line = stream_get_line($fh, 0, RS)) {
+ if ($line === FALSE) break;
+ $entries[] = explode(FS, $line);
+ }
+ return $entries;
+}
+
+function add_entry($fh, $entry) {
+ $fields = array('name', 'tel', 'mail');
+
+ $new = array();
+ foreach($fields as $key) {
+ if (!isset($entry[$key])) return;
+ $new[$key] = $entry[$key];
+ trim($new[$key]);
+ preg_replace('/['.RS.FS.' ]+/', ' ', $new[$key]);
+ if (empty($new[$key])) return;
+ }
+
+ flock($fh, LOCK_EX);
+ fputs($fh, join(FS, $new) . RS);
+ flock($fh, LOCK_UN);
+ header("Location: $_SERVER[PHP_SELF]?action=add");
+ exit;
+}
+
+function search_entries($fh, $pattern) {
+ fseek($fh, 0, SEEK_SET);
+ $entries = array();
+ while ($line = stream_get_line($fh, 0, RS)) {
+ if ($line === FALSE) break;
+ if (!preg_match("/$pattern/i", $line)) continue;
+ $entry = explode(FS, $line);
+ $entries[] = array('name' => $entry[0],
+ 'tel' => $entry[1],
+ 'mail' => $entry[2]);
+ }
+ return $entries;
+}
+
+
+$abook = open_db(FILE);
+
+switch ($_REQUEST['action']) {
+ case 'add': add_entry($abook, $_REQUEST);
+ break;
+ case 'search': $entries = search_entries($abook, $_REQUEST['pattern']);
+ break;
+}
+
+
+?>
+<!-- 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>