# HG changeset patch # User Heiko Schlittermann (IT Trainingshaus) # Date 1319632758 -7200 # Node ID fb55da5ecb8a38b26c92ed66be4b811a26e72d4f # Parent 2a05edf9dc87f0b3c303ba0ab6cad10b03414732 OOP diff -r 2a05edf9dc87 -r fb55da5ecb8a class.Address_Book_File.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/class.Address_Book_File.php Wed Oct 26 14:39:18 2011 +0200 @@ -0,0 +1,70 @@ +fh = fopen($file, "a+b"); + if (!$this->fh) { + throw new Exception("Oooops, Problem mit $file"); + } + 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); + } + + public function get_all_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; + } + + public function add_entry($entry) { + $fields = array('name', 'tel', 'mail'); + $new = array(); + foreach($fields as $key) { + if (!isset($entry[$key])) return; + $new[$key] = $entry[$key]; + trim($new[$key]); + preg_replace('/['.self::RS.self::FS.' ]+/', ' ', $new[$key]); + if (empty($new[$key])) return; + } + + flock($this->fh, LOCK_EX); + fputs($this->fh, join(self::FS, $new) . self::RS); + flock($this->fh, LOCK_UN); + header("Location: $_SERVER[PHP_SELF]?action=add"); + exit; + } + + public function search_entries($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; + } + + public function delete_entries($ids) { } +} + +?>