b9152b527ca275c6e8b34d7d04bcc75aa09b7986
[squirrelmail.git] / functions / abook_global_file.php
1 <?php
2
3 /**
4 * abook_local_file.php
5 *
6 * Copyright (c) 1999-2002 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 * $Id$
20 */
21
22 class abook_global_file extends addressbook_backend {
23 var $btype = 'local';
24 var $bname = 'global_file';
25
26 var $filehandle = 0;
27
28 /* ========================== Private ======================= */
29
30 /* Constructor */
31 function abook_global_file() {
32 global $address_book_global_filename;
33 $this->global_filename = $address_book_global_filename;
34
35 $this->sname = _("Global address book");
36
37 $this->open(true);
38 }
39
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) {
45 $this->error = '';
46
47 /* Return true is file is open and $new is unset */
48 if($this->filehandle && !$new) {
49 return true;
50 }
51
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"));
57 }
58
59 /* Close old file, if any */
60 if ($this->filehandle) {
61 $this->close();
62 }
63
64 /* Open file, read only. */
65 $fh = @fopen($this->global_filename, 'r');
66 $this->writeable = false;
67 if(! $fh) {
68 return $this->set_error($this->global_filename . ': ' .
69 _("Open failed"));
70 }
71
72 $this->filehandle = &$fh;
73 return true;
74 }
75
76 /* Close the file and forget the filehandle */
77 function close() {
78 @fclose($this->filehandle);
79 $this->filehandle = 0;
80 $this->global_filename = '';
81 $this->writable = false;
82 }
83
84 /* ========================== Public ======================== */
85
86 /* Search the file */
87 function search($expr) {
88
89 /* To be replaced by advanded search expression parsing */
90 if(is_array($expr)) {
91 return;
92 }
93
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);
98
99 $res = array();
100 if(!$this->open()) {
101 return false;
102 }
103
104 @rewind($this->filehandle);
105
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],
113 'email' => $row[3],
114 'label' => $row[4],
115 'backend' => $this->bnum,
116 'source' => &$this->sname);
117 }
118 }
119
120 return $res;
121 }
122
123 /* Lookup alias */
124 function lookup($alias) {
125 if (empty($alias)) {
126 return array();
127 }
128
129 $alias = strtolower($alias);
130
131 $this->open();
132 @rewind($this->filehandle);
133
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],
140 'email' => $row[3],
141 'label' => $row[4],
142 'backend' => $this->bnum,
143 'source' => &$this->sname);
144 }
145 }
146
147 return array();
148 }
149
150 /* List all addresses */
151 function list_addr() {
152 $res = array();
153 $this->open();
154 @rewind($this->filehandle);
155
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],
161 'email' => $row[3],
162 'label' => $row[4],
163 'backend' => $this->bnum,
164 'source' => &$this->sname);
165 }
166 return $res;
167 }
168
169 /* Add address */
170 function add($userdata) {
171 $this->set_error(_("Can not modify global address book"));
172 return false;
173 }
174
175 /* Delete address */
176 function remove($alias) {
177 $this->set_error(_("Can not modify global address book"));
178 return false;
179 }
180
181 /* Modify address */
182 function modify($alias, $userdata) {
183 $this->set_error(_("Can not modify global address book"));
184 return false;
185 }
186
187 } /* End of class abook_local_file */
188 ?>