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