4 ** abook_local_file.php
6 ** Backend for addressbook as a pipe separated file
8 ** An array with the following elements must be passed to
9 ** the class constructor (elements marked ? are optional):
11 ** filename => path to addressbook file
12 ** ? create => if true: file is created if it does not exist.
13 ** ? umask => umask set before opening file.
15 ** NOTE. This class should not be used directly. Use the
16 ** "AddressBook" class instead.
19 class abook_local_file
extends addressbook_backend
{
21 var $bname = "local_file";
28 // ========================== Private =======================
31 function abook_local_file($param) {
32 $this->sname
= _("Personal address book");
33 $this->umask
= Umask();
35 if(is_array($param)) {
36 if(empty($param["filename"]))
37 return $this->set_error("Invalid parameters");
38 if(!is_string($param["filename"]))
39 return $this->set_error($param["filename"] . ": ".
40 _("Not a file name"));
42 $this->filename
= $param["filename"];
46 if(isset($param["umask"]))
47 $this->umask
= $param["umask"];
51 $this->set_error("Invalid argument to constructor");
55 // Open the addressbook file and store the file pointer.
56 // Use $file as the file to open, or the class' own
57 // filename property. If $param is empty and file is
59 function open($new = false) {
61 $file = $this->filename
;
62 $create = $this->create
;
64 // Return true is file is open and $new is unset
65 if($this->filehandle
&& !$new)
68 // Check that new file exitsts
69 if((!(file_exists($file) && is_readable($file))) && !$create)
70 return $this->set_error("$file: " .
71 _("No such file or directory"));
73 // Close old file, if any
74 if($this->filehandle
) $this->close();
76 // Open file. First try to open for reading and writing,
77 // but fall back to read only.
79 $fh = @fopen
($file, "a+");
81 $this->filehandle
= &$fh;
82 $this->filename
= $file;
83 $this->writeable
= true;
85 $fh = @fopen
($file, "r");
87 $this->filehandle
= &$fh;
88 $this->filename
= $file;
89 $this->writeable
= false;
91 return $this->set_error("$file: "._("Open failed"));
98 // Close the file and forget the filehandle
100 @fclose
($this->filehandle
);
101 $this->filehandle
= 0;
102 $this->filename
= "";
103 $this->writable
= false;
106 // ========================== Public ========================
109 function search($expr) {
111 // To be replaced by advanded search expression parsing
112 if(is_array($expr)) return;
114 // Make regexp from glob'ed expression
115 $expr = ereg_replace("\?", ".", $expr);
116 $expr = ereg_replace("\*", ".*", $expr);
122 @rewind
($this->filehandle
);
124 while ($row = @fgetcsv
($this->filehandle
, 2048, "|")) {
125 $line = join(" ", $row);
126 if(eregi($expr, $line)) {
127 array_push($res, array("nickname" => $row[0],
128 "name" => $row[1] . " " . $row[2],
129 "firstname" => $row[1],
130 "lastname" => $row[2],
133 "backend" => $this->bnum
,
134 "source" => &$this->sname
));
142 function lookup($alias) {
146 $alias = strtolower($alias);
149 @rewind
($this->filehandle
);
151 while ($row = @fgetcsv
($this->filehandle
, 2048, "|")) {
152 if(strtolower($row[0]) == $alias) {
153 return array("nickname" => $row[0],
154 "name" => $row[1] . " " . $row[2],
155 "firstname" => $row[1],
156 "lastname" => $row[2],
159 "backend" => $this->bnum
,
160 "source" => &$this->sname
);
167 // List all addresses
168 function list_addr() {
171 @rewind
($this->filehandle
);
173 while ($row = @fgetcsv
($this->filehandle
, 2048, "|")) {
174 array_push($res, array("nickname" => $row[0],
175 "name" => $row[1] . " " . $row[2],
176 "firstname" => $row[1],
177 "lastname" => $row[2],
180 "backend" => $this->bnum
,
181 "source" => &$this->sname
));
187 function add($userdata) {
188 if(!$this->writeable
)
189 return $this->set_error(_("Addressbook is read-only"));
191 // See if user exist already
192 $ret = $this->lookup($userdata["nickname"]);
194 return $this->set_error(sprintf(_("User '%s' already exist"),
197 // Here is the data to write
198 $data = sprintf("%s|%s|%s|%s|%s", $userdata["nickname"],
199 $userdata["firstname"], $userdata["lastname"],
200 $userdata["email"], $userdata["label"]);
202 $data = ereg_replace("[\r\n]", " ", $data);
203 // Add linefeed at end
206 // Reopen file, just to be sure
208 if(!$this->writeable
)
209 return $this->set_error(_("Addressbook is read-only"));
211 $r = fwrite($this->filehandle
, $data);
215 $this->set_error(_("Write to addressbook failed"));