1 <?php |
1 <?php |
2 |
2 |
3 // configuration |
|
4 const FILE = "var/abook.txt"; |
3 const FILE = "var/abook.txt"; |
|
4 const DB = "sqlite:var/abook.sqlite"; |
|
5 const ABOOK = DB; |
5 |
6 |
6 set_error_handler(create_function( |
7 set_error_handler(create_function( |
7 '$errno, $errstr, $errfile, $errline, $errcontext', |
8 '$errno, $errstr, $errfile, $errline, $errcontext', |
8 'if (!error_reporting()) return; |
9 'if (!error_reporting()) return; |
9 throw new ErrorException($errstr, 0, $errno, $errfile, $errline);')); |
10 throw new ErrorException($errstr, 0, $errno, $errfile, $errline);'), -1); |
10 |
11 |
11 // no user serviceable parts below! |
12 spl_autoload_register(create_function( |
|
13 '$class', |
|
14 '$file = "class.$class.php"; |
|
15 if (!file_exists($file)) return; |
|
16 require_once $file;')); |
12 |
17 |
13 function __autoload($class) { |
18 require_once 'Twig-1.3.0/lib/Twig/Autoloader.php'; |
14 require_once "class.$class.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 } |
15 } |
81 } |
16 |
82 |
17 try { |
83 echo $twig->render("search.html", array()); |
18 //$abook = new Address_Book_DB("mysql:host=localhost;dbname=abook", "hans", "x"); |
|
19 $abook = new Address_Book_DB("sqlite:var/abook.sqlite"); |
|
20 switch (@$_REQUEST['action']) { |
|
21 case 'add': $abook->add_entry($_REQUEST); |
|
22 break; |
|
23 case 'search': $entries = $abook->search_entries($_REQUEST['pattern']); |
|
24 break; |
|
25 } |
|
26 |
|
27 } |
|
28 catch (ErrorException $e) { |
|
29 print "INTERNAL ERROR: " . $e->getMessage(); |
|
30 } |
|
31 catch (Exception $e) { |
|
32 print "Application error: " . $e->getMessage(); |
|
33 exit; |
|
34 } |
|
35 |
|
36 ?> |
|
37 <!-- HTML --> |
|
38 <html> |
|
39 |
|
40 <style type="text/css"> |
|
41 form label { display:block; float:left; width:10ex; } |
|
42 </style> |
|
43 |
|
44 <body> |
|
45 |
|
46 <? if (@$_REQUEST['action'] == 'add') { ?> |
|
47 [ <a href="<?=$_SERVER['PHP_SELF']?>">Home</a> ] |
|
48 <? } else { ?> |
|
49 [ <a href="<?=$_SERVER['PHP_SELF']?>?action=add">Add Entries</a> ] |
|
50 <? } ?> |
|
51 |
|
52 <h1>Adressbuch</h1> |
|
53 |
|
54 <? if (@$_REQUEST['action'] == 'add') { ?> |
|
55 <p> |
|
56 <form> |
|
57 <label for=name>Name</label> |
|
58 <input id=name type=text name=name /><br> |
|
59 <label for=tel>Telefon</label> |
|
60 <input id=tel type=text name=tel /><br> |
|
61 <label for=mail>Mail</label> |
|
62 <input id=mail type=text name=mail /><br> |
|
63 <input type=hidden name=action value=add /> |
|
64 <input type=submit /> |
|
65 </form> |
|
66 |
|
67 <? } else { ?> |
|
68 |
|
69 <form> |
|
70 <input type=text name=pattern onChange="submit()" /> |
|
71 <input type=hidden name=action value=search /> |
|
72 <noscript><input type=submit /></noscript> |
|
73 </form> |
|
74 |
|
75 <? if (@$entries) { ?> |
|
76 <table> |
|
77 <tr><th>Name<th>Telefon<th>Mail</tr> |
|
78 <? foreach ($entries as $entry) { ?> |
|
79 <tr> |
|
80 <td><?=$entry['name']?> |
|
81 <td><?=$entry['tel']?> |
|
82 <td><?=$entry['mail']?> |
|
83 </tr> |
|
84 <? } ?> |
|
85 </table> |
|
86 <? } else { ?> |
|
87 Sorry. |
|
88 <? } ?> |
|
89 |
|
90 <? } ?> |
|
91 |
|
92 </body> |
|