738be3ffa7d0d11ecd209a5fd39d65569fc7c9e7
[squirrelmail.git] / functions / addressbook.php
1 <?php
2
3 /**
4 ** addressbook.php
5 **
6 ** Functions and classes for the addressbook system.
7 **
8 **/
9
10 $addressbook_php = true;
11
12 // Include backends here.
13 include("../functions/abook_local_file.php");
14 include("../functions/abook_ldap_server.php");
15
16 // Create and initialize an addressbook object.
17 // Returns the created object
18 function addressbook_init($showerr = true, $onlylocal = false) {
19 global $data_dir, $username, $ldap_server;
20
21 // Create a new addressbook object
22 $abook = new AddressBook;
23
24 // Always add a local backend
25 $filename = sprintf("%s%s.abook", $data_dir, $username);
26 $r = $abook->add_backend("local_file", Array("filename" => $filename,
27 "create" => true));
28 if(!$r && $showerr) {
29 printf(_("Error opening file %s"), $filename);
30 exit;
31 }
32
33 if($onlylocal)
34 return $abook;
35
36 // Load configured LDAP servers
37 reset($ldap_server);
38 while(list($key,$param) = each($ldap_server)) {
39 if(is_array($param)) {
40 $r = $abook->add_backend("ldap_server", $param);
41 if(!$r && $showerr) {
42 printf("&nbsp;"._("Error initializing LDAP server %s:")."<BR>\n",
43 $param["host"]);
44 printf("&nbsp;".$abook->error);
45 exit;
46 }
47 }
48 }
49
50 // Return the initialized object
51 return $abook;
52 }
53
54
55
56 /**
57 ** This is the main address book class that connect all the
58 ** backends and provide services to the functions above.
59 **
60 **/
61 class AddressBook {
62 var $backends = array();
63 var $numbackends = 0;
64 var $error = "";
65
66 // Constructor function.
67 function AddressBook() {
68 }
69
70 // Return an array of backends of a given type,
71 // or all backends if no type is specified.
72 function get_backend_list($type = "") {
73 $ret = array();
74 for($i = 1 ; $i <= $this->numbackends ; $i++) {
75 if(empty($type) || $type == $this->backends[$i]->btype) {
76 array_push($ret, &$this->backends[$i]);
77 }
78 }
79 return $ret;
80 }
81
82
83 // ========================== Public ========================
84
85 // Add a new backend. $backend is the name of a backend
86 // (without the abook_ prefix), and $param is an optional
87 // mixed variable that is passed to the backend constructor.
88 // See each of the backend classes for valid parameters.
89 function add_backend($backend, $param = "") {
90 $backend_name = "abook_".$backend;
91 eval("\$newback = new $backend_name(\$param);");
92 if(!empty($newback->error)) {
93 $this->error = $newback->error;
94 return false;
95 }
96
97 $this->numbackends++;
98
99 $newback->bnum = $this->numbackends;
100 $this->backends[$this->numbackends] = $newback;
101 return $this->numbackends;
102 }
103
104
105 // Return a list of addresses matching expression in
106 // all backends of a given type.
107 function search($expression, $btype = "") {
108 $ret = array();
109 $this->error = "";
110
111 $sel = $this->get_backend_list($btype);
112 $failed = 0;
113 for($i = 0 ; $i < sizeof($sel) ; $i++) {
114 $backend = &$sel[$i];
115 $backend->error = "";
116 $res = $backend->search($expression);
117 if(is_array($res)) {
118 $ret = array_merge($ret, $res);
119 } else {
120 $this->error = $this->error . "<br>\n". $backend->error;
121 $failed++;
122 }
123 }
124
125 // Only fail if all backends failed
126 if($failed >= sizeof($sel))
127 return false;
128
129 return $ret;
130 }
131
132
133 // Return a sorted search
134 function s_search($expression, $btype = "") {
135
136 $ret = $this->search($expression, $btype);
137 if(!is_array($ret))
138 return $ret;
139
140 // Inline function - Not nice, but still..
141 function cmp($a,$b) {
142 if($a["backend"] > $b["backend"])
143 return 1;
144 else if($a["backend"] < $b["backend"])
145 return -1;
146
147 return (strtolower($a["name"]) > strtolower($b["name"])) ? 1 : -1;
148 }
149
150 usort($ret, 'cmp');
151 return $ret;
152 }
153
154
155 // Lookup an address by alias. Only possible in
156 // local backends.
157 function lookup($alias) {
158 $ret = array();
159
160 $sel = $this->get_backend_list("local");
161 for($i = 0 ; $i < sizeof($sel) ; $i++) {
162 $backend = &$sel[$i];
163 $backend->error = "";
164 $res = $backend->lookup($alias);
165 if(is_array($res)) {
166 return $res;
167 } else {
168 $this->error = $backend->error;
169 return false;
170 }
171 }
172
173 return $ret;
174 }
175
176
177 // Return all addresses
178 function list_addr() {
179 $ret = array();
180
181 $sel = $this->get_backend_list("local");
182 for($i = 0 ; $i < sizeof($sel) ; $i++) {
183 $backend = &$sel[$i];
184 $backend->error = "";
185 $res = $backend->list_addr();
186 if(is_array($res)) {
187 $ret = array_merge($ret, $res);
188 } else {
189 $this->error = $backend->error;
190 return false;
191 }
192 }
193
194 return $ret;
195 }
196
197
198 // Create a new address from $userdata, in backend $bnum.
199 // Return the backend number that the/ address was added
200 // to, or false if it failed.
201 function add($userdata, $bnum) {
202
203 // Validate data
204 if(!is_array($userdata)) {
205 $this->error = _("Invalid input data");
206 return false;
207 }
208 if(empty($userdata["fullname"]) &&
209 empty($userdata["lastname"])) {
210 $this->error = _("Name is missing");
211 return false;
212 }
213 if(empty($userdata["email"])) {
214 $this->error = _("E-mail address is missing");
215 return false;
216 }
217 if(empty($userdata["nickname"])) {
218 $userdata["nickname"] = $userdata["email"];
219 }
220
221 // Check that specified backend accept new entries
222 if(!$this->backends[$bnum]->writeable) {
223 $this->error = _("Addressbook is read-only");
224 return false;
225 }
226
227 // Add address to backend
228 $res = $this->backends[$bnum]->add($userdata);
229 if($res) {
230 return $bnum;
231 } else {
232 $this->error = $this->backends[$bnum]->error;
233 return false;
234 }
235
236 return false; // Not reached
237 }
238
239 }
240
241
242 /**
243 ** Generic backend that all other backends extend
244 **/
245 class addressbook_backend {
246
247 // Variables that all backends must provide.
248 var $btype = "dummy";
249 var $bname = "dummy";
250 var $sname = "Dummy backend";
251
252 // Variables common for all backends, but that
253 // should not be changed by the backends.
254 var $bnum = -1;
255 var $error = "";
256 var $writeable = false;
257
258 function set_error($string) {
259 $this->error = "[" . $this->sname . "] " . $string;
260 return false;
261 }
262
263
264 // ========================== Public ========================
265
266 function search($expression) {
267 $this->set_error("search not implemented");
268 return false;
269 }
270
271 function lookup($alias) {
272 $this->set_error("lookup not implemented");
273 return false;
274 }
275
276 function list_addr() {
277 $this->set_error("list_addr not implemented");
278 return false;
279 }
280
281 function add($userdata) {
282 $this->set_error("add not implemented");
283 return false;
284 }
285
286 }
287
288 ?>