make a start with adding labels for html form elements. this aids disabled
[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/**
202bcbcc 16 * Include the SquirrelMail initialization file.
30967a1e 17 */
202bcbcc 18include('../include/init.php');
86725763 19
8f6f9ba5 20/** SquirrelMail required files. */
df4162cb 21/* address book functions */
202bcbcc 22require_once(SM_PATH . 'functions/addressbook.php');
df4162cb 23/* form functions */
202bcbcc 24require_once(SM_PATH . 'functions/forms.php');
ffd8224c 25
8f6f9ba5 26/** lets get the global vars we may need */
0b97a708 27
28/* From the address form */
1e12d1ff 29sqgetGlobalVar('addaddr', $addaddr, SQ_POST);
30sqgetGlobalVar('editaddr', $editaddr, SQ_POST);
31sqgetGlobalVar('deladdr', $deladdr, SQ_POST);
32sqgetGlobalVar('sel', $sel, SQ_POST);
33sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
34sqgetGlobalVar('backend', $backend, SQ_POST);
2741cc97 35sqgetGlobalVar('doedit', $doedit, SQ_POST);
0b97a708 36
08e71631 37/* Get sorting order */
38$abook_sort_order = get_abook_sort();
39
703fa6b5 40/* Create page header before addressbook_init in order to display error messages correctly. */
41displayPageHeader($color, 'None');
42
de00443c 43/* Open addressbook with error messages on.
44 remote backends (LDAP) are enabled because they can be used. (list_addr function)
45*/
46$abook = addressbook_init(true, false);
ced653f3 47
48// FIXME: do we have to stop use of address book, when localbackend is not present.
daba719e 49if($abook->localbackend == 0) {
50 plain_error_message(
056a005c 51 _("No personal address book is defined. Contact administrator."),
52 $color);
daba719e 53 exit();
54}
ffd8224c 55
daba719e 56$defdata = array();
57$formerror = '';
58$abortform = false;
59$showaddrlist = true;
60$defselected = array();
07dcee9f 61$form_url = 'addressbook.php';
daba719e 62
f6c945b9 63/* Handle user's actions */
1e12d1ff 64if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
daba719e 65
f6c945b9 66 /**************************************************
67 * Add new address *
68 **************************************************/
39b26252 69 if (isset($addaddr)) {
a123157f 70 if (isset($backend)) {
71 $r = $abook->add($addaddr, $backend);
72 } else {
73 $r = $abook->add($addaddr, $abook->localbackend);
c6554ec0 74 }
ffd8224c 75
f6c945b9 76 /* Handle error messages */
77 if (!$r) {
78 /* Remove backend name from error string */
ffd8224c 79 $errstr = $abook->error;
80 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
81
82 $formerror = $errstr;
83 $showaddrlist = false;
84 $defdata = $addaddr;
85 }
daba719e 86 } else {
ffd8224c 87
f6c945b9 88 /************************************************
89 * Delete address(es) *
90 ************************************************/
91 if ((!empty($deladdr)) && sizeof($sel) > 0) {
daba719e 92 $orig_sel = $sel;
93 sort($sel);
94
f6c945b9 95 /* The selected addresses are identidied by "backend:nickname". *
96 * Sort the list and process one backend at the time */
daba719e 97 $prevback = -1;
98 $subsel = array();
99 $delfailed = false;
100
f6c945b9 101 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
daba719e 102 list($sbackend, $snick) = explode(':', $sel[$i]);
103
f6c945b9 104 /* When we get to a new backend, process addresses in *
105 * previous one. */
106 if ($prevback != $sbackend && $prevback != -1) {
daba719e 107
108 $r = $abook->remove($subsel, $prevback);
f6c945b9 109 if (!$r) {
daba719e 110 $formerror = $abook->error;
111 $i = sizeof($sel);
112 $delfailed = true;
113 break;
114 }
115 $subsel = array();
116 }
117
f6c945b9 118 /* Queue for processing */
daba719e 119 array_push($subsel, $snick);
120 $prevback = $sbackend;
ffd8224c 121 }
ffd8224c 122
f6c945b9 123 if (!$delfailed) {
daba719e 124 $r = $abook->remove($subsel, $prevback);
f6c945b9 125 if (!$r) { /* Handle errors */
daba719e 126 $formerror = $abook->error;
127 $delfailed = true;
128 }
ffd8224c 129 }
ffd8224c 130
f6c945b9 131 if ($delfailed) {
daba719e 132 $showaddrlist = true;
133 $defselected = $orig_sel;
ffd8224c 134 }
ffd8224c 135
daba719e 136 } else {
137
f6c945b9 138 /***********************************************
139 * Update/modify address *
140 ***********************************************/
141 if (!empty($editaddr)) {
f6c945b9 142 /* Stage one: Copy data into form */
daba719e 143 if (isset($sel) && sizeof($sel) > 0) {
144 if(sizeof($sel) > 1) {
145 $formerror = _("You can only edit one address at the time");
146 $showaddrlist = true;
147 $defselected = $sel;
148 } else {
149 $abortform = true;
150 list($ebackend, $enick) = explode(':', $sel[0]);
151 $olddata = $abook->lookup($enick, $ebackend);
a2b193bc 152 // FIXME: Test if $olddata really contains anything and return an error message if it doesn't
daba719e 153
f6c945b9 154 /* Display the "new address" form */
77fe415a 155 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
2741cc97 156 echo addHidden('oldnick', $olddata['nickname']).
056a005c 157 addHidden('backend', $olddata['backend']).
158 addHidden('doedit', '1').
159 '</form>';
daba719e 160 }
3e87f870 161 } elseif ($doedit == 1) {
f6c945b9 162 /* Stage two: Write new data */
3e87f870 163 $newdata = $editaddr;
164 $r = $abook->modify($oldnick, $newdata, $backend);
daba719e 165
3e87f870 166 /* Handle error messages */
167 if (!$r) {
168 /* Display error */
169 echo html_tag( 'table',
a2b193bc 170 html_tag( 'tr',
171 html_tag( 'td',
172 "\n". '<strong><font color="' . $color[2] .
173 '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
174 'center' )
175 ),
176 'center', '', 'width="100%"' );
3e87f870 177
178 /* Display the "new address" form again */
179 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
180 echo addHidden('oldnick', $oldnick).
181 addHidden('backend', $backend).
182 addHidden('doedit', '1').
183 "\n" . '</form>';
daba719e 184 $abortform = true;
185 }
3e87f870 186 } else {
187 /**
f8a1ed5a 188 * $editaddr is set, but $sel (address selection in address listing)
189 * and $doedit (address edit form) are not set.
3e87f870 190 * Assume that user clicked on "Edit address" without selecting any address.
191 */
192 $formerror = _("Please select address that you want to edit");
193 $showaddrlist = true;
194 } /* end of edit stage detection */
daba719e 195 } /* !empty($editaddr) - Update/modify address */
196 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
197 } /* !empty($addaddr['nickname']) - Add new address */
198
199 // Some times we end output before forms are printed
200 if($abortform) {
056a005c 201 echo "</body></html>\n";
202 exit();
ffd8224c 203 }
daba719e 204}
ffd8224c 205
206
f6c945b9 207/* =================================================================== *
208 * The following is only executed on a GET request, or on a POST when *
209 * a user is added, or when "delete" or "modify" was successful. *
210 * =================================================================== */
ffd8224c 211
f6c945b9 212/* Display error messages */
213if (!empty($formerror)) {
ac987a56 214 echo html_tag( 'table',
056a005c 215 html_tag( 'tr',
216 html_tag( 'td',
217 "\n". '<br /><strong><font color="' . $color[2] .
35235328 218 '">' . _("ERROR") . ': ' . nl2br(htmlspecialchars($formerror)) . '</font></strong>' ."\n",
056a005c 219 'center' )
220 ),
221 'center', '', 'width="100%"' );
daba719e 222}
ffd8224c 223
224
f6c945b9 225/* Display the address management part */
226if ($showaddrlist) {
227 /* Get and sort address list */
daba719e 228 $alist = $abook->list_addr();
229 if(!is_array($alist)) {
35235328 230 plain_error_message(nl2br(htmlspecialchars($abook->error)), $color);
ffd8224c 231 exit;
daba719e 232 }
ffd8224c 233
daba719e 234 usort($alist,'alistcmp');
235 $prevbackend = -1;
236 $headerprinted = false;
ffd8224c 237
ac987a56 238 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
ffd8224c 239
f6c945b9 240 /* List addresses */
91821fc0 241 if (count($alist) > 0) {
056a005c 242 echo addForm($form_url, 'post');
7b0ea860 243 if ($abook->add_extra_field) {
244 $abook_fields = 6;
245 } else {
246 $abook_fields = 5;
247 }
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'),
7b0ea860 258 'center', '', "colspan=\"$abook_fields\"" )
134e4174 259 ) .
260 html_tag( 'tr',
7b0ea860 261 html_tag( 'td', '&nbsp;<br />', 'center', '', "colspan=\"$abook_fields\"" )
134e4174 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),
7b0ea860 279 'left', '', 'width="1%"' ) .
5c4ff7bf 280 ($abook->add_extra_field ? html_tag( 'th', '&nbsp;','left', '', 'width="1%"'): '') .
7b0ea860 281 "\n",
056a005c 282 '', $color[9] ) . "\n";
d9879e29 283 }
284
2741cc97 285 // Separate different backends with <hr />
d9879e29 286 if($prevbackend > 0) {
287 echo html_tag( 'tr',
7b0ea860 288 html_tag( 'td', "<hr />", 'center', '' ,"colspan=\"$abook_fields\"" )
056a005c 289 );
2741cc97 290 }
291
292 // Print backend name
a2b193bc 293 echo html_tag( 'tr',
7b0ea860 294 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,"colspan=\"$abook_fields\"" )
056a005c 295 );
d9879e29 296
91821fc0 297 $line = 0;
298 $headerprinted = true;
299 } /* End of header */
fc93f97c 300
91821fc0 301 $prevbackend = $row['backend'];
056a005c 302
91821fc0 303 /* Check if this user is selected */
19347763 304 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
056a005c 305
9f6c97c3 306 /* Print one row, with alternating color */
056a005c 307 if ($line % 2) {
9f6c97c3 308 $tr_bgcolor = $color[12];
309 } else {
310 $tr_bgcolor = $color[4];
311 }
7b0ea860 312 echo html_tag( 'tr', '', '', $tr_bgcolor);
313 if ($abook->backends[$row['backend']]->writeable) {
828c58f1 314 $id = $row['backend'].':'.$row['nickname'];
7b0ea860 315 echo html_tag( 'td',
316 '<small>' .
828c58f1 317 addCheckBox("sel[$id]", $selected, $id).
7b0ea860 318 '</small>' ,
319 'center', '', 'valign="top" width="1%"' );
828c58f1 320 $label1 = '<label for="sel_'.$id.'_">'; $label2='</label>';
7b0ea860 321 } else {
322 echo html_tag( 'td',
323 '&nbsp;' ,
324 'center', '', 'valign="top" width="1%"' );
828c58f1 325 $label1 = $label2 = '';
7b0ea860 326 }
5c4ff7bf 327 echo html_tag( 'td',
828c58f1 328 '&nbsp;' . $label1 . htmlspecialchars($row['nickname']) . $label2 . '&nbsp;',
7b0ea860 329 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
330
35235328 331 echo html_tag( 'td',
828c58f1 332 '&nbsp;' . $label1 . htmlspecialchars($row['name']) . $label2 . '&nbsp;',
35235328 333 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
7b0ea860 334
335 // email address column
336 echo html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . '&nbsp;';
3d0cada3 337 $email = $abook->full_address($row);
d62c4938 338 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
056a005c 339 htmlspecialchars($row['email'])).
7b0ea860 340 '&nbsp;</td>'."\n";
341
342 // info column
343 echo html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' );
344
345 // add extra column if third party backend needs it
346 if ($abook->add_extra_field) {
5c4ff7bf 347 echo html_tag( 'td',
7b0ea860 348 '&nbsp;' . (isset($row['extra']) ? $row['extra'] : '') . '&nbsp;',
349 'left', '', 'valign="top" width="1%"' );
350 }
351 echo "</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'),
7b0ea860 364 'center', '', "colspan=\"$abook_fields\"" )
134e4174 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
df4162cb 378/* Hook for extra address book blocks */
8811538c 379echo "<!-- start of addressbook_bottom hook-->\n";
daba719e 380do_hook('addressbook_bottom');
8811538c 381echo "\n<!-- end of addressbook_bottom hook-->\n";
5c4ff7bf 382$oTemplate->display('footer.tpl');
828c58f1 383?>