added Exception class for address book.
authorHeiko Schlittermann (JUMPER) <hs@schlittermann.de>
Wed, 26 Oct 2011 00:56:32 +0200
changeset 7 47446e75ceea
parent 6 48ffdc97f54b
child 8 636f54747018
added Exception class for address book.
addressbook.class.php
index.php
templates/results.html
--- a/addressbook.class.php	Wed Oct 26 00:37:13 2011 +0200
+++ b/addressbook.class.php	Wed Oct 26 00:56:32 2011 +0200
@@ -18,8 +18,7 @@
 
     function __construct($file) {
 	@mkdir(dirname($file));
-	@$this->fh = fopen($file, "a+b");
-	if (!$this->fh) throw new Address_Book_Exception("Can't open address book.");
+	$this->fh = fopen($file, "a+b");
 	flock($this->fh, LOCK_EX);
 	$stat = fstat($this->fh);
 	if ($stat['size'] == 0) {
@@ -61,7 +60,9 @@
     function search_entries($pattern) {
 	$pattern = trim($pattern);
 	if (empty($pattern)) return;
-	$pattern = preg_replace('/\//', '\/', $pattern);
+	//$pattern = preg_replace('/\//', '\/', $pattern);
+	if (preg_match('/\//', $pattern))
+	    throw new Address_Book_Exception("Pattern must not contain '/'");
 	fseek($this->fh, 0, SEEK_SET);
 	$entries = array();
 	while ($line = stream_get_line($this->fh, 0, self::RS)) {
--- a/index.php	Wed Oct 26 00:37:13 2011 +0200
+++ b/index.php	Wed Oct 26 00:56:32 2011 +0200
@@ -6,6 +6,10 @@
 
 const FILE = "var/abook.txt";
 
+// stop on any error!
+set_error_handler(create_function('$a, $b, $c, $d', 
+    'if (error_reporting()) throw new ErrorException($b, 0, $a, $c, $d);'), -1);
+
 if (isset($_REQUEST['action'])) {
     require 'addressbook.class.php';
 
@@ -24,7 +28,15 @@
 			    break;
 
 		// Suchen…
-		case 'search':  $entries = $abook->search_entries($_REQUEST['pattern']);
+		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'];
@@ -34,13 +46,15 @@
 					    $left = NULL;
 				    }
 				    echo $twig->render("results.html",
-					array('entries' => $entries, 'left' => $left));
+					array('entries' => $entries,
+					'left' => $left, 'error' => $error));
 				    exit;
 				}
-				echo $twig->render("search.html", array('entries' => $entries));
+				echo $twig->render("search.html",
+				    array('entries' => $entries, 'error' => $error));
 				exit;
-				break;
-	}
+			    }
+			    break;
 
     }
     catch (Address_Book_Exception $e) {
@@ -50,7 +64,7 @@
     }
     catch (Exception $e) {
 	header("Content-Type: text/plain; charset=utf-8");
-	print "Ohoh\n\n" .$e->getMessage();
+	print "Ohoh\n\n" . $e;
 	exit;
     }
 }
--- a/templates/results.html	Wed Oct 26 00:37:13 2011 +0200
+++ b/templates/results.html	Wed Oct 26 00:56:32 2011 +0200
@@ -2,6 +2,9 @@
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <body>
 
+{% if error %}
+<font color=red> {{ error }} </font>
+{% else %}
 {% for entry in entries %}
 
     {% if loop.first %}
@@ -20,5 +23,6 @@
 {% else %}
 Sorry, keine Einträge gefunden.
 {% endfor %}
+{% endif %}
 </body>
 </html>