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) { |
137 | $newfh = @fopen($this->filename, 'w'); |
138 | if(!$newfh) { |
139 | return $this->set_error("$file: " . _("Open failed")); |
140 | } |
141 | |
142 | for($i = 0 ; $i < sizeof($rows) ; $i++) { |
143 | if(is_array($rows[$i])) { |
144 | fwrite($newfh, join('|', $rows[$i]) . "\n"); |
145 | } |
146 | } |
147 | |
148 | fclose($newfh); |
149 | $this->unlock(); |
150 | $this->open(true); |
151 | return true; |
152 | } |
153 | |
154 | /* ========================== Public ======================== */ |
155 | |
156 | /* Search the file */ |
157 | function search($expr) { |
158 | |
159 | /* To be replaced by advanded search expression parsing */ |
160 | if(is_array($expr)) { return; } |
161 | |
162 | /* Make regexp from glob'ed expression |
163 | * May want to quote other special characters like (, ), -, [, ], etc. */ |
164 | $expr = str_replace('?', '.', $expr); |
165 | $expr = str_replace('*', '.*', $expr); |
166 | |
167 | $res = array(); |
168 | if(!$this->open()) { |
169 | return false; |
170 | } |
171 | @rewind($this->filehandle); |
172 | |
173 | while ($row = @fgetcsv($this->filehandle, 2048, '|')) { |
174 | $line = join(' ', $row); |
175 | if(eregi($expr, $line)) { |
176 | array_push($res, array('nickname' => $row[0], |
177 | 'name' => $row[1] . ' ' . $row[2], |
178 | 'firstname' => $row[1], |
179 | 'lastname' => $row[2], |
180 | 'email' => $row[3], |
181 | 'label' => $row[4], |
182 | 'backend' => $this->bnum, |
183 | 'source' => &$this->sname)); |
184 | } |
185 | } |
186 | |
187 | return $res; |
188 | } |
189 | |
190 | /* Lookup alias */ |
191 | function lookup($alias) { |
192 | if(empty($alias)) { |
193 | return array(); |
194 | } |
195 | |
196 | $alias = strtolower($alias); |
197 | |
198 | $this->open(); |
199 | @rewind($this->filehandle); |
200 | |
201 | while ($row = @fgetcsv($this->filehandle, 2048, '|')) { |
202 | if(strtolower($row[0]) == $alias) { |
203 | return array('nickname' => $row[0], |
204 | 'name' => $row[1] . ' ' . $row[2], |
205 | 'firstname' => $row[1], |
206 | 'lastname' => $row[2], |
207 | 'email' => $row[3], |
208 | 'label' => $row[4], |
209 | 'backend' => $this->bnum, |
210 | 'source' => &$this->sname); |
211 | } |
212 | } |
213 | |
214 | return array(); |
215 | } |
216 | |
217 | /* List all addresses */ |
218 | function list_addr() { |
219 | $res = array(); |
220 | $this->open(); |
221 | @rewind($this->filehandle); |
222 | |
223 | while ($row = @fgetcsv($this->filehandle, 2048, '|')) { |
224 | array_push($res, array('nickname' => $row[0], |
225 | 'name' => $row[1] . ' ' . $row[2], |
226 | 'firstname' => $row[1], |
227 | 'lastname' => $row[2], |
228 | 'email' => $row[3], |
229 | 'label' => $row[4], |
230 | 'backend' => $this->bnum, |
231 | 'source' => &$this->sname)); |
232 | } |
233 | return $res; |
234 | } |
235 | |
236 | /* Add address */ |
237 | function add($userdata) { |
238 | if(!$this->writeable) { |
239 | return $this->set_error(_("Addressbook is read-only")); |
240 | } |
241 | /* See if user exists already */ |
242 | $ret = $this->lookup($userdata['nickname']); |
243 | if(!empty($ret)) { |
244 | return $this->set_error(sprintf(_("User '%s' already exist"), |
245 | $ret['nickname'])); |
246 | } |
247 | |
248 | /* Here is the data to write */ |
249 | $data = $userdata['nickname'] . '|' . $userdata['firstname'] . '|' . |
250 | $userdata['lastname'] . '|' . $userdata['email'] . '|' . |
251 | $userdata['label']; |
252 | /* Strip linefeeds */ |
253 | $data = ereg_replace("[\r\n]", ' ', $data); |
254 | /* Add linefeed at end */ |
255 | $data = $data . "\n"; |
256 | |
257 | /* Reopen file, just to be sure */ |
258 | $this->open(true); |
259 | if(!$this->writeable) { |
260 | return $this->set_error(_("Addressbook is read-only")); |
261 | } |
262 | |
263 | /* Lock the file */ |
264 | if(!$this->lock()) { |
265 | return $this->set_error(_("Could not lock datafile")); |
266 | } |
267 | |
268 | /* Write */ |
269 | $r = fwrite($this->filehandle, $data); |
270 | |
271 | /* Unlock file */ |
272 | $this->unlock(); |
273 | |
274 | /* Test write result and exit if OK */ |
275 | if($r > 0) return true; |
276 | |
277 | /* Fail */ |
278 | $this->set_error(_("Write to addressbook failed")); |
279 | return false; |
280 | } |
281 | |
282 | /* Delete address */ |
283 | function remove($alias) { |
284 | if(!$this->writeable) { |
285 | return $this->set_error(_("Addressbook is read-only")); |
286 | } |
287 | |
288 | /* Lock the file to make sure we're the only process working |
289 | * on it. */ |
290 | if(!$this->lock()) { |
291 | return $this->set_error(_("Could not lock datafile")); |
292 | } |
293 | |
294 | /* Read file into memory, ignoring nicknames to delete */ |
295 | @rewind($this->filehandle); |
296 | $i = 0; |
297 | $rows = array(); |
298 | while($row = @fgetcsv($this->filehandle, 2048, '|')) { |
299 | if(!in_array($row[0], $alias)) { |
300 | $rows[$i++] = $row; |
301 | } |
302 | } |
303 | |
304 | /* Write data back */ |
305 | if(!$this->overwrite($rows)) { |
306 | $this->unlock(); |
307 | return false; |
308 | } |
309 | |
310 | $this->unlock(); |
311 | return true; |
312 | } |
313 | |
314 | /* Modify address */ |
315 | function modify($alias, $userdata) { |
316 | if(!$this->writeable) { |
317 | return $this->set_error(_("Addressbook is read-only")); |
318 | } |
319 | |
320 | /* See if user exists */ |
321 | $ret = $this->lookup($alias); |
322 | if(empty($ret)) { |
323 | return $this->set_error(sprintf(_("User '%s' does not exist"), |
324 | $alias)); |
325 | } |
326 | |
327 | /* Lock the file to make sure we're the only process working |
328 | * on it. */ |
329 | if(!$this->lock()) { |
330 | return $this->set_error(_("Could not lock datafile")); |
331 | } |
332 | |
333 | /* Read file into memory, modifying the data for the |
334 | * user identified by $alias */ |
335 | $this->open(true); |
336 | @rewind($this->filehandle); |
337 | $i = 0; |
338 | $rows = array(); |
339 | while($row = @fgetcsv($this->filehandle, 2048, '|')) { |
340 | if(strtolower($row[0]) != strtolower($alias)) { |
341 | $rows[$i++] = $row; |
342 | } else { |
343 | $rows[$i++] = array(0 => $userdata['nickname'], |
344 | 1 => $userdata['firstname'], |
345 | 2 => $userdata['lastname'], |
346 | 3 => $userdata['email'], |
347 | 4 => $userdata['label']); |
348 | } |
349 | } |
350 | |
351 | /* Write data back */ |
352 | if(!$this->overwrite($rows)) { |
353 | $this->unlock(); |
354 | return false; |
355 | } |
356 | |
357 | $this->unlock(); |
358 | return true; |
359 | } |
d50a8c48 |
360 | |
06b4facd |
361 | } /* End of class abook_local_file */ |
362 | ?> |