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.
21 class abook_local_file
extends addressbook_backend
{
23 var $bname = "local_file";
30 // ========================== Private =======================
33 function abook_local_file($param) {
34 $this->sname
= _("Personal address book");
35 $this->umask
= Umask();
37 if(is_array($param)) {
38 if(empty($param["filename"]))
39 return $this->set_error("Invalid parameters");
40 if(!is_string($param["filename"]))
41 return $this->set_error($param["filename"] . ": ".
42 _("Not a file name"));
44 $this->filename
= $param["filename"];
48 if(isset($param["umask"]))
49 $this->umask
= $param["umask"];
51 if(!empty($param["name"]))
52 $this->sname
= $param["name"];
56 $this->set_error("Invalid argument to constructor");
60 // Open the addressbook file and store the file pointer.
61 // Use $file as the file to open, or the class' own
62 // filename property. If $param is empty and file is
64 function open($new = false) {
66 $file = $this->filename
;
67 $create = $this->create
;
69 // Return true is file is open and $new is unset
70 if($this->filehandle
&& !$new)
73 // Check that new file exitsts
74 if((!(file_exists($file) && is_readable($file))) && !$create)
75 return $this->set_error("$file: " .
76 _("No such file or directory"));
78 // Close old file, if any
79 if($this->filehandle
) $this->close();
81 // Open file. First try to open for reading and writing,
82 // but fall back to read only.
84 $fh = @fopen
($file, "a+");
86 $this->filehandle
= &$fh;
87 $this->filename
= $file;
88 $this->writeable
= true;
90 $fh = @fopen
($file, "r");
92 $this->filehandle
= &$fh;
93 $this->filename
= $file;
94 $this->writeable
= false;
96 return $this->set_error("$file: "._("Open failed"));
103 // Close the file and forget the filehandle
105 @fclose
($this->filehandle
);
106 $this->filehandle
= 0;
107 $this->filename
= "";
108 $this->writable
= false;
111 // Lock the datafile - try 20 times in 5 seconds
113 for($i = 0 ; $i < 20 ; $i++
) {
114 if(flock($this->filehandle
, 2 +
4))
124 return flock($this->filehandle
, 3);
127 // Overwrite the file with data from $rows
128 // NOTE! Previous locks are broken by this function
129 function overwrite($rows) {
130 $newfh = @fopen
($this->filename
, "w");
132 return $this->set_error("$file: "._("Open failed"));
134 for($i = 0 ; $i < sizeof($rows) ; $i++
) {
135 if(is_array($rows[$i]))
136 fwrite($newfh, join("|", $rows[$i])."\n");
145 // ========================== Public ========================
148 function search($expr) {
150 // To be replaced by advanded search expression parsing
151 if(is_array($expr)) return;
153 // Make regexp from glob'ed expression
154 $expr = ereg_replace("\?", ".", $expr);
155 $expr = ereg_replace("\*", ".*", $expr);
161 @rewind
($this->filehandle
);
163 while ($row = @fgetcsv
($this->filehandle
, 2048, "|")) {
164 $line = join(" ", $row);
165 if(eregi($expr, $line)) {
166 array_push($res, array("nickname" => $row[0],
167 "name" => $row[1] . " " . $row[2],
168 "firstname" => $row[1],
169 "lastname" => $row[2],
172 "backend" => $this->bnum
,
173 "source" => &$this->sname
));
181 function lookup($alias) {
185 $alias = strtolower($alias);
188 @rewind
($this->filehandle
);
190 while ($row = @fgetcsv
($this->filehandle
, 2048, "|")) {
191 if(strtolower($row[0]) == $alias) {
192 return array("nickname" => $row[0],
193 "name" => $row[1] . " " . $row[2],
194 "firstname" => $row[1],
195 "lastname" => $row[2],
198 "backend" => $this->bnum
,
199 "source" => &$this->sname
);
206 // List all addresses
207 function list_addr() {
210 @rewind
($this->filehandle
);
212 while ($row = @fgetcsv
($this->filehandle
, 2048, "|")) {
213 array_push($res, array("nickname" => $row[0],
214 "name" => $row[1] . " " . $row[2],
215 "firstname" => $row[1],
216 "lastname" => $row[2],
219 "backend" => $this->bnum
,
220 "source" => &$this->sname
));
226 function add($userdata) {
227 if(!$this->writeable
)
228 return $this->set_error(_("Addressbook is read-only"));
230 // See if user exist already
231 $ret = $this->lookup($userdata["nickname"]);
233 return $this->set_error(sprintf(_("User '%s' already exist"),
236 // Here is the data to write
237 $data = sprintf("%s|%s|%s|%s|%s", $userdata["nickname"],
238 $userdata["firstname"], $userdata["lastname"],
239 $userdata["email"], $userdata["label"]);
241 $data = ereg_replace("[\r\n]", " ", $data);
242 // Add linefeed at end
245 // Reopen file, just to be sure
247 if(!$this->writeable
)
248 return $this->set_error(_("Addressbook is read-only"));
252 return $this->set_error(_("Could not lock datafile"));
255 $r = fwrite($this->filehandle
, $data);
260 // Test write result and exit if OK
261 if($r > 0) return true;
264 $this->set_error(_("Write to addressbook failed"));
269 function remove($alias) {
270 if(!$this->writeable
)
271 return $this->set_error(_("Addressbook is read-only"));
273 // Lock the file to make sure we're the only process working
276 return $this->set_error(_("Could not lock datafile"));
278 // Read file into memory, ignoring nicknames to delete
280 @rewind
($this->filehandle
);
283 while($row = @fgetcsv
($this->filehandle
, 2048, "|")) {
284 if(!in_array($row[0], $alias))
289 if(!$this->overwrite(&$rows)) {
299 function modify($alias, $userdata) {
300 if(!$this->writeable
)
301 return $this->set_error(_("Addressbook is read-only"));
304 $ret = $this->lookup($alias);
306 return $this->set_error(sprintf(_("User '%s' does not exist"),
309 // Lock the file to make sure we're the only process working
312 return $this->set_error(_("Could not lock datafile"));
314 // Read file into memory, modifying the data for the
315 // user identifyed by $alias
317 @rewind
($this->filehandle
);
320 while($row = @fgetcsv
($this->filehandle
, 2048, "|")) {
321 if(strtolower($row[0]) != strtolower($alias)) {
324 $rows[$i++
] = array(0 => $userdata["nickname"],
325 1 => $userdata["firstname"],
326 2 => $userdata["lastname"],
327 3 => $userdata["email"],
328 4 => $userdata["label"]);
333 if(!$this->overwrite(&$rows)) {
342 } // End of class abook_local_file