6 * Manage personal address book.
8 * @copyright © 1999-2006 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
12 * @subpackage addressbook
16 * Include the SquirrelMail initialization file.
18 include('../include/init.php');
20 /** SquirrelMail required files. */
21 /* address book functions */
22 require_once(SM_PATH
. 'functions/addressbook.php');
24 require_once(SM_PATH
. 'functions/forms.php');
26 /** lets get the global vars we may need */
28 /* From the address form */
29 sqgetGlobalVar('addaddr', $addaddr, SQ_POST
);
30 sqgetGlobalVar('editaddr', $editaddr, SQ_POST
);
31 sqgetGlobalVar('deladdr', $deladdr, SQ_POST
);
32 sqgetGlobalVar('sel', $sel, SQ_POST
);
33 sqgetGlobalVar('oldnick', $oldnick, SQ_POST
);
34 sqgetGlobalVar('backend', $backend, SQ_POST
);
35 sqgetGlobalVar('doedit', $doedit, SQ_POST
);
37 /* Get sorting order */
38 $abook_sort_order = get_abook_sort();
40 /* Create page header before addressbook_init in order to display error messages correctly. */
41 displayPageHeader($color, 'None');
43 /* Open addressbook with error messages on.
44 remote backends (LDAP) are enabled because they can be used. (list_addr function)
46 $abook = addressbook_init(true, false);
48 // FIXME: do we have to stop use of address book, when localbackend is not present.
49 if($abook->localbackend
== 0) {
51 _("No personal address book is defined. Contact administrator."),
60 $defselected = array();
61 $form_url = 'addressbook.php';
63 /* Handle user's actions */
64 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
66 /**************************************************
68 **************************************************/
69 if (isset($addaddr)) {
70 if (isset($backend)) {
71 $r = $abook->add($addaddr, $backend);
73 $r = $abook->add($addaddr, $abook->localbackend
);
76 /* Handle error messages */
78 /* Remove backend name from error string */
79 $errstr = $abook->error
;
80 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
83 $showaddrlist = false;
88 /************************************************
89 * Delete address(es) *
90 ************************************************/
91 if ((!empty($deladdr)) && sizeof($sel) > 0) {
95 /* The selected addresses are identidied by "backend:nickname". *
96 * Sort the list and process one backend at the time */
101 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
102 list($sbackend, $snick) = explode(':', $sel[$i]);
104 /* When we get to a new backend, process addresses in *
106 if ($prevback != $sbackend && $prevback != -1) {
108 $r = $abook->remove($subsel, $prevback);
110 $formerror = $abook->error
;
118 /* Queue for processing */
119 array_push($subsel, $snick);
120 $prevback = $sbackend;
124 $r = $abook->remove($subsel, $prevback);
125 if (!$r) { /* Handle errors */
126 $formerror = $abook->error
;
132 $showaddrlist = true;
133 $defselected = $orig_sel;
138 /***********************************************
139 * Update/modify address *
140 ***********************************************/
141 if (!empty($editaddr)) {
142 /* Stage one: Copy data into form */
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;
150 list($ebackend, $enick) = explode(':', current($sel));
151 $olddata = $abook->lookup($enick, $ebackend);
152 // Test if $olddata really contains anything and return an error message if it doesn't
154 error_box(nl2br(htmlspecialchars($abook->error
)));
156 /* Display the "new address" form */
157 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
158 echo addHidden('oldnick', $olddata['nickname']).
159 addHidden('backend', $olddata['backend']).
160 addHidden('doedit', '1').
164 } elseif ($doedit == 1) {
165 /* Stage two: Write new data */
166 $newdata = $editaddr;
167 $r = $abook->modify($oldnick, $newdata, $backend);
169 /* Handle error messages */
172 echo html_tag( 'table',
175 "\n". '<strong><font color="' . $color[2] .
176 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
179 'center', '', 'width="100%"' );
181 /* Display the "new address" form again */
182 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
183 echo addHidden('oldnick', $oldnick).
184 addHidden('backend', $backend).
185 addHidden('doedit', '1').
191 * $editaddr is set, but $sel (address selection in address listing)
192 * and $doedit (address edit form) are not set.
193 * Assume that user clicked on "Edit address" without selecting any address.
195 $formerror = _("Please select address that you want to edit");
196 $showaddrlist = true;
197 } /* end of edit stage detection */
198 } /* !empty($editaddr) - Update/modify address */
199 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
200 } /* !empty($addaddr['nickname']) - Add new address */
202 // Some times we end output before forms are printed
204 echo "</body></html>\n";
210 /* =================================================================== *
211 * The following is only executed on a GET request, or on a POST when *
212 * a user is added, or when "delete" or "modify" was successful. *
213 * =================================================================== */
215 /* Display error messages */
216 if (!empty($formerror)) {
217 echo html_tag( 'table',
220 "\n". '<br /><strong><font color="' . $color[2] .
221 '">' . _("ERROR") . ': ' . nl2br(htmlspecialchars($formerror)) . '</font></strong>' ."\n",
224 'center', '', 'width="100%"' );
228 /* Display the address management part */
230 /* Get and sort address list */
231 $alist = $abook->list_addr();
232 if(!is_array($alist)) {
233 plain_error_message(nl2br(htmlspecialchars($abook->error
)), $color);
237 usort($alist,'alistcmp');
239 $headerprinted = false;
241 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
244 if (count($alist) > 0) {
245 echo addForm($form_url, 'post');
246 if ($abook->add_extra_field
) {
251 while(list($undef,$row) = each($alist)) {
253 /* New table header for each backend */
254 if($prevbackend != $row['backend']) {
255 if($prevbackend < 0) {
256 echo html_tag( 'table',
259 addSubmit(_("Edit selected"), 'editaddr').
260 addSubmit(_("Delete selected"), 'deladdr'),
261 'center', '', "colspan=\"$abook_fields\"" )
264 html_tag( 'td', ' <br />', 'center', '', "colspan=\"$abook_fields\"" )
267 echo "\n<!-- start of address book table -->\n" .
268 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
269 html_tag( 'tr', "\n" .
270 html_tag( 'th', ' ', '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" .
274 html_tag( 'th', _("Name") .
275 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
276 'left', '', 'width="1%"' ) . "\n" .
277 html_tag( 'th', _("E-mail") .
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%"' ) .
283 ($abook->add_extra_field ?
html_tag( 'th', ' ','left', '', 'width="1%"'): '') .
285 '', $color[9] ) . "\n";
288 // Separate different backends with <hr />
289 if($prevbackend > 0) {
291 html_tag( 'td', "<hr />", 'center', '' ,"colspan=\"$abook_fields\"" )
295 // Print backend name
297 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,"colspan=\"$abook_fields\"" )
301 $headerprinted = true;
302 } /* End of header */
304 $prevbackend = $row['backend'];
306 /* Check if this user is selected */
307 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
309 /* Print one row, with alternating color */
311 $tr_bgcolor = $color[12];
313 $tr_bgcolor = $color[4];
315 echo html_tag( 'tr', '', '', $tr_bgcolor);
316 if ($abook->backends
[$row['backend']]->writeable
) {
317 $id = $row['backend'].':'.$row['nickname'];
320 addCheckBox("sel[$id]", $selected, $id).
322 'center', '', 'valign="top" width="1%"' );
323 $label1 = '<label for="sel_'.$id.'_">'; $label2='</label>';
327 'center', '', 'valign="top" width="1%"' );
328 $label1 = $label2 = '';
331 ' ' . $label1 . htmlspecialchars($row['nickname']) . $label2 . ' ',
332 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
335 ' ' . $label1 . htmlspecialchars($row['name']) . $label2 . ' ',
336 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
338 // email address column
339 echo html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . ' ';
340 $email = $abook->full_address($row);
341 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
342 htmlspecialchars($row['email'])).
346 echo html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' );
348 // add extra column if third party backend needs it
349 if ($abook->add_extra_field
) {
351 ' ' . (isset($row['extra']) ?
$row['extra'] : '') . ' ',
352 'left', '', 'valign="top" width="1%"' );
358 "\n<!-- end of address book table -->\n";
360 /* End of list. Add edit/delete select buttons */
361 if ($headerprinted) {
362 echo html_tag( 'table',
365 addSubmit(_("Edit selected"), 'editaddr') .
366 addSubmit(_("Delete selected"), 'deladdr'),
367 'center', '', "colspan=\"$abook_fields\"" )
373 } /* end of addresslist */
376 /* Display the "new address" form */
377 echo '<a name="AddAddress"></a>' . "\n";
378 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
381 /* Hook for extra address book blocks */
382 echo "<!-- start of addressbook_bottom hook-->\n";
383 do_hook('addressbook_bottom');
384 echo "\n<!-- end of addressbook_bottom hook-->\n";
385 $oTemplate->display('footer.tpl');