Reintroduce alternating colors in addressbook, something that has
[squirrelmail.git] / src / addressbook.php
1 <?php
2
3 /**
4 * addressbook.php
5 *
6 * Copyright (c) 1999-2003 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 html_tag( 'tr',
82 html_tag( 'td',
83 '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' .
84 $submittext . '">',
85 'center', $color[4], 'colspan="2"')
86 )
87 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
88 } else {
89 echo html_tag( 'table',
90 adressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
91 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
92 adressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
93 adressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
94 adressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
95 adressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
96 html_tag( 'tr',
97 html_tag( 'td',
98 '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' .
99 $submittext . '">',
100 'center', $color[4], 'colspan="2"')
101 )
102 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
103 }
104 }
105
106 /* Open addressbook, with error messages on but without LDAP (the *
107 * second "true"). Don't need LDAP here anyway */
108 $abook = addressbook_init(true, true);
109 if($abook->localbackend == 0) {
110 plain_error_message(
111 _("No personal address book is defined. Contact administrator."),
112 $color);
113 exit();
114 }
115
116 displayPageHeader($color, 'None');
117
118 $defdata = array();
119 $formerror = '';
120 $abortform = false;
121 $showaddrlist = true;
122 $defselected = array();
123 $form_url = 'addressbook.php';
124
125
126 /* Handle user's actions */
127 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
128
129 /**************************************************
130 * Add new address *
131 **************************************************/
132 if (isset($addaddr)) {
133 foreach( $addaddr as $k => $adr ) {
134 $addaddr[$k] = strip_tags( $adr );
135 }
136 $r = $abook->add($addaddr, $abook->localbackend);
137
138 /* Handle error messages */
139 if (!$r) {
140 /* Remove backend name from error string */
141 $errstr = $abook->error;
142 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
143
144 $formerror = $errstr;
145 $showaddrlist = false;
146 $defdata = $addaddr;
147 }
148 } else {
149
150 /************************************************
151 * Delete address(es) *
152 ************************************************/
153 if ((!empty($deladdr)) && sizeof($sel) > 0) {
154 $orig_sel = $sel;
155 sort($sel);
156
157 /* The selected addresses are identidied by "backend:nickname". *
158 * Sort the list and process one backend at the time */
159 $prevback = -1;
160 $subsel = array();
161 $delfailed = false;
162
163 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
164 list($sbackend, $snick) = explode(':', $sel[$i]);
165
166 /* When we get to a new backend, process addresses in *
167 * previous one. */
168 if ($prevback != $sbackend && $prevback != -1) {
169
170 $r = $abook->remove($subsel, $prevback);
171 if (!$r) {
172 $formerror = $abook->error;
173 $i = sizeof($sel);
174 $delfailed = true;
175 break;
176 }
177 $subsel = array();
178 }
179
180 /* Queue for processing */
181 array_push($subsel, $snick);
182 $prevback = $sbackend;
183 }
184
185 if (!$delfailed) {
186 $r = $abook->remove($subsel, $prevback);
187 if (!$r) { /* Handle errors */
188 $formerror = $abook->error;
189 $delfailed = true;
190 }
191 }
192
193 if ($delfailed) {
194 $showaddrlist = true;
195 $defselected = $orig_sel;
196 }
197
198 } else {
199
200 /***********************************************
201 * Update/modify address *
202 ***********************************************/
203 if (!empty($editaddr)) {
204
205 /* Stage one: Copy data into form */
206 if (isset($sel) && sizeof($sel) > 0) {
207 if(sizeof($sel) > 1) {
208 $formerror = _("You can only edit one address at the time");
209 $showaddrlist = true;
210 $defselected = $sel;
211 } else {
212 $abortform = true;
213 list($ebackend, $enick) = explode(':', $sel[0]);
214 $olddata = $abook->lookup($enick, $ebackend);
215
216 /* Display the "new address" form */
217 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' .
218 "\n" .
219 html_tag( 'table',
220 html_tag( 'tr',
221 html_tag( 'td',
222 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
223 'center', $color[0] )
224 ),
225 'center', '', 'width="100%" ' );
226 address_form("editaddr", _("Update address"), $olddata);
227 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
228 htmlspecialchars($olddata["nickname"]) . "\">\n" .
229 '<INPUT TYPE=hidden NAME=backend VALUE="' .
230 htmlspecialchars($olddata["backend"]) . "\">\n" .
231 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' . "\n" .
232 '</FORM>';
233 }
234 } else {
235
236 /* Stage two: Write new data */
237 if ($doedit = 1) {
238 $newdata = $editaddr;
239 $r = $abook->modify($oldnick, $newdata, $backend);
240
241 /* Handle error messages */
242 if (!$r) {
243 /* Display error */
244 echo html_tag( 'table',
245 html_tag( 'tr',
246 html_tag( 'td',
247 "\n". '<strong><font color="' . $color[2] .
248 '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
249 'center' )
250 ),
251 'center', '', 'width="100%"' );
252
253 /* Display the "new address" form again */
254 echo '<FORM ACTION="' . $form_url .
255 '" METHOD="POST">' . "\n" .
256 html_tag( 'table',
257 html_tag( 'tr',
258 html_tag( 'td',
259 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
260 'center', $color[0] )
261 ),
262 'center', '', 'width="100%"' );
263 address_form("editaddr", _("Update address"), $newdata);
264 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
265 htmlspecialchars($oldnick) . "\">\n" .
266 '<INPUT TYPE=hidden NAME=backend VALUE="' .
267 htmlspecialchars($backend) . "\">\n" .
268 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' .
269 "\n" . '</FORM>';
270 $abortform = true;
271 }
272 } else {
273
274 /* Should not get here... */
275 plain_error_message(_("Unknown error"), $color);
276 $abortform = true;
277 }
278 }
279 } /* !empty($editaddr) - Update/modify address */
280 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
281 } /* !empty($addaddr['nickname']) - Add new address */
282
283 // Some times we end output before forms are printed
284 if($abortform) {
285 echo "</BODY></HTML>\n";
286 exit();
287 }
288 }
289
290
291 /* =================================================================== *
292 * The following is only executed on a GET request, or on a POST when *
293 * a user is added, or when "delete" or "modify" was successful. *
294 * =================================================================== */
295
296 /* Display error messages */
297 if (!empty($formerror)) {
298 echo html_tag( 'table',
299 html_tag( 'tr',
300 html_tag( 'td',
301 "\n". '<br><strong><font color="' . $color[2] .
302 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
303 'center' )
304 ),
305 'center', '', 'width="100%"' );
306 }
307
308
309 /* Display the address management part */
310 if ($showaddrlist) {
311 /* Get and sort address list */
312 $alist = $abook->list_addr();
313 if(!is_array($alist)) {
314 plain_error_message($abook->error, $color);
315 exit;
316 }
317
318 usort($alist,'alistcmp');
319 $prevbackend = -1;
320 $headerprinted = false;
321
322 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
323
324 /* List addresses */
325 if (count($alist) > 0) {
326 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' . "\n";
327 while(list($undef,$row) = each($alist)) {
328
329 /* New table header for each backend */
330 if($prevbackend != $row['backend']) {
331 if($prevbackend < 0) {
332 echo html_tag( 'table',
333 html_tag( 'tr',
334 html_tag( 'td',
335 '<INPUT TYPE=submit NAME=editaddr VALUE="' .
336 _("Edit selected") . "\">\n" .
337 '<INPUT TYPE=submit NAME=deladdr VALUE="' .
338 _("Delete selected") . "\">\n",
339 'center', '', 'colspan="5"' )
340 ) .
341 html_tag( 'tr',
342 html_tag( 'td', '&nbsp;<br>', 'center', '', 'colspan="5"' )
343 ) ,
344 'center' );
345 }
346
347 echo html_tag( 'table',
348 html_tag( 'tr',
349 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] )
350 ) ,
351 'center', '', 'width="95%"' ) ."\n"
352 . html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
353 html_tag( 'tr', "\n" .
354 html_tag( 'th', '&nbsp;', 'left', '', 'width="1%"' ) .
355 html_tag( 'th', _("Nickname"), 'left', '', 'width="1%"' ) .
356 html_tag( 'th', _("Name"), 'left', '', 'width="1%"' ) .
357 html_tag( 'th', _("E-mail"), 'left', '', 'width="1%"' ) .
358 html_tag( 'th', _("Info"), 'left', '', 'width="1%"' ) ,
359 '', $color[9] ) . "\n";
360
361 $line = 0;
362 $headerprinted = true;
363 } /* End of header */
364
365 $prevbackend = $row['backend'];
366
367 /* Check if this user is selected */
368 if(in_array($row['backend'] . ':' . $row['nickname'], $defselected)) {
369 $selected = 'CHECKED';
370 } else {
371 $selected = '';
372 }
373
374 /* Print one row */
375 $tr_bgcolor = '';
376 if ($line % 2) { $tr_bgcolor = $color[0]; }
377 if ($squirrelmail_language == 'ja_JP')
378 {
379 echo html_tag( 'tr', '', '', $tr_bgcolor) .
380 html_tag( 'td',
381 '<SMALL>' .
382 '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' .
383 $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' ,
384 'center', '', 'valign="top" width="1%"' ) .
385 html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
386 html_tag( 'td', '&nbsp;' . $row['lastname'] . ' ' . $row['firstname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
387 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
388 } else {
389 echo html_tag( 'tr', '', '', $tr_bgcolor) .
390 html_tag( 'td',
391 '<SMALL>' .
392 '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' .
393 $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' ,
394 'center', '', 'valign="top" width="1%"' ) .
395 html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
396 html_tag( 'td', '&nbsp;' . $row['name'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
397 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
398 }
399 $email = $abook->full_address($row);
400 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
401 htmlspecialchars($row['email']) ) .
402 '&nbsp;</td>'."\n".
403 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
404 "</tr>\n";
405 $line++;
406 }
407
408 /* End of list. Close table. */
409 if ($headerprinted) {
410 echo html_tag( 'tr',
411 html_tag( 'td',
412 '<INPUT TYPE="submit" NAME="editaddr" VALUE="' . _("Edit selected") .
413 "\">\n" .
414 '<INPUT TYPE="submit" NAME="deladdr" VALUE="' . _("Delete selected") .
415 "\">\n",
416 'center', '', 'colspan="5"' )
417 );
418 }
419 echo '</table></FORM>';
420 }
421 } /* end of addresslist */
422
423
424 /* Display the "new address" form */
425 echo '<a name="AddAddress"></a>' . "\n" .
426 '<FORM ACTION="' . $form_url . '" NAME=f_add METHOD="POST">' . "\n" .
427 html_tag( 'table',
428 html_tag( 'tr',
429 html_tag( 'td', "\n". '<strong>' . sprintf(_("Add to %s"), $abook->localbackendname) . '</strong>' . "\n",
430 'center', $color[0]
431 )
432 )
433 , 'center', '', 'width="100%"' ) ."\n";
434 address_form('addaddr', _("Add address"), $defdata);
435 echo '</FORM>';
436
437 /* Add hook for anything that wants on the bottom */
438 do_hook('addressbook_bottom');
439 ?>
440
441 </BODY></HTML>