# HG changeset patch # User Heiko Schlittermann (JUMPER) # Date 1319574276 -7200 # Node ID def69d70eb6e1026c790f21c942d1f80f895d300 # Parent ae829f97a638598814591129db7664af59fb27c6 Using a class and AJAX! diff -r ae829f97a638 -r def69d70eb6e .htaccess --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.htaccess Tue Oct 25 22:24:36 2011 +0200 @@ -0,0 +1,1 @@ +php_flag display_errors 1 diff -r ae829f97a638 -r def69d70eb6e addressbook.class.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/addressbook.class.php Tue Oct 25 22:24:36 2011 +0200 @@ -0,0 +1,75 @@ +fh = fopen($file, "a+b"); + flock($this->fh, LOCK_EX); + $stat = fstat($this->fh); + if ($stat['size'] == 0) { + fputs($this->fh, join(self::FS, array("Hans Hanson", "0815", "hans@hanson.de")) + . self::RS); + } + flock($this->fh, LOCK_SH); + fseek($this->fh, 0, SEEK_SET); + } + + function get_entries() { + $entries = array(); + fseek($this->fh, 0, SEEK_SET); + while($line = stream_get_line($this->fh, 0, self::RS)) { + if ($line === FALSE) break; + $entries[] = explode(self::FS, $line); + } + return $entries; + } + + function add_entry($entry) { + $fields = array('name', 'tel', 'mail'); + + $new = array(); + foreach($fields as $key) { + if (!isset($entry[$key])) return false; + $new[$key] = trim($entry[$key]); + $new[$key] = preg_replace('/['.self::RS.self::FS.' ]+/', ' ', $new[$key]); + if (empty($new[$key])) return false; + } + + flock($this->fh, LOCK_EX); + fputs($this->fh, join(self::FS, $new) . self::RS); + flock($this->fh, LOCK_UN); + + return true; + } + + function search_entries($pattern) { + $pattern = trim($pattern); + if (empty($pattern)) return; + $pattern = preg_replace('/\//', '\/', $pattern); + fseek($this->fh, 0, SEEK_SET); + $entries = array(); + while ($line = stream_get_line($this->fh, 0, self::RS)) { + if ($line === FALSE) break; + if (!preg_match("/$pattern/i", $line)) continue; + $entry = explode(self::FS, $line); + $entries[] = array('name' => $entry[0], + 'tel' => $entry[1], + 'mail' => $entry[2]); + } + return $entries; + } +} + +?> diff -r ae829f97a638 -r def69d70eb6e index.php --- a/index.php Tue Oct 25 16:45:47 2011 +0200 +++ b/index.php Tue Oct 25 22:24:36 2011 +0200 @@ -1,98 +1,85 @@ $entry[0], - 'tel' => $entry[1], - 'mail' => $entry[2]); - } - return $entries; -} - - -$abook = open_db(FILE); - -switch ($_REQUEST['action']) { - case 'add': add_entry($abook, $_REQUEST); - break; - case 'search': $entries = search_entries($abook, $_REQUEST['pattern']); - break; +switch (@$_REQUEST['action']) { + case 'add': if ($abook->add_entry($_REQUEST)) { + header("Location: $_SERVER[PHP_SELF]?action=add"); + exit(0); + } + break; + case 'search': $entries = $abook->search_entries($_REQUEST['pattern']); + if (@$_REQUEST['format'] == 'table') { + header("Content-Type: text/html; charset=UTF-8"); + if (!$entries) { + echo "Sorry, keine Einträge."; + exit(0); + } + echo ""; + for ($i = 1; $entry = array_shift($entries); $i++) { + echo "" + . "
NameTelMail
" . htmlspecialchars($entry['name']) + . "" . htmlspecialchars($entry['tel']) + . "" . htmlspecialchars($entry['mail']); + if ($i > @$_REQUEST['max']) break; + } + echo "
"; + if ($entries) + echo "

Und noch ".count($entries)." weitere Einträge"; + exit(0); + } + break; } ?> + + + + - + - + [ Home ] [ Add Entries ] +

+ DEBUG +

Adressbuch

- +

@@ -107,13 +94,16 @@ - - - + + +
- +
+ @@ -127,6 +117,7 @@ Sorry. +
NameTelefonMail