1 <?php |
|
2 |
|
3 const FILE = "var/abook.txt"; |
|
4 const DB = "sqlite:var/abook.sqlite"; |
|
5 const ABOOK = DB; |
|
6 |
|
7 set_error_handler(create_function( |
|
8 '$errno, $errstr, $errfile, $errline, $errcontext', |
|
9 'if (!error_reporting()) return; |
|
10 throw new ErrorException($errstr, 0, $errno, $errfile, $errline);'), -1); |
|
11 |
|
12 spl_autoload_register(create_function( |
|
13 '$class', |
|
14 '$file = "class.$class.php"; |
|
15 if (!file_exists($file)) return; |
|
16 require_once $file;')); |
|
17 |
|
18 require_once 'Twig-1.3.0/lib/Twig/Autoloader.php'; |
|
19 Twig_Autoloader::register(); |
|
20 $twig = new Twig_Environment(new Twig_Loader_Filesystem("templates")); |
|
21 |
|
22 if (isset($_REQUEST['action'])) { |
|
23 |
|
24 try { |
|
25 if (preg_match('/^[\w]+\//', ABOOK)) |
|
26 $abook = new Address_Book_File(FILE); |
|
27 else |
|
28 $abook = new Address_Book_DB(DB); |
|
29 |
|
30 switch (@$_REQUEST['action']) { |
|
31 // Nach dem Eintragen bleiben wir auf der Eintragsseite, |
|
32 // aber wir verhindern Duplikate, die mit RELOAD passieren |
|
33 case 'add': if ($abook->add_entry($_REQUEST)) { |
|
34 header("Location: $_SERVER[PHP_SELF]?action=add"); |
|
35 exit(0); |
|
36 } |
|
37 echo $twig->render("add.html", array()); |
|
38 exit; |
|
39 break; |
|
40 |
|
41 // Suchen… |
|
42 case 'search': $entries = null; |
|
43 $error = null; |
|
44 try { |
|
45 $entries = $abook->search_entries($_REQUEST['pattern']); |
|
46 } |
|
47 catch (Address_Book_Exception $e) { |
|
48 $error = $e->getMessage(); |
|
49 } |
|
50 |
|
51 if (@$_REQUEST['format'] == 'table') { |
|
52 if (is_numeric($_REQUEST['max'])) { |
|
53 $left = count($entries) - $_REQUEST['max']; |
|
54 if ($left > 0) |
|
55 $entries = array_slice($entries, 0, $_REQUEST['max']); |
|
56 else |
|
57 $left = NULL; |
|
58 } |
|
59 echo $twig->render("results.html", |
|
60 array('entries' => $entries, |
|
61 'left' => $left, 'error' => $error)); |
|
62 exit; |
|
63 } |
|
64 echo $twig->render("search.html", |
|
65 array('entries' => $entries, 'error' => $error)); |
|
66 exit; |
|
67 } |
|
68 break; |
|
69 |
|
70 } |
|
71 catch (Address_Book_Exception $e) { |
|
72 header("Content-Type: text/plain; charset=utf-8"); |
|
73 print "Address Book Exception: " . $e->getMessage(); |
|
74 exit; |
|
75 } |
|
76 catch (Exception $e) { |
|
77 header("Content-Type: text/plain; charset=utf-8"); |
|
78 print "Ohoh\n\n" . $e; |
|
79 exit; |
|
80 } |
|
81 } |
|
82 |
|
83 echo $twig->render("search.html", array()); |
|