changed <? to <?php in everything
[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
101 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
102 print "<HTML><HEAD><TITLE>\n";
103 printf("%s: %s\n", $org_title, _("Address Book"));
104 print "</TITLE></HEAD>\n\n";
105
106 printf('<BODY TEXT="%s" BGCOLOR="%s" LINK="%s" VLINK="%s" ALINK="%s">',
107 $color[8], $color[4], $color[7], $color[7], $color[7]);
108 displayPageHeader($color, "None");
109
110
111 $defdata = array();
112 $formerror = "";
113 $abortform = false;
114 $showaddrlist = true;
a82f283d 115 $defselected = array();
abdfb4d0 116
117
118 // Handle user's actions
119 if($REQUEST_METHOD == "POST") {
120
a82f283d 121 // ***********************************************
122 // Add new address
123 // ***********************************************
b43bdb85 124 if(!empty($addaddr["nickname"])) {
abdfb4d0 125
b43bdb85 126 $r = $abook->add($addaddr, $abook->localbackend);
abdfb4d0 127
128 // Handle error messages
129 if(!$r) {
130 // Remove backend name from error string
131 $errstr = $abook->error;
132 $errstr = ereg_replace("^\[.*\] *", "", $errstr);
133
134 $formerror = $errstr;
135 $showaddrlist = false;
b43bdb85 136 $defdata = $addaddr;
abdfb4d0 137 }
138
a82f283d 139 }
abdfb4d0 140
abdfb4d0 141
a82f283d 142 // ***********************************************
143 // Delete address(es)
144 // ***********************************************
b43bdb85 145 else if((!empty($deladdr)) &&
146 sizeof($sel) > 0) {
147 $orig_sel = $sel;
a82f283d 148 sort($sel);
149
150 // The selected addresses are identidied by "backend:nickname".
151 // Sort the list and process one backend at the time
152 $prevback = -1;
153 $subsel = array();
154 $delfailed = false;
155
156 for($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
157 list($sbackend, $snick) = split(":", $sel[$i]);
158
159 // When we get to a new backend, process addresses in
160 // previous one.
161 if($prevback != $sbackend && $prevback != -1) {
162
163 $r = $abook->remove($subsel, $prevback);
164 if(!$r) {
165 $formerror = $abook->error;
166 $i = sizeof($sel);
167 $delfailed = true;
168 break;
169 }
170 $subsel = array();
171 }
172
173 // Queue for processing
174 array_push($subsel, $snick);
175 $prevback = $sbackend;
176 }
177
178 if(!$delfailed) {
179 $r = $abook->remove($subsel, $prevback);
180 if(!$r) { // Handle errors
181 $formerror = $abook->error;
182 $delfailed = true;
183 }
184 }
185
186 if($delfailed) {
187 $showaddrlist = true;
b43bdb85 188 $defselected = $orig_sel;
a82f283d 189 }
abdfb4d0 190 }
191
a82f283d 192
193 // ***********************************************
194 // Update/modify address
195 // ***********************************************
b43bdb85 196 else if(!empty($editaddr)) {
a82f283d 197
198 // Stage one: Copy data into form
b43bdb85 199 if(sizeof($sel) > 0) {
200 if(sizeof($sel) > 1) {
a82f283d 201 $formerror = _("You can only edit one address at the time");
202 $showaddrlist = true;
b43bdb85 203 $defselected = $sel;
a82f283d 204 } else {
205 $abortform = true;
b43bdb85 206 list($ebackend, $enick) = split(":", $sel[0]);
a82f283d 207 $olddata = $abook->lookup($enick, $ebackend);
208
209 // Display the "new address" form
210 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
211 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
212 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
213 print _("Update address");
214 print "<STRONG>\n</TD></TR>\n";
215 print "</TABLE>\n";
216 address_form("editaddr", _("Update address"), $olddata);
217 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
218 htmlspecialchars($olddata["nickname"]));
219 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
220 htmlspecialchars($olddata["backend"]));
221 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
222 print "</FORM>";
223 }
224 }
225
226 // Stage two: Write new data
b43bdb85 227 else if($doedit = 1) {
228 $newdata = $editaddr;
229 $r = $abook->modify($oldnick, $newdata, $backend);
a82f283d 230
231 // Handle error messages
232 if(!$r) {
233 // Display error
234 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
235 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
236 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": ".
237 $abook->error."</FONT>";
238 print "<STRONG>\n</TD></TR>\n";
239 print "</TABLE>\n";
240
241 // Display the "new address" form again
242 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
243 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
244 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
245 print _("Update address");
246 print "<STRONG>\n</TD></TR>\n";
247 print "</TABLE>\n";
248 address_form("editaddr", _("Update address"), $newdata);
249 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
250 htmlspecialchars($newdata["nickname"]));
251 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
252 htmlspecialchars($newdata["backend"]));
253 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
254 print "</FORM>";
255
256 $abortform = true;
257 }
258 }
259
260 // Should not get here...
261 else {
262 plain_error_message(_("Unknown error"), $color);
263 $abortform = true;
264 }
265 } // End of edit address
266
267
268
abdfb4d0 269 // Some times we end output before forms are printed
270 if($abortform) {
271 print "</BODY></HTML>\n";
272 exit();
273 }
abdfb4d0 274 }
275
a82f283d 276
abdfb4d0 277 // ===================================================================
278 // The following is only executed on a GET request, or on a POST when
279 // a user is added, or when "delete" or "modify" was successful.
280 // ===================================================================
281
282 // Display error messages
283 if(!empty($formerror)) {
284 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
285 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
286 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": $formerror</FONT>";
287 print "<STRONG>\n</TD></TR>\n";
288 print "</TABLE>\n";
289 }
290
a82f283d 291
abdfb4d0 292 // Display the address management part
293 if($showaddrlist) {
294 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
295
abdfb4d0 296 // Get and sort address list
297 $alist = $abook->list_addr();
298 usort($alist,'alistcmp');
a82f283d 299 $prevbackend = -1;
6ed96f54 300 $headerprinted = false;
a82f283d 301
302 // List addresses
abdfb4d0 303 while(list($key,$row) = each($alist)) {
a82f283d 304
305 // New table header for each backend
306 if($prevbackend != $row["backend"]) {
307 if($prevbackend >= 0) {
308 print "<TR><TD COLSPAN=5 ALIGN=center>";
309 print "&nbsp;<BR></TD></TR></TABLE>\n";
310 }
311
312 print "<TABLE WIDTH=\"95%\" COLS=1 ALIGN=CENTER>\n";
313 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
314 print $row["source"];
315 print "<STRONG>\n</TD></TR>\n";
316 print "</TABLE>\n";
317
318 print "<TABLE COLS=5 BORDER=0 CELLPADDING=1 CELLSPACING=0 ".
319 "WIDTH=\"90%\" ALIGN=center>";
320 printf("<tr bgcolor=\"$color[9]\"><TH align=left width=\"3%%\">".
321 "&nbsp;<TH align=left width=\"10%%\">%s<TH align=left>%s".
322 "<TH align=left>%s<TH align=left>%s</TR>\n",
323 _("Nickname"), _("Name"), _("E-mail"), _("Info"));
324 $line = 0;
6ed96f54 325 $headerprinted = true;
a82f283d 326 } // End of header
327
328 $prevbackend = $row["backend"];
329
330 // Check if this user is selected
331 if(in_array($row["backend"].":".$row["nickname"], $defselected))
332 $selected = "CHECKED";
333 else
334 $selected = "";
335
336 // Print one row
337 printf("<TR%s NOWRAP>\n <TD align=center><SMALL>".
338 "<INPUT TYPE=checkbox %s NAME=\"sel[]\" VALUE=\"%d:%s\">".
339 "</SMALL><TD NOWRAP>&nbsp;%s&nbsp;<TD NOWRAP>&nbsp;%s&nbsp;".
2a32fc83 340 "<TD NOWRAP>&nbsp;<A HREF=\"compose.php?PHPSESSID=$PHPSESSID&send_to=%s\">%s</A>".
a82f283d 341 "&nbsp;<TD NOWRAP>&nbsp;%s</TR>\n",
342 ($line % 2) ? " bgcolor=\"$color[0]\"" : "",
343 $selected, $row["backend"], $row["nickname"],
344 $row["nickname"], $row["name"],
345 rawurlencode($row["email"]), $row["email"], $row["label"]);
abdfb4d0 346 $line++;
a82f283d 347 }
348
349 // End of list. Close table.
6ed96f54 350 if($headerprinted) {
351 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
352 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
353 _("Edit selected"));
354 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
355 _("Delete selected"));
356 print "</TR></TABLE></FORM>";
357 }
a82f283d 358 } // end of addresslist
359
abdfb4d0 360
361 // Display the "new address" form
362 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
363 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
364 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
a82f283d 365 printf(_("Add to %s"), $abook->localbackendname);
abdfb4d0 366 print "<STRONG>\n</TD></TR>\n";
367 print "</TABLE>\n";
368 address_form("addaddr", _("Add address"), $defdata);
369 print "</FORM>";
370
371?>
372
373</BODY></HTML>