ef1f30a54607bc52c11e7e677424e6a31c72a248
[squirrelmail.git] / functions / abook_global_file.php
1 <?php
2
3 /**
4 * abook_global_file.php
5 *
6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
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 * NOTE. This class should not be used directly. Use the
15 * "AddressBook" class instead.
16 *
17 * Make sure you configure this before using it!
18 *
19 * @version $Id$
20 * @package squirrelmail
21 * @subpackage addressbook
22 */
23
24 /**
25 * Undocumented class - fixme
26 * @package squirrelmail
27 */
28 class abook_global_file extends addressbook_backend {
29 var $btype = 'local';
30 var $bname = 'global_file';
31
32 var $filehandle = 0;
33
34 /* ========================== Private ======================= */
35
36 /* Constructor */
37 function abook_global_file() {
38 global $address_book_global_filename;
39 $this->global_filename = $address_book_global_filename;
40
41 $this->sname = _("Global address book");
42
43 $this->open(true);
44 }
45
46 /* Open the addressbook file and store the file pointer.
47 * Use $file as the file to open, or the class' own
48 * filename property. If $param is empty and file is
49 * open, do nothing. */
50 function open($new = false) {
51 $this->error = '';
52
53 /* Return true is file is open and $new is unset */
54 if($this->filehandle && !$new) {
55 return true;
56 }
57
58 /* Check that new file exists */
59 if (! file_exists($this->global_filename) ||
60 ! is_readable($this->global_filename)) {
61 return $this->set_error($this->global_filename . ': ' .
62 _("No such file or directory"));
63 }
64
65 /* Close old file, if any */
66 if ($this->filehandle) {
67 $this->close();
68 }
69
70 /* Open file, read only. */
71 $fh = @fopen($this->global_filename, 'r');
72 $this->writeable = false;
73 if(! $fh) {
74 return $this->set_error($this->global_filename . ': ' .
75 _("Open failed"));
76 }
77
78 $this->filehandle = &$fh;
79 return true;
80 }
81
82 /* Close the file and forget the filehandle */
83 function close() {
84 @fclose($this->filehandle);
85 $this->filehandle = 0;
86 $this->global_filename = '';
87 $this->writable = false;
88 }
89
90 /* ========================== Public ======================== */
91
92 /* Search the file */
93 function search($expr) {
94
95 /* To be replaced by advanded search expression parsing */
96 if(is_array($expr)) {
97 return;
98 }
99
100 /* Make regexp from glob'ed expression
101 * May want to quote other special characters like (, ), -, [, ], etc. */
102 $expr = str_replace('?', '.', $expr);
103 $expr = str_replace('*', '.*', $expr);
104
105 $res = array();
106 if(!$this->open()) {
107 return false;
108 }
109
110 @rewind($this->filehandle);
111
112 while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
113 $line = join(' ', $row);
114 if (eregi($expr, $line)) {
115 $res[] = array('nickname' => $row[0],
116 'name' => $row[1] . ' ' . $row[2],
117 'firstname' => $row[1],
118 'lastname' => $row[2],
119 'email' => $row[3],
120 'label' => $row[4],
121 'backend' => $this->bnum,
122 'source' => &$this->sname);
123 }
124 }
125
126 return $res;
127 }
128
129 /* Lookup alias */
130 function lookup($alias) {
131 if (empty($alias)) {
132 return array();
133 }
134
135 $alias = strtolower($alias);
136
137 $this->open();
138 @rewind($this->filehandle);
139
140 while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
141 if (strtolower($row[0]) == $alias) {
142 return array('nickname' => $row[0],
143 'name' => $row[1] . ' ' . $row[2],
144 'firstname' => $row[1],
145 'lastname' => $row[2],
146 'email' => $row[3],
147 'label' => $row[4],
148 'backend' => $this->bnum,
149 'source' => &$this->sname);
150 }
151 }
152
153 return array();
154 }
155
156 /* List all addresses */
157 function list_addr() {
158 $res = array();
159 $this->open();
160 @rewind($this->filehandle);
161
162 while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
163 $res[] = array('nickname' => $row[0],
164 'name' => $row[1] . ' ' . $row[2],
165 'firstname' => $row[1],
166 'lastname' => $row[2],
167 'email' => $row[3],
168 'label' => $row[4],
169 'backend' => $this->bnum,
170 'source' => &$this->sname);
171 }
172 return $res;
173 }
174
175 /* Add address */
176 function add($userdata) {
177 $this->set_error(_("Can not modify global address book"));
178 return false;
179 }
180
181 /* Delete address */
182 function remove($alias) {
183 $this->set_error(_("Can not modify global address book"));
184 return false;
185 }
186
187 /* Modify address */
188 function modify($alias, $userdata) {
189 $this->set_error(_("Can not modify global address book"));
190 return false;
191 }
192
193 } /* End of class abook_local_file */
194 ?>