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