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