6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Backend for addressbook as a pipe separated file
11 * An array with the following elements must be passed to
12 * the class constructor (elements marked ? are optional):
14 * NOTE. This class should not be used directly. Use the
15 * "AddressBook" class instead.
17 * Make sure you configure this before using it!
22 class abook_global_file
extends addressbook_backend
{
24 var $bname = 'global_file';
28 /* ========================== Private ======================= */
31 function abook_global_file() {
32 global $address_book_global_filename;
33 $this->global_filename
= $address_book_global_filename;
35 $this->sname
= _("Global address book");
40 /* Open the addressbook file and store the file pointer.
41 * Use $file as the file to open, or the class' own
42 * filename property. If $param is empty and file is
43 * open, do nothing. */
44 function open($new = false) {
47 /* Return true is file is open and $new is unset */
48 if($this->filehandle
&& !$new) {
52 /* Check that new file exists */
53 if (! file_exists($this->global_filename
) ||
54 ! is_readable($this->global_filename
)) {
55 return $this->set_error($this->global_filename
. ': ' .
56 _("No such file or directory"));
59 /* Close old file, if any */
60 if ($this->filehandle
) {
64 /* Open file, read only. */
65 $fh = @fopen
($this->global_filename
, 'r');
66 $this->writeable
= false;
68 return $this->set_error($this->global_filename
. ': ' .
72 $this->filehandle
= &$fh;
76 /* Close the file and forget the filehandle */
78 @fclose
($this->filehandle
);
79 $this->filehandle
= 0;
80 $this->global_filename
= '';
81 $this->writable
= false;
84 /* ========================== Public ======================== */
87 function search($expr) {
89 /* To be replaced by advanded search expression parsing */
94 /* Make regexp from glob'ed expression
95 * May want to quote other special characters like (, ), -, [, ], etc. */
96 $expr = str_replace('?', '.', $expr);
97 $expr = str_replace('*', '.*', $expr);
104 @rewind
($this->filehandle
);
106 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
107 $line = join(' ', $row);
108 if (eregi($expr, $line)) {
109 $res[] = array('nickname' => $row[0],
110 'name' => $row[1] . ' ' . $row[2],
111 'firstname' => $row[1],
112 'lastname' => $row[2],
115 'backend' => $this->bnum
,
116 'source' => &$this->sname
);
124 function lookup($alias) {
129 $alias = strtolower($alias);
132 @rewind
($this->filehandle
);
134 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
135 if (strtolower($row[0]) == $alias) {
136 return array('nickname' => $row[0],
137 'name' => $row[1] . ' ' . $row[2],
138 'firstname' => $row[1],
139 'lastname' => $row[2],
142 'backend' => $this->bnum
,
143 'source' => &$this->sname
);
150 /* List all addresses */
151 function list_addr() {
154 @rewind
($this->filehandle
);
156 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
157 $res[] = array('nickname' => $row[0],
158 'name' => $row[1] . ' ' . $row[2],
159 'firstname' => $row[1],
160 'lastname' => $row[2],
163 'backend' => $this->bnum
,
164 'source' => &$this->sname
);
170 function add($userdata) {
171 $this->set_error(_("Can not modify global address book"));
176 function remove($alias) {
177 $this->set_error(_("Can not modify global address book"));
182 function modify($alias, $userdata) {
183 $this->set_error(_("Can not modify global address book"));
187 } /* End of class abook_local_file */