6 * Manage personal address book.
8 * @copyright © 1999-2007 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
12 * @subpackage addressbook
15 /** This is the addressbook page */
16 define('PAGE_NAME', 'addressbook');
19 * Include the SquirrelMail initialization file.
21 include('../include/init.php');
23 /** SquirrelMail required files. */
24 /* address book functions */
25 require_once(SM_PATH
. 'functions/addressbook.php');
26 include_once(SM_PATH
. 'templates/util_addressbook.php');
29 require_once(SM_PATH
. 'functions/forms.php');
31 /** lets get the global vars we may need */
33 /* From the address form */
34 sqgetGlobalVar('addaddr', $addaddr, SQ_POST
);
35 sqgetGlobalVar('editaddr', $editaddr, SQ_POST
);
36 sqgetGlobalVar('deladdr', $deladdr, SQ_POST
);
37 sqgetGlobalVar('compose_to', $compose_to, SQ_POST
);
38 sqgetGlobalVar('sel', $sel, SQ_POST
);
39 sqgetGlobalVar('oldnick', $oldnick, SQ_POST
);
40 sqgetGlobalVar('backend', $backend, SQ_POST
);
41 sqgetGlobalVar('doedit', $doedit, SQ_POST
);
43 /* Get sorting order */
44 $abook_sort_order = get_abook_sort();
46 /* Create page header before addressbook_init in order to display error messages correctly. */
47 displayPageHeader($color);
49 /* Open addressbook with error messages on.
50 remote backends (LDAP) are enabled because they can be used. (list_addr function)
52 $abook = addressbook_init(true, false);
54 // FIXME: do we have to stop use of address book, when localbackend is not present.
55 if($abook->localbackend
== 0) {
56 plain_error_message(_("No personal address book is defined. Contact administrator."));
60 $current_backend = $abook->localbackend
;
61 if (sqgetGlobalVar('new_bnum', $new_backend, SQ_FORM
)
62 && array_key_exists($new_backend, $abook->backends
)) {
63 $current_backend = (int) $new_backend;
66 $abook_selection = ' ';
67 $list_backends = array();
68 if (count($abook->backends
) > 1) {
69 foreach($abook->get_backend_list() as $oBackend) {
70 if ($oBackend->listing
) {
71 $list_backends[$oBackend->bnum
]=$oBackend->sname
;
74 if (count($list_backends)>1) {
75 $abook_selection = addSelect('new_bnum',$list_backends,$current_backend,true)
76 .addSubmit(_("Change"),'change_abook');
84 $defselected = array();
85 $form_url = 'addressbook.php';
87 /* Handle user's actions */
88 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
90 /**************************************************
92 **************************************************/
93 if (isset($addaddr)) {
94 if (isset($backend)) {
95 $r = $abook->add($addaddr, $backend);
97 $r = $abook->add($addaddr, $abook->localbackend
);
100 /* Handle error messages */
102 /* Remove backend name from error string */
103 $errstr = $abook->error
;
104 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
106 $formerror = $errstr;
107 $showaddrlist = false;
112 /************************************************
113 * Delete address(es) *
114 ************************************************/
115 if ((!empty($deladdr)) && sizeof($sel) > 0) {
119 /* The selected addresses are identified by "backend_nickname". *
120 * Sort the list and process one backend at the time */
125 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
126 list($sbackend, $snick) = explode('_', $sel[$i], 2);
128 /* When we get to a new backend, process addresses in *
130 if ($prevback != $sbackend && $prevback != -1) {
132 $r = $abook->remove($subsel, $prevback);
134 $formerror = $abook->error
;
142 /* Queue for processing */
143 array_push($subsel, $snick);
144 $prevback = $sbackend;
148 $r = $abook->remove($subsel, $prevback);
149 if (!$r) { /* Handle errors */
150 $formerror = $abook->error
;
156 $showaddrlist = true;
157 $defselected = $orig_sel;
160 /************************************************
161 * Compose to selected address(es) *
162 ************************************************/
163 } else if ((!empty($compose_to)) && sizeof($sel) > 0) {
167 // The selected addresses are identified by "backend_nickname"
168 $lookup_failed = false;
171 for ($i = 0 ; (($i < sizeof($sel)) && !$lookup_failed) ; $i++
) {
172 list($sbackend, $snick) = explode('_', $sel[$i], 2);
174 $data = $abook->lookup($snick, $sbackend);
177 $formerror = $abook->error
;
178 $lookup_failed = true;
181 $addr = $abook->full_address($data);
183 $send_to .= $addr . ', ';
188 if ($lookup_failed ||
empty($send_to)) {
189 $showaddrlist = true;
194 // send off to compose screen
196 $send_to = trim($send_to, ', ');
197 header('Location: ' . $base_uri . 'src/compose.php?send_to=' . rawurlencode($send_to));
203 /***********************************************
204 * Update/modify address *
205 ***********************************************/
206 if (!empty($editaddr)) {
207 /* Stage one: Copy data into form */
208 if (isset($sel) && sizeof($sel) > 0) {
209 if(sizeof($sel) > 1) {
210 $formerror = _("You can only edit one address at the time");
211 $showaddrlist = true;
215 list($ebackend, $enick) = explode('_', current($sel), 2);
216 $olddata = $abook->lookup($enick, $ebackend);
217 // Test if $olddata really contains anything and return an error message if it doesn't
219 error_box(nl2br(htmlspecialchars($abook->error
)));
221 /* Display the "new address" form */
222 echo abook_create_form($form_url, 'editaddr',
227 echo addHidden('oldnick', $olddata['nickname']).
228 addHidden('backend', $olddata['backend']).
229 addHidden('doedit', '1').
233 } elseif ($doedit == 1) {
234 /* Stage two: Write new data */
235 $newdata = $editaddr;
236 $r = $abook->modify($oldnick, $newdata, $backend);
238 /* Handle error messages */
241 plain_error_message( nl2br(htmlspecialchars($abook->error
)));
243 /* Display the "new address" form again */
244 echo abook_create_form($form_url, 'editaddr',
249 echo addHidden('oldnick', $oldnick).
250 addHidden('backend', $backend).
251 addHidden('doedit', '1').
257 * $editaddr is set, but $sel (address selection in address listing)
258 * and $doedit (address edit form) are not set.
259 * Assume that user clicked on "Edit address" without selecting any address.
261 $formerror = _("Please select address that you want to edit");
262 $showaddrlist = true;
263 } /* end of edit stage detection */
264 } /* !empty($editaddr) - Update/modify address */
265 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es)
266 or (!empty($compose_to)) && sizeof($sel) > 0 - Compose to address(es) */
267 } /* !empty($addaddr['nickname']) - Add new address */
269 // Some times we end output before forms are printed
271 echo "</body></html>\n";
277 /* =================================================================== *
278 * The following is only executed on a GET request, or on a POST when *
279 * a user is added, or when "delete" or "modify" was successful. *
280 * =================================================================== */
282 /* Display error messages */
283 if (!empty($formerror)) {
284 plain_error_message(nl2br(htmlspecialchars($formerror)));
288 /* Display the address management part */
289 $addresses = array();
290 while (list($k, $backend) = each ($abook->backends
)) {
292 $a['BackendID'] = $backend->bnum
;
293 $a['BackendSource'] = $backend->sname
;
294 $a['BackendWritable'] = $backend->writeable
;
295 $a['Addresses'] = array();
297 $alist = $abook->list_addr($backend->bnum
);
299 /* check return (array with data or boolean false) */
300 if (is_array($alist)) {
301 usort($alist,'alistcmp');
303 $a['Addresses'] = formatAddressList($alist);
305 $addresses[$backend->bnum
] = $a;
307 // list_addr() returns boolean
308 plain_error_message(nl2br(htmlspecialchars($abook->error
)));
314 //FIXME: Remove HTML from here!
315 echo addForm($form_url, 'post', 'address_book_form');
317 $oTemplate->assign('compose_new_win', $compose_new_win);
318 $oTemplate->assign('compose_height', $compose_height);
319 $oTemplate->assign('compose_width', $compose_width);
320 $oTemplate->assign('addresses', $addresses);
321 $oTemplate->assign('current_backend', $current_backend);
322 $oTemplate->assign('backends', $list_backends);
323 $oTemplate->assign('abook_has_extra_field', $abook->add_extra_field
);
325 $oTemplate->display('addressbook_list.tpl');
327 //FIXME: Remove HTML from here!
331 /* Display the "new address" form */
332 //FIXME: Remove HTML from here! (echo abook_create_form() is OK, since it is all template based output
333 echo '<a name="AddAddress"></a>' . "\n";
334 echo abook_create_form($form_url, 'addaddr',
335 _("Add to address book"),
341 /* Hook for extra address book blocks */
342 do_hook('addressbook_bottom', $null);
344 $oTemplate->display('footer.tpl');