--- a/index.php Wed Oct 26 10:30:00 2011 +0200
+++ b/index.php Wed Oct 26 14:35:50 2011 +0200
@@ -1,79 +1,37 @@
<?php
-const FILE = "var/abook.txt"; // parent dir must exist
-const RS = "\n";
-const FS = "\t";
-
-function open_db($file) {
- @mkdir(dirname($file));
+// configuration
+const FILE = "var/abook.txt";
- $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);
+set_error_handler(create_function(
+ '$errno, $errstr, $errfile, $errline, $errcontext',
+ 'if (!error_reporting()) return;
+ throw new ErrorException($errstr, 0, $errno, $errfile, $errline);'));
- return $fh;
-}
+// no user serviceable parts below!
-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 __autoload($class) {
+ require_once "class.$class.php";
}
-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;
+try {
+ $abook = new Address_Book_File(FILE);
+ switch (@$_REQUEST['action']) {
+ case 'add': $abook->add_entry($_REQUEST);
+ break;
+ case 'search': $entries = $abook->search_entries($_REQUEST['pattern']);
+ break;
}
- flock($fh, LOCK_EX);
- fputs($fh, join(FS, $new) . RS);
- flock($fh, LOCK_UN);
- header("Location: $_SERVER[PHP_SELF]?action=add");
+}
+catch (ErrorException $e) {
+ print "INTERNAL ERROR: " . $e->getMessage();
+}
+catch (Exception $e) {
+ print "Application error: " . $e->getMessage();
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>
@@ -84,7 +42,7 @@
<body>
-<? if ($_REQUEST['action'] == 'add') { ?>
+<? if (@$_REQUEST['action'] == 'add') { ?>
[ <a href="<?=$_SERVER['PHP_SELF']?>">Home</a> ]
<? } else { ?>
[ <a href="<?=$_SERVER['PHP_SELF']?>?action=add">Add Entries</a> ]
@@ -92,7 +50,7 @@
<h1>Adressbuch</h1>
-<? if ($_REQUEST['action'] == 'add') { ?>
+<? if (@$_REQUEST['action'] == 'add') { ?>
<p>
<form>
<label for=name>Name</label>
@@ -113,7 +71,7 @@
<noscript><input type=submit /></noscript>
</form>
- <? if ($entries) { ?>
+ <? if (@$entries) { ?>
<table>
<tr><th>Name<th>Telefon<th>Mail</tr>
<? foreach ($entries as $entry) { ?>