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