6 * Copyright (c) 1999-2001 The Squirrelmail Development 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
44 function open($new = false) {
47 // Return true is file is open and $new is unset
48 if($this->filehandle
&& !$new)
51 // Check that new file exists
52 if (! file_exists($this->global_filename
) ||
53 ! is_readable($this->global_filename
))
54 return $this->set_error($this->global_filename
. ': ' .
55 _("No such file or directory"));
57 // Close old file, if any
58 if ($this->filehandle
) $this->close();
60 // Open file, read only.
61 $fh = @fopen
($this->global_filename
, 'r');
62 $this->writeable
= false;
64 return $this->set_error($this->global_filename
. ': ' .
67 $this->filehandle
= &$fh;
71 // Close the file and forget the filehandle
73 @fclose
($this->filehandle
);
74 $this->filehandle
= 0;
75 $this->global_filename
= '';
76 $this->writable
= false;
79 // ========================== Public ========================
82 function search($expr) {
84 // To be replaced by advanded search expression parsing
85 if(is_array($expr)) return;
87 // Make regexp from glob'ed expression
88 // May want to quote other special characters like (, ), -, [, ], etc.
89 $expr = str_replace('?', '.', $expr);
90 $expr = str_replace('*', '.*', $expr);
96 @rewind
($this->filehandle
);
98 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
99 $line = join(' ', $row);
100 if (eregi($expr, $line)) {
101 $res[] = array('nickname' => $row[0],
102 'name' => $row[1] . ' ' . $row[2],
103 'firstname' => $row[1],
104 'lastname' => $row[2],
107 'backend' => $this->bnum
,
108 'source' => &$this->sname
);
116 function lookup($alias) {
120 $alias = strtolower($alias);
123 @rewind
($this->filehandle
);
125 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
126 if (strtolower($row[0]) == $alias) {
127 return array('nickname' => $row[0],
128 'name' => $row[1] . ' ' . $row[2],
129 'firstname' => $row[1],
130 'lastname' => $row[2],
133 'backend' => $this->bnum
,
134 'source' => &$this->sname
);
141 // List all addresses
142 function list_addr() {
145 @rewind
($this->filehandle
);
147 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
148 $res[] = array('nickname' => $row[0],
149 'name' => $row[1] . ' ' . $row[2],
150 'firstname' => $row[1],
151 'lastname' => $row[2],
154 'backend' => $this->bnum
,
155 'source' => &$this->sname
);
161 function add($userdata) {
162 $this->set_error(_("Can not modify global address book"));
167 function remove($alias) {
168 $this->set_error(_("Can not modify global address book"));
173 function modify($alias, $userdata) {
174 $this->set_error(_("Can not modify global address book"));
178 } // End of class abook_local_file