|
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 = "php/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/twig")); |
|
21 |
|
22 if (isset($_REQUEST['action'])) { |
|
23 |
|
24 try { |
|
25 |
|
26 switch ($_SERVER['PATH_INFO']) { |
|
27 case "/db": $abook = new Address_Book_DB(DB); break; |
|
28 case "/file": |
|
29 default: $abook = new Address_Book_File(FILE); break; |
|
30 }; |
|
31 |
|
32 switch (@$_REQUEST['action']) { |
|
33 // Nach dem Eintragen bleiben wir auf der Eintragsseite, |
|
34 // aber wir verhindern Duplikate, die mit RELOAD passieren |
|
35 case 'add': if ($abook->add_entry($_REQUEST)) { |
|
36 header("Location: $_SERVER[PHP_SELF]?action=add"); |
|
37 exit(0); |
|
38 } |
|
39 echo $twig->render("add.html", array()); |
|
40 exit; |
|
41 break; |
|
42 |
|
43 // Suchen… |
|
44 case 'search': $entries = null; |
|
45 $error = null; |
|
46 try { |
|
47 $entries = $abook->search_entries($_REQUEST['pattern']); |
|
48 } |
|
49 catch (Address_Book_Exception $e) { |
|
50 $error = $e->getMessage(); |
|
51 } |
|
52 |
|
53 if (@$_REQUEST['format'] == 'table') { |
|
54 if (is_numeric($_REQUEST['max'])) { |
|
55 $left = count($entries) - $_REQUEST['max']; |
|
56 if ($left > 0) |
|
57 $entries = array_slice($entries, 0, $_REQUEST['max']); |
|
58 else |
|
59 $left = NULL; |
|
60 } |
|
61 echo $twig->render("results.html", |
|
62 array('entries' => $entries, |
|
63 'left' => $left, 'error' => $error)); |
|
64 exit; |
|
65 } |
|
66 echo $twig->render("search.html", |
|
67 array('entries' => $entries, 'error' => $error)); |
|
68 exit; |
|
69 } |
|
70 break; |
|
71 |
|
72 } |
|
73 catch (Address_Book_Exception $e) { |
|
74 header("Content-Type: text/plain; charset=utf-8"); |
|
75 print "Address Book Exception: " . $e->getMessage(); |
|
76 exit; |
|
77 } |
|
78 catch (Exception $e) { |
|
79 header("Content-Type: text/plain; charset=utf-8"); |
|
80 print "Ohoh\n\n" . $e; |
|
81 exit; |
|
82 } |
|
83 } |
|
84 |
|
85 echo $twig->render("search.html", array()); |