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