addressbook.class.php
changeset 7 47446e75ceea
parent 6 48ffdc97f54b
equal deleted inserted replaced
6:48ffdc97f54b 7:47446e75ceea
    16     private $fh;
    16     private $fh;
    17     private $file;
    17     private $file;
    18 
    18 
    19     function __construct($file) {
    19     function __construct($file) {
    20 	@mkdir(dirname($file));
    20 	@mkdir(dirname($file));
    21 	@$this->fh = fopen($file, "a+b");
    21 	$this->fh = fopen($file, "a+b");
    22 	if (!$this->fh) throw new Address_Book_Exception("Can't open address book.");
       
    23 	flock($this->fh, LOCK_EX);
    22 	flock($this->fh, LOCK_EX);
    24 	$stat = fstat($this->fh);
    23 	$stat = fstat($this->fh);
    25 	if ($stat['size'] == 0) {
    24 	if ($stat['size'] == 0) {
    26 		fputs($this->fh, join(self::FS, array("Hans Hanson", "0815", "hans@hanson.de"))
    25 		fputs($this->fh, join(self::FS, array("Hans Hanson", "0815", "hans@hanson.de"))
    27 			. self::RS);
    26 			. self::RS);
    59     }
    58     }
    60 
    59 
    61     function search_entries($pattern) {
    60     function search_entries($pattern) {
    62 	$pattern = trim($pattern);
    61 	$pattern = trim($pattern);
    63 	if (empty($pattern)) return;
    62 	if (empty($pattern)) return;
    64 	$pattern = preg_replace('/\//', '\/', $pattern);
    63 	//$pattern = preg_replace('/\//', '\/', $pattern);
       
    64 	if (preg_match('/\//', $pattern))
       
    65 	    throw new Address_Book_Exception("Pattern must not contain '/'");
    65 	fseek($this->fh, 0, SEEK_SET);
    66 	fseek($this->fh, 0, SEEK_SET);
    66 	$entries = array();
    67 	$entries = array();
    67 	while ($line = stream_get_line($this->fh, 0, self::RS)) {
    68 	while ($line = stream_get_line($this->fh, 0, self::RS)) {
    68 		if ($line === FALSE) break;
    69 		if ($line === FALSE) break;
    69 		if (!preg_match("/$pattern/i", $line)) continue;
    70 		if (!preg_match("/$pattern/i", $line)) continue;