Improve html encoding of addressbook, and rename a misspelled function.
[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 require_once(SM_PATH . 'functions/forms.php');
26
27 /** lets get the global vars we may need */
28 sqgetGlobalVar('key', $key, SQ_COOKIE);
29
30 sqgetGlobalVar('username', $username, SQ_SESSION);
31 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
32 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
33 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
34
35 /* From the address form */
36 sqgetGlobalVar('addaddr', $addaddr, SQ_POST);
37 sqgetGlobalVar('editaddr', $editaddr, SQ_POST);
38 sqgetGlobalVar('deladdr', $deladdr, SQ_POST);
39 sqgetGlobalVar('sel', $sel, SQ_POST);
40 sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
41 sqgetGlobalVar('backend', $backend, SQ_POST);
42 sqgetGlobalVar('doedit', $doedit, SQ_POST);
43
44 /**
45 * Make an input field
46 * @param string $label
47 * @param string $field
48 * @param string $name
49 * @param string $size
50 * @param array $values
51 * @param string $add
52 */
53 function addressbook_inp_field($label, $field, $name, $size, $values, $add) {
54 global $color;
55 $value = ( isset($values[$field]) ? $values[$field] : '');
56
57 $td_str = addInput($name.'['.$field.']', $value, $size)
58 . $add ;
59
60 return html_tag( 'tr' ,
61 html_tag( 'td', $label . ':', 'right', $color[4]) .
62 html_tag( 'td', $td_str, 'left', $color[4])
63 )
64 . "\n";
65 }
66
67 /**
68 * Output form to add and modify address data
69 */
70 function address_form($name, $submittext, $values = array()) {
71 global $color, $squirrelmail_language;
72
73 if ($squirrelmail_language == 'ja_JP')
74 {
75 echo html_tag( 'table',
76 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
77 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
78 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
79 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
80 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
81 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
82 list_writable_backends($name) .
83 html_tag( 'tr',
84 html_tag( 'td',
85 '<INPUT TYPE=submit NAME="' . htmlentities($name) . '[SUBMIT]" VALUE="' .
86 $submittext . '">',
87 'center', $color[4], 'colspan="2"')
88 )
89 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
90 } else {
91 echo html_tag( 'table',
92 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
93 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
94 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
95 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
96 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
97 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
98 list_writable_backends($name) .
99 html_tag( 'tr',
100 html_tag( 'td',
101 '<INPUT TYPE=submit NAME="' . htmlentities($name) . '[SUBMIT]" VALUE="' .
102 $submittext . '">',
103 'center', $color[4], 'colspan="2"')
104 )
105 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
106 }
107 }
108
109 function list_writable_backends($name) {
110 global $color, $abook;
111 if ( $name != 'addaddr' ) { return; }
112 if ( $abook->numbackends > 1 ) {
113 $ret = "<select name=backend>";
114 $backends = $abook->get_backend_list();
115 while (list($undef,$v) = each($backends)) {
116 if ($v->writeable) {
117 $ret .= '<OPTION VALUE=' . $v->bnum;
118 $ret .= '>' . $v->sname . "\n";
119 }
120 }
121 $ret .= "</select>";
122 return html_tag( 'tr',
123 html_tag( 'td', _("Add to:"),'right', $color[4] ) .
124 html_tag( 'td', $ret, 'left', $color[4] )) . "\n";
125 } else {
126 return html_tag( 'tr',
127 html_tag( 'td',
128 addHidden('backend', '1'),
129 'center', $color[4], 'colspan="2"')) . "\n";
130 }
131 }
132
133 /* Open addressbook, with error messages on but without LDAP (the *
134 * second "true"). Don't need LDAP here anyway */
135 $abook = addressbook_init(true, true);
136 if($abook->localbackend == 0) {
137 plain_error_message(
138 _("No personal address book is defined. Contact administrator."),
139 $color);
140 exit();
141 }
142
143 displayPageHeader($color, 'None');
144
145 $defdata = array();
146 $formerror = '';
147 $abortform = false;
148 $showaddrlist = true;
149 $defselected = array();
150 $form_url = 'addressbook.php';
151
152
153 /* Handle user's actions */
154 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
155
156 /**************************************************
157 * Add new address *
158 **************************************************/
159 if (isset($addaddr)) {
160 foreach( $addaddr as $k => $adr ) {
161 $addaddr[$k] = strip_tags( $adr );
162 }
163 if (isset($backend)) {
164 $r = $abook->add($addaddr, $backend);
165 } else {
166 $r = $abook->add($addaddr, $abook->localbackend);
167 }
168
169 /* Handle error messages */
170 if (!$r) {
171 /* Remove backend name from error string */
172 $errstr = $abook->error;
173 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
174
175 $formerror = $errstr;
176 $showaddrlist = false;
177 $defdata = $addaddr;
178 }
179 } else {
180
181 /************************************************
182 * Delete address(es) *
183 ************************************************/
184 if ((!empty($deladdr)) && sizeof($sel) > 0) {
185 $orig_sel = $sel;
186 sort($sel);
187
188 /* The selected addresses are identidied by "backend:nickname". *
189 * Sort the list and process one backend at the time */
190 $prevback = -1;
191 $subsel = array();
192 $delfailed = false;
193
194 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
195 list($sbackend, $snick) = explode(':', $sel[$i]);
196
197 /* When we get to a new backend, process addresses in *
198 * previous one. */
199 if ($prevback != $sbackend && $prevback != -1) {
200
201 $r = $abook->remove($subsel, $prevback);
202 if (!$r) {
203 $formerror = $abook->error;
204 $i = sizeof($sel);
205 $delfailed = true;
206 break;
207 }
208 $subsel = array();
209 }
210
211 /* Queue for processing */
212 array_push($subsel, $snick);
213 $prevback = $sbackend;
214 }
215
216 if (!$delfailed) {
217 $r = $abook->remove($subsel, $prevback);
218 if (!$r) { /* Handle errors */
219 $formerror = $abook->error;
220 $delfailed = true;
221 }
222 }
223
224 if ($delfailed) {
225 $showaddrlist = true;
226 $defselected = $orig_sel;
227 }
228
229 } else {
230
231 /***********************************************
232 * Update/modify address *
233 ***********************************************/
234 if (!empty($editaddr)) {
235
236 /* Stage one: Copy data into form */
237 if (isset($sel) && sizeof($sel) > 0) {
238 if(sizeof($sel) > 1) {
239 $formerror = _("You can only edit one address at the time");
240 $showaddrlist = true;
241 $defselected = $sel;
242 } else {
243 $abortform = true;
244 list($ebackend, $enick) = explode(':', $sel[0]);
245 $olddata = $abook->lookup($enick, $ebackend);
246
247 /* Display the "new address" form */
248 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' .
249 "\n" .
250 html_tag( 'table',
251 html_tag( 'tr',
252 html_tag( 'td',
253 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
254 'center', $color[0] )
255 ),
256 'center', '', 'width="100%" ' );
257 address_form("editaddr", _("Update address"), $olddata);
258 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
259 htmlspecialchars($olddata["nickname"]) . "\">\n" .
260 '<INPUT TYPE=hidden NAME=backend VALUE="' .
261 htmlspecialchars($olddata["backend"]) . "\">\n" .
262 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' . "\n" .
263 '</FORM>';
264 }
265 } else {
266
267 /* Stage two: Write new data */
268 if ($doedit = 1) {
269 $newdata = $editaddr;
270 $r = $abook->modify($oldnick, $newdata, $backend);
271
272 /* Handle error messages */
273 if (!$r) {
274 /* Display error */
275 echo html_tag( 'table',
276 html_tag( 'tr',
277 html_tag( 'td',
278 "\n". '<strong><font color="' . $color[2] .
279 '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
280 'center' )
281 ),
282 'center', '', 'width="100%"' );
283
284 /* Display the "new address" form again */
285 echo '<FORM ACTION="' . $form_url .
286 '" METHOD="POST">' . "\n" .
287 html_tag( 'table',
288 html_tag( 'tr',
289 html_tag( 'td',
290 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
291 'center', $color[0] )
292 ),
293 'center', '', 'width="100%"' );
294 address_form("editaddr", _("Update address"), $newdata);
295 echo
296 addHidden('oldnick', $oldnick).
297 addHidden('backend', $backend).
298 addHidden('doedit', '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 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
399
400 /* Print one row */
401 $tr_bgcolor = '';
402 if ($line % 2) { $tr_bgcolor = $color[0]; }
403 if ($squirrelmail_language == 'ja_JP')
404 {
405 echo html_tag( 'tr', '', '', $tr_bgcolor) .
406 html_tag( 'td',
407 '<SMALL>' .
408 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
409 '</SMALL>' ,
410 'center', '', 'valign="top" width="1%"' ) .
411 html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
412 html_tag( 'td', '&nbsp;' . $row['lastname'] . ' ' . $row['firstname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
413 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
414 } else {
415 echo html_tag( 'tr', '', '', $tr_bgcolor) .
416 html_tag( 'td',
417 '<SMALL>' .
418 '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' .
419 $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' ,
420 'center', '', 'valign="top" width="1%"' ) .
421 html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
422 html_tag( 'td', '&nbsp;' . $row['name'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
423 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
424 }
425 $email = $abook->full_address($row);
426 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
427 htmlspecialchars($row['email']) ) .
428 '&nbsp;</td>'."\n".
429 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
430 "</tr>\n";
431 $line++;
432 }
433
434 /* End of list. Close table. */
435 if ($headerprinted) {
436 echo html_tag( 'tr',
437 html_tag( 'td',
438 '<INPUT TYPE="submit" NAME="editaddr" VALUE="' . _("Edit selected") .
439 "\">\n" .
440 '<INPUT TYPE="submit" NAME="deladdr" VALUE="' . _("Delete selected") .
441 "\">\n",
442 'center', '', 'colspan="5"' )
443 );
444 }
445 echo '</table></FORM>';
446 }
447 } /* end of addresslist */
448
449
450 /* Display the "new address" form */
451 echo '<a name="AddAddress"></a>' . "\n" .
452 '<FORM ACTION="' . $form_url . '" NAME=f_add METHOD="POST">' . "\n" .
453 html_tag( 'table',
454 html_tag( 'tr',
455 html_tag( 'td', "\n". '<strong>' . _("Add to address book") . '</strong>' . "\n",
456 'center', $color[0]
457 )
458 )
459 , 'center', '', 'width="100%"' ) ."\n";
460 address_form('addaddr', _("Add address"), $defdata);
461 echo '</FORM>';
462
463 /* Add hook for anything that wants on the bottom */
464 do_hook('addressbook_bottom');
465 ?>
466
467 </BODY></HTML>