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 * Path for SquirrelMail required files.
19 define('SM_PATH','../');
21 /** SquirrelMail required files. */
22 include_once(SM_PATH
. 'include/validate.php');
23 /* plain_error_message() */
24 include_once(SM_PATH
. 'functions/display_messages.php');
25 /* address book functions */
26 include_once(SM_PATH
. 'functions/addressbook.php');
28 include_once(SM_PATH
. 'functions/forms.php');
30 /** lets get the global vars we may need */
32 /* From the address form */
33 sqgetGlobalVar('addaddr', $addaddr, SQ_POST
);
34 sqgetGlobalVar('editaddr', $editaddr, SQ_POST
);
35 sqgetGlobalVar('deladdr', $deladdr, SQ_POST
);
36 sqgetGlobalVar('sel', $sel, SQ_POST
);
37 sqgetGlobalVar('oldnick', $oldnick, SQ_POST
);
38 sqgetGlobalVar('backend', $backend, SQ_POST
);
39 sqgetGlobalVar('doedit', $doedit, SQ_POST
);
41 /* Get sorting order */
42 $abook_sort_order = get_abook_sort();
44 /* Create page header before addressbook_init in order to display error messages correctly. */
45 displayPageHeader($color, 'None');
47 /* Open addressbook with error messages on.
48 remote backends (LDAP) are enabled because they can be used. (list_addr function)
50 $abook = addressbook_init(true, false);
52 // FIXME: do we have to stop use of address book, when localbackend is not present.
53 if($abook->localbackend
== 0) {
55 _("No personal address book is defined. Contact administrator."),
64 $defselected = array();
65 $form_url = 'addressbook.php';
67 /* Handle user's actions */
68 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
70 /**************************************************
72 **************************************************/
73 if (isset($addaddr)) {
74 if (isset($backend)) {
75 $r = $abook->add($addaddr, $backend);
77 $r = $abook->add($addaddr, $abook->localbackend
);
80 /* Handle error messages */
82 /* Remove backend name from error string */
83 $errstr = $abook->error
;
84 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
87 $showaddrlist = false;
92 /************************************************
93 * Delete address(es) *
94 ************************************************/
95 if ((!empty($deladdr)) && sizeof($sel) > 0) {
99 /* The selected addresses are identidied by "backend:nickname". *
100 * Sort the list and process one backend at the time */
105 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
106 list($sbackend, $snick) = explode(':', $sel[$i]);
108 /* When we get to a new backend, process addresses in *
110 if ($prevback != $sbackend && $prevback != -1) {
112 $r = $abook->remove($subsel, $prevback);
114 $formerror = $abook->error
;
122 /* Queue for processing */
123 array_push($subsel, $snick);
124 $prevback = $sbackend;
128 $r = $abook->remove($subsel, $prevback);
129 if (!$r) { /* Handle errors */
130 $formerror = $abook->error
;
136 $showaddrlist = true;
137 $defselected = $orig_sel;
142 /***********************************************
143 * Update/modify address *
144 ***********************************************/
145 if (!empty($editaddr)) {
146 /* Stage one: Copy data into form */
147 if (isset($sel) && sizeof($sel) > 0) {
148 if(sizeof($sel) > 1) {
149 $formerror = _("You can only edit one address at the time");
150 $showaddrlist = true;
154 list($ebackend, $enick) = explode(':', $sel[0]);
155 $olddata = $abook->lookup($enick, $ebackend);
156 // FIXME: Test if $olddata really contains anything and return an error message if it doesn't
158 /* Display the "new address" form */
159 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
160 echo addHidden('oldnick', $olddata['nickname']).
161 addHidden('backend', $olddata['backend']).
162 addHidden('doedit', '1').
165 } elseif ($doedit == 1) {
166 /* Stage two: Write new data */
167 $newdata = $editaddr;
168 $r = $abook->modify($oldnick, $newdata, $backend);
170 /* Handle error messages */
173 echo html_tag( 'table',
176 "\n". '<strong><font color="' . $color[2] .
177 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
180 'center', '', 'width="100%"' );
182 /* Display the "new address" form again */
183 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
184 echo addHidden('oldnick', $oldnick).
185 addHidden('backend', $backend).
186 addHidden('doedit', '1').
192 * $editaddr is set, but $sel (address selection in address listing)
193 * and $doedit (address edit form) are not set.
194 * Assume that user clicked on "Edit address" without selecting any address.
196 $formerror = _("Please select address that you want to edit");
197 $showaddrlist = true;
198 } /* end of edit stage detection */
199 } /* !empty($editaddr) - Update/modify address */
200 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
201 } /* !empty($addaddr['nickname']) - Add new address */
203 // Some times we end output before forms are printed
205 echo "</body></html>\n";
211 /* =================================================================== *
212 * The following is only executed on a GET request, or on a POST when *
213 * a user is added, or when "delete" or "modify" was successful. *
214 * =================================================================== */
216 /* Display error messages */
217 if (!empty($formerror)) {
218 echo html_tag( 'table',
221 "\n". '<br /><strong><font color="' . $color[2] .
222 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
225 'center', '', 'width="100%"' );
229 /* Display the address management part */
231 /* Get and sort address list */
232 $alist = $abook->list_addr();
233 if(!is_array($alist)) {
234 plain_error_message($abook->error
, $color);
238 usort($alist,'alistcmp');
240 $headerprinted = false;
242 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
245 if (count($alist) > 0) {
246 echo addForm($form_url, 'post');
247 if ($abook->add_extra_field
) {
252 while(list($undef,$row) = each($alist)) {
254 /* New table header for each backend */
255 if($prevbackend != $row['backend']) {
256 if($prevbackend < 0) {
257 echo html_tag( 'table',
260 addSubmit(_("Edit selected"), 'editaddr').
261 addSubmit(_("Delete selected"), 'deladdr'),
262 'center', '', "colspan=\"$abook_fields\"" )
265 html_tag( 'td', ' <br />', 'center', '', "colspan=\"$abook_fields\"" )
268 echo "\n<!-- start of address book table -->\n" .
269 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
270 html_tag( 'tr', "\n" .
271 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) . "\n" .
272 html_tag( 'th', _("Nickname") .
273 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1),
274 'left', '', 'width="1%"' ) . "\n" .
275 html_tag( 'th', _("Name") .
276 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
277 'left', '', 'width="1%"' ) . "\n" .
278 html_tag( 'th', _("E-mail") .
279 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5),
280 'left', '', 'width="1%"' ) . "\n" .
281 html_tag( 'th', _("Info") .
282 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7),
283 'left', '', 'width="1%"' ) .
284 ($abook->add_extra_field ?
html_tag( 'th', ' ','left', '', 'width="1%"'): '') .
286 '', $color[9] ) . "\n";
289 // Separate different backends with <hr />
290 if($prevbackend > 0) {
292 html_tag( 'td', "<hr />", 'center', '' ,"colspan=\"$abook_fields\"" )
296 // Print backend name
298 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,"colspan=\"$abook_fields\"" )
302 $headerprinted = true;
303 } /* End of header */
305 $prevbackend = $row['backend'];
307 /* Check if this user is selected */
308 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
310 /* Print one row, with alternating color */
312 $tr_bgcolor = $color[12];
314 $tr_bgcolor = $color[4];
316 echo html_tag( 'tr', '', '', $tr_bgcolor);
317 if ($abook->backends
[$row['backend']]->writeable
) {
320 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
322 'center', '', 'valign="top" width="1%"' );
326 'center', '', 'valign="top" width="1%"' );
329 ' ' . htmlspecialchars($row['nickname']) . ' ',
330 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
332 // different full name display formating for Japanese translation
333 if ($squirrelmail_language == 'ja_JP') {
335 * translation uses euc-jp character set internally.
336 * htmlspecialchars() should not break any characters.
339 ' ' . htmlspecialchars($row['lastname']) . ' ' . htmlspecialchars($row['firstname']) . ' ',
340 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
343 ' ' . htmlspecialchars($row['name']) . ' ',
344 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
347 // email address column
348 echo html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . ' ';
349 $email = $abook->full_address($row);
350 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
351 htmlspecialchars($row['email'])).
355 echo html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' );
357 // add extra column if third party backend needs it
358 if ($abook->add_extra_field
) {
360 ' ' . (isset($row['extra']) ?
$row['extra'] : '') . ' ',
361 'left', '', 'valign="top" width="1%"' );
367 "\n<!-- end of address book table -->\n";
369 /* End of list. Add edit/delete select buttons */
370 if ($headerprinted) {
371 echo html_tag( 'table',
374 addSubmit(_("Edit selected"), 'editaddr') .
375 addSubmit(_("Delete selected"), 'deladdr'),
376 'center', '', "colspan=\"$abook_fields\"" )
382 } /* end of addresslist */
385 /* Display the "new address" form */
386 echo '<a name="AddAddress"></a>' . "\n";
387 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
390 /* Hook for extra address book blocks */
391 echo "<!-- start of addressbook_bottom hook-->\n";
392 do_hook('addressbook_bottom');
393 echo "\n<!-- end of addressbook_bottom hook-->\n";
394 $oTemplate->display('footer.tpl');