01589f26 |
1 | <?php |
2 | |
2ba13803 |
3 | /** |
7350889b |
4 | * abook_local_file.php |
5 | * |
6 | * Copyright (c) 1999-2001 The Squirrelmail Development 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 | */ |
01589f26 |
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 | // 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")); |
56 | |
57 | // Close old file, if any |
58 | if ($this->filehandle) $this->close(); |
59 | |
60 | // Open file, read only. |
61 | $fh = @fopen($this->global_filename, 'r'); |
62 | $this->writeable = false; |
63 | if(! $fh) |
64 | return $this->set_error($this->global_filename . ': ' . |
65 | _("Open failed")); |
66 | |
67 | $this->filehandle = &$fh; |
68 | return true; |
69 | } |
70 | |
71 | // Close the file and forget the filehandle |
72 | function close() { |
73 | @fclose($this->filehandle); |
74 | $this->filehandle = 0; |
75 | $this->global_filename = ''; |
76 | $this->writable = false; |
77 | } |
78 | |
79 | // ========================== Public ======================== |
80 | |
81 | // Search the file |
82 | function search($expr) { |
83 | |
84 | // To be replaced by advanded search expression parsing |
85 | if(is_array($expr)) return; |
86 | |
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); |
91 | |
92 | $res = array(); |
93 | if(!$this->open()) |
94 | return false; |
95 | |
96 | @rewind($this->filehandle); |
97 | |
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], |
105 | 'email' => $row[3], |
106 | 'label' => $row[4], |
107 | 'backend' => $this->bnum, |
108 | 'source' => &$this->sname); |
109 | } |
110 | } |
111 | |
112 | return $res; |
113 | } |
114 | |
115 | // Lookup alias |
116 | function lookup($alias) { |
117 | if (empty($alias)) |
118 | return array(); |
119 | |
120 | $alias = strtolower($alias); |
121 | |
122 | $this->open(); |
123 | @rewind($this->filehandle); |
124 | |
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], |
131 | 'email' => $row[3], |
132 | 'label' => $row[4], |
133 | 'backend' => $this->bnum, |
134 | 'source' => &$this->sname); |
135 | } |
136 | } |
137 | |
138 | return array(); |
139 | } |
140 | |
141 | // List all addresses |
142 | function list_addr() { |
143 | $res = array(); |
144 | $this->open(); |
145 | @rewind($this->filehandle); |
146 | |
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], |
152 | 'email' => $row[3], |
153 | 'label' => $row[4], |
154 | 'backend' => $this->bnum, |
155 | 'source' => &$this->sname); |
156 | } |
157 | return $res; |
158 | } |
159 | |
160 | // Add address |
161 | function add($userdata) { |
162 | $this->set_error(_("Can not modify global address book")); |
163 | return false; |
164 | } |
165 | |
166 | // Delete address |
167 | function remove($alias) { |
168 | $this->set_error(_("Can not modify global address book")); |
169 | return false; |
170 | } |
171 | |
172 | // Modify address |
173 | function modify($alias, $userdata) { |
174 | $this->set_error(_("Can not modify global address book")); |
175 | return false; |
176 | } |
177 | |
178 | } // End of class abook_local_file |
179 | ?> |