--- a/addressbook.class.php Wed Oct 26 00:15:59 2011 +0200
+++ b/addressbook.class.php Wed Oct 26 00:37:13 2011 +0200
@@ -1,10 +1,13 @@
<?
+
interface Address_Book {
public function get_entries();
public function search_entries($pattern);
public function add_entry($entry);
}
+class Address_Book_Exception extends Exception { }
+
class Address_Book_File implements Address_Book {
const RS = "\n";
@@ -15,7 +18,8 @@
function __construct($file) {
@mkdir(dirname($file));
- $this->fh = fopen($file, "a+b");
+ @$this->fh = fopen($file, "a+b");
+ if (!$this->fh) throw new Address_Book_Exception("Can't open address book.");
flock($this->fh, LOCK_EX);
$stat = fstat($this->fh);
if ($stat['size'] == 0) {
--- a/index.php Wed Oct 26 00:15:59 2011 +0200
+++ b/index.php Wed Oct 26 00:37:13 2011 +0200
@@ -8,36 +8,50 @@
if (isset($_REQUEST['action'])) {
require 'addressbook.class.php';
- $abook = new Address_Book_File(FILE);
- 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;
+ try {
+ $abook = new Address_Book_File(FILE);
- // Suchen…
- case 'search': $entries = $abook->search_entries($_REQUEST['pattern']);
- 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));
- exit;
+ 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("search.html", array('entries' => $entries));
+ echo $twig->render("add.html", array());
exit;
break;
+
+ // Suchen…
+ case 'search': $entries = $abook->search_entries($_REQUEST['pattern']);
+ 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));
+ exit;
+ }
+ echo $twig->render("search.html", array('entries' => $entries));
+ 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->getMessage();
+ exit;
}
}