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