1 <?php |
1 <?php |
2 |
2 |
3 const FILE = "var/abook.txt"; // parent dir must exist |
3 const FILE = "var/abook.txt"; |
4 const RS = "\n"; |
|
5 const FS = "\t"; |
|
6 |
4 |
7 function open_db($file) { |
5 require 'addressbook.class.php'; |
8 @mkdir(dirname($file)); |
|
9 |
6 |
10 $fh = fopen($file, "a+b"); |
7 $abook = new AddressBook(FILE); |
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 |
8 |
20 return $fh; |
9 switch (@$_REQUEST['action']) { |
21 } |
10 case 'add': if ($abook->add_entry($_REQUEST)) { |
22 |
11 header("Location: $_SERVER[PHP_SELF]?action=add"); |
23 function get_entries($fh) { |
12 exit(0); |
24 $entries = array(); |
13 } |
25 fseek($fh, 0, SEEK_SET); |
14 break; |
26 while($line = stream_get_line($fh, 0, RS)) { |
15 case 'search': $entries = $abook->search_entries($_REQUEST['pattern']); |
27 if ($line === FALSE) break; |
16 if (@$_REQUEST['format'] == 'table') { |
28 $entries[] = explode(FS, $line); |
17 header("Content-Type: text/html; charset=UTF-8"); |
29 } |
18 if (!$entries) { |
30 return $entries; |
19 echo "Sorry, keine Einträge."; |
31 } |
20 exit(0); |
32 |
21 } |
33 function add_entry($fh, $entry) { |
22 echo "<table><tr><th>Name<th>Tel<th>Mail</tr>"; |
34 $fields = array('name', 'tel', 'mail'); |
23 for ($i = 1; $entry = array_shift($entries); $i++) { |
35 |
24 echo "<tr>" |
36 $new = array(); |
25 . "<td>" . htmlspecialchars($entry['name']) |
37 foreach($fields as $key) { |
26 . "<td>" . htmlspecialchars($entry['tel']) |
38 if (!isset($entry[$key])) return; |
27 . "<td>" . htmlspecialchars($entry['mail']); |
39 $new[$key] = $entry[$key]; |
28 if ($i > @$_REQUEST['max']) break; |
40 trim($new[$key]); |
29 } |
41 preg_replace('/['.RS.FS.' ]+/', ' ', $new[$key]); |
30 echo "</table>"; |
42 if (empty($new[$key])) return; |
31 if ($entries) |
43 } |
32 echo "<p>Und noch ".count($entries)." weitere Einträge"; |
44 |
33 exit(0); |
45 flock($fh, LOCK_EX); |
34 } |
46 fputs($fh, join(FS, $new) . RS); |
35 break; |
47 flock($fh, LOCK_UN); |
|
48 header("Location: $_SERVER[PHP_SELF]?action=add"); |
|
49 exit; |
|
50 } |
|
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 } |
36 } |
75 |
37 |
76 |
38 |
77 ?> |
39 ?> |
78 <!-- HTML --> |
40 <!-- HTML --> |
|
41 <?=header("Content-Type: text/html; charset=UTF-8");?> |
79 <html> |
42 <html> |
|
43 <head> |
|
44 |
|
45 <script type=text/javascript> |
|
46 // source: http://de.wikibooks.org/wiki/Websiteentwicklung:_AJAX:_Erstes_Programm |
|
47 var _ajax; |
|
48 |
|
49 function got_answer() { |
|
50 if (_ajax.readyState == 4 && _ajax.status == 200) { |
|
51 document.getElementById('result').innerHTML = _ajax.responseText; |
|
52 } |
|
53 } |
|
54 |
|
55 function search_entries(value) { |
|
56 value = encodeURIComponent(value); |
|
57 request = "?action=search&format=table&max=3&pattern=" + value; |
|
58 _ajax = new XMLHttpRequest(); |
|
59 _ajax.onreadystatechange = got_answer; |
|
60 _ajax.open("GET", request, true); |
|
61 _ajax.send(); |
|
62 } |
|
63 </script> |
80 |
64 |
81 <style type="text/css"> |
65 <style type="text/css"> |
82 form label { display:block; float:left; width:10ex; } |
66 form label { display:block; float:left; width:10ex; } |
83 </style> |
67 </style> |
84 |
68 |
85 <body> |
69 </head><body> |
86 |
70 |
87 <? if ($_REQUEST['action'] == 'add') { ?> |
71 <? if (@$_REQUEST['action'] == 'add') { ?> |
88 [ <a href="<?=$_SERVER['PHP_SELF']?>">Home</a> ] |
72 [ <a href="<?=$_SERVER['PHP_SELF']?>">Home</a> ] |
89 <? } else { ?> |
73 <? } else { ?> |
90 [ <a href="<?=$_SERVER['PHP_SELF']?>?action=add">Add Entries</a> ] |
74 [ <a href="<?=$_SERVER['PHP_SELF']?>?action=add">Add Entries</a> ] |
91 <? } ?> |
75 <? } ?> |
92 |
76 |
|
77 <div id=debug> |
|
78 DEBUG |
|
79 </div> |
93 <h1>Adressbuch</h1> |
80 <h1>Adressbuch</h1> |
94 |
81 |
95 <? if ($_REQUEST['action'] == 'add') { ?> |
82 <? if (@$_REQUEST['action'] == 'add') { ?> |
96 <p> |
83 <p> |
97 <form> |
84 <form> |
98 <label for=name>Name</label> |
85 <label for=name>Name</label> |
99 <input id=name type=text name=name /><br> |
86 <input id=name type=text name=name /><br> |
100 <label for=tel>Telefon</label> |
87 <label for=tel>Telefon</label> |