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