5100704d |
1 | <?php |
2 | |
35586184 |
3 | /** |
4 | * abook_local_file.php |
5 | * |
15e6162e |
6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
35586184 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * Backend for addressbook as a pipe separated file |
10 | * |
11 | * An array with the following elements must be passed to |
12 | * the class constructor (elements marked ? are optional): |
13 | * |
14 | * filename => path to addressbook file |
15 | * ? create => if true: file is created if it does not exist. |
16 | * ? umask => umask set before opening file. |
17 | * |
18 | * NOTE. This class should not be used directly. Use the |
19 | * "AddressBook" class instead. |
20 | * |
21 | * $Id$ |
22 | */ |
5100704d |
23 | |
35586184 |
24 | class abook_local_file extends addressbook_backend { |
06b4facd |
25 | var $btype = 'local'; |
26 | var $bname = 'local_file'; |
27 | |
28 | var $filename = ''; |
29 | var $filehandle = 0; |
30 | var $create = false; |
31 | var $umask; |
32 | |
33 | /* ========================== Private ======================= */ |
34 | |
35 | /* Constructor */ |
36 | function abook_local_file($param) { |
37 | $this->sname = _("Personal address book"); |
38 | $this->umask = Umask(); |
39 | |
40 | if(is_array($param)) { |
41 | if(empty($param['filename'])) { |
42 | return $this->set_error('Invalid parameters'); |
43 | } |
44 | if(!is_string($param['filename'])) { |
45 | return $this->set_error($param['filename'] . ': '. |
46 | _("Not a file name")); |
47 | } |
48 | |
49 | $this->filename = $param['filename']; |
50 | |
51 | if($param['create']) { |
52 | $this->create = true; |
53 | } |
54 | if(isset($param['umask'])) { |
55 | $this->umask = $param['umask']; |
56 | } |
57 | if(!empty($param['name'])) { |
58 | $this->sname = $param['name']; |
59 | } |
60 | |
61 | $this->open(true); |
62 | } else { |
63 | $this->set_error('Invalid argument to constructor'); |
64 | } |
65 | } |
66 | |
67 | /* Open the addressbook file and store the file pointer. |
68 | * Use $file as the file to open, or the class' own |
69 | * filename property. If $param is empty and file is |
70 | * open, do nothing. */ |
71 | function open($new = false) { |
72 | $this->error = ''; |
73 | $file = $this->filename; |
74 | $create = $this->create; |
75 | |
76 | /* Return true is file is open and $new is unset */ |
77 | if($this->filehandle && !$new) { |
78 | return true; |
79 | } |
80 | |
81 | /* Check that new file exitsts */ |
82 | if((!(file_exists($file) && is_readable($file))) && !$create) { |
83 | return $this->set_error("$file: " . _("No such file or directory")); |
84 | } |
85 | |
86 | /* Close old file, if any */ |
87 | if($this->filehandle) { $this->close(); } |
88 | |
89 | /* Open file. First try to open for reading and writing, |
90 | * but fall back to read only. */ |
91 | umask($this->umask); |
92 | $fh = @fopen($file, 'a+'); |
93 | if($fh) { |
94 | $this->filehandle = &$fh; |
95 | $this->filename = $file; |
96 | $this->writeable = true; |
97 | } else { |
98 | $fh = @fopen($file, 'r'); |
99 | if($fh) { |
100 | $this->filehandle = &$fh; |
101 | $this->filename = $file; |
102 | $this->writeable = false; |
103 | } else { |
104 | return $this->set_error("$file: " . _("Open failed")); |
105 | } |
106 | } |
107 | return true; |
108 | } |
109 | |
110 | /* Close the file and forget the filehandle */ |
111 | function close() { |
112 | @fclose($this->filehandle); |
113 | $this->filehandle = 0; |
114 | $this->filename = ''; |
115 | $this->writable = false; |
116 | } |
117 | |
118 | /* Lock the datafile - try 20 times in 5 seconds */ |
119 | function lock() { |
120 | for($i = 0 ; $i < 20 ; $i++) { |
121 | if(flock($this->filehandle, 2 + 4)) |
122 | return true; |
123 | else |
124 | usleep(250000); |
125 | } |
126 | return false; |
127 | } |
128 | |
129 | /* Lock the datafile */ |
130 | function unlock() { |
131 | return flock($this->filehandle, 3); |
132 | } |
133 | |
134 | /* Overwrite the file with data from $rows |
135 | * NOTE! Previous locks are broken by this function */ |
136 | function overwrite(&$rows) { |
01265fba |
137 | $this->unlock(); |
45df3062 |
138 | $newfh = @fopen($this->filename .'.tmp', 'w'); |
06b4facd |
139 | if(!$newfh) { |
45df3062 |
140 | return $this->set_error($this->filename .'.tmp: '. _("Open failed")); |
06b4facd |
141 | } |
142 | |
143 | for($i = 0 ; $i < sizeof($rows) ; $i++) { |
144 | if(is_array($rows[$i])) { |
77ec28e9 |
145 | for($j = 0 ; $j < count($rows[$i]) ; $j++) { |
146 | $rows[$i][$j] = $this->quotevalue($rows[$i][$j]); |
147 | } |
06b4facd |
148 | fwrite($newfh, join('|', $rows[$i]) . "\n"); |
149 | } |
150 | } |
151 | |
152 | fclose($newfh); |
baa59994 |
153 | if (!@copy($this->filename . '.tmp' , $this->filename)) { |
154 | return $this->set_error($file->filename.':' . _("Unable to update")); |
155 | } |
156 | @unlink( $this->filename .'.tmp'); |
06b4facd |
157 | $this->unlock(); |
158 | $this->open(true); |
159 | return true; |
160 | } |
161 | |
162 | /* ========================== Public ======================== */ |
163 | |
164 | /* Search the file */ |
165 | function search($expr) { |
166 | |
167 | /* To be replaced by advanded search expression parsing */ |
168 | if(is_array($expr)) { return; } |
169 | |
170 | /* Make regexp from glob'ed expression |
171 | * May want to quote other special characters like (, ), -, [, ], etc. */ |
172 | $expr = str_replace('?', '.', $expr); |
173 | $expr = str_replace('*', '.*', $expr); |
174 | |
175 | $res = array(); |
176 | if(!$this->open()) { |
177 | return false; |
178 | } |
179 | @rewind($this->filehandle); |
180 | |
181 | while ($row = @fgetcsv($this->filehandle, 2048, '|')) { |
182 | $line = join(' ', $row); |
183 | if(eregi($expr, $line)) { |
184 | array_push($res, array('nickname' => $row[0], |
185 | 'name' => $row[1] . ' ' . $row[2], |
186 | 'firstname' => $row[1], |
187 | 'lastname' => $row[2], |
188 | 'email' => $row[3], |
189 | 'label' => $row[4], |
190 | 'backend' => $this->bnum, |
191 | 'source' => &$this->sname)); |
192 | } |
193 | } |
194 | |
195 | return $res; |
196 | } |
197 | |
198 | /* Lookup alias */ |
199 | function lookup($alias) { |
200 | if(empty($alias)) { |
201 | return array(); |
202 | } |
203 | |
204 | $alias = strtolower($alias); |
205 | |
206 | $this->open(); |
207 | @rewind($this->filehandle); |
208 | |
209 | while ($row = @fgetcsv($this->filehandle, 2048, '|')) { |
210 | if(strtolower($row[0]) == $alias) { |
211 | return array('nickname' => $row[0], |
212 | 'name' => $row[1] . ' ' . $row[2], |
213 | 'firstname' => $row[1], |
214 | 'lastname' => $row[2], |
215 | 'email' => $row[3], |
216 | 'label' => $row[4], |
217 | 'backend' => $this->bnum, |
218 | 'source' => &$this->sname); |
219 | } |
220 | } |
221 | |
222 | return array(); |
223 | } |
224 | |
225 | /* List all addresses */ |
226 | function list_addr() { |
227 | $res = array(); |
228 | $this->open(); |
229 | @rewind($this->filehandle); |
230 | |
231 | while ($row = @fgetcsv($this->filehandle, 2048, '|')) { |
232 | array_push($res, array('nickname' => $row[0], |
233 | 'name' => $row[1] . ' ' . $row[2], |
234 | 'firstname' => $row[1], |
235 | 'lastname' => $row[2], |
236 | 'email' => $row[3], |
237 | 'label' => $row[4], |
238 | 'backend' => $this->bnum, |
239 | 'source' => &$this->sname)); |
240 | } |
241 | return $res; |
242 | } |
243 | |
244 | /* Add address */ |
245 | function add($userdata) { |
246 | if(!$this->writeable) { |
247 | return $this->set_error(_("Addressbook is read-only")); |
248 | } |
249 | /* See if user exists already */ |
250 | $ret = $this->lookup($userdata['nickname']); |
251 | if(!empty($ret)) { |
252 | return $this->set_error(sprintf(_("User '%s' already exist"), |
253 | $ret['nickname'])); |
254 | } |
255 | |
256 | /* Here is the data to write */ |
77ec28e9 |
257 | $data = $this->quotevalue($userdata['nickname']) . '|' . |
258 | $this->quotevalue($userdata['firstname']) . '|' . |
259 | $this->quotevalue($userdata['lastname']) . '|' . |
260 | $this->quotevalue($userdata['email']) . '|' . |
261 | $this->quotevalue($userdata['label']); |
262 | |
06b4facd |
263 | /* Strip linefeeds */ |
264 | $data = ereg_replace("[\r\n]", ' ', $data); |
265 | /* Add linefeed at end */ |
266 | $data = $data . "\n"; |
267 | |
268 | /* Reopen file, just to be sure */ |
269 | $this->open(true); |
270 | if(!$this->writeable) { |
271 | return $this->set_error(_("Addressbook is read-only")); |
272 | } |
273 | |
274 | /* Lock the file */ |
275 | if(!$this->lock()) { |
276 | return $this->set_error(_("Could not lock datafile")); |
277 | } |
278 | |
279 | /* Write */ |
280 | $r = fwrite($this->filehandle, $data); |
281 | |
282 | /* Unlock file */ |
283 | $this->unlock(); |
284 | |
285 | /* Test write result and exit if OK */ |
286 | if($r > 0) return true; |
287 | |
288 | /* Fail */ |
289 | $this->set_error(_("Write to addressbook failed")); |
290 | return false; |
291 | } |
292 | |
293 | /* Delete address */ |
294 | function remove($alias) { |
295 | if(!$this->writeable) { |
296 | return $this->set_error(_("Addressbook is read-only")); |
297 | } |
298 | |
299 | /* Lock the file to make sure we're the only process working |
300 | * on it. */ |
301 | if(!$this->lock()) { |
302 | return $this->set_error(_("Could not lock datafile")); |
303 | } |
304 | |
305 | /* Read file into memory, ignoring nicknames to delete */ |
306 | @rewind($this->filehandle); |
307 | $i = 0; |
308 | $rows = array(); |
309 | while($row = @fgetcsv($this->filehandle, 2048, '|')) { |
310 | if(!in_array($row[0], $alias)) { |
311 | $rows[$i++] = $row; |
312 | } |
313 | } |
314 | |
315 | /* Write data back */ |
316 | if(!$this->overwrite($rows)) { |
317 | $this->unlock(); |
318 | return false; |
319 | } |
320 | |
321 | $this->unlock(); |
322 | return true; |
323 | } |
324 | |
325 | /* Modify address */ |
326 | function modify($alias, $userdata) { |
327 | if(!$this->writeable) { |
328 | return $this->set_error(_("Addressbook is read-only")); |
329 | } |
330 | |
331 | /* See if user exists */ |
332 | $ret = $this->lookup($alias); |
333 | if(empty($ret)) { |
334 | return $this->set_error(sprintf(_("User '%s' does not exist"), |
335 | $alias)); |
336 | } |
337 | |
338 | /* Lock the file to make sure we're the only process working |
339 | * on it. */ |
340 | if(!$this->lock()) { |
341 | return $this->set_error(_("Could not lock datafile")); |
342 | } |
343 | |
344 | /* Read file into memory, modifying the data for the |
345 | * user identified by $alias */ |
346 | $this->open(true); |
347 | @rewind($this->filehandle); |
348 | $i = 0; |
349 | $rows = array(); |
350 | while($row = @fgetcsv($this->filehandle, 2048, '|')) { |
351 | if(strtolower($row[0]) != strtolower($alias)) { |
352 | $rows[$i++] = $row; |
353 | } else { |
354 | $rows[$i++] = array(0 => $userdata['nickname'], |
355 | 1 => $userdata['firstname'], |
356 | 2 => $userdata['lastname'], |
357 | 3 => $userdata['email'], |
358 | 4 => $userdata['label']); |
359 | } |
360 | } |
361 | |
362 | /* Write data back */ |
363 | if(!$this->overwrite($rows)) { |
364 | $this->unlock(); |
365 | return false; |
366 | } |
367 | |
368 | $this->unlock(); |
369 | return true; |
370 | } |
d50a8c48 |
371 | |
77ec28e9 |
372 | /* Function for quoting values before saving */ |
373 | function quotevalue($value) { |
374 | /* Quote the field if it contains | or ". Double quotes need to |
375 | * be replaced with "" */ |
376 | if(ereg("[|\"]", $value)) { |
377 | $value = '"' . str_replace('"', '""', $value) . '"'; |
378 | } |
379 | return $value; |
380 | } |
381 | |
06b4facd |
382 | } /* End of class abook_local_file */ |
45df3062 |
383 | ?> |