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