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