32f5d9a092add06e15435dbeeba62ad9b0ea542d
[squirrelmail.git] / src / addressbook.php
1 <?php
2 /**
3 ** addressbook.php
4 **
5 ** Copyright (c) 1999-2000 The SquirrelMail development team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
7 **
8 ** Manage personal address book.
9 **
10 **/
11
12 session_start();
13
14 if (!isset($config_php))
15 include("../config/config.php");
16 if (!isset($array_php))
17 include("../functions/array.php");
18 if (!isset($auth_php))
19 include("../functions/auth.php");
20 if (!isset($strings_php))
21 include("../functions/strings.php");
22 if (!isset($page_header_php))
23 include("../functions/page_header.php");
24 if (!isset($display_messages_php))
25 include("../functions/display_messages.php");
26 if (!isset($addressbook_php))
27 include("../functions/addressbook.php");
28
29 is_logged_in();
30
31 // Sort array by the key "name"
32 function alistcmp($a,$b) {
33 if($a["backend"] > $b["backend"])
34 return 1;
35 else if($a["backend"] < $b["backend"])
36 return -1;
37
38 return (strtolower($a["name"]) > strtolower($b["name"])) ? 1 : -1;
39 }
40
41 // Output form to add and modify address data
42 function address_form($name, $submittext, $values = array()) {
43 global $color;
44 print "<TABLE BORDER=0 CELLPADDING=1 COLS=2 WIDTH=\"90%\" ALIGN=center>\n";
45 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
46 _("Nickname"));
47 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
48 "<INPUT NAME=\"%s[nickname]\" SIZE=15 VALUE=\"%s\">".
49 "&nbsp;<SMALL>%s</SMALL></TD></TR>\n",
50 $color[4], $name, htmlspecialchars($values["nickname"]),
51 _("Must be unique"));
52 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
53 _("E-mail address"));
54 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
55 "<INPUT NAME=\"%s[email]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
56 $color[4], $name, htmlspecialchars($values["email"]));
57 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
58 _("First name"));
59 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
60 "<INPUT NAME=\"%s[firstname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
61 $color[4], $name, htmlspecialchars($values["firstname"]));
62 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
63 _("Last name"));
64 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
65 "<INPUT NAME=\"%s[lastname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
66 $color[4], $name, htmlspecialchars($values["lastname"]));
67 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
68 _("Additional info"));
69 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
70 "<INPUT NAME=\"%s[label]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
71 $color[4], $name, htmlspecialchars($values["label"]));
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
81 include("../src/load_prefs.php");
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
91 displayPageHeader($color, "None");
92
93
94 $defdata = array();
95 $formerror = "";
96 $abortform = false;
97 $showaddrlist = true;
98 $defselected = array();
99
100
101 // Handle user's actions
102 if($REQUEST_METHOD == "POST") {
103
104 // ***********************************************
105 // Add new address
106 // ***********************************************
107 if(!empty($addaddr["nickname"])) {
108
109 $r = $abook->add($addaddr, $abook->localbackend);
110
111 // Handle error messages
112 if(!$r) {
113 // Remove backend name from error string
114 $errstr = $abook->error;
115 $errstr = ereg_replace("^\[.*\] *", "", $errstr);
116
117 $formerror = $errstr;
118 $showaddrlist = false;
119 $defdata = $addaddr;
120 }
121
122 }
123
124
125 // ***********************************************
126 // Delete address(es)
127 // ***********************************************
128 else if((!empty($deladdr)) &&
129 sizeof($sel) > 0) {
130 $orig_sel = $sel;
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++) {
140 list($sbackend, $snick) = split(":", $sel[$i]);
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;
171 $defselected = $orig_sel;
172 }
173 }
174
175
176 // ***********************************************
177 // Update/modify address
178 // ***********************************************
179 else if(!empty($editaddr)) {
180
181 // Stage one: Copy data into form
182 if(sizeof($sel) > 0) {
183 if(sizeof($sel) > 1) {
184 $formerror = _("You can only edit one address at the time");
185 $showaddrlist = true;
186 $defselected = $sel;
187 } else {
188 $abortform = true;
189 list($ebackend, $enick) = split(":", $sel[0]);
190 $olddata = $abook->lookup($enick, $ebackend);
191
192 // Display the "new address" form
193 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
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";
205 print "</FORM>";
206 }
207 }
208
209 // Stage two: Write new data
210 else if($doedit = 1) {
211 $newdata = $editaddr;
212 $r = $abook->modify($oldnick, $newdata, $backend);
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",
233 htmlspecialchars($newdata["nickname"]));
234 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
235 htmlspecialchars($newdata["backend"]));
236 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
237 print "</FORM>";
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
252 // Some times we end output before forms are printed
253 if($abortform) {
254 print "</BODY></HTML>\n";
255 exit();
256 }
257 }
258
259
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
274
275 // Display the address management part
276 if($showaddrlist) {
277 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
278
279 // Get and sort address list
280 $alist = $abook->list_addr();
281 usort($alist,'alistcmp');
282 $prevbackend = -1;
283 $headerprinted = false;
284
285 // List addresses
286 while(list($key,$row) = each($alist)) {
287
288 // New table header for each backend
289 if($prevbackend != $row["backend"]) {
290 if($prevbackend >= 0) {
291 print "<TR><TD COLSPAN=5 ALIGN=center>";
292 print "&nbsp;<BR></TD></TR></TABLE>\n";
293 }
294
295 print "<TABLE WIDTH=\"95%\" COLS=1 ALIGN=CENTER>\n";
296 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
297 print $row["source"];
298 print "<STRONG>\n</TD></TR>\n";
299 print "</TABLE>\n";
300
301 print '<TABLE COLS=5 BORDER=0 CELLPADDING=1 CELLSPACING=0 WIDTH="90%" ALIGN=center>';
302 printf('<TR BGCOLOR="%s"><TH ALIGN=left WIDTH="%s">&nbsp;'.
303 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
304 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
305 "</TR>\n", $color[9], "1%",
306 "1%", _("Nickname"),
307 "1%", _("Name"),
308 "1%", _("E-mail"),
309 "%", _("Info"));
310 $line = 0;
311 $headerprinted = true;
312 } // End of header
313
314 $prevbackend = $row["backend"];
315
316 // Check if this user is selected
317 if(in_array($row["backend"].":".$row["nickname"], $defselected))
318 $selected = "CHECKED";
319 else
320 $selected = "";
321
322 // Print one row
323 printf("<TR%s>",
324 (($line % 2) ? " bgcolor=\"$color[0]\"" : ""));
325 print '<TD VALIGN=top ALIGN=center WIDTH="1%"><SMALL>';
326 printf('<INPUT TYPE=checkbox %s NAME="sel[]" VALUE="%s:%s"></SMALL></TD>',
327 $selected, $row["backend"], $row["nickname"]);
328 printf('<TD VALIGN=top NOWRAP WIDTH="%s">&nbsp;%s&nbsp;</TD>'.
329 '<TD VALIGN=top NOWRAP WIDTH="%s">&nbsp;%s&nbsp;</TD>',
330 "1%", $row["nickname"],
331 "1%", $row["name"]);
332 printf('<TD VALIGN=top NOWRAP WIDTH="%s">&nbsp;<A HREF="compose.php?send_to=%s">%s</A>&nbsp;</TD>'."\n",
333 "1%", rawurlencode($row["email"]), $row["email"]);
334 printf('<TD VALIGN=top WIDTH="%s">&nbsp;%s&nbsp;</TD>',
335 "%", $row["label"]);
336 print "</TR>\n";
337 $line++;
338 }
339
340 // End of list. Close table.
341 if($headerprinted) {
342 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
343 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
344 _("Edit selected"));
345 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
346 _("Delete selected"));
347 print "</TR></TABLE></FORM>";
348 }
349 } // end of addresslist
350
351
352 // Display the "new address" form
353 printf("<FORM ACTION=\"%s\" NAME=f_add METHOD=\"POST\">\n", $PHP_SELF);
354 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
355 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
356 printf(_("Add to %s"), $abook->localbackendname);
357 print "<STRONG>\n</TD></TR>\n";
358 print "</TABLE>\n";
359 address_form("addaddr", _("Add address"), $defdata);
360 print "</FORM>";
361
362 ?>
363 <!-- ----------------- csv import form --------------------- -->
364
365 <FORM ENCTYPE="multipart/form-data" ACTION="addressbook_csvimport.php" METHOD=POST>
366 <INPUT TYPE="hidden" NAME="max_file_size" value="5000">
367 Import CSV File: <INPUT NAME="smusercsv" TYPE="file">
368 <INPUT TYPE="submit" VALUE="Import CSV File">
369 </FORM>
370
371 </BODY></HTML>