R2L by Yoav
[squirrelmail.git] / functions / addressbook.php
CommitLineData
5100704d 1<?php
2
35586184 3/**
4 * addressbook.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
75e19c7f 9 * Manage personal address book.
35586184 10 *
11 * $Id$
12 */
13
75e19c7f 14require_once('../src/validate.php');
15require_once('../functions/array.php');
16require_once('../functions/display_messages.php');
17require_once('../functions/addressbook.php');
18require_once('../functions/strings.php');
19require_once('../functions/html.php');
20
21/* Make an input field */
22function 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";
4935919f 34}
35
75e19c7f 36/* Output form to add and modify address data */
37function 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";
4935919f 53}
54
4935919f 55
75e19c7f 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);
59if($abook->localbackend == 0) {
60 plain_error_message(
61 _("No personal address book is defined. Contact administrator."),
62 $color);
63 exit();
4935919f 64}
65
75e19c7f 66displayPageHeader($color, 'None');
4935919f 67
4935919f 68
75e19c7f 69$defdata = array();
70$formerror = '';
71$abortform = false;
72$showaddrlist = true;
73$defselected = array();
4935919f 74
4935919f 75
75e19c7f 76/* Handle user's actions */
77if($REQUEST_METHOD == 'POST') {
4935919f 78
75e19c7f 79 /**************************************************
80 * Add new address *
81 **************************************************/
82 if (!empty($addaddr['nickname'])) {
5100704d 83
75e19c7f 84 $r = $abook->add($addaddr, $abook->localbackend);
4935919f 85
75e19c7f 86 /* Handle error messages */
87 if (!$r) {
88 /* Remove backend name from error string */
89 $errstr = $abook->error;
90 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
4935919f 91
75e19c7f 92 $formerror = $errstr;
93 $showaddrlist = false;
94 $defdata = $addaddr;
4935919f 95 }
4935919f 96
75e19c7f 97 } else {
4935919f 98
75e19c7f 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();
4935919f 127 }
4935919f 128
75e19c7f 129 /* Queue for processing */
130 array_push($subsel, $snick);
131 $prevback = $sbackend;
4935919f 132 }
133
75e19c7f 134 if (!$delfailed) {
135 $r = $abook->remove($subsel, $prevback);
136 if (!$r) { /* Handle errors */
137 $formerror = $abook->error;
138 $delfailed = true;
139 }
140 }
4935919f 141
75e19c7f 142 if ($delfailed) {
143 $showaddrlist = true;
144 $defselected = $orig_sel;
4935919f 145 }
4935919f 146
75e19c7f 147 } else {
4935919f 148
75e19c7f 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 {
4935919f 184
75e19c7f 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();
4935919f 236 }
75e19c7f 237}
4935919f 238
239
75e19c7f 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 */
246if (!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}
4935919f 256
257
75e19c7f 258/* Display the address management part */
259if ($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;
4935919f 265 }
266
75e19c7f 267 usort($alist,'alistcmp');
268 $prevbackend = -1;
269 $headerprinted = false;
4935919f 270
75e19c7f 271 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
4935919f 272
75e19c7f 273 /* List addresses */
274 if (count($alist) > 0) {
275 echo '<FORM ACTION="' . $PHP_SELF . '" METHOD="POST">' . "\n";
276 while(list($undef,$row) = each($alist)) {
4935919f 277
75e19c7f 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 }
4935919f 295
75e19c7f 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";
4935919f 309
75e19c7f 310 $line = 0;
311 $headerprinted = true;
312 } /* End of header */
4935919f 313
75e19c7f 314 $prevbackend = $row['backend'];
4935919f 315
75e19c7f 316 /* Check if this user is selected */
317 if(in_array($row['backend'] . ':' . $row['nickname'], $defselected)) {
318 $selected = 'CHECKED';
319 } else {
320 $selected = '';
321 }
4935919f 322
75e19c7f 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 if ($compose_new_win == '1') {
336 echo '<a href="javascript:void(0)" onclick=comp_in_new(false,"compose.php?send_to='.rawurlencode($row['email']).'")>';
337 }
338 else {
339 echo '<A HREF="compose.php?send_to=' . rawurlencode($row['email']).'">';
340 }
341 echo $row['email'] . '</A>&nbsp;</td>'."\n".
342 html_tag( 'td', '&nbsp;' . $row['label'] . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
343 "</tr>\n";
344 $line++;
a10110a5 345 }
75e19c7f 346
347 /* End of list. Close table. */
348 if ($headerprinted) {
349 echo html_tag( 'tr',
350 html_tag( 'td',
351 '<INPUT TYPE="submit" NAME="editaddr" VALUE="' . _("Edit selected") .
352 "\">\n" .
353 '<INPUT TYPE="submit" NAME="deladdr" VALUE="' . _("Delete selected") .
354 "\">\n",
355 'center', '', 'colspan="5"' )
356 );
357 }
358 echo '</table></FORM>';
a10110a5 359 }
75e19c7f 360} /* end of addresslist */
361
362
363/* Display the "new address" form */
364echo '<a name="AddAddress"></a>' . "\n" .
365 '<FORM ACTION="' . $PHP_SELF . '" NAME=f_add METHOD="POST">' . "\n" .
366 html_tag( 'table',
367 html_tag( 'tr',
368 html_tag( 'td', "\n". '<strong>' . sprintf(_("Add to %s"), $abook->localbackendname) . '</strong>' . "\n",
369 'center', $color[0]
370 )
371 )
372 , 'center', '', 'width="100%" cols="1"' ) ."\n";
373address_form('addaddr', _("Add address"), $defdata);
374echo '</FORM>';
375
376/* Add hook for anything that wants on the bottom */
377do_hook('addressbook_bottom');
15e6162e 378?>
75e19c7f 379
380</BODY></HTML>