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