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