1 <?php |
1 <?php |
2 |
2 |
3 const FILE = "var/abook.txt"; // parent dir must exist |
3 // configuration |
4 const RS = "\n"; |
4 const FILE = "var/abook.txt"; |
5 const FS = "\t"; |
|
6 |
5 |
7 function open_db($file) { |
6 set_error_handler(create_function( |
8 @mkdir(dirname($file)); |
7 '$errno, $errstr, $errfile, $errline, $errcontext', |
|
8 'if (!error_reporting()) return; |
|
9 throw new ErrorException($errstr, 0, $errno, $errfile, $errline);')); |
9 |
10 |
10 $fh = fopen($file, "a+b"); |
11 // no user serviceable parts below! |
11 flock($fh, LOCK_EX); |
|
12 $stat = fstat($fh); |
|
13 if ($stat['size'] == 0) { |
|
14 fputs($fh, join(FS, array("Hans Hanson", "0815", "hans@hanson.de")) |
|
15 . RS); |
|
16 } |
|
17 flock($fh, LOCK_SH); |
|
18 fseek($fh, 0, SEEK_SET); |
|
19 |
12 |
20 return $fh; |
13 function __autoload($class) { |
|
14 require_once "class.$class.php"; |
21 } |
15 } |
22 |
16 |
23 function get_entries($fh) { |
17 try { |
24 $entries = array(); |
18 $abook = new Address_Book_File(FILE); |
25 fseek($fh, 0, SEEK_SET); |
19 switch (@$_REQUEST['action']) { |
26 while($line = stream_get_line($fh, 0, RS)) { |
20 case 'add': $abook->add_entry($_REQUEST); |
27 if ($line === FALSE) break; |
21 break; |
28 $entries[] = explode(FS, $line); |
22 case 'search': $entries = $abook->search_entries($_REQUEST['pattern']); |
29 } |
23 break; |
30 return $entries; |
|
31 } |
|
32 |
|
33 function add_entry($fh, $entry) { |
|
34 $fields = array('name', 'tel', 'mail'); |
|
35 |
|
36 $new = array(); |
|
37 foreach($fields as $key) { |
|
38 if (!isset($entry[$key])) return; |
|
39 $new[$key] = $entry[$key]; |
|
40 trim($new[$key]); |
|
41 preg_replace('/['.RS.FS.' ]+/', ' ', $new[$key]); |
|
42 if (empty($new[$key])) return; |
|
43 } |
24 } |
44 |
25 |
45 flock($fh, LOCK_EX); |
26 } |
46 fputs($fh, join(FS, $new) . RS); |
27 catch (ErrorException $e) { |
47 flock($fh, LOCK_UN); |
28 print "INTERNAL ERROR: " . $e->getMessage(); |
48 header("Location: $_SERVER[PHP_SELF]?action=add"); |
29 } |
|
30 catch (Exception $e) { |
|
31 print "Application error: " . $e->getMessage(); |
49 exit; |
32 exit; |
50 } |
33 } |
51 |
|
52 function search_entries($fh, $pattern) { |
|
53 fseek($fh, 0, SEEK_SET); |
|
54 $entries = array(); |
|
55 while ($line = stream_get_line($fh, 0, RS)) { |
|
56 if ($line === FALSE) break; |
|
57 if (!preg_match("/$pattern/i", $line)) continue; |
|
58 $entry = explode(FS, $line); |
|
59 $entries[] = array('name' => $entry[0], |
|
60 'tel' => $entry[1], |
|
61 'mail' => $entry[2]); |
|
62 } |
|
63 return $entries; |
|
64 } |
|
65 |
|
66 |
|
67 $abook = open_db(FILE); |
|
68 |
|
69 switch ($_REQUEST['action']) { |
|
70 case 'add': add_entry($abook, $_REQUEST); |
|
71 break; |
|
72 case 'search': $entries = search_entries($abook, $_REQUEST['pattern']); |
|
73 break; |
|
74 } |
|
75 |
|
76 |
34 |
77 ?> |
35 ?> |
78 <!-- HTML --> |
36 <!-- HTML --> |
79 <html> |
37 <html> |
80 |
38 |
82 form label { display:block; float:left; width:10ex; } |
40 form label { display:block; float:left; width:10ex; } |
83 </style> |
41 </style> |
84 |
42 |
85 <body> |
43 <body> |
86 |
44 |
87 <? if ($_REQUEST['action'] == 'add') { ?> |
45 <? if (@$_REQUEST['action'] == 'add') { ?> |
88 [ <a href="<?=$_SERVER['PHP_SELF']?>">Home</a> ] |
46 [ <a href="<?=$_SERVER['PHP_SELF']?>">Home</a> ] |
89 <? } else { ?> |
47 <? } else { ?> |
90 [ <a href="<?=$_SERVER['PHP_SELF']?>?action=add">Add Entries</a> ] |
48 [ <a href="<?=$_SERVER['PHP_SELF']?>?action=add">Add Entries</a> ] |
91 <? } ?> |
49 <? } ?> |
92 |
50 |
93 <h1>Adressbuch</h1> |
51 <h1>Adressbuch</h1> |
94 |
52 |
95 <? if ($_REQUEST['action'] == 'add') { ?> |
53 <? if (@$_REQUEST['action'] == 'add') { ?> |
96 <p> |
54 <p> |
97 <form> |
55 <form> |
98 <label for=name>Name</label> |
56 <label for=name>Name</label> |
99 <input id=name type=text name=name /><br> |
57 <input id=name type=text name=name /><br> |
100 <label for=tel>Telefon</label> |
58 <label for=tel>Telefon</label> |
111 <input type=text name=pattern onChange="submit()" /> |
69 <input type=text name=pattern onChange="submit()" /> |
112 <input type=hidden name=action value=search /> |
70 <input type=hidden name=action value=search /> |
113 <noscript><input type=submit /></noscript> |
71 <noscript><input type=submit /></noscript> |
114 </form> |
72 </form> |
115 |
73 |
116 <? if ($entries) { ?> |
74 <? if (@$entries) { ?> |
117 <table> |
75 <table> |
118 <tr><th>Name<th>Telefon<th>Mail</tr> |
76 <tr><th>Name<th>Telefon<th>Mail</tr> |
119 <? foreach ($entries as $entry) { ?> |
77 <? foreach ($entries as $entry) { ?> |
120 <tr> |
78 <tr> |
121 <td><?=$entry['name']?> |
79 <td><?=$entry['name']?> |