ChangeLog
[squirrelmail.git] / src / addressbook.php
CommitLineData
abdfb4d0 1<?php
134e4174 2
35586184 3/**
4 * addressbook.php
5 *
6c84ba1e 6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Manage personal address book.
10 *
30967a1e 11 * @version $Id$
8f6f9ba5 12 * @package squirrelmail
de00443c 13 * @subpackage addressbook
35586184 14 */
15
30967a1e 16/**
17 * Path for SquirrelMail required files.
18 * @ignore
19 */
86725763 20define('SM_PATH','../');
21
8f6f9ba5 22/** SquirrelMail required files. */
08185f2a 23require_once(SM_PATH . 'include/validate.php');
86725763 24require_once(SM_PATH . 'functions/display_messages.php');
25require_once(SM_PATH . 'functions/addressbook.php');
19347763 26require_once(SM_PATH . 'functions/forms.php');
ffd8224c 27
8f6f9ba5 28/** lets get the global vars we may need */
1e12d1ff 29sqgetGlobalVar('key', $key, SQ_COOKIE);
0b97a708 30
1e12d1ff 31sqgetGlobalVar('username', $username, SQ_SESSION);
32sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
33sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
34sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
0b97a708 35
36/* From the address form */
1e12d1ff 37sqgetGlobalVar('addaddr', $addaddr, SQ_POST);
38sqgetGlobalVar('editaddr', $editaddr, SQ_POST);
39sqgetGlobalVar('deladdr', $deladdr, SQ_POST);
40sqgetGlobalVar('sel', $sel, SQ_POST);
41sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
42sqgetGlobalVar('backend', $backend, SQ_POST);
2741cc97 43sqgetGlobalVar('doedit', $doedit, SQ_POST);
0b97a708 44
08e71631 45/* Get sorting order */
46$abook_sort_order = get_abook_sort();
47
de00443c 48/* Open addressbook with error messages on.
49 remote backends (LDAP) are enabled because they can be used. (list_addr function)
50*/
51$abook = addressbook_init(true, false);
daba719e 52if($abook->localbackend == 0) {
53 plain_error_message(
056a005c 54 _("No personal address book is defined. Contact administrator."),
55 $color);
daba719e 56 exit();
57}
ffd8224c 58
daba719e 59displayPageHeader($color, 'None');
ffd8224c 60
daba719e 61$defdata = array();
62$formerror = '';
63$abortform = false;
64$showaddrlist = true;
65$defselected = array();
07dcee9f 66$form_url = 'addressbook.php';
daba719e 67
f6c945b9 68/* Handle user's actions */
1e12d1ff 69if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
daba719e 70
f6c945b9 71 /**************************************************
72 * Add new address *
73 **************************************************/
39b26252 74 if (isset($addaddr)) {
a123157f 75 if (isset($backend)) {
76 $r = $abook->add($addaddr, $backend);
77 } else {
78 $r = $abook->add($addaddr, $abook->localbackend);
c6554ec0 79 }
ffd8224c 80
f6c945b9 81 /* Handle error messages */
82 if (!$r) {
83 /* Remove backend name from error string */
ffd8224c 84 $errstr = $abook->error;
85 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
86
87 $formerror = $errstr;
88 $showaddrlist = false;
89 $defdata = $addaddr;
90 }
daba719e 91 } else {
ffd8224c 92
f6c945b9 93 /************************************************
94 * Delete address(es) *
95 ************************************************/
96 if ((!empty($deladdr)) && sizeof($sel) > 0) {
daba719e 97 $orig_sel = $sel;
98 sort($sel);
99
f6c945b9 100 /* The selected addresses are identidied by "backend:nickname". *
101 * Sort the list and process one backend at the time */
daba719e 102 $prevback = -1;
103 $subsel = array();
104 $delfailed = false;
105
f6c945b9 106 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
daba719e 107 list($sbackend, $snick) = explode(':', $sel[$i]);
108
f6c945b9 109 /* When we get to a new backend, process addresses in *
110 * previous one. */
111 if ($prevback != $sbackend && $prevback != -1) {
daba719e 112
113 $r = $abook->remove($subsel, $prevback);
f6c945b9 114 if (!$r) {
daba719e 115 $formerror = $abook->error;
116 $i = sizeof($sel);
117 $delfailed = true;
118 break;
119 }
120 $subsel = array();
121 }
122
f6c945b9 123 /* Queue for processing */
daba719e 124 array_push($subsel, $snick);
125 $prevback = $sbackend;
ffd8224c 126 }
ffd8224c 127
f6c945b9 128 if (!$delfailed) {
daba719e 129 $r = $abook->remove($subsel, $prevback);
f6c945b9 130 if (!$r) { /* Handle errors */
daba719e 131 $formerror = $abook->error;
132 $delfailed = true;
133 }
ffd8224c 134 }
ffd8224c 135
f6c945b9 136 if ($delfailed) {
daba719e 137 $showaddrlist = true;
138 $defselected = $orig_sel;
ffd8224c 139 }
ffd8224c 140
daba719e 141 } else {
142
f6c945b9 143 /***********************************************
144 * Update/modify address *
145 ***********************************************/
146 if (!empty($editaddr)) {
daba719e 147
f6c945b9 148 /* Stage one: Copy data into form */
daba719e 149 if (isset($sel) && sizeof($sel) > 0) {
150 if(sizeof($sel) > 1) {
151 $formerror = _("You can only edit one address at the time");
152 $showaddrlist = true;
153 $defselected = $sel;
154 } else {
155 $abortform = true;
156 list($ebackend, $enick) = explode(':', $sel[0]);
157 $olddata = $abook->lookup($enick, $ebackend);
158
f6c945b9 159 /* Display the "new address" form */
77fe415a 160 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
2741cc97 161 echo addHidden('oldnick', $olddata['nickname']).
056a005c 162 addHidden('backend', $olddata['backend']).
163 addHidden('doedit', '1').
164 '</form>';
daba719e 165 }
166 } else {
167
f6c945b9 168 /* Stage two: Write new data */
169 if ($doedit = 1) {
daba719e 170 $newdata = $editaddr;
171 $r = $abook->modify($oldnick, $newdata, $backend);
172
f6c945b9 173 /* Handle error messages */
174 if (!$r) {
175 /* Display error */
056a005c 176 echo html_tag( 'table',
177 html_tag( 'tr',
178 html_tag( 'td',
179 "\n". '<strong><font color="' . $color[2] .
180 '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
181 'center' )
182 ),
183 'center', '', 'width="100%"' );
f6c945b9 184
185 /* Display the "new address" form again */
77fe415a 186 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
c1ac62d4 187 echo addHidden('oldnick', $oldnick).
056a005c 188 addHidden('backend', $backend).
189 addHidden('doedit', '1').
190 "\n" . '</form>';
daba719e 191 $abortform = true;
192 }
193 } else {
194
f6c945b9 195 /* Should not get here... */
daba719e 196 plain_error_message(_("Unknown error"), $color);
197 $abortform = true;
198 }
199 }
200 } /* !empty($editaddr) - Update/modify address */
201 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
202 } /* !empty($addaddr['nickname']) - Add new address */
203
204 // Some times we end output before forms are printed
205 if($abortform) {
056a005c 206 echo "</body></html>\n";
207 exit();
ffd8224c 208 }
daba719e 209}
ffd8224c 210
211
f6c945b9 212/* =================================================================== *
213 * The following is only executed on a GET request, or on a POST when *
214 * a user is added, or when "delete" or "modify" was successful. *
215 * =================================================================== */
ffd8224c 216
f6c945b9 217/* Display error messages */
218if (!empty($formerror)) {
ac987a56 219 echo html_tag( 'table',
056a005c 220 html_tag( 'tr',
221 html_tag( 'td',
222 "\n". '<br /><strong><font color="' . $color[2] .
223 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
224 'center' )
225 ),
226 'center', '', 'width="100%"' );
daba719e 227}
ffd8224c 228
229
f6c945b9 230/* Display the address management part */
231if ($showaddrlist) {
232 /* Get and sort address list */
daba719e 233 $alist = $abook->list_addr();
234 if(!is_array($alist)) {
ffd8224c 235 plain_error_message($abook->error, $color);
236 exit;
daba719e 237 }
ffd8224c 238
daba719e 239 usort($alist,'alistcmp');
240 $prevbackend = -1;
241 $headerprinted = false;
ffd8224c 242
ac987a56 243 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
ffd8224c 244
f6c945b9 245 /* List addresses */
91821fc0 246 if (count($alist) > 0) {
056a005c 247 echo addForm($form_url, 'post');
91821fc0 248 while(list($undef,$row) = each($alist)) {
056a005c 249
91821fc0 250 /* New table header for each backend */
251 if($prevbackend != $row['backend']) {
92cd1e8e 252 if($prevbackend < 0) {
ac987a56 253 echo html_tag( 'table',
134e4174 254 html_tag( 'tr',
255 html_tag( 'td',
056a005c 256 addSubmit(_("Edit selected"), 'editaddr').
257 addSubmit(_("Delete selected"), 'deladdr'),
134e4174 258 'center', '', 'colspan="5"' )
259 ) .
260 html_tag( 'tr',
261 html_tag( 'td', '&nbsp;<br />', 'center', '', 'colspan="5"' )
262 ),
263 'center' );
2741cc97 264 echo "\n<!-- start of address book table -->\n" .
056a005c 265 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
266 html_tag( 'tr', "\n" .
267 html_tag( 'th', '&nbsp;', 'left', '', 'width="1%"' ) . "\n" .
268 html_tag( 'th', _("Nickname") .
269 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1),
270 'left', '', 'width="1%"' ) . "\n" .
91e0dccc 271 html_tag( 'th', _("Name") .
056a005c 272 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
273 'left', '', 'width="1%"' ) . "\n" .
91e0dccc 274 html_tag( 'th', _("E-mail") .
056a005c 275 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5),
276 'left', '', 'width="1%"' ) . "\n" .
277 html_tag( 'th', _("Info") .
278 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7),
279 'left', '', 'width="1%"' ) . "\n",
280 '', $color[9] ) . "\n";
d9879e29 281 }
282
2741cc97 283 // Separate different backends with <hr />
d9879e29 284 if($prevbackend > 0) {
285 echo html_tag( 'tr',
056a005c 286 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
287 );
2741cc97 288 }
289
290 // Print backend name
d9879e29 291 echo html_tag( 'tr',
292 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
056a005c 293 );
d9879e29 294
91821fc0 295 $line = 0;
296 $headerprinted = true;
297 } /* End of header */
fc93f97c 298
91821fc0 299 $prevbackend = $row['backend'];
056a005c 300
91821fc0 301 /* Check if this user is selected */
19347763 302 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
056a005c 303
9f6c97c3 304 /* Print one row, with alternating color */
056a005c 305 if ($line % 2) {
9f6c97c3 306 $tr_bgcolor = $color[12];
307 } else {
308 $tr_bgcolor = $color[4];
309 }
056a005c 310 if ($squirrelmail_language == 'ja_JP') {
311 echo html_tag( 'tr', '', '', $tr_bgcolor);
312 if ($abook->backends[$row['backend']]->writeable) {
313 echo html_tag( 'td',
314 '<small>' .
315 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
316 '</small>' ,
317 'center', '', 'valign="top" width="1%"' );
e842b215 318 } else {
056a005c 319 echo html_tag( 'td',
320 '&nbsp;' ,
321 'center', '', 'valign="top" width="1%"' );
322 }
c435f076 323 echo html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
324 html_tag( 'td', '&nbsp;' . $row['lastname'] . ' ' . $row['firstname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
325 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . '&nbsp;';
19d01c6b 326 } else {
056a005c 327 echo html_tag( 'tr', '', '', $tr_bgcolor);
328 if ($abook->backends[$row['backend']]->writeable) {
329 echo html_tag( 'td',
330 '<small>' .
331 addCheckBox('sel[]', $selected, $row['backend'] . ':' . $row['nickname']).
332 '</small>' ,
333 'center', '', 'valign="top" width="1%"' );
334 } else {
335 echo html_tag( 'td',
336 '&nbsp;' ,
337 'center', '', 'valign="top" width="1%"' );
e842b215 338 }
c435f076 339 echo html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
340 html_tag( 'td', '&nbsp;' . $row['name'] . '&nbsp;', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
341 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . '&nbsp;';
056a005c 342 }
3d0cada3 343 $email = $abook->full_address($row);
d62c4938 344 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
056a005c 345 htmlspecialchars($row['email'])).
346 '&nbsp;</td>'."\n".
347 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
348 "</tr>\n";
91821fc0 349 $line++;
daba719e 350 }
2741cc97 351 echo "</table>" .
056a005c 352 "\n<!-- end of address book table -->\n";
d9879e29 353
fc93f97c 354 /* End of list. Add edit/delete select buttons */
91821fc0 355 if ($headerprinted) {
fc93f97c 356 echo html_tag( 'table',
134e4174 357 html_tag( 'tr',
358 html_tag( 'td',
056a005c 359 addSubmit(_("Edit selected"), 'editaddr') .
360 addSubmit(_("Delete selected"), 'deladdr'),
134e4174 361 'center', '', 'colspan="5"' )
362 ),
056a005c 363 'center' );
91821fc0 364 }
fc93f97c 365 echo "</form>\n";
daba719e 366 }
f6c945b9 367} /* end of addresslist */
daba719e 368
369
f6c945b9 370/* Display the "new address" form */
c1ac62d4 371echo '<a name="AddAddress"></a>' . "\n";
372abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
fc93f97c 373echo "</form>\n";
daba719e 374
f6c945b9 375/* Add hook for anything that wants on the bottom */
8811538c 376echo "<!-- start of addressbook_bottom hook-->\n";
daba719e 377do_hook('addressbook_bottom');
8811538c 378echo "\n<!-- end of addressbook_bottom hook-->\n";
dcc1cc82 379?>
91e0dccc 380</body></html>