abdfb4d0 |
1 | <?php |
2 | /** |
3 | ** addressbook.php |
4 | ** |
ef870322 |
5 | ** Copyright (c) 1999-2000 The SquirrelMail development team |
6 | ** Licensed under the GNU GPL. For full terms see the file COPYING. |
7 | ** |
abdfb4d0 |
8 | ** Manage personal address book. |
9 | ** |
245a6892 |
10 | ** $Id$ |
abdfb4d0 |
11 | **/ |
12 | |
f740c049 |
13 | include('../src/validate.php'); |
14 | include('../functions/strings.php'); |
15 | include('../config/config.php'); |
16 | include('../functions/array.php'); |
17 | include('../functions/page_header.php'); |
18 | include('../functions/display_messages.php'); |
19 | include('../functions/addressbook.php'); |
abdfb4d0 |
20 | |
21 | // Sort array by the key "name" |
22 | function alistcmp($a,$b) { |
c9e9b23c |
23 | if($a['backend'] > $b['backend']) |
a82f283d |
24 | return 1; |
c9e9b23c |
25 | else if($a['backend'] < $b['backend']) |
a82f283d |
26 | return -1; |
27 | |
c9e9b23c |
28 | return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1; |
abdfb4d0 |
29 | } |
30 | |
31 | // Output form to add and modify address data |
32 | function address_form($name, $submittext, $values = array()) { |
33 | global $color; |
34 | print "<TABLE BORDER=0 CELLPADDING=1 COLS=2 WIDTH=\"90%\" ALIGN=center>\n"; |
35 | printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>", |
36 | _("Nickname")); |
37 | printf("<TD BGCOLOR=\"%s\" ALIGN=left>". |
38 | "<INPUT NAME=\"%s[nickname]\" SIZE=15 VALUE=\"%s\">". |
39 | " <SMALL>%s</SMALL></TD></TR>\n", |
245a6892 |
40 | $color[4], $name, |
c9e9b23c |
41 | (isset($values['nickname']))? |
42 | htmlspecialchars($values['nickname']):"", |
abdfb4d0 |
43 | _("Must be unique")); |
44 | printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>", |
45 | _("E-mail address")); |
46 | printf("<TD BGCOLOR=\"%s\" ALIGN=left>". |
47 | "<INPUT NAME=\"%s[email]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n", |
245a6892 |
48 | $color[4], $name, |
49 | (isset($values["email"]))? |
50 | htmlspecialchars($values["email"]):""); |
abdfb4d0 |
51 | printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>", |
52 | _("First name")); |
53 | printf("<TD BGCOLOR=\"%s\" ALIGN=left>". |
54 | "<INPUT NAME=\"%s[firstname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n", |
245a6892 |
55 | $color[4], $name, |
56 | (isset($values["firstname"]))? |
57 | htmlspecialchars($values["firstname"]):""); |
abdfb4d0 |
58 | printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>", |
59 | _("Last name")); |
60 | printf("<TD BGCOLOR=\"%s\" ALIGN=left>". |
61 | "<INPUT NAME=\"%s[lastname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n", |
245a6892 |
62 | $color[4], $name, |
63 | (isset($values["lastname"]))? |
64 | htmlspecialchars($values["lastname"]):""); |
abdfb4d0 |
65 | printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>", |
66 | _("Additional info")); |
67 | printf("<TD BGCOLOR=\"%s\" ALIGN=left>". |
68 | "<INPUT NAME=\"%s[label]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n", |
245a6892 |
69 | $color[4], $name, |
70 | (isset($values["label"]))? |
71 | htmlspecialchars($values["label"]):""); |
abdfb4d0 |
72 | |
73 | printf("<TR><TD COLSPAN=2 BGCOLOR=\"%s\" ALIGN=center>\n". |
74 | "<INPUT TYPE=submit NAME=\"%s[SUBMIT]\" VALUE=\"%s\"></TD></TR>\n", |
75 | $color[4], $name, $submittext); |
76 | |
77 | print "</TABLE>\n"; |
78 | } |
79 | |
80 | |
c9e9b23c |
81 | include('../src/load_prefs.php'); |
abdfb4d0 |
82 | |
83 | // Open addressbook, with error messages on but without LDAP (the |
84 | // second "true"). Don't need LDAP here anyway |
85 | $abook = addressbook_init(true, true); |
86 | if($abook->localbackend == 0) { |
87 | plain_error_message(_("No personal address book is defined. Contact administrator."), $color); |
88 | exit(); |
89 | } |
90 | |
c9e9b23c |
91 | displayPageHeader($color, 'None'); |
abdfb4d0 |
92 | |
93 | |
94 | $defdata = array(); |
c9e9b23c |
95 | $formerror = ''; |
abdfb4d0 |
96 | $abortform = false; |
97 | $showaddrlist = true; |
a82f283d |
98 | $defselected = array(); |
abdfb4d0 |
99 | |
100 | |
101 | // Handle user's actions |
c9e9b23c |
102 | if($REQUEST_METHOD == 'POST') { |
abdfb4d0 |
103 | |
a82f283d |
104 | // *********************************************** |
105 | // Add new address |
106 | // *********************************************** |
c9e9b23c |
107 | if(!empty($addaddr['nickname'])) { |
abdfb4d0 |
108 | |
b43bdb85 |
109 | $r = $abook->add($addaddr, $abook->localbackend); |
abdfb4d0 |
110 | |
111 | // Handle error messages |
112 | if(!$r) { |
113 | // Remove backend name from error string |
114 | $errstr = $abook->error; |
c9e9b23c |
115 | $errstr = ereg_replace('^\[.*\] *', '', $errstr); |
abdfb4d0 |
116 | |
117 | $formerror = $errstr; |
118 | $showaddrlist = false; |
b43bdb85 |
119 | $defdata = $addaddr; |
abdfb4d0 |
120 | } |
121 | |
a82f283d |
122 | } |
abdfb4d0 |
123 | |
abdfb4d0 |
124 | |
a82f283d |
125 | // *********************************************** |
126 | // Delete address(es) |
127 | // *********************************************** |
b43bdb85 |
128 | else if((!empty($deladdr)) && |
129 | sizeof($sel) > 0) { |
130 | $orig_sel = $sel; |
a82f283d |
131 | sort($sel); |
132 | |
133 | // The selected addresses are identidied by "backend:nickname". |
134 | // Sort the list and process one backend at the time |
135 | $prevback = -1; |
136 | $subsel = array(); |
137 | $delfailed = false; |
138 | |
139 | for($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) { |
506cb44a |
140 | list($sbackend, $snick) = explode(':', $sel[$i]); |
a82f283d |
141 | |
142 | // When we get to a new backend, process addresses in |
143 | // previous one. |
144 | if($prevback != $sbackend && $prevback != -1) { |
145 | |
146 | $r = $abook->remove($subsel, $prevback); |
147 | if(!$r) { |
148 | $formerror = $abook->error; |
149 | $i = sizeof($sel); |
150 | $delfailed = true; |
151 | break; |
152 | } |
153 | $subsel = array(); |
154 | } |
155 | |
156 | // Queue for processing |
157 | array_push($subsel, $snick); |
158 | $prevback = $sbackend; |
159 | } |
160 | |
161 | if(!$delfailed) { |
162 | $r = $abook->remove($subsel, $prevback); |
163 | if(!$r) { // Handle errors |
164 | $formerror = $abook->error; |
165 | $delfailed = true; |
166 | } |
167 | } |
168 | |
169 | if($delfailed) { |
170 | $showaddrlist = true; |
b43bdb85 |
171 | $defselected = $orig_sel; |
a82f283d |
172 | } |
abdfb4d0 |
173 | } |
174 | |
a82f283d |
175 | |
176 | // *********************************************** |
177 | // Update/modify address |
178 | // *********************************************** |
b43bdb85 |
179 | else if(!empty($editaddr)) { |
a82f283d |
180 | |
181 | // Stage one: Copy data into form |
b43bdb85 |
182 | if(sizeof($sel) > 0) { |
183 | if(sizeof($sel) > 1) { |
a82f283d |
184 | $formerror = _("You can only edit one address at the time"); |
185 | $showaddrlist = true; |
b43bdb85 |
186 | $defselected = $sel; |
a82f283d |
187 | } else { |
188 | $abortform = true; |
506cb44a |
189 | list($ebackend, $enick) = explode(':', $sel[0]); |
a82f283d |
190 | $olddata = $abook->lookup($enick, $ebackend); |
191 | |
192 | // Display the "new address" form |
c9e9b23c |
193 | print "<FORM ACTION=\"$PHP_SELF\" METHOD=\"POST\">\n"; |
a82f283d |
194 | print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n"; |
195 | print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>"; |
196 | print _("Update address"); |
197 | print "<STRONG>\n</TD></TR>\n"; |
198 | print "</TABLE>\n"; |
199 | address_form("editaddr", _("Update address"), $olddata); |
200 | printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n", |
201 | htmlspecialchars($olddata["nickname"])); |
202 | printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n", |
203 | htmlspecialchars($olddata["backend"])); |
204 | print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n"; |
c9e9b23c |
205 | print '</FORM>'; |
a82f283d |
206 | } |
207 | } |
208 | |
209 | // Stage two: Write new data |
b43bdb85 |
210 | else if($doedit = 1) { |
211 | $newdata = $editaddr; |
212 | $r = $abook->modify($oldnick, $newdata, $backend); |
a82f283d |
213 | |
214 | // Handle error messages |
215 | if(!$r) { |
216 | // Display error |
217 | print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n"; |
218 | print "<TR><TD ALIGN=CENTER>\n<br><STRONG>"; |
219 | print "<FONT COLOR=\"$color[2]\">"._("ERROR").": ". |
220 | $abook->error."</FONT>"; |
221 | print "<STRONG>\n</TD></TR>\n"; |
222 | print "</TABLE>\n"; |
223 | |
224 | // Display the "new address" form again |
225 | printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF); |
226 | print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n"; |
227 | print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>"; |
228 | print _("Update address"); |
229 | print "<STRONG>\n</TD></TR>\n"; |
230 | print "</TABLE>\n"; |
231 | address_form("editaddr", _("Update address"), $newdata); |
232 | printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n", |
d9fdc8d3 |
233 | htmlspecialchars($oldnick)); |
a82f283d |
234 | printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n", |
d9fdc8d3 |
235 | htmlspecialchars($backend)); |
a82f283d |
236 | print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n"; |
c9e9b23c |
237 | print '</FORM>'; |
a82f283d |
238 | |
239 | $abortform = true; |
240 | } |
241 | } |
242 | |
243 | // Should not get here... |
244 | else { |
245 | plain_error_message(_("Unknown error"), $color); |
246 | $abortform = true; |
247 | } |
248 | } // End of edit address |
249 | |
250 | |
251 | |
abdfb4d0 |
252 | // Some times we end output before forms are printed |
253 | if($abortform) { |
254 | print "</BODY></HTML>\n"; |
255 | exit(); |
256 | } |
abdfb4d0 |
257 | } |
258 | |
a82f283d |
259 | |
abdfb4d0 |
260 | // =================================================================== |
261 | // The following is only executed on a GET request, or on a POST when |
262 | // a user is added, or when "delete" or "modify" was successful. |
263 | // =================================================================== |
264 | |
265 | // Display error messages |
266 | if(!empty($formerror)) { |
267 | print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n"; |
268 | print "<TR><TD ALIGN=CENTER>\n<br><STRONG>"; |
269 | print "<FONT COLOR=\"$color[2]\">"._("ERROR").": $formerror</FONT>"; |
270 | print "<STRONG>\n</TD></TR>\n"; |
271 | print "</TABLE>\n"; |
272 | } |
273 | |
a82f283d |
274 | |
abdfb4d0 |
275 | // Display the address management part |
276 | if($showaddrlist) { |
abdfb4d0 |
277 | // Get and sort address list |
278 | $alist = $abook->list_addr(); |
9428553f |
279 | if(!is_array($alist)) { |
280 | plain_error_message($abook->error, $color); |
281 | exit; |
282 | } |
283 | |
abdfb4d0 |
284 | usort($alist,'alistcmp'); |
a82f283d |
285 | $prevbackend = -1; |
6ed96f54 |
286 | $headerprinted = false; |
a82f283d |
287 | |
288 | // List addresses |
9428553f |
289 | printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF); |
fb16d219 |
290 | while(list($undef,$row) = each($alist)) { |
a82f283d |
291 | |
292 | // New table header for each backend |
293 | if($prevbackend != $row["backend"]) { |
294 | if($prevbackend >= 0) { |
c9e9b23c |
295 | print '<TR><TD COLSPAN="5" ALIGN=center>'; |
a82f283d |
296 | print " <BR></TD></TR></TABLE>\n"; |
297 | } |
298 | |
299 | print "<TABLE WIDTH=\"95%\" COLS=1 ALIGN=CENTER>\n"; |
300 | print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>"; |
301 | print $row["source"]; |
302 | print "<STRONG>\n</TD></TR>\n"; |
303 | print "</TABLE>\n"; |
304 | |
c9e9b23c |
305 | print '<TABLE COLS="5" BORDER="0" CELLPADDING="1" CELLSPACING="0" WIDTH="90%" ALIGN="center">'; |
59bfec55 |
306 | printf('<TR BGCOLOR="%s"><TH ALIGN=left WIDTH="%s"> '. |
307 | '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'. |
308 | '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'. |
309 | "</TR>\n", $color[9], "1%", |
310 | "1%", _("Nickname"), |
311 | "1%", _("Name"), |
312 | "1%", _("E-mail"), |
313 | "%", _("Info")); |
a82f283d |
314 | $line = 0; |
6ed96f54 |
315 | $headerprinted = true; |
a82f283d |
316 | } // End of header |
317 | |
c9e9b23c |
318 | $prevbackend = $row['backend']; |
a82f283d |
319 | |
320 | // Check if this user is selected |
c9e9b23c |
321 | if(in_array($row['backend'].':'.$row['nickname'], $defselected)) |
322 | $selected = 'CHECKED'; |
a82f283d |
323 | else |
c9e9b23c |
324 | $selected = ''; |
a82f283d |
325 | |
326 | // Print one row |
59bfec55 |
327 | printf("<TR%s>", |
328 | (($line % 2) ? " bgcolor=\"$color[0]\"" : "")); |
329 | print '<TD VALIGN=top ALIGN=center WIDTH="1%"><SMALL>'; |
330 | printf('<INPUT TYPE=checkbox %s NAME="sel[]" VALUE="%s:%s"></SMALL></TD>', |
331 | $selected, $row["backend"], $row["nickname"]); |
332 | printf('<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>'. |
333 | '<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>', |
334 | "1%", $row["nickname"], |
335 | "1%", $row["name"]); |
336 | printf('<TD VALIGN=top NOWRAP WIDTH="%s"> <A HREF="compose.php?send_to=%s">%s</A> </TD>'."\n", |
337 | "1%", rawurlencode($row["email"]), $row["email"]); |
338 | printf('<TD VALIGN=top WIDTH="%s"> %s </TD>', |
339 | "%", $row["label"]); |
340 | print "</TR>\n"; |
abdfb4d0 |
341 | $line++; |
a82f283d |
342 | } |
343 | |
344 | // End of list. Close table. |
6ed96f54 |
345 | if($headerprinted) { |
346 | print "<TR><TD COLSPAN=5 ALIGN=center>\n"; |
347 | printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n", |
348 | _("Edit selected")); |
349 | printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n", |
350 | _("Delete selected")); |
351 | print "</TR></TABLE></FORM>"; |
352 | } |
a82f283d |
353 | } // end of addresslist |
354 | |
abdfb4d0 |
355 | |
356 | // Display the "new address" form |
d7d3c4d4 |
357 | printf("<FORM ACTION=\"%s\" NAME=f_add METHOD=\"POST\">\n", $PHP_SELF); |
abdfb4d0 |
358 | print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n"; |
359 | print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>"; |
a82f283d |
360 | printf(_("Add to %s"), $abook->localbackendname); |
abdfb4d0 |
361 | print "<STRONG>\n</TD></TR>\n"; |
362 | print "</TABLE>\n"; |
c9e9b23c |
363 | address_form('addaddr', _("Add address"), $defdata); |
364 | print '</FORM>'; |
abdfb4d0 |
365 | |
efe94e8e |
366 | // Add hook for anything that wants on the bottom |
367 | do_hook("addressbook_bottom"); |
abdfb4d0 |
368 | ?> |
369 | |
370 | </BODY></HTML> |