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('sel', $sel, SQ_POST
);
38 sqgetGlobalVar('oldnick', $oldnick, SQ_POST
);
39 sqgetGlobalVar('backend', $backend, SQ_POST
);
40 sqgetGlobalVar('doedit', $doedit, SQ_POST
);
42 /* Get sorting order */
43 $abook_sort_order = get_abook_sort();
45 /* Create page header before addressbook_init in order to display error messages correctly. */
46 displayPageHeader($color);
48 /* Open addressbook with error messages on.
49 remote backends (LDAP) are enabled because they can be used. (list_addr function)
51 $abook = addressbook_init(true, false);
53 // FIXME: do we have to stop use of address book, when localbackend is not present.
54 if($abook->localbackend
== 0) {
55 plain_error_message(_("No personal address book is defined. Contact administrator."));
59 $current_backend = $abook->localbackend
;
60 if (sqgetGlobalVar('new_bnum', $new_backend, SQ_FORM
)
61 && array_key_exists($new_backend, $abook->backends
)) {
62 $current_backend = (int) $new_backend;
65 $abook_selection = ' ';
66 $list_backends = array();
67 if (count($abook->backends
) > 1) {
68 foreach($abook->get_backend_list() as $oBackend) {
69 if ($oBackend->listing
) {
70 $list_backends[$oBackend->bnum
]=$oBackend->sname
;
73 if (count($list_backends)>1) {
74 $abook_selection = addSelect('new_bnum',$list_backends,$current_backend,true)
75 .addSubmit(_("Change"),'change_abook');
83 $defselected = array();
84 $form_url = 'addressbook.php';
86 /* Handle user's actions */
87 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
89 /**************************************************
91 **************************************************/
92 if (isset($addaddr)) {
93 if (isset($backend)) {
94 $r = $abook->add($addaddr, $backend);
96 $r = $abook->add($addaddr, $abook->localbackend
);
99 /* Handle error messages */
101 /* Remove backend name from error string */
102 $errstr = $abook->error
;
103 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
105 $formerror = $errstr;
106 $showaddrlist = false;
111 /************************************************
112 * Delete address(es) *
113 ************************************************/
114 if ((!empty($deladdr)) && sizeof($sel) > 0) {
118 /* The selected addresses are identidied by "nickname_backend". *
119 * Sort the list and process one backend at the time */
124 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
125 list($snick, $sbackend) = explode('_', $sel[$i]);
127 /* When we get to a new backend, process addresses in *
129 if ($prevback != $sbackend && $prevback != -1) {
131 $r = $abook->remove($subsel, $prevback);
133 $formerror = $abook->error
;
141 /* Queue for processing */
142 array_push($subsel, $snick);
143 $prevback = $sbackend;
147 $r = $abook->remove($subsel, $prevback);
148 if (!$r) { /* Handle errors */
149 $formerror = $abook->error
;
155 $showaddrlist = true;
156 $defselected = $orig_sel;
161 /***********************************************
162 * Update/modify address *
163 ***********************************************/
164 if (!empty($editaddr)) {
165 /* Stage one: Copy data into form */
166 if (isset($sel) && sizeof($sel) > 0) {
167 if(sizeof($sel) > 1) {
168 $formerror = _("You can only edit one address at the time");
169 $showaddrlist = true;
173 list($enick, $ebackend) = explode('_', current($sel));
174 $olddata = $abook->lookup($enick, $ebackend);
175 // Test if $olddata really contains anything and return an error message if it doesn't
177 error_box(nl2br(htmlspecialchars($abook->error
)));
179 /* Display the "new address" form */
180 echo abook_create_form($form_url, 'editaddr',
185 echo addHidden('oldnick', $olddata['nickname']).
186 addHidden('backend', $olddata['backend']).
187 addHidden('doedit', '1').
191 } elseif ($doedit == 1) {
192 /* Stage two: Write new data */
193 $newdata = $editaddr;
194 $r = $abook->modify($oldnick, $newdata, $backend);
196 /* Handle error messages */
199 plain_error_message( nl2br(htmlspecialchars($abook->error
)));
201 /* Display the "new address" form again */
202 echo abook_create_form($form_url, 'editaddr',
207 echo addHidden('oldnick', $oldnick).
208 addHidden('backend', $backend).
209 addHidden('doedit', '1').
215 * $editaddr is set, but $sel (address selection in address listing)
216 * and $doedit (address edit form) are not set.
217 * Assume that user clicked on "Edit address" without selecting any address.
219 $formerror = _("Please select address that you want to edit");
220 $showaddrlist = true;
221 } /* end of edit stage detection */
222 } /* !empty($editaddr) - Update/modify address */
223 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
224 } /* !empty($addaddr['nickname']) - Add new address */
226 // Some times we end output before forms are printed
228 echo "</body></html>\n";
234 /* =================================================================== *
235 * The following is only executed on a GET request, or on a POST when *
236 * a user is added, or when "delete" or "modify" was successful. *
237 * =================================================================== */
239 /* Display error messages */
240 if (!empty($formerror)) {
241 plain_error_message(nl2br(htmlspecialchars($formerror)));
245 /* Display the address management part */
246 $addresses = array();
247 while (list($k, $backend) = each ($abook->backends
)) {
249 $a['BackendID'] = $backend->bnum
;
250 $a['BackendSource'] = $backend->sname
;
251 $a['BackendWritable'] = $backend->writeable
;
252 $a['Addresses'] = array();
254 $alist = $abook->list_addr($backend->bnum
);
256 /* check return (array with data or boolean false) */
257 if (is_array($alist)) {
258 usort($alist,'alistcmp');
260 $a['Addresses'] = formatAddressList($alist);
262 $addresses[$backend->bnum
] = $a;
264 // list_addr() returns boolean
265 plain_error_message(nl2br(htmlspecialchars($abook->error
)));
271 //FIXME: Remove HTML from here!
272 echo addForm($form_url, 'post', 'address_book_form');
274 $oTemplate->assign('addresses', $addresses);
275 $oTemplate->assign('current_backend', $current_backend);
276 $oTemplate->assign('backends', $list_backends);
277 $oTemplate->assign('abook_has_extra_field', $abook->add_extra_field
);
279 $oTemplate->display('addressbook_list.tpl');
281 //FIXME: Remove HTML from here!
285 /* Display the "new address" form */
286 //FIXME: Remove HTML from here! (echo abook_create_form() is OK, since it is all template based output
287 echo '<a name="AddAddress"></a>' . "\n";
288 echo abook_create_form($form_url, 'addaddr',
289 _("Add to address book"),
295 /* Hook for extra address book blocks */
296 do_hook('addressbook_bottom', $null);
298 $oTemplate->display('footer.tpl');