Make sort links and add form return to the same backend that is currently being viewed
[squirrelmail.git] / src / addressbook.php
CommitLineData
abdfb4d0 1<?php
134e4174 2
35586184 3/**
4 * addressbook.php
5 *
35586184 6 * Manage personal address book.
7 *
4b5049de 8 * @copyright &copy; 1999-2007 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
ebd2391c 15/** This is the addressbook page */
16define('PAGE_NAME', 'addressbook');
17
30967a1e 18/**
202bcbcc 19 * Include the SquirrelMail initialization file.
30967a1e 20 */
202bcbcc 21include('../include/init.php');
86725763 22
8f6f9ba5 23/** SquirrelMail required files. */
df4162cb 24/* address book functions */
202bcbcc 25require_once(SM_PATH . 'functions/addressbook.php');
3f92c0c7 26include_once(SM_PATH . 'templates/util_addressbook.php');
27
df4162cb 28/* form functions */
202bcbcc 29require_once(SM_PATH . 'functions/forms.php');
ffd8224c 30
8f6f9ba5 31/** lets get the global vars we may need */
0b97a708 32
33/* From the address form */
a71b394e 34sqgetGlobalVar('addaddr', $addaddr, SQ_POST);
35sqgetGlobalVar('editaddr', $editaddr, SQ_POST);
36sqgetGlobalVar('deladdr', $deladdr, SQ_POST);
37sqgetGlobalVar('sel', $sel, SQ_POST);
38sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
39sqgetGlobalVar('backend', $backend, SQ_POST);
40sqgetGlobalVar('doedit', $doedit, SQ_POST);
0b97a708 41
08e71631 42/* Get sorting order */
43$abook_sort_order = get_abook_sort();
44
703fa6b5 45/* Create page header before addressbook_init in order to display error messages correctly. */
876fdb60 46displayPageHeader($color);
703fa6b5 47
de00443c 48/* Open addressbook with error messages on.
49 remote backends (LDAP) are enabled because they can be used. (list_addr function)
50*/
51$abook = addressbook_init(true, false);
ced653f3 52
53// FIXME: do we have to stop use of address book, when localbackend is not present.
daba719e 54if($abook->localbackend == 0) {
a71b394e 55 plain_error_message(_("No personal address book is defined. Contact administrator."));
daba719e 56 exit();
57}
ffd8224c 58
a71b394e 59$current_backend = $abook->localbackend;
caa596b2 60if (sqgetGlobalVar('new_bnum', $new_backend, SQ_FORM)
61 && array_key_exists($new_backend, $abook->backends)) {
a71b394e 62 $current_backend = (int) $new_backend;
63}
64
65$abook_selection = '&nbsp;';
66$list_backends = array();
67if (count($abook->backends) > 1) {
68 foreach($abook->get_backend_list() as $oBackend) {
69 if ($oBackend->listing) {
70 $list_backends[$oBackend->bnum]=$oBackend->sname;
71 }
72 }
73 if (count($list_backends)>1) {
74 $abook_selection = addSelect('new_bnum',$list_backends,$current_backend,true)
75 .addSubmit(_("Change"),'change_abook');
76 }
77}
78
daba719e 79$defdata = array();
80$formerror = '';
81$abortform = false;
82$showaddrlist = true;
83$defselected = array();
07dcee9f 84$form_url = 'addressbook.php';
daba719e 85
f6c945b9 86/* Handle user's actions */
1e12d1ff 87if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
daba719e 88
f6c945b9 89 /**************************************************
90 * Add new address *
91 **************************************************/
39b26252 92 if (isset($addaddr)) {
a123157f 93 if (isset($backend)) {
94 $r = $abook->add($addaddr, $backend);
95 } else {
96 $r = $abook->add($addaddr, $abook->localbackend);
c6554ec0 97 }
ffd8224c 98
f6c945b9 99 /* Handle error messages */
100 if (!$r) {
101 /* Remove backend name from error string */
ffd8224c 102 $errstr = $abook->error;
103 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
104
105 $formerror = $errstr;
106 $showaddrlist = false;
107 $defdata = $addaddr;
108 }
daba719e 109 } else {
ffd8224c 110
f6c945b9 111 /************************************************
112 * Delete address(es) *
113 ************************************************/
114 if ((!empty($deladdr)) && sizeof($sel) > 0) {
daba719e 115 $orig_sel = $sel;
116 sort($sel);
117
3c656c5f 118 /* The selected addresses are identidied by "nickname_backend". *
f6c945b9 119 * Sort the list and process one backend at the time */
daba719e 120 $prevback = -1;
121 $subsel = array();
122 $delfailed = false;
123
f6c945b9 124 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
3c656c5f 125 list($snick, $sbackend) = explode('_', $sel[$i]);
daba719e 126
f6c945b9 127 /* When we get to a new backend, process addresses in *
128 * previous one. */
129 if ($prevback != $sbackend && $prevback != -1) {
daba719e 130
131 $r = $abook->remove($subsel, $prevback);
f6c945b9 132 if (!$r) {
daba719e 133 $formerror = $abook->error;
134 $i = sizeof($sel);
135 $delfailed = true;
136 break;
137 }
138 $subsel = array();
139 }
140
f6c945b9 141 /* Queue for processing */
daba719e 142 array_push($subsel, $snick);
143 $prevback = $sbackend;
ffd8224c 144 }
ffd8224c 145
f6c945b9 146 if (!$delfailed) {
daba719e 147 $r = $abook->remove($subsel, $prevback);
f6c945b9 148 if (!$r) { /* Handle errors */
daba719e 149 $formerror = $abook->error;
150 $delfailed = true;
151 }
ffd8224c 152 }
ffd8224c 153
f6c945b9 154 if ($delfailed) {
daba719e 155 $showaddrlist = true;
156 $defselected = $orig_sel;
ffd8224c 157 }
ffd8224c 158
daba719e 159 } else {
160
f6c945b9 161 /***********************************************
162 * Update/modify address *
163 ***********************************************/
164 if (!empty($editaddr)) {
f6c945b9 165 /* Stage one: Copy data into form */
daba719e 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;
170 $defselected = $sel;
171 } else {
172 $abortform = true;
3c656c5f 173 list($enick, $ebackend) = explode('_', current($sel));
daba719e 174 $olddata = $abook->lookup($enick, $ebackend);
5b61a40d 175 // Test if $olddata really contains anything and return an error message if it doesn't
176 if (!$olddata) {
177 error_box(nl2br(htmlspecialchars($abook->error)));
178 } else {
179 /* Display the "new address" form */
caa596b2 180 echo abook_create_form($form_url, 'editaddr',
181 _("Update address"),
182 _("Update address"),
183 $current_backend,
184 $olddata);
5b61a40d 185 echo addHidden('oldnick', $olddata['nickname']).
186 addHidden('backend', $olddata['backend']).
187 addHidden('doedit', '1').
188 '</form>';
189 }
daba719e 190 }
3e87f870 191 } elseif ($doedit == 1) {
f6c945b9 192 /* Stage two: Write new data */
3e87f870 193 $newdata = $editaddr;
194 $r = $abook->modify($oldnick, $newdata, $backend);
daba719e 195
3e87f870 196 /* Handle error messages */
197 if (!$r) {
198 /* Display error */
3c656c5f 199 plain_error_message( nl2br(htmlspecialchars($abook->error)));
3e87f870 200
201 /* Display the "new address" form again */
caa596b2 202 echo abook_create_form($form_url, 'editaddr',
203 _("Update address"),
204 _("Update address"),
205 $current_backend,
206 $newdata);
3e87f870 207 echo addHidden('oldnick', $oldnick).
208 addHidden('backend', $backend).
209 addHidden('doedit', '1').
210 "\n" . '</form>';
daba719e 211 $abortform = true;
212 }
3e87f870 213 } else {
214 /**
f8a1ed5a 215 * $editaddr is set, but $sel (address selection in address listing)
216 * and $doedit (address edit form) are not set.
3e87f870 217 * Assume that user clicked on "Edit address" without selecting any address.
218 */
219 $formerror = _("Please select address that you want to edit");
220 $showaddrlist = true;
221 } /* end of edit stage detection */
daba719e 222 } /* !empty($editaddr) - Update/modify address */
223 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
224 } /* !empty($addaddr['nickname']) - Add new address */
225
226 // Some times we end output before forms are printed
227 if($abortform) {
056a005c 228 echo "</body></html>\n";
229 exit();
ffd8224c 230 }
daba719e 231}
ffd8224c 232
233
f6c945b9 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 * =================================================================== */
ffd8224c 238
f6c945b9 239/* Display error messages */
240if (!empty($formerror)) {
a71b394e 241 plain_error_message(nl2br(htmlspecialchars($formerror)));
daba719e 242}
ffd8224c 243
244
f6c945b9 245/* Display the address management part */
a71b394e 246$addresses = array();
247while (list($k, $backend) = each ($abook->backends)) {
248 $a = array();
249 $a['BackendID'] = $backend->bnum;
250 $a['BackendSource'] = $backend->sname;
251 $a['BackendWritable'] = $backend->writeable;
252 $a['Addresses'] = array();
253
254 $alist = $abook->list_addr($backend->bnum);
5b1c13d3 255
256 /* check return (array with data or boolean false) */
257 if (is_array($alist)) {
258 usort($alist,'alistcmp');
259
260 $a['Addresses'] = formatAddressList($alist);
a71b394e 261
5b1c13d3 262 $addresses[$backend->bnum] = $a;
263 } else {
264 // list_addr() returns boolean
265 plain_error_message(nl2br(htmlspecialchars($abook->error)));
266 }
a71b394e 267}
daba719e 268
269
a71b394e 270if ($showaddrlist) {
caa596b2 271//FIXME: Remove HTML from here!
c7009c16 272 echo addForm($form_url, 'post', 'address_book_form');
a71b394e 273
274 $oTemplate->assign('addresses', $addresses);
275 $oTemplate->assign('current_backend', $current_backend);
276 $oTemplate->assign('backends', $list_backends);
a140c3b1 277 $oTemplate->assign('abook_has_extra_field', $abook->add_extra_field);
a71b394e 278
279 $oTemplate->display('addressbook_list.tpl');
280
6e515418 281//FIXME: Remove HTML from here!
a71b394e 282 echo "</form>\n";
283}
284
f6c945b9 285/* Display the "new address" form */
caa596b2 286//FIXME: Remove HTML from here! (echo abook_create_form() is OK, since it is all template based output
c1ac62d4 287echo '<a name="AddAddress"></a>' . "\n";
caa596b2 288echo abook_create_form($form_url, 'addaddr',
289 _("Add to address book"),
290 _("Add address"),
291 $current_backend,
292 $defdata);
fc93f97c 293echo "</form>\n";
daba719e 294
df4162cb 295/* Hook for extra address book blocks */
6e515418 296do_hook('addressbook_bottom', $null);
a71b394e 297
5c4ff7bf 298$oTemplate->display('footer.tpl');