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