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